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"
30 #include "obt/keyboard.h"
31 #include "obrender/theme.h"
34 #define MAX_MENU_WIDTH 400
36 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
38 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
40 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
41 ButtonPressMask | ButtonReleaseMask)
43 GList
*menu_frame_visible
;
44 GHashTable
*menu_frame_map
;
46 static RrAppearance
*a_sep
;
48 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
50 static void menu_entry_frame_free(ObMenuEntryFrame
*self
);
51 static void menu_frame_update(ObMenuFrame
*self
);
52 static gboolean
submenu_show_timeout(gpointer data
);
53 static void menu_frame_hide(ObMenuFrame
*self
);
55 static gboolean
submenu_hide_timeout(gpointer data
);
57 static Window
createWindow(Window parent
, gulong mask
,
58 XSetWindowAttributes
*attrib
)
60 return XCreateWindow(obt_display
, parent
, 0, 0, 1, 1, 0,
61 RrDepth(ob_rr_inst
), InputOutput
,
62 RrVisual(ob_rr_inst
), mask
, attrib
);
65 static void client_dest(ObClient
*client
, gpointer data
)
69 /* menus can be associated with a client, so null those refs since
70 we are disappearing now */
71 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
72 ObMenuFrame
*f
= it
->data
;
73 if (f
->client
== client
)
78 void menu_frame_startup(gboolean reconfig
)
82 a_sep
= RrAppearanceCopy(ob_rr_theme
->a_clear
);
83 RrAppearanceAddTextures(a_sep
, ob_rr_theme
->menu_sep_width
);
84 for (i
= 0; i
< ob_rr_theme
->menu_sep_width
; ++i
) {
85 a_sep
->texture
[i
].type
= RR_TEXTURE_LINE_ART
;
86 a_sep
->texture
[i
].data
.lineart
.color
=
87 ob_rr_theme
->menu_sep_color
;
92 client_add_destroy_notify(client_dest
, NULL
);
93 menu_frame_map
= g_hash_table_new(g_int_hash
, g_int_equal
);
96 void menu_frame_shutdown(gboolean reconfig
)
98 RrAppearanceFree(a_sep
);
100 if (reconfig
) return;
102 client_remove_destroy_notify(client_dest
);
103 g_hash_table_destroy(menu_frame_map
);
106 ObMenuFrame
* menu_frame_new(ObMenu
*menu
, guint show_from
, ObClient
*client
)
109 XSetWindowAttributes attr
;
111 self
= g_new0(ObMenuFrame
, 1);
112 self
->obwin
.type
= OB_WINDOW_CLASS_MENUFRAME
;
114 self
->selected
= NULL
;
115 self
->client
= client
;
116 self
->direction_right
= TRUE
;
117 self
->show_from
= show_from
;
119 attr
.event_mask
= FRAME_EVENTMASK
;
120 self
->window
= createWindow(obt_root(ob_screen
),
123 /* make it a popup menu type window */
124 OBT_PROP_SET32(self
->window
, NET_WM_WINDOW_TYPE
, ATOM
,
125 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU
));
127 XSetWindowBorderWidth(obt_display
, self
->window
, ob_rr_theme
->mbwidth
);
128 XSetWindowBorder(obt_display
, self
->window
,
129 RrColorPixel(ob_rr_theme
->menu_border_color
));
131 self
->a_items
= RrAppearanceCopy(ob_rr_theme
->a_menu
);
133 window_add(&self
->window
, MENUFRAME_AS_WINDOW(self
));
134 stacking_add(MENUFRAME_AS_WINDOW(self
));
139 void menu_frame_free(ObMenuFrame
*self
)
142 while (self
->entries
) {
143 menu_entry_frame_free(self
->entries
->data
);
144 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
147 stacking_remove(MENUFRAME_AS_WINDOW(self
));
148 window_remove(self
->window
);
150 RrAppearanceFree(self
->a_items
);
152 XDestroyWindow(obt_display
, self
->window
);
158 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
161 ObMenuEntryFrame
*self
;
162 XSetWindowAttributes attr
;
164 self
= g_new0(ObMenuEntryFrame
, 1);
168 menu_entry_ref(entry
);
170 attr
.event_mask
= ENTRY_EVENTMASK
;
171 self
->window
= createWindow(self
->frame
->window
, CWEventMask
, &attr
);
172 self
->text
= createWindow(self
->window
, 0, NULL
);
173 g_hash_table_insert(menu_frame_map
, &self
->window
, self
);
174 g_hash_table_insert(menu_frame_map
, &self
->text
, self
);
175 if (entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
176 self
->icon
= createWindow(self
->window
, 0, NULL
);
177 g_hash_table_insert(menu_frame_map
, &self
->icon
, self
);
179 if (entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
180 self
->bullet
= createWindow(self
->window
, 0, NULL
);
181 g_hash_table_insert(menu_frame_map
, &self
->bullet
, self
);
184 XMapWindow(obt_display
, self
->window
);
185 XMapWindow(obt_display
, self
->text
);
187 window_add(&self
->window
, MENUFRAME_AS_WINDOW(self
->frame
));
192 static void menu_entry_frame_free(ObMenuEntryFrame
*self
)
195 menu_entry_unref(self
->entry
);
197 window_remove(self
->window
);
199 XDestroyWindow(obt_display
, self
->text
);
200 XDestroyWindow(obt_display
, self
->window
);
201 g_hash_table_remove(menu_frame_map
, &self
->text
);
202 g_hash_table_remove(menu_frame_map
, &self
->window
);
203 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
204 XDestroyWindow(obt_display
, self
->icon
);
205 g_hash_table_remove(menu_frame_map
, &self
->icon
);
207 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
208 XDestroyWindow(obt_display
, self
->bullet
);
209 g_hash_table_remove(menu_frame_map
, &self
->bullet
);
216 void menu_frame_move(ObMenuFrame
*self
, gint x
, gint y
)
218 RECT_SET_POINT(self
->area
, x
, y
);
219 self
->monitor
= screen_find_monitor_point(x
, y
);
220 XMoveWindow(obt_display
, self
->window
, self
->area
.x
, self
->area
.y
);
223 static void menu_frame_place_topmenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
227 if (config_menu_middle
) {
231 *y
-= self
->area
.height
/ 2;
233 /* try to the right of the cursor */
234 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
235 self
->direction_right
= TRUE
;
237 /* try to the left of the cursor */
238 myx
= *x
- self
->area
.width
;
239 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
240 self
->direction_right
= FALSE
;
243 /* if didnt fit on either side so just use what it says */
245 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
246 self
->direction_right
= TRUE
;
256 /* try to the bottom right of the cursor */
257 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
258 self
->direction_right
= TRUE
;
259 if (dx
!= 0 || dy
!= 0) {
260 /* try to the bottom left of the cursor */
261 myx
= *x
- self
->area
.width
;
263 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
264 self
->direction_right
= FALSE
;
266 if (dx
!= 0 || dy
!= 0) {
267 /* try to the top right of the cursor */
269 myy
= *y
- self
->area
.height
;
270 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
271 self
->direction_right
= TRUE
;
273 if (dx
!= 0 || dy
!= 0) {
274 /* try to the top left of the cursor */
275 myx
= *x
- self
->area
.width
;
276 myy
= *y
- self
->area
.height
;
277 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
278 self
->direction_right
= FALSE
;
280 if (dx
!= 0 || dy
!= 0) {
281 /* if didnt fit on either side so just use what it says */
284 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
285 self
->direction_right
= TRUE
;
292 static void menu_frame_place_submenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
294 gint overlapx
, overlapy
;
297 overlapx
= ob_rr_theme
->menu_overlap_x
;
298 overlapy
= ob_rr_theme
->menu_overlap_y
;
299 bwidth
= ob_rr_theme
->mbwidth
;
301 if (self
->direction_right
)
302 *x
= self
->parent
->area
.x
+ self
->parent
->area
.width
-
305 *x
= self
->parent
->area
.x
- self
->area
.width
+ overlapx
+ bwidth
;
307 *y
= self
->parent
->area
.y
+ self
->parent_entry
->area
.y
;
308 if (config_menu_middle
)
309 *y
-= (self
->area
.height
- (bwidth
* 2) - ITEM_HEIGHT
) / 2;
314 void menu_frame_move_on_screen(ObMenuFrame
*self
, gint x
, gint y
,
322 a
= screen_physical_area_monitor(screen_find_monitor_point(x
, y
));
324 half
= g_list_length(self
->entries
) / 2;
325 pos
= g_list_index(self
->entries
, self
->selected
);
327 /* if in the bottom half then check this stuff first, will keep the bottom
328 edge of the menu visible */
330 *dx
= MAX(*dx
, a
->x
- x
);
331 *dy
= MAX(*dy
, a
->y
- y
);
333 *dx
= MIN(*dx
, (a
->x
+ a
->width
) - (x
+ self
->area
.width
));
334 *dy
= MIN(*dy
, (a
->y
+ a
->height
) - (y
+ self
->area
.height
));
335 /* if in the top half then check this stuff last, will keep the top
336 edge of the menu visible */
338 *dx
= MAX(*dx
, a
->x
- x
);
339 *dy
= MAX(*dy
, a
->y
- y
);
345 static void menu_entry_frame_render(ObMenuEntryFrame
*self
)
347 RrAppearance
*item_a
, *text_a
;
350 ObMenuFrame
*frame
= self
->frame
;
352 switch (self
->entry
->type
) {
353 case OB_MENU_ENTRY_TYPE_NORMAL
:
354 case OB_MENU_ENTRY_TYPE_SUBMENU
:
355 item_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
356 !self
->entry
->data
.normal
.enabled
?
358 (self
== self
->frame
->selected
?
359 ob_rr_theme
->a_menu_disabled_selected
:
360 ob_rr_theme
->a_menu_disabled
) :
362 (self
== self
->frame
->selected
?
363 ob_rr_theme
->a_menu_selected
:
364 ob_rr_theme
->a_menu_normal
));
367 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
368 if (self
->entry
->data
.separator
.label
) {
369 item_a
= ob_rr_theme
->a_menu_title
;
370 th
= ob_rr_theme
->menu_title_height
;
372 item_a
= ob_rr_theme
->a_menu_normal
;
373 th
= ob_rr_theme
->menu_sep_width
+
374 2*ob_rr_theme
->menu_sep_paddingy
;
378 g_assert_not_reached();
381 RECT_SET_SIZE(self
->area
, self
->frame
->inner_w
, th
);
382 XResizeWindow(obt_display
, self
->window
,
383 self
->area
.width
, self
->area
.height
);
384 item_a
->surface
.parent
= self
->frame
->a_items
;
385 item_a
->surface
.parentx
= self
->area
.x
;
386 item_a
->surface
.parenty
= self
->area
.y
;
387 RrPaint(item_a
, self
->window
, self
->area
.width
, self
->area
.height
);
389 switch (self
->entry
->type
) {
390 case OB_MENU_ENTRY_TYPE_NORMAL
:
391 text_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
392 !self
->entry
->data
.normal
.enabled
?
394 (self
== self
->frame
->selected
?
395 ob_rr_theme
->a_menu_text_disabled_selected
:
396 ob_rr_theme
->a_menu_text_disabled
) :
398 (self
== self
->frame
->selected
?
399 ob_rr_theme
->a_menu_text_selected
:
400 ob_rr_theme
->a_menu_text_normal
));
401 text_a
->texture
[0].data
.text
.string
= self
->entry
->data
.normal
.label
;
402 if (self
->entry
->data
.normal
.shortcut
&&
403 (self
->frame
->menu
->show_all_shortcuts
||
404 self
->entry
->data
.normal
.shortcut_always_show
||
405 self
->entry
->data
.normal
.shortcut_position
> 0))
407 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
408 text_a
->texture
[0].data
.text
.shortcut_pos
=
409 self
->entry
->data
.normal
.shortcut_position
;
411 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
413 case OB_MENU_ENTRY_TYPE_SUBMENU
:
414 text_a
= (self
== self
->frame
->selected
?
415 ob_rr_theme
->a_menu_text_selected
:
416 ob_rr_theme
->a_menu_text_normal
);
417 sub
= self
->entry
->data
.submenu
.submenu
;
418 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
419 if (sub
&& sub
->shortcut
&& (self
->frame
->menu
->show_all_shortcuts
||
420 sub
->shortcut_always_show
||
421 sub
->shortcut_position
> 0))
423 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
424 text_a
->texture
[0].data
.text
.shortcut_pos
= sub
->shortcut_position
;
426 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
428 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
429 if (self
->entry
->data
.separator
.label
!= NULL
) {
430 text_a
= ob_rr_theme
->a_menu_text_title
;
431 text_a
->texture
[0].data
.text
.string
=
432 self
->entry
->data
.separator
.label
;
435 text_a
= ob_rr_theme
->a_menu_text_normal
;
438 g_assert_not_reached();
441 switch (self
->entry
->type
) {
442 case OB_MENU_ENTRY_TYPE_NORMAL
:
443 XMoveResizeWindow(obt_display
, self
->text
,
444 self
->frame
->text_x
, PADDING
,
446 ITEM_HEIGHT
- 2*PADDING
);
447 text_a
->surface
.parent
= item_a
;
448 text_a
->surface
.parentx
= self
->frame
->text_x
;
449 text_a
->surface
.parenty
= PADDING
;
450 RrPaint(text_a
, self
->text
, self
->frame
->text_w
,
451 ITEM_HEIGHT
- 2*PADDING
);
453 case OB_MENU_ENTRY_TYPE_SUBMENU
:
454 XMoveResizeWindow(obt_display
, self
->text
,
455 self
->frame
->text_x
, PADDING
,
456 self
->frame
->text_w
- ITEM_HEIGHT
,
457 ITEM_HEIGHT
- 2*PADDING
);
458 text_a
->surface
.parent
= item_a
;
459 text_a
->surface
.parentx
= self
->frame
->text_x
;
460 text_a
->surface
.parenty
= PADDING
;
461 RrPaint(text_a
, self
->text
, self
->frame
->text_w
- ITEM_HEIGHT
,
462 ITEM_HEIGHT
- 2*PADDING
);
464 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
465 if (self
->entry
->data
.separator
.label
!= NULL
) {
466 /* labeled separator */
467 XMoveResizeWindow(obt_display
, self
->text
,
468 ob_rr_theme
->paddingx
, ob_rr_theme
->paddingy
,
469 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
470 ob_rr_theme
->menu_title_height
-
471 2*ob_rr_theme
->paddingy
);
472 text_a
->surface
.parent
= item_a
;
473 text_a
->surface
.parentx
= ob_rr_theme
->paddingx
;
474 text_a
->surface
.parenty
= ob_rr_theme
->paddingy
;
475 RrPaint(text_a
, self
->text
,
476 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
477 ob_rr_theme
->menu_title_height
-
478 2*ob_rr_theme
->paddingy
);
482 /* unlabeled separator */
483 XMoveResizeWindow(obt_display
, self
->text
, 0, 0,
485 ob_rr_theme
->menu_sep_width
+
486 2*ob_rr_theme
->menu_sep_paddingy
);
488 a_sep
->surface
.parent
= item_a
;
489 a_sep
->surface
.parentx
= 0;
490 a_sep
->surface
.parenty
= 0;
491 for (i
= 0; i
< ob_rr_theme
->menu_sep_width
; ++i
) {
492 a_sep
->texture
[i
].data
.lineart
.x1
=
493 ob_rr_theme
->menu_sep_paddingx
;
494 a_sep
->texture
[i
].data
.lineart
.y1
=
495 ob_rr_theme
->menu_sep_paddingy
+ i
;
496 a_sep
->texture
[i
].data
.lineart
.x2
=
497 self
->area
.width
- ob_rr_theme
->menu_sep_paddingx
- 1;
498 a_sep
->texture
[i
].data
.lineart
.y2
=
499 ob_rr_theme
->menu_sep_paddingy
+ i
;
502 RrPaint(a_sep
, self
->text
, self
->area
.width
,
503 ob_rr_theme
->menu_sep_width
+
504 2*ob_rr_theme
->menu_sep_paddingy
);
508 g_assert_not_reached();
511 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
512 self
->entry
->data
.normal
.icon
)
516 XMoveResizeWindow(obt_display
, self
->icon
,
517 PADDING
, frame
->item_margin
.top
,
518 ITEM_HEIGHT
- frame
->item_margin
.top
519 - frame
->item_margin
.bottom
,
520 ITEM_HEIGHT
- frame
->item_margin
.top
521 - frame
->item_margin
.bottom
);
523 clear
= ob_rr_theme
->a_clear_tex
;
524 RrAppearanceClearTextures(clear
);
525 clear
->texture
[0].type
= RR_TEXTURE_IMAGE
;
526 clear
->texture
[0].data
.image
.image
=
527 self
->entry
->data
.normal
.icon
;
528 clear
->texture
[0].data
.image
.alpha
=
529 self
->entry
->data
.normal
.icon_alpha
;
530 clear
->surface
.parent
= item_a
;
531 clear
->surface
.parentx
= PADDING
;
532 clear
->surface
.parenty
= frame
->item_margin
.top
;
533 RrPaint(clear
, self
->icon
,
534 ITEM_HEIGHT
- frame
->item_margin
.top
535 - frame
->item_margin
.bottom
,
536 ITEM_HEIGHT
- frame
->item_margin
.top
537 - frame
->item_margin
.bottom
);
538 XMapWindow(obt_display
, self
->icon
);
539 } else if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
540 self
->entry
->data
.normal
.mask
)
545 XMoveResizeWindow(obt_display
, self
->icon
,
546 PADDING
, frame
->item_margin
.top
,
547 ITEM_HEIGHT
- frame
->item_margin
.top
548 - frame
->item_margin
.bottom
,
549 ITEM_HEIGHT
- frame
->item_margin
.top
550 - frame
->item_margin
.bottom
);
552 clear
= ob_rr_theme
->a_clear_tex
;
553 RrAppearanceClearTextures(clear
);
554 clear
->texture
[0].type
= RR_TEXTURE_MASK
;
555 clear
->texture
[0].data
.mask
.mask
=
556 self
->entry
->data
.normal
.mask
;
558 c
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
559 !self
->entry
->data
.normal
.enabled
?
561 (self
== self
->frame
->selected
?
562 self
->entry
->data
.normal
.mask_disabled_selected_color
:
563 self
->entry
->data
.normal
.mask_disabled_color
) :
565 (self
== self
->frame
->selected
?
566 self
->entry
->data
.normal
.mask_selected_color
:
567 self
->entry
->data
.normal
.mask_normal_color
));
568 clear
->texture
[0].data
.mask
.color
= c
;
570 clear
->surface
.parent
= item_a
;
571 clear
->surface
.parentx
= PADDING
;
572 clear
->surface
.parenty
= frame
->item_margin
.top
;
573 RrPaint(clear
, self
->icon
,
574 ITEM_HEIGHT
- frame
->item_margin
.top
575 - frame
->item_margin
.bottom
,
576 ITEM_HEIGHT
- frame
->item_margin
.top
577 - frame
->item_margin
.bottom
);
578 XMapWindow(obt_display
, self
->icon
);
580 XUnmapWindow(obt_display
, self
->icon
);
582 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
583 RrAppearance
*bullet_a
;
584 XMoveResizeWindow(obt_display
, self
->bullet
,
585 self
->frame
->text_x
+ self
->frame
->text_w
-
586 ITEM_HEIGHT
+ PADDING
, PADDING
,
587 ITEM_HEIGHT
- 2*PADDING
,
588 ITEM_HEIGHT
- 2*PADDING
);
589 bullet_a
= (self
== self
->frame
->selected
?
590 ob_rr_theme
->a_menu_bullet_selected
:
591 ob_rr_theme
->a_menu_bullet_normal
);
592 bullet_a
->surface
.parent
= item_a
;
593 bullet_a
->surface
.parentx
=
594 self
->frame
->text_x
+ self
->frame
->text_w
- ITEM_HEIGHT
+ PADDING
;
595 bullet_a
->surface
.parenty
= PADDING
;
596 RrPaint(bullet_a
, self
->bullet
,
597 ITEM_HEIGHT
- 2*PADDING
,
598 ITEM_HEIGHT
- 2*PADDING
);
599 XMapWindow(obt_display
, self
->bullet
);
601 XUnmapWindow(obt_display
, self
->bullet
);
606 /*! this code is taken from the menu_frame_render. if that changes, this won't
608 static gint
menu_entry_frame_get_height(ObMenuEntryFrame
*self
,
609 gboolean first_entry
,
618 t
= self
->entry
->type
;
620 /* this is the More... entry, it's NORMAL type */
621 t
= OB_MENU_ENTRY_TYPE_NORMAL
;
624 case OB_MENU_ENTRY_TYPE_NORMAL
:
625 case OB_MENU_ENTRY_TYPE_SUBMENU
:
626 h
+= ob_rr_theme
->menu_font_height
;
628 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
629 if (self
->entry
->data
.separator
.label
!= NULL
) {
630 h
+= ob_rr_theme
->menu_title_height
+
631 (ob_rr_theme
->mbwidth
- PADDING
) * 2;
633 /* if the first entry is a labeled separator, then make its border
634 overlap with the menu's outside border */
636 h
-= ob_rr_theme
->mbwidth
;
637 /* if the last entry is a labeled separator, then make its border
638 overlap with the menu's outside border */
640 h
-= ob_rr_theme
->mbwidth
;
642 h
+= ob_rr_theme
->menu_sep_width
+
643 2*ob_rr_theme
->menu_sep_paddingy
- PADDING
* 2;
651 void menu_frame_render(ObMenuFrame
*self
)
654 gint tw
, th
; /* temps */
656 gboolean has_icon
= FALSE
;
660 /* find text dimensions */
662 STRUT_SET(self
->item_margin
, 0, 0, 0, 0);
667 e
= self
->entries
->data
;
668 ob_rr_theme
->a_menu_text_normal
->texture
[0].data
.text
.string
= "";
669 tw
= RrMinWidth(ob_rr_theme
->a_menu_text_normal
);
674 RrMargins(ob_rr_theme
->a_menu_normal
, &l
, &t
, &r
, &b
);
675 STRUT_SET(self
->item_margin
,
676 MAX(self
->item_margin
.left
, l
),
677 MAX(self
->item_margin
.top
, t
),
678 MAX(self
->item_margin
.right
, r
),
679 MAX(self
->item_margin
.bottom
, b
));
680 RrMargins(ob_rr_theme
->a_menu_selected
, &l
, &t
, &r
, &b
);
681 STRUT_SET(self
->item_margin
,
682 MAX(self
->item_margin
.left
, l
),
683 MAX(self
->item_margin
.top
, t
),
684 MAX(self
->item_margin
.right
, r
),
685 MAX(self
->item_margin
.bottom
, b
));
686 RrMargins(ob_rr_theme
->a_menu_disabled
, &l
, &t
, &r
, &b
);
687 STRUT_SET(self
->item_margin
,
688 MAX(self
->item_margin
.left
, l
),
689 MAX(self
->item_margin
.top
, t
),
690 MAX(self
->item_margin
.right
, r
),
691 MAX(self
->item_margin
.bottom
, b
));
692 RrMargins(ob_rr_theme
->a_menu_disabled_selected
, &l
, &t
, &r
, &b
);
693 STRUT_SET(self
->item_margin
,
694 MAX(self
->item_margin
.left
, l
),
695 MAX(self
->item_margin
.top
, t
),
696 MAX(self
->item_margin
.right
, r
),
697 MAX(self
->item_margin
.bottom
, b
));
700 /* render the entries */
702 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
703 RrAppearance
*text_a
;
706 /* if the first entry is a labeled separator, then make its border
707 overlap with the menu's outside border */
708 if (it
== self
->entries
&&
709 e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
710 e
->entry
->data
.separator
.label
)
712 h
-= ob_rr_theme
->mbwidth
;
715 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
716 e
->entry
->data
.separator
.label
)
718 e
->border
= ob_rr_theme
->mbwidth
;
721 RECT_SET_POINT(e
->area
, 0, h
+e
->border
);
722 XMoveWindow(obt_display
, e
->window
,
723 e
->area
.x
-e
->border
, e
->area
.y
-e
->border
);
724 XSetWindowBorderWidth(obt_display
, e
->window
, e
->border
);
725 XSetWindowBorder(obt_display
, e
->window
,
726 RrColorPixel(ob_rr_theme
->menu_border_color
));
728 text_a
= (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
729 !e
->entry
->data
.normal
.enabled
?
731 (e
== self
->selected
?
732 ob_rr_theme
->a_menu_text_disabled_selected
:
733 ob_rr_theme
->a_menu_text_disabled
) :
735 (e
== self
->selected
?
736 ob_rr_theme
->a_menu_text_selected
:
737 ob_rr_theme
->a_menu_text_normal
));
738 switch (e
->entry
->type
) {
739 case OB_MENU_ENTRY_TYPE_NORMAL
:
740 text_a
->texture
[0].data
.text
.string
= e
->entry
->data
.normal
.label
;
741 tw
= RrMinWidth(text_a
);
742 tw
= MIN(tw
, MAX_MENU_WIDTH
);
743 th
= ob_rr_theme
->menu_font_height
;
745 if (e
->entry
->data
.normal
.icon
||
746 e
->entry
->data
.normal
.mask
)
749 case OB_MENU_ENTRY_TYPE_SUBMENU
:
750 sub
= e
->entry
->data
.submenu
.submenu
;
751 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
752 tw
= RrMinWidth(text_a
);
753 tw
= MIN(tw
, MAX_MENU_WIDTH
);
754 th
= ob_rr_theme
->menu_font_height
;
756 if (e
->entry
->data
.normal
.icon
||
757 e
->entry
->data
.normal
.mask
)
760 tw
+= ITEM_HEIGHT
- PADDING
;
762 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
763 if (e
->entry
->data
.separator
.label
!= NULL
) {
764 ob_rr_theme
->a_menu_text_title
->texture
[0].data
.text
.string
=
765 e
->entry
->data
.separator
.label
;
766 tw
= RrMinWidth(ob_rr_theme
->a_menu_text_title
) +
767 2*ob_rr_theme
->paddingx
;
768 tw
= MIN(tw
, MAX_MENU_WIDTH
);
769 th
= ob_rr_theme
->menu_title_height
+
770 (ob_rr_theme
->mbwidth
- PADDING
) *2;
773 th
= ob_rr_theme
->menu_sep_width
+
774 2*ob_rr_theme
->menu_sep_paddingy
- 2*PADDING
;
778 g_assert_not_reached();
786 /* if the last entry is a labeled separator, then make its border
787 overlap with the menu's outside border */
788 it
= g_list_last(self
->entries
);
789 e
= it
? it
->data
: NULL
;
790 if (e
&& e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
791 e
->entry
->data
.separator
.label
)
793 h
-= ob_rr_theme
->mbwidth
;
796 self
->text_x
= PADDING
;
801 w
+= ITEM_HEIGHT
+ PADDING
;
802 self
->text_x
+= ITEM_HEIGHT
+ PADDING
;
809 XResizeWindow(obt_display
, self
->window
, w
, h
);
813 RrPaint(self
->a_items
, self
->window
, w
, h
);
815 for (it
= self
->entries
; it
; it
= g_list_next(it
))
816 menu_entry_frame_render(it
->data
);
818 w
+= ob_rr_theme
->mbwidth
* 2;
819 h
+= ob_rr_theme
->mbwidth
* 2;
821 RECT_SET_SIZE(self
->area
, w
, h
);
826 static void menu_frame_update(ObMenuFrame
*self
)
832 menu_pipe_execute(self
->menu
);
833 menu_find_submenus(self
->menu
);
835 self
->selected
= NULL
;
837 /* start at show_from */
838 mit
= g_list_nth(self
->menu
->entries
, self
->show_from
);
840 /* go through the menu's and frame's entries and connect the frame entries
841 to the menu entries */
842 for (fit
= self
->entries
; mit
&& fit
;
843 mit
= g_list_next(mit
), fit
= g_list_next(fit
))
845 ObMenuEntryFrame
*f
= fit
->data
;
846 f
->entry
= mit
->data
;
849 /* if there are more menu entries than in the frame, add them */
851 ObMenuEntryFrame
*e
= menu_entry_frame_new(mit
->data
, self
);
852 self
->entries
= g_list_append(self
->entries
, e
);
853 mit
= g_list_next(mit
);
856 /* if there are more frame entries than menu entries then get rid of
859 GList
*n
= g_list_next(fit
);
860 menu_entry_frame_free(fit
->data
);
861 self
->entries
= g_list_delete_link(self
->entries
, fit
);
865 /* * make the menu fit on the screen */
867 /* calculate the height of the menu */
869 for (fit
= self
->entries
; fit
; fit
= g_list_next(fit
))
870 h
+= menu_entry_frame_get_height(fit
->data
,
871 fit
== self
->entries
,
872 g_list_next(fit
) == NULL
);
873 /* add the border at the top and bottom */
874 h
+= ob_rr_theme
->mbwidth
* 2;
876 a
= screen_physical_area_monitor(self
->monitor
);
880 gboolean last_entry
= TRUE
;
882 /* take the height of our More... entry into account */
883 h
+= menu_entry_frame_get_height(NULL
, FALSE
, TRUE
);
885 /* start at the end of the entries */
886 flast
= g_list_last(self
->entries
);
888 /* pull out all the entries from the frame that don't
889 fit on the screen, leaving at least 1 though */
890 while (h
> a
->height
&& g_list_previous(flast
) != NULL
) {
891 /* update the height, without this entry */
892 h
-= menu_entry_frame_get_height(flast
->data
, FALSE
, last_entry
);
894 /* destroy the entry we're not displaying */
896 flast
= g_list_previous(flast
);
897 menu_entry_frame_free(tmp
->data
);
898 self
->entries
= g_list_delete_link(self
->entries
, tmp
);
900 /* only the first one that we see is the last entry in the menu */
905 ObMenuEntry
*more_entry
;
906 ObMenuEntryFrame
*more_frame
;
907 /* make the More... menu entry frame which will display in this
909 if self->menu->more_menu is NULL that means that this is already
910 More... menu, so just use ourself.
912 more_entry
= menu_get_more((self
->menu
->more_menu
?
913 self
->menu
->more_menu
:
915 /* continue where we left off */
917 g_list_length(self
->entries
));
918 more_frame
= menu_entry_frame_new(more_entry
, self
);
919 /* make it get deleted when the menu frame goes away */
920 menu_entry_unref(more_entry
);
922 /* add our More... entry to the frame */
923 self
->entries
= g_list_append(self
->entries
, more_frame
);
929 menu_frame_render(self
);
932 static gboolean
menu_frame_is_visible(ObMenuFrame
*self
)
934 return !!(g_list_find(menu_frame_visible
, self
));
937 static gboolean
menu_frame_show(ObMenuFrame
*self
)
941 /* determine if the underlying menu is already visible */
942 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
943 ObMenuFrame
*f
= it
->data
;
944 if (f
->menu
== self
->menu
)
948 if (self
->menu
->update_func
)
949 if (!self
->menu
->update_func(self
, self
->menu
->data
))
953 if (menu_frame_visible
== NULL
) {
954 /* no menus shown yet */
956 /* grab the pointer in such a way as to pass through "owner events"
957 so that we can get enter/leave notifies in the menu. */
958 if (!grab_pointer(TRUE
, FALSE
, OB_CURSOR_POINTER
))
960 if (!grab_keyboard()) {
966 menu_frame_update(self
);
968 menu_frame_visible
= g_list_prepend(menu_frame_visible
, self
);
970 if (self
->menu
->show_func
)
971 self
->menu
->show_func(self
, self
->menu
->data
);
976 gboolean
menu_frame_show_topmenu(ObMenuFrame
*self
, gint x
, gint y
,
981 if (menu_frame_is_visible(self
))
983 if (!menu_frame_show(self
))
986 if (self
->menu
->place_func
)
987 self
->menu
->place_func(self
, &x
, &y
, mouse
, self
->menu
->data
);
989 menu_frame_place_topmenu(self
, &x
, &y
);
991 menu_frame_move(self
, x
, y
);
993 XMapWindow(obt_display
, self
->window
);
995 if (screen_pointer_pos(&px
, &py
)) {
996 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
997 if (e
&& e
->frame
== self
)
1004 /*! Stop hiding an open submenu.
1005 @child The OnMenuFrame of the submenu to be hidden
1007 static void remove_submenu_hide_timeout(ObMenuFrame
*child
)
1009 obt_main_loop_timeout_remove_data(ob_main_loop
, submenu_hide_timeout
,
1013 gboolean
menu_frame_show_submenu(ObMenuFrame
*self
, ObMenuFrame
*parent
,
1014 ObMenuEntryFrame
*parent_entry
)
1019 if (menu_frame_is_visible(self
))
1022 self
->monitor
= parent
->monitor
;
1023 self
->parent
= parent
;
1024 self
->parent_entry
= parent_entry
;
1026 /* set up parent's child to be us */
1027 if ((parent
->child
) != self
) {
1029 menu_frame_hide(parent
->child
);
1030 parent
->child
= self
;
1031 parent
->child_entry
= parent_entry
;
1034 if (!menu_frame_show(self
))
1037 menu_frame_place_submenu(self
, &x
, &y
);
1038 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1041 /*try the other side */
1042 self
->direction_right
= !self
->direction_right
;
1043 menu_frame_place_submenu(self
, &x
, &y
);
1044 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1046 menu_frame_move(self
, x
+ dx
, y
+ dy
);
1048 XMapWindow(obt_display
, self
->window
);
1050 if (screen_pointer_pos(&px
, &py
)) {
1051 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
1052 if (e
&& e
->frame
== self
)
1059 static void menu_frame_hide(ObMenuFrame
*self
)
1061 GList
*it
= g_list_find(menu_frame_visible
, self
);
1062 gulong ignore_start
;
1067 if (self
->menu
->hide_func
)
1068 self
->menu
->hide_func(self
, self
->menu
->data
);
1071 menu_frame_hide(self
->child
);
1074 remove_submenu_hide_timeout(self
);
1076 self
->parent
->child
= NULL
;
1077 self
->parent
->child_entry
= NULL
;
1079 self
->parent
= NULL
;
1080 self
->parent_entry
= NULL
;
1082 menu_frame_visible
= g_list_delete_link(menu_frame_visible
, it
);
1084 if (menu_frame_visible
== NULL
) {
1085 /* last menu shown */
1090 ignore_start
= event_start_ignore_all_enters();
1091 XUnmapWindow(obt_display
, self
->window
);
1092 event_end_ignore_all_enters(ignore_start
);
1094 menu_frame_free(self
);
1097 void menu_frame_hide_all(void)
1101 if (config_submenu_show_delay
) {
1102 /* remove any submenu open requests */
1103 obt_main_loop_timeout_remove(ob_main_loop
, submenu_show_timeout
);
1105 if ((it
= g_list_last(menu_frame_visible
)))
1106 menu_frame_hide(it
->data
);
1109 ObMenuFrame
* menu_frame_under(gint x
, gint y
)
1111 ObMenuFrame
*ret
= NULL
;
1114 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
1115 ObMenuFrame
*f
= it
->data
;
1117 if (RECT_CONTAINS(f
->area
, x
, y
)) {
1125 ObMenuEntryFrame
* menu_entry_frame_under(gint x
, gint y
)
1128 ObMenuEntryFrame
*ret
= NULL
;
1131 if ((frame
= menu_frame_under(x
, y
))) {
1132 x
-= ob_rr_theme
->mbwidth
+ frame
->area
.x
;
1133 y
-= ob_rr_theme
->mbwidth
+ frame
->area
.y
;
1135 for (it
= frame
->entries
; it
; it
= g_list_next(it
)) {
1136 ObMenuEntryFrame
*e
= it
->data
;
1138 if (RECT_CONTAINS(e
->area
, x
, y
)) {
1147 static gboolean
submenu_show_timeout(gpointer data
)
1149 g_assert(menu_frame_visible
);
1150 menu_entry_frame_show_submenu((ObMenuEntryFrame
*)data
);
1154 static gboolean
submenu_hide_timeout(gpointer data
)
1156 g_assert(menu_frame_visible
);
1157 menu_frame_hide((ObMenuFrame
*)data
);
1161 void menu_frame_select(ObMenuFrame
*self
, ObMenuEntryFrame
*entry
,
1164 ObMenuEntryFrame
*old
= self
->selected
;
1165 ObMenuFrame
*oldchild
= self
->child
;
1166 ObMenuEntryFrame
*oldchild_entry
= self
->child_entry
;
1168 /* if the user selected a separator, ignore it and reselect what we had
1170 if (entry
&& entry
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
1174 (!old
|| old
->entry
->type
!= OB_MENU_ENTRY_TYPE_SUBMENU
))
1177 /* if the user left this menu but we have a submenu open, move the
1178 selection back to that submenu */
1179 if (!entry
&& oldchild_entry
)
1180 entry
= oldchild_entry
;
1182 if (config_submenu_show_delay
) {
1183 /* remove any submenu open requests */
1184 obt_main_loop_timeout_remove(ob_main_loop
, submenu_show_timeout
);
1187 self
->selected
= entry
;
1190 menu_entry_frame_render(old
);
1192 if (oldchild_entry
) {
1193 /* There is an open submenu */
1194 if (oldchild_entry
== self
->selected
) {
1195 /* The open submenu has been reselected, so stop hiding the
1197 remove_submenu_hide_timeout(oldchild
);
1199 else if (oldchild_entry
== old
) {
1200 /* The open submenu was selected and is no longer, so hide the
1202 if (immediate
|| config_submenu_hide_delay
== 0)
1203 menu_frame_hide(oldchild
);
1204 else if (config_submenu_hide_delay
> 0)
1205 obt_main_loop_timeout_add(ob_main_loop
,
1206 config_submenu_hide_delay
* 1000,
1207 submenu_hide_timeout
,
1208 oldchild
, g_direct_equal
,
1213 if (self
->selected
) {
1214 menu_entry_frame_render(self
->selected
);
1216 if (self
->selected
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
1217 /* only show if the submenu isn't already showing */
1218 if (oldchild_entry
!= self
->selected
) {
1219 if (immediate
|| config_submenu_hide_delay
== 0)
1220 menu_entry_frame_show_submenu(self
->selected
);
1221 else if (config_submenu_hide_delay
> 0)
1222 obt_main_loop_timeout_add(ob_main_loop
,
1223 config_submenu_show_delay
* 1000,
1224 submenu_show_timeout
,
1225 self
->selected
, g_direct_equal
,
1228 /* hide the grandchildren of this menu. and move the cursor to
1230 else if (immediate
&& self
->child
&& self
->child
->child
) {
1231 menu_frame_hide(self
->child
->child
);
1232 menu_frame_select(self
->child
, NULL
, TRUE
);
1238 void menu_entry_frame_show_submenu(ObMenuEntryFrame
*self
)
1242 if (!self
->entry
->data
.submenu
.submenu
) return;
1244 f
= menu_frame_new(self
->entry
->data
.submenu
.submenu
,
1245 self
->entry
->data
.submenu
.show_from
,
1246 self
->frame
->client
);
1247 /* pass our direction on to our child */
1248 f
->direction_right
= self
->frame
->direction_right
;
1250 menu_frame_show_submenu(f
, self
->frame
, self
);
1253 void menu_entry_frame_execute(ObMenuEntryFrame
*self
, guint state
)
1255 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1256 self
->entry
->data
.normal
.enabled
)
1258 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1260 ObMenuEntry
*entry
= self
->entry
;
1261 ObMenuExecuteFunc func
= self
->frame
->menu
->execute_func
;
1262 gpointer data
= self
->frame
->menu
->data
;
1263 GSList
*acts
= self
->entry
->data
.normal
.actions
;
1264 ObClient
*client
= self
->frame
->client
;
1265 ObMenuFrame
*frame
= self
->frame
;
1266 guint mods
= obt_keyboard_only_modmasks(state
);
1268 /* release grabs before executing the shit */
1269 if (!(mods
& ControlMask
)) {
1270 event_cancel_all_key_grabs();
1275 func(entry
, frame
, client
, state
, data
);
1277 actions_run_acts(acts
, OB_USER_ACTION_MENU_SELECTION
,
1278 state
, -1, -1, 0, OB_FRAME_CONTEXT_NONE
, client
);
1282 void menu_frame_select_previous(ObMenuFrame
*self
)
1284 GList
*it
= NULL
, *start
;
1286 if (self
->entries
) {
1287 start
= it
= g_list_find(self
->entries
, self
->selected
);
1289 ObMenuEntryFrame
*e
;
1291 it
= it
? g_list_previous(it
) : g_list_last(self
->entries
);
1297 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1299 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1304 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);
1307 void menu_frame_select_next(ObMenuFrame
*self
)
1309 GList
*it
= NULL
, *start
;
1311 if (self
->entries
) {
1312 start
= it
= g_list_find(self
->entries
, self
->selected
);
1314 ObMenuEntryFrame
*e
;
1316 it
= it
? g_list_next(it
) : self
->entries
;
1322 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1324 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1329 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);
1332 void menu_frame_select_first(ObMenuFrame
*self
)
1336 if (self
->entries
) {
1337 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
1338 ObMenuEntryFrame
*e
= it
->data
;
1339 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1341 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1345 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);
1348 void menu_frame_select_last(ObMenuFrame
*self
)
1352 if (self
->entries
) {
1353 for (it
= g_list_last(self
->entries
); it
; it
= g_list_previous(it
)) {
1354 ObMenuEntryFrame
*e
= it
->data
;
1355 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1357 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1361 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);