Mercurial > sdl-ios-xcode
diff test/testime.c @ 4757:140be6839185
Correctly draw cursor based on position.
Had to add UTF-8 functions utf8_length, utf8_next, utf8_advance.
author | dewyatt |
---|---|
date | Sun, 25 Jul 2010 13:17:31 -0400 |
parents | b99b1024607a |
children | 518d1679d2d0 |
line wrap: on
line diff
--- a/test/testime.c Sat Jul 24 20:48:50 2010 -0400 +++ b/test/testime.c Sun Jul 25 13:17:31 2010 -0400 @@ -26,6 +26,48 @@ Uint32 lineColor, backColor; SDL_Color textColor = { 0, 0, 0 }; 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() { @@ -181,6 +223,19 @@ 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 @@ -190,8 +245,6 @@ underlineRect.h = 2; underlineRect.w = w; - cursorRect.x += w + 1; - SDL_FillRect(screen, &underlineRect, lineColor); } @@ -308,6 +361,7 @@ event.edit.text, event.edit.start, event.edit.length); strcpy(markedText, event.edit.text); + cursor = event.edit.start; Redraw(); break;