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 MAX_MENU_WIDTH 400
34 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
36 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
38 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
39 ButtonPressMask | ButtonReleaseMask)
41 GList *menu_frame_visible;
42 GHashTable *menu_frame_map;
44 static RrAppearance *a_sep;
46 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
48 static void menu_entry_frame_free(ObMenuEntryFrame *self);
49 static void menu_frame_update(ObMenuFrame *self);
50 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
51 static void menu_frame_hide(ObMenuFrame *self);
53 static Window createWindow(Window parent, gulong mask,
54 XSetWindowAttributes *attrib)
56 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
57 RrDepth(ob_rr_inst), InputOutput,
58 RrVisual(ob_rr_inst), mask, attrib);
61 void menu_frame_startup(gboolean reconfig)
65 a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
66 RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
67 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
68 a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
69 a_sep->texture[i].data.lineart.color =
70 ob_rr_theme->menu_sep_color;
75 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
78 void menu_frame_shutdown(gboolean reconfig)
80 RrAppearanceFree(a_sep);
84 g_hash_table_destroy(menu_frame_map);
87 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
90 XSetWindowAttributes attr;
92 self = g_new0(ObMenuFrame, 1);
93 self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
95 self->selected = NULL;
96 self->client = client;
97 self->direction_right = TRUE;
98 self->show_from = show_from;
100 attr.event_mask = FRAME_EVENTMASK;
101 self->window = createWindow(obt_root(ob_screen),
104 /* make it a popup menu type window */
105 OBT_PROP_SET32(self->window, NET_WM_WINDOW_TYPE, ATOM,
106 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU));
108 XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
109 XSetWindowBorder(obt_display, self->window,
110 RrColorPixel(ob_rr_theme->menu_border_color));
112 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
114 window_add(&self->window, MENUFRAME_AS_WINDOW(self));
115 stacking_add(MENUFRAME_AS_WINDOW(self));
120 void menu_frame_free(ObMenuFrame *self)
123 while (self->entries) {
124 menu_entry_frame_free(self->entries->data);
125 self->entries = g_list_delete_link(self->entries, self->entries);
128 stacking_remove(MENUFRAME_AS_WINDOW(self));
129 window_remove(self->window);
131 RrAppearanceFree(self->a_items);
133 XDestroyWindow(obt_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(obt_display, self->window);
166 XMapWindow(obt_display, self->text);
168 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
173 static void menu_entry_frame_free(ObMenuEntryFrame *self)
176 menu_entry_unref(self->entry);
178 window_remove(self->window);
180 XDestroyWindow(obt_display, self->text);
181 XDestroyWindow(obt_display, self->window);
182 g_hash_table_remove(menu_frame_map, &self->text);
183 g_hash_table_remove(menu_frame_map, &self->window);
184 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
185 XDestroyWindow(obt_display, self->icon);
186 g_hash_table_remove(menu_frame_map, &self->icon);
188 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
189 XDestroyWindow(obt_display, self->bullet);
190 g_hash_table_remove(menu_frame_map, &self->bullet);
197 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
199 RECT_SET_POINT(self->area, x, y);
200 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
203 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
207 if (config_menu_middle) {
209 *x -= self->area.width / 2;
211 /* try below the cursor */
212 menu_frame_move_on_screen(self, *x, *y, &dx, &dy);
213 self->direction_right = TRUE;
222 /* try to the bottom right of the cursor */
223 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
224 self->direction_right = TRUE;
225 if (dx != 0 || dy != 0) {
226 /* try to the bottom left of the cursor */
227 myx = *x - self->area.width;
229 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
230 self->direction_right = FALSE;
232 if (dx != 0 || dy != 0) {
233 /* try to the top right of the cursor */
235 myy = *y - self->area.height;
236 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
237 self->direction_right = TRUE;
239 if (dx != 0 || dy != 0) {
240 /* try to the top left of the cursor */
241 myx = *x - self->area.width;
242 myy = *y - self->area.height;
243 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
244 self->direction_right = FALSE;
246 if (dx != 0 || dy != 0) {
247 /* if didnt fit on either side so just use what it says */
250 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
251 self->direction_right = TRUE;
258 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
260 gint overlapx, overlapy;
263 overlapx = ob_rr_theme->menu_overlap_x;
264 overlapy = ob_rr_theme->menu_overlap_y;
265 bwidth = ob_rr_theme->mbwidth;
267 if (self->direction_right)
268 *x = self->parent->area.x + self->parent->area.width -
271 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
273 *y = self->parent->area.y + self->parent_entry->area.y;
274 if (config_menu_middle)
275 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
280 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
288 a = screen_physical_area_monitor(self->monitor);
290 half = g_list_length(self->entries) / 2;
291 pos = g_list_index(self->entries, self->selected);
293 /* if in the bottom half then check this stuff first, will keep the bottom
294 edge of the menu visible */
296 *dx = MAX(*dx, a->x - x);
297 *dy = MAX(*dy, a->y - y);
299 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
300 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
301 /* if in the top half then check this stuff last, will keep the top
302 edge of the menu visible */
304 *dx = MAX(*dx, a->x - x);
305 *dy = MAX(*dy, a->y - y);
311 static void menu_entry_frame_render(ObMenuEntryFrame *self)
313 RrAppearance *item_a, *text_a;
316 ObMenuFrame *frame = self->frame;
318 switch (self->entry->type) {
319 case OB_MENU_ENTRY_TYPE_NORMAL:
320 case OB_MENU_ENTRY_TYPE_SUBMENU:
321 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
322 !self->entry->data.normal.enabled ?
324 (self == self->frame->selected ?
325 ob_rr_theme->a_menu_disabled_selected :
326 ob_rr_theme->a_menu_disabled) :
328 (self == self->frame->selected ?
329 ob_rr_theme->a_menu_selected :
330 ob_rr_theme->a_menu_normal));
333 case OB_MENU_ENTRY_TYPE_SEPARATOR:
334 if (self->entry->data.separator.label) {
335 item_a = ob_rr_theme->a_menu_title;
336 th = ob_rr_theme->menu_title_height;
338 item_a = ob_rr_theme->a_menu_normal;
339 th = ob_rr_theme->menu_sep_width +
340 2*ob_rr_theme->menu_sep_paddingy;
344 g_assert_not_reached();
347 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
348 XResizeWindow(obt_display, self->window,
349 self->area.width, self->area.height);
350 item_a->surface.parent = self->frame->a_items;
351 item_a->surface.parentx = self->area.x;
352 item_a->surface.parenty = self->area.y;
353 RrPaint(item_a, self->window, self->area.width, self->area.height);
355 switch (self->entry->type) {
356 case OB_MENU_ENTRY_TYPE_NORMAL:
357 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
358 !self->entry->data.normal.enabled ?
360 (self == self->frame->selected ?
361 ob_rr_theme->a_menu_text_disabled_selected :
362 ob_rr_theme->a_menu_text_disabled) :
364 (self == self->frame->selected ?
365 ob_rr_theme->a_menu_text_selected :
366 ob_rr_theme->a_menu_text_normal));
367 text_a->texture[0].data.text.string = self->entry->data.normal.label;
368 if (self->entry->data.normal.shortcut &&
369 (self->frame->menu->show_all_shortcuts ||
370 self->entry->data.normal.shortcut_always_show ||
371 self->entry->data.normal.shortcut_position > 0))
373 text_a->texture[0].data.text.shortcut = TRUE;
374 text_a->texture[0].data.text.shortcut_pos =
375 self->entry->data.normal.shortcut_position;
377 text_a->texture[0].data.text.shortcut = FALSE;
379 case OB_MENU_ENTRY_TYPE_SUBMENU:
380 text_a = (self == self->frame->selected ?
381 ob_rr_theme->a_menu_text_selected :
382 ob_rr_theme->a_menu_text_normal);
383 sub = self->entry->data.submenu.submenu;
384 text_a->texture[0].data.text.string = sub ? sub->title : "";
385 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
386 sub->shortcut_always_show ||
387 sub->shortcut_position > 0))
389 text_a->texture[0].data.text.shortcut = TRUE;
390 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
392 text_a->texture[0].data.text.shortcut = FALSE;
394 case OB_MENU_ENTRY_TYPE_SEPARATOR:
395 if (self->entry->data.separator.label != NULL) {
396 text_a = ob_rr_theme->a_menu_text_title;
397 text_a->texture[0].data.text.string =
398 self->entry->data.separator.label;
401 text_a = ob_rr_theme->a_menu_text_normal;
405 switch (self->entry->type) {
406 case OB_MENU_ENTRY_TYPE_NORMAL:
407 XMoveResizeWindow(obt_display, self->text,
408 self->frame->text_x, PADDING,
410 ITEM_HEIGHT - 2*PADDING);
411 text_a->surface.parent = item_a;
412 text_a->surface.parentx = self->frame->text_x;
413 text_a->surface.parenty = PADDING;
414 RrPaint(text_a, self->text, self->frame->text_w,
415 ITEM_HEIGHT - 2*PADDING);
417 case OB_MENU_ENTRY_TYPE_SUBMENU:
418 XMoveResizeWindow(obt_display, self->text,
419 self->frame->text_x, PADDING,
420 self->frame->text_w - ITEM_HEIGHT,
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 - ITEM_HEIGHT,
426 ITEM_HEIGHT - 2*PADDING);
428 case OB_MENU_ENTRY_TYPE_SEPARATOR:
429 if (self->entry->data.separator.label != NULL) {
430 /* labeled separator */
431 XMoveResizeWindow(obt_display, self->text,
432 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
433 self->area.width - 2*ob_rr_theme->paddingx,
434 ob_rr_theme->menu_title_height -
435 2*ob_rr_theme->paddingy);
436 text_a->surface.parent = item_a;
437 text_a->surface.parentx = ob_rr_theme->paddingx;
438 text_a->surface.parenty = ob_rr_theme->paddingy;
439 RrPaint(text_a, self->text,
440 self->area.width - 2*ob_rr_theme->paddingx,
441 ob_rr_theme->menu_title_height -
442 2*ob_rr_theme->paddingy);
446 /* unlabeled separator */
447 XMoveResizeWindow(obt_display, self->text, 0, 0,
449 ob_rr_theme->menu_sep_width +
450 2*ob_rr_theme->menu_sep_paddingy);
452 a_sep->surface.parent = item_a;
453 a_sep->surface.parentx = 0;
454 a_sep->surface.parenty = 0;
455 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
456 a_sep->texture[i].data.lineart.x1 =
457 ob_rr_theme->menu_sep_paddingx;
458 a_sep->texture[i].data.lineart.y1 =
459 ob_rr_theme->menu_sep_paddingy + i;
460 a_sep->texture[i].data.lineart.x2 =
461 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
462 a_sep->texture[i].data.lineart.y2 =
463 ob_rr_theme->menu_sep_paddingy + i;
466 RrPaint(a_sep, self->text, self->area.width,
467 ob_rr_theme->menu_sep_width +
468 2*ob_rr_theme->menu_sep_paddingy);
473 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
474 self->entry->data.normal.icon)
478 XMoveResizeWindow(obt_display, self->icon,
479 PADDING, frame->item_margin.top,
480 ITEM_HEIGHT - frame->item_margin.top
481 - frame->item_margin.bottom,
482 ITEM_HEIGHT - frame->item_margin.top
483 - frame->item_margin.bottom);
485 clear = ob_rr_theme->a_clear_tex;
486 RrAppearanceClearTextures(clear);
487 clear->texture[0].type = RR_TEXTURE_IMAGE;
488 clear->texture[0].data.image.image =
489 self->entry->data.normal.icon;
490 clear->texture[0].data.image.alpha =
491 self->entry->data.normal.icon_alpha;
492 clear->surface.parent = item_a;
493 clear->surface.parentx = PADDING;
494 clear->surface.parenty = frame->item_margin.top;
495 RrPaint(clear, self->icon,
496 ITEM_HEIGHT - frame->item_margin.top
497 - frame->item_margin.bottom,
498 ITEM_HEIGHT - frame->item_margin.top
499 - frame->item_margin.bottom);
500 XMapWindow(obt_display, self->icon);
501 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
502 self->entry->data.normal.mask)
507 XMoveResizeWindow(obt_display, self->icon,
508 PADDING, frame->item_margin.top,
509 ITEM_HEIGHT - frame->item_margin.top
510 - frame->item_margin.bottom,
511 ITEM_HEIGHT - frame->item_margin.top
512 - frame->item_margin.bottom);
514 clear = ob_rr_theme->a_clear_tex;
515 RrAppearanceClearTextures(clear);
516 clear->texture[0].type = RR_TEXTURE_MASK;
517 clear->texture[0].data.mask.mask =
518 self->entry->data.normal.mask;
520 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
521 !self->entry->data.normal.enabled ?
523 (self == self->frame->selected ?
524 self->entry->data.normal.mask_disabled_selected_color :
525 self->entry->data.normal.mask_disabled_color) :
527 (self == self->frame->selected ?
528 self->entry->data.normal.mask_selected_color :
529 self->entry->data.normal.mask_normal_color));
530 clear->texture[0].data.mask.color = c;
532 clear->surface.parent = item_a;
533 clear->surface.parentx = PADDING;
534 clear->surface.parenty = frame->item_margin.top;
535 RrPaint(clear, self->icon,
536 ITEM_HEIGHT - frame->item_margin.top
537 - frame->item_margin.bottom,
538 ITEM_HEIGHT - frame->item_margin.top
539 - frame->item_margin.bottom);
540 XMapWindow(obt_display, self->icon);
542 XUnmapWindow(obt_display, self->icon);
544 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
545 RrAppearance *bullet_a;
546 XMoveResizeWindow(obt_display, self->bullet,
547 self->frame->text_x + self->frame->text_w -
548 ITEM_HEIGHT + PADDING, PADDING,
549 ITEM_HEIGHT - 2*PADDING,
550 ITEM_HEIGHT - 2*PADDING);
551 bullet_a = (self == self->frame->selected ?
552 ob_rr_theme->a_menu_bullet_selected :
553 ob_rr_theme->a_menu_bullet_normal);
554 bullet_a->surface.parent = item_a;
555 bullet_a->surface.parentx =
556 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
557 bullet_a->surface.parenty = PADDING;
558 RrPaint(bullet_a, self->bullet,
559 ITEM_HEIGHT - 2*PADDING,
560 ITEM_HEIGHT - 2*PADDING);
561 XMapWindow(obt_display, self->bullet);
563 XUnmapWindow(obt_display, self->bullet);
568 /*! this code is taken from the menu_frame_render. if that changes, this won't
570 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
571 gboolean first_entry,
580 t = self->entry->type;
582 /* this is the More... entry, it's NORMAL type */
583 t = OB_MENU_ENTRY_TYPE_NORMAL;
586 case OB_MENU_ENTRY_TYPE_NORMAL:
587 case OB_MENU_ENTRY_TYPE_SUBMENU:
588 h += ob_rr_theme->menu_font_height;
590 case OB_MENU_ENTRY_TYPE_SEPARATOR:
591 if (self->entry->data.separator.label != NULL) {
592 h += ob_rr_theme->menu_title_height +
593 (ob_rr_theme->mbwidth - PADDING) * 2;
595 /* if the first entry is a labeled separator, then make its border
596 overlap with the menu's outside border */
598 h -= ob_rr_theme->mbwidth;
599 /* if the last entry is a labeled separator, then make its border
600 overlap with the menu's outside border */
602 h -= ob_rr_theme->mbwidth;
604 h += ob_rr_theme->menu_sep_width +
605 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
613 void menu_frame_render(ObMenuFrame *self)
616 gint tw, th; /* temps */
618 gboolean has_icon = FALSE;
622 /* find text dimensions */
624 STRUT_SET(self->item_margin, 0, 0, 0, 0);
629 e = self->entries->data;
630 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
631 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
636 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
637 STRUT_SET(self->item_margin,
638 MAX(self->item_margin.left, l),
639 MAX(self->item_margin.top, t),
640 MAX(self->item_margin.right, r),
641 MAX(self->item_margin.bottom, b));
642 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
643 STRUT_SET(self->item_margin,
644 MAX(self->item_margin.left, l),
645 MAX(self->item_margin.top, t),
646 MAX(self->item_margin.right, r),
647 MAX(self->item_margin.bottom, b));
648 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
649 STRUT_SET(self->item_margin,
650 MAX(self->item_margin.left, l),
651 MAX(self->item_margin.top, t),
652 MAX(self->item_margin.right, r),
653 MAX(self->item_margin.bottom, b));
654 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
655 STRUT_SET(self->item_margin,
656 MAX(self->item_margin.left, l),
657 MAX(self->item_margin.top, t),
658 MAX(self->item_margin.right, r),
659 MAX(self->item_margin.bottom, b));
662 /* render the entries */
664 for (it = self->entries; it; it = g_list_next(it)) {
665 RrAppearance *text_a;
668 /* if the first entry is a labeled separator, then make its border
669 overlap with the menu's outside border */
670 if (it == self->entries &&
671 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
672 e->entry->data.separator.label)
674 h -= ob_rr_theme->mbwidth;
677 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
678 e->entry->data.separator.label)
680 e->border = ob_rr_theme->mbwidth;
683 RECT_SET_POINT(e->area, 0, h+e->border);
684 XMoveWindow(obt_display, e->window,
685 e->area.x-e->border, e->area.y-e->border);
686 XSetWindowBorderWidth(obt_display, e->window, e->border);
687 XSetWindowBorder(obt_display, e->window,
688 RrColorPixel(ob_rr_theme->menu_border_color));
690 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
691 !e->entry->data.normal.enabled ?
693 (e == self->selected ?
694 ob_rr_theme->a_menu_text_disabled_selected :
695 ob_rr_theme->a_menu_text_disabled) :
697 (e == self->selected ?
698 ob_rr_theme->a_menu_text_selected :
699 ob_rr_theme->a_menu_text_normal));
700 switch (e->entry->type) {
701 case OB_MENU_ENTRY_TYPE_NORMAL:
702 text_a->texture[0].data.text.string = e->entry->data.normal.label;
703 tw = RrMinWidth(text_a);
704 tw = MIN(tw, MAX_MENU_WIDTH);
705 th = ob_rr_theme->menu_font_height;
707 if (e->entry->data.normal.icon ||
708 e->entry->data.normal.mask)
711 case OB_MENU_ENTRY_TYPE_SUBMENU:
712 sub = e->entry->data.submenu.submenu;
713 text_a->texture[0].data.text.string = sub ? sub->title : "";
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 tw += ITEM_HEIGHT - PADDING;
724 case OB_MENU_ENTRY_TYPE_SEPARATOR:
725 if (e->entry->data.separator.label != NULL) {
726 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
727 e->entry->data.separator.label;
728 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
729 2*ob_rr_theme->paddingx;
730 tw = MIN(tw, MAX_MENU_WIDTH);
731 th = ob_rr_theme->menu_title_height +
732 (ob_rr_theme->mbwidth - PADDING) *2;
735 th = ob_rr_theme->menu_sep_width +
736 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
748 /* if the last entry is a labeled separator, then make its border
749 overlap with the menu's outside border */
750 it = g_list_last(self->entries);
751 e = it ? it->data : NULL;
752 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
753 e->entry->data.separator.label)
755 h -= ob_rr_theme->mbwidth;
758 self->text_x = PADDING;
763 w += ITEM_HEIGHT + PADDING;
764 self->text_x += ITEM_HEIGHT + PADDING;
771 XResizeWindow(obt_display, self->window, w, h);
775 RrPaint(self->a_items, self->window, w, h);
777 for (it = self->entries; it; it = g_list_next(it))
778 menu_entry_frame_render(it->data);
780 w += ob_rr_theme->mbwidth * 2;
781 h += ob_rr_theme->mbwidth * 2;
783 RECT_SET_SIZE(self->area, w, h);
788 static void menu_frame_update(ObMenuFrame *self)
794 menu_pipe_execute(self->menu);
795 menu_find_submenus(self->menu);
797 self->selected = NULL;
799 /* start at show_from */
800 mit = g_list_nth(self->menu->entries, self->show_from);
802 /* go through the menu's and frame's entries and connect the frame entries
803 to the menu entries */
804 for (fit = self->entries; mit && fit;
805 mit = g_list_next(mit), fit = g_list_next(fit))
807 ObMenuEntryFrame *f = fit->data;
808 f->entry = mit->data;
811 /* if there are more menu entries than in the frame, add them */
813 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
814 self->entries = g_list_append(self->entries, e);
815 mit = g_list_next(mit);
818 /* if there are more frame entries than menu entries then get rid of
821 GList *n = g_list_next(fit);
822 menu_entry_frame_free(fit->data);
823 self->entries = g_list_delete_link(self->entries, fit);
827 /* * make the menu fit on the screen */
829 /* calculate the height of the menu */
831 for (fit = self->entries; fit; fit = g_list_next(fit))
832 h += menu_entry_frame_get_height(fit->data,
833 fit == self->entries,
834 g_list_next(fit) == NULL);
835 /* add the border at the top and bottom */
836 h += ob_rr_theme->mbwidth * 2;
838 a = screen_physical_area_monitor(self->monitor);
842 gboolean last_entry = TRUE;
844 /* take the height of our More... entry into account */
845 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
847 /* start at the end of the entries */
848 flast = g_list_last(self->entries);
850 /* pull out all the entries from the frame that don't
851 fit on the screen, leaving at least 1 though */
852 while (h > a->height && g_list_previous(flast) != NULL) {
853 /* update the height, without this entry */
854 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
856 /* destroy the entry we're not displaying */
858 flast = g_list_previous(flast);
859 menu_entry_frame_free(tmp->data);
860 self->entries = g_list_delete_link(self->entries, tmp);
862 /* only the first one that we see is the last entry in the menu */
867 ObMenuEntry *more_entry;
868 ObMenuEntryFrame *more_frame;
869 /* make the More... menu entry frame which will display in this
871 if self->menu->more_menu is NULL that means that this is already
872 More... menu, so just use ourself.
874 more_entry = menu_get_more((self->menu->more_menu ?
875 self->menu->more_menu :
877 /* continue where we left off */
879 g_list_length(self->entries));
880 more_frame = menu_entry_frame_new(more_entry, self);
881 /* make it get deleted when the menu frame goes away */
882 menu_entry_unref(more_entry);
884 /* add our More... entry to the frame */
885 self->entries = g_list_append(self->entries, more_frame);
891 menu_frame_render(self);
894 static gboolean menu_frame_is_visible(ObMenuFrame *self)
896 return !!(g_list_find(menu_frame_visible, self));
899 static gboolean menu_frame_show(ObMenuFrame *self)
903 /* determine if the underlying menu is already visible */
904 for (it = menu_frame_visible; it; it = g_list_next(it)) {
905 ObMenuFrame *f = it->data;
906 if (f->menu == self->menu)
910 if (self->menu->update_func)
911 if (!self->menu->update_func(self, self->menu->data))
915 if (menu_frame_visible == NULL) {
916 /* no menus shown yet */
918 /* grab the pointer in such a way as to pass through "owner events"
919 so that we can get enter/leave notifies in the menu. */
920 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
922 if (!grab_keyboard()) {
928 menu_frame_update(self);
930 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
932 if (self->menu->show_func)
933 self->menu->show_func(self, self->menu->data);
938 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
944 if (menu_frame_is_visible(self))
946 if (!menu_frame_show(self))
949 /* find the monitor the menu is on */
950 for (i = 0; i < screen_num_monitors; ++i) {
951 Rect *a = screen_physical_area_monitor(i);
952 gboolean contains = RECT_CONTAINS(*a, x, y);
960 if (self->menu->place_func)
961 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
963 menu_frame_place_topmenu(self, &x, &y);
965 menu_frame_move(self, x, y);
967 XMapWindow(obt_display, self->window);
969 if (screen_pointer_pos(&px, &py)) {
970 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
971 if (e && e->frame == self)
978 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
979 ObMenuEntryFrame *parent_entry)
984 if (menu_frame_is_visible(self))
987 self->monitor = parent->monitor;
988 self->parent = parent;
989 self->parent_entry = parent_entry;
991 /* set up parent's child to be us */
993 menu_frame_hide(parent->child);
994 parent->child = self;
996 if (!menu_frame_show(self))
999 menu_frame_place_submenu(self, &x, &y);
1000 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1003 /*try the other side */
1004 self->direction_right = !self->direction_right;
1005 menu_frame_place_submenu(self, &x, &y);
1006 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1008 menu_frame_move(self, x + dx, y + dy);
1010 XMapWindow(obt_display, self->window);
1012 if (screen_pointer_pos(&px, &py)) {
1013 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1014 if (e && e->frame == self)
1021 static void menu_frame_hide(ObMenuFrame *self)
1023 GList *it = g_list_find(menu_frame_visible, self);
1028 if (self->menu->hide_func)
1029 self->menu->hide_func(self, self->menu->data);
1032 menu_frame_hide(self->child);
1035 self->parent->child = NULL;
1036 self->parent = NULL;
1037 self->parent_entry = NULL;
1039 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1041 if (menu_frame_visible == NULL) {
1042 /* last menu shown */
1047 XUnmapWindow(obt_display, self->window);
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 obt_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 obt_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 obt_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 obt_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);