1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 menuframe.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "menuframe.h"
30 #include "obt/keyboard.h"
31 #include "obrender/theme.h"
34 #define MAX_MENU_WIDTH 400
36 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
38 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
40 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
41 ButtonPressMask | ButtonReleaseMask | \
44 GList *menu_frame_visible;
45 GHashTable *menu_frame_map;
47 static RrAppearance *a_sep;
48 static guint submenu_show_timer = 0;
49 static guint submenu_hide_timer = 0;
51 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
53 static void menu_entry_frame_free(ObMenuEntryFrame *self);
54 static void menu_frame_update(ObMenuFrame *self);
55 static gboolean submenu_show_timeout(gpointer data);
56 static void menu_frame_hide(ObMenuFrame *self);
58 static gboolean submenu_hide_timeout(gpointer data);
60 static Window createWindow(Window parent, gulong mask,
61 XSetWindowAttributes *attrib)
63 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
64 RrDepth(ob_rr_inst), InputOutput,
65 RrVisual(ob_rr_inst), mask, attrib);
68 static void client_dest(ObClient *client, gpointer data)
72 /* menus can be associated with a client, so null those refs since
73 we are disappearing now */
74 for (it = menu_frame_visible; it; it = g_list_next(it)) {
75 ObMenuFrame *f = it->data;
76 if (f->client == client)
81 void menu_frame_startup(gboolean reconfig)
85 a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
86 RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
87 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
88 a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
89 a_sep->texture[i].data.lineart.color =
90 ob_rr_theme->menu_sep_color;
95 client_add_destroy_notify(client_dest, NULL);
96 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
99 void menu_frame_shutdown(gboolean reconfig)
101 RrAppearanceFree(a_sep);
103 if (reconfig) return;
105 client_remove_destroy_notify(client_dest);
106 g_hash_table_destroy(menu_frame_map);
109 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
112 XSetWindowAttributes attr;
114 self = g_slice_new0(ObMenuFrame);
115 self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
117 self->selected = NULL;
118 self->client = client;
119 self->direction_right = TRUE;
120 self->show_from = show_from;
122 attr.event_mask = FRAME_EVENTMASK;
123 self->window = createWindow(obt_root(ob_screen),
126 /* make it a popup menu type window */
127 OBT_PROP_SET32(self->window, NET_WM_WINDOW_TYPE, ATOM,
128 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU));
130 XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
131 XSetWindowBorder(obt_display, self->window,
132 RrColorPixel(ob_rr_theme->menu_border_color));
134 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
136 window_add(&self->window, MENUFRAME_AS_WINDOW(self));
137 stacking_add(MENUFRAME_AS_WINDOW(self));
142 void menu_frame_free(ObMenuFrame *self)
145 while (self->entries) {
146 menu_entry_frame_free(self->entries->data);
147 self->entries = g_list_delete_link(self->entries, self->entries);
150 stacking_remove(MENUFRAME_AS_WINDOW(self));
151 window_remove(self->window);
153 RrAppearanceFree(self->a_items);
155 XDestroyWindow(obt_display, self->window);
157 g_slice_free(ObMenuFrame, self);
161 ObtIC* menu_frame_ic(ObMenuFrame *self)
163 /* menus are always used through a grab right now, so they can always use
164 the grab input context */
165 return grab_input_context();
168 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
171 ObMenuEntryFrame *self;
172 XSetWindowAttributes attr;
174 self = g_slice_new0(ObMenuEntryFrame);
178 menu_entry_ref(entry);
180 attr.event_mask = ENTRY_EVENTMASK;
181 self->window = createWindow(self->frame->window, CWEventMask, &attr);
182 self->text = createWindow(self->window, 0, NULL);
183 g_hash_table_insert(menu_frame_map, &self->window, self);
184 g_hash_table_insert(menu_frame_map, &self->text, self);
185 if ((entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
186 (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
187 self->icon = createWindow(self->window, 0, NULL);
188 g_hash_table_insert(menu_frame_map, &self->icon, self);
190 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
191 self->bullet = createWindow(self->window, 0, NULL);
192 g_hash_table_insert(menu_frame_map, &self->bullet, self);
195 XMapWindow(obt_display, self->window);
196 XMapWindow(obt_display, self->text);
198 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
203 static void menu_entry_frame_free(ObMenuEntryFrame *self)
206 menu_entry_unref(self->entry);
208 window_remove(self->window);
210 XDestroyWindow(obt_display, self->text);
211 XDestroyWindow(obt_display, self->window);
212 g_hash_table_remove(menu_frame_map, &self->text);
213 g_hash_table_remove(menu_frame_map, &self->window);
214 if ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
215 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
216 XDestroyWindow(obt_display, self->icon);
217 g_hash_table_remove(menu_frame_map, &self->icon);
219 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
220 XDestroyWindow(obt_display, self->bullet);
221 g_hash_table_remove(menu_frame_map, &self->bullet);
224 g_slice_free(ObMenuEntryFrame, self);
228 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
230 RECT_SET_POINT(self->area, x, y);
231 self->monitor = screen_find_monitor_point(x, y);
232 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
235 static void calc_position(ObMenuFrame *self, GravityPoint *position,
236 gint *x, gint *y, gint monitor)
238 const Rect *area = screen_physical_area_monitor(monitor);
240 if (position->x.center)
241 *x = area->width / 2 - self->area.width / 2;
243 *x = position->x.pos;
244 if (position->x.denom)
245 *x = (*x * area->width) / position->x.denom;
246 if (position->x.opposite)
247 *x = area->width - self->area.width - *x;
250 if (position->y.center)
251 *y = area->height / 2 - self->area.height / 2;
253 *y = position->y.pos;
254 if (position->y.denom)
255 *y = (*y * area->height) / position->y.denom;
256 if (position->y.opposite)
257 *y = area->height - self->area.height - *y;
264 static void menu_frame_place_topmenu(ObMenuFrame *self, GravityPoint *pos,
265 gint *x, gint *y, gint monitor)
269 calc_position(self, pos, x, y, monitor);
271 if (config_menu_middle) {
275 *y -= self->area.height / 2;
277 /* try to the right of the cursor */
278 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
279 self->direction_right = TRUE;
281 /* try to the left of the cursor */
282 myx = *x - self->area.width;
283 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
284 self->direction_right = FALSE;
287 /* if didnt fit on either side so just use what it says */
289 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
290 self->direction_right = TRUE;
300 /* try to the bottom right of the cursor */
301 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
302 self->direction_right = TRUE;
303 if (dx != 0 || dy != 0) {
304 /* try to the bottom left of the cursor */
305 myx = *x - self->area.width;
307 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
308 self->direction_right = FALSE;
310 if (dx != 0 || dy != 0) {
311 /* try to the top right of the cursor */
313 myy = *y - self->area.height;
314 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
315 self->direction_right = TRUE;
317 if (dx != 0 || dy != 0) {
318 /* try to the top left of the cursor */
319 myx = *x - self->area.width;
320 myy = *y - self->area.height;
321 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
322 self->direction_right = FALSE;
324 if (dx != 0 || dy != 0) {
325 /* if didnt fit on either side so just use what it says */
328 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
329 self->direction_right = TRUE;
336 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
338 gint overlapx, overlapy;
341 overlapx = ob_rr_theme->menu_overlap_x;
342 overlapy = ob_rr_theme->menu_overlap_y;
343 bwidth = ob_rr_theme->mbwidth;
345 if (self->direction_right)
346 *x = self->parent->area.x + self->parent->area.width -
349 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
351 *y = self->parent->area.y + self->parent_entry->area.y;
352 if (config_menu_middle)
353 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
358 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
361 const Rect *a = NULL;
362 Rect search = self->area;
363 gint pos, half, monitor;
366 RECT_SET_POINT(search, x, y);
369 monitor = self->parent->monitor;
371 monitor = screen_find_monitor(&search);
373 a = screen_physical_area_monitor(monitor);
375 half = g_list_length(self->entries) / 2;
376 pos = g_list_index(self->entries, self->selected);
378 /* if in the bottom half then check this stuff first, will keep the bottom
379 edge of the menu visible */
381 *dx = MAX(*dx, a->x - x);
382 *dy = MAX(*dy, a->y - y);
384 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
385 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
386 /* if in the top half then check this stuff last, will keep the top
387 edge of the menu visible */
389 *dx = MAX(*dx, a->x - x);
390 *dy = MAX(*dy, a->y - y);
394 static void menu_entry_frame_render(ObMenuEntryFrame *self)
396 RrAppearance *item_a, *text_a;
399 ObMenuFrame *frame = self->frame;
401 switch (self->entry->type) {
402 case OB_MENU_ENTRY_TYPE_NORMAL:
403 case OB_MENU_ENTRY_TYPE_SUBMENU:
404 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
405 !self->entry->data.normal.enabled ?
407 (self == self->frame->selected ?
408 ob_rr_theme->a_menu_disabled_selected :
409 ob_rr_theme->a_menu_disabled) :
411 (self == self->frame->selected ?
412 ob_rr_theme->a_menu_selected :
413 ob_rr_theme->a_menu_normal));
416 case OB_MENU_ENTRY_TYPE_SEPARATOR:
417 if (self->entry->data.separator.label) {
418 item_a = ob_rr_theme->a_menu_title;
419 th = ob_rr_theme->menu_title_height;
421 item_a = ob_rr_theme->a_menu_normal;
422 th = ob_rr_theme->menu_sep_width +
423 2*ob_rr_theme->menu_sep_paddingy;
427 g_assert_not_reached();
430 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
431 XResizeWindow(obt_display, self->window,
432 self->area.width, self->area.height);
433 item_a->surface.parent = self->frame->a_items;
434 item_a->surface.parentx = self->area.x;
435 item_a->surface.parenty = self->area.y;
436 RrPaint(item_a, self->window, self->area.width, self->area.height);
438 switch (self->entry->type) {
439 case OB_MENU_ENTRY_TYPE_NORMAL:
440 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
441 !self->entry->data.normal.enabled ?
443 (self == self->frame->selected ?
444 ob_rr_theme->a_menu_text_disabled_selected :
445 ob_rr_theme->a_menu_text_disabled) :
447 (self == self->frame->selected ?
448 ob_rr_theme->a_menu_text_selected :
449 ob_rr_theme->a_menu_text_normal));
450 text_a->texture[0].data.text.string = self->entry->data.normal.label;
451 if (self->entry->data.normal.shortcut &&
452 (self->frame->menu->show_all_shortcuts ||
453 self->entry->data.normal.shortcut_always_show ||
454 self->entry->data.normal.shortcut_position > 0))
456 text_a->texture[0].data.text.shortcut = TRUE;
457 text_a->texture[0].data.text.shortcut_pos =
458 self->entry->data.normal.shortcut_position;
460 text_a->texture[0].data.text.shortcut = FALSE;
462 case OB_MENU_ENTRY_TYPE_SUBMENU:
463 text_a = (self == self->frame->selected ?
464 ob_rr_theme->a_menu_text_selected :
465 ob_rr_theme->a_menu_text_normal);
466 sub = self->entry->data.submenu.submenu;
467 text_a->texture[0].data.text.string = sub ? sub->title : "";
468 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
469 sub->shortcut_always_show ||
470 sub->shortcut_position > 0))
472 text_a->texture[0].data.text.shortcut = TRUE;
473 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
475 text_a->texture[0].data.text.shortcut = FALSE;
477 case OB_MENU_ENTRY_TYPE_SEPARATOR:
478 if (self->entry->data.separator.label != NULL) {
479 text_a = ob_rr_theme->a_menu_text_title;
480 text_a->texture[0].data.text.string =
481 self->entry->data.separator.label;
484 text_a = ob_rr_theme->a_menu_text_normal;
487 g_assert_not_reached();
490 switch (self->entry->type) {
491 case OB_MENU_ENTRY_TYPE_NORMAL:
492 XMoveResizeWindow(obt_display, self->text,
493 self->frame->text_x, PADDING,
495 ITEM_HEIGHT - 2*PADDING);
496 text_a->surface.parent = item_a;
497 text_a->surface.parentx = self->frame->text_x;
498 text_a->surface.parenty = PADDING;
499 RrPaint(text_a, self->text, self->frame->text_w,
500 ITEM_HEIGHT - 2*PADDING);
502 case OB_MENU_ENTRY_TYPE_SUBMENU:
503 XMoveResizeWindow(obt_display, self->text,
504 self->frame->text_x, PADDING,
505 self->frame->text_w - ITEM_HEIGHT,
506 ITEM_HEIGHT - 2*PADDING);
507 text_a->surface.parent = item_a;
508 text_a->surface.parentx = self->frame->text_x;
509 text_a->surface.parenty = PADDING;
510 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
511 ITEM_HEIGHT - 2*PADDING);
513 case OB_MENU_ENTRY_TYPE_SEPARATOR:
514 if (self->entry->data.separator.label != NULL) {
515 /* labeled separator */
516 XMoveResizeWindow(obt_display, self->text,
517 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
518 self->area.width - 2*ob_rr_theme->paddingx,
519 ob_rr_theme->menu_title_height -
520 2*ob_rr_theme->paddingy);
521 text_a->surface.parent = item_a;
522 text_a->surface.parentx = ob_rr_theme->paddingx;
523 text_a->surface.parenty = ob_rr_theme->paddingy;
524 RrPaint(text_a, self->text,
525 self->area.width - 2*ob_rr_theme->paddingx,
526 ob_rr_theme->menu_title_height -
527 2*ob_rr_theme->paddingy);
531 /* unlabeled separator */
532 XMoveResizeWindow(obt_display, self->text, 0, 0,
534 ob_rr_theme->menu_sep_width +
535 2*ob_rr_theme->menu_sep_paddingy);
537 a_sep->surface.parent = item_a;
538 a_sep->surface.parentx = 0;
539 a_sep->surface.parenty = 0;
540 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
541 a_sep->texture[i].data.lineart.x1 =
542 ob_rr_theme->menu_sep_paddingx;
543 a_sep->texture[i].data.lineart.y1 =
544 ob_rr_theme->menu_sep_paddingy + i;
545 a_sep->texture[i].data.lineart.x2 =
546 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
547 a_sep->texture[i].data.lineart.y2 =
548 ob_rr_theme->menu_sep_paddingy + i;
551 RrPaint(a_sep, self->text, self->area.width,
552 ob_rr_theme->menu_sep_width +
553 2*ob_rr_theme->menu_sep_paddingy);
557 g_assert_not_reached();
560 if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
561 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
562 self->entry->data.normal.icon)
566 XMoveResizeWindow(obt_display, self->icon,
567 PADDING, frame->item_margin.top,
568 ITEM_HEIGHT - frame->item_margin.top
569 - frame->item_margin.bottom,
570 ITEM_HEIGHT - frame->item_margin.top
571 - frame->item_margin.bottom);
573 clear = ob_rr_theme->a_clear_tex;
574 RrAppearanceClearTextures(clear);
575 clear->texture[0].type = RR_TEXTURE_IMAGE;
576 clear->texture[0].data.image.image =
577 self->entry->data.normal.icon;
578 clear->texture[0].data.image.alpha =
579 self->entry->data.normal.icon_alpha;
580 clear->surface.parent = item_a;
581 clear->surface.parentx = PADDING;
582 clear->surface.parenty = frame->item_margin.top;
583 RrPaint(clear, self->icon,
584 ITEM_HEIGHT - frame->item_margin.top
585 - frame->item_margin.bottom,
586 ITEM_HEIGHT - frame->item_margin.top
587 - frame->item_margin.bottom);
588 XMapWindow(obt_display, self->icon);
589 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
590 self->entry->data.normal.mask)
595 XMoveResizeWindow(obt_display, self->icon,
596 PADDING, frame->item_margin.top,
597 ITEM_HEIGHT - frame->item_margin.top
598 - frame->item_margin.bottom,
599 ITEM_HEIGHT - frame->item_margin.top
600 - frame->item_margin.bottom);
602 clear = ob_rr_theme->a_clear_tex;
603 RrAppearanceClearTextures(clear);
604 clear->texture[0].type = RR_TEXTURE_MASK;
605 clear->texture[0].data.mask.mask =
606 self->entry->data.normal.mask;
608 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
609 !self->entry->data.normal.enabled ?
611 (self == self->frame->selected ?
612 self->entry->data.normal.mask_disabled_selected_color :
613 self->entry->data.normal.mask_disabled_color) :
615 (self == self->frame->selected ?
616 self->entry->data.normal.mask_selected_color :
617 self->entry->data.normal.mask_normal_color));
618 clear->texture[0].data.mask.color = c;
620 clear->surface.parent = item_a;
621 clear->surface.parentx = PADDING;
622 clear->surface.parenty = frame->item_margin.top;
623 RrPaint(clear, self->icon,
624 ITEM_HEIGHT - frame->item_margin.top
625 - frame->item_margin.bottom,
626 ITEM_HEIGHT - frame->item_margin.top
627 - frame->item_margin.bottom);
628 XMapWindow(obt_display, self->icon);
630 XUnmapWindow(obt_display, self->icon);
632 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
633 RrAppearance *bullet_a;
634 XMoveResizeWindow(obt_display, self->bullet,
635 self->frame->text_x + self->frame->text_w -
636 ITEM_HEIGHT + PADDING, PADDING,
637 ITEM_HEIGHT - 2*PADDING,
638 ITEM_HEIGHT - 2*PADDING);
639 bullet_a = (self == self->frame->selected ?
640 ob_rr_theme->a_menu_bullet_selected :
641 ob_rr_theme->a_menu_bullet_normal);
642 bullet_a->surface.parent = item_a;
643 bullet_a->surface.parentx =
644 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
645 bullet_a->surface.parenty = PADDING;
646 RrPaint(bullet_a, self->bullet,
647 ITEM_HEIGHT - 2*PADDING,
648 ITEM_HEIGHT - 2*PADDING);
649 XMapWindow(obt_display, self->bullet);
651 XUnmapWindow(obt_display, self->bullet);
656 /*! this code is taken from the menu_frame_render. if that changes, this won't
658 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
659 gboolean first_entry,
668 t = self->entry->type;
670 /* this is the More... entry, it's NORMAL type */
671 t = OB_MENU_ENTRY_TYPE_NORMAL;
674 case OB_MENU_ENTRY_TYPE_NORMAL:
675 case OB_MENU_ENTRY_TYPE_SUBMENU:
676 h += ob_rr_theme->menu_font_height;
678 case OB_MENU_ENTRY_TYPE_SEPARATOR:
679 if (self->entry->data.separator.label != NULL) {
680 h += ob_rr_theme->menu_title_height +
681 (ob_rr_theme->mbwidth - PADDING) * 2;
683 /* if the first entry is a labeled separator, then make its border
684 overlap with the menu's outside border */
686 h -= ob_rr_theme->mbwidth;
687 /* if the last entry is a labeled separator, then make its border
688 overlap with the menu's outside border */
690 h -= ob_rr_theme->mbwidth;
692 h += ob_rr_theme->menu_sep_width +
693 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
701 void menu_frame_render(ObMenuFrame *self)
704 gint tw, th; /* temps */
706 gboolean has_icon = FALSE;
710 /* find text dimensions */
712 STRUT_SET(self->item_margin, 0, 0, 0, 0);
717 e = self->entries->data;
718 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
719 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
724 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
725 STRUT_SET(self->item_margin,
726 MAX(self->item_margin.left, l),
727 MAX(self->item_margin.top, t),
728 MAX(self->item_margin.right, r),
729 MAX(self->item_margin.bottom, b));
730 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
731 STRUT_SET(self->item_margin,
732 MAX(self->item_margin.left, l),
733 MAX(self->item_margin.top, t),
734 MAX(self->item_margin.right, r),
735 MAX(self->item_margin.bottom, b));
736 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
737 STRUT_SET(self->item_margin,
738 MAX(self->item_margin.left, l),
739 MAX(self->item_margin.top, t),
740 MAX(self->item_margin.right, r),
741 MAX(self->item_margin.bottom, b));
742 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
743 STRUT_SET(self->item_margin,
744 MAX(self->item_margin.left, l),
745 MAX(self->item_margin.top, t),
746 MAX(self->item_margin.right, r),
747 MAX(self->item_margin.bottom, b));
750 /* render the entries */
752 for (it = self->entries; it; it = g_list_next(it)) {
753 RrAppearance *text_a;
756 /* if the first entry is a labeled separator, then make its border
757 overlap with the menu's outside border */
758 if (it == self->entries &&
759 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
760 e->entry->data.separator.label)
762 h -= ob_rr_theme->mbwidth;
765 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
766 e->entry->data.separator.label)
768 e->border = ob_rr_theme->mbwidth;
771 RECT_SET_POINT(e->area, 0, h+e->border);
772 XMoveWindow(obt_display, e->window,
773 e->area.x-e->border, e->area.y-e->border);
774 XSetWindowBorderWidth(obt_display, e->window, e->border);
775 XSetWindowBorder(obt_display, e->window,
776 RrColorPixel(ob_rr_theme->menu_border_color));
778 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
779 !e->entry->data.normal.enabled ?
781 (e == self->selected ?
782 ob_rr_theme->a_menu_text_disabled_selected :
783 ob_rr_theme->a_menu_text_disabled) :
785 (e == self->selected ?
786 ob_rr_theme->a_menu_text_selected :
787 ob_rr_theme->a_menu_text_normal));
788 switch (e->entry->type) {
789 case OB_MENU_ENTRY_TYPE_NORMAL:
790 text_a->texture[0].data.text.string = e->entry->data.normal.label;
791 tw = RrMinWidth(text_a);
792 tw = MIN(tw, MAX_MENU_WIDTH);
793 th = ob_rr_theme->menu_font_height;
795 if (e->entry->data.normal.icon ||
796 e->entry->data.normal.mask)
799 case OB_MENU_ENTRY_TYPE_SUBMENU:
800 sub = e->entry->data.submenu.submenu;
801 text_a->texture[0].data.text.string = sub ? sub->title : "";
802 tw = RrMinWidth(text_a);
803 tw = MIN(tw, MAX_MENU_WIDTH);
804 th = ob_rr_theme->menu_font_height;
806 if (e->entry->data.normal.icon ||
807 e->entry->data.normal.mask)
810 tw += ITEM_HEIGHT - PADDING;
812 case OB_MENU_ENTRY_TYPE_SEPARATOR:
813 if (e->entry->data.separator.label != NULL) {
814 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
815 e->entry->data.separator.label;
816 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
817 2*ob_rr_theme->paddingx;
818 tw = MIN(tw, MAX_MENU_WIDTH);
819 th = ob_rr_theme->menu_title_height +
820 (ob_rr_theme->mbwidth - PADDING) *2;
823 th = ob_rr_theme->menu_sep_width +
824 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
828 g_assert_not_reached();
836 /* if the last entry is a labeled separator, then make its border
837 overlap with the menu's outside border */
838 it = g_list_last(self->entries);
839 e = it ? it->data : NULL;
840 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
841 e->entry->data.separator.label)
843 h -= ob_rr_theme->mbwidth;
846 self->text_x = PADDING;
851 w += ITEM_HEIGHT + PADDING;
852 self->text_x += ITEM_HEIGHT + PADDING;
859 XResizeWindow(obt_display, self->window, w, h);
863 RrPaint(self->a_items, self->window, w, h);
865 for (it = self->entries; it; it = g_list_next(it))
866 menu_entry_frame_render(it->data);
868 w += ob_rr_theme->mbwidth * 2;
869 h += ob_rr_theme->mbwidth * 2;
871 RECT_SET_SIZE(self->area, w, h);
876 static void menu_frame_update(ObMenuFrame *self)
882 menu_pipe_execute(self->menu);
883 menu_find_submenus(self->menu);
885 self->selected = NULL;
887 /* start at show_from */
888 mit = g_list_nth(self->menu->entries, self->show_from);
890 /* go through the menu's and frame's entries and connect the frame entries
891 to the menu entries */
892 for (fit = self->entries; mit && fit;
893 mit = g_list_next(mit), fit = g_list_next(fit))
895 ObMenuEntryFrame *f = fit->data;
896 f->entry = mit->data;
899 /* if there are more menu entries than in the frame, add them */
901 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
902 self->entries = g_list_append(self->entries, e);
903 mit = g_list_next(mit);
906 /* if there are more frame entries than menu entries then get rid of
909 GList *n = g_list_next(fit);
910 menu_entry_frame_free(fit->data);
911 self->entries = g_list_delete_link(self->entries, fit);
915 /* * make the menu fit on the screen */
917 /* calculate the height of the menu */
919 for (fit = self->entries; fit; fit = g_list_next(fit))
920 h += menu_entry_frame_get_height(fit->data,
921 fit == self->entries,
922 g_list_next(fit) == NULL);
923 /* add the border at the top and bottom */
924 h += ob_rr_theme->mbwidth * 2;
926 a = screen_physical_area_monitor(self->monitor);
930 gboolean last_entry = TRUE;
932 /* take the height of our More... entry into account */
933 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
935 /* start at the end of the entries */
936 flast = g_list_last(self->entries);
938 /* pull out all the entries from the frame that don't
939 fit on the screen, leaving at least 1 though */
940 while (h > a->height && g_list_previous(flast) != NULL) {
941 /* update the height, without this entry */
942 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
944 /* destroy the entry we're not displaying */
946 flast = g_list_previous(flast);
947 menu_entry_frame_free(tmp->data);
948 self->entries = g_list_delete_link(self->entries, tmp);
950 /* only the first one that we see is the last entry in the menu */
955 ObMenuEntry *more_entry;
956 ObMenuEntryFrame *more_frame;
957 /* make the More... menu entry frame which will display in this
959 if self->menu->more_menu is NULL that means that this is already
960 More... menu, so just use ourself.
962 more_entry = menu_get_more((self->menu->more_menu ?
963 self->menu->more_menu :
965 /* continue where we left off */
967 g_list_length(self->entries));
968 more_frame = menu_entry_frame_new(more_entry, self);
969 /* make it get deleted when the menu frame goes away */
970 menu_entry_unref(more_entry);
972 /* add our More... entry to the frame */
973 self->entries = g_list_append(self->entries, more_frame);
977 menu_frame_render(self);
980 static gboolean menu_frame_is_visible(ObMenuFrame *self)
982 return !!(g_list_find(menu_frame_visible, self));
985 static gboolean menu_frame_show(ObMenuFrame *self)
989 /* determine if the underlying menu is already visible */
990 for (it = menu_frame_visible; it; it = g_list_next(it)) {
991 ObMenuFrame *f = it->data;
992 if (f->menu == self->menu)
996 if (self->menu->update_func)
997 if (!self->menu->update_func(self, self->menu->data))
1001 if (menu_frame_visible == NULL) {
1002 /* no menus shown yet */
1004 /* grab the pointer in such a way as to pass through "owner events"
1005 so that we can get enter/leave notifies in the menu. */
1006 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
1008 if (!grab_keyboard()) {
1014 menu_frame_update(self);
1016 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
1018 if (self->menu->show_func)
1019 self->menu->show_func(self, self->menu->data);
1024 gboolean menu_frame_show_topmenu(ObMenuFrame *self, GravityPoint pos,
1025 gint monitor, gboolean mouse)
1030 if (menu_frame_is_visible(self))
1032 if (!menu_frame_show(self))
1035 if (self->menu->place_func) {
1038 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1040 menu_frame_place_topmenu(self, &pos, &x, &y, monitor);
1043 menu_frame_move(self, x, y);
1045 XMapWindow(obt_display, self->window);
1047 if (screen_pointer_pos(&px, &py)) {
1048 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1049 if (e && e->frame == self)
1056 /*! Stop hiding an open submenu.
1057 @child The OnMenuFrame of the submenu to be hidden
1059 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1061 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1062 submenu_hide_timer = 0;
1065 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1066 ObMenuEntryFrame *parent_entry)
1071 if (menu_frame_is_visible(self))
1074 self->monitor = parent->monitor;
1075 self->parent = parent;
1076 self->parent_entry = parent_entry;
1078 /* set up parent's child to be us */
1079 if ((parent->child) != self) {
1081 menu_frame_hide(parent->child);
1082 parent->child = self;
1083 parent->child_entry = parent_entry;
1086 if (!menu_frame_show(self)) {
1087 parent->child = NULL;
1088 parent->child_entry = NULL;
1092 menu_frame_place_submenu(self, &x, &y);
1093 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1096 /*try the other side */
1097 self->direction_right = !self->direction_right;
1098 menu_frame_place_submenu(self, &x, &y);
1099 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1101 menu_frame_move(self, x + dx, y + dy);
1103 XMapWindow(obt_display, self->window);
1105 if (screen_pointer_pos(&px, &py)) {
1106 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1107 if (e && e->frame == self)
1114 static void menu_frame_hide(ObMenuFrame *self)
1116 ObMenu *const menu = self->menu;
1117 GList *it = g_list_find(menu_frame_visible, self);
1118 gulong ignore_start;
1123 if (menu->hide_func)
1124 menu->hide_func(self, menu->data);
1127 menu_frame_hide(self->child);
1130 remove_submenu_hide_timeout(self);
1132 self->parent->child = NULL;
1133 self->parent->child_entry = NULL;
1135 self->parent = NULL;
1136 self->parent_entry = NULL;
1138 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1140 if (menu_frame_visible == NULL) {
1141 /* last menu shown */
1146 ignore_start = event_start_ignore_all_enters();
1147 XUnmapWindow(obt_display, self->window);
1148 event_end_ignore_all_enters(ignore_start);
1150 menu_frame_free(self);
1152 if (menu->cleanup_func)
1153 menu->cleanup_func(menu, menu->data);
1156 void menu_frame_hide_all(void)
1160 if (config_submenu_show_delay) {
1161 /* remove any submenu open requests */
1162 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1163 submenu_show_timer = 0;
1165 if ((it = g_list_last(menu_frame_visible)))
1166 menu_frame_hide(it->data);
1169 ObMenuFrame* menu_frame_under(gint x, gint y)
1171 ObMenuFrame *ret = NULL;
1174 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1175 ObMenuFrame *f = it->data;
1177 if (RECT_CONTAINS(f->area, x, y)) {
1185 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1188 ObMenuEntryFrame *ret = NULL;
1191 if ((frame = menu_frame_under(x, y))) {
1192 x -= ob_rr_theme->mbwidth + frame->area.x;
1193 y -= ob_rr_theme->mbwidth + frame->area.y;
1195 for (it = frame->entries; it; it = g_list_next(it)) {
1196 ObMenuEntryFrame *e = it->data;
1198 if (RECT_CONTAINS(e->area, x, y)) {
1207 static gboolean submenu_show_timeout(gpointer data)
1209 g_assert(menu_frame_visible);
1210 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1214 static gboolean submenu_hide_timeout(gpointer data)
1216 g_assert(menu_frame_visible);
1217 menu_frame_hide((ObMenuFrame*)data);
1221 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1224 ObMenuEntryFrame *old = self->selected;
1225 ObMenuFrame *oldchild = self->child;
1226 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1228 /* if the user selected a separator, ignore it and reselect what we had
1230 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1234 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1237 /* if the user left this menu but we have a submenu open, move the
1238 selection back to that submenu */
1239 if (!entry && oldchild_entry)
1240 entry = oldchild_entry;
1242 if (config_submenu_show_delay) {
1243 /* remove any submenu open requests */
1244 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1245 submenu_show_timer = 0;
1248 self->selected = entry;
1251 menu_entry_frame_render(old);
1253 if (oldchild_entry) {
1254 /* There is an open submenu */
1255 if (oldchild_entry == self->selected) {
1256 /* The open submenu has been reselected, so stop hiding the
1258 remove_submenu_hide_timeout(oldchild);
1260 else if (oldchild_entry == old) {
1261 /* The open submenu was selected and is no longer, so hide the
1263 if (immediate || config_submenu_hide_delay == 0)
1264 menu_frame_hide(oldchild);
1265 else if (config_submenu_hide_delay > 0) {
1266 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1267 submenu_hide_timer =
1268 g_timeout_add_full(G_PRIORITY_DEFAULT,
1269 config_submenu_hide_delay,
1270 submenu_hide_timeout, oldchild, NULL);
1275 if (self->selected) {
1276 menu_entry_frame_render(self->selected);
1278 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1279 /* only show if the submenu isn't already showing */
1280 if (oldchild_entry != self->selected) {
1281 if (immediate || config_submenu_show_delay == 0)
1282 menu_entry_frame_show_submenu(self->selected);
1283 else if (config_submenu_show_delay > 0) {
1284 if (submenu_show_timer)
1285 g_source_remove(submenu_show_timer);
1286 submenu_show_timer =
1287 g_timeout_add_full(G_PRIORITY_DEFAULT,
1288 config_submenu_show_delay,
1289 submenu_show_timeout,
1290 self->selected, NULL);
1293 /* hide the grandchildren of this menu. and move the cursor to
1295 else if (immediate && self->child && self->child->child) {
1296 menu_frame_hide(self->child->child);
1297 menu_frame_select(self->child, NULL, TRUE);
1303 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1307 if (!self->entry->data.submenu.submenu) return;
1309 f = menu_frame_new(self->entry->data.submenu.submenu,
1310 self->entry->data.submenu.show_from,
1311 self->frame->client);
1312 /* pass our direction on to our child */
1313 f->direction_right = self->frame->direction_right;
1315 if (!menu_frame_show_submenu(f, self->frame, self))
1319 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1321 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1322 self->entry->data.normal.enabled)
1324 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1326 ObMenuEntry *entry = self->entry;
1327 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1328 gpointer data = self->frame->menu->data;
1329 GSList *acts = self->entry->data.normal.actions;
1330 ObClient *client = self->frame->client;
1331 ObMenuFrame *frame = self->frame;
1332 guint mods = obt_keyboard_only_modmasks(state);
1334 /* release grabs before executing the shit */
1335 if (!(mods & ControlMask)) {
1336 event_cancel_all_key_grabs();
1341 func(entry, frame, client, state, data);
1343 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1344 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1348 void menu_frame_select_previous(ObMenuFrame *self)
1350 GList *it = NULL, *start;
1352 if (self->entries) {
1353 start = it = g_list_find(self->entries, self->selected);
1355 ObMenuEntryFrame *e;
1357 it = it ? g_list_previous(it) : g_list_last(self->entries);
1363 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1365 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1370 menu_frame_select(self, it ? it->data : NULL, FALSE);
1373 void menu_frame_select_next(ObMenuFrame *self)
1375 GList *it = NULL, *start;
1377 if (self->entries) {
1378 start = it = g_list_find(self->entries, self->selected);
1380 ObMenuEntryFrame *e;
1382 it = it ? g_list_next(it) : self->entries;
1388 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1390 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1395 menu_frame_select(self, it ? it->data : NULL, FALSE);
1398 void menu_frame_select_first(ObMenuFrame *self)
1402 if (self->entries) {
1403 for (it = self->entries; it; it = g_list_next(it)) {
1404 ObMenuEntryFrame *e = it->data;
1405 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1407 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1411 menu_frame_select(self, it ? it->data : NULL, FALSE);
1414 void menu_frame_select_last(ObMenuFrame *self)
1418 if (self->entries) {
1419 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1420 ObMenuEntryFrame *e = it->data;
1421 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1423 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1427 menu_frame_select(self, it ? it->data : NULL, FALSE);