diff options
author | Simon Howard | 2012-02-03 21:10:36 +0000 |
---|---|---|
committer | Simon Howard | 2012-02-03 21:10:36 +0000 |
commit | 85b5a7487d5b44b94e6d2fc74c997ec69e352b1a (patch) | |
tree | 2d5c58da8f23deb97b08f1c8e3ed8017197aa7cd | |
parent | d745a6409c84a9ffb85e5d2029132642435c5879 (diff) | |
download | chocolate-doom-85b5a7487d5b44b94e6d2fc74c997ec69e352b1a.tar.gz chocolate-doom-85b5a7487d5b44b94e6d2fc74c997ec69e352b1a.tar.bz2 chocolate-doom-85b5a7487d5b44b94e6d2fc74c997ec69e352b1a.zip |
Upgrade the input box and label widgets to use UTF-8 strings.
Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 2491
-rw-r--r-- | textscreen/examples/guitest.c | 4 | ||||
-rw-r--r-- | textscreen/txt_inputbox.c | 44 | ||||
-rw-r--r-- | textscreen/txt_label.c | 19 |
3 files changed, 46 insertions, 21 deletions
diff --git a/textscreen/examples/guitest.c b/textscreen/examples/guitest.c index df79be2d..a5aad93f 100644 --- a/textscreen/examples/guitest.c +++ b/textscreen/examples/guitest.c @@ -179,6 +179,10 @@ void Window2(void) NULL); TXT_AddWidget(unselectable_table, TXT_NewLabel("* Unselectable table *")); + TXT_AddWidget(unselectable_table, TXT_NewLabel( + "This is a UTF-8 string:\n" + "\xc3\x80 bient\xc3\xb4t na\xc3\xaet " + "\xc3\xa9v\xc3\xaaque \xc3\xa0 l'\xc5\x93uvre p\xc3\xa8re.")); TXT_AddWidget(window, TXT_NewSeparator("Input boxes")); table = TXT_NewTable(2); 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; diff --git a/textscreen/txt_label.c b/textscreen/txt_label.c index a2afc13b..c4238bf3 100644 --- a/textscreen/txt_label.c +++ b/textscreen/txt_label.c @@ -26,6 +26,7 @@ #include "txt_gui.h" #include "txt_io.h" #include "txt_main.h" +#include "txt_utf8.h" #include "txt_window.h" static void TXT_LabelSizeCalc(TXT_UNCAST_ARG(label)) @@ -68,7 +69,7 @@ static void TXT_LabelDrawer(TXT_UNCAST_ARG(label)) align_indent = label->w - strlen(label->lines[y]); break; } - + // Draw this line TXT_GotoXY(origin_x, origin_y + y); @@ -82,8 +83,8 @@ static void TXT_LabelDrawer(TXT_UNCAST_ARG(label)) // The string itself - TXT_DrawString(label->lines[y]); - x += strlen(label->lines[y]); + TXT_DrawUTF8String(label->lines[y]); + x += TXT_UTF8_Strlen(label->lines[y]); // Gap at the end @@ -123,7 +124,7 @@ void TXT_SetLabel(txt_label_t *label, char *value) free(label->label); free(label->lines); - // Set the new value + // Set the new value label->label = strdup(value); @@ -144,7 +145,7 @@ void TXT_SetLabel(txt_label_t *label, char *value) label->lines = malloc(sizeof(char *) * label->h); label->lines[0] = label->label; y = 1; - + for (p = label->label; *p != '\0'; ++p) { if (*p == '\n') @@ -159,8 +160,12 @@ void TXT_SetLabel(txt_label_t *label, char *value) for (y=0; y<label->h; ++y) { - if (strlen(label->lines[y]) > label->w) - label->w = strlen(label->lines[y]); + unsigned int line_len; + + line_len = TXT_UTF8_Strlen(label->lines[y]); + + if (line_len > label->w) + label->w = line_len; } } |