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, GravityPoint *pos,
236 gint *x, gint *y, gint monitor)
240 screen_apply_gravity_point(x, y, self->area.width, self->area.height,
241 pos, screen_physical_area_monitor(monitor));
243 if (config_menu_middle) {
247 *y -= self->area.height / 2;
249 /* try to the right of the cursor */
250 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
251 self->direction_right = TRUE;
253 /* try to the left of the cursor */
254 myx = *x - self->area.width;
255 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
256 self->direction_right = FALSE;
259 /* if didnt fit on either side so just use what it says */
261 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
262 self->direction_right = TRUE;
272 /* try to the bottom right of the cursor */
273 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
274 self->direction_right = TRUE;
275 if (dx != 0 || dy != 0) {
276 /* try to the bottom left of the cursor */
277 myx = *x - self->area.width;
279 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
280 self->direction_right = FALSE;
282 if (dx != 0 || dy != 0) {
283 /* try to the top right of the cursor */
285 myy = *y - self->area.height;
286 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
287 self->direction_right = TRUE;
289 if (dx != 0 || dy != 0) {
290 /* try to the top left of the cursor */
291 myx = *x - self->area.width;
292 myy = *y - self->area.height;
293 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
294 self->direction_right = FALSE;
296 if (dx != 0 || dy != 0) {
297 /* if didnt fit on either side so just use what it says */
300 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
301 self->direction_right = TRUE;
308 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
310 gint overlapx, overlapy;
313 overlapx = ob_rr_theme->menu_overlap_x;
314 overlapy = ob_rr_theme->menu_overlap_y;
315 bwidth = ob_rr_theme->mbwidth;
317 if (self->direction_right)
318 *x = self->parent->area.x + self->parent->area.width -
321 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
323 *y = self->parent->area.y + self->parent_entry->area.y;
324 if (config_menu_middle)
325 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
330 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
333 const Rect *a = NULL;
334 Rect search = self->area;
335 gint pos, half, monitor;
338 RECT_SET_POINT(search, x, y);
341 monitor = self->parent->monitor;
343 monitor = screen_find_monitor(&search);
345 a = screen_physical_area_monitor(monitor);
347 half = g_list_length(self->entries) / 2;
348 pos = g_list_index(self->entries, self->selected);
350 /* if in the bottom half then check this stuff first, will keep the bottom
351 edge of the menu visible */
353 *dx = MAX(*dx, a->x - x);
354 *dy = MAX(*dy, a->y - y);
356 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
357 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
358 /* if in the top half then check this stuff last, will keep the top
359 edge of the menu visible */
361 *dx = MAX(*dx, a->x - x);
362 *dy = MAX(*dy, a->y - y);
366 static void menu_entry_frame_render(ObMenuEntryFrame *self)
368 RrAppearance *item_a, *text_a;
371 ObMenuFrame *frame = self->frame;
373 switch (self->entry->type) {
374 case OB_MENU_ENTRY_TYPE_NORMAL:
375 case OB_MENU_ENTRY_TYPE_SUBMENU:
376 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
377 !self->entry->data.normal.enabled ?
379 (self == self->frame->selected ?
380 ob_rr_theme->a_menu_disabled_selected :
381 ob_rr_theme->a_menu_disabled) :
383 (self == self->frame->selected ?
384 ob_rr_theme->a_menu_selected :
385 ob_rr_theme->a_menu_normal));
388 case OB_MENU_ENTRY_TYPE_SEPARATOR:
389 if (self->entry->data.separator.label) {
390 item_a = ob_rr_theme->a_menu_title;
391 th = ob_rr_theme->menu_title_height;
393 item_a = ob_rr_theme->a_menu_normal;
394 th = ob_rr_theme->menu_sep_width +
395 2*ob_rr_theme->menu_sep_paddingy;
399 g_assert_not_reached();
402 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
403 XResizeWindow(obt_display, self->window,
404 self->area.width, self->area.height);
405 item_a->surface.parent = self->frame->a_items;
406 item_a->surface.parentx = self->area.x;
407 item_a->surface.parenty = self->area.y;
408 RrPaint(item_a, self->window, self->area.width, self->area.height);
410 switch (self->entry->type) {
411 case OB_MENU_ENTRY_TYPE_NORMAL:
412 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
413 !self->entry->data.normal.enabled ?
415 (self == self->frame->selected ?
416 ob_rr_theme->a_menu_text_disabled_selected :
417 ob_rr_theme->a_menu_text_disabled) :
419 (self == self->frame->selected ?
420 ob_rr_theme->a_menu_text_selected :
421 ob_rr_theme->a_menu_text_normal));
422 text_a->texture[0].data.text.string = self->entry->data.normal.label;
423 if (self->entry->data.normal.shortcut &&
424 (self->frame->menu->show_all_shortcuts ||
425 self->entry->data.normal.shortcut_always_show ||
426 self->entry->data.normal.shortcut_position > 0))
428 text_a->texture[0].data.text.shortcut = TRUE;
429 text_a->texture[0].data.text.shortcut_pos =
430 self->entry->data.normal.shortcut_position;
432 text_a->texture[0].data.text.shortcut = FALSE;
434 case OB_MENU_ENTRY_TYPE_SUBMENU:
435 text_a = (self == self->frame->selected ?
436 ob_rr_theme->a_menu_text_selected :
437 ob_rr_theme->a_menu_text_normal);
438 sub = self->entry->data.submenu.submenu;
439 text_a->texture[0].data.text.string = sub ? sub->title : "";
440 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
441 sub->shortcut_always_show ||
442 sub->shortcut_position > 0))
444 text_a->texture[0].data.text.shortcut = TRUE;
445 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
447 text_a->texture[0].data.text.shortcut = FALSE;
449 case OB_MENU_ENTRY_TYPE_SEPARATOR:
450 if (self->entry->data.separator.label != NULL) {
451 text_a = ob_rr_theme->a_menu_text_title;
452 text_a->texture[0].data.text.string =
453 self->entry->data.separator.label;
456 text_a = ob_rr_theme->a_menu_text_normal;
459 g_assert_not_reached();
462 switch (self->entry->type) {
463 case OB_MENU_ENTRY_TYPE_NORMAL:
464 XMoveResizeWindow(obt_display, self->text,
465 self->frame->text_x, PADDING,
467 ITEM_HEIGHT - 2*PADDING);
468 text_a->surface.parent = item_a;
469 text_a->surface.parentx = self->frame->text_x;
470 text_a->surface.parenty = PADDING;
471 RrPaint(text_a, self->text, self->frame->text_w,
472 ITEM_HEIGHT - 2*PADDING);
474 case OB_MENU_ENTRY_TYPE_SUBMENU:
475 XMoveResizeWindow(obt_display, self->text,
476 self->frame->text_x, PADDING,
477 self->frame->text_w - ITEM_HEIGHT,
478 ITEM_HEIGHT - 2*PADDING);
479 text_a->surface.parent = item_a;
480 text_a->surface.parentx = self->frame->text_x;
481 text_a->surface.parenty = PADDING;
482 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
483 ITEM_HEIGHT - 2*PADDING);
485 case OB_MENU_ENTRY_TYPE_SEPARATOR:
486 if (self->entry->data.separator.label != NULL) {
487 /* labeled separator */
488 XMoveResizeWindow(obt_display, self->text,
489 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
490 self->area.width - 2*ob_rr_theme->paddingx,
491 ob_rr_theme->menu_title_height -
492 2*ob_rr_theme->paddingy);
493 text_a->surface.parent = item_a;
494 text_a->surface.parentx = ob_rr_theme->paddingx;
495 text_a->surface.parenty = ob_rr_theme->paddingy;
496 RrPaint(text_a, self->text,
497 self->area.width - 2*ob_rr_theme->paddingx,
498 ob_rr_theme->menu_title_height -
499 2*ob_rr_theme->paddingy);
503 /* unlabeled separator */
504 XMoveResizeWindow(obt_display, self->text, 0, 0,
506 ob_rr_theme->menu_sep_width +
507 2*ob_rr_theme->menu_sep_paddingy);
509 a_sep->surface.parent = item_a;
510 a_sep->surface.parentx = 0;
511 a_sep->surface.parenty = 0;
512 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
513 a_sep->texture[i].data.lineart.x1 =
514 ob_rr_theme->menu_sep_paddingx;
515 a_sep->texture[i].data.lineart.y1 =
516 ob_rr_theme->menu_sep_paddingy + i;
517 a_sep->texture[i].data.lineart.x2 =
518 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
519 a_sep->texture[i].data.lineart.y2 =
520 ob_rr_theme->menu_sep_paddingy + i;
523 RrPaint(a_sep, self->text, self->area.width,
524 ob_rr_theme->menu_sep_width +
525 2*ob_rr_theme->menu_sep_paddingy);
529 g_assert_not_reached();
532 if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
533 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
534 self->entry->data.normal.icon)
538 XMoveResizeWindow(obt_display, self->icon,
539 PADDING, frame->item_margin.top,
540 ITEM_HEIGHT - frame->item_margin.top
541 - frame->item_margin.bottom,
542 ITEM_HEIGHT - frame->item_margin.top
543 - frame->item_margin.bottom);
545 clear = ob_rr_theme->a_clear_tex;
546 RrAppearanceClearTextures(clear);
547 clear->texture[0].type = RR_TEXTURE_IMAGE;
548 clear->texture[0].data.image.image =
549 self->entry->data.normal.icon;
550 clear->texture[0].data.image.alpha =
551 self->entry->data.normal.icon_alpha;
552 clear->surface.parent = item_a;
553 clear->surface.parentx = PADDING;
554 clear->surface.parenty = frame->item_margin.top;
555 RrPaint(clear, self->icon,
556 ITEM_HEIGHT - frame->item_margin.top
557 - frame->item_margin.bottom,
558 ITEM_HEIGHT - frame->item_margin.top
559 - frame->item_margin.bottom);
560 XMapWindow(obt_display, self->icon);
561 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
562 self->entry->data.normal.mask)
567 XMoveResizeWindow(obt_display, self->icon,
568 PADDING, frame->item_margin.top,
569 ITEM_HEIGHT - frame->item_margin.top
570 - frame->item_margin.bottom,
571 ITEM_HEIGHT - frame->item_margin.top
572 - frame->item_margin.bottom);
574 clear = ob_rr_theme->a_clear_tex;
575 RrAppearanceClearTextures(clear);
576 clear->texture[0].type = RR_TEXTURE_MASK;
577 clear->texture[0].data.mask.mask =
578 self->entry->data.normal.mask;
580 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
581 !self->entry->data.normal.enabled ?
583 (self == self->frame->selected ?
584 self->entry->data.normal.mask_disabled_selected_color :
585 self->entry->data.normal.mask_disabled_color) :
587 (self == self->frame->selected ?
588 self->entry->data.normal.mask_selected_color :
589 self->entry->data.normal.mask_normal_color));
590 clear->texture[0].data.mask.color = c;
592 clear->surface.parent = item_a;
593 clear->surface.parentx = PADDING;
594 clear->surface.parenty = frame->item_margin.top;
595 RrPaint(clear, self->icon,
596 ITEM_HEIGHT - frame->item_margin.top
597 - frame->item_margin.bottom,
598 ITEM_HEIGHT - frame->item_margin.top
599 - frame->item_margin.bottom);
600 XMapWindow(obt_display, self->icon);
602 XUnmapWindow(obt_display, self->icon);
604 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
605 RrAppearance *bullet_a;
606 XMoveResizeWindow(obt_display, self->bullet,
607 self->frame->text_x + self->frame->text_w -
608 ITEM_HEIGHT + PADDING, PADDING,
609 ITEM_HEIGHT - 2*PADDING,
610 ITEM_HEIGHT - 2*PADDING);
611 bullet_a = (self == self->frame->selected ?
612 ob_rr_theme->a_menu_bullet_selected :
613 ob_rr_theme->a_menu_bullet_normal);
614 bullet_a->surface.parent = item_a;
615 bullet_a->surface.parentx =
616 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
617 bullet_a->surface.parenty = PADDING;
618 RrPaint(bullet_a, self->bullet,
619 ITEM_HEIGHT - 2*PADDING,
620 ITEM_HEIGHT - 2*PADDING);
621 XMapWindow(obt_display, self->bullet);
623 XUnmapWindow(obt_display, self->bullet);
628 /*! this code is taken from the menu_frame_render. if that changes, this won't
630 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
631 gboolean first_entry,
640 t = self->entry->type;
642 /* this is the More... entry, it's NORMAL type */
643 t = OB_MENU_ENTRY_TYPE_NORMAL;
646 case OB_MENU_ENTRY_TYPE_NORMAL:
647 case OB_MENU_ENTRY_TYPE_SUBMENU:
648 h += ob_rr_theme->menu_font_height;
650 case OB_MENU_ENTRY_TYPE_SEPARATOR:
651 if (self->entry->data.separator.label != NULL) {
652 h += ob_rr_theme->menu_title_height +
653 (ob_rr_theme->mbwidth - PADDING) * 2;
655 /* if the first entry is a labeled separator, then make its border
656 overlap with the menu's outside border */
658 h -= ob_rr_theme->mbwidth;
659 /* if the last entry is a labeled separator, then make its border
660 overlap with the menu's outside border */
662 h -= ob_rr_theme->mbwidth;
664 h += ob_rr_theme->menu_sep_width +
665 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
673 void menu_frame_render(ObMenuFrame *self)
676 gint tw, th; /* temps */
678 gboolean has_icon = FALSE;
682 /* find text dimensions */
684 STRUT_SET(self->item_margin, 0, 0, 0, 0);
689 e = self->entries->data;
690 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
691 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
696 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
697 STRUT_SET(self->item_margin,
698 MAX(self->item_margin.left, l),
699 MAX(self->item_margin.top, t),
700 MAX(self->item_margin.right, r),
701 MAX(self->item_margin.bottom, b));
702 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
703 STRUT_SET(self->item_margin,
704 MAX(self->item_margin.left, l),
705 MAX(self->item_margin.top, t),
706 MAX(self->item_margin.right, r),
707 MAX(self->item_margin.bottom, b));
708 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
709 STRUT_SET(self->item_margin,
710 MAX(self->item_margin.left, l),
711 MAX(self->item_margin.top, t),
712 MAX(self->item_margin.right, r),
713 MAX(self->item_margin.bottom, b));
714 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
715 STRUT_SET(self->item_margin,
716 MAX(self->item_margin.left, l),
717 MAX(self->item_margin.top, t),
718 MAX(self->item_margin.right, r),
719 MAX(self->item_margin.bottom, b));
722 /* render the entries */
724 for (it = self->entries; it; it = g_list_next(it)) {
725 RrAppearance *text_a;
728 /* if the first entry is a labeled separator, then make its border
729 overlap with the menu's outside border */
730 if (it == self->entries &&
731 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
732 e->entry->data.separator.label)
734 h -= ob_rr_theme->mbwidth;
737 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
738 e->entry->data.separator.label)
740 e->border = ob_rr_theme->mbwidth;
743 RECT_SET_POINT(e->area, 0, h+e->border);
744 XMoveWindow(obt_display, e->window,
745 e->area.x-e->border, e->area.y-e->border);
746 XSetWindowBorderWidth(obt_display, e->window, e->border);
747 XSetWindowBorder(obt_display, e->window,
748 RrColorPixel(ob_rr_theme->menu_border_color));
750 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
751 !e->entry->data.normal.enabled ?
753 (e == self->selected ?
754 ob_rr_theme->a_menu_text_disabled_selected :
755 ob_rr_theme->a_menu_text_disabled) :
757 (e == self->selected ?
758 ob_rr_theme->a_menu_text_selected :
759 ob_rr_theme->a_menu_text_normal));
760 switch (e->entry->type) {
761 case OB_MENU_ENTRY_TYPE_NORMAL:
762 text_a->texture[0].data.text.string = e->entry->data.normal.label;
763 tw = RrMinWidth(text_a);
764 tw = MIN(tw, MAX_MENU_WIDTH);
765 th = ob_rr_theme->menu_font_height;
767 if (e->entry->data.normal.icon ||
768 e->entry->data.normal.mask)
771 case OB_MENU_ENTRY_TYPE_SUBMENU:
772 sub = e->entry->data.submenu.submenu;
773 text_a->texture[0].data.text.string = sub ? sub->title : "";
774 tw = RrMinWidth(text_a);
775 tw = MIN(tw, MAX_MENU_WIDTH);
776 th = ob_rr_theme->menu_font_height;
778 if (e->entry->data.normal.icon ||
779 e->entry->data.normal.mask)
782 tw += ITEM_HEIGHT - PADDING;
784 case OB_MENU_ENTRY_TYPE_SEPARATOR:
785 if (e->entry->data.separator.label != NULL) {
786 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
787 e->entry->data.separator.label;
788 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
789 2*ob_rr_theme->paddingx;
790 tw = MIN(tw, MAX_MENU_WIDTH);
791 th = ob_rr_theme->menu_title_height +
792 (ob_rr_theme->mbwidth - PADDING) *2;
795 th = ob_rr_theme->menu_sep_width +
796 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
800 g_assert_not_reached();
808 /* if the last entry is a labeled separator, then make its border
809 overlap with the menu's outside border */
810 it = g_list_last(self->entries);
811 e = it ? it->data : NULL;
812 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
813 e->entry->data.separator.label)
815 h -= ob_rr_theme->mbwidth;
818 self->text_x = PADDING;
823 w += ITEM_HEIGHT + PADDING;
824 self->text_x += ITEM_HEIGHT + PADDING;
831 XResizeWindow(obt_display, self->window, w, h);
835 RrPaint(self->a_items, self->window, w, h);
837 for (it = self->entries; it; it = g_list_next(it))
838 menu_entry_frame_render(it->data);
840 w += ob_rr_theme->mbwidth * 2;
841 h += ob_rr_theme->mbwidth * 2;
843 RECT_SET_SIZE(self->area, w, h);
848 static void menu_frame_update(ObMenuFrame *self)
854 menu_pipe_execute(self->menu);
855 menu_find_submenus(self->menu);
857 self->selected = NULL;
859 /* start at show_from */
860 mit = g_list_nth(self->menu->entries, self->show_from);
862 /* go through the menu's and frame's entries and connect the frame entries
863 to the menu entries */
864 for (fit = self->entries; mit && fit;
865 mit = g_list_next(mit), fit = g_list_next(fit))
867 ObMenuEntryFrame *f = fit->data;
868 f->entry = mit->data;
871 /* if there are more menu entries than in the frame, add them */
873 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
874 self->entries = g_list_append(self->entries, e);
875 mit = g_list_next(mit);
878 /* if there are more frame entries than menu entries then get rid of
881 GList *n = g_list_next(fit);
882 menu_entry_frame_free(fit->data);
883 self->entries = g_list_delete_link(self->entries, fit);
887 /* * make the menu fit on the screen */
889 /* calculate the height of the menu */
891 for (fit = self->entries; fit; fit = g_list_next(fit))
892 h += menu_entry_frame_get_height(fit->data,
893 fit == self->entries,
894 g_list_next(fit) == NULL);
895 /* add the border at the top and bottom */
896 h += ob_rr_theme->mbwidth * 2;
898 a = screen_physical_area_monitor(self->monitor);
902 gboolean last_entry = TRUE;
904 /* take the height of our More... entry into account */
905 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
907 /* start at the end of the entries */
908 flast = g_list_last(self->entries);
910 /* pull out all the entries from the frame that don't
911 fit on the screen, leaving at least 1 though */
912 while (h > a->height && g_list_previous(flast) != NULL) {
913 /* update the height, without this entry */
914 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
916 /* destroy the entry we're not displaying */
918 flast = g_list_previous(flast);
919 menu_entry_frame_free(tmp->data);
920 self->entries = g_list_delete_link(self->entries, tmp);
922 /* only the first one that we see is the last entry in the menu */
927 ObMenuEntry *more_entry;
928 ObMenuEntryFrame *more_frame;
929 /* make the More... menu entry frame which will display in this
931 if self->menu->more_menu is NULL that means that this is already
932 More... menu, so just use ourself.
934 more_entry = menu_get_more((self->menu->more_menu ?
935 self->menu->more_menu :
937 /* continue where we left off */
939 g_list_length(self->entries));
940 more_frame = menu_entry_frame_new(more_entry, self);
941 /* make it get deleted when the menu frame goes away */
942 menu_entry_unref(more_entry);
944 /* add our More... entry to the frame */
945 self->entries = g_list_append(self->entries, more_frame);
949 menu_frame_render(self);
952 static gboolean menu_frame_is_visible(ObMenuFrame *self)
954 return !!(g_list_find(menu_frame_visible, self));
957 static gboolean menu_frame_show(ObMenuFrame *self)
961 /* determine if the underlying menu is already visible */
962 for (it = menu_frame_visible; it; it = g_list_next(it)) {
963 ObMenuFrame *f = it->data;
964 if (f->menu == self->menu)
968 if (self->menu->update_func)
969 if (!self->menu->update_func(self, self->menu->data))
973 if (menu_frame_visible == NULL) {
974 /* no menus shown yet */
976 /* grab the pointer in such a way as to pass through "owner events"
977 so that we can get enter/leave notifies in the menu. */
978 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
980 if (!grab_keyboard()) {
986 menu_frame_update(self);
988 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
990 if (self->menu->show_func)
991 self->menu->show_func(self, self->menu->data);
996 gboolean menu_frame_show_topmenu(ObMenuFrame *self, GravityPoint pos,
997 gint monitor, gboolean mouse)
1002 if (menu_frame_is_visible(self))
1004 if (!menu_frame_show(self))
1007 if (self->menu->place_func) {
1010 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1012 menu_frame_place_topmenu(self, &pos, &x, &y, monitor);
1015 menu_frame_move(self, x, y);
1017 XMapWindow(obt_display, self->window);
1019 if (screen_pointer_pos(&px, &py)) {
1020 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1021 if (e && e->frame == self)
1028 /*! Stop hiding an open submenu.
1029 @child The OnMenuFrame of the submenu to be hidden
1031 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1033 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1034 submenu_hide_timer = 0;
1037 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1038 ObMenuEntryFrame *parent_entry)
1043 if (menu_frame_is_visible(self))
1046 self->monitor = parent->monitor;
1047 self->parent = parent;
1048 self->parent_entry = parent_entry;
1050 /* set up parent's child to be us */
1051 if ((parent->child) != self) {
1053 menu_frame_hide(parent->child);
1054 parent->child = self;
1055 parent->child_entry = parent_entry;
1058 if (!menu_frame_show(self)) {
1059 parent->child = NULL;
1060 parent->child_entry = NULL;
1064 menu_frame_place_submenu(self, &x, &y);
1065 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1068 /*try the other side */
1069 self->direction_right = !self->direction_right;
1070 menu_frame_place_submenu(self, &x, &y);
1071 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1073 menu_frame_move(self, x + dx, y + dy);
1075 XMapWindow(obt_display, self->window);
1077 if (screen_pointer_pos(&px, &py)) {
1078 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1079 if (e && e->frame == self)
1086 static void menu_frame_hide(ObMenuFrame *self)
1088 ObMenu *const menu = self->menu;
1089 GList *it = g_list_find(menu_frame_visible, self);
1090 gulong ignore_start;
1095 if (menu->hide_func)
1096 menu->hide_func(self, menu->data);
1099 menu_frame_hide(self->child);
1102 remove_submenu_hide_timeout(self);
1104 self->parent->child = NULL;
1105 self->parent->child_entry = NULL;
1107 self->parent = NULL;
1108 self->parent_entry = NULL;
1110 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1112 if (menu_frame_visible == NULL) {
1113 /* last menu shown */
1118 ignore_start = event_start_ignore_all_enters();
1119 XUnmapWindow(obt_display, self->window);
1120 event_end_ignore_all_enters(ignore_start);
1122 menu_frame_free(self);
1124 if (menu->cleanup_func)
1125 menu->cleanup_func(menu, menu->data);
1128 void menu_frame_hide_all(void)
1132 if (config_submenu_show_delay) {
1133 /* remove any submenu open requests */
1134 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1135 submenu_show_timer = 0;
1137 if ((it = g_list_last(menu_frame_visible)))
1138 menu_frame_hide(it->data);
1141 ObMenuFrame* menu_frame_under(gint x, gint y)
1143 ObMenuFrame *ret = NULL;
1146 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1147 ObMenuFrame *f = it->data;
1149 if (RECT_CONTAINS(f->area, x, y)) {
1157 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1160 ObMenuEntryFrame *ret = NULL;
1163 if ((frame = menu_frame_under(x, y))) {
1164 x -= ob_rr_theme->mbwidth + frame->area.x;
1165 y -= ob_rr_theme->mbwidth + frame->area.y;
1167 for (it = frame->entries; it; it = g_list_next(it)) {
1168 ObMenuEntryFrame *e = it->data;
1170 if (RECT_CONTAINS(e->area, x, y)) {
1179 static gboolean submenu_show_timeout(gpointer data)
1181 g_assert(menu_frame_visible);
1182 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1186 static gboolean submenu_hide_timeout(gpointer data)
1188 g_assert(menu_frame_visible);
1189 menu_frame_hide((ObMenuFrame*)data);
1193 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1196 ObMenuEntryFrame *old = self->selected;
1197 ObMenuFrame *oldchild = self->child;
1198 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1200 /* if the user selected a separator, ignore it and reselect what we had
1202 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1206 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1209 /* if the user left this menu but we have a submenu open, move the
1210 selection back to that submenu */
1211 if (!entry && oldchild_entry)
1212 entry = oldchild_entry;
1214 if (config_submenu_show_delay) {
1215 /* remove any submenu open requests */
1216 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1217 submenu_show_timer = 0;
1220 self->selected = entry;
1223 menu_entry_frame_render(old);
1225 if (oldchild_entry) {
1226 /* There is an open submenu */
1227 if (oldchild_entry == self->selected) {
1228 /* The open submenu has been reselected, so stop hiding the
1230 remove_submenu_hide_timeout(oldchild);
1232 else if (oldchild_entry == old) {
1233 /* The open submenu was selected and is no longer, so hide the
1235 if (immediate || config_submenu_hide_delay == 0)
1236 menu_frame_hide(oldchild);
1237 else if (config_submenu_hide_delay > 0) {
1238 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1239 submenu_hide_timer =
1240 g_timeout_add_full(G_PRIORITY_DEFAULT,
1241 config_submenu_hide_delay,
1242 submenu_hide_timeout, oldchild, NULL);
1247 if (self->selected) {
1248 menu_entry_frame_render(self->selected);
1250 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1251 /* only show if the submenu isn't already showing */
1252 if (oldchild_entry != self->selected) {
1253 if (immediate || config_submenu_show_delay == 0)
1254 menu_entry_frame_show_submenu(self->selected);
1255 else if (config_submenu_show_delay > 0) {
1256 if (submenu_show_timer)
1257 g_source_remove(submenu_show_timer);
1258 submenu_show_timer =
1259 g_timeout_add_full(G_PRIORITY_DEFAULT,
1260 config_submenu_show_delay,
1261 submenu_show_timeout,
1262 self->selected, NULL);
1265 /* hide the grandchildren of this menu. and move the cursor to
1267 else if (immediate && self->child && self->child->child) {
1268 menu_frame_hide(self->child->child);
1269 menu_frame_select(self->child, NULL, TRUE);
1275 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1279 if (!self->entry->data.submenu.submenu) return;
1281 f = menu_frame_new(self->entry->data.submenu.submenu,
1282 self->entry->data.submenu.show_from,
1283 self->frame->client);
1284 /* pass our direction on to our child */
1285 f->direction_right = self->frame->direction_right;
1287 if (!menu_frame_show_submenu(f, self->frame, self))
1291 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1293 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1294 self->entry->data.normal.enabled)
1296 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1298 ObMenuEntry *entry = self->entry;
1299 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1300 gpointer data = self->frame->menu->data;
1301 GSList *acts = self->entry->data.normal.actions;
1302 ObClient *client = self->frame->client;
1303 ObMenuFrame *frame = self->frame;
1304 guint mods = obt_keyboard_only_modmasks(state);
1306 /* release grabs before executing the shit */
1307 if (!(mods & ControlMask)) {
1308 event_cancel_all_key_grabs();
1313 func(entry, frame, client, state, data);
1315 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1316 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1320 void menu_frame_select_previous(ObMenuFrame *self)
1322 GList *it = NULL, *start;
1324 if (self->entries) {
1325 start = it = g_list_find(self->entries, self->selected);
1327 ObMenuEntryFrame *e;
1329 it = it ? g_list_previous(it) : g_list_last(self->entries);
1335 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1337 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1342 menu_frame_select(self, it ? it->data : NULL, FALSE);
1345 void menu_frame_select_next(ObMenuFrame *self)
1347 GList *it = NULL, *start;
1349 if (self->entries) {
1350 start = it = g_list_find(self->entries, self->selected);
1352 ObMenuEntryFrame *e;
1354 it = it ? g_list_next(it) : self->entries;
1360 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1362 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1367 menu_frame_select(self, it ? it->data : NULL, FALSE);
1370 void menu_frame_select_first(ObMenuFrame *self)
1374 if (self->entries) {
1375 for (it = self->entries; it; it = g_list_next(it)) {
1376 ObMenuEntryFrame *e = it->data;
1377 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1379 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1383 menu_frame_select(self, it ? it->data : NULL, FALSE);
1386 void menu_frame_select_last(ObMenuFrame *self)
1390 if (self->entries) {
1391 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1392 ObMenuEntryFrame *e = it->data;
1393 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1395 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1399 menu_frame_select(self, it ? it->data : NULL, FALSE);