comparison console/libgnt/gntmain.c @ 14317:c8ddda5d8ec8

[gaim-migrate @ 17010] This should allow mouse-events to work for everyone. Also, you can click on any window to bring it to focus. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Thu, 24 Aug 2006 02:35:15 +0000
parents 8b8188fa98f4
children 985e5e3f6f2c
comparison
equal deleted inserted replaced
14316:b3697123b01a 14317:c8ddda5d8ec8
22 * Need to wattrset for colors to use with PDCurses. 22 * Need to wattrset for colors to use with PDCurses.
23 */ 23 */
24 24
25 static int lock_focus_list; 25 static int lock_focus_list;
26 static GList *focus_list; 26 static GList *focus_list;
27 static GList *ordered;
27 28
28 static int X_MIN; 29 static int X_MIN;
29 static int X_MAX; 30 static int X_MAX;
30 static int Y_MIN; 31 static int Y_MIN;
31 static int Y_MAX; 32 static int Y_MAX;
62 static void draw_taskbar(gboolean reposition); 63 static void draw_taskbar(gboolean reposition);
63 static void bring_on_top(GntWidget *widget); 64 static void bring_on_top(GntWidget *widget);
64 65
65 static gboolean refresh_screen(); 66 static gboolean refresh_screen();
66 67
68 static GList *
69 g_list_bring_to_front(GList *list, gpointer data)
70 {
71 list = g_list_remove(list, data);
72 list = g_list_prepend(list, data);
73 return list;
74 }
75
67 static gboolean 76 static gboolean
68 update_screen(gpointer null) 77 update_screen(gpointer null)
69 { 78 {
70 update_panels(); 79 update_panels();
71 doupdate(); 80 doupdate();
76 { 85 {
77 GntWidget *w = NULL; 86 GntWidget *w = NULL;
78 87
79 if (lock_focus_list) 88 if (lock_focus_list)
80 return; 89 return;
81 if (g_list_find(g_list_first(focus_list), widget)) 90 if (g_list_find(focus_list, widget))
82 return; 91 return;
83 92
84 if (focus_list) 93 if (ordered)
85 w = focus_list->data; 94 w = ordered->data;
86 95
87 /* XXX: ew */
88 focus_list = g_list_first(focus_list);
89 focus_list = g_list_append(focus_list, widget); 96 focus_list = g_list_append(focus_list, widget);
90 focus_list = g_list_find(focus_list, w ? w : widget); 97
98 ordered = g_list_append(ordered, widget);
91 99
92 gnt_widget_set_focus(widget, TRUE); 100 gnt_widget_set_focus(widget, TRUE);
93 if (w) 101 if (w)
94 gnt_widget_set_focus(w, FALSE); 102 gnt_widget_set_focus(w, FALSE);
95 draw_taskbar(FALSE); 103 draw_taskbar(FALSE);
96 } 104 }
97 105
98 void gnt_screen_remove_widget(GntWidget *widget) 106 void gnt_screen_remove_widget(GntWidget *widget)
99 { 107 {
100 int pos = g_list_index(g_list_first(focus_list), widget); 108 int pos = g_list_index(focus_list, widget);
101 GList *next;
102 109
103 if (lock_focus_list) 110 if (lock_focus_list)
104 return; 111 return;
105 112
106 if (pos == -1) 113 if (pos == -1)
107 return; 114 return;
108 115
109 focus_list = g_list_first(focus_list);
110 focus_list = g_list_remove(focus_list, widget); 116 focus_list = g_list_remove(focus_list, widget);
111 next = g_list_nth(focus_list, pos - 1); 117 ordered = g_list_remove(ordered, widget);
112 if (next) 118
113 focus_list = next; 119 if (ordered)
114 120 {
115 if (focus_list) 121 bring_on_top(ordered->data);
116 {
117 bring_on_top(focus_list->data);
118 } 122 }
119 draw_taskbar(FALSE); 123 draw_taskbar(FALSE);
120 } 124 }
121 125
122 static void 126 static void
123 bring_on_top(GntWidget *widget) 127 bring_on_top(GntWidget *widget)
124 { 128 {
125 GntNode *node = g_hash_table_lookup(nodes, widget); 129 GntNode *node = g_hash_table_lookup(nodes, widget);
126 130
127 g_return_if_fail(focus_list->data == widget);
128
129 if (!node) 131 if (!node)
130 return; 132 return;
131 133
132 gnt_widget_set_focus(focus_list->data, TRUE); 134 gnt_widget_set_focus(widget, TRUE);
133 gnt_widget_draw(focus_list->data); 135 gnt_widget_draw(widget);
134
135 top_panel(node->panel); 136 top_panel(node->panel);
136 137
137 if (window_list.window) 138 if (window_list.window)
138 { 139 {
139 GntNode *nd = g_hash_table_lookup(nodes, window_list.window); 140 GntNode *nd = g_hash_table_lookup(nodes, window_list.window);
149 GntTextFormatFlags flag = 0; 150 GntTextFormatFlags flag = 0;
150 151
151 if (window_list.window == NULL) 152 if (window_list.window == NULL)
152 return; 153 return;
153 154
154 if (wid == focus_list->data) 155 if (wid == ordered->data)
155 flag |= GNT_TEXT_FLAG_DIM; 156 flag |= GNT_TEXT_FLAG_DIM;
156 else if (GNT_WIDGET_IS_FLAG_SET(wid, GNT_WIDGET_URGENT)) 157 else if (GNT_WIDGET_IS_FLAG_SET(wid, GNT_WIDGET_URGENT))
157 flag |= GNT_TEXT_FLAG_BOLD; 158 flag |= GNT_TEXT_FLAG_BOLD;
158 159
159 gnt_tree_set_row_flags(GNT_TREE(window_list.tree), wid, flag); 160 gnt_tree_set_row_flags(GNT_TREE(window_list.tree), wid, flag);
177 } 178 }
178 179
179 wbkgdset(taskbar, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL)); 180 wbkgdset(taskbar, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL));
180 werase(taskbar); 181 werase(taskbar);
181 182
182 n = g_list_length(g_list_first(focus_list)); 183 n = g_list_length(focus_list);
183 if (n) 184 if (n)
184 width = getmaxx(stdscr) / n; 185 width = getmaxx(stdscr) / n;
185 186
186 for (i = 0, iter = g_list_first(focus_list); iter; iter = iter->next, i++) 187 for (i = 0, iter = focus_list; iter; iter = iter->next, i++)
187 { 188 {
188 GntWidget *w = iter->data; 189 GntWidget *w = iter->data;
189 int color; 190 int color;
190 const char *title; 191 const char *title;
191 192
192 if (w == focus_list->data) 193 if (w == ordered->data)
193 { 194 {
194 /* This is the current window in focus */ 195 /* This is the current window in focus */
195 color = GNT_COLOR_TITLE; 196 color = GNT_COLOR_TITLE;
196 GNT_WIDGET_UNSET_FLAGS(w, GNT_WIDGET_URGENT); 197 GNT_WIDGET_UNSET_FLAGS(w, GNT_WIDGET_URGENT);
197 } 198 }
216 } 217 }
217 218
218 static void 219 static void
219 switch_window(int direction) 220 switch_window(int direction)
220 { 221 {
221 GntWidget *w = NULL; 222 GntWidget *w = NULL, *wid = NULL;
222 if (focus_list) 223 int pos;
223 w = focus_list->data; 224
224 225 if (!ordered || !ordered->next)
225 if (direction == 1) 226 return;
226 { 227
227 if (focus_list && focus_list->next) 228 w = ordered->data;
228 focus_list = focus_list->next; 229 pos = g_list_index(focus_list, w);
229 else 230 pos += direction;
230 focus_list = g_list_first(focus_list); 231
231 } 232 if (pos < 0)
232 else if (direction == -1) 233 wid = g_list_last(focus_list)->data;
233 { 234 else if (pos >= g_list_length(focus_list))
234 if (focus_list && focus_list->prev) 235 wid = focus_list->data;
235 focus_list = focus_list->prev; 236 else if (pos >= 0)
236 else 237 wid = g_list_nth_data(focus_list, pos);
237 focus_list = g_list_last(focus_list); 238
238 } 239 ordered = g_list_bring_to_front(ordered, wid);
239 240
240 if (focus_list) 241 bring_on_top(ordered->data);
241 { 242
242 bring_on_top(focus_list->data); 243 if (w != wid)
243 }
244
245 if (w && (!focus_list || w != focus_list->data))
246 { 244 {
247 gnt_widget_set_focus(w, FALSE); 245 gnt_widget_set_focus(w, FALSE);
248 } 246 }
249 } 247 }
250 248
252 switch_window_n(int n) 250 switch_window_n(int n)
253 { 251 {
254 GntWidget *w = NULL; 252 GntWidget *w = NULL;
255 GList *l; 253 GList *l;
256 254
257 if (focus_list) 255 if (!ordered)
258 w = focus_list->data; 256 return;
259 257
260 if ((l = g_list_nth(g_list_first(focus_list), n)) != NULL) 258 w = ordered->data;
261 { 259
262 focus_list = l; 260 if ((l = g_list_nth(focus_list, n)) != NULL)
263 bring_on_top(focus_list->data); 261 {
264 } 262 ordered = g_list_bring_to_front(ordered, l->data);
265 263 bring_on_top(ordered->data);
266 if (w && (!focus_list || w != focus_list->data)) 264 }
265
266 if (l && w != l->data)
267 { 267 {
268 gnt_widget_set_focus(w, FALSE); 268 gnt_widget_set_focus(w, FALSE);
269 } 269 }
270 } 270 }
271 271
273 window_list_activate(GntTree *tree, gpointer null) 273 window_list_activate(GntTree *tree, gpointer null)
274 { 274 {
275 GntWidget *widget = gnt_tree_get_selection_data(GNT_TREE(tree)); 275 GntWidget *widget = gnt_tree_get_selection_data(GNT_TREE(tree));
276 GntWidget *old = NULL; 276 GntWidget *old = NULL;
277 277
278 if (focus_list) 278 if (!ordered || !widget)
279 old = focus_list->data; 279 return;
280 280
281 focus_list = g_list_find(g_list_first(focus_list), widget); 281 old = ordered->data;
282 ordered = g_list_bring_to_front(ordered, widget);
282 bring_on_top(widget); 283 bring_on_top(widget);
283 284
284 if (old && (!focus_list || old != focus_list->data)) 285 if (old != widget)
285 { 286 {
286 gnt_widget_set_focus(old, FALSE); 287 gnt_widget_set_focus(old, FALSE);
287 } 288 }
288 } 289 }
289 290
301 gnt_box_set_title(GNT_BOX(win), "Window List"); 302 gnt_box_set_title(GNT_BOX(win), "Window List");
302 gnt_box_set_pad(GNT_BOX(win), 0); 303 gnt_box_set_pad(GNT_BOX(win), 0);
303 304
304 tree = window_list.tree = gnt_tree_new(); 305 tree = window_list.tree = gnt_tree_new();
305 306
306 for (iter = g_list_first(focus_list); iter; iter = iter->next) 307 for (iter = focus_list; iter; iter = iter->next)
307 { 308 {
308 GntBox *box = GNT_BOX(iter->data); 309 GntBox *box = GNT_BOX(iter->data);
309 310
310 gnt_tree_add_row_last(GNT_TREE(tree), box, 311 gnt_tree_add_row_last(GNT_TREE(tree), box,
311 gnt_tree_create_row(GNT_TREE(tree), box->title), NULL); 312 gnt_tree_create_row(GNT_TREE(tree), box->title), NULL);
312 update_window_in_list(GNT_WIDGET(box)); 313 update_window_in_list(GNT_WIDGET(box));
313 } 314 }
314 315
315 gnt_tree_set_selected(GNT_TREE(tree), focus_list->data); 316 gnt_tree_set_selected(GNT_TREE(tree), ordered->data);
316 gnt_box_add_widget(GNT_BOX(win), tree); 317 gnt_box_add_widget(GNT_BOX(win), tree);
317 318
318 gnt_tree_set_col_width(GNT_TREE(tree), 0, getmaxx(stdscr) / 3); 319 gnt_tree_set_col_width(GNT_TREE(tree), 0, getmaxx(stdscr) / 3);
319 gnt_widget_set_size(tree, 0, getmaxy(stdscr) / 2); 320 gnt_widget_set_size(tree, 0, getmaxy(stdscr) / 2);
320 gnt_widget_set_position(win, getmaxx(stdscr) / 3, getmaxy(stdscr) / 4); 321 gnt_widget_set_position(win, getmaxx(stdscr) / 3, getmaxy(stdscr) / 4);
327 } 328 }
328 329
329 static void 330 static void
330 shift_window(GntWidget *widget, int dir) 331 shift_window(GntWidget *widget, int dir)
331 { 332 {
332 GList *all = g_list_first(focus_list); 333 GList *all = focus_list;
333 GList *list = g_list_find(all, widget); 334 GList *list = g_list_find(all, widget);
334 int length, pos; 335 int length, pos;
335 if (!list) 336 if (!list)
336 return; 337 return;
337 338
347 else if (pos > length) 348 else if (pos > length)
348 pos = 0; 349 pos = 0;
349 350
350 all = g_list_insert(all, widget, pos); 351 all = g_list_insert(all, widget, pos);
351 all = g_list_delete_link(all, list); 352 all = g_list_delete_link(all, list);
352 if (focus_list == list) 353 focus_list = all;
353 focus_list = g_list_find(all, widget);
354 draw_taskbar(FALSE); 354 draw_taskbar(FALSE);
355 } 355 }
356 356
357 static void 357 static void
358 dump_screen() 358 dump_screen()
520 x -= 33; 520 x -= 33;
521 y -= 33; 521 y -= 33;
522 522
523 if (strncmp(buffer, "[M ", 3) == 0) { 523 if (strncmp(buffer, "[M ", 3) == 0) {
524 /* left button down */ 524 /* left button down */
525 /* If you clicked on the top-bar of the active window, then you can move it by dragging it */ 525 /* Bring the window you clicked on to front */
526 if (focus_list) { 526 /* If you click on the topbar, then you can drag to move the window */
527 GntWidget *wid = focus_list->data; 527 GList *iter;
528 if (x >= wid->priv.x && x < wid->priv.x + wid->priv.width && 528 for (iter = ordered; iter; iter = iter->next) {
529 y == wid->priv.y) { 529 GntWidget *wid = iter->data;
530 offset = x - wid->priv.x; 530 if (x >= wid->priv.x && x < wid->priv.x + wid->priv.width) {
531 remember = wid; 531 if (y >= wid->priv.y && y < wid->priv.y + wid->priv.height) {
532 button = MOUSE_LEFT; 532 if (iter != ordered) {
533 GntWidget *w = ordered->data;
534 ordered = g_list_bring_to_front(ordered, iter->data);
535 bring_on_top(ordered->data);
536 gnt_widget_set_focus(w, FALSE);
537 }
538 if (y == wid->priv.y) {
539 offset = x - wid->priv.x;
540 remember = wid;
541 button = MOUSE_LEFT;
542 }
543 break;
544 }
533 } 545 }
534 } 546 }
535 } else if (strncmp(buffer, "[M\"", 3) == 0) { 547 } else if (strncmp(buffer, "[M\"", 3) == 0) {
536 /* right button down */ 548 /* right button down */
537 } else if (strncmp(buffer, "[M!", 3) == 0) { 549 } else if (strncmp(buffer, "[M!", 3) == 0) {
541 } else if (strncmp(buffer, "[Ma", 3) == 0) { 553 } else if (strncmp(buffer, "[Ma", 3) == 0) {
542 /* wheel down */ 554 /* wheel down */
543 } else if (strncmp(buffer, "[M#", 3) == 0) { 555 } else if (strncmp(buffer, "[M#", 3) == 0) {
544 /* button up */ 556 /* button up */
545 if (button == MOUSE_NONE && y == getmaxy(stdscr) - 1) { 557 if (button == MOUSE_NONE && y == getmaxy(stdscr) - 1) {
546 int n = g_list_length(g_list_first(focus_list)); 558 int n = g_list_length(focus_list);
547 if (n) { 559 if (n) {
548 int width = getmaxx(stdscr) / n; 560 int width = getmaxx(stdscr) / n;
549 switch_window_n(x / width); 561 switch_window_n(x / width);
550 } 562 }
551 } else if (button == MOUSE_LEFT && remember) { 563 } else if (button == MOUSE_LEFT && remember) {
597 if (mouse_enabled && detect_mouse_action(buffer)) 609 if (mouse_enabled && detect_mouse_action(buffer))
598 return TRUE; 610 return TRUE;
599 611
600 if (mode == GNT_KP_MODE_NORMAL) 612 if (mode == GNT_KP_MODE_NORMAL)
601 { 613 {
602 if (focus_list) 614 if (ordered)
603 { 615 {
604 ret = gnt_widget_key_pressed(focus_list->data, buffer); 616 ret = gnt_widget_key_pressed(ordered->data, buffer);
605 } 617 }
606 618
607 if (!ret) 619 if (!ret)
608 { 620 {
609 if (buffer[0] == 27) 621 if (buffer[0] == 27)
612 if (strcmp(buffer+1, GNT_KEY_POPUP) == 0) 624 if (strcmp(buffer+1, GNT_KEY_POPUP) == 0)
613 {} 625 {}
614 else if (strcmp(buffer + 1, "c") == 0) 626 else if (strcmp(buffer + 1, "c") == 0)
615 { 627 {
616 /* Alt + c was pressed. I am going to use it to close a window. */ 628 /* Alt + c was pressed. I am going to use it to close a window. */
617 if (focus_list) 629 if (ordered)
618 { 630 {
619 gnt_widget_destroy(focus_list->data); 631 gnt_widget_destroy(ordered->data);
620 } 632 }
621 } 633 }
622 else if (strcmp(buffer + 1, "q") == 0) 634 else if (strcmp(buffer + 1, "q") == 0)
623 { 635 {
624 /* I am going to use Alt + q to quit. */ 636 /* I am going to use Alt + q to quit. */
651 mode = GNT_KP_MODE_RESIZE; 663 mode = GNT_KP_MODE_RESIZE;
652 } 664 }
653 else if (strcmp(buffer + 1, ",") == 0 && focus_list) 665 else if (strcmp(buffer + 1, ",") == 0 && focus_list)
654 { 666 {
655 /* Re-order the list of windows */ 667 /* Re-order the list of windows */
656 shift_window(focus_list->data, -1); 668 shift_window(ordered->data, -1);
657 } 669 }
658 else if (strcmp(buffer + 1, ".") == 0 && focus_list) 670 else if (strcmp(buffer + 1, ".") == 0 && focus_list)
659 { 671 {
660 shift_window(focus_list->data, 1); 672 shift_window(ordered->data, 1);
661 } 673 }
662 else if (strcmp(buffer + 1, "l") == 0) 674 else if (strcmp(buffer + 1, "l") == 0)
663 { 675 {
664 refresh_screen(); 676 refresh_screen();
665 } 677 }
679 { 691 {
680 if (buffer[0] == 27) 692 if (buffer[0] == 27)
681 { 693 {
682 gboolean changed = FALSE; 694 gboolean changed = FALSE;
683 int x, y, w, h; 695 int x, y, w, h;
684 GntWidget *widget = GNT_WIDGET(focus_list->data); 696 GntWidget *widget = GNT_WIDGET(ordered->data);
685 697
686 gnt_widget_get_position(widget, &x, &y); 698 gnt_widget_get_position(widget, &x, &y);
687 gnt_widget_get_size(widget, &w, &h); 699 gnt_widget_get_size(widget, &w, &h);
688 700
689 if (strcmp(buffer + 1, GNT_KEY_LEFT) == 0) 701 if (strcmp(buffer + 1, GNT_KEY_LEFT) == 0)
753 { 765 {
754 if (buffer[0] == '\r' || (buffer[0] == 27 && buffer[1] == 0)) 766 if (buffer[0] == '\r' || (buffer[0] == 27 && buffer[1] == 0))
755 mode = GNT_KP_MODE_NORMAL; 767 mode = GNT_KP_MODE_NORMAL;
756 else if (buffer[0] == 27) 768 else if (buffer[0] == 27)
757 { 769 {
758 GntWidget *widget = focus_list->data; 770 GntWidget *widget = ordered->data;
759 gboolean changed = FALSE; 771 gboolean changed = FALSE;
760 int width, height; 772 int width, height;
761 773
762 gnt_widget_get_size(widget, &width, &height); 774 gnt_widget_get_size(widget, &width, &height);
763 775
883 nodes = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free_node); 895 nodes = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free_node);
884 896
885 wbkgdset(stdscr, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL)); 897 wbkgdset(stdscr, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL));
886 refresh(); 898 refresh();
887 899
888 #ifdef NCURSES_BUTTON_PRESSEDaa 900 #ifdef ALL_MOUSE_EVENTS
889 if ((mouse_enabled = gnt_style_get_bool(GNT_STYLE_MOUSE, FALSE))) 901 if ((mouse_enabled = gnt_style_get_bool(GNT_STYLE_MOUSE, FALSE)))
890 mousemask(NCURSES_BUTTON_PRESSED | NCURSES_BUTTON_RELEASED | REPORT_MOUSE_POSITION, NULL); 902 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
891 #endif 903 #endif
892 904
893 wbkgdset(stdscr, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL)); 905 wbkgdset(stdscr, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL));
894 werase(stdscr); 906 werase(stdscr);
895 wrefresh(stdscr); 907 wrefresh(stdscr);
1012 widget = widget->parent; 1024 widget = widget->parent;
1013 1025
1014 if (widget == window_list.window) 1026 if (widget == window_list.window)
1015 return TRUE; 1027 return TRUE;
1016 1028
1017 if (focus_list && focus_list->data == widget) 1029 if (ordered && ordered->data == widget)
1018 { 1030 {
1019 if (GNT_IS_BOX(widget) && 1031 if (GNT_IS_BOX(widget) &&
1020 (GNT_BOX(widget)->active == w || widget == w)) 1032 (GNT_BOX(widget)->active == w || widget == w))
1021 return TRUE; 1033 return TRUE;
1022 } 1034 }
1027 void gnt_widget_set_urgent(GntWidget *widget) 1039 void gnt_widget_set_urgent(GntWidget *widget)
1028 { 1040 {
1029 while (widget->parent) 1041 while (widget->parent)
1030 widget = widget->parent; 1042 widget = widget->parent;
1031 1043
1032 if (focus_list && focus_list->data == widget) 1044 if (ordered && ordered->data == widget)
1033 return; 1045 return;
1034 1046
1035 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_URGENT); 1047 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_URGENT);
1036 draw_taskbar(FALSE); 1048 draw_taskbar(FALSE);
1037 } 1049 }