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"
28 #include "render/theme.h"
31 #define SEPARATOR_HEIGHT 3
32 #define MAX_MENU_WIDTH 400
34 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
36 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
38 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
39 ButtonPressMask | ButtonReleaseMask)
41 GList *menu_frame_visible;
43 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
45 static void menu_entry_frame_free(ObMenuEntryFrame *self);
46 static void menu_frame_update(ObMenuFrame *self);
47 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
48 static void menu_frame_hide(ObMenuFrame *self);
50 static Window createWindow(Window parent, gulong mask,
51 XSetWindowAttributes *attrib)
53 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, 0,
54 RrDepth(ob_rr_inst), InputOutput,
55 RrVisual(ob_rr_inst), mask, attrib);
58 GHashTable *menu_frame_map;
60 void menu_frame_startup(gboolean reconfig)
64 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
67 void menu_frame_shutdown(gboolean reconfig)
71 g_hash_table_destroy(menu_frame_map);
74 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
77 XSetWindowAttributes attr;
79 self = g_new0(ObMenuFrame, 1);
80 self->obwin.type = OB_WINDOW_CLASS_MENUFRAME;
82 self->selected = NULL;
83 self->client = client;
84 self->direction_right = TRUE;
85 self->show_from = show_from;
87 attr.event_mask = FRAME_EVENTMASK;
88 self->window = createWindow(obt_root(ob_screen),
91 XSetWindowBorderWidth(obt_display, self->window, ob_rr_theme->mbwidth);
92 XSetWindowBorder(obt_display, self->window,
93 RrColorPixel(ob_rr_theme->menu_border_color));
95 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
97 window_add(&self->window, MENUFRAME_AS_WINDOW(self));
98 stacking_add(MENUFRAME_AS_WINDOW(self));
103 void menu_frame_free(ObMenuFrame *self)
106 while (self->entries) {
107 menu_entry_frame_free(self->entries->data);
108 self->entries = g_list_delete_link(self->entries, self->entries);
111 stacking_remove(MENUFRAME_AS_WINDOW(self));
112 window_remove(self->window);
114 RrAppearanceFree(self->a_items);
116 XDestroyWindow(obt_display, self->window);
122 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
125 ObMenuEntryFrame *self;
126 XSetWindowAttributes attr;
128 self = g_new0(ObMenuEntryFrame, 1);
132 menu_entry_ref(entry);
134 attr.event_mask = ENTRY_EVENTMASK;
135 self->window = createWindow(self->frame->window, CWEventMask, &attr);
136 self->text = createWindow(self->window, 0, NULL);
137 g_hash_table_insert(menu_frame_map, &self->window, self);
138 g_hash_table_insert(menu_frame_map, &self->text, self);
139 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
140 self->icon = createWindow(self->window, 0, NULL);
141 g_hash_table_insert(menu_frame_map, &self->icon, self);
143 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
144 self->bullet = createWindow(self->window, 0, NULL);
145 g_hash_table_insert(menu_frame_map, &self->bullet, self);
148 XMapWindow(obt_display, self->window);
149 XMapWindow(obt_display, self->text);
151 window_add(&self->window, MENUFRAME_AS_WINDOW(self->frame));
156 static void menu_entry_frame_free(ObMenuEntryFrame *self)
159 menu_entry_unref(self->entry);
161 window_remove(self->window);
163 XDestroyWindow(obt_display, self->text);
164 XDestroyWindow(obt_display, self->window);
165 g_hash_table_remove(menu_frame_map, &self->text);
166 g_hash_table_remove(menu_frame_map, &self->window);
167 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
168 XDestroyWindow(obt_display, self->icon);
169 g_hash_table_remove(menu_frame_map, &self->icon);
171 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
172 XDestroyWindow(obt_display, self->bullet);
173 g_hash_table_remove(menu_frame_map, &self->bullet);
180 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
182 RECT_SET_POINT(self->area, x, y);
183 XMoveWindow(obt_display, self->window, self->area.x, self->area.y);
186 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
190 if (config_menu_middle) {
194 *y -= self->area.height / 2;
196 /* try to the right of the cursor */
197 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
198 self->direction_right = TRUE;
200 /* try to the left of the cursor */
201 myx = *x - self->area.width;
202 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
203 self->direction_right = FALSE;
206 /* if didnt fit on either side so just use what it says */
208 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
209 self->direction_right = TRUE;
219 /* try to the bottom right of the cursor */
220 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
221 self->direction_right = TRUE;
222 if (dx != 0 || dy != 0) {
223 /* try to the bottom left of the cursor */
224 myx = *x - self->area.width;
226 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
227 self->direction_right = FALSE;
229 if (dx != 0 || dy != 0) {
230 /* try to the top right of the cursor */
232 myy = *y - self->area.height;
233 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
234 self->direction_right = TRUE;
236 if (dx != 0 || dy != 0) {
237 /* try to the top left of the cursor */
238 myx = *x - self->area.width;
239 myy = *y - self->area.height;
240 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
241 self->direction_right = FALSE;
243 if (dx != 0 || dy != 0) {
244 /* if didnt fit on either side so just use what it says */
247 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
248 self->direction_right = TRUE;
255 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
260 overlap = ob_rr_theme->menu_overlap;
261 bwidth = ob_rr_theme->mbwidth;
263 if (self->direction_right)
264 *x = self->parent->area.x + self->parent->area.width -
267 *x = self->parent->area.x - self->area.width + overlap + bwidth;
269 *y = self->parent->area.y + self->parent_entry->area.y;
270 if (config_menu_middle)
271 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
276 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
284 a = screen_physical_area_monitor(self->monitor);
286 half = g_list_length(self->entries) / 2;
287 pos = g_list_index(self->entries, self->selected);
289 /* if in the bottom half then check this stuff first, will keep the bottom
290 edge of the menu visible */
292 *dx = MAX(*dx, a->x - x);
293 *dy = MAX(*dy, a->y - y);
295 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
296 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
297 /* if in the top half then check this stuff last, will keep the top
298 edge of the menu visible */
300 *dx = MAX(*dx, a->x - x);
301 *dy = MAX(*dy, a->y - y);
307 static void menu_entry_frame_render(ObMenuEntryFrame *self)
309 RrAppearance *item_a, *text_a;
312 ObMenuFrame *frame = self->frame;
314 switch (self->entry->type) {
315 case OB_MENU_ENTRY_TYPE_NORMAL:
316 case OB_MENU_ENTRY_TYPE_SUBMENU:
317 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
318 !self->entry->data.normal.enabled ?
320 (self == self->frame->selected ?
321 ob_rr_theme->a_menu_disabled_selected :
322 ob_rr_theme->a_menu_disabled) :
324 (self == self->frame->selected ?
325 ob_rr_theme->a_menu_selected :
326 ob_rr_theme->a_menu_normal));
329 case OB_MENU_ENTRY_TYPE_SEPARATOR:
330 if (self->entry->data.separator.label) {
331 item_a = ob_rr_theme->a_menu_title;
332 th = ob_rr_theme->menu_title_height;
334 item_a = ob_rr_theme->a_menu_normal;
335 th = SEPARATOR_HEIGHT + 2*PADDING;
339 g_assert_not_reached();
342 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
343 XResizeWindow(obt_display, self->window,
344 self->area.width, self->area.height);
345 item_a->surface.parent = self->frame->a_items;
346 item_a->surface.parentx = self->area.x;
347 item_a->surface.parenty = self->area.y;
348 RrPaint(item_a, self->window, self->area.width, self->area.height);
350 switch (self->entry->type) {
351 case OB_MENU_ENTRY_TYPE_NORMAL:
352 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
353 !self->entry->data.normal.enabled ?
355 (self == self->frame->selected ?
356 ob_rr_theme->a_menu_text_disabled_selected :
357 ob_rr_theme->a_menu_text_disabled) :
359 (self == self->frame->selected ?
360 ob_rr_theme->a_menu_text_selected :
361 ob_rr_theme->a_menu_text_normal));
362 text_a->texture[0].data.text.string = self->entry->data.normal.label;
363 if (self->entry->data.normal.shortcut &&
364 (self->frame->menu->show_all_shortcuts ||
365 self->entry->data.normal.shortcut_always_show ||
366 self->entry->data.normal.shortcut_position > 0))
368 text_a->texture[0].data.text.shortcut = TRUE;
369 text_a->texture[0].data.text.shortcut_pos =
370 self->entry->data.normal.shortcut_position;
372 text_a->texture[0].data.text.shortcut = FALSE;
374 case OB_MENU_ENTRY_TYPE_SUBMENU:
375 text_a = (self == self->frame->selected ?
376 ob_rr_theme->a_menu_text_selected :
377 ob_rr_theme->a_menu_text_normal);
378 sub = self->entry->data.submenu.submenu;
379 text_a->texture[0].data.text.string = sub ? sub->title : "";
380 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
381 sub->shortcut_always_show ||
382 sub->shortcut_position > 0))
384 text_a->texture[0].data.text.shortcut = TRUE;
385 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
387 text_a->texture[0].data.text.shortcut = FALSE;
389 case OB_MENU_ENTRY_TYPE_SEPARATOR:
390 if (self->entry->data.separator.label != NULL) {
391 text_a = ob_rr_theme->a_menu_text_title;
392 text_a->texture[0].data.text.string =
393 self->entry->data.separator.label;
396 text_a = ob_rr_theme->a_menu_text_normal;
400 switch (self->entry->type) {
401 case OB_MENU_ENTRY_TYPE_NORMAL:
402 XMoveResizeWindow(obt_display, self->text,
403 self->frame->text_x, PADDING,
405 ITEM_HEIGHT - 2*PADDING);
406 text_a->surface.parent = item_a;
407 text_a->surface.parentx = self->frame->text_x;
408 text_a->surface.parenty = PADDING;
409 RrPaint(text_a, self->text, self->frame->text_w,
410 ITEM_HEIGHT - 2*PADDING);
412 case OB_MENU_ENTRY_TYPE_SUBMENU:
413 XMoveResizeWindow(obt_display, self->text,
414 self->frame->text_x, PADDING,
415 self->frame->text_w - ITEM_HEIGHT,
416 ITEM_HEIGHT - 2*PADDING);
417 text_a->surface.parent = item_a;
418 text_a->surface.parentx = self->frame->text_x;
419 text_a->surface.parenty = PADDING;
420 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
421 ITEM_HEIGHT - 2*PADDING);
423 case OB_MENU_ENTRY_TYPE_SEPARATOR:
424 if (self->entry->data.separator.label != NULL) {
425 /* labeled separator */
426 XMoveResizeWindow(obt_display, self->text,
427 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
428 self->area.width - 2*ob_rr_theme->paddingx,
429 ob_rr_theme->menu_title_height -
430 2*ob_rr_theme->paddingy);
431 text_a->surface.parent = item_a;
432 text_a->surface.parentx = ob_rr_theme->paddingx;
433 text_a->surface.parenty = ob_rr_theme->paddingy;
434 RrPaint(text_a, self->text,
435 self->area.width - 2*ob_rr_theme->paddingx,
436 ob_rr_theme->menu_title_height -
437 2*ob_rr_theme->paddingy);
441 /* unlabeled separaator */
442 XMoveResizeWindow(obt_display, self->text, PADDING, PADDING,
443 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
445 clear = ob_rr_theme->a_clear_tex;
446 RrAppearanceClearTextures(clear);
447 clear->texture[0].type = RR_TEXTURE_LINE_ART;
448 clear->surface.parent = item_a;
449 clear->surface.parentx = PADDING;
450 clear->surface.parenty = PADDING;
451 clear->texture[0].data.lineart.color =
452 text_a->texture[0].data.text.color;
453 clear->texture[0].data.lineart.x1 = 2*PADDING;
454 clear->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
455 clear->texture[0].data.lineart.x2 = self->area.width - 4*PADDING;
456 clear->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
457 RrPaint(clear, self->text,
458 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
463 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
464 self->entry->data.normal.icon)
468 XMoveResizeWindow(obt_display, self->icon,
469 PADDING, frame->item_margin.top,
470 ITEM_HEIGHT - frame->item_margin.top
471 - frame->item_margin.bottom,
472 ITEM_HEIGHT - frame->item_margin.top
473 - frame->item_margin.bottom);
475 clear = ob_rr_theme->a_clear_tex;
476 RrAppearanceClearTextures(clear);
477 clear->texture[0].type = RR_TEXTURE_IMAGE;
478 clear->texture[0].data.image.image =
479 self->entry->data.normal.icon;
480 clear->texture[0].data.image.alpha =
481 self->entry->data.normal.icon_alpha;
482 clear->surface.parent = item_a;
483 clear->surface.parentx = PADDING;
484 clear->surface.parenty = frame->item_margin.top;
485 RrPaint(clear, self->icon,
486 ITEM_HEIGHT - frame->item_margin.top
487 - frame->item_margin.bottom,
488 ITEM_HEIGHT - frame->item_margin.top
489 - frame->item_margin.bottom);
490 XMapWindow(obt_display, self->icon);
491 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
492 self->entry->data.normal.mask)
497 XMoveResizeWindow(obt_display, self->icon,
498 PADDING, frame->item_margin.top,
499 ITEM_HEIGHT - frame->item_margin.top
500 - frame->item_margin.bottom,
501 ITEM_HEIGHT - frame->item_margin.top
502 - frame->item_margin.bottom);
504 clear = ob_rr_theme->a_clear_tex;
505 RrAppearanceClearTextures(clear);
506 clear->texture[0].type = RR_TEXTURE_MASK;
507 clear->texture[0].data.mask.mask =
508 self->entry->data.normal.mask;
510 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
511 !self->entry->data.normal.enabled ?
513 (self == self->frame->selected ?
514 self->entry->data.normal.mask_disabled_selected_color :
515 self->entry->data.normal.mask_disabled_color) :
517 (self == self->frame->selected ?
518 self->entry->data.normal.mask_selected_color :
519 self->entry->data.normal.mask_normal_color));
520 clear->texture[0].data.mask.color = c;
522 clear->surface.parent = item_a;
523 clear->surface.parentx = PADDING;
524 clear->surface.parenty = frame->item_margin.top;
525 RrPaint(clear, self->icon,
526 ITEM_HEIGHT - frame->item_margin.top
527 - frame->item_margin.bottom,
528 ITEM_HEIGHT - frame->item_margin.top
529 - frame->item_margin.bottom);
530 XMapWindow(obt_display, self->icon);
532 XUnmapWindow(obt_display, self->icon);
534 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
535 RrAppearance *bullet_a;
536 XMoveResizeWindow(obt_display, self->bullet,
537 self->frame->text_x + self->frame->text_w -
538 ITEM_HEIGHT + PADDING, PADDING,
539 ITEM_HEIGHT - 2*PADDING,
540 ITEM_HEIGHT - 2*PADDING);
541 bullet_a = (self == self->frame->selected ?
542 ob_rr_theme->a_menu_bullet_selected :
543 ob_rr_theme->a_menu_bullet_normal);
544 bullet_a->surface.parent = item_a;
545 bullet_a->surface.parentx =
546 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
547 bullet_a->surface.parenty = PADDING;
548 RrPaint(bullet_a, self->bullet,
549 ITEM_HEIGHT - 2*PADDING,
550 ITEM_HEIGHT - 2*PADDING);
551 XMapWindow(obt_display, self->bullet);
553 XUnmapWindow(obt_display, self->bullet);
558 /*! this code is taken from the menu_frame_render. if that changes, this won't
560 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
561 gboolean first_entry,
570 t = self->entry->type;
572 /* this is the More... entry, it's NORMAL type */
573 t = OB_MENU_ENTRY_TYPE_NORMAL;
576 case OB_MENU_ENTRY_TYPE_NORMAL:
577 case OB_MENU_ENTRY_TYPE_SUBMENU:
578 h += ob_rr_theme->menu_font_height;
580 case OB_MENU_ENTRY_TYPE_SEPARATOR:
581 if (self->entry->data.separator.label != NULL) {
582 h += ob_rr_theme->menu_title_height +
583 (ob_rr_theme->mbwidth - PADDING) * 2;
585 /* if the first entry is a labeled separator, then make its border
586 overlap with the menu's outside border */
588 h -= ob_rr_theme->mbwidth;
589 /* if the last entry is a labeled separator, then make its border
590 overlap with the menu's outside border */
592 h -= ob_rr_theme->mbwidth;
594 h += SEPARATOR_HEIGHT;
602 void menu_frame_render(ObMenuFrame *self)
605 gint tw, th; /* temps */
607 gboolean has_icon = FALSE;
611 /* find text dimensions */
613 STRUT_SET(self->item_margin, 0, 0, 0, 0);
618 e = self->entries->data;
619 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
620 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
625 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
626 STRUT_SET(self->item_margin,
627 MAX(self->item_margin.left, l),
628 MAX(self->item_margin.top, t),
629 MAX(self->item_margin.right, r),
630 MAX(self->item_margin.bottom, b));
631 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
632 STRUT_SET(self->item_margin,
633 MAX(self->item_margin.left, l),
634 MAX(self->item_margin.top, t),
635 MAX(self->item_margin.right, r),
636 MAX(self->item_margin.bottom, b));
637 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
638 STRUT_SET(self->item_margin,
639 MAX(self->item_margin.left, l),
640 MAX(self->item_margin.top, t),
641 MAX(self->item_margin.right, r),
642 MAX(self->item_margin.bottom, b));
643 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
644 STRUT_SET(self->item_margin,
645 MAX(self->item_margin.left, l),
646 MAX(self->item_margin.top, t),
647 MAX(self->item_margin.right, r),
648 MAX(self->item_margin.bottom, b));
651 /* render the entries */
653 for (it = self->entries; it; it = g_list_next(it)) {
654 RrAppearance *text_a;
657 /* if the first entry is a labeled separator, then make its border
658 overlap with the menu's outside border */
659 if (it == self->entries &&
660 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
661 e->entry->data.separator.label)
663 h -= ob_rr_theme->mbwidth;
666 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
667 e->entry->data.separator.label)
669 e->border = ob_rr_theme->mbwidth;
672 RECT_SET_POINT(e->area, 0, h+e->border);
673 XMoveWindow(obt_display, e->window,
674 e->area.x-e->border, e->area.y-e->border);
675 XSetWindowBorderWidth(obt_display, e->window, e->border);
676 XSetWindowBorder(obt_display, e->window,
677 RrColorPixel(ob_rr_theme->menu_border_color));
680 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
681 !e->entry->data.normal.enabled ?
683 (e == self->selected ?
684 ob_rr_theme->a_menu_text_disabled_selected :
685 ob_rr_theme->a_menu_text_disabled) :
687 (e == self->selected ?
688 ob_rr_theme->a_menu_text_selected :
689 ob_rr_theme->a_menu_text_normal));
690 switch (e->entry->type) {
691 case OB_MENU_ENTRY_TYPE_NORMAL:
692 text_a->texture[0].data.text.string = e->entry->data.normal.label;
693 tw = RrMinWidth(text_a);
694 tw = MIN(tw, MAX_MENU_WIDTH);
695 th = ob_rr_theme->menu_font_height;
697 if (e->entry->data.normal.icon ||
698 e->entry->data.normal.mask)
701 case OB_MENU_ENTRY_TYPE_SUBMENU:
702 sub = e->entry->data.submenu.submenu;
703 text_a->texture[0].data.text.string = sub ? sub->title : "";
704 tw = RrMinWidth(text_a);
705 tw = MIN(tw, MAX_MENU_WIDTH);
706 th = ob_rr_theme->menu_font_height;
708 if (e->entry->data.normal.icon ||
709 e->entry->data.normal.mask)
712 tw += ITEM_HEIGHT - PADDING;
714 case OB_MENU_ENTRY_TYPE_SEPARATOR:
715 if (e->entry->data.separator.label != NULL) {
716 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
717 e->entry->data.separator.label;
718 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
719 2*ob_rr_theme->paddingx;
720 tw = MIN(tw, MAX_MENU_WIDTH);
721 th = ob_rr_theme->menu_title_height +
722 (ob_rr_theme->mbwidth - PADDING) *2;
725 th = SEPARATOR_HEIGHT;
735 /* if the last entry is a labeled separator, then make its border
736 overlap with the menu's outside border */
737 it = g_list_last(self->entries);
738 e = it ? it->data : NULL;
739 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
740 e->entry->data.separator.label)
742 h -= ob_rr_theme->mbwidth;
745 self->text_x = PADDING;
750 w += ITEM_HEIGHT + PADDING;
751 self->text_x += ITEM_HEIGHT + PADDING;
758 XResizeWindow(obt_display, self->window, w, h);
762 RrPaint(self->a_items, self->window, w, h);
764 for (it = self->entries; it; it = g_list_next(it))
765 menu_entry_frame_render(it->data);
767 w += ob_rr_theme->mbwidth * 2;
768 h += ob_rr_theme->mbwidth * 2;
770 RECT_SET_SIZE(self->area, w, h);
775 static void menu_frame_update(ObMenuFrame *self)
781 menu_pipe_execute(self->menu);
782 menu_find_submenus(self->menu);
784 self->selected = NULL;
786 /* start at show_from */
787 mit = g_list_nth(self->menu->entries, self->show_from);
789 /* go through the menu's and frame's entries and connect the frame entries
790 to the menu entries */
791 for (fit = self->entries; mit && fit;
792 mit = g_list_next(mit), fit = g_list_next(fit))
794 ObMenuEntryFrame *f = fit->data;
795 f->entry = mit->data;
798 /* if there are more menu entries than in the frame, add them */
800 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
801 self->entries = g_list_append(self->entries, e);
802 mit = g_list_next(mit);
805 /* if there are more frame entries than menu entries then get rid of
808 GList *n = g_list_next(fit);
809 menu_entry_frame_free(fit->data);
810 self->entries = g_list_delete_link(self->entries, fit);
814 /* * make the menu fit on the screen */
816 /* calculate the height of the menu */
818 for (fit = self->entries; fit; fit = g_list_next(fit))
819 h += menu_entry_frame_get_height(fit->data,
820 fit == self->entries,
821 g_list_next(fit) == NULL);
822 /* add the border at the top and bottom */
823 h += ob_rr_theme->mbwidth * 2;
825 a = screen_physical_area_monitor(self->monitor);
829 gboolean last_entry = TRUE;
831 /* take the height of our More... entry into account */
832 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
834 /* start at the end of the entries */
835 flast = g_list_last(self->entries);
837 /* pull out all the entries from the frame that don't
838 fit on the screen, leaving at least 1 though */
839 while (h > a->height && g_list_previous(flast) != NULL) {
840 /* update the height, without this entry */
841 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
843 /* destroy the entry we're not displaying */
845 flast = g_list_previous(flast);
846 menu_entry_frame_free(tmp->data);
847 self->entries = g_list_delete_link(self->entries, tmp);
849 /* only the first one that we see is the last entry in the menu */
854 ObMenuEntry *more_entry;
855 ObMenuEntryFrame *more_frame;
856 /* make the More... menu entry frame which will display in this
858 if self->menu->more_menu is NULL that means that this is already
859 More... menu, so just use ourself.
861 more_entry = menu_get_more((self->menu->more_menu ?
862 self->menu->more_menu :
864 /* continue where we left off */
866 g_list_length(self->entries));
867 more_frame = menu_entry_frame_new(more_entry, self);
868 /* make it get deleted when the menu frame goes away */
869 menu_entry_unref(more_entry);
871 /* add our More... entry to the frame */
872 self->entries = g_list_append(self->entries, more_frame);
878 menu_frame_render(self);
881 static gboolean menu_frame_is_visible(ObMenuFrame *self)
883 return !!(g_list_find(menu_frame_visible, self));
886 static gboolean menu_frame_show(ObMenuFrame *self)
890 /* determine if the underlying menu is already visible */
891 for (it = menu_frame_visible; it; it = g_list_next(it)) {
892 ObMenuFrame *f = it->data;
893 if (f->menu == self->menu)
897 if (self->menu->update_func)
898 if (!self->menu->update_func(self, self->menu->data))
902 if (menu_frame_visible == NULL) {
903 /* no menus shown yet */
905 /* grab the pointer in such a way as to pass through "owner events"
906 so that we can get enter/leave notifies in the menu. */
907 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
909 if (!grab_keyboard()) {
915 menu_frame_update(self);
917 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
919 if (self->menu->show_func)
920 self->menu->show_func(self, self->menu->data);
925 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
931 if (menu_frame_is_visible(self))
933 if (!menu_frame_show(self))
936 /* find the monitor the menu is on */
937 for (i = 0; i < screen_num_monitors; ++i) {
938 Rect *a = screen_physical_area_monitor(i);
939 gboolean contains = RECT_CONTAINS(*a, x, y);
947 if (self->menu->place_func)
948 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
950 menu_frame_place_topmenu(self, &x, &y);
952 menu_frame_move(self, x, y);
954 XMapWindow(obt_display, self->window);
956 if (screen_pointer_pos(&px, &py)) {
957 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
958 if (e && e->frame == self)
965 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
966 ObMenuEntryFrame *parent_entry)
971 if (menu_frame_is_visible(self))
974 self->monitor = parent->monitor;
975 self->parent = parent;
976 self->parent_entry = parent_entry;
978 /* set up parent's child to be us */
980 menu_frame_hide(parent->child);
981 parent->child = self;
983 if (!menu_frame_show(self))
986 menu_frame_place_submenu(self, &x, &y);
987 menu_frame_move_on_screen(self, x, y, &dx, &dy);
990 /*try the other side */
991 self->direction_right = !self->direction_right;
992 menu_frame_place_submenu(self, &x, &y);
993 menu_frame_move_on_screen(self, x, y, &dx, &dy);
995 menu_frame_move(self, x + dx, y + dy);
997 XMapWindow(obt_display, self->window);
999 if (screen_pointer_pos(&px, &py)) {
1000 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1001 if (e && e->frame == self)
1008 static void menu_frame_hide(ObMenuFrame *self)
1010 GList *it = g_list_find(menu_frame_visible, self);
1015 if (self->menu->hide_func)
1016 self->menu->hide_func(self, self->menu->data);
1019 menu_frame_hide(self->child);
1022 self->parent->child = NULL;
1023 self->parent = NULL;
1024 self->parent_entry = NULL;
1026 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1028 if (menu_frame_visible == NULL) {
1029 /* last menu shown */
1034 XUnmapWindow(obt_display, self->window);
1036 menu_frame_free(self);
1039 void menu_frame_hide_all(void)
1043 if (config_submenu_show_delay) {
1044 /* remove any submenu open requests */
1045 obt_main_loop_timeout_remove(ob_main_loop,
1046 menu_entry_frame_submenu_timeout);
1048 if ((it = g_list_last(menu_frame_visible)))
1049 menu_frame_hide(it->data);
1052 void menu_frame_hide_all_client(ObClient *client)
1054 GList *it = g_list_last(menu_frame_visible);
1056 ObMenuFrame *f = it->data;
1057 if (f->client == client) {
1058 if (config_submenu_show_delay) {
1059 /* remove any submenu open requests */
1060 obt_main_loop_timeout_remove(ob_main_loop,
1061 menu_entry_frame_submenu_timeout);
1069 ObMenuFrame* menu_frame_under(gint x, gint y)
1071 ObMenuFrame *ret = NULL;
1074 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1075 ObMenuFrame *f = it->data;
1077 if (RECT_CONTAINS(f->area, x, y)) {
1085 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1088 ObMenuEntryFrame *ret = NULL;
1091 if ((frame = menu_frame_under(x, y))) {
1092 x -= ob_rr_theme->mbwidth + frame->area.x;
1093 y -= ob_rr_theme->mbwidth + frame->area.y;
1095 for (it = frame->entries; it; it = g_list_next(it)) {
1096 ObMenuEntryFrame *e = it->data;
1098 if (RECT_CONTAINS(e->area, x, y)) {
1107 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1109 g_assert(menu_frame_visible);
1110 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1114 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1117 ObMenuEntryFrame *old = self->selected;
1118 ObMenuFrame *oldchild = self->child;
1120 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1123 if (old == entry) return;
1125 if (config_submenu_show_delay) {
1126 /* remove any submenu open requests */
1127 obt_main_loop_timeout_remove(ob_main_loop,
1128 menu_entry_frame_submenu_timeout);
1131 self->selected = entry;
1134 menu_entry_frame_render(old);
1136 menu_frame_hide(oldchild);
1138 if (self->selected) {
1139 menu_entry_frame_render(self->selected);
1141 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1142 if (config_submenu_show_delay && !immediate) {
1143 /* initiate a new submenu open request */
1144 obt_main_loop_timeout_add(ob_main_loop,
1145 config_submenu_show_delay * 1000,
1146 menu_entry_frame_submenu_timeout,
1147 self->selected, g_direct_equal,
1150 menu_entry_frame_show_submenu(self->selected);
1156 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1160 if (!self->entry->data.submenu.submenu) return;
1162 f = menu_frame_new(self->entry->data.submenu.submenu,
1163 self->entry->data.submenu.show_from,
1164 self->frame->client);
1165 /* pass our direction on to our child */
1166 f->direction_right = self->frame->direction_right;
1168 menu_frame_show_submenu(f, self->frame, self);
1171 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1173 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1174 self->entry->data.normal.enabled)
1176 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1178 ObMenuEntry *entry = self->entry;
1179 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1180 gpointer data = self->frame->menu->data;
1181 GSList *acts = self->entry->data.normal.actions;
1182 ObClient *client = self->frame->client;
1183 ObMenuFrame *frame = self->frame;
1185 /* release grabs before executing the shit */
1186 if (!(state & ControlMask)) {
1187 menu_frame_hide_all();
1192 func(entry, frame, client, state, data);
1194 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1195 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1199 void menu_frame_select_previous(ObMenuFrame *self)
1201 GList *it = NULL, *start;
1203 if (self->entries) {
1204 start = it = g_list_find(self->entries, self->selected);
1206 ObMenuEntryFrame *e;
1208 it = it ? g_list_previous(it) : g_list_last(self->entries);
1214 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1216 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1221 menu_frame_select(self, it ? it->data : NULL, TRUE);
1224 void menu_frame_select_next(ObMenuFrame *self)
1226 GList *it = NULL, *start;
1228 if (self->entries) {
1229 start = it = g_list_find(self->entries, self->selected);
1231 ObMenuEntryFrame *e;
1233 it = it ? g_list_next(it) : self->entries;
1239 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1241 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1246 menu_frame_select(self, it ? it->data : NULL, TRUE);