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;
387 text_a = ob_rr_theme->a_menu_text_normal;
391 switch (self->entry->type) {
392 case OB_MENU_ENTRY_TYPE_NORMAL:
393 XMoveResizeWindow(ob_display, self->text,
394 self->frame->text_x, PADDING,
396 ITEM_HEIGHT - 2*PADDING);
397 text_a->surface.parent = item_a;
398 text_a->surface.parentx = self->frame->text_x;
399 text_a->surface.parenty = PADDING;
400 RrPaint(text_a, self->text, self->frame->text_w,
401 ITEM_HEIGHT - 2*PADDING);
403 case OB_MENU_ENTRY_TYPE_SUBMENU:
404 XMoveResizeWindow(ob_display, self->text,
405 self->frame->text_x, PADDING,
406 self->frame->text_w - ITEM_HEIGHT,
407 ITEM_HEIGHT - 2*PADDING);
408 text_a->surface.parent = item_a;
409 text_a->surface.parentx = self->frame->text_x;
410 text_a->surface.parenty = PADDING;
411 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
412 ITEM_HEIGHT - 2*PADDING);
414 case OB_MENU_ENTRY_TYPE_SEPARATOR:
415 if (self->entry->data.separator.label != NULL) {
416 /* labeled separator */
417 XMoveResizeWindow(ob_display, self->text,
418 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
419 self->area.width - 2*ob_rr_theme->paddingx,
420 ob_rr_theme->menu_title_height -
421 2*ob_rr_theme->paddingy);
422 text_a->surface.parent = item_a;
423 text_a->surface.parentx = ob_rr_theme->paddingx;
424 text_a->surface.parenty = ob_rr_theme->paddingy;
425 RrPaint(text_a, self->text,
426 self->area.width - 2*ob_rr_theme->paddingx,
427 ob_rr_theme->menu_title_height -
428 2*ob_rr_theme->paddingy);
432 /* unlabeled separaator */
433 XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
434 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
436 clear = ob_rr_theme->a_clear_tex;
437 clear->texture[0].type = RR_TEXTURE_LINE_ART;
438 clear->surface.parent = item_a;
439 clear->surface.parentx = PADDING;
440 clear->surface.parenty = PADDING;
441 clear->texture[0].data.lineart.color =
442 text_a->texture[0].data.text.color;
443 clear->texture[0].data.lineart.x1 = 2*PADDING;
444 clear->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
445 clear->texture[0].data.lineart.x2 = self->area.width - 4*PADDING;
446 clear->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
447 RrPaint(clear, self->text,
448 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
453 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
454 self->entry->data.normal.icon_data)
458 XMoveResizeWindow(ob_display, self->icon,
459 PADDING, frame->item_margin.top,
460 ITEM_HEIGHT - frame->item_margin.top
461 - frame->item_margin.bottom,
462 ITEM_HEIGHT - frame->item_margin.top
463 - frame->item_margin.bottom);
465 clear = ob_rr_theme->a_clear_tex;
466 clear->texture[0].type = RR_TEXTURE_RGBA;
467 clear->texture[0].data.rgba.width =
468 self->entry->data.normal.icon_width;
469 clear->texture[0].data.rgba.height =
470 self->entry->data.normal.icon_height;
471 clear->texture[0].data.rgba.alpha =
472 self->entry->data.normal.icon_alpha;
473 clear->texture[0].data.rgba.data =
474 self->entry->data.normal.icon_data;
475 clear->surface.parent = item_a;
476 clear->surface.parentx = PADDING;
477 clear->surface.parenty = frame->item_margin.top;
478 RrPaint(clear, self->icon,
479 ITEM_HEIGHT - frame->item_margin.top
480 - frame->item_margin.bottom,
481 ITEM_HEIGHT - frame->item_margin.top
482 - frame->item_margin.bottom);
483 XMapWindow(ob_display, self->icon);
484 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
485 self->entry->data.normal.mask)
490 XMoveResizeWindow(ob_display, self->icon,
491 PADDING, frame->item_margin.top,
492 ITEM_HEIGHT - frame->item_margin.top
493 - frame->item_margin.bottom,
494 ITEM_HEIGHT - frame->item_margin.top
495 - frame->item_margin.bottom);
497 clear = ob_rr_theme->a_clear_tex;
498 clear->texture[0].type = RR_TEXTURE_MASK;
499 clear->texture[0].data.mask.mask =
500 self->entry->data.normal.mask;
502 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
503 !self->entry->data.normal.enabled ?
505 (self == self->frame->selected ?
506 self->entry->data.normal.mask_disabled_selected_color :
507 self->entry->data.normal.mask_disabled_color) :
509 (self == self->frame->selected ?
510 self->entry->data.normal.mask_selected_color :
511 self->entry->data.normal.mask_normal_color));
512 clear->texture[0].data.mask.color = c;
514 clear->surface.parent = item_a;
515 clear->surface.parentx = PADDING;
516 clear->surface.parenty = frame->item_margin.top;
517 RrPaint(clear, self->icon,
518 ITEM_HEIGHT - frame->item_margin.top
519 - frame->item_margin.bottom,
520 ITEM_HEIGHT - frame->item_margin.top
521 - frame->item_margin.bottom);
522 XMapWindow(ob_display, self->icon);
524 XUnmapWindow(ob_display, self->icon);
526 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
527 RrAppearance *bullet_a;
528 XMoveResizeWindow(ob_display, self->bullet,
529 self->frame->text_x + self->frame->text_w -
530 ITEM_HEIGHT + PADDING, PADDING,
531 ITEM_HEIGHT - 2*PADDING,
532 ITEM_HEIGHT - 2*PADDING);
533 bullet_a = (self == self->frame->selected ?
534 ob_rr_theme->a_menu_bullet_selected :
535 ob_rr_theme->a_menu_bullet_normal);
536 bullet_a->surface.parent = item_a;
537 bullet_a->surface.parentx =
538 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
539 bullet_a->surface.parenty = PADDING;
540 RrPaint(bullet_a, self->bullet,
541 ITEM_HEIGHT - 2*PADDING,
542 ITEM_HEIGHT - 2*PADDING);
543 XMapWindow(ob_display, self->bullet);
545 XUnmapWindow(ob_display, self->bullet);
550 /*! this code is taken from the menu_frame_render. if that changes, this won't
552 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
553 gboolean first_entry,
562 t = self->entry->type;
564 /* this is the More... entry, it's NORMAL type */
565 t = OB_MENU_ENTRY_TYPE_NORMAL;
568 case OB_MENU_ENTRY_TYPE_NORMAL:
569 case OB_MENU_ENTRY_TYPE_SUBMENU:
570 h += ob_rr_theme->menu_font_height;
572 case OB_MENU_ENTRY_TYPE_SEPARATOR:
573 if (self->entry->data.separator.label != NULL) {
574 h += ob_rr_theme->menu_title_height +
575 (ob_rr_theme->mbwidth - PADDING) * 2;
577 /* if the first entry is a labeled separator, then make its border
578 overlap with the menu's outside border */
580 h -= ob_rr_theme->mbwidth;
581 /* if the last entry is a labeled separator, then make its border
582 overlap with the menu's outside border */
584 h -= ob_rr_theme->mbwidth;
586 h += SEPARATOR_HEIGHT;
594 void menu_frame_render(ObMenuFrame *self)
597 gint tw, th; /* temps */
599 gboolean has_icon = FALSE;
603 /* find text dimensions */
605 STRUT_SET(self->item_margin, 0, 0, 0, 0);
610 e = self->entries->data;
611 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
612 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
617 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
618 STRUT_SET(self->item_margin,
619 MAX(self->item_margin.left, l),
620 MAX(self->item_margin.top, t),
621 MAX(self->item_margin.right, r),
622 MAX(self->item_margin.bottom, b));
623 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
624 STRUT_SET(self->item_margin,
625 MAX(self->item_margin.left, l),
626 MAX(self->item_margin.top, t),
627 MAX(self->item_margin.right, r),
628 MAX(self->item_margin.bottom, b));
629 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
630 STRUT_SET(self->item_margin,
631 MAX(self->item_margin.left, l),
632 MAX(self->item_margin.top, t),
633 MAX(self->item_margin.right, r),
634 MAX(self->item_margin.bottom, b));
635 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
636 STRUT_SET(self->item_margin,
637 MAX(self->item_margin.left, l),
638 MAX(self->item_margin.top, t),
639 MAX(self->item_margin.right, r),
640 MAX(self->item_margin.bottom, b));
643 /* render the entries */
645 for (it = self->entries; it; it = g_list_next(it)) {
646 RrAppearance *text_a;
649 /* if the first entry is a labeled separator, then make its border
650 overlap with the menu's outside border */
651 if (it == self->entries &&
652 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
653 e->entry->data.separator.label)
655 h -= ob_rr_theme->mbwidth;
658 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
659 e->entry->data.separator.label)
661 e->border = ob_rr_theme->mbwidth;
664 RECT_SET_POINT(e->area, 0, h+e->border);
665 XMoveWindow(ob_display, e->window,
666 e->area.x-e->border, e->area.y-e->border);
667 XSetWindowBorderWidth(ob_display, e->window, e->border);
668 XSetWindowBorder(ob_display, e->window,
669 RrColorPixel(ob_rr_theme->menu_border_color));
672 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
673 !e->entry->data.normal.enabled ?
675 (e == self->selected ?
676 ob_rr_theme->a_menu_text_disabled_selected :
677 ob_rr_theme->a_menu_text_disabled) :
679 (e == self->selected ?
680 ob_rr_theme->a_menu_text_selected :
681 ob_rr_theme->a_menu_text_normal));
682 switch (e->entry->type) {
683 case OB_MENU_ENTRY_TYPE_NORMAL:
684 text_a->texture[0].data.text.string = e->entry->data.normal.label;
685 tw = RrMinWidth(text_a);
686 tw = MIN(tw, MAX_MENU_WIDTH);
687 th = ob_rr_theme->menu_font_height;
689 if (e->entry->data.normal.icon_data ||
690 e->entry->data.normal.mask)
693 case OB_MENU_ENTRY_TYPE_SUBMENU:
694 sub = e->entry->data.submenu.submenu;
695 text_a->texture[0].data.text.string = sub ? sub->title : "";
696 tw = RrMinWidth(text_a);
697 tw = MIN(tw, MAX_MENU_WIDTH);
698 th = ob_rr_theme->menu_font_height;
700 if (e->entry->data.normal.icon_data ||
701 e->entry->data.normal.mask)
704 tw += ITEM_HEIGHT - PADDING;
706 case OB_MENU_ENTRY_TYPE_SEPARATOR:
707 if (e->entry->data.separator.label != NULL) {
708 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
709 e->entry->data.separator.label;
710 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
711 2*ob_rr_theme->paddingx;
712 tw = MIN(tw, MAX_MENU_WIDTH);
713 th = ob_rr_theme->menu_title_height +
714 (ob_rr_theme->mbwidth - PADDING) *2;
717 th = SEPARATOR_HEIGHT;
727 /* if the last entry is a labeled separator, then make its border
728 overlap with the menu's outside border */
729 it = g_list_last(self->entries);
730 e = it ? it->data : NULL;
731 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
732 e->entry->data.separator.label)
734 h -= ob_rr_theme->mbwidth;
737 self->text_x = PADDING;
742 w += ITEM_HEIGHT + PADDING;
743 self->text_x += ITEM_HEIGHT + PADDING;
750 XResizeWindow(ob_display, self->window, w, h);
754 RrPaint(self->a_items, self->window, w, h);
756 for (it = self->entries; it; it = g_list_next(it))
757 menu_entry_frame_render(it->data);
759 w += ob_rr_theme->mbwidth * 2;
760 h += ob_rr_theme->mbwidth * 2;
762 RECT_SET_SIZE(self->area, w, h);
767 static void menu_frame_update(ObMenuFrame *self)
773 menu_pipe_execute(self->menu);
774 menu_find_submenus(self->menu);
776 self->selected = NULL;
778 /* start at show_from */
779 mit = g_list_nth(self->menu->entries, self->show_from);
781 /* go through the menu's and frame's entries and connect the frame entries
782 to the menu entries */
783 for (fit = self->entries; mit && fit;
784 mit = g_list_next(mit), fit = g_list_next(fit))
786 ObMenuEntryFrame *f = fit->data;
787 f->entry = mit->data;
790 /* if there are more menu entries than in the frame, add them */
792 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
793 self->entries = g_list_append(self->entries, e);
794 mit = g_list_next(mit);
797 /* if there are more frame entries than menu entries then get rid of
800 GList *n = g_list_next(fit);
801 menu_entry_frame_free(fit->data);
802 self->entries = g_list_delete_link(self->entries, fit);
806 /* * make the menu fit on the screen */
808 /* calculate the height of the menu */
810 for (fit = self->entries; fit; fit = g_list_next(fit))
811 h += menu_entry_frame_get_height(fit->data,
812 fit == self->entries,
813 g_list_next(fit) == NULL);
814 /* add the border at the top and bottom */
815 h += ob_rr_theme->mbwidth * 2;
817 a = screen_physical_area_monitor(self->monitor);
821 gboolean last_entry = TRUE;
823 /* take the height of our More... entry into account */
824 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
826 /* start at the end of the entries */
827 flast = g_list_last(self->entries);
829 /* pull out all the entries from the frame that don't
830 fit on the screen, leaving at least 1 though */
831 while (h > a->height && g_list_previous(flast) != NULL) {
832 /* update the height, without this entry */
833 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
835 /* destroy the entry we're not displaying */
837 flast = g_list_previous(flast);
838 menu_entry_frame_free(tmp->data);
839 self->entries = g_list_delete_link(self->entries, tmp);
841 /* only the first one that we see is the last entry in the menu */
846 ObMenuEntry *more_entry;
847 ObMenuEntryFrame *more_frame;
848 /* make the More... menu entry frame which will display in this
850 if self->menu->more_menu is NULL that means that this is already
851 More... menu, so just use ourself.
853 more_entry = menu_get_more((self->menu->more_menu ?
854 self->menu->more_menu :
856 /* continue where we left off */
858 g_list_length(self->entries));
859 more_frame = menu_entry_frame_new(more_entry, self);
860 /* make it get deleted when the menu frame goes away */
861 menu_entry_unref(more_entry);
863 /* add our More... entry to the frame */
864 self->entries = g_list_append(self->entries, more_frame);
870 menu_frame_render(self);
873 static gboolean menu_frame_is_visible(ObMenuFrame *self)
875 return !!(g_list_find(menu_frame_visible, self));
878 static gboolean menu_frame_show(ObMenuFrame *self)
882 /* determine if the underlying menu is already visible */
883 for (it = menu_frame_visible; it; it = g_list_next(it)) {
884 ObMenuFrame *f = it->data;
885 if (f->menu == self->menu)
889 if (self->menu->update_func)
890 if (!self->menu->update_func(self, self->menu->data))
894 if (menu_frame_visible == NULL) {
895 /* no menus shown yet */
897 /* grab the pointer in such a way as to pass through "owner events"
898 so that we can get enter/leave notifies in the menu. */
899 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
901 if (!grab_keyboard()) {
907 menu_frame_update(self);
909 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
911 if (self->menu->show_func)
912 self->menu->show_func(self, self->menu->data);
917 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
923 if (menu_frame_is_visible(self))
925 if (!menu_frame_show(self))
928 /* find the monitor the menu is on */
929 for (i = 0; i < screen_num_monitors; ++i) {
930 Rect *a = screen_physical_area_monitor(i);
931 gboolean contains = RECT_CONTAINS(*a, x, y);
939 if (self->menu->place_func)
940 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
942 menu_frame_place_topmenu(self, &x, &y);
944 menu_frame_move(self, x, y);
946 XMapWindow(ob_display, self->window);
948 if (screen_pointer_pos(&px, &py)) {
949 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
950 if (e && e->frame == self)
957 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
958 ObMenuEntryFrame *parent_entry)
963 if (menu_frame_is_visible(self))
966 self->monitor = parent->monitor;
967 self->parent = parent;
968 self->parent_entry = parent_entry;
970 /* set up parent's child to be us */
972 menu_frame_hide(parent->child);
973 parent->child = self;
975 if (!menu_frame_show(self))
978 menu_frame_place_submenu(self, &x, &y);
979 menu_frame_move_on_screen(self, x, y, &dx, &dy);
982 /*try the other side */
983 self->direction_right = !self->direction_right;
984 menu_frame_place_submenu(self, &x, &y);
985 menu_frame_move_on_screen(self, x, y, &dx, &dy);
987 menu_frame_move(self, x + dx, y + dy);
989 XMapWindow(ob_display, self->window);
991 if (screen_pointer_pos(&px, &py)) {
992 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
993 if (e && e->frame == self)
1000 static void menu_frame_hide(ObMenuFrame *self)
1002 GList *it = g_list_find(menu_frame_visible, self);
1007 if (self->menu->hide_func)
1008 self->menu->hide_func(self, self->menu->data);
1011 menu_frame_hide(self->child);
1014 self->parent->child = NULL;
1015 self->parent = NULL;
1016 self->parent_entry = NULL;
1018 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1020 if (menu_frame_visible == NULL) {
1021 /* last menu shown */
1026 XUnmapWindow(ob_display, self->window);
1028 menu_frame_free(self);
1031 void menu_frame_hide_all(void)
1035 if (config_submenu_show_delay) {
1036 /* remove any submenu open requests */
1037 ob_main_loop_timeout_remove(ob_main_loop,
1038 menu_entry_frame_submenu_timeout);
1040 if ((it = g_list_last(menu_frame_visible)))
1041 menu_frame_hide(it->data);
1044 void menu_frame_hide_all_client(ObClient *client)
1046 GList *it = g_list_last(menu_frame_visible);
1048 ObMenuFrame *f = it->data;
1049 if (f->client == client) {
1050 if (config_submenu_show_delay) {
1051 /* remove any submenu open requests */
1052 ob_main_loop_timeout_remove(ob_main_loop,
1053 menu_entry_frame_submenu_timeout);
1061 ObMenuFrame* menu_frame_under(gint x, gint y)
1063 ObMenuFrame *ret = NULL;
1066 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1067 ObMenuFrame *f = it->data;
1069 if (RECT_CONTAINS(f->area, x, y)) {
1077 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1080 ObMenuEntryFrame *ret = NULL;
1083 if ((frame = menu_frame_under(x, y))) {
1084 x -= ob_rr_theme->mbwidth + frame->area.x;
1085 y -= ob_rr_theme->mbwidth + frame->area.y;
1087 for (it = frame->entries; it; it = g_list_next(it)) {
1088 ObMenuEntryFrame *e = it->data;
1090 if (RECT_CONTAINS(e->area, x, y)) {
1099 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1101 g_assert(menu_frame_visible);
1102 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1106 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1109 ObMenuEntryFrame *old = self->selected;
1110 ObMenuFrame *oldchild = self->child;
1112 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1115 if (old == entry) return;
1117 if (config_submenu_show_delay) {
1118 /* remove any submenu open requests */
1119 ob_main_loop_timeout_remove(ob_main_loop,
1120 menu_entry_frame_submenu_timeout);
1123 self->selected = entry;
1126 menu_entry_frame_render(old);
1128 menu_frame_hide(oldchild);
1130 if (self->selected) {
1131 menu_entry_frame_render(self->selected);
1133 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1134 if (config_submenu_show_delay && !immediate) {
1135 /* initiate a new submenu open request */
1136 ob_main_loop_timeout_add(ob_main_loop,
1137 config_submenu_show_delay * 1000,
1138 menu_entry_frame_submenu_timeout,
1139 self->selected, g_direct_equal,
1142 menu_entry_frame_show_submenu(self->selected);
1148 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1152 if (!self->entry->data.submenu.submenu) return;
1154 f = menu_frame_new(self->entry->data.submenu.submenu,
1155 self->entry->data.submenu.show_from,
1156 self->frame->client);
1157 /* pass our direction on to our child */
1158 f->direction_right = self->frame->direction_right;
1160 menu_frame_show_submenu(f, self->frame, self);
1163 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1165 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1166 self->entry->data.normal.enabled)
1168 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1170 ObMenuEntry *entry = self->entry;
1171 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1172 gpointer data = self->frame->menu->data;
1173 GSList *acts = self->entry->data.normal.actions;
1174 ObClient *client = self->frame->client;
1175 ObMenuFrame *frame = self->frame;
1177 /* release grabs before executing the shit */
1178 if (!(state & ControlMask)) {
1179 menu_frame_hide_all();
1184 func(entry, frame, client, state, data);
1186 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1187 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1191 void menu_frame_select_previous(ObMenuFrame *self)
1193 GList *it = NULL, *start;
1195 if (self->entries) {
1196 start = it = g_list_find(self->entries, self->selected);
1198 ObMenuEntryFrame *e;
1200 it = it ? g_list_previous(it) : g_list_last(self->entries);
1206 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1208 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1213 menu_frame_select(self, it ? it->data : NULL, TRUE);
1216 void menu_frame_select_next(ObMenuFrame *self)
1218 GList *it = NULL, *start;
1220 if (self->entries) {
1221 start = it = g_list_find(self->entries, self->selected);
1223 ObMenuEntryFrame *e;
1225 it = it ? g_list_next(it) : self->entries;
1231 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1233 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1238 menu_frame_select(self, it ? it->data : NULL, TRUE);