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"
30 #include "obt/keyboard.h"
31 #include "obrender/theme.h"
34 #define MAX_MENU_WIDTH 400
36 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
38 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
40 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
41 ButtonPressMask | ButtonReleaseMask | \
44 GList *menu_frame_visible;
45 GHashTable *menu_frame_map;
47 static RrAppearance *a_sep;
48 static guint submenu_show_timer = 0;
49 static guint submenu_hide_timer = 0;
51 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
53 static void menu_entry_frame_free(ObMenuEntryFrame *self);
54 static void menu_frame_update(ObMenuFrame *self);
55 static gboolean submenu_show_timeout(gpointer data);
56 static void menu_frame_hide(ObMenuFrame *self);
58 static gboolean submenu_hide_timeout(gpointer data);
60 static Window createWindow(Window parent, gulong mask,
61 XSetWindowAttributes *attrib)
63 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
64 RrDepth(ob_rr_inst), InputOutput,
65 RrVisual(ob_rr_inst), mask, attrib);
68 static void client_dest(ObClient *client, gpointer data)
72 /* menus can be associated with a client, so null those refs since
73 we are disappearing now */
74 for (it = menu_frame_visible; it; it = g_list_next(it)) {
75 ObMenuFrame *f = it->data;
76 if (f->client == client)
81 void menu_frame_startup(gboolean reconfig)
85 a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
86 RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
87 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
88 a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
89 a_sep->texture[i].data.lineart.color =
90 ob_rr_theme->menu_sep_color;
95 client_add_destroy_notify(client_dest, NULL);
96 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
99 void menu_frame_shutdown(gboolean reconfig)
101 RrAppearanceFree(a_sep);
103 if (reconfig) return;
105 client_remove_destroy_notify(client_dest);
106 g_hash_table_destroy(menu_frame_map);
109 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
112 XSetWindowAttributes attr;
114 self = g_slice_new0(ObMenuFrame);
115 self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
117 self->selected = NULL;
118 self->client = client;
119 self->direction_right = TRUE;
120 self->show_from = show_from;
122 attr.event_mask = FRAME_EVENTMASK;
123 self->window = createWindow(obt_root(ob_screen),
126 /* make it a popup menu type window */
127 OBT_PROP_SET32(self->window, NET_WM_WINDOW_TYPE, ATOM,
128 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU));
130 XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
131 XSetWindowBorder(obt_display, self->window,
132 RrColorPixel(ob_rr_theme->menu_border_color));
134 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
136 window_add(&self->window, MENUFRAME_AS_WINDOW(self));
137 stacking_add(MENUFRAME_AS_WINDOW(self));
142 void menu_frame_free(ObMenuFrame *self)
145 while (self->entries) {
146 menu_entry_frame_free(self->entries->data);
147 self->entries = g_list_delete_link(self->entries, self->entries);
150 stacking_remove(MENUFRAME_AS_WINDOW(self));
151 window_remove(self->window);
153 RrAppearanceFree(self->a_items);
155 XDestroyWindow(obt_display, self->window);
157 g_slice_free(ObMenuFrame, self);
161 ObtIC* menu_frame_ic(ObMenuFrame *self)
163 /* menus are always used through a grab right now, so they can always use
164 the grab input context */
165 return grab_input_context();
168 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
171 ObMenuEntryFrame *self;
172 XSetWindowAttributes attr;
174 self = g_slice_new0(ObMenuEntryFrame);
178 menu_entry_ref(entry);
180 attr.event_mask = ENTRY_EVENTMASK;
181 self->window = createWindow(self->frame->window, CWEventMask, &attr);
182 self->text = createWindow(self->window, 0, NULL);
183 g_hash_table_insert(menu_frame_map, &self->window, self);
184 g_hash_table_insert(menu_frame_map, &self->text, self);
185 if ((entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
186 (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
187 self->icon = createWindow(self->window, 0, NULL);
188 g_hash_table_insert(menu_frame_map, &self->icon, self);
190 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
191 self->bullet = createWindow(self->window, 0, NULL);
192 g_hash_table_insert(menu_frame_map, &self->bullet, self);
195 XMapWindow(obt_display, self->window);
196 XMapWindow(obt_display, self->text);
198 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
203 static void menu_entry_frame_free(ObMenuEntryFrame *self)
206 menu_entry_unref(self->entry);
208 window_remove(self->window);
210 XDestroyWindow(obt_display, self->text);
211 XDestroyWindow(obt_display, self->window);
212 g_hash_table_remove(menu_frame_map, &self->text);
213 g_hash_table_remove(menu_frame_map, &self->window);
214 if ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
215 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
216 XDestroyWindow(obt_display, self->icon);
217 g_hash_table_remove(menu_frame_map, &self->icon);
219 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
220 XDestroyWindow(obt_display, self->bullet);
221 g_hash_table_remove(menu_frame_map, &self->bullet);
224 g_slice_free(ObMenuEntryFrame, self);
228 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
230 RECT_SET_POINT(self->area, x, y);
231 self->monitor = screen_find_monitor_point(x, y);
232 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
235 static void menu_frame_place_topmenu(ObMenuFrame *self, GravityPoint *pos,
236 gint *x, gint *y, gint monitor)
240 screen_apply_gravity_point(pos, x, y, screen_physical_area_monitor(monitor),
241 self->area.width, self->area.height);
243 if (config_menu_middle) {
245 *x -= self->area.width / 2;
247 /* try below the cursor */
248 menu_frame_move_on_screen(self, *x, *y, &dx, &dy);
249 self->direction_right = TRUE;
258 /* try to the bottom right of the cursor */
259 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
260 self->direction_right = TRUE;
261 if (dx != 0 || dy != 0) {
262 /* try to the bottom left of the cursor */
263 myx = *x - self->area.width;
265 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
266 self->direction_right = FALSE;
268 if (dx != 0 || dy != 0) {
269 /* try to the top right of the cursor */
271 myy = *y - self->area.height;
272 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
273 self->direction_right = TRUE;
275 if (dx != 0 || dy != 0) {
276 /* try to the top left of the cursor */
277 myx = *x - self->area.width;
278 myy = *y - self->area.height;
279 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
280 self->direction_right = FALSE;
282 if (dx != 0 || dy != 0) {
283 /* if didnt fit on either side so just use what it says */
286 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
287 self->direction_right = TRUE;
294 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
296 gint overlapx, overlapy;
299 overlapx = ob_rr_theme->menu_overlap_x;
300 overlapy = ob_rr_theme->menu_overlap_y;
301 bwidth = ob_rr_theme->mbwidth;
303 if (self->direction_right)
304 *x = self->parent->area.x + self->parent->area.width -
307 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
309 *y = self->parent->area.y + self->parent_entry->area.y;
310 if (config_menu_middle)
311 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
316 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
319 const Rect *a = NULL;
320 Rect search = self->area;
321 gint pos, half, monitor;
324 RECT_SET_POINT(search, x, y);
327 monitor = self->parent->monitor;
329 monitor = screen_find_monitor(&search);
331 a = screen_physical_area_monitor(monitor);
333 half = g_list_length(self->entries) / 2;
334 pos = g_list_index(self->entries, self->selected);
336 /* if in the bottom half then check this stuff first, will keep the bottom
337 edge of the menu visible */
339 *dx = MAX(*dx, a->x - x);
340 *dy = MAX(*dy, a->y - y);
342 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
343 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
344 /* if in the top half then check this stuff last, will keep the top
345 edge of the menu visible */
347 *dx = MAX(*dx, a->x - x);
348 *dy = MAX(*dy, a->y - y);
352 static void menu_entry_frame_render(ObMenuEntryFrame *self)
354 RrAppearance *item_a, *text_a;
357 ObMenuFrame *frame = self->frame;
359 switch (self->entry->type) {
360 case OB_MENU_ENTRY_TYPE_NORMAL:
361 case OB_MENU_ENTRY_TYPE_SUBMENU:
362 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
363 !self->entry->data.normal.enabled ?
365 (self == self->frame->selected ?
366 ob_rr_theme->a_menu_disabled_selected :
367 ob_rr_theme->a_menu_disabled) :
369 (self == self->frame->selected ?
370 ob_rr_theme->a_menu_selected :
371 ob_rr_theme->a_menu_normal));
374 case OB_MENU_ENTRY_TYPE_SEPARATOR:
375 if (self->entry->data.separator.label) {
376 item_a = ob_rr_theme->a_menu_title;
377 th = ob_rr_theme->menu_title_height;
379 item_a = ob_rr_theme->a_menu_normal;
380 th = ob_rr_theme->menu_sep_width +
381 2*ob_rr_theme->menu_sep_paddingy;
385 g_assert_not_reached();
388 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
389 XResizeWindow(obt_display, self->window,
390 self->area.width, self->area.height);
391 item_a->surface.parent = self->frame->a_items;
392 item_a->surface.parentx = self->area.x;
393 item_a->surface.parenty = self->area.y;
394 RrPaint(item_a, self->window, self->area.width, self->area.height);
396 switch (self->entry->type) {
397 case OB_MENU_ENTRY_TYPE_NORMAL:
398 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
399 !self->entry->data.normal.enabled ?
401 (self == self->frame->selected ?
402 ob_rr_theme->a_menu_text_disabled_selected :
403 ob_rr_theme->a_menu_text_disabled) :
405 (self == self->frame->selected ?
406 ob_rr_theme->a_menu_text_selected :
407 ob_rr_theme->a_menu_text_normal));
408 text_a->texture[0].data.text.string = self->entry->data.normal.label;
409 if (self->entry->data.normal.shortcut &&
410 (self->frame->menu->show_all_shortcuts ||
411 self->entry->data.normal.shortcut_always_show ||
412 self->entry->data.normal.shortcut_position > 0))
414 text_a->texture[0].data.text.shortcut = TRUE;
415 text_a->texture[0].data.text.shortcut_pos =
416 self->entry->data.normal.shortcut_position;
418 text_a->texture[0].data.text.shortcut = FALSE;
420 case OB_MENU_ENTRY_TYPE_SUBMENU:
421 text_a = (self == self->frame->selected ?
422 ob_rr_theme->a_menu_text_selected :
423 ob_rr_theme->a_menu_text_normal);
424 sub = self->entry->data.submenu.submenu;
425 text_a->texture[0].data.text.string = sub ? sub->title : "";
426 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
427 sub->shortcut_always_show ||
428 sub->shortcut_position > 0))
430 text_a->texture[0].data.text.shortcut = TRUE;
431 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
433 text_a->texture[0].data.text.shortcut = FALSE;
435 case OB_MENU_ENTRY_TYPE_SEPARATOR:
436 if (self->entry->data.separator.label != NULL) {
437 text_a = ob_rr_theme->a_menu_text_title;
438 text_a->texture[0].data.text.string =
439 self->entry->data.separator.label;
442 text_a = ob_rr_theme->a_menu_text_normal;
445 g_assert_not_reached();
448 switch (self->entry->type) {
449 case OB_MENU_ENTRY_TYPE_NORMAL:
450 XMoveResizeWindow(obt_display, self->text,
451 self->frame->text_x, PADDING,
453 ITEM_HEIGHT - 2*PADDING);
454 text_a->surface.parent = item_a;
455 text_a->surface.parentx = self->frame->text_x;
456 text_a->surface.parenty = PADDING;
457 RrPaint(text_a, self->text, self->frame->text_w,
458 ITEM_HEIGHT - 2*PADDING);
460 case OB_MENU_ENTRY_TYPE_SUBMENU:
461 XMoveResizeWindow(obt_display, self->text,
462 self->frame->text_x, PADDING,
463 self->frame->text_w - ITEM_HEIGHT,
464 ITEM_HEIGHT - 2*PADDING);
465 text_a->surface.parent = item_a;
466 text_a->surface.parentx = self->frame->text_x;
467 text_a->surface.parenty = PADDING;
468 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
469 ITEM_HEIGHT - 2*PADDING);
471 case OB_MENU_ENTRY_TYPE_SEPARATOR:
472 if (self->entry->data.separator.label != NULL) {
473 /* labeled separator */
474 XMoveResizeWindow(obt_display, self->text,
475 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
476 self->area.width - 2*ob_rr_theme->paddingx,
477 ob_rr_theme->menu_title_height -
478 2*ob_rr_theme->paddingy);
479 text_a->surface.parent = item_a;
480 text_a->surface.parentx = ob_rr_theme->paddingx;
481 text_a->surface.parenty = ob_rr_theme->paddingy;
482 RrPaint(text_a, self->text,
483 self->area.width - 2*ob_rr_theme->paddingx,
484 ob_rr_theme->menu_title_height -
485 2*ob_rr_theme->paddingy);
489 /* unlabeled separator */
490 XMoveResizeWindow(obt_display, self->text, 0, 0,
492 ob_rr_theme->menu_sep_width +
493 2*ob_rr_theme->menu_sep_paddingy);
495 a_sep->surface.parent = item_a;
496 a_sep->surface.parentx = 0;
497 a_sep->surface.parenty = 0;
498 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
499 a_sep->texture[i].data.lineart.x1 =
500 ob_rr_theme->menu_sep_paddingx;
501 a_sep->texture[i].data.lineart.y1 =
502 ob_rr_theme->menu_sep_paddingy + i;
503 a_sep->texture[i].data.lineart.x2 =
504 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
505 a_sep->texture[i].data.lineart.y2 =
506 ob_rr_theme->menu_sep_paddingy + i;
509 RrPaint(a_sep, self->text, self->area.width,
510 ob_rr_theme->menu_sep_width +
511 2*ob_rr_theme->menu_sep_paddingy);
515 g_assert_not_reached();
518 if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
519 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
520 self->entry->data.normal.icon)
524 XMoveResizeWindow(obt_display, self->icon,
525 PADDING, frame->item_margin.top,
526 ITEM_HEIGHT - frame->item_margin.top
527 - frame->item_margin.bottom,
528 ITEM_HEIGHT - frame->item_margin.top
529 - frame->item_margin.bottom);
531 clear = ob_rr_theme->a_clear_tex;
532 RrAppearanceClearTextures(clear);
533 clear->texture[0].type = RR_TEXTURE_IMAGE;
534 clear->texture[0].data.image.image =
535 self->entry->data.normal.icon;
536 clear->texture[0].data.image.alpha =
537 self->entry->data.normal.icon_alpha;
538 clear->surface.parent = item_a;
539 clear->surface.parentx = PADDING;
540 clear->surface.parenty = frame->item_margin.top;
541 RrPaint(clear, self->icon,
542 ITEM_HEIGHT - frame->item_margin.top
543 - frame->item_margin.bottom,
544 ITEM_HEIGHT - frame->item_margin.top
545 - frame->item_margin.bottom);
546 XMapWindow(obt_display, self->icon);
547 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
548 self->entry->data.normal.mask)
553 XMoveResizeWindow(obt_display, self->icon,
554 PADDING, frame->item_margin.top,
555 ITEM_HEIGHT - frame->item_margin.top
556 - frame->item_margin.bottom,
557 ITEM_HEIGHT - frame->item_margin.top
558 - frame->item_margin.bottom);
560 clear = ob_rr_theme->a_clear_tex;
561 RrAppearanceClearTextures(clear);
562 clear->texture[0].type = RR_TEXTURE_MASK;
563 clear->texture[0].data.mask.mask =
564 self->entry->data.normal.mask;
566 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
567 !self->entry->data.normal.enabled ?
569 (self == self->frame->selected ?
570 self->entry->data.normal.mask_disabled_selected_color :
571 self->entry->data.normal.mask_disabled_color) :
573 (self == self->frame->selected ?
574 self->entry->data.normal.mask_selected_color :
575 self->entry->data.normal.mask_normal_color));
576 clear->texture[0].data.mask.color = c;
578 clear->surface.parent = item_a;
579 clear->surface.parentx = PADDING;
580 clear->surface.parenty = frame->item_margin.top;
581 RrPaint(clear, self->icon,
582 ITEM_HEIGHT - frame->item_margin.top
583 - frame->item_margin.bottom,
584 ITEM_HEIGHT - frame->item_margin.top
585 - frame->item_margin.bottom);
586 XMapWindow(obt_display, self->icon);
588 XUnmapWindow(obt_display, self->icon);
590 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
591 RrAppearance *bullet_a;
592 XMoveResizeWindow(obt_display, self->bullet,
593 self->frame->text_x + self->frame->text_w -
594 ITEM_HEIGHT + PADDING, PADDING,
595 ITEM_HEIGHT - 2*PADDING,
596 ITEM_HEIGHT - 2*PADDING);
597 bullet_a = (self == self->frame->selected ?
598 ob_rr_theme->a_menu_bullet_selected :
599 ob_rr_theme->a_menu_bullet_normal);
600 bullet_a->surface.parent = item_a;
601 bullet_a->surface.parentx =
602 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
603 bullet_a->surface.parenty = PADDING;
604 RrPaint(bullet_a, self->bullet,
605 ITEM_HEIGHT - 2*PADDING,
606 ITEM_HEIGHT - 2*PADDING);
607 XMapWindow(obt_display, self->bullet);
609 XUnmapWindow(obt_display, self->bullet);
614 /*! this code is taken from the menu_frame_render. if that changes, this won't
616 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
617 gboolean first_entry,
626 t = self->entry->type;
628 /* this is the More... entry, it's NORMAL type */
629 t = OB_MENU_ENTRY_TYPE_NORMAL;
632 case OB_MENU_ENTRY_TYPE_NORMAL:
633 case OB_MENU_ENTRY_TYPE_SUBMENU:
634 h += ob_rr_theme->menu_font_height;
636 case OB_MENU_ENTRY_TYPE_SEPARATOR:
637 if (self->entry->data.separator.label != NULL) {
638 h += ob_rr_theme->menu_title_height +
639 (ob_rr_theme->mbwidth - PADDING) * 2;
641 /* if the first entry is a labeled separator, then make its border
642 overlap with the menu's outside border */
644 h -= ob_rr_theme->mbwidth;
645 /* if the last entry is a labeled separator, then make its border
646 overlap with the menu's outside border */
648 h -= ob_rr_theme->mbwidth;
650 h += ob_rr_theme->menu_sep_width +
651 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
659 void menu_frame_render(ObMenuFrame *self)
662 gint tw, th; /* temps */
664 gboolean has_icon = FALSE;
668 /* find text dimensions */
670 STRUT_SET(self->item_margin, 0, 0, 0, 0);
675 e = self->entries->data;
676 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
677 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
682 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
683 STRUT_SET(self->item_margin,
684 MAX(self->item_margin.left, l),
685 MAX(self->item_margin.top, t),
686 MAX(self->item_margin.right, r),
687 MAX(self->item_margin.bottom, b));
688 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
689 STRUT_SET(self->item_margin,
690 MAX(self->item_margin.left, l),
691 MAX(self->item_margin.top, t),
692 MAX(self->item_margin.right, r),
693 MAX(self->item_margin.bottom, b));
694 RrMargins(ob_rr_theme->a_menu_disabled, &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_disabled_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));
708 /* render the entries */
710 for (it = self->entries; it; it = g_list_next(it)) {
711 RrAppearance *text_a;
714 /* if the first entry is a labeled separator, then make its border
715 overlap with the menu's outside border */
716 if (it == self->entries &&
717 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
718 e->entry->data.separator.label)
720 h -= ob_rr_theme->mbwidth;
723 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
724 e->entry->data.separator.label)
726 e->border = ob_rr_theme->mbwidth;
729 RECT_SET_POINT(e->area, 0, h+e->border);
730 XMoveWindow(obt_display, e->window,
731 e->area.x-e->border, e->area.y-e->border);
732 XSetWindowBorderWidth(obt_display, e->window, e->border);
733 XSetWindowBorder(obt_display, e->window,
734 RrColorPixel(ob_rr_theme->menu_border_color));
736 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
737 !e->entry->data.normal.enabled ?
739 (e == self->selected ?
740 ob_rr_theme->a_menu_text_disabled_selected :
741 ob_rr_theme->a_menu_text_disabled) :
743 (e == self->selected ?
744 ob_rr_theme->a_menu_text_selected :
745 ob_rr_theme->a_menu_text_normal));
746 switch (e->entry->type) {
747 case OB_MENU_ENTRY_TYPE_NORMAL:
748 text_a->texture[0].data.text.string = e->entry->data.normal.label;
749 tw = RrMinWidth(text_a);
750 tw = MIN(tw, MAX_MENU_WIDTH);
751 th = ob_rr_theme->menu_font_height;
753 if (e->entry->data.normal.icon ||
754 e->entry->data.normal.mask)
757 case OB_MENU_ENTRY_TYPE_SUBMENU:
758 sub = e->entry->data.submenu.submenu;
759 text_a->texture[0].data.text.string = sub ? sub->title : "";
760 tw = RrMinWidth(text_a);
761 tw = MIN(tw, MAX_MENU_WIDTH);
762 th = ob_rr_theme->menu_font_height;
764 if (e->entry->data.normal.icon ||
765 e->entry->data.normal.mask)
768 tw += ITEM_HEIGHT - PADDING;
770 case OB_MENU_ENTRY_TYPE_SEPARATOR:
771 if (e->entry->data.separator.label != NULL) {
772 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
773 e->entry->data.separator.label;
774 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
775 2*ob_rr_theme->paddingx;
776 tw = MIN(tw, MAX_MENU_WIDTH);
777 th = ob_rr_theme->menu_title_height +
778 (ob_rr_theme->mbwidth - PADDING) *2;
781 th = ob_rr_theme->menu_sep_width +
782 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
786 g_assert_not_reached();
796 /* if the last entry is a labeled separator, then make its border
797 overlap with the menu's outside border */
798 it = g_list_last(self->entries);
799 e = it ? it->data : NULL;
800 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
801 e->entry->data.separator.label)
803 h -= ob_rr_theme->mbwidth;
806 self->text_x = PADDING;
811 w += ITEM_HEIGHT + PADDING;
812 self->text_x += ITEM_HEIGHT + PADDING;
819 XResizeWindow(obt_display, self->window, w, h);
823 RrPaint(self->a_items, self->window, w, h);
825 for (it = self->entries; it; it = g_list_next(it))
826 menu_entry_frame_render(it->data);
828 w += ob_rr_theme->mbwidth * 2;
829 h += ob_rr_theme->mbwidth * 2;
831 RECT_SET_SIZE(self->area, w, h);
836 static void menu_frame_update(ObMenuFrame *self)
842 menu_pipe_execute(self->menu);
843 menu_find_submenus(self->menu);
845 self->selected = NULL;
847 /* start at show_from */
848 mit = g_list_nth(self->menu->entries, self->show_from);
850 /* go through the menu's and frame's entries and connect the frame entries
851 to the menu entries */
852 for (fit = self->entries; mit && fit;
853 mit = g_list_next(mit), fit = g_list_next(fit))
855 ObMenuEntryFrame *f = fit->data;
856 f->entry = mit->data;
859 /* if there are more menu entries than in the frame, add them */
861 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
862 self->entries = g_list_append(self->entries, e);
863 mit = g_list_next(mit);
866 /* if there are more frame entries than menu entries then get rid of
869 GList *n = g_list_next(fit);
870 menu_entry_frame_free(fit->data);
871 self->entries = g_list_delete_link(self->entries, fit);
875 /* * make the menu fit on the screen */
877 /* calculate the height of the menu */
879 for (fit = self->entries; fit; fit = g_list_next(fit))
880 h += menu_entry_frame_get_height(fit->data,
881 fit == self->entries,
882 g_list_next(fit) == NULL);
883 /* add the border at the top and bottom */
884 h += ob_rr_theme->mbwidth * 2;
886 a = screen_physical_area_monitor(self->monitor);
890 gboolean last_entry = TRUE;
892 /* take the height of our More... entry into account */
893 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
895 /* start at the end of the entries */
896 flast = g_list_last(self->entries);
898 /* pull out all the entries from the frame that don't
899 fit on the screen, leaving at least 1 though */
900 while (h > a->height && g_list_previous(flast) != NULL) {
901 /* update the height, without this entry */
902 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
904 /* destroy the entry we're not displaying */
906 flast = g_list_previous(flast);
907 menu_entry_frame_free(tmp->data);
908 self->entries = g_list_delete_link(self->entries, tmp);
910 /* only the first one that we see is the last entry in the menu */
915 ObMenuEntry *more_entry;
916 ObMenuEntryFrame *more_frame;
917 /* make the More... menu entry frame which will display in this
919 if self->menu->more_menu is NULL that means that this is already
920 More... menu, so just use ourself.
922 more_entry = menu_get_more((self->menu->more_menu ?
923 self->menu->more_menu :
925 /* continue where we left off */
927 g_list_length(self->entries));
928 more_frame = menu_entry_frame_new(more_entry, self);
929 /* make it get deleted when the menu frame goes away */
930 menu_entry_unref(more_entry);
932 /* add our More... entry to the frame */
933 self->entries = g_list_append(self->entries, more_frame);
937 menu_frame_render(self);
940 static gboolean menu_frame_is_visible(ObMenuFrame *self)
942 return !!(g_list_find(menu_frame_visible, self));
945 static gboolean menu_frame_show(ObMenuFrame *self)
949 /* determine if the underlying menu is already visible */
950 for (it = menu_frame_visible; it; it = g_list_next(it)) {
951 ObMenuFrame *f = it->data;
952 if (f->menu == self->menu)
956 if (self->menu->update_func)
957 if (!self->menu->update_func(self, self->menu->data))
961 if (menu_frame_visible == NULL) {
962 /* no menus shown yet */
964 /* grab the pointer in such a way as to pass through "owner events"
965 so that we can get enter/leave notifies in the menu. */
966 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
968 if (!grab_keyboard()) {
974 menu_frame_update(self);
976 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
978 if (self->menu->show_func)
979 self->menu->show_func(self, self->menu->data);
984 gboolean menu_frame_show_topmenu(ObMenuFrame *self, GravityPoint pos,
985 gint monitor, gboolean mouse)
990 if (menu_frame_is_visible(self))
992 if (!menu_frame_show(self))
995 if (self->menu->place_func) {
998 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1000 menu_frame_place_topmenu(self, &pos, &x, &y, monitor);
1003 menu_frame_move(self, x, y);
1004 XMapWindow(obt_display, self->window);
1006 if (screen_pointer_pos(&px, &py)) {
1008 if (self->menu->warp) {
1009 x += self->area.width / 2;
1011 XWarpPointer(obt_display, None, obt_root(ob_screen), 0, 0, 0, 0, x, y);
1015 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1016 if (e && e->frame == self)
1025 /*! Stop hiding an open submenu.
1026 @child The OnMenuFrame of the submenu to be hidden
1028 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1030 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1031 submenu_hide_timer = 0;
1034 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1035 ObMenuEntryFrame *parent_entry)
1040 if (menu_frame_is_visible(self))
1043 self->monitor = parent->monitor;
1044 self->parent = parent;
1045 self->parent_entry = parent_entry;
1047 /* set up parent's child to be us */
1048 if ((parent->child) != self) {
1050 menu_frame_hide(parent->child);
1051 parent->child = self;
1052 parent->child_entry = parent_entry;
1055 if (!menu_frame_show(self)) {
1056 parent->child = NULL;
1057 parent->child_entry = NULL;
1061 menu_frame_place_submenu(self, &x, &y);
1062 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1065 /*try the other side */
1066 self->direction_right = !self->direction_right;
1067 menu_frame_place_submenu(self, &x, &y);
1068 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1070 menu_frame_move(self, x + dx, y + dy);
1072 XMapWindow(obt_display, self->window);
1074 if (screen_pointer_pos(&px, &py)) {
1075 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1076 if (e && e->frame == self)
1083 static void menu_frame_hide(ObMenuFrame *self)
1085 ObMenu *const menu = self->menu;
1086 GList *it = g_list_find(menu_frame_visible, self);
1087 gulong ignore_start;
1092 if (menu->hide_func)
1093 menu->hide_func(self, menu->data);
1096 menu_frame_hide(self->child);
1098 if (!self->parent && self->menu->warp)
1099 XWarpPointer(obt_display, None, obt_root(ob_screen), 0, 0, 0, 0, self->ox, self->oy);
1102 remove_submenu_hide_timeout(self);
1104 self->parent->child = NULL;
1105 self->parent->child_entry = NULL;
1107 self->parent = NULL;
1108 self->parent_entry = NULL;
1110 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1112 if (menu_frame_visible == NULL) {
1113 /* last menu shown */
1118 ignore_start = event_start_ignore_all_enters();
1119 XUnmapWindow(obt_display, self->window);
1120 event_end_ignore_all_enters(ignore_start);
1122 menu_frame_free(self);
1124 if (menu->cleanup_func)
1125 menu->cleanup_func(menu, menu->data);
1128 void menu_frame_hide_all(void)
1132 if (config_submenu_show_delay) {
1133 /* remove any submenu open requests */
1134 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1135 submenu_show_timer = 0;
1137 if ((it = g_list_last(menu_frame_visible)))
1138 menu_frame_hide(it->data);
1141 ObMenuFrame* menu_frame_under(gint x, gint y)
1143 ObMenuFrame *ret = NULL;
1146 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1147 ObMenuFrame *f = it->data;
1149 if (RECT_CONTAINS(f->area, x, y)) {
1157 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1160 ObMenuEntryFrame *ret = NULL;
1163 if ((frame = menu_frame_under(x, y))) {
1164 x -= ob_rr_theme->mbwidth + frame->area.x;
1165 y -= ob_rr_theme->mbwidth + frame->area.y;
1167 for (it = frame->entries; it; it = g_list_next(it)) {
1168 ObMenuEntryFrame *e = it->data;
1170 if (RECT_CONTAINS(e->area, x, y)) {
1179 static gboolean submenu_show_timeout(gpointer data)
1181 g_assert(menu_frame_visible);
1182 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1186 static gboolean submenu_hide_timeout(gpointer data)
1188 g_assert(menu_frame_visible);
1189 menu_frame_hide((ObMenuFrame*)data);
1193 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1196 ObMenuEntryFrame *old = self->selected;
1197 ObMenuFrame *oldchild = self->child;
1198 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1200 /* if the user selected a separator, ignore it and reselect what we had
1202 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1206 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1209 /* if the user left this menu but we have a submenu open, move the
1210 selection back to that submenu */
1211 if (!entry && oldchild_entry)
1212 entry = oldchild_entry;
1214 if (config_submenu_show_delay) {
1215 /* remove any submenu open requests */
1216 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1217 submenu_show_timer = 0;
1220 self->selected = entry;
1223 menu_entry_frame_render(old);
1225 if (oldchild_entry) {
1226 /* There is an open submenu */
1227 if (oldchild_entry == self->selected) {
1228 /* The open submenu has been reselected, so stop hiding the
1230 remove_submenu_hide_timeout(oldchild);
1232 else if (oldchild_entry == old) {
1233 /* The open submenu was selected and is no longer, so hide the
1235 if (immediate || config_submenu_hide_delay == 0)
1236 menu_frame_hide(oldchild);
1237 else if (config_submenu_hide_delay > 0) {
1238 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1239 submenu_hide_timer =
1240 g_timeout_add_full(G_PRIORITY_DEFAULT,
1241 config_submenu_hide_delay,
1242 submenu_hide_timeout, oldchild, NULL);
1247 if (self->selected) {
1248 menu_entry_frame_render(self->selected);
1250 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1251 /* only show if the submenu isn't already showing */
1252 if (oldchild_entry != self->selected) {
1253 if (immediate || config_submenu_show_delay == 0)
1254 menu_entry_frame_show_submenu(self->selected);
1255 else if (config_submenu_show_delay > 0) {
1256 if (submenu_show_timer)
1257 g_source_remove(submenu_show_timer);
1258 submenu_show_timer =
1259 g_timeout_add_full(G_PRIORITY_DEFAULT,
1260 config_submenu_show_delay,
1261 submenu_show_timeout,
1262 self->selected, NULL);
1265 /* hide the grandchildren of this menu. and move the cursor to
1267 else if (immediate && self->child && self->child->child) {
1268 menu_frame_hide(self->child->child);
1269 menu_frame_select(self->child, NULL, TRUE);
1275 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1279 if (!self->entry->data.submenu.submenu) return;
1281 f = menu_frame_new(self->entry->data.submenu.submenu,
1282 self->entry->data.submenu.show_from,
1283 self->frame->client);
1284 /* pass our direction on to our child */
1285 f->direction_right = self->frame->direction_right;
1287 if (!menu_frame_show_submenu(f, self->frame, self))
1291 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1293 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1294 self->entry->data.normal.enabled)
1296 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1298 ObMenuEntry *entry = self->entry;
1299 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1300 gpointer data = self->frame->menu->data;
1301 GSList *acts = self->entry->data.normal.actions;
1302 ObClient *client = self->frame->client;
1303 ObMenuFrame *frame = self->frame;
1304 guint mods = obt_keyboard_only_modmasks(state);
1306 /* release grabs before executing the shit */
1307 if (!(mods & ControlMask)) {
1308 event_cancel_all_key_grabs();
1313 func(entry, frame, client, state, data);
1315 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1316 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1320 void menu_frame_select_previous(ObMenuFrame *self)
1322 GList *it = NULL, *start;
1324 if (self->entries) {
1325 start = it = g_list_find(self->entries, self->selected);
1327 ObMenuEntryFrame *e;
1329 it = it ? g_list_previous(it) : g_list_last(self->entries);
1335 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1337 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1342 menu_frame_select(self, it ? it->data : NULL, FALSE);
1345 void menu_frame_select_next(ObMenuFrame *self)
1347 GList *it = NULL, *start;
1349 if (self->entries) {
1350 start = it = g_list_find(self->entries, self->selected);
1352 ObMenuEntryFrame *e;
1354 it = it ? g_list_next(it) : self->entries;
1360 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1362 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1367 menu_frame_select(self, it ? it->data : NULL, FALSE);
1370 void menu_frame_select_first(ObMenuFrame *self)
1374 if (self->entries) {
1375 for (it = self->entries; it; it = g_list_next(it)) {
1376 ObMenuEntryFrame *e = it->data;
1377 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1379 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1383 menu_frame_select(self, it ? it->data : NULL, FALSE);
1386 void menu_frame_select_last(ObMenuFrame *self)
1390 if (self->entries) {
1391 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1392 ObMenuEntryFrame *e = it->data;
1393 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1395 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1399 menu_frame_select(self, it ? it->data : NULL, FALSE);