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 (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
186 self->icon = createWindow(self->window, 0, NULL);
187 g_hash_table_insert(menu_frame_map, &self->icon, self);
189 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
190 self->bullet = createWindow(self->window, 0, NULL);
191 g_hash_table_insert(menu_frame_map, &self->bullet, self);
194 XMapWindow(obt_display, self->window);
195 XMapWindow(obt_display, self->text);
197 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
202 static void menu_entry_frame_free(ObMenuEntryFrame *self)
205 menu_entry_unref(self->entry);
207 window_remove(self->window);
209 XDestroyWindow(obt_display, self->text);
210 XDestroyWindow(obt_display, self->window);
211 g_hash_table_remove(menu_frame_map, &self->text);
212 g_hash_table_remove(menu_frame_map, &self->window);
213 if ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
214 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
215 XDestroyWindow(obt_display, self->icon);
216 g_hash_table_remove(menu_frame_map, &self->icon);
218 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
219 XDestroyWindow(obt_display, self->bullet);
220 g_hash_table_remove(menu_frame_map, &self->bullet);
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, gint *x, gint *y)
238 if (config_menu_middle) {
242 *y -= self->area.height / 2;
244 /* try to the right of the cursor */
245 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
246 self->direction_right = TRUE;
248 /* try to the left of the cursor */
249 myx = *x - self->area.width;
250 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
251 self->direction_right = FALSE;
254 /* if didnt fit on either side so just use what it says */
256 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
257 self->direction_right = TRUE;
267 /* try to the bottom right of the cursor */
268 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
269 self->direction_right = TRUE;
270 if (dx != 0 || dy != 0) {
271 /* try to the bottom left of the cursor */
272 myx = *x - self->area.width;
274 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
275 self->direction_right = FALSE;
277 if (dx != 0 || dy != 0) {
278 /* try to the top right of the cursor */
280 myy = *y - self->area.height;
281 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
282 self->direction_right = TRUE;
284 if (dx != 0 || dy != 0) {
285 /* try to the top left of the cursor */
286 myx = *x - self->area.width;
287 myy = *y - self->area.height;
288 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
289 self->direction_right = FALSE;
291 if (dx != 0 || dy != 0) {
292 /* if didnt fit on either side so just use what it says */
295 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
296 self->direction_right = TRUE;
303 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
305 gint overlapx, overlapy;
308 overlapx = ob_rr_theme->menu_overlap_x;
309 overlapy = ob_rr_theme->menu_overlap_y;
310 bwidth = ob_rr_theme->mbwidth;
312 if (self->direction_right)
313 *x = self->parent->area.x + self->parent->area.width -
316 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
318 *y = self->parent->area.y + self->parent_entry->area.y;
319 if (config_menu_middle)
320 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
325 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
328 const Rect *a = NULL;
329 Rect search = self->area;
330 gint pos, half, monitor;
333 RECT_SET_POINT(search, x, y);
336 monitor = self->parent->monitor;
338 monitor = screen_find_monitor(&search);
340 a = screen_physical_area_monitor(monitor);
342 half = g_list_length(self->entries) / 2;
343 pos = g_list_index(self->entries, self->selected);
345 /* if in the bottom half then check this stuff first, will keep the bottom
346 edge of the menu visible */
348 *dx = MAX(*dx, a->x - x);
349 *dy = MAX(*dy, a->y - y);
351 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
352 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
353 /* if in the top half then check this stuff last, will keep the top
354 edge of the menu visible */
356 *dx = MAX(*dx, a->x - x);
357 *dy = MAX(*dy, a->y - y);
361 static void menu_entry_frame_render(ObMenuEntryFrame *self)
363 RrAppearance *item_a, *text_a;
366 ObMenuFrame *frame = self->frame;
368 switch (self->entry->type) {
369 case OB_MENU_ENTRY_TYPE_NORMAL:
370 case OB_MENU_ENTRY_TYPE_SUBMENU:
371 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
372 !self->entry->data.normal.enabled ?
374 (self == self->frame->selected ?
375 ob_rr_theme->a_menu_disabled_selected :
376 ob_rr_theme->a_menu_disabled) :
378 (self == self->frame->selected ?
379 ob_rr_theme->a_menu_selected :
380 ob_rr_theme->a_menu_normal));
383 case OB_MENU_ENTRY_TYPE_SEPARATOR:
384 if (self->entry->data.separator.label) {
385 item_a = ob_rr_theme->a_menu_title;
386 th = ob_rr_theme->menu_title_height;
388 item_a = ob_rr_theme->a_menu_normal;
389 th = ob_rr_theme->menu_sep_width +
390 2*ob_rr_theme->menu_sep_paddingy;
394 g_assert_not_reached();
397 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
398 XResizeWindow(obt_display, self->window,
399 self->area.width, self->area.height);
400 item_a->surface.parent = self->frame->a_items;
401 item_a->surface.parentx = self->area.x;
402 item_a->surface.parenty = self->area.y;
403 RrPaint(item_a, self->window, self->area.width, self->area.height);
405 switch (self->entry->type) {
406 case OB_MENU_ENTRY_TYPE_NORMAL:
407 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
408 !self->entry->data.normal.enabled ?
410 (self == self->frame->selected ?
411 ob_rr_theme->a_menu_text_disabled_selected :
412 ob_rr_theme->a_menu_text_disabled) :
414 (self == self->frame->selected ?
415 ob_rr_theme->a_menu_text_selected :
416 ob_rr_theme->a_menu_text_normal));
417 text_a->texture[0].data.text.string = self->entry->data.normal.label;
418 if (self->entry->data.normal.shortcut &&
419 (self->frame->menu->show_all_shortcuts ||
420 self->entry->data.normal.shortcut_always_show ||
421 self->entry->data.normal.shortcut_position > 0))
423 text_a->texture[0].data.text.shortcut = TRUE;
424 text_a->texture[0].data.text.shortcut_pos =
425 self->entry->data.normal.shortcut_position;
427 text_a->texture[0].data.text.shortcut = FALSE;
429 case OB_MENU_ENTRY_TYPE_SUBMENU:
430 text_a = (self == self->frame->selected ?
431 ob_rr_theme->a_menu_text_selected :
432 ob_rr_theme->a_menu_text_normal);
433 sub = self->entry->data.submenu.submenu;
434 text_a->texture[0].data.text.string = sub ? sub->title : "";
435 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
436 sub->shortcut_always_show ||
437 sub->shortcut_position > 0))
439 text_a->texture[0].data.text.shortcut = TRUE;
440 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
442 text_a->texture[0].data.text.shortcut = FALSE;
444 case OB_MENU_ENTRY_TYPE_SEPARATOR:
445 if (self->entry->data.separator.label != NULL) {
446 text_a = ob_rr_theme->a_menu_text_title;
447 text_a->texture[0].data.text.string =
448 self->entry->data.separator.label;
451 text_a = ob_rr_theme->a_menu_text_normal;
454 g_assert_not_reached();
457 switch (self->entry->type) {
458 case OB_MENU_ENTRY_TYPE_NORMAL:
459 XMoveResizeWindow(obt_display, self->text,
460 self->frame->text_x, PADDING,
462 ITEM_HEIGHT - 2*PADDING);
463 text_a->surface.parent = item_a;
464 text_a->surface.parentx = self->frame->text_x;
465 text_a->surface.parenty = PADDING;
466 RrPaint(text_a, self->text, self->frame->text_w,
467 ITEM_HEIGHT - 2*PADDING);
469 case OB_MENU_ENTRY_TYPE_SUBMENU:
470 XMoveResizeWindow(obt_display, self->text,
471 self->frame->text_x, PADDING,
472 self->frame->text_w - ITEM_HEIGHT,
473 ITEM_HEIGHT - 2*PADDING);
474 text_a->surface.parent = item_a;
475 text_a->surface.parentx = self->frame->text_x;
476 text_a->surface.parenty = PADDING;
477 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
478 ITEM_HEIGHT - 2*PADDING);
480 case OB_MENU_ENTRY_TYPE_SEPARATOR:
481 if (self->entry->data.separator.label != NULL) {
482 /* labeled separator */
483 XMoveResizeWindow(obt_display, self->text,
484 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
485 self->area.width - 2*ob_rr_theme->paddingx,
486 ob_rr_theme->menu_title_height -
487 2*ob_rr_theme->paddingy);
488 text_a->surface.parent = item_a;
489 text_a->surface.parentx = ob_rr_theme->paddingx;
490 text_a->surface.parenty = ob_rr_theme->paddingy;
491 RrPaint(text_a, self->text,
492 self->area.width - 2*ob_rr_theme->paddingx,
493 ob_rr_theme->menu_title_height -
494 2*ob_rr_theme->paddingy);
498 /* unlabeled separator */
499 XMoveResizeWindow(obt_display, self->text, 0, 0,
501 ob_rr_theme->menu_sep_width +
502 2*ob_rr_theme->menu_sep_paddingy);
504 a_sep->surface.parent = item_a;
505 a_sep->surface.parentx = 0;
506 a_sep->surface.parenty = 0;
507 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
508 a_sep->texture[i].data.lineart.x1 =
509 ob_rr_theme->menu_sep_paddingx;
510 a_sep->texture[i].data.lineart.y1 =
511 ob_rr_theme->menu_sep_paddingy + i;
512 a_sep->texture[i].data.lineart.x2 =
513 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
514 a_sep->texture[i].data.lineart.y2 =
515 ob_rr_theme->menu_sep_paddingy + i;
518 RrPaint(a_sep, self->text, self->area.width,
519 ob_rr_theme->menu_sep_width +
520 2*ob_rr_theme->menu_sep_paddingy);
524 g_assert_not_reached();
527 if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
528 (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) &&
529 self->entry->data.normal.icon)
533 XMoveResizeWindow(obt_display, self->icon,
534 PADDING, frame->item_margin.top,
535 ITEM_HEIGHT - frame->item_margin.top
536 - frame->item_margin.bottom,
537 ITEM_HEIGHT - frame->item_margin.top
538 - frame->item_margin.bottom);
540 clear = ob_rr_theme->a_clear_tex;
541 RrAppearanceClearTextures(clear);
542 clear->texture[0].type = RR_TEXTURE_IMAGE;
543 clear->texture[0].data.image.image =
544 self->entry->data.normal.icon;
545 clear->texture[0].data.image.alpha =
546 self->entry->data.normal.icon_alpha;
547 clear->surface.parent = item_a;
548 clear->surface.parentx = PADDING;
549 clear->surface.parenty = frame->item_margin.top;
550 RrPaint(clear, self->icon,
551 ITEM_HEIGHT - frame->item_margin.top
552 - frame->item_margin.bottom,
553 ITEM_HEIGHT - frame->item_margin.top
554 - frame->item_margin.bottom);
555 XMapWindow(obt_display, self->icon);
556 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
557 self->entry->data.normal.mask)
562 XMoveResizeWindow(obt_display, self->icon,
563 PADDING, frame->item_margin.top,
564 ITEM_HEIGHT - frame->item_margin.top
565 - frame->item_margin.bottom,
566 ITEM_HEIGHT - frame->item_margin.top
567 - frame->item_margin.bottom);
569 clear = ob_rr_theme->a_clear_tex;
570 RrAppearanceClearTextures(clear);
571 clear->texture[0].type = RR_TEXTURE_MASK;
572 clear->texture[0].data.mask.mask =
573 self->entry->data.normal.mask;
575 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
576 !self->entry->data.normal.enabled ?
578 (self == self->frame->selected ?
579 self->entry->data.normal.mask_disabled_selected_color :
580 self->entry->data.normal.mask_disabled_color) :
582 (self == self->frame->selected ?
583 self->entry->data.normal.mask_selected_color :
584 self->entry->data.normal.mask_normal_color));
585 clear->texture[0].data.mask.color = c;
587 clear->surface.parent = item_a;
588 clear->surface.parentx = PADDING;
589 clear->surface.parenty = frame->item_margin.top;
590 RrPaint(clear, self->icon,
591 ITEM_HEIGHT - frame->item_margin.top
592 - frame->item_margin.bottom,
593 ITEM_HEIGHT - frame->item_margin.top
594 - frame->item_margin.bottom);
595 XMapWindow(obt_display, self->icon);
597 XUnmapWindow(obt_display, self->icon);
599 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
600 RrAppearance *bullet_a;
601 XMoveResizeWindow(obt_display, self->bullet,
602 self->frame->text_x + self->frame->text_w -
603 ITEM_HEIGHT + PADDING, PADDING,
604 ITEM_HEIGHT - 2*PADDING,
605 ITEM_HEIGHT - 2*PADDING);
606 bullet_a = (self == self->frame->selected ?
607 ob_rr_theme->a_menu_bullet_selected :
608 ob_rr_theme->a_menu_bullet_normal);
609 bullet_a->surface.parent = item_a;
610 bullet_a->surface.parentx =
611 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
612 bullet_a->surface.parenty = PADDING;
613 RrPaint(bullet_a, self->bullet,
614 ITEM_HEIGHT - 2*PADDING,
615 ITEM_HEIGHT - 2*PADDING);
616 XMapWindow(obt_display, self->bullet);
618 XUnmapWindow(obt_display, self->bullet);
623 /*! this code is taken from the menu_frame_render. if that changes, this won't
625 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
626 gboolean first_entry,
635 t = self->entry->type;
637 /* this is the More... entry, it's NORMAL type */
638 t = OB_MENU_ENTRY_TYPE_NORMAL;
641 case OB_MENU_ENTRY_TYPE_NORMAL:
642 case OB_MENU_ENTRY_TYPE_SUBMENU:
643 h += ob_rr_theme->menu_font_height;
645 case OB_MENU_ENTRY_TYPE_SEPARATOR:
646 if (self->entry->data.separator.label != NULL) {
647 h += ob_rr_theme->menu_title_height +
648 (ob_rr_theme->mbwidth - PADDING) * 2;
650 /* if the first entry is a labeled separator, then make its border
651 overlap with the menu's outside border */
653 h -= ob_rr_theme->mbwidth;
654 /* if the last entry is a labeled separator, then make its border
655 overlap with the menu's outside border */
657 h -= ob_rr_theme->mbwidth;
659 h += ob_rr_theme->menu_sep_width +
660 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
668 void menu_frame_render(ObMenuFrame *self)
671 gint tw, th; /* temps */
673 gboolean has_icon = FALSE;
677 /* find text dimensions */
679 STRUT_SET(self->item_margin, 0, 0, 0, 0);
684 e = self->entries->data;
685 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
686 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
691 RrMargins(ob_rr_theme->a_menu_normal, &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_selected, &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, &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));
709 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
710 STRUT_SET(self->item_margin,
711 MAX(self->item_margin.left, l),
712 MAX(self->item_margin.top, t),
713 MAX(self->item_margin.right, r),
714 MAX(self->item_margin.bottom, b));
717 /* render the entries */
719 for (it = self->entries; it; it = g_list_next(it)) {
720 RrAppearance *text_a;
723 /* if the first entry is a labeled separator, then make its border
724 overlap with the menu's outside border */
725 if (it == self->entries &&
726 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
727 e->entry->data.separator.label)
729 h -= ob_rr_theme->mbwidth;
732 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
733 e->entry->data.separator.label)
735 e->border = ob_rr_theme->mbwidth;
738 RECT_SET_POINT(e->area, 0, h+e->border);
739 XMoveWindow(obt_display, e->window,
740 e->area.x-e->border, e->area.y-e->border);
741 XSetWindowBorderWidth(obt_display, e->window, e->border);
742 XSetWindowBorder(obt_display, e->window,
743 RrColorPixel(ob_rr_theme->menu_border_color));
745 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
746 !e->entry->data.normal.enabled ?
748 (e == self->selected ?
749 ob_rr_theme->a_menu_text_disabled_selected :
750 ob_rr_theme->a_menu_text_disabled) :
752 (e == self->selected ?
753 ob_rr_theme->a_menu_text_selected :
754 ob_rr_theme->a_menu_text_normal));
755 switch (e->entry->type) {
756 case OB_MENU_ENTRY_TYPE_NORMAL:
757 text_a->texture[0].data.text.string = e->entry->data.normal.label;
758 tw = RrMinWidth(text_a);
759 tw = MIN(tw, MAX_MENU_WIDTH);
760 th = ob_rr_theme->menu_font_height;
762 if (e->entry->data.normal.icon ||
763 e->entry->data.normal.mask)
766 case OB_MENU_ENTRY_TYPE_SUBMENU:
767 sub = e->entry->data.submenu.submenu;
768 text_a->texture[0].data.text.string = sub ? sub->title : "";
769 tw = RrMinWidth(text_a);
770 tw = MIN(tw, MAX_MENU_WIDTH);
771 th = ob_rr_theme->menu_font_height;
773 if (e->entry->data.normal.icon ||
774 e->entry->data.normal.mask)
777 tw += ITEM_HEIGHT - PADDING;
779 case OB_MENU_ENTRY_TYPE_SEPARATOR:
780 if (e->entry->data.separator.label != NULL) {
781 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
782 e->entry->data.separator.label;
783 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
784 2*ob_rr_theme->paddingx;
785 tw = MIN(tw, MAX_MENU_WIDTH);
786 th = ob_rr_theme->menu_title_height +
787 (ob_rr_theme->mbwidth - PADDING) *2;
790 th = ob_rr_theme->menu_sep_width +
791 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
795 g_assert_not_reached();
803 /* if the last entry is a labeled separator, then make its border
804 overlap with the menu's outside border */
805 it = g_list_last(self->entries);
806 e = it ? it->data : NULL;
807 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
808 e->entry->data.separator.label)
810 h -= ob_rr_theme->mbwidth;
813 self->text_x = PADDING;
818 w += ITEM_HEIGHT + PADDING;
819 self->text_x += ITEM_HEIGHT + PADDING;
826 XResizeWindow(obt_display, self->window, w, h);
830 RrPaint(self->a_items, self->window, w, h);
832 for (it = self->entries; it; it = g_list_next(it))
833 menu_entry_frame_render(it->data);
835 w += ob_rr_theme->mbwidth * 2;
836 h += ob_rr_theme->mbwidth * 2;
838 RECT_SET_SIZE(self->area, w, h);
843 static void menu_frame_update(ObMenuFrame *self)
849 menu_pipe_execute(self->menu);
850 menu_find_submenus(self->menu);
852 self->selected = NULL;
854 /* start at show_from */
855 mit = g_list_nth(self->menu->entries, self->show_from);
857 /* go through the menu's and frame's entries and connect the frame entries
858 to the menu entries */
859 for (fit = self->entries; mit && fit;
860 mit = g_list_next(mit), fit = g_list_next(fit))
862 ObMenuEntryFrame *f = fit->data;
863 f->entry = mit->data;
866 /* if there are more menu entries than in the frame, add them */
868 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
869 self->entries = g_list_append(self->entries, e);
870 mit = g_list_next(mit);
873 /* if there are more frame entries than menu entries then get rid of
876 GList *n = g_list_next(fit);
877 menu_entry_frame_free(fit->data);
878 self->entries = g_list_delete_link(self->entries, fit);
882 /* * make the menu fit on the screen */
884 /* calculate the height of the menu */
886 for (fit = self->entries; fit; fit = g_list_next(fit))
887 h += menu_entry_frame_get_height(fit->data,
888 fit == self->entries,
889 g_list_next(fit) == NULL);
890 /* add the border at the top and bottom */
891 h += ob_rr_theme->mbwidth * 2;
893 a = screen_physical_area_monitor(self->monitor);
897 gboolean last_entry = TRUE;
899 /* take the height of our More... entry into account */
900 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
902 /* start at the end of the entries */
903 flast = g_list_last(self->entries);
905 /* pull out all the entries from the frame that don't
906 fit on the screen, leaving at least 1 though */
907 while (h > a->height && g_list_previous(flast) != NULL) {
908 /* update the height, without this entry */
909 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
911 /* destroy the entry we're not displaying */
913 flast = g_list_previous(flast);
914 menu_entry_frame_free(tmp->data);
915 self->entries = g_list_delete_link(self->entries, tmp);
917 /* only the first one that we see is the last entry in the menu */
922 ObMenuEntry *more_entry;
923 ObMenuEntryFrame *more_frame;
924 /* make the More... menu entry frame which will display in this
926 if self->menu->more_menu is NULL that means that this is already
927 More... menu, so just use ourself.
929 more_entry = menu_get_more((self->menu->more_menu ?
930 self->menu->more_menu :
932 /* continue where we left off */
934 g_list_length(self->entries));
935 more_frame = menu_entry_frame_new(more_entry, self);
936 /* make it get deleted when the menu frame goes away */
937 menu_entry_unref(more_entry);
939 /* add our More... entry to the frame */
940 self->entries = g_list_append(self->entries, more_frame);
944 menu_frame_render(self);
947 static gboolean menu_frame_is_visible(ObMenuFrame *self)
949 return !!(g_list_find(menu_frame_visible, self));
952 static gboolean menu_frame_show(ObMenuFrame *self)
956 /* determine if the underlying menu is already visible */
957 for (it = menu_frame_visible; it; it = g_list_next(it)) {
958 ObMenuFrame *f = it->data;
959 if (f->menu == self->menu)
963 if (self->menu->update_func)
964 if (!self->menu->update_func(self, self->menu->data))
968 if (menu_frame_visible == NULL) {
969 /* no menus shown yet */
971 /* grab the pointer in such a way as to pass through "owner events"
972 so that we can get enter/leave notifies in the menu. */
973 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
975 if (!grab_keyboard()) {
981 menu_frame_update(self);
983 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
985 if (self->menu->show_func)
986 self->menu->show_func(self, self->menu->data);
991 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
996 if (menu_frame_is_visible(self))
998 if (!menu_frame_show(self))
1001 if (self->menu->place_func)
1002 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1004 menu_frame_place_topmenu(self, &x, &y);
1006 menu_frame_move(self, x, y);
1008 XMapWindow(obt_display, self->window);
1010 if (screen_pointer_pos(&px, &py)) {
1011 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1012 if (e && e->frame == self)
1019 /*! Stop hiding an open submenu.
1020 @child The OnMenuFrame of the submenu to be hidden
1022 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1024 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1025 submenu_hide_timer = 0;
1028 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1029 ObMenuEntryFrame *parent_entry)
1034 if (menu_frame_is_visible(self))
1037 self->monitor = parent->monitor;
1038 self->parent = parent;
1039 self->parent_entry = parent_entry;
1041 /* set up parent's child to be us */
1042 if ((parent->child) != self) {
1044 menu_frame_hide(parent->child);
1045 parent->child = self;
1046 parent->child_entry = parent_entry;
1049 if (!menu_frame_show(self)) {
1050 parent->child = NULL;
1051 parent->child_entry = NULL;
1055 menu_frame_place_submenu(self, &x, &y);
1056 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1059 /*try the other side */
1060 self->direction_right = !self->direction_right;
1061 menu_frame_place_submenu(self, &x, &y);
1062 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1064 menu_frame_move(self, x + dx, y + dy);
1066 XMapWindow(obt_display, self->window);
1068 if (screen_pointer_pos(&px, &py)) {
1069 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1070 if (e && e->frame == self)
1077 static void menu_frame_hide(ObMenuFrame *self)
1079 ObMenu *const menu = self->menu;
1080 GList *it = g_list_find(menu_frame_visible, self);
1081 gulong ignore_start;
1086 if (menu->hide_func)
1087 menu->hide_func(self, menu->data);
1090 menu_frame_hide(self->child);
1093 remove_submenu_hide_timeout(self);
1095 self->parent->child = NULL;
1096 self->parent->child_entry = NULL;
1098 self->parent = NULL;
1099 self->parent_entry = NULL;
1101 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1103 if (menu_frame_visible == NULL) {
1104 /* last menu shown */
1109 ignore_start = event_start_ignore_all_enters();
1110 XUnmapWindow(obt_display, self->window);
1111 event_end_ignore_all_enters(ignore_start);
1113 menu_frame_free(self);
1115 if (menu->cleanup_func)
1116 menu->cleanup_func(menu, menu->data);
1119 void menu_frame_hide_all(void)
1123 if (config_submenu_show_delay) {
1124 /* remove any submenu open requests */
1125 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1126 submenu_show_timer = 0;
1128 if ((it = g_list_last(menu_frame_visible)))
1129 menu_frame_hide(it->data);
1132 ObMenuFrame* menu_frame_under(gint x, gint y)
1134 ObMenuFrame *ret = NULL;
1137 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1138 ObMenuFrame *f = it->data;
1140 if (RECT_CONTAINS(f->area, x, y)) {
1148 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1151 ObMenuEntryFrame *ret = NULL;
1154 if ((frame = menu_frame_under(x, y))) {
1155 x -= ob_rr_theme->mbwidth + frame->area.x;
1156 y -= ob_rr_theme->mbwidth + frame->area.y;
1158 for (it = frame->entries; it; it = g_list_next(it)) {
1159 ObMenuEntryFrame *e = it->data;
1161 if (RECT_CONTAINS(e->area, x, y)) {
1170 static gboolean submenu_show_timeout(gpointer data)
1172 g_assert(menu_frame_visible);
1173 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1177 static gboolean submenu_hide_timeout(gpointer data)
1179 g_assert(menu_frame_visible);
1180 menu_frame_hide((ObMenuFrame*)data);
1184 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1187 ObMenuEntryFrame *old = self->selected;
1188 ObMenuFrame *oldchild = self->child;
1189 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1191 /* if the user selected a separator, ignore it and reselect what we had
1193 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1197 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1200 /* if the user left this menu but we have a submenu open, move the
1201 selection back to that submenu */
1202 if (!entry && oldchild_entry)
1203 entry = oldchild_entry;
1205 if (config_submenu_show_delay) {
1206 /* remove any submenu open requests */
1207 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1208 submenu_show_timer = 0;
1211 self->selected = entry;
1214 menu_entry_frame_render(old);
1216 if (oldchild_entry) {
1217 /* There is an open submenu */
1218 if (oldchild_entry == self->selected) {
1219 /* The open submenu has been reselected, so stop hiding the
1221 remove_submenu_hide_timeout(oldchild);
1223 else if (oldchild_entry == old) {
1224 /* The open submenu was selected and is no longer, so hide the
1226 if (immediate || config_submenu_hide_delay == 0)
1227 menu_frame_hide(oldchild);
1228 else if (config_submenu_hide_delay > 0) {
1229 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1230 submenu_hide_timer =
1231 g_timeout_add_full(G_PRIORITY_DEFAULT,
1232 config_submenu_hide_delay,
1233 submenu_hide_timeout, oldchild, NULL);
1238 if (self->selected) {
1239 menu_entry_frame_render(self->selected);
1241 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1242 /* only show if the submenu isn't already showing */
1243 if (oldchild_entry != self->selected) {
1244 if (immediate || config_submenu_hide_delay == 0)
1245 menu_entry_frame_show_submenu(self->selected);
1246 else if (config_submenu_hide_delay > 0) {
1247 if (submenu_show_timer)
1248 g_source_remove(submenu_show_timer);
1249 submenu_show_timer =
1250 g_timeout_add_full(G_PRIORITY_DEFAULT,
1251 config_submenu_show_delay,
1252 submenu_show_timeout,
1253 self->selected, NULL);
1256 /* hide the grandchildren of this menu. and move the cursor to
1258 else if (immediate && self->child && self->child->child) {
1259 menu_frame_hide(self->child->child);
1260 menu_frame_select(self->child, NULL, TRUE);
1266 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1270 if (!self->entry->data.submenu.submenu) return;
1272 f = menu_frame_new(self->entry->data.submenu.submenu,
1273 self->entry->data.submenu.show_from,
1274 self->frame->client);
1275 /* pass our direction on to our child */
1276 f->direction_right = self->frame->direction_right;
1278 if (!menu_frame_show_submenu(f, self->frame, self))
1282 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1284 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1285 self->entry->data.normal.enabled)
1287 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1289 ObMenuEntry *entry = self->entry;
1290 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1291 gpointer data = self->frame->menu->data;
1292 GSList *acts = self->entry->data.normal.actions;
1293 ObClient *client = self->frame->client;
1294 ObMenuFrame *frame = self->frame;
1295 guint mods = obt_keyboard_only_modmasks(state);
1297 /* release grabs before executing the shit */
1298 if (!(mods & ControlMask)) {
1299 event_cancel_all_key_grabs();
1304 func(entry, frame, client, state, data);
1306 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1307 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1311 void menu_frame_select_previous(ObMenuFrame *self)
1313 GList *it = NULL, *start;
1315 if (self->entries) {
1316 start = it = g_list_find(self->entries, self->selected);
1318 ObMenuEntryFrame *e;
1320 it = it ? g_list_previous(it) : g_list_last(self->entries);
1326 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1328 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1333 menu_frame_select(self, it ? it->data : NULL, FALSE);
1336 void menu_frame_select_next(ObMenuFrame *self)
1338 GList *it = NULL, *start;
1340 if (self->entries) {
1341 start = it = g_list_find(self->entries, self->selected);
1343 ObMenuEntryFrame *e;
1345 it = it ? g_list_next(it) : self->entries;
1351 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1353 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1358 menu_frame_select(self, it ? it->data : NULL, FALSE);
1361 void menu_frame_select_first(ObMenuFrame *self)
1365 if (self->entries) {
1366 for (it = self->entries; it; it = g_list_next(it)) {
1367 ObMenuEntryFrame *e = it->data;
1368 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1370 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1374 menu_frame_select(self, it ? it->data : NULL, FALSE);
1377 void menu_frame_select_last(ObMenuFrame *self)
1381 if (self->entries) {
1382 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1383 ObMenuEntryFrame *e = it->data;
1384 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1386 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1390 menu_frame_select(self, it ? it->data : NULL, FALSE);