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 "obrender/theme.h"
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
;
43 GHashTable
*menu_frame_map
;
45 static RrAppearance
*a_sep
;
47 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
49 static void menu_entry_frame_free(ObMenuEntryFrame
*self
);
50 static void menu_frame_update(ObMenuFrame
*self
);
51 static gboolean
submenu_show_timeout(gpointer data
);
52 static void menu_frame_hide(ObMenuFrame
*self
);
54 static gboolean
submenu_hide_timeout(gpointer data
);
56 static Window
createWindow(Window parent
, gulong mask
,
57 XSetWindowAttributes
*attrib
)
59 return XCreateWindow(obt_display
, parent
, 0, 0, 1, 1, 0,
60 RrDepth(ob_rr_inst
), InputOutput
,
61 RrVisual(ob_rr_inst
), mask
, attrib
);
64 static void client_dest(ObClient
*client
, gpointer data
)
68 /* menus can be associated with a client, so null those refs since
69 we are disappearing now */
70 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
71 ObMenuFrame
*f
= it
->data
;
72 if (f
->client
== client
)
77 void menu_frame_startup(gboolean reconfig
)
81 a_sep
= RrAppearanceCopy(ob_rr_theme
->a_clear
);
82 RrAppearanceAddTextures(a_sep
, ob_rr_theme
->menu_sep_width
);
83 for (i
= 0; i
< ob_rr_theme
->menu_sep_width
; ++i
) {
84 a_sep
->texture
[i
].type
= RR_TEXTURE_LINE_ART
;
85 a_sep
->texture
[i
].data
.lineart
.color
=
86 ob_rr_theme
->menu_sep_color
;
91 client_add_destroy_notify(client_dest
, NULL
);
92 menu_frame_map
= g_hash_table_new(g_int_hash
, g_int_equal
);
95 void menu_frame_shutdown(gboolean reconfig
)
97 RrAppearanceFree(a_sep
);
101 client_remove_destroy_notify(client_dest
);
102 g_hash_table_destroy(menu_frame_map
);
105 ObMenuFrame
* menu_frame_new(ObMenu
*menu
, guint show_from
, ObClient
*client
)
108 XSetWindowAttributes attr
;
110 self
= g_new0(ObMenuFrame
, 1);
111 self
->obwin
.type
= OB_WINDOW_CLASS_MENUFRAME
;
113 self
->selected
= NULL
;
114 self
->client
= client
;
115 self
->direction_right
= TRUE
;
116 self
->show_from
= show_from
;
118 attr
.event_mask
= FRAME_EVENTMASK
;
119 self
->window
= createWindow(obt_root(ob_screen
),
122 /* make it a popup menu type window */
123 OBT_PROP_SET32(self
->window
, NET_WM_WINDOW_TYPE
, ATOM
,
124 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU
));
126 XSetWindowBorderWidth(obt_display
, self
->window
, ob_rr_theme
->mbwidth
);
127 XSetWindowBorder(obt_display
, self
->window
,
128 RrColorPixel(ob_rr_theme
->menu_border_color
));
130 self
->a_items
= RrAppearanceCopy(ob_rr_theme
->a_menu
);
132 window_add(&self
->window
, MENUFRAME_AS_WINDOW(self
));
133 stacking_add(MENUFRAME_AS_WINDOW(self
));
138 void menu_frame_free(ObMenuFrame
*self
)
141 while (self
->entries
) {
142 menu_entry_frame_free(self
->entries
->data
);
143 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
146 stacking_remove(MENUFRAME_AS_WINDOW(self
));
147 window_remove(self
->window
);
149 RrAppearanceFree(self
->a_items
);
151 XDestroyWindow(obt_display
, self
->window
);
157 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
160 ObMenuEntryFrame
*self
;
161 XSetWindowAttributes attr
;
163 self
= g_new0(ObMenuEntryFrame
, 1);
167 menu_entry_ref(entry
);
169 attr
.event_mask
= ENTRY_EVENTMASK
;
170 self
->window
= createWindow(self
->frame
->window
, CWEventMask
, &attr
);
171 self
->text
= createWindow(self
->window
, 0, NULL
);
172 g_hash_table_insert(menu_frame_map
, &self
->window
, self
);
173 g_hash_table_insert(menu_frame_map
, &self
->text
, self
);
174 if (entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
175 self
->icon
= createWindow(self
->window
, 0, NULL
);
176 g_hash_table_insert(menu_frame_map
, &self
->icon
, self
);
178 if (entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
179 self
->bullet
= createWindow(self
->window
, 0, NULL
);
180 g_hash_table_insert(menu_frame_map
, &self
->bullet
, self
);
183 XMapWindow(obt_display
, self
->window
);
184 XMapWindow(obt_display
, self
->text
);
186 window_add(&self
->window
, MENUFRAME_AS_WINDOW(self
->frame
));
191 static void menu_entry_frame_free(ObMenuEntryFrame
*self
)
194 menu_entry_unref(self
->entry
);
196 window_remove(self
->window
);
198 XDestroyWindow(obt_display
, self
->text
);
199 XDestroyWindow(obt_display
, self
->window
);
200 g_hash_table_remove(menu_frame_map
, &self
->text
);
201 g_hash_table_remove(menu_frame_map
, &self
->window
);
202 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
203 XDestroyWindow(obt_display
, self
->icon
);
204 g_hash_table_remove(menu_frame_map
, &self
->icon
);
206 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
207 XDestroyWindow(obt_display
, self
->bullet
);
208 g_hash_table_remove(menu_frame_map
, &self
->bullet
);
215 void menu_frame_move(ObMenuFrame
*self
, gint x
, gint y
)
217 RECT_SET_POINT(self
->area
, x
, y
);
218 self
->monitor
= screen_find_monitor_point(x
, y
);
219 XMoveWindow(obt_display
, self
->window
, self
->area
.x
, self
->area
.y
);
222 static void menu_frame_place_topmenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
226 if (config_menu_middle
) {
230 *y
-= self
->area
.height
/ 2;
232 /* try to the right of the cursor */
233 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
234 self
->direction_right
= TRUE
;
236 /* try to the left of the cursor */
237 myx
= *x
- self
->area
.width
;
238 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
239 self
->direction_right
= FALSE
;
242 /* if didnt fit on either side so just use what it says */
244 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
245 self
->direction_right
= TRUE
;
255 /* try to the bottom right of the cursor */
256 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
257 self
->direction_right
= TRUE
;
258 if (dx
!= 0 || dy
!= 0) {
259 /* try to the bottom left of the cursor */
260 myx
= *x
- self
->area
.width
;
262 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
263 self
->direction_right
= FALSE
;
265 if (dx
!= 0 || dy
!= 0) {
266 /* try to the top right of the cursor */
268 myy
= *y
- self
->area
.height
;
269 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
270 self
->direction_right
= TRUE
;
272 if (dx
!= 0 || dy
!= 0) {
273 /* try to the top left of the cursor */
274 myx
= *x
- self
->area
.width
;
275 myy
= *y
- self
->area
.height
;
276 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
277 self
->direction_right
= FALSE
;
279 if (dx
!= 0 || dy
!= 0) {
280 /* if didnt fit on either side so just use what it says */
283 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
284 self
->direction_right
= TRUE
;
291 static void menu_frame_place_submenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
293 gint overlapx
, overlapy
;
296 overlapx
= ob_rr_theme
->menu_overlap_x
;
297 overlapy
= ob_rr_theme
->menu_overlap_y
;
298 bwidth
= ob_rr_theme
->mbwidth
;
300 if (self
->direction_right
)
301 *x
= self
->parent
->area
.x
+ self
->parent
->area
.width
-
304 *x
= self
->parent
->area
.x
- self
->area
.width
+ overlapx
+ bwidth
;
306 *y
= self
->parent
->area
.y
+ self
->parent_entry
->area
.y
;
307 if (config_menu_middle
)
308 *y
-= (self
->area
.height
- (bwidth
* 2) - ITEM_HEIGHT
) / 2;
313 void menu_frame_move_on_screen(ObMenuFrame
*self
, gint x
, gint y
,
321 a
= screen_physical_area_monitor(screen_find_monitor_point(x
, y
));
323 half
= g_list_length(self
->entries
) / 2;
324 pos
= g_list_index(self
->entries
, self
->selected
);
326 /* if in the bottom half then check this stuff first, will keep the bottom
327 edge of the menu visible */
329 *dx
= MAX(*dx
, a
->x
- x
);
330 *dy
= MAX(*dy
, a
->y
- y
);
332 *dx
= MIN(*dx
, (a
->x
+ a
->width
) - (x
+ self
->area
.width
));
333 *dy
= MIN(*dy
, (a
->y
+ a
->height
) - (y
+ self
->area
.height
));
334 /* if in the top half then check this stuff last, will keep the top
335 edge of the menu visible */
337 *dx
= MAX(*dx
, a
->x
- x
);
338 *dy
= MAX(*dy
, a
->y
- y
);
344 static void menu_entry_frame_render(ObMenuEntryFrame
*self
)
346 RrAppearance
*item_a
, *text_a
;
349 ObMenuFrame
*frame
= self
->frame
;
351 switch (self
->entry
->type
) {
352 case OB_MENU_ENTRY_TYPE_NORMAL
:
353 case OB_MENU_ENTRY_TYPE_SUBMENU
:
354 item_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
355 !self
->entry
->data
.normal
.enabled
?
357 (self
== self
->frame
->selected
?
358 ob_rr_theme
->a_menu_disabled_selected
:
359 ob_rr_theme
->a_menu_disabled
) :
361 (self
== self
->frame
->selected
?
362 ob_rr_theme
->a_menu_selected
:
363 ob_rr_theme
->a_menu_normal
));
366 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
367 if (self
->entry
->data
.separator
.label
) {
368 item_a
= ob_rr_theme
->a_menu_title
;
369 th
= ob_rr_theme
->menu_title_height
;
371 item_a
= ob_rr_theme
->a_menu_normal
;
372 th
= ob_rr_theme
->menu_sep_width
+
373 2*ob_rr_theme
->menu_sep_paddingy
;
377 g_assert_not_reached();
380 RECT_SET_SIZE(self
->area
, self
->frame
->inner_w
, th
);
381 XResizeWindow(obt_display
, self
->window
,
382 self
->area
.width
, self
->area
.height
);
383 item_a
->surface
.parent
= self
->frame
->a_items
;
384 item_a
->surface
.parentx
= self
->area
.x
;
385 item_a
->surface
.parenty
= self
->area
.y
;
386 RrPaint(item_a
, self
->window
, self
->area
.width
, self
->area
.height
);
388 switch (self
->entry
->type
) {
389 case OB_MENU_ENTRY_TYPE_NORMAL
:
390 text_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
391 !self
->entry
->data
.normal
.enabled
?
393 (self
== self
->frame
->selected
?
394 ob_rr_theme
->a_menu_text_disabled_selected
:
395 ob_rr_theme
->a_menu_text_disabled
) :
397 (self
== self
->frame
->selected
?
398 ob_rr_theme
->a_menu_text_selected
:
399 ob_rr_theme
->a_menu_text_normal
));
400 text_a
->texture
[0].data
.text
.string
= self
->entry
->data
.normal
.label
;
401 if (self
->entry
->data
.normal
.shortcut
&&
402 (self
->frame
->menu
->show_all_shortcuts
||
403 self
->entry
->data
.normal
.shortcut_always_show
||
404 self
->entry
->data
.normal
.shortcut_position
> 0))
406 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
407 text_a
->texture
[0].data
.text
.shortcut_pos
=
408 self
->entry
->data
.normal
.shortcut_position
;
410 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
412 case OB_MENU_ENTRY_TYPE_SUBMENU
:
413 text_a
= (self
== self
->frame
->selected
?
414 ob_rr_theme
->a_menu_text_selected
:
415 ob_rr_theme
->a_menu_text_normal
);
416 sub
= self
->entry
->data
.submenu
.submenu
;
417 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
418 if (sub
&& sub
->shortcut
&& (self
->frame
->menu
->show_all_shortcuts
||
419 sub
->shortcut_always_show
||
420 sub
->shortcut_position
> 0))
422 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
423 text_a
->texture
[0].data
.text
.shortcut_pos
= sub
->shortcut_position
;
425 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
427 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
428 if (self
->entry
->data
.separator
.label
!= NULL
) {
429 text_a
= ob_rr_theme
->a_menu_text_title
;
430 text_a
->texture
[0].data
.text
.string
=
431 self
->entry
->data
.separator
.label
;
434 text_a
= ob_rr_theme
->a_menu_text_normal
;
437 g_assert_not_reached();
440 switch (self
->entry
->type
) {
441 case OB_MENU_ENTRY_TYPE_NORMAL
:
442 XMoveResizeWindow(obt_display
, self
->text
,
443 self
->frame
->text_x
, PADDING
,
445 ITEM_HEIGHT
- 2*PADDING
);
446 text_a
->surface
.parent
= item_a
;
447 text_a
->surface
.parentx
= self
->frame
->text_x
;
448 text_a
->surface
.parenty
= PADDING
;
449 RrPaint(text_a
, self
->text
, self
->frame
->text_w
,
450 ITEM_HEIGHT
- 2*PADDING
);
452 case OB_MENU_ENTRY_TYPE_SUBMENU
:
453 XMoveResizeWindow(obt_display
, self
->text
,
454 self
->frame
->text_x
, PADDING
,
455 self
->frame
->text_w
- ITEM_HEIGHT
,
456 ITEM_HEIGHT
- 2*PADDING
);
457 text_a
->surface
.parent
= item_a
;
458 text_a
->surface
.parentx
= self
->frame
->text_x
;
459 text_a
->surface
.parenty
= PADDING
;
460 RrPaint(text_a
, self
->text
, self
->frame
->text_w
- ITEM_HEIGHT
,
461 ITEM_HEIGHT
- 2*PADDING
);
463 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
464 if (self
->entry
->data
.separator
.label
!= NULL
) {
465 /* labeled separator */
466 XMoveResizeWindow(obt_display
, self
->text
,
467 ob_rr_theme
->paddingx
, ob_rr_theme
->paddingy
,
468 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
469 ob_rr_theme
->menu_title_height
-
470 2*ob_rr_theme
->paddingy
);
471 text_a
->surface
.parent
= item_a
;
472 text_a
->surface
.parentx
= ob_rr_theme
->paddingx
;
473 text_a
->surface
.parenty
= ob_rr_theme
->paddingy
;
474 RrPaint(text_a
, self
->text
,
475 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
476 ob_rr_theme
->menu_title_height
-
477 2*ob_rr_theme
->paddingy
);
481 /* unlabeled separator */
482 XMoveResizeWindow(obt_display
, self
->text
, 0, 0,
484 ob_rr_theme
->menu_sep_width
+
485 2*ob_rr_theme
->menu_sep_paddingy
);
487 a_sep
->surface
.parent
= item_a
;
488 a_sep
->surface
.parentx
= 0;
489 a_sep
->surface
.parenty
= 0;
490 for (i
= 0; i
< ob_rr_theme
->menu_sep_width
; ++i
) {
491 a_sep
->texture
[i
].data
.lineart
.x1
=
492 ob_rr_theme
->menu_sep_paddingx
;
493 a_sep
->texture
[i
].data
.lineart
.y1
=
494 ob_rr_theme
->menu_sep_paddingy
+ i
;
495 a_sep
->texture
[i
].data
.lineart
.x2
=
496 self
->area
.width
- ob_rr_theme
->menu_sep_paddingx
- 1;
497 a_sep
->texture
[i
].data
.lineart
.y2
=
498 ob_rr_theme
->menu_sep_paddingy
+ i
;
501 RrPaint(a_sep
, self
->text
, self
->area
.width
,
502 ob_rr_theme
->menu_sep_width
+
503 2*ob_rr_theme
->menu_sep_paddingy
);
507 g_assert_not_reached();
510 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
511 self
->entry
->data
.normal
.icon
)
515 XMoveResizeWindow(obt_display
, self
->icon
,
516 PADDING
, frame
->item_margin
.top
,
517 ITEM_HEIGHT
- frame
->item_margin
.top
518 - frame
->item_margin
.bottom
,
519 ITEM_HEIGHT
- frame
->item_margin
.top
520 - frame
->item_margin
.bottom
);
522 clear
= ob_rr_theme
->a_clear_tex
;
523 RrAppearanceClearTextures(clear
);
524 clear
->texture
[0].type
= RR_TEXTURE_IMAGE
;
525 clear
->texture
[0].data
.image
.image
=
526 self
->entry
->data
.normal
.icon
;
527 clear
->texture
[0].data
.image
.alpha
=
528 self
->entry
->data
.normal
.icon_alpha
;
529 clear
->surface
.parent
= item_a
;
530 clear
->surface
.parentx
= PADDING
;
531 clear
->surface
.parenty
= frame
->item_margin
.top
;
532 RrPaint(clear
, self
->icon
,
533 ITEM_HEIGHT
- frame
->item_margin
.top
534 - frame
->item_margin
.bottom
,
535 ITEM_HEIGHT
- frame
->item_margin
.top
536 - frame
->item_margin
.bottom
);
537 XMapWindow(obt_display
, self
->icon
);
538 } else if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
539 self
->entry
->data
.normal
.mask
)
544 XMoveResizeWindow(obt_display
, self
->icon
,
545 PADDING
, frame
->item_margin
.top
,
546 ITEM_HEIGHT
- frame
->item_margin
.top
547 - frame
->item_margin
.bottom
,
548 ITEM_HEIGHT
- frame
->item_margin
.top
549 - frame
->item_margin
.bottom
);
551 clear
= ob_rr_theme
->a_clear_tex
;
552 RrAppearanceClearTextures(clear
);
553 clear
->texture
[0].type
= RR_TEXTURE_MASK
;
554 clear
->texture
[0].data
.mask
.mask
=
555 self
->entry
->data
.normal
.mask
;
557 c
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
558 !self
->entry
->data
.normal
.enabled
?
560 (self
== self
->frame
->selected
?
561 self
->entry
->data
.normal
.mask_disabled_selected_color
:
562 self
->entry
->data
.normal
.mask_disabled_color
) :
564 (self
== self
->frame
->selected
?
565 self
->entry
->data
.normal
.mask_selected_color
:
566 self
->entry
->data
.normal
.mask_normal_color
));
567 clear
->texture
[0].data
.mask
.color
= c
;
569 clear
->surface
.parent
= item_a
;
570 clear
->surface
.parentx
= PADDING
;
571 clear
->surface
.parenty
= frame
->item_margin
.top
;
572 RrPaint(clear
, self
->icon
,
573 ITEM_HEIGHT
- frame
->item_margin
.top
574 - frame
->item_margin
.bottom
,
575 ITEM_HEIGHT
- frame
->item_margin
.top
576 - frame
->item_margin
.bottom
);
577 XMapWindow(obt_display
, self
->icon
);
579 XUnmapWindow(obt_display
, self
->icon
);
581 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
582 RrAppearance
*bullet_a
;
583 XMoveResizeWindow(obt_display
, self
->bullet
,
584 self
->frame
->text_x
+ self
->frame
->text_w
-
585 ITEM_HEIGHT
+ PADDING
, PADDING
,
586 ITEM_HEIGHT
- 2*PADDING
,
587 ITEM_HEIGHT
- 2*PADDING
);
588 bullet_a
= (self
== self
->frame
->selected
?
589 ob_rr_theme
->a_menu_bullet_selected
:
590 ob_rr_theme
->a_menu_bullet_normal
);
591 bullet_a
->surface
.parent
= item_a
;
592 bullet_a
->surface
.parentx
=
593 self
->frame
->text_x
+ self
->frame
->text_w
- ITEM_HEIGHT
+ PADDING
;
594 bullet_a
->surface
.parenty
= PADDING
;
595 RrPaint(bullet_a
, self
->bullet
,
596 ITEM_HEIGHT
- 2*PADDING
,
597 ITEM_HEIGHT
- 2*PADDING
);
598 XMapWindow(obt_display
, self
->bullet
);
600 XUnmapWindow(obt_display
, self
->bullet
);
605 /*! this code is taken from the menu_frame_render. if that changes, this won't
607 static gint
menu_entry_frame_get_height(ObMenuEntryFrame
*self
,
608 gboolean first_entry
,
617 t
= self
->entry
->type
;
619 /* this is the More... entry, it's NORMAL type */
620 t
= OB_MENU_ENTRY_TYPE_NORMAL
;
623 case OB_MENU_ENTRY_TYPE_NORMAL
:
624 case OB_MENU_ENTRY_TYPE_SUBMENU
:
625 h
+= ob_rr_theme
->menu_font_height
;
627 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
628 if (self
->entry
->data
.separator
.label
!= NULL
) {
629 h
+= ob_rr_theme
->menu_title_height
+
630 (ob_rr_theme
->mbwidth
- PADDING
) * 2;
632 /* if the first entry is a labeled separator, then make its border
633 overlap with the menu's outside border */
635 h
-= ob_rr_theme
->mbwidth
;
636 /* if the last entry is a labeled separator, then make its border
637 overlap with the menu's outside border */
639 h
-= ob_rr_theme
->mbwidth
;
641 h
+= ob_rr_theme
->menu_sep_width
+
642 2*ob_rr_theme
->menu_sep_paddingy
- PADDING
* 2;
650 void menu_frame_render(ObMenuFrame
*self
)
653 gint tw
, th
; /* temps */
655 gboolean has_icon
= FALSE
;
659 /* find text dimensions */
661 STRUT_SET(self
->item_margin
, 0, 0, 0, 0);
666 e
= self
->entries
->data
;
667 ob_rr_theme
->a_menu_text_normal
->texture
[0].data
.text
.string
= "";
668 tw
= RrMinWidth(ob_rr_theme
->a_menu_text_normal
);
673 RrMargins(ob_rr_theme
->a_menu_normal
, &l
, &t
, &r
, &b
);
674 STRUT_SET(self
->item_margin
,
675 MAX(self
->item_margin
.left
, l
),
676 MAX(self
->item_margin
.top
, t
),
677 MAX(self
->item_margin
.right
, r
),
678 MAX(self
->item_margin
.bottom
, b
));
679 RrMargins(ob_rr_theme
->a_menu_selected
, &l
, &t
, &r
, &b
);
680 STRUT_SET(self
->item_margin
,
681 MAX(self
->item_margin
.left
, l
),
682 MAX(self
->item_margin
.top
, t
),
683 MAX(self
->item_margin
.right
, r
),
684 MAX(self
->item_margin
.bottom
, b
));
685 RrMargins(ob_rr_theme
->a_menu_disabled
, &l
, &t
, &r
, &b
);
686 STRUT_SET(self
->item_margin
,
687 MAX(self
->item_margin
.left
, l
),
688 MAX(self
->item_margin
.top
, t
),
689 MAX(self
->item_margin
.right
, r
),
690 MAX(self
->item_margin
.bottom
, b
));
691 RrMargins(ob_rr_theme
->a_menu_disabled_selected
, &l
, &t
, &r
, &b
);
692 STRUT_SET(self
->item_margin
,
693 MAX(self
->item_margin
.left
, l
),
694 MAX(self
->item_margin
.top
, t
),
695 MAX(self
->item_margin
.right
, r
),
696 MAX(self
->item_margin
.bottom
, b
));
699 /* render the entries */
701 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
702 RrAppearance
*text_a
;
705 /* if the first entry is a labeled separator, then make its border
706 overlap with the menu's outside border */
707 if (it
== self
->entries
&&
708 e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
709 e
->entry
->data
.separator
.label
)
711 h
-= ob_rr_theme
->mbwidth
;
714 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
715 e
->entry
->data
.separator
.label
)
717 e
->border
= ob_rr_theme
->mbwidth
;
720 RECT_SET_POINT(e
->area
, 0, h
+e
->border
);
721 XMoveWindow(obt_display
, e
->window
,
722 e
->area
.x
-e
->border
, e
->area
.y
-e
->border
);
723 XSetWindowBorderWidth(obt_display
, e
->window
, e
->border
);
724 XSetWindowBorder(obt_display
, e
->window
,
725 RrColorPixel(ob_rr_theme
->menu_border_color
));
727 text_a
= (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
728 !e
->entry
->data
.normal
.enabled
?
730 (e
== self
->selected
?
731 ob_rr_theme
->a_menu_text_disabled_selected
:
732 ob_rr_theme
->a_menu_text_disabled
) :
734 (e
== self
->selected
?
735 ob_rr_theme
->a_menu_text_selected
:
736 ob_rr_theme
->a_menu_text_normal
));
737 switch (e
->entry
->type
) {
738 case OB_MENU_ENTRY_TYPE_NORMAL
:
739 text_a
->texture
[0].data
.text
.string
= e
->entry
->data
.normal
.label
;
740 tw
= RrMinWidth(text_a
);
741 tw
= MIN(tw
, MAX_MENU_WIDTH
);
742 th
= ob_rr_theme
->menu_font_height
;
744 if (e
->entry
->data
.normal
.icon
||
745 e
->entry
->data
.normal
.mask
)
748 case OB_MENU_ENTRY_TYPE_SUBMENU
:
749 sub
= e
->entry
->data
.submenu
.submenu
;
750 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
751 tw
= RrMinWidth(text_a
);
752 tw
= MIN(tw
, MAX_MENU_WIDTH
);
753 th
= ob_rr_theme
->menu_font_height
;
755 if (e
->entry
->data
.normal
.icon
||
756 e
->entry
->data
.normal
.mask
)
759 tw
+= ITEM_HEIGHT
- PADDING
;
761 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
762 if (e
->entry
->data
.separator
.label
!= NULL
) {
763 ob_rr_theme
->a_menu_text_title
->texture
[0].data
.text
.string
=
764 e
->entry
->data
.separator
.label
;
765 tw
= RrMinWidth(ob_rr_theme
->a_menu_text_title
) +
766 2*ob_rr_theme
->paddingx
;
767 tw
= MIN(tw
, MAX_MENU_WIDTH
);
768 th
= ob_rr_theme
->menu_title_height
+
769 (ob_rr_theme
->mbwidth
- PADDING
) *2;
772 th
= ob_rr_theme
->menu_sep_width
+
773 2*ob_rr_theme
->menu_sep_paddingy
- 2*PADDING
;
777 g_assert_not_reached();
785 /* if the last entry is a labeled separator, then make its border
786 overlap with the menu's outside border */
787 it
= g_list_last(self
->entries
);
788 e
= it
? it
->data
: NULL
;
789 if (e
&& e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
790 e
->entry
->data
.separator
.label
)
792 h
-= ob_rr_theme
->mbwidth
;
795 self
->text_x
= PADDING
;
800 w
+= ITEM_HEIGHT
+ PADDING
;
801 self
->text_x
+= ITEM_HEIGHT
+ PADDING
;
808 XResizeWindow(obt_display
, self
->window
, w
, h
);
812 RrPaint(self
->a_items
, self
->window
, w
, h
);
814 for (it
= self
->entries
; it
; it
= g_list_next(it
))
815 menu_entry_frame_render(it
->data
);
817 w
+= ob_rr_theme
->mbwidth
* 2;
818 h
+= ob_rr_theme
->mbwidth
* 2;
820 RECT_SET_SIZE(self
->area
, w
, h
);
825 static void menu_frame_update(ObMenuFrame
*self
)
831 menu_pipe_execute(self
->menu
);
832 menu_find_submenus(self
->menu
);
834 self
->selected
= NULL
;
836 /* start at show_from */
837 mit
= g_list_nth(self
->menu
->entries
, self
->show_from
);
839 /* go through the menu's and frame's entries and connect the frame entries
840 to the menu entries */
841 for (fit
= self
->entries
; mit
&& fit
;
842 mit
= g_list_next(mit
), fit
= g_list_next(fit
))
844 ObMenuEntryFrame
*f
= fit
->data
;
845 f
->entry
= mit
->data
;
848 /* if there are more menu entries than in the frame, add them */
850 ObMenuEntryFrame
*e
= menu_entry_frame_new(mit
->data
, self
);
851 self
->entries
= g_list_append(self
->entries
, e
);
852 mit
= g_list_next(mit
);
855 /* if there are more frame entries than menu entries then get rid of
858 GList
*n
= g_list_next(fit
);
859 menu_entry_frame_free(fit
->data
);
860 self
->entries
= g_list_delete_link(self
->entries
, fit
);
864 /* * make the menu fit on the screen */
866 /* calculate the height of the menu */
868 for (fit
= self
->entries
; fit
; fit
= g_list_next(fit
))
869 h
+= menu_entry_frame_get_height(fit
->data
,
870 fit
== self
->entries
,
871 g_list_next(fit
) == NULL
);
872 /* add the border at the top and bottom */
873 h
+= ob_rr_theme
->mbwidth
* 2;
875 a
= screen_physical_area_monitor(self
->monitor
);
879 gboolean last_entry
= TRUE
;
881 /* take the height of our More... entry into account */
882 h
+= menu_entry_frame_get_height(NULL
, FALSE
, TRUE
);
884 /* start at the end of the entries */
885 flast
= g_list_last(self
->entries
);
887 /* pull out all the entries from the frame that don't
888 fit on the screen, leaving at least 1 though */
889 while (h
> a
->height
&& g_list_previous(flast
) != NULL
) {
890 /* update the height, without this entry */
891 h
-= menu_entry_frame_get_height(flast
->data
, FALSE
, last_entry
);
893 /* destroy the entry we're not displaying */
895 flast
= g_list_previous(flast
);
896 menu_entry_frame_free(tmp
->data
);
897 self
->entries
= g_list_delete_link(self
->entries
, tmp
);
899 /* only the first one that we see is the last entry in the menu */
904 ObMenuEntry
*more_entry
;
905 ObMenuEntryFrame
*more_frame
;
906 /* make the More... menu entry frame which will display in this
908 if self->menu->more_menu is NULL that means that this is already
909 More... menu, so just use ourself.
911 more_entry
= menu_get_more((self
->menu
->more_menu
?
912 self
->menu
->more_menu
:
914 /* continue where we left off */
916 g_list_length(self
->entries
));
917 more_frame
= menu_entry_frame_new(more_entry
, self
);
918 /* make it get deleted when the menu frame goes away */
919 menu_entry_unref(more_entry
);
921 /* add our More... entry to the frame */
922 self
->entries
= g_list_append(self
->entries
, more_frame
);
928 menu_frame_render(self
);
931 static gboolean
menu_frame_is_visible(ObMenuFrame
*self
)
933 return !!(g_list_find(menu_frame_visible
, self
));
936 static gboolean
menu_frame_show(ObMenuFrame
*self
)
940 /* determine if the underlying menu is already visible */
941 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
942 ObMenuFrame
*f
= it
->data
;
943 if (f
->menu
== self
->menu
)
947 if (self
->menu
->update_func
)
948 if (!self
->menu
->update_func(self
, self
->menu
->data
))
952 if (menu_frame_visible
== NULL
) {
953 /* no menus shown yet */
955 /* grab the pointer in such a way as to pass through "owner events"
956 so that we can get enter/leave notifies in the menu. */
957 if (!grab_pointer(TRUE
, FALSE
, OB_CURSOR_POINTER
))
959 if (!grab_keyboard()) {
965 menu_frame_update(self
);
967 menu_frame_visible
= g_list_prepend(menu_frame_visible
, self
);
969 if (self
->menu
->show_func
)
970 self
->menu
->show_func(self
, self
->menu
->data
);
975 gboolean
menu_frame_show_topmenu(ObMenuFrame
*self
, gint x
, gint y
,
980 if (menu_frame_is_visible(self
))
982 if (!menu_frame_show(self
))
985 if (self
->menu
->place_func
)
986 self
->menu
->place_func(self
, &x
, &y
, mouse
, self
->menu
->data
);
988 menu_frame_place_topmenu(self
, &x
, &y
);
990 menu_frame_move(self
, x
, y
);
992 XMapWindow(obt_display
, self
->window
);
994 if (screen_pointer_pos(&px
, &py
)) {
995 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
996 if (e
&& e
->frame
== self
)
1003 /*! Stop hiding an open submenu.
1004 @child The OnMenuFrame of the submenu to be hidden
1006 static void remove_submenu_hide_timeout(ObMenuFrame
*child
)
1008 obt_main_loop_timeout_remove_data(ob_main_loop
, submenu_hide_timeout
,
1012 gboolean
menu_frame_show_submenu(ObMenuFrame
*self
, ObMenuFrame
*parent
,
1013 ObMenuEntryFrame
*parent_entry
)
1018 if (menu_frame_is_visible(self
))
1021 self
->monitor
= parent
->monitor
;
1022 self
->parent
= parent
;
1023 self
->parent_entry
= parent_entry
;
1025 /* set up parent's child to be us */
1026 if ((parent
->child
) != self
) {
1028 menu_frame_hide(parent
->child
);
1029 parent
->child
= self
;
1030 parent
->child_entry
= parent_entry
;
1033 if (!menu_frame_show(self
))
1036 menu_frame_place_submenu(self
, &x
, &y
);
1037 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1040 /*try the other side */
1041 self
->direction_right
= !self
->direction_right
;
1042 menu_frame_place_submenu(self
, &x
, &y
);
1043 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1045 menu_frame_move(self
, x
+ dx
, y
+ dy
);
1047 XMapWindow(obt_display
, self
->window
);
1049 if (screen_pointer_pos(&px
, &py
)) {
1050 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
1051 if (e
&& e
->frame
== self
)
1058 static void menu_frame_hide(ObMenuFrame
*self
)
1060 GList
*it
= g_list_find(menu_frame_visible
, self
);
1061 gulong ignore_start
;
1066 if (self
->menu
->hide_func
)
1067 self
->menu
->hide_func(self
, self
->menu
->data
);
1070 menu_frame_hide(self
->child
);
1073 remove_submenu_hide_timeout(self
);
1075 self
->parent
->child
= NULL
;
1076 self
->parent
->child_entry
= NULL
;
1078 self
->parent
= NULL
;
1079 self
->parent_entry
= NULL
;
1081 menu_frame_visible
= g_list_delete_link(menu_frame_visible
, it
);
1083 if (menu_frame_visible
== NULL
) {
1084 /* last menu shown */
1089 ignore_start
= event_start_ignore_all_enters();
1090 XUnmapWindow(obt_display
, self
->window
);
1091 event_end_ignore_all_enters(ignore_start
);
1093 menu_frame_free(self
);
1096 void menu_frame_hide_all(void)
1100 if (config_submenu_show_delay
) {
1101 /* remove any submenu open requests */
1102 obt_main_loop_timeout_remove(ob_main_loop
, submenu_show_timeout
);
1104 if ((it
= g_list_last(menu_frame_visible
)))
1105 menu_frame_hide(it
->data
);
1108 ObMenuFrame
* menu_frame_under(gint x
, gint y
)
1110 ObMenuFrame
*ret
= NULL
;
1113 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
1114 ObMenuFrame
*f
= it
->data
;
1116 if (RECT_CONTAINS(f
->area
, x
, y
)) {
1124 ObMenuEntryFrame
* menu_entry_frame_under(gint x
, gint y
)
1127 ObMenuEntryFrame
*ret
= NULL
;
1130 if ((frame
= menu_frame_under(x
, y
))) {
1131 x
-= ob_rr_theme
->mbwidth
+ frame
->area
.x
;
1132 y
-= ob_rr_theme
->mbwidth
+ frame
->area
.y
;
1134 for (it
= frame
->entries
; it
; it
= g_list_next(it
)) {
1135 ObMenuEntryFrame
*e
= it
->data
;
1137 if (RECT_CONTAINS(e
->area
, x
, y
)) {
1146 static gboolean
submenu_show_timeout(gpointer data
)
1148 g_assert(menu_frame_visible
);
1149 menu_entry_frame_show_submenu((ObMenuEntryFrame
*)data
);
1153 static gboolean
submenu_hide_timeout(gpointer data
)
1155 g_assert(menu_frame_visible
);
1156 menu_frame_hide((ObMenuFrame
*)data
);
1160 void menu_frame_select(ObMenuFrame
*self
, ObMenuEntryFrame
*entry
,
1163 ObMenuEntryFrame
*old
= self
->selected
;
1164 ObMenuFrame
*oldchild
= self
->child
;
1165 ObMenuEntryFrame
*oldchild_entry
= self
->child_entry
;
1167 /* if the user selected a separator, ignore it and reselect what we had
1169 if (entry
&& entry
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
1173 (!old
|| old
->entry
->type
!= OB_MENU_ENTRY_TYPE_SUBMENU
))
1176 /* if the user left this menu but we have a submenu open, move the
1177 selection back to that submenu */
1178 if (!entry
&& oldchild_entry
)
1179 entry
= oldchild_entry
;
1181 if (config_submenu_show_delay
) {
1182 /* remove any submenu open requests */
1183 obt_main_loop_timeout_remove(ob_main_loop
, submenu_show_timeout
);
1186 self
->selected
= entry
;
1189 menu_entry_frame_render(old
);
1191 if (oldchild_entry
) {
1192 /* There is an open submenu */
1193 if (oldchild_entry
== self
->selected
) {
1194 /* The open submenu has been reselected, so stop hiding the
1196 remove_submenu_hide_timeout(oldchild
);
1198 else if (oldchild_entry
== old
) {
1199 /* The open submenu was selected and is no longer, so hide the
1201 if (immediate
|| config_submenu_hide_delay
== 0)
1202 menu_frame_hide(oldchild
);
1203 else if (config_submenu_hide_delay
> 0)
1204 obt_main_loop_timeout_add(ob_main_loop
,
1205 config_submenu_hide_delay
* 1000,
1206 submenu_hide_timeout
,
1207 oldchild
, g_direct_equal
,
1212 if (self
->selected
) {
1213 menu_entry_frame_render(self
->selected
);
1215 if (self
->selected
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
1216 /* only show if the submenu isn't already showing */
1217 if (oldchild_entry
!= self
->selected
) {
1218 if (immediate
|| config_submenu_hide_delay
== 0)
1219 menu_entry_frame_show_submenu(self
->selected
);
1220 else if (config_submenu_hide_delay
> 0)
1221 obt_main_loop_timeout_add(ob_main_loop
,
1222 config_submenu_show_delay
* 1000,
1223 submenu_show_timeout
,
1224 self
->selected
, g_direct_equal
,
1227 /* hide the grandchildren of this menu. and move the cursor to
1229 else if (immediate
&& self
->child
&& self
->child
->child
) {
1230 menu_frame_hide(self
->child
->child
);
1231 menu_frame_select(self
->child
, NULL
, TRUE
);
1237 void menu_entry_frame_show_submenu(ObMenuEntryFrame
*self
)
1241 if (!self
->entry
->data
.submenu
.submenu
) return;
1243 f
= menu_frame_new(self
->entry
->data
.submenu
.submenu
,
1244 self
->entry
->data
.submenu
.show_from
,
1245 self
->frame
->client
);
1246 /* pass our direction on to our child */
1247 f
->direction_right
= self
->frame
->direction_right
;
1249 menu_frame_show_submenu(f
, self
->frame
, self
);
1252 void menu_entry_frame_execute(ObMenuEntryFrame
*self
, guint state
)
1254 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1255 self
->entry
->data
.normal
.enabled
)
1257 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1259 ObMenuEntry
*entry
= self
->entry
;
1260 ObMenuExecuteFunc func
= self
->frame
->menu
->execute_func
;
1261 gpointer data
= self
->frame
->menu
->data
;
1262 GSList
*acts
= self
->entry
->data
.normal
.actions
;
1263 ObClient
*client
= self
->frame
->client
;
1264 ObMenuFrame
*frame
= self
->frame
;
1266 /* release grabs before executing the shit */
1267 if (!(state
& ControlMask
)) {
1268 event_cancel_all_key_grabs();
1273 func(entry
, frame
, client
, state
, data
);
1275 actions_run_acts(acts
, OB_USER_ACTION_MENU_SELECTION
,
1276 state
, -1, -1, 0, OB_FRAME_CONTEXT_NONE
, client
);
1280 void menu_frame_select_previous(ObMenuFrame
*self
)
1282 GList
*it
= NULL
, *start
;
1284 if (self
->entries
) {
1285 start
= it
= g_list_find(self
->entries
, self
->selected
);
1287 ObMenuEntryFrame
*e
;
1289 it
= it
? g_list_previous(it
) : g_list_last(self
->entries
);
1295 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1297 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1302 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);
1305 void menu_frame_select_next(ObMenuFrame
*self
)
1307 GList
*it
= NULL
, *start
;
1309 if (self
->entries
) {
1310 start
= it
= g_list_find(self
->entries
, self
->selected
);
1312 ObMenuEntryFrame
*e
;
1314 it
= it
? g_list_next(it
) : self
->entries
;
1320 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1322 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1327 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);
1330 void menu_frame_select_first(ObMenuFrame
*self
)
1334 if (self
->entries
) {
1335 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
1336 ObMenuEntryFrame
*e
= it
->data
;
1337 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1339 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1343 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);
1346 void menu_frame_select_last(ObMenuFrame
*self
)
1350 if (self
->entries
) {
1351 for (it
= g_list_last(self
->entries
); it
; it
= g_list_previous(it
)) {
1352 ObMenuEntryFrame
*e
= it
->data
;
1353 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1355 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1359 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);