testime.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* A simple program to test the Input Method support in the SDL library (2.0+) */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "SDL.h"
  15. #ifdef HAVE_SDL_TTF
  16. #include "SDL_ttf.h"
  17. #endif
  18. #include "SDL_test_common.h"
  19. #define DEFAULT_PTSIZE 30
  20. #define DEFAULT_FONT "/System/Library/Fonts/华文细黑.ttf"
  21. #define MAX_TEXT_LENGTH 256
  22. static SDLTest_CommonState *state;
  23. static SDL_Rect textRect, markedRect;
  24. static SDL_Color lineColor = {0,0,0,0};
  25. static SDL_Color backColor = {255,255,255,0};
  26. static SDL_Color textColor = {0,0,0,0};
  27. static char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
  28. static int cursor = 0;
  29. #ifdef HAVE_SDL_TTF
  30. static TTF_Font *font;
  31. #endif
  32. size_t utf8_length(unsigned char c)
  33. {
  34. c = (unsigned char)(0xff & c);
  35. if (c < 0x80)
  36. return 1;
  37. else if ((c >> 5) ==0x6)
  38. return 2;
  39. else if ((c >> 4) == 0xe)
  40. return 3;
  41. else if ((c >> 3) == 0x1e)
  42. return 4;
  43. else
  44. return 0;
  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. for (; i < len; ++i)
  53. {
  54. ++p;
  55. if (!*p)
  56. return 0;
  57. }
  58. return p;
  59. }
  60. char *utf8_advance(char *p, size_t distance)
  61. {
  62. size_t i = 0;
  63. for (; i < distance && p; ++i)
  64. {
  65. p = utf8_next(p);
  66. }
  67. return p;
  68. }
  69. void usage()
  70. {
  71. SDL_Log("usage: testime [--font fontfile]\n");
  72. exit(0);
  73. }
  74. void InitInput()
  75. {
  76. /* Prepare a rect for text input */
  77. textRect.x = textRect.y = 100;
  78. textRect.w = DEFAULT_WINDOW_WIDTH - 2 * textRect.x;
  79. textRect.h = 50;
  80. text[0] = 0;
  81. markedRect = textRect;
  82. markedText[0] = 0;
  83. SDL_StartTextInput();
  84. }
  85. void CleanupVideo()
  86. {
  87. SDL_StopTextInput();
  88. #ifdef HAVE_SDL_TTF
  89. TTF_CloseFont(font);
  90. TTF_Quit();
  91. #endif
  92. }
  93. void _Redraw(SDL_Renderer * renderer) {
  94. int w = 0, h = textRect.h;
  95. SDL_Rect cursorRect, underlineRect;
  96. SDL_SetRenderDrawColor(renderer, 255,255,255,255);
  97. SDL_RenderFillRect(renderer,&textRect);
  98. #ifdef HAVE_SDL_TTF
  99. if (*text)
  100. {
  101. SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
  102. SDL_Rect dest = {textRect.x, textRect.y, textSur->w, textSur->h };
  103. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
  104. SDL_FreeSurface(textSur);
  105. SDL_RenderCopy(renderer,texture,NULL,&dest);
  106. SDL_DestroyTexture(texture);
  107. TTF_SizeUTF8(font, text, &w, &h);
  108. }
  109. #endif
  110. markedRect.x = textRect.x + w;
  111. markedRect.w = textRect.w - w;
  112. if (markedRect.w < 0)
  113. {
  114. /* Stop text input because we cannot hold any more characters */
  115. SDL_StopTextInput();
  116. return;
  117. }
  118. else
  119. {
  120. SDL_StartTextInput();
  121. }
  122. cursorRect = markedRect;
  123. cursorRect.w = 2;
  124. cursorRect.h = h;
  125. SDL_SetRenderDrawColor(renderer, 255,255,255,255);
  126. SDL_RenderFillRect(renderer,&markedRect);
  127. if (markedText[0])
  128. {
  129. #ifdef HAVE_SDL_TTF
  130. if (cursor)
  131. {
  132. char *p = utf8_advance(markedText, cursor);
  133. char c = 0;
  134. if (!p)
  135. p = &markedText[strlen(markedText)];
  136. c = *p;
  137. *p = 0;
  138. TTF_SizeUTF8(font, markedText, &w, 0);
  139. cursorRect.x += w;
  140. *p = c;
  141. }
  142. SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, markedText, textColor);
  143. SDL_Rect dest = {markedRect.x, markedRect.y, textSur->w, textSur->h };
  144. TTF_SizeUTF8(font, markedText, &w, &h);
  145. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
  146. SDL_FreeSurface(textSur);
  147. SDL_RenderCopy(renderer,texture,NULL,&dest);
  148. SDL_DestroyTexture(texture);
  149. #endif
  150. underlineRect = markedRect;
  151. underlineRect.y += (h - 2);
  152. underlineRect.h = 2;
  153. underlineRect.w = w;
  154. SDL_SetRenderDrawColor(renderer, 0,0,0,0);
  155. SDL_RenderFillRect(renderer,&markedRect);
  156. }
  157. SDL_SetRenderDrawColor(renderer, 0,0,0,0);
  158. SDL_RenderFillRect(renderer,&cursorRect);
  159. SDL_SetTextInputRect(&markedRect);
  160. }
  161. void Redraw() {
  162. int i;
  163. for (i = 0; i < state->num_windows; ++i) {
  164. SDL_Renderer *renderer = state->renderers[i];
  165. if (state->windows[i] == NULL)
  166. continue;
  167. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  168. SDL_RenderClear(renderer);
  169. _Redraw(renderer);
  170. SDL_RenderPresent(renderer);
  171. }
  172. }
  173. int main(int argc, char *argv[]) {
  174. int i, done;
  175. SDL_Event event;
  176. const char *fontname = DEFAULT_FONT;
  177. /* Enable standard application logging */
  178. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  179. /* Initialize test framework */
  180. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  181. if (!state) {
  182. return 1;
  183. }
  184. for (i = 1; i < argc;i++) {
  185. SDLTest_CommonArg(state, i);
  186. }
  187. for (argc--, argv++; argc > 0; argc--, argv++)
  188. {
  189. if (strcmp(argv[0], "--help") == 0) {
  190. usage();
  191. return 0;
  192. }
  193. else if (strcmp(argv[0], "--font") == 0)
  194. {
  195. argc--;
  196. argv++;
  197. if (argc > 0)
  198. fontname = argv[0];
  199. else {
  200. usage();
  201. return 0;
  202. }
  203. }
  204. }
  205. if (!SDLTest_CommonInit(state)) {
  206. return 2;
  207. }
  208. #ifdef HAVE_SDL_TTF
  209. /* Initialize fonts */
  210. TTF_Init();
  211. font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
  212. if (! font)
  213. {
  214. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find font: %s\n", TTF_GetError());
  215. exit(-1);
  216. }
  217. #endif
  218. SDL_Log("Using font: %s\n", fontname);
  219. atexit(SDL_Quit);
  220. InitInput();
  221. /* Create the windows and initialize the renderers */
  222. for (i = 0; i < state->num_windows; ++i) {
  223. SDL_Renderer *renderer = state->renderers[i];
  224. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  225. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  226. SDL_RenderClear(renderer);
  227. }
  228. Redraw();
  229. /* Main render loop */
  230. done = 0;
  231. while (!done) {
  232. /* Check for events */
  233. while (SDL_PollEvent(&event)) {
  234. SDLTest_CommonEvent(state, &event, &done);
  235. switch(event.type) {
  236. case SDL_KEYDOWN: {
  237. switch (event.key.keysym.sym)
  238. {
  239. case SDLK_RETURN:
  240. text[0]=0x00;
  241. Redraw();
  242. break;
  243. case SDLK_BACKSPACE:
  244. {
  245. int textlen=SDL_strlen(text);
  246. do {
  247. if (textlen==0)
  248. {
  249. break;
  250. }
  251. if ((text[textlen-1] & 0x80) == 0x00)
  252. {
  253. /* One byte */
  254. text[textlen-1]=0x00;
  255. break;
  256. }
  257. if ((text[textlen-1] & 0xC0) == 0x80)
  258. {
  259. /* Byte from the multibyte sequence */
  260. text[textlen-1]=0x00;
  261. textlen--;
  262. }
  263. if ((text[textlen-1] & 0xC0) == 0xC0)
  264. {
  265. /* First byte of multibyte sequence */
  266. text[textlen-1]=0x00;
  267. break;
  268. }
  269. } while(1);
  270. Redraw();
  271. }
  272. break;
  273. }
  274. if (done)
  275. {
  276. break;
  277. }
  278. SDL_Log("Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
  279. event.key.keysym.scancode,
  280. SDL_GetScancodeName(event.key.keysym.scancode),
  281. event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
  282. break;
  283. case SDL_TEXTINPUT:
  284. if (event.text.text[0] == '\0' || event.text.text[0] == '\n' ||
  285. markedRect.w < 0)
  286. break;
  287. SDL_Log("Keyboard: text input \"%s\"\n", event.text.text);
  288. if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
  289. SDL_strlcat(text, event.text.text, sizeof(text));
  290. SDL_Log("text inputed: %s\n", text);
  291. /* After text inputed, we can clear up markedText because it */
  292. /* is committed */
  293. markedText[0] = 0;
  294. Redraw();
  295. break;
  296. case SDL_TEXTEDITING:
  297. SDL_Log("text editing \"%s\", selected range (%d, %d)\n",
  298. event.edit.text, event.edit.start, event.edit.length);
  299. strcpy(markedText, event.edit.text);
  300. cursor = event.edit.start;
  301. Redraw();
  302. break;
  303. }
  304. break;
  305. }
  306. }
  307. }
  308. CleanupVideo();
  309. SDLTest_CommonQuit(state);
  310. return 0;
  311. }
  312. /* vi: set ts=4 sw=4 expandtab: */