annotate test/testime.c @ 3133:84119fe89d26 gsoc2009_IME

Draw cursor, underline marked text
author Jiang Jiang <gzjjgod@gmail.com>
date Wed, 01 Jul 2009 16:12:00 +0000
parents 88861448961f
children f896821736fb
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
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
14 static void render_text(SDL_Surface *sur,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
15 TTF_Font *font,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
16 const char *text,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
17 int x, int y,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
18 SDL_Color color)
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
19 {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
20 SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
21 SDL_Rect dest = { x, y, textSur->w, textSur->h };
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
22
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
23 SDL_BlitSurface(textSur, NULL, sur, &dest);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
24 SDL_FreeSurface(textSur);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
25 }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
26
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
27 int main(int argc, char *argv[])
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
28 {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
29 int width, height;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
30 SDL_Surface *screen;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
31 TTF_Font *font;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
32
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
33 width = 500, height = 250;
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 SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
36
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
37 if (SDL_Init(SDL_INIT_VIDEO) < 0)
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
38 {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
39 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
40 return -1;
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
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
43 /* Initialize fonts */
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
44 TTF_Init();
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
45
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
46 font = TTF_OpenFont(DEFAULT_FONT, DEFAULT_PTSIZE);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
47 if (! font)
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
48 {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
49 fprintf(stderr, "Failed to find font: %s\n", SDL_GetError());
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
50 exit(-1);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
51 }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
52
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
53 atexit(SDL_Quit);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
54
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
55 /* Create window */
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
56 screen = SDL_SetVideoMode(width, height, 32,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
57 SDL_HWSURFACE | SDL_DOUBLEBUF);
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());
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
62 return -1;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
63 }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
64
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
65 /* Prepare a rect for text input */
3133
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
66 SDL_Rect textRect = { 100, 80, 300, 50 }, markedRect, underlineRect, cursorRect;
3131
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
67 Uint32 backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
3133
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
68 Uint32 lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0);
3131
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
69 SDL_Color textColor = { 0, 0, 0 };
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
70 SDL_FillRect(screen, &textRect, backColor);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
71
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
72 markedRect = textRect;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
73 SDL_StartTextInput(&markedRect);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
74
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
75 SDL_Flip(screen);
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 SDL_Event event;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
78 int done = 0, inputed = 0;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
79 int w, h;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
80 char text[MAX_TEXT_LENGTH];
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
81
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
82 while (! done && SDL_WaitEvent(&event))
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
83 {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
84 switch (event.type)
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
85 {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
86 case SDL_KEYDOWN:
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
87 fprintf(stderr,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
88 "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
89 event.key.which, event.key.keysym.scancode,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
90 SDL_GetScancodeName(event.key.keysym.scancode),
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
91 event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
92 break;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
93
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
94 case SDL_TEXTINPUT:
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
95 fprintf(stderr, "Keyboard %d: text input \"%s\"\n",
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
96 event.text.which, event.text.text);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
97
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
98 if (inputed < sizeof(text))
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
99 {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
100 strcpy(text + inputed, event.text.text);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
101 inputed += strlen(event.text.text);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
102 }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
103
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
104 fprintf(stderr, "text inputed: %s\n", text);
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
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
107 render_text(screen, font, text, textRect.x, textRect.y, textColor);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
108 TTF_SizeUTF8(font, text, &w, &h);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
109 markedRect.x = textRect.x + w;
3133
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
110
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
111 cursorRect = markedRect;
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
112 cursorRect.w = 2;
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
113 cursorRect.h = h;
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
114 SDL_FillRect(screen, &cursorRect, lineColor);
3131
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
115 SDL_Flip(screen);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
116
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
117 SDL_StartTextInput(&markedRect);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
118 break;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
119
3132
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
120 case SDL_TEXTEDITING:
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
121 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
122 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
123
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
124 SDL_FillRect(screen, &markedRect, backColor);
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
125 render_text(screen, font, event.edit.text, markedRect.x, markedRect.y, textColor);
3133
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
126 TTF_SizeUTF8(font, event.edit.text, &w, &h);
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
127 underlineRect = markedRect;
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
128 underlineRect.y += (h - 2);
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
129 underlineRect.h = 2;
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
130 underlineRect.w = w;
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
131 SDL_FillRect(screen, &underlineRect, lineColor);
84119fe89d26 Draw cursor, underline marked text
Jiang Jiang <gzjjgod@gmail.com>
parents: 3132
diff changeset
132
3132
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
133 SDL_Flip(screen);
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
134 break;
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
135
3131
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
136 case SDL_QUIT:
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
137 done = 1;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
138 break;
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 default:
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
141 break;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
142 }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
143 }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
144
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
145 TTF_CloseFont(font);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
146 TTF_Quit();
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
147
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
148 return 0;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
149 }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
150