summaryrefslogtreecommitdiff
path: root/textscreen/txt_inputbox.c
diff options
context:
space:
mode:
authorSimon Howard2012-10-28 23:45:08 +0000
committerSimon Howard2012-10-28 23:45:08 +0000
commit993315afc4b1fddaf8952e7e55d1373b5052dd7c (patch)
tree94aa07d101ddced8404ceaa92ce051ed17fe2a88 /textscreen/txt_inputbox.c
parenta1b2ce54d02823aa85c7df6aa016c567185451ae (diff)
parentad11652dcd8e0923432ad272e6535276c51d39eb (diff)
downloadchocolate-doom-993315afc4b1fddaf8952e7e55d1373b5052dd7c.tar.gz
chocolate-doom-993315afc4b1fddaf8952e7e55d1373b5052dd7c.tar.bz2
chocolate-doom-993315afc4b1fddaf8952e7e55d1373b5052dd7c.zip
Merge from trunk.
Subversion-branch: /branches/v2-branch Subversion-revision: 2537
Diffstat (limited to 'textscreen/txt_inputbox.c')
-rw-r--r--textscreen/txt_inputbox.c67
1 files changed, 39 insertions, 28 deletions
diff --git a/textscreen/txt_inputbox.c b/textscreen/txt_inputbox.c
index 2ba08ac6..0f2986cc 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;
@@ -131,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.
@@ -140,9 +139,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 +165,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 +217,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;
@@ -277,32 +288,32 @@ 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;
- 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;
}
-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);
}