]> Dogcows Code - chaz/openbox/blob - openbox/menuframe.c
Revert "Don't hide submenus immediately when unselecting the parent's entry"
[chaz/openbox] / openbox / menuframe.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 menuframe.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "menuframe.h"
21 #include "client.h"
22 #include "menu.h"
23 #include "screen.h"
24 #include "prop.h"
25 #include "actions.h"
26 #include "event.h"
27 #include "grab.h"
28 #include "openbox.h"
29 #include "mainloop.h"
30 #include "config.h"
31 #include "render/theme.h"
32
33 #define PADDING 2
34 #define MAX_MENU_WIDTH 400
35
36 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
37
38 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
39 LeaveWindowMask)
40 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
41 ButtonPressMask | ButtonReleaseMask)
42
43 GList *menu_frame_visible;
44 GHashTable *menu_frame_map;
45
46 static RrAppearance *a_sep;
47
48 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
49 ObMenuFrame *frame);
50 static void menu_entry_frame_free(ObMenuEntryFrame *self);
51 static void menu_frame_update(ObMenuFrame *self);
52 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
53 static void menu_frame_hide(ObMenuFrame *self);
54
55 static Window createWindow(Window parent, gulong mask,
56 XSetWindowAttributes *attrib)
57 {
58 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
59 RrDepth(ob_rr_inst), InputOutput,
60 RrVisual(ob_rr_inst), mask, attrib);
61 }
62
63 void menu_frame_startup(gboolean reconfig)
64 {
65 gint i;
66
67 a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
68 RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
69 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
70 a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
71 a_sep->texture[i].data.lineart.color =
72 ob_rr_theme->menu_sep_color;
73 }
74
75 if (reconfig) return;
76
77 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
78 }
79
80 void menu_frame_shutdown(gboolean reconfig)
81 {
82 RrAppearanceFree(a_sep);
83
84 if (reconfig) return;
85
86 g_hash_table_destroy(menu_frame_map);
87 }
88
89 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
90 {
91 ObMenuFrame *self;
92 XSetWindowAttributes attr;
93
94 self = g_new0(ObMenuFrame, 1);
95 self->type = Window_Menu;
96 self->menu = menu;
97 self->selected = NULL;
98 self->client = client;
99 self->direction_right = TRUE;
100 self->show_from = show_from;
101
102 attr.event_mask = FRAME_EVENTMASK;
103 self->window = createWindow(RootWindow(ob_display, ob_screen),
104 CWEventMask, &attr);
105
106 /* make it a popup menu type window */
107 PROP_SET32(self->window, net_wm_window_type, atom,
108 prop_atoms.net_wm_window_type_popup_menu);
109
110 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
111 XSetWindowBorder(ob_display, self->window,
112 RrColorPixel(ob_rr_theme->menu_border_color));
113
114 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
115
116 stacking_add(MENU_AS_WINDOW(self));
117
118 return self;
119 }
120
121 void menu_frame_free(ObMenuFrame *self)
122 {
123 if (self) {
124 while (self->entries) {
125 menu_entry_frame_free(self->entries->data);
126 self->entries = g_list_delete_link(self->entries, self->entries);
127 }
128
129 stacking_remove(MENU_AS_WINDOW(self));
130
131 RrAppearanceFree(self->a_items);
132
133 XDestroyWindow(ob_display, self->window);
134
135 g_free(self);
136 }
137 }
138
139 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
140 ObMenuFrame *frame)
141 {
142 ObMenuEntryFrame *self;
143 XSetWindowAttributes attr;
144
145 self = g_new0(ObMenuEntryFrame, 1);
146 self->entry = entry;
147 self->frame = frame;
148
149 menu_entry_ref(entry);
150
151 attr.event_mask = ENTRY_EVENTMASK;
152 self->window = createWindow(self->frame->window, CWEventMask, &attr);
153 self->text = createWindow(self->window, 0, NULL);
154 g_hash_table_insert(menu_frame_map, &self->window, self);
155 g_hash_table_insert(menu_frame_map, &self->text, self);
156 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
157 self->icon = createWindow(self->window, 0, NULL);
158 g_hash_table_insert(menu_frame_map, &self->icon, self);
159 }
160 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
161 self->bullet = createWindow(self->window, 0, NULL);
162 g_hash_table_insert(menu_frame_map, &self->bullet, self);
163 }
164
165 XMapWindow(ob_display, self->window);
166 XMapWindow(ob_display, self->text);
167
168 return self;
169 }
170
171 static void menu_entry_frame_free(ObMenuEntryFrame *self)
172 {
173 if (self) {
174 menu_entry_unref(self->entry);
175
176 XDestroyWindow(ob_display, self->text);
177 XDestroyWindow(ob_display, self->window);
178 g_hash_table_remove(menu_frame_map, &self->text);
179 g_hash_table_remove(menu_frame_map, &self->window);
180 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
181 XDestroyWindow(ob_display, self->icon);
182 g_hash_table_remove(menu_frame_map, &self->icon);
183 }
184 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
185 XDestroyWindow(ob_display, self->bullet);
186 g_hash_table_remove(menu_frame_map, &self->bullet);
187 }
188
189 g_free(self);
190 }
191 }
192
193 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
194 {
195 RECT_SET_POINT(self->area, x, y);
196 self->monitor = screen_find_monitor_point(x, y);
197 XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
198 }
199
200 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
201 {
202 gint dx, dy;
203
204 if (config_menu_middle) {
205 gint myx;
206
207 myx = *x;
208 *y -= self->area.height / 2;
209
210 /* try to the right of the cursor */
211 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
212 self->direction_right = TRUE;
213 if (dx != 0) {
214 /* try to the left of the cursor */
215 myx = *x - self->area.width;
216 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
217 self->direction_right = FALSE;
218 }
219 if (dx != 0) {
220 /* if didnt fit on either side so just use what it says */
221 myx = *x;
222 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
223 self->direction_right = TRUE;
224 }
225 *x = myx + dx;
226 *y += dy;
227 } else {
228 gint myx, myy;
229
230 myx = *x;
231 myy = *y;
232
233 /* try to the bottom right of the cursor */
234 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
235 self->direction_right = TRUE;
236 if (dx != 0 || dy != 0) {
237 /* try to the bottom left of the cursor */
238 myx = *x - self->area.width;
239 myy = *y;
240 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
241 self->direction_right = FALSE;
242 }
243 if (dx != 0 || dy != 0) {
244 /* try to the top right of the cursor */
245 myx = *x;
246 myy = *y - self->area.height;
247 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
248 self->direction_right = TRUE;
249 }
250 if (dx != 0 || dy != 0) {
251 /* try to the top left of the cursor */
252 myx = *x - self->area.width;
253 myy = *y - self->area.height;
254 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
255 self->direction_right = FALSE;
256 }
257 if (dx != 0 || dy != 0) {
258 /* if didnt fit on either side so just use what it says */
259 myx = *x;
260 myy = *y;
261 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
262 self->direction_right = TRUE;
263 }
264 *x = myx + dx;
265 *y = myy + dy;
266 }
267 }
268
269 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
270 {
271 gint overlapx, overlapy;
272 gint bwidth;
273
274 overlapx = ob_rr_theme->menu_overlap_x;
275 overlapy = ob_rr_theme->menu_overlap_y;
276 bwidth = ob_rr_theme->mbwidth;
277
278 if (self->direction_right)
279 *x = self->parent->area.x + self->parent->area.width -
280 overlapx - bwidth;
281 else
282 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
283
284 *y = self->parent->area.y + self->parent_entry->area.y;
285 if (config_menu_middle)
286 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
287 else
288 *y += overlapy;
289 }
290
291 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
292 gint *dx, gint *dy)
293 {
294 Rect *a = NULL;
295 gint pos, half;
296
297 *dx = *dy = 0;
298
299 a = screen_physical_area_monitor(screen_find_monitor_point(x, y));
300
301 half = g_list_length(self->entries) / 2;
302 pos = g_list_index(self->entries, self->selected);
303
304 /* if in the bottom half then check this stuff first, will keep the bottom
305 edge of the menu visible */
306 if (pos > half) {
307 *dx = MAX(*dx, a->x - x);
308 *dy = MAX(*dy, a->y - y);
309 }
310 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
311 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
312 /* if in the top half then check this stuff last, will keep the top
313 edge of the menu visible */
314 if (pos <= half) {
315 *dx = MAX(*dx, a->x - x);
316 *dy = MAX(*dy, a->y - y);
317 }
318
319 g_free(a);
320 }
321
322 static void menu_entry_frame_render(ObMenuEntryFrame *self)
323 {
324 RrAppearance *item_a, *text_a;
325 gint th; /* temp */
326 ObMenu *sub;
327 ObMenuFrame *frame = self->frame;
328
329 switch (self->entry->type) {
330 case OB_MENU_ENTRY_TYPE_NORMAL:
331 case OB_MENU_ENTRY_TYPE_SUBMENU:
332 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
333 !self->entry->data.normal.enabled ?
334 /* disabled */
335 (self == self->frame->selected ?
336 ob_rr_theme->a_menu_disabled_selected :
337 ob_rr_theme->a_menu_disabled) :
338 /* enabled */
339 (self == self->frame->selected ?
340 ob_rr_theme->a_menu_selected :
341 ob_rr_theme->a_menu_normal));
342 th = ITEM_HEIGHT;
343 break;
344 case OB_MENU_ENTRY_TYPE_SEPARATOR:
345 if (self->entry->data.separator.label) {
346 item_a = ob_rr_theme->a_menu_title;
347 th = ob_rr_theme->menu_title_height;
348 } else {
349 item_a = ob_rr_theme->a_menu_normal;
350 th = ob_rr_theme->menu_sep_width +
351 2*ob_rr_theme->menu_sep_paddingy;
352 }
353 break;
354 default:
355 g_assert_not_reached();
356 }
357
358 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
359 XResizeWindow(ob_display, self->window,
360 self->area.width, self->area.height);
361 item_a->surface.parent = self->frame->a_items;
362 item_a->surface.parentx = self->area.x;
363 item_a->surface.parenty = self->area.y;
364 RrPaint(item_a, self->window, self->area.width, self->area.height);
365
366 switch (self->entry->type) {
367 case OB_MENU_ENTRY_TYPE_NORMAL:
368 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
369 !self->entry->data.normal.enabled ?
370 /* disabled */
371 (self == self->frame->selected ?
372 ob_rr_theme->a_menu_text_disabled_selected :
373 ob_rr_theme->a_menu_text_disabled) :
374 /* enabled */
375 (self == self->frame->selected ?
376 ob_rr_theme->a_menu_text_selected :
377 ob_rr_theme->a_menu_text_normal));
378 text_a->texture[0].data.text.string = self->entry->data.normal.label;
379 if (self->entry->data.normal.shortcut &&
380 (self->frame->menu->show_all_shortcuts ||
381 self->entry->data.normal.shortcut_always_show ||
382 self->entry->data.normal.shortcut_position > 0))
383 {
384 text_a->texture[0].data.text.shortcut = TRUE;
385 text_a->texture[0].data.text.shortcut_pos =
386 self->entry->data.normal.shortcut_position;
387 } else
388 text_a->texture[0].data.text.shortcut = FALSE;
389 break;
390 case OB_MENU_ENTRY_TYPE_SUBMENU:
391 text_a = (self == self->frame->selected ?
392 ob_rr_theme->a_menu_text_selected :
393 ob_rr_theme->a_menu_text_normal);
394 sub = self->entry->data.submenu.submenu;
395 text_a->texture[0].data.text.string = sub ? sub->title : "";
396 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
397 sub->shortcut_always_show ||
398 sub->shortcut_position > 0))
399 {
400 text_a->texture[0].data.text.shortcut = TRUE;
401 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
402 } else
403 text_a->texture[0].data.text.shortcut = FALSE;
404 break;
405 case OB_MENU_ENTRY_TYPE_SEPARATOR:
406 if (self->entry->data.separator.label != NULL) {
407 text_a = ob_rr_theme->a_menu_text_title;
408 text_a->texture[0].data.text.string =
409 self->entry->data.separator.label;
410 }
411 else
412 text_a = ob_rr_theme->a_menu_text_normal;
413 break;
414 }
415
416 switch (self->entry->type) {
417 case OB_MENU_ENTRY_TYPE_NORMAL:
418 XMoveResizeWindow(ob_display, self->text,
419 self->frame->text_x, PADDING,
420 self->frame->text_w,
421 ITEM_HEIGHT - 2*PADDING);
422 text_a->surface.parent = item_a;
423 text_a->surface.parentx = self->frame->text_x;
424 text_a->surface.parenty = PADDING;
425 RrPaint(text_a, self->text, self->frame->text_w,
426 ITEM_HEIGHT - 2*PADDING);
427 break;
428 case OB_MENU_ENTRY_TYPE_SUBMENU:
429 XMoveResizeWindow(ob_display, self->text,
430 self->frame->text_x, PADDING,
431 self->frame->text_w - ITEM_HEIGHT,
432 ITEM_HEIGHT - 2*PADDING);
433 text_a->surface.parent = item_a;
434 text_a->surface.parentx = self->frame->text_x;
435 text_a->surface.parenty = PADDING;
436 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
437 ITEM_HEIGHT - 2*PADDING);
438 break;
439 case OB_MENU_ENTRY_TYPE_SEPARATOR:
440 if (self->entry->data.separator.label != NULL) {
441 /* labeled separator */
442 XMoveResizeWindow(ob_display, self->text,
443 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
444 self->area.width - 2*ob_rr_theme->paddingx,
445 ob_rr_theme->menu_title_height -
446 2*ob_rr_theme->paddingy);
447 text_a->surface.parent = item_a;
448 text_a->surface.parentx = ob_rr_theme->paddingx;
449 text_a->surface.parenty = ob_rr_theme->paddingy;
450 RrPaint(text_a, self->text,
451 self->area.width - 2*ob_rr_theme->paddingx,
452 ob_rr_theme->menu_title_height -
453 2*ob_rr_theme->paddingy);
454 } else {
455 gint i;
456
457 /* unlabeled separator */
458 XMoveResizeWindow(ob_display, self->text, 0, 0,
459 self->area.width,
460 ob_rr_theme->menu_sep_width +
461 2*ob_rr_theme->menu_sep_paddingy);
462
463 a_sep->surface.parent = item_a;
464 a_sep->surface.parentx = 0;
465 a_sep->surface.parenty = 0;
466 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
467 a_sep->texture[i].data.lineart.x1 =
468 ob_rr_theme->menu_sep_paddingx;
469 a_sep->texture[i].data.lineart.y1 =
470 ob_rr_theme->menu_sep_paddingy + i;
471 a_sep->texture[i].data.lineart.x2 =
472 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
473 a_sep->texture[i].data.lineart.y2 =
474 ob_rr_theme->menu_sep_paddingy + i;
475 }
476
477 RrPaint(a_sep, self->text, self->area.width,
478 ob_rr_theme->menu_sep_width +
479 2*ob_rr_theme->menu_sep_paddingy);
480 }
481 break;
482 }
483
484 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
485 self->entry->data.normal.icon)
486 {
487 RrAppearance *clear;
488
489 XMoveResizeWindow(ob_display, self->icon,
490 PADDING, frame->item_margin.top,
491 ITEM_HEIGHT - frame->item_margin.top
492 - frame->item_margin.bottom,
493 ITEM_HEIGHT - frame->item_margin.top
494 - frame->item_margin.bottom);
495
496 clear = ob_rr_theme->a_clear_tex;
497 RrAppearanceClearTextures(clear);
498 clear->texture[0].type = RR_TEXTURE_IMAGE;
499 clear->texture[0].data.image.image =
500 self->entry->data.normal.icon;
501 clear->texture[0].data.image.alpha =
502 self->entry->data.normal.icon_alpha;
503 clear->surface.parent = item_a;
504 clear->surface.parentx = PADDING;
505 clear->surface.parenty = frame->item_margin.top;
506 RrPaint(clear, self->icon,
507 ITEM_HEIGHT - frame->item_margin.top
508 - frame->item_margin.bottom,
509 ITEM_HEIGHT - frame->item_margin.top
510 - frame->item_margin.bottom);
511 XMapWindow(ob_display, self->icon);
512 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
513 self->entry->data.normal.mask)
514 {
515 RrColor *c;
516 RrAppearance *clear;
517
518 XMoveResizeWindow(ob_display, self->icon,
519 PADDING, frame->item_margin.top,
520 ITEM_HEIGHT - frame->item_margin.top
521 - frame->item_margin.bottom,
522 ITEM_HEIGHT - frame->item_margin.top
523 - frame->item_margin.bottom);
524
525 clear = ob_rr_theme->a_clear_tex;
526 RrAppearanceClearTextures(clear);
527 clear->texture[0].type = RR_TEXTURE_MASK;
528 clear->texture[0].data.mask.mask =
529 self->entry->data.normal.mask;
530
531 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
532 !self->entry->data.normal.enabled ?
533 /* disabled */
534 (self == self->frame->selected ?
535 self->entry->data.normal.mask_disabled_selected_color :
536 self->entry->data.normal.mask_disabled_color) :
537 /* enabled */
538 (self == self->frame->selected ?
539 self->entry->data.normal.mask_selected_color :
540 self->entry->data.normal.mask_normal_color));
541 clear->texture[0].data.mask.color = c;
542
543 clear->surface.parent = item_a;
544 clear->surface.parentx = PADDING;
545 clear->surface.parenty = frame->item_margin.top;
546 RrPaint(clear, self->icon,
547 ITEM_HEIGHT - frame->item_margin.top
548 - frame->item_margin.bottom,
549 ITEM_HEIGHT - frame->item_margin.top
550 - frame->item_margin.bottom);
551 XMapWindow(ob_display, self->icon);
552 } else
553 XUnmapWindow(ob_display, self->icon);
554
555 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
556 RrAppearance *bullet_a;
557 XMoveResizeWindow(ob_display, self->bullet,
558 self->frame->text_x + self->frame->text_w -
559 ITEM_HEIGHT + PADDING, PADDING,
560 ITEM_HEIGHT - 2*PADDING,
561 ITEM_HEIGHT - 2*PADDING);
562 bullet_a = (self == self->frame->selected ?
563 ob_rr_theme->a_menu_bullet_selected :
564 ob_rr_theme->a_menu_bullet_normal);
565 bullet_a->surface.parent = item_a;
566 bullet_a->surface.parentx =
567 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
568 bullet_a->surface.parenty = PADDING;
569 RrPaint(bullet_a, self->bullet,
570 ITEM_HEIGHT - 2*PADDING,
571 ITEM_HEIGHT - 2*PADDING);
572 XMapWindow(ob_display, self->bullet);
573 } else
574 XUnmapWindow(ob_display, self->bullet);
575
576 XFlush(ob_display);
577 }
578
579 /*! this code is taken from the menu_frame_render. if that changes, this won't
580 work.. */
581 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
582 gboolean first_entry,
583 gboolean last_entry)
584 {
585 ObMenuEntryType t;
586 gint h = 0;
587
588 h += 2*PADDING;
589
590 if (self)
591 t = self->entry->type;
592 else
593 /* this is the More... entry, it's NORMAL type */
594 t = OB_MENU_ENTRY_TYPE_NORMAL;
595
596 switch (t) {
597 case OB_MENU_ENTRY_TYPE_NORMAL:
598 case OB_MENU_ENTRY_TYPE_SUBMENU:
599 h += ob_rr_theme->menu_font_height;
600 break;
601 case OB_MENU_ENTRY_TYPE_SEPARATOR:
602 if (self->entry->data.separator.label != NULL) {
603 h += ob_rr_theme->menu_title_height +
604 (ob_rr_theme->mbwidth - PADDING) * 2;
605
606 /* if the first entry is a labeled separator, then make its border
607 overlap with the menu's outside border */
608 if (first_entry)
609 h -= ob_rr_theme->mbwidth;
610 /* if the last entry is a labeled separator, then make its border
611 overlap with the menu's outside border */
612 if (last_entry)
613 h -= ob_rr_theme->mbwidth;
614 } else {
615 h += ob_rr_theme->menu_sep_width +
616 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
617 }
618 break;
619 }
620
621 return h;
622 }
623
624 void menu_frame_render(ObMenuFrame *self)
625 {
626 gint w = 0, h = 0;
627 gint tw, th; /* temps */
628 GList *it;
629 gboolean has_icon = FALSE;
630 ObMenu *sub;
631 ObMenuEntryFrame *e;
632
633 /* find text dimensions */
634
635 STRUT_SET(self->item_margin, 0, 0, 0, 0);
636
637 if (self->entries) {
638 gint l, t, r, b;
639
640 e = self->entries->data;
641 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
642 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
643 tw += 2*PADDING;
644
645 th = ITEM_HEIGHT;
646
647 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
648 STRUT_SET(self->item_margin,
649 MAX(self->item_margin.left, l),
650 MAX(self->item_margin.top, t),
651 MAX(self->item_margin.right, r),
652 MAX(self->item_margin.bottom, b));
653 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
654 STRUT_SET(self->item_margin,
655 MAX(self->item_margin.left, l),
656 MAX(self->item_margin.top, t),
657 MAX(self->item_margin.right, r),
658 MAX(self->item_margin.bottom, b));
659 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
660 STRUT_SET(self->item_margin,
661 MAX(self->item_margin.left, l),
662 MAX(self->item_margin.top, t),
663 MAX(self->item_margin.right, r),
664 MAX(self->item_margin.bottom, b));
665 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
666 STRUT_SET(self->item_margin,
667 MAX(self->item_margin.left, l),
668 MAX(self->item_margin.top, t),
669 MAX(self->item_margin.right, r),
670 MAX(self->item_margin.bottom, b));
671 }
672
673 /* render the entries */
674
675 for (it = self->entries; it; it = g_list_next(it)) {
676 RrAppearance *text_a;
677 e = it->data;
678
679 /* if the first entry is a labeled separator, then make its border
680 overlap with the menu's outside border */
681 if (it == self->entries &&
682 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
683 e->entry->data.separator.label)
684 {
685 h -= ob_rr_theme->mbwidth;
686 }
687
688 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
689 e->entry->data.separator.label)
690 {
691 e->border = ob_rr_theme->mbwidth;
692 }
693
694 RECT_SET_POINT(e->area, 0, h+e->border);
695 XMoveWindow(ob_display, e->window,
696 e->area.x-e->border, e->area.y-e->border);
697 XSetWindowBorderWidth(ob_display, e->window, e->border);
698 XSetWindowBorder(ob_display, e->window,
699 RrColorPixel(ob_rr_theme->menu_border_color));
700
701 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
702 !e->entry->data.normal.enabled ?
703 /* disabled */
704 (e == self->selected ?
705 ob_rr_theme->a_menu_text_disabled_selected :
706 ob_rr_theme->a_menu_text_disabled) :
707 /* enabled */
708 (e == self->selected ?
709 ob_rr_theme->a_menu_text_selected :
710 ob_rr_theme->a_menu_text_normal));
711 switch (e->entry->type) {
712 case OB_MENU_ENTRY_TYPE_NORMAL:
713 text_a->texture[0].data.text.string = e->entry->data.normal.label;
714 tw = RrMinWidth(text_a);
715 tw = MIN(tw, MAX_MENU_WIDTH);
716 th = ob_rr_theme->menu_font_height;
717
718 if (e->entry->data.normal.icon ||
719 e->entry->data.normal.mask)
720 has_icon = TRUE;
721 break;
722 case OB_MENU_ENTRY_TYPE_SUBMENU:
723 sub = e->entry->data.submenu.submenu;
724 text_a->texture[0].data.text.string = sub ? sub->title : "";
725 tw = RrMinWidth(text_a);
726 tw = MIN(tw, MAX_MENU_WIDTH);
727 th = ob_rr_theme->menu_font_height;
728
729 if (e->entry->data.normal.icon ||
730 e->entry->data.normal.mask)
731 has_icon = TRUE;
732
733 tw += ITEM_HEIGHT - PADDING;
734 break;
735 case OB_MENU_ENTRY_TYPE_SEPARATOR:
736 if (e->entry->data.separator.label != NULL) {
737 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
738 e->entry->data.separator.label;
739 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
740 2*ob_rr_theme->paddingx;
741 tw = MIN(tw, MAX_MENU_WIDTH);
742 th = ob_rr_theme->menu_title_height +
743 (ob_rr_theme->mbwidth - PADDING) *2;
744 } else {
745 tw = 0;
746 th = ob_rr_theme->menu_sep_width +
747 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
748 }
749 break;
750 }
751 tw += 2*PADDING;
752 th += 2*PADDING;
753 w = MAX(w, tw);
754 h += th;
755 }
756
757 /* if the last entry is a labeled separator, then make its border
758 overlap with the menu's outside border */
759 it = g_list_last(self->entries);
760 e = it ? it->data : NULL;
761 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
762 e->entry->data.separator.label)
763 {
764 h -= ob_rr_theme->mbwidth;
765 }
766
767 self->text_x = PADDING;
768 self->text_w = w;
769
770 if (self->entries) {
771 if (has_icon) {
772 w += ITEM_HEIGHT + PADDING;
773 self->text_x += ITEM_HEIGHT + PADDING;
774 }
775 }
776
777 if (!w) w = 10;
778 if (!h) h = 3;
779
780 XResizeWindow(ob_display, self->window, w, h);
781
782 self->inner_w = w;
783
784 RrPaint(self->a_items, self->window, w, h);
785
786 for (it = self->entries; it; it = g_list_next(it))
787 menu_entry_frame_render(it->data);
788
789 w += ob_rr_theme->mbwidth * 2;
790 h += ob_rr_theme->mbwidth * 2;
791
792 RECT_SET_SIZE(self->area, w, h);
793
794 XFlush(ob_display);
795 }
796
797 static void menu_frame_update(ObMenuFrame *self)
798 {
799 GList *mit, *fit;
800 Rect *a;
801 gint h;
802
803 menu_pipe_execute(self->menu);
804 menu_find_submenus(self->menu);
805
806 self->selected = NULL;
807
808 /* start at show_from */
809 mit = g_list_nth(self->menu->entries, self->show_from);
810
811 /* go through the menu's and frame's entries and connect the frame entries
812 to the menu entries */
813 for (fit = self->entries; mit && fit;
814 mit = g_list_next(mit), fit = g_list_next(fit))
815 {
816 ObMenuEntryFrame *f = fit->data;
817 f->entry = mit->data;
818 }
819
820 /* if there are more menu entries than in the frame, add them */
821 while (mit) {
822 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
823 self->entries = g_list_append(self->entries, e);
824 mit = g_list_next(mit);
825 }
826
827 /* if there are more frame entries than menu entries then get rid of
828 them */
829 while (fit) {
830 GList *n = g_list_next(fit);
831 menu_entry_frame_free(fit->data);
832 self->entries = g_list_delete_link(self->entries, fit);
833 fit = n;
834 }
835
836 /* * make the menu fit on the screen */
837
838 /* calculate the height of the menu */
839 h = 0;
840 for (fit = self->entries; fit; fit = g_list_next(fit))
841 h += menu_entry_frame_get_height(fit->data,
842 fit == self->entries,
843 g_list_next(fit) == NULL);
844 /* add the border at the top and bottom */
845 h += ob_rr_theme->mbwidth * 2;
846
847 a = screen_physical_area_monitor(self->monitor);
848
849 if (h > a->height) {
850 GList *flast, *tmp;
851 gboolean last_entry = TRUE;
852
853 /* take the height of our More... entry into account */
854 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
855
856 /* start at the end of the entries */
857 flast = g_list_last(self->entries);
858
859 /* pull out all the entries from the frame that don't
860 fit on the screen, leaving at least 1 though */
861 while (h > a->height && g_list_previous(flast) != NULL) {
862 /* update the height, without this entry */
863 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
864
865 /* destroy the entry we're not displaying */
866 tmp = flast;
867 flast = g_list_previous(flast);
868 menu_entry_frame_free(tmp->data);
869 self->entries = g_list_delete_link(self->entries, tmp);
870
871 /* only the first one that we see is the last entry in the menu */
872 last_entry = FALSE;
873 };
874
875 {
876 ObMenuEntry *more_entry;
877 ObMenuEntryFrame *more_frame;
878 /* make the More... menu entry frame which will display in this
879 frame.
880 if self->menu->more_menu is NULL that means that this is already
881 More... menu, so just use ourself.
882 */
883 more_entry = menu_get_more((self->menu->more_menu ?
884 self->menu->more_menu :
885 self->menu),
886 /* continue where we left off */
887 self->show_from +
888 g_list_length(self->entries));
889 more_frame = menu_entry_frame_new(more_entry, self);
890 /* make it get deleted when the menu frame goes away */
891 menu_entry_unref(more_entry);
892
893 /* add our More... entry to the frame */
894 self->entries = g_list_append(self->entries, more_frame);
895 }
896 }
897
898 g_free(a);
899
900 menu_frame_render(self);
901 }
902
903 static gboolean menu_frame_is_visible(ObMenuFrame *self)
904 {
905 return !!(g_list_find(menu_frame_visible, self));
906 }
907
908 static gboolean menu_frame_show(ObMenuFrame *self)
909 {
910 GList *it;
911
912 /* determine if the underlying menu is already visible */
913 for (it = menu_frame_visible; it; it = g_list_next(it)) {
914 ObMenuFrame *f = it->data;
915 if (f->menu == self->menu)
916 break;
917 }
918 if (!it) {
919 if (self->menu->update_func)
920 if (!self->menu->update_func(self, self->menu->data))
921 return FALSE;
922 }
923
924 if (menu_frame_visible == NULL) {
925 /* no menus shown yet */
926
927 /* grab the pointer in such a way as to pass through "owner events"
928 so that we can get enter/leave notifies in the menu. */
929 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
930 return FALSE;
931 if (!grab_keyboard()) {
932 ungrab_pointer();
933 return FALSE;
934 }
935 }
936
937 menu_frame_update(self);
938
939 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
940
941 if (self->menu->show_func)
942 self->menu->show_func(self, self->menu->data);
943
944 return TRUE;
945 }
946
947 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
948 gboolean mouse)
949 {
950 gint px, py;
951
952 if (menu_frame_is_visible(self))
953 return TRUE;
954 if (!menu_frame_show(self))
955 return FALSE;
956
957 if (self->menu->place_func)
958 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
959 else
960 menu_frame_place_topmenu(self, &x, &y);
961
962 menu_frame_move(self, x, y);
963
964 XMapWindow(ob_display, self->window);
965
966 if (screen_pointer_pos(&px, &py)) {
967 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
968 if (e && e->frame == self)
969 e->ignore_enters++;
970 }
971
972 return TRUE;
973 }
974
975 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
976 ObMenuEntryFrame *parent_entry)
977 {
978 gint x, y, dx, dy;
979 gint px, py;
980
981 if (menu_frame_is_visible(self))
982 return TRUE;
983
984 self->monitor = parent->monitor;
985 self->parent = parent;
986 self->parent_entry = parent_entry;
987
988 /* set up parent's child to be us */
989 if (parent->child)
990 menu_frame_hide(parent->child);
991 parent->child = self;
992
993 if (!menu_frame_show(self))
994 return FALSE;
995
996 menu_frame_place_submenu(self, &x, &y);
997 menu_frame_move_on_screen(self, x, y, &dx, &dy);
998
999 if (dx != 0) {
1000 /*try the other side */
1001 self->direction_right = !self->direction_right;
1002 menu_frame_place_submenu(self, &x, &y);
1003 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1004 }
1005 menu_frame_move(self, x + dx, y + dy);
1006
1007 XMapWindow(ob_display, self->window);
1008
1009 if (screen_pointer_pos(&px, &py)) {
1010 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1011 if (e && e->frame == self)
1012 e->ignore_enters++;
1013 }
1014
1015 return TRUE;
1016 }
1017
1018 static void menu_frame_hide(ObMenuFrame *self)
1019 {
1020 GList *it = g_list_find(menu_frame_visible, self);
1021 gulong ignore_start;
1022
1023 if (!it)
1024 return;
1025
1026 if (self->menu->hide_func)
1027 self->menu->hide_func(self, self->menu->data);
1028
1029 if (self->child)
1030 menu_frame_hide(self->child);
1031
1032 if (self->parent)
1033 self->parent->child = NULL;
1034 self->parent = NULL;
1035 self->parent_entry = NULL;
1036
1037 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1038
1039 if (menu_frame_visible == NULL) {
1040 /* last menu shown */
1041 ungrab_pointer();
1042 ungrab_keyboard();
1043 }
1044
1045 ignore_start = event_start_ignore_all_enters();
1046 XUnmapWindow(ob_display, self->window);
1047 event_end_ignore_all_enters(ignore_start);
1048
1049 menu_frame_free(self);
1050 }
1051
1052 void menu_frame_hide_all(void)
1053 {
1054 GList *it;
1055
1056 if (config_submenu_show_delay) {
1057 /* remove any submenu open requests */
1058 ob_main_loop_timeout_remove(ob_main_loop,
1059 menu_entry_frame_submenu_timeout);
1060 }
1061 if ((it = g_list_last(menu_frame_visible)))
1062 menu_frame_hide(it->data);
1063 }
1064
1065 void menu_frame_hide_all_client(ObClient *client)
1066 {
1067 GList *it = g_list_last(menu_frame_visible);
1068 if (it) {
1069 ObMenuFrame *f = it->data;
1070 if (f->client == client) {
1071 if (config_submenu_show_delay) {
1072 /* remove any submenu open requests */
1073 ob_main_loop_timeout_remove(ob_main_loop,
1074 menu_entry_frame_submenu_timeout);
1075 }
1076 menu_frame_hide(f);
1077 }
1078 }
1079 }
1080
1081 ObMenuFrame* menu_frame_under(gint x, gint y)
1082 {
1083 ObMenuFrame *ret = NULL;
1084 GList *it;
1085
1086 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1087 ObMenuFrame *f = it->data;
1088
1089 if (RECT_CONTAINS(f->area, x, y)) {
1090 ret = f;
1091 break;
1092 }
1093 }
1094 return ret;
1095 }
1096
1097 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1098 {
1099 ObMenuFrame *frame;
1100 ObMenuEntryFrame *ret = NULL;
1101 GList *it;
1102
1103 if ((frame = menu_frame_under(x, y))) {
1104 x -= ob_rr_theme->mbwidth + frame->area.x;
1105 y -= ob_rr_theme->mbwidth + frame->area.y;
1106
1107 for (it = frame->entries; it; it = g_list_next(it)) {
1108 ObMenuEntryFrame *e = it->data;
1109
1110 if (RECT_CONTAINS(e->area, x, y)) {
1111 ret = e;
1112 break;
1113 }
1114 }
1115 }
1116 return ret;
1117 }
1118
1119 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1120 {
1121 g_assert(menu_frame_visible);
1122 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1123 return FALSE;
1124 }
1125
1126 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1127 gboolean immediate)
1128 {
1129 ObMenuEntryFrame *old = self->selected;
1130 ObMenuFrame *oldchild = self->child;
1131
1132 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1133 entry = old;
1134
1135 if (old == entry) return;
1136
1137 if (config_submenu_show_delay) {
1138 /* remove any submenu open requests */
1139 ob_main_loop_timeout_remove(ob_main_loop,
1140 menu_entry_frame_submenu_timeout);
1141 }
1142
1143 self->selected = entry;
1144
1145 if (old)
1146 menu_entry_frame_render(old);
1147 if (oldchild)
1148 menu_frame_hide(oldchild);
1149
1150 if (self->selected) {
1151 menu_entry_frame_render(self->selected);
1152
1153 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1154 if (config_submenu_show_delay && !immediate) {
1155 /* initiate a new submenu open request */
1156 ob_main_loop_timeout_add(ob_main_loop,
1157 config_submenu_show_delay * 1000,
1158 menu_entry_frame_submenu_timeout,
1159 self->selected, g_direct_equal,
1160 NULL);
1161 } else {
1162 menu_entry_frame_show_submenu(self->selected);
1163 }
1164 }
1165 }
1166 }
1167
1168 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1169 {
1170 ObMenuFrame *f;
1171
1172 if (!self->entry->data.submenu.submenu) return;
1173
1174 f = menu_frame_new(self->entry->data.submenu.submenu,
1175 self->entry->data.submenu.show_from,
1176 self->frame->client);
1177 /* pass our direction on to our child */
1178 f->direction_right = self->frame->direction_right;
1179
1180 menu_frame_show_submenu(f, self->frame, self);
1181 }
1182
1183 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1184 {
1185 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1186 self->entry->data.normal.enabled)
1187 {
1188 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1189 gets freed */
1190 ObMenuEntry *entry = self->entry;
1191 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1192 gpointer data = self->frame->menu->data;
1193 GSList *acts = self->entry->data.normal.actions;
1194 ObClient *client = self->frame->client;
1195 ObMenuFrame *frame = self->frame;
1196
1197 /* release grabs before executing the shit */
1198 if (!(state & ControlMask)) {
1199 menu_frame_hide_all();
1200 frame = NULL;
1201 }
1202
1203 if (func)
1204 func(entry, frame, client, state, data);
1205 else
1206 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1207 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1208 }
1209 }
1210
1211 void menu_frame_select_previous(ObMenuFrame *self)
1212 {
1213 GList *it = NULL, *start;
1214
1215 if (self->entries) {
1216 start = it = g_list_find(self->entries, self->selected);
1217 while (TRUE) {
1218 ObMenuEntryFrame *e;
1219
1220 it = it ? g_list_previous(it) : g_list_last(self->entries);
1221 if (it == start)
1222 break;
1223
1224 if (it) {
1225 e = it->data;
1226 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1227 break;
1228 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1229 break;
1230 }
1231 }
1232 }
1233 menu_frame_select(self, it ? it->data : NULL, TRUE);
1234 }
1235
1236 void menu_frame_select_next(ObMenuFrame *self)
1237 {
1238 GList *it = NULL, *start;
1239
1240 if (self->entries) {
1241 start = it = g_list_find(self->entries, self->selected);
1242 while (TRUE) {
1243 ObMenuEntryFrame *e;
1244
1245 it = it ? g_list_next(it) : self->entries;
1246 if (it == start)
1247 break;
1248
1249 if (it) {
1250 e = it->data;
1251 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1252 break;
1253 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1254 break;
1255 }
1256 }
1257 }
1258 menu_frame_select(self, it ? it->data : NULL, TRUE);
1259 }
This page took 0.095998 seconds and 5 git commands to generate.