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 switch (entry->type) {
157 case OB_MENU_ENTRY_TYPE_SUBMENU:
158 self->bullet = createWindow(self->window, 0, NULL);
159 g_hash_table_insert(menu_frame_map, &self->bullet, self);
161 case OB_MENU_ENTRY_TYPE_NORMAL:
162 self->icon = createWindow(self->window, 0, NULL);
163 g_hash_table_insert(menu_frame_map, &self->icon, self);
167 XMapWindow(obt_display, self->window);
168 XMapWindow(obt_display, self->text);
170 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
175 static void menu_entry_frame_free(ObMenuEntryFrame *self)
178 menu_entry_unref(self->entry);
180 window_remove(self->window);
182 XDestroyWindow(obt_display, self->text);
183 XDestroyWindow(obt_display, self->window);
184 g_hash_table_remove(menu_frame_map, &self->text);
185 g_hash_table_remove(menu_frame_map, &self->window);
186 switch (self->entry->type) {
187 case OB_MENU_ENTRY_TYPE_SUBMENU:
188 XDestroyWindow(obt_display, self->bullet);
189 g_hash_table_remove(menu_frame_map, &self->bullet);
191 case OB_MENU_ENTRY_TYPE_NORMAL:
192 XDestroyWindow(obt_display, self->icon);
193 g_hash_table_remove(menu_frame_map, &self->icon);
201 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
203 RECT_SET_POINT(self->area, x, y);
204 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
207 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
211 if (config_menu_middle) {
213 *x -= self->area.width / 2;
215 /* try below the cursor */
216 menu_frame_move_on_screen(self, *x, *y, &dx, &dy);
217 self->direction_right = TRUE;
226 /* try to the bottom right of the cursor */
227 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
228 self->direction_right = TRUE;
229 if (dx != 0 || dy != 0) {
230 /* try to the bottom left of the cursor */
231 myx = *x - self->area.width;
233 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
234 self->direction_right = FALSE;
236 if (dx != 0 || dy != 0) {
237 /* try to the top right of the cursor */
239 myy = *y - self->area.height;
240 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
241 self->direction_right = TRUE;
243 if (dx != 0 || dy != 0) {
244 /* try to the top left of the cursor */
245 myx = *x - self->area.width;
246 myy = *y - self->area.height;
247 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
248 self->direction_right = FALSE;
250 if (dx != 0 || dy != 0) {
251 /* if didnt fit on either side so just use what it says */
254 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
255 self->direction_right = TRUE;
262 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
264 gint overlapx, overlapy;
267 overlapx = ob_rr_theme->menu_overlap_x;
268 overlapy = ob_rr_theme->menu_overlap_y;
269 bwidth = ob_rr_theme->mbwidth;
271 if (self->direction_right)
272 *x = self->parent->area.x + self->parent->area.width -
275 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
277 *y = self->parent->area.y + self->parent_entry->area.y;
278 if (config_menu_middle)
279 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
284 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
292 a = screen_physical_area_monitor(self->monitor);
294 half = g_list_length(self->entries) / 2;
295 pos = g_list_index(self->entries, self->selected);
297 /* if in the bottom half then check this stuff first, will keep the bottom
298 edge of the menu visible */
300 *dx = MAX(*dx, a->x - x);
301 *dy = MAX(*dy, a->y - y);
303 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
304 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
305 /* if in the top half then check this stuff last, will keep the top
306 edge of the menu visible */
308 *dx = MAX(*dx, a->x - x);
309 *dy = MAX(*dy, a->y - y);
315 static void menu_entry_frame_render(ObMenuEntryFrame *self)
317 RrAppearance *item_a, *text_a;
320 ObMenuFrame *frame = self->frame;
322 /* find size and background item appearance to use */
323 switch (self->entry->type) {
324 case OB_MENU_ENTRY_TYPE_NORMAL:
325 case OB_MENU_ENTRY_TYPE_SUBMENU:
326 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
327 !self->entry->data.normal.enabled ?
329 (self == self->frame->selected ?
330 ob_rr_theme->a_menu_disabled_selected :
331 ob_rr_theme->a_menu_disabled) :
333 (self == self->frame->selected ?
334 ob_rr_theme->a_menu_selected :
335 ob_rr_theme->a_menu_normal));
336 height = ITEM_HEIGHT;
338 case OB_MENU_ENTRY_TYPE_SEPARATOR:
339 if (self->entry->data.separator.label) {
340 item_a = ob_rr_theme->a_menu_title;
341 height = ob_rr_theme->menu_title_height;
343 item_a = ob_rr_theme->a_menu_normal;
344 height = ob_rr_theme->menu_sep_width +
345 2*ob_rr_theme->menu_sep_paddingy;
349 g_assert_not_reached();
352 RECT_SET_SIZE(self->area, self->frame->inner_w, height);
353 XResizeWindow(obt_display, self->window,
354 self->area.width, self->area.height);
355 item_a->surface.parent = self->frame->a_items;
356 item_a->surface.parentx = self->area.x;
357 item_a->surface.parenty = self->area.y;
358 RrPaint(item_a, self->window, self->area.width, self->area.height);
360 /* find the appearance for the text */
361 switch (self->entry->type) {
362 case OB_MENU_ENTRY_TYPE_NORMAL:
363 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
364 !self->entry->data.normal.enabled ?
366 (self == self->frame->selected ?
367 ob_rr_theme->a_menu_text_disabled_selected :
368 ob_rr_theme->a_menu_text_disabled) :
370 (self == self->frame->selected ?
371 ob_rr_theme->a_menu_text_selected :
372 ob_rr_theme->a_menu_text_normal));
373 text_a->texture[0].data.text.string = self->entry->data.normal.label;
374 if (self->entry->data.normal.shortcut &&
375 (self->frame->menu->show_all_shortcuts ||
376 self->entry->data.normal.shortcut_always_show ||
377 self->entry->data.normal.shortcut_position > 0))
379 text_a->texture[0].data.text.shortcut = TRUE;
380 text_a->texture[0].data.text.shortcut_pos =
381 self->entry->data.normal.shortcut_position;
383 text_a->texture[0].data.text.shortcut = FALSE;
385 case OB_MENU_ENTRY_TYPE_SUBMENU:
386 text_a = (self == self->frame->selected ?
387 ob_rr_theme->a_menu_text_selected :
388 ob_rr_theme->a_menu_text_normal);
389 sub = self->entry->data.submenu.submenu;
390 text_a->texture[0].data.text.string = sub ? sub->title : "";
391 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
392 sub->shortcut_always_show ||
393 sub->shortcut_position > 0))
395 text_a->texture[0].data.text.shortcut = TRUE;
396 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
398 text_a->texture[0].data.text.shortcut = FALSE;
400 case OB_MENU_ENTRY_TYPE_SEPARATOR:
401 if (self->entry->data.separator.label != NULL) {
402 text_a = ob_rr_theme->a_menu_text_title;
403 text_a->texture[0].data.text.string =
404 self->entry->data.separator.label;
407 text_a = ob_rr_theme->a_menu_text_normal;
411 /* Draw the text, dunno why this is separate from the above? */
412 switch (self->entry->type) {
413 case OB_MENU_ENTRY_TYPE_NORMAL:
414 XMoveResizeWindow(obt_display, self->text,
415 self->frame->text_x, PADDING,
417 ITEM_HEIGHT - 2*PADDING);
418 text_a->surface.parent = item_a;
419 text_a->surface.parentx = self->frame->text_x;
420 text_a->surface.parenty = PADDING;
421 RrPaint(text_a, self->text, self->frame->text_w,
422 ITEM_HEIGHT - 2*PADDING);
424 case OB_MENU_ENTRY_TYPE_SUBMENU:
425 XMoveResizeWindow(obt_display, self->text,
426 self->frame->text_x, PADDING,
427 self->frame->text_w - ITEM_HEIGHT,
428 ITEM_HEIGHT - 2*PADDING);
429 text_a->surface.parent = item_a;
430 text_a->surface.parentx = self->frame->text_x;
431 text_a->surface.parenty = PADDING;
432 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
433 ITEM_HEIGHT - 2*PADDING);
435 case OB_MENU_ENTRY_TYPE_SEPARATOR:
436 if (self->entry->data.separator.label != NULL) {
437 /* labeled separator */
438 XMoveResizeWindow(obt_display, self->text,
439 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
440 self->area.width - 2*ob_rr_theme->paddingx,
441 ob_rr_theme->menu_title_height -
442 2*ob_rr_theme->paddingy);
443 text_a->surface.parent = item_a;
444 text_a->surface.parentx = ob_rr_theme->paddingx;
445 text_a->surface.parenty = ob_rr_theme->paddingy;
446 RrPaint(text_a, self->text,
447 self->area.width - 2*ob_rr_theme->paddingx,
448 ob_rr_theme->menu_title_height -
449 2*ob_rr_theme->paddingy);
453 /* unlabeled separator */
454 XMoveResizeWindow(obt_display, self->text, 0, 0,
456 ob_rr_theme->menu_sep_width +
457 2*ob_rr_theme->menu_sep_paddingy);
459 a_sep->surface.parent = item_a;
460 a_sep->surface.parentx = 0;
461 a_sep->surface.parenty = 0;
462 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
463 a_sep->texture[i].data.lineart.x1 =
464 ob_rr_theme->menu_sep_paddingx;
465 a_sep->texture[i].data.lineart.y1 =
466 ob_rr_theme->menu_sep_paddingy + i;
467 a_sep->texture[i].data.lineart.x2 =
468 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
469 a_sep->texture[i].data.lineart.y2 =
470 ob_rr_theme->menu_sep_paddingy + i;
473 RrPaint(a_sep, self->text, self->area.width,
474 ob_rr_theme->menu_sep_width +
475 2*ob_rr_theme->menu_sep_paddingy);
480 /* draw a color icon */
481 if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
482 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
483 self->entry->data.normal.icon)
487 XMoveResizeWindow(obt_display, self->icon,
488 PADDING, frame->item_margin.top,
489 ITEM_HEIGHT - frame->item_margin.top
490 - frame->item_margin.bottom,
491 ITEM_HEIGHT - frame->item_margin.top
492 - frame->item_margin.bottom);
494 clear = ob_rr_theme->a_clear_tex;
495 RrAppearanceClearTextures(clear);
496 clear->texture[0].type = RR_TEXTURE_IMAGE;
497 clear->texture[0].data.image.image =
498 self->entry->data.normal.icon;
499 clear->texture[0].data.image.alpha =
500 self->entry->data.normal.icon_alpha;
501 clear->surface.parent = item_a;
502 clear->surface.parentx = PADDING;
503 clear->surface.parenty = frame->item_margin.top;
504 RrPaint(clear, self->icon,
505 ITEM_HEIGHT - frame->item_margin.top
506 - frame->item_margin.bottom,
507 ITEM_HEIGHT - frame->item_margin.top
508 - frame->item_margin.bottom);
509 XMapWindow(obt_display, self->icon);
510 /* draw a boring monochrome bitmap */
511 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
512 self->entry->data.normal.mask)
517 XMoveResizeWindow(obt_display, self->icon,
518 PADDING, frame->item_margin.top,
519 ITEM_HEIGHT - frame->item_margin.top
520 - frame->item_margin.bottom,
521 ITEM_HEIGHT - frame->item_margin.top
522 - frame->item_margin.bottom);
524 clear = ob_rr_theme->a_clear_tex;
525 RrAppearanceClearTextures(clear);
526 clear->texture[0].type = RR_TEXTURE_MASK;
527 clear->texture[0].data.mask.mask =
528 self->entry->data.normal.mask;
530 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
531 !self->entry->data.normal.enabled ?
533 (self == self->frame->selected ?
534 self->entry->data.normal.mask_disabled_selected_color :
535 self->entry->data.normal.mask_disabled_color) :
537 (self == self->frame->selected ?
538 self->entry->data.normal.mask_selected_color :
539 self->entry->data.normal.mask_normal_color));
540 clear->texture[0].data.mask.color = c;
542 clear->surface.parent = item_a;
543 clear->surface.parentx = PADDING;
544 clear->surface.parenty = frame->item_margin.top;
545 RrPaint(clear, self->icon,
546 ITEM_HEIGHT - frame->item_margin.top
547 - frame->item_margin.bottom,
548 ITEM_HEIGHT - frame->item_margin.top
549 - frame->item_margin.bottom);
550 XMapWindow(obt_display, self->icon);
551 /* don't draw any icon at all */
553 XUnmapWindow(obt_display, self->icon);
555 /* draw the bullet icon indicating a submenu */
556 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
557 RrAppearance *bullet_a;
558 XMoveResizeWindow(obt_display, self->bullet,
559 self->frame->text_x + self->frame->text_w -
560 ITEM_HEIGHT + PADDING, PADDING,
561 ITEM_HEIGHT - 2*PADDING,
562 ITEM_HEIGHT - 2*PADDING);
563 bullet_a = (self == self->frame->selected ?
564 ob_rr_theme->a_menu_bullet_selected :
565 ob_rr_theme->a_menu_bullet_normal);
566 bullet_a->surface.parent = item_a;
567 bullet_a->surface.parentx =
568 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
569 bullet_a->surface.parenty = PADDING;
570 RrPaint(bullet_a, self->bullet,
571 ITEM_HEIGHT - 2*PADDING,
572 ITEM_HEIGHT - 2*PADDING);
573 XMapWindow(obt_display, self->bullet);
575 XUnmapWindow(obt_display, self->bullet);
580 /*! this code is taken from the menu_frame_render. if that changes, this won't
582 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
583 gboolean first_entry,
592 t = self->entry->type;
594 /* this is the More... entry, it's NORMAL type */
595 t = OB_MENU_ENTRY_TYPE_NORMAL;
598 case OB_MENU_ENTRY_TYPE_NORMAL:
599 case OB_MENU_ENTRY_TYPE_SUBMENU:
600 h += ob_rr_theme->menu_font_height;
602 case OB_MENU_ENTRY_TYPE_SEPARATOR:
603 if (self->entry->data.separator.label != NULL) {
604 h += ob_rr_theme->menu_title_height +
605 (ob_rr_theme->mbwidth - PADDING) * 2;
607 /* if the first entry is a labeled separator, then make its border
608 overlap with the menu's outside border */
610 h -= ob_rr_theme->mbwidth;
611 /* if the last entry is a labeled separator, then make its border
612 overlap with the menu's outside border */
614 h -= ob_rr_theme->mbwidth;
616 h += ob_rr_theme->menu_sep_width +
617 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
625 void menu_frame_render(ObMenuFrame *self)
628 gint tw, th; /* temps */
630 gboolean has_icon = FALSE;
634 /* find text dimensions */
636 STRUT_SET(self->item_margin, 0, 0, 0, 0);
641 e = self->entries->data;
642 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
643 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
648 RrMargins(ob_rr_theme->a_menu_normal, &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_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));
660 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
661 STRUT_SET(self->item_margin,
662 MAX(self->item_margin.left, l),
663 MAX(self->item_margin.top, t),
664 MAX(self->item_margin.right, r),
665 MAX(self->item_margin.bottom, b));
666 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
667 STRUT_SET(self->item_margin,
668 MAX(self->item_margin.left, l),
669 MAX(self->item_margin.top, t),
670 MAX(self->item_margin.right, r),
671 MAX(self->item_margin.bottom, b));
674 /* render the entries */
676 for (it = self->entries; it; it = g_list_next(it)) {
677 RrAppearance *text_a;
680 /* if the first entry is a labeled separator, then make its border
681 overlap with the menu's outside border */
682 if (it == self->entries &&
683 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
684 e->entry->data.separator.label)
686 h -= ob_rr_theme->mbwidth;
689 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
690 e->entry->data.separator.label)
692 e->border = ob_rr_theme->mbwidth;
695 RECT_SET_POINT(e->area, 0, h+e->border);
696 XMoveWindow(obt_display, e->window,
697 e->area.x-e->border, e->area.y-e->border);
698 XSetWindowBorderWidth(obt_display, e->window, e->border);
699 XSetWindowBorder(obt_display, e->window,
700 RrColorPixel(ob_rr_theme->menu_border_color));
702 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
703 !e->entry->data.normal.enabled ?
705 (e == self->selected ?
706 ob_rr_theme->a_menu_text_disabled_selected :
707 ob_rr_theme->a_menu_text_disabled) :
709 (e == self->selected ?
710 ob_rr_theme->a_menu_text_selected :
711 ob_rr_theme->a_menu_text_normal));
712 switch (e->entry->type) {
713 case OB_MENU_ENTRY_TYPE_NORMAL:
714 text_a->texture[0].data.text.string = e->entry->data.normal.label;
715 tw = RrMinWidth(text_a);
716 tw = MIN(tw, MAX_MENU_WIDTH);
717 th = ob_rr_theme->menu_font_height;
719 if (e->entry->data.normal.icon ||
720 e->entry->data.normal.mask)
723 case OB_MENU_ENTRY_TYPE_SUBMENU:
724 sub = e->entry->data.submenu.submenu;
725 text_a->texture[0].data.text.string = sub ? sub->title : "";
726 tw = RrMinWidth(text_a);
727 tw = MIN(tw, MAX_MENU_WIDTH);
728 th = ob_rr_theme->menu_font_height;
730 #warning this is completely bogus, can't use .normal in a submenu, even if it works because .submenu is shorter.
731 if (e->entry->data.normal.icon ||
732 e->entry->data.normal.mask)
735 tw += ITEM_HEIGHT - PADDING;
737 case OB_MENU_ENTRY_TYPE_SEPARATOR:
738 if (e->entry->data.separator.label != NULL) {
739 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
740 e->entry->data.separator.label;
741 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
742 2*ob_rr_theme->paddingx;
743 tw = MIN(tw, MAX_MENU_WIDTH);
744 th = ob_rr_theme->menu_title_height +
745 (ob_rr_theme->mbwidth - PADDING) *2;
748 th = ob_rr_theme->menu_sep_width +
749 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
761 /* if the last entry is a labeled separator, then make its border
762 overlap with the menu's outside border */
763 it = g_list_last(self->entries);
764 e = it ? it->data : NULL;
765 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
766 e->entry->data.separator.label)
768 h -= ob_rr_theme->mbwidth;
771 self->text_x = PADDING;
776 w += ITEM_HEIGHT + PADDING;
777 self->text_x += ITEM_HEIGHT + PADDING;
784 XResizeWindow(obt_display, self->window, w, h);
788 RrPaint(self->a_items, self->window, w, h);
790 for (it = self->entries; it; it = g_list_next(it))
791 menu_entry_frame_render(it->data);
793 w += ob_rr_theme->mbwidth * 2;
794 h += ob_rr_theme->mbwidth * 2;
796 RECT_SET_SIZE(self->area, w, h);
801 static void menu_frame_update(ObMenuFrame *self)
807 menu_pipe_execute(self->menu);
808 menu_find_submenus(self->menu);
810 self->selected = NULL;
812 /* start at show_from */
813 mit = g_list_nth(self->menu->entries, self->show_from);
815 /* go through the menu's and frame's entries and connect the frame entries
816 to the menu entries */
817 for (fit = self->entries; mit && fit;
818 mit = g_list_next(mit), fit = g_list_next(fit))
820 ObMenuEntryFrame *f = fit->data;
821 f->entry = mit->data;
824 /* if there are more menu entries than in the frame, add them */
826 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
827 self->entries = g_list_append(self->entries, e);
828 mit = g_list_next(mit);
831 /* if there are more frame entries than menu entries then get rid of
834 GList *n = g_list_next(fit);
835 menu_entry_frame_free(fit->data);
836 self->entries = g_list_delete_link(self->entries, fit);
840 /* * make the menu fit on the screen */
842 /* calculate the height of the menu */
844 for (fit = self->entries; fit; fit = g_list_next(fit))
845 h += menu_entry_frame_get_height(fit->data,
846 fit == self->entries,
847 g_list_next(fit) == NULL);
848 /* add the border at the top and bottom */
849 h += ob_rr_theme->mbwidth * 2;
851 a = screen_physical_area_monitor(self->monitor);
855 gboolean last_entry = TRUE;
857 /* take the height of our More... entry into account */
858 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
860 /* start at the end of the entries */
861 flast = g_list_last(self->entries);
863 /* pull out all the entries from the frame that don't
864 fit on the screen, leaving at least 1 though */
865 while (h > a->height && g_list_previous(flast) != NULL) {
866 /* update the height, without this entry */
867 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
869 /* destroy the entry we're not displaying */
871 flast = g_list_previous(flast);
872 menu_entry_frame_free(tmp->data);
873 self->entries = g_list_delete_link(self->entries, tmp);
875 /* only the first one that we see is the last entry in the menu */
880 ObMenuEntry *more_entry;
881 ObMenuEntryFrame *more_frame;
882 /* make the More... menu entry frame which will display in this
884 if self->menu->more_menu is NULL that means that this is already
885 More... menu, so just use ourself.
887 more_entry = menu_get_more((self->menu->more_menu ?
888 self->menu->more_menu :
890 /* continue where we left off */
892 g_list_length(self->entries));
893 more_frame = menu_entry_frame_new(more_entry, self);
894 /* make it get deleted when the menu frame goes away */
895 menu_entry_unref(more_entry);
897 /* add our More... entry to the frame */
898 self->entries = g_list_append(self->entries, more_frame);
904 menu_frame_render(self);
907 static gboolean menu_frame_is_visible(ObMenuFrame *self)
909 return !!(g_list_find(menu_frame_visible, self));
912 static gboolean menu_frame_show(ObMenuFrame *self)
916 /* determine if the underlying menu is already visible */
917 for (it = menu_frame_visible; it; it = g_list_next(it)) {
918 ObMenuFrame *f = it->data;
919 if (f->menu == self->menu)
923 if (self->menu->update_func)
924 if (!self->menu->update_func(self, self->menu->data))
928 if (menu_frame_visible == NULL) {
929 /* no menus shown yet */
931 /* grab the pointer in such a way as to pass through "owner events"
932 so that we can get enter/leave notifies in the menu. */
933 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
935 if (!grab_keyboard()) {
941 menu_frame_update(self);
943 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
945 if (self->menu->show_func)
946 self->menu->show_func(self, self->menu->data);
951 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
957 if (menu_frame_is_visible(self))
959 if (!menu_frame_show(self))
962 /* find the monitor the menu is on */
963 for (i = 0; i < screen_num_monitors; ++i) {
964 Rect *a = screen_physical_area_monitor(i);
965 gboolean contains = RECT_CONTAINS(*a, x, y);
973 if (self->menu->place_func)
974 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
976 menu_frame_place_topmenu(self, &x, &y);
978 menu_frame_move(self, x, y);
980 XMapWindow(obt_display, self->window);
982 if (screen_pointer_pos(&px, &py)) {
983 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
984 if (e && e->frame == self)
991 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
992 ObMenuEntryFrame *parent_entry)
997 if (menu_frame_is_visible(self))
1000 self->monitor = parent->monitor;
1001 self->parent = parent;
1002 self->parent_entry = parent_entry;
1004 /* set up parent's child to be us */
1006 menu_frame_hide(parent->child);
1007 parent->child = self;
1009 if (!menu_frame_show(self))
1012 menu_frame_place_submenu(self, &x, &y);
1013 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1016 /*try the other side */
1017 self->direction_right = !self->direction_right;
1018 menu_frame_place_submenu(self, &x, &y);
1019 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1021 menu_frame_move(self, x + dx, y + dy);
1023 XMapWindow(obt_display, self->window);
1025 if (screen_pointer_pos(&px, &py)) {
1026 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1027 if (e && e->frame == self)
1034 static void menu_frame_hide(ObMenuFrame *self)
1036 GList *it = g_list_find(menu_frame_visible, self);
1041 if (self->menu->hide_func)
1042 self->menu->hide_func(self, self->menu->data);
1045 menu_frame_hide(self->child);
1048 self->parent->child = NULL;
1049 self->parent = NULL;
1050 self->parent_entry = NULL;
1052 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1054 if (menu_frame_visible == NULL) {
1055 /* last menu shown */
1060 XUnmapWindow(obt_display, self->window);
1062 menu_frame_free(self);
1065 void menu_frame_hide_all(void)
1069 if (config_submenu_show_delay) {
1070 /* remove any submenu open requests */
1071 obt_main_loop_timeout_remove(ob_main_loop,
1072 menu_entry_frame_submenu_timeout);
1074 if ((it = g_list_last(menu_frame_visible)))
1075 menu_frame_hide(it->data);
1078 void menu_frame_hide_all_client(ObClient *client)
1080 GList *it = g_list_last(menu_frame_visible);
1082 ObMenuFrame *f = it->data;
1083 if (f->client == client) {
1084 if (config_submenu_show_delay) {
1085 /* remove any submenu open requests */
1086 obt_main_loop_timeout_remove(ob_main_loop,
1087 menu_entry_frame_submenu_timeout);
1094 ObMenuFrame* menu_frame_under(gint x, gint y)
1096 ObMenuFrame *ret = NULL;
1099 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1100 ObMenuFrame *f = it->data;
1102 if (RECT_CONTAINS(f->area, x, y)) {
1110 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1113 ObMenuEntryFrame *ret = NULL;
1116 if ((frame = menu_frame_under(x, y))) {
1117 x -= ob_rr_theme->mbwidth + frame->area.x;
1118 y -= ob_rr_theme->mbwidth + frame->area.y;
1120 for (it = frame->entries; it; it = g_list_next(it)) {
1121 ObMenuEntryFrame *e = it->data;
1123 if (RECT_CONTAINS(e->area, x, y)) {
1132 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1134 g_assert(menu_frame_visible);
1135 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1139 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1142 ObMenuEntryFrame *old = self->selected;
1143 ObMenuFrame *oldchild = self->child;
1145 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1148 if (old == entry) return;
1150 if (config_submenu_show_delay) {
1151 /* remove any submenu open requests */
1152 obt_main_loop_timeout_remove(ob_main_loop,
1153 menu_entry_frame_submenu_timeout);
1156 self->selected = entry;
1159 menu_entry_frame_render(old);
1161 menu_frame_hide(oldchild);
1163 if (self->selected) {
1164 menu_entry_frame_render(self->selected);
1166 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1167 if (config_submenu_show_delay && !immediate) {
1168 /* initiate a new submenu open request */
1169 obt_main_loop_timeout_add(ob_main_loop,
1170 config_submenu_show_delay * 1000,
1171 menu_entry_frame_submenu_timeout,
1172 self->selected, g_direct_equal,
1175 menu_entry_frame_show_submenu(self->selected);
1181 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1185 if (!self->entry->data.submenu.submenu) return;
1187 f = menu_frame_new(self->entry->data.submenu.submenu,
1188 self->entry->data.submenu.show_from,
1189 self->frame->client);
1190 /* pass our direction on to our child */
1191 f->direction_right = self->frame->direction_right;
1193 menu_frame_show_submenu(f, self->frame, self);
1196 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1198 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1199 self->entry->data.normal.enabled)
1201 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1203 ObMenuEntry *entry = self->entry;
1204 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1205 gpointer data = self->frame->menu->data;
1206 GSList *acts = self->entry->data.normal.actions;
1207 ObClient *client = self->frame->client;
1208 ObMenuFrame *frame = self->frame;
1210 /* release grabs before executing the shit */
1211 if (!(state & ControlMask)) {
1212 menu_frame_hide_all();
1217 func(entry, frame, client, state, data);
1219 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1220 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1224 static void menu_frame_select_next_previous(ObMenuFrame *self, gint direction)
1226 GList *it = NULL, *start;
1228 if (self->entries) {
1229 start = it = g_list_find(self->entries, self->selected);
1231 ObMenuEntryFrame *e;
1233 if (direction) /* 1 == next */
1234 it = it ? g_list_next(it) : self->entries;
1236 it = it ? g_list_previous(it) : g_list_last(self->entries);
1242 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1244 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1249 menu_frame_select(self, it ? it->data : NULL, TRUE);
1252 #warning XXX TODO BLAG ETC remove these wrappers and change callers
1253 void menu_frame_select_previous(ObMenuFrame *self)
1255 menu_frame_select_next_previous(self, 0);
1258 void menu_frame_select_next(ObMenuFrame *self)
1260 menu_frame_select_next_previous(self, 1);