1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 menuframe.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "menuframe.h"
30 #include "obt/keyboard.h"
31 #include "obrender/theme.h"
34 #define MAX_MENU_WIDTH 400
36 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
38 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
40 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
41 ButtonPressMask | ButtonReleaseMask | \
44 GList *menu_frame_visible;
45 GHashTable *menu_frame_map;
47 static RrAppearance *a_sep;
48 static guint submenu_show_timer = 0;
49 static guint submenu_hide_timer = 0;
51 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
53 static void menu_entry_frame_free(ObMenuEntryFrame *self);
54 static void menu_frame_update(ObMenuFrame *self);
55 static gboolean submenu_show_timeout(gpointer data);
56 static void menu_frame_hide(ObMenuFrame *self);
58 static gboolean submenu_hide_timeout(gpointer data);
60 static Window createWindow(Window parent, gulong mask,
61 XSetWindowAttributes *attrib)
63 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
64 RrDepth(ob_rr_inst), InputOutput,
65 RrVisual(ob_rr_inst), mask, attrib);
68 static void client_dest(ObClient *client, gpointer data)
72 /* menus can be associated with a client, so null those refs since
73 we are disappearing now */
74 for (it = menu_frame_visible; it; it = g_list_next(it)) {
75 ObMenuFrame *f = it->data;
76 if (f->client == client)
81 void menu_frame_startup(gboolean reconfig)
85 a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
86 RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
87 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
88 a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
89 a_sep->texture[i].data.lineart.color =
90 ob_rr_theme->menu_sep_color;
95 client_add_destroy_notify(client_dest, NULL);
96 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
99 void menu_frame_shutdown(gboolean reconfig)
101 RrAppearanceFree(a_sep);
103 if (reconfig) return;
105 client_remove_destroy_notify(client_dest);
106 g_hash_table_destroy(menu_frame_map);
109 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
112 XSetWindowAttributes attr;
114 self = g_slice_new0(ObMenuFrame);
115 self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
117 self->selected = NULL;
118 self->client = client;
119 self->direction_right = TRUE;
120 self->show_from = show_from;
122 attr.event_mask = FRAME_EVENTMASK;
123 self->window = createWindow(obt_root(ob_screen),
126 /* make it a popup menu type window */
127 OBT_PROP_SET32(self->window, NET_WM_WINDOW_TYPE, ATOM,
128 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU));
130 XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
131 XSetWindowBorder(obt_display, self->window,
132 RrColorPixel(ob_rr_theme->menu_border_color));
134 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
136 window_add(&self->window, MENUFRAME_AS_WINDOW(self));
137 stacking_add(MENUFRAME_AS_WINDOW(self));
142 void menu_frame_free(ObMenuFrame *self)
145 while (self->entries) {
146 menu_entry_frame_free(self->entries->data);
147 self->entries = g_list_delete_link(self->entries, self->entries);
150 stacking_remove(MENUFRAME_AS_WINDOW(self));
151 window_remove(self->window);
153 RrAppearanceFree(self->a_items);
155 XDestroyWindow(obt_display, self->window);
157 g_slice_free(ObMenuFrame, self);
161 ObtIC* menu_frame_ic(ObMenuFrame *self)
163 /* menus are always used through a grab right now, so they can always use
164 the grab input context */
165 return grab_input_context();
168 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
171 ObMenuEntryFrame *self;
172 XSetWindowAttributes attr;
174 self = g_slice_new0(ObMenuEntryFrame);
178 menu_entry_ref(entry);
180 attr.event_mask = ENTRY_EVENTMASK;
181 self->window = createWindow(self->frame->window, CWEventMask, &attr);
182 self->text = createWindow(self->window, 0, NULL);
183 g_hash_table_insert(menu_frame_map, &self->window, self);
184 g_hash_table_insert(menu_frame_map, &self->text, self);
185 if ((entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
186 (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
187 self->icon = createWindow(self->window, 0, NULL);
188 g_hash_table_insert(menu_frame_map, &self->icon, self);
190 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
191 self->bullet = createWindow(self->window, 0, NULL);
192 g_hash_table_insert(menu_frame_map, &self->bullet, self);
195 XMapWindow(obt_display, self->window);
196 XMapWindow(obt_display, self->text);
198 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
203 static void menu_entry_frame_free(ObMenuEntryFrame *self)
206 menu_entry_unref(self->entry);
208 window_remove(self->window);
210 XDestroyWindow(obt_display, self->text);
211 XDestroyWindow(obt_display, self->window);
212 g_hash_table_remove(menu_frame_map, &self->text);
213 g_hash_table_remove(menu_frame_map, &self->window);
214 if ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
215 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
216 XDestroyWindow(obt_display, self->icon);
217 g_hash_table_remove(menu_frame_map, &self->icon);
219 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
220 XDestroyWindow(obt_display, self->bullet);
221 g_hash_table_remove(menu_frame_map, &self->bullet);
224 g_slice_free(ObMenuEntryFrame, self);
228 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
230 RECT_SET_POINT(self->area, x, y);
231 self->monitor = screen_find_monitor_point(x, y);
232 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
235 static void menu_frame_place_topmenu(ObMenuFrame *self, const GravityPoint *pos,
236 gint *x, gint *y, gint monitor,
237 gboolean user_positioned)
241 screen_apply_gravity_point(x, y, self->area.width, self->area.height,
242 pos, screen_physical_area_monitor(monitor));
247 if (config_menu_middle) {
249 *x -= self->area.width / 2;
251 /* try below the cursor */
252 menu_frame_move_on_screen(self, *x, *y, &dx, &dy);
253 self->direction_right = TRUE;
262 /* try to the bottom right of the cursor */
263 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
264 self->direction_right = TRUE;
265 if (dx != 0 || dy != 0) {
266 /* try to the bottom left of the cursor */
267 myx = *x - self->area.width;
269 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
270 self->direction_right = FALSE;
272 if (dx != 0 || dy != 0) {
273 /* try to the top right of the cursor */
275 myy = *y - self->area.height;
276 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
277 self->direction_right = TRUE;
279 if (dx != 0 || dy != 0) {
280 /* try to the top left of the cursor */
281 myx = *x - self->area.width;
282 myy = *y - self->area.height;
283 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
284 self->direction_right = FALSE;
286 if (dx != 0 || dy != 0) {
287 /* if didnt fit on either side so just use what it says */
290 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
291 self->direction_right = TRUE;
298 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
300 gint overlapx, overlapy;
303 overlapx = ob_rr_theme->menu_overlap_x;
304 overlapy = ob_rr_theme->menu_overlap_y;
305 bwidth = ob_rr_theme->mbwidth;
307 if (self->direction_right)
308 *x = self->parent->area.x + self->parent->area.width -
311 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
313 *y = self->parent->area.y + self->parent_entry->area.y;
314 if (config_menu_middle)
315 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
320 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
323 const Rect *a = NULL;
324 Rect search = self->area;
325 gint pos, half, monitor;
328 RECT_SET_POINT(search, x, y);
331 monitor = self->parent->monitor;
333 monitor = screen_find_monitor(&search);
335 a = screen_physical_area_monitor(monitor);
337 half = g_list_length(self->entries) / 2;
338 pos = g_list_index(self->entries, self->selected);
340 /* if in the bottom half then check this stuff first, will keep the bottom
341 edge of the menu visible */
343 *dx = MAX(*dx, a->x - x);
344 *dy = MAX(*dy, a->y - y);
346 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
347 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
348 /* if in the top half then check this stuff last, will keep the top
349 edge of the menu visible */
351 *dx = MAX(*dx, a->x - x);
352 *dy = MAX(*dy, a->y - y);
356 static void menu_entry_frame_render(ObMenuEntryFrame *self)
358 RrAppearance *item_a, *text_a;
361 ObMenuFrame *frame = self->frame;
363 switch (self->entry->type) {
364 case OB_MENU_ENTRY_TYPE_NORMAL:
365 case OB_MENU_ENTRY_TYPE_SUBMENU:
366 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
367 !self->entry->data.normal.enabled ?
369 (self == self->frame->selected ?
370 ob_rr_theme->a_menu_disabled_selected :
371 ob_rr_theme->a_menu_disabled) :
373 (self == self->frame->selected ?
374 ob_rr_theme->a_menu_selected :
375 ob_rr_theme->a_menu_normal));
378 case OB_MENU_ENTRY_TYPE_SEPARATOR:
379 if (self->entry->data.separator.label) {
380 item_a = ob_rr_theme->a_menu_title;
381 th = ob_rr_theme->menu_title_height;
383 item_a = ob_rr_theme->a_menu_normal;
384 th = ob_rr_theme->menu_sep_width +
385 2*ob_rr_theme->menu_sep_paddingy;
389 g_assert_not_reached();
392 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
393 XResizeWindow(obt_display, self->window,
394 self->area.width, self->area.height);
395 item_a->surface.parent = self->frame->a_items;
396 item_a->surface.parentx = self->area.x;
397 item_a->surface.parenty = self->area.y;
398 RrPaint(item_a, self->window, self->area.width, self->area.height);
400 switch (self->entry->type) {
401 case OB_MENU_ENTRY_TYPE_NORMAL:
402 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
403 !self->entry->data.normal.enabled ?
405 (self == self->frame->selected ?
406 ob_rr_theme->a_menu_text_disabled_selected :
407 ob_rr_theme->a_menu_text_disabled) :
409 (self == self->frame->selected ?
410 ob_rr_theme->a_menu_text_selected :
411 ob_rr_theme->a_menu_text_normal));
412 text_a->texture[0].data.text.string = self->entry->data.normal.label;
413 if (self->entry->data.normal.shortcut &&
414 (self->frame->menu->show_all_shortcuts ||
415 self->entry->data.normal.shortcut_always_show ||
416 self->entry->data.normal.shortcut_position > 0))
418 text_a->texture[0].data.text.shortcut = TRUE;
419 text_a->texture[0].data.text.shortcut_pos =
420 self->entry->data.normal.shortcut_position;
422 text_a->texture[0].data.text.shortcut = FALSE;
424 case OB_MENU_ENTRY_TYPE_SUBMENU:
425 text_a = (self == self->frame->selected ?
426 ob_rr_theme->a_menu_text_selected :
427 ob_rr_theme->a_menu_text_normal);
428 sub = self->entry->data.submenu.submenu;
429 text_a->texture[0].data.text.string = sub ? sub->title : "";
430 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
431 sub->shortcut_always_show ||
432 sub->shortcut_position > 0))
434 text_a->texture[0].data.text.shortcut = TRUE;
435 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
437 text_a->texture[0].data.text.shortcut = FALSE;
439 case OB_MENU_ENTRY_TYPE_SEPARATOR:
440 if (self->entry->data.separator.label != NULL) {
441 text_a = ob_rr_theme->a_menu_text_title;
442 text_a->texture[0].data.text.string =
443 self->entry->data.separator.label;
446 text_a = ob_rr_theme->a_menu_text_normal;
449 g_assert_not_reached();
452 switch (self->entry->type) {
453 case OB_MENU_ENTRY_TYPE_NORMAL:
454 XMoveResizeWindow(obt_display, self->text,
455 self->frame->text_x, PADDING,
457 ITEM_HEIGHT - 2*PADDING);
458 text_a->surface.parent = item_a;
459 text_a->surface.parentx = self->frame->text_x;
460 text_a->surface.parenty = PADDING;
461 RrPaint(text_a, self->text, self->frame->text_w,
462 ITEM_HEIGHT - 2*PADDING);
464 case OB_MENU_ENTRY_TYPE_SUBMENU:
465 XMoveResizeWindow(obt_display, self->text,
466 self->frame->text_x, PADDING,
467 self->frame->text_w - ITEM_HEIGHT,
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 - ITEM_HEIGHT,
473 ITEM_HEIGHT - 2*PADDING);
475 case OB_MENU_ENTRY_TYPE_SEPARATOR:
476 if (self->entry->data.separator.label != NULL) {
477 /* labeled separator */
478 XMoveResizeWindow(obt_display, self->text,
479 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
480 self->area.width - 2*ob_rr_theme->paddingx,
481 ob_rr_theme->menu_title_height -
482 2*ob_rr_theme->paddingy);
483 text_a->surface.parent = item_a;
484 text_a->surface.parentx = ob_rr_theme->paddingx;
485 text_a->surface.parenty = ob_rr_theme->paddingy;
486 RrPaint(text_a, self->text,
487 self->area.width - 2*ob_rr_theme->paddingx,
488 ob_rr_theme->menu_title_height -
489 2*ob_rr_theme->paddingy);
493 /* unlabeled separator */
494 XMoveResizeWindow(obt_display, self->text, 0, 0,
496 ob_rr_theme->menu_sep_width +
497 2*ob_rr_theme->menu_sep_paddingy);
499 a_sep->surface.parent = item_a;
500 a_sep->surface.parentx = 0;
501 a_sep->surface.parenty = 0;
502 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
503 a_sep->texture[i].data.lineart.x1 =
504 ob_rr_theme->menu_sep_paddingx;
505 a_sep->texture[i].data.lineart.y1 =
506 ob_rr_theme->menu_sep_paddingy + i;
507 a_sep->texture[i].data.lineart.x2 =
508 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
509 a_sep->texture[i].data.lineart.y2 =
510 ob_rr_theme->menu_sep_paddingy + i;
513 RrPaint(a_sep, self->text, self->area.width,
514 ob_rr_theme->menu_sep_width +
515 2*ob_rr_theme->menu_sep_paddingy);
519 g_assert_not_reached();
522 if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
523 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
524 self->entry->data.normal.icon)
528 XMoveResizeWindow(obt_display, self->icon,
529 PADDING, frame->item_margin.top,
530 ITEM_HEIGHT - frame->item_margin.top
531 - frame->item_margin.bottom,
532 ITEM_HEIGHT - frame->item_margin.top
533 - frame->item_margin.bottom);
535 clear = ob_rr_theme->a_clear_tex;
536 RrAppearanceClearTextures(clear);
537 clear->texture[0].type = RR_TEXTURE_IMAGE;
538 clear->texture[0].data.image.image =
539 self->entry->data.normal.icon;
540 clear->texture[0].data.image.alpha =
541 self->entry->data.normal.icon_alpha;
542 clear->surface.parent = item_a;
543 clear->surface.parentx = PADDING;
544 clear->surface.parenty = frame->item_margin.top;
545 RrPaint(clear, self->icon,
546 ITEM_HEIGHT - frame->item_margin.top
547 - frame->item_margin.bottom,
548 ITEM_HEIGHT - frame->item_margin.top
549 - frame->item_margin.bottom);
550 XMapWindow(obt_display, self->icon);
551 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
552 self->entry->data.normal.mask)
557 XMoveResizeWindow(obt_display, self->icon,
558 PADDING, frame->item_margin.top,
559 ITEM_HEIGHT - frame->item_margin.top
560 - frame->item_margin.bottom,
561 ITEM_HEIGHT - frame->item_margin.top
562 - frame->item_margin.bottom);
564 clear = ob_rr_theme->a_clear_tex;
565 RrAppearanceClearTextures(clear);
566 clear->texture[0].type = RR_TEXTURE_MASK;
567 clear->texture[0].data.mask.mask =
568 self->entry->data.normal.mask;
570 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
571 !self->entry->data.normal.enabled ?
573 (self == self->frame->selected ?
574 self->entry->data.normal.mask_disabled_selected_color :
575 self->entry->data.normal.mask_disabled_color) :
577 (self == self->frame->selected ?
578 self->entry->data.normal.mask_selected_color :
579 self->entry->data.normal.mask_normal_color));
580 clear->texture[0].data.mask.color = c;
582 clear->surface.parent = item_a;
583 clear->surface.parentx = PADDING;
584 clear->surface.parenty = frame->item_margin.top;
585 RrPaint(clear, self->icon,
586 ITEM_HEIGHT - frame->item_margin.top
587 - frame->item_margin.bottom,
588 ITEM_HEIGHT - frame->item_margin.top
589 - frame->item_margin.bottom);
590 XMapWindow(obt_display, self->icon);
592 XUnmapWindow(obt_display, self->icon);
594 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
595 RrAppearance *bullet_a;
596 XMoveResizeWindow(obt_display, self->bullet,
597 self->frame->text_x + self->frame->text_w -
598 ITEM_HEIGHT + PADDING, PADDING,
599 ITEM_HEIGHT - 2*PADDING,
600 ITEM_HEIGHT - 2*PADDING);
601 bullet_a = (self == self->frame->selected ?
602 ob_rr_theme->a_menu_bullet_selected :
603 ob_rr_theme->a_menu_bullet_normal);
604 bullet_a->surface.parent = item_a;
605 bullet_a->surface.parentx =
606 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
607 bullet_a->surface.parenty = PADDING;
608 RrPaint(bullet_a, self->bullet,
609 ITEM_HEIGHT - 2*PADDING,
610 ITEM_HEIGHT - 2*PADDING);
611 XMapWindow(obt_display, self->bullet);
613 XUnmapWindow(obt_display, self->bullet);
618 /*! this code is taken from the menu_frame_render. if that changes, this won't
620 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
621 gboolean first_entry,
630 t = self->entry->type;
632 /* this is the More... entry, it's NORMAL type */
633 t = OB_MENU_ENTRY_TYPE_NORMAL;
636 case OB_MENU_ENTRY_TYPE_NORMAL:
637 case OB_MENU_ENTRY_TYPE_SUBMENU:
638 h += ob_rr_theme->menu_font_height;
640 case OB_MENU_ENTRY_TYPE_SEPARATOR:
641 if (self->entry->data.separator.label != NULL) {
642 h += ob_rr_theme->menu_title_height +
643 (ob_rr_theme->mbwidth - PADDING) * 2;
645 /* if the first entry is a labeled separator, then make its border
646 overlap with the menu's outside border */
648 h -= ob_rr_theme->mbwidth;
649 /* if the last entry is a labeled separator, then make its border
650 overlap with the menu's outside border */
652 h -= ob_rr_theme->mbwidth;
654 h += ob_rr_theme->menu_sep_width +
655 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
663 void menu_frame_render(ObMenuFrame *self)
666 gint tw, th; /* temps */
668 gboolean has_icon = FALSE;
672 /* find text dimensions */
674 STRUT_SET(self->item_margin, 0, 0, 0, 0);
679 e = self->entries->data;
680 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
681 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
686 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
687 STRUT_SET(self->item_margin,
688 MAX(self->item_margin.left, l),
689 MAX(self->item_margin.top, t),
690 MAX(self->item_margin.right, r),
691 MAX(self->item_margin.bottom, b));
692 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
693 STRUT_SET(self->item_margin,
694 MAX(self->item_margin.left, l),
695 MAX(self->item_margin.top, t),
696 MAX(self->item_margin.right, r),
697 MAX(self->item_margin.bottom, b));
698 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
699 STRUT_SET(self->item_margin,
700 MAX(self->item_margin.left, l),
701 MAX(self->item_margin.top, t),
702 MAX(self->item_margin.right, r),
703 MAX(self->item_margin.bottom, b));
704 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
705 STRUT_SET(self->item_margin,
706 MAX(self->item_margin.left, l),
707 MAX(self->item_margin.top, t),
708 MAX(self->item_margin.right, r),
709 MAX(self->item_margin.bottom, b));
712 /* render the entries */
714 for (it = self->entries; it; it = g_list_next(it)) {
715 RrAppearance *text_a;
718 /* if the first entry is a labeled separator, then make its border
719 overlap with the menu's outside border */
720 if (it == self->entries &&
721 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
722 e->entry->data.separator.label)
724 h -= ob_rr_theme->mbwidth;
727 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
728 e->entry->data.separator.label)
730 e->border = ob_rr_theme->mbwidth;
733 RECT_SET_POINT(e->area, 0, h+e->border);
734 XMoveWindow(obt_display, e->window,
735 e->area.x-e->border, e->area.y-e->border);
736 XSetWindowBorderWidth(obt_display, e->window, e->border);
737 XSetWindowBorder(obt_display, e->window,
738 RrColorPixel(ob_rr_theme->menu_border_color));
740 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
741 !e->entry->data.normal.enabled ?
743 (e == self->selected ?
744 ob_rr_theme->a_menu_text_disabled_selected :
745 ob_rr_theme->a_menu_text_disabled) :
747 (e == self->selected ?
748 ob_rr_theme->a_menu_text_selected :
749 ob_rr_theme->a_menu_text_normal));
750 switch (e->entry->type) {
751 case OB_MENU_ENTRY_TYPE_NORMAL:
752 text_a->texture[0].data.text.string = e->entry->data.normal.label;
753 tw = RrMinWidth(text_a);
754 tw = MIN(tw, MAX_MENU_WIDTH);
755 th = ob_rr_theme->menu_font_height;
757 if (e->entry->data.normal.icon ||
758 e->entry->data.normal.mask)
761 case OB_MENU_ENTRY_TYPE_SUBMENU:
762 sub = e->entry->data.submenu.submenu;
763 text_a->texture[0].data.text.string = sub ? sub->title : "";
764 tw = RrMinWidth(text_a);
765 tw = MIN(tw, MAX_MENU_WIDTH);
766 th = ob_rr_theme->menu_font_height;
768 if (e->entry->data.normal.icon ||
769 e->entry->data.normal.mask)
772 tw += ITEM_HEIGHT - PADDING;
774 case OB_MENU_ENTRY_TYPE_SEPARATOR:
775 if (e->entry->data.separator.label != NULL) {
776 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
777 e->entry->data.separator.label;
778 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
779 2*ob_rr_theme->paddingx;
780 tw = MIN(tw, MAX_MENU_WIDTH);
781 th = ob_rr_theme->menu_title_height +
782 (ob_rr_theme->mbwidth - PADDING) *2;
785 th = ob_rr_theme->menu_sep_width +
786 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
790 g_assert_not_reached();
800 /* if the last entry is a labeled separator, then make its border
801 overlap with the menu's outside border */
802 it = g_list_last(self->entries);
803 e = it ? it->data : NULL;
804 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
805 e->entry->data.separator.label)
807 h -= ob_rr_theme->mbwidth;
810 self->text_x = PADDING;
815 w += ITEM_HEIGHT + PADDING;
816 self->text_x += ITEM_HEIGHT + PADDING;
823 XResizeWindow(obt_display, self->window, w, h);
827 RrPaint(self->a_items, self->window, w, h);
829 for (it = self->entries; it; it = g_list_next(it))
830 menu_entry_frame_render(it->data);
832 w += ob_rr_theme->mbwidth * 2;
833 h += ob_rr_theme->mbwidth * 2;
835 RECT_SET_SIZE(self->area, w, h);
840 static void menu_frame_update(ObMenuFrame *self)
846 menu_pipe_execute(self->menu);
847 menu_find_submenus(self->menu);
849 self->selected = NULL;
851 /* start at show_from */
852 mit = g_list_nth(self->menu->entries, self->show_from);
854 /* go through the menu's and frame's entries and connect the frame entries
855 to the menu entries */
856 for (fit = self->entries; mit && fit;
857 mit = g_list_next(mit), fit = g_list_next(fit))
859 ObMenuEntryFrame *f = fit->data;
860 f->entry = mit->data;
863 /* if there are more menu entries than in the frame, add them */
865 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
866 self->entries = g_list_append(self->entries, e);
867 mit = g_list_next(mit);
870 /* if there are more frame entries than menu entries then get rid of
873 GList *n = g_list_next(fit);
874 menu_entry_frame_free(fit->data);
875 self->entries = g_list_delete_link(self->entries, fit);
879 /* * make the menu fit on the screen */
881 /* calculate the height of the menu */
883 for (fit = self->entries; fit; fit = g_list_next(fit))
884 h += menu_entry_frame_get_height(fit->data,
885 fit == self->entries,
886 g_list_next(fit) == NULL);
887 /* add the border at the top and bottom */
888 h += ob_rr_theme->mbwidth * 2;
890 a = screen_physical_area_monitor(self->monitor);
894 gboolean last_entry = TRUE;
896 /* take the height of our More... entry into account */
897 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
899 /* start at the end of the entries */
900 flast = g_list_last(self->entries);
902 /* pull out all the entries from the frame that don't
903 fit on the screen, leaving at least 1 though */
904 while (h > a->height && g_list_previous(flast) != NULL) {
905 /* update the height, without this entry */
906 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
908 /* destroy the entry we're not displaying */
910 flast = g_list_previous(flast);
911 menu_entry_frame_free(tmp->data);
912 self->entries = g_list_delete_link(self->entries, tmp);
914 /* only the first one that we see is the last entry in the menu */
919 ObMenuEntry *more_entry;
920 ObMenuEntryFrame *more_frame;
921 /* make the More... menu entry frame which will display in this
923 if self->menu->more_menu is NULL that means that this is already
924 More... menu, so just use ourself.
926 more_entry = menu_get_more((self->menu->more_menu ?
927 self->menu->more_menu :
929 /* continue where we left off */
931 g_list_length(self->entries));
932 more_frame = menu_entry_frame_new(more_entry, self);
933 /* make it get deleted when the menu frame goes away */
934 menu_entry_unref(more_entry);
936 /* add our More... entry to the frame */
937 self->entries = g_list_append(self->entries, more_frame);
941 menu_frame_render(self);
944 static gboolean menu_frame_is_visible(ObMenuFrame *self)
946 return !!(g_list_find(menu_frame_visible, self));
949 static gboolean menu_frame_show(ObMenuFrame *self)
953 /* determine if the underlying menu is already visible */
954 for (it = menu_frame_visible; it; it = g_list_next(it)) {
955 ObMenuFrame *f = it->data;
956 if (f->menu == self->menu)
960 if (self->menu->update_func)
961 if (!self->menu->update_func(self, self->menu->data))
965 if (menu_frame_visible == NULL) {
966 /* no menus shown yet */
968 /* grab the pointer in such a way as to pass through "owner events"
969 so that we can get enter/leave notifies in the menu. */
970 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
972 if (!grab_keyboard()) {
978 menu_frame_update(self);
980 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
982 if (self->menu->show_func)
983 self->menu->show_func(self, self->menu->data);
988 gboolean menu_frame_show_topmenu(ObMenuFrame *self, const GravityPoint *pos,
989 gint monitor, gboolean mouse,
990 gboolean user_positioned)
995 if (menu_frame_is_visible(self))
997 if (!menu_frame_show(self))
1000 if (self->menu->place_func) {
1003 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1005 menu_frame_place_topmenu(self, pos, &x, &y, monitor,
1009 menu_frame_move(self, x, y);
1011 XMapWindow(obt_display, self->window);
1013 if (screen_pointer_pos(&px, &py)) {
1014 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1015 if (e && e->frame == self)
1022 /*! Stop hiding an open submenu.
1023 @child The OnMenuFrame of the submenu to be hidden
1025 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1027 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1028 submenu_hide_timer = 0;
1031 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1032 ObMenuEntryFrame *parent_entry)
1037 if (menu_frame_is_visible(self))
1040 self->monitor = parent->monitor;
1041 self->parent = parent;
1042 self->parent_entry = parent_entry;
1044 /* set up parent's child to be us */
1045 if ((parent->child) != self) {
1047 menu_frame_hide(parent->child);
1048 parent->child = self;
1049 parent->child_entry = parent_entry;
1052 if (!menu_frame_show(self)) {
1053 parent->child = NULL;
1054 parent->child_entry = NULL;
1058 menu_frame_place_submenu(self, &x, &y);
1059 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1062 /*try the other side */
1063 self->direction_right = !self->direction_right;
1064 menu_frame_place_submenu(self, &x, &y);
1065 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1067 menu_frame_move(self, x + dx, y + dy);
1069 XMapWindow(obt_display, self->window);
1071 if (screen_pointer_pos(&px, &py)) {
1072 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1073 if (e && e->frame == self)
1080 static void menu_frame_hide(ObMenuFrame *self)
1082 ObMenu *const menu = self->menu;
1083 GList *it = g_list_find(menu_frame_visible, self);
1084 gulong ignore_start;
1089 if (menu->hide_func)
1090 menu->hide_func(self, menu->data);
1093 menu_frame_hide(self->child);
1096 remove_submenu_hide_timeout(self);
1098 self->parent->child = NULL;
1099 self->parent->child_entry = NULL;
1101 self->parent = NULL;
1102 self->parent_entry = NULL;
1104 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1106 if (menu_frame_visible == NULL) {
1107 /* last menu shown */
1112 ignore_start = event_start_ignore_all_enters();
1113 XUnmapWindow(obt_display, self->window);
1114 event_end_ignore_all_enters(ignore_start);
1116 menu_frame_free(self);
1118 if (menu->cleanup_func)
1119 menu->cleanup_func(menu, menu->data);
1122 void menu_frame_hide_all(void)
1126 if (config_submenu_show_delay) {
1127 /* remove any submenu open requests */
1128 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1129 submenu_show_timer = 0;
1131 if ((it = g_list_last(menu_frame_visible)))
1132 menu_frame_hide(it->data);
1135 ObMenuFrame* menu_frame_under(gint x, gint y)
1137 ObMenuFrame *ret = NULL;
1140 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1141 ObMenuFrame *f = it->data;
1143 if (RECT_CONTAINS(f->area, x, y)) {
1151 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1154 ObMenuEntryFrame *ret = NULL;
1157 if ((frame = menu_frame_under(x, y))) {
1158 x -= ob_rr_theme->mbwidth + frame->area.x;
1159 y -= ob_rr_theme->mbwidth + frame->area.y;
1161 for (it = frame->entries; it; it = g_list_next(it)) {
1162 ObMenuEntryFrame *e = it->data;
1164 if (RECT_CONTAINS(e->area, x, y)) {
1173 static gboolean submenu_show_timeout(gpointer data)
1175 g_assert(menu_frame_visible);
1176 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1180 static gboolean submenu_hide_timeout(gpointer data)
1182 g_assert(menu_frame_visible);
1183 menu_frame_hide((ObMenuFrame*)data);
1187 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1190 ObMenuEntryFrame *old = self->selected;
1191 ObMenuFrame *oldchild = self->child;
1192 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1194 /* if the user selected a separator, ignore it and reselect what we had
1196 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1200 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1203 /* if the user left this menu but we have a submenu open, move the
1204 selection back to that submenu */
1205 if (!entry && oldchild_entry)
1206 entry = oldchild_entry;
1208 if (config_submenu_show_delay) {
1209 /* remove any submenu open requests */
1210 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1211 submenu_show_timer = 0;
1214 self->selected = entry;
1217 menu_entry_frame_render(old);
1219 if (oldchild_entry) {
1220 /* There is an open submenu */
1221 if (oldchild_entry == self->selected) {
1222 /* The open submenu has been reselected, so stop hiding the
1224 remove_submenu_hide_timeout(oldchild);
1226 else if (oldchild_entry == old) {
1227 /* The open submenu was selected and is no longer, so hide the
1229 if (immediate || config_submenu_hide_delay == 0)
1230 menu_frame_hide(oldchild);
1231 else if (config_submenu_hide_delay > 0) {
1232 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1233 submenu_hide_timer =
1234 g_timeout_add_full(G_PRIORITY_DEFAULT,
1235 config_submenu_hide_delay,
1236 submenu_hide_timeout, oldchild, NULL);
1241 if (self->selected) {
1242 menu_entry_frame_render(self->selected);
1244 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1245 /* only show if the submenu isn't already showing */
1246 if (oldchild_entry != self->selected) {
1247 if (immediate || config_submenu_show_delay == 0)
1248 menu_entry_frame_show_submenu(self->selected);
1249 else if (config_submenu_show_delay > 0) {
1250 if (submenu_show_timer)
1251 g_source_remove(submenu_show_timer);
1252 submenu_show_timer =
1253 g_timeout_add_full(G_PRIORITY_DEFAULT,
1254 config_submenu_show_delay,
1255 submenu_show_timeout,
1256 self->selected, NULL);
1259 /* hide the grandchildren of this menu. and move the cursor to
1261 else if (immediate && self->child && self->child->child) {
1262 menu_frame_hide(self->child->child);
1263 menu_frame_select(self->child, NULL, TRUE);
1269 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1273 if (!self->entry->data.submenu.submenu) return;
1275 f = menu_frame_new(self->entry->data.submenu.submenu,
1276 self->entry->data.submenu.show_from,
1277 self->frame->client);
1278 /* pass our direction on to our child */
1279 f->direction_right = self->frame->direction_right;
1281 if (!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);