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)
43 GList *menu_frame_visible;
44 GHashTable *menu_frame_map;
46 static RrAppearance *a_sep;
47 static guint submenu_show_timer = 0;
48 static guint submenu_hide_timer = 0;
50 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
52 static void menu_entry_frame_free(ObMenuEntryFrame *self);
53 static void menu_frame_update(ObMenuFrame *self);
54 static gboolean submenu_show_timeout(gpointer data);
55 static void menu_frame_hide(ObMenuFrame *self);
57 static gboolean submenu_hide_timeout(gpointer data);
59 static Window createWindow(Window parent, gulong mask,
60 XSetWindowAttributes *attrib)
62 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
63 RrDepth(ob_rr_inst), InputOutput,
64 RrVisual(ob_rr_inst), mask, attrib);
67 static void client_dest(ObClient *client, gpointer data)
71 /* menus can be associated with a client, so null those refs since
72 we are disappearing now */
73 for (it = menu_frame_visible; it; it = g_list_next(it)) {
74 ObMenuFrame *f = it->data;
75 if (f->client == client)
80 void menu_frame_startup(gboolean reconfig)
84 a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
85 RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
86 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
87 a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
88 a_sep->texture[i].data.lineart.color =
89 ob_rr_theme->menu_sep_color;
94 client_add_destroy_notify(client_dest, NULL);
95 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
98 void menu_frame_shutdown(gboolean reconfig)
100 RrAppearanceFree(a_sep);
102 if (reconfig) return;
104 client_remove_destroy_notify(client_dest);
105 g_hash_table_destroy(menu_frame_map);
108 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
111 XSetWindowAttributes attr;
112 const Rect r = {0, 0, 1, 1};
115 self = window_new(OB_WINDOW_CLASS_MENUFRAME, ObMenuFrame);
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->depth = RrDepth(ob_rr_inst);
124 self->window = XCreateWindow(obt_display, obt_root(ob_screen),
125 r.x, r.y, r.width, r.height, b, self->depth,
126 InputOutput, RrVisual(ob_rr_inst),
128 self->layer = OB_STACKING_LAYER_INTERNAL;
130 /* make it a popup menu type window */
131 OBT_PROP_SET32(self->window, NET_WM_WINDOW_TYPE, ATOM,
132 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU));
134 XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
135 XSetWindowBorder(obt_display, self->window,
136 RrColorPixel(ob_rr_theme->menu_border_color));
138 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
140 window_set_top_area(MENUFRAME_AS_WINDOW(self), &r, b);
141 window_set_abstract(MENUFRAME_AS_WINDOW(self),
142 &self->window, /* top level window */
143 &self->window, /* composite redir window */
144 &self->layer, /* stacking layer */
145 &self->depth, /* window depth */
148 window_add(&self->window, MENUFRAME_AS_WINDOW(self));
149 stacking_add(MENUFRAME_AS_WINDOW(self));
154 void menu_frame_free(ObMenuFrame *self)
157 while (self->entries) {
158 menu_entry_frame_free(self->entries->data);
159 self->entries = g_list_delete_link(self->entries, self->entries);
162 window_cleanup(MENUFRAME_AS_WINDOW(self));
164 stacking_remove(MENUFRAME_AS_WINDOW(self));
165 window_remove(self->window);
167 RrAppearanceFree(self->a_items);
169 XDestroyWindow(obt_display, self->window);
171 window_free(MENUFRAME_AS_WINDOW(self));
175 ObtIC* menu_frame_ic(ObMenuFrame *self)
177 /* menus are always used through a grab right now, so they can always use
178 the grab input context */
179 return grab_input_context();
182 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
185 ObMenuEntryFrame *self;
186 XSetWindowAttributes attr;
188 self = g_slice_new0(ObMenuEntryFrame);
192 menu_entry_ref(entry);
194 attr.event_mask = ENTRY_EVENTMASK;
195 self->window = createWindow(self->frame->window, CWEventMask, &attr);
196 self->text = createWindow(self->window, 0, NULL);
197 g_hash_table_insert(menu_frame_map, &self->window, self);
198 g_hash_table_insert(menu_frame_map, &self->text, self);
199 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
200 self->icon = createWindow(self->window, 0, NULL);
201 g_hash_table_insert(menu_frame_map, &self->icon, self);
203 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
204 self->bullet = createWindow(self->window, 0, NULL);
205 g_hash_table_insert(menu_frame_map, &self->bullet, self);
208 XMapWindow(obt_display, self->window);
209 XMapWindow(obt_display, self->text);
211 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
216 static void menu_entry_frame_free(ObMenuEntryFrame *self)
219 menu_entry_unref(self->entry);
221 window_remove(self->window);
223 XDestroyWindow(obt_display, self->text);
224 XDestroyWindow(obt_display, self->window);
225 g_hash_table_remove(menu_frame_map, &self->text);
226 g_hash_table_remove(menu_frame_map, &self->window);
227 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
228 XDestroyWindow(obt_display, self->icon);
229 g_hash_table_remove(menu_frame_map, &self->icon);
231 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
232 XDestroyWindow(obt_display, self->bullet);
233 g_hash_table_remove(menu_frame_map, &self->bullet);
236 g_slice_free(ObMenuEntryFrame, self);
240 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
242 RECT_SET_POINT(self->area, x, y);
243 self->monitor = screen_find_monitor_point(x, y);
244 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
247 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
251 if (config_menu_middle) {
255 *y -= self->area.height / 2;
257 /* try to the right of the cursor */
258 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
259 self->direction_right = TRUE;
261 /* try to the left of the cursor */
262 myx = *x - self->area.width;
263 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
264 self->direction_right = FALSE;
267 /* if didnt fit on either side so just use what it says */
269 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
270 self->direction_right = TRUE;
280 /* try to the bottom right of the cursor */
281 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
282 self->direction_right = TRUE;
283 if (dx != 0 || dy != 0) {
284 /* try to the bottom left of the cursor */
285 myx = *x - self->area.width;
287 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
288 self->direction_right = FALSE;
290 if (dx != 0 || dy != 0) {
291 /* try to the top right of the cursor */
293 myy = *y - self->area.height;
294 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
295 self->direction_right = TRUE;
297 if (dx != 0 || dy != 0) {
298 /* try to the top left of the cursor */
299 myx = *x - self->area.width;
300 myy = *y - self->area.height;
301 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
302 self->direction_right = FALSE;
304 if (dx != 0 || dy != 0) {
305 /* if didnt fit on either side so just use what it says */
308 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
309 self->direction_right = TRUE;
316 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
318 gint overlapx, overlapy;
321 overlapx = ob_rr_theme->menu_overlap_x;
322 overlapy = ob_rr_theme->menu_overlap_y;
323 bwidth = ob_rr_theme->mbwidth;
325 if (self->direction_right)
326 *x = self->parent->area.x + self->parent->area.width -
329 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
331 *y = self->parent->area.y + self->parent_entry->area.y;
332 if (config_menu_middle)
333 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
338 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
341 const Rect *a = NULL;
346 a = screen_physical_area_monitor(screen_find_monitor_point(x, y));
348 half = g_list_length(self->entries) / 2;
349 pos = g_list_index(self->entries, self->selected);
351 /* if in the bottom half then check this stuff first, will keep the bottom
352 edge of the menu visible */
354 *dx = MAX(*dx, a->x - x);
355 *dy = MAX(*dy, a->y - y);
357 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
358 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
359 /* if in the top half then check this stuff last, will keep the top
360 edge of the menu visible */
362 *dx = MAX(*dx, a->x - x);
363 *dy = MAX(*dy, a->y - y);
367 static void menu_entry_frame_render(ObMenuEntryFrame *self)
369 RrAppearance *item_a, *text_a;
372 ObMenuFrame *frame = self->frame;
374 switch (self->entry->type) {
375 case OB_MENU_ENTRY_TYPE_NORMAL:
376 case OB_MENU_ENTRY_TYPE_SUBMENU:
377 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
378 !self->entry->data.normal.enabled ?
380 (self == self->frame->selected ?
381 ob_rr_theme->a_menu_disabled_selected :
382 ob_rr_theme->a_menu_disabled) :
384 (self == self->frame->selected ?
385 ob_rr_theme->a_menu_selected :
386 ob_rr_theme->a_menu_normal));
389 case OB_MENU_ENTRY_TYPE_SEPARATOR:
390 if (self->entry->data.separator.label) {
391 item_a = ob_rr_theme->a_menu_title;
392 th = ob_rr_theme->menu_title_height;
394 item_a = ob_rr_theme->a_menu_normal;
395 th = ob_rr_theme->menu_sep_width +
396 2*ob_rr_theme->menu_sep_paddingy;
400 g_assert_not_reached();
403 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
404 XResizeWindow(obt_display, self->window,
405 self->area.width, self->area.height);
406 item_a->surface.parent = self->frame->a_items;
407 item_a->surface.parentx = self->area.x;
408 item_a->surface.parenty = self->area.y;
409 RrPaint(item_a, self->window, self->area.width, self->area.height);
411 switch (self->entry->type) {
412 case OB_MENU_ENTRY_TYPE_NORMAL:
413 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
414 !self->entry->data.normal.enabled ?
416 (self == self->frame->selected ?
417 ob_rr_theme->a_menu_text_disabled_selected :
418 ob_rr_theme->a_menu_text_disabled) :
420 (self == self->frame->selected ?
421 ob_rr_theme->a_menu_text_selected :
422 ob_rr_theme->a_menu_text_normal));
423 text_a->texture[0].data.text.string = self->entry->data.normal.label;
424 if (self->entry->data.normal.shortcut &&
425 (self->frame->menu->show_all_shortcuts ||
426 self->entry->data.normal.shortcut_always_show ||
427 self->entry->data.normal.shortcut_position > 0))
429 text_a->texture[0].data.text.shortcut = TRUE;
430 text_a->texture[0].data.text.shortcut_pos =
431 self->entry->data.normal.shortcut_position;
433 text_a->texture[0].data.text.shortcut = FALSE;
435 case OB_MENU_ENTRY_TYPE_SUBMENU:
436 text_a = (self == self->frame->selected ?
437 ob_rr_theme->a_menu_text_selected :
438 ob_rr_theme->a_menu_text_normal);
439 sub = self->entry->data.submenu.submenu;
440 text_a->texture[0].data.text.string = sub ? sub->title : "";
441 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
442 sub->shortcut_always_show ||
443 sub->shortcut_position > 0))
445 text_a->texture[0].data.text.shortcut = TRUE;
446 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
448 text_a->texture[0].data.text.shortcut = FALSE;
450 case OB_MENU_ENTRY_TYPE_SEPARATOR:
451 if (self->entry->data.separator.label != NULL) {
452 text_a = ob_rr_theme->a_menu_text_title;
453 text_a->texture[0].data.text.string =
454 self->entry->data.separator.label;
457 text_a = ob_rr_theme->a_menu_text_normal;
460 g_assert_not_reached();
463 switch (self->entry->type) {
464 case OB_MENU_ENTRY_TYPE_NORMAL:
465 XMoveResizeWindow(obt_display, self->text,
466 self->frame->text_x, PADDING,
468 ITEM_HEIGHT - 2*PADDING);
469 text_a->surface.parent = item_a;
470 text_a->surface.parentx = self->frame->text_x;
471 text_a->surface.parenty = PADDING;
472 RrPaint(text_a, self->text, self->frame->text_w,
473 ITEM_HEIGHT - 2*PADDING);
475 case OB_MENU_ENTRY_TYPE_SUBMENU:
476 XMoveResizeWindow(obt_display, self->text,
477 self->frame->text_x, PADDING,
478 self->frame->text_w - ITEM_HEIGHT,
479 ITEM_HEIGHT - 2*PADDING);
480 text_a->surface.parent = item_a;
481 text_a->surface.parentx = self->frame->text_x;
482 text_a->surface.parenty = PADDING;
483 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
484 ITEM_HEIGHT - 2*PADDING);
486 case OB_MENU_ENTRY_TYPE_SEPARATOR:
487 if (self->entry->data.separator.label != NULL) {
488 /* labeled separator */
489 XMoveResizeWindow(obt_display, self->text,
490 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
491 self->area.width - 2*ob_rr_theme->paddingx,
492 ob_rr_theme->menu_title_height -
493 2*ob_rr_theme->paddingy);
494 text_a->surface.parent = item_a;
495 text_a->surface.parentx = ob_rr_theme->paddingx;
496 text_a->surface.parenty = ob_rr_theme->paddingy;
497 RrPaint(text_a, self->text,
498 self->area.width - 2*ob_rr_theme->paddingx,
499 ob_rr_theme->menu_title_height -
500 2*ob_rr_theme->paddingy);
504 /* unlabeled separator */
505 XMoveResizeWindow(obt_display, self->text, 0, 0,
507 ob_rr_theme->menu_sep_width +
508 2*ob_rr_theme->menu_sep_paddingy);
510 a_sep->surface.parent = item_a;
511 a_sep->surface.parentx = 0;
512 a_sep->surface.parenty = 0;
513 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
514 a_sep->texture[i].data.lineart.x1 =
515 ob_rr_theme->menu_sep_paddingx;
516 a_sep->texture[i].data.lineart.y1 =
517 ob_rr_theme->menu_sep_paddingy + i;
518 a_sep->texture[i].data.lineart.x2 =
519 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
520 a_sep->texture[i].data.lineart.y2 =
521 ob_rr_theme->menu_sep_paddingy + i;
524 RrPaint(a_sep, self->text, self->area.width,
525 ob_rr_theme->menu_sep_width +
526 2*ob_rr_theme->menu_sep_paddingy);
530 g_assert_not_reached();
533 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
534 self->entry->data.normal.icon)
538 XMoveResizeWindow(obt_display, self->icon,
539 PADDING, frame->item_margin.top,
540 ITEM_HEIGHT - frame->item_margin.top
541 - frame->item_margin.bottom,
542 ITEM_HEIGHT - frame->item_margin.top
543 - frame->item_margin.bottom);
545 clear = ob_rr_theme->a_clear_tex;
546 RrAppearanceClearTextures(clear);
547 clear->texture[0].type = RR_TEXTURE_IMAGE;
548 clear->texture[0].data.image.image =
549 self->entry->data.normal.icon;
550 clear->texture[0].data.image.alpha =
551 self->entry->data.normal.icon_alpha;
552 clear->surface.parent = item_a;
553 clear->surface.parentx = PADDING;
554 clear->surface.parenty = frame->item_margin.top;
555 RrPaint(clear, self->icon,
556 ITEM_HEIGHT - frame->item_margin.top
557 - frame->item_margin.bottom,
558 ITEM_HEIGHT - frame->item_margin.top
559 - frame->item_margin.bottom);
560 XMapWindow(obt_display, self->icon);
561 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
562 self->entry->data.normal.mask)
567 XMoveResizeWindow(obt_display, self->icon,
568 PADDING, frame->item_margin.top,
569 ITEM_HEIGHT - frame->item_margin.top
570 - frame->item_margin.bottom,
571 ITEM_HEIGHT - frame->item_margin.top
572 - frame->item_margin.bottom);
574 clear = ob_rr_theme->a_clear_tex;
575 RrAppearanceClearTextures(clear);
576 clear->texture[0].type = RR_TEXTURE_MASK;
577 clear->texture[0].data.mask.mask =
578 self->entry->data.normal.mask;
580 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
581 !self->entry->data.normal.enabled ?
583 (self == self->frame->selected ?
584 self->entry->data.normal.mask_disabled_selected_color :
585 self->entry->data.normal.mask_disabled_color) :
587 (self == self->frame->selected ?
588 self->entry->data.normal.mask_selected_color :
589 self->entry->data.normal.mask_normal_color));
590 clear->texture[0].data.mask.color = c;
592 clear->surface.parent = item_a;
593 clear->surface.parentx = PADDING;
594 clear->surface.parenty = frame->item_margin.top;
595 RrPaint(clear, self->icon,
596 ITEM_HEIGHT - frame->item_margin.top
597 - frame->item_margin.bottom,
598 ITEM_HEIGHT - frame->item_margin.top
599 - frame->item_margin.bottom);
600 XMapWindow(obt_display, self->icon);
602 XUnmapWindow(obt_display, self->icon);
604 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
605 RrAppearance *bullet_a;
606 XMoveResizeWindow(obt_display, self->bullet,
607 self->frame->text_x + self->frame->text_w -
608 ITEM_HEIGHT + PADDING, PADDING,
609 ITEM_HEIGHT - 2*PADDING,
610 ITEM_HEIGHT - 2*PADDING);
611 bullet_a = (self == self->frame->selected ?
612 ob_rr_theme->a_menu_bullet_selected :
613 ob_rr_theme->a_menu_bullet_normal);
614 bullet_a->surface.parent = item_a;
615 bullet_a->surface.parentx =
616 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
617 bullet_a->surface.parenty = PADDING;
618 RrPaint(bullet_a, self->bullet,
619 ITEM_HEIGHT - 2*PADDING,
620 ITEM_HEIGHT - 2*PADDING);
621 XMapWindow(obt_display, self->bullet);
623 XUnmapWindow(obt_display, self->bullet);
628 /*! this code is taken from the menu_frame_render. if that changes, this won't
630 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
631 gboolean first_entry,
640 t = self->entry->type;
642 /* this is the More... entry, it's NORMAL type */
643 t = OB_MENU_ENTRY_TYPE_NORMAL;
646 case OB_MENU_ENTRY_TYPE_NORMAL:
647 case OB_MENU_ENTRY_TYPE_SUBMENU:
648 h += ob_rr_theme->menu_font_height;
650 case OB_MENU_ENTRY_TYPE_SEPARATOR:
651 if (self->entry->data.separator.label != NULL) {
652 h += ob_rr_theme->menu_title_height +
653 (ob_rr_theme->mbwidth - PADDING) * 2;
655 /* if the first entry is a labeled separator, then make its border
656 overlap with the menu's outside border */
658 h -= ob_rr_theme->mbwidth;
659 /* if the last entry is a labeled separator, then make its border
660 overlap with the menu's outside border */
662 h -= ob_rr_theme->mbwidth;
664 h += ob_rr_theme->menu_sep_width +
665 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
673 void menu_frame_render(ObMenuFrame *self)
676 gint tw, th; /* temps */
678 gboolean has_icon = FALSE;
682 /* find text dimensions */
684 STRUT_SET(self->item_margin, 0, 0, 0, 0);
689 e = self->entries->data;
690 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
691 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
696 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
697 STRUT_SET(self->item_margin,
698 MAX(self->item_margin.left, l),
699 MAX(self->item_margin.top, t),
700 MAX(self->item_margin.right, r),
701 MAX(self->item_margin.bottom, b));
702 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
703 STRUT_SET(self->item_margin,
704 MAX(self->item_margin.left, l),
705 MAX(self->item_margin.top, t),
706 MAX(self->item_margin.right, r),
707 MAX(self->item_margin.bottom, b));
708 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
709 STRUT_SET(self->item_margin,
710 MAX(self->item_margin.left, l),
711 MAX(self->item_margin.top, t),
712 MAX(self->item_margin.right, r),
713 MAX(self->item_margin.bottom, b));
714 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
715 STRUT_SET(self->item_margin,
716 MAX(self->item_margin.left, l),
717 MAX(self->item_margin.top, t),
718 MAX(self->item_margin.right, r),
719 MAX(self->item_margin.bottom, b));
722 /* render the entries */
724 for (it = self->entries; it; it = g_list_next(it)) {
725 RrAppearance *text_a;
728 /* if the first entry is a labeled separator, then make its border
729 overlap with the menu's outside border */
730 if (it == self->entries &&
731 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
732 e->entry->data.separator.label)
734 h -= ob_rr_theme->mbwidth;
737 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
738 e->entry->data.separator.label)
740 e->border = ob_rr_theme->mbwidth;
743 RECT_SET_POINT(e->area, 0, h+e->border);
744 XMoveWindow(obt_display, e->window,
745 e->area.x-e->border, e->area.y-e->border);
746 XSetWindowBorderWidth(obt_display, e->window, e->border);
747 XSetWindowBorder(obt_display, e->window,
748 RrColorPixel(ob_rr_theme->menu_border_color));
750 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
751 !e->entry->data.normal.enabled ?
753 (e == self->selected ?
754 ob_rr_theme->a_menu_text_disabled_selected :
755 ob_rr_theme->a_menu_text_disabled) :
757 (e == self->selected ?
758 ob_rr_theme->a_menu_text_selected :
759 ob_rr_theme->a_menu_text_normal));
760 switch (e->entry->type) {
761 case OB_MENU_ENTRY_TYPE_NORMAL:
762 text_a->texture[0].data.text.string = e->entry->data.normal.label;
763 tw = RrMinWidth(text_a);
764 tw = MIN(tw, MAX_MENU_WIDTH);
765 th = ob_rr_theme->menu_font_height;
767 if (e->entry->data.normal.icon ||
768 e->entry->data.normal.mask)
771 case OB_MENU_ENTRY_TYPE_SUBMENU:
772 sub = e->entry->data.submenu.submenu;
773 text_a->texture[0].data.text.string = sub ? sub->title : "";
774 tw = RrMinWidth(text_a);
775 tw = MIN(tw, MAX_MENU_WIDTH);
776 th = ob_rr_theme->menu_font_height;
778 if (e->entry->data.normal.icon ||
779 e->entry->data.normal.mask)
782 tw += ITEM_HEIGHT - PADDING;
784 case OB_MENU_ENTRY_TYPE_SEPARATOR:
785 if (e->entry->data.separator.label != NULL) {
786 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
787 e->entry->data.separator.label;
788 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
789 2*ob_rr_theme->paddingx;
790 tw = MIN(tw, MAX_MENU_WIDTH);
791 th = ob_rr_theme->menu_title_height +
792 (ob_rr_theme->mbwidth - PADDING) *2;
795 th = ob_rr_theme->menu_sep_width +
796 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
800 g_assert_not_reached();
808 /* if the last entry is a labeled separator, then make its border
809 overlap with the menu's outside border */
810 it = g_list_last(self->entries);
811 e = it ? it->data : NULL;
812 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
813 e->entry->data.separator.label)
815 h -= ob_rr_theme->mbwidth;
818 self->text_x = PADDING;
823 w += ITEM_HEIGHT + PADDING;
824 self->text_x += ITEM_HEIGHT + PADDING;
831 XResizeWindow(obt_display, self->window, w, h);
835 RrPaint(self->a_items, self->window, w, h);
837 for (it = self->entries; it; it = g_list_next(it))
838 menu_entry_frame_render(it->data);
840 w += ob_rr_theme->mbwidth * 2;
841 h += ob_rr_theme->mbwidth * 2;
843 RECT_SET_SIZE(self->area, w, h);
848 static void menu_frame_update(ObMenuFrame *self)
854 menu_pipe_execute(self->menu);
855 menu_find_submenus(self->menu);
857 self->selected = NULL;
859 /* start at show_from */
860 mit = g_list_nth(self->menu->entries, self->show_from);
862 /* go through the menu's and frame's entries and connect the frame entries
863 to the menu entries */
864 for (fit = self->entries; mit && fit;
865 mit = g_list_next(mit), fit = g_list_next(fit))
867 ObMenuEntryFrame *f = fit->data;
868 f->entry = mit->data;
871 /* if there are more menu entries than in the frame, add them */
873 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
874 self->entries = g_list_append(self->entries, e);
875 mit = g_list_next(mit);
878 /* if there are more frame entries than menu entries then get rid of
881 GList *n = g_list_next(fit);
882 menu_entry_frame_free(fit->data);
883 self->entries = g_list_delete_link(self->entries, fit);
887 /* * make the menu fit on the screen */
889 /* calculate the height of the menu */
891 for (fit = self->entries; fit; fit = g_list_next(fit))
892 h += menu_entry_frame_get_height(fit->data,
893 fit == self->entries,
894 g_list_next(fit) == NULL);
895 /* add the border at the top and bottom */
896 h += ob_rr_theme->mbwidth * 2;
898 a = screen_physical_area_monitor(self->monitor);
902 gboolean last_entry = TRUE;
904 /* take the height of our More... entry into account */
905 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
907 /* start at the end of the entries */
908 flast = g_list_last(self->entries);
910 /* pull out all the entries from the frame that don't
911 fit on the screen, leaving at least 1 though */
912 while (h > a->height && g_list_previous(flast) != NULL) {
913 /* update the height, without this entry */
914 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
916 /* destroy the entry we're not displaying */
918 flast = g_list_previous(flast);
919 menu_entry_frame_free(tmp->data);
920 self->entries = g_list_delete_link(self->entries, tmp);
922 /* only the first one that we see is the last entry in the menu */
927 ObMenuEntry *more_entry;
928 ObMenuEntryFrame *more_frame;
929 /* make the More... menu entry frame which will display in this
931 if self->menu->more_menu is NULL that means that this is already
932 More... menu, so just use ourself.
934 more_entry = menu_get_more((self->menu->more_menu ?
935 self->menu->more_menu :
937 /* continue where we left off */
939 g_list_length(self->entries));
940 more_frame = menu_entry_frame_new(more_entry, self);
941 /* make it get deleted when the menu frame goes away */
942 menu_entry_unref(more_entry);
944 /* add our More... entry to the frame */
945 self->entries = g_list_append(self->entries, more_frame);
949 menu_frame_render(self);
952 static gboolean menu_frame_is_visible(ObMenuFrame *self)
954 return !!(g_list_find(menu_frame_visible, self));
957 static gboolean menu_frame_show(ObMenuFrame *self)
961 /* determine if the underlying menu is already visible */
962 for (it = menu_frame_visible; it; it = g_list_next(it)) {
963 ObMenuFrame *f = it->data;
964 if (f->menu == self->menu)
968 if (self->menu->update_func)
969 if (!self->menu->update_func(self, self->menu->data))
973 if (menu_frame_visible == NULL) {
974 /* no menus shown yet */
976 /* grab the pointer in such a way as to pass through "owner events"
977 so that we can get enter/leave notifies in the menu. */
978 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
980 if (!grab_keyboard()) {
986 menu_frame_update(self);
988 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
990 if (self->menu->show_func)
991 self->menu->show_func(self, self->menu->data);
996 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
1001 if (menu_frame_is_visible(self))
1003 if (!menu_frame_show(self))
1006 if (self->menu->place_func)
1007 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1009 menu_frame_place_topmenu(self, &x, &y);
1011 menu_frame_move(self, x, y);
1013 XMapWindow(obt_display, self->window);
1015 if (screen_pointer_pos(&px, &py)) {
1016 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1017 if (e && e->frame == self)
1024 /*! Stop hiding an open submenu.
1025 @child The OnMenuFrame of the submenu to be hidden
1027 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1029 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1030 submenu_hide_timer = 0;
1033 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1034 ObMenuEntryFrame *parent_entry)
1039 if (menu_frame_is_visible(self))
1042 self->monitor = parent->monitor;
1043 self->parent = parent;
1044 self->parent_entry = parent_entry;
1046 /* set up parent's child to be us */
1047 if ((parent->child) != self) {
1049 menu_frame_hide(parent->child);
1050 parent->child = self;
1051 parent->child_entry = parent_entry;
1054 if (!menu_frame_show(self))
1057 menu_frame_place_submenu(self, &x, &y);
1058 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1061 /*try the other side */
1062 self->direction_right = !self->direction_right;
1063 menu_frame_place_submenu(self, &x, &y);
1064 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1066 menu_frame_move(self, x + dx, y + dy);
1068 XMapWindow(obt_display, self->window);
1070 if (screen_pointer_pos(&px, &py)) {
1071 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1072 if (e && e->frame == self)
1079 static void menu_frame_hide(ObMenuFrame *self)
1081 ObMenu *const menu = self->menu;
1082 GList *it = g_list_find(menu_frame_visible, self);
1083 gulong ignore_start;
1088 if (menu->hide_func)
1089 menu->hide_func(self, menu->data);
1092 menu_frame_hide(self->child);
1095 remove_submenu_hide_timeout(self);
1097 self->parent->child = NULL;
1098 self->parent->child_entry = NULL;
1100 self->parent = NULL;
1101 self->parent_entry = NULL;
1103 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1105 if (menu_frame_visible == NULL) {
1106 /* last menu shown */
1111 ignore_start = event_start_ignore_all_enters();
1112 XUnmapWindow(obt_display, self->window);
1113 event_end_ignore_all_enters(ignore_start);
1115 menu_frame_free(self);
1117 if (menu->cleanup_func)
1118 menu->cleanup_func(menu, menu->data);
1121 void menu_frame_hide_all(void)
1125 if (config_submenu_show_delay) {
1126 /* remove any submenu open requests */
1127 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1128 submenu_show_timer = 0;
1130 if ((it = g_list_last(menu_frame_visible)))
1131 menu_frame_hide(it->data);
1134 ObMenuFrame* menu_frame_under(gint x, gint y)
1136 ObMenuFrame *ret = NULL;
1139 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1140 ObMenuFrame *f = it->data;
1142 if (RECT_CONTAINS(f->area, x, y)) {
1150 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1153 ObMenuEntryFrame *ret = NULL;
1156 if ((frame = menu_frame_under(x, y))) {
1157 x -= ob_rr_theme->mbwidth + frame->area.x;
1158 y -= ob_rr_theme->mbwidth + frame->area.y;
1160 for (it = frame->entries; it; it = g_list_next(it)) {
1161 ObMenuEntryFrame *e = it->data;
1163 if (RECT_CONTAINS(e->area, x, y)) {
1172 static gboolean submenu_show_timeout(gpointer data)
1174 g_assert(menu_frame_visible);
1175 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1176 XFlush(obt_display);
1180 static gboolean submenu_hide_timeout(gpointer data)
1182 g_assert(menu_frame_visible);
1183 menu_frame_hide((ObMenuFrame*)data);
1184 XFlush(obt_display);
1188 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1191 ObMenuEntryFrame *old = self->selected;
1192 ObMenuFrame *oldchild = self->child;
1193 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1195 /* if the user selected a separator, ignore it and reselect what we had
1197 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1201 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1204 /* if the user left this menu but we have a submenu open, move the
1205 selection back to that submenu */
1206 if (!entry && oldchild_entry)
1207 entry = oldchild_entry;
1209 if (config_submenu_show_delay) {
1210 /* remove any submenu open requests */
1211 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1212 submenu_show_timer = 0;
1215 self->selected = entry;
1218 menu_entry_frame_render(old);
1220 if (oldchild_entry) {
1221 /* There is an open submenu */
1222 if (oldchild_entry == self->selected) {
1223 /* The open submenu has been reselected, so stop hiding the
1225 remove_submenu_hide_timeout(oldchild);
1227 else if (oldchild_entry == old) {
1228 /* The open submenu was selected and is no longer, so hide the
1230 if (immediate || config_submenu_hide_delay == 0)
1231 menu_frame_hide(oldchild);
1232 else if (config_submenu_hide_delay > 0) {
1233 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1234 submenu_hide_timer =
1235 g_timeout_add_full(G_PRIORITY_DEFAULT,
1236 config_submenu_hide_delay,
1237 submenu_hide_timeout, oldchild, NULL);
1242 if (self->selected) {
1243 menu_entry_frame_render(self->selected);
1245 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1246 /* only show if the submenu isn't already showing */
1247 if (oldchild_entry != self->selected) {
1248 if (immediate || config_submenu_hide_delay == 0)
1249 menu_entry_frame_show_submenu(self->selected);
1250 else if (config_submenu_hide_delay > 0) {
1251 if (submenu_show_timer)
1252 g_source_remove(submenu_show_timer);
1253 submenu_show_timer =
1254 g_timeout_add_full(G_PRIORITY_DEFAULT,
1255 config_submenu_show_delay,
1256 submenu_show_timeout,
1257 self->selected, NULL);
1260 /* hide the grandchildren of this menu. and move the cursor to
1262 else if (immediate && self->child && self->child->child) {
1263 menu_frame_hide(self->child->child);
1264 menu_frame_select(self->child, NULL, TRUE);
1270 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1274 if (!self->entry->data.submenu.submenu) return;
1276 f = menu_frame_new(self->entry->data.submenu.submenu,
1277 self->entry->data.submenu.show_from,
1278 self->frame->client);
1279 /* pass our direction on to our child */
1280 f->direction_right = self->frame->direction_right;
1282 menu_frame_show_submenu(f, self->frame, self);
1285 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1287 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1288 self->entry->data.normal.enabled)
1290 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1292 ObMenuEntry *entry = self->entry;
1293 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1294 gpointer data = self->frame->menu->data;
1295 GSList *acts = self->entry->data.normal.actions;
1296 ObClient *client = self->frame->client;
1297 ObMenuFrame *frame = self->frame;
1298 guint mods = obt_keyboard_only_modmasks(state);
1300 /* release grabs before executing the shit */
1301 if (!(mods & ControlMask)) {
1302 event_cancel_all_key_grabs();
1307 func(entry, frame, client, state, data);
1309 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1310 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1314 void menu_frame_select_previous(ObMenuFrame *self)
1316 GList *it = NULL, *start;
1318 if (self->entries) {
1319 start = it = g_list_find(self->entries, self->selected);
1321 ObMenuEntryFrame *e;
1323 it = it ? g_list_previous(it) : g_list_last(self->entries);
1329 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1331 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1336 menu_frame_select(self, it ? it->data : NULL, FALSE);
1339 void menu_frame_select_next(ObMenuFrame *self)
1341 GList *it = NULL, *start;
1343 if (self->entries) {
1344 start = it = g_list_find(self->entries, self->selected);
1346 ObMenuEntryFrame *e;
1348 it = it ? g_list_next(it) : self->entries;
1354 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1356 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1361 menu_frame_select(self, it ? it->data : NULL, FALSE);
1364 void menu_frame_select_first(ObMenuFrame *self)
1368 if (self->entries) {
1369 for (it = self->entries; it; it = g_list_next(it)) {
1370 ObMenuEntryFrame *e = it->data;
1371 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1373 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1377 menu_frame_select(self, it ? it->data : NULL, FALSE);
1380 void menu_frame_select_last(ObMenuFrame *self)
1384 if (self->entries) {
1385 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1386 ObMenuEntryFrame *e = it->data;
1387 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1389 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1393 menu_frame_select(self, it ? it->data : NULL, FALSE);