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, gint depth, gulong mask,
60 XSetWindowAttributes *attrib)
62 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
63 (depth ? depth : 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 = window_new(OB_WINDOW_CLASS_MENUFRAME, ObMenuFrame);
115 self->selected = NULL;
116 self->client = client;
117 self->direction_right = TRUE;
118 self->show_from = show_from;
120 attr.event_mask = FRAME_EVENTMASK;
121 self->depth = RrDepth(ob_rr_inst);
122 self->window = createWindow(obt_root(ob_screen), self->depth,
124 self->layer = OB_STACKING_LAYER_INTERNAL;
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_set_abstract(MENUFRAME_AS_WINDOW(self),
137 &self->window, /* top level window */
138 &self->layer, /* stacking layer */
139 &self->depth); /* window depth */
141 window_add(&self->window, MENUFRAME_AS_WINDOW(self));
142 stacking_add(MENUFRAME_AS_WINDOW(self));
147 void menu_frame_free(ObMenuFrame *self)
150 while (self->entries) {
151 menu_entry_frame_free(self->entries->data);
152 self->entries = g_list_delete_link(self->entries, self->entries);
155 stacking_remove(MENUFRAME_AS_WINDOW(self));
156 window_remove(self->window);
158 RrAppearanceFree(self->a_items);
160 XDestroyWindow(obt_display, self->window);
162 window_free(MENUFRAME_AS_WINDOW(self));
166 ObtIC* menu_frame_ic(ObMenuFrame *self)
168 /* menus are always used through a grab right now, so they can always use
169 the grab input context */
170 return grab_input_context();
173 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
176 ObMenuEntryFrame *self;
177 XSetWindowAttributes attr;
179 self = g_slice_new0(ObMenuEntryFrame);
183 menu_entry_ref(entry);
185 attr.event_mask = ENTRY_EVENTMASK;
186 self->window = createWindow(self->frame->window, 0, CWEventMask, &attr);
187 self->text = createWindow(self->window, 0, 0, NULL);
188 g_hash_table_insert(menu_frame_map, &self->window, self);
189 g_hash_table_insert(menu_frame_map, &self->text, self);
190 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
191 self->icon = createWindow(self->window, 0, 0, NULL);
192 g_hash_table_insert(menu_frame_map, &self->icon, self);
194 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
195 self->bullet = createWindow(self->window, 0, 0, NULL);
196 g_hash_table_insert(menu_frame_map, &self->bullet, self);
199 XMapWindow(obt_display, self->window);
200 XMapWindow(obt_display, self->text);
202 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
207 static void menu_entry_frame_free(ObMenuEntryFrame *self)
210 menu_entry_unref(self->entry);
212 window_remove(self->window);
214 XDestroyWindow(obt_display, self->text);
215 XDestroyWindow(obt_display, self->window);
216 g_hash_table_remove(menu_frame_map, &self->text);
217 g_hash_table_remove(menu_frame_map, &self->window);
218 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
219 XDestroyWindow(obt_display, self->icon);
220 g_hash_table_remove(menu_frame_map, &self->icon);
222 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
223 XDestroyWindow(obt_display, self->bullet);
224 g_hash_table_remove(menu_frame_map, &self->bullet);
227 g_slice_free(ObMenuEntryFrame, self);
231 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
233 RECT_SET_POINT(self->area, x, y);
234 self->monitor = screen_find_monitor_point(x, y);
235 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
238 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
242 if (config_menu_middle) {
246 *y -= self->area.height / 2;
248 /* try to the right of the cursor */
249 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
250 self->direction_right = TRUE;
252 /* try to the left of the cursor */
253 myx = *x - self->area.width;
254 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
255 self->direction_right = FALSE;
258 /* if didnt fit on either side so just use what it says */
260 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
261 self->direction_right = TRUE;
271 /* try to the bottom right of the cursor */
272 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
273 self->direction_right = TRUE;
274 if (dx != 0 || dy != 0) {
275 /* try to the bottom left of the cursor */
276 myx = *x - self->area.width;
278 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
279 self->direction_right = FALSE;
281 if (dx != 0 || dy != 0) {
282 /* try to the top right of the cursor */
284 myy = *y - self->area.height;
285 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
286 self->direction_right = TRUE;
288 if (dx != 0 || dy != 0) {
289 /* try to the top left of the cursor */
290 myx = *x - self->area.width;
291 myy = *y - self->area.height;
292 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
293 self->direction_right = FALSE;
295 if (dx != 0 || dy != 0) {
296 /* if didnt fit on either side so just use what it says */
299 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
300 self->direction_right = TRUE;
307 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
309 gint overlapx, overlapy;
312 overlapx = ob_rr_theme->menu_overlap_x;
313 overlapy = ob_rr_theme->menu_overlap_y;
314 bwidth = ob_rr_theme->mbwidth;
316 if (self->direction_right)
317 *x = self->parent->area.x + self->parent->area.width -
320 *x = self->parent->area.x - self->area.width + overlapx + bwidth;
322 *y = self->parent->area.y + self->parent_entry->area.y;
323 if (config_menu_middle)
324 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
329 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
332 const Rect *a = NULL;
337 a = screen_physical_area_monitor(screen_find_monitor_point(x, y));
339 half = g_list_length(self->entries) / 2;
340 pos = g_list_index(self->entries, self->selected);
342 /* if in the bottom half then check this stuff first, will keep the bottom
343 edge of the menu visible */
345 *dx = MAX(*dx, a->x - x);
346 *dy = MAX(*dy, a->y - y);
348 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
349 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
350 /* if in the top half then check this stuff last, will keep the top
351 edge of the menu visible */
353 *dx = MAX(*dx, a->x - x);
354 *dy = MAX(*dy, a->y - y);
358 static void menu_entry_frame_render(ObMenuEntryFrame *self)
360 RrAppearance *item_a, *text_a;
363 ObMenuFrame *frame = self->frame;
365 switch (self->entry->type) {
366 case OB_MENU_ENTRY_TYPE_NORMAL:
367 case OB_MENU_ENTRY_TYPE_SUBMENU:
368 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
369 !self->entry->data.normal.enabled ?
371 (self == self->frame->selected ?
372 ob_rr_theme->a_menu_disabled_selected :
373 ob_rr_theme->a_menu_disabled) :
375 (self == self->frame->selected ?
376 ob_rr_theme->a_menu_selected :
377 ob_rr_theme->a_menu_normal));
380 case OB_MENU_ENTRY_TYPE_SEPARATOR:
381 if (self->entry->data.separator.label) {
382 item_a = ob_rr_theme->a_menu_title;
383 th = ob_rr_theme->menu_title_height;
385 item_a = ob_rr_theme->a_menu_normal;
386 th = ob_rr_theme->menu_sep_width +
387 2*ob_rr_theme->menu_sep_paddingy;
391 g_assert_not_reached();
394 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
395 XResizeWindow(obt_display, self->window,
396 self->area.width, self->area.height);
397 item_a->surface.parent = self->frame->a_items;
398 item_a->surface.parentx = self->area.x;
399 item_a->surface.parenty = self->area.y;
400 RrPaint(item_a, self->window, self->area.width, self->area.height);
402 switch (self->entry->type) {
403 case OB_MENU_ENTRY_TYPE_NORMAL:
404 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
405 !self->entry->data.normal.enabled ?
407 (self == self->frame->selected ?
408 ob_rr_theme->a_menu_text_disabled_selected :
409 ob_rr_theme->a_menu_text_disabled) :
411 (self == self->frame->selected ?
412 ob_rr_theme->a_menu_text_selected :
413 ob_rr_theme->a_menu_text_normal));
414 text_a->texture[0].data.text.string = self->entry->data.normal.label;
415 if (self->entry->data.normal.shortcut &&
416 (self->frame->menu->show_all_shortcuts ||
417 self->entry->data.normal.shortcut_always_show ||
418 self->entry->data.normal.shortcut_position > 0))
420 text_a->texture[0].data.text.shortcut = TRUE;
421 text_a->texture[0].data.text.shortcut_pos =
422 self->entry->data.normal.shortcut_position;
424 text_a->texture[0].data.text.shortcut = FALSE;
426 case OB_MENU_ENTRY_TYPE_SUBMENU:
427 text_a = (self == self->frame->selected ?
428 ob_rr_theme->a_menu_text_selected :
429 ob_rr_theme->a_menu_text_normal);
430 sub = self->entry->data.submenu.submenu;
431 text_a->texture[0].data.text.string = sub ? sub->title : "";
432 if (sub && sub->shortcut && (self->frame->menu->show_all_shortcuts ||
433 sub->shortcut_always_show ||
434 sub->shortcut_position > 0))
436 text_a->texture[0].data.text.shortcut = TRUE;
437 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
439 text_a->texture[0].data.text.shortcut = FALSE;
441 case OB_MENU_ENTRY_TYPE_SEPARATOR:
442 if (self->entry->data.separator.label != NULL) {
443 text_a = ob_rr_theme->a_menu_text_title;
444 text_a->texture[0].data.text.string =
445 self->entry->data.separator.label;
448 text_a = ob_rr_theme->a_menu_text_normal;
451 g_assert_not_reached();
454 switch (self->entry->type) {
455 case OB_MENU_ENTRY_TYPE_NORMAL:
456 XMoveResizeWindow(obt_display, self->text,
457 self->frame->text_x, PADDING,
459 ITEM_HEIGHT - 2*PADDING);
460 text_a->surface.parent = item_a;
461 text_a->surface.parentx = self->frame->text_x;
462 text_a->surface.parenty = PADDING;
463 RrPaint(text_a, self->text, self->frame->text_w,
464 ITEM_HEIGHT - 2*PADDING);
466 case OB_MENU_ENTRY_TYPE_SUBMENU:
467 XMoveResizeWindow(obt_display, self->text,
468 self->frame->text_x, PADDING,
469 self->frame->text_w - ITEM_HEIGHT,
470 ITEM_HEIGHT - 2*PADDING);
471 text_a->surface.parent = item_a;
472 text_a->surface.parentx = self->frame->text_x;
473 text_a->surface.parenty = PADDING;
474 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
475 ITEM_HEIGHT - 2*PADDING);
477 case OB_MENU_ENTRY_TYPE_SEPARATOR:
478 if (self->entry->data.separator.label != NULL) {
479 /* labeled separator */
480 XMoveResizeWindow(obt_display, self->text,
481 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
482 self->area.width - 2*ob_rr_theme->paddingx,
483 ob_rr_theme->menu_title_height -
484 2*ob_rr_theme->paddingy);
485 text_a->surface.parent = item_a;
486 text_a->surface.parentx = ob_rr_theme->paddingx;
487 text_a->surface.parenty = ob_rr_theme->paddingy;
488 RrPaint(text_a, self->text,
489 self->area.width - 2*ob_rr_theme->paddingx,
490 ob_rr_theme->menu_title_height -
491 2*ob_rr_theme->paddingy);
495 /* unlabeled separator */
496 XMoveResizeWindow(obt_display, self->text, 0, 0,
498 ob_rr_theme->menu_sep_width +
499 2*ob_rr_theme->menu_sep_paddingy);
501 a_sep->surface.parent = item_a;
502 a_sep->surface.parentx = 0;
503 a_sep->surface.parenty = 0;
504 for (i = 0; i < ob_rr_theme->menu_sep_width; ++i) {
505 a_sep->texture[i].data.lineart.x1 =
506 ob_rr_theme->menu_sep_paddingx;
507 a_sep->texture[i].data.lineart.y1 =
508 ob_rr_theme->menu_sep_paddingy + i;
509 a_sep->texture[i].data.lineart.x2 =
510 self->area.width - ob_rr_theme->menu_sep_paddingx - 1;
511 a_sep->texture[i].data.lineart.y2 =
512 ob_rr_theme->menu_sep_paddingy + i;
515 RrPaint(a_sep, self->text, self->area.width,
516 ob_rr_theme->menu_sep_width +
517 2*ob_rr_theme->menu_sep_paddingy);
521 g_assert_not_reached();
524 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
525 self->entry->data.normal.icon)
529 XMoveResizeWindow(obt_display, self->icon,
530 PADDING, frame->item_margin.top,
531 ITEM_HEIGHT - frame->item_margin.top
532 - frame->item_margin.bottom,
533 ITEM_HEIGHT - frame->item_margin.top
534 - frame->item_margin.bottom);
536 clear = ob_rr_theme->a_clear_tex;
537 RrAppearanceClearTextures(clear);
538 clear->texture[0].type = RR_TEXTURE_IMAGE;
539 clear->texture[0].data.image.image =
540 self->entry->data.normal.icon;
541 clear->texture[0].data.image.alpha =
542 self->entry->data.normal.icon_alpha;
543 clear->surface.parent = item_a;
544 clear->surface.parentx = PADDING;
545 clear->surface.parenty = frame->item_margin.top;
546 RrPaint(clear, self->icon,
547 ITEM_HEIGHT - frame->item_margin.top
548 - frame->item_margin.bottom,
549 ITEM_HEIGHT - frame->item_margin.top
550 - frame->item_margin.bottom);
551 XMapWindow(obt_display, self->icon);
552 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
553 self->entry->data.normal.mask)
558 XMoveResizeWindow(obt_display, self->icon,
559 PADDING, frame->item_margin.top,
560 ITEM_HEIGHT - frame->item_margin.top
561 - frame->item_margin.bottom,
562 ITEM_HEIGHT - frame->item_margin.top
563 - frame->item_margin.bottom);
565 clear = ob_rr_theme->a_clear_tex;
566 RrAppearanceClearTextures(clear);
567 clear->texture[0].type = RR_TEXTURE_MASK;
568 clear->texture[0].data.mask.mask =
569 self->entry->data.normal.mask;
571 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
572 !self->entry->data.normal.enabled ?
574 (self == self->frame->selected ?
575 self->entry->data.normal.mask_disabled_selected_color :
576 self->entry->data.normal.mask_disabled_color) :
578 (self == self->frame->selected ?
579 self->entry->data.normal.mask_selected_color :
580 self->entry->data.normal.mask_normal_color));
581 clear->texture[0].data.mask.color = c;
583 clear->surface.parent = item_a;
584 clear->surface.parentx = PADDING;
585 clear->surface.parenty = frame->item_margin.top;
586 RrPaint(clear, self->icon,
587 ITEM_HEIGHT - frame->item_margin.top
588 - frame->item_margin.bottom,
589 ITEM_HEIGHT - frame->item_margin.top
590 - frame->item_margin.bottom);
591 XMapWindow(obt_display, self->icon);
593 XUnmapWindow(obt_display, self->icon);
595 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
596 RrAppearance *bullet_a;
597 XMoveResizeWindow(obt_display, self->bullet,
598 self->frame->text_x + self->frame->text_w -
599 ITEM_HEIGHT + PADDING, PADDING,
600 ITEM_HEIGHT - 2*PADDING,
601 ITEM_HEIGHT - 2*PADDING);
602 bullet_a = (self == self->frame->selected ?
603 ob_rr_theme->a_menu_bullet_selected :
604 ob_rr_theme->a_menu_bullet_normal);
605 bullet_a->surface.parent = item_a;
606 bullet_a->surface.parentx =
607 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
608 bullet_a->surface.parenty = PADDING;
609 RrPaint(bullet_a, self->bullet,
610 ITEM_HEIGHT - 2*PADDING,
611 ITEM_HEIGHT - 2*PADDING);
612 XMapWindow(obt_display, self->bullet);
614 XUnmapWindow(obt_display, self->bullet);
619 /*! this code is taken from the menu_frame_render. if that changes, this won't
621 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
622 gboolean first_entry,
631 t = self->entry->type;
633 /* this is the More... entry, it's NORMAL type */
634 t = OB_MENU_ENTRY_TYPE_NORMAL;
637 case OB_MENU_ENTRY_TYPE_NORMAL:
638 case OB_MENU_ENTRY_TYPE_SUBMENU:
639 h += ob_rr_theme->menu_font_height;
641 case OB_MENU_ENTRY_TYPE_SEPARATOR:
642 if (self->entry->data.separator.label != NULL) {
643 h += ob_rr_theme->menu_title_height +
644 (ob_rr_theme->mbwidth - PADDING) * 2;
646 /* if the first entry is a labeled separator, then make its border
647 overlap with the menu's outside border */
649 h -= ob_rr_theme->mbwidth;
650 /* if the last entry is a labeled separator, then make its border
651 overlap with the menu's outside border */
653 h -= ob_rr_theme->mbwidth;
655 h += ob_rr_theme->menu_sep_width +
656 2*ob_rr_theme->menu_sep_paddingy - PADDING * 2;
664 void menu_frame_render(ObMenuFrame *self)
667 gint tw, th; /* temps */
669 gboolean has_icon = FALSE;
673 /* find text dimensions */
675 STRUT_SET(self->item_margin, 0, 0, 0, 0);
680 e = self->entries->data;
681 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
682 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
687 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
688 STRUT_SET(self->item_margin,
689 MAX(self->item_margin.left, l),
690 MAX(self->item_margin.top, t),
691 MAX(self->item_margin.right, r),
692 MAX(self->item_margin.bottom, b));
693 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
694 STRUT_SET(self->item_margin,
695 MAX(self->item_margin.left, l),
696 MAX(self->item_margin.top, t),
697 MAX(self->item_margin.right, r),
698 MAX(self->item_margin.bottom, b));
699 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
700 STRUT_SET(self->item_margin,
701 MAX(self->item_margin.left, l),
702 MAX(self->item_margin.top, t),
703 MAX(self->item_margin.right, r),
704 MAX(self->item_margin.bottom, b));
705 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
706 STRUT_SET(self->item_margin,
707 MAX(self->item_margin.left, l),
708 MAX(self->item_margin.top, t),
709 MAX(self->item_margin.right, r),
710 MAX(self->item_margin.bottom, b));
713 /* render the entries */
715 for (it = self->entries; it; it = g_list_next(it)) {
716 RrAppearance *text_a;
719 /* if the first entry is a labeled separator, then make its border
720 overlap with the menu's outside border */
721 if (it == self->entries &&
722 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
723 e->entry->data.separator.label)
725 h -= ob_rr_theme->mbwidth;
728 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
729 e->entry->data.separator.label)
731 e->border = ob_rr_theme->mbwidth;
734 RECT_SET_POINT(e->area, 0, h+e->border);
735 XMoveWindow(obt_display, e->window,
736 e->area.x-e->border, e->area.y-e->border);
737 XSetWindowBorderWidth(obt_display, e->window, e->border);
738 XSetWindowBorder(obt_display, e->window,
739 RrColorPixel(ob_rr_theme->menu_border_color));
741 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
742 !e->entry->data.normal.enabled ?
744 (e == self->selected ?
745 ob_rr_theme->a_menu_text_disabled_selected :
746 ob_rr_theme->a_menu_text_disabled) :
748 (e == self->selected ?
749 ob_rr_theme->a_menu_text_selected :
750 ob_rr_theme->a_menu_text_normal));
751 switch (e->entry->type) {
752 case OB_MENU_ENTRY_TYPE_NORMAL:
753 text_a->texture[0].data.text.string = e->entry->data.normal.label;
754 tw = RrMinWidth(text_a);
755 tw = MIN(tw, MAX_MENU_WIDTH);
756 th = ob_rr_theme->menu_font_height;
758 if (e->entry->data.normal.icon ||
759 e->entry->data.normal.mask)
762 case OB_MENU_ENTRY_TYPE_SUBMENU:
763 sub = e->entry->data.submenu.submenu;
764 text_a->texture[0].data.text.string = sub ? sub->title : "";
765 tw = RrMinWidth(text_a);
766 tw = MIN(tw, MAX_MENU_WIDTH);
767 th = ob_rr_theme->menu_font_height;
769 if (e->entry->data.normal.icon ||
770 e->entry->data.normal.mask)
773 tw += ITEM_HEIGHT - PADDING;
775 case OB_MENU_ENTRY_TYPE_SEPARATOR:
776 if (e->entry->data.separator.label != NULL) {
777 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
778 e->entry->data.separator.label;
779 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
780 2*ob_rr_theme->paddingx;
781 tw = MIN(tw, MAX_MENU_WIDTH);
782 th = ob_rr_theme->menu_title_height +
783 (ob_rr_theme->mbwidth - PADDING) *2;
786 th = ob_rr_theme->menu_sep_width +
787 2*ob_rr_theme->menu_sep_paddingy - 2*PADDING;
791 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, gint x, gint y,
992 if (menu_frame_is_visible(self))
994 if (!menu_frame_show(self))
997 if (self->menu->place_func)
998 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
1000 menu_frame_place_topmenu(self, &x, &y);
1002 menu_frame_move(self, x, y);
1004 XMapWindow(obt_display, self->window);
1006 if (screen_pointer_pos(&px, &py)) {
1007 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1008 if (e && e->frame == self)
1015 /*! Stop hiding an open submenu.
1016 @child The OnMenuFrame of the submenu to be hidden
1018 static void remove_submenu_hide_timeout(ObMenuFrame *child)
1020 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1021 submenu_hide_timer = 0;
1024 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
1025 ObMenuEntryFrame *parent_entry)
1030 if (menu_frame_is_visible(self))
1033 self->monitor = parent->monitor;
1034 self->parent = parent;
1035 self->parent_entry = parent_entry;
1037 /* set up parent's child to be us */
1038 if ((parent->child) != self) {
1040 menu_frame_hide(parent->child);
1041 parent->child = self;
1042 parent->child_entry = parent_entry;
1045 if (!menu_frame_show(self))
1048 menu_frame_place_submenu(self, &x, &y);
1049 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1052 /*try the other side */
1053 self->direction_right = !self->direction_right;
1054 menu_frame_place_submenu(self, &x, &y);
1055 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1057 menu_frame_move(self, x + dx, y + dy);
1059 XMapWindow(obt_display, self->window);
1061 if (screen_pointer_pos(&px, &py)) {
1062 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1063 if (e && e->frame == self)
1070 static void menu_frame_hide(ObMenuFrame *self)
1072 ObMenu *const menu = self->menu;
1073 GList *it = g_list_find(menu_frame_visible, self);
1074 gulong ignore_start;
1079 if (menu->hide_func)
1080 menu->hide_func(self, menu->data);
1083 menu_frame_hide(self->child);
1086 remove_submenu_hide_timeout(self);
1088 self->parent->child = NULL;
1089 self->parent->child_entry = NULL;
1091 self->parent = NULL;
1092 self->parent_entry = NULL;
1094 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1096 if (menu_frame_visible == NULL) {
1097 /* last menu shown */
1102 ignore_start = event_start_ignore_all_enters();
1103 XUnmapWindow(obt_display, self->window);
1104 event_end_ignore_all_enters(ignore_start);
1106 menu_frame_free(self);
1108 if (menu->cleanup_func)
1109 menu->cleanup_func(menu, menu->data);
1112 void menu_frame_hide_all(void)
1116 if (config_submenu_show_delay) {
1117 /* remove any submenu open requests */
1118 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1119 submenu_show_timer = 0;
1121 if ((it = g_list_last(menu_frame_visible)))
1122 menu_frame_hide(it->data);
1125 ObMenuFrame* menu_frame_under(gint x, gint y)
1127 ObMenuFrame *ret = NULL;
1130 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1131 ObMenuFrame *f = it->data;
1133 if (RECT_CONTAINS(f->area, x, y)) {
1141 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1144 ObMenuEntryFrame *ret = NULL;
1147 if ((frame = menu_frame_under(x, y))) {
1148 x -= ob_rr_theme->mbwidth + frame->area.x;
1149 y -= ob_rr_theme->mbwidth + frame->area.y;
1151 for (it = frame->entries; it; it = g_list_next(it)) {
1152 ObMenuEntryFrame *e = it->data;
1154 if (RECT_CONTAINS(e->area, x, y)) {
1163 static gboolean submenu_show_timeout(gpointer data)
1165 g_assert(menu_frame_visible);
1166 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1167 XFlush(obt_display);
1171 static gboolean submenu_hide_timeout(gpointer data)
1173 g_assert(menu_frame_visible);
1174 menu_frame_hide((ObMenuFrame*)data);
1175 XFlush(obt_display);
1179 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1182 ObMenuEntryFrame *old = self->selected;
1183 ObMenuFrame *oldchild = self->child;
1184 ObMenuEntryFrame *oldchild_entry = self->child_entry;
1186 /* if the user selected a separator, ignore it and reselect what we had
1188 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1192 (!old || old->entry->type != OB_MENU_ENTRY_TYPE_SUBMENU))
1195 /* if the user left this menu but we have a submenu open, move the
1196 selection back to that submenu */
1197 if (!entry && oldchild_entry)
1198 entry = oldchild_entry;
1200 if (config_submenu_show_delay) {
1201 /* remove any submenu open requests */
1202 if (submenu_show_timer) g_source_remove(submenu_show_timer);
1203 submenu_show_timer = 0;
1206 self->selected = entry;
1209 menu_entry_frame_render(old);
1211 if (oldchild_entry) {
1212 /* There is an open submenu */
1213 if (oldchild_entry == self->selected) {
1214 /* The open submenu has been reselected, so stop hiding the
1216 remove_submenu_hide_timeout(oldchild);
1218 else if (oldchild_entry == old) {
1219 /* The open submenu was selected and is no longer, so hide the
1221 if (immediate || config_submenu_hide_delay == 0)
1222 menu_frame_hide(oldchild);
1223 else if (config_submenu_hide_delay > 0) {
1224 if (submenu_hide_timer) g_source_remove(submenu_hide_timer);
1225 submenu_hide_timer =
1226 g_timeout_add_full(G_PRIORITY_DEFAULT,
1227 config_submenu_hide_delay,
1228 submenu_hide_timeout, oldchild, NULL);
1233 if (self->selected) {
1234 menu_entry_frame_render(self->selected);
1236 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1237 /* only show if the submenu isn't already showing */
1238 if (oldchild_entry != self->selected) {
1239 if (immediate || config_submenu_hide_delay == 0)
1240 menu_entry_frame_show_submenu(self->selected);
1241 else if (config_submenu_hide_delay > 0) {
1242 if (submenu_show_timer)
1243 g_source_remove(submenu_show_timer);
1244 submenu_show_timer =
1245 g_timeout_add_full(G_PRIORITY_DEFAULT,
1246 config_submenu_show_delay,
1247 submenu_show_timeout,
1248 self->selected, NULL);
1251 /* hide the grandchildren of this menu. and move the cursor to
1253 else if (immediate && self->child && self->child->child) {
1254 menu_frame_hide(self->child->child);
1255 menu_frame_select(self->child, NULL, TRUE);
1261 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1265 if (!self->entry->data.submenu.submenu) return;
1267 f = menu_frame_new(self->entry->data.submenu.submenu,
1268 self->entry->data.submenu.show_from,
1269 self->frame->client);
1270 /* pass our direction on to our child */
1271 f->direction_right = self->frame->direction_right;
1273 menu_frame_show_submenu(f, self->frame, self);
1276 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1278 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1279 self->entry->data.normal.enabled)
1281 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1283 ObMenuEntry *entry = self->entry;
1284 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1285 gpointer data = self->frame->menu->data;
1286 GSList *acts = self->entry->data.normal.actions;
1287 ObClient *client = self->frame->client;
1288 ObMenuFrame *frame = self->frame;
1289 guint mods = obt_keyboard_only_modmasks(state);
1291 /* release grabs before executing the shit */
1292 if (!(mods & ControlMask)) {
1293 event_cancel_all_key_grabs();
1298 func(entry, frame, client, state, data);
1300 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1301 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1305 void menu_frame_select_previous(ObMenuFrame *self)
1307 GList *it = NULL, *start;
1309 if (self->entries) {
1310 start = it = g_list_find(self->entries, self->selected);
1312 ObMenuEntryFrame *e;
1314 it = it ? g_list_previous(it) : g_list_last(self->entries);
1320 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1322 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1327 menu_frame_select(self, it ? it->data : NULL, FALSE);
1330 void menu_frame_select_next(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_next(it) : 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_first(ObMenuFrame *self)
1359 if (self->entries) {
1360 for (it = self->entries; it; it = g_list_next(it)) {
1361 ObMenuEntryFrame *e = it->data;
1362 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1364 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1368 menu_frame_select(self, it ? it->data : NULL, FALSE);
1371 void menu_frame_select_last(ObMenuFrame *self)
1375 if (self->entries) {
1376 for (it = g_list_last(self->entries); it; it = g_list_previous(it)) {
1377 ObMenuEntryFrame *e = it->data;
1378 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1380 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1384 menu_frame_select(self, it ? it->data : NULL, FALSE);