diff options
author | Simon Howard | 2006-09-20 18:05:10 +0000 |
---|---|---|
committer | Simon Howard | 2006-09-20 18:05:10 +0000 |
commit | 7768cfde84316dc1181a30eabce731d6089e3cfd (patch) | |
tree | 8821479d08c62ee153509d7f03390ec9106a3665 | |
parent | 16c643057cef3a49a6713090ffa6e5f8aba2ad0a (diff) | |
download | chocolate-doom-7768cfde84316dc1181a30eabce731d6089e3cfd.tar.gz chocolate-doom-7768cfde84316dc1181a30eabce731d6089e3cfd.tar.bz2 chocolate-doom-7768cfde84316dc1181a30eabce731d6089e3cfd.zip |
Add spin control widget.
Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 635
-rw-r--r-- | textscreen/Makefile.am | 1 | ||||
-rw-r--r-- | textscreen/examples/guitest.c | 2 | ||||
-rw-r--r-- | textscreen/textscreen.h | 1 | ||||
-rw-r--r-- | textscreen/txt_spinctrl.c | 183 | ||||
-rw-r--r-- | textscreen/txt_spinctrl.h | 40 |
5 files changed, 227 insertions, 0 deletions
diff --git a/textscreen/Makefile.am b/textscreen/Makefile.am index 299e3b9d..71989c3b 100644 --- a/textscreen/Makefile.am +++ b/textscreen/Makefile.am @@ -20,6 +20,7 @@ libtextscreen_a_SOURCES = \ txt_label.c txt_label.h \ txt_radiobutton.c txt_radiobutton.h \ txt_separator.c txt_separator.h \ + txt_spinctrl.c txt_spinctrl.h \ txt_strut.c txt_strut.h \ txt_table.c txt_table.h \ txt_widget.c txt_widget.h \ diff --git a/textscreen/examples/guitest.c b/textscreen/examples/guitest.c index b5bd1ea5..130c858f 100644 --- a/textscreen/examples/guitest.c +++ b/textscreen/examples/guitest.c @@ -157,6 +157,8 @@ void Window2(void) TXT_AddWidget(table, TXT_NewInputBox(&textbox_value, 30)); TXT_AddWidget(table, TXT_NewLabel("Int: ")); TXT_AddWidget(table, TXT_NewIntInputBox(&numbox_value, 10)); + TXT_AddWidget(table, TXT_NewLabel("Spin control:")); + TXT_AddWidget(table, TXT_NewSpinControl(&numbox_value, 0, 15)); } int main(int argc, char *argv[]) diff --git a/textscreen/textscreen.h b/textscreen/textscreen.h index 12cfc047..dcefa409 100644 --- a/textscreen/textscreen.h +++ b/textscreen/textscreen.h @@ -33,6 +33,7 @@ #include "txt_label.h" #include "txt_radiobutton.h" #include "txt_separator.h" +#include "txt_spinctrl.h" #include "txt_strut.h" #include "txt_table.h" #include "txt_widget.h" diff --git a/textscreen/txt_spinctrl.c b/textscreen/txt_spinctrl.c new file mode 100644 index 00000000..44c59815 --- /dev/null +++ b/textscreen/txt_spinctrl.c @@ -0,0 +1,183 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2006 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// + +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> + +#include "doomkeys.h" + +#include "txt_spinctrl.h" +#include "txt_gui.h" +#include "txt_io.h" +#include "txt_main.h" +#include "txt_window.h" + +// Number of characters needed to represent a character + +static int IntWidth(int val) +{ + if (val < 0) + { + return ((int) log(-val)) + 2; + } + else + { + return ((int) log(val)) + 1; + } +} + +static void TXT_SpinControlSizeCalc(TXT_UNCAST_ARG(spincontrol)) +{ + TXT_CAST_ARG(txt_spincontrol_t, spincontrol); + int minw, maxw; + int w; + + minw = IntWidth(spincontrol->min); + maxw = IntWidth(spincontrol->max); + + if (minw > maxw) + { + w = minw; + } + else + { + w = maxw; + } + + spincontrol->widget.w = w + 4; + spincontrol->widget.h = 1; +} + +static void TXT_SpinControlDrawer(TXT_UNCAST_ARG(spincontrol), int selected) +{ + TXT_CAST_ARG(txt_spincontrol_t, spincontrol); + char buf[20]; + int i; + + TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); + TXT_BGColor(TXT_COLOR_BLUE, 0); + + TXT_DrawString("< "); + + // Choose background color + + if (selected) + { + TXT_BGColor(TXT_COLOR_GREY, 0); + } + else + { + TXT_BGColor(TXT_COLOR_BLUE, 0); + } + + sprintf(buf, "%i", *spincontrol->value); + + for (i=strlen(buf); i<spincontrol->widget.w - 4; ++i) + { + TXT_DrawString(" "); + } + + TXT_DrawString(buf); + + TXT_BGColor(TXT_COLOR_BLUE, 0); + TXT_DrawString(" >"); +} + +static void TXT_SpinControlDestructor(TXT_UNCAST_ARG(spincontrol)) +{ +} + +static int TXT_SpinControlKeyPress(TXT_UNCAST_ARG(spincontrol), int key) +{ + TXT_CAST_ARG(txt_spincontrol_t, spincontrol); + + if (key == KEY_LEFTARROW) + { + --*spincontrol->value; + + if (*spincontrol->value < spincontrol->min) + { + *spincontrol->value = spincontrol->min; + } + + return 1; + } + + if (key == KEY_RIGHTARROW) + { + ++*spincontrol->value; + + if (*spincontrol->value > spincontrol->max) + { + *spincontrol->value = spincontrol->max; + } + + return 1; + } + + return 0; +} + +static void TXT_SpinControlMousePress(TXT_UNCAST_ARG(spincontrol), + int x, int y, int b) +{ + TXT_CAST_ARG(txt_spincontrol_t, spincontrol); + int rel_x; + + rel_x = x - spincontrol->widget.x; + + if (rel_x < 2) + { + TXT_SpinControlKeyPress(spincontrol, KEY_LEFTARROW); + } + else if (rel_x >= spincontrol->widget.w - 2) + { + TXT_SpinControlKeyPress(spincontrol, KEY_RIGHTARROW); + } +} + +txt_widget_class_t txt_spincontrol_class = +{ + TXT_SpinControlSizeCalc, + TXT_SpinControlDrawer, + TXT_SpinControlKeyPress, + TXT_SpinControlDestructor, + TXT_SpinControlMousePress, +}; + +txt_spincontrol_t *TXT_NewSpinControl(int *value, int min, int max) +{ + txt_spincontrol_t *spincontrol; + + spincontrol = malloc(sizeof(txt_spincontrol_t)); + + TXT_InitWidget(spincontrol, &txt_spincontrol_class); + spincontrol->value = value; + spincontrol->min = min; + spincontrol->max = max; + + return spincontrol; +} + + diff --git a/textscreen/txt_spinctrl.h b/textscreen/txt_spinctrl.h new file mode 100644 index 00000000..b0503d40 --- /dev/null +++ b/textscreen/txt_spinctrl.h @@ -0,0 +1,40 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2006 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// + +#ifndef TXT_SPINCONTROL_H +#define TXT_SPINCONTROL_H + +typedef struct txt_spincontrol_s txt_spincontrol_t; + +#include "txt_widget.h" + +struct txt_spincontrol_s +{ + txt_widget_t widget; + int min, max; + int *value; +}; + +txt_spincontrol_t *TXT_NewSpinControl(int *value, int min, int max); + +#endif /* #ifndef TXT_SPINCONTROL_H */ + + |