2 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
12 /* A simple program to test the Input Method support in the SDL library (2.0+) */
23 #include "SDL_test_common.h"
25 #define DEFAULT_PTSIZE 30
26 #define DEFAULT_FONT "/System/Library/Fonts/华文细黑.ttf"
27 #define MAX_TEXT_LENGTH 256
29 static SDLTest_CommonState *state;
30 static SDL_Rect textRect, markedRect;
31 static SDL_Color lineColor = {0,0,0,0};
32 static SDL_Color backColor = {255,255,255,0};
33 static SDL_Color textColor = {0,0,0,0};
34 static char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
35 static int cursor = 0;
37 static TTF_Font *font;
40 size_t utf8_length(unsigned char c)
42 c = (unsigned char)(0xff & c);
45 else if ((c >> 5) ==0x6)
47 else if ((c >> 4) == 0xe)
49 else if ((c >> 3) == 0x1e)
55 char *utf8_next(char *p)
57 size_t len = utf8_length(*p);
71 char *utf8_advance(char *p, size_t distance)
74 for (; i < distance && p; ++i)
83 SDL_Log("usage: testime [--font fontfile]\n");
90 /* Prepare a rect for text input */
91 textRect.x = textRect.y = 100;
92 textRect.w = DEFAULT_WINDOW_WIDTH - 2 * textRect.x;
96 markedRect = textRect;
112 void _Redraw(SDL_Renderer * renderer) {
113 int w = 0, h = textRect.h;
114 SDL_Rect cursorRect, underlineRect;
116 SDL_SetRenderDrawColor(renderer, 255,255,255,255);
117 SDL_RenderFillRect(renderer,&textRect);
122 SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
123 SDL_Rect dest = {textRect.x, textRect.y, textSur->w, textSur->h };
125 SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
126 SDL_FreeSurface(textSur);
128 SDL_RenderCopy(renderer,texture,NULL,&dest);
129 SDL_DestroyTexture(texture);
130 TTF_SizeUTF8(font, text, &w, &h);
134 markedRect.x = textRect.x + w;
135 markedRect.w = textRect.w - w;
136 if (markedRect.w < 0)
138 /* Stop text input because we cannot hold any more characters */
144 SDL_StartTextInput();
147 cursorRect = markedRect;
151 SDL_SetRenderDrawColor(renderer, 255,255,255,255);
152 SDL_RenderFillRect(renderer,&markedRect);
159 char *p = utf8_advance(markedText, cursor);
162 p = &markedText[strlen(markedText)];
166 TTF_SizeUTF8(font, markedText, &w, 0);
170 SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, markedText, textColor);
171 SDL_Rect dest = {markedRect.x, markedRect.y, textSur->w, textSur->h };
172 TTF_SizeUTF8(font, markedText, &w, &h);
173 SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
174 SDL_FreeSurface(textSur);
176 SDL_RenderCopy(renderer,texture,NULL,&dest);
177 SDL_DestroyTexture(texture);
180 underlineRect = markedRect;
181 underlineRect.y += (h - 2);
185 SDL_SetRenderDrawColor(renderer, 0,0,0,0);
186 SDL_RenderFillRect(renderer,&markedRect);
189 SDL_SetRenderDrawColor(renderer, 0,0,0,0);
190 SDL_RenderFillRect(renderer,&cursorRect);
192 SDL_SetTextInputRect(&markedRect);
197 for (i = 0; i < state->num_windows; ++i) {
198 SDL_Renderer *renderer = state->renderers[i];
199 if (state->windows[i] == NULL)
201 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
202 SDL_RenderClear(renderer);
206 SDL_RenderPresent(renderer);
210 int main(int argc, char *argv[]) {
213 const char *fontname = DEFAULT_FONT;
215 /* Enable standard application logging */
216 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
218 /* Initialize test framework */
219 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
223 for (i = 1; i < argc;i++) {
224 SDLTest_CommonArg(state, i);
226 for (argc--, argv++; argc > 0; argc--, argv++)
228 if (strcmp(argv[0], "--help") == 0) {
233 else if (strcmp(argv[0], "--font") == 0)
247 if (!SDLTest_CommonInit(state)) {
253 /* Initialize fonts */
256 font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
259 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find font: %s\n", TTF_GetError());
264 SDL_Log("Using font: %s\n", fontname);
268 /* Create the windows and initialize the renderers */
269 for (i = 0; i < state->num_windows; ++i) {
270 SDL_Renderer *renderer = state->renderers[i];
271 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
272 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
273 SDL_RenderClear(renderer);
276 /* Main render loop */
279 /* Check for events */
280 while (SDL_PollEvent(&event)) {
281 SDLTest_CommonEvent(state, &event, &done);
284 switch (event.key.keysym.sym)
292 size_t textlen = SDL_strlen(text);
299 if ((text[textlen-1] & 0x80) == 0x00)
302 text[textlen-1]=0x00;
305 if ((text[textlen-1] & 0xC0) == 0x80)
307 /* Byte from the multibyte sequence */
308 text[textlen-1]=0x00;
311 if ((text[textlen-1] & 0xC0) == 0xC0)
313 /* First byte of multibyte sequence */
314 text[textlen-1]=0x00;
329 SDL_Log("Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
330 event.key.keysym.scancode,
331 SDL_GetScancodeName(event.key.keysym.scancode),
332 event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
336 if (event.text.text[0] == '\0' || event.text.text[0] == '\n' ||
340 SDL_Log("Keyboard: text input \"%s\"\n", event.text.text);
342 if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
343 SDL_strlcat(text, event.text.text, sizeof(text));
345 SDL_Log("text inputed: %s\n", text);
347 /* After text inputed, we can clear up markedText because it */
353 case SDL_TEXTEDITING:
354 SDL_Log("text editing \"%s\", selected range (%d, %d)\n",
355 event.edit.text, event.edit.start, event.edit.length);
357 strcpy(markedText, event.edit.text);
358 cursor = event.edit.start;
368 SDLTest_CommonQuit(state);
373 /* vi: set ts=4 sw=4 expandtab: */