initial import for sdlhaa & sdlhim
[sdlhildon] / sdlhim / test / test.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4
5 #include <SDL.h>
6 #include <SDL_him.h>
7 #include <SDL_ttf.h>
8
9 static SDL_Surface *screen;
10 static TTF_Font* font;
11 static char text[100] = { '\0' };
12 static char editing[100] = { '\0' };
13 static char temp[100];
14 static int cursor = 0;
15 static SDL_Color back_color = { 255, 255, 255 };
16 static SDL_Color text_color = { 0, 0, 0 };
17 static SDL_Color editing_color = { 125, 125, 125 };
18 unsigned int him_flags = HIM_MODE_FULL | HIM_MODE_MULTILINE | HIM_MODE_AUTOCAP | HIM_MODE_DICTIONARY;
19
20 #define HIM_EVENT (SDL_USEREVENT + 1)
21
22 static void draw()
23 {
24         SDL_Surface *text_surface;
25         char in_cursor;
26         int res;
27         unsigned int cursor_x, cursor_h, after_x;
28
29         SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,
30                 back_color.r, back_color.g, back_color.b));
31
32         cursor_h = 22; /* Sane default */
33
34         /* Render everything before cursor */
35         in_cursor =  text[cursor];
36         text[cursor] = '\0';
37         if (text[0]) {
38                 text_surface = TTF_RenderUTF8_Shaded(font, text, text_color, back_color);
39                 assert(text_surface != NULL);
40                 res = SDL_BlitSurface(text_surface, NULL, screen, NULL);
41                 assert(res == 0);
42                 cursor_x = text_surface->w;
43                 cursor_h = text_surface->h;
44                 SDL_FreeSurface(text_surface);
45         } else {
46                 cursor_x = 0;
47         }
48         text[cursor] = in_cursor;
49         
50         /* Render editing text, if any */
51         if (editing[0]) {
52                 SDL_Rect rect = {cursor_x, 0, 0, 0};
53                 text_surface = TTF_RenderUTF8_Shaded(font, editing, editing_color, back_color);
54                 assert(text_surface != NULL);
55                 res = SDL_BlitSurface(text_surface, NULL, screen, &rect);
56                 assert(res == 0);
57                 after_x = cursor_x + text_surface->w;
58                 cursor_h = text_surface->h;
59                 SDL_FreeSurface(text_surface);
60         } else {
61                 after_x = cursor_x;
62         }
63         
64         /* Render everything after the cursor */
65         if (in_cursor) {
66                 SDL_Rect rect = {after_x, 0, 0, 0};
67                 text_surface = TTF_RenderUTF8_Shaded(font, &text[cursor], text_color, back_color);
68                 assert(text_surface != NULL);
69                 res = SDL_BlitSurface(text_surface, NULL, screen, &rect);
70                 assert(res == 0);
71                 cursor_h = text_surface->h;
72                 SDL_FreeSurface(text_surface);
73         }
74
75         /* Draw the cursor */
76         {
77                 SDL_Rect rect = {cursor_x, 0, 4, cursor_h};
78                 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format,
79                         text_color.r, text_color.g, text_color.b));
80         }
81
82         SDL_Flip(screen);
83 }
84
85 static void toggle_fullscreen()
86 {
87         unsigned int flags;
88         unsigned short x, y;
89         if (screen->flags & SDL_FULLSCREEN) {
90                 flags = screen->flags & ~SDL_FULLSCREEN;
91                 x = 720; y = 420;
92         } else {
93                 flags = screen->flags | SDL_FULLSCREEN;
94                 x = 800; y = 480;
95         }
96
97         HIM_Disable();
98         screen = SDL_SetVideoMode(x, y, 16, flags);
99         HIM_Enable(him_flags);
100
101         draw();
102 }
103
104 static void handle_special_key(SDLKey key) {
105         switch(key) {
106                 case SDLK_BACKSPACE:
107                         if (cursor > 0) {
108                                 cursor--;
109                                 strcpy(&text[cursor], &text[cursor+1]);
110                         }
111                         draw();
112                         break;
113                 case SDLK_SPACE:
114                         text[cursor++] = ' ';
115                         break;
116                 case SDLK_LEFT:
117                         if (cursor > 0) {
118                                 cursor--;
119                                 draw();
120                         }
121                         break;
122                 case SDLK_RIGHT:
123                         if (cursor < strlen(text)) {
124                                 cursor++;
125                                 draw();
126                         }
127                         break;  
128                 case SDLK_F6:
129                         toggle_fullscreen();
130                         break;
131                 default:
132                         /* blah */
133                         break;
134         }
135 }
136
137 int main() 
138 {
139         int res;
140         res = SDL_Init(SDL_INIT_VIDEO);
141         assert(res == 0);
142
143         screen = SDL_SetVideoMode(720, 420, 16, SDL_SWSURFACE | SDL_RESIZABLE);
144         assert(screen != NULL);
145
146         res = HIM_Init(0, HIM_EVENT);
147         assert(res == 0);
148
149         res = TTF_Init();
150         assert(res == 0);
151
152         font = TTF_OpenFont("/usr/share/fonts/nokia/nosnr.ttf", 26);
153         assert(font != NULL);
154
155         draw();
156
157         HIM_Enable(him_flags);
158
159         SDL_Event event;
160         while (SDL_WaitEvent(&event)) {
161                 if (HIM_FilterEvent(&event)) continue;
162                 switch (event.type) {
163                         case SDL_QUIT:
164                                 goto quit;
165                         case SDL_VIDEOEXPOSE:
166                                 draw();
167                                 break;
168                         case SDL_VIDEORESIZE:
169                                 printf("New screen size %dx%d\n", event.resize.w, event.resize.h);
170                                 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 16, screen->flags);
171                                 draw();
172                                 break;
173                         case SDL_MOUSEBUTTONDOWN:
174                                 HIM_ShowKeyboard(HIM_TRIGGER_FINGER);
175                                 break;
176                         case SDL_KEYDOWN:
177                                 printf("Key Down: %s (unicode=%x)\n",
178                                         SDL_GetKeyName(event.key.keysym.sym),
179                                         event.key.keysym.unicode);
180                                 handle_special_key(event.key.keysym.sym);
181                                 break;
182                         case SDL_KEYUP:
183                                 printf("Key Up: %s\n", SDL_GetKeyName(event.key.keysym.sym));
184                                 break;
185                         case HIM_EVENT:
186                                 switch (((HIM_TextEvent*)&event)->code) {
187                                         case HIM_TEXTINPUTEVENT:
188                                                 printf("Text Input Event: %s\n", 
189                                                         ((HIM_TextInputEvent*)&event)->text);
190                                                 strcpy(temp, &text[cursor]);
191                                                 strcpy(&text[cursor], ((HIM_TextInputEvent*)&event)->text);
192                                                 strcat(&text[cursor], temp);
193                                                 cursor+=strlen(((HIM_TextInputEvent*)&event)->text);
194                                                 editing[0] = '\0'; /* On input, clear the preedit area. */
195                                                 draw();
196                                                 break;
197                                         case HIM_TEXTEDITINGEVENT:
198                                                 printf("Text Editing Event: %s\n", 
199                                                         ((HIM_TextEditingEvent*)&event)->text);
200                                                 strcpy(editing, ((HIM_TextEditingEvent*)&event)->text);
201                                                 draw();
202                                                 break;
203                                         case HIM_SPECIALKEYEVENT:
204                                                 printf("Special Key: %s\n", SDL_GetKeyName(
205                                                         ((HIM_SpecialKeyEvent*)&event)->sym));
206                                                 handle_special_key(((HIM_SpecialKeyEvent*)&event)->sym);
207                                                 break;
208                                         case HIM_CURSORMOVEEVENT:
209                                                 printf("Cursor Move (%s): %d\n", 
210                                                         ((HIM_CursorMoveEvent*)&event)->relative ? "relative" : "absolute",
211                                                         ((HIM_CursorMoveEvent*)&event)->offset);
212                                                 if (((HIM_CursorMoveEvent*)&event)->relative) {
213                                                         cursor += ((HIM_CursorMoveEvent*)&event)->offset;
214                                                 }
215                                                 draw();
216                                                 break;
217                                         case HIM_REQUESTSURROUNDINGEVENT:
218                                                 printf("Surrounding Request\n");
219                                                 HIM_SendSurrounding(text, cursor);
220                                                 break;
221                                         case HIM_CLIPBOARDEVENT:
222                                                 switch (((HIM_ClipboardEvent*)&event)->action) {
223                                                         case HIM_CLIPBOARD_CUT:
224                                                                 printf("Clipboard Cut\n");
225                                                                 break;
226                                                         case HIM_CLIPBOARD_COPY:
227                                                                 printf("Clipboard Copy\n");
228                                                                 break;
229                                                         case HIM_CLIPBOARD_PASTE:
230                                                                 printf("Clipboard Paste\n");
231                                                                 break;
232                                                         case HIM_CLIPBOARD_REQUEST_SELECTION:
233                                                                 printf("Clipboard Request Selection\n");
234                                                                 HIM_SendSelection("Selected");
235                                                                 break;                                                                                          
236                                                 }
237                                                 break;
238                                 }
239                                 break;
240                 }
241         }
242         
243 quit:
244         HIM_Quit();
245         TTF_CloseFont(font);
246         TTF_Quit();
247         SDL_Quit();
248         return 0;
249 }