test/testime.c
changeset 4763 518d1679d2d0
parent 4638 08d22c54a21f
parent 4757 140be6839185
child 5150 1435f8a6425c
     1.1 --- a/test/testime.c	Sun Aug 22 12:35:34 2010 -0700
     1.2 +++ b/test/testime.c	Sun Aug 22 12:39:27 2010 -0700
     1.3 @@ -25,7 +25,49 @@
     1.4  SDL_Rect textRect, markedRect;
     1.5  Uint32 lineColor, backColor;
     1.6  SDL_Color textColor = { 0, 0, 0 };
     1.7 -char text[MAX_TEXT_LENGTH], *markedText;
     1.8 +char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
     1.9 +int cursor = 0;
    1.10 +
    1.11 +size_t utf8_length(unsigned char c)
    1.12 +{
    1.13 +    c = (unsigned char)(0xff & c);
    1.14 +    if (c < 0x80)
    1.15 +        return 1;
    1.16 +    else if ((c >> 5) ==0x6)
    1.17 +        return 2;
    1.18 +    else if ((c >> 4) == 0xe)
    1.19 +        return 3;
    1.20 +    else if ((c >> 3) == 0x1e)
    1.21 +        return 4;
    1.22 +    else
    1.23 +        return 0;
    1.24 +}
    1.25 +
    1.26 +char *utf8_next(char *p)
    1.27 +{
    1.28 +    size_t len = utf8_length(*p);
    1.29 +    size_t i = 0;
    1.30 +    if (!len)
    1.31 +        return 0;
    1.32 +
    1.33 +    for (; i < len; ++i)
    1.34 +    {
    1.35 +        ++p;
    1.36 +        if (!*p)
    1.37 +            return 0;
    1.38 +    }
    1.39 +    return p;
    1.40 +}
    1.41 +
    1.42 +char *utf8_advance(char *p, size_t distance)
    1.43 +{
    1.44 +    size_t i = 0;
    1.45 +    for (; i < distance && p; ++i)
    1.46 +    {
    1.47 +        p = utf8_next(p);
    1.48 +    }
    1.49 +    return p;
    1.50 +}
    1.51  
    1.52  void usage()
    1.53  {
    1.54 @@ -124,7 +166,7 @@
    1.55  
    1.56      text[0] = 0;
    1.57      markedRect = textRect;
    1.58 -    markedText = NULL;
    1.59 +    markedText[0] = 0;
    1.60  
    1.61      SDL_StartTextInput();
    1.62  }
    1.63 @@ -180,9 +222,22 @@
    1.64      cursorRect.h = h;
    1.65  
    1.66      SDL_FillRect(screen, &markedRect, backColor);
    1.67 -    if (markedText)
    1.68 +    if (markedText[0])
    1.69      {
    1.70  #ifdef HAVE_SDL_TTF
    1.71 +        if (cursor)
    1.72 +        {
    1.73 +            char *p = utf8_advance(markedText, cursor);
    1.74 +            char c = 0;
    1.75 +            if (!p)
    1.76 +                p = &markedText[strlen(markedText)];
    1.77 +
    1.78 +            c = *p;
    1.79 +            *p = 0;
    1.80 +            TTF_SizeUTF8(font, markedText, &w, 0);
    1.81 +            cursorRect.x += w;
    1.82 +            *p = c;
    1.83 +        }
    1.84          RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
    1.85          TTF_SizeUTF8(font, markedText, &w, &h);
    1.86  #endif
    1.87 @@ -192,8 +247,6 @@
    1.88          underlineRect.h = 2;
    1.89          underlineRect.w = w;
    1.90  
    1.91 -        cursorRect.x += w + 1;
    1.92 -
    1.93          SDL_FillRect(screen, &underlineRect, lineColor);
    1.94      }
    1.95  
    1.96 @@ -295,13 +348,13 @@
    1.97              fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text);
    1.98  
    1.99              if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
   1.100 -                SDL_strlcpy(text + SDL_strlen(text), event.text.text, sizeof(text));
   1.101 +                SDL_strlcat(text, event.text.text, sizeof(text));
   1.102  
   1.103              fprintf(stderr, "text inputed: %s\n", text);
   1.104  
   1.105              // After text inputed, we can clear up markedText because it
   1.106              // is committed
   1.107 -            markedText = NULL;
   1.108 +            markedText[0] = 0;
   1.109              Redraw();
   1.110              break;
   1.111  
   1.112 @@ -309,7 +362,8 @@
   1.113              fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
   1.114                      event.edit.text, event.edit.start, event.edit.length);
   1.115  
   1.116 -            markedText = event.edit.text;
   1.117 +            strcpy(markedText, event.edit.text);
   1.118 +            cursor = event.edit.start;
   1.119              Redraw();
   1.120              break;
   1.121