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 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
658 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
663 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
664 STRUT_SET(self->item_margin,
665 MAX(self->item_margin.left, l),
666 MAX(self->item_margin.top, t),
667 MAX(self->item_margin.right, r),
668 MAX(self->item_margin.bottom, b));
669 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
670 STRUT_SET(self->item_margin,
671 MAX(self->item_margin.left, l),
672 MAX(self->item_margin.top, t),
673 MAX(self->item_margin.right, r),
674 MAX(self->item_margin.bottom, b));
675 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
676 STRUT_SET(self->item_margin,
677 MAX(self->item_margin.left, l),
678 MAX(self->item_margin.top, t),
679 MAX(self->item_margin.right, r),
680 MAX(self->item_margin.bottom, b));
681 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
682 STRUT_SET(self->item_margin,
683 MAX(self->item_margin.left, l),
684 MAX(self->item_margin.top, t),
685 MAX(self->item_margin.right, r),
686 MAX(self->item_margin.bottom, b));
689 /* render the entries */
691 for (it = self->entries; it; it = g_list_next(it)) {
692 RrAppearance *text_a;
695 /* if the first entry is a labeled separator, then make its border
696 overlap with the menu's outside border */
697 if (it == self->entries &&
698 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
699 e->entry->data.separator.label)
701 h -= ob_rr_theme->mbwidth;
704 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
705 e->entry->data.separator.label)
707 e->border = ob_rr_theme->mbwidth;
710 RECT_SET_POINT(e->area, 0, h+e->border);
711 XMoveWindow(ob_display, e->window,
712 e->area.x-e->border, e->area.y-e->border);
713 XSetWindowBorderWidth(ob_display, e->window, e->border);
714 XSetWindowBorder(ob_display, e->window,
715 RrColorPixel(ob_rr_theme->menu_border_color));
717 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
718 !e->entry->data.normal.enabled ?
720 (e == self->selected ?
721 ob_rr_theme->a_menu_text_disabled_selected :
722 ob_rr_theme->a_menu_text_disabled) :
724 (e == self->selected ?
725 ob_rr_theme->a_menu_text_selected :
726 ob_rr_theme->a_menu_text_normal));
727 switch (e->entry->type) {
728 case OB_MENU_ENTRY_TYPE_NORMAL:
729 text_a->texture[0].data.text.string = e->entry->data.normal.label;
730 tw = RrMinWidth(text_a);
731 tw = MIN(tw, MAX_MENU_WIDTH);
732 th = ob_rr_theme->menu_font_height;
734 if (e->entry->data.normal.icon ||
735 e->entry->data.normal.mask)
738 case OB_MENU_ENTRY_TYPE_SUBMENU:
739 sub = e->entry->data.submenu.submenu;
740 text_a->texture[0].data.text.string = sub ? sub->title : "";
741 tw = RrMinWidth(text_a);
742 tw = MIN(tw, MAX_MENU_WIDTH);
743 th = ob_rr_theme->menu_font_height;
745 if (e->entry->data.normal.icon ||
746 e->entry->data.normal.mask)
749 tw += ITEM_HEIGHT - PADDING;
751 case OB_MENU_ENTRY_TYPE_SEPARATOR:
752 if (e->entry->data.separator.label != NULL) {
753 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
754 e->entry->data.separator.label;
755 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
756 2*ob_rr_theme->paddingx;
757 tw = MIN(tw, MAX_MENU_WIDTH);
758 th = ob_rr_theme->menu_title_height +
759 (ob_rr_theme->mbwidth - PADDING) *2;
762 th = ob_rr_theme->menu_sep_width +
763 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
773 /* if the last entry is a labeled separator, then make its border
774 overlap with the menu's outside border */
775 it = g_list_last(self->entries);
776 e = it ? it->data : NULL;
777 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
778 e->entry->data.separator.label)
780 h -= ob_rr_theme->mbwidth;
783 self->text_x = PADDING;
788 w += ITEM_HEIGHT + PADDING;
789 self->text_x += ITEM_HEIGHT + PADDING;
796 XResizeWindow(ob_display, self->window, w, h);
800 RrPaint(self->a_items, self->window, w, h);
802 for (it = self->entries; it; it = g_list_next(it))
803 menu_entry_frame_render(it->data);
805 w += ob_rr_theme->mbwidth * 2;
806 h += ob_rr_theme->mbwidth * 2;
808 RECT_SET_SIZE(self->area, w, h);
813 static void menu_frame_update(ObMenuFrame *self)
819 menu_pipe_execute(self->menu);
820 menu_find_submenus(self->menu);
822 self->selected = NULL;
824 /* start at show_from */
825 mit = g_list_nth(self->menu->entries, self->show_from);
827 /* go through the menu's and frame's entries and connect the frame entries
828 to the menu entries */
829 for (fit = self->entries; mit && fit;
830 mit = g_list_next(mit), fit = g_list_next(fit))
832 ObMenuEntryFrame *f = fit->data;
833 f->entry = mit->data;
836 /* if there are more menu entries than in the frame, add them */
838 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
839 self->entries = g_list_append(self->entries, e);
840 mit = g_list_next(mit);
843 /* if there are more frame entries than menu entries then get rid of
846 GList *n = g_list_next(fit);
847 menu_entry_frame_free(fit->data);
848 self->entries = g_list_delete_link(self->entries, fit);
852 /* * make the menu fit on the screen */
854 /* calculate the height of the menu */
856 for (fit = self->entries; fit; fit = g_list_next(fit))
857 h += menu_entry_frame_get_height(fit->data,
858 fit == self->entries,
859 g_list_next(fit) == NULL);
860 /* add the border at the top and bottom */
861 h += ob_rr_theme->mbwidth * 2;
863 a = screen_physical_area_monitor(self->monitor);
867 gboolean last_entry = TRUE;
869 /* take the height of our More... entry into account */
870 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
872 /* start at the end of the entries */
873 flast = g_list_last(self->entries);
875 /* pull out all the entries from the frame that don't
876 fit on the screen, leaving at least 1 though */
877 while (h > a->height && g_list_previous(flast) != NULL) {
878 /* update the height, without this entry */
879 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
881 /* destroy the entry we're not displaying */
883 flast = g_list_previous(flast);
884 menu_entry_frame_free(tmp->data);
885 self->entries = g_list_delete_link(self->entries, tmp);
887 /* only the first one that we see is the last entry in the menu */
892 ObMenuEntry *more_entry;
893 ObMenuEntryFrame *more_frame;
894 /* make the More... menu entry frame which will display in this
896 if self->menu->more_menu is NULL that means that this is already
897 More... menu, so just use ourself.
899 more_entry = menu_get_more((self->menu->more_menu ?
900 self->menu->more_menu :
902 /* continue where we left off */
904 g_list_length(self->entries));
905 more_frame = menu_entry_frame_new(more_entry, self);
906 /* make it get deleted when the menu frame goes away */
907 menu_entry_unref(more_entry);
909 /* add our More... entry to the frame */
910 self->entries = g_list_append(self->entries, more_frame);
916 menu_frame_render(self);
919 static gboolean menu_frame_is_visible(ObMenuFrame *self)
921 return !!(g_list_find(menu_frame_visible, self));
924 static gboolean menu_frame_show(ObMenuFrame *self)
928 /* determine if the underlying menu is already visible */
929 for (it = menu_frame_visible; it; it = g_list_next(it)) {
930 ObMenuFrame *f = it->data;
931 if (f->menu == self->menu)
935 if (self->menu->update_func)
936 if (!self->menu->update_func(self, self->menu->data))
940 if (menu_frame_visible == NULL) {
941 /* no menus shown yet */
943 /* grab the pointer in such a way as to pass through "owner events"
944 so that we can get enter/leave notifies in the menu. */
945 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
947 if (!grab_keyboard()) {
953 menu_frame_update(self);
955 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
957 if (self->menu->show_func)
958 self->menu->show_func(self, self->menu->data);
963 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
968 if (menu_frame_is_visible(self))
970 if (!menu_frame_show(self))
973 if (self->menu->place_func)
974 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
976 menu_frame_place_topmenu(self, &x, &y);
978 menu_frame_move(self, x, y);
980 XMapWindow(ob_display, self->window);
982 if (screen_pointer_pos(&px, &py)) {
983 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
984 if (e && e->frame == self)
991 /*! Stop hiding an open submenu.
992 @child The OnMenuFrame of the submenu to be hidden
994 static void remove_submenu_hide_timeout(ObMenuFrame *child)
996 ob_main_loop_timeout_remove_data(ob_main_loop, submenu_hide_timeout,
1000 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1001 ObMenuEntryFrame *parent_entry)
1006 if (menu_frame_is_visible(self))
1009 self->monitor = parent->monitor;
1010 self->parent = parent;
1011 self->parent_entry = parent_entry;
1013 /* set up parent's child to be us */
1014 if ((parent->child) != self) {
1016 menu_frame_hide(parent->child);
1017 parent->child = self;
1018 parent->child_entry = parent_entry;
1021 if (!menu_frame_show(self))
1024 menu_frame_place_submenu(self, &x, &y);
1025 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1028 /*try the other side */
1029 self->direction_right = !self->direction_right;
1030 menu_frame_place_submenu(self, &x, &y);
1031 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1033 menu_frame_move(self, x + dx, y + dy);
1035 XMapWindow(ob_display, self->window);
1037 if (screen_pointer_pos(&px, &py)) {
1038 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1039 if (e && e->frame == self)
1046 static void menu_frame_hide(ObMenuFrame *self)
1048 GList *it = g_list_find(menu_frame_visible, self);
1049 gulong ignore_start;
1054 if (self->menu->hide_func)
1055 self->menu->hide_func(self, self->menu->data);
1058 menu_frame_hide(self->child);
1061 remove_submenu_hide_timeout(self);
1063 self->parent->child = NULL;
1064 self->parent->child_entry = NULL;
1066 self->parent = NULL;
1067 self->parent_entry = NULL;
1069 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1071 if (menu_frame_visible == NULL) {
1072 /* last menu shown */
1077 ignore_start = event_start_ignore_all_enters();
1078 XUnmapWindow(ob_display, self->window);
1079 event_end_ignore_all_enters(ignore_start);
1081 menu_frame_free(self);
1084 void menu_frame_hide_all(void)
1088 if (config_submenu_show_delay) {
1089 /* remove any submenu open requests */
1090 ob_main_loop_timeout_remove(ob_main_loop, submenu_show_timeout);
1092 if ((it = g_list_last(menu_frame_visible)))
1093 menu_frame_hide(it->data);
1096 ObMenuFrame* menu_frame_under(gint x, gint y)
1098 ObMenuFrame *ret = NULL;
1101 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1102 ObMenuFrame *f = it->data;
1104 if (RECT_CONTAINS(f->area, x, y)) {
1112 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1115 ObMenuEntryFrame *ret = NULL;
1118 if ((frame = menu_frame_under(x, y))) {
1119 x -= ob_rr_theme->mbwidth + frame->area.x;
1120 y -= ob_rr_theme->mbwidth + frame->area.y;
1122 for (it = frame->entries; it; it = g_list_next(it)) {
1123 ObMenuEntryFrame *e = it->data;
1125 if (RECT_CONTAINS(e->area, x, y)) {
1134 static gboolean submenu_show_timeout(gpointer data)
1136 g_assert(menu_frame_visible);
1137 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1141 static gboolean submenu_hide_timeout(gpointer data)
1143 g_assert(menu_frame_visible);
1144 menu_frame_hide((ObMenuFrame*)data);
1148 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1151 ObMenuEntryFrame *old = self->selected;
1152 ObMenuFrame *oldchild = self->child;
1153 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1155 /* if the user selected a separator, ignore it and reselect what we had
1157 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1161 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1164 /* if the user left this menu but we have a submenu open, move the
1165 selection back to that submenu */
1166 if (!entry && oldchild_entry)
1167 entry = oldchild_entry;
1169 if (config_submenu_show_delay) {
1170 /* remove any submenu open requests */
1171 ob_main_loop_timeout_remove(ob_main_loop, submenu_show_timeout);
1174 self->selected = entry;
1177 menu_entry_frame_render(old);
1179 if (oldchild_entry) {
1180 /* There is an open submenu */
1181 if (oldchild_entry == self->selected) {
1182 /* The open submenu has been reselected, so stop hiding the
1184 remove_submenu_hide_timeout(oldchild);
1186 else if (oldchild_entry == old) {
1187 /* The open submenu was selected and is no longer, so hide the
1189 if (immediate || config_submenu_hide_delay == 0)
1190 menu_frame_hide(oldchild);
1191 else if (config_submenu_hide_delay > 0)
1192 ob_main_loop_timeout_add(ob_main_loop,
1193 config_submenu_hide_delay * 1000,
1194 submenu_hide_timeout,
1195 oldchild, g_direct_equal,
1200 if (self->selected) {
1201 menu_entry_frame_render(self->selected);
1203 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1204 /* only show if the submenu isn't already showing */
1205 if (oldchild_entry != self->selected) {
1206 if (immediate || config_submenu_hide_delay == 0)
1207 menu_entry_frame_show_submenu(self->selected);
1208 else if (config_submenu_hide_delay > 0)
1209 ob_main_loop_timeout_add(ob_main_loop,
1210 config_submenu_show_delay * 1000,
1211 submenu_show_timeout,
1212 self->selected, g_direct_equal,
1215 /* hide the grandchildren of this menu. and move the cursor to
1217 else if (immediate && self->child && self->child->child) {
1218 menu_frame_hide(self->child->child);
1219 menu_frame_select(self->child, NULL, TRUE);
1225 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1229 if (!self->entry->data.submenu.submenu) return;
1231 f = menu_frame_new(self->entry->data.submenu.submenu,
1232 self->entry->data.submenu.show_from,
1233 self->frame->client);
1234 /* pass our direction on to our child */
1235 f->direction_right = self->frame->direction_right;
1237 menu_frame_show_submenu(f, self->frame, self);
1240 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1242 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1243 self->entry->data.normal.enabled)
1245 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1247 ObMenuEntry *entry = self->entry;
1248 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1249 gpointer data = self->frame->menu->data;
1250 GSList *acts = self->entry->data.normal.actions;
1251 ObClient *client = self->frame->client;
1252 ObMenuFrame *frame = self->frame;
1254 /* release grabs before executing the shit */
1255 if (!(state & ControlMask)) {
1256 event_cancel_all_key_grabs();
1261 func(entry, frame, client, state, data);
1263 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1264 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1268 void menu_frame_select_previous(ObMenuFrame *self)
1270 GList *it = NULL, *start;
1272 if (self->entries) {
1273 start = it = g_list_find(self->entries, self->selected);
1275 ObMenuEntryFrame *e;
1277 it = it ? g_list_previous(it) : g_list_last(self->entries);
1283 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1285 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1290 menu_frame_select(self, it ? it->data : NULL, FALSE);
1293 void menu_frame_select_next(ObMenuFrame *self)
1295 GList *it = NULL, *start;
1297 if (self->entries) {
1298 start = it = g_list_find(self->entries, self->selected);
1300 ObMenuEntryFrame *e;
1302 it = it ? g_list_next(it) : self->entries;
1308 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1310 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1315 menu_frame_select(self, it ? it->data : NULL, FALSE);
1318 void menu_frame_select_first(ObMenuFrame *self)
1322 if (self->entries) {
1323 for (it = self->entries; it; it = g_list_next(it)) {
1324 ObMenuEntryFrame *e = it->data;
1325 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1327 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1331 menu_frame_select(self, it ? it->data : NULL, FALSE);
1334 void menu_frame_select_last(ObMenuFrame *self)
1338 if (self->entries) {
1339 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1340 ObMenuEntryFrame *e = it->data;
1341 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1343 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1347 menu_frame_select(self, it ? it->data : NULL, FALSE);