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 FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
36 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
37 ButtonPressMask | ButtonReleaseMask)
39 GList *menu_frame_visible;
41 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
43 static void menu_entry_frame_free(ObMenuEntryFrame *self);
44 static void menu_frame_render(ObMenuFrame *self);
45 static void menu_frame_update(ObMenuFrame *self);
46 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
48 static Window createWindow(Window parent, gulong mask,
49 XSetWindowAttributes *attrib)
51 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
52 RrDepth(ob_rr_inst), InputOutput,
53 RrVisual(ob_rr_inst), mask, attrib);
56 GHashTable *menu_frame_map;
58 void menu_frame_startup(gboolean reconfig)
62 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
65 void menu_frame_shutdown(gboolean reconfig)
69 g_hash_table_destroy(menu_frame_map);
72 ObMenuFrame* menu_frame_new(ObMenu *menu, ObClient *client)
75 XSetWindowAttributes attr;
77 self = g_new0(ObMenuFrame, 1);
78 self->type = Window_Menu;
80 self->selected = NULL;
81 self->client = client;
82 self->direction_right = TRUE;
84 attr.event_mask = FRAME_EVENTMASK;
85 self->window = createWindow(RootWindow(ob_display, ob_screen),
88 self->a_title = RrAppearanceCopy(ob_rr_theme->a_menu_title);
89 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
91 stacking_add(MENU_AS_WINDOW(self));
96 void menu_frame_free(ObMenuFrame *self)
99 while (self->entries) {
100 menu_entry_frame_free(self->entries->data);
101 self->entries = g_list_delete_link(self->entries, self->entries);
104 stacking_remove(MENU_AS_WINDOW(self));
106 XDestroyWindow(ob_display, self->window);
108 RrAppearanceFree(self->a_items);
109 RrAppearanceFree(self->a_title);
115 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
118 ObMenuEntryFrame *self;
119 XSetWindowAttributes attr;
121 self = g_new0(ObMenuEntryFrame, 1);
125 attr.event_mask = ENTRY_EVENTMASK;
126 self->window = createWindow(self->frame->window, CWEventMask, &attr);
127 self->text = createWindow(self->window, 0, NULL);
128 g_hash_table_insert(menu_frame_map, &self->window, self);
129 g_hash_table_insert(menu_frame_map, &self->text, self);
130 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
131 self->icon = createWindow(self->window, 0, NULL);
132 g_hash_table_insert(menu_frame_map, &self->icon, self);
134 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
135 self->bullet = createWindow(self->window, 0, NULL);
136 g_hash_table_insert(menu_frame_map, &self->bullet, self);
139 XMapWindow(ob_display, self->window);
140 XMapWindow(ob_display, self->text);
142 self->a_normal = RrAppearanceCopy(ob_rr_theme->a_menu_normal);
143 self->a_disabled = RrAppearanceCopy(ob_rr_theme->a_menu_disabled);
144 self->a_selected = RrAppearanceCopy(ob_rr_theme->a_menu_selected);
146 if (entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR) {
147 self->a_separator = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
148 self->a_separator->texture[0].type = RR_TEXTURE_LINE_ART;
150 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
151 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
152 self->a_mask = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
153 self->a_mask->texture[0].type = RR_TEXTURE_MASK;
154 self->a_bullet_normal =
155 RrAppearanceCopy(ob_rr_theme->a_menu_bullet_normal);
156 self->a_bullet_selected =
157 RrAppearanceCopy(ob_rr_theme->a_menu_bullet_selected);
160 self->a_text_normal =
161 RrAppearanceCopy(ob_rr_theme->a_menu_text_normal);
162 self->a_text_disabled =
163 RrAppearanceCopy(ob_rr_theme->a_menu_text_disabled);
164 self->a_text_selected =
165 RrAppearanceCopy(ob_rr_theme->a_menu_text_selected);
167 RrAppearanceCopy(ob_rr_theme->a_menu_text_title);
172 static void menu_entry_frame_free(ObMenuEntryFrame *self)
175 XDestroyWindow(ob_display, self->text);
176 XDestroyWindow(ob_display, self->window);
177 g_hash_table_remove(menu_frame_map, &self->text);
178 g_hash_table_remove(menu_frame_map, &self->window);
179 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
180 XDestroyWindow(ob_display, self->icon);
181 g_hash_table_remove(menu_frame_map, &self->icon);
183 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
184 XDestroyWindow(ob_display, self->bullet);
185 g_hash_table_remove(menu_frame_map, &self->bullet);
188 RrAppearanceFree(self->a_normal);
189 RrAppearanceFree(self->a_disabled);
190 RrAppearanceFree(self->a_selected);
192 RrAppearanceFree(self->a_separator);
193 RrAppearanceFree(self->a_icon);
194 RrAppearanceFree(self->a_mask);
195 RrAppearanceFree(self->a_text_normal);
196 RrAppearanceFree(self->a_text_disabled);
197 RrAppearanceFree(self->a_text_selected);
198 RrAppearanceFree(self->a_text_title);
199 RrAppearanceFree(self->a_bullet_normal);
200 RrAppearanceFree(self->a_bullet_selected);
206 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
208 RECT_SET_POINT(self->area, x, y);
209 XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
212 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
216 if (config_menu_middle) {
220 *y -= self->area.height / 2;
222 /* try to the right of the cursor */
223 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
224 self->direction_right = TRUE;
226 /* try to the left of the cursor */
227 myx = *x - self->area.width;
228 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
229 self->direction_right = FALSE;
232 /* if didnt fit on either side so just use what it says */
234 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
235 self->direction_right = TRUE;
245 /* try to the bottom right of the cursor */
246 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
247 self->direction_right = TRUE;
248 if (dx != 0 || dy != 0) {
249 /* try to the bottom left of the cursor */
250 myx = *x - self->area.width;
252 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
253 self->direction_right = FALSE;
255 if (dx != 0 || dy != 0) {
256 /* try to the top right of the cursor */
258 myy = *y - self->area.height;
259 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
260 self->direction_right = TRUE;
262 if (dx != 0 || dy != 0) {
263 /* try to the top left of the cursor */
264 myx = *x - self->area.width;
265 myy = *y - self->area.height;
266 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
267 self->direction_right = FALSE;
269 if (dx != 0 || dy != 0) {
270 /* if didnt fit on either side so just use what it says */
273 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
274 self->direction_right = TRUE;
281 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
286 overlap = ob_rr_theme->menu_overlap;
287 bwidth = ob_rr_theme->mbwidth;
289 if (self->direction_right)
290 *x = self->parent->area.x + self->parent->area.width -
293 *x = self->parent->area.x - self->area.width + overlap + bwidth;
295 *y = self->parent->area.y + self->parent_entry->area.y;
296 if (config_menu_middle)
297 *y -= (self->area.height - (bwidth * 2) - self->item_h) / 2;
302 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
310 a = screen_physical_area_monitor(self->monitor);
312 half = g_list_length(self->entries) / 2;
313 pos = g_list_index(self->entries, self->selected);
315 /* if in the bottom half then check this stuff first, will keep the bottom
316 edge of the menu visible */
318 *dx = MAX(*dx, a->x - x);
319 *dy = MAX(*dy, a->y - y);
321 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
322 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
323 /* if in the top half then check this stuff last, will keep the top
324 edge of the menu visible */
326 *dx = MAX(*dx, a->x - x);
327 *dy = MAX(*dy, a->y - y);
331 static void menu_entry_frame_render(ObMenuEntryFrame *self)
333 RrAppearance *item_a, *text_a;
336 ObMenuFrame *frame = self->frame;
338 switch (self->entry->type) {
339 case OB_MENU_ENTRY_TYPE_NORMAL:
340 case OB_MENU_ENTRY_TYPE_SUBMENU:
341 item_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
342 !self->entry->data.normal.enabled) ?
344 (self == self->frame->selected ?
347 th = self->frame->item_h;
349 case OB_MENU_ENTRY_TYPE_SEPARATOR:
350 if (self->entry->data.separator.label) {
351 item_a = self->frame->a_title;
352 th = ob_rr_theme->menu_title_height;
354 item_a = self->a_normal;
355 th = SEPARATOR_HEIGHT + 2*PADDING;
359 g_assert_not_reached();
361 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
362 XResizeWindow(ob_display, self->window,
363 self->area.width, self->area.height);
364 item_a->surface.parent = self->frame->a_items;
365 item_a->surface.parentx = self->area.x;
366 item_a->surface.parenty = self->area.y;
367 RrPaint(item_a, self->window, self->area.width, self->area.height);
369 switch (self->entry->type) {
370 case OB_MENU_ENTRY_TYPE_NORMAL:
371 text_a = (!self->entry->data.normal.enabled ?
372 self->a_text_disabled :
373 (self == self->frame->selected ?
374 self->a_text_selected :
375 self->a_text_normal));
376 text_a->texture[0].data.text.string = self->entry->data.normal.label;
377 if (self->entry->data.normal.shortcut &&
378 (self->frame->menu->show_all_shortcuts ||
379 self->entry->data.normal.shortcut_position > 0))
381 text_a->texture[0].data.text.shortcut = TRUE;
382 text_a->texture[0].data.text.shortcut_pos =
383 self->entry->data.normal.shortcut_position;
385 text_a->texture[0].data.text.shortcut = FALSE;
387 case OB_MENU_ENTRY_TYPE_SUBMENU:
388 text_a = (self == self->frame->selected ?
389 self->a_text_selected :
390 self->a_text_normal);
391 sub = self->entry->data.submenu.submenu;
392 text_a->texture[0].data.text.string = sub ? sub->title : "";
393 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
394 sub->shortcut_position > 0))
396 text_a->texture[0].data.text.shortcut = TRUE;
397 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
399 text_a->texture[0].data.text.shortcut = FALSE;
401 case OB_MENU_ENTRY_TYPE_SEPARATOR:
402 if (self->entry->data.separator.label != NULL)
403 text_a = self->a_text_title;
405 text_a = self->a_text_normal;
409 switch (self->entry->type) {
410 case OB_MENU_ENTRY_TYPE_NORMAL:
411 XMoveResizeWindow(ob_display, self->text,
412 self->frame->text_x, PADDING,
414 self->frame->item_h - 2*PADDING);
415 text_a->surface.parent = item_a;
416 text_a->surface.parentx = self->frame->text_x;
417 text_a->surface.parenty = PADDING;
418 RrPaint(text_a, self->text, self->frame->text_w,
419 self->frame->item_h - 2*PADDING);
421 case OB_MENU_ENTRY_TYPE_SUBMENU:
422 XMoveResizeWindow(ob_display, self->text,
423 self->frame->text_x, PADDING,
424 self->frame->text_w - self->frame->item_h,
425 self->frame->item_h - 2*PADDING);
426 text_a->surface.parent = item_a;
427 text_a->surface.parentx = self->frame->text_x;
428 text_a->surface.parenty = PADDING;
429 RrPaint(text_a, self->text, self->frame->text_w - self->frame->item_h,
430 self->frame->item_h - 2*PADDING);
432 case OB_MENU_ENTRY_TYPE_SEPARATOR:
433 if (self->entry->data.separator.label != NULL) {
434 /* labeled separator */
435 XMoveResizeWindow(ob_display, self->text,
436 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
437 self->area.width - 2*ob_rr_theme->paddingx,
438 ob_rr_theme->menu_title_height -
439 2*ob_rr_theme->paddingy);
440 text_a->surface.parent = item_a;
441 text_a->surface.parentx = ob_rr_theme->paddingx;
442 text_a->surface.parenty = ob_rr_theme->paddingy;
443 RrPaint(text_a, self->text,
444 self->area.width - 2*ob_rr_theme->paddingx,
445 ob_rr_theme->menu_title_height -
446 2*ob_rr_theme->paddingy);
448 /* unlabeled separaator */
449 XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
450 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
451 self->a_separator->surface.parent = item_a;
452 self->a_separator->surface.parentx = PADDING;
453 self->a_separator->surface.parenty = PADDING;
454 self->a_separator->texture[0].data.lineart.color =
455 text_a->texture[0].data.text.color;
456 self->a_separator->texture[0].data.lineart.x1 = 2*PADDING;
457 self->a_separator->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
458 self->a_separator->texture[0].data.lineart.x2 =
459 self->area.width - 4*PADDING;
460 self->a_separator->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
461 RrPaint(self->a_separator, self->text,
462 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
467 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
468 self->entry->data.normal.icon_data)
470 XMoveResizeWindow(ob_display, self->icon,
471 PADDING, frame->item_margin.top,
472 self->frame->item_h - frame->item_margin.top
473 - frame->item_margin.bottom,
474 self->frame->item_h - frame->item_margin.top
475 - frame->item_margin.bottom);
476 self->a_icon->texture[0].data.rgba.width =
477 self->entry->data.normal.icon_width;
478 self->a_icon->texture[0].data.rgba.height =
479 self->entry->data.normal.icon_height;
480 self->a_icon->texture[0].data.rgba.data =
481 self->entry->data.normal.icon_data;
482 self->a_icon->surface.parent = item_a;
483 self->a_icon->surface.parentx = PADDING;
484 self->a_icon->surface.parenty = frame->item_margin.top;
485 RrPaint(self->a_icon, self->icon,
486 self->frame->item_h - frame->item_margin.top
487 - frame->item_margin.bottom,
488 self->frame->item_h - frame->item_margin.top
489 - frame->item_margin.bottom);
490 XMapWindow(ob_display, self->icon);
491 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
492 self->entry->data.normal.mask)
496 XMoveResizeWindow(ob_display, self->icon,
497 PADDING, frame->item_margin.top,
498 self->frame->item_h - frame->item_margin.top
499 - frame->item_margin.bottom,
500 self->frame->item_h - frame->item_margin.top
501 - frame->item_margin.bottom);
502 self->a_mask->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) ?
507 self->entry->data.normal.mask_disabled_color :
508 (self == self->frame->selected ?
509 self->entry->data.normal.mask_selected_color :
510 self->entry->data.normal.mask_normal_color));
511 self->a_mask->texture[0].data.mask.color = c;
513 self->a_mask->surface.parent = item_a;
514 self->a_mask->surface.parentx = PADDING;
515 self->a_mask->surface.parenty = frame->item_margin.top;
516 RrPaint(self->a_mask, self->icon,
517 self->frame->item_h - frame->item_margin.top
518 - frame->item_margin.bottom,
519 self->frame->item_h - frame->item_margin.top
520 - frame->item_margin.bottom);
521 XMapWindow(ob_display, self->icon);
523 XUnmapWindow(ob_display, self->icon);
525 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
526 RrAppearance *bullet_a;
527 XMoveResizeWindow(ob_display, self->bullet,
528 self->frame->text_x + self->frame->text_w
529 - self->frame->item_h + PADDING, PADDING,
530 self->frame->item_h - 2*PADDING,
531 self->frame->item_h - 2*PADDING);
532 bullet_a = (self == self->frame->selected ?
533 self->a_bullet_selected :
534 self->a_bullet_normal);
535 bullet_a->surface.parent = item_a;
536 bullet_a->surface.parentx =
537 self->frame->text_x + self->frame->text_w - self->frame->item_h
539 bullet_a->surface.parenty = PADDING;
540 RrPaint(bullet_a, self->bullet,
541 self->frame->item_h - 2*PADDING,
542 self->frame->item_h - 2*PADDING);
543 XMapWindow(ob_display, self->bullet);
545 XUnmapWindow(ob_display, self->bullet);
550 static void menu_frame_render(ObMenuFrame *self)
553 gint tw, th; /* temps */
555 gboolean has_icon = FALSE;
559 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
560 XSetWindowBorder(ob_display, self->window,
561 RrColorPixel(ob_rr_theme->menu_b_color));
563 /* find text dimensions */
565 STRUT_SET(self->item_margin, 0, 0, 0, 0);
568 ObMenuEntryFrame *e = self->entries->data;
571 e->a_text_normal->texture[0].data.text.string = "";
572 RrMinSize(e->a_text_normal, &tw, &th);
577 RrMargins(e->a_normal, &l, &t, &r, &b);
578 STRUT_SET(self->item_margin,
579 MAX(self->item_margin.left, l),
580 MAX(self->item_margin.top, t),
581 MAX(self->item_margin.right, r),
582 MAX(self->item_margin.bottom, b));
583 RrMargins(e->a_selected, &l, &t, &r, &b);
584 STRUT_SET(self->item_margin,
585 MAX(self->item_margin.left, l),
586 MAX(self->item_margin.top, t),
587 MAX(self->item_margin.right, r),
588 MAX(self->item_margin.bottom, b));
589 RrMargins(e->a_disabled, &l, &t, &r, &b);
590 STRUT_SET(self->item_margin,
591 MAX(self->item_margin.left, l),
592 MAX(self->item_margin.top, t),
593 MAX(self->item_margin.right, r),
594 MAX(self->item_margin.bottom, b));
598 /* render the entries */
600 for (it = self->entries; it; it = g_list_next(it)) {
601 RrAppearance *text_a;
604 /* if the first entry is a labeled separator, then make its border
605 overlap with the menu's outside border */
606 if (it == self->entries &&
607 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
608 e->entry->data.separator.label)
610 h -= ob_rr_theme->mbwidth;
613 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
614 e->entry->data.separator.label)
616 e->border = ob_rr_theme->mbwidth;
619 RECT_SET_POINT(e->area, 0, h+e->border);
620 XMoveWindow(ob_display, e->window, e->area.x-e->border, e->area.y-e->border);
621 XSetWindowBorderWidth(ob_display, e->window, e->border);
622 XSetWindowBorder(ob_display, e->window,
623 RrColorPixel(ob_rr_theme->menu_b_color));
625 text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
626 !e->entry->data.normal.enabled) ?
628 (e == self->selected ?
631 switch (e->entry->type) {
632 case OB_MENU_ENTRY_TYPE_NORMAL:
633 text_a->texture[0].data.text.string = e->entry->data.normal.label;
634 RrMinSize(text_a, &tw, &th);
635 tw = MIN(tw, MAX_MENU_WIDTH);
637 if (e->entry->data.normal.icon_data ||
638 e->entry->data.normal.mask)
641 case OB_MENU_ENTRY_TYPE_SUBMENU:
642 sub = e->entry->data.submenu.submenu;
643 text_a->texture[0].data.text.string = sub ? sub->title : "";
644 RrMinSize(text_a, &tw, &th);
645 tw = MIN(tw, MAX_MENU_WIDTH);
647 if (e->entry->data.normal.icon_data ||
648 e->entry->data.normal.mask)
651 tw += self->item_h - PADDING;
653 case OB_MENU_ENTRY_TYPE_SEPARATOR:
654 if (e->entry->data.separator.label != NULL) {
655 e->a_text_title->texture[0].data.text.string =
656 e->entry->data.separator.label;
657 RrMinSize(e->a_text_title, &tw, &th);
658 tw = MIN(tw, MAX_MENU_WIDTH);
659 th = ob_rr_theme->menu_title_height +
660 (ob_rr_theme->mbwidth - PADDING) *2;
663 th = SEPARATOR_HEIGHT;
673 /* if the last entry is a labeled separator, then make its border
674 overlap with the menu's outside border */
675 it = g_list_last(self->entries);
676 e = it ? it->data : NULL;
677 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
678 e->entry->data.separator.label)
680 h -= ob_rr_theme->mbwidth;
683 self->text_x = PADDING;
688 w += self->item_h + PADDING;
689 self->text_x += self->item_h + PADDING;
696 XResizeWindow(ob_display, self->window, w, h);
700 RrPaint(self->a_items, self->window, w, h);
702 for (it = self->entries; it; it = g_list_next(it))
703 menu_entry_frame_render(it->data);
705 w += ob_rr_theme->mbwidth * 2;
706 h += ob_rr_theme->mbwidth * 2;
708 RECT_SET_SIZE(self->area, w, h);
713 static void menu_frame_update(ObMenuFrame *self)
717 menu_pipe_execute(self->menu);
718 menu_find_submenus(self->menu);
720 self->selected = NULL;
722 for (mit = self->menu->entries, fit = self->entries; mit && fit;
723 mit = g_list_next(mit), fit = g_list_next(fit))
725 ObMenuEntryFrame *f = fit->data;
726 f->entry = mit->data;
730 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
731 self->entries = g_list_append(self->entries, e);
732 mit = g_list_next(mit);
736 GList *n = g_list_next(fit);
737 menu_entry_frame_free(fit->data);
738 self->entries = g_list_delete_link(self->entries, fit);
742 menu_frame_render(self);
745 static gboolean menu_frame_is_visible(ObMenuFrame *self)
747 return !!(g_list_find(menu_frame_visible, self));
750 static gboolean menu_frame_show(ObMenuFrame *self)
754 if (menu_frame_visible == NULL) {
755 /* no menus shown yet */
756 if (!grab_pointer(TRUE, TRUE, OB_CURSOR_POINTER))
758 if (!grab_keyboard(TRUE)) {
759 grab_pointer(FALSE, TRUE, OB_CURSOR_POINTER);
764 /* determine if the underlying menu is already visible */
765 for (it = menu_frame_visible; it; it = g_list_next(it)) {
766 ObMenuFrame *f = it->data;
767 if (f->menu == self->menu)
771 if (self->menu->update_func)
772 self->menu->update_func(self, self->menu->data);
775 menu_frame_update(self);
777 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
782 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
787 if (menu_frame_is_visible(self))
789 if (!menu_frame_show(self))
792 /* find the monitor the menu is on */
793 for (i = 0; i < screen_num_monitors; ++i) {
794 Rect *a = screen_physical_area_monitor(i);
795 if (RECT_CONTAINS(*a, x, y)) {
801 if (self->menu->place_func)
802 self->menu->place_func(self, &x, &y, button, self->menu->data);
804 menu_frame_place_topmenu(self, &x, &y);
806 menu_frame_move(self, x, y);
808 XMapWindow(ob_display, self->window);
813 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
814 ObMenuEntryFrame *parent_entry)
819 if (menu_frame_is_visible(self))
822 self->monitor = parent->monitor;
823 self->parent = parent;
824 self->parent_entry = parent_entry;
826 /* set up parent's child to be us */
828 menu_frame_hide(parent->child);
829 parent->child = self;
831 if (!menu_frame_show(self))
834 menu_frame_place_submenu(self, &x, &y);
835 menu_frame_move_on_screen(self, x, y, &dx, &dy);
838 /*try the other side */
839 self->direction_right = !self->direction_right;
840 menu_frame_place_submenu(self, &x, &y);
841 menu_frame_move_on_screen(self, x, y, &dx, &dy);
843 menu_frame_move(self, x + dx, y + dy);
845 XMapWindow(ob_display, self->window);
847 if (screen_pointer_pos(&dx, &dy) && (e = menu_entry_frame_under(dx, dy)) &&
854 void menu_frame_hide(ObMenuFrame *self)
856 GList *it = g_list_find(menu_frame_visible, self);
862 menu_frame_hide(self->child);
865 self->parent->child = NULL;
867 self->parent_entry = NULL;
869 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
871 if (menu_frame_visible == NULL) {
872 /* last menu shown */
873 grab_pointer(FALSE, TRUE, OB_CURSOR_NONE);
874 grab_keyboard(FALSE);
877 XUnmapWindow(ob_display, self->window);
879 menu_frame_free(self);
882 void menu_frame_hide_all()
886 if (config_submenu_show_delay) {
887 /* remove any submenu open requests */
888 ob_main_loop_timeout_remove(ob_main_loop,
889 menu_entry_frame_submenu_timeout);
891 if ((it = g_list_last(menu_frame_visible)))
892 menu_frame_hide(it->data);
895 void menu_frame_hide_all_client(ObClient *client)
897 GList *it = g_list_last(menu_frame_visible);
899 ObMenuFrame *f = it->data;
900 if (f->client == client)
906 ObMenuFrame* menu_frame_under(gint x, gint y)
908 ObMenuFrame *ret = NULL;
911 for (it = menu_frame_visible; it; it = g_list_next(it)) {
912 ObMenuFrame *f = it->data;
914 if (RECT_CONTAINS(f->area, x, y)) {
922 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
925 ObMenuEntryFrame *ret = NULL;
928 if ((frame = menu_frame_under(x, y))) {
929 x -= ob_rr_theme->mbwidth + frame->area.x;
930 y -= ob_rr_theme->mbwidth + frame->area.y;
932 for (it = frame->entries; it; it = g_list_next(it)) {
933 ObMenuEntryFrame *e = it->data;
935 if (RECT_CONTAINS(e->area, x, y)) {
944 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
946 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
950 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
953 ObMenuEntryFrame *old = self->selected;
954 ObMenuFrame *oldchild = self->child;
956 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
959 if (old == entry) return;
961 if (config_submenu_show_delay) {
962 /* remove any submenu open requests */
963 ob_main_loop_timeout_remove(ob_main_loop,
964 menu_entry_frame_submenu_timeout);
967 self->selected = entry;
970 menu_entry_frame_render(old);
972 menu_frame_hide(oldchild);
974 if (self->selected) {
975 menu_entry_frame_render(self->selected);
977 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
978 if (config_submenu_show_delay && !immediate) {
979 /* initiate a new submenu open request */
980 ob_main_loop_timeout_add(ob_main_loop,
981 config_submenu_show_delay * 1000,
982 menu_entry_frame_submenu_timeout,
983 self->selected, g_direct_equal,
986 menu_entry_frame_show_submenu(self->selected);
992 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
996 if (!self->entry->data.submenu.submenu) return;
998 f = menu_frame_new(self->entry->data.submenu.submenu,
999 self->frame->client);
1000 /* pass our direction on to our child */
1001 f->direction_right = self->frame->direction_right;
1003 menu_frame_show_submenu(f, self->frame, self);
1006 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state, Time time)
1008 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1009 self->entry->data.normal.enabled)
1011 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1013 ObMenuEntry *entry = self->entry;
1014 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1015 gpointer data = self->frame->menu->data;
1016 GSList *acts = self->entry->data.normal.actions;
1017 ObClient *client = self->frame->client;
1019 /* release grabs before executing the shit */
1020 if (!(state & ControlMask))
1021 menu_frame_hide_all();
1024 func(entry, state, data, time);
1026 action_run(acts, client, state, time);
1030 void menu_frame_select_previous(ObMenuFrame *self)
1032 GList *it = NULL, *start;
1034 if (self->entries) {
1035 start = it = g_list_find(self->entries, self->selected);
1037 ObMenuEntryFrame *e;
1039 it = it ? g_list_previous(it) : g_list_last(self->entries);
1045 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1047 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1048 e->entry->data.normal.enabled)
1053 menu_frame_select(self, it ? it->data : NULL, TRUE);
1056 void menu_frame_select_next(ObMenuFrame *self)
1058 GList *it = NULL, *start;
1060 if (self->entries) {
1061 start = it = g_list_find(self->entries, self->selected);
1063 ObMenuEntryFrame *e;
1065 it = it ? g_list_next(it) : self->entries;
1071 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1073 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1074 e->entry->data.normal.enabled)
1079 menu_frame_select(self, it ? it->data : NULL, TRUE);