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;
113 self = g_slice_new0(ObMenuFrame);
114 self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
116 self->selected = NULL;
117 self->client = client;
118 self->direction_right = TRUE;
119 self->show_from = show_from;
121 attr.event_mask = FRAME_EVENTMASK;
122 self->window = createWindow(obt_root(ob_screen),
125 /* make it a popup menu type window */
126 OBT_PROP_SET32(self->window, NET_WM_WINDOW_TYPE, ATOM,
127 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU));
129 XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
130 XSetWindowBorder(obt_display, self->window,
131 RrColorPixel(ob_rr_theme->menu_border_color));
133 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
135 window_add(&self->window, MENUFRAME_AS_WINDOW(self));
136 stacking_add(MENUFRAME_AS_WINDOW(self));
141 void menu_frame_free(ObMenuFrame *self)
144 while (self->entries) {
145 menu_entry_frame_free(self->entries->data);
146 self->entries = g_list_delete_link(self->entries, self->entries);
149 stacking_remove(MENUFRAME_AS_WINDOW(self));
150 window_remove(self->window);
152 RrAppearanceFree(self->a_items);
154 XDestroyWindow(obt_display, self->window);
156 g_slice_free(ObMenuFrame, self);
160 ObtIC* menu_frame_ic(ObMenuFrame *self)
162 /* menus are always used through a grab right now, so they can always use
163 the grab input context */
164 return grab_input_context();
167 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
170 ObMenuEntryFrame *self;
171 XSetWindowAttributes attr;
173 self = g_slice_new0(ObMenuEntryFrame);
177 menu_entry_ref(entry);
179 attr.event_mask = ENTRY_EVENTMASK;
180 self->window = createWindow(self->frame->window, CWEventMask, &attr);
181 self->text = createWindow(self->window, 0, NULL);
182 g_hash_table_insert(menu_frame_map, &self->window, self);
183 g_hash_table_insert(menu_frame_map, &self->text, self);
184 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
185 self->icon = createWindow(self->window, 0, NULL);
186 g_hash_table_insert(menu_frame_map, &self->icon, self);
188 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
189 self->bullet = createWindow(self->window, 0, NULL);
190 g_hash_table_insert(menu_frame_map, &self->bullet, self);
193 XMapWindow(obt_display, self->window);
194 XMapWindow(obt_display, self->text);
196 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
201 static void menu_entry_frame_free(ObMenuEntryFrame *self)
204 menu_entry_unref(self->entry);
206 window_remove(self->window);
208 XDestroyWindow(obt_display, self->text);
209 XDestroyWindow(obt_display, self->window);
210 g_hash_table_remove(menu_frame_map, &self->text);
211 g_hash_table_remove(menu_frame_map, &self->window);
212 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
213 XDestroyWindow(obt_display, self->icon);
214 g_hash_table_remove(menu_frame_map, &self->icon);
216 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
217 XDestroyWindow(obt_display, self->bullet);
218 g_hash_table_remove(menu_frame_map, &self->bullet);
221 g_slice_free(ObMenuEntryFrame, self);
225 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
227 RECT_SET_POINT(self->area, x, y);
228 self->monitor = screen_find_monitor_point(x, y);
229 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
232 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
236 if (config_menu_middle) {
238 *x -= self->area.width / 2;
240 /* try below the cursor */
241 menu_frame_move_on_screen(self, *x, *y, &dx, &dy);
242 self->direction_right = TRUE;
251 /* try to the bottom right of the cursor */
252 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
253 self->direction_right = TRUE;
254 if (dx != 0 || dy != 0) {
255 /* try to the bottom left of the cursor */
256 myx = *x - self->area.width;
258 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
259 self->direction_right = FALSE;
261 if (dx != 0 || dy != 0) {
262 /* try to the top right of the cursor */
264 myy = *y - self->area.height;
265 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
266 self->direction_right = TRUE;
268 if (dx != 0 || dy != 0) {
269 /* try to the top left of the cursor */
270 myx = *x - self->area.width;
271 myy = *y - self->area.height;
272 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
273 self->direction_right = FALSE;
275 if (dx != 0 || dy != 0) {
276 /* if didnt fit on either side so just use what it says */
279 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
280 self->direction_right = TRUE;
287 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
289 gint overlapx, overlapy;
292 overlapx = ob_rr_theme->menu_overlap_x;
293 overlapy = ob_rr_theme->menu_overlap_y;
294 bwidth = ob_rr_theme->mbwidth;
296 if (self->direction_right)
297 *x = self->parent->area.x + self->parent->area.width -
300 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
302 *y = self->parent->area.y + self->parent_entry->area.y;
303 if (config_menu_middle)
304 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
309 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
312 const Rect *a = NULL;
317 a = screen_physical_area_monitor(screen_find_monitor_point(x, y));
319 half = g_list_length(self->entries) / 2;
320 pos = g_list_index(self->entries, self->selected);
322 /* if in the bottom half then check this stuff first, will keep the bottom
323 edge of the menu visible */
325 *dx = MAX(*dx, a->x - x);
326 *dy = MAX(*dy, a->y - y);
328 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
329 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
330 /* if in the top half then check this stuff last, will keep the top
331 edge of the menu visible */
333 *dx = MAX(*dx, a->x - x);
334 *dy = MAX(*dy, a->y - y);
338 static void menu_entry_frame_render(ObMenuEntryFrame *self)
340 RrAppearance *item_a, *text_a;
343 ObMenuFrame *frame = self->frame;
345 switch (self->entry->type) {
346 case OB_MENU_ENTRY_TYPE_NORMAL:
347 case OB_MENU_ENTRY_TYPE_SUBMENU:
348 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
349 !self->entry->data.normal.enabled ?
351 (self == self->frame->selected ?
352 ob_rr_theme->a_menu_disabled_selected :
353 ob_rr_theme->a_menu_disabled) :
355 (self == self->frame->selected ?
356 ob_rr_theme->a_menu_selected :
357 ob_rr_theme->a_menu_normal));
360 case OB_MENU_ENTRY_TYPE_SEPARATOR:
361 if (self->entry->data.separator.label) {
362 item_a = ob_rr_theme->a_menu_title;
363 th = ob_rr_theme->menu_title_height;
365 item_a = ob_rr_theme->a_menu_normal;
366 th = ob_rr_theme->menu_sep_width +
367 2*ob_rr_theme->menu_sep_paddingy;
371 g_assert_not_reached();
374 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
375 XResizeWindow(obt_display, self->window,
376 self->area.width, self->area.height);
377 item_a->surface.parent = self->frame->a_items;
378 item_a->surface.parentx = self->area.x;
379 item_a->surface.parenty = self->area.y;
380 RrPaint(item_a, self->window, self->area.width, self->area.height);
382 switch (self->entry->type) {
383 case OB_MENU_ENTRY_TYPE_NORMAL:
384 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
385 !self->entry->data.normal.enabled ?
387 (self == self->frame->selected ?
388 ob_rr_theme->a_menu_text_disabled_selected :
389 ob_rr_theme->a_menu_text_disabled) :
391 (self == self->frame->selected ?
392 ob_rr_theme->a_menu_text_selected :
393 ob_rr_theme->a_menu_text_normal));
394 text_a->texture[0].data.text.string = self->entry->data.normal.label;
395 if (self->entry->data.normal.shortcut &&
396 (self->frame->menu->show_all_shortcuts ||
397 self->entry->data.normal.shortcut_always_show ||
398 self->entry->data.normal.shortcut_position > 0))
400 text_a->texture[0].data.text.shortcut = TRUE;
401 text_a->texture[0].data.text.shortcut_pos =
402 self->entry->data.normal.shortcut_position;
404 text_a->texture[0].data.text.shortcut = FALSE;
406 case OB_MENU_ENTRY_TYPE_SUBMENU:
407 text_a = (self == self->frame->selected ?
408 ob_rr_theme->a_menu_text_selected :
409 ob_rr_theme->a_menu_text_normal);
410 sub = self->entry->data.submenu.submenu;
411 text_a->texture[0].data.text.string = sub ? sub->title : "";
412 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
413 sub->shortcut_always_show ||
414 sub->shortcut_position > 0))
416 text_a->texture[0].data.text.shortcut = TRUE;
417 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
419 text_a->texture[0].data.text.shortcut = FALSE;
421 case OB_MENU_ENTRY_TYPE_SEPARATOR:
422 if (self->entry->data.separator.label != NULL) {
423 text_a = ob_rr_theme->a_menu_text_title;
424 text_a->texture[0].data.text.string =
425 self->entry->data.separator.label;
428 text_a = ob_rr_theme->a_menu_text_normal;
431 g_assert_not_reached();
434 switch (self->entry->type) {
435 case OB_MENU_ENTRY_TYPE_NORMAL:
436 XMoveResizeWindow(obt_display, self->text,
437 self->frame->text_x, PADDING,
439 ITEM_HEIGHT - 2*PADDING);
440 text_a->surface.parent = item_a;
441 text_a->surface.parentx = self->frame->text_x;
442 text_a->surface.parenty = PADDING;
443 RrPaint(text_a, self->text, self->frame->text_w,
444 ITEM_HEIGHT - 2*PADDING);
446 case OB_MENU_ENTRY_TYPE_SUBMENU:
447 XMoveResizeWindow(obt_display, self->text,
448 self->frame->text_x, PADDING,
449 self->frame->text_w - ITEM_HEIGHT,
450 ITEM_HEIGHT - 2*PADDING);
451 text_a->surface.parent = item_a;
452 text_a->surface.parentx = self->frame->text_x;
453 text_a->surface.parenty = PADDING;
454 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
455 ITEM_HEIGHT - 2*PADDING);
457 case OB_MENU_ENTRY_TYPE_SEPARATOR:
458 if (self->entry->data.separator.label != NULL) {
459 /* labeled separator */
460 XMoveResizeWindow(obt_display, self->text,
461 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
462 self->area.width - 2*ob_rr_theme->paddingx,
463 ob_rr_theme->menu_title_height -
464 2*ob_rr_theme->paddingy);
465 text_a->surface.parent = item_a;
466 text_a->surface.parentx = ob_rr_theme->paddingx;
467 text_a->surface.parenty = ob_rr_theme->paddingy;
468 RrPaint(text_a, self->text,
469 self->area.width - 2*ob_rr_theme->paddingx,
470 ob_rr_theme->menu_title_height -
471 2*ob_rr_theme->paddingy);
475 /* unlabeled separator */
476 XMoveResizeWindow(obt_display, self->text, 0, 0,
478 ob_rr_theme->menu_sep_width +
479 2*ob_rr_theme->menu_sep_paddingy);
481 a_sep->surface.parent = item_a;
482 a_sep->surface.parentx = 0;
483 a_sep->surface.parenty = 0;
484 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
485 a_sep->texture[i].data.lineart.x1 =
486 ob_rr_theme->menu_sep_paddingx;
487 a_sep->texture[i].data.lineart.y1 =
488 ob_rr_theme->menu_sep_paddingy + i;
489 a_sep->texture[i].data.lineart.x2 =
490 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
491 a_sep->texture[i].data.lineart.y2 =
492 ob_rr_theme->menu_sep_paddingy + i;
495 RrPaint(a_sep, self->text, self->area.width,
496 ob_rr_theme->menu_sep_width +
497 2*ob_rr_theme->menu_sep_paddingy);
501 g_assert_not_reached();
504 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
505 self->entry->data.normal.icon)
509 XMoveResizeWindow(obt_display, self->icon,
510 PADDING, frame->item_margin.top,
511 ITEM_HEIGHT - frame->item_margin.top
512 - frame->item_margin.bottom,
513 ITEM_HEIGHT - frame->item_margin.top
514 - frame->item_margin.bottom);
516 clear = ob_rr_theme->a_clear_tex;
517 RrAppearanceClearTextures(clear);
518 clear->texture[0].type = RR_TEXTURE_IMAGE;
519 clear->texture[0].data.image.image =
520 self->entry->data.normal.icon;
521 clear->texture[0].data.image.alpha =
522 self->entry->data.normal.icon_alpha;
523 clear->surface.parent = item_a;
524 clear->surface.parentx = PADDING;
525 clear->surface.parenty = frame->item_margin.top;
526 RrPaint(clear, self->icon,
527 ITEM_HEIGHT - frame->item_margin.top
528 - frame->item_margin.bottom,
529 ITEM_HEIGHT - frame->item_margin.top
530 - frame->item_margin.bottom);
531 XMapWindow(obt_display, self->icon);
532 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
533 self->entry->data.normal.mask)
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_MASK;
548 clear->texture[0].data.mask.mask =
549 self->entry->data.normal.mask;
551 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
552 !self->entry->data.normal.enabled ?
554 (self == self->frame->selected ?
555 self->entry->data.normal.mask_disabled_selected_color :
556 self->entry->data.normal.mask_disabled_color) :
558 (self == self->frame->selected ?
559 self->entry->data.normal.mask_selected_color :
560 self->entry->data.normal.mask_normal_color));
561 clear->texture[0].data.mask.color = c;
563 clear->surface.parent = item_a;
564 clear->surface.parentx = PADDING;
565 clear->surface.parenty = frame->item_margin.top;
566 RrPaint(clear, self->icon,
567 ITEM_HEIGHT - frame->item_margin.top
568 - frame->item_margin.bottom,
569 ITEM_HEIGHT - frame->item_margin.top
570 - frame->item_margin.bottom);
571 XMapWindow(obt_display, self->icon);
573 XUnmapWindow(obt_display, self->icon);
575 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
576 RrAppearance *bullet_a;
577 XMoveResizeWindow(obt_display, self->bullet,
578 self->frame->text_x + self->frame->text_w -
579 ITEM_HEIGHT + PADDING, PADDING,
580 ITEM_HEIGHT - 2*PADDING,
581 ITEM_HEIGHT - 2*PADDING);
582 bullet_a = (self == self->frame->selected ?
583 ob_rr_theme->a_menu_bullet_selected :
584 ob_rr_theme->a_menu_bullet_normal);
585 bullet_a->surface.parent = item_a;
586 bullet_a->surface.parentx =
587 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
588 bullet_a->surface.parenty = PADDING;
589 RrPaint(bullet_a, self->bullet,
590 ITEM_HEIGHT - 2*PADDING,
591 ITEM_HEIGHT - 2*PADDING);
592 XMapWindow(obt_display, self->bullet);
594 XUnmapWindow(obt_display, self->bullet);
599 /*! this code is taken from the menu_frame_render. if that changes, this won't
601 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
602 gboolean first_entry,
611 t = self->entry->type;
613 /* this is the More... entry, it's NORMAL type */
614 t = OB_MENU_ENTRY_TYPE_NORMAL;
617 case OB_MENU_ENTRY_TYPE_NORMAL:
618 case OB_MENU_ENTRY_TYPE_SUBMENU:
619 h += ob_rr_theme->menu_font_height;
621 case OB_MENU_ENTRY_TYPE_SEPARATOR:
622 if (self->entry->data.separator.label != NULL) {
623 h += ob_rr_theme->menu_title_height +
624 (ob_rr_theme->mbwidth - PADDING) * 2;
626 /* if the first entry is a labeled separator, then make its border
627 overlap with the menu's outside border */
629 h -= ob_rr_theme->mbwidth;
630 /* if the last entry is a labeled separator, then make its border
631 overlap with the menu's outside border */
633 h -= ob_rr_theme->mbwidth;
635 h += ob_rr_theme->menu_sep_width +
636 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
644 void menu_frame_render(ObMenuFrame *self)
647 gint tw, th; /* temps */
649 gboolean has_icon = FALSE;
653 /* find text dimensions */
655 STRUT_SET(self->item_margin, 0, 0, 0, 0);
660 e = self->entries->data;
661 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
662 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
667 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
668 STRUT_SET(self->item_margin,
669 MAX(self->item_margin.left, l),
670 MAX(self->item_margin.top, t),
671 MAX(self->item_margin.right, r),
672 MAX(self->item_margin.bottom, b));
673 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
674 STRUT_SET(self->item_margin,
675 MAX(self->item_margin.left, l),
676 MAX(self->item_margin.top, t),
677 MAX(self->item_margin.right, r),
678 MAX(self->item_margin.bottom, b));
679 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
680 STRUT_SET(self->item_margin,
681 MAX(self->item_margin.left, l),
682 MAX(self->item_margin.top, t),
683 MAX(self->item_margin.right, r),
684 MAX(self->item_margin.bottom, b));
685 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
686 STRUT_SET(self->item_margin,
687 MAX(self->item_margin.left, l),
688 MAX(self->item_margin.top, t),
689 MAX(self->item_margin.right, r),
690 MAX(self->item_margin.bottom, b));
693 /* render the entries */
695 for (it = self->entries; it; it = g_list_next(it)) {
696 RrAppearance *text_a;
699 /* if the first entry is a labeled separator, then make its border
700 overlap with the menu's outside border */
701 if (it == self->entries &&
702 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
703 e->entry->data.separator.label)
705 h -= ob_rr_theme->mbwidth;
708 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
709 e->entry->data.separator.label)
711 e->border = ob_rr_theme->mbwidth;
714 RECT_SET_POINT(e->area, 0, h+e->border);
715 XMoveWindow(obt_display, e->window,
716 e->area.x-e->border, e->area.y-e->border);
717 XSetWindowBorderWidth(obt_display, e->window, e->border);
718 XSetWindowBorder(obt_display, e->window,
719 RrColorPixel(ob_rr_theme->menu_border_color));
721 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
722 !e->entry->data.normal.enabled ?
724 (e == self->selected ?
725 ob_rr_theme->a_menu_text_disabled_selected :
726 ob_rr_theme->a_menu_text_disabled) :
728 (e == self->selected ?
729 ob_rr_theme->a_menu_text_selected :
730 ob_rr_theme->a_menu_text_normal));
731 switch (e->entry->type) {
732 case OB_MENU_ENTRY_TYPE_NORMAL:
733 text_a->texture[0].data.text.string = e->entry->data.normal.label;
734 tw = RrMinWidth(text_a);
735 tw = MIN(tw, MAX_MENU_WIDTH);
736 th = ob_rr_theme->menu_font_height;
738 if (e->entry->data.normal.icon ||
739 e->entry->data.normal.mask)
742 case OB_MENU_ENTRY_TYPE_SUBMENU:
743 sub = e->entry->data.submenu.submenu;
744 text_a->texture[0].data.text.string = sub ? sub->title : "";
745 tw = RrMinWidth(text_a);
746 tw = MIN(tw, MAX_MENU_WIDTH);
747 th = ob_rr_theme->menu_font_height;
749 if (e->entry->data.normal.icon ||
750 e->entry->data.normal.mask)
753 tw += ITEM_HEIGHT - PADDING;
755 case OB_MENU_ENTRY_TYPE_SEPARATOR:
756 if (e->entry->data.separator.label != NULL) {
757 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
758 e->entry->data.separator.label;
759 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
760 2*ob_rr_theme->paddingx;
761 tw = MIN(tw, MAX_MENU_WIDTH);
762 th = ob_rr_theme->menu_title_height +
763 (ob_rr_theme->mbwidth - PADDING) *2;
766 th = ob_rr_theme->menu_sep_width +
767 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
771 g_assert_not_reached();
781 /* if the last entry is a labeled separator, then make its border
782 overlap with the menu's outside border */
783 it = g_list_last(self->entries);
784 e = it ? it->data : NULL;
785 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
786 e->entry->data.separator.label)
788 h -= ob_rr_theme->mbwidth;
791 self->text_x = PADDING;
796 w += ITEM_HEIGHT + PADDING;
797 self->text_x += ITEM_HEIGHT + PADDING;
804 XResizeWindow(obt_display, self->window, w, h);
808 RrPaint(self->a_items, self->window, w, h);
810 for (it = self->entries; it; it = g_list_next(it))
811 menu_entry_frame_render(it->data);
813 w += ob_rr_theme->mbwidth * 2;
814 h += ob_rr_theme->mbwidth * 2;
816 RECT_SET_SIZE(self->area, w, h);
821 static void menu_frame_update(ObMenuFrame *self)
827 menu_pipe_execute(self->menu);
828 menu_find_submenus(self->menu);
830 self->selected = NULL;
832 /* start at show_from */
833 mit = g_list_nth(self->menu->entries, self->show_from);
835 /* go through the menu's and frame's entries and connect the frame entries
836 to the menu entries */
837 for (fit = self->entries; mit && fit;
838 mit = g_list_next(mit), fit = g_list_next(fit))
840 ObMenuEntryFrame *f = fit->data;
841 f->entry = mit->data;
844 /* if there are more menu entries than in the frame, add them */
846 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
847 self->entries = g_list_append(self->entries, e);
848 mit = g_list_next(mit);
851 /* if there are more frame entries than menu entries then get rid of
854 GList *n = g_list_next(fit);
855 menu_entry_frame_free(fit->data);
856 self->entries = g_list_delete_link(self->entries, fit);
860 /* * make the menu fit on the screen */
862 /* calculate the height of the menu */
864 for (fit = self->entries; fit; fit = g_list_next(fit))
865 h += menu_entry_frame_get_height(fit->data,
866 fit == self->entries,
867 g_list_next(fit) == NULL);
868 /* add the border at the top and bottom */
869 h += ob_rr_theme->mbwidth * 2;
871 a = screen_physical_area_monitor(self->monitor);
875 gboolean last_entry = TRUE;
877 /* take the height of our More... entry into account */
878 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
880 /* start at the end of the entries */
881 flast = g_list_last(self->entries);
883 /* pull out all the entries from the frame that don't
884 fit on the screen, leaving at least 1 though */
885 while (h > a->height && g_list_previous(flast) != NULL) {
886 /* update the height, without this entry */
887 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
889 /* destroy the entry we're not displaying */
891 flast = g_list_previous(flast);
892 menu_entry_frame_free(tmp->data);
893 self->entries = g_list_delete_link(self->entries, tmp);
895 /* only the first one that we see is the last entry in the menu */
900 ObMenuEntry *more_entry;
901 ObMenuEntryFrame *more_frame;
902 /* make the More... menu entry frame which will display in this
904 if self->menu->more_menu is NULL that means that this is already
905 More... menu, so just use ourself.
907 more_entry = menu_get_more((self->menu->more_menu ?
908 self->menu->more_menu :
910 /* continue where we left off */
912 g_list_length(self->entries));
913 more_frame = menu_entry_frame_new(more_entry, self);
914 /* make it get deleted when the menu frame goes away */
915 menu_entry_unref(more_entry);
917 /* add our More... entry to the frame */
918 self->entries = g_list_append(self->entries, more_frame);
922 menu_frame_render(self);
925 static gboolean menu_frame_is_visible(ObMenuFrame *self)
927 return !!(g_list_find(menu_frame_visible, self));
930 static gboolean menu_frame_show(ObMenuFrame *self)
934 /* determine if the underlying menu is already visible */
935 for (it = menu_frame_visible; it; it = g_list_next(it)) {
936 ObMenuFrame *f = it->data;
937 if (f->menu == self->menu)
941 if (self->menu->update_func)
942 if (!self->menu->update_func(self, self->menu->data))
946 if (menu_frame_visible == NULL) {
947 /* no menus shown yet */
949 /* grab the pointer in such a way as to pass through "owner events"
950 so that we can get enter/leave notifies in the menu. */
951 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
953 if (!grab_keyboard()) {
959 menu_frame_update(self);
961 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
963 if (self->menu->show_func)
964 self->menu->show_func(self, self->menu->data);
969 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
974 if (menu_frame_is_visible(self))
976 if (!menu_frame_show(self))
979 if (self->menu->place_func)
980 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
982 menu_frame_place_topmenu(self, &x, &y);
984 menu_frame_move(self, x, y);
986 XMapWindow(obt_display, self->window);
988 if (screen_pointer_pos(&px, &py)) {
989 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
990 if (e && e->frame == self)
997 /*! Stop hiding an open submenu.
998 @child The OnMenuFrame of the submenu to be hidden
1000 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1002 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1003 submenu_hide_timer = 0;
1006 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1007 ObMenuEntryFrame *parent_entry)
1012 if (menu_frame_is_visible(self))
1015 self->monitor = parent->monitor;
1016 self->parent = parent;
1017 self->parent_entry = parent_entry;
1019 /* set up parent's child to be us */
1020 if ((parent->child) != self) {
1022 menu_frame_hide(parent->child);
1023 parent->child = self;
1024 parent->child_entry = parent_entry;
1027 if (!menu_frame_show(self))
1030 menu_frame_place_submenu(self, &x, &y);
1031 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1034 /*try the other side */
1035 self->direction_right = !self->direction_right;
1036 menu_frame_place_submenu(self, &x, &y);
1037 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1039 menu_frame_move(self, x + dx, y + dy);
1041 XMapWindow(obt_display, self->window);
1043 if (screen_pointer_pos(&px, &py)) {
1044 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1045 if (e && e->frame == self)
1052 static void menu_frame_hide(ObMenuFrame *self)
1054 ObMenu *const menu = self->menu;
1055 GList *it = g_list_find(menu_frame_visible, self);
1056 gulong ignore_start;
1061 if (menu->hide_func)
1062 menu->hide_func(self, menu->data);
1065 menu_frame_hide(self->child);
1068 remove_submenu_hide_timeout(self);
1070 self->parent->child = NULL;
1071 self->parent->child_entry = NULL;
1073 self->parent = NULL;
1074 self->parent_entry = NULL;
1076 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1078 if (menu_frame_visible == NULL) {
1079 /* last menu shown */
1084 ignore_start = event_start_ignore_all_enters();
1085 XUnmapWindow(obt_display, self->window);
1086 event_end_ignore_all_enters(ignore_start);
1088 menu_frame_free(self);
1090 if (menu->cleanup_func)
1091 menu->cleanup_func(menu, menu->data);
1094 void menu_frame_hide_all(void)
1098 if (config_submenu_show_delay) {
1099 /* remove any submenu open requests */
1100 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1101 submenu_show_timer = 0;
1103 if ((it = g_list_last(menu_frame_visible)))
1104 menu_frame_hide(it->data);
1107 ObMenuFrame* menu_frame_under(gint x, gint y)
1109 ObMenuFrame *ret = NULL;
1112 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1113 ObMenuFrame *f = it->data;
1115 if (RECT_CONTAINS(f->area, x, y)) {
1123 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1126 ObMenuEntryFrame *ret = NULL;
1129 if ((frame = menu_frame_under(x, y))) {
1130 x -= ob_rr_theme->mbwidth + frame->area.x;
1131 y -= ob_rr_theme->mbwidth + frame->area.y;
1133 for (it = frame->entries; it; it = g_list_next(it)) {
1134 ObMenuEntryFrame *e = it->data;
1136 if (RECT_CONTAINS(e->area, x, y)) {
1145 static gboolean submenu_show_timeout(gpointer data)
1147 g_assert(menu_frame_visible);
1148 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1152 static gboolean submenu_hide_timeout(gpointer data)
1154 g_assert(menu_frame_visible);
1155 menu_frame_hide((ObMenuFrame*)data);
1159 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1162 ObMenuEntryFrame *old = self->selected;
1163 ObMenuFrame *oldchild = self->child;
1164 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1166 /* if the user selected a separator, ignore it and reselect what we had
1168 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1172 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1175 /* if the user left this menu but we have a submenu open, move the
1176 selection back to that submenu */
1177 if (!entry && oldchild_entry)
1178 entry = oldchild_entry;
1180 if (config_submenu_show_delay) {
1181 /* remove any submenu open requests */
1182 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1183 submenu_show_timer = 0;
1186 self->selected = entry;
1189 menu_entry_frame_render(old);
1191 if (oldchild_entry) {
1192 /* There is an open submenu */
1193 if (oldchild_entry == self->selected) {
1194 /* The open submenu has been reselected, so stop hiding the
1196 remove_submenu_hide_timeout(oldchild);
1198 else if (oldchild_entry == old) {
1199 /* The open submenu was selected and is no longer, so hide the
1201 if (immediate || config_submenu_hide_delay == 0)
1202 menu_frame_hide(oldchild);
1203 else if (config_submenu_hide_delay > 0) {
1204 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1205 submenu_hide_timer =
1206 g_timeout_add_full(G_PRIORITY_DEFAULT,
1207 config_submenu_hide_delay,
1208 submenu_hide_timeout, oldchild, NULL);
1213 if (self->selected) {
1214 menu_entry_frame_render(self->selected);
1216 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1217 /* only show if the submenu isn't already showing */
1218 if (oldchild_entry != self->selected) {
1219 if (immediate || config_submenu_hide_delay == 0)
1220 menu_entry_frame_show_submenu(self->selected);
1221 else if (config_submenu_hide_delay > 0) {
1222 if (submenu_show_timer)
1223 g_source_remove(submenu_show_timer);
1224 submenu_show_timer =
1225 g_timeout_add_full(G_PRIORITY_DEFAULT,
1226 config_submenu_show_delay,
1227 submenu_show_timeout,
1228 self->selected, NULL);
1231 /* hide the grandchildren of this menu. and move the cursor to
1233 else if (immediate && self->child && self->child->child) {
1234 menu_frame_hide(self->child->child);
1235 menu_frame_select(self->child, NULL, TRUE);
1241 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1245 if (!self->entry->data.submenu.submenu) return;
1247 f = menu_frame_new(self->entry->data.submenu.submenu,
1248 self->entry->data.submenu.show_from,
1249 self->frame->client);
1250 /* pass our direction on to our child */
1251 f->direction_right = self->frame->direction_right;
1253 menu_frame_show_submenu(f, self->frame, self);
1256 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1258 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1259 self->entry->data.normal.enabled)
1261 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1263 ObMenuEntry *entry = self->entry;
1264 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1265 gpointer data = self->frame->menu->data;
1266 GSList *acts = self->entry->data.normal.actions;
1267 ObClient *client = self->frame->client;
1268 ObMenuFrame *frame = self->frame;
1269 guint mods = obt_keyboard_only_modmasks(state);
1271 /* release grabs before executing the shit */
1272 if (!(mods & ControlMask)) {
1273 event_cancel_all_key_grabs();
1278 func(entry, frame, client, state, data);
1280 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1281 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1285 void menu_frame_select_previous(ObMenuFrame *self)
1287 GList *it = NULL, *start;
1289 if (self->entries) {
1290 start = it = g_list_find(self->entries, self->selected);
1292 ObMenuEntryFrame *e;
1294 it = it ? g_list_previous(it) : g_list_last(self->entries);
1300 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1302 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1307 menu_frame_select(self, it ? it->data : NULL, FALSE);
1310 void menu_frame_select_next(ObMenuFrame *self)
1312 GList *it = NULL, *start;
1314 if (self->entries) {
1315 start = it = g_list_find(self->entries, self->selected);
1317 ObMenuEntryFrame *e;
1319 it = it ? g_list_next(it) : self->entries;
1325 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1327 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1332 menu_frame_select(self, it ? it->data : NULL, FALSE);
1335 void menu_frame_select_first(ObMenuFrame *self)
1339 if (self->entries) {
1340 for (it = self->entries; it; it = g_list_next(it)) {
1341 ObMenuEntryFrame *e = it->data;
1342 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1344 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1348 menu_frame_select(self, it ? it->data : NULL, FALSE);
1351 void menu_frame_select_last(ObMenuFrame *self)
1355 if (self->entries) {
1356 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1357 ObMenuEntryFrame *e = it->data;
1358 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1360 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1364 menu_frame_select(self, it ? it->data : NULL, FALSE);