1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 menuframe.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "menuframe.h"
31 #include "render/theme.h"
34 #define MAX_MENU_WIDTH 400
36 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
38 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
40 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
41 ButtonPressMask | ButtonReleaseMask)
43 GList *menu_frame_visible;
44 GHashTable *menu_frame_map;
46 static RrAppearance *a_sep;
48 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
50 static void menu_entry_frame_free(ObMenuEntryFrame *self);
51 static void menu_frame_update(ObMenuFrame *self);
52 static gboolean submenu_show_timeout(gpointer data);
53 static void menu_frame_hide(ObMenuFrame *self);
55 static gboolean submenu_hide_timeout(gpointer data);
57 static Window createWindow(Window parent, gulong mask,
58 XSetWindowAttributes *attrib)
60 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
61 RrDepth(ob_rr_inst), InputOutput,
62 RrVisual(ob_rr_inst), mask, attrib);
65 static void client_dest(ObClient *client, gpointer data)
69 /* menus can be associated with a client, so null those refs since
70 we are disappearing now */
71 for (it = menu_frame_visible; it; it = g_list_next(it)) {
72 ObMenuFrame *f = it->data;
73 if (f->client == client)
78 void menu_frame_startup(gboolean reconfig)
82 a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
83 RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
84 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
85 a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
86 a_sep->texture[i].data.lineart.color =
87 ob_rr_theme->menu_sep_color;
92 client_add_destroy_notify(client_dest, NULL);
93 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
96 void menu_frame_shutdown(gboolean reconfig)
98 RrAppearanceFree(a_sep);
100 if (reconfig) return;
102 client_remove_destroy_notify(client_dest);
103 g_hash_table_destroy(menu_frame_map);
106 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
109 XSetWindowAttributes attr;
111 self = g_new0(ObMenuFrame, 1);
112 self->type = Window_Menu;
114 self->selected = NULL;
115 self->client = client;
116 self->direction_right = TRUE;
117 self->show_from = show_from;
119 attr.event_mask = FRAME_EVENTMASK;
120 self->window = createWindow(RootWindow(ob_display, ob_screen),
123 /* make it a popup menu type window */
124 PROP_SET32(self->window, net_wm_window_type, atom,
125 prop_atoms.net_wm_window_type_popup_menu);
127 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
128 XSetWindowBorder(ob_display, self->window,
129 RrColorPixel(ob_rr_theme->menu_border_color));
131 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
133 stacking_add(MENU_AS_WINDOW(self));
138 void menu_frame_free(ObMenuFrame *self)
141 while (self->entries) {
142 menu_entry_frame_free(self->entries->data);
143 self->entries = g_list_delete_link(self->entries, self->entries);
146 stacking_remove(MENU_AS_WINDOW(self));
148 RrAppearanceFree(self->a_items);
150 XDestroyWindow(ob_display, self->window);
156 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
159 ObMenuEntryFrame *self;
160 XSetWindowAttributes attr;
162 self = g_new0(ObMenuEntryFrame, 1);
166 menu_entry_ref(entry);
168 attr.event_mask = ENTRY_EVENTMASK;
169 self->window = createWindow(self->frame->window, CWEventMask, &attr);
170 self->text = createWindow(self->window, 0, NULL);
171 g_hash_table_insert(menu_frame_map, &self->window, self);
172 g_hash_table_insert(menu_frame_map, &self->text, self);
173 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
174 self->icon = createWindow(self->window, 0, NULL);
175 g_hash_table_insert(menu_frame_map, &self->icon, self);
177 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
178 self->bullet = createWindow(self->window, 0, NULL);
179 g_hash_table_insert(menu_frame_map, &self->bullet, self);
182 XMapWindow(ob_display, self->window);
183 XMapWindow(ob_display, self->text);
188 static void menu_entry_frame_free(ObMenuEntryFrame *self)
191 menu_entry_unref(self->entry);
193 XDestroyWindow(ob_display, self->text);
194 XDestroyWindow(ob_display, self->window);
195 g_hash_table_remove(menu_frame_map, &self->text);
196 g_hash_table_remove(menu_frame_map, &self->window);
197 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
198 XDestroyWindow(ob_display, self->icon);
199 g_hash_table_remove(menu_frame_map, &self->icon);
201 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
202 XDestroyWindow(ob_display, self->bullet);
203 g_hash_table_remove(menu_frame_map, &self->bullet);
210 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
212 RECT_SET_POINT(self->area, x, y);
213 self->monitor = screen_find_monitor_point(x, y);
214 XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
217 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
221 if (config_menu_middle) {
225 *y -= self->area.height / 2;
227 /* try to the right of the cursor */
228 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
229 self->direction_right = TRUE;
231 /* try to the left of the cursor */
232 myx = *x - self->area.width;
233 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
234 self->direction_right = FALSE;
237 /* if didnt fit on either side so just use what it says */
239 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
240 self->direction_right = TRUE;
250 /* try to the bottom right of the cursor */
251 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
252 self->direction_right = TRUE;
253 if (dx != 0 || dy != 0) {
254 /* try to the bottom left of the cursor */
255 myx = *x - self->area.width;
257 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
258 self->direction_right = FALSE;
260 if (dx != 0 || dy != 0) {
261 /* try to the top right of the cursor */
263 myy = *y - self->area.height;
264 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
265 self->direction_right = TRUE;
267 if (dx != 0 || dy != 0) {
268 /* try to the top left of the cursor */
269 myx = *x - self->area.width;
270 myy = *y - self->area.height;
271 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
272 self->direction_right = FALSE;
274 if (dx != 0 || dy != 0) {
275 /* if didnt fit on either side so just use what it says */
278 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
279 self->direction_right = TRUE;
286 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
288 gint overlapx, overlapy;
291 overlapx = ob_rr_theme->menu_overlap_x;
292 overlapy = ob_rr_theme->menu_overlap_y;
293 bwidth = ob_rr_theme->mbwidth;
295 if (self->direction_right)
296 *x = self->parent->area.x + self->parent->area.width -
299 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
301 *y = self->parent->area.y + self->parent_entry->area.y;
302 if (config_menu_middle)
303 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
308 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
316 a = screen_physical_area_monitor(screen_find_monitor_point(x, y));
318 half = g_list_length(self->entries) / 2;
319 pos = g_list_index(self->entries, self->selected);
321 /* if in the bottom half then check this stuff first, will keep the bottom
322 edge of the menu visible */
324 *dx = MAX(*dx, a->x - x);
325 *dy = MAX(*dy, a->y - y);
327 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
328 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
329 /* if in the top half then check this stuff last, will keep the top
330 edge of the menu visible */
332 *dx = MAX(*dx, a->x - x);
333 *dy = MAX(*dy, a->y - y);
339 static void menu_entry_frame_render(ObMenuEntryFrame *self)
341 RrAppearance *item_a, *text_a;
344 ObMenuFrame *frame = self->frame;
346 switch (self->entry->type) {
347 case OB_MENU_ENTRY_TYPE_NORMAL:
348 case OB_MENU_ENTRY_TYPE_SUBMENU:
349 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
350 !self->entry->data.normal.enabled ?
352 (self == self->frame->selected ?
353 ob_rr_theme->a_menu_disabled_selected :
354 ob_rr_theme->a_menu_disabled) :
356 (self == self->frame->selected ?
357 ob_rr_theme->a_menu_selected :
358 ob_rr_theme->a_menu_normal));
361 case OB_MENU_ENTRY_TYPE_SEPARATOR:
362 if (self->entry->data.separator.label) {
363 item_a = ob_rr_theme->a_menu_title;
364 th = ob_rr_theme->menu_title_height;
366 item_a = ob_rr_theme->a_menu_normal;
367 th = ob_rr_theme->menu_sep_width +
368 2*ob_rr_theme->menu_sep_paddingy;
372 g_assert_not_reached();
375 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
376 XResizeWindow(ob_display, self->window,
377 self->area.width, self->area.height);
378 item_a->surface.parent = self->frame->a_items;
379 item_a->surface.parentx = self->area.x;
380 item_a->surface.parenty = self->area.y;
381 RrPaint(item_a, self->window, self->area.width, self->area.height);
383 switch (self->entry->type) {
384 case OB_MENU_ENTRY_TYPE_NORMAL:
385 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
386 !self->entry->data.normal.enabled ?
388 (self == self->frame->selected ?
389 ob_rr_theme->a_menu_text_disabled_selected :
390 ob_rr_theme->a_menu_text_disabled) :
392 (self == self->frame->selected ?
393 ob_rr_theme->a_menu_text_selected :
394 ob_rr_theme->a_menu_text_normal));
395 text_a->texture[0].data.text.string = self->entry->data.normal.label;
396 if (self->entry->data.normal.shortcut &&
397 (self->frame->menu->show_all_shortcuts ||
398 self->entry->data.normal.shortcut_always_show ||
399 self->entry->data.normal.shortcut_position > 0))
401 text_a->texture[0].data.text.shortcut = TRUE;
402 text_a->texture[0].data.text.shortcut_pos =
403 self->entry->data.normal.shortcut_position;
405 text_a->texture[0].data.text.shortcut = FALSE;
407 case OB_MENU_ENTRY_TYPE_SUBMENU:
408 text_a = (self == self->frame->selected ?
409 ob_rr_theme->a_menu_text_selected :
410 ob_rr_theme->a_menu_text_normal);
411 sub = self->entry->data.submenu.submenu;
412 text_a->texture[0].data.text.string = sub ? sub->title : "";
413 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
414 sub->shortcut_always_show ||
415 sub->shortcut_position > 0))
417 text_a->texture[0].data.text.shortcut = TRUE;
418 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
420 text_a->texture[0].data.text.shortcut = FALSE;
422 case OB_MENU_ENTRY_TYPE_SEPARATOR:
423 if (self->entry->data.separator.label != NULL) {
424 text_a = ob_rr_theme->a_menu_text_title;
425 text_a->texture[0].data.text.string =
426 self->entry->data.separator.label;
429 text_a = ob_rr_theme->a_menu_text_normal;
433 switch (self->entry->type) {
434 case OB_MENU_ENTRY_TYPE_NORMAL:
435 XMoveResizeWindow(ob_display, self->text,
436 self->frame->text_x, PADDING,
438 ITEM_HEIGHT - 2*PADDING);
439 text_a->surface.parent = item_a;
440 text_a->surface.parentx = self->frame->text_x;
441 text_a->surface.parenty = PADDING;
442 RrPaint(text_a, self->text, self->frame->text_w,
443 ITEM_HEIGHT - 2*PADDING);
445 case OB_MENU_ENTRY_TYPE_SUBMENU:
446 XMoveResizeWindow(ob_display, self->text,
447 self->frame->text_x, PADDING,
448 self->frame->text_w - ITEM_HEIGHT,
449 ITEM_HEIGHT - 2*PADDING);
450 text_a->surface.parent = item_a;
451 text_a->surface.parentx = self->frame->text_x;
452 text_a->surface.parenty = PADDING;
453 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
454 ITEM_HEIGHT - 2*PADDING);
456 case OB_MENU_ENTRY_TYPE_SEPARATOR:
457 if (self->entry->data.separator.label != NULL) {
458 /* labeled separator */
459 XMoveResizeWindow(ob_display, self->text,
460 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
461 self->area.width - 2*ob_rr_theme->paddingx,
462 ob_rr_theme->menu_title_height -
463 2*ob_rr_theme->paddingy);
464 text_a->surface.parent = item_a;
465 text_a->surface.parentx = ob_rr_theme->paddingx;
466 text_a->surface.parenty = ob_rr_theme->paddingy;
467 RrPaint(text_a, self->text,
468 self->area.width - 2*ob_rr_theme->paddingx,
469 ob_rr_theme->menu_title_height -
470 2*ob_rr_theme->paddingy);
474 /* unlabeled separator */
475 XMoveResizeWindow(ob_display, self->text, 0, 0,
477 ob_rr_theme->menu_sep_width +
478 2*ob_rr_theme->menu_sep_paddingy);
480 a_sep->surface.parent = item_a;
481 a_sep->surface.parentx = 0;
482 a_sep->surface.parenty = 0;
483 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
484 a_sep->texture[i].data.lineart.x1 =
485 ob_rr_theme->menu_sep_paddingx;
486 a_sep->texture[i].data.lineart.y1 =
487 ob_rr_theme->menu_sep_paddingy + i;
488 a_sep->texture[i].data.lineart.x2 =
489 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
490 a_sep->texture[i].data.lineart.y2 =
491 ob_rr_theme->menu_sep_paddingy + i;
494 RrPaint(a_sep, self->text, self->area.width,
495 ob_rr_theme->menu_sep_width +
496 2*ob_rr_theme->menu_sep_paddingy);
501 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
502 self->entry->data.normal.icon)
506 XMoveResizeWindow(ob_display, self->icon,
507 PADDING, frame->item_margin.top,
508 ITEM_HEIGHT - frame->item_margin.top
509 - frame->item_margin.bottom,
510 ITEM_HEIGHT - frame->item_margin.top
511 - frame->item_margin.bottom);
513 clear = ob_rr_theme->a_clear_tex;
514 RrAppearanceClearTextures(clear);
515 clear->texture[0].type = RR_TEXTURE_IMAGE;
516 clear->texture[0].data.image.image =
517 self->entry->data.normal.icon;
518 clear->texture[0].data.image.alpha =
519 self->entry->data.normal.icon_alpha;
520 clear->surface.parent = item_a;
521 clear->surface.parentx = PADDING;
522 clear->surface.parenty = frame->item_margin.top;
523 RrPaint(clear, self->icon,
524 ITEM_HEIGHT - frame->item_margin.top
525 - frame->item_margin.bottom,
526 ITEM_HEIGHT - frame->item_margin.top
527 - frame->item_margin.bottom);
528 XMapWindow(ob_display, self->icon);
529 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
530 self->entry->data.normal.mask)
535 XMoveResizeWindow(ob_display, self->icon,
536 PADDING, frame->item_margin.top,
537 ITEM_HEIGHT - frame->item_margin.top
538 - frame->item_margin.bottom,
539 ITEM_HEIGHT - frame->item_margin.top
540 - frame->item_margin.bottom);
542 clear = ob_rr_theme->a_clear_tex;
543 RrAppearanceClearTextures(clear);
544 clear->texture[0].type = RR_TEXTURE_MASK;
545 clear->texture[0].data.mask.mask =
546 self->entry->data.normal.mask;
548 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
549 !self->entry->data.normal.enabled ?
551 (self == self->frame->selected ?
552 self->entry->data.normal.mask_disabled_selected_color :
553 self->entry->data.normal.mask_disabled_color) :
555 (self == self->frame->selected ?
556 self->entry->data.normal.mask_selected_color :
557 self->entry->data.normal.mask_normal_color));
558 clear->texture[0].data.mask.color = c;
560 clear->surface.parent = item_a;
561 clear->surface.parentx = PADDING;
562 clear->surface.parenty = frame->item_margin.top;
563 RrPaint(clear, self->icon,
564 ITEM_HEIGHT - frame->item_margin.top
565 - frame->item_margin.bottom,
566 ITEM_HEIGHT - frame->item_margin.top
567 - frame->item_margin.bottom);
568 XMapWindow(ob_display, self->icon);
570 XUnmapWindow(ob_display, self->icon);
572 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
573 RrAppearance *bullet_a;
574 XMoveResizeWindow(ob_display, self->bullet,
575 self->frame->text_x + self->frame->text_w -
576 ITEM_HEIGHT + PADDING, PADDING,
577 ITEM_HEIGHT - 2*PADDING,
578 ITEM_HEIGHT - 2*PADDING);
579 bullet_a = (self == self->frame->selected ?
580 ob_rr_theme->a_menu_bullet_selected :
581 ob_rr_theme->a_menu_bullet_normal);
582 bullet_a->surface.parent = item_a;
583 bullet_a->surface.parentx =
584 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
585 bullet_a->surface.parenty = PADDING;
586 RrPaint(bullet_a, self->bullet,
587 ITEM_HEIGHT - 2*PADDING,
588 ITEM_HEIGHT - 2*PADDING);
589 XMapWindow(ob_display, self->bullet);
591 XUnmapWindow(ob_display, self->bullet);
596 /*! this code is taken from the menu_frame_render. if that changes, this won't
598 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
599 gboolean first_entry,
608 t = self->entry->type;
610 /* this is the More... entry, it's NORMAL type */
611 t = OB_MENU_ENTRY_TYPE_NORMAL;
614 case OB_MENU_ENTRY_TYPE_NORMAL:
615 case OB_MENU_ENTRY_TYPE_SUBMENU:
616 h += ob_rr_theme->menu_font_height;
618 case OB_MENU_ENTRY_TYPE_SEPARATOR:
619 if (self->entry->data.separator.label != NULL) {
620 h += ob_rr_theme->menu_title_height +
621 (ob_rr_theme->mbwidth - PADDING) * 2;
623 /* if the first entry is a labeled separator, then make its border
624 overlap with the menu's outside border */
626 h -= ob_rr_theme->mbwidth;
627 /* if the last entry is a labeled separator, then make its border
628 overlap with the menu's outside border */
630 h -= ob_rr_theme->mbwidth;
632 h += ob_rr_theme->menu_sep_width +
633 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
641 void menu_frame_render(ObMenuFrame *self)
644 gint tw, th; /* temps */
646 gboolean has_icon = FALSE;
650 /* find text dimensions */
652 STRUT_SET(self->item_margin, 0, 0, 0, 0);
657 e = self->entries->data;
658 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
659 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
664 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
665 STRUT_SET(self->item_margin,
666 MAX(self->item_margin.left, l),
667 MAX(self->item_margin.top, t),
668 MAX(self->item_margin.right, r),
669 MAX(self->item_margin.bottom, b));
670 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
671 STRUT_SET(self->item_margin,
672 MAX(self->item_margin.left, l),
673 MAX(self->item_margin.top, t),
674 MAX(self->item_margin.right, r),
675 MAX(self->item_margin.bottom, b));
676 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
677 STRUT_SET(self->item_margin,
678 MAX(self->item_margin.left, l),
679 MAX(self->item_margin.top, t),
680 MAX(self->item_margin.right, r),
681 MAX(self->item_margin.bottom, b));
682 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
683 STRUT_SET(self->item_margin,
684 MAX(self->item_margin.left, l),
685 MAX(self->item_margin.top, t),
686 MAX(self->item_margin.right, r),
687 MAX(self->item_margin.bottom, b));
690 /* render the entries */
692 for (it = self->entries; it; it = g_list_next(it)) {
693 RrAppearance *text_a;
696 /* if the first entry is a labeled separator, then make its border
697 overlap with the menu's outside border */
698 if (it == self->entries &&
699 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
700 e->entry->data.separator.label)
702 h -= ob_rr_theme->mbwidth;
705 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
706 e->entry->data.separator.label)
708 e->border = ob_rr_theme->mbwidth;
711 RECT_SET_POINT(e->area, 0, h+e->border);
712 XMoveWindow(ob_display, e->window,
713 e->area.x-e->border, e->area.y-e->border);
714 XSetWindowBorderWidth(ob_display, e->window, e->border);
715 XSetWindowBorder(ob_display, e->window,
716 RrColorPixel(ob_rr_theme->menu_border_color));
718 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
719 !e->entry->data.normal.enabled ?
721 (e == self->selected ?
722 ob_rr_theme->a_menu_text_disabled_selected :
723 ob_rr_theme->a_menu_text_disabled) :
725 (e == self->selected ?
726 ob_rr_theme->a_menu_text_selected :
727 ob_rr_theme->a_menu_text_normal));
728 switch (e->entry->type) {
729 case OB_MENU_ENTRY_TYPE_NORMAL:
730 text_a->texture[0].data.text.string = e->entry->data.normal.label;
731 tw = RrMinWidth(text_a);
732 tw = MIN(tw, MAX_MENU_WIDTH);
733 th = ob_rr_theme->menu_font_height;
735 if (e->entry->data.normal.icon ||
736 e->entry->data.normal.mask)
739 case OB_MENU_ENTRY_TYPE_SUBMENU:
740 sub = e->entry->data.submenu.submenu;
741 text_a->texture[0].data.text.string = sub ? sub->title : "";
742 tw = RrMinWidth(text_a);
743 tw = MIN(tw, MAX_MENU_WIDTH);
744 th = ob_rr_theme->menu_font_height;
746 if (e->entry->data.normal.icon ||
747 e->entry->data.normal.mask)
750 tw += ITEM_HEIGHT - PADDING;
752 case OB_MENU_ENTRY_TYPE_SEPARATOR:
753 if (e->entry->data.separator.label != NULL) {
754 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
755 e->entry->data.separator.label;
756 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
757 2*ob_rr_theme->paddingx;
758 tw = MIN(tw, MAX_MENU_WIDTH);
759 th = ob_rr_theme->menu_title_height +
760 (ob_rr_theme->mbwidth - PADDING) *2;
763 th = ob_rr_theme->menu_sep_width +
764 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
774 /* if the last entry is a labeled separator, then make its border
775 overlap with the menu's outside border */
776 it = g_list_last(self->entries);
777 e = it ? it->data : NULL;
778 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
779 e->entry->data.separator.label)
781 h -= ob_rr_theme->mbwidth;
784 self->text_x = PADDING;
789 w += ITEM_HEIGHT + PADDING;
790 self->text_x += ITEM_HEIGHT + PADDING;
797 XResizeWindow(ob_display, self->window, w, h);
801 RrPaint(self->a_items, self->window, w, h);
803 for (it = self->entries; it; it = g_list_next(it))
804 menu_entry_frame_render(it->data);
806 w += ob_rr_theme->mbwidth * 2;
807 h += ob_rr_theme->mbwidth * 2;
809 RECT_SET_SIZE(self->area, w, h);
814 static void menu_frame_update(ObMenuFrame *self)
820 menu_pipe_execute(self->menu);
821 menu_find_submenus(self->menu);
823 self->selected = NULL;
825 /* start at show_from */
826 mit = g_list_nth(self->menu->entries, self->show_from);
828 /* go through the menu's and frame's entries and connect the frame entries
829 to the menu entries */
830 for (fit = self->entries; mit && fit;
831 mit = g_list_next(mit), fit = g_list_next(fit))
833 ObMenuEntryFrame *f = fit->data;
834 f->entry = mit->data;
837 /* if there are more menu entries than in the frame, add them */
839 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
840 self->entries = g_list_append(self->entries, e);
841 mit = g_list_next(mit);
844 /* if there are more frame entries than menu entries then get rid of
847 GList *n = g_list_next(fit);
848 menu_entry_frame_free(fit->data);
849 self->entries = g_list_delete_link(self->entries, fit);
853 /* * make the menu fit on the screen */
855 /* calculate the height of the menu */
857 for (fit = self->entries; fit; fit = g_list_next(fit))
858 h += menu_entry_frame_get_height(fit->data,
859 fit == self->entries,
860 g_list_next(fit) == NULL);
861 /* add the border at the top and bottom */
862 h += ob_rr_theme->mbwidth * 2;
864 a = screen_physical_area_monitor(self->monitor);
868 gboolean last_entry = TRUE;
870 /* take the height of our More... entry into account */
871 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
873 /* start at the end of the entries */
874 flast = g_list_last(self->entries);
876 /* pull out all the entries from the frame that don't
877 fit on the screen, leaving at least 1 though */
878 while (h > a->height && g_list_previous(flast) != NULL) {
879 /* update the height, without this entry */
880 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
882 /* destroy the entry we're not displaying */
884 flast = g_list_previous(flast);
885 menu_entry_frame_free(tmp->data);
886 self->entries = g_list_delete_link(self->entries, tmp);
888 /* only the first one that we see is the last entry in the menu */
893 ObMenuEntry *more_entry;
894 ObMenuEntryFrame *more_frame;
895 /* make the More... menu entry frame which will display in this
897 if self->menu->more_menu is NULL that means that this is already
898 More... menu, so just use ourself.
900 more_entry = menu_get_more((self->menu->more_menu ?
901 self->menu->more_menu :
903 /* continue where we left off */
905 g_list_length(self->entries));
906 more_frame = menu_entry_frame_new(more_entry, self);
907 /* make it get deleted when the menu frame goes away */
908 menu_entry_unref(more_entry);
910 /* add our More... entry to the frame */
911 self->entries = g_list_append(self->entries, more_frame);
917 menu_frame_render(self);
920 static gboolean menu_frame_is_visible(ObMenuFrame *self)
922 return !!(g_list_find(menu_frame_visible, self));
925 static gboolean menu_frame_show(ObMenuFrame *self)
929 /* determine if the underlying menu is already visible */
930 for (it = menu_frame_visible; it; it = g_list_next(it)) {
931 ObMenuFrame *f = it->data;
932 if (f->menu == self->menu)
936 if (self->menu->update_func)
937 if (!self->menu->update_func(self, self->menu->data))
941 if (menu_frame_visible == NULL) {
942 /* no menus shown yet */
944 /* grab the pointer in such a way as to pass through "owner events"
945 so that we can get enter/leave notifies in the menu. */
946 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
948 if (!grab_keyboard()) {
954 menu_frame_update(self);
956 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
958 if (self->menu->show_func)
959 self->menu->show_func(self, self->menu->data);
964 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
969 if (menu_frame_is_visible(self))
971 if (!menu_frame_show(self))
974 if (self->menu->place_func)
975 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
977 menu_frame_place_topmenu(self, &x, &y);
979 menu_frame_move(self, x, y);
981 XMapWindow(ob_display, self->window);
983 if (screen_pointer_pos(&px, &py)) {
984 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
985 if (e && e->frame == self)
992 /*! Stop hiding an open submenu.
993 @child The OnMenuFrame of the submenu to be hidden
995 static void remove_submenu_hide_timeout(ObMenuFrame *child)
997 ob_main_loop_timeout_remove_data(ob_main_loop, submenu_hide_timeout,
1001 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1002 ObMenuEntryFrame *parent_entry)
1007 if (menu_frame_is_visible(self))
1010 self->monitor = parent->monitor;
1011 self->parent = parent;
1012 self->parent_entry = parent_entry;
1014 /* set up parent's child to be us */
1015 if ((parent->child) != self) {
1017 menu_frame_hide(parent->child);
1018 parent->child = self;
1019 parent->child_entry = parent_entry;
1022 if (!menu_frame_show(self))
1025 menu_frame_place_submenu(self, &x, &y);
1026 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1029 /*try the other side */
1030 self->direction_right = !self->direction_right;
1031 menu_frame_place_submenu(self, &x, &y);
1032 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1034 menu_frame_move(self, x + dx, y + dy);
1036 XMapWindow(ob_display, self->window);
1038 if (screen_pointer_pos(&px, &py)) {
1039 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1040 if (e && e->frame == self)
1047 static void menu_frame_hide(ObMenuFrame *self)
1049 GList *it = g_list_find(menu_frame_visible, self);
1050 gulong ignore_start;
1055 if (self->menu->hide_func)
1056 self->menu->hide_func(self, self->menu->data);
1059 menu_frame_hide(self->child);
1062 remove_submenu_hide_timeout(self);
1064 self->parent->child = NULL;
1065 self->parent->child_entry = NULL;
1067 self->parent = NULL;
1068 self->parent_entry = NULL;
1070 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1072 if (menu_frame_visible == NULL) {
1073 /* last menu shown */
1078 ignore_start = event_start_ignore_all_enters();
1079 XUnmapWindow(ob_display, self->window);
1080 event_end_ignore_all_enters(ignore_start);
1082 menu_frame_free(self);
1085 void menu_frame_hide_all(void)
1089 if (config_submenu_show_delay) {
1090 /* remove any submenu open requests */
1091 ob_main_loop_timeout_remove(ob_main_loop, submenu_show_timeout);
1093 if ((it = g_list_last(menu_frame_visible)))
1094 menu_frame_hide(it->data);
1097 ObMenuFrame* menu_frame_under(gint x, gint y)
1099 ObMenuFrame *ret = NULL;
1102 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1103 ObMenuFrame *f = it->data;
1105 if (RECT_CONTAINS(f->area, x, y)) {
1113 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1116 ObMenuEntryFrame *ret = NULL;
1119 if ((frame = menu_frame_under(x, y))) {
1120 x -= ob_rr_theme->mbwidth + frame->area.x;
1121 y -= ob_rr_theme->mbwidth + frame->area.y;
1123 for (it = frame->entries; it; it = g_list_next(it)) {
1124 ObMenuEntryFrame *e = it->data;
1126 if (RECT_CONTAINS(e->area, x, y)) {
1135 static gboolean submenu_show_timeout(gpointer data)
1137 g_assert(menu_frame_visible);
1138 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1142 static gboolean submenu_hide_timeout(gpointer data)
1144 g_assert(menu_frame_visible);
1145 menu_frame_hide((ObMenuFrame*)data);
1149 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1152 ObMenuEntryFrame *old = self->selected;
1153 ObMenuFrame *oldchild = self->child;
1154 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1156 /* if the user selected a separator, ignore it and reselect what we had
1158 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1162 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1165 /* if the user left this menu but we have a submenu open, move the
1166 selection back to that submenu */
1167 if (!entry && oldchild_entry)
1168 entry = oldchild_entry;
1170 if (config_submenu_show_delay) {
1171 /* remove any submenu open requests */
1172 ob_main_loop_timeout_remove(ob_main_loop, submenu_show_timeout);
1175 self->selected = entry;
1178 menu_entry_frame_render(old);
1180 if (oldchild_entry) {
1181 /* There is an open submenu */
1182 if (oldchild_entry == self->selected) {
1183 /* The open submenu has been reselected, so stop hiding the
1185 remove_submenu_hide_timeout(oldchild);
1187 else if (oldchild_entry == old) {
1188 /* The open submenu was selected and is no longer, so hide the
1190 if (immediate || config_submenu_hide_delay == 0)
1191 menu_frame_hide(oldchild);
1192 else if (config_submenu_hide_delay > 0)
1193 ob_main_loop_timeout_add(ob_main_loop,
1194 config_submenu_hide_delay * 1000,
1195 submenu_hide_timeout,
1196 oldchild, g_direct_equal,
1201 if (self->selected) {
1202 menu_entry_frame_render(self->selected);
1204 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1205 /* only show if the submenu isn't already showing */
1206 if (oldchild_entry != self->selected) {
1207 if (immediate || config_submenu_hide_delay == 0)
1208 menu_entry_frame_show_submenu(self->selected);
1209 else if (config_submenu_hide_delay > 0)
1210 ob_main_loop_timeout_add(ob_main_loop,
1211 config_submenu_show_delay * 1000,
1212 submenu_show_timeout,
1213 self->selected, g_direct_equal,
1216 /* hide the grandchildren of this menu. and move the cursor to
1218 else if (immediate && self->child && self->child->child) {
1219 menu_frame_hide(self->child->child);
1220 menu_frame_select(self->child, NULL, TRUE);
1226 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1230 if (!self->entry->data.submenu.submenu) return;
1232 f = menu_frame_new(self->entry->data.submenu.submenu,
1233 self->entry->data.submenu.show_from,
1234 self->frame->client);
1235 /* pass our direction on to our child */
1236 f->direction_right = self->frame->direction_right;
1238 menu_frame_show_submenu(f, self->frame, self);
1241 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1243 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1244 self->entry->data.normal.enabled)
1246 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1248 ObMenuEntry *entry = self->entry;
1249 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1250 gpointer data = self->frame->menu->data;
1251 GSList *acts = self->entry->data.normal.actions;
1252 ObClient *client = self->frame->client;
1253 ObMenuFrame *frame = self->frame;
1255 /* release grabs before executing the shit */
1256 if (!(state & ControlMask)) {
1257 event_cancel_all_key_grabs();
1262 func(entry, frame, client, state, data);
1264 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1265 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1269 void menu_frame_select_previous(ObMenuFrame *self)
1271 GList *it = NULL, *start;
1273 if (self->entries) {
1274 start = it = g_list_find(self->entries, self->selected);
1276 ObMenuEntryFrame *e;
1278 it = it ? g_list_previous(it) : g_list_last(self->entries);
1284 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1286 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1291 menu_frame_select(self, it ? it->data : NULL, FALSE);
1294 void menu_frame_select_next(ObMenuFrame *self)
1296 GList *it = NULL, *start;
1298 if (self->entries) {
1299 start = it = g_list_find(self->entries, self->selected);
1301 ObMenuEntryFrame *e;
1303 it = it ? g_list_next(it) : self->entries;
1309 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1311 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1316 menu_frame_select(self, it ? it->data : NULL, FALSE);
1319 void menu_frame_select_first(ObMenuFrame *self)
1323 if (self->entries) {
1324 for (it = self->entries; it; it = g_list_next(it)) {
1325 ObMenuEntryFrame *e = it->data;
1326 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1328 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1332 menu_frame_select(self, it ? it->data : NULL, FALSE);
1335 void menu_frame_select_last(ObMenuFrame *self)
1339 if (self->entries) {
1340 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1341 ObMenuEntryFrame *e = it->data;
1342 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1344 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1348 menu_frame_select(self, it ? it->data : NULL, FALSE);