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"
25 #include "action_list.h"
26 #include "action_list_run.h"
32 #include "obt/keyboard.h"
33 #include "obrender/theme.h"
36 #define MAX_MENU_WIDTH 400
38 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
40 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
42 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
43 ButtonPressMask | ButtonReleaseMask | \
46 GList *menu_frame_visible;
47 GHashTable *menu_frame_map;
49 static RrAppearance *a_sep;
50 static guint submenu_show_timer = 0;
51 static guint submenu_hide_timer = 0;
53 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
55 static void menu_entry_frame_free(ObMenuEntryFrame *self);
56 static void menu_frame_update(ObMenuFrame *self);
57 static gboolean submenu_show_timeout(gpointer data);
58 static void menu_frame_hide(ObMenuFrame *self);
60 static gboolean submenu_hide_timeout(gpointer data);
62 static Window createWindow(Window parent, gulong mask,
63 XSetWindowAttributes *attrib)
65 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
66 RrDepth(ob_rr_inst), InputOutput,
67 RrVisual(ob_rr_inst), mask, attrib);
70 static void client_dest(ObClient *client, gpointer data)
74 /* menus can be associated with a client, so null those refs since
75 we are disappearing now */
76 for (it = menu_frame_visible; it; it = g_list_next(it)) {
77 ObMenuFrame *f = it->data;
78 if (f->client == client)
83 void menu_frame_startup(gboolean reconfig)
87 a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
88 RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
89 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
90 a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
91 a_sep->texture[i].data.lineart.color =
92 ob_rr_theme->menu_sep_color;
97 client_add_destroy_notify(client_dest, NULL);
98 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
101 void menu_frame_shutdown(gboolean reconfig)
103 RrAppearanceFree(a_sep);
105 if (reconfig) return;
107 client_remove_destroy_notify(client_dest);
108 g_hash_table_destroy(menu_frame_map);
111 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
114 XSetWindowAttributes attr;
116 self = g_slice_new0(ObMenuFrame);
117 self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
119 self->selected = NULL;
120 self->client = client;
121 self->direction_right = TRUE;
122 self->show_from = show_from;
124 attr.event_mask = FRAME_EVENTMASK;
125 self->window = createWindow(obt_root(ob_screen),
128 /* make it a popup menu type window */
129 OBT_PROP_SET32(self->window, NET_WM_WINDOW_TYPE, ATOM,
130 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU));
132 XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
133 XSetWindowBorder(obt_display, self->window,
134 RrColorPixel(ob_rr_theme->menu_border_color));
136 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
138 window_add(&self->window, MENUFRAME_AS_WINDOW(self));
139 stacking_add(MENUFRAME_AS_WINDOW(self));
144 void menu_frame_free(ObMenuFrame *self)
147 while (self->entries) {
148 menu_entry_frame_free(self->entries->data);
149 self->entries = g_list_delete_link(self->entries, self->entries);
152 stacking_remove(MENUFRAME_AS_WINDOW(self));
153 window_remove(self->window);
155 RrAppearanceFree(self->a_items);
157 XDestroyWindow(obt_display, self->window);
159 g_slice_free(ObMenuFrame, self);
163 ObtIC* menu_frame_ic(ObMenuFrame *self)
165 /* menus are always used through a grab right now, so they can always use
166 the grab input context */
167 return grab_input_context();
170 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
173 ObMenuEntryFrame *self;
174 XSetWindowAttributes attr;
176 self = g_slice_new0(ObMenuEntryFrame);
180 menu_entry_ref(entry);
182 attr.event_mask = ENTRY_EVENTMASK;
183 self->window = createWindow(self->frame->window, CWEventMask, &attr);
184 self->text = createWindow(self->window, 0, NULL);
185 g_hash_table_insert(menu_frame_map, &self->window, self);
186 g_hash_table_insert(menu_frame_map, &self->text, self);
187 if ((entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
188 (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
189 self->icon = createWindow(self->window, 0, NULL);
190 g_hash_table_insert(menu_frame_map, &self->icon, self);
192 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
193 self->bullet = createWindow(self->window, 0, NULL);
194 g_hash_table_insert(menu_frame_map, &self->bullet, self);
197 XMapWindow(obt_display, self->window);
198 XMapWindow(obt_display, self->text);
200 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
205 static void menu_entry_frame_free(ObMenuEntryFrame *self)
208 menu_entry_unref(self->entry);
210 window_remove(self->window);
212 XDestroyWindow(obt_display, self->text);
213 XDestroyWindow(obt_display, self->window);
214 g_hash_table_remove(menu_frame_map, &self->text);
215 g_hash_table_remove(menu_frame_map, &self->window);
216 if ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
217 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
218 XDestroyWindow(obt_display, self->icon);
219 g_hash_table_remove(menu_frame_map, &self->icon);
221 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
222 XDestroyWindow(obt_display, self->bullet);
223 g_hash_table_remove(menu_frame_map, &self->bullet);
226 g_slice_free(ObMenuEntryFrame, self);
230 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
232 RECT_SET_POINT(self->area, x, y);
233 self->monitor = screen_find_monitor_point(x, y);
234 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
237 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
241 if (config_menu_middle) {
245 *y -= self->area.height / 2;
247 /* try to the right of the cursor */
248 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
249 self->direction_right = TRUE;
251 /* try to the left of the cursor */
252 myx = *x - self->area.width;
253 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
254 self->direction_right = FALSE;
257 /* if didnt fit on either side so just use what it says */
259 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
260 self->direction_right = TRUE;
270 /* try to the bottom right of the cursor */
271 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
272 self->direction_right = TRUE;
273 if (dx != 0 || dy != 0) {
274 /* try to the bottom left of the cursor */
275 myx = *x - self->area.width;
277 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
278 self->direction_right = FALSE;
280 if (dx != 0 || dy != 0) {
281 /* try to the top right of the cursor */
283 myy = *y - self->area.height;
284 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
285 self->direction_right = TRUE;
287 if (dx != 0 || dy != 0) {
288 /* try to the top left of the cursor */
289 myx = *x - self->area.width;
290 myy = *y - self->area.height;
291 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
292 self->direction_right = FALSE;
294 if (dx != 0 || dy != 0) {
295 /* if didnt fit on either side so just use what it says */
298 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
299 self->direction_right = TRUE;
306 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
308 gint overlapx, overlapy;
311 overlapx = ob_rr_theme->menu_overlap_x;
312 overlapy = ob_rr_theme->menu_overlap_y;
313 bwidth = ob_rr_theme->mbwidth;
315 if (self->direction_right)
316 *x = self->parent->area.x + self->parent->area.width -
319 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
321 *y = self->parent->area.y + self->parent_entry->area.y;
322 if (config_menu_middle)
323 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
328 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
331 const Rect *a = NULL;
332 Rect search = self->area;
333 gint pos, half, monitor;
336 RECT_SET_POINT(search, x, y);
339 monitor = self->parent->monitor;
341 monitor = screen_find_monitor(&search);
343 a = screen_physical_area_monitor(monitor);
345 half = g_list_length(self->entries) / 2;
346 pos = g_list_index(self->entries, self->selected);
348 /* if in the bottom half then check this stuff first, will keep the bottom
349 edge of the menu visible */
351 *dx = MAX(*dx, a->x - x);
352 *dy = MAX(*dy, a->y - y);
354 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
355 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
356 /* if in the top half then check this stuff last, will keep the top
357 edge of the menu visible */
359 *dx = MAX(*dx, a->x - x);
360 *dy = MAX(*dy, a->y - y);
364 static void menu_entry_frame_render(ObMenuEntryFrame *self)
366 RrAppearance *item_a, *text_a;
369 ObMenuFrame *frame = self->frame;
371 switch (self->entry->type) {
372 case OB_MENU_ENTRY_TYPE_NORMAL:
373 case OB_MENU_ENTRY_TYPE_SUBMENU:
374 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
375 !self->entry->data.normal.enabled ?
377 (self == self->frame->selected ?
378 ob_rr_theme->a_menu_disabled_selected :
379 ob_rr_theme->a_menu_disabled) :
381 (self == self->frame->selected ?
382 ob_rr_theme->a_menu_selected :
383 ob_rr_theme->a_menu_normal));
386 case OB_MENU_ENTRY_TYPE_SEPARATOR:
387 if (self->entry->data.separator.label) {
388 item_a = ob_rr_theme->a_menu_title;
389 th = ob_rr_theme->menu_title_height;
391 item_a = ob_rr_theme->a_menu_normal;
392 th = ob_rr_theme->menu_sep_width +
393 2*ob_rr_theme->menu_sep_paddingy;
397 g_assert_not_reached();
400 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
401 XResizeWindow(obt_display, self->window,
402 self->area.width, self->area.height);
403 item_a->surface.parent = self->frame->a_items;
404 item_a->surface.parentx = self->area.x;
405 item_a->surface.parenty = self->area.y;
406 RrPaint(item_a, self->window, self->area.width, self->area.height);
408 switch (self->entry->type) {
409 case OB_MENU_ENTRY_TYPE_NORMAL:
410 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
411 !self->entry->data.normal.enabled ?
413 (self == self->frame->selected ?
414 ob_rr_theme->a_menu_text_disabled_selected :
415 ob_rr_theme->a_menu_text_disabled) :
417 (self == self->frame->selected ?
418 ob_rr_theme->a_menu_text_selected :
419 ob_rr_theme->a_menu_text_normal));
420 text_a->texture[0].data.text.string = self->entry->data.normal.label;
421 if (self->entry->data.normal.shortcut &&
422 (self->frame->menu->show_all_shortcuts ||
423 self->entry->data.normal.shortcut_always_show ||
424 self->entry->data.normal.shortcut_position > 0))
426 text_a->texture[0].data.text.shortcut = TRUE;
427 text_a->texture[0].data.text.shortcut_pos =
428 self->entry->data.normal.shortcut_position;
430 text_a->texture[0].data.text.shortcut = FALSE;
432 case OB_MENU_ENTRY_TYPE_SUBMENU:
433 text_a = (self == self->frame->selected ?
434 ob_rr_theme->a_menu_text_selected :
435 ob_rr_theme->a_menu_text_normal);
436 sub = self->entry->data.submenu.submenu;
437 text_a->texture[0].data.text.string = sub ? sub->title : "";
438 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
439 sub->shortcut_always_show ||
440 sub->shortcut_position > 0))
442 text_a->texture[0].data.text.shortcut = TRUE;
443 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
445 text_a->texture[0].data.text.shortcut = FALSE;
447 case OB_MENU_ENTRY_TYPE_SEPARATOR:
448 if (self->entry->data.separator.label != NULL) {
449 text_a = ob_rr_theme->a_menu_text_title;
450 text_a->texture[0].data.text.string =
451 self->entry->data.separator.label;
454 text_a = ob_rr_theme->a_menu_text_normal;
457 g_assert_not_reached();
460 switch (self->entry->type) {
461 case OB_MENU_ENTRY_TYPE_NORMAL:
462 XMoveResizeWindow(obt_display, self->text,
463 self->frame->text_x, PADDING,
465 ITEM_HEIGHT - 2*PADDING);
466 text_a->surface.parent = item_a;
467 text_a->surface.parentx = self->frame->text_x;
468 text_a->surface.parenty = PADDING;
469 RrPaint(text_a, self->text, self->frame->text_w,
470 ITEM_HEIGHT - 2*PADDING);
472 case OB_MENU_ENTRY_TYPE_SUBMENU:
473 XMoveResizeWindow(obt_display, self->text,
474 self->frame->text_x, PADDING,
475 self->frame->text_w - ITEM_HEIGHT,
476 ITEM_HEIGHT - 2*PADDING);
477 text_a->surface.parent = item_a;
478 text_a->surface.parentx = self->frame->text_x;
479 text_a->surface.parenty = PADDING;
480 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
481 ITEM_HEIGHT - 2*PADDING);
483 case OB_MENU_ENTRY_TYPE_SEPARATOR:
484 if (self->entry->data.separator.label != NULL) {
485 /* labeled separator */
486 XMoveResizeWindow(obt_display, self->text,
487 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
488 self->area.width - 2*ob_rr_theme->paddingx,
489 ob_rr_theme->menu_title_height -
490 2*ob_rr_theme->paddingy);
491 text_a->surface.parent = item_a;
492 text_a->surface.parentx = ob_rr_theme->paddingx;
493 text_a->surface.parenty = ob_rr_theme->paddingy;
494 RrPaint(text_a, self->text,
495 self->area.width - 2*ob_rr_theme->paddingx,
496 ob_rr_theme->menu_title_height -
497 2*ob_rr_theme->paddingy);
501 /* unlabeled separator */
502 XMoveResizeWindow(obt_display, self->text, 0, 0,
504 ob_rr_theme->menu_sep_width +
505 2*ob_rr_theme->menu_sep_paddingy);
507 a_sep->surface.parent = item_a;
508 a_sep->surface.parentx = 0;
509 a_sep->surface.parenty = 0;
510 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
511 a_sep->texture[i].data.lineart.x1 =
512 ob_rr_theme->menu_sep_paddingx;
513 a_sep->texture[i].data.lineart.y1 =
514 ob_rr_theme->menu_sep_paddingy + i;
515 a_sep->texture[i].data.lineart.x2 =
516 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
517 a_sep->texture[i].data.lineart.y2 =
518 ob_rr_theme->menu_sep_paddingy + i;
521 RrPaint(a_sep, self->text, self->area.width,
522 ob_rr_theme->menu_sep_width +
523 2*ob_rr_theme->menu_sep_paddingy);
527 g_assert_not_reached();
530 if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
531 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
532 self->entry->data.normal.icon)
536 XMoveResizeWindow(obt_display, self->icon,
537 PADDING, frame->item_margin.top,
538 ITEM_HEIGHT - frame->item_margin.top
539 - frame->item_margin.bottom,
540 ITEM_HEIGHT - frame->item_margin.top
541 - frame->item_margin.bottom);
543 clear = ob_rr_theme->a_clear_tex;
544 RrAppearanceClearTextures(clear);
545 clear->texture[0].type = RR_TEXTURE_IMAGE;
546 clear->texture[0].data.image.image =
547 self->entry->data.normal.icon;
548 clear->texture[0].data.image.alpha =
549 self->entry->data.normal.icon_alpha;
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);
559 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
560 self->entry->data.normal.mask)
565 XMoveResizeWindow(obt_display, self->icon,
566 PADDING, frame->item_margin.top,
567 ITEM_HEIGHT - frame->item_margin.top
568 - frame->item_margin.bottom,
569 ITEM_HEIGHT - frame->item_margin.top
570 - frame->item_margin.bottom);
572 clear = ob_rr_theme->a_clear_tex;
573 RrAppearanceClearTextures(clear);
574 clear->texture[0].type = RR_TEXTURE_MASK;
575 clear->texture[0].data.mask.mask =
576 self->entry->data.normal.mask;
578 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
579 !self->entry->data.normal.enabled ?
581 (self == self->frame->selected ?
582 self->entry->data.normal.mask_disabled_selected_color :
583 self->entry->data.normal.mask_disabled_color) :
585 (self == self->frame->selected ?
586 self->entry->data.normal.mask_selected_color :
587 self->entry->data.normal.mask_normal_color));
588 clear->texture[0].data.mask.color = c;
590 clear->surface.parent = item_a;
591 clear->surface.parentx = PADDING;
592 clear->surface.parenty = frame->item_margin.top;
593 RrPaint(clear, self->icon,
594 ITEM_HEIGHT - frame->item_margin.top
595 - frame->item_margin.bottom,
596 ITEM_HEIGHT - frame->item_margin.top
597 - frame->item_margin.bottom);
598 XMapWindow(obt_display, self->icon);
600 XUnmapWindow(obt_display, self->icon);
602 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
603 RrAppearance *bullet_a;
604 XMoveResizeWindow(obt_display, self->bullet,
605 self->frame->text_x + self->frame->text_w -
606 ITEM_HEIGHT + PADDING, PADDING,
607 ITEM_HEIGHT - 2*PADDING,
608 ITEM_HEIGHT - 2*PADDING);
609 bullet_a = (self == self->frame->selected ?
610 ob_rr_theme->a_menu_bullet_selected :
611 ob_rr_theme->a_menu_bullet_normal);
612 bullet_a->surface.parent = item_a;
613 bullet_a->surface.parentx =
614 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
615 bullet_a->surface.parenty = PADDING;
616 RrPaint(bullet_a, self->bullet,
617 ITEM_HEIGHT - 2*PADDING,
618 ITEM_HEIGHT - 2*PADDING);
619 XMapWindow(obt_display, self->bullet);
621 XUnmapWindow(obt_display, self->bullet);
626 /*! this code is taken from the menu_frame_render. if that changes, this won't
628 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
629 gboolean first_entry,
638 t = self->entry->type;
640 /* this is the More... entry, it's NORMAL type */
641 t = OB_MENU_ENTRY_TYPE_NORMAL;
644 case OB_MENU_ENTRY_TYPE_NORMAL:
645 case OB_MENU_ENTRY_TYPE_SUBMENU:
646 h += ob_rr_theme->menu_font_height;
648 case OB_MENU_ENTRY_TYPE_SEPARATOR:
649 if (self->entry->data.separator.label != NULL) {
650 h += ob_rr_theme->menu_title_height +
651 (ob_rr_theme->mbwidth - PADDING) * 2;
653 /* if the first entry is a labeled separator, then make its border
654 overlap with the menu's outside border */
656 h -= ob_rr_theme->mbwidth;
657 /* if the last entry is a labeled separator, then make its border
658 overlap with the menu's outside border */
660 h -= ob_rr_theme->mbwidth;
662 h += ob_rr_theme->menu_sep_width +
663 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
671 void menu_frame_render(ObMenuFrame *self)
674 gint tw, th; /* temps */
676 gboolean has_icon = FALSE;
680 /* find text dimensions */
682 STRUT_SET(self->item_margin, 0, 0, 0, 0);
687 e = self->entries->data;
688 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
689 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
694 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
695 STRUT_SET(self->item_margin,
696 MAX(self->item_margin.left, l),
697 MAX(self->item_margin.top, t),
698 MAX(self->item_margin.right, r),
699 MAX(self->item_margin.bottom, b));
700 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
701 STRUT_SET(self->item_margin,
702 MAX(self->item_margin.left, l),
703 MAX(self->item_margin.top, t),
704 MAX(self->item_margin.right, r),
705 MAX(self->item_margin.bottom, b));
706 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
707 STRUT_SET(self->item_margin,
708 MAX(self->item_margin.left, l),
709 MAX(self->item_margin.top, t),
710 MAX(self->item_margin.right, r),
711 MAX(self->item_margin.bottom, b));
712 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
713 STRUT_SET(self->item_margin,
714 MAX(self->item_margin.left, l),
715 MAX(self->item_margin.top, t),
716 MAX(self->item_margin.right, r),
717 MAX(self->item_margin.bottom, b));
720 /* render the entries */
722 for (it = self->entries; it; it = g_list_next(it)) {
723 RrAppearance *text_a;
726 /* if the first entry is a labeled separator, then make its border
727 overlap with the menu's outside border */
728 if (it == self->entries &&
729 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
730 e->entry->data.separator.label)
732 h -= ob_rr_theme->mbwidth;
735 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
736 e->entry->data.separator.label)
738 e->border = ob_rr_theme->mbwidth;
741 RECT_SET_POINT(e->area, 0, h+e->border);
742 XMoveWindow(obt_display, e->window,
743 e->area.x-e->border, e->area.y-e->border);
744 XSetWindowBorderWidth(obt_display, e->window, e->border);
745 XSetWindowBorder(obt_display, e->window,
746 RrColorPixel(ob_rr_theme->menu_border_color));
748 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
749 !e->entry->data.normal.enabled ?
751 (e == self->selected ?
752 ob_rr_theme->a_menu_text_disabled_selected :
753 ob_rr_theme->a_menu_text_disabled) :
755 (e == self->selected ?
756 ob_rr_theme->a_menu_text_selected :
757 ob_rr_theme->a_menu_text_normal));
758 switch (e->entry->type) {
759 case OB_MENU_ENTRY_TYPE_NORMAL:
760 text_a->texture[0].data.text.string = e->entry->data.normal.label;
761 tw = RrMinWidth(text_a);
762 tw = MIN(tw, MAX_MENU_WIDTH);
763 th = ob_rr_theme->menu_font_height;
765 if (e->entry->data.normal.icon ||
766 e->entry->data.normal.mask)
769 case OB_MENU_ENTRY_TYPE_SUBMENU:
770 sub = e->entry->data.submenu.submenu;
771 text_a->texture[0].data.text.string = sub ? sub->title : "";
772 tw = RrMinWidth(text_a);
773 tw = MIN(tw, MAX_MENU_WIDTH);
774 th = ob_rr_theme->menu_font_height;
776 if (e->entry->data.normal.icon ||
777 e->entry->data.normal.mask)
780 tw += ITEM_HEIGHT - PADDING;
782 case OB_MENU_ENTRY_TYPE_SEPARATOR:
783 if (e->entry->data.separator.label != NULL) {
784 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
785 e->entry->data.separator.label;
786 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
787 2*ob_rr_theme->paddingx;
788 tw = MIN(tw, MAX_MENU_WIDTH);
789 th = ob_rr_theme->menu_title_height +
790 (ob_rr_theme->mbwidth - PADDING) *2;
793 th = ob_rr_theme->menu_sep_width +
794 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
798 g_assert_not_reached();
806 /* if the last entry is a labeled separator, then make its border
807 overlap with the menu's outside border */
808 it = g_list_last(self->entries);
809 e = it ? it->data : NULL;
810 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
811 e->entry->data.separator.label)
813 h -= ob_rr_theme->mbwidth;
816 self->text_x = PADDING;
821 w += ITEM_HEIGHT + PADDING;
822 self->text_x += ITEM_HEIGHT + PADDING;
829 XResizeWindow(obt_display, self->window, w, h);
833 RrPaint(self->a_items, self->window, w, h);
835 for (it = self->entries; it; it = g_list_next(it))
836 menu_entry_frame_render(it->data);
838 w += ob_rr_theme->mbwidth * 2;
839 h += ob_rr_theme->mbwidth * 2;
841 RECT_SET_SIZE(self->area, w, h);
846 static void menu_frame_update(ObMenuFrame *self)
852 menu_pipe_execute(self->menu);
853 menu_find_submenus(self->menu);
855 self->selected = NULL;
857 /* start at show_from */
858 mit = g_list_nth(self->menu->entries, self->show_from);
860 /* go through the menu's and frame's entries and connect the frame entries
861 to the menu entries */
862 for (fit = self->entries; mit && fit;
863 mit = g_list_next(mit), fit = g_list_next(fit))
865 ObMenuEntryFrame *f = fit->data;
866 f->entry = mit->data;
869 /* if there are more menu entries than in the frame, add them */
871 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
872 self->entries = g_list_append(self->entries, e);
873 mit = g_list_next(mit);
876 /* if there are more frame entries than menu entries then get rid of
879 GList *n = g_list_next(fit);
880 menu_entry_frame_free(fit->data);
881 self->entries = g_list_delete_link(self->entries, fit);
885 /* * make the menu fit on the screen */
887 /* calculate the height of the menu */
889 for (fit = self->entries; fit; fit = g_list_next(fit))
890 h += menu_entry_frame_get_height(fit->data,
891 fit == self->entries,
892 g_list_next(fit) == NULL);
893 /* add the border at the top and bottom */
894 h += ob_rr_theme->mbwidth * 2;
896 a = screen_physical_area_monitor(self->monitor);
900 gboolean last_entry = TRUE;
902 /* take the height of our More... entry into account */
903 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
905 /* start at the end of the entries */
906 flast = g_list_last(self->entries);
908 /* pull out all the entries from the frame that don't
909 fit on the screen, leaving at least 1 though */
910 while (h > a->height && g_list_previous(flast) != NULL) {
911 /* update the height, without this entry */
912 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
914 /* destroy the entry we're not displaying */
916 flast = g_list_previous(flast);
917 menu_entry_frame_free(tmp->data);
918 self->entries = g_list_delete_link(self->entries, tmp);
920 /* only the first one that we see is the last entry in the menu */
925 ObMenuEntry *more_entry;
926 ObMenuEntryFrame *more_frame;
927 /* make the More... menu entry frame which will display in this
929 if self->menu->more_menu is NULL that means that this is already
930 More... menu, so just use ourself.
932 more_entry = menu_get_more((self->menu->more_menu ?
933 self->menu->more_menu :
935 /* continue where we left off */
937 g_list_length(self->entries));
938 more_frame = menu_entry_frame_new(more_entry, self);
939 /* make it get deleted when the menu frame goes away */
940 menu_entry_unref(more_entry);
942 /* add our More... entry to the frame */
943 self->entries = g_list_append(self->entries, more_frame);
947 menu_frame_render(self);
950 static gboolean menu_frame_is_visible(ObMenuFrame *self)
952 return !!(g_list_find(menu_frame_visible, self));
955 static gboolean menu_frame_show(ObMenuFrame *self)
959 /* determine if the underlying menu is already visible */
960 for (it = menu_frame_visible; it; it = g_list_next(it)) {
961 ObMenuFrame *f = it->data;
962 if (f->menu == self->menu)
966 if (self->menu->update_func)
967 if (!self->menu->update_func(self, self->menu->data))
971 if (menu_frame_visible == NULL) {
972 /* no menus shown yet */
974 /* grab the pointer in such a way as to pass through "owner events"
975 so that we can get enter/leave notifies in the menu. */
976 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
978 if (!grab_keyboard()) {
984 menu_frame_update(self);
986 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
988 if (self->menu->show_func)
989 self->menu->show_func(self, self->menu->data);
994 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
999 if (menu_frame_is_visible(self))
1001 if (!menu_frame_show(self))
1004 if (self->menu->place_func)
1005 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1007 menu_frame_place_topmenu(self, &x, &y);
1009 menu_frame_move(self, x, y);
1011 XMapWindow(obt_display, self->window);
1013 if (screen_pointer_pos(&px, &py)) {
1014 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1015 if (e && e->frame == self)
1022 /*! Stop hiding an open submenu.
1023 @child The OnMenuFrame of the submenu to be hidden
1025 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1027 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1028 submenu_hide_timer = 0;
1031 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1032 ObMenuEntryFrame *parent_entry)
1037 if (menu_frame_is_visible(self))
1040 self->monitor = parent->monitor;
1041 self->parent = parent;
1042 self->parent_entry = parent_entry;
1044 /* set up parent's child to be us */
1045 if ((parent->child) != self) {
1047 menu_frame_hide(parent->child);
1048 parent->child = self;
1049 parent->child_entry = parent_entry;
1052 if (!menu_frame_show(self)) {
1053 parent->child = NULL;
1054 parent->child_entry = NULL;
1058 menu_frame_place_submenu(self, &x, &y);
1059 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1062 /*try the other side */
1063 self->direction_right = !self->direction_right;
1064 menu_frame_place_submenu(self, &x, &y);
1065 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1067 menu_frame_move(self, x + dx, y + dy);
1069 XMapWindow(obt_display, self->window);
1071 if (screen_pointer_pos(&px, &py)) {
1072 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1073 if (e && e->frame == self)
1080 static void menu_frame_hide(ObMenuFrame *self)
1082 ObMenu *const menu = self->menu;
1083 GList *it = g_list_find(menu_frame_visible, self);
1084 gulong ignore_start;
1089 if (menu->hide_func)
1090 menu->hide_func(self, menu->data);
1093 menu_frame_hide(self->child);
1096 remove_submenu_hide_timeout(self);
1098 self->parent->child = NULL;
1099 self->parent->child_entry = NULL;
1101 self->parent = NULL;
1102 self->parent_entry = NULL;
1104 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1106 if (menu_frame_visible == NULL) {
1107 /* last menu shown */
1112 ignore_start = event_start_ignore_all_enters();
1113 XUnmapWindow(obt_display, self->window);
1114 event_end_ignore_all_enters(ignore_start);
1116 menu_frame_free(self);
1118 if (menu->cleanup_func)
1119 menu->cleanup_func(menu, menu->data);
1122 void menu_frame_hide_all(void)
1126 if (config_submenu_show_delay) {
1127 /* remove any submenu open requests */
1128 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1129 submenu_show_timer = 0;
1131 if ((it = g_list_last(menu_frame_visible)))
1132 menu_frame_hide(it->data);
1135 ObMenuFrame* menu_frame_under(gint x, gint y)
1137 ObMenuFrame *ret = NULL;
1140 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1141 ObMenuFrame *f = it->data;
1143 if (RECT_CONTAINS(f->area, x, y)) {
1151 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1154 ObMenuEntryFrame *ret = NULL;
1157 if ((frame = menu_frame_under(x, y))) {
1158 x -= ob_rr_theme->mbwidth + frame->area.x;
1159 y -= ob_rr_theme->mbwidth + frame->area.y;
1161 for (it = frame->entries; it; it = g_list_next(it)) {
1162 ObMenuEntryFrame *e = it->data;
1164 if (RECT_CONTAINS(e->area, x, y)) {
1173 static gboolean submenu_show_timeout(gpointer data)
1175 g_assert(menu_frame_visible);
1176 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1180 static gboolean submenu_hide_timeout(gpointer data)
1182 g_assert(menu_frame_visible);
1183 menu_frame_hide((ObMenuFrame*)data);
1187 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1190 ObMenuEntryFrame *old = self->selected;
1191 ObMenuFrame *oldchild = self->child;
1192 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1194 /* if the user selected a separator, ignore it and reselect what we had
1196 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1200 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1203 /* if the user left this menu but we have a submenu open, move the
1204 selection back to that submenu */
1205 if (!entry && oldchild_entry)
1206 entry = oldchild_entry;
1208 if (config_submenu_show_delay) {
1209 /* remove any submenu open requests */
1210 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1211 submenu_show_timer = 0;
1214 self->selected = entry;
1217 menu_entry_frame_render(old);
1219 if (oldchild_entry) {
1220 /* There is an open submenu */
1221 if (oldchild_entry == self->selected) {
1222 /* The open submenu has been reselected, so stop hiding the
1224 remove_submenu_hide_timeout(oldchild);
1226 else if (oldchild_entry == old) {
1227 /* The open submenu was selected and is no longer, so hide the
1229 if (immediate || config_submenu_hide_delay == 0)
1230 menu_frame_hide(oldchild);
1231 else if (config_submenu_hide_delay > 0) {
1232 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1233 submenu_hide_timer =
1234 g_timeout_add_full(G_PRIORITY_DEFAULT,
1235 config_submenu_hide_delay,
1236 submenu_hide_timeout, oldchild, NULL);
1241 if (self->selected) {
1242 menu_entry_frame_render(self->selected);
1244 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1245 /* only show if the submenu isn't already showing */
1246 if (oldchild_entry != self->selected) {
1247 if (immediate || config_submenu_hide_delay == 0)
1248 menu_entry_frame_show_submenu(self->selected);
1249 else if (config_submenu_hide_delay > 0) {
1250 if (submenu_show_timer)
1251 g_source_remove(submenu_show_timer);
1252 submenu_show_timer =
1253 g_timeout_add_full(G_PRIORITY_DEFAULT,
1254 config_submenu_show_delay,
1255 submenu_show_timeout,
1256 self->selected, NULL);
1259 /* hide the grandchildren of this menu. and move the cursor to
1261 else if (immediate && self->child && self->child->child) {
1262 menu_frame_hide(self->child->child);
1263 menu_frame_select(self->child, NULL, TRUE);
1269 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1273 if (!self->entry->data.submenu.submenu) return;
1275 f = menu_frame_new(self->entry->data.submenu.submenu,
1276 self->entry->data.submenu.show_from,
1277 self->frame->client);
1278 /* pass our direction on to our child */
1279 f->direction_right = self->frame->direction_right;
1281 if (!menu_frame_show_submenu(f, self->frame, self))
1285 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1287 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1288 self->entry->data.normal.enabled)
1290 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1292 ObMenuEntry *entry = self->entry;
1293 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1294 gpointer data = self->frame->menu->data;
1295 ObActionList *acts = self->entry->data.normal.actions;
1296 ObClient *client = self->frame->client;
1297 ObMenuFrame *frame = self->frame;
1298 guint mods = obt_keyboard_only_modmasks(state);
1300 /* release grabs before executing the shit */
1301 if (!(mods & ControlMask)) {
1302 event_cancel_all_key_grabs();
1307 func(entry, frame, client, state, data);
1309 action_list_run(acts, OB_USER_ACTION_MENU_SELECTION,
1310 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1314 void menu_frame_select_previous(ObMenuFrame *self)
1316 GList *it = NULL, *start;
1318 if (self->entries) {
1319 start = it = g_list_find(self->entries, self->selected);
1321 ObMenuEntryFrame *e;
1323 it = it ? g_list_previous(it) : g_list_last(self->entries);
1329 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1331 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1336 menu_frame_select(self, it ? it->data : NULL, FALSE);
1339 void menu_frame_select_next(ObMenuFrame *self)
1341 GList *it = NULL, *start;
1343 if (self->entries) {
1344 start = it = g_list_find(self->entries, self->selected);
1346 ObMenuEntryFrame *e;
1348 it = it ? g_list_next(it) : self->entries;
1354 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1356 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1361 menu_frame_select(self, it ? it->data : NULL, FALSE);
1364 void menu_frame_select_first(ObMenuFrame *self)
1368 if (self->entries) {
1369 for (it = self->entries; it; it = g_list_next(it)) {
1370 ObMenuEntryFrame *e = it->data;
1371 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1373 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1377 menu_frame_select(self, it ? it->data : NULL, FALSE);
1380 void menu_frame_select_last(ObMenuFrame *self)
1384 if (self->entries) {
1385 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1386 ObMenuEntryFrame *e = it->data;
1387 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1389 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1393 menu_frame_select(self, it ? it->data : NULL, FALSE);