Mercurial > sdl-ios-xcode
comparison 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 |
comparison
equal
deleted
inserted
replaced
4762:833a225613e2 | 4763:518d1679d2d0 |
---|---|
23 TTF_Font *font; | 23 TTF_Font *font; |
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; | 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); |
122 textRect.w = screen->w - 2 * textRect.x; | 164 textRect.w = screen->w - 2 * textRect.x; |
123 textRect.h = 50; | 165 textRect.h = 50; |
124 | 166 |
125 text[0] = 0; | 167 text[0] = 0; |
126 markedRect = textRect; | 168 markedRect = textRect; |
127 markedText = NULL; | 169 markedText[0] = 0; |
128 | 170 |
129 SDL_StartTextInput(); | 171 SDL_StartTextInput(); |
130 } | 172 } |
131 | 173 |
132 #ifdef HAVE_SDL_TTF | 174 #ifdef HAVE_SDL_TTF |
178 cursorRect = markedRect; | 220 cursorRect = markedRect; |
179 cursorRect.w = 2; | 221 cursorRect.w = 2; |
180 cursorRect.h = h; | 222 cursorRect.h = h; |
181 | 223 |
182 SDL_FillRect(screen, &markedRect, backColor); | 224 SDL_FillRect(screen, &markedRect, backColor); |
183 if (markedText) | 225 if (markedText[0]) |
184 { | 226 { |
185 #ifdef HAVE_SDL_TTF | 227 #ifdef HAVE_SDL_TTF |
228 if (cursor) | |
229 { | |
230 char *p = utf8_advance(markedText, cursor); | |
231 char c = 0; | |
232 if (!p) | |
233 p = &markedText[strlen(markedText)]; | |
234 | |
235 c = *p; | |
236 *p = 0; | |
237 TTF_SizeUTF8(font, markedText, &w, 0); | |
238 cursorRect.x += w; | |
239 *p = c; | |
240 } | |
186 RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor); | 241 RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor); |
187 TTF_SizeUTF8(font, markedText, &w, &h); | 242 TTF_SizeUTF8(font, markedText, &w, &h); |
188 #endif | 243 #endif |
189 | 244 |
190 underlineRect = markedRect; | 245 underlineRect = markedRect; |
191 underlineRect.y += (h - 2); | 246 underlineRect.y += (h - 2); |
192 underlineRect.h = 2; | 247 underlineRect.h = 2; |
193 underlineRect.w = w; | 248 underlineRect.w = w; |
194 | |
195 cursorRect.x += w + 1; | |
196 | 249 |
197 SDL_FillRect(screen, &underlineRect, lineColor); | 250 SDL_FillRect(screen, &underlineRect, lineColor); |
198 } | 251 } |
199 | 252 |
200 SDL_FillRect(screen, &cursorRect, lineColor); | 253 SDL_FillRect(screen, &cursorRect, lineColor); |
293 break; | 346 break; |
294 | 347 |
295 fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text); | 348 fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text); |
296 | 349 |
297 if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text)) | 350 if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text)) |
298 SDL_strlcpy(text + SDL_strlen(text), event.text.text, sizeof(text)); | 351 SDL_strlcat(text, event.text.text, sizeof(text)); |
299 | 352 |
300 fprintf(stderr, "text inputed: %s\n", text); | 353 fprintf(stderr, "text inputed: %s\n", text); |
301 | 354 |
302 // After text inputed, we can clear up markedText because it | 355 // After text inputed, we can clear up markedText because it |
303 // is committed | 356 // is committed |
304 markedText = NULL; | 357 markedText[0] = 0; |
305 Redraw(); | 358 Redraw(); |
306 break; | 359 break; |
307 | 360 |
308 case SDL_TEXTEDITING: | 361 case SDL_TEXTEDITING: |
309 fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n", | 362 fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n", |
310 event.edit.text, event.edit.start, event.edit.length); | 363 event.edit.text, event.edit.start, event.edit.length); |
311 | 364 |
312 markedText = event.edit.text; | 365 strcpy(markedText, event.edit.text); |
366 cursor = event.edit.start; | |
313 Redraw(); | 367 Redraw(); |
314 break; | 368 break; |
315 | 369 |
316 case SDL_QUIT: | 370 case SDL_QUIT: |
317 done = 1; | 371 done = 1; |