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