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,
237 gboolean user_positioned)
241 screen_apply_gravity_point(x, y, self->area.width, self->area.height,
242 pos, screen_physical_area_monitor(monitor));
247 if (config_menu_middle) {
251 *y -= self->area.height / 2;
253 /* try to the right of the cursor */
254 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
255 self->direction_right = TRUE;
257 /* try to the left of the cursor */
258 myx = *x - self->area.width;
259 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
260 self->direction_right = FALSE;
263 /* if didnt fit on either side so just use what it says */
265 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
266 self->direction_right = TRUE;
276 /* try to the bottom right of the cursor */
277 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
278 self->direction_right = TRUE;
279 if (dx != 0 || dy != 0) {
280 /* try to the bottom left of the cursor */
281 myx = *x - self->area.width;
283 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
284 self->direction_right = FALSE;
286 if (dx != 0 || dy != 0) {
287 /* try to the top right of the cursor */
289 myy = *y - self->area.height;
290 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
291 self->direction_right = TRUE;
293 if (dx != 0 || dy != 0) {
294 /* try to the top left of the cursor */
295 myx = *x - self->area.width;
296 myy = *y - self->area.height;
297 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
298 self->direction_right = FALSE;
300 if (dx != 0 || dy != 0) {
301 /* if didnt fit on either side so just use what it says */
304 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
305 self->direction_right = TRUE;
312 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
314 gint overlapx, overlapy;
317 overlapx = ob_rr_theme->menu_overlap_x;
318 overlapy = ob_rr_theme->menu_overlap_y;
319 bwidth = ob_rr_theme->mbwidth;
321 if (self->direction_right)
322 *x = self->parent->area.x + self->parent->area.width -
325 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
327 *y = self->parent->area.y + self->parent_entry->area.y;
328 if (config_menu_middle)
329 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
334 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
337 const Rect *a = NULL;
338 Rect search = self->area;
339 gint pos, half, monitor;
342 RECT_SET_POINT(search, x, y);
345 monitor = self->parent->monitor;
347 monitor = screen_find_monitor(&search);
349 a = screen_physical_area_monitor(monitor);
351 half = g_list_length(self->entries) / 2;
352 pos = g_list_index(self->entries, self->selected);
354 /* if in the bottom half then check this stuff first, will keep the bottom
355 edge of the menu visible */
357 *dx = MAX(*dx, a->x - x);
358 *dy = MAX(*dy, a->y - y);
360 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
361 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
362 /* if in the top half then check this stuff last, will keep the top
363 edge of the menu visible */
365 *dx = MAX(*dx, a->x - x);
366 *dy = MAX(*dy, a->y - y);
370 static void menu_entry_frame_render(ObMenuEntryFrame *self)
372 RrAppearance *item_a, *text_a;
375 ObMenuFrame *frame = self->frame;
377 switch (self->entry->type) {
378 case OB_MENU_ENTRY_TYPE_NORMAL:
379 case OB_MENU_ENTRY_TYPE_SUBMENU:
380 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
381 !self->entry->data.normal.enabled ?
383 (self == self->frame->selected ?
384 ob_rr_theme->a_menu_disabled_selected :
385 ob_rr_theme->a_menu_disabled) :
387 (self == self->frame->selected ?
388 ob_rr_theme->a_menu_selected :
389 ob_rr_theme->a_menu_normal));
392 case OB_MENU_ENTRY_TYPE_SEPARATOR:
393 if (self->entry->data.separator.label) {
394 item_a = ob_rr_theme->a_menu_title;
395 th = ob_rr_theme->menu_title_height;
397 item_a = ob_rr_theme->a_menu_normal;
398 th = ob_rr_theme->menu_sep_width +
399 2*ob_rr_theme->menu_sep_paddingy;
403 g_assert_not_reached();
406 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
407 XResizeWindow(obt_display, self->window,
408 self->area.width, self->area.height);
409 item_a->surface.parent = self->frame->a_items;
410 item_a->surface.parentx = self->area.x;
411 item_a->surface.parenty = self->area.y;
412 RrPaint(item_a, self->window, self->area.width, self->area.height);
414 switch (self->entry->type) {
415 case OB_MENU_ENTRY_TYPE_NORMAL:
416 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
417 !self->entry->data.normal.enabled ?
419 (self == self->frame->selected ?
420 ob_rr_theme->a_menu_text_disabled_selected :
421 ob_rr_theme->a_menu_text_disabled) :
423 (self == self->frame->selected ?
424 ob_rr_theme->a_menu_text_selected :
425 ob_rr_theme->a_menu_text_normal));
426 text_a->texture[0].data.text.string = self->entry->data.normal.label;
427 if (self->entry->data.normal.shortcut &&
428 (self->frame->menu->show_all_shortcuts ||
429 self->entry->data.normal.shortcut_always_show ||
430 self->entry->data.normal.shortcut_position > 0))
432 text_a->texture[0].data.text.shortcut = TRUE;
433 text_a->texture[0].data.text.shortcut_pos =
434 self->entry->data.normal.shortcut_position;
436 text_a->texture[0].data.text.shortcut = FALSE;
438 case OB_MENU_ENTRY_TYPE_SUBMENU:
439 text_a = (self == self->frame->selected ?
440 ob_rr_theme->a_menu_text_selected :
441 ob_rr_theme->a_menu_text_normal);
442 sub = self->entry->data.submenu.submenu;
443 text_a->texture[0].data.text.string = sub ? sub->title : "";
444 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
445 sub->shortcut_always_show ||
446 sub->shortcut_position > 0))
448 text_a->texture[0].data.text.shortcut = TRUE;
449 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
451 text_a->texture[0].data.text.shortcut = FALSE;
453 case OB_MENU_ENTRY_TYPE_SEPARATOR:
454 if (self->entry->data.separator.label != NULL) {
455 text_a = ob_rr_theme->a_menu_text_title;
456 text_a->texture[0].data.text.string =
457 self->entry->data.separator.label;
460 text_a = ob_rr_theme->a_menu_text_normal;
463 g_assert_not_reached();
466 switch (self->entry->type) {
467 case OB_MENU_ENTRY_TYPE_NORMAL:
468 XMoveResizeWindow(obt_display, self->text,
469 self->frame->text_x, PADDING,
471 ITEM_HEIGHT - 2*PADDING);
472 text_a->surface.parent = item_a;
473 text_a->surface.parentx = self->frame->text_x;
474 text_a->surface.parenty = PADDING;
475 RrPaint(text_a, self->text, self->frame->text_w,
476 ITEM_HEIGHT - 2*PADDING);
478 case OB_MENU_ENTRY_TYPE_SUBMENU:
479 XMoveResizeWindow(obt_display, self->text,
480 self->frame->text_x, PADDING,
481 self->frame->text_w - ITEM_HEIGHT,
482 ITEM_HEIGHT - 2*PADDING);
483 text_a->surface.parent = item_a;
484 text_a->surface.parentx = self->frame->text_x;
485 text_a->surface.parenty = PADDING;
486 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
487 ITEM_HEIGHT - 2*PADDING);
489 case OB_MENU_ENTRY_TYPE_SEPARATOR:
490 if (self->entry->data.separator.label != NULL) {
491 /* labeled separator */
492 XMoveResizeWindow(obt_display, self->text,
493 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
494 self->area.width - 2*ob_rr_theme->paddingx,
495 ob_rr_theme->menu_title_height -
496 2*ob_rr_theme->paddingy);
497 text_a->surface.parent = item_a;
498 text_a->surface.parentx = ob_rr_theme->paddingx;
499 text_a->surface.parenty = ob_rr_theme->paddingy;
500 RrPaint(text_a, self->text,
501 self->area.width - 2*ob_rr_theme->paddingx,
502 ob_rr_theme->menu_title_height -
503 2*ob_rr_theme->paddingy);
507 /* unlabeled separator */
508 XMoveResizeWindow(obt_display, self->text, 0, 0,
510 ob_rr_theme->menu_sep_width +
511 2*ob_rr_theme->menu_sep_paddingy);
513 a_sep->surface.parent = item_a;
514 a_sep->surface.parentx = 0;
515 a_sep->surface.parenty = 0;
516 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
517 a_sep->texture[i].data.lineart.x1 =
518 ob_rr_theme->menu_sep_paddingx;
519 a_sep->texture[i].data.lineart.y1 =
520 ob_rr_theme->menu_sep_paddingy + i;
521 a_sep->texture[i].data.lineart.x2 =
522 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
523 a_sep->texture[i].data.lineart.y2 =
524 ob_rr_theme->menu_sep_paddingy + i;
527 RrPaint(a_sep, self->text, self->area.width,
528 ob_rr_theme->menu_sep_width +
529 2*ob_rr_theme->menu_sep_paddingy);
533 g_assert_not_reached();
536 if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
537 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
538 self->entry->data.normal.icon)
542 XMoveResizeWindow(obt_display, self->icon,
543 PADDING, frame->item_margin.top,
544 ITEM_HEIGHT - frame->item_margin.top
545 - frame->item_margin.bottom,
546 ITEM_HEIGHT - frame->item_margin.top
547 - frame->item_margin.bottom);
549 clear = ob_rr_theme->a_clear_tex;
550 RrAppearanceClearTextures(clear);
551 clear->texture[0].type = RR_TEXTURE_IMAGE;
552 clear->texture[0].data.image.image =
553 self->entry->data.normal.icon;
554 clear->texture[0].data.image.alpha =
555 self->entry->data.normal.icon_alpha;
556 clear->surface.parent = item_a;
557 clear->surface.parentx = PADDING;
558 clear->surface.parenty = frame->item_margin.top;
559 RrPaint(clear, self->icon,
560 ITEM_HEIGHT - frame->item_margin.top
561 - frame->item_margin.bottom,
562 ITEM_HEIGHT - frame->item_margin.top
563 - frame->item_margin.bottom);
564 XMapWindow(obt_display, self->icon);
565 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
566 self->entry->data.normal.mask)
571 XMoveResizeWindow(obt_display, self->icon,
572 PADDING, frame->item_margin.top,
573 ITEM_HEIGHT - frame->item_margin.top
574 - frame->item_margin.bottom,
575 ITEM_HEIGHT - frame->item_margin.top
576 - frame->item_margin.bottom);
578 clear = ob_rr_theme->a_clear_tex;
579 RrAppearanceClearTextures(clear);
580 clear->texture[0].type = RR_TEXTURE_MASK;
581 clear->texture[0].data.mask.mask =
582 self->entry->data.normal.mask;
584 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
585 !self->entry->data.normal.enabled ?
587 (self == self->frame->selected ?
588 self->entry->data.normal.mask_disabled_selected_color :
589 self->entry->data.normal.mask_disabled_color) :
591 (self == self->frame->selected ?
592 self->entry->data.normal.mask_selected_color :
593 self->entry->data.normal.mask_normal_color));
594 clear->texture[0].data.mask.color = c;
596 clear->surface.parent = item_a;
597 clear->surface.parentx = PADDING;
598 clear->surface.parenty = frame->item_margin.top;
599 RrPaint(clear, self->icon,
600 ITEM_HEIGHT - frame->item_margin.top
601 - frame->item_margin.bottom,
602 ITEM_HEIGHT - frame->item_margin.top
603 - frame->item_margin.bottom);
604 XMapWindow(obt_display, self->icon);
606 XUnmapWindow(obt_display, self->icon);
608 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
609 RrAppearance *bullet_a;
610 XMoveResizeWindow(obt_display, self->bullet,
611 self->frame->text_x + self->frame->text_w -
612 ITEM_HEIGHT + PADDING, PADDING,
613 ITEM_HEIGHT - 2*PADDING,
614 ITEM_HEIGHT - 2*PADDING);
615 bullet_a = (self == self->frame->selected ?
616 ob_rr_theme->a_menu_bullet_selected :
617 ob_rr_theme->a_menu_bullet_normal);
618 bullet_a->surface.parent = item_a;
619 bullet_a->surface.parentx =
620 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
621 bullet_a->surface.parenty = PADDING;
622 RrPaint(bullet_a, self->bullet,
623 ITEM_HEIGHT - 2*PADDING,
624 ITEM_HEIGHT - 2*PADDING);
625 XMapWindow(obt_display, self->bullet);
627 XUnmapWindow(obt_display, self->bullet);
632 /*! this code is taken from the menu_frame_render. if that changes, this won't
634 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
635 gboolean first_entry,
644 t = self->entry->type;
646 /* this is the More... entry, it's NORMAL type */
647 t = OB_MENU_ENTRY_TYPE_NORMAL;
650 case OB_MENU_ENTRY_TYPE_NORMAL:
651 case OB_MENU_ENTRY_TYPE_SUBMENU:
652 h += ob_rr_theme->menu_font_height;
654 case OB_MENU_ENTRY_TYPE_SEPARATOR:
655 if (self->entry->data.separator.label != NULL) {
656 h += ob_rr_theme->menu_title_height +
657 (ob_rr_theme->mbwidth - PADDING) * 2;
659 /* if the first entry is a labeled separator, then make its border
660 overlap with the menu's outside border */
662 h -= ob_rr_theme->mbwidth;
663 /* if the last entry is a labeled separator, then make its border
664 overlap with the menu's outside border */
666 h -= ob_rr_theme->mbwidth;
668 h += ob_rr_theme->menu_sep_width +
669 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
677 void menu_frame_render(ObMenuFrame *self)
680 gint tw, th; /* temps */
682 gboolean has_icon = FALSE;
686 /* find text dimensions */
688 STRUT_SET(self->item_margin, 0, 0, 0, 0);
693 e = self->entries->data;
694 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
695 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
700 RrMargins(ob_rr_theme->a_menu_normal, &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_selected, &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, &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));
718 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
719 STRUT_SET(self->item_margin,
720 MAX(self->item_margin.left, l),
721 MAX(self->item_margin.top, t),
722 MAX(self->item_margin.right, r),
723 MAX(self->item_margin.bottom, b));
726 /* render the entries */
728 for (it = self->entries; it; it = g_list_next(it)) {
729 RrAppearance *text_a;
732 /* if the first entry is a labeled separator, then make its border
733 overlap with the menu's outside border */
734 if (it == self->entries &&
735 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
736 e->entry->data.separator.label)
738 h -= ob_rr_theme->mbwidth;
741 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
742 e->entry->data.separator.label)
744 e->border = ob_rr_theme->mbwidth;
747 RECT_SET_POINT(e->area, 0, h+e->border);
748 XMoveWindow(obt_display, e->window,
749 e->area.x-e->border, e->area.y-e->border);
750 XSetWindowBorderWidth(obt_display, e->window, e->border);
751 XSetWindowBorder(obt_display, e->window,
752 RrColorPixel(ob_rr_theme->menu_border_color));
754 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
755 !e->entry->data.normal.enabled ?
757 (e == self->selected ?
758 ob_rr_theme->a_menu_text_disabled_selected :
759 ob_rr_theme->a_menu_text_disabled) :
761 (e == self->selected ?
762 ob_rr_theme->a_menu_text_selected :
763 ob_rr_theme->a_menu_text_normal));
764 switch (e->entry->type) {
765 case OB_MENU_ENTRY_TYPE_NORMAL:
766 text_a->texture[0].data.text.string = e->entry->data.normal.label;
767 tw = RrMinWidth(text_a);
768 tw = MIN(tw, MAX_MENU_WIDTH);
769 th = ob_rr_theme->menu_font_height;
771 if (e->entry->data.normal.icon ||
772 e->entry->data.normal.mask)
775 case OB_MENU_ENTRY_TYPE_SUBMENU:
776 sub = e->entry->data.submenu.submenu;
777 text_a->texture[0].data.text.string = sub ? sub->title : "";
778 tw = RrMinWidth(text_a);
779 tw = MIN(tw, MAX_MENU_WIDTH);
780 th = ob_rr_theme->menu_font_height;
782 if (e->entry->data.normal.icon ||
783 e->entry->data.normal.mask)
786 tw += ITEM_HEIGHT - PADDING;
788 case OB_MENU_ENTRY_TYPE_SEPARATOR:
789 if (e->entry->data.separator.label != NULL) {
790 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
791 e->entry->data.separator.label;
792 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
793 2*ob_rr_theme->paddingx;
794 tw = MIN(tw, MAX_MENU_WIDTH);
795 th = ob_rr_theme->menu_title_height +
796 (ob_rr_theme->mbwidth - PADDING) *2;
799 th = ob_rr_theme->menu_sep_width +
800 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
804 g_assert_not_reached();
812 /* if the last entry is a labeled separator, then make its border
813 overlap with the menu's outside border */
814 it = g_list_last(self->entries);
815 e = it ? it->data : NULL;
816 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
817 e->entry->data.separator.label)
819 h -= ob_rr_theme->mbwidth;
822 self->text_x = PADDING;
827 w += ITEM_HEIGHT + PADDING;
828 self->text_x += ITEM_HEIGHT + PADDING;
835 XResizeWindow(obt_display, self->window, w, h);
839 RrPaint(self->a_items, self->window, w, h);
841 for (it = self->entries; it; it = g_list_next(it))
842 menu_entry_frame_render(it->data);
844 w += ob_rr_theme->mbwidth * 2;
845 h += ob_rr_theme->mbwidth * 2;
847 RECT_SET_SIZE(self->area, w, h);
852 static void menu_frame_update(ObMenuFrame *self)
858 menu_pipe_execute(self->menu);
859 menu_find_submenus(self->menu);
861 self->selected = NULL;
863 /* start at show_from */
864 mit = g_list_nth(self->menu->entries, self->show_from);
866 /* go through the menu's and frame's entries and connect the frame entries
867 to the menu entries */
868 for (fit = self->entries; mit && fit;
869 mit = g_list_next(mit), fit = g_list_next(fit))
871 ObMenuEntryFrame *f = fit->data;
872 f->entry = mit->data;
875 /* if there are more menu entries than in the frame, add them */
877 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
878 self->entries = g_list_append(self->entries, e);
879 mit = g_list_next(mit);
882 /* if there are more frame entries than menu entries then get rid of
885 GList *n = g_list_next(fit);
886 menu_entry_frame_free(fit->data);
887 self->entries = g_list_delete_link(self->entries, fit);
891 /* * make the menu fit on the screen */
893 /* calculate the height of the menu */
895 for (fit = self->entries; fit; fit = g_list_next(fit))
896 h += menu_entry_frame_get_height(fit->data,
897 fit == self->entries,
898 g_list_next(fit) == NULL);
899 /* add the border at the top and bottom */
900 h += ob_rr_theme->mbwidth * 2;
902 a = screen_physical_area_monitor(self->monitor);
906 gboolean last_entry = TRUE;
908 /* take the height of our More... entry into account */
909 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
911 /* start at the end of the entries */
912 flast = g_list_last(self->entries);
914 /* pull out all the entries from the frame that don't
915 fit on the screen, leaving at least 1 though */
916 while (h > a->height && g_list_previous(flast) != NULL) {
917 /* update the height, without this entry */
918 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
920 /* destroy the entry we're not displaying */
922 flast = g_list_previous(flast);
923 menu_entry_frame_free(tmp->data);
924 self->entries = g_list_delete_link(self->entries, tmp);
926 /* only the first one that we see is the last entry in the menu */
931 ObMenuEntry *more_entry;
932 ObMenuEntryFrame *more_frame;
933 /* make the More... menu entry frame which will display in this
935 if self->menu->more_menu is NULL that means that this is already
936 More... menu, so just use ourself.
938 more_entry = menu_get_more((self->menu->more_menu ?
939 self->menu->more_menu :
941 /* continue where we left off */
943 g_list_length(self->entries));
944 more_frame = menu_entry_frame_new(more_entry, self);
945 /* make it get deleted when the menu frame goes away */
946 menu_entry_unref(more_entry);
948 /* add our More... entry to the frame */
949 self->entries = g_list_append(self->entries, more_frame);
953 menu_frame_render(self);
956 static gboolean menu_frame_is_visible(ObMenuFrame *self)
958 return !!(g_list_find(menu_frame_visible, self));
961 static gboolean menu_frame_show(ObMenuFrame *self)
965 /* determine if the underlying menu is already visible */
966 for (it = menu_frame_visible; it; it = g_list_next(it)) {
967 ObMenuFrame *f = it->data;
968 if (f->menu == self->menu)
972 if (self->menu->update_func)
973 if (!self->menu->update_func(self, self->menu->data))
977 if (menu_frame_visible == NULL) {
978 /* no menus shown yet */
980 /* grab the pointer in such a way as to pass through "owner events"
981 so that we can get enter/leave notifies in the menu. */
982 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
984 if (!grab_keyboard()) {
990 menu_frame_update(self);
992 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
994 if (self->menu->show_func)
995 self->menu->show_func(self, self->menu->data);
1000 gboolean menu_frame_show_topmenu(ObMenuFrame *self, GravityPoint pos,
1001 gint monitor, gboolean mouse,
1002 gboolean user_positioned)
1007 if (menu_frame_is_visible(self))
1009 if (!menu_frame_show(self))
1012 if (self->menu->place_func) {
1015 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1017 menu_frame_place_topmenu(self, &pos, &x, &y, monitor,
1021 menu_frame_move(self, x, y);
1023 XMapWindow(obt_display, self->window);
1025 if (screen_pointer_pos(&px, &py)) {
1026 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1027 if (e && e->frame == self)
1034 /*! Stop hiding an open submenu.
1035 @child The OnMenuFrame of the submenu to be hidden
1037 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1039 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1040 submenu_hide_timer = 0;
1043 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1044 ObMenuEntryFrame *parent_entry)
1049 if (menu_frame_is_visible(self))
1052 self->monitor = parent->monitor;
1053 self->parent = parent;
1054 self->parent_entry = parent_entry;
1056 /* set up parent's child to be us */
1057 if ((parent->child) != self) {
1059 menu_frame_hide(parent->child);
1060 parent->child = self;
1061 parent->child_entry = parent_entry;
1064 if (!menu_frame_show(self)) {
1065 parent->child = NULL;
1066 parent->child_entry = NULL;
1070 menu_frame_place_submenu(self, &x, &y);
1071 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1074 /*try the other side */
1075 self->direction_right = !self->direction_right;
1076 menu_frame_place_submenu(self, &x, &y);
1077 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1079 menu_frame_move(self, x + dx, y + dy);
1081 XMapWindow(obt_display, self->window);
1083 if (screen_pointer_pos(&px, &py)) {
1084 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1085 if (e && e->frame == self)
1092 static void menu_frame_hide(ObMenuFrame *self)
1094 ObMenu *const menu = self->menu;
1095 GList *it = g_list_find(menu_frame_visible, self);
1096 gulong ignore_start;
1101 if (menu->hide_func)
1102 menu->hide_func(self, menu->data);
1105 menu_frame_hide(self->child);
1108 remove_submenu_hide_timeout(self);
1110 self->parent->child = NULL;
1111 self->parent->child_entry = NULL;
1113 self->parent = NULL;
1114 self->parent_entry = NULL;
1116 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1118 if (menu_frame_visible == NULL) {
1119 /* last menu shown */
1124 ignore_start = event_start_ignore_all_enters();
1125 XUnmapWindow(obt_display, self->window);
1126 event_end_ignore_all_enters(ignore_start);
1128 menu_frame_free(self);
1130 if (menu->cleanup_func)
1131 menu->cleanup_func(menu, menu->data);
1134 void menu_frame_hide_all(void)
1138 if (config_submenu_show_delay) {
1139 /* remove any submenu open requests */
1140 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1141 submenu_show_timer = 0;
1143 if ((it = g_list_last(menu_frame_visible)))
1144 menu_frame_hide(it->data);
1147 ObMenuFrame* menu_frame_under(gint x, gint y)
1149 ObMenuFrame *ret = NULL;
1152 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1153 ObMenuFrame *f = it->data;
1155 if (RECT_CONTAINS(f->area, x, y)) {
1163 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1166 ObMenuEntryFrame *ret = NULL;
1169 if ((frame = menu_frame_under(x, y))) {
1170 x -= ob_rr_theme->mbwidth + frame->area.x;
1171 y -= ob_rr_theme->mbwidth + frame->area.y;
1173 for (it = frame->entries; it; it = g_list_next(it)) {
1174 ObMenuEntryFrame *e = it->data;
1176 if (RECT_CONTAINS(e->area, x, y)) {
1185 static gboolean submenu_show_timeout(gpointer data)
1187 g_assert(menu_frame_visible);
1188 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1192 static gboolean submenu_hide_timeout(gpointer data)
1194 g_assert(menu_frame_visible);
1195 menu_frame_hide((ObMenuFrame*)data);
1199 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1202 ObMenuEntryFrame *old = self->selected;
1203 ObMenuFrame *oldchild = self->child;
1204 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1206 /* if the user selected a separator, ignore it and reselect what we had
1208 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1212 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1215 /* if the user left this menu but we have a submenu open, move the
1216 selection back to that submenu */
1217 if (!entry && oldchild_entry)
1218 entry = oldchild_entry;
1220 if (config_submenu_show_delay) {
1221 /* remove any submenu open requests */
1222 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1223 submenu_show_timer = 0;
1226 self->selected = entry;
1229 menu_entry_frame_render(old);
1231 if (oldchild_entry) {
1232 /* There is an open submenu */
1233 if (oldchild_entry == self->selected) {
1234 /* The open submenu has been reselected, so stop hiding the
1236 remove_submenu_hide_timeout(oldchild);
1238 else if (oldchild_entry == old) {
1239 /* The open submenu was selected and is no longer, so hide the
1241 if (immediate || config_submenu_hide_delay == 0)
1242 menu_frame_hide(oldchild);
1243 else if (config_submenu_hide_delay > 0) {
1244 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1245 submenu_hide_timer =
1246 g_timeout_add_full(G_PRIORITY_DEFAULT,
1247 config_submenu_hide_delay,
1248 submenu_hide_timeout, oldchild, NULL);
1253 if (self->selected) {
1254 menu_entry_frame_render(self->selected);
1256 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1257 /* only show if the submenu isn't already showing */
1258 if (oldchild_entry != self->selected) {
1259 if (immediate || config_submenu_show_delay == 0)
1260 menu_entry_frame_show_submenu(self->selected);
1261 else if (config_submenu_show_delay > 0) {
1262 if (submenu_show_timer)
1263 g_source_remove(submenu_show_timer);
1264 submenu_show_timer =
1265 g_timeout_add_full(G_PRIORITY_DEFAULT,
1266 config_submenu_show_delay,
1267 submenu_show_timeout,
1268 self->selected, NULL);
1271 /* hide the grandchildren of this menu. and move the cursor to
1273 else if (immediate && self->child && self->child->child) {
1274 menu_frame_hide(self->child->child);
1275 menu_frame_select(self->child, NULL, TRUE);
1281 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1285 if (!self->entry->data.submenu.submenu) return;
1287 f = menu_frame_new(self->entry->data.submenu.submenu,
1288 self->entry->data.submenu.show_from,
1289 self->frame->client);
1290 /* pass our direction on to our child */
1291 f->direction_right = self->frame->direction_right;
1293 if (!menu_frame_show_submenu(f, self->frame, self))
1297 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1299 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1300 self->entry->data.normal.enabled)
1302 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1304 ObMenuEntry *entry = self->entry;
1305 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1306 gpointer data = self->frame->menu->data;
1307 GSList *acts = self->entry->data.normal.actions;
1308 ObClient *client = self->frame->client;
1309 ObMenuFrame *frame = self->frame;
1310 guint mods = obt_keyboard_only_modmasks(state);
1312 /* release grabs before executing the shit */
1313 if (!(mods & ControlMask)) {
1314 event_cancel_all_key_grabs();
1319 func(entry, frame, client, state, data);
1321 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1322 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1326 void menu_frame_select_previous(ObMenuFrame *self)
1328 GList *it = NULL, *start;
1330 if (self->entries) {
1331 start = it = g_list_find(self->entries, self->selected);
1333 ObMenuEntryFrame *e;
1335 it = it ? g_list_previous(it) : g_list_last(self->entries);
1341 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1343 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1348 menu_frame_select(self, it ? it->data : NULL, FALSE);
1351 void menu_frame_select_next(ObMenuFrame *self)
1353 GList *it = NULL, *start;
1355 if (self->entries) {
1356 start = it = g_list_find(self->entries, self->selected);
1358 ObMenuEntryFrame *e;
1360 it = it ? g_list_next(it) : self->entries;
1366 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1368 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1373 menu_frame_select(self, it ? it->data : NULL, FALSE);
1376 void menu_frame_select_first(ObMenuFrame *self)
1380 if (self->entries) {
1381 for (it = self->entries; it; it = g_list_next(it)) {
1382 ObMenuEntryFrame *e = it->data;
1383 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1385 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1389 menu_frame_select(self, it ? it->data : NULL, FALSE);
1392 void menu_frame_select_last(ObMenuFrame *self)
1396 if (self->entries) {
1397 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1398 ObMenuEntryFrame *e = it->data;
1399 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1401 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1405 menu_frame_select(self, it ? it->data : NULL, FALSE);