comparison console/libgnt/gntmain.c @ 13907:cc60d0861337

[gaim-migrate @ 16402] This commit has 1234 lines of diff :) Windows can now be moved (alt+m, then the arrow keys, then escape/enter). Add a window to enable/disable accounts. But the 'add' etc. buttons don't have any callbacks yet. I am going to need to do some more widgets (checkbox, combobox) before I do anything else. I have also updated the test programs to work with the changes in libgnt. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Sun, 02 Jul 2006 22:13:06 +0000
parents eaaf73de9188
children b210409cdc56
comparison
equal deleted inserted replaced
13906:b986b6e2441b 13907:cc60d0861337
8 #include <locale.h> 8 #include <locale.h>
9 #include <unistd.h> 9 #include <unistd.h>
10 #include <string.h> 10 #include <string.h>
11 11
12 static GList *focus_list; 12 static GList *focus_list;
13 static int max_x; 13 static int X_MIN;
14 static int max_y; 14 static int X_MAX;
15 static int Y_MIN;
16 static int Y_MAX;
17
18 static GMainLoop *loop;
15 19
16 typedef struct 20 typedef struct
17 { 21 {
18 GntWidget *me; 22 GntWidget *me;
19 GList *below; /* List of widgets below me */ 23 GList *below; /* List of widgets below me */
20 GList *above; /* List of widgets above me */ 24 GList *above; /* List of widgets above me */
21 } GntNode; 25 } GntNode;
22 26
27 typedef enum
28 {
29 GNT_KP_MODE_NORMAL,
30 GNT_KP_MODE_RESIZE,
31 GNT_KP_MODE_MOVE,
32 GNT_KP_MODE_MENU,
33 } GntKeyPressMode;
34
23 static GHashTable *nodes; 35 static GHashTable *nodes;
24 36
25 static void free_node(gpointer data); 37 static void free_node(gpointer data);
26 static void draw_taskbar(); 38 static void draw_taskbar();
27 39
28 void gnt_screen_take_focus(GntWidget *widget) 40 void gnt_screen_take_focus(GntWidget *widget)
29 { 41 {
30 GntWidget *w = NULL; 42 GntWidget *w = NULL;
43
31 if (focus_list) 44 if (focus_list)
32 w = focus_list->data; 45 w = focus_list->data;
33 focus_list = g_list_prepend(focus_list, widget); 46
47 /* XXX: ew */
48 focus_list = g_list_first(focus_list);
49 focus_list = g_list_append(focus_list, widget);
50 focus_list = g_list_find(focus_list, widget);
51
34 gnt_widget_set_focus(widget, TRUE); 52 gnt_widget_set_focus(widget, TRUE);
35 if (w) 53 if (w)
36 gnt_widget_set_focus(w, FALSE); 54 gnt_widget_set_focus(w, FALSE);
37 draw_taskbar(); 55 draw_taskbar();
38 } 56 }
39 57
40 void gnt_screen_remove_widget(GntWidget *widget) 58 void gnt_screen_remove_widget(GntWidget *widget)
41 { 59 {
60 int pos = g_list_index(g_list_first(focus_list), widget);
61 GList *next;
62
63 if (pos == -1)
64 return;
65
66 focus_list = g_list_first(focus_list);
42 focus_list = g_list_remove(focus_list, widget); 67 focus_list = g_list_remove(focus_list, widget);
68 next = g_list_nth(focus_list, pos - 1);
69 if (next)
70 focus_list = next;
71
43 if (focus_list) 72 if (focus_list)
44 { 73 {
45 gnt_widget_set_focus(focus_list->data, TRUE); 74 gnt_widget_set_focus(focus_list->data, TRUE);
46 gnt_widget_draw(focus_list->data); 75 gnt_widget_draw(focus_list->data);
47 } 76 }
80 if (taskbar == NULL) 109 if (taskbar == NULL)
81 { 110 {
82 taskbar = newwin(1, getmaxx(stdscr), getmaxy(stdscr) - 1, 0); 111 taskbar = newwin(1, getmaxx(stdscr), getmaxy(stdscr) - 1, 0);
83 } 112 }
84 113
114 wbkgdset(taskbar, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL));
85 werase(taskbar); 115 werase(taskbar);
86 116
87 n = g_list_length(g_list_first(focus_list)); 117 n = g_list_length(g_list_first(focus_list));
88 if (n) 118 if (n)
89 width = getmaxx(stdscr) / n; 119 width = getmaxx(stdscr) / n;
114 } 144 }
115 145
116 wrefresh(taskbar); 146 wrefresh(taskbar);
117 } 147 }
118 148
149 static void
150 switch_window(int direction)
151 {
152 GntWidget *w = NULL;
153 if (focus_list)
154 w = focus_list->data;
155
156 if (direction == 1)
157 {
158 if (focus_list && focus_list->next)
159 focus_list = focus_list->next;
160 else
161 focus_list = g_list_first(focus_list);
162 }
163 else if (direction == -1)
164 {
165 if (focus_list && focus_list->prev)
166 focus_list = focus_list->prev;
167 else
168 focus_list = g_list_last(focus_list);
169 }
170
171 if (focus_list)
172 {
173 gnt_widget_set_focus(focus_list->data, TRUE);
174 bring_on_top(focus_list->data);
175 gnt_widget_draw(focus_list->data);
176 }
177
178 if (w && (!focus_list || w != focus_list->data))
179 gnt_widget_set_focus(w, FALSE);
180 }
181
119 static gboolean 182 static gboolean
120 io_invoke(GIOChannel *source, GIOCondition cond, gpointer null) 183 io_invoke(GIOChannel *source, GIOCondition cond, gpointer null)
121 { 184 {
122 char buffer[256]; 185 char buffer[256];
186 static GntKeyPressMode mode = GNT_KP_MODE_NORMAL;
187 gboolean ret = FALSE;
123 188
124 int rd = read(0, buffer, sizeof(buffer) - 1); 189 int rd = read(0, buffer, sizeof(buffer) - 1);
125 if (rd < 0) 190 if (rd < 0)
126 { 191 {
127 endwin(); 192 endwin();
135 exit(1); 200 exit(1);
136 } 201 }
137 202
138 buffer[rd] = 0; 203 buffer[rd] = 0;
139 204
140 if (focus_list) 205 if (mode == GNT_KP_MODE_NORMAL)
141 { 206 {
142 gboolean ret = FALSE; 207 if (focus_list)
143 ret = gnt_widget_key_pressed(focus_list->data, buffer); 208 {
144 } 209 ret = gnt_widget_key_pressed(focus_list->data, buffer);
145 210 }
146 if (buffer[0] == 27) 211
147 { 212 if (!ret)
148 /* Some special key has been pressed */ 213 {
149 if (strcmp(buffer+1, GNT_KEY_POPUP) == 0) 214 if (buffer[0] == 27)
150 {} 215 {
151 else if (strcmp(buffer + 1, "c") == 0) 216 /* Some special key has been pressed */
152 { 217 if (strcmp(buffer+1, GNT_KEY_POPUP) == 0)
153 /* Alt + c was pressed. I am going to use it to close a window. */ 218 {}
154 if (focus_list) 219 else if (strcmp(buffer + 1, "c") == 0)
155 { 220 {
156 gnt_widget_destroy(focus_list->data); 221 /* Alt + c was pressed. I am going to use it to close a window. */
157 gnt_screen_remove_widget(focus_list->data); 222 if (focus_list)
158 } 223 {
159 } 224 gnt_widget_destroy(focus_list->data);
160 else if (strcmp(buffer + 1, "q") == 0) 225 }
161 { 226 }
162 /* I am going to use Alt + q to quit. */ 227 else if (strcmp(buffer + 1, "q") == 0)
163 endwin(); 228 {
164 exit(1); 229 /* I am going to use Alt + q to quit. */
165 } 230 g_main_loop_quit(loop);
166 else if (strcmp(buffer + 1, "n") == 0) 231 }
167 { 232 else if (strcmp(buffer + 1, "n") == 0)
168 /* Alt + n to go to the next window */ 233 {
169 GntWidget *w = NULL; 234 /* Alt + n to go to the next window */
170 if (focus_list) 235 switch_window(1);
171 w = focus_list->data; 236 }
172 237 else if (strcmp(buffer + 1, "p") == 0)
173 if (focus_list && focus_list->next) 238 {
174 focus_list = focus_list->next; 239 /* Alt + p to go to the previous window */
175 else 240 switch_window(-1);
176 focus_list = g_list_first(focus_list); 241 }
177 if (focus_list) 242 else if (strcmp(buffer + 1, "m") == 0 && focus_list)
178 { 243 {
179 gnt_widget_set_focus(focus_list->data, TRUE); 244 mode = GNT_KP_MODE_MOVE;
180 bring_on_top(focus_list->data); 245 }
181 gnt_widget_draw(focus_list->data); 246 }
182 } 247 }
183 248 }
184 if (w && w != focus_list->data) 249 else if (mode == GNT_KP_MODE_MOVE && focus_list)
185 gnt_widget_set_focus(w, FALSE); 250 {
251 if (buffer[0] == 27)
252 {
253 gboolean changed = FALSE;
254 int x, y, w, h;
255 GntWidget *widget = GNT_WIDGET(focus_list->data);
256
257 gnt_widget_get_position(widget, &x, &y);
258 gnt_widget_get_size(widget, &w, &h);
259
260 if (strcmp(buffer + 1, GNT_KEY_LEFT) == 0)
261 {
262 if (x > X_MIN)
263 {
264 x--;
265 changed = TRUE;
266 }
267 }
268 else if (strcmp(buffer + 1, GNT_KEY_RIGHT) == 0)
269 {
270 if (x + w < X_MAX)
271 {
272 x++;
273 changed = TRUE;
274 }
275 }
276 else if (strcmp(buffer + 1, GNT_KEY_UP) == 0)
277 {
278 if (y > Y_MIN)
279 {
280 y--;
281 changed = TRUE;
282 }
283 }
284 else if (strcmp(buffer + 1, GNT_KEY_DOWN) == 0)
285 {
286 if (y + h < Y_MAX)
287 {
288 y++;
289 changed = TRUE;
290 }
291 }
292 else if (buffer[1] == 0)
293 {
294 mode = GNT_KP_MODE_NORMAL;
295 changed = TRUE;
296 }
297
298 if (changed)
299 {
300 gnt_widget_hide(widget);
301 gnt_widget_set_position(widget, x, y);
302 gnt_widget_show(widget);
303 }
304 }
305 else if (*buffer == '\r')
306 {
307 mode = GNT_KP_MODE_NORMAL;
186 } 308 }
187 } 309 }
188 310
189 draw_taskbar(); 311 draw_taskbar();
190 refresh(); 312 refresh();
207 setlocale(LC_ALL, ""); 329 setlocale(LC_ALL, "");
208 initscr(); 330 initscr();
209 start_color(); 331 start_color();
210 gnt_init_colors(); 332 gnt_init_colors();
211 333
212 max_x = getmaxx(stdscr); 334 X_MIN = 0;
213 max_y = getmaxy(stdscr); 335 Y_MIN = 0;
336 X_MAX = getmaxx(stdscr);
337 Y_MAX = getmaxy(stdscr) - 1;
214 338
215 nodes = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free_node); 339 nodes = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free_node);
216 340
217 wbkgdset(stdscr, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL)); 341 wbkgdset(stdscr, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL));
218 noecho(); 342 noecho();
219 refresh(); 343 refresh();
220 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL); 344 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
345 wbkgdset(stdscr, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL));
346 werase(stdscr);
347 wrefresh(stdscr);
348
221 g_type_init(); 349 g_type_init();
222 } 350 }
223 351
224 void gnt_main() 352 void gnt_main()
225 { 353 {
226 GMainLoop *loop = g_main_new(FALSE); 354 loop = g_main_loop_new(NULL, FALSE);
227 g_main_run(loop); 355 g_main_loop_run(loop);
228 } 356 }
229 357
230 /********************************* 358 /*********************************
231 * Stuff for 'window management' * 359 * Stuff for 'window management' *
232 *********************************/ 360 *********************************/
288 void gnt_screen_release(GntWidget *widget) 416 void gnt_screen_release(GntWidget *widget)
289 { 417 {
290 WINDOW *win; 418 WINDOW *win;
291 GList *iter; 419 GList *iter;
292 GntNode *node = g_hash_table_lookup(nodes, widget); 420 GntNode *node = g_hash_table_lookup(nodes, widget);
421
422 gnt_screen_remove_widget(widget);
423
293 if (node == NULL) /* Yay! Nothing to do. */ 424 if (node == NULL) /* Yay! Nothing to do. */
294 return; 425 return;
295 426
296 win = dupwin(widget->window); 427 win = dupwin(widget->window);
297 werase(win); 428 werase(win);
382 513
383 w = widget; 514 w = widget;
384 515
385 while (widget->parent) 516 while (widget->parent)
386 { 517 {
387 fprintf(stderr, "%p %p\n", widget, widget->parent);
388 widget = widget->parent; 518 widget = widget->parent;
389 } 519 }
390 fprintf(stderr, "%p %p\n", widget, widget->parent); 520
391 521 if (focus_list && focus_list->data == widget)
392 if (focus_list && focus_list->data == widget && 522 {
393 (!GNT_WIDGET_IS_FLAG_SET(w, GNT_WIDGET_CAN_TAKE_FOCUS) || 523 if (GNT_IS_BOX(widget) &&
394 GNT_WIDGET_IS_FLAG_SET(w, GNT_WIDGET_HAS_FOCUS))) 524 (GNT_BOX(widget)->active == w || widget == w))
395 return TRUE; 525 return TRUE;
526 }
396 527
397 return FALSE; 528 return FALSE;
398 } 529 }
399 530
400 void gnt_widget_set_urgent(GntWidget *widget) 531 void gnt_widget_set_urgent(GntWidget *widget)
407 538
408 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_URGENT); 539 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_URGENT);
409 draw_taskbar(); 540 draw_taskbar();
410 } 541 }
411 542
543 void gnt_quit()
544 {
545 endwin();
546 }
547