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"
29 #include "render/theme.h"
32 #define SEPARATOR_HEIGHT 3
33 #define MAX_MENU_WIDTH 400
35 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
37 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
39 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
40 ButtonPressMask | ButtonReleaseMask)
42 GList *menu_frame_visible;
44 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
46 static void menu_entry_frame_free(ObMenuEntryFrame *self);
47 static void menu_frame_update(ObMenuFrame *self);
48 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
49 static void menu_frame_hide(ObMenuFrame *self);
51 static Window createWindow(Window parent, gulong mask,
52 XSetWindowAttributes *attrib)
54 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
55 RrDepth(ob_rr_inst), InputOutput,
56 RrVisual(ob_rr_inst), mask, attrib);
59 GHashTable *menu_frame_map;
61 void menu_frame_startup(gboolean reconfig)
65 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
68 void menu_frame_shutdown(gboolean reconfig)
72 g_hash_table_destroy(menu_frame_map);
75 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
78 XSetWindowAttributes attr;
80 self = g_new0(ObMenuFrame, 1);
81 self->type = Window_Menu;
83 self->selected = NULL;
84 self->client = client;
85 self->direction_right = TRUE;
86 self->show_from = show_from;
88 attr.event_mask = FRAME_EVENTMASK;
89 self->window = createWindow(RootWindow(ob_display, ob_screen),
92 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
93 XSetWindowBorder(ob_display, self->window,
94 RrColorPixel(ob_rr_theme->menu_border_color));
96 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
98 stacking_add(MENU_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(MENU_AS_WINDOW(self));
113 RrAppearanceFree(self->a_items);
115 XDestroyWindow(ob_display, self->window);
121 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
124 ObMenuEntryFrame *self;
125 XSetWindowAttributes attr;
127 self = g_new0(ObMenuEntryFrame, 1);
131 menu_entry_ref(entry);
133 attr.event_mask = ENTRY_EVENTMASK;
134 self->window = createWindow(self->frame->window, CWEventMask, &attr);
135 self->text = createWindow(self->window, 0, NULL);
136 g_hash_table_insert(menu_frame_map, &self->window, self);
137 g_hash_table_insert(menu_frame_map, &self->text, self);
138 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
139 self->icon = createWindow(self->window, 0, NULL);
140 g_hash_table_insert(menu_frame_map, &self->icon, self);
142 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
143 self->bullet = createWindow(self->window, 0, NULL);
144 g_hash_table_insert(menu_frame_map, &self->bullet, self);
147 XMapWindow(ob_display, self->window);
148 XMapWindow(ob_display, self->text);
153 static void menu_entry_frame_free(ObMenuEntryFrame *self)
156 menu_entry_unref(self->entry);
158 XDestroyWindow(ob_display, self->text);
159 XDestroyWindow(ob_display, self->window);
160 g_hash_table_remove(menu_frame_map, &self->text);
161 g_hash_table_remove(menu_frame_map, &self->window);
162 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
163 XDestroyWindow(ob_display, self->icon);
164 g_hash_table_remove(menu_frame_map, &self->icon);
166 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
167 XDestroyWindow(ob_display, self->bullet);
168 g_hash_table_remove(menu_frame_map, &self->bullet);
175 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
177 RECT_SET_POINT(self->area, x, y);
178 XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
181 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
185 if (config_menu_middle) {
189 *y -= self->area.height / 2;
191 /* try to the right of the cursor */
192 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
193 self->direction_right = TRUE;
195 /* try to the left of the cursor */
196 myx = *x - self->area.width;
197 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
198 self->direction_right = FALSE;
201 /* if didnt fit on either side so just use what it says */
203 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
204 self->direction_right = TRUE;
214 /* try to the bottom right of the cursor */
215 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
216 self->direction_right = TRUE;
217 if (dx != 0 || dy != 0) {
218 /* try to the bottom left of the cursor */
219 myx = *x - self->area.width;
221 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
222 self->direction_right = FALSE;
224 if (dx != 0 || dy != 0) {
225 /* try to the top right of the cursor */
227 myy = *y - self->area.height;
228 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
229 self->direction_right = TRUE;
231 if (dx != 0 || dy != 0) {
232 /* try to the top left of the cursor */
233 myx = *x - self->area.width;
234 myy = *y - self->area.height;
235 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
236 self->direction_right = FALSE;
238 if (dx != 0 || dy != 0) {
239 /* if didnt fit on either side so just use what it says */
242 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
243 self->direction_right = TRUE;
250 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
255 overlap = ob_rr_theme->menu_overlap;
256 bwidth = ob_rr_theme->mbwidth;
258 if (self->direction_right)
259 *x = self->parent->area.x + self->parent->area.width -
262 *x = self->parent->area.x - self->area.width + overlap + bwidth;
264 *y = self->parent->area.y + self->parent_entry->area.y;
265 if (config_menu_middle)
266 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
271 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
279 a = screen_physical_area_monitor(self->monitor);
281 half = g_list_length(self->entries) / 2;
282 pos = g_list_index(self->entries, self->selected);
284 /* if in the bottom half then check this stuff first, will keep the bottom
285 edge of the menu visible */
287 *dx = MAX(*dx, a->x - x);
288 *dy = MAX(*dy, a->y - y);
290 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
291 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
292 /* if in the top half then check this stuff last, will keep the top
293 edge of the menu visible */
295 *dx = MAX(*dx, a->x - x);
296 *dy = MAX(*dy, a->y - y);
302 static void menu_entry_frame_render(ObMenuEntryFrame *self)
304 RrAppearance *item_a, *text_a;
307 ObMenuFrame *frame = self->frame;
309 switch (self->entry->type) {
310 case OB_MENU_ENTRY_TYPE_NORMAL:
311 case OB_MENU_ENTRY_TYPE_SUBMENU:
312 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
313 !self->entry->data.normal.enabled ?
315 (self == self->frame->selected ?
316 ob_rr_theme->a_menu_disabled_selected :
317 ob_rr_theme->a_menu_disabled) :
319 (self == self->frame->selected ?
320 ob_rr_theme->a_menu_selected :
321 ob_rr_theme->a_menu_normal));
324 case OB_MENU_ENTRY_TYPE_SEPARATOR:
325 if (self->entry->data.separator.label) {
326 item_a = ob_rr_theme->a_menu_title;
327 th = ob_rr_theme->menu_title_height;
329 item_a = ob_rr_theme->a_menu_normal;
330 th = SEPARATOR_HEIGHT + 2*PADDING;
334 g_assert_not_reached();
336 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
337 XResizeWindow(ob_display, self->window,
338 self->area.width, self->area.height);
339 item_a->surface.parent = self->frame->a_items;
340 item_a->surface.parentx = self->area.x;
341 item_a->surface.parenty = self->area.y;
342 RrPaint(item_a, self->window, self->area.width, self->area.height);
344 switch (self->entry->type) {
345 case OB_MENU_ENTRY_TYPE_NORMAL:
346 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
347 !self->entry->data.normal.enabled ?
349 (self == self->frame->selected ?
350 ob_rr_theme->a_menu_text_disabled_selected :
351 ob_rr_theme->a_menu_text_disabled) :
353 (self == self->frame->selected ?
354 ob_rr_theme->a_menu_text_selected :
355 ob_rr_theme->a_menu_text_normal));
356 text_a->texture[0].data.text.string = self->entry->data.normal.label;
357 if (self->entry->data.normal.shortcut &&
358 (self->frame->menu->show_all_shortcuts ||
359 self->entry->data.normal.shortcut_always_show ||
360 self->entry->data.normal.shortcut_position > 0))
362 text_a->texture[0].data.text.shortcut = TRUE;
363 text_a->texture[0].data.text.shortcut_pos =
364 self->entry->data.normal.shortcut_position;
366 text_a->texture[0].data.text.shortcut = FALSE;
368 case OB_MENU_ENTRY_TYPE_SUBMENU:
369 text_a = (self == self->frame->selected ?
370 ob_rr_theme->a_menu_text_selected :
371 ob_rr_theme->a_menu_text_normal);
372 sub = self->entry->data.submenu.submenu;
373 text_a->texture[0].data.text.string = sub ? sub->title : "";
374 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
375 sub->shortcut_always_show ||
376 sub->shortcut_position > 0))
378 text_a->texture[0].data.text.shortcut = TRUE;
379 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
381 text_a->texture[0].data.text.shortcut = FALSE;
383 case OB_MENU_ENTRY_TYPE_SEPARATOR:
384 if (self->entry->data.separator.label != NULL) {
385 text_a = ob_rr_theme->a_menu_text_title;
386 text_a->texture[0].data.text.string =
387 self->entry->data.separator.label;
390 text_a = ob_rr_theme->a_menu_text_normal;
394 switch (self->entry->type) {
395 case OB_MENU_ENTRY_TYPE_NORMAL:
396 XMoveResizeWindow(ob_display, self->text,
397 self->frame->text_x, PADDING,
399 ITEM_HEIGHT - 2*PADDING);
400 text_a->surface.parent = item_a;
401 text_a->surface.parentx = self->frame->text_x;
402 text_a->surface.parenty = PADDING;
403 RrPaint(text_a, self->text, self->frame->text_w,
404 ITEM_HEIGHT - 2*PADDING);
406 case OB_MENU_ENTRY_TYPE_SUBMENU:
407 XMoveResizeWindow(ob_display, self->text,
408 self->frame->text_x, PADDING,
409 self->frame->text_w - ITEM_HEIGHT,
410 ITEM_HEIGHT - 2*PADDING);
411 text_a->surface.parent = item_a;
412 text_a->surface.parentx = self->frame->text_x;
413 text_a->surface.parenty = PADDING;
414 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
415 ITEM_HEIGHT - 2*PADDING);
417 case OB_MENU_ENTRY_TYPE_SEPARATOR:
418 if (self->entry->data.separator.label != NULL) {
419 /* labeled separator */
420 XMoveResizeWindow(ob_display, self->text,
421 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
422 self->area.width - 2*ob_rr_theme->paddingx,
423 ob_rr_theme->menu_title_height -
424 2*ob_rr_theme->paddingy);
425 text_a->surface.parent = item_a;
426 text_a->surface.parentx = ob_rr_theme->paddingx;
427 text_a->surface.parenty = ob_rr_theme->paddingy;
428 RrPaint(text_a, self->text,
429 self->area.width - 2*ob_rr_theme->paddingx,
430 ob_rr_theme->menu_title_height -
431 2*ob_rr_theme->paddingy);
435 /* unlabeled separaator */
436 XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
437 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
439 clear = ob_rr_theme->a_clear_tex;
440 clear->texture[0].type = RR_TEXTURE_LINE_ART;
441 clear->surface.parent = item_a;
442 clear->surface.parentx = PADDING;
443 clear->surface.parenty = PADDING;
444 clear->texture[0].data.lineart.color =
445 text_a->texture[0].data.text.color;
446 clear->texture[0].data.lineart.x1 = 2*PADDING;
447 clear->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
448 clear->texture[0].data.lineart.x2 = self->area.width - 4*PADDING;
449 clear->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
450 RrPaint(clear, self->text,
451 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
456 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
457 self->entry->data.normal.icon_data)
461 XMoveResizeWindow(ob_display, self->icon,
462 PADDING, frame->item_margin.top,
463 ITEM_HEIGHT - frame->item_margin.top
464 - frame->item_margin.bottom,
465 ITEM_HEIGHT - frame->item_margin.top
466 - frame->item_margin.bottom);
468 clear = ob_rr_theme->a_clear_tex;
469 clear->texture[0].type = RR_TEXTURE_RGBA;
470 clear->texture[0].data.rgba.width =
471 self->entry->data.normal.icon_width;
472 clear->texture[0].data.rgba.height =
473 self->entry->data.normal.icon_height;
474 clear->texture[0].data.rgba.alpha =
475 self->entry->data.normal.icon_alpha;
476 clear->texture[0].data.rgba.data =
477 self->entry->data.normal.icon_data;
478 clear->surface.parent = item_a;
479 clear->surface.parentx = PADDING;
480 clear->surface.parenty = frame->item_margin.top;
481 RrPaint(clear, self->icon,
482 ITEM_HEIGHT - frame->item_margin.top
483 - frame->item_margin.bottom,
484 ITEM_HEIGHT - frame->item_margin.top
485 - frame->item_margin.bottom);
486 XMapWindow(ob_display, self->icon);
487 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
488 self->entry->data.normal.mask)
493 XMoveResizeWindow(ob_display, self->icon,
494 PADDING, frame->item_margin.top,
495 ITEM_HEIGHT - frame->item_margin.top
496 - frame->item_margin.bottom,
497 ITEM_HEIGHT - frame->item_margin.top
498 - frame->item_margin.bottom);
500 clear = ob_rr_theme->a_clear_tex;
501 clear->texture[0].type = RR_TEXTURE_MASK;
502 clear->texture[0].data.mask.mask =
503 self->entry->data.normal.mask;
505 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
506 !self->entry->data.normal.enabled ?
508 (self == self->frame->selected ?
509 self->entry->data.normal.mask_disabled_selected_color :
510 self->entry->data.normal.mask_disabled_color) :
512 (self == self->frame->selected ?
513 self->entry->data.normal.mask_selected_color :
514 self->entry->data.normal.mask_normal_color));
515 clear->texture[0].data.mask.color = c;
517 clear->surface.parent = item_a;
518 clear->surface.parentx = PADDING;
519 clear->surface.parenty = frame->item_margin.top;
520 RrPaint(clear, self->icon,
521 ITEM_HEIGHT - frame->item_margin.top
522 - frame->item_margin.bottom,
523 ITEM_HEIGHT - frame->item_margin.top
524 - frame->item_margin.bottom);
525 XMapWindow(ob_display, self->icon);
527 XUnmapWindow(ob_display, self->icon);
529 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
530 RrAppearance *bullet_a;
531 XMoveResizeWindow(ob_display, self->bullet,
532 self->frame->text_x + self->frame->text_w -
533 ITEM_HEIGHT + PADDING, PADDING,
534 ITEM_HEIGHT - 2*PADDING,
535 ITEM_HEIGHT - 2*PADDING);
536 bullet_a = (self == self->frame->selected ?
537 ob_rr_theme->a_menu_bullet_selected :
538 ob_rr_theme->a_menu_bullet_normal);
539 bullet_a->surface.parent = item_a;
540 bullet_a->surface.parentx =
541 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
542 bullet_a->surface.parenty = PADDING;
543 RrPaint(bullet_a, self->bullet,
544 ITEM_HEIGHT - 2*PADDING,
545 ITEM_HEIGHT - 2*PADDING);
546 XMapWindow(ob_display, self->bullet);
548 XUnmapWindow(ob_display, self->bullet);
553 /*! this code is taken from the menu_frame_render. if that changes, this won't
555 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
556 gboolean first_entry,
565 t = self->entry->type;
567 /* this is the More... entry, it's NORMAL type */
568 t = OB_MENU_ENTRY_TYPE_NORMAL;
571 case OB_MENU_ENTRY_TYPE_NORMAL:
572 case OB_MENU_ENTRY_TYPE_SUBMENU:
573 h += ob_rr_theme->menu_font_height;
575 case OB_MENU_ENTRY_TYPE_SEPARATOR:
576 if (self->entry->data.separator.label != NULL) {
577 h += ob_rr_theme->menu_title_height +
578 (ob_rr_theme->mbwidth - PADDING) * 2;
580 /* if the first entry is a labeled separator, then make its border
581 overlap with the menu's outside border */
583 h -= ob_rr_theme->mbwidth;
584 /* if the last entry is a labeled separator, then make its border
585 overlap with the menu's outside border */
587 h -= ob_rr_theme->mbwidth;
589 h += SEPARATOR_HEIGHT;
597 void menu_frame_render(ObMenuFrame *self)
600 gint tw, th; /* temps */
602 gboolean has_icon = FALSE;
606 /* find text dimensions */
608 STRUT_SET(self->item_margin, 0, 0, 0, 0);
613 e = self->entries->data;
614 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
615 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
620 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
621 STRUT_SET(self->item_margin,
622 MAX(self->item_margin.left, l),
623 MAX(self->item_margin.top, t),
624 MAX(self->item_margin.right, r),
625 MAX(self->item_margin.bottom, b));
626 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
627 STRUT_SET(self->item_margin,
628 MAX(self->item_margin.left, l),
629 MAX(self->item_margin.top, t),
630 MAX(self->item_margin.right, r),
631 MAX(self->item_margin.bottom, b));
632 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
633 STRUT_SET(self->item_margin,
634 MAX(self->item_margin.left, l),
635 MAX(self->item_margin.top, t),
636 MAX(self->item_margin.right, r),
637 MAX(self->item_margin.bottom, b));
638 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
639 STRUT_SET(self->item_margin,
640 MAX(self->item_margin.left, l),
641 MAX(self->item_margin.top, t),
642 MAX(self->item_margin.right, r),
643 MAX(self->item_margin.bottom, b));
646 /* render the entries */
648 for (it = self->entries; it; it = g_list_next(it)) {
649 RrAppearance *text_a;
652 /* if the first entry is a labeled separator, then make its border
653 overlap with the menu's outside border */
654 if (it == self->entries &&
655 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
656 e->entry->data.separator.label)
658 h -= ob_rr_theme->mbwidth;
661 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
662 e->entry->data.separator.label)
664 e->border = ob_rr_theme->mbwidth;
667 RECT_SET_POINT(e->area, 0, h+e->border);
668 XMoveWindow(ob_display, e->window,
669 e->area.x-e->border, e->area.y-e->border);
670 XSetWindowBorderWidth(ob_display, e->window, e->border);
671 XSetWindowBorder(ob_display, e->window,
672 RrColorPixel(ob_rr_theme->menu_border_color));
675 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
676 !e->entry->data.normal.enabled ?
678 (e == self->selected ?
679 ob_rr_theme->a_menu_text_disabled_selected :
680 ob_rr_theme->a_menu_text_disabled) :
682 (e == self->selected ?
683 ob_rr_theme->a_menu_text_selected :
684 ob_rr_theme->a_menu_text_normal));
685 switch (e->entry->type) {
686 case OB_MENU_ENTRY_TYPE_NORMAL:
687 text_a->texture[0].data.text.string = e->entry->data.normal.label;
688 tw = RrMinWidth(text_a);
689 tw = MIN(tw, MAX_MENU_WIDTH);
690 th = ob_rr_theme->menu_font_height;
692 if (e->entry->data.normal.icon_data ||
693 e->entry->data.normal.mask)
696 case OB_MENU_ENTRY_TYPE_SUBMENU:
697 sub = e->entry->data.submenu.submenu;
698 text_a->texture[0].data.text.string = sub ? sub->title : "";
699 tw = RrMinWidth(text_a);
700 tw = MIN(tw, MAX_MENU_WIDTH);
701 th = ob_rr_theme->menu_font_height;
703 if (e->entry->data.normal.icon_data ||
704 e->entry->data.normal.mask)
707 tw += ITEM_HEIGHT - PADDING;
709 case OB_MENU_ENTRY_TYPE_SEPARATOR:
710 if (e->entry->data.separator.label != NULL) {
711 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
712 e->entry->data.separator.label;
713 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
714 2*ob_rr_theme->paddingx;
715 tw = MIN(tw, MAX_MENU_WIDTH);
716 th = ob_rr_theme->menu_title_height +
717 (ob_rr_theme->mbwidth - PADDING) *2;
720 th = SEPARATOR_HEIGHT;
730 /* if the last entry is a labeled separator, then make its border
731 overlap with the menu's outside border */
732 it = g_list_last(self->entries);
733 e = it ? it->data : NULL;
734 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
735 e->entry->data.separator.label)
737 h -= ob_rr_theme->mbwidth;
740 self->text_x = PADDING;
745 w += ITEM_HEIGHT + PADDING;
746 self->text_x += ITEM_HEIGHT + PADDING;
753 XResizeWindow(ob_display, self->window, w, h);
757 RrPaint(self->a_items, self->window, w, h);
759 for (it = self->entries; it; it = g_list_next(it))
760 menu_entry_frame_render(it->data);
762 w += ob_rr_theme->mbwidth * 2;
763 h += ob_rr_theme->mbwidth * 2;
765 RECT_SET_SIZE(self->area, w, h);
770 static void menu_frame_update(ObMenuFrame *self)
776 menu_pipe_execute(self->menu);
777 menu_find_submenus(self->menu);
779 self->selected = NULL;
781 /* start at show_from */
782 mit = g_list_nth(self->menu->entries, self->show_from);
784 /* go through the menu's and frame's entries and connect the frame entries
785 to the menu entries */
786 for (fit = self->entries; mit && fit;
787 mit = g_list_next(mit), fit = g_list_next(fit))
789 ObMenuEntryFrame *f = fit->data;
790 f->entry = mit->data;
793 /* if there are more menu entries than in the frame, add them */
795 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
796 self->entries = g_list_append(self->entries, e);
797 mit = g_list_next(mit);
800 /* if there are more frame entries than menu entries then get rid of
803 GList *n = g_list_next(fit);
804 menu_entry_frame_free(fit->data);
805 self->entries = g_list_delete_link(self->entries, fit);
809 /* * make the menu fit on the screen */
811 /* calculate the height of the menu */
813 for (fit = self->entries; fit; fit = g_list_next(fit))
814 h += menu_entry_frame_get_height(fit->data,
815 fit == self->entries,
816 g_list_next(fit) == NULL);
817 /* add the border at the top and bottom */
818 h += ob_rr_theme->mbwidth * 2;
820 a = screen_physical_area_monitor(self->monitor);
824 gboolean last_entry = TRUE;
826 /* take the height of our More... entry into account */
827 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
829 /* start at the end of the entries */
830 flast = g_list_last(self->entries);
832 /* pull out all the entries from the frame that don't
833 fit on the screen, leaving at least 1 though */
834 while (h > a->height && g_list_previous(flast) != NULL) {
835 /* update the height, without this entry */
836 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
838 /* destroy the entry we're not displaying */
840 flast = g_list_previous(flast);
841 menu_entry_frame_free(tmp->data);
842 self->entries = g_list_delete_link(self->entries, tmp);
844 /* only the first one that we see is the last entry in the menu */
849 ObMenuEntry *more_entry;
850 ObMenuEntryFrame *more_frame;
851 /* make the More... menu entry frame which will display in this
853 if self->menu->more_menu is NULL that means that this is already
854 More... menu, so just use ourself.
856 more_entry = menu_get_more((self->menu->more_menu ?
857 self->menu->more_menu :
859 /* continue where we left off */
861 g_list_length(self->entries));
862 more_frame = menu_entry_frame_new(more_entry, self);
863 /* make it get deleted when the menu frame goes away */
864 menu_entry_unref(more_entry);
866 /* add our More... entry to the frame */
867 self->entries = g_list_append(self->entries, more_frame);
873 menu_frame_render(self);
876 static gboolean menu_frame_is_visible(ObMenuFrame *self)
878 return !!(g_list_find(menu_frame_visible, self));
881 static gboolean menu_frame_show(ObMenuFrame *self)
885 /* determine if the underlying menu is already visible */
886 for (it = menu_frame_visible; it; it = g_list_next(it)) {
887 ObMenuFrame *f = it->data;
888 if (f->menu == self->menu)
892 if (self->menu->update_func)
893 if (!self->menu->update_func(self, self->menu->data))
897 if (menu_frame_visible == NULL) {
898 /* no menus shown yet */
900 /* grab the pointer in such a way as to pass through "owner events"
901 so that we can get enter/leave notifies in the menu. */
902 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
904 if (!grab_keyboard()) {
910 menu_frame_update(self);
912 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
914 if (self->menu->show_func)
915 self->menu->show_func(self, self->menu->data);
920 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
926 if (menu_frame_is_visible(self))
928 if (!menu_frame_show(self))
931 /* find the monitor the menu is on */
932 for (i = 0; i < screen_num_monitors; ++i) {
933 Rect *a = screen_physical_area_monitor(i);
934 gboolean contains = RECT_CONTAINS(*a, x, y);
942 if (self->menu->place_func)
943 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
945 menu_frame_place_topmenu(self, &x, &y);
947 menu_frame_move(self, x, y);
949 XMapWindow(ob_display, self->window);
951 if (screen_pointer_pos(&px, &py)) {
952 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
953 if (e && e->frame == self)
960 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
961 ObMenuEntryFrame *parent_entry)
966 if (menu_frame_is_visible(self))
969 self->monitor = parent->monitor;
970 self->parent = parent;
971 self->parent_entry = parent_entry;
973 /* set up parent's child to be us */
975 menu_frame_hide(parent->child);
976 parent->child = self;
978 if (!menu_frame_show(self))
981 menu_frame_place_submenu(self, &x, &y);
982 menu_frame_move_on_screen(self, x, y, &dx, &dy);
985 /*try the other side */
986 self->direction_right = !self->direction_right;
987 menu_frame_place_submenu(self, &x, &y);
988 menu_frame_move_on_screen(self, x, y, &dx, &dy);
990 menu_frame_move(self, x + dx, y + dy);
992 XMapWindow(ob_display, self->window);
994 if (screen_pointer_pos(&px, &py)) {
995 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
996 if (e && e->frame == self)
1003 static void menu_frame_hide(ObMenuFrame *self)
1005 GList *it = g_list_find(menu_frame_visible, self);
1010 if (self->menu->hide_func)
1011 self->menu->hide_func(self, self->menu->data);
1014 menu_frame_hide(self->child);
1017 self->parent->child = NULL;
1018 self->parent = NULL;
1019 self->parent_entry = NULL;
1021 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1023 if (menu_frame_visible == NULL) {
1024 /* last menu shown */
1029 XUnmapWindow(ob_display, self->window);
1031 menu_frame_free(self);
1034 void menu_frame_hide_all(void)
1038 if (config_submenu_show_delay) {
1039 /* remove any submenu open requests */
1040 ob_main_loop_timeout_remove(ob_main_loop,
1041 menu_entry_frame_submenu_timeout);
1043 if ((it = g_list_last(menu_frame_visible)))
1044 menu_frame_hide(it->data);
1047 void menu_frame_hide_all_client(ObClient *client)
1049 GList *it = g_list_last(menu_frame_visible);
1051 ObMenuFrame *f = it->data;
1052 if (f->client == client) {
1053 if (config_submenu_show_delay) {
1054 /* remove any submenu open requests */
1055 ob_main_loop_timeout_remove(ob_main_loop,
1056 menu_entry_frame_submenu_timeout);
1064 ObMenuFrame* menu_frame_under(gint x, gint y)
1066 ObMenuFrame *ret = NULL;
1069 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1070 ObMenuFrame *f = it->data;
1072 if (RECT_CONTAINS(f->area, x, y)) {
1080 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1083 ObMenuEntryFrame *ret = NULL;
1086 if ((frame = menu_frame_under(x, y))) {
1087 x -= ob_rr_theme->mbwidth + frame->area.x;
1088 y -= ob_rr_theme->mbwidth + frame->area.y;
1090 for (it = frame->entries; it; it = g_list_next(it)) {
1091 ObMenuEntryFrame *e = it->data;
1093 if (RECT_CONTAINS(e->area, x, y)) {
1102 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1104 g_assert(menu_frame_visible);
1105 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1109 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1112 ObMenuEntryFrame *old = self->selected;
1113 ObMenuFrame *oldchild = self->child;
1115 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1118 if (old == entry) return;
1120 if (config_submenu_show_delay) {
1121 /* remove any submenu open requests */
1122 ob_main_loop_timeout_remove(ob_main_loop,
1123 menu_entry_frame_submenu_timeout);
1126 self->selected = entry;
1129 menu_entry_frame_render(old);
1131 menu_frame_hide(oldchild);
1133 if (self->selected) {
1134 menu_entry_frame_render(self->selected);
1136 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1137 if (config_submenu_show_delay && !immediate) {
1138 /* initiate a new submenu open request */
1139 ob_main_loop_timeout_add(ob_main_loop,
1140 config_submenu_show_delay * 1000,
1141 menu_entry_frame_submenu_timeout,
1142 self->selected, g_direct_equal,
1145 menu_entry_frame_show_submenu(self->selected);
1151 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1155 if (!self->entry->data.submenu.submenu) return;
1157 f = menu_frame_new(self->entry->data.submenu.submenu,
1158 self->entry->data.submenu.show_from,
1159 self->frame->client);
1160 /* pass our direction on to our child */
1161 f->direction_right = self->frame->direction_right;
1163 menu_frame_show_submenu(f, self->frame, self);
1166 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1168 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1169 self->entry->data.normal.enabled)
1171 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1173 ObMenuEntry *entry = self->entry;
1174 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1175 gpointer data = self->frame->menu->data;
1176 GSList *acts = self->entry->data.normal.actions;
1177 ObClient *client = self->frame->client;
1178 ObMenuFrame *frame = self->frame;
1180 /* release grabs before executing the shit */
1181 if (!(state & ControlMask)) {
1182 menu_frame_hide_all();
1187 func(entry, frame, client, state, data);
1189 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1190 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1194 void menu_frame_select_previous(ObMenuFrame *self)
1196 GList *it = NULL, *start;
1198 if (self->entries) {
1199 start = it = g_list_find(self->entries, self->selected);
1201 ObMenuEntryFrame *e;
1203 it = it ? g_list_previous(it) : g_list_last(self->entries);
1209 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1211 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1216 menu_frame_select(self, it ? it->data : NULL, TRUE);
1219 void menu_frame_select_next(ObMenuFrame *self)
1221 GList *it = NULL, *start;
1223 if (self->entries) {
1224 start = it = g_list_find(self->entries, self->selected);
1226 ObMenuEntryFrame *e;
1228 it = it ? g_list_next(it) : self->entries;
1234 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1236 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1241 menu_frame_select(self, it ? it->data : NULL, TRUE);