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 window_remove(self->window);
208 XDestroyWindow(obt_display, self->text);
209 XDestroyWindow(obt_display, self->window);
210 g_hash_table_remove(menu_frame_map, &self->text);
211 g_hash_table_remove(menu_frame_map, &self->window);
212 if ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
213 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
214 XDestroyWindow(obt_display, self->icon);
215 g_hash_table_remove(menu_frame_map, &self->icon);
217 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
218 XDestroyWindow(obt_display, self->bullet);
219 g_hash_table_remove(menu_frame_map, &self->bullet);
222 menu_entry_unref(self->entry);
223 g_slice_free(ObMenuEntryFrame, self);
227 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
229 RECT_SET_POINT(self->area, x, y);
230 self->monitor = screen_find_monitor_point(x, y);
231 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
234 static void menu_frame_place_topmenu(ObMenuFrame *self, const GravityPoint *pos,
235 gint *x, gint *y, gint monitor,
236 gboolean user_positioned)
240 screen_apply_gravity_point(x, y, self->area.width, self->area.height,
241 pos, screen_physical_area_monitor(monitor));
246 if (config_menu_middle) {
248 *x -= self->area.width / 2;
250 /* try below the cursor */
251 menu_frame_move_on_screen(self, *x, *y, &dx, &dy);
252 self->direction_right = TRUE;
261 /* try to the bottom right of the cursor */
262 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
263 self->direction_right = TRUE;
264 if (dx != 0 || dy != 0) {
265 /* try to the bottom left of the cursor */
266 myx = *x - self->area.width;
268 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
269 self->direction_right = FALSE;
271 if (dx != 0 || dy != 0) {
272 /* try to the top right of the cursor */
274 myy = *y - self->area.height;
275 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
276 self->direction_right = TRUE;
278 if (dx != 0 || dy != 0) {
279 /* try to the top left of the cursor */
280 myx = *x - self->area.width;
281 myy = *y - self->area.height;
282 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
283 self->direction_right = FALSE;
285 if (dx != 0 || dy != 0) {
286 /* if didnt fit on either side so just use what it says */
289 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
290 self->direction_right = TRUE;
297 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
299 gint overlapx, overlapy;
302 overlapx = ob_rr_theme->menu_overlap_x;
303 overlapy = ob_rr_theme->menu_overlap_y;
304 bwidth = ob_rr_theme->mbwidth;
306 if (self->direction_right)
307 *x = self->parent->area.x + self->parent->area.width -
310 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
312 *y = self->parent->area.y + self->parent_entry->area.y;
313 if (config_menu_middle)
314 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
319 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
322 const Rect *a = NULL;
323 Rect search = self->area;
324 gint pos, half, monitor;
327 RECT_SET_POINT(search, x, y);
330 monitor = self->parent->monitor;
332 monitor = screen_find_monitor(&search);
334 a = screen_physical_area_monitor(monitor);
336 half = g_list_length(self->entries) / 2;
337 pos = g_list_index(self->entries, self->selected);
339 /* if in the bottom half then check this stuff first, will keep the bottom
340 edge of the menu visible */
342 *dx = MAX(*dx, a->x - x);
343 *dy = MAX(*dy, a->y - y);
345 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
346 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
347 /* if in the top half then check this stuff last, will keep the top
348 edge of the menu visible */
350 *dx = MAX(*dx, a->x - x);
351 *dy = MAX(*dy, a->y - y);
355 static void menu_entry_frame_render(ObMenuEntryFrame *self)
357 RrAppearance *item_a, *text_a;
360 ObMenuFrame *frame = self->frame;
362 switch (self->entry->type) {
363 case OB_MENU_ENTRY_TYPE_NORMAL:
364 case OB_MENU_ENTRY_TYPE_SUBMENU:
365 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
366 !self->entry->data.normal.enabled ?
368 (self == self->frame->selected ?
369 ob_rr_theme->a_menu_disabled_selected :
370 ob_rr_theme->a_menu_disabled) :
372 (self == self->frame->selected ?
373 ob_rr_theme->a_menu_selected :
374 ob_rr_theme->a_menu_normal));
377 case OB_MENU_ENTRY_TYPE_SEPARATOR:
378 if (self->entry->data.separator.label) {
379 item_a = ob_rr_theme->a_menu_title;
380 th = ob_rr_theme->menu_title_height;
382 item_a = ob_rr_theme->a_menu_normal;
383 th = ob_rr_theme->menu_sep_width +
384 2*ob_rr_theme->menu_sep_paddingy;
388 g_assert_not_reached();
391 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
392 XResizeWindow(obt_display, self->window,
393 self->area.width, self->area.height);
394 item_a->surface.parent = self->frame->a_items;
395 item_a->surface.parentx = self->area.x;
396 item_a->surface.parenty = self->area.y;
397 RrPaint(item_a, self->window, self->area.width, self->area.height);
399 switch (self->entry->type) {
400 case OB_MENU_ENTRY_TYPE_NORMAL:
401 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
402 !self->entry->data.normal.enabled ?
404 (self == self->frame->selected ?
405 ob_rr_theme->a_menu_text_disabled_selected :
406 ob_rr_theme->a_menu_text_disabled) :
408 (self == self->frame->selected ?
409 ob_rr_theme->a_menu_text_selected :
410 ob_rr_theme->a_menu_text_normal));
411 text_a->texture[0].data.text.string = self->entry->data.normal.label;
412 if (self->entry->data.normal.shortcut &&
413 (self->frame->menu->show_all_shortcuts ||
414 self->entry->data.normal.shortcut_always_show ||
415 self->entry->data.normal.shortcut_position > 0))
417 text_a->texture[0].data.text.shortcut = TRUE;
418 text_a->texture[0].data.text.shortcut_pos =
419 self->entry->data.normal.shortcut_position;
421 text_a->texture[0].data.text.shortcut = FALSE;
423 case OB_MENU_ENTRY_TYPE_SUBMENU:
424 text_a = (self == self->frame->selected ?
425 ob_rr_theme->a_menu_text_selected :
426 ob_rr_theme->a_menu_text_normal);
427 sub = self->entry->data.submenu.submenu;
428 text_a->texture[0].data.text.string = sub ? sub->title : "";
429 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
430 sub->shortcut_always_show ||
431 sub->shortcut_position > 0))
433 text_a->texture[0].data.text.shortcut = TRUE;
434 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
436 text_a->texture[0].data.text.shortcut = FALSE;
438 case OB_MENU_ENTRY_TYPE_SEPARATOR:
439 if (self->entry->data.separator.label != NULL) {
440 text_a = ob_rr_theme->a_menu_text_title;
441 text_a->texture[0].data.text.string =
442 self->entry->data.separator.label;
445 text_a = ob_rr_theme->a_menu_text_normal;
448 g_assert_not_reached();
451 switch (self->entry->type) {
452 case OB_MENU_ENTRY_TYPE_NORMAL:
453 XMoveResizeWindow(obt_display, self->text,
454 self->frame->text_x, PADDING,
456 ITEM_HEIGHT - 2*PADDING);
457 text_a->surface.parent = item_a;
458 text_a->surface.parentx = self->frame->text_x;
459 text_a->surface.parenty = PADDING;
460 RrPaint(text_a, self->text, self->frame->text_w,
461 ITEM_HEIGHT - 2*PADDING);
463 case OB_MENU_ENTRY_TYPE_SUBMENU:
464 XMoveResizeWindow(obt_display, self->text,
465 self->frame->text_x, PADDING,
466 self->frame->text_w - ITEM_HEIGHT,
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 - ITEM_HEIGHT,
472 ITEM_HEIGHT - 2*PADDING);
474 case OB_MENU_ENTRY_TYPE_SEPARATOR:
475 if (self->entry->data.separator.label != NULL) {
476 /* labeled separator */
477 XMoveResizeWindow(obt_display, self->text,
478 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
479 self->area.width - 2*ob_rr_theme->paddingx,
480 ob_rr_theme->menu_title_height -
481 2*ob_rr_theme->paddingy);
482 text_a->surface.parent = item_a;
483 text_a->surface.parentx = ob_rr_theme->paddingx;
484 text_a->surface.parenty = ob_rr_theme->paddingy;
485 RrPaint(text_a, self->text,
486 self->area.width - 2*ob_rr_theme->paddingx,
487 ob_rr_theme->menu_title_height -
488 2*ob_rr_theme->paddingy);
492 /* unlabeled separator */
493 XMoveResizeWindow(obt_display, self->text, 0, 0,
495 ob_rr_theme->menu_sep_width +
496 2*ob_rr_theme->menu_sep_paddingy);
498 a_sep->surface.parent = item_a;
499 a_sep->surface.parentx = 0;
500 a_sep->surface.parenty = 0;
501 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
502 a_sep->texture[i].data.lineart.x1 =
503 ob_rr_theme->menu_sep_paddingx;
504 a_sep->texture[i].data.lineart.y1 =
505 ob_rr_theme->menu_sep_paddingy + i;
506 a_sep->texture[i].data.lineart.x2 =
507 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
508 a_sep->texture[i].data.lineart.y2 =
509 ob_rr_theme->menu_sep_paddingy + i;
512 RrPaint(a_sep, self->text, self->area.width,
513 ob_rr_theme->menu_sep_width +
514 2*ob_rr_theme->menu_sep_paddingy);
518 g_assert_not_reached();
521 if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
522 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
523 self->entry->data.normal.icon)
527 XMoveResizeWindow(obt_display, self->icon,
528 PADDING, frame->item_margin.top,
529 ITEM_HEIGHT - frame->item_margin.top
530 - frame->item_margin.bottom,
531 ITEM_HEIGHT - frame->item_margin.top
532 - frame->item_margin.bottom);
534 clear = ob_rr_theme->a_clear_tex;
535 RrAppearanceClearTextures(clear);
536 clear->texture[0].type = RR_TEXTURE_IMAGE;
537 clear->texture[0].data.image.image =
538 self->entry->data.normal.icon;
539 clear->texture[0].data.image.alpha =
540 self->entry->data.normal.icon_alpha;
541 clear->surface.parent = item_a;
542 clear->surface.parentx = PADDING;
543 clear->surface.parenty = frame->item_margin.top;
544 RrPaint(clear, self->icon,
545 ITEM_HEIGHT - frame->item_margin.top
546 - frame->item_margin.bottom,
547 ITEM_HEIGHT - frame->item_margin.top
548 - frame->item_margin.bottom);
549 XMapWindow(obt_display, self->icon);
550 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
551 self->entry->data.normal.mask)
556 XMoveResizeWindow(obt_display, self->icon,
557 PADDING, frame->item_margin.top,
558 ITEM_HEIGHT - frame->item_margin.top
559 - frame->item_margin.bottom,
560 ITEM_HEIGHT - frame->item_margin.top
561 - frame->item_margin.bottom);
563 clear = ob_rr_theme->a_clear_tex;
564 RrAppearanceClearTextures(clear);
565 clear->texture[0].type = RR_TEXTURE_MASK;
566 clear->texture[0].data.mask.mask =
567 self->entry->data.normal.mask;
569 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
570 !self->entry->data.normal.enabled ?
572 (self == self->frame->selected ?
573 self->entry->data.normal.mask_disabled_selected_color :
574 self->entry->data.normal.mask_disabled_color) :
576 (self == self->frame->selected ?
577 self->entry->data.normal.mask_selected_color :
578 self->entry->data.normal.mask_normal_color));
579 clear->texture[0].data.mask.color = c;
581 clear->surface.parent = item_a;
582 clear->surface.parentx = PADDING;
583 clear->surface.parenty = frame->item_margin.top;
584 RrPaint(clear, self->icon,
585 ITEM_HEIGHT - frame->item_margin.top
586 - frame->item_margin.bottom,
587 ITEM_HEIGHT - frame->item_margin.top
588 - frame->item_margin.bottom);
589 XMapWindow(obt_display, self->icon);
591 XUnmapWindow(obt_display, self->icon);
593 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
594 RrAppearance *bullet_a;
595 XMoveResizeWindow(obt_display, self->bullet,
596 self->frame->text_x + self->frame->text_w -
597 ITEM_HEIGHT + PADDING, PADDING,
598 ITEM_HEIGHT - 2*PADDING,
599 ITEM_HEIGHT - 2*PADDING);
600 bullet_a = (self == self->frame->selected ?
601 ob_rr_theme->a_menu_bullet_selected :
602 ob_rr_theme->a_menu_bullet_normal);
603 bullet_a->surface.parent = item_a;
604 bullet_a->surface.parentx =
605 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
606 bullet_a->surface.parenty = PADDING;
607 RrPaint(bullet_a, self->bullet,
608 ITEM_HEIGHT - 2*PADDING,
609 ITEM_HEIGHT - 2*PADDING);
610 XMapWindow(obt_display, self->bullet);
612 XUnmapWindow(obt_display, self->bullet);
617 /*! this code is taken from the menu_frame_render. if that changes, this won't
619 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
620 gboolean first_entry,
629 t = self->entry->type;
631 /* this is the More... entry, it's NORMAL type */
632 t = OB_MENU_ENTRY_TYPE_NORMAL;
635 case OB_MENU_ENTRY_TYPE_NORMAL:
636 case OB_MENU_ENTRY_TYPE_SUBMENU:
637 h += ob_rr_theme->menu_font_height;
639 case OB_MENU_ENTRY_TYPE_SEPARATOR:
640 if (self->entry->data.separator.label != NULL) {
641 h += ob_rr_theme->menu_title_height +
642 (ob_rr_theme->mbwidth - PADDING) * 2;
644 /* if the first entry is a labeled separator, then make its border
645 overlap with the menu's outside border */
647 h -= ob_rr_theme->mbwidth;
648 /* if the last entry is a labeled separator, then make its border
649 overlap with the menu's outside border */
651 h -= ob_rr_theme->mbwidth;
653 h += ob_rr_theme->menu_sep_width +
654 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
662 void menu_frame_render(ObMenuFrame *self)
665 gint tw, th; /* temps */
667 gboolean has_icon = FALSE;
671 /* find text dimensions */
673 STRUT_SET(self->item_margin, 0, 0, 0, 0);
678 e = self->entries->data;
679 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
680 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
685 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
686 STRUT_SET(self->item_margin,
687 MAX(self->item_margin.left, l),
688 MAX(self->item_margin.top, t),
689 MAX(self->item_margin.right, r),
690 MAX(self->item_margin.bottom, b));
691 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
692 STRUT_SET(self->item_margin,
693 MAX(self->item_margin.left, l),
694 MAX(self->item_margin.top, t),
695 MAX(self->item_margin.right, r),
696 MAX(self->item_margin.bottom, b));
697 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
698 STRUT_SET(self->item_margin,
699 MAX(self->item_margin.left, l),
700 MAX(self->item_margin.top, t),
701 MAX(self->item_margin.right, r),
702 MAX(self->item_margin.bottom, b));
703 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
704 STRUT_SET(self->item_margin,
705 MAX(self->item_margin.left, l),
706 MAX(self->item_margin.top, t),
707 MAX(self->item_margin.right, r),
708 MAX(self->item_margin.bottom, b));
711 /* render the entries */
713 for (it = self->entries; it; it = g_list_next(it)) {
714 RrAppearance *text_a;
717 /* if the first entry is a labeled separator, then make its border
718 overlap with the menu's outside border */
719 if (it == self->entries &&
720 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
721 e->entry->data.separator.label)
723 h -= ob_rr_theme->mbwidth;
726 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
727 e->entry->data.separator.label)
729 e->border = ob_rr_theme->mbwidth;
732 RECT_SET_POINT(e->area, 0, h+e->border);
733 XMoveWindow(obt_display, e->window,
734 e->area.x-e->border, e->area.y-e->border);
735 XSetWindowBorderWidth(obt_display, e->window, e->border);
736 XSetWindowBorder(obt_display, e->window,
737 RrColorPixel(ob_rr_theme->menu_border_color));
739 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
740 !e->entry->data.normal.enabled ?
742 (e == self->selected ?
743 ob_rr_theme->a_menu_text_disabled_selected :
744 ob_rr_theme->a_menu_text_disabled) :
746 (e == self->selected ?
747 ob_rr_theme->a_menu_text_selected :
748 ob_rr_theme->a_menu_text_normal));
749 switch (e->entry->type) {
750 case OB_MENU_ENTRY_TYPE_NORMAL:
751 text_a->texture[0].data.text.string = e->entry->data.normal.label;
752 tw = RrMinWidth(text_a);
753 tw = MIN(tw, MAX_MENU_WIDTH);
754 th = ob_rr_theme->menu_font_height;
756 if (e->entry->data.normal.icon ||
757 e->entry->data.normal.mask)
760 case OB_MENU_ENTRY_TYPE_SUBMENU:
761 sub = e->entry->data.submenu.submenu;
762 text_a->texture[0].data.text.string = sub ? sub->title : "";
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 tw += ITEM_HEIGHT - PADDING;
773 case OB_MENU_ENTRY_TYPE_SEPARATOR:
774 if (e->entry->data.separator.label != NULL) {
775 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
776 e->entry->data.separator.label;
777 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
778 2*ob_rr_theme->paddingx;
779 tw = MIN(tw, MAX_MENU_WIDTH);
780 th = ob_rr_theme->menu_title_height +
781 (ob_rr_theme->mbwidth - PADDING) *2;
784 th = ob_rr_theme->menu_sep_width +
785 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
789 g_assert_not_reached();
799 /* if the last entry is a labeled separator, then make its border
800 overlap with the menu's outside border */
801 it = g_list_last(self->entries);
802 e = it ? it->data : NULL;
803 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
804 e->entry->data.separator.label)
806 h -= ob_rr_theme->mbwidth;
809 self->text_x = PADDING;
814 w += ITEM_HEIGHT + PADDING;
815 self->text_x += ITEM_HEIGHT + PADDING;
822 XResizeWindow(obt_display, self->window, w, h);
826 RrPaint(self->a_items, self->window, w, h);
828 for (it = self->entries; it; it = g_list_next(it))
829 menu_entry_frame_render(it->data);
831 w += ob_rr_theme->mbwidth * 2;
832 h += ob_rr_theme->mbwidth * 2;
834 RECT_SET_SIZE(self->area, w, h);
839 static void menu_frame_update(ObMenuFrame *self)
845 menu_pipe_execute(self->menu);
846 menu_find_submenus(self->menu);
848 self->selected = NULL;
850 /* start at show_from */
851 mit = g_list_nth(self->menu->entries, self->show_from);
853 /* go through the menu's and frame's entries and connect the frame entries
854 to the menu entries */
855 for (fit = self->entries; mit && fit;
856 mit = g_list_next(mit), fit = g_list_next(fit))
858 ObMenuEntryFrame *f = fit->data;
859 f->entry = mit->data;
862 /* if there are more menu entries than in the frame, add them */
864 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
865 self->entries = g_list_append(self->entries, e);
866 mit = g_list_next(mit);
869 /* if there are more frame entries than menu entries then get rid of
872 GList *n = g_list_next(fit);
873 menu_entry_frame_free(fit->data);
874 self->entries = g_list_delete_link(self->entries, fit);
878 /* * make the menu fit on the screen */
880 /* calculate the height of the menu */
882 for (fit = self->entries; fit; fit = g_list_next(fit))
883 h += menu_entry_frame_get_height(fit->data,
884 fit == self->entries,
885 g_list_next(fit) == NULL);
886 /* add the border at the top and bottom */
887 h += ob_rr_theme->mbwidth * 2;
889 a = screen_physical_area_monitor(self->monitor);
893 gboolean last_entry = TRUE;
895 /* take the height of our More... entry into account */
896 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
898 /* start at the end of the entries */
899 flast = g_list_last(self->entries);
901 /* pull out all the entries from the frame that don't
902 fit on the screen, leaving at least 1 though */
903 while (h > a->height && g_list_previous(flast) != NULL) {
904 /* update the height, without this entry */
905 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
907 /* destroy the entry we're not displaying */
909 flast = g_list_previous(flast);
910 menu_entry_frame_free(tmp->data);
911 self->entries = g_list_delete_link(self->entries, tmp);
913 /* only the first one that we see is the last entry in the menu */
918 ObMenuEntry *more_entry;
919 ObMenuEntryFrame *more_frame;
920 /* make the More... menu entry frame which will display in this
922 if self->menu->more_menu is NULL that means that this is already
923 More... menu, so just use ourself.
925 more_entry = menu_get_more((self->menu->more_menu ?
926 self->menu->more_menu :
928 /* continue where we left off */
930 g_list_length(self->entries));
931 more_frame = menu_entry_frame_new(more_entry, self);
932 /* make it get deleted when the menu frame goes away */
933 menu_entry_unref(more_entry);
935 /* add our More... entry to the frame */
936 self->entries = g_list_append(self->entries, more_frame);
940 menu_frame_render(self);
943 static gboolean menu_frame_is_visible(ObMenuFrame *self)
945 return !!(g_list_find(menu_frame_visible, self));
948 static gboolean menu_frame_show(ObMenuFrame *self)
952 /* determine if the underlying menu is already visible */
953 for (it = menu_frame_visible; it; it = g_list_next(it)) {
954 ObMenuFrame *f = it->data;
955 if (f->menu == self->menu)
959 if (self->menu->update_func)
960 if (!self->menu->update_func(self, self->menu->data))
964 if (menu_frame_visible == NULL) {
965 /* no menus shown yet */
967 /* grab the pointer in such a way as to pass through "owner events"
968 so that we can get enter/leave notifies in the menu. */
969 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
971 if (!grab_keyboard()) {
977 menu_frame_update(self);
979 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
981 if (self->menu->show_func)
982 self->menu->show_func(self, self->menu->data);
987 gboolean menu_frame_show_topmenu(ObMenuFrame *self, const GravityPoint *pos,
988 gint monitor, gboolean mouse,
989 gboolean user_positioned)
994 if (menu_frame_is_visible(self))
996 if (!menu_frame_show(self))
999 if (self->menu->place_func) {
1002 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1004 menu_frame_place_topmenu(self, pos, &x, &y, monitor,
1008 menu_frame_move(self, x, y);
1009 XMapWindow(obt_display, self->window);
1011 if (screen_pointer_pos(&px, &py)) {
1013 if (self->menu->warp) {
1014 x += self->area.width / 2;
1016 XWarpPointer(obt_display, None, obt_root(ob_screen), 0, 0, 0, 0, x, y);
1020 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1021 if (e && e->frame == self)
1030 /*! Stop hiding an open submenu.
1031 @child The OnMenuFrame of the submenu to be hidden
1033 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1035 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1038 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1039 ObMenuEntryFrame *parent_entry)
1044 if (menu_frame_is_visible(self))
1047 self->monitor = parent->monitor;
1048 self->parent = parent;
1049 self->parent_entry = parent_entry;
1051 /* set up parent's child to be us */
1052 if ((parent->child) != self) {
1054 menu_frame_hide(parent->child);
1055 parent->child = self;
1056 parent->child_entry = parent_entry;
1059 if (!menu_frame_show(self)) {
1060 parent->child = NULL;
1061 parent->child_entry = NULL;
1065 menu_frame_place_submenu(self, &x, &y);
1066 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1069 /*try the other side */
1070 self->direction_right = !self->direction_right;
1071 menu_frame_place_submenu(self, &x, &y);
1072 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1074 menu_frame_move(self, x + dx, y + dy);
1076 XMapWindow(obt_display, self->window);
1078 if (screen_pointer_pos(&px, &py)) {
1079 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1080 if (e && e->frame == self)
1087 static void menu_frame_hide(ObMenuFrame *self)
1089 ObMenu *const menu = self->menu;
1090 GList *it = g_list_find(menu_frame_visible, self);
1091 gulong ignore_start;
1096 if (menu->hide_func)
1097 menu->hide_func(self, menu->data);
1100 menu_frame_hide(self->child);
1102 if (!self->parent && self->menu->warp)
1103 XWarpPointer(obt_display, None, obt_root(ob_screen), 0, 0, 0, 0, self->ox, self->oy);
1106 remove_submenu_hide_timeout(self);
1108 self->parent->child = NULL;
1109 self->parent->child_entry = NULL;
1111 self->parent = NULL;
1112 self->parent_entry = NULL;
1114 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1116 if (menu_frame_visible == NULL) {
1117 /* last menu shown */
1122 ignore_start = event_start_ignore_all_enters();
1123 XUnmapWindow(obt_display, self->window);
1124 event_end_ignore_all_enters(ignore_start);
1126 menu_frame_free(self);
1128 if (menu->cleanup_func)
1129 menu->cleanup_func(menu, menu->data);
1132 void menu_frame_hide_all(void)
1136 if (config_submenu_show_delay && submenu_show_timer)
1137 /* remove any submenu open requests */
1138 g_source_remove(submenu_show_timer);
1139 if ((it = g_list_last(menu_frame_visible)))
1140 menu_frame_hide(it->data);
1143 ObMenuFrame* menu_frame_under(gint x, gint y)
1145 ObMenuFrame *ret = NULL;
1148 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1149 ObMenuFrame *f = it->data;
1151 if (RECT_CONTAINS(f->area, x, y)) {
1159 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1162 ObMenuEntryFrame *ret = NULL;
1165 if ((frame = menu_frame_under(x, y))) {
1166 x -= ob_rr_theme->mbwidth + frame->area.x;
1167 y -= ob_rr_theme->mbwidth + frame->area.y;
1169 for (it = frame->entries; it; it = g_list_next(it)) {
1170 ObMenuEntryFrame *e = it->data;
1172 if (RECT_CONTAINS(e->area, x, y)) {
1181 static gboolean submenu_show_timeout(gpointer data)
1183 g_assert(menu_frame_visible);
1184 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1188 static void submenu_show_dest(gpointer data)
1190 submenu_show_timer = 0;
1193 static gboolean submenu_hide_timeout(gpointer data)
1195 g_assert(menu_frame_visible);
1196 menu_frame_hide((ObMenuFrame*)data);
1200 static void submenu_hide_dest(gpointer data)
1202 submenu_hide_timer = 0;
1205 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1208 ObMenuEntryFrame *old = self->selected;
1209 ObMenuFrame *oldchild = self->child;
1210 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1212 /* if the user selected a separator, ignore it and reselect what we had
1214 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1218 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1221 /* if the user left this menu but we have a submenu open, move the
1222 selection back to that submenu */
1223 if (!entry && oldchild_entry)
1224 entry = oldchild_entry;
1226 if (config_submenu_show_delay && submenu_show_timer)
1227 /* remove any submenu open requests */
1228 g_source_remove(submenu_show_timer);
1230 self->selected = entry;
1233 menu_entry_frame_render(old);
1235 if (oldchild_entry) {
1236 /* There is an open submenu */
1237 if (oldchild_entry == self->selected) {
1238 /* The open submenu has been reselected, so stop hiding the
1240 remove_submenu_hide_timeout(oldchild);
1242 else if (oldchild_entry == old) {
1243 /* The open submenu was selected and is no longer, so hide the
1245 if (immediate || config_submenu_hide_delay == 0)
1246 menu_frame_hide(oldchild);
1247 else if (config_submenu_hide_delay > 0) {
1248 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1249 submenu_hide_timer =
1250 g_timeout_add_full(G_PRIORITY_DEFAULT,
1251 config_submenu_hide_delay,
1252 submenu_hide_timeout, oldchild, submenu_hide_dest);
1257 if (self->selected) {
1258 menu_entry_frame_render(self->selected);
1260 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1261 /* only show if the submenu isn't already showing */
1262 if (oldchild_entry != self->selected) {
1263 if (immediate || config_submenu_show_delay == 0)
1264 menu_entry_frame_show_submenu(self->selected);
1265 else if (config_submenu_show_delay > 0) {
1266 if (submenu_show_timer)
1267 g_source_remove(submenu_show_timer);
1268 submenu_show_timer =
1269 g_timeout_add_full(G_PRIORITY_DEFAULT,
1270 config_submenu_show_delay,
1271 submenu_show_timeout,
1272 self->selected, submenu_show_dest);
1275 /* hide the grandchildren of this menu. and move the cursor to
1277 else if (immediate && self->child && self->child->child) {
1278 menu_frame_hide(self->child->child);
1279 menu_frame_select(self->child, NULL, TRUE);
1285 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1289 if (!self->entry->data.submenu.submenu) return;
1291 f = menu_frame_new(self->entry->data.submenu.submenu,
1292 self->entry->data.submenu.show_from,
1293 self->frame->client);
1294 /* pass our direction on to our child */
1295 f->direction_right = self->frame->direction_right;
1297 if (!menu_frame_show_submenu(f, self->frame, self))
1301 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1303 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1304 self->entry->data.normal.enabled)
1306 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1308 ObMenuEntry *entry = self->entry;
1309 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1310 gpointer data = self->frame->menu->data;
1311 GSList *acts = self->entry->data.normal.actions;
1312 ObClient *client = self->frame->client;
1313 ObMenuFrame *frame = self->frame;
1314 guint mods = obt_keyboard_only_modmasks(state);
1316 /* release grabs before executing the shit */
1317 if (!(mods & ControlMask)) {
1318 event_cancel_all_key_grabs();
1323 func(entry, frame, client, state, data);
1325 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1326 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1330 void menu_frame_select_previous(ObMenuFrame *self)
1332 GList *it = NULL, *start;
1334 if (self->entries) {
1335 start = it = g_list_find(self->entries, self->selected);
1337 ObMenuEntryFrame *e;
1339 it = it ? g_list_previous(it) : g_list_last(self->entries);
1345 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1347 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1352 menu_frame_select(self, it ? it->data : NULL, FALSE);
1355 void menu_frame_select_next(ObMenuFrame *self)
1357 GList *it = NULL, *start;
1359 if (self->entries) {
1360 start = it = g_list_find(self->entries, self->selected);
1362 ObMenuEntryFrame *e;
1364 it = it ? g_list_next(it) : self->entries;
1370 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1372 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1377 menu_frame_select(self, it ? it->data : NULL, FALSE);
1380 void menu_frame_select_first(ObMenuFrame *self)
1384 if (self->entries) {
1385 for (it = self->entries; it; it = g_list_next(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);
1396 void menu_frame_select_last(ObMenuFrame *self)
1400 if (self->entries) {
1401 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1402 ObMenuEntryFrame *e = it->data;
1403 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1405 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1409 menu_frame_select(self, it ? it->data : NULL, FALSE);