Mercurial > sdl-ios-xcode
comparison 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 |
comparison
equal
deleted
inserted
replaced
4756:b99b1024607a | 4757:140be6839185 |
---|---|
24 #endif | 24 #endif |
25 SDL_Rect textRect, markedRect; | 25 SDL_Rect textRect, markedRect; |
26 Uint32 lineColor, backColor; | 26 Uint32 lineColor, backColor; |
27 SDL_Color textColor = { 0, 0, 0 }; | 27 SDL_Color textColor = { 0, 0, 0 }; |
28 char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]; | 28 char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]; |
29 int cursor = 0; | |
30 | |
31 size_t utf8_length(unsigned char c) | |
32 { | |
33 c = (unsigned char)(0xff & c); | |
34 if (c < 0x80) | |
35 return 1; | |
36 else if ((c >> 5) ==0x6) | |
37 return 2; | |
38 else if ((c >> 4) == 0xe) | |
39 return 3; | |
40 else if ((c >> 3) == 0x1e) | |
41 return 4; | |
42 else | |
43 return 0; | |
44 } | |
45 | |
46 char *utf8_next(char *p) | |
47 { | |
48 size_t len = utf8_length(*p); | |
49 size_t i = 0; | |
50 if (!len) | |
51 return 0; | |
52 | |
53 for (; i < len; ++i) | |
54 { | |
55 ++p; | |
56 if (!*p) | |
57 return 0; | |
58 } | |
59 return p; | |
60 } | |
61 | |
62 char *utf8_advance(char *p, size_t distance) | |
63 { | |
64 size_t i = 0; | |
65 for (; i < distance && p; ++i) | |
66 { | |
67 p = utf8_next(p); | |
68 } | |
69 return p; | |
70 } | |
29 | 71 |
30 void usage() | 72 void usage() |
31 { | 73 { |
32 printf("usage: testime [--font fontfile] [--fullscreen]\n"); | 74 printf("usage: testime [--font fontfile] [--fullscreen]\n"); |
33 exit(0); | 75 exit(0); |
179 | 221 |
180 SDL_FillRect(screen, &markedRect, backColor); | 222 SDL_FillRect(screen, &markedRect, backColor); |
181 if (markedText[0]) | 223 if (markedText[0]) |
182 { | 224 { |
183 #ifdef HAVE_SDL_TTF | 225 #ifdef HAVE_SDL_TTF |
226 if (cursor) | |
227 { | |
228 char *p = utf8_advance(markedText, cursor); | |
229 char c = 0; | |
230 if (!p) | |
231 p = &markedText[strlen(markedText)]; | |
232 | |
233 c = *p; | |
234 *p = 0; | |
235 TTF_SizeUTF8(font, markedText, &w, 0); | |
236 cursorRect.x += w; | |
237 *p = c; | |
238 } | |
184 RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor); | 239 RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor); |
185 TTF_SizeUTF8(font, markedText, &w, &h); | 240 TTF_SizeUTF8(font, markedText, &w, &h); |
186 #endif | 241 #endif |
187 | 242 |
188 underlineRect = markedRect; | 243 underlineRect = markedRect; |
189 underlineRect.y += (h - 2); | 244 underlineRect.y += (h - 2); |
190 underlineRect.h = 2; | 245 underlineRect.h = 2; |
191 underlineRect.w = w; | 246 underlineRect.w = w; |
192 | |
193 cursorRect.x += w + 1; | |
194 | 247 |
195 SDL_FillRect(screen, &underlineRect, lineColor); | 248 SDL_FillRect(screen, &underlineRect, lineColor); |
196 } | 249 } |
197 | 250 |
198 SDL_FillRect(screen, &cursorRect, lineColor); | 251 SDL_FillRect(screen, &cursorRect, lineColor); |
306 case SDL_TEXTEDITING: | 359 case SDL_TEXTEDITING: |
307 fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n", | 360 fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n", |
308 event.edit.text, event.edit.start, event.edit.length); | 361 event.edit.text, event.edit.start, event.edit.length); |
309 | 362 |
310 strcpy(markedText, event.edit.text); | 363 strcpy(markedText, event.edit.text); |
364 cursor = event.edit.start; | |
311 Redraw(); | 365 Redraw(); |
312 break; | 366 break; |
313 | 367 |
314 case SDL_QUIT: | 368 case SDL_QUIT: |
315 done = 1; | 369 done = 1; |