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) {
211 *y -= self->area.height / 2;
213 /* try to the right of the cursor */
214 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
215 self->direction_right = TRUE;
217 /* try to the left of the cursor */
218 myx = *x - self->area.width;
219 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
220 self->direction_right = FALSE;
223 /* if didnt fit on either side so just use what it says */
225 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
226 self->direction_right = TRUE;
236 /* try to the bottom right of the cursor */
237 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
238 self->direction_right = TRUE;
239 if (dx != 0 || dy != 0) {
240 /* try to the bottom left of the cursor */
241 myx = *x - self->area.width;
243 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
244 self->direction_right = FALSE;
246 if (dx != 0 || dy != 0) {
247 /* try to the top right of the cursor */
249 myy = *y - self->area.height;
250 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
251 self->direction_right = TRUE;
253 if (dx != 0 || dy != 0) {
254 /* try to the top left of the cursor */
255 myx = *x - self->area.width;
256 myy = *y - self->area.height;
257 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
258 self->direction_right = FALSE;
260 if (dx != 0 || dy != 0) {
261 /* if didnt fit on either side so just use what it says */
264 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
265 self->direction_right = TRUE;
272 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
274 gint overlapx, overlapy;
277 overlapx = ob_rr_theme->menu_overlap_x;
278 overlapy = ob_rr_theme->menu_overlap_y;
279 bwidth = ob_rr_theme->mbwidth;
281 if (self->direction_right)
282 *x = self->parent->area.x + self->parent->area.width -
285 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
287 *y = self->parent->area.y + self->parent_entry->area.y;
288 if (config_menu_middle)
289 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
294 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
302 a = screen_physical_area_monitor(self->monitor);
304 half = g_list_length(self->entries) / 2;
305 pos = g_list_index(self->entries, self->selected);
307 /* if in the bottom half then check this stuff first, will keep the bottom
308 edge of the menu visible */
310 *dx = MAX(*dx, a->x - x);
311 *dy = MAX(*dy, a->y - y);
313 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
314 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
315 /* if in the top half then check this stuff last, will keep the top
316 edge of the menu visible */
318 *dx = MAX(*dx, a->x - x);
319 *dy = MAX(*dy, a->y - y);
325 static void menu_entry_frame_render(ObMenuEntryFrame *self)
327 RrAppearance *item_a, *text_a;
330 ObMenuFrame *frame = self->frame;
332 switch (self->entry->type) {
333 case OB_MENU_ENTRY_TYPE_NORMAL:
334 case OB_MENU_ENTRY_TYPE_SUBMENU:
335 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
336 !self->entry->data.normal.enabled ?
338 (self == self->frame->selected ?
339 ob_rr_theme->a_menu_disabled_selected :
340 ob_rr_theme->a_menu_disabled) :
342 (self == self->frame->selected ?
343 ob_rr_theme->a_menu_selected :
344 ob_rr_theme->a_menu_normal));
347 case OB_MENU_ENTRY_TYPE_SEPARATOR:
348 if (self->entry->data.separator.label) {
349 item_a = ob_rr_theme->a_menu_title;
350 th = ob_rr_theme->menu_title_height;
352 item_a = ob_rr_theme->a_menu_normal;
353 th = ob_rr_theme->menu_sep_width +
354 2*ob_rr_theme->menu_sep_paddingy;
358 g_assert_not_reached();
361 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
362 XResizeWindow(obt_display, self->window,
363 self->area.width, self->area.height);
364 item_a->surface.parent = self->frame->a_items;
365 item_a->surface.parentx = self->area.x;
366 item_a->surface.parenty = self->area.y;
367 RrPaint(item_a, self->window, self->area.width, self->area.height);
369 switch (self->entry->type) {
370 case OB_MENU_ENTRY_TYPE_NORMAL:
371 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
372 !self->entry->data.normal.enabled ?
374 (self == self->frame->selected ?
375 ob_rr_theme->a_menu_text_disabled_selected :
376 ob_rr_theme->a_menu_text_disabled) :
378 (self == self->frame->selected ?
379 ob_rr_theme->a_menu_text_selected :
380 ob_rr_theme->a_menu_text_normal));
381 text_a->texture[0].data.text.string = self->entry->data.normal.label;
382 if (self->entry->data.normal.shortcut &&
383 (self->frame->menu->show_all_shortcuts ||
384 self->entry->data.normal.shortcut_always_show ||
385 self->entry->data.normal.shortcut_position > 0))
387 text_a->texture[0].data.text.shortcut = TRUE;
388 text_a->texture[0].data.text.shortcut_pos =
389 self->entry->data.normal.shortcut_position;
391 text_a->texture[0].data.text.shortcut = FALSE;
393 case OB_MENU_ENTRY_TYPE_SUBMENU:
394 text_a = (self == self->frame->selected ?
395 ob_rr_theme->a_menu_text_selected :
396 ob_rr_theme->a_menu_text_normal);
397 sub = self->entry->data.submenu.submenu;
398 text_a->texture[0].data.text.string = sub ? sub->title : "";
399 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
400 sub->shortcut_always_show ||
401 sub->shortcut_position > 0))
403 text_a->texture[0].data.text.shortcut = TRUE;
404 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
406 text_a->texture[0].data.text.shortcut = FALSE;
408 case OB_MENU_ENTRY_TYPE_SEPARATOR:
409 if (self->entry->data.separator.label != NULL) {
410 text_a = ob_rr_theme->a_menu_text_title;
411 text_a->texture[0].data.text.string =
412 self->entry->data.separator.label;
415 text_a = ob_rr_theme->a_menu_text_normal;
418 g_assert_not_reached();
421 switch (self->entry->type) {
422 case OB_MENU_ENTRY_TYPE_NORMAL:
423 XMoveResizeWindow(obt_display, self->text,
424 self->frame->text_x, PADDING,
426 ITEM_HEIGHT - 2*PADDING);
427 text_a->surface.parent = item_a;
428 text_a->surface.parentx = self->frame->text_x;
429 text_a->surface.parenty = PADDING;
430 RrPaint(text_a, self->text, self->frame->text_w,
431 ITEM_HEIGHT - 2*PADDING);
433 case OB_MENU_ENTRY_TYPE_SUBMENU:
434 XMoveResizeWindow(obt_display, self->text,
435 self->frame->text_x, PADDING,
436 self->frame->text_w - ITEM_HEIGHT,
437 ITEM_HEIGHT - 2*PADDING);
438 text_a->surface.parent = item_a;
439 text_a->surface.parentx = self->frame->text_x;
440 text_a->surface.parenty = PADDING;
441 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
442 ITEM_HEIGHT - 2*PADDING);
444 case OB_MENU_ENTRY_TYPE_SEPARATOR:
445 if (self->entry->data.separator.label != NULL) {
446 /* labeled separator */
447 XMoveResizeWindow(obt_display, self->text,
448 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
449 self->area.width - 2*ob_rr_theme->paddingx,
450 ob_rr_theme->menu_title_height -
451 2*ob_rr_theme->paddingy);
452 text_a->surface.parent = item_a;
453 text_a->surface.parentx = ob_rr_theme->paddingx;
454 text_a->surface.parenty = ob_rr_theme->paddingy;
455 RrPaint(text_a, self->text,
456 self->area.width - 2*ob_rr_theme->paddingx,
457 ob_rr_theme->menu_title_height -
458 2*ob_rr_theme->paddingy);
462 /* unlabeled separator */
463 XMoveResizeWindow(obt_display, self->text, 0, 0,
465 ob_rr_theme->menu_sep_width +
466 2*ob_rr_theme->menu_sep_paddingy);
468 a_sep->surface.parent = item_a;
469 a_sep->surface.parentx = 0;
470 a_sep->surface.parenty = 0;
471 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
472 a_sep->texture[i].data.lineart.x1 =
473 ob_rr_theme->menu_sep_paddingx;
474 a_sep->texture[i].data.lineart.y1 =
475 ob_rr_theme->menu_sep_paddingy + i;
476 a_sep->texture[i].data.lineart.x2 =
477 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
478 a_sep->texture[i].data.lineart.y2 =
479 ob_rr_theme->menu_sep_paddingy + i;
482 RrPaint(a_sep, self->text, self->area.width,
483 ob_rr_theme->menu_sep_width +
484 2*ob_rr_theme->menu_sep_paddingy);
488 g_assert_not_reached();
491 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
492 self->entry->data.normal.icon)
496 XMoveResizeWindow(obt_display, self->icon,
497 PADDING, frame->item_margin.top,
498 ITEM_HEIGHT - frame->item_margin.top
499 - frame->item_margin.bottom,
500 ITEM_HEIGHT - frame->item_margin.top
501 - frame->item_margin.bottom);
503 clear = ob_rr_theme->a_clear_tex;
504 RrAppearanceClearTextures(clear);
505 clear->texture[0].type = RR_TEXTURE_IMAGE;
506 clear->texture[0].data.image.image =
507 self->entry->data.normal.icon;
508 clear->texture[0].data.image.alpha =
509 self->entry->data.normal.icon_alpha;
510 clear->surface.parent = item_a;
511 clear->surface.parentx = PADDING;
512 clear->surface.parenty = frame->item_margin.top;
513 RrPaint(clear, self->icon,
514 ITEM_HEIGHT - frame->item_margin.top
515 - frame->item_margin.bottom,
516 ITEM_HEIGHT - frame->item_margin.top
517 - frame->item_margin.bottom);
518 XMapWindow(obt_display, self->icon);
519 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
520 self->entry->data.normal.mask)
525 XMoveResizeWindow(obt_display, self->icon,
526 PADDING, frame->item_margin.top,
527 ITEM_HEIGHT - frame->item_margin.top
528 - frame->item_margin.bottom,
529 ITEM_HEIGHT - frame->item_margin.top
530 - frame->item_margin.bottom);
532 clear = ob_rr_theme->a_clear_tex;
533 RrAppearanceClearTextures(clear);
534 clear->texture[0].type = RR_TEXTURE_MASK;
535 clear->texture[0].data.mask.mask =
536 self->entry->data.normal.mask;
538 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
539 !self->entry->data.normal.enabled ?
541 (self == self->frame->selected ?
542 self->entry->data.normal.mask_disabled_selected_color :
543 self->entry->data.normal.mask_disabled_color) :
545 (self == self->frame->selected ?
546 self->entry->data.normal.mask_selected_color :
547 self->entry->data.normal.mask_normal_color));
548 clear->texture[0].data.mask.color = c;
550 clear->surface.parent = item_a;
551 clear->surface.parentx = PADDING;
552 clear->surface.parenty = frame->item_margin.top;
553 RrPaint(clear, self->icon,
554 ITEM_HEIGHT - frame->item_margin.top
555 - frame->item_margin.bottom,
556 ITEM_HEIGHT - frame->item_margin.top
557 - frame->item_margin.bottom);
558 XMapWindow(obt_display, self->icon);
560 XUnmapWindow(obt_display, self->icon);
562 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
563 RrAppearance *bullet_a;
564 XMoveResizeWindow(obt_display, self->bullet,
565 self->frame->text_x + self->frame->text_w -
566 ITEM_HEIGHT + PADDING, PADDING,
567 ITEM_HEIGHT - 2*PADDING,
568 ITEM_HEIGHT - 2*PADDING);
569 bullet_a = (self == self->frame->selected ?
570 ob_rr_theme->a_menu_bullet_selected :
571 ob_rr_theme->a_menu_bullet_normal);
572 bullet_a->surface.parent = item_a;
573 bullet_a->surface.parentx =
574 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
575 bullet_a->surface.parenty = PADDING;
576 RrPaint(bullet_a, self->bullet,
577 ITEM_HEIGHT - 2*PADDING,
578 ITEM_HEIGHT - 2*PADDING);
579 XMapWindow(obt_display, self->bullet);
581 XUnmapWindow(obt_display, self->bullet);
586 /*! this code is taken from the menu_frame_render. if that changes, this won't
588 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
589 gboolean first_entry,
598 t = self->entry->type;
600 /* this is the More... entry, it's NORMAL type */
601 t = OB_MENU_ENTRY_TYPE_NORMAL;
604 case OB_MENU_ENTRY_TYPE_NORMAL:
605 case OB_MENU_ENTRY_TYPE_SUBMENU:
606 h += ob_rr_theme->menu_font_height;
608 case OB_MENU_ENTRY_TYPE_SEPARATOR:
609 if (self->entry->data.separator.label != NULL) {
610 h += ob_rr_theme->menu_title_height +
611 (ob_rr_theme->mbwidth - PADDING) * 2;
613 /* if the first entry is a labeled separator, then make its border
614 overlap with the menu's outside border */
616 h -= ob_rr_theme->mbwidth;
617 /* if the last entry is a labeled separator, then make its border
618 overlap with the menu's outside border */
620 h -= ob_rr_theme->mbwidth;
622 h += ob_rr_theme->menu_sep_width +
623 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
631 void menu_frame_render(ObMenuFrame *self)
634 gint tw, th; /* temps */
636 gboolean has_icon = FALSE;
640 /* find text dimensions */
642 STRUT_SET(self->item_margin, 0, 0, 0, 0);
647 e = self->entries->data;
648 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
649 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
654 RrMargins(ob_rr_theme->a_menu_normal, &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_selected, &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, &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));
672 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
673 STRUT_SET(self->item_margin,
674 MAX(self->item_margin.left, l),
675 MAX(self->item_margin.top, t),
676 MAX(self->item_margin.right, r),
677 MAX(self->item_margin.bottom, b));
680 /* render the entries */
682 for (it = self->entries; it; it = g_list_next(it)) {
683 RrAppearance *text_a;
686 /* if the first entry is a labeled separator, then make its border
687 overlap with the menu's outside border */
688 if (it == self->entries &&
689 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
690 e->entry->data.separator.label)
692 h -= ob_rr_theme->mbwidth;
695 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
696 e->entry->data.separator.label)
698 e->border = ob_rr_theme->mbwidth;
701 RECT_SET_POINT(e->area, 0, h+e->border);
702 XMoveWindow(obt_display, e->window,
703 e->area.x-e->border, e->area.y-e->border);
704 XSetWindowBorderWidth(obt_display, e->window, e->border);
705 XSetWindowBorder(obt_display, e->window,
706 RrColorPixel(ob_rr_theme->menu_border_color));
708 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
709 !e->entry->data.normal.enabled ?
711 (e == self->selected ?
712 ob_rr_theme->a_menu_text_disabled_selected :
713 ob_rr_theme->a_menu_text_disabled) :
715 (e == self->selected ?
716 ob_rr_theme->a_menu_text_selected :
717 ob_rr_theme->a_menu_text_normal));
718 switch (e->entry->type) {
719 case OB_MENU_ENTRY_TYPE_NORMAL:
720 text_a->texture[0].data.text.string = e->entry->data.normal.label;
721 tw = RrMinWidth(text_a);
722 tw = MIN(tw, MAX_MENU_WIDTH);
723 th = ob_rr_theme->menu_font_height;
725 if (e->entry->data.normal.icon ||
726 e->entry->data.normal.mask)
729 case OB_MENU_ENTRY_TYPE_SUBMENU:
730 sub = e->entry->data.submenu.submenu;
731 text_a->texture[0].data.text.string = sub ? sub->title : "";
732 tw = RrMinWidth(text_a);
733 tw = MIN(tw, MAX_MENU_WIDTH);
734 th = ob_rr_theme->menu_font_height;
736 if (e->entry->data.normal.icon ||
737 e->entry->data.normal.mask)
740 tw += ITEM_HEIGHT - PADDING;
742 case OB_MENU_ENTRY_TYPE_SEPARATOR:
743 if (e->entry->data.separator.label != NULL) {
744 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
745 e->entry->data.separator.label;
746 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
747 2*ob_rr_theme->paddingx;
748 tw = MIN(tw, MAX_MENU_WIDTH);
749 th = ob_rr_theme->menu_title_height +
750 (ob_rr_theme->mbwidth - PADDING) *2;
753 th = ob_rr_theme->menu_sep_width +
754 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
758 g_assert_not_reached();
766 /* if the last entry is a labeled separator, then make its border
767 overlap with the menu's outside border */
768 it = g_list_last(self->entries);
769 e = it ? it->data : NULL;
770 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
771 e->entry->data.separator.label)
773 h -= ob_rr_theme->mbwidth;
776 self->text_x = PADDING;
781 w += ITEM_HEIGHT + PADDING;
782 self->text_x += ITEM_HEIGHT + PADDING;
789 XResizeWindow(obt_display, self->window, w, h);
793 RrPaint(self->a_items, self->window, w, h);
795 for (it = self->entries; it; it = g_list_next(it))
796 menu_entry_frame_render(it->data);
798 w += ob_rr_theme->mbwidth * 2;
799 h += ob_rr_theme->mbwidth * 2;
801 RECT_SET_SIZE(self->area, w, h);
806 static void menu_frame_update(ObMenuFrame *self)
812 menu_pipe_execute(self->menu);
813 menu_find_submenus(self->menu);
815 self->selected = NULL;
817 /* start at show_from */
818 mit = g_list_nth(self->menu->entries, self->show_from);
820 /* go through the menu's and frame's entries and connect the frame entries
821 to the menu entries */
822 for (fit = self->entries; mit && fit;
823 mit = g_list_next(mit), fit = g_list_next(fit))
825 ObMenuEntryFrame *f = fit->data;
826 f->entry = mit->data;
829 /* if there are more menu entries than in the frame, add them */
831 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
832 self->entries = g_list_append(self->entries, e);
833 mit = g_list_next(mit);
836 /* if there are more frame entries than menu entries then get rid of
839 GList *n = g_list_next(fit);
840 menu_entry_frame_free(fit->data);
841 self->entries = g_list_delete_link(self->entries, fit);
845 /* * make the menu fit on the screen */
847 /* calculate the height of the menu */
849 for (fit = self->entries; fit; fit = g_list_next(fit))
850 h += menu_entry_frame_get_height(fit->data,
851 fit == self->entries,
852 g_list_next(fit) == NULL);
853 /* add the border at the top and bottom */
854 h += ob_rr_theme->mbwidth * 2;
856 a = screen_physical_area_monitor(self->monitor);
860 gboolean last_entry = TRUE;
862 /* take the height of our More... entry into account */
863 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
865 /* start at the end of the entries */
866 flast = g_list_last(self->entries);
868 /* pull out all the entries from the frame that don't
869 fit on the screen, leaving at least 1 though */
870 while (h > a->height && g_list_previous(flast) != NULL) {
871 /* update the height, without this entry */
872 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
874 /* destroy the entry we're not displaying */
876 flast = g_list_previous(flast);
877 menu_entry_frame_free(tmp->data);
878 self->entries = g_list_delete_link(self->entries, tmp);
880 /* only the first one that we see is the last entry in the menu */
885 ObMenuEntry *more_entry;
886 ObMenuEntryFrame *more_frame;
887 /* make the More... menu entry frame which will display in this
889 if self->menu->more_menu is NULL that means that this is already
890 More... menu, so just use ourself.
892 more_entry = menu_get_more((self->menu->more_menu ?
893 self->menu->more_menu :
895 /* continue where we left off */
897 g_list_length(self->entries));
898 more_frame = menu_entry_frame_new(more_entry, self);
899 /* make it get deleted when the menu frame goes away */
900 menu_entry_unref(more_entry);
902 /* add our More... entry to the frame */
903 self->entries = g_list_append(self->entries, more_frame);
909 menu_frame_render(self);
912 static gboolean menu_frame_is_visible(ObMenuFrame *self)
914 return !!(g_list_find(menu_frame_visible, self));
917 static gboolean menu_frame_show(ObMenuFrame *self)
921 /* determine if the underlying menu is already visible */
922 for (it = menu_frame_visible; it; it = g_list_next(it)) {
923 ObMenuFrame *f = it->data;
924 if (f->menu == self->menu)
928 if (self->menu->update_func)
929 if (!self->menu->update_func(self, self->menu->data))
933 if (menu_frame_visible == NULL) {
934 /* no menus shown yet */
936 /* grab the pointer in such a way as to pass through "owner events"
937 so that we can get enter/leave notifies in the menu. */
938 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
940 if (!grab_keyboard()) {
946 menu_frame_update(self);
948 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
950 if (self->menu->show_func)
951 self->menu->show_func(self, self->menu->data);
956 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
962 if (menu_frame_is_visible(self))
964 if (!menu_frame_show(self))
967 /* find the monitor the menu is on */
968 for (i = 0; i < screen_num_monitors; ++i) {
969 Rect *a = screen_physical_area_monitor(i);
970 gboolean contains = RECT_CONTAINS(*a, x, y);
978 if (self->menu->place_func)
979 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
981 menu_frame_place_topmenu(self, &x, &y);
983 menu_frame_move(self, x, y);
985 XMapWindow(obt_display, self->window);
987 if (screen_pointer_pos(&px, &py)) {
988 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
989 if (e && e->frame == self)
996 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
997 ObMenuEntryFrame *parent_entry)
1002 if (menu_frame_is_visible(self))
1005 self->monitor = parent->monitor;
1006 self->parent = parent;
1007 self->parent_entry = parent_entry;
1009 /* set up parent's child to be us */
1011 menu_frame_hide(parent->child);
1012 parent->child = self;
1014 if (!menu_frame_show(self))
1017 menu_frame_place_submenu(self, &x, &y);
1018 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1021 /*try the other side */
1022 self->direction_right = !self->direction_right;
1023 menu_frame_place_submenu(self, &x, &y);
1024 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1026 menu_frame_move(self, x + dx, y + dy);
1028 XMapWindow(obt_display, self->window);
1030 if (screen_pointer_pos(&px, &py)) {
1031 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1032 if (e && e->frame == self)
1039 static void menu_frame_hide(ObMenuFrame *self)
1041 GList *it = g_list_find(menu_frame_visible, self);
1046 if (self->menu->hide_func)
1047 self->menu->hide_func(self, self->menu->data);
1050 menu_frame_hide(self->child);
1053 self->parent->child = NULL;
1054 self->parent = NULL;
1055 self->parent_entry = NULL;
1057 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1059 if (menu_frame_visible == NULL) {
1060 /* last menu shown */
1065 XUnmapWindow(obt_display, self->window);
1067 menu_frame_free(self);
1070 void menu_frame_hide_all(void)
1074 if (config_submenu_show_delay) {
1075 /* remove any submenu open requests */
1076 obt_main_loop_timeout_remove(ob_main_loop,
1077 menu_entry_frame_submenu_timeout);
1079 if ((it = g_list_last(menu_frame_visible)))
1080 menu_frame_hide(it->data);
1083 void menu_frame_hide_all_client(ObClient *client)
1085 GList *it = g_list_last(menu_frame_visible);
1087 ObMenuFrame *f = it->data;
1088 if (f->client == client) {
1089 if (config_submenu_show_delay) {
1090 /* remove any submenu open requests */
1091 obt_main_loop_timeout_remove(ob_main_loop,
1092 menu_entry_frame_submenu_timeout);
1099 ObMenuFrame* menu_frame_under(gint x, gint y)
1101 ObMenuFrame *ret = NULL;
1104 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1105 ObMenuFrame *f = it->data;
1107 if (RECT_CONTAINS(f->area, x, y)) {
1115 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1118 ObMenuEntryFrame *ret = NULL;
1121 if ((frame = menu_frame_under(x, y))) {
1122 x -= ob_rr_theme->mbwidth + frame->area.x;
1123 y -= ob_rr_theme->mbwidth + frame->area.y;
1125 for (it = frame->entries; it; it = g_list_next(it)) {
1126 ObMenuEntryFrame *e = it->data;
1128 if (RECT_CONTAINS(e->area, x, y)) {
1137 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1139 g_assert(menu_frame_visible);
1140 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1144 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1147 ObMenuEntryFrame *old = self->selected;
1148 ObMenuFrame *oldchild = self->child;
1150 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1153 if (old == entry) return;
1155 if (config_submenu_show_delay) {
1156 /* remove any submenu open requests */
1157 obt_main_loop_timeout_remove(ob_main_loop,
1158 menu_entry_frame_submenu_timeout);
1161 self->selected = entry;
1164 menu_entry_frame_render(old);
1166 menu_frame_hide(oldchild);
1168 if (self->selected) {
1169 menu_entry_frame_render(self->selected);
1171 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1172 if (config_submenu_show_delay && !immediate) {
1173 /* initiate a new submenu open request */
1174 obt_main_loop_timeout_add(ob_main_loop,
1175 config_submenu_show_delay * 1000,
1176 menu_entry_frame_submenu_timeout,
1177 self->selected, g_direct_equal,
1180 menu_entry_frame_show_submenu(self->selected);
1186 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1190 if (!self->entry->data.submenu.submenu) return;
1192 f = menu_frame_new(self->entry->data.submenu.submenu,
1193 self->entry->data.submenu.show_from,
1194 self->frame->client);
1195 /* pass our direction on to our child */
1196 f->direction_right = self->frame->direction_right;
1198 menu_frame_show_submenu(f, self->frame, self);
1201 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1203 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1204 self->entry->data.normal.enabled)
1206 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1208 ObMenuEntry *entry = self->entry;
1209 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1210 gpointer data = self->frame->menu->data;
1211 GSList *acts = self->entry->data.normal.actions;
1212 ObClient *client = self->frame->client;
1213 ObMenuFrame *frame = self->frame;
1215 /* release grabs before executing the shit */
1216 if (!(state & ControlMask)) {
1217 menu_frame_hide_all();
1222 func(entry, frame, client, state, data);
1224 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1225 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1229 void menu_frame_select_previous(ObMenuFrame *self)
1231 GList *it = NULL, *start;
1233 if (self->entries) {
1234 start = it = g_list_find(self->entries, self->selected);
1236 ObMenuEntryFrame *e;
1238 it = it ? g_list_previous(it) : g_list_last(self->entries);
1244 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1246 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1251 menu_frame_select(self, it ? it->data : NULL, TRUE);
1254 void menu_frame_select_next(ObMenuFrame *self)
1256 GList *it = NULL, *start;
1258 if (self->entries) {
1259 start = it = g_list_find(self->entries, self->selected);
1261 ObMenuEntryFrame *e;
1263 it = it ? g_list_next(it) : self->entries;
1269 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1271 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1276 menu_frame_select(self, it ? it->data : NULL, TRUE);