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();
341 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
342 XResizeWindow(obt_display, self->window,
343 self->area.width, self->area.height);
344 item_a->surface.parent = self->frame->a_items;
345 item_a->surface.parentx = self->area.x;
346 item_a->surface.parenty = self->area.y;
347 RrPaint(item_a, self->window, self->area.width, self->area.height);
349 switch (self->entry->type) {
350 case OB_MENU_ENTRY_TYPE_NORMAL:
351 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
352 !self->entry->data.normal.enabled ?
354 (self == self->frame->selected ?
355 ob_rr_theme->a_menu_text_disabled_selected :
356 ob_rr_theme->a_menu_text_disabled) :
358 (self == self->frame->selected ?
359 ob_rr_theme->a_menu_text_selected :
360 ob_rr_theme->a_menu_text_normal));
361 text_a->texture[0].data.text.string = self->entry->data.normal.label;
362 if (self->entry->data.normal.shortcut &&
363 (self->frame->menu->show_all_shortcuts ||
364 self->entry->data.normal.shortcut_always_show ||
365 self->entry->data.normal.shortcut_position > 0))
367 text_a->texture[0].data.text.shortcut = TRUE;
368 text_a->texture[0].data.text.shortcut_pos =
369 self->entry->data.normal.shortcut_position;
371 text_a->texture[0].data.text.shortcut = FALSE;
373 case OB_MENU_ENTRY_TYPE_SUBMENU:
374 text_a = (self == self->frame->selected ?
375 ob_rr_theme->a_menu_text_selected :
376 ob_rr_theme->a_menu_text_normal);
377 sub = self->entry->data.submenu.submenu;
378 text_a->texture[0].data.text.string = sub ? sub->title : "";
379 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
380 sub->shortcut_always_show ||
381 sub->shortcut_position > 0))
383 text_a->texture[0].data.text.shortcut = TRUE;
384 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
386 text_a->texture[0].data.text.shortcut = FALSE;
388 case OB_MENU_ENTRY_TYPE_SEPARATOR:
389 if (self->entry->data.separator.label != NULL)
390 text_a = ob_rr_theme->a_menu_text_title;
392 text_a = ob_rr_theme->a_menu_text_normal;
396 switch (self->entry->type) {
397 case OB_MENU_ENTRY_TYPE_NORMAL:
398 XMoveResizeWindow(obt_display, self->text,
399 self->frame->text_x, PADDING,
401 ITEM_HEIGHT - 2*PADDING);
402 text_a->surface.parent = item_a;
403 text_a->surface.parentx = self->frame->text_x;
404 text_a->surface.parenty = PADDING;
405 RrPaint(text_a, self->text, self->frame->text_w,
406 ITEM_HEIGHT - 2*PADDING);
408 case OB_MENU_ENTRY_TYPE_SUBMENU:
409 XMoveResizeWindow(obt_display, self->text,
410 self->frame->text_x, PADDING,
411 self->frame->text_w - ITEM_HEIGHT,
412 ITEM_HEIGHT - 2*PADDING);
413 text_a->surface.parent = item_a;
414 text_a->surface.parentx = self->frame->text_x;
415 text_a->surface.parenty = PADDING;
416 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
417 ITEM_HEIGHT - 2*PADDING);
419 case OB_MENU_ENTRY_TYPE_SEPARATOR:
420 if (self->entry->data.separator.label != NULL) {
421 /* labeled separator */
422 XMoveResizeWindow(obt_display, self->text,
423 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
424 self->area.width - 2*ob_rr_theme->paddingx,
425 ob_rr_theme->menu_title_height -
426 2*ob_rr_theme->paddingy);
427 text_a->surface.parent = item_a;
428 text_a->surface.parentx = ob_rr_theme->paddingx;
429 text_a->surface.parenty = ob_rr_theme->paddingy;
430 RrPaint(text_a, self->text,
431 self->area.width - 2*ob_rr_theme->paddingx,
432 ob_rr_theme->menu_title_height -
433 2*ob_rr_theme->paddingy);
437 /* unlabeled separaator */
438 XMoveResizeWindow(obt_display, self->text, PADDING, PADDING,
439 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
441 clear = ob_rr_theme->a_clear_tex;
442 clear->texture[0].type = RR_TEXTURE_LINE_ART;
443 clear->surface.parent = item_a;
444 clear->surface.parentx = PADDING;
445 clear->surface.parenty = PADDING;
446 clear->texture[0].data.lineart.color =
447 text_a->texture[0].data.text.color;
448 clear->texture[0].data.lineart.x1 = 2*PADDING;
449 clear->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
450 clear->texture[0].data.lineart.x2 = self->area.width - 4*PADDING;
451 clear->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
452 RrPaint(clear, self->text,
453 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
458 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
459 self->entry->data.normal.icon_data)
463 XMoveResizeWindow(obt_display, self->icon,
464 PADDING, frame->item_margin.top,
465 ITEM_HEIGHT - frame->item_margin.top
466 - frame->item_margin.bottom,
467 ITEM_HEIGHT - frame->item_margin.top
468 - frame->item_margin.bottom);
470 clear = ob_rr_theme->a_clear_tex;
471 clear->texture[0].type = RR_TEXTURE_RGBA;
472 clear->texture[0].data.rgba.width =
473 self->entry->data.normal.icon_width;
474 clear->texture[0].data.rgba.height =
475 self->entry->data.normal.icon_height;
476 clear->texture[0].data.rgba.alpha =
477 self->entry->data.normal.icon_alpha;
478 clear->texture[0].data.rgba.data =
479 self->entry->data.normal.icon_data;
480 clear->surface.parent = item_a;
481 clear->surface.parentx = PADDING;
482 clear->surface.parenty = frame->item_margin.top;
483 RrPaint(clear, self->icon,
484 ITEM_HEIGHT - frame->item_margin.top
485 - frame->item_margin.bottom,
486 ITEM_HEIGHT - frame->item_margin.top
487 - frame->item_margin.bottom);
488 XMapWindow(obt_display, self->icon);
489 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
490 self->entry->data.normal.mask)
495 XMoveResizeWindow(obt_display, self->icon,
496 PADDING, frame->item_margin.top,
497 ITEM_HEIGHT - frame->item_margin.top
498 - frame->item_margin.bottom,
499 ITEM_HEIGHT - frame->item_margin.top
500 - frame->item_margin.bottom);
502 clear = ob_rr_theme->a_clear_tex;
503 clear->texture[0].type = RR_TEXTURE_MASK;
504 clear->texture[0].data.mask.mask =
505 self->entry->data.normal.mask;
507 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
508 !self->entry->data.normal.enabled ?
510 (self == self->frame->selected ?
511 self->entry->data.normal.mask_disabled_selected_color :
512 self->entry->data.normal.mask_disabled_color) :
514 (self == self->frame->selected ?
515 self->entry->data.normal.mask_selected_color :
516 self->entry->data.normal.mask_normal_color));
517 clear->texture[0].data.mask.color = c;
519 clear->surface.parent = item_a;
520 clear->surface.parentx = PADDING;
521 clear->surface.parenty = frame->item_margin.top;
522 RrPaint(clear, self->icon,
523 ITEM_HEIGHT - frame->item_margin.top
524 - frame->item_margin.bottom,
525 ITEM_HEIGHT - frame->item_margin.top
526 - frame->item_margin.bottom);
527 XMapWindow(obt_display, self->icon);
529 XUnmapWindow(obt_display, self->icon);
531 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
532 RrAppearance *bullet_a;
533 XMoveResizeWindow(obt_display, self->bullet,
534 self->frame->text_x + self->frame->text_w -
535 ITEM_HEIGHT + PADDING, PADDING,
536 ITEM_HEIGHT - 2*PADDING,
537 ITEM_HEIGHT - 2*PADDING);
538 bullet_a = (self == self->frame->selected ?
539 ob_rr_theme->a_menu_bullet_selected :
540 ob_rr_theme->a_menu_bullet_normal);
541 bullet_a->surface.parent = item_a;
542 bullet_a->surface.parentx =
543 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
544 bullet_a->surface.parenty = PADDING;
545 RrPaint(bullet_a, self->bullet,
546 ITEM_HEIGHT - 2*PADDING,
547 ITEM_HEIGHT - 2*PADDING);
548 XMapWindow(obt_display, self->bullet);
550 XUnmapWindow(obt_display, self->bullet);
555 /*! this code is taken from the menu_frame_render. if that changes, this won't
557 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
558 gboolean first_entry,
567 t = self->entry->type;
569 /* this is the More... entry, it's NORMAL type */
570 t = OB_MENU_ENTRY_TYPE_NORMAL;
573 case OB_MENU_ENTRY_TYPE_NORMAL:
574 case OB_MENU_ENTRY_TYPE_SUBMENU:
575 h += ob_rr_theme->menu_font_height;
577 case OB_MENU_ENTRY_TYPE_SEPARATOR:
578 if (self->entry->data.separator.label != NULL) {
579 h += ob_rr_theme->menu_title_height +
580 (ob_rr_theme->mbwidth - PADDING) * 2;
582 /* if the first entry is a labeled separator, then make its border
583 overlap with the menu's outside border */
585 h -= ob_rr_theme->mbwidth;
586 /* if the last entry is a labeled separator, then make its border
587 overlap with the menu's outside border */
589 h -= ob_rr_theme->mbwidth;
591 h += SEPARATOR_HEIGHT;
599 void menu_frame_render(ObMenuFrame *self)
602 gint tw, th; /* temps */
604 gboolean has_icon = FALSE;
608 /* find text dimensions */
610 STRUT_SET(self->item_margin, 0, 0, 0, 0);
615 e = self->entries->data;
616 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
617 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
622 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
623 STRUT_SET(self->item_margin,
624 MAX(self->item_margin.left, l),
625 MAX(self->item_margin.top, t),
626 MAX(self->item_margin.right, r),
627 MAX(self->item_margin.bottom, b));
628 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
629 STRUT_SET(self->item_margin,
630 MAX(self->item_margin.left, l),
631 MAX(self->item_margin.top, t),
632 MAX(self->item_margin.right, r),
633 MAX(self->item_margin.bottom, b));
634 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
635 STRUT_SET(self->item_margin,
636 MAX(self->item_margin.left, l),
637 MAX(self->item_margin.top, t),
638 MAX(self->item_margin.right, r),
639 MAX(self->item_margin.bottom, b));
640 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
641 STRUT_SET(self->item_margin,
642 MAX(self->item_margin.left, l),
643 MAX(self->item_margin.top, t),
644 MAX(self->item_margin.right, r),
645 MAX(self->item_margin.bottom, b));
648 /* render the entries */
650 for (it = self->entries; it; it = g_list_next(it)) {
651 RrAppearance *text_a;
654 /* if the first entry is a labeled separator, then make its border
655 overlap with the menu's outside border */
656 if (it == self->entries &&
657 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
658 e->entry->data.separator.label)
660 h -= ob_rr_theme->mbwidth;
663 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
664 e->entry->data.separator.label)
666 e->border = ob_rr_theme->mbwidth;
669 RECT_SET_POINT(e->area, 0, h+e->border);
670 XMoveWindow(obt_display, e->window,
671 e->area.x-e->border, e->area.y-e->border);
672 XSetWindowBorderWidth(obt_display, e->window, e->border);
673 XSetWindowBorder(obt_display, e->window,
674 RrColorPixel(ob_rr_theme->menu_border_color));
677 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
678 !e->entry->data.normal.enabled ?
680 (e == self->selected ?
681 ob_rr_theme->a_menu_text_disabled_selected :
682 ob_rr_theme->a_menu_text_disabled) :
684 (e == self->selected ?
685 ob_rr_theme->a_menu_text_selected :
686 ob_rr_theme->a_menu_text_normal));
687 switch (e->entry->type) {
688 case OB_MENU_ENTRY_TYPE_NORMAL:
689 text_a->texture[0].data.text.string = e->entry->data.normal.label;
690 tw = RrMinWidth(text_a);
691 tw = MIN(tw, MAX_MENU_WIDTH);
692 th = ob_rr_theme->menu_font_height;
694 if (e->entry->data.normal.icon_data ||
695 e->entry->data.normal.mask)
698 case OB_MENU_ENTRY_TYPE_SUBMENU:
699 sub = e->entry->data.submenu.submenu;
700 text_a->texture[0].data.text.string = sub ? sub->title : "";
701 tw = RrMinWidth(text_a);
702 tw = MIN(tw, MAX_MENU_WIDTH);
703 th = ob_rr_theme->menu_font_height;
705 if (e->entry->data.normal.icon_data ||
706 e->entry->data.normal.mask)
709 tw += ITEM_HEIGHT - PADDING;
711 case OB_MENU_ENTRY_TYPE_SEPARATOR:
712 if (e->entry->data.separator.label != NULL) {
713 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
714 e->entry->data.separator.label;
715 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
716 2*ob_rr_theme->paddingx;
717 tw = MIN(tw, MAX_MENU_WIDTH);
718 th = ob_rr_theme->menu_title_height +
719 (ob_rr_theme->mbwidth - PADDING) *2;
722 th = SEPARATOR_HEIGHT;
732 /* if the last entry is a labeled separator, then make its border
733 overlap with the menu's outside border */
734 it = g_list_last(self->entries);
735 e = it ? it->data : NULL;
736 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
737 e->entry->data.separator.label)
739 h -= ob_rr_theme->mbwidth;
742 self->text_x = PADDING;
747 w += ITEM_HEIGHT + PADDING;
748 self->text_x += ITEM_HEIGHT + PADDING;
755 XResizeWindow(obt_display, self->window, w, h);
759 RrPaint(self->a_items, self->window, w, h);
761 for (it = self->entries; it; it = g_list_next(it))
762 menu_entry_frame_render(it->data);
764 w += ob_rr_theme->mbwidth * 2;
765 h += ob_rr_theme->mbwidth * 2;
767 RECT_SET_SIZE(self->area, w, h);
772 static void menu_frame_update(ObMenuFrame *self)
778 menu_pipe_execute(self->menu);
779 menu_find_submenus(self->menu);
781 self->selected = NULL;
783 /* start at show_from */
784 mit = g_list_nth(self->menu->entries, self->show_from);
786 /* go through the menu's and frame's entries and connect the frame entries
787 to the menu entries */
788 for (fit = self->entries; mit && fit;
789 mit = g_list_next(mit), fit = g_list_next(fit))
791 ObMenuEntryFrame *f = fit->data;
792 f->entry = mit->data;
795 /* if there are more menu entries than in the frame, add them */
797 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
798 self->entries = g_list_append(self->entries, e);
799 mit = g_list_next(mit);
802 /* if there are more frame entries than menu entries then get rid of
805 GList *n = g_list_next(fit);
806 menu_entry_frame_free(fit->data);
807 self->entries = g_list_delete_link(self->entries, fit);
811 /* * make the menu fit on the screen */
813 /* calculate the height of the menu */
815 for (fit = self->entries; fit; fit = g_list_next(fit))
816 h += menu_entry_frame_get_height(fit->data,
817 fit == self->entries,
818 g_list_next(fit) == NULL);
819 /* add the border at the top and bottom */
820 h += ob_rr_theme->mbwidth * 2;
822 a = screen_physical_area_monitor(self->monitor);
826 gboolean last_entry = TRUE;
828 /* take the height of our More... entry into account */
829 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
831 /* start at the end of the entries */
832 flast = g_list_last(self->entries);
834 /* pull out all the entries from the frame that don't
835 fit on the screen, leaving at least 1 though */
836 while (h > a->height && g_list_previous(flast) != NULL) {
837 /* update the height, without this entry */
838 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
840 /* destroy the entry we're not displaying */
842 flast = g_list_previous(flast);
843 menu_entry_frame_free(tmp->data);
844 self->entries = g_list_delete_link(self->entries, tmp);
846 /* only the first one that we see is the last entry in the menu */
851 ObMenuEntry *more_entry;
852 ObMenuEntryFrame *more_frame;
853 /* make the More... menu entry frame which will display in this
855 if self->menu->more_menu is NULL that means that this is already
856 More... menu, so just use ourself.
858 more_entry = menu_get_more((self->menu->more_menu ?
859 self->menu->more_menu :
861 /* continue where we left off */
863 g_list_length(self->entries));
864 more_frame = menu_entry_frame_new(more_entry, self);
865 /* make it get deleted when the menu frame goes away */
866 menu_entry_unref(more_entry);
868 /* add our More... entry to the frame */
869 self->entries = g_list_append(self->entries, more_frame);
875 menu_frame_render(self);
878 static gboolean menu_frame_is_visible(ObMenuFrame *self)
880 return !!(g_list_find(menu_frame_visible, self));
883 static gboolean menu_frame_show(ObMenuFrame *self)
887 /* determine if the underlying menu is already visible */
888 for (it = menu_frame_visible; it; it = g_list_next(it)) {
889 ObMenuFrame *f = it->data;
890 if (f->menu == self->menu)
894 if (self->menu->update_func)
895 if (!self->menu->update_func(self, self->menu->data))
899 if (menu_frame_visible == NULL) {
900 /* no menus shown yet */
902 /* grab the pointer in such a way as to pass through "owner events"
903 so that we can get enter/leave notifies in the menu. */
904 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
906 if (!grab_keyboard()) {
912 menu_frame_update(self);
914 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
916 if (self->menu->show_func)
917 self->menu->show_func(self, self->menu->data);
922 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
928 if (menu_frame_is_visible(self))
930 if (!menu_frame_show(self))
933 /* find the monitor the menu is on */
934 for (i = 0; i < screen_num_monitors; ++i) {
935 Rect *a = screen_physical_area_monitor(i);
936 gboolean contains = RECT_CONTAINS(*a, x, y);
944 if (self->menu->place_func)
945 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
947 menu_frame_place_topmenu(self, &x, &y);
949 menu_frame_move(self, x, y);
951 XMapWindow(obt_display, self->window);
953 if (screen_pointer_pos(&px, &py)) {
954 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
955 if (e && e->frame == self)
962 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
963 ObMenuEntryFrame *parent_entry)
968 if (menu_frame_is_visible(self))
971 self->monitor = parent->monitor;
972 self->parent = parent;
973 self->parent_entry = parent_entry;
975 /* set up parent's child to be us */
977 menu_frame_hide(parent->child);
978 parent->child = self;
980 if (!menu_frame_show(self))
983 menu_frame_place_submenu(self, &x, &y);
984 menu_frame_move_on_screen(self, x, y, &dx, &dy);
987 /*try the other side */
988 self->direction_right = !self->direction_right;
989 menu_frame_place_submenu(self, &x, &y);
990 menu_frame_move_on_screen(self, x, y, &dx, &dy);
992 menu_frame_move(self, x + dx, y + dy);
994 XMapWindow(obt_display, self->window);
996 if (screen_pointer_pos(&px, &py)) {
997 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
998 if (e && e->frame == self)
1005 static void menu_frame_hide(ObMenuFrame *self)
1007 GList *it = g_list_find(menu_frame_visible, self);
1012 if (self->menu->hide_func)
1013 self->menu->hide_func(self, self->menu->data);
1016 menu_frame_hide(self->child);
1019 self->parent->child = NULL;
1020 self->parent = NULL;
1021 self->parent_entry = NULL;
1023 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1025 if (menu_frame_visible == NULL) {
1026 /* last menu shown */
1031 XUnmapWindow(obt_display, self->window);
1033 menu_frame_free(self);
1036 void menu_frame_hide_all(void)
1040 if (config_submenu_show_delay) {
1041 /* remove any submenu open requests */
1042 obt_main_loop_timeout_remove(ob_main_loop,
1043 menu_entry_frame_submenu_timeout);
1045 if ((it = g_list_last(menu_frame_visible)))
1046 menu_frame_hide(it->data);
1049 void menu_frame_hide_all_client(ObClient *client)
1051 GList *it = g_list_last(menu_frame_visible);
1053 ObMenuFrame *f = it->data;
1054 if (f->client == client) {
1055 if (config_submenu_show_delay) {
1056 /* remove any submenu open requests */
1057 obt_main_loop_timeout_remove(ob_main_loop,
1058 menu_entry_frame_submenu_timeout);
1066 ObMenuFrame* menu_frame_under(gint x, gint y)
1068 ObMenuFrame *ret = NULL;
1071 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1072 ObMenuFrame *f = it->data;
1074 if (RECT_CONTAINS(f->area, x, y)) {
1082 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1085 ObMenuEntryFrame *ret = NULL;
1088 if ((frame = menu_frame_under(x, y))) {
1089 x -= ob_rr_theme->mbwidth + frame->area.x;
1090 y -= ob_rr_theme->mbwidth + frame->area.y;
1092 for (it = frame->entries; it; it = g_list_next(it)) {
1093 ObMenuEntryFrame *e = it->data;
1095 if (RECT_CONTAINS(e->area, x, y)) {
1104 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1106 g_assert(menu_frame_visible);
1107 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1111 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1114 ObMenuEntryFrame *old = self->selected;
1115 ObMenuFrame *oldchild = self->child;
1117 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1120 if (old == entry) return;
1122 if (config_submenu_show_delay) {
1123 /* remove any submenu open requests */
1124 obt_main_loop_timeout_remove(ob_main_loop,
1125 menu_entry_frame_submenu_timeout);
1128 self->selected = entry;
1131 menu_entry_frame_render(old);
1133 menu_frame_hide(oldchild);
1135 if (self->selected) {
1136 menu_entry_frame_render(self->selected);
1138 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1139 if (config_submenu_show_delay && !immediate) {
1140 /* initiate a new submenu open request */
1141 obt_main_loop_timeout_add(ob_main_loop,
1142 config_submenu_show_delay * 1000,
1143 menu_entry_frame_submenu_timeout,
1144 self->selected, g_direct_equal,
1147 menu_entry_frame_show_submenu(self->selected);
1153 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1157 if (!self->entry->data.submenu.submenu) return;
1159 f = menu_frame_new(self->entry->data.submenu.submenu,
1160 self->entry->data.submenu.show_from,
1161 self->frame->client);
1162 /* pass our direction on to our child */
1163 f->direction_right = self->frame->direction_right;
1165 menu_frame_show_submenu(f, self->frame, self);
1168 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1170 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1171 self->entry->data.normal.enabled)
1173 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1175 ObMenuEntry *entry = self->entry;
1176 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1177 gpointer data = self->frame->menu->data;
1178 GSList *acts = self->entry->data.normal.actions;
1179 ObClient *client = self->frame->client;
1180 ObMenuFrame *frame = self->frame;
1182 /* release grabs before executing the shit */
1183 if (!(state & ControlMask)) {
1184 menu_frame_hide_all();
1189 func(entry, frame, client, state, data);
1191 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1192 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1196 void menu_frame_select_previous(ObMenuFrame *self)
1198 GList *it = NULL, *start;
1200 if (self->entries) {
1201 start = it = g_list_find(self->entries, self->selected);
1203 ObMenuEntryFrame *e;
1205 it = it ? g_list_previous(it) : g_list_last(self->entries);
1211 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1213 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1218 menu_frame_select(self, it ? it->data : NULL, TRUE);
1221 void menu_frame_select_next(ObMenuFrame *self)
1223 GList *it = NULL, *start;
1225 if (self->entries) {
1226 start = it = g_list_find(self->entries, self->selected);
1228 ObMenuEntryFrame *e;
1230 it = it ? g_list_next(it) : self->entries;
1236 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1238 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1243 menu_frame_select(self, it ? it->data : NULL, TRUE);