From 85b5a7487d5b44b94e6d2fc74c997ec69e352b1a Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 3 Feb 2012 21:10:36 +0000 Subject: Upgrade the input box and label widgets to use UTF-8 strings. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2491 --- textscreen/txt_inputbox.c | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) (limited to 'textscreen/txt_inputbox.c') diff --git a/textscreen/txt_inputbox.c b/textscreen/txt_inputbox.c index 2ba08ac6..32704284 100644 --- a/textscreen/txt_inputbox.c +++ b/textscreen/txt_inputbox.c @@ -30,6 +30,7 @@ #include "txt_gui.h" #include "txt_io.h" #include "txt_main.h" +#include "txt_utf8.h" #include "txt_window.h" extern txt_widget_class_t txt_inputbox_class; @@ -140,9 +141,9 @@ static void TXT_InputBoxDrawer(TXT_UNCAST_ARG(inputbox)) SetBufferFromValue(inputbox); } - TXT_DrawString(inputbox->buffer); + TXT_DrawUTF8String(inputbox->buffer); - chars = strlen(inputbox->buffer); + chars = TXT_UTF8_Strlen(inputbox->buffer); if (chars < w && inputbox->editing && focused) { @@ -166,26 +167,36 @@ static void TXT_InputBoxDestructor(TXT_UNCAST_ARG(inputbox)) static void Backspace(txt_inputbox_t *inputbox) { - if (strlen(inputbox->buffer) > 0) + unsigned int len; + char *p; + + len = TXT_UTF8_Strlen(inputbox->buffer); + + if (len > 0) { - inputbox->buffer[strlen(inputbox->buffer) - 1] = '\0'; + p = TXT_UTF8_SkipChars(inputbox->buffer, len - 1); + *p = '\0'; } } static void AddCharacter(txt_inputbox_t *inputbox, int key) { - if (strlen(inputbox->buffer) < inputbox->size) + char *end, *p; + + if (TXT_UTF8_Strlen(inputbox->buffer) < inputbox->size) { // Add character to the buffer - inputbox->buffer[strlen(inputbox->buffer) + 1] = '\0'; - inputbox->buffer[strlen(inputbox->buffer)] = key; + end = inputbox->buffer + strlen(inputbox->buffer); + p = TXT_EncodeUTF8(end, key); + *p = '\0'; } } static int TXT_InputBoxKeyPress(TXT_UNCAST_ARG(inputbox), int key) { TXT_CAST_ARG(txt_inputbox_t, inputbox); + unsigned int c; if (!inputbox->editing) { @@ -208,16 +219,18 @@ static int TXT_InputBoxKeyPress(TXT_UNCAST_ARG(inputbox), int key) inputbox->editing = 0; } - if (isprint(key)) + if (key == KEY_BACKSPACE) { - // Add character to the buffer - - AddCharacter(inputbox, key); + Backspace(inputbox); } - if (key == KEY_BACKSPACE) + c = TXT_KEY_TO_UNICODE(key); + + if (c >= 128 || isprint(c)) { - Backspace(inputbox); + // Add character to the buffer + + AddCharacter(inputbox, c); } return 1; @@ -286,7 +299,10 @@ txt_inputbox_t *TXT_NewInputBox(char **value, int size) TXT_InitWidget(inputbox, &txt_inputbox_class); inputbox->value = value; inputbox->size = size; - inputbox->buffer = malloc(size + 1); + // 'size' is the maximum number of characters that can be entered, + // but for a UTF-8 string, each character can take up to four + // characters. + inputbox->buffer = malloc(size * 4 + 1); inputbox->editing = 0; return inputbox; -- cgit v1.2.3 From c6b8f1163708db85251daac24b2ffa6bea24a72a Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 3 Feb 2012 22:05:49 +0000 Subject: Fix crash when typing lots of Unicode characters into a number input box. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2494 --- textscreen/txt_inputbox.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'textscreen/txt_inputbox.c') diff --git a/textscreen/txt_inputbox.c b/textscreen/txt_inputbox.c index 32704284..d5ad440a 100644 --- a/textscreen/txt_inputbox.c +++ b/textscreen/txt_inputbox.c @@ -290,13 +290,14 @@ txt_widget_class_t txt_int_inputbox_class = TXT_InputBoxFocused, }; -txt_inputbox_t *TXT_NewInputBox(char **value, int size) +static txt_inputbox_t *NewInputBox(txt_widget_class_t *widget_class, + void *value, int size) { txt_inputbox_t *inputbox; inputbox = malloc(sizeof(txt_inputbox_t)); - TXT_InitWidget(inputbox, &txt_inputbox_class); + TXT_InitWidget(inputbox, widget_class); inputbox->value = value; inputbox->size = size; // 'size' is the maximum number of characters that can be entered, @@ -308,17 +309,13 @@ txt_inputbox_t *TXT_NewInputBox(char **value, int size) return inputbox; } -txt_inputbox_t *TXT_NewIntInputBox(int *value, int size) +txt_inputbox_t *TXT_NewInputBox(char **value, int size) { - txt_inputbox_t *inputbox; - - inputbox = malloc(sizeof(txt_inputbox_t)); + return NewInputBox(&txt_inputbox_class, value, size); +} - TXT_InitWidget(inputbox, &txt_int_inputbox_class); - inputbox->value = value; - inputbox->size = size; - inputbox->buffer = malloc(15); - inputbox->editing = 0; - return inputbox; +txt_inputbox_t *TXT_NewIntInputBox(int *value, int size) +{ + return NewInputBox(&txt_int_inputbox_class, value, size); } -- cgit v1.2.3 From 003c82ce37610a66bc61b9380fda6fd74223473b Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 4 Mar 2012 12:06:29 +0000 Subject: Remove some calls to TXT_FGColor by using the new TXT_SaveColors system instead. Remove the unused "embedded color code" system from TXT_Puts. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2508 --- textscreen/txt_inputbox.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'textscreen/txt_inputbox.c') diff --git a/textscreen/txt_inputbox.c b/textscreen/txt_inputbox.c index d5ad440a..0f2986cc 100644 --- a/textscreen/txt_inputbox.c +++ b/textscreen/txt_inputbox.c @@ -132,8 +132,6 @@ static void TXT_InputBoxDrawer(TXT_UNCAST_ARG(inputbox)) TXT_SetWidgetBG(inputbox); } - TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); - if (!inputbox->editing) { // If not editing, use the current value from inputbox->value. -- cgit v1.2.3