test/testime.c
changeset 4757 140be6839185
parent 4756 b99b1024607a
child 4763 518d1679d2d0
     1.1 --- a/test/testime.c	Sat Jul 24 20:48:50 2010 -0400
     1.2 +++ b/test/testime.c	Sun Jul 25 13:17:31 2010 -0400
     1.3 @@ -26,6 +26,48 @@
     1.4  Uint32 lineColor, backColor;
     1.5  SDL_Color textColor = { 0, 0, 0 };
     1.6  char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
     1.7 +int cursor = 0;
     1.8 +
     1.9 +size_t utf8_length(unsigned char c)
    1.10 +{
    1.11 +    c = (unsigned char)(0xff & c);
    1.12 +    if (c < 0x80)
    1.13 +        return 1;
    1.14 +    else if ((c >> 5) ==0x6)
    1.15 +        return 2;
    1.16 +    else if ((c >> 4) == 0xe)
    1.17 +        return 3;
    1.18 +    else if ((c >> 3) == 0x1e)
    1.19 +        return 4;
    1.20 +    else
    1.21 +        return 0;
    1.22 +}
    1.23 +
    1.24 +char *utf8_next(char *p)
    1.25 +{
    1.26 +    size_t len = utf8_length(*p);
    1.27 +    size_t i = 0;
    1.28 +    if (!len)
    1.29 +        return 0;
    1.30 +
    1.31 +    for (; i < len; ++i)
    1.32 +    {
    1.33 +        ++p;
    1.34 +        if (!*p)
    1.35 +            return 0;
    1.36 +    }
    1.37 +    return p;
    1.38 +}
    1.39 +
    1.40 +char *utf8_advance(char *p, size_t distance)
    1.41 +{
    1.42 +    size_t i = 0;
    1.43 +    for (; i < distance && p; ++i)
    1.44 +    {
    1.45 +        p = utf8_next(p);
    1.46 +    }
    1.47 +    return p;
    1.48 +}
    1.49  
    1.50  void usage()
    1.51  {
    1.52 @@ -181,6 +223,19 @@
    1.53      if (markedText[0])
    1.54      {
    1.55  #ifdef HAVE_SDL_TTF
    1.56 +        if (cursor)
    1.57 +        {
    1.58 +            char *p = utf8_advance(markedText, cursor);
    1.59 +            char c = 0;
    1.60 +            if (!p)
    1.61 +                p = &markedText[strlen(markedText)];
    1.62 +
    1.63 +            c = *p;
    1.64 +            *p = 0;
    1.65 +            TTF_SizeUTF8(font, markedText, &w, 0);
    1.66 +            cursorRect.x += w;
    1.67 +            *p = c;
    1.68 +        }
    1.69          RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
    1.70          TTF_SizeUTF8(font, markedText, &w, &h);
    1.71  #endif
    1.72 @@ -190,8 +245,6 @@
    1.73          underlineRect.h = 2;
    1.74          underlineRect.w = w;
    1.75  
    1.76 -        cursorRect.x += w + 1;
    1.77 -
    1.78          SDL_FillRect(screen, &underlineRect, lineColor);
    1.79      }
    1.80  
    1.81 @@ -308,6 +361,7 @@
    1.82                      event.edit.text, event.edit.start, event.edit.length);
    1.83  
    1.84              strcpy(markedText, event.edit.text);
    1.85 +            cursor = event.edit.start;
    1.86              Redraw();
    1.87              break;
    1.88