comparison test/testime.c @ 3134:f896821736fb gsoc2009_IME

Polish test code
author Jiang Jiang <gzjjgod@gmail.com>
date Thu, 06 Aug 2009 08:53:00 +0000
parents 84119fe89d26
children 962357f325e1
comparison
equal deleted inserted replaced
3133:84119fe89d26 3134:f896821736fb
9 9
10 #define DEFAULT_PTSIZE 30 10 #define DEFAULT_PTSIZE 30
11 #define DEFAULT_FONT "DroidSansFallback.ttf" 11 #define DEFAULT_FONT "DroidSansFallback.ttf"
12 #define MAX_TEXT_LENGTH 256 12 #define MAX_TEXT_LENGTH 256
13 13
14 static void render_text(SDL_Surface *sur, 14 SDL_Surface *screen;
15 TTF_Font *font;
16 SDL_Rect textRect, markedRect;
17 Uint32 lineColor, backColor;
18 SDL_Color textColor = { 0, 0, 0 };
19 char text[MAX_TEXT_LENGTH], *markedText;
20
21 void InitVideo(int argc, char *argv[])
22 {
23 int width = 500, height = 250;
24
25 SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
26 if (SDL_Init(SDL_INIT_VIDEO) < 0)
27 {
28 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
29 exit(-1);
30 }
31
32 /* Initialize fonts */
33 TTF_Init();
34
35 font = TTF_OpenFont(DEFAULT_FONT, DEFAULT_PTSIZE);
36 if (! font)
37 {
38 fprintf(stderr, "Failed to find font: %s\n", SDL_GetError());
39 exit(-1);
40 }
41
42 atexit(SDL_Quit);
43
44 int flags = SDL_HWSURFACE;
45 if (argc > 1 && strcmp(argv[1], "--fullscreen") == 0)
46 {
47 SDL_DisplayMode mode;
48 SDL_GetDesktopDisplayMode(&mode);
49
50 width = mode.w;
51 height = mode.h;
52 fprintf(stderr, "%dx%d\n", width, height);
53 flags |= SDL_FULLSCREEN;
54 }
55
56 /* Create window */
57 screen = SDL_SetVideoMode(width, height, 32, flags);
58 if (screen == NULL)
59 {
60 fprintf(stderr, "Unable to set %dx%d video: %s\n",
61 width, height, SDL_GetError());
62 exit(-1);
63 }
64 }
65
66 void CleanupVideo()
67 {
68 TTF_CloseFont(font);
69 TTF_Quit();
70 }
71
72 void InitInput()
73 {
74 backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
75 lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0);
76
77 /* Prepare a rect for text input */
78 textRect.x = textRect.y = 100;
79 textRect.w = screen->w - 2 * textRect.x;
80 textRect.h = 50;
81
82 text[0] = 0;
83 markedRect = textRect;
84 markedText = NULL;
85 }
86
87 static void RenderText(SDL_Surface *sur,
15 TTF_Font *font, 88 TTF_Font *font,
16 const char *text, 89 const char *text,
17 int x, int y, 90 int x, int y,
18 SDL_Color color) 91 SDL_Color color)
19 { 92 {
22 95
23 SDL_BlitSurface(textSur, NULL, sur, &dest); 96 SDL_BlitSurface(textSur, NULL, sur, &dest);
24 SDL_FreeSurface(textSur); 97 SDL_FreeSurface(textSur);
25 } 98 }
26 99
100 void Redraw()
101 {
102 int w = 0, h = textRect.h;
103 SDL_Rect cursorRect, underlineRect;
104
105 SDL_FillRect(screen, &textRect, backColor);
106
107 if (strlen(text))
108 {
109 RenderText(screen, font, text, textRect.x, textRect.y, textColor);
110 TTF_SizeUTF8(font, text, &w, &h);
111 }
112
113 markedRect.x = textRect.x + w;
114 markedRect.w = textRect.w - w;
115 if (markedRect.w < 0)
116 {
117 SDL_Flip(screen);
118 return;
119 }
120
121 SDL_FillRect(screen, &markedRect, backColor);
122
123 if (markedText)
124 {
125 RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
126 TTF_SizeUTF8(font, markedText, &w, &h);
127
128 underlineRect = markedRect;
129 underlineRect.y += (h - 2);
130 underlineRect.h = 2;
131 underlineRect.w = w;
132 SDL_FillRect(screen, &underlineRect, lineColor);
133 }
134
135 cursorRect = markedRect;
136 cursorRect.w = 2;
137 cursorRect.h = h;
138 SDL_FillRect(screen, &cursorRect, lineColor);
139
140 SDL_Flip(screen);
141
142 SDL_StartTextInput(&markedRect);
143 }
144
145 void
146 HotKey_ToggleFullScreen(void)
147 {
148 SDL_Surface *screen;
149
150 screen = SDL_GetVideoSurface();
151 if (SDL_WM_ToggleFullScreen(screen)) {
152 printf("Toggled fullscreen mode - now %s\n",
153 (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
154 } else {
155 printf("Unable to toggle fullscreen mode\n");
156 }
157 }
158
27 int main(int argc, char *argv[]) 159 int main(int argc, char *argv[])
28 { 160 {
29 int width, height; 161 InitVideo(argc, argv);
30 SDL_Surface *screen; 162 InitInput();
31 TTF_Font *font; 163 Redraw();
32
33 width = 500, height = 250;
34
35 SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
36
37 if (SDL_Init(SDL_INIT_VIDEO) < 0)
38 {
39 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
40 return -1;
41 }
42
43 /* Initialize fonts */
44 TTF_Init();
45
46 font = TTF_OpenFont(DEFAULT_FONT, DEFAULT_PTSIZE);
47 if (! font)
48 {
49 fprintf(stderr, "Failed to find font: %s\n", SDL_GetError());
50 exit(-1);
51 }
52
53 atexit(SDL_Quit);
54
55 /* Create window */
56 screen = SDL_SetVideoMode(width, height, 32,
57 SDL_HWSURFACE | SDL_DOUBLEBUF);
58 if (screen == NULL)
59 {
60 fprintf(stderr, "Unable to set %dx%d video: %s\n",
61 width, height, SDL_GetError());
62 return -1;
63 }
64
65 /* Prepare a rect for text input */
66 SDL_Rect textRect = { 100, 80, 300, 50 }, markedRect, underlineRect, cursorRect;
67 Uint32 backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
68 Uint32 lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0);
69 SDL_Color textColor = { 0, 0, 0 };
70 SDL_FillRect(screen, &textRect, backColor);
71
72 markedRect = textRect;
73 SDL_StartTextInput(&markedRect);
74
75 SDL_Flip(screen);
76 164
77 SDL_Event event; 165 SDL_Event event;
78 int done = 0, inputed = 0; 166 int done = 0;
79 int w, h;
80 char text[MAX_TEXT_LENGTH];
81 167
82 while (! done && SDL_WaitEvent(&event)) 168 while (! done && SDL_WaitEvent(&event))
83 { 169 {
84 switch (event.type) 170 switch (event.type)
85 { 171 {
86 case SDL_KEYDOWN: 172 case SDL_KEYDOWN:
173 if (event.key.keysym.sym == SDLK_ESCAPE) {
174 done = 1;
175 break;
176 }
177
87 fprintf(stderr, 178 fprintf(stderr,
88 "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n", 179 "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
89 event.key.which, event.key.keysym.scancode, 180 event.key.which, event.key.keysym.scancode,
90 SDL_GetScancodeName(event.key.keysym.scancode), 181 SDL_GetScancodeName(event.key.keysym.scancode),
91 event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym)); 182 event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
92 break; 183 break;
93 184
94 case SDL_TEXTINPUT: 185 case SDL_TEXTINPUT:
186 if (strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
187 markedRect.w < 0)
188 break;
189
95 fprintf(stderr, "Keyboard %d: text input \"%s\"\n", 190 fprintf(stderr, "Keyboard %d: text input \"%s\"\n",
96 event.text.which, event.text.text); 191 event.text.which, event.text.text);
97 192
98 if (inputed < sizeof(text)) 193 if (strlen(text) + strlen(event.text.text) < sizeof(text))
99 { 194 strcpy(text + strlen(text), event.text.text);
100 strcpy(text + inputed, event.text.text);
101 inputed += strlen(event.text.text);
102 }
103 195
104 fprintf(stderr, "text inputed: %s\n", text); 196 fprintf(stderr, "text inputed: %s\n", text);
105 SDL_FillRect(screen, &textRect, backColor); 197
106 198 // After text inputed, we can clear up markedText because it
107 render_text(screen, font, text, textRect.x, textRect.y, textColor); 199 // is committed
108 TTF_SizeUTF8(font, text, &w, &h); 200 markedText = NULL;
109 markedRect.x = textRect.x + w; 201 Redraw();
110
111 cursorRect = markedRect;
112 cursorRect.w = 2;
113 cursorRect.h = h;
114 SDL_FillRect(screen, &cursorRect, lineColor);
115 SDL_Flip(screen);
116
117 SDL_StartTextInput(&markedRect);
118 break; 202 break;
119 203
120 case SDL_TEXTEDITING: 204 case SDL_TEXTEDITING:
121 fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n", 205 fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
122 event.edit.text, event.edit.start, event.edit.length); 206 event.edit.text, event.edit.start, event.edit.length);
123 207
124 SDL_FillRect(screen, &markedRect, backColor); 208 markedText = event.edit.text;
125 render_text(screen, font, event.edit.text, markedRect.x, markedRect.y, textColor); 209 Redraw();
126 TTF_SizeUTF8(font, event.edit.text, &w, &h);
127 underlineRect = markedRect;
128 underlineRect.y += (h - 2);
129 underlineRect.h = 2;
130 underlineRect.w = w;
131 SDL_FillRect(screen, &underlineRect, lineColor);
132
133 SDL_Flip(screen);
134 break; 210 break;
135 211
136 case SDL_QUIT: 212 case SDL_QUIT:
137 done = 1; 213 done = 1;
138 break; 214 break;
140 default: 216 default:
141 break; 217 break;
142 } 218 }
143 } 219 }
144 220
145 TTF_CloseFont(font); 221 CleanupVideo();
146 TTF_Quit();
147
148 return 0; 222 return 0;
149 } 223 }
150 224