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 menu_entry_frame_submenu_timeout(gpointer data);
53 static void menu_frame_hide(ObMenuFrame *self);
55 static Window createWindow(Window parent, gulong mask,
56 XSetWindowAttributes *attrib)
58 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
59 RrDepth(ob_rr_inst), InputOutput,
60 RrVisual(ob_rr_inst), mask, attrib);
63 void menu_frame_startup(gboolean reconfig)
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;
77 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
80 void menu_frame_shutdown(gboolean reconfig)
82 RrAppearanceFree(a_sep);
86 g_hash_table_destroy(menu_frame_map);
89 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
92 XSetWindowAttributes attr;
94 self = g_new0(ObMenuFrame, 1);
95 self->type = Window_Menu;
97 self->selected = NULL;
98 self->client = client;
99 self->direction_right = TRUE;
100 self->show_from = show_from;
102 attr.event_mask = FRAME_EVENTMASK;
103 self->window = createWindow(RootWindow(ob_display, ob_screen),
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);
110 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
111 XSetWindowBorder(ob_display, self->window,
112 RrColorPixel(ob_rr_theme->menu_border_color));
114 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
116 stacking_add(MENU_AS_WINDOW(self));
121 void menu_frame_free(ObMenuFrame *self)
124 while (self->entries) {
125 menu_entry_frame_free(self->entries->data);
126 self->entries = g_list_delete_link(self->entries, self->entries);
129 stacking_remove(MENU_AS_WINDOW(self));
131 RrAppearanceFree(self->a_items);
133 XDestroyWindow(ob_display, self->window);
139 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
142 ObMenuEntryFrame *self;
143 XSetWindowAttributes attr;
145 self = g_new0(ObMenuEntryFrame, 1);
149 menu_entry_ref(entry);
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);
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);
165 XMapWindow(ob_display, self->window);
166 XMapWindow(ob_display, self->text);
171 static void menu_entry_frame_free(ObMenuEntryFrame *self)
174 menu_entry_unref(self->entry);
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);
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);
193 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
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);
200 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
204 if (config_menu_middle) {
208 *y -= self->area.height / 2;
210 /* try to the right of the cursor */
211 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
212 self->direction_right = TRUE;
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;
220 /* if didnt fit on either side so just use what it says */
222 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
223 self->direction_right = TRUE;
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;
240 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
241 self->direction_right = FALSE;
243 if (dx != 0 || dy != 0) {
244 /* try to the top right of the cursor */
246 myy = *y - self->area.height;
247 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
248 self->direction_right = TRUE;
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;
257 if (dx != 0 || dy != 0) {
258 /* if didnt fit on either side so just use what it says */
261 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
262 self->direction_right = TRUE;
269 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
271 gint overlapx, overlapy;
274 overlapx = ob_rr_theme->menu_overlap_x;
275 overlapy = ob_rr_theme->menu_overlap_y;
276 bwidth = ob_rr_theme->mbwidth;
278 if (self->direction_right)
279 *x = self->parent->area.x + self->parent->area.width -
282 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
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;
291 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
299 a = screen_physical_area_monitor(screen_find_monitor_point(x, y));
301 half = g_list_length(self->entries) / 2;
302 pos = g_list_index(self->entries, self->selected);
304 /* if in the bottom half then check this stuff first, will keep the bottom
305 edge of the menu visible */
307 *dx = MAX(*dx, a->x - x);
308 *dy = MAX(*dy, a->y - y);
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 */
315 *dx = MAX(*dx, a->x - x);
316 *dy = MAX(*dy, a->y - y);
322 static void menu_entry_frame_render(ObMenuEntryFrame *self)
324 RrAppearance *item_a, *text_a;
327 ObMenuFrame *frame = self->frame;
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 ?
335 (self == self->frame->selected ?
336 ob_rr_theme->a_menu_disabled_selected :
337 ob_rr_theme->a_menu_disabled) :
339 (self == self->frame->selected ?
340 ob_rr_theme->a_menu_selected :
341 ob_rr_theme->a_menu_normal));
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;
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;
355 g_assert_not_reached();
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);
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 ?
371 (self == self->frame->selected ?
372 ob_rr_theme->a_menu_text_disabled_selected :
373 ob_rr_theme->a_menu_text_disabled) :
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))
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;
388 text_a->texture[0].data.text.shortcut = FALSE;
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))
400 text_a->texture[0].data.text.shortcut = TRUE;
401 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
403 text_a->texture[0].data.text.shortcut = FALSE;
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;
412 text_a = ob_rr_theme->a_menu_text_normal;
416 switch (self->entry->type) {
417 case OB_MENU_ENTRY_TYPE_NORMAL:
418 XMoveResizeWindow(ob_display, self->text,
419 self->frame->text_x, PADDING,
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);
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);
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);
457 /* unlabeled separator */
458 XMoveResizeWindow(ob_display, self->text, 0, 0,
460 ob_rr_theme->menu_sep_width +
461 2*ob_rr_theme->menu_sep_paddingy);
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;
477 RrPaint(a_sep, self->text, self->area.width,
478 ob_rr_theme->menu_sep_width +
479 2*ob_rr_theme->menu_sep_paddingy);
484 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
485 self->entry->data.normal.icon)
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);
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)
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);
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;
531 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
532 !self->entry->data.normal.enabled ?
534 (self == self->frame->selected ?
535 self->entry->data.normal.mask_disabled_selected_color :
536 self->entry->data.normal.mask_disabled_color) :
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;
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);
553 XUnmapWindow(ob_display, self->icon);
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);
574 XUnmapWindow(ob_display, self->bullet);
579 /*! this code is taken from the menu_frame_render. if that changes, this won't
581 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
582 gboolean first_entry,
591 t = self->entry->type;
593 /* this is the More... entry, it's NORMAL type */
594 t = OB_MENU_ENTRY_TYPE_NORMAL;
597 case OB_MENU_ENTRY_TYPE_NORMAL:
598 case OB_MENU_ENTRY_TYPE_SUBMENU:
599 h += ob_rr_theme->menu_font_height;
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;
606 /* if the first entry is a labeled separator, then make its border
607 overlap with the menu's outside border */
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 */
613 h -= ob_rr_theme->mbwidth;
615 h += ob_rr_theme->menu_sep_width +
616 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
624 void menu_frame_render(ObMenuFrame *self)
627 gint tw, th; /* temps */
629 gboolean has_icon = FALSE;
633 /* find text dimensions */
635 STRUT_SET(self->item_margin, 0, 0, 0, 0);
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);
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));
673 /* render the entries */
675 for (it = self->entries; it; it = g_list_next(it)) {
676 RrAppearance *text_a;
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)
685 h -= ob_rr_theme->mbwidth;
688 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
689 e->entry->data.separator.label)
691 e->border = ob_rr_theme->mbwidth;
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));
701 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
702 !e->entry->data.normal.enabled ?
704 (e == self->selected ?
705 ob_rr_theme->a_menu_text_disabled_selected :
706 ob_rr_theme->a_menu_text_disabled) :
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;
718 if (e->entry->data.normal.icon ||
719 e->entry->data.normal.mask)
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;
729 if (e->entry->data.normal.icon ||
730 e->entry->data.normal.mask)
733 tw += ITEM_HEIGHT - PADDING;
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;
746 th = ob_rr_theme->menu_sep_width +
747 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
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)
764 h -= ob_rr_theme->mbwidth;
767 self->text_x = PADDING;
772 w += ITEM_HEIGHT + PADDING;
773 self->text_x += ITEM_HEIGHT + PADDING;
780 XResizeWindow(ob_display, self->window, w, h);
784 RrPaint(self->a_items, self->window, w, h);
786 for (it = self->entries; it; it = g_list_next(it))
787 menu_entry_frame_render(it->data);
789 w += ob_rr_theme->mbwidth * 2;
790 h += ob_rr_theme->mbwidth * 2;
792 RECT_SET_SIZE(self->area, w, h);
797 static void menu_frame_update(ObMenuFrame *self)
803 menu_pipe_execute(self->menu);
804 menu_find_submenus(self->menu);
806 self->selected = NULL;
808 /* start at show_from */
809 mit = g_list_nth(self->menu->entries, self->show_from);
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))
816 ObMenuEntryFrame *f = fit->data;
817 f->entry = mit->data;
820 /* if there are more menu entries than in the frame, add them */
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);
827 /* if there are more frame entries than menu entries then get rid of
830 GList *n = g_list_next(fit);
831 menu_entry_frame_free(fit->data);
832 self->entries = g_list_delete_link(self->entries, fit);
836 /* * make the menu fit on the screen */
838 /* calculate the height of the menu */
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;
847 a = screen_physical_area_monitor(self->monitor);
851 gboolean last_entry = TRUE;
853 /* take the height of our More... entry into account */
854 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
856 /* start at the end of the entries */
857 flast = g_list_last(self->entries);
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);
865 /* destroy the entry we're not displaying */
867 flast = g_list_previous(flast);
868 menu_entry_frame_free(tmp->data);
869 self->entries = g_list_delete_link(self->entries, tmp);
871 /* only the first one that we see is the last entry in the menu */
876 ObMenuEntry *more_entry;
877 ObMenuEntryFrame *more_frame;
878 /* make the More... menu entry frame which will display in this
880 if self->menu->more_menu is NULL that means that this is already
881 More... menu, so just use ourself.
883 more_entry = menu_get_more((self->menu->more_menu ?
884 self->menu->more_menu :
886 /* continue where we left off */
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);
893 /* add our More... entry to the frame */
894 self->entries = g_list_append(self->entries, more_frame);
900 menu_frame_render(self);
903 static gboolean menu_frame_is_visible(ObMenuFrame *self)
905 return !!(g_list_find(menu_frame_visible, self));
908 static gboolean menu_frame_show(ObMenuFrame *self)
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)
919 if (self->menu->update_func)
920 if (!self->menu->update_func(self, self->menu->data))
924 if (menu_frame_visible == NULL) {
925 /* no menus shown yet */
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))
931 if (!grab_keyboard()) {
937 menu_frame_update(self);
939 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
941 if (self->menu->show_func)
942 self->menu->show_func(self, self->menu->data);
947 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
952 if (menu_frame_is_visible(self))
954 if (!menu_frame_show(self))
957 if (self->menu->place_func)
958 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
960 menu_frame_place_topmenu(self, &x, &y);
962 menu_frame_move(self, x, y);
964 XMapWindow(ob_display, self->window);
966 if (screen_pointer_pos(&px, &py)) {
967 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
968 if (e && e->frame == self)
975 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
976 ObMenuEntryFrame *parent_entry)
981 if (menu_frame_is_visible(self))
984 self->monitor = parent->monitor;
985 self->parent = parent;
986 self->parent_entry = parent_entry;
988 /* set up parent's child to be us */
990 menu_frame_hide(parent->child);
991 parent->child = self;
993 if (!menu_frame_show(self))
996 menu_frame_place_submenu(self, &x, &y);
997 menu_frame_move_on_screen(self, x, y, &dx, &dy);
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);
1005 menu_frame_move(self, x + dx, y + dy);
1007 XMapWindow(ob_display, self->window);
1009 if (screen_pointer_pos(&px, &py)) {
1010 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1011 if (e && e->frame == self)
1018 static void menu_frame_hide(ObMenuFrame *self)
1020 GList *it = g_list_find(menu_frame_visible, self);
1021 gulong ignore_start;
1026 if (self->menu->hide_func)
1027 self->menu->hide_func(self, self->menu->data);
1030 menu_frame_hide(self->child);
1033 self->parent->child = NULL;
1034 self->parent = NULL;
1035 self->parent_entry = NULL;
1037 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1039 if (menu_frame_visible == NULL) {
1040 /* last menu shown */
1045 ignore_start = event_start_ignore_all_enters();
1046 XUnmapWindow(ob_display, self->window);
1047 event_end_ignore_all_enters(ignore_start);
1049 menu_frame_free(self);
1052 void menu_frame_hide_all(void)
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);
1061 if ((it = g_list_last(menu_frame_visible)))
1062 menu_frame_hide(it->data);
1065 void menu_frame_hide_all_client(ObClient *client)
1067 GList *it = g_list_last(menu_frame_visible);
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);
1081 ObMenuFrame* menu_frame_under(gint x, gint y)
1083 ObMenuFrame *ret = NULL;
1086 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1087 ObMenuFrame *f = it->data;
1089 if (RECT_CONTAINS(f->area, x, y)) {
1097 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1100 ObMenuEntryFrame *ret = NULL;
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;
1107 for (it = frame->entries; it; it = g_list_next(it)) {
1108 ObMenuEntryFrame *e = it->data;
1110 if (RECT_CONTAINS(e->area, x, y)) {
1119 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1121 g_assert(menu_frame_visible);
1122 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1126 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1129 ObMenuEntryFrame *old = self->selected;
1130 ObMenuFrame *oldchild = self->child;
1132 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1135 if (old == entry) return;
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);
1143 self->selected = entry;
1146 menu_entry_frame_render(old);
1148 menu_frame_hide(oldchild);
1150 if (self->selected) {
1151 menu_entry_frame_render(self->selected);
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,
1162 menu_entry_frame_show_submenu(self->selected);
1168 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1172 if (!self->entry->data.submenu.submenu) return;
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;
1180 menu_frame_show_submenu(f, self->frame, self);
1183 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1185 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1186 self->entry->data.normal.enabled)
1188 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
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;
1197 /* release grabs before executing the shit */
1198 if (!(state & ControlMask)) {
1199 menu_frame_hide_all();
1204 func(entry, frame, client, state, data);
1206 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1207 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1211 void menu_frame_select_previous(ObMenuFrame *self)
1213 GList *it = NULL, *start;
1215 if (self->entries) {
1216 start = it = g_list_find(self->entries, self->selected);
1218 ObMenuEntryFrame *e;
1220 it = it ? g_list_previous(it) : g_list_last(self->entries);
1226 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1228 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1233 menu_frame_select(self, it ? it->data : NULL, TRUE);
1236 void menu_frame_select_next(ObMenuFrame *self)
1238 GList *it = NULL, *start;
1240 if (self->entries) {
1241 start = it = g_list_find(self->entries, self->selected);
1243 ObMenuEntryFrame *e;
1245 it = it ? g_list_next(it) : self->entries;
1251 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1253 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1258 menu_frame_select(self, it ? it->data : NULL, TRUE);