1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 menuframe.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "menuframe.h"
30 #include "obt/keyboard.h"
31 #include "obrender/theme.h"
34 #define MAX_MENU_WIDTH 400
36 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
38 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
40 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
41 ButtonPressMask | ButtonReleaseMask)
43 GList *menu_frame_visible;
44 GHashTable *menu_frame_map;
46 static RrAppearance *a_sep;
47 static guint submenu_show_timer = 0;
48 static guint submenu_hide_timer = 0;
50 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
52 static void menu_entry_frame_free(ObMenuEntryFrame *self);
53 static void menu_frame_update(ObMenuFrame *self);
54 static gboolean submenu_show_timeout(gpointer data);
55 static void menu_frame_hide(ObMenuFrame *self);
57 static gboolean submenu_hide_timeout(gpointer data);
59 static Window createWindow(Window parent, gulong mask,
60 XSetWindowAttributes *attrib)
62 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
63 RrDepth(ob_rr_inst), InputOutput,
64 RrVisual(ob_rr_inst), mask, attrib);
67 static void client_dest(ObClient *client, gpointer data)
71 /* menus can be associated with a client, so null those refs since
72 we are disappearing now */
73 for (it = menu_frame_visible; it; it = g_list_next(it)) {
74 ObMenuFrame *f = it->data;
75 if (f->client == client)
80 void menu_frame_startup(gboolean reconfig)
84 a_sep = RrAppearanceCopy(ob_rr_theme->a_clear);
85 RrAppearanceAddTextures(a_sep, ob_rr_theme->menu_sep_width);
86 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
87 a_sep->texture[i].type = RR_TEXTURE_LINE_ART;
88 a_sep->texture[i].data.lineart.color =
89 ob_rr_theme->menu_sep_color;
94 client_add_destroy_notify(client_dest, NULL);
95 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
98 void menu_frame_shutdown(gboolean reconfig)
100 RrAppearanceFree(a_sep);
102 if (reconfig) return;
104 client_remove_destroy_notify(client_dest);
105 g_hash_table_destroy(menu_frame_map);
108 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
111 XSetWindowAttributes attr;
113 self = g_slice_new0(ObMenuFrame);
114 self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
116 self->selected = NULL;
117 self->client = client;
118 self->direction_right = TRUE;
119 self->show_from = show_from;
121 attr.event_mask = FRAME_EVENTMASK;
122 self->window = createWindow(obt_root(ob_screen),
125 /* make it a popup menu type window */
126 OBT_PROP_SET32(self->window, NET_WM_WINDOW_TYPE, ATOM,
127 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU));
129 XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
130 XSetWindowBorder(obt_display, self->window,
131 RrColorPixel(ob_rr_theme->menu_border_color));
133 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
135 window_add(&self->window, MENUFRAME_AS_WINDOW(self));
136 stacking_add(MENUFRAME_AS_WINDOW(self));
141 void menu_frame_free(ObMenuFrame *self)
144 while (self->entries) {
145 menu_entry_frame_free(self->entries->data);
146 self->entries = g_list_delete_link(self->entries, self->entries);
149 stacking_remove(MENUFRAME_AS_WINDOW(self));
150 window_remove(self->window);
152 RrAppearanceFree(self->a_items);
154 XDestroyWindow(obt_display, self->window);
156 g_slice_free(ObMenuFrame, self);
160 ObtIC* menu_frame_ic(ObMenuFrame *self)
162 /* menus are always used through a grab right now, so they can always use
163 the grab input context */
164 return grab_input_context();
167 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
170 ObMenuEntryFrame *self;
171 XSetWindowAttributes attr;
173 self = g_slice_new0(ObMenuEntryFrame);
177 menu_entry_ref(entry);
179 attr.event_mask = ENTRY_EVENTMASK;
180 self->window = createWindow(self->frame->window, CWEventMask, &attr);
181 self->text = createWindow(self->window, 0, NULL);
182 g_hash_table_insert(menu_frame_map, &self->window, self);
183 g_hash_table_insert(menu_frame_map, &self->text, self);
184 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
185 self->icon = createWindow(self->window, 0, NULL);
186 g_hash_table_insert(menu_frame_map, &self->icon, self);
188 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
189 self->bullet = createWindow(self->window, 0, NULL);
190 g_hash_table_insert(menu_frame_map, &self->bullet, self);
193 XMapWindow(obt_display, self->window);
194 XMapWindow(obt_display, self->text);
196 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
201 static void menu_entry_frame_free(ObMenuEntryFrame *self)
204 menu_entry_unref(self->entry);
206 window_remove(self->window);
208 XDestroyWindow(obt_display, self->text);
209 XDestroyWindow(obt_display, self->window);
210 g_hash_table_remove(menu_frame_map, &self->text);
211 g_hash_table_remove(menu_frame_map, &self->window);
212 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
213 XDestroyWindow(obt_display, self->icon);
214 g_hash_table_remove(menu_frame_map, &self->icon);
216 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
217 XDestroyWindow(obt_display, self->bullet);
218 g_hash_table_remove(menu_frame_map, &self->bullet);
221 g_slice_free(ObMenuEntryFrame, self);
225 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
227 RECT_SET_POINT(self->area, x, y);
228 self->monitor = screen_find_monitor_point(x, y);
229 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
232 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
236 if (config_menu_middle) {
240 *y -= self->area.height / 2;
242 /* try to the right of the cursor */
243 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
244 self->direction_right = TRUE;
246 /* try to the left of the cursor */
247 myx = *x - self->area.width;
248 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
249 self->direction_right = FALSE;
252 /* if didnt fit on either side so just use what it says */
254 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
255 self->direction_right = TRUE;
265 /* try to the bottom right of the cursor */
266 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
267 self->direction_right = TRUE;
268 if (dx != 0 || dy != 0) {
269 /* try to the bottom left of the cursor */
270 myx = *x - self->area.width;
272 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
273 self->direction_right = FALSE;
275 if (dx != 0 || dy != 0) {
276 /* try to the top right of the cursor */
278 myy = *y - self->area.height;
279 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
280 self->direction_right = TRUE;
282 if (dx != 0 || dy != 0) {
283 /* try to the top left of the cursor */
284 myx = *x - self->area.width;
285 myy = *y - self->area.height;
286 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
287 self->direction_right = FALSE;
289 if (dx != 0 || dy != 0) {
290 /* if didnt fit on either side so just use what it says */
293 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
294 self->direction_right = TRUE;
301 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
303 gint overlapx, overlapy;
306 overlapx = ob_rr_theme->menu_overlap_x;
307 overlapy = ob_rr_theme->menu_overlap_y;
308 bwidth = ob_rr_theme->mbwidth;
310 if (self->direction_right)
311 *x = self->parent->area.x + self->parent->area.width -
314 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
316 *y = self->parent->area.y + self->parent_entry->area.y;
317 if (config_menu_middle)
318 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
323 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
326 const Rect *a = NULL;
327 Rect search = self->area;
328 gint pos, half, monitor;
331 RECT_SET_POINT(search, x, y);
334 monitor = self->parent->monitor;
336 monitor = screen_find_monitor(&search);
338 a = screen_physical_area_monitor(monitor);
340 half = g_list_length(self->entries) / 2;
341 pos = g_list_index(self->entries, self->selected);
343 /* if in the bottom half then check this stuff first, will keep the bottom
344 edge of the menu visible */
346 *dx = MAX(*dx, a->x - x);
347 *dy = MAX(*dy, a->y - y);
349 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
350 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
351 /* if in the top half then check this stuff last, will keep the top
352 edge of the menu visible */
354 *dx = MAX(*dx, a->x - x);
355 *dy = MAX(*dy, a->y - y);
359 static void menu_entry_frame_render(ObMenuEntryFrame *self)
361 RrAppearance *item_a, *text_a;
364 ObMenuFrame *frame = self->frame;
366 switch (self->entry->type) {
367 case OB_MENU_ENTRY_TYPE_NORMAL:
368 case OB_MENU_ENTRY_TYPE_SUBMENU:
369 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
370 !self->entry->data.normal.enabled ?
372 (self == self->frame->selected ?
373 ob_rr_theme->a_menu_disabled_selected :
374 ob_rr_theme->a_menu_disabled) :
376 (self == self->frame->selected ?
377 ob_rr_theme->a_menu_selected :
378 ob_rr_theme->a_menu_normal));
381 case OB_MENU_ENTRY_TYPE_SEPARATOR:
382 if (self->entry->data.separator.label) {
383 item_a = ob_rr_theme->a_menu_title;
384 th = ob_rr_theme->menu_title_height;
386 item_a = ob_rr_theme->a_menu_normal;
387 th = ob_rr_theme->menu_sep_width +
388 2*ob_rr_theme->menu_sep_paddingy;
392 g_assert_not_reached();
395 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
396 XResizeWindow(obt_display, self->window,
397 self->area.width, self->area.height);
398 item_a->surface.parent = self->frame->a_items;
399 item_a->surface.parentx = self->area.x;
400 item_a->surface.parenty = self->area.y;
401 RrPaint(item_a, self->window, self->area.width, self->area.height);
403 switch (self->entry->type) {
404 case OB_MENU_ENTRY_TYPE_NORMAL:
405 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
406 !self->entry->data.normal.enabled ?
408 (self == self->frame->selected ?
409 ob_rr_theme->a_menu_text_disabled_selected :
410 ob_rr_theme->a_menu_text_disabled) :
412 (self == self->frame->selected ?
413 ob_rr_theme->a_menu_text_selected :
414 ob_rr_theme->a_menu_text_normal));
415 text_a->texture[0].data.text.string = self->entry->data.normal.label;
416 if (self->entry->data.normal.shortcut &&
417 (self->frame->menu->show_all_shortcuts ||
418 self->entry->data.normal.shortcut_always_show ||
419 self->entry->data.normal.shortcut_position > 0))
421 text_a->texture[0].data.text.shortcut = TRUE;
422 text_a->texture[0].data.text.shortcut_pos =
423 self->entry->data.normal.shortcut_position;
425 text_a->texture[0].data.text.shortcut = FALSE;
427 case OB_MENU_ENTRY_TYPE_SUBMENU:
428 text_a = (self == self->frame->selected ?
429 ob_rr_theme->a_menu_text_selected :
430 ob_rr_theme->a_menu_text_normal);
431 sub = self->entry->data.submenu.submenu;
432 text_a->texture[0].data.text.string = sub ? sub->title : "";
433 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
434 sub->shortcut_always_show ||
435 sub->shortcut_position > 0))
437 text_a->texture[0].data.text.shortcut = TRUE;
438 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
440 text_a->texture[0].data.text.shortcut = FALSE;
442 case OB_MENU_ENTRY_TYPE_SEPARATOR:
443 if (self->entry->data.separator.label != NULL) {
444 text_a = ob_rr_theme->a_menu_text_title;
445 text_a->texture[0].data.text.string =
446 self->entry->data.separator.label;
449 text_a = ob_rr_theme->a_menu_text_normal;
452 g_assert_not_reached();
455 switch (self->entry->type) {
456 case OB_MENU_ENTRY_TYPE_NORMAL:
457 XMoveResizeWindow(obt_display, self->text,
458 self->frame->text_x, PADDING,
460 ITEM_HEIGHT - 2*PADDING);
461 text_a->surface.parent = item_a;
462 text_a->surface.parentx = self->frame->text_x;
463 text_a->surface.parenty = PADDING;
464 RrPaint(text_a, self->text, self->frame->text_w,
465 ITEM_HEIGHT - 2*PADDING);
467 case OB_MENU_ENTRY_TYPE_SUBMENU:
468 XMoveResizeWindow(obt_display, self->text,
469 self->frame->text_x, PADDING,
470 self->frame->text_w - ITEM_HEIGHT,
471 ITEM_HEIGHT - 2*PADDING);
472 text_a->surface.parent = item_a;
473 text_a->surface.parentx = self->frame->text_x;
474 text_a->surface.parenty = PADDING;
475 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
476 ITEM_HEIGHT - 2*PADDING);
478 case OB_MENU_ENTRY_TYPE_SEPARATOR:
479 if (self->entry->data.separator.label != NULL) {
480 /* labeled separator */
481 XMoveResizeWindow(obt_display, self->text,
482 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
483 self->area.width - 2*ob_rr_theme->paddingx,
484 ob_rr_theme->menu_title_height -
485 2*ob_rr_theme->paddingy);
486 text_a->surface.parent = item_a;
487 text_a->surface.parentx = ob_rr_theme->paddingx;
488 text_a->surface.parenty = ob_rr_theme->paddingy;
489 RrPaint(text_a, self->text,
490 self->area.width - 2*ob_rr_theme->paddingx,
491 ob_rr_theme->menu_title_height -
492 2*ob_rr_theme->paddingy);
496 /* unlabeled separator */
497 XMoveResizeWindow(obt_display, self->text, 0, 0,
499 ob_rr_theme->menu_sep_width +
500 2*ob_rr_theme->menu_sep_paddingy);
502 a_sep->surface.parent = item_a;
503 a_sep->surface.parentx = 0;
504 a_sep->surface.parenty = 0;
505 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
506 a_sep->texture[i].data.lineart.x1 =
507 ob_rr_theme->menu_sep_paddingx;
508 a_sep->texture[i].data.lineart.y1 =
509 ob_rr_theme->menu_sep_paddingy + i;
510 a_sep->texture[i].data.lineart.x2 =
511 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
512 a_sep->texture[i].data.lineart.y2 =
513 ob_rr_theme->menu_sep_paddingy + i;
516 RrPaint(a_sep, self->text, self->area.width,
517 ob_rr_theme->menu_sep_width +
518 2*ob_rr_theme->menu_sep_paddingy);
522 g_assert_not_reached();
525 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
526 self->entry->data.normal.icon)
530 XMoveResizeWindow(obt_display, self->icon,
531 PADDING, frame->item_margin.top,
532 ITEM_HEIGHT - frame->item_margin.top
533 - frame->item_margin.bottom,
534 ITEM_HEIGHT - frame->item_margin.top
535 - frame->item_margin.bottom);
537 clear = ob_rr_theme->a_clear_tex;
538 RrAppearanceClearTextures(clear);
539 clear->texture[0].type = RR_TEXTURE_IMAGE;
540 clear->texture[0].data.image.image =
541 self->entry->data.normal.icon;
542 clear->texture[0].data.image.alpha =
543 self->entry->data.normal.icon_alpha;
544 clear->surface.parent = item_a;
545 clear->surface.parentx = PADDING;
546 clear->surface.parenty = frame->item_margin.top;
547 RrPaint(clear, self->icon,
548 ITEM_HEIGHT - frame->item_margin.top
549 - frame->item_margin.bottom,
550 ITEM_HEIGHT - frame->item_margin.top
551 - frame->item_margin.bottom);
552 XMapWindow(obt_display, self->icon);
553 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
554 self->entry->data.normal.mask)
559 XMoveResizeWindow(obt_display, self->icon,
560 PADDING, frame->item_margin.top,
561 ITEM_HEIGHT - frame->item_margin.top
562 - frame->item_margin.bottom,
563 ITEM_HEIGHT - frame->item_margin.top
564 - frame->item_margin.bottom);
566 clear = ob_rr_theme->a_clear_tex;
567 RrAppearanceClearTextures(clear);
568 clear->texture[0].type = RR_TEXTURE_MASK;
569 clear->texture[0].data.mask.mask =
570 self->entry->data.normal.mask;
572 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
573 !self->entry->data.normal.enabled ?
575 (self == self->frame->selected ?
576 self->entry->data.normal.mask_disabled_selected_color :
577 self->entry->data.normal.mask_disabled_color) :
579 (self == self->frame->selected ?
580 self->entry->data.normal.mask_selected_color :
581 self->entry->data.normal.mask_normal_color));
582 clear->texture[0].data.mask.color = c;
584 clear->surface.parent = item_a;
585 clear->surface.parentx = PADDING;
586 clear->surface.parenty = frame->item_margin.top;
587 RrPaint(clear, self->icon,
588 ITEM_HEIGHT - frame->item_margin.top
589 - frame->item_margin.bottom,
590 ITEM_HEIGHT - frame->item_margin.top
591 - frame->item_margin.bottom);
592 XMapWindow(obt_display, self->icon);
594 XUnmapWindow(obt_display, self->icon);
596 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
597 RrAppearance *bullet_a;
598 XMoveResizeWindow(obt_display, self->bullet,
599 self->frame->text_x + self->frame->text_w -
600 ITEM_HEIGHT + PADDING, PADDING,
601 ITEM_HEIGHT - 2*PADDING,
602 ITEM_HEIGHT - 2*PADDING);
603 bullet_a = (self == self->frame->selected ?
604 ob_rr_theme->a_menu_bullet_selected :
605 ob_rr_theme->a_menu_bullet_normal);
606 bullet_a->surface.parent = item_a;
607 bullet_a->surface.parentx =
608 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
609 bullet_a->surface.parenty = PADDING;
610 RrPaint(bullet_a, self->bullet,
611 ITEM_HEIGHT - 2*PADDING,
612 ITEM_HEIGHT - 2*PADDING);
613 XMapWindow(obt_display, self->bullet);
615 XUnmapWindow(obt_display, self->bullet);
620 /*! this code is taken from the menu_frame_render. if that changes, this won't
622 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
623 gboolean first_entry,
632 t = self->entry->type;
634 /* this is the More... entry, it's NORMAL type */
635 t = OB_MENU_ENTRY_TYPE_NORMAL;
638 case OB_MENU_ENTRY_TYPE_NORMAL:
639 case OB_MENU_ENTRY_TYPE_SUBMENU:
640 h += ob_rr_theme->menu_font_height;
642 case OB_MENU_ENTRY_TYPE_SEPARATOR:
643 if (self->entry->data.separator.label != NULL) {
644 h += ob_rr_theme->menu_title_height +
645 (ob_rr_theme->mbwidth - PADDING) * 2;
647 /* if the first entry is a labeled separator, then make its border
648 overlap with the menu's outside border */
650 h -= ob_rr_theme->mbwidth;
651 /* if the last entry is a labeled separator, then make its border
652 overlap with the menu's outside border */
654 h -= ob_rr_theme->mbwidth;
656 h += ob_rr_theme->menu_sep_width +
657 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
665 void menu_frame_render(ObMenuFrame *self)
668 gint tw, th; /* temps */
670 gboolean has_icon = FALSE;
674 /* find text dimensions */
676 STRUT_SET(self->item_margin, 0, 0, 0, 0);
681 e = self->entries->data;
682 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
683 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
688 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
689 STRUT_SET(self->item_margin,
690 MAX(self->item_margin.left, l),
691 MAX(self->item_margin.top, t),
692 MAX(self->item_margin.right, r),
693 MAX(self->item_margin.bottom, b));
694 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
695 STRUT_SET(self->item_margin,
696 MAX(self->item_margin.left, l),
697 MAX(self->item_margin.top, t),
698 MAX(self->item_margin.right, r),
699 MAX(self->item_margin.bottom, b));
700 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
701 STRUT_SET(self->item_margin,
702 MAX(self->item_margin.left, l),
703 MAX(self->item_margin.top, t),
704 MAX(self->item_margin.right, r),
705 MAX(self->item_margin.bottom, b));
706 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
707 STRUT_SET(self->item_margin,
708 MAX(self->item_margin.left, l),
709 MAX(self->item_margin.top, t),
710 MAX(self->item_margin.right, r),
711 MAX(self->item_margin.bottom, b));
714 /* render the entries */
716 for (it = self->entries; it; it = g_list_next(it)) {
717 RrAppearance *text_a;
720 /* if the first entry is a labeled separator, then make its border
721 overlap with the menu's outside border */
722 if (it == self->entries &&
723 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
724 e->entry->data.separator.label)
726 h -= ob_rr_theme->mbwidth;
729 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
730 e->entry->data.separator.label)
732 e->border = ob_rr_theme->mbwidth;
735 RECT_SET_POINT(e->area, 0, h+e->border);
736 XMoveWindow(obt_display, e->window,
737 e->area.x-e->border, e->area.y-e->border);
738 XSetWindowBorderWidth(obt_display, e->window, e->border);
739 XSetWindowBorder(obt_display, e->window,
740 RrColorPixel(ob_rr_theme->menu_border_color));
742 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
743 !e->entry->data.normal.enabled ?
745 (e == self->selected ?
746 ob_rr_theme->a_menu_text_disabled_selected :
747 ob_rr_theme->a_menu_text_disabled) :
749 (e == self->selected ?
750 ob_rr_theme->a_menu_text_selected :
751 ob_rr_theme->a_menu_text_normal));
752 switch (e->entry->type) {
753 case OB_MENU_ENTRY_TYPE_NORMAL:
754 text_a->texture[0].data.text.string = e->entry->data.normal.label;
755 tw = RrMinWidth(text_a);
756 tw = MIN(tw, MAX_MENU_WIDTH);
757 th = ob_rr_theme->menu_font_height;
759 if (e->entry->data.normal.icon ||
760 e->entry->data.normal.mask)
763 case OB_MENU_ENTRY_TYPE_SUBMENU:
764 sub = e->entry->data.submenu.submenu;
765 text_a->texture[0].data.text.string = sub ? sub->title : "";
766 tw = RrMinWidth(text_a);
767 tw = MIN(tw, MAX_MENU_WIDTH);
768 th = ob_rr_theme->menu_font_height;
770 if (e->entry->data.normal.icon ||
771 e->entry->data.normal.mask)
774 tw += ITEM_HEIGHT - PADDING;
776 case OB_MENU_ENTRY_TYPE_SEPARATOR:
777 if (e->entry->data.separator.label != NULL) {
778 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
779 e->entry->data.separator.label;
780 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
781 2*ob_rr_theme->paddingx;
782 tw = MIN(tw, MAX_MENU_WIDTH);
783 th = ob_rr_theme->menu_title_height +
784 (ob_rr_theme->mbwidth - PADDING) *2;
787 th = ob_rr_theme->menu_sep_width +
788 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
792 g_assert_not_reached();
800 /* if the last entry is a labeled separator, then make its border
801 overlap with the menu's outside border */
802 it = g_list_last(self->entries);
803 e = it ? it->data : NULL;
804 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
805 e->entry->data.separator.label)
807 h -= ob_rr_theme->mbwidth;
810 self->text_x = PADDING;
815 w += ITEM_HEIGHT + PADDING;
816 self->text_x += ITEM_HEIGHT + PADDING;
823 XResizeWindow(obt_display, self->window, w, h);
827 RrPaint(self->a_items, self->window, w, h);
829 for (it = self->entries; it; it = g_list_next(it))
830 menu_entry_frame_render(it->data);
832 w += ob_rr_theme->mbwidth * 2;
833 h += ob_rr_theme->mbwidth * 2;
835 RECT_SET_SIZE(self->area, w, h);
840 static void menu_frame_update(ObMenuFrame *self)
846 menu_pipe_execute(self->menu);
847 menu_find_submenus(self->menu);
849 self->selected = NULL;
851 /* start at show_from */
852 mit = g_list_nth(self->menu->entries, self->show_from);
854 /* go through the menu's and frame's entries and connect the frame entries
855 to the menu entries */
856 for (fit = self->entries; mit && fit;
857 mit = g_list_next(mit), fit = g_list_next(fit))
859 ObMenuEntryFrame *f = fit->data;
860 f->entry = mit->data;
863 /* if there are more menu entries than in the frame, add them */
865 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
866 self->entries = g_list_append(self->entries, e);
867 mit = g_list_next(mit);
870 /* if there are more frame entries than menu entries then get rid of
873 GList *n = g_list_next(fit);
874 menu_entry_frame_free(fit->data);
875 self->entries = g_list_delete_link(self->entries, fit);
879 /* * make the menu fit on the screen */
881 /* calculate the height of the menu */
883 for (fit = self->entries; fit; fit = g_list_next(fit))
884 h += menu_entry_frame_get_height(fit->data,
885 fit == self->entries,
886 g_list_next(fit) == NULL);
887 /* add the border at the top and bottom */
888 h += ob_rr_theme->mbwidth * 2;
890 a = screen_physical_area_monitor(self->monitor);
894 gboolean last_entry = TRUE;
896 /* take the height of our More... entry into account */
897 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
899 /* start at the end of the entries */
900 flast = g_list_last(self->entries);
902 /* pull out all the entries from the frame that don't
903 fit on the screen, leaving at least 1 though */
904 while (h > a->height && g_list_previous(flast) != NULL) {
905 /* update the height, without this entry */
906 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
908 /* destroy the entry we're not displaying */
910 flast = g_list_previous(flast);
911 menu_entry_frame_free(tmp->data);
912 self->entries = g_list_delete_link(self->entries, tmp);
914 /* only the first one that we see is the last entry in the menu */
919 ObMenuEntry *more_entry;
920 ObMenuEntryFrame *more_frame;
921 /* make the More... menu entry frame which will display in this
923 if self->menu->more_menu is NULL that means that this is already
924 More... menu, so just use ourself.
926 more_entry = menu_get_more((self->menu->more_menu ?
927 self->menu->more_menu :
929 /* continue where we left off */
931 g_list_length(self->entries));
932 more_frame = menu_entry_frame_new(more_entry, self);
933 /* make it get deleted when the menu frame goes away */
934 menu_entry_unref(more_entry);
936 /* add our More... entry to the frame */
937 self->entries = g_list_append(self->entries, more_frame);
941 menu_frame_render(self);
944 static gboolean menu_frame_is_visible(ObMenuFrame *self)
946 return !!(g_list_find(menu_frame_visible, self));
949 static gboolean menu_frame_show(ObMenuFrame *self)
953 /* determine if the underlying menu is already visible */
954 for (it = menu_frame_visible; it; it = g_list_next(it)) {
955 ObMenuFrame *f = it->data;
956 if (f->menu == self->menu)
960 if (self->menu->update_func)
961 if (!self->menu->update_func(self, self->menu->data))
965 if (menu_frame_visible == NULL) {
966 /* no menus shown yet */
968 /* grab the pointer in such a way as to pass through "owner events"
969 so that we can get enter/leave notifies in the menu. */
970 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
972 if (!grab_keyboard()) {
978 menu_frame_update(self);
980 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
982 if (self->menu->show_func)
983 self->menu->show_func(self, self->menu->data);
988 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
993 if (menu_frame_is_visible(self))
995 if (!menu_frame_show(self))
998 if (self->menu->place_func)
999 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1001 menu_frame_place_topmenu(self, &x, &y);
1003 menu_frame_move(self, x, y);
1005 XMapWindow(obt_display, self->window);
1007 if (screen_pointer_pos(&px, &py)) {
1008 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1009 if (e && e->frame == self)
1016 /*! Stop hiding an open submenu.
1017 @child The OnMenuFrame of the submenu to be hidden
1019 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1021 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1022 submenu_hide_timer = 0;
1025 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1026 ObMenuEntryFrame *parent_entry)
1031 if (menu_frame_is_visible(self))
1034 self->monitor = parent->monitor;
1035 self->parent = parent;
1036 self->parent_entry = parent_entry;
1038 /* set up parent's child to be us */
1039 if ((parent->child) != self) {
1041 menu_frame_hide(parent->child);
1042 parent->child = self;
1043 parent->child_entry = parent_entry;
1046 if (!menu_frame_show(self))
1049 menu_frame_place_submenu(self, &x, &y);
1050 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1053 /*try the other side */
1054 self->direction_right = !self->direction_right;
1055 menu_frame_place_submenu(self, &x, &y);
1056 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1058 menu_frame_move(self, x + dx, y + dy);
1060 XMapWindow(obt_display, self->window);
1062 if (screen_pointer_pos(&px, &py)) {
1063 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1064 if (e && e->frame == self)
1071 static void menu_frame_hide(ObMenuFrame *self)
1073 ObMenu *const menu = self->menu;
1074 GList *it = g_list_find(menu_frame_visible, self);
1075 gulong ignore_start;
1080 if (menu->hide_func)
1081 menu->hide_func(self, menu->data);
1084 menu_frame_hide(self->child);
1087 remove_submenu_hide_timeout(self);
1089 self->parent->child = NULL;
1090 self->parent->child_entry = NULL;
1092 self->parent = NULL;
1093 self->parent_entry = NULL;
1095 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1097 if (menu_frame_visible == NULL) {
1098 /* last menu shown */
1103 ignore_start = event_start_ignore_all_enters();
1104 XUnmapWindow(obt_display, self->window);
1105 event_end_ignore_all_enters(ignore_start);
1107 menu_frame_free(self);
1109 if (menu->cleanup_func)
1110 menu->cleanup_func(menu, menu->data);
1113 void menu_frame_hide_all(void)
1117 if (config_submenu_show_delay) {
1118 /* remove any submenu open requests */
1119 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1120 submenu_show_timer = 0;
1122 if ((it = g_list_last(menu_frame_visible)))
1123 menu_frame_hide(it->data);
1126 ObMenuFrame* menu_frame_under(gint x, gint y)
1128 ObMenuFrame *ret = NULL;
1131 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1132 ObMenuFrame *f = it->data;
1134 if (RECT_CONTAINS(f->area, x, y)) {
1142 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1145 ObMenuEntryFrame *ret = NULL;
1148 if ((frame = menu_frame_under(x, y))) {
1149 x -= ob_rr_theme->mbwidth + frame->area.x;
1150 y -= ob_rr_theme->mbwidth + frame->area.y;
1152 for (it = frame->entries; it; it = g_list_next(it)) {
1153 ObMenuEntryFrame *e = it->data;
1155 if (RECT_CONTAINS(e->area, x, y)) {
1164 static gboolean submenu_show_timeout(gpointer data)
1166 g_assert(menu_frame_visible);
1167 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1171 static gboolean submenu_hide_timeout(gpointer data)
1173 g_assert(menu_frame_visible);
1174 menu_frame_hide((ObMenuFrame*)data);
1178 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1181 ObMenuEntryFrame *old = self->selected;
1182 ObMenuFrame *oldchild = self->child;
1183 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1185 /* if the user selected a separator, ignore it and reselect what we had
1187 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1191 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1194 /* if the user left this menu but we have a submenu open, move the
1195 selection back to that submenu */
1196 if (!entry && oldchild_entry)
1197 entry = oldchild_entry;
1199 if (config_submenu_show_delay) {
1200 /* remove any submenu open requests */
1201 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1202 submenu_show_timer = 0;
1205 self->selected = entry;
1208 menu_entry_frame_render(old);
1210 if (oldchild_entry) {
1211 /* There is an open submenu */
1212 if (oldchild_entry == self->selected) {
1213 /* The open submenu has been reselected, so stop hiding the
1215 remove_submenu_hide_timeout(oldchild);
1217 else if (oldchild_entry == old) {
1218 /* The open submenu was selected and is no longer, so hide the
1220 if (immediate || config_submenu_hide_delay == 0)
1221 menu_frame_hide(oldchild);
1222 else if (config_submenu_hide_delay > 0) {
1223 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1224 submenu_hide_timer =
1225 g_timeout_add_full(G_PRIORITY_DEFAULT,
1226 config_submenu_hide_delay,
1227 submenu_hide_timeout, oldchild, NULL);
1232 if (self->selected) {
1233 menu_entry_frame_render(self->selected);
1235 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1236 /* only show if the submenu isn't already showing */
1237 if (oldchild_entry != self->selected) {
1238 if (immediate || config_submenu_hide_delay == 0)
1239 menu_entry_frame_show_submenu(self->selected);
1240 else if (config_submenu_hide_delay > 0) {
1241 if (submenu_show_timer)
1242 g_source_remove(submenu_show_timer);
1243 submenu_show_timer =
1244 g_timeout_add_full(G_PRIORITY_DEFAULT,
1245 config_submenu_show_delay,
1246 submenu_show_timeout,
1247 self->selected, NULL);
1250 /* hide the grandchildren of this menu. and move the cursor to
1252 else if (immediate && self->child && self->child->child) {
1253 menu_frame_hide(self->child->child);
1254 menu_frame_select(self->child, NULL, TRUE);
1260 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1264 if (!self->entry->data.submenu.submenu) return;
1266 f = menu_frame_new(self->entry->data.submenu.submenu,
1267 self->entry->data.submenu.show_from,
1268 self->frame->client);
1269 /* pass our direction on to our child */
1270 f->direction_right = self->frame->direction_right;
1272 menu_frame_show_submenu(f, self->frame, self);
1275 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1277 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1278 self->entry->data.normal.enabled)
1280 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1282 ObMenuEntry *entry = self->entry;
1283 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1284 gpointer data = self->frame->menu->data;
1285 GSList *acts = self->entry->data.normal.actions;
1286 ObClient *client = self->frame->client;
1287 ObMenuFrame *frame = self->frame;
1288 guint mods = obt_keyboard_only_modmasks(state);
1290 /* release grabs before executing the shit */
1291 if (!(mods & ControlMask)) {
1292 event_cancel_all_key_grabs();
1297 func(entry, frame, client, state, data);
1299 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1300 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1304 void menu_frame_select_previous(ObMenuFrame *self)
1306 GList *it = NULL, *start;
1308 if (self->entries) {
1309 start = it = g_list_find(self->entries, self->selected);
1311 ObMenuEntryFrame *e;
1313 it = it ? g_list_previous(it) : g_list_last(self->entries);
1319 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1321 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1326 menu_frame_select(self, it ? it->data : NULL, FALSE);
1329 void menu_frame_select_next(ObMenuFrame *self)
1331 GList *it = NULL, *start;
1333 if (self->entries) {
1334 start = it = g_list_find(self->entries, self->selected);
1336 ObMenuEntryFrame *e;
1338 it = it ? g_list_next(it) : self->entries;
1344 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1346 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1351 menu_frame_select(self, it ? it->data : NULL, FALSE);
1354 void menu_frame_select_first(ObMenuFrame *self)
1358 if (self->entries) {
1359 for (it = self->entries; it; it = g_list_next(it)) {
1360 ObMenuEntryFrame *e = it->data;
1361 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1363 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1367 menu_frame_select(self, it ? it->data : NULL, FALSE);
1370 void menu_frame_select_last(ObMenuFrame *self)
1374 if (self->entries) {
1375 for (it = g_list_last(self->entries); it; it = g_list_previous(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);