diff options
author | Simon Howard | 2006-05-23 22:52:01 +0000 |
---|---|---|
committer | Simon Howard | 2006-05-23 22:52:01 +0000 |
commit | 6ea4e1e25cfe2f55e02a371654c1dd7814276398 (patch) | |
tree | 7d0b6b21c0d489b37f162d69a100bb01e1c03154 | |
parent | 3fbef9d2788184c491c120d00e6d5bccecc4310c (diff) | |
download | chocolate-doom-6ea4e1e25cfe2f55e02a371654c1dd7814276398.tar.gz chocolate-doom-6ea4e1e25cfe2f55e02a371654c1dd7814276398.tar.bz2 chocolate-doom-6ea4e1e25cfe2f55e02a371654c1dd7814276398.zip |
Add input box widget, and include in guitest.
Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 520
-rw-r--r-- | textscreen/Makefile.am | 1 | ||||
-rw-r--r-- | textscreen/guitest.c | 12 | ||||
-rw-r--r-- | textscreen/txt_inputbox.c | 254 | ||||
-rw-r--r-- | textscreen/txt_inputbox.h | 46 |
4 files changed, 313 insertions, 0 deletions
diff --git a/textscreen/Makefile.am b/textscreen/Makefile.am index 90b7e441..49a0374d 100644 --- a/textscreen/Makefile.am +++ b/textscreen/Makefile.am @@ -8,6 +8,7 @@ libtextscreen_a_SOURCES = \ txt_checkbox.c txt_checkbox.h \ txt_desktop.c txt_desktop.h \ txt_gui.c txt_gui.h \ + txt_inputbox.c txt_inputbox.h \ txt_io.c txt_io.h \ txt_main.c txt_main.h \ txt_button.c txt_button.h \ diff --git a/textscreen/guitest.c b/textscreen/guitest.c index cf1079b3..23df5282 100644 --- a/textscreen/guitest.c +++ b/textscreen/guitest.c @@ -9,6 +9,7 @@ #include "txt_checkbox.h" #include "txt_button.h" #include "txt_desktop.h" +#include "txt_inputbox.h" #include "txt_label.h" #include "txt_radiobutton.h" #include "txt_separator.h" @@ -22,6 +23,8 @@ enum RADIO_VALUE_SNAKE, }; char *radio_values[] = { "Badger", "Mushroom", "Snake" }; +char *textbox_value = NULL; +int numbox_value = 0; int radiobutton_value; txt_label_t *value_label; txt_window_t *firstwin; @@ -141,6 +144,7 @@ void SetupWindow(void) void Window2(void) { txt_window_t *window; + txt_table_t *table; int i; window = TXT_NewWindow("Another test"); @@ -153,6 +157,14 @@ void Window2(void) { TXT_AddWidget(window, TXT_NewButton("hello there blah blah blah blah")); } + + TXT_AddWidget(window, TXT_NewSeparator("Input boxes")); + table = TXT_NewTable(2); + TXT_AddWidget(window, table); + TXT_AddWidget(table, TXT_NewLabel(" String: ")); + TXT_AddWidget(table, TXT_NewInputBox(&textbox_value, 30)); + TXT_AddWidget(table, TXT_NewLabel(" Int: ")); + TXT_AddWidget(table, TXT_NewIntInputBox(&numbox_value, 10)); } void DrawASCIIChart() diff --git a/textscreen/txt_inputbox.c b/textscreen/txt_inputbox.c new file mode 100644 index 00000000..79c9e90e --- /dev/null +++ b/textscreen/txt_inputbox.c @@ -0,0 +1,254 @@ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "doomkeys.h" + +#include "txt_inputbox.h" +#include "txt_io.h" +#include "txt_main.h" +#include "txt_window.h" + +static void SetBufferFromValue(txt_inputbox_t *inputbox); + +static void TXT_InputBoxSizeCalc(TXT_UNCAST_ARG(inputbox), int *w, int *h) +{ + TXT_CAST_ARG(txt_inputbox_t, inputbox); + + // Enough space for the box + cursor + + *w = inputbox->size + 1; + *h = 1; +} + +static void TXT_InputBoxDrawer(TXT_UNCAST_ARG(inputbox), int w, int selected) +{ + TXT_CAST_ARG(txt_inputbox_t, inputbox); + int i; + int chars; + + // Select the background colour based on whether we are currently + // editing, and if not, whether the widget is selected. + + if (inputbox->editing && selected) + { + TXT_BGColor(TXT_COLOR_BLACK, 0); + } + else if (selected) + { + TXT_BGColor(TXT_COLOR_GREY, 0); + } + else + { + // Not even selected + + TXT_BGColor(TXT_COLOR_BLUE, 0); + } + + TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); + + if (!inputbox->editing) + { + // If not editing, use the current value from inputbox->value. + + SetBufferFromValue(inputbox); + } + + TXT_DrawString(inputbox->buffer); + + chars = strlen(inputbox->buffer); + + if (chars < w && inputbox->editing && selected) + { + TXT_BGColor(TXT_COLOR_BLACK, 1); + TXT_DrawString("_"); + ++chars; + } + + for (i=chars; i < w; ++i) + { + TXT_DrawString(" "); + } +} + +static void TXT_InputBoxDestructor(TXT_UNCAST_ARG(inputbox)) +{ + TXT_CAST_ARG(txt_inputbox_t, inputbox); + + free(inputbox->buffer); +} + +static void Backspace(txt_inputbox_t *inputbox) +{ + if (strlen(inputbox->buffer) > 0) + { + inputbox->buffer[strlen(inputbox->buffer) - 1] = '\0'; + } +} + +static void AddCharacter(txt_inputbox_t *inputbox, int key) +{ + if (strlen(inputbox->buffer) < inputbox->size) + { + // Add character to the buffer + + inputbox->buffer[strlen(inputbox->buffer) + 1] = '\0'; + inputbox->buffer[strlen(inputbox->buffer)] = key; + } +} + +static int TXT_InputBoxKeyPress(TXT_UNCAST_ARG(inputbox), int key) +{ + TXT_CAST_ARG(txt_inputbox_t, inputbox); + + if (!inputbox->editing) + { + if (key == KEY_ENTER) + { + SetBufferFromValue(inputbox); + inputbox->editing = 1; + return 1; + } + + return 0; + } + + if (key == KEY_ENTER) + { + free(*((char **)inputbox->value)); + *((char **) inputbox->value) = strdup(inputbox->buffer); + + inputbox->editing = 0; + } + + if (key == KEY_ESCAPE) + { + inputbox->editing = 0; + } + + if (isprint(key)) + { + // Add character to the buffer + + AddCharacter(inputbox, key); + } + + if (key == KEY_BACKSPACE) + { + Backspace(inputbox); + } + + return 1; +} + +static int TXT_IntInputBoxKeyPress(TXT_UNCAST_ARG(inputbox), int key) +{ + TXT_CAST_ARG(txt_inputbox_t, inputbox); + + if (!inputbox->editing) + { + if (key == KEY_ENTER) + { + SetBufferFromValue(inputbox); + inputbox->editing = 1; + return 1; + } + + return 0; + } + + if (key == KEY_ENTER) + { + *((int *) inputbox->value) = atoi(inputbox->buffer); + + inputbox->editing = 0; + } + + if (key == KEY_ESCAPE) + { + inputbox->editing = 0; + } + + if (isprint(key)) + { + // Add character to the buffer + + AddCharacter(inputbox, key); + } + + if (key == KEY_BACKSPACE) + { + Backspace(inputbox); + } + + return 1; +} + +txt_widget_class_t txt_inputbox_class = +{ + TXT_InputBoxSizeCalc, + TXT_InputBoxDrawer, + TXT_InputBoxKeyPress, + TXT_InputBoxDestructor, +}; + +txt_widget_class_t txt_int_inputbox_class = +{ + TXT_InputBoxSizeCalc, + TXT_InputBoxDrawer, + TXT_IntInputBoxKeyPress, + TXT_InputBoxDestructor, +}; + +static void SetBufferFromValue(txt_inputbox_t *inputbox) +{ + if (inputbox->widget.widget_class == &txt_inputbox_class) + { + char **value = (char **) inputbox->value; + + if (*value != NULL) + { + strncpy(inputbox->buffer, *value, inputbox->size); + inputbox->buffer[inputbox->size] = '\0'; + } + else + { + strcpy(inputbox->buffer, ""); + } + } + else if (inputbox->widget.widget_class == &txt_int_inputbox_class) + { + int *value = (int *) inputbox->value; + sprintf(inputbox->buffer, "%i", *value); + } +} + +txt_inputbox_t *TXT_NewInputBox(char **value, int size) +{ + txt_inputbox_t *inputbox; + + inputbox = malloc(sizeof(txt_inputbox_t)); + + TXT_InitWidget(inputbox, &txt_inputbox_class); + inputbox->value = value; + inputbox->size = size; + inputbox->buffer = malloc(size + 1); + + return inputbox; +} + +txt_inputbox_t *TXT_NewIntInputBox(int *value, int size) +{ + txt_inputbox_t *inputbox; + + inputbox = malloc(sizeof(txt_inputbox_t)); + + TXT_InitWidget(inputbox, &txt_int_inputbox_class); + inputbox->value = value; + inputbox->size = size; + inputbox->buffer = malloc(15); + inputbox->editing = 0; + return inputbox; +} + diff --git a/textscreen/txt_inputbox.h b/textscreen/txt_inputbox.h new file mode 100644 index 00000000..c553df44 --- /dev/null +++ b/textscreen/txt_inputbox.h @@ -0,0 +1,46 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// $Id$ +// +// Copyright(C) 1993-1996 Id Software, Inc. +// 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_INPUTBOX_H +#define TXT_INPUTBOX_H + +typedef struct txt_inputbox_s txt_inputbox_t; + +#include "txt_widget.h" + +struct txt_inputbox_s +{ + txt_widget_t widget; + char *buffer; + int size; + int editing; + void *value; +}; + +txt_inputbox_t *TXT_NewInputBox(char **value, int size); +txt_inputbox_t *TXT_NewIntInputBox(int *value, int size); + +#endif /* #ifndef TXT_INPUTBOX_H */ + + |