summaryrefslogtreecommitdiff
path: root/textscreen/txt_inputbox.c
diff options
context:
space:
mode:
authorSimon Howard2012-02-03 21:10:36 +0000
committerSimon Howard2012-02-03 21:10:36 +0000
commit85b5a7487d5b44b94e6d2fc74c997ec69e352b1a (patch)
tree2d5c58da8f23deb97b08f1c8e3ed8017197aa7cd /textscreen/txt_inputbox.c
parentd745a6409c84a9ffb85e5d2029132642435c5879 (diff)
downloadchocolate-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
Diffstat (limited to 'textscreen/txt_inputbox.c')
-rw-r--r--textscreen/txt_inputbox.c44
1 files changed, 30 insertions, 14 deletions
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;