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 calc_position(ObMenuFrame *self, GravityPoint *position,
236 gint *x, gint *y, gint monitor)
238 const Rect *area = screen_physical_area_monitor(monitor);
240 if (position->x.center)
241 *x = area->width / 2 - self->area.width / 2;
243 *x = position->x.pos;
244 if (position->x.denom)
245 *x = (*x * area->width) / position->x.denom;
246 if (position->x.opposite)
247 *x = area->width - self->area.width - *x;
250 if (position->y.center)
251 *y = area->height / 2 - self->area.height / 2;
253 *y = position->y.pos;
254 if (position->y.denom)
255 *y = (*y * area->height) / position->y.denom;
256 if (position->y.opposite)
257 *y = area->height - self->area.height - *y;
264 static void menu_frame_place_topmenu(ObMenuFrame *self, GravityPoint *pos,
265 gint *x, gint *y, gint monitor)
269 calc_position(self, pos, x, y, monitor);
271 if (config_menu_middle) {
273 *x -= self->area.width / 2;
275 /* try below the cursor */
276 menu_frame_move_on_screen(self, *x, *y, &dx, &dy);
277 self->direction_right = TRUE;
286 /* try to the bottom right of the cursor */
287 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
288 self->direction_right = TRUE;
289 if (dx != 0 || dy != 0) {
290 /* try to the bottom left of the cursor */
291 myx = *x - self->area.width;
293 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
294 self->direction_right = FALSE;
296 if (dx != 0 || dy != 0) {
297 /* try to the top right of the cursor */
299 myy = *y - self->area.height;
300 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
301 self->direction_right = TRUE;
303 if (dx != 0 || dy != 0) {
304 /* try to the top left of the cursor */
305 myx = *x - self->area.width;
306 myy = *y - self->area.height;
307 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
308 self->direction_right = FALSE;
310 if (dx != 0 || dy != 0) {
311 /* if didnt fit on either side so just use what it says */
314 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
315 self->direction_right = TRUE;
322 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
324 gint overlapx, overlapy;
327 overlapx = ob_rr_theme->menu_overlap_x;
328 overlapy = ob_rr_theme->menu_overlap_y;
329 bwidth = ob_rr_theme->mbwidth;
331 if (self->direction_right)
332 *x = self->parent->area.x + self->parent->area.width -
335 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
337 *y = self->parent->area.y + self->parent_entry->area.y;
338 if (config_menu_middle)
339 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
344 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
347 const Rect *a = NULL;
348 Rect search = self->area;
349 gint pos, half, monitor;
352 RECT_SET_POINT(search, x, y);
355 monitor = self->parent->monitor;
357 monitor = screen_find_monitor(&search);
359 a = screen_physical_area_monitor(monitor);
361 half = g_list_length(self->entries) / 2;
362 pos = g_list_index(self->entries, self->selected);
364 /* if in the bottom half then check this stuff first, will keep the bottom
365 edge of the menu visible */
367 *dx = MAX(*dx, a->x - x);
368 *dy = MAX(*dy, a->y - y);
370 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
371 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
372 /* if in the top half then check this stuff last, will keep the top
373 edge of the menu visible */
375 *dx = MAX(*dx, a->x - x);
376 *dy = MAX(*dy, a->y - y);
380 static void menu_entry_frame_render(ObMenuEntryFrame *self)
382 RrAppearance *item_a, *text_a;
385 ObMenuFrame *frame = self->frame;
387 switch (self->entry->type) {
388 case OB_MENU_ENTRY_TYPE_NORMAL:
389 case OB_MENU_ENTRY_TYPE_SUBMENU:
390 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
391 !self->entry->data.normal.enabled ?
393 (self == self->frame->selected ?
394 ob_rr_theme->a_menu_disabled_selected :
395 ob_rr_theme->a_menu_disabled) :
397 (self == self->frame->selected ?
398 ob_rr_theme->a_menu_selected :
399 ob_rr_theme->a_menu_normal));
402 case OB_MENU_ENTRY_TYPE_SEPARATOR:
403 if (self->entry->data.separator.label) {
404 item_a = ob_rr_theme->a_menu_title;
405 th = ob_rr_theme->menu_title_height;
407 item_a = ob_rr_theme->a_menu_normal;
408 th = ob_rr_theme->menu_sep_width +
409 2*ob_rr_theme->menu_sep_paddingy;
413 g_assert_not_reached();
416 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
417 XResizeWindow(obt_display, self->window,
418 self->area.width, self->area.height);
419 item_a->surface.parent = self->frame->a_items;
420 item_a->surface.parentx = self->area.x;
421 item_a->surface.parenty = self->area.y;
422 RrPaint(item_a, self->window, self->area.width, self->area.height);
424 switch (self->entry->type) {
425 case OB_MENU_ENTRY_TYPE_NORMAL:
426 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
427 !self->entry->data.normal.enabled ?
429 (self == self->frame->selected ?
430 ob_rr_theme->a_menu_text_disabled_selected :
431 ob_rr_theme->a_menu_text_disabled) :
433 (self == self->frame->selected ?
434 ob_rr_theme->a_menu_text_selected :
435 ob_rr_theme->a_menu_text_normal));
436 text_a->texture[0].data.text.string = self->entry->data.normal.label;
437 if (self->entry->data.normal.shortcut &&
438 (self->frame->menu->show_all_shortcuts ||
439 self->entry->data.normal.shortcut_always_show ||
440 self->entry->data.normal.shortcut_position > 0))
442 text_a->texture[0].data.text.shortcut = TRUE;
443 text_a->texture[0].data.text.shortcut_pos =
444 self->entry->data.normal.shortcut_position;
446 text_a->texture[0].data.text.shortcut = FALSE;
448 case OB_MENU_ENTRY_TYPE_SUBMENU:
449 text_a = (self == self->frame->selected ?
450 ob_rr_theme->a_menu_text_selected :
451 ob_rr_theme->a_menu_text_normal);
452 sub = self->entry->data.submenu.submenu;
453 text_a->texture[0].data.text.string = sub ? sub->title : "";
454 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
455 sub->shortcut_always_show ||
456 sub->shortcut_position > 0))
458 text_a->texture[0].data.text.shortcut = TRUE;
459 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
461 text_a->texture[0].data.text.shortcut = FALSE;
463 case OB_MENU_ENTRY_TYPE_SEPARATOR:
464 if (self->entry->data.separator.label != NULL) {
465 text_a = ob_rr_theme->a_menu_text_title;
466 text_a->texture[0].data.text.string =
467 self->entry->data.separator.label;
470 text_a = ob_rr_theme->a_menu_text_normal;
473 g_assert_not_reached();
476 switch (self->entry->type) {
477 case OB_MENU_ENTRY_TYPE_NORMAL:
478 XMoveResizeWindow(obt_display, self->text,
479 self->frame->text_x, PADDING,
481 ITEM_HEIGHT - 2*PADDING);
482 text_a->surface.parent = item_a;
483 text_a->surface.parentx = self->frame->text_x;
484 text_a->surface.parenty = PADDING;
485 RrPaint(text_a, self->text, self->frame->text_w,
486 ITEM_HEIGHT - 2*PADDING);
488 case OB_MENU_ENTRY_TYPE_SUBMENU:
489 XMoveResizeWindow(obt_display, self->text,
490 self->frame->text_x, PADDING,
491 self->frame->text_w - ITEM_HEIGHT,
492 ITEM_HEIGHT - 2*PADDING);
493 text_a->surface.parent = item_a;
494 text_a->surface.parentx = self->frame->text_x;
495 text_a->surface.parenty = PADDING;
496 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
497 ITEM_HEIGHT - 2*PADDING);
499 case OB_MENU_ENTRY_TYPE_SEPARATOR:
500 if (self->entry->data.separator.label != NULL) {
501 /* labeled separator */
502 XMoveResizeWindow(obt_display, self->text,
503 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
504 self->area.width - 2*ob_rr_theme->paddingx,
505 ob_rr_theme->menu_title_height -
506 2*ob_rr_theme->paddingy);
507 text_a->surface.parent = item_a;
508 text_a->surface.parentx = ob_rr_theme->paddingx;
509 text_a->surface.parenty = ob_rr_theme->paddingy;
510 RrPaint(text_a, self->text,
511 self->area.width - 2*ob_rr_theme->paddingx,
512 ob_rr_theme->menu_title_height -
513 2*ob_rr_theme->paddingy);
517 /* unlabeled separator */
518 XMoveResizeWindow(obt_display, self->text, 0, 0,
520 ob_rr_theme->menu_sep_width +
521 2*ob_rr_theme->menu_sep_paddingy);
523 a_sep->surface.parent = item_a;
524 a_sep->surface.parentx = 0;
525 a_sep->surface.parenty = 0;
526 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
527 a_sep->texture[i].data.lineart.x1 =
528 ob_rr_theme->menu_sep_paddingx;
529 a_sep->texture[i].data.lineart.y1 =
530 ob_rr_theme->menu_sep_paddingy + i;
531 a_sep->texture[i].data.lineart.x2 =
532 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
533 a_sep->texture[i].data.lineart.y2 =
534 ob_rr_theme->menu_sep_paddingy + i;
537 RrPaint(a_sep, self->text, self->area.width,
538 ob_rr_theme->menu_sep_width +
539 2*ob_rr_theme->menu_sep_paddingy);
543 g_assert_not_reached();
546 if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
547 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
548 self->entry->data.normal.icon)
552 XMoveResizeWindow(obt_display, self->icon,
553 PADDING, frame->item_margin.top,
554 ITEM_HEIGHT - frame->item_margin.top
555 - frame->item_margin.bottom,
556 ITEM_HEIGHT - frame->item_margin.top
557 - frame->item_margin.bottom);
559 clear = ob_rr_theme->a_clear_tex;
560 RrAppearanceClearTextures(clear);
561 clear->texture[0].type = RR_TEXTURE_IMAGE;
562 clear->texture[0].data.image.image =
563 self->entry->data.normal.icon;
564 clear->texture[0].data.image.alpha =
565 self->entry->data.normal.icon_alpha;
566 clear->surface.parent = item_a;
567 clear->surface.parentx = PADDING;
568 clear->surface.parenty = frame->item_margin.top;
569 RrPaint(clear, self->icon,
570 ITEM_HEIGHT - frame->item_margin.top
571 - frame->item_margin.bottom,
572 ITEM_HEIGHT - frame->item_margin.top
573 - frame->item_margin.bottom);
574 XMapWindow(obt_display, self->icon);
575 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
576 self->entry->data.normal.mask)
581 XMoveResizeWindow(obt_display, self->icon,
582 PADDING, frame->item_margin.top,
583 ITEM_HEIGHT - frame->item_margin.top
584 - frame->item_margin.bottom,
585 ITEM_HEIGHT - frame->item_margin.top
586 - frame->item_margin.bottom);
588 clear = ob_rr_theme->a_clear_tex;
589 RrAppearanceClearTextures(clear);
590 clear->texture[0].type = RR_TEXTURE_MASK;
591 clear->texture[0].data.mask.mask =
592 self->entry->data.normal.mask;
594 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
595 !self->entry->data.normal.enabled ?
597 (self == self->frame->selected ?
598 self->entry->data.normal.mask_disabled_selected_color :
599 self->entry->data.normal.mask_disabled_color) :
601 (self == self->frame->selected ?
602 self->entry->data.normal.mask_selected_color :
603 self->entry->data.normal.mask_normal_color));
604 clear->texture[0].data.mask.color = c;
606 clear->surface.parent = item_a;
607 clear->surface.parentx = PADDING;
608 clear->surface.parenty = frame->item_margin.top;
609 RrPaint(clear, self->icon,
610 ITEM_HEIGHT - frame->item_margin.top
611 - frame->item_margin.bottom,
612 ITEM_HEIGHT - frame->item_margin.top
613 - frame->item_margin.bottom);
614 XMapWindow(obt_display, self->icon);
616 XUnmapWindow(obt_display, self->icon);
618 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
619 RrAppearance *bullet_a;
620 XMoveResizeWindow(obt_display, self->bullet,
621 self->frame->text_x + self->frame->text_w -
622 ITEM_HEIGHT + PADDING, PADDING,
623 ITEM_HEIGHT - 2*PADDING,
624 ITEM_HEIGHT - 2*PADDING);
625 bullet_a = (self == self->frame->selected ?
626 ob_rr_theme->a_menu_bullet_selected :
627 ob_rr_theme->a_menu_bullet_normal);
628 bullet_a->surface.parent = item_a;
629 bullet_a->surface.parentx =
630 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
631 bullet_a->surface.parenty = PADDING;
632 RrPaint(bullet_a, self->bullet,
633 ITEM_HEIGHT - 2*PADDING,
634 ITEM_HEIGHT - 2*PADDING);
635 XMapWindow(obt_display, self->bullet);
637 XUnmapWindow(obt_display, self->bullet);
642 /*! this code is taken from the menu_frame_render. if that changes, this won't
644 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
645 gboolean first_entry,
654 t = self->entry->type;
656 /* this is the More... entry, it's NORMAL type */
657 t = OB_MENU_ENTRY_TYPE_NORMAL;
660 case OB_MENU_ENTRY_TYPE_NORMAL:
661 case OB_MENU_ENTRY_TYPE_SUBMENU:
662 h += ob_rr_theme->menu_font_height;
664 case OB_MENU_ENTRY_TYPE_SEPARATOR:
665 if (self->entry->data.separator.label != NULL) {
666 h += ob_rr_theme->menu_title_height +
667 (ob_rr_theme->mbwidth - PADDING) * 2;
669 /* if the first entry is a labeled separator, then make its border
670 overlap with the menu's outside border */
672 h -= ob_rr_theme->mbwidth;
673 /* if the last entry is a labeled separator, then make its border
674 overlap with the menu's outside border */
676 h -= ob_rr_theme->mbwidth;
678 h += ob_rr_theme->menu_sep_width +
679 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
687 void menu_frame_render(ObMenuFrame *self)
690 gint tw, th; /* temps */
692 gboolean has_icon = FALSE;
696 /* find text dimensions */
698 STRUT_SET(self->item_margin, 0, 0, 0, 0);
703 e = self->entries->data;
704 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
705 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
710 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
711 STRUT_SET(self->item_margin,
712 MAX(self->item_margin.left, l),
713 MAX(self->item_margin.top, t),
714 MAX(self->item_margin.right, r),
715 MAX(self->item_margin.bottom, b));
716 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
717 STRUT_SET(self->item_margin,
718 MAX(self->item_margin.left, l),
719 MAX(self->item_margin.top, t),
720 MAX(self->item_margin.right, r),
721 MAX(self->item_margin.bottom, b));
722 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
723 STRUT_SET(self->item_margin,
724 MAX(self->item_margin.left, l),
725 MAX(self->item_margin.top, t),
726 MAX(self->item_margin.right, r),
727 MAX(self->item_margin.bottom, b));
728 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
729 STRUT_SET(self->item_margin,
730 MAX(self->item_margin.left, l),
731 MAX(self->item_margin.top, t),
732 MAX(self->item_margin.right, r),
733 MAX(self->item_margin.bottom, b));
736 /* render the entries */
738 for (it = self->entries; it; it = g_list_next(it)) {
739 RrAppearance *text_a;
742 /* if the first entry is a labeled separator, then make its border
743 overlap with the menu's outside border */
744 if (it == self->entries &&
745 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
746 e->entry->data.separator.label)
748 h -= ob_rr_theme->mbwidth;
751 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
752 e->entry->data.separator.label)
754 e->border = ob_rr_theme->mbwidth;
757 RECT_SET_POINT(e->area, 0, h+e->border);
758 XMoveWindow(obt_display, e->window,
759 e->area.x-e->border, e->area.y-e->border);
760 XSetWindowBorderWidth(obt_display, e->window, e->border);
761 XSetWindowBorder(obt_display, e->window,
762 RrColorPixel(ob_rr_theme->menu_border_color));
764 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
765 !e->entry->data.normal.enabled ?
767 (e == self->selected ?
768 ob_rr_theme->a_menu_text_disabled_selected :
769 ob_rr_theme->a_menu_text_disabled) :
771 (e == self->selected ?
772 ob_rr_theme->a_menu_text_selected :
773 ob_rr_theme->a_menu_text_normal));
774 switch (e->entry->type) {
775 case OB_MENU_ENTRY_TYPE_NORMAL:
776 text_a->texture[0].data.text.string = e->entry->data.normal.label;
777 tw = RrMinWidth(text_a);
778 tw = MIN(tw, MAX_MENU_WIDTH);
779 th = ob_rr_theme->menu_font_height;
781 if (e->entry->data.normal.icon ||
782 e->entry->data.normal.mask)
785 case OB_MENU_ENTRY_TYPE_SUBMENU:
786 sub = e->entry->data.submenu.submenu;
787 text_a->texture[0].data.text.string = sub ? sub->title : "";
788 tw = RrMinWidth(text_a);
789 tw = MIN(tw, MAX_MENU_WIDTH);
790 th = ob_rr_theme->menu_font_height;
792 if (e->entry->data.normal.icon ||
793 e->entry->data.normal.mask)
796 tw += ITEM_HEIGHT - PADDING;
798 case OB_MENU_ENTRY_TYPE_SEPARATOR:
799 if (e->entry->data.separator.label != NULL) {
800 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
801 e->entry->data.separator.label;
802 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
803 2*ob_rr_theme->paddingx;
804 tw = MIN(tw, MAX_MENU_WIDTH);
805 th = ob_rr_theme->menu_title_height +
806 (ob_rr_theme->mbwidth - PADDING) *2;
809 th = ob_rr_theme->menu_sep_width +
810 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
814 g_assert_not_reached();
824 /* if the last entry is a labeled separator, then make its border
825 overlap with the menu's outside border */
826 it = g_list_last(self->entries);
827 e = it ? it->data : NULL;
828 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
829 e->entry->data.separator.label)
831 h -= ob_rr_theme->mbwidth;
834 self->text_x = PADDING;
839 w += ITEM_HEIGHT + PADDING;
840 self->text_x += ITEM_HEIGHT + PADDING;
847 XResizeWindow(obt_display, self->window, w, h);
851 RrPaint(self->a_items, self->window, w, h);
853 for (it = self->entries; it; it = g_list_next(it))
854 menu_entry_frame_render(it->data);
856 w += ob_rr_theme->mbwidth * 2;
857 h += ob_rr_theme->mbwidth * 2;
859 RECT_SET_SIZE(self->area, w, h);
864 static void menu_frame_update(ObMenuFrame *self)
870 menu_pipe_execute(self->menu);
871 menu_find_submenus(self->menu);
873 self->selected = NULL;
875 /* start at show_from */
876 mit = g_list_nth(self->menu->entries, self->show_from);
878 /* go through the menu's and frame's entries and connect the frame entries
879 to the menu entries */
880 for (fit = self->entries; mit && fit;
881 mit = g_list_next(mit), fit = g_list_next(fit))
883 ObMenuEntryFrame *f = fit->data;
884 f->entry = mit->data;
887 /* if there are more menu entries than in the frame, add them */
889 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
890 self->entries = g_list_append(self->entries, e);
891 mit = g_list_next(mit);
894 /* if there are more frame entries than menu entries then get rid of
897 GList *n = g_list_next(fit);
898 menu_entry_frame_free(fit->data);
899 self->entries = g_list_delete_link(self->entries, fit);
903 /* * make the menu fit on the screen */
905 /* calculate the height of the menu */
907 for (fit = self->entries; fit; fit = g_list_next(fit))
908 h += menu_entry_frame_get_height(fit->data,
909 fit == self->entries,
910 g_list_next(fit) == NULL);
911 /* add the border at the top and bottom */
912 h += ob_rr_theme->mbwidth * 2;
914 a = screen_physical_area_monitor(self->monitor);
918 gboolean last_entry = TRUE;
920 /* take the height of our More... entry into account */
921 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
923 /* start at the end of the entries */
924 flast = g_list_last(self->entries);
926 /* pull out all the entries from the frame that don't
927 fit on the screen, leaving at least 1 though */
928 while (h > a->height && g_list_previous(flast) != NULL) {
929 /* update the height, without this entry */
930 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
932 /* destroy the entry we're not displaying */
934 flast = g_list_previous(flast);
935 menu_entry_frame_free(tmp->data);
936 self->entries = g_list_delete_link(self->entries, tmp);
938 /* only the first one that we see is the last entry in the menu */
943 ObMenuEntry *more_entry;
944 ObMenuEntryFrame *more_frame;
945 /* make the More... menu entry frame which will display in this
947 if self->menu->more_menu is NULL that means that this is already
948 More... menu, so just use ourself.
950 more_entry = menu_get_more((self->menu->more_menu ?
951 self->menu->more_menu :
953 /* continue where we left off */
955 g_list_length(self->entries));
956 more_frame = menu_entry_frame_new(more_entry, self);
957 /* make it get deleted when the menu frame goes away */
958 menu_entry_unref(more_entry);
960 /* add our More... entry to the frame */
961 self->entries = g_list_append(self->entries, more_frame);
965 menu_frame_render(self);
968 static gboolean menu_frame_is_visible(ObMenuFrame *self)
970 return !!(g_list_find(menu_frame_visible, self));
973 static gboolean menu_frame_show(ObMenuFrame *self)
977 /* determine if the underlying menu is already visible */
978 for (it = menu_frame_visible; it; it = g_list_next(it)) {
979 ObMenuFrame *f = it->data;
980 if (f->menu == self->menu)
984 if (self->menu->update_func)
985 if (!self->menu->update_func(self, self->menu->data))
989 if (menu_frame_visible == NULL) {
990 /* no menus shown yet */
992 /* grab the pointer in such a way as to pass through "owner events"
993 so that we can get enter/leave notifies in the menu. */
994 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
996 if (!grab_keyboard()) {
1002 menu_frame_update(self);
1004 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
1006 if (self->menu->show_func)
1007 self->menu->show_func(self, self->menu->data);
1012 gboolean menu_frame_show_topmenu(ObMenuFrame *self, GravityPoint pos,
1013 gint monitor, gboolean mouse)
1018 if (menu_frame_is_visible(self))
1020 if (!menu_frame_show(self))
1023 if (self->menu->place_func) {
1026 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1028 menu_frame_place_topmenu(self, &pos, &x, &y, monitor);
1031 menu_frame_move(self, x, y);
1032 XMapWindow(obt_display, self->window);
1034 if (screen_pointer_pos(&px, &py)) {
1036 if (self->menu->warp) {
1037 x += self->area.width / 2;
1039 XWarpPointer(obt_display, None, obt_root(ob_screen), 0, 0, 0, 0, x, y);
1043 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1044 if (e && e->frame == self)
1053 /*! Stop hiding an open submenu.
1054 @child The OnMenuFrame of the submenu to be hidden
1056 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1058 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1059 submenu_hide_timer = 0;
1062 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1063 ObMenuEntryFrame *parent_entry)
1068 if (menu_frame_is_visible(self))
1071 self->monitor = parent->monitor;
1072 self->parent = parent;
1073 self->parent_entry = parent_entry;
1075 /* set up parent's child to be us */
1076 if ((parent->child) != self) {
1078 menu_frame_hide(parent->child);
1079 parent->child = self;
1080 parent->child_entry = parent_entry;
1083 if (!menu_frame_show(self)) {
1084 parent->child = NULL;
1085 parent->child_entry = NULL;
1089 menu_frame_place_submenu(self, &x, &y);
1090 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1093 /*try the other side */
1094 self->direction_right = !self->direction_right;
1095 menu_frame_place_submenu(self, &x, &y);
1096 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1098 menu_frame_move(self, x + dx, y + dy);
1100 XMapWindow(obt_display, self->window);
1102 if (screen_pointer_pos(&px, &py)) {
1103 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1104 if (e && e->frame == self)
1111 static void menu_frame_hide(ObMenuFrame *self)
1113 ObMenu *const menu = self->menu;
1114 GList *it = g_list_find(menu_frame_visible, self);
1115 gulong ignore_start;
1120 if (menu->hide_func)
1121 menu->hide_func(self, menu->data);
1124 menu_frame_hide(self->child);
1126 if (!self->parent && self->menu->warp)
1127 XWarpPointer(obt_display, None, obt_root(ob_screen), 0, 0, 0, 0, self->ox, self->oy);
1130 remove_submenu_hide_timeout(self);
1132 self->parent->child = NULL;
1133 self->parent->child_entry = NULL;
1135 self->parent = NULL;
1136 self->parent_entry = NULL;
1138 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1140 if (menu_frame_visible == NULL) {
1141 /* last menu shown */
1146 ignore_start = event_start_ignore_all_enters();
1147 XUnmapWindow(obt_display, self->window);
1148 event_end_ignore_all_enters(ignore_start);
1150 menu_frame_free(self);
1152 if (menu->cleanup_func)
1153 menu->cleanup_func(menu, menu->data);
1156 void menu_frame_hide_all(void)
1160 if (config_submenu_show_delay) {
1161 /* remove any submenu open requests */
1162 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1163 submenu_show_timer = 0;
1165 if ((it = g_list_last(menu_frame_visible)))
1166 menu_frame_hide(it->data);
1169 ObMenuFrame* menu_frame_under(gint x, gint y)
1171 ObMenuFrame *ret = NULL;
1174 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1175 ObMenuFrame *f = it->data;
1177 if (RECT_CONTAINS(f->area, x, y)) {
1185 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1188 ObMenuEntryFrame *ret = NULL;
1191 if ((frame = menu_frame_under(x, y))) {
1192 x -= ob_rr_theme->mbwidth + frame->area.x;
1193 y -= ob_rr_theme->mbwidth + frame->area.y;
1195 for (it = frame->entries; it; it = g_list_next(it)) {
1196 ObMenuEntryFrame *e = it->data;
1198 if (RECT_CONTAINS(e->area, x, y)) {
1207 static gboolean submenu_show_timeout(gpointer data)
1209 g_assert(menu_frame_visible);
1210 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1214 static gboolean submenu_hide_timeout(gpointer data)
1216 g_assert(menu_frame_visible);
1217 menu_frame_hide((ObMenuFrame*)data);
1221 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1224 ObMenuEntryFrame *old = self->selected;
1225 ObMenuFrame *oldchild = self->child;
1226 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1228 /* if the user selected a separator, ignore it and reselect what we had
1230 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1234 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1237 /* if the user left this menu but we have a submenu open, move the
1238 selection back to that submenu */
1239 if (!entry && oldchild_entry)
1240 entry = oldchild_entry;
1242 if (config_submenu_show_delay) {
1243 /* remove any submenu open requests */
1244 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1245 submenu_show_timer = 0;
1248 self->selected = entry;
1251 menu_entry_frame_render(old);
1253 if (oldchild_entry) {
1254 /* There is an open submenu */
1255 if (oldchild_entry == self->selected) {
1256 /* The open submenu has been reselected, so stop hiding the
1258 remove_submenu_hide_timeout(oldchild);
1260 else if (oldchild_entry == old) {
1261 /* The open submenu was selected and is no longer, so hide the
1263 if (immediate || config_submenu_hide_delay == 0)
1264 menu_frame_hide(oldchild);
1265 else if (config_submenu_hide_delay > 0) {
1266 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1267 submenu_hide_timer =
1268 g_timeout_add_full(G_PRIORITY_DEFAULT,
1269 config_submenu_hide_delay,
1270 submenu_hide_timeout, oldchild, NULL);
1275 if (self->selected) {
1276 menu_entry_frame_render(self->selected);
1278 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1279 /* only show if the submenu isn't already showing */
1280 if (oldchild_entry != self->selected) {
1281 if (immediate || config_submenu_show_delay == 0)
1282 menu_entry_frame_show_submenu(self->selected);
1283 else if (config_submenu_show_delay > 0) {
1284 if (submenu_show_timer)
1285 g_source_remove(submenu_show_timer);
1286 submenu_show_timer =
1287 g_timeout_add_full(G_PRIORITY_DEFAULT,
1288 config_submenu_show_delay,
1289 submenu_show_timeout,
1290 self->selected, NULL);
1293 /* hide the grandchildren of this menu. and move the cursor to
1295 else if (immediate && self->child && self->child->child) {
1296 menu_frame_hide(self->child->child);
1297 menu_frame_select(self->child, NULL, TRUE);
1303 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1307 if (!self->entry->data.submenu.submenu) return;
1309 f = menu_frame_new(self->entry->data.submenu.submenu,
1310 self->entry->data.submenu.show_from,
1311 self->frame->client);
1312 /* pass our direction on to our child */
1313 f->direction_right = self->frame->direction_right;
1315 if (!menu_frame_show_submenu(f, self->frame, self))
1319 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1321 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1322 self->entry->data.normal.enabled)
1324 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1326 ObMenuEntry *entry = self->entry;
1327 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1328 gpointer data = self->frame->menu->data;
1329 GSList *acts = self->entry->data.normal.actions;
1330 ObClient *client = self->frame->client;
1331 ObMenuFrame *frame = self->frame;
1332 guint mods = obt_keyboard_only_modmasks(state);
1334 /* release grabs before executing the shit */
1335 if (!(mods & ControlMask)) {
1336 event_cancel_all_key_grabs();
1341 func(entry, frame, client, state, data);
1343 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1344 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1348 void menu_frame_select_previous(ObMenuFrame *self)
1350 GList *it = NULL, *start;
1352 if (self->entries) {
1353 start = it = g_list_find(self->entries, self->selected);
1355 ObMenuEntryFrame *e;
1357 it = it ? g_list_previous(it) : g_list_last(self->entries);
1363 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1365 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1370 menu_frame_select(self, it ? it->data : NULL, FALSE);
1373 void menu_frame_select_next(ObMenuFrame *self)
1375 GList *it = NULL, *start;
1377 if (self->entries) {
1378 start = it = g_list_find(self->entries, self->selected);
1380 ObMenuEntryFrame *e;
1382 it = it ? g_list_next(it) : self->entries;
1388 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1390 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1395 menu_frame_select(self, it ? it->data : NULL, FALSE);
1398 void menu_frame_select_first(ObMenuFrame *self)
1402 if (self->entries) {
1403 for (it = self->entries; it; it = g_list_next(it)) {
1404 ObMenuEntryFrame *e = it->data;
1405 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1407 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1411 menu_frame_select(self, it ? it->data : NULL, FALSE);
1414 void menu_frame_select_last(ObMenuFrame *self)
1418 if (self->entries) {
1419 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1420 ObMenuEntryFrame *e = it->data;
1421 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1423 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1427 menu_frame_select(self, it ? it->data : NULL, FALSE);