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"
29 #include "render/theme.h"
32 #define SEPARATOR_HEIGHT 3
33 #define MAX_MENU_WIDTH 400
35 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
37 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
39 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
40 ButtonPressMask | ButtonReleaseMask)
42 GList *menu_frame_visible;
44 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
46 static void menu_entry_frame_free(ObMenuEntryFrame *self);
47 static void menu_frame_update(ObMenuFrame *self);
48 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
49 static void menu_frame_hide(ObMenuFrame *self);
51 static Window createWindow(Window parent, gulong mask,
52 XSetWindowAttributes *attrib)
54 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
55 RrDepth(ob_rr_inst), InputOutput,
56 RrVisual(ob_rr_inst), mask, attrib);
59 GHashTable *menu_frame_map;
61 void menu_frame_startup(gboolean reconfig)
65 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
68 void menu_frame_shutdown(gboolean reconfig)
72 g_hash_table_destroy(menu_frame_map);
75 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
78 XSetWindowAttributes attr;
80 self = g_new0(ObMenuFrame, 1);
81 self->type = Window_Menu;
83 self->selected = NULL;
84 self->client = client;
85 self->direction_right = TRUE;
86 self->show_from = show_from;
88 attr.event_mask = FRAME_EVENTMASK;
89 self->window = createWindow(RootWindow(ob_display, ob_screen),
92 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
93 XSetWindowBorder(ob_display, self->window,
94 RrColorPixel(ob_rr_theme->menu_border_color));
96 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
98 stacking_add(MENU_AS_WINDOW(self));
103 void menu_frame_free(ObMenuFrame *self)
106 while (self->entries) {
107 menu_entry_frame_free(self->entries->data);
108 self->entries = g_list_delete_link(self->entries, self->entries);
111 stacking_remove(MENU_AS_WINDOW(self));
113 RrAppearanceFree(self->a_items);
115 XDestroyWindow(ob_display, self->window);
121 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
124 ObMenuEntryFrame *self;
125 XSetWindowAttributes attr;
127 self = g_new0(ObMenuEntryFrame, 1);
131 menu_entry_ref(entry);
133 attr.event_mask = ENTRY_EVENTMASK;
134 self->window = createWindow(self->frame->window, CWEventMask, &attr);
135 self->text = createWindow(self->window, 0, NULL);
136 g_hash_table_insert(menu_frame_map, &self->window, self);
137 g_hash_table_insert(menu_frame_map, &self->text, self);
138 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
139 self->icon = createWindow(self->window, 0, NULL);
140 g_hash_table_insert(menu_frame_map, &self->icon, self);
142 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
143 self->bullet = createWindow(self->window, 0, NULL);
144 g_hash_table_insert(menu_frame_map, &self->bullet, self);
147 XMapWindow(ob_display, self->window);
148 XMapWindow(ob_display, self->text);
153 static void menu_entry_frame_free(ObMenuEntryFrame *self)
156 menu_entry_unref(self->entry);
158 XDestroyWindow(ob_display, self->text);
159 XDestroyWindow(ob_display, self->window);
160 g_hash_table_remove(menu_frame_map, &self->text);
161 g_hash_table_remove(menu_frame_map, &self->window);
162 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
163 XDestroyWindow(ob_display, self->icon);
164 g_hash_table_remove(menu_frame_map, &self->icon);
166 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
167 XDestroyWindow(ob_display, self->bullet);
168 g_hash_table_remove(menu_frame_map, &self->bullet);
175 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
177 RECT_SET_POINT(self->area, x, y);
178 XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
181 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
185 if (config_menu_middle) {
189 *y -= self->area.height / 2;
191 /* try to the right of the cursor */
192 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
193 self->direction_right = TRUE;
195 /* try to the left of the cursor */
196 myx = *x - self->area.width;
197 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
198 self->direction_right = FALSE;
201 /* if didnt fit on either side so just use what it says */
203 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
204 self->direction_right = TRUE;
214 /* try to the bottom right of the cursor */
215 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
216 self->direction_right = TRUE;
217 if (dx != 0 || dy != 0) {
218 /* try to the bottom left of the cursor */
219 myx = *x - self->area.width;
221 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
222 self->direction_right = FALSE;
224 if (dx != 0 || dy != 0) {
225 /* try to the top right of the cursor */
227 myy = *y - self->area.height;
228 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
229 self->direction_right = TRUE;
231 if (dx != 0 || dy != 0) {
232 /* try to the top left of the cursor */
233 myx = *x - self->area.width;
234 myy = *y - self->area.height;
235 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
236 self->direction_right = FALSE;
238 if (dx != 0 || dy != 0) {
239 /* if didnt fit on either side so just use what it says */
242 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
243 self->direction_right = TRUE;
250 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
252 gint overlapx, overlapy;
255 overlapx = ob_rr_theme->menu_overlap_x;
256 overlapy = ob_rr_theme->menu_overlap_y;
257 bwidth = ob_rr_theme->mbwidth;
259 if (self->direction_right)
260 *x = self->parent->area.x + self->parent->area.width -
263 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
265 *y = self->parent->area.y + self->parent_entry->area.y;
266 if (config_menu_middle)
267 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
272 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
280 a = screen_physical_area_monitor(self->monitor);
282 half = g_list_length(self->entries) / 2;
283 pos = g_list_index(self->entries, self->selected);
285 /* if in the bottom half then check this stuff first, will keep the bottom
286 edge of the menu visible */
288 *dx = MAX(*dx, a->x - x);
289 *dy = MAX(*dy, a->y - y);
291 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
292 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
293 /* if in the top half then check this stuff last, will keep the top
294 edge of the menu visible */
296 *dx = MAX(*dx, a->x - x);
297 *dy = MAX(*dy, a->y - y);
303 static void menu_entry_frame_render(ObMenuEntryFrame *self)
305 RrAppearance *item_a, *text_a;
308 ObMenuFrame *frame = self->frame;
310 switch (self->entry->type) {
311 case OB_MENU_ENTRY_TYPE_NORMAL:
312 case OB_MENU_ENTRY_TYPE_SUBMENU:
313 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
314 !self->entry->data.normal.enabled ?
316 (self == self->frame->selected ?
317 ob_rr_theme->a_menu_disabled_selected :
318 ob_rr_theme->a_menu_disabled) :
320 (self == self->frame->selected ?
321 ob_rr_theme->a_menu_selected :
322 ob_rr_theme->a_menu_normal));
325 case OB_MENU_ENTRY_TYPE_SEPARATOR:
326 if (self->entry->data.separator.label) {
327 item_a = ob_rr_theme->a_menu_title;
328 th = ob_rr_theme->menu_title_height;
330 item_a = ob_rr_theme->a_menu_normal;
331 th = SEPARATOR_HEIGHT + 2*PADDING;
335 g_assert_not_reached();
337 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
338 XResizeWindow(ob_display, self->window,
339 self->area.width, self->area.height);
340 item_a->surface.parent = self->frame->a_items;
341 item_a->surface.parentx = self->area.x;
342 item_a->surface.parenty = self->area.y;
343 RrPaint(item_a, self->window, self->area.width, self->area.height);
345 switch (self->entry->type) {
346 case OB_MENU_ENTRY_TYPE_NORMAL:
347 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
348 !self->entry->data.normal.enabled ?
350 (self == self->frame->selected ?
351 ob_rr_theme->a_menu_text_disabled_selected :
352 ob_rr_theme->a_menu_text_disabled) :
354 (self == self->frame->selected ?
355 ob_rr_theme->a_menu_text_selected :
356 ob_rr_theme->a_menu_text_normal));
357 text_a->texture[0].data.text.string = self->entry->data.normal.label;
358 if (self->entry->data.normal.shortcut &&
359 (self->frame->menu->show_all_shortcuts ||
360 self->entry->data.normal.shortcut_always_show ||
361 self->entry->data.normal.shortcut_position > 0))
363 text_a->texture[0].data.text.shortcut = TRUE;
364 text_a->texture[0].data.text.shortcut_pos =
365 self->entry->data.normal.shortcut_position;
367 text_a->texture[0].data.text.shortcut = FALSE;
369 case OB_MENU_ENTRY_TYPE_SUBMENU:
370 text_a = (self == self->frame->selected ?
371 ob_rr_theme->a_menu_text_selected :
372 ob_rr_theme->a_menu_text_normal);
373 sub = self->entry->data.submenu.submenu;
374 text_a->texture[0].data.text.string = sub ? sub->title : "";
375 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
376 sub->shortcut_always_show ||
377 sub->shortcut_position > 0))
379 text_a->texture[0].data.text.shortcut = TRUE;
380 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
382 text_a->texture[0].data.text.shortcut = FALSE;
384 case OB_MENU_ENTRY_TYPE_SEPARATOR:
385 if (self->entry->data.separator.label != NULL) {
386 text_a = ob_rr_theme->a_menu_text_title;
387 text_a->texture[0].data.text.string =
388 self->entry->data.separator.label;
391 text_a = ob_rr_theme->a_menu_text_normal;
395 switch (self->entry->type) {
396 case OB_MENU_ENTRY_TYPE_NORMAL:
397 XMoveResizeWindow(ob_display, self->text,
398 self->frame->text_x, PADDING,
400 ITEM_HEIGHT - 2*PADDING);
401 text_a->surface.parent = item_a;
402 text_a->surface.parentx = self->frame->text_x;
403 text_a->surface.parenty = PADDING;
404 RrPaint(text_a, self->text, self->frame->text_w,
405 ITEM_HEIGHT - 2*PADDING);
407 case OB_MENU_ENTRY_TYPE_SUBMENU:
408 XMoveResizeWindow(ob_display, self->text,
409 self->frame->text_x, PADDING,
410 self->frame->text_w - ITEM_HEIGHT,
411 ITEM_HEIGHT - 2*PADDING);
412 text_a->surface.parent = item_a;
413 text_a->surface.parentx = self->frame->text_x;
414 text_a->surface.parenty = PADDING;
415 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
416 ITEM_HEIGHT - 2*PADDING);
418 case OB_MENU_ENTRY_TYPE_SEPARATOR:
419 if (self->entry->data.separator.label != NULL) {
420 /* labeled separator */
421 XMoveResizeWindow(ob_display, self->text,
422 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
423 self->area.width - 2*ob_rr_theme->paddingx,
424 ob_rr_theme->menu_title_height -
425 2*ob_rr_theme->paddingy);
426 text_a->surface.parent = item_a;
427 text_a->surface.parentx = ob_rr_theme->paddingx;
428 text_a->surface.parenty = ob_rr_theme->paddingy;
429 RrPaint(text_a, self->text,
430 self->area.width - 2*ob_rr_theme->paddingx,
431 ob_rr_theme->menu_title_height -
432 2*ob_rr_theme->paddingy);
436 /* unlabeled separaator */
437 XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
438 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
440 clear = ob_rr_theme->a_clear_tex;
441 clear->texture[0].type = RR_TEXTURE_LINE_ART;
442 clear->surface.parent = item_a;
443 clear->surface.parentx = PADDING;
444 clear->surface.parenty = PADDING;
445 clear->texture[0].data.lineart.color =
446 text_a->texture[0].data.text.color;
447 clear->texture[0].data.lineart.x1 = 2*PADDING;
448 clear->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
449 clear->texture[0].data.lineart.x2 = self->area.width - 4*PADDING;
450 clear->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
451 RrPaint(clear, self->text,
452 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
457 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
458 self->entry->data.normal.icon_data)
462 XMoveResizeWindow(ob_display, self->icon,
463 PADDING, frame->item_margin.top,
464 ITEM_HEIGHT - frame->item_margin.top
465 - frame->item_margin.bottom,
466 ITEM_HEIGHT - frame->item_margin.top
467 - frame->item_margin.bottom);
469 clear = ob_rr_theme->a_clear_tex;
470 clear->texture[0].type = RR_TEXTURE_RGBA;
471 clear->texture[0].data.rgba.width =
472 self->entry->data.normal.icon_width;
473 clear->texture[0].data.rgba.height =
474 self->entry->data.normal.icon_height;
475 clear->texture[0].data.rgba.alpha =
476 self->entry->data.normal.icon_alpha;
477 clear->texture[0].data.rgba.data =
478 self->entry->data.normal.icon_data;
479 clear->surface.parent = item_a;
480 clear->surface.parentx = PADDING;
481 clear->surface.parenty = frame->item_margin.top;
482 RrPaint(clear, self->icon,
483 ITEM_HEIGHT - frame->item_margin.top
484 - frame->item_margin.bottom,
485 ITEM_HEIGHT - frame->item_margin.top
486 - frame->item_margin.bottom);
487 XMapWindow(ob_display, self->icon);
488 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
489 self->entry->data.normal.mask)
494 XMoveResizeWindow(ob_display, self->icon,
495 PADDING, frame->item_margin.top,
496 ITEM_HEIGHT - frame->item_margin.top
497 - frame->item_margin.bottom,
498 ITEM_HEIGHT - frame->item_margin.top
499 - frame->item_margin.bottom);
501 clear = ob_rr_theme->a_clear_tex;
502 clear->texture[0].type = RR_TEXTURE_MASK;
503 clear->texture[0].data.mask.mask =
504 self->entry->data.normal.mask;
506 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
507 !self->entry->data.normal.enabled ?
509 (self == self->frame->selected ?
510 self->entry->data.normal.mask_disabled_selected_color :
511 self->entry->data.normal.mask_disabled_color) :
513 (self == self->frame->selected ?
514 self->entry->data.normal.mask_selected_color :
515 self->entry->data.normal.mask_normal_color));
516 clear->texture[0].data.mask.color = c;
518 clear->surface.parent = item_a;
519 clear->surface.parentx = PADDING;
520 clear->surface.parenty = frame->item_margin.top;
521 RrPaint(clear, self->icon,
522 ITEM_HEIGHT - frame->item_margin.top
523 - frame->item_margin.bottom,
524 ITEM_HEIGHT - frame->item_margin.top
525 - frame->item_margin.bottom);
526 XMapWindow(ob_display, self->icon);
528 XUnmapWindow(ob_display, self->icon);
530 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
531 RrAppearance *bullet_a;
532 XMoveResizeWindow(ob_display, self->bullet,
533 self->frame->text_x + self->frame->text_w -
534 ITEM_HEIGHT + PADDING, PADDING,
535 ITEM_HEIGHT - 2*PADDING,
536 ITEM_HEIGHT - 2*PADDING);
537 bullet_a = (self == self->frame->selected ?
538 ob_rr_theme->a_menu_bullet_selected :
539 ob_rr_theme->a_menu_bullet_normal);
540 bullet_a->surface.parent = item_a;
541 bullet_a->surface.parentx =
542 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
543 bullet_a->surface.parenty = PADDING;
544 RrPaint(bullet_a, self->bullet,
545 ITEM_HEIGHT - 2*PADDING,
546 ITEM_HEIGHT - 2*PADDING);
547 XMapWindow(ob_display, self->bullet);
549 XUnmapWindow(ob_display, self->bullet);
554 /*! this code is taken from the menu_frame_render. if that changes, this won't
556 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
557 gboolean first_entry,
566 t = self->entry->type;
568 /* this is the More... entry, it's NORMAL type */
569 t = OB_MENU_ENTRY_TYPE_NORMAL;
572 case OB_MENU_ENTRY_TYPE_NORMAL:
573 case OB_MENU_ENTRY_TYPE_SUBMENU:
574 h += ob_rr_theme->menu_font_height;
576 case OB_MENU_ENTRY_TYPE_SEPARATOR:
577 if (self->entry->data.separator.label != NULL) {
578 h += ob_rr_theme->menu_title_height +
579 (ob_rr_theme->mbwidth - PADDING) * 2;
581 /* if the first entry is a labeled separator, then make its border
582 overlap with the menu's outside border */
584 h -= ob_rr_theme->mbwidth;
585 /* if the last entry is a labeled separator, then make its border
586 overlap with the menu's outside border */
588 h -= ob_rr_theme->mbwidth;
590 h += SEPARATOR_HEIGHT;
598 void menu_frame_render(ObMenuFrame *self)
601 gint tw, th; /* temps */
603 gboolean has_icon = FALSE;
607 /* find text dimensions */
609 STRUT_SET(self->item_margin, 0, 0, 0, 0);
614 e = self->entries->data;
615 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
616 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
621 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
622 STRUT_SET(self->item_margin,
623 MAX(self->item_margin.left, l),
624 MAX(self->item_margin.top, t),
625 MAX(self->item_margin.right, r),
626 MAX(self->item_margin.bottom, b));
627 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
628 STRUT_SET(self->item_margin,
629 MAX(self->item_margin.left, l),
630 MAX(self->item_margin.top, t),
631 MAX(self->item_margin.right, r),
632 MAX(self->item_margin.bottom, b));
633 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
634 STRUT_SET(self->item_margin,
635 MAX(self->item_margin.left, l),
636 MAX(self->item_margin.top, t),
637 MAX(self->item_margin.right, r),
638 MAX(self->item_margin.bottom, b));
639 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
640 STRUT_SET(self->item_margin,
641 MAX(self->item_margin.left, l),
642 MAX(self->item_margin.top, t),
643 MAX(self->item_margin.right, r),
644 MAX(self->item_margin.bottom, b));
647 /* render the entries */
649 for (it = self->entries; it; it = g_list_next(it)) {
650 RrAppearance *text_a;
653 /* if the first entry is a labeled separator, then make its border
654 overlap with the menu's outside border */
655 if (it == self->entries &&
656 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
657 e->entry->data.separator.label)
659 h -= ob_rr_theme->mbwidth;
662 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
663 e->entry->data.separator.label)
665 e->border = ob_rr_theme->mbwidth;
668 RECT_SET_POINT(e->area, 0, h+e->border);
669 XMoveWindow(ob_display, e->window,
670 e->area.x-e->border, e->area.y-e->border);
671 XSetWindowBorderWidth(ob_display, e->window, e->border);
672 XSetWindowBorder(ob_display, e->window,
673 RrColorPixel(ob_rr_theme->menu_border_color));
676 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
677 !e->entry->data.normal.enabled ?
679 (e == self->selected ?
680 ob_rr_theme->a_menu_text_disabled_selected :
681 ob_rr_theme->a_menu_text_disabled) :
683 (e == self->selected ?
684 ob_rr_theme->a_menu_text_selected :
685 ob_rr_theme->a_menu_text_normal));
686 switch (e->entry->type) {
687 case OB_MENU_ENTRY_TYPE_NORMAL:
688 text_a->texture[0].data.text.string = e->entry->data.normal.label;
689 tw = RrMinWidth(text_a);
690 tw = MIN(tw, MAX_MENU_WIDTH);
691 th = ob_rr_theme->menu_font_height;
693 if (e->entry->data.normal.icon_data ||
694 e->entry->data.normal.mask)
697 case OB_MENU_ENTRY_TYPE_SUBMENU:
698 sub = e->entry->data.submenu.submenu;
699 text_a->texture[0].data.text.string = sub ? sub->title : "";
700 tw = RrMinWidth(text_a);
701 tw = MIN(tw, MAX_MENU_WIDTH);
702 th = ob_rr_theme->menu_font_height;
704 if (e->entry->data.normal.icon_data ||
705 e->entry->data.normal.mask)
708 tw += ITEM_HEIGHT - PADDING;
710 case OB_MENU_ENTRY_TYPE_SEPARATOR:
711 if (e->entry->data.separator.label != NULL) {
712 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
713 e->entry->data.separator.label;
714 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
715 2*ob_rr_theme->paddingx;
716 tw = MIN(tw, MAX_MENU_WIDTH);
717 th = ob_rr_theme->menu_title_height +
718 (ob_rr_theme->mbwidth - PADDING) *2;
721 th = SEPARATOR_HEIGHT;
731 /* if the last entry is a labeled separator, then make its border
732 overlap with the menu's outside border */
733 it = g_list_last(self->entries);
734 e = it ? it->data : NULL;
735 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
736 e->entry->data.separator.label)
738 h -= ob_rr_theme->mbwidth;
741 self->text_x = PADDING;
746 w += ITEM_HEIGHT + PADDING;
747 self->text_x += ITEM_HEIGHT + PADDING;
754 XResizeWindow(ob_display, self->window, w, h);
758 RrPaint(self->a_items, self->window, w, h);
760 for (it = self->entries; it; it = g_list_next(it))
761 menu_entry_frame_render(it->data);
763 w += ob_rr_theme->mbwidth * 2;
764 h += ob_rr_theme->mbwidth * 2;
766 RECT_SET_SIZE(self->area, w, h);
771 static void menu_frame_update(ObMenuFrame *self)
777 menu_pipe_execute(self->menu);
778 menu_find_submenus(self->menu);
780 self->selected = NULL;
782 /* start at show_from */
783 mit = g_list_nth(self->menu->entries, self->show_from);
785 /* go through the menu's and frame's entries and connect the frame entries
786 to the menu entries */
787 for (fit = self->entries; mit && fit;
788 mit = g_list_next(mit), fit = g_list_next(fit))
790 ObMenuEntryFrame *f = fit->data;
791 f->entry = mit->data;
794 /* if there are more menu entries than in the frame, add them */
796 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
797 self->entries = g_list_append(self->entries, e);
798 mit = g_list_next(mit);
801 /* if there are more frame entries than menu entries then get rid of
804 GList *n = g_list_next(fit);
805 menu_entry_frame_free(fit->data);
806 self->entries = g_list_delete_link(self->entries, fit);
810 /* * make the menu fit on the screen */
812 /* calculate the height of the menu */
814 for (fit = self->entries; fit; fit = g_list_next(fit))
815 h += menu_entry_frame_get_height(fit->data,
816 fit == self->entries,
817 g_list_next(fit) == NULL);
818 /* add the border at the top and bottom */
819 h += ob_rr_theme->mbwidth * 2;
821 a = screen_physical_area_monitor(self->monitor);
825 gboolean last_entry = TRUE;
827 /* take the height of our More... entry into account */
828 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
830 /* start at the end of the entries */
831 flast = g_list_last(self->entries);
833 /* pull out all the entries from the frame that don't
834 fit on the screen, leaving at least 1 though */
835 while (h > a->height && g_list_previous(flast) != NULL) {
836 /* update the height, without this entry */
837 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
839 /* destroy the entry we're not displaying */
841 flast = g_list_previous(flast);
842 menu_entry_frame_free(tmp->data);
843 self->entries = g_list_delete_link(self->entries, tmp);
845 /* only the first one that we see is the last entry in the menu */
850 ObMenuEntry *more_entry;
851 ObMenuEntryFrame *more_frame;
852 /* make the More... menu entry frame which will display in this
854 if self->menu->more_menu is NULL that means that this is already
855 More... menu, so just use ourself.
857 more_entry = menu_get_more((self->menu->more_menu ?
858 self->menu->more_menu :
860 /* continue where we left off */
862 g_list_length(self->entries));
863 more_frame = menu_entry_frame_new(more_entry, self);
864 /* make it get deleted when the menu frame goes away */
865 menu_entry_unref(more_entry);
867 /* add our More... entry to the frame */
868 self->entries = g_list_append(self->entries, more_frame);
874 menu_frame_render(self);
877 static gboolean menu_frame_is_visible(ObMenuFrame *self)
879 return !!(g_list_find(menu_frame_visible, self));
882 static gboolean menu_frame_show(ObMenuFrame *self)
886 /* determine if the underlying menu is already visible */
887 for (it = menu_frame_visible; it; it = g_list_next(it)) {
888 ObMenuFrame *f = it->data;
889 if (f->menu == self->menu)
893 if (self->menu->update_func)
894 if (!self->menu->update_func(self, self->menu->data))
898 if (menu_frame_visible == NULL) {
899 /* no menus shown yet */
901 /* grab the pointer in such a way as to pass through "owner events"
902 so that we can get enter/leave notifies in the menu. */
903 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
905 if (!grab_keyboard()) {
911 menu_frame_update(self);
913 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
915 if (self->menu->show_func)
916 self->menu->show_func(self, self->menu->data);
921 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
927 if (menu_frame_is_visible(self))
929 if (!menu_frame_show(self))
932 /* find the monitor the menu is on */
933 for (i = 0; i < screen_num_monitors; ++i) {
934 Rect *a = screen_physical_area_monitor(i);
935 gboolean contains = RECT_CONTAINS(*a, x, y);
943 if (self->menu->place_func)
944 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
946 menu_frame_place_topmenu(self, &x, &y);
948 menu_frame_move(self, x, y);
950 XMapWindow(ob_display, self->window);
952 if (screen_pointer_pos(&px, &py)) {
953 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
954 if (e && e->frame == self)
961 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
962 ObMenuEntryFrame *parent_entry)
967 if (menu_frame_is_visible(self))
970 self->monitor = parent->monitor;
971 self->parent = parent;
972 self->parent_entry = parent_entry;
974 /* set up parent's child to be us */
976 menu_frame_hide(parent->child);
977 parent->child = self;
979 if (!menu_frame_show(self))
982 menu_frame_place_submenu(self, &x, &y);
983 menu_frame_move_on_screen(self, x, y, &dx, &dy);
986 /*try the other side */
987 self->direction_right = !self->direction_right;
988 menu_frame_place_submenu(self, &x, &y);
989 menu_frame_move_on_screen(self, x, y, &dx, &dy);
991 menu_frame_move(self, x + dx, y + dy);
993 XMapWindow(ob_display, self->window);
995 if (screen_pointer_pos(&px, &py)) {
996 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
997 if (e && e->frame == self)
1004 static void menu_frame_hide(ObMenuFrame *self)
1006 GList *it = g_list_find(menu_frame_visible, self);
1011 if (self->menu->hide_func)
1012 self->menu->hide_func(self, self->menu->data);
1015 menu_frame_hide(self->child);
1018 self->parent->child = NULL;
1019 self->parent = NULL;
1020 self->parent_entry = NULL;
1022 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1024 if (menu_frame_visible == NULL) {
1025 /* last menu shown */
1030 XUnmapWindow(ob_display, self->window);
1032 menu_frame_free(self);
1035 void menu_frame_hide_all(void)
1039 if (config_submenu_show_delay) {
1040 /* remove any submenu open requests */
1041 ob_main_loop_timeout_remove(ob_main_loop,
1042 menu_entry_frame_submenu_timeout);
1044 if ((it = g_list_last(menu_frame_visible)))
1045 menu_frame_hide(it->data);
1048 void menu_frame_hide_all_client(ObClient *client)
1050 GList *it = g_list_last(menu_frame_visible);
1052 ObMenuFrame *f = it->data;
1053 if (f->client == client) {
1054 if (config_submenu_show_delay) {
1055 /* remove any submenu open requests */
1056 ob_main_loop_timeout_remove(ob_main_loop,
1057 menu_entry_frame_submenu_timeout);
1065 ObMenuFrame* menu_frame_under(gint x, gint y)
1067 ObMenuFrame *ret = NULL;
1070 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1071 ObMenuFrame *f = it->data;
1073 if (RECT_CONTAINS(f->area, x, y)) {
1081 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1084 ObMenuEntryFrame *ret = NULL;
1087 if ((frame = menu_frame_under(x, y))) {
1088 x -= ob_rr_theme->mbwidth + frame->area.x;
1089 y -= ob_rr_theme->mbwidth + frame->area.y;
1091 for (it = frame->entries; it; it = g_list_next(it)) {
1092 ObMenuEntryFrame *e = it->data;
1094 if (RECT_CONTAINS(e->area, x, y)) {
1103 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1105 g_assert(menu_frame_visible);
1106 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1110 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1113 ObMenuEntryFrame *old = self->selected;
1114 ObMenuFrame *oldchild = self->child;
1116 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1119 if (old == entry) return;
1121 if (config_submenu_show_delay) {
1122 /* remove any submenu open requests */
1123 ob_main_loop_timeout_remove(ob_main_loop,
1124 menu_entry_frame_submenu_timeout);
1127 self->selected = entry;
1130 menu_entry_frame_render(old);
1132 menu_frame_hide(oldchild);
1134 if (self->selected) {
1135 menu_entry_frame_render(self->selected);
1137 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1138 if (config_submenu_show_delay && !immediate) {
1139 /* initiate a new submenu open request */
1140 ob_main_loop_timeout_add(ob_main_loop,
1141 config_submenu_show_delay * 1000,
1142 menu_entry_frame_submenu_timeout,
1143 self->selected, g_direct_equal,
1146 menu_entry_frame_show_submenu(self->selected);
1152 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1156 if (!self->entry->data.submenu.submenu) return;
1158 f = menu_frame_new(self->entry->data.submenu.submenu,
1159 self->entry->data.submenu.show_from,
1160 self->frame->client);
1161 /* pass our direction on to our child */
1162 f->direction_right = self->frame->direction_right;
1164 menu_frame_show_submenu(f, self->frame, self);
1167 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1169 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1170 self->entry->data.normal.enabled)
1172 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1174 ObMenuEntry *entry = self->entry;
1175 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1176 gpointer data = self->frame->menu->data;
1177 GSList *acts = self->entry->data.normal.actions;
1178 ObClient *client = self->frame->client;
1179 ObMenuFrame *frame = self->frame;
1181 /* release grabs before executing the shit */
1182 if (!(state & ControlMask)) {
1183 menu_frame_hide_all();
1188 func(entry, frame, client, state, data);
1190 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1191 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1195 void menu_frame_select_previous(ObMenuFrame *self)
1197 GList *it = NULL, *start;
1199 if (self->entries) {
1200 start = it = g_list_find(self->entries, self->selected);
1202 ObMenuEntryFrame *e;
1204 it = it ? g_list_previous(it) : g_list_last(self->entries);
1210 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1212 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1217 menu_frame_select(self, it ? it->data : NULL, TRUE);
1220 void menu_frame_select_next(ObMenuFrame *self)
1222 GList *it = NULL, *start;
1224 if (self->entries) {
1225 start = it = g_list_find(self->entries, self->selected);
1227 ObMenuEntryFrame *e;
1229 it = it ? g_list_next(it) : self->entries;
1235 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1237 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1242 menu_frame_select(self, it ? it->data : NULL, TRUE);