ref: 85b5a7487d5b44b94e6d2fc74c997ec69e352b1a
parent: d745a6409c84a9ffb85e5d2029132642435c5879
author: Simon Howard <[email protected]>
date: Fri Feb 3 16:10:36 EST 2012
Upgrade the input box and label widgets to use UTF-8 strings. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2491
--- a/textscreen/examples/guitest.c
+++ b/textscreen/examples/guitest.c
@@ -179,6 +179,10 @@
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);
--- 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 @@
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,20 +167,29 @@
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';
}
}
@@ -186,6 +196,7 @@
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 @@
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_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;
--- 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 @@
align_indent = label->w - strlen(label->lines[y]);
break;
}
-
+
// Draw this line
TXT_GotoXY(origin_x, origin_y + y);
@@ -82,8 +83,8 @@
// 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 @@
free(label->label);
free(label->lines);
- // Set the new value
+ // Set the new value
label->label = strdup(value);
@@ -144,7 +145,7 @@
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 @@
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;
}
}