diff test/testime.c @ 4763:518d1679d2d0

Merged Daniel's Google Summer of Code work from SDL-gsoc2010_IME
author Sam Lantinga <slouken@libsdl.org>
date Sun, 22 Aug 2010 12:39:27 -0700
parents 08d22c54a21f 140be6839185
children 1435f8a6425c
line wrap: on
line diff
--- a/test/testime.c	Sun Aug 22 12:35:34 2010 -0700
+++ b/test/testime.c	Sun Aug 22 12:39:27 2010 -0700
@@ -25,7 +25,49 @@
 SDL_Rect textRect, markedRect;
 Uint32 lineColor, backColor;
 SDL_Color textColor = { 0, 0, 0 };
-char text[MAX_TEXT_LENGTH], *markedText;
+char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
+int cursor = 0;
+
+size_t utf8_length(unsigned char c)
+{
+    c = (unsigned char)(0xff & c);
+    if (c < 0x80)
+        return 1;
+    else if ((c >> 5) ==0x6)
+        return 2;
+    else if ((c >> 4) == 0xe)
+        return 3;
+    else if ((c >> 3) == 0x1e)
+        return 4;
+    else
+        return 0;
+}
+
+char *utf8_next(char *p)
+{
+    size_t len = utf8_length(*p);
+    size_t i = 0;
+    if (!len)
+        return 0;
+
+    for (; i < len; ++i)
+    {
+        ++p;
+        if (!*p)
+            return 0;
+    }
+    return p;
+}
+
+char *utf8_advance(char *p, size_t distance)
+{
+    size_t i = 0;
+    for (; i < distance && p; ++i)
+    {
+        p = utf8_next(p);
+    }
+    return p;
+}
 
 void usage()
 {
@@ -124,7 +166,7 @@
 
     text[0] = 0;
     markedRect = textRect;
-    markedText = NULL;
+    markedText[0] = 0;
 
     SDL_StartTextInput();
 }
@@ -180,9 +222,22 @@
     cursorRect.h = h;
 
     SDL_FillRect(screen, &markedRect, backColor);
-    if (markedText)
+    if (markedText[0])
     {
 #ifdef HAVE_SDL_TTF
+        if (cursor)
+        {
+            char *p = utf8_advance(markedText, cursor);
+            char c = 0;
+            if (!p)
+                p = &markedText[strlen(markedText)];
+
+            c = *p;
+            *p = 0;
+            TTF_SizeUTF8(font, markedText, &w, 0);
+            cursorRect.x += w;
+            *p = c;
+        }
         RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
         TTF_SizeUTF8(font, markedText, &w, &h);
 #endif
@@ -192,8 +247,6 @@
         underlineRect.h = 2;
         underlineRect.w = w;
 
-        cursorRect.x += w + 1;
-
         SDL_FillRect(screen, &underlineRect, lineColor);
     }
 
@@ -295,13 +348,13 @@
             fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text);
 
             if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
-                SDL_strlcpy(text + SDL_strlen(text), event.text.text, sizeof(text));
+                SDL_strlcat(text, event.text.text, sizeof(text));
 
             fprintf(stderr, "text inputed: %s\n", text);
 
             // After text inputed, we can clear up markedText because it
             // is committed
-            markedText = NULL;
+            markedText[0] = 0;
             Redraw();
             break;
 
@@ -309,7 +362,8 @@
             fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
                     event.edit.text, event.edit.start, event.edit.length);
 
-            markedText = event.edit.text;
+            strcpy(markedText, event.edit.text);
+            cursor = event.edit.start;
             Redraw();
             break;