Mercurial > pidgin
annotate finch/libgnt/gntwm.c @ 15979:2c81ebc7bf0b
Add a way to get a list of bindings for a widget. This can be used by, eg, a window-manager to show helpful messages to the user.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Wed, 04 Apr 2007 03:47:13 +0000 |
parents | 2a82bc8d57f7 |
children | bc2dd3358d46 |
rev | line source |
---|---|
15817 | 1 #define _GNU_SOURCE |
2 #if defined(__APPLE__) | |
3 #define _XOPEN_SOURCE_EXTENDED | |
4 #endif | |
5 | |
6 #include "config.h" | |
7 | |
8 #include <ctype.h> | |
9 #include <stdlib.h> | |
10 #include <string.h> | |
11 #include <time.h> | |
12 | |
13 #include "gntwm.h" | |
14 #include "gntstyle.h" | |
15 #include "gntmarshal.h" | |
16 #include "gnt.h" | |
17 #include "gntbox.h" | |
18 #include "gntmenu.h" | |
19 #include "gnttextview.h" | |
20 #include "gnttree.h" | |
21 #include "gntutils.h" | |
22 | |
23 #define IDLE_CHECK_INTERVAL 5 /* 5 seconds */ | |
24 | |
25 enum | |
26 { | |
27 SIG_NEW_WIN, | |
28 SIG_DECORATE_WIN, | |
29 SIG_CLOSE_WIN, | |
30 SIG_CONFIRM_RESIZE, | |
31 SIG_RESIZED, | |
32 SIG_CONFIRM_MOVE, | |
33 SIG_MOVED, | |
34 SIG_UPDATE_WIN, | |
35 SIG_GIVE_FOCUS, | |
36 SIG_KEY_PRESS, | |
37 SIG_MOUSE_CLICK, | |
38 SIGS | |
39 }; | |
40 | |
41 static guint signals[SIGS] = { 0 }; | |
42 static void gnt_wm_new_window_real(GntWM *wm, GntWidget *widget); | |
43 static void gnt_wm_win_resized(GntWM *wm, GntNode *node); | |
44 static void gnt_wm_win_moved(GntWM *wm, GntNode *node); | |
45 static void gnt_wm_give_focus(GntWM *wm, GntWidget *widget); | |
46 static void update_window_in_list(GntWM *wm, GntWidget *wid); | |
47 static void shift_window(GntWM *wm, GntWidget *widget, int dir); | |
48 | |
49 static gboolean write_already(gpointer data); | |
50 static int write_timeout; | |
51 static time_t last_active_time; | |
52 static gboolean idle_update; | |
53 | |
54 static GList * | |
55 g_list_bring_to_front(GList *list, gpointer data) | |
56 { | |
57 list = g_list_remove(list, data); | |
58 list = g_list_prepend(list, data); | |
59 return list; | |
60 } | |
61 | |
62 static void | |
63 free_node(gpointer data) | |
64 { | |
65 GntNode *node = data; | |
66 hide_panel(node->panel); | |
67 del_panel(node->panel); | |
68 g_free(node); | |
69 } | |
70 | |
71 static void | |
72 draw_taskbar(GntWM *wm, gboolean reposition) | |
73 { | |
74 static WINDOW *taskbar = NULL; | |
75 GList *iter; | |
76 int n, width = 0; | |
77 int i; | |
78 | |
79 if (taskbar == NULL) { | |
80 taskbar = newwin(1, getmaxx(stdscr), getmaxy(stdscr) - 1, 0); | |
81 } else if (reposition) { | |
82 int Y_MAX = getmaxy(stdscr) - 1; | |
83 mvwin(taskbar, Y_MAX, 0); | |
84 } | |
85 | |
86 wbkgdset(taskbar, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL)); | |
87 werase(taskbar); | |
88 | |
89 n = g_list_length(wm->list); | |
90 if (n) | |
91 width = getmaxx(stdscr) / n; | |
92 | |
93 for (i = 0, iter = wm->list; iter; iter = iter->next, i++) | |
94 { | |
95 GntWidget *w = iter->data; | |
96 int color; | |
97 const char *title; | |
98 | |
99 if (w == wm->ordered->data) { | |
100 /* This is the current window in focus */ | |
101 color = GNT_COLOR_TITLE; | |
102 } else if (GNT_WIDGET_IS_FLAG_SET(w, GNT_WIDGET_URGENT)) { | |
103 /* This is a window with the URGENT hint set */ | |
104 color = GNT_COLOR_URGENT; | |
105 } else { | |
106 color = GNT_COLOR_NORMAL; | |
107 } | |
108 wbkgdset(taskbar, '\0' | COLOR_PAIR(color)); | |
109 if (iter->next) | |
110 mvwhline(taskbar, 0, width * i, ' ' | COLOR_PAIR(color), width); | |
111 else | |
112 mvwhline(taskbar, 0, width * i, ' ' | COLOR_PAIR(color), getmaxx(stdscr) - width * i); | |
113 title = GNT_BOX(w)->title; | |
114 mvwprintw(taskbar, 0, width * i, "%s", title ? title : "<gnt>"); | |
115 if (i) | |
116 mvwaddch(taskbar, 0, width *i - 1, ACS_VLINE | A_STANDOUT | COLOR_PAIR(GNT_COLOR_NORMAL)); | |
117 | |
118 update_window_in_list(wm, w); | |
119 } | |
120 | |
121 wrefresh(taskbar); | |
122 } | |
123 | |
124 static void | |
125 copy_win(GntWidget *widget, GntNode *node) | |
126 { | |
127 WINDOW *src, *dst; | |
128 int shadow; | |
129 if (!node) | |
130 return; | |
131 src = widget->window; | |
132 dst = node->window; | |
133 shadow = gnt_widget_has_shadow(widget) ? 1 : 0; | |
134 copywin(src, dst, node->scroll, 0, 0, 0, getmaxy(dst) - 1, getmaxx(dst) - 1, 0); | |
135 } | |
136 | |
137 static gboolean | |
138 update_screen(GntWM *wm) | |
139 { | |
140 if (wm->menu) { | |
141 GntMenu *top = wm->menu; | |
142 while (top) { | |
143 GntNode *node = g_hash_table_lookup(wm->nodes, top); | |
144 if (node) | |
145 top_panel(node->panel); | |
146 top = top->submenu; | |
147 } | |
148 } | |
149 update_panels(); | |
150 doupdate(); | |
151 return TRUE; | |
152 } | |
153 | |
154 static gboolean | |
155 sanitize_position(GntWidget *widget, int *x, int *y) | |
156 { | |
157 int X_MAX = getmaxx(stdscr); | |
158 int Y_MAX = getmaxy(stdscr) - 1; | |
159 int w, h; | |
160 int nx, ny; | |
161 gboolean changed = FALSE; | |
162 | |
163 gnt_widget_get_size(widget, &w, &h); | |
164 if (x) { | |
165 if (*x + w > X_MAX) { | |
166 nx = MAX(0, X_MAX - w); | |
167 if (nx != *x) { | |
168 *x = nx; | |
169 changed = TRUE; | |
170 } | |
171 } | |
172 } | |
173 if (y) { | |
174 if (*y + h > Y_MAX) { | |
175 ny = MAX(0, Y_MAX - h); | |
176 if (ny != *y) { | |
177 *y = ny; | |
178 changed = TRUE; | |
179 } | |
180 } | |
181 } | |
182 return changed; | |
183 } | |
184 | |
185 static void | |
186 refresh_node(GntWidget *widget, GntNode *node, gpointer null) | |
187 { | |
188 int x, y, w, h; | |
189 int nw, nh; | |
190 | |
191 int X_MAX = getmaxx(stdscr); | |
192 int Y_MAX = getmaxy(stdscr) - 1; | |
193 | |
194 gnt_widget_get_position(widget, &x, &y); | |
195 gnt_widget_get_size(widget, &w, &h); | |
196 | |
197 if (sanitize_position(widget, &x, &y)) | |
198 gnt_screen_move_widget(widget, x, y); | |
199 | |
200 nw = MIN(w, X_MAX); | |
201 nh = MIN(h, Y_MAX); | |
202 if (nw != w || nh != h) | |
203 gnt_screen_resize_widget(widget, nw, nh); | |
204 } | |
205 | |
206 static void | |
207 read_window_positions(GntWM *wm) | |
208 { | |
209 #if GLIB_CHECK_VERSION(2,6,0) | |
210 GKeyFile *gfile = g_key_file_new(); | |
211 char *filename = g_build_filename(g_get_home_dir(), ".gntpositions", NULL); | |
212 GError *error = NULL; | |
213 char **keys; | |
214 gsize nk; | |
215 | |
216 if (!g_key_file_load_from_file(gfile, filename, G_KEY_FILE_NONE, &error)) { | |
217 g_printerr("GntWM: %s\n", error->message); | |
218 g_error_free(error); | |
219 g_free(filename); | |
220 return; | |
221 } | |
222 | |
223 keys = g_key_file_get_keys(gfile, "positions", &nk, &error); | |
224 if (error) { | |
225 g_printerr("GntWM: %s\n", error->message); | |
226 g_error_free(error); | |
227 error = NULL; | |
228 } else { | |
229 while (nk--) { | |
230 char *title = keys[nk]; | |
231 gsize l; | |
232 char **coords = g_key_file_get_string_list(gfile, "positions", title, &l, NULL); | |
233 if (l == 2) { | |
234 int x = atoi(coords[0]); | |
235 int y = atoi(coords[1]); | |
236 GntPosition *p = g_new0(GntPosition, 1); | |
237 p->x = x; | |
238 p->y = y; | |
239 g_hash_table_replace(wm->positions, g_strdup(title + 1), p); | |
240 } else { | |
241 g_printerr("GntWM: Invalid number of arguments for positioing a window.\n"); | |
242 } | |
243 g_strfreev(coords); | |
244 } | |
245 g_strfreev(keys); | |
246 } | |
247 | |
248 g_free(filename); | |
15964 | 249 g_key_file_free(gfile); |
15817 | 250 #endif |
251 } | |
252 | |
253 static gboolean check_idle(gpointer n) | |
254 { | |
255 if (idle_update) { | |
256 time(&last_active_time); | |
257 idle_update = FALSE; | |
258 } | |
259 return TRUE; | |
260 } | |
261 | |
262 static void | |
263 gnt_wm_init(GTypeInstance *instance, gpointer class) | |
264 { | |
265 GntWM *wm = GNT_WM(instance); | |
266 wm->list = NULL; | |
267 wm->ordered = NULL; | |
268 wm->event_stack = FALSE; | |
269 wm->windows = NULL; | |
270 wm->actions = NULL; | |
271 wm->nodes = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free_node); | |
272 wm->positions = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); | |
273 if (gnt_style_get_bool(GNT_STYLE_REMPOS, TRUE)) | |
274 read_window_positions(wm); | |
275 g_timeout_add(IDLE_CHECK_INTERVAL * 1000, check_idle, NULL); | |
276 time(&last_active_time); | |
277 } | |
278 | |
279 static void | |
280 switch_window(GntWM *wm, int direction) | |
281 { | |
282 GntWidget *w = NULL, *wid = NULL; | |
283 int pos; | |
284 | |
285 if (wm->_list.window || wm->menu) | |
286 return; | |
287 | |
288 if (!wm->ordered || !wm->ordered->next) | |
289 return; | |
290 | |
291 w = wm->ordered->data; | |
292 pos = g_list_index(wm->list, w); | |
293 pos += direction; | |
294 | |
295 if (pos < 0) | |
296 wid = g_list_last(wm->list)->data; | |
297 else if (pos >= g_list_length(wm->list)) | |
298 wid = wm->list->data; | |
299 else if (pos >= 0) | |
300 wid = g_list_nth_data(wm->list, pos); | |
301 | |
302 wm->ordered = g_list_bring_to_front(wm->ordered, wid); | |
303 | |
304 gnt_wm_raise_window(wm, wm->ordered->data); | |
305 | |
306 if (w != wid) { | |
307 gnt_widget_set_focus(w, FALSE); | |
308 } | |
309 } | |
310 | |
311 static gboolean | |
312 window_next(GntBindable *bindable, GList *null) | |
313 { | |
314 GntWM *wm = GNT_WM(bindable); | |
315 switch_window(wm, 1); | |
316 return TRUE; | |
317 } | |
318 | |
319 static gboolean | |
320 window_prev(GntBindable *bindable, GList *null) | |
321 { | |
322 GntWM *wm = GNT_WM(bindable); | |
323 switch_window(wm, -1); | |
324 return TRUE; | |
325 } | |
326 | |
327 static gboolean | |
328 switch_window_n(GntBindable *bind, GList *list) | |
329 { | |
330 GntWM *wm = GNT_WM(bind); | |
331 GntWidget *w = NULL; | |
332 GList *l; | |
333 int n; | |
334 | |
335 if (!wm->ordered) | |
336 return TRUE; | |
337 | |
338 if (list) | |
339 n = GPOINTER_TO_INT(list->data); | |
340 else | |
341 n = 0; | |
342 | |
343 w = wm->ordered->data; | |
344 | |
345 if ((l = g_list_nth(wm->list, n)) != NULL) | |
346 { | |
347 gnt_wm_raise_window(wm, l->data); | |
348 } | |
349 | |
350 if (l && w != l->data) | |
351 { | |
352 gnt_widget_set_focus(w, FALSE); | |
353 } | |
354 return TRUE; | |
355 } | |
356 | |
357 static gboolean | |
358 window_scroll_up(GntBindable *bindable, GList *null) | |
359 { | |
360 GntWM *wm = GNT_WM(bindable); | |
361 GntWidget *window; | |
362 GntNode *node; | |
363 | |
364 if (!wm->ordered) | |
365 return TRUE; | |
366 | |
367 window = wm->ordered->data; | |
368 node = g_hash_table_lookup(wm->nodes, window); | |
369 if (!node) | |
370 return TRUE; | |
371 | |
372 if (node->scroll) { | |
373 node->scroll--; | |
374 copy_win(window, node); | |
375 update_screen(wm); | |
376 } | |
377 return TRUE; | |
378 } | |
379 | |
380 static gboolean | |
381 window_scroll_down(GntBindable *bindable, GList *null) | |
382 { | |
383 GntWM *wm = GNT_WM(bindable); | |
384 GntWidget *window; | |
385 GntNode *node; | |
386 int w, h; | |
387 | |
388 if (!wm->ordered) | |
389 return TRUE; | |
390 | |
391 window = wm->ordered->data; | |
392 node = g_hash_table_lookup(wm->nodes, window); | |
393 if (!node) | |
394 return TRUE; | |
395 | |
396 gnt_widget_get_size(window, &w, &h); | |
397 if (h - node->scroll > getmaxy(node->window)) { | |
398 node->scroll++; | |
399 copy_win(window, node); | |
400 update_screen(wm); | |
401 } | |
402 return TRUE; | |
403 } | |
404 | |
405 static gboolean | |
406 window_close(GntBindable *bindable, GList *null) | |
407 { | |
408 GntWM *wm = GNT_WM(bindable); | |
409 | |
410 if (wm->_list.window) | |
411 return TRUE; | |
412 | |
413 if (wm->ordered) { | |
414 gnt_widget_destroy(wm->ordered->data); | |
415 } | |
416 | |
417 return TRUE; | |
418 } | |
419 | |
420 static void | |
421 destroy__list(GntWidget *widget, GntWM *wm) | |
422 { | |
423 wm->_list.window = NULL; | |
424 wm->_list.tree = NULL; | |
425 wm->windows = NULL; | |
426 wm->actions = NULL; | |
427 update_screen(wm); | |
428 } | |
429 | |
430 static void | |
431 setup__list(GntWM *wm) | |
432 { | |
433 GntWidget *tree, *win; | |
434 win = wm->_list.window = gnt_box_new(FALSE, FALSE); | |
435 gnt_box_set_toplevel(GNT_BOX(win), TRUE); | |
436 gnt_box_set_pad(GNT_BOX(win), 0); | |
437 GNT_WIDGET_SET_FLAGS(win, GNT_WIDGET_TRANSIENT); | |
438 | |
439 tree = wm->_list.tree = gnt_tree_new(); | |
440 gnt_box_add_widget(GNT_BOX(win), tree); | |
441 | |
442 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(destroy__list), wm); | |
443 } | |
444 | |
445 static void | |
446 window_list_activate(GntTree *tree, GntWM *wm) | |
447 { | |
448 GntWidget *widget = gnt_tree_get_selection_data(GNT_TREE(tree)); | |
449 | |
450 if (!wm->ordered || !widget) | |
451 return; | |
452 | |
453 gnt_widget_destroy(wm->_list.window); | |
454 gnt_wm_raise_window(wm, widget); | |
455 } | |
456 | |
457 static void | |
458 populate_window_list(GntWM *wm) | |
459 { | |
460 GList *iter; | |
461 GntTree *tree = GNT_TREE(wm->windows->tree); | |
462 for (iter = wm->list; iter; iter = iter->next) { | |
463 GntBox *box = GNT_BOX(iter->data); | |
464 | |
465 gnt_tree_add_row_last(tree, box, | |
466 gnt_tree_create_row(tree, box->title), NULL); | |
467 update_window_in_list(wm, GNT_WIDGET(box)); | |
468 } | |
469 } | |
470 | |
471 static gboolean | |
472 window_list_key_pressed(GntWidget *widget, const char *text, GntWM *wm) | |
473 { | |
474 if (text[1] == 0 && wm->ordered) { | |
475 GntWidget *sel = gnt_tree_get_selection_data(GNT_TREE(widget)); | |
476 switch (text[0]) { | |
477 case '-': | |
478 case '<': | |
479 shift_window(wm, sel, -1); | |
480 break; | |
481 case '+': | |
482 case '>': | |
483 shift_window(wm, sel, 1); | |
484 break; | |
485 default: | |
486 return FALSE; | |
487 } | |
488 gnt_tree_remove_all(GNT_TREE(widget)); | |
489 populate_window_list(wm); | |
490 gnt_tree_set_selected(GNT_TREE(widget), sel); | |
491 return TRUE; | |
492 } | |
493 return FALSE; | |
494 } | |
495 | |
496 static gboolean | |
497 window_list(GntBindable *bindable, GList *null) | |
498 { | |
499 GntWM *wm = GNT_WM(bindable); | |
500 GntWidget *tree, *win; | |
501 | |
502 if (wm->_list.window || wm->menu) | |
503 return TRUE; | |
504 | |
505 if (!wm->ordered) | |
506 return TRUE; | |
507 | |
508 setup__list(wm); | |
509 wm->windows = &wm->_list; | |
510 | |
511 win = wm->windows->window; | |
512 tree = wm->windows->tree; | |
513 | |
514 gnt_box_set_title(GNT_BOX(win), "Window List"); | |
515 | |
516 populate_window_list(wm); | |
517 | |
518 gnt_tree_set_selected(GNT_TREE(tree), wm->ordered->data); | |
519 g_signal_connect(G_OBJECT(tree), "activate", G_CALLBACK(window_list_activate), wm); | |
520 g_signal_connect(G_OBJECT(tree), "key_pressed", G_CALLBACK(window_list_key_pressed), wm); | |
521 | |
522 gnt_tree_set_col_width(GNT_TREE(tree), 0, getmaxx(stdscr) / 3); | |
523 gnt_widget_set_size(tree, 0, getmaxy(stdscr) / 2); | |
524 gnt_widget_set_position(win, getmaxx(stdscr) / 3, getmaxy(stdscr) / 4); | |
525 | |
526 gnt_widget_show(win); | |
527 return TRUE; | |
528 } | |
529 | |
530 static gboolean | |
531 dump_screen(GntBindable *bindable, GList *null) | |
532 { | |
533 int x, y; | |
534 chtype old = 0, now = 0; | |
535 FILE *file = fopen("dump.html", "w"); | |
536 | |
537 fprintf(file, "<pre>"); | |
538 for (y = 0; y < getmaxy(stdscr); y++) { | |
539 for (x = 0; x < getmaxx(stdscr); x++) { | |
540 char ch; | |
541 now = mvwinch(curscr, y, x); | |
542 ch = now & A_CHARTEXT; | |
543 now ^= ch; | |
544 | |
545 #define CHECK(attr, start, end) \ | |
546 do \ | |
547 { \ | |
548 if (now & attr) \ | |
549 { \ | |
550 if (!(old & attr)) \ | |
551 fprintf(file, "%s", start); \ | |
552 } \ | |
553 else if (old & attr) \ | |
554 { \ | |
555 fprintf(file, "%s", end); \ | |
556 } \ | |
557 } while (0) | |
558 | |
559 CHECK(A_BOLD, "<b>", "</b>"); | |
560 CHECK(A_UNDERLINE, "<u>", "</u>"); | |
561 CHECK(A_BLINK, "<blink>", "</blink>"); | |
562 | |
563 if ((now & A_COLOR) != (old & A_COLOR) || | |
564 (now & A_REVERSE) != (old & A_REVERSE)) | |
565 { | |
566 int ret; | |
567 short fgp, bgp, r, g, b; | |
568 struct | |
569 { | |
570 int r, g, b; | |
571 } fg, bg; | |
572 | |
573 ret = pair_content(PAIR_NUMBER(now & A_COLOR), &fgp, &bgp); | |
574 if (fgp == -1) | |
575 fgp = COLOR_BLACK; | |
576 if (bgp == -1) | |
577 bgp = COLOR_WHITE; | |
578 if (now & A_REVERSE) | |
579 fgp ^= bgp ^= fgp ^= bgp; /* *wink* */ | |
580 ret = color_content(fgp, &r, &g, &b); | |
581 fg.r = r; fg.b = b; fg.g = g; | |
582 ret = color_content(bgp, &r, &g, &b); | |
583 bg.r = r; bg.b = b; bg.g = g; | |
584 #define ADJUST(x) (x = x * 255 / 1000) | |
585 ADJUST(fg.r); | |
586 ADJUST(fg.g); | |
587 ADJUST(fg.b); | |
588 ADJUST(bg.r); | |
589 ADJUST(bg.b); | |
590 ADJUST(bg.g); | |
591 | |
592 if (x) fprintf(file, "</span>"); | |
593 fprintf(file, "<span style=\"background:#%02x%02x%02x;color:#%02x%02x%02x\">", | |
594 bg.r, bg.g, bg.b, fg.r, fg.g, fg.b); | |
595 } | |
596 if (now & A_ALTCHARSET) | |
597 { | |
598 switch (ch) | |
599 { | |
600 case 'q': | |
601 ch = '-'; break; | |
602 case 't': | |
603 case 'u': | |
604 case 'x': | |
605 ch = '|'; break; | |
606 case 'v': | |
607 case 'w': | |
608 case 'l': | |
609 case 'm': | |
610 case 'k': | |
611 case 'j': | |
612 case 'n': | |
613 ch = '+'; break; | |
614 case '-': | |
615 ch = '^'; break; | |
616 case '.': | |
617 ch = 'v'; break; | |
618 case 'a': | |
619 ch = '#'; break; | |
620 default: | |
621 ch = ' '; break; | |
622 } | |
623 } | |
624 if (ch == '&') | |
625 fprintf(file, "&"); | |
626 else if (ch == '<') | |
627 fprintf(file, "<"); | |
628 else if (ch == '>') | |
629 fprintf(file, ">"); | |
630 else | |
631 fprintf(file, "%c", ch); | |
632 old = now; | |
633 } | |
634 fprintf(file, "</span>\n"); | |
635 old = 0; | |
636 } | |
637 fprintf(file, "</pre>"); | |
638 fclose(file); | |
639 return TRUE; | |
640 } | |
641 | |
642 static void | |
643 shift_window(GntWM *wm, GntWidget *widget, int dir) | |
644 { | |
645 GList *all = wm->list; | |
646 GList *list = g_list_find(all, widget); | |
647 int length, pos; | |
648 if (!list) | |
649 return; | |
650 | |
651 length = g_list_length(all); | |
652 pos = g_list_position(all, list); | |
653 | |
654 pos += dir; | |
655 if (dir > 0) | |
656 pos++; | |
657 | |
658 if (pos < 0) | |
659 pos = length; | |
660 else if (pos > length) | |
661 pos = 0; | |
662 | |
663 all = g_list_insert(all, widget, pos); | |
664 all = g_list_delete_link(all, list); | |
665 wm->list = all; | |
666 draw_taskbar(wm, FALSE); | |
667 } | |
668 | |
669 static gboolean | |
670 shift_left(GntBindable *bindable, GList *null) | |
671 { | |
672 GntWM *wm = GNT_WM(bindable); | |
673 if (wm->_list.window) | |
674 return TRUE; | |
675 | |
676 shift_window(wm, wm->ordered->data, -1); | |
677 return TRUE; | |
678 } | |
679 | |
680 static gboolean | |
681 shift_right(GntBindable *bindable, GList *null) | |
682 { | |
683 GntWM *wm = GNT_WM(bindable); | |
684 if (wm->_list.window) | |
685 return TRUE; | |
686 | |
687 shift_window(wm, wm->ordered->data, 1); | |
688 return TRUE; | |
689 } | |
690 | |
691 static void | |
692 action_list_activate(GntTree *tree, GntWM *wm) | |
693 { | |
694 GntAction *action = gnt_tree_get_selection_data(tree); | |
695 action->callback(); | |
696 gnt_widget_destroy(wm->_list.window); | |
697 } | |
698 | |
699 static int | |
700 compare_action(gconstpointer p1, gconstpointer p2) | |
701 { | |
702 const GntAction *a1 = p1; | |
703 const GntAction *a2 = p2; | |
704 | |
705 return g_utf8_collate(a1->label, a2->label); | |
706 } | |
707 | |
708 static gboolean | |
709 list_actions(GntBindable *bindable, GList *null) | |
710 { | |
711 GntWidget *tree, *win; | |
712 GList *iter; | |
713 GntWM *wm = GNT_WM(bindable); | |
714 if (wm->_list.window || wm->menu) | |
715 return TRUE; | |
716 | |
717 if (wm->acts == NULL) | |
718 return TRUE; | |
719 | |
720 setup__list(wm); | |
721 wm->actions = &wm->_list; | |
722 | |
723 win = wm->actions->window; | |
724 tree = wm->actions->tree; | |
725 | |
726 gnt_box_set_title(GNT_BOX(win), "Actions"); | |
727 GNT_WIDGET_SET_FLAGS(tree, GNT_WIDGET_NO_BORDER); | |
728 /* XXX: Do we really want this? */ | |
729 gnt_tree_set_compare_func(GNT_TREE(tree), compare_action); | |
730 | |
731 for (iter = wm->acts; iter; iter = iter->next) { | |
732 GntAction *action = iter->data; | |
733 gnt_tree_add_row_last(GNT_TREE(tree), action, | |
734 gnt_tree_create_row(GNT_TREE(tree), action->label), NULL); | |
735 } | |
736 g_signal_connect(G_OBJECT(tree), "activate", G_CALLBACK(action_list_activate), wm); | |
737 gnt_widget_set_size(tree, 0, g_list_length(wm->acts)); | |
738 gnt_widget_set_position(win, 0, getmaxy(stdscr) - 3 - g_list_length(wm->acts)); | |
739 | |
740 gnt_widget_show(win); | |
741 return TRUE; | |
742 } | |
743 | |
744 #ifndef NO_WIDECHAR | |
745 static int | |
746 widestringwidth(wchar_t *wide) | |
747 { | |
748 int len, ret; | |
749 char *string; | |
750 | |
751 len = wcstombs(NULL, wide, 0) + 1; | |
752 string = g_new0(char, len); | |
753 wcstombs(string, wide, len); | |
754 ret = gnt_util_onscreen_width(string, NULL); | |
755 g_free(string); | |
756 return ret; | |
757 } | |
758 #endif | |
759 | |
760 /* Returns the onscreen width of the character at the position */ | |
761 static int | |
762 reverse_char(WINDOW *d, int y, int x, gboolean set) | |
763 { | |
764 #define DECIDE(ch) (set ? ((ch) | A_REVERSE) : ((ch) & ~A_REVERSE)) | |
765 | |
766 #ifdef NO_WIDECHAR | |
767 chtype ch; | |
768 ch = mvwinch(d, y, x); | |
769 mvwaddch(d, y, x, DECIDE(ch)); | |
770 return 1; | |
771 #else | |
772 cchar_t ch; | |
773 int wc = 1; | |
774 if (mvwin_wch(d, y, x, &ch) == OK) { | |
775 wc = widestringwidth(ch.chars); | |
776 ch.attr = DECIDE(ch.attr); | |
777 ch.attr &= WA_ATTRIBUTES; /* XXX: This is a workaround for a bug */ | |
778 mvwadd_wch(d, y, x, &ch); | |
779 } | |
780 | |
781 return wc; | |
782 #endif | |
783 } | |
784 | |
785 static void | |
786 window_reverse(GntWidget *win, gboolean set, GntWM *wm) | |
787 { | |
788 int i; | |
789 int w, h; | |
790 WINDOW *d; | |
791 | |
792 if (GNT_WIDGET_IS_FLAG_SET(win, GNT_WIDGET_NO_BORDER)) | |
793 return; | |
794 | |
795 d = win->window; | |
796 gnt_widget_get_size(win, &w, &h); | |
797 | |
798 if (gnt_widget_has_shadow(win)) { | |
799 --w; | |
800 --h; | |
801 } | |
802 | |
803 /* the top and bottom */ | |
804 for (i = 0; i < w; i += reverse_char(d, 0, i, set)); | |
805 for (i = 0; i < w; i += reverse_char(d, h-1, i, set)); | |
806 | |
807 /* the left and right */ | |
808 for (i = 0; i < h; i += reverse_char(d, i, 0, set)); | |
809 for (i = 0; i < h; i += reverse_char(d, i, w-1, set)); | |
810 | |
811 copy_win(win, g_hash_table_lookup(wm->nodes, win)); | |
812 update_screen(wm); | |
813 } | |
814 | |
815 static gboolean | |
816 start_move(GntBindable *bindable, GList *null) | |
817 { | |
818 GntWM *wm = GNT_WM(bindable); | |
819 if (wm->_list.window || wm->menu) | |
820 return TRUE; | |
821 if (!wm->ordered) | |
822 return TRUE; | |
823 | |
824 wm->mode = GNT_KP_MODE_MOVE; | |
825 window_reverse(GNT_WIDGET(wm->ordered->data), TRUE, wm); | |
826 | |
827 return TRUE; | |
828 } | |
829 | |
830 static gboolean | |
831 start_resize(GntBindable *bindable, GList *null) | |
832 { | |
833 GntWM *wm = GNT_WM(bindable); | |
834 if (wm->_list.window || wm->menu) | |
835 return TRUE; | |
836 if (!wm->ordered) | |
837 return TRUE; | |
838 | |
839 wm->mode = GNT_KP_MODE_RESIZE; | |
840 window_reverse(GNT_WIDGET(wm->ordered->data), TRUE, wm); | |
841 | |
842 return TRUE; | |
843 } | |
844 | |
845 static gboolean | |
846 wm_quit(GntBindable *bindable, GList *list) | |
847 { | |
848 GntWM *wm = GNT_WM(bindable); | |
849 if (write_timeout) | |
850 write_already(wm); | |
851 g_main_loop_quit(wm->loop); | |
852 return TRUE; | |
853 } | |
854 | |
855 static gboolean | |
856 return_true(GntWM *wm, GntWidget *w, int *a, int *b) | |
857 { | |
858 return TRUE; | |
859 } | |
860 | |
861 static gboolean | |
862 refresh_screen(GntBindable *bindable, GList *null) | |
863 { | |
864 GntWM *wm = GNT_WM(bindable); | |
865 | |
866 endwin(); | |
867 refresh(); | |
868 curs_set(0); /* endwin resets the cursor to normal */ | |
869 | |
870 g_hash_table_foreach(wm->nodes, (GHFunc)refresh_node, NULL); | |
871 update_screen(wm); | |
872 draw_taskbar(wm, TRUE); | |
873 | |
874 return FALSE; | |
875 } | |
876 | |
877 static void | |
878 gnt_wm_class_init(GntWMClass *klass) | |
879 { | |
880 int i; | |
881 | |
882 klass->new_window = gnt_wm_new_window_real; | |
883 klass->decorate_window = NULL; | |
884 klass->close_window = NULL; | |
885 klass->window_resize_confirm = return_true; | |
886 klass->window_resized = gnt_wm_win_resized; | |
887 klass->window_move_confirm = return_true; | |
888 klass->window_moved = gnt_wm_win_moved; | |
889 klass->window_update = NULL; | |
890 klass->key_pressed = NULL; | |
891 klass->mouse_clicked = NULL; | |
892 klass->give_focus = gnt_wm_give_focus; | |
893 | |
894 signals[SIG_NEW_WIN] = | |
895 g_signal_new("new_win", | |
896 G_TYPE_FROM_CLASS(klass), | |
897 G_SIGNAL_RUN_LAST, | |
898 G_STRUCT_OFFSET(GntWMClass, new_window), | |
899 NULL, NULL, | |
900 g_cclosure_marshal_VOID__POINTER, | |
901 G_TYPE_NONE, 1, G_TYPE_POINTER); | |
902 signals[SIG_DECORATE_WIN] = | |
903 g_signal_new("decorate_win", | |
904 G_TYPE_FROM_CLASS(klass), | |
905 G_SIGNAL_RUN_LAST, | |
906 G_STRUCT_OFFSET(GntWMClass, decorate_window), | |
907 NULL, NULL, | |
908 g_cclosure_marshal_VOID__POINTER, | |
909 G_TYPE_NONE, 1, G_TYPE_POINTER); | |
910 signals[SIG_CLOSE_WIN] = | |
911 g_signal_new("close_win", | |
912 G_TYPE_FROM_CLASS(klass), | |
913 G_SIGNAL_RUN_LAST, | |
914 G_STRUCT_OFFSET(GntWMClass, close_window), | |
915 NULL, NULL, | |
916 g_cclosure_marshal_VOID__POINTER, | |
917 G_TYPE_NONE, 1, G_TYPE_POINTER); | |
918 signals[SIG_CONFIRM_RESIZE] = | |
919 g_signal_new("confirm_resize", | |
920 G_TYPE_FROM_CLASS(klass), | |
921 G_SIGNAL_RUN_LAST, | |
922 G_STRUCT_OFFSET(GntWMClass, window_resize_confirm), | |
923 gnt_boolean_handled_accumulator, NULL, | |
924 gnt_closure_marshal_BOOLEAN__POINTER_POINTER_POINTER, | |
925 G_TYPE_BOOLEAN, 3, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER); | |
926 | |
927 signals[SIG_CONFIRM_MOVE] = | |
928 g_signal_new("confirm_move", | |
929 G_TYPE_FROM_CLASS(klass), | |
930 G_SIGNAL_RUN_LAST, | |
931 G_STRUCT_OFFSET(GntWMClass, window_move_confirm), | |
932 gnt_boolean_handled_accumulator, NULL, | |
933 gnt_closure_marshal_BOOLEAN__POINTER_POINTER_POINTER, | |
934 G_TYPE_BOOLEAN, 3, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER); | |
935 | |
936 signals[SIG_RESIZED] = | |
937 g_signal_new("window_resized", | |
938 G_TYPE_FROM_CLASS(klass), | |
939 G_SIGNAL_RUN_LAST, | |
940 G_STRUCT_OFFSET(GntWMClass, window_resized), | |
941 NULL, NULL, | |
942 g_cclosure_marshal_VOID__POINTER, | |
943 G_TYPE_NONE, 1, G_TYPE_POINTER); | |
944 signals[SIG_MOVED] = | |
945 g_signal_new("window_moved", | |
946 G_TYPE_FROM_CLASS(klass), | |
947 G_SIGNAL_RUN_LAST, | |
948 G_STRUCT_OFFSET(GntWMClass, window_moved), | |
949 NULL, NULL, | |
950 g_cclosure_marshal_VOID__POINTER, | |
951 G_TYPE_NONE, 1, G_TYPE_POINTER); | |
952 signals[SIG_UPDATE_WIN] = | |
953 g_signal_new("window_update", | |
954 G_TYPE_FROM_CLASS(klass), | |
955 G_SIGNAL_RUN_LAST, | |
956 G_STRUCT_OFFSET(GntWMClass, window_update), | |
957 NULL, NULL, | |
958 g_cclosure_marshal_VOID__POINTER, | |
959 G_TYPE_NONE, 1, G_TYPE_POINTER); | |
960 | |
961 signals[SIG_GIVE_FOCUS] = | |
962 g_signal_new("give_focus", | |
963 G_TYPE_FROM_CLASS(klass), | |
964 G_SIGNAL_RUN_LAST, | |
965 G_STRUCT_OFFSET(GntWMClass, give_focus), | |
966 NULL, NULL, | |
967 g_cclosure_marshal_VOID__POINTER, | |
968 G_TYPE_NONE, 1, G_TYPE_POINTER); | |
969 | |
970 signals[SIG_MOUSE_CLICK] = | |
971 g_signal_new("mouse_clicked", | |
972 G_TYPE_FROM_CLASS(klass), | |
973 G_SIGNAL_RUN_LAST, | |
974 G_STRUCT_OFFSET(GntWMClass, mouse_clicked), | |
975 gnt_boolean_handled_accumulator, NULL, | |
976 gnt_closure_marshal_BOOLEAN__INT_INT_INT_POINTER, | |
977 G_TYPE_BOOLEAN, 4, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_POINTER); | |
978 | |
979 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "window-next", window_next, | |
980 "\033" "n", NULL); | |
981 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "window-prev", window_prev, | |
982 "\033" "p", NULL); | |
983 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "window-close", window_close, | |
984 "\033" "c", NULL); | |
985 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "window-list", window_list, | |
986 "\033" "w", NULL); | |
987 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "dump-screen", dump_screen, | |
988 "\033" "d", NULL); | |
989 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "shift-left", shift_left, | |
990 "\033" ",", NULL); | |
991 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "shift-right", shift_right, | |
992 "\033" ".", NULL); | |
993 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "action-list", list_actions, | |
994 "\033" "a", NULL); | |
995 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "start-move", start_move, | |
996 "\033" "m", NULL); | |
997 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "start-resize", start_resize, | |
998 "\033" "r", NULL); | |
999 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "wm-quit", wm_quit, | |
1000 "\033" "q", NULL); | |
1001 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "refresh-screen", refresh_screen, | |
1002 "\033" "l", NULL); | |
1003 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "switch-window-n", switch_window_n, | |
1004 NULL, NULL); | |
1005 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "window-scroll-down", window_scroll_down, | |
1006 "\033" GNT_KEY_CTRL_J, NULL); | |
1007 gnt_bindable_class_register_action(GNT_BINDABLE_CLASS(klass), "window-scroll-up", window_scroll_up, | |
1008 "\033" GNT_KEY_CTRL_K, NULL); | |
1009 | |
1010 gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), GNT_BINDABLE_CLASS(klass)); | |
1011 | |
1012 /* Make sure Alt+x are detected properly. */ | |
1013 for (i = '0'; i <= '9'; i++) { | |
1014 char str[] = "\033X"; | |
1015 str[1] = i; | |
1016 gnt_keys_add_combination(str); | |
1017 } | |
1018 | |
1019 GNTDEBUG; | |
1020 } | |
1021 | |
1022 /****************************************************************************** | |
1023 * GntWM API | |
1024 *****************************************************************************/ | |
1025 GType | |
1026 gnt_wm_get_gtype(void) | |
1027 { | |
1028 static GType type = 0; | |
1029 | |
1030 if(type == 0) { | |
1031 static const GTypeInfo info = { | |
1032 sizeof(GntWMClass), | |
1033 NULL, /* base_init */ | |
1034 NULL, /* base_finalize */ | |
1035 (GClassInitFunc)gnt_wm_class_init, | |
1036 NULL, | |
1037 NULL, /* class_data */ | |
1038 sizeof(GntWM), | |
1039 0, /* n_preallocs */ | |
1040 gnt_wm_init, /* instance_init */ | |
1041 NULL /* value_table */ | |
1042 }; | |
1043 | |
1044 type = g_type_register_static(GNT_TYPE_BINDABLE, | |
1045 "GntWM", | |
1046 &info, 0); | |
1047 } | |
1048 | |
1049 return type; | |
1050 } | |
1051 static void | |
1052 update_window_in_list(GntWM *wm, GntWidget *wid) | |
1053 { | |
1054 GntTextFormatFlags flag = 0; | |
1055 | |
1056 if (wm->windows == NULL) | |
1057 return; | |
1058 | |
1059 if (wid == wm->ordered->data) | |
1060 flag |= GNT_TEXT_FLAG_DIM; | |
1061 else if (GNT_WIDGET_IS_FLAG_SET(wid, GNT_WIDGET_URGENT)) | |
1062 flag |= GNT_TEXT_FLAG_BOLD; | |
1063 | |
1064 gnt_tree_set_row_flags(GNT_TREE(wm->windows->tree), wid, flag); | |
1065 } | |
1066 | |
1067 static void | |
1068 gnt_wm_new_window_real(GntWM *wm, GntWidget *widget) | |
1069 { | |
1070 GntNode *node; | |
1071 gboolean transient = FALSE; | |
1072 | |
1073 if (widget->window == NULL) | |
1074 return; | |
1075 | |
1076 node = g_new0(GntNode, 1); | |
1077 node->me = widget; | |
1078 node->scroll = 0; | |
1079 | |
1080 g_hash_table_replace(wm->nodes, widget, node); | |
1081 | |
1082 refresh_node(widget, node, NULL); | |
1083 | |
1084 transient = !!GNT_WIDGET_IS_FLAG_SET(node->me, GNT_WIDGET_TRANSIENT); | |
1085 | |
1086 #if 1 | |
1087 { | |
1088 int x, y, w, h, maxx, maxy; | |
1089 gboolean shadow = TRUE; | |
1090 | |
1091 if (!gnt_widget_has_shadow(widget)) | |
1092 shadow = FALSE; | |
1093 x = widget->priv.x; | |
1094 y = widget->priv.y; | |
1095 w = widget->priv.width; | |
1096 h = widget->priv.height; | |
1097 | |
1098 getmaxyx(stdscr, maxy, maxx); | |
1099 maxy -= 1; /* room for the taskbar */ | |
1100 maxy -= shadow; | |
1101 maxx -= shadow; | |
1102 | |
1103 x = MAX(0, x); | |
1104 y = MAX(0, y); | |
1105 if (x + w >= maxx) | |
1106 x = MAX(0, maxx - w); | |
1107 if (y + h >= maxy) | |
1108 y = MAX(0, maxy - h); | |
1109 | |
1110 w = MIN(w, maxx); | |
1111 h = MIN(h, maxy); | |
1112 node->window = newwin(h + shadow, w + shadow, y, x); | |
1113 copy_win(widget, node); | |
1114 } | |
1115 #endif | |
1116 | |
1117 node->panel = new_panel(node->window); | |
1118 set_panel_userptr(node->panel, node); | |
1119 | |
1120 if (!transient) { | |
1121 if (node->me != wm->_list.window) { | |
1122 GntWidget *w = NULL; | |
1123 | |
1124 if (wm->ordered) | |
1125 w = wm->ordered->data; | |
1126 | |
1127 wm->list = g_list_append(wm->list, widget); | |
1128 | |
1129 if (wm->event_stack) | |
1130 wm->ordered = g_list_prepend(wm->ordered, widget); | |
1131 else | |
1132 wm->ordered = g_list_append(wm->ordered, widget); | |
1133 | |
1134 gnt_widget_set_focus(widget, TRUE); | |
1135 if (w) | |
1136 gnt_widget_set_focus(w, FALSE); | |
1137 } | |
1138 | |
1139 if (wm->event_stack || node->me == wm->_list.window) { | |
1140 gnt_wm_raise_window(wm, node->me); | |
1141 } else { | |
1142 bottom_panel(node->panel); /* New windows should not grab focus */ | |
1143 gnt_widget_set_urgent(node->me); | |
1144 } | |
1145 } | |
1146 } | |
1147 | |
1148 void gnt_wm_new_window(GntWM *wm, GntWidget *widget) | |
1149 { | |
1150 while (widget->parent) | |
1151 widget = widget->parent; | |
1152 | |
1153 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_INVISIBLE) || | |
1154 g_hash_table_lookup(wm->nodes, widget)) { | |
1155 update_screen(wm); | |
1156 return; | |
1157 } | |
1158 | |
1159 if (GNT_IS_BOX(widget)) { | |
1160 const char *title = GNT_BOX(widget)->title; | |
1161 GntPosition *p = NULL; | |
1162 if (title && (p = g_hash_table_lookup(wm->positions, title)) != NULL) { | |
1163 sanitize_position(widget, &p->x, &p->y); | |
1164 gnt_widget_set_position(widget, p->x, p->y); | |
1165 mvwin(widget->window, p->y, p->x); | |
1166 } | |
1167 } | |
1168 | |
1169 g_signal_emit(wm, signals[SIG_NEW_WIN], 0, widget); | |
1170 g_signal_emit(wm, signals[SIG_DECORATE_WIN], 0, widget); | |
1171 | |
1172 if (wm->windows && !GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_TRANSIENT)) { | |
1173 if ((GNT_IS_BOX(widget) && GNT_BOX(widget)->title) && wm->_list.window != widget | |
1174 && GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_CAN_TAKE_FOCUS)) { | |
1175 gnt_tree_add_row_last(GNT_TREE(wm->windows->tree), widget, | |
1176 gnt_tree_create_row(GNT_TREE(wm->windows->tree), GNT_BOX(widget)->title), | |
1177 NULL); | |
1178 update_window_in_list(wm, widget); | |
1179 } | |
1180 } | |
1181 | |
1182 update_screen(wm); | |
1183 draw_taskbar(wm, FALSE); | |
1184 } | |
1185 | |
1186 void gnt_wm_window_decorate(GntWM *wm, GntWidget *widget) | |
1187 { | |
1188 g_signal_emit(wm, signals[SIG_DECORATE_WIN], 0, widget); | |
1189 } | |
1190 | |
1191 void gnt_wm_window_close(GntWM *wm, GntWidget *widget) | |
1192 { | |
1193 GntNode *node; | |
1194 int pos; | |
1195 | |
1196 if ((node = g_hash_table_lookup(wm->nodes, widget)) == NULL) | |
1197 return; | |
1198 | |
1199 g_signal_emit(wm, signals[SIG_CLOSE_WIN], 0, widget); | |
1200 g_hash_table_remove(wm->nodes, widget); | |
1201 | |
1202 if (wm->windows) { | |
1203 gnt_tree_remove(GNT_TREE(wm->windows->tree), widget); | |
1204 } | |
1205 | |
1206 pos = g_list_index(wm->list, widget); | |
1207 | |
1208 if (pos != -1) { | |
1209 wm->list = g_list_remove(wm->list, widget); | |
1210 wm->ordered = g_list_remove(wm->ordered, widget); | |
1211 | |
1212 if (wm->ordered) | |
1213 gnt_wm_raise_window(wm, wm->ordered->data); | |
1214 } | |
1215 | |
1216 update_screen(wm); | |
1217 draw_taskbar(wm, FALSE); | |
1218 } | |
1219 | |
1220 time_t gnt_wm_get_idle_time() | |
1221 { | |
1222 return time(NULL) - last_active_time; | |
1223 } | |
1224 | |
1225 gboolean gnt_wm_process_input(GntWM *wm, const char *keys) | |
1226 { | |
1227 gboolean ret = FALSE; | |
1228 | |
1229 keys = gnt_bindable_remap_keys(GNT_BINDABLE(wm), keys); | |
1230 | |
1231 idle_update = TRUE; | |
1232 | |
1233 if (gnt_bindable_perform_action_key(GNT_BINDABLE(wm), keys)) | |
1234 return TRUE; | |
1235 | |
1236 /* Do some manual checking */ | |
1237 if (wm->ordered && wm->mode != GNT_KP_MODE_NORMAL) { | |
1238 int xmin = 0, ymin = 0, xmax = getmaxx(stdscr), ymax = getmaxy(stdscr) - 1; | |
1239 int x, y, w, h; | |
1240 GntWidget *widget = GNT_WIDGET(wm->ordered->data); | |
1241 int ox, oy, ow, oh; | |
1242 | |
1243 gnt_widget_get_position(widget, &x, &y); | |
1244 gnt_widget_get_size(widget, &w, &h); | |
1245 ox = x; oy = y; | |
1246 ow = w; oh = h; | |
1247 | |
1248 if (wm->mode == GNT_KP_MODE_MOVE) { | |
1249 if (strcmp(keys, GNT_KEY_LEFT) == 0) { | |
1250 if (x > xmin) | |
1251 x--; | |
1252 } else if (strcmp(keys, GNT_KEY_RIGHT) == 0) { | |
1253 if (x + w < xmax) | |
1254 x++; | |
1255 } else if (strcmp(keys, GNT_KEY_UP) == 0) { | |
1256 if (y > ymin) | |
1257 y--; | |
1258 } else if (strcmp(keys, GNT_KEY_DOWN) == 0) { | |
1259 if (y + h < ymax) | |
1260 y++; | |
1261 } | |
1262 if (ox != x || oy != y) { | |
1263 gnt_screen_move_widget(widget, x, y); | |
1264 window_reverse(widget, TRUE, wm); | |
1265 return TRUE; | |
1266 } | |
1267 } else if (wm->mode == GNT_KP_MODE_RESIZE) { | |
1268 if (strcmp(keys, GNT_KEY_LEFT) == 0) { | |
1269 w--; | |
1270 } else if (strcmp(keys, GNT_KEY_RIGHT) == 0) { | |
1271 if (x + w < xmax) | |
1272 w++; | |
1273 } else if (strcmp(keys, GNT_KEY_UP) == 0) { | |
1274 h--; | |
1275 } else if (strcmp(keys, GNT_KEY_DOWN) == 0) { | |
1276 if (y + h < ymax) | |
1277 h++; | |
1278 } | |
1279 if (oh != h || ow != w) { | |
1280 gnt_screen_resize_widget(widget, w, h); | |
1281 window_reverse(widget, TRUE, wm); | |
1282 return TRUE; | |
1283 } | |
1284 } | |
1285 if (strcmp(keys, "\r") == 0 || strcmp(keys, "\033") == 0) { | |
1286 window_reverse(widget, FALSE, wm); | |
1287 wm->mode = GNT_KP_MODE_NORMAL; | |
1288 } | |
1289 return TRUE; | |
1290 } | |
1291 | |
1292 wm->event_stack = TRUE; | |
1293 | |
1294 /* Escape to close the window-list or action-list window */ | |
1295 if (strcmp(keys, "\033") == 0) { | |
1296 if (wm->_list.window) { | |
1297 gnt_widget_destroy(wm->_list.window); | |
1298 wm->event_stack = FALSE; | |
1299 return TRUE; | |
1300 } | |
1301 } else if (keys[0] == '\033' && isdigit(keys[1]) && keys[2] == '\0') { | |
1302 /* Alt+x for quick switch */ | |
1303 int n = *(keys + 1) - '0'; | |
1304 GList *list = NULL; | |
1305 | |
1306 if (n == 0) | |
1307 n = 10; | |
1308 | |
1309 list = g_list_append(list, GINT_TO_POINTER(n - 1)); | |
1310 switch_window_n(GNT_BINDABLE(wm), list); | |
1311 g_list_free(list); | |
1312 return TRUE; | |
1313 } | |
1314 | |
1315 if (wm->menu) | |
1316 ret = gnt_widget_key_pressed(GNT_WIDGET(wm->menu), keys); | |
1317 else if (wm->_list.window) | |
1318 ret = gnt_widget_key_pressed(wm->_list.window, keys); | |
1319 else if (wm->ordered) | |
1320 ret = gnt_widget_key_pressed(GNT_WIDGET(wm->ordered->data), keys); | |
1321 wm->event_stack = FALSE; | |
1322 return ret; | |
1323 } | |
1324 | |
1325 static void | |
1326 gnt_wm_win_resized(GntWM *wm, GntNode *node) | |
1327 { | |
1328 /*refresh_node(node->me, node, NULL);*/ | |
1329 } | |
1330 | |
1331 static void | |
1332 gnt_wm_win_moved(GntWM *wm, GntNode *node) | |
1333 { | |
1334 refresh_node(node->me, node, NULL); | |
1335 } | |
1336 | |
1337 void gnt_wm_resize_window(GntWM *wm, GntWidget *widget, int width, int height) | |
1338 { | |
1339 gboolean ret = TRUE; | |
1340 GntNode *node; | |
1341 int shadow; | |
1342 int maxx, maxy; | |
1343 | |
1344 while (widget->parent) | |
1345 widget = widget->parent; | |
1346 node = g_hash_table_lookup(wm->nodes, widget); | |
1347 if (!node) | |
1348 return; | |
1349 | |
1350 g_signal_emit(wm, signals[SIG_CONFIRM_RESIZE], 0, widget, &width, &height, &ret); | |
1351 if (!ret) | |
1352 return; /* resize is not permitted */ | |
1353 hide_panel(node->panel); | |
1354 gnt_widget_set_size(widget, width, height); | |
1355 gnt_widget_draw(widget); | |
1356 | |
1357 shadow = gnt_widget_has_shadow(widget) ? 1 : 0; | |
1358 maxx = getmaxx(stdscr) - shadow; | |
1359 maxy = getmaxy(stdscr) - 1 - shadow; | |
1360 height = MIN(height, maxy); | |
1361 width = MIN(width, maxx); | |
15978
2a82bc8d57f7
More fixes for resizing when shadow is turned on.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15964
diff
changeset
|
1362 wresize(node->window, height, width); |
15817 | 1363 replace_panel(node->panel, node->window); |
1364 | |
1365 g_signal_emit(wm, signals[SIG_RESIZED], 0, node); | |
1366 | |
1367 show_panel(node->panel); | |
1368 update_screen(wm); | |
1369 } | |
1370 | |
1371 static void | |
1372 write_gdi(gpointer key, gpointer value, gpointer data) | |
1373 { | |
1374 GntPosition *p = value; | |
1375 fprintf(data, ".%s = %d;%d\n", (char *)key, p->x, p->y); | |
1376 } | |
1377 | |
1378 static gboolean | |
1379 write_already(gpointer data) | |
1380 { | |
1381 GntWM *wm = data; | |
1382 FILE *file; | |
1383 char *filename; | |
1384 | |
1385 filename = g_build_filename(g_get_home_dir(), ".gntpositions", NULL); | |
1386 | |
1387 file = fopen(filename, "wb"); | |
1388 if (file == NULL) { | |
1389 g_printerr("GntWM: error opening file to save positions\n"); | |
1390 } else { | |
1391 fprintf(file, "[positions]\n"); | |
1392 g_hash_table_foreach(wm->positions, write_gdi, file); | |
1393 fclose(file); | |
1394 } | |
1395 | |
1396 g_free(filename); | |
1397 g_source_remove(write_timeout); | |
1398 write_timeout = 0; | |
1399 return FALSE; | |
1400 } | |
1401 | |
1402 static void | |
1403 write_positions_to_file(GntWM *wm) | |
1404 { | |
1405 if (write_timeout) { | |
1406 g_source_remove(write_timeout); | |
1407 } | |
1408 write_timeout = g_timeout_add(10000, write_already, wm); | |
1409 } | |
1410 | |
1411 void gnt_wm_move_window(GntWM *wm, GntWidget *widget, int x, int y) | |
1412 { | |
1413 gboolean ret = TRUE; | |
1414 GntNode *node; | |
1415 | |
1416 while (widget->parent) | |
1417 widget = widget->parent; | |
1418 node = g_hash_table_lookup(wm->nodes, widget); | |
1419 if (!node) | |
1420 return; | |
1421 | |
1422 g_signal_emit(wm, signals[SIG_CONFIRM_MOVE], 0, widget, &x, &y, &ret); | |
1423 if (!ret) | |
1424 return; /* resize is not permitted */ | |
1425 | |
1426 gnt_widget_set_position(widget, x, y); | |
1427 move_panel(node->panel, y, x); | |
1428 | |
1429 g_signal_emit(wm, signals[SIG_MOVED], 0, node); | |
1430 if (gnt_style_get_bool(GNT_STYLE_REMPOS, TRUE) && GNT_IS_BOX(widget)) { | |
1431 const char *title = GNT_BOX(widget)->title; | |
1432 if (title) { | |
1433 GntPosition *p = g_new0(GntPosition, 1); | |
1434 GntWidget *wid = node->me; | |
1435 p->x = wid->priv.x; | |
1436 p->y = wid->priv.y; | |
1437 g_hash_table_replace(wm->positions, g_strdup(title), p); | |
1438 write_positions_to_file(wm); | |
1439 } | |
1440 } | |
1441 | |
1442 update_screen(wm); | |
1443 } | |
1444 | |
1445 static void | |
1446 gnt_wm_give_focus(GntWM *wm, GntWidget *widget) | |
1447 { | |
1448 GntNode *node = g_hash_table_lookup(wm->nodes, widget); | |
1449 | |
1450 if (!node) | |
1451 return; | |
1452 | |
1453 if (widget != wm->_list.window && !GNT_IS_MENU(widget) && | |
1454 wm->ordered->data != widget) { | |
1455 GntWidget *w = wm->ordered->data; | |
1456 wm->ordered = g_list_bring_to_front(wm->ordered, widget); | |
1457 gnt_widget_set_focus(w, FALSE); | |
1458 } | |
1459 | |
1460 gnt_widget_set_focus(widget, TRUE); | |
1461 GNT_WIDGET_UNSET_FLAGS(widget, GNT_WIDGET_URGENT); | |
1462 gnt_widget_draw(widget); | |
1463 top_panel(node->panel); | |
1464 | |
1465 if (wm->_list.window) { | |
1466 GntNode *nd = g_hash_table_lookup(wm->nodes, wm->_list.window); | |
1467 top_panel(nd->panel); | |
1468 } | |
1469 update_screen(wm); | |
1470 draw_taskbar(wm, FALSE); | |
1471 } | |
1472 | |
1473 void gnt_wm_update_window(GntWM *wm, GntWidget *widget) | |
1474 { | |
1475 GntNode *node; | |
1476 | |
1477 while (widget->parent) | |
1478 widget = widget->parent; | |
1479 if (!GNT_IS_MENU(widget)) | |
1480 gnt_box_sync_children(GNT_BOX(widget)); | |
1481 | |
1482 node = g_hash_table_lookup(wm->nodes, widget); | |
1483 if (node == NULL) { | |
1484 gnt_wm_new_window(wm, widget); | |
1485 } else | |
1486 g_signal_emit(wm, signals[SIG_UPDATE_WIN], 0, node); | |
1487 | |
1488 copy_win(widget, node); | |
1489 update_screen(wm); | |
1490 draw_taskbar(wm, FALSE); | |
1491 } | |
1492 | |
1493 gboolean gnt_wm_process_click(GntWM *wm, GntMouseEvent event, int x, int y, GntWidget *widget) | |
1494 { | |
1495 gboolean ret = TRUE; | |
1496 idle_update = TRUE; | |
1497 g_signal_emit(wm, signals[SIG_MOUSE_CLICK], 0, event, x, y, widget, &ret); | |
1498 return ret; | |
1499 } | |
1500 | |
1501 void gnt_wm_raise_window(GntWM *wm, GntWidget *widget) | |
1502 { | |
1503 g_signal_emit(wm, signals[SIG_GIVE_FOCUS], 0, widget); | |
1504 } | |
1505 |