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 void menu_frame_startup(gboolean reconfig
)
68 a_sep
= RrAppearanceCopy(ob_rr_theme
->a_clear
);
69 RrAppearanceAddTextures(a_sep
, ob_rr_theme
->menu_sep_width
);
70 for (i
= 0; i
< ob_rr_theme
->menu_sep_width
; ++i
) {
71 a_sep
->texture
[i
].type
= RR_TEXTURE_LINE_ART
;
72 a_sep
->texture
[i
].data
.lineart
.color
=
73 ob_rr_theme
->menu_sep_color
;
78 menu_frame_map
= g_hash_table_new(g_int_hash
, g_int_equal
);
81 void menu_frame_shutdown(gboolean reconfig
)
83 RrAppearanceFree(a_sep
);
87 g_hash_table_destroy(menu_frame_map
);
90 ObMenuFrame
* menu_frame_new(ObMenu
*menu
, guint show_from
, ObClient
*client
)
93 XSetWindowAttributes attr
;
95 self
= g_new0(ObMenuFrame
, 1);
96 self
->obwin
.type
= OB_WINDOW_CLASS_MENUFRAME
;
98 self
->selected
= NULL
;
99 self
->client
= client
;
100 self
->direction_right
= TRUE
;
101 self
->show_from
= show_from
;
103 attr
.event_mask
= FRAME_EVENTMASK
;
104 self
->window
= createWindow(obt_root(ob_screen
),
107 /* make it a popup menu type window */
108 OBT_PROP_SET32(self
->window
, NET_WM_WINDOW_TYPE
, ATOM
,
109 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU
));
111 XSetWindowBorderWidth(obt_display
, self
->window
, ob_rr_theme
->mbwidth
);
112 XSetWindowBorder(obt_display
, self
->window
,
113 RrColorPixel(ob_rr_theme
->menu_border_color
));
115 self
->a_items
= RrAppearanceCopy(ob_rr_theme
->a_menu
);
117 window_add(&self
->window
, MENUFRAME_AS_WINDOW(self
));
118 stacking_add(MENUFRAME_AS_WINDOW(self
));
123 void menu_frame_free(ObMenuFrame
*self
)
126 while (self
->entries
) {
127 menu_entry_frame_free(self
->entries
->data
);
128 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
131 stacking_remove(MENUFRAME_AS_WINDOW(self
));
132 window_remove(self
->window
);
134 RrAppearanceFree(self
->a_items
);
136 XDestroyWindow(obt_display
, self
->window
);
142 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
145 ObMenuEntryFrame
*self
;
146 XSetWindowAttributes attr
;
148 self
= g_new0(ObMenuEntryFrame
, 1);
152 menu_entry_ref(entry
);
154 attr
.event_mask
= ENTRY_EVENTMASK
;
155 self
->window
= createWindow(self
->frame
->window
, CWEventMask
, &attr
);
156 self
->text
= createWindow(self
->window
, 0, NULL
);
157 g_hash_table_insert(menu_frame_map
, &self
->window
, self
);
158 g_hash_table_insert(menu_frame_map
, &self
->text
, self
);
159 if (entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
160 self
->icon
= createWindow(self
->window
, 0, NULL
);
161 g_hash_table_insert(menu_frame_map
, &self
->icon
, self
);
163 if (entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
164 self
->bullet
= createWindow(self
->window
, 0, NULL
);
165 g_hash_table_insert(menu_frame_map
, &self
->bullet
, self
);
168 XMapWindow(obt_display
, self
->window
);
169 XMapWindow(obt_display
, self
->text
);
171 window_add(&self
->window
, MENUFRAME_AS_WINDOW(self
->frame
));
176 static void menu_entry_frame_free(ObMenuEntryFrame
*self
)
179 menu_entry_unref(self
->entry
);
181 window_remove(self
->window
);
183 XDestroyWindow(obt_display
, self
->text
);
184 XDestroyWindow(obt_display
, self
->window
);
185 g_hash_table_remove(menu_frame_map
, &self
->text
);
186 g_hash_table_remove(menu_frame_map
, &self
->window
);
187 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
188 XDestroyWindow(obt_display
, self
->icon
);
189 g_hash_table_remove(menu_frame_map
, &self
->icon
);
191 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
192 XDestroyWindow(obt_display
, self
->bullet
);
193 g_hash_table_remove(menu_frame_map
, &self
->bullet
);
200 void menu_frame_move(ObMenuFrame
*self
, gint x
, gint y
)
202 RECT_SET_POINT(self
->area
, x
, y
);
203 self
->monitor
= screen_find_monitor_point(x
, y
);
204 XMoveWindow(obt_display
, self
->window
, self
->area
.x
, self
->area
.y
);
207 static void menu_frame_place_topmenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
211 if (config_menu_middle
) {
215 *y
-= self
->area
.height
/ 2;
217 /* try to the right of the cursor */
218 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
219 self
->direction_right
= TRUE
;
221 /* try to the left of the cursor */
222 myx
= *x
- self
->area
.width
;
223 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
224 self
->direction_right
= FALSE
;
227 /* if didnt fit on either side so just use what it says */
229 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
230 self
->direction_right
= TRUE
;
240 /* try to the bottom right of the cursor */
241 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
242 self
->direction_right
= TRUE
;
243 if (dx
!= 0 || dy
!= 0) {
244 /* try to the bottom left of the cursor */
245 myx
= *x
- self
->area
.width
;
247 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
248 self
->direction_right
= FALSE
;
250 if (dx
!= 0 || dy
!= 0) {
251 /* try to the top right of the cursor */
253 myy
= *y
- self
->area
.height
;
254 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
255 self
->direction_right
= TRUE
;
257 if (dx
!= 0 || dy
!= 0) {
258 /* try to the top left of the cursor */
259 myx
= *x
- self
->area
.width
;
260 myy
= *y
- self
->area
.height
;
261 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
262 self
->direction_right
= FALSE
;
264 if (dx
!= 0 || dy
!= 0) {
265 /* if didnt fit on either side so just use what it says */
268 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
269 self
->direction_right
= TRUE
;
276 static void menu_frame_place_submenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
278 gint overlapx
, overlapy
;
281 overlapx
= ob_rr_theme
->menu_overlap_x
;
282 overlapy
= ob_rr_theme
->menu_overlap_y
;
283 bwidth
= ob_rr_theme
->mbwidth
;
285 if (self
->direction_right
)
286 *x
= self
->parent
->area
.x
+ self
->parent
->area
.width
-
289 *x
= self
->parent
->area
.x
- self
->area
.width
+ overlapx
+ bwidth
;
291 *y
= self
->parent
->area
.y
+ self
->parent_entry
->area
.y
;
292 if (config_menu_middle
)
293 *y
-= (self
->area
.height
- (bwidth
* 2) - ITEM_HEIGHT
) / 2;
298 void menu_frame_move_on_screen(ObMenuFrame
*self
, gint x
, gint y
,
306 a
= screen_physical_area_monitor(screen_find_monitor_point(x
, y
));
308 half
= g_list_length(self
->entries
) / 2;
309 pos
= g_list_index(self
->entries
, self
->selected
);
311 /* if in the bottom half then check this stuff first, will keep the bottom
312 edge of the menu visible */
314 *dx
= MAX(*dx
, a
->x
- x
);
315 *dy
= MAX(*dy
, a
->y
- y
);
317 *dx
= MIN(*dx
, (a
->x
+ a
->width
) - (x
+ self
->area
.width
));
318 *dy
= MIN(*dy
, (a
->y
+ a
->height
) - (y
+ self
->area
.height
));
319 /* if in the top half then check this stuff last, will keep the top
320 edge of the menu visible */
322 *dx
= MAX(*dx
, a
->x
- x
);
323 *dy
= MAX(*dy
, a
->y
- y
);
329 static void menu_entry_frame_render(ObMenuEntryFrame
*self
)
331 RrAppearance
*item_a
, *text_a
;
334 ObMenuFrame
*frame
= self
->frame
;
336 switch (self
->entry
->type
) {
337 case OB_MENU_ENTRY_TYPE_NORMAL
:
338 case OB_MENU_ENTRY_TYPE_SUBMENU
:
339 item_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
340 !self
->entry
->data
.normal
.enabled
?
342 (self
== self
->frame
->selected
?
343 ob_rr_theme
->a_menu_disabled_selected
:
344 ob_rr_theme
->a_menu_disabled
) :
346 (self
== self
->frame
->selected
?
347 ob_rr_theme
->a_menu_selected
:
348 ob_rr_theme
->a_menu_normal
));
351 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
352 if (self
->entry
->data
.separator
.label
) {
353 item_a
= ob_rr_theme
->a_menu_title
;
354 th
= ob_rr_theme
->menu_title_height
;
356 item_a
= ob_rr_theme
->a_menu_normal
;
357 th
= ob_rr_theme
->menu_sep_width
+
358 2*ob_rr_theme
->menu_sep_paddingy
;
362 g_assert_not_reached();
365 RECT_SET_SIZE(self
->area
, self
->frame
->inner_w
, th
);
366 XResizeWindow(obt_display
, self
->window
,
367 self
->area
.width
, self
->area
.height
);
368 item_a
->surface
.parent
= self
->frame
->a_items
;
369 item_a
->surface
.parentx
= self
->area
.x
;
370 item_a
->surface
.parenty
= self
->area
.y
;
371 RrPaint(item_a
, self
->window
, self
->area
.width
, self
->area
.height
);
373 switch (self
->entry
->type
) {
374 case OB_MENU_ENTRY_TYPE_NORMAL
:
375 text_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
376 !self
->entry
->data
.normal
.enabled
?
378 (self
== self
->frame
->selected
?
379 ob_rr_theme
->a_menu_text_disabled_selected
:
380 ob_rr_theme
->a_menu_text_disabled
) :
382 (self
== self
->frame
->selected
?
383 ob_rr_theme
->a_menu_text_selected
:
384 ob_rr_theme
->a_menu_text_normal
));
385 text_a
->texture
[0].data
.text
.string
= self
->entry
->data
.normal
.label
;
386 if (self
->entry
->data
.normal
.shortcut
&&
387 (self
->frame
->menu
->show_all_shortcuts
||
388 self
->entry
->data
.normal
.shortcut_always_show
||
389 self
->entry
->data
.normal
.shortcut_position
> 0))
391 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
392 text_a
->texture
[0].data
.text
.shortcut_pos
=
393 self
->entry
->data
.normal
.shortcut_position
;
395 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
397 case OB_MENU_ENTRY_TYPE_SUBMENU
:
398 text_a
= (self
== self
->frame
->selected
?
399 ob_rr_theme
->a_menu_text_selected
:
400 ob_rr_theme
->a_menu_text_normal
);
401 sub
= self
->entry
->data
.submenu
.submenu
;
402 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
403 if (sub
&& sub
->shortcut
&& (self
->frame
->menu
->show_all_shortcuts
||
404 sub
->shortcut_always_show
||
405 sub
->shortcut_position
> 0))
407 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
408 text_a
->texture
[0].data
.text
.shortcut_pos
= sub
->shortcut_position
;
410 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
412 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
413 if (self
->entry
->data
.separator
.label
!= NULL
) {
414 text_a
= ob_rr_theme
->a_menu_text_title
;
415 text_a
->texture
[0].data
.text
.string
=
416 self
->entry
->data
.separator
.label
;
419 text_a
= ob_rr_theme
->a_menu_text_normal
;
422 g_assert_not_reached();
425 switch (self
->entry
->type
) {
426 case OB_MENU_ENTRY_TYPE_NORMAL
:
427 XMoveResizeWindow(obt_display
, self
->text
,
428 self
->frame
->text_x
, PADDING
,
430 ITEM_HEIGHT
- 2*PADDING
);
431 text_a
->surface
.parent
= item_a
;
432 text_a
->surface
.parentx
= self
->frame
->text_x
;
433 text_a
->surface
.parenty
= PADDING
;
434 RrPaint(text_a
, self
->text
, self
->frame
->text_w
,
435 ITEM_HEIGHT
- 2*PADDING
);
437 case OB_MENU_ENTRY_TYPE_SUBMENU
:
438 XMoveResizeWindow(obt_display
, self
->text
,
439 self
->frame
->text_x
, PADDING
,
440 self
->frame
->text_w
- ITEM_HEIGHT
,
441 ITEM_HEIGHT
- 2*PADDING
);
442 text_a
->surface
.parent
= item_a
;
443 text_a
->surface
.parentx
= self
->frame
->text_x
;
444 text_a
->surface
.parenty
= PADDING
;
445 RrPaint(text_a
, self
->text
, self
->frame
->text_w
- ITEM_HEIGHT
,
446 ITEM_HEIGHT
- 2*PADDING
);
448 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
449 if (self
->entry
->data
.separator
.label
!= NULL
) {
450 /* labeled separator */
451 XMoveResizeWindow(obt_display
, self
->text
,
452 ob_rr_theme
->paddingx
, ob_rr_theme
->paddingy
,
453 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
454 ob_rr_theme
->menu_title_height
-
455 2*ob_rr_theme
->paddingy
);
456 text_a
->surface
.parent
= item_a
;
457 text_a
->surface
.parentx
= ob_rr_theme
->paddingx
;
458 text_a
->surface
.parenty
= ob_rr_theme
->paddingy
;
459 RrPaint(text_a
, self
->text
,
460 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
461 ob_rr_theme
->menu_title_height
-
462 2*ob_rr_theme
->paddingy
);
466 /* unlabeled separator */
467 XMoveResizeWindow(obt_display
, self
->text
, 0, 0,
469 ob_rr_theme
->menu_sep_width
+
470 2*ob_rr_theme
->menu_sep_paddingy
);
472 a_sep
->surface
.parent
= item_a
;
473 a_sep
->surface
.parentx
= 0;
474 a_sep
->surface
.parenty
= 0;
475 for (i
= 0; i
< ob_rr_theme
->menu_sep_width
; ++i
) {
476 a_sep
->texture
[i
].data
.lineart
.x1
=
477 ob_rr_theme
->menu_sep_paddingx
;
478 a_sep
->texture
[i
].data
.lineart
.y1
=
479 ob_rr_theme
->menu_sep_paddingy
+ i
;
480 a_sep
->texture
[i
].data
.lineart
.x2
=
481 self
->area
.width
- ob_rr_theme
->menu_sep_paddingx
- 1;
482 a_sep
->texture
[i
].data
.lineart
.y2
=
483 ob_rr_theme
->menu_sep_paddingy
+ i
;
486 RrPaint(a_sep
, self
->text
, self
->area
.width
,
487 ob_rr_theme
->menu_sep_width
+
488 2*ob_rr_theme
->menu_sep_paddingy
);
492 g_assert_not_reached();
495 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
496 self
->entry
->data
.normal
.icon
)
500 XMoveResizeWindow(obt_display
, self
->icon
,
501 PADDING
, frame
->item_margin
.top
,
502 ITEM_HEIGHT
- frame
->item_margin
.top
503 - frame
->item_margin
.bottom
,
504 ITEM_HEIGHT
- frame
->item_margin
.top
505 - frame
->item_margin
.bottom
);
507 clear
= ob_rr_theme
->a_clear_tex
;
508 RrAppearanceClearTextures(clear
);
509 clear
->texture
[0].type
= RR_TEXTURE_IMAGE
;
510 clear
->texture
[0].data
.image
.image
=
511 self
->entry
->data
.normal
.icon
;
512 clear
->texture
[0].data
.image
.alpha
=
513 self
->entry
->data
.normal
.icon_alpha
;
514 clear
->surface
.parent
= item_a
;
515 clear
->surface
.parentx
= PADDING
;
516 clear
->surface
.parenty
= frame
->item_margin
.top
;
517 RrPaint(clear
, self
->icon
,
518 ITEM_HEIGHT
- frame
->item_margin
.top
519 - frame
->item_margin
.bottom
,
520 ITEM_HEIGHT
- frame
->item_margin
.top
521 - frame
->item_margin
.bottom
);
522 XMapWindow(obt_display
, self
->icon
);
523 } else if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
524 self
->entry
->data
.normal
.mask
)
529 XMoveResizeWindow(obt_display
, self
->icon
,
530 PADDING
, frame
->item_margin
.top
,
531 ITEM_HEIGHT
- frame
->item_margin
.top
532 - frame
->item_margin
.bottom
,
533 ITEM_HEIGHT
- frame
->item_margin
.top
534 - frame
->item_margin
.bottom
);
536 clear
= ob_rr_theme
->a_clear_tex
;
537 RrAppearanceClearTextures(clear
);
538 clear
->texture
[0].type
= RR_TEXTURE_MASK
;
539 clear
->texture
[0].data
.mask
.mask
=
540 self
->entry
->data
.normal
.mask
;
542 c
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
543 !self
->entry
->data
.normal
.enabled
?
545 (self
== self
->frame
->selected
?
546 self
->entry
->data
.normal
.mask_disabled_selected_color
:
547 self
->entry
->data
.normal
.mask_disabled_color
) :
549 (self
== self
->frame
->selected
?
550 self
->entry
->data
.normal
.mask_selected_color
:
551 self
->entry
->data
.normal
.mask_normal_color
));
552 clear
->texture
[0].data
.mask
.color
= c
;
554 clear
->surface
.parent
= item_a
;
555 clear
->surface
.parentx
= PADDING
;
556 clear
->surface
.parenty
= frame
->item_margin
.top
;
557 RrPaint(clear
, self
->icon
,
558 ITEM_HEIGHT
- frame
->item_margin
.top
559 - frame
->item_margin
.bottom
,
560 ITEM_HEIGHT
- frame
->item_margin
.top
561 - frame
->item_margin
.bottom
);
562 XMapWindow(obt_display
, self
->icon
);
564 XUnmapWindow(obt_display
, self
->icon
);
566 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
567 RrAppearance
*bullet_a
;
568 XMoveResizeWindow(obt_display
, self
->bullet
,
569 self
->frame
->text_x
+ self
->frame
->text_w
-
570 ITEM_HEIGHT
+ PADDING
, PADDING
,
571 ITEM_HEIGHT
- 2*PADDING
,
572 ITEM_HEIGHT
- 2*PADDING
);
573 bullet_a
= (self
== self
->frame
->selected
?
574 ob_rr_theme
->a_menu_bullet_selected
:
575 ob_rr_theme
->a_menu_bullet_normal
);
576 bullet_a
->surface
.parent
= item_a
;
577 bullet_a
->surface
.parentx
=
578 self
->frame
->text_x
+ self
->frame
->text_w
- ITEM_HEIGHT
+ PADDING
;
579 bullet_a
->surface
.parenty
= PADDING
;
580 RrPaint(bullet_a
, self
->bullet
,
581 ITEM_HEIGHT
- 2*PADDING
,
582 ITEM_HEIGHT
- 2*PADDING
);
583 XMapWindow(obt_display
, self
->bullet
);
585 XUnmapWindow(obt_display
, self
->bullet
);
590 /*! this code is taken from the menu_frame_render. if that changes, this won't
592 static gint
menu_entry_frame_get_height(ObMenuEntryFrame
*self
,
593 gboolean first_entry
,
602 t
= self
->entry
->type
;
604 /* this is the More... entry, it's NORMAL type */
605 t
= OB_MENU_ENTRY_TYPE_NORMAL
;
608 case OB_MENU_ENTRY_TYPE_NORMAL
:
609 case OB_MENU_ENTRY_TYPE_SUBMENU
:
610 h
+= ob_rr_theme
->menu_font_height
;
612 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
613 if (self
->entry
->data
.separator
.label
!= NULL
) {
614 h
+= ob_rr_theme
->menu_title_height
+
615 (ob_rr_theme
->mbwidth
- PADDING
) * 2;
617 /* if the first entry is a labeled separator, then make its border
618 overlap with the menu's outside border */
620 h
-= ob_rr_theme
->mbwidth
;
621 /* if the last entry is a labeled separator, then make its border
622 overlap with the menu's outside border */
624 h
-= ob_rr_theme
->mbwidth
;
626 h
+= ob_rr_theme
->menu_sep_width
+
627 2*ob_rr_theme
->menu_sep_paddingy
- PADDING
* 2;
635 void menu_frame_render(ObMenuFrame
*self
)
638 gint tw
, th
; /* temps */
640 gboolean has_icon
= FALSE
;
644 /* find text dimensions */
646 STRUT_SET(self
->item_margin
, 0, 0, 0, 0);
651 e
= self
->entries
->data
;
652 ob_rr_theme
->a_menu_text_normal
->texture
[0].data
.text
.string
= "";
653 tw
= RrMinWidth(ob_rr_theme
->a_menu_text_normal
);
658 RrMargins(ob_rr_theme
->a_menu_normal
, &l
, &t
, &r
, &b
);
659 STRUT_SET(self
->item_margin
,
660 MAX(self
->item_margin
.left
, l
),
661 MAX(self
->item_margin
.top
, t
),
662 MAX(self
->item_margin
.right
, r
),
663 MAX(self
->item_margin
.bottom
, b
));
664 RrMargins(ob_rr_theme
->a_menu_selected
, &l
, &t
, &r
, &b
);
665 STRUT_SET(self
->item_margin
,
666 MAX(self
->item_margin
.left
, l
),
667 MAX(self
->item_margin
.top
, t
),
668 MAX(self
->item_margin
.right
, r
),
669 MAX(self
->item_margin
.bottom
, b
));
670 RrMargins(ob_rr_theme
->a_menu_disabled
, &l
, &t
, &r
, &b
);
671 STRUT_SET(self
->item_margin
,
672 MAX(self
->item_margin
.left
, l
),
673 MAX(self
->item_margin
.top
, t
),
674 MAX(self
->item_margin
.right
, r
),
675 MAX(self
->item_margin
.bottom
, b
));
676 RrMargins(ob_rr_theme
->a_menu_disabled_selected
, &l
, &t
, &r
, &b
);
677 STRUT_SET(self
->item_margin
,
678 MAX(self
->item_margin
.left
, l
),
679 MAX(self
->item_margin
.top
, t
),
680 MAX(self
->item_margin
.right
, r
),
681 MAX(self
->item_margin
.bottom
, b
));
684 /* render the entries */
686 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
687 RrAppearance
*text_a
;
690 /* if the first entry is a labeled separator, then make its border
691 overlap with the menu's outside border */
692 if (it
== self
->entries
&&
693 e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
694 e
->entry
->data
.separator
.label
)
696 h
-= ob_rr_theme
->mbwidth
;
699 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
700 e
->entry
->data
.separator
.label
)
702 e
->border
= ob_rr_theme
->mbwidth
;
705 RECT_SET_POINT(e
->area
, 0, h
+e
->border
);
706 XMoveWindow(obt_display
, e
->window
,
707 e
->area
.x
-e
->border
, e
->area
.y
-e
->border
);
708 XSetWindowBorderWidth(obt_display
, e
->window
, e
->border
);
709 XSetWindowBorder(obt_display
, e
->window
,
710 RrColorPixel(ob_rr_theme
->menu_border_color
));
712 text_a
= (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
713 !e
->entry
->data
.normal
.enabled
?
715 (e
== self
->selected
?
716 ob_rr_theme
->a_menu_text_disabled_selected
:
717 ob_rr_theme
->a_menu_text_disabled
) :
719 (e
== self
->selected
?
720 ob_rr_theme
->a_menu_text_selected
:
721 ob_rr_theme
->a_menu_text_normal
));
722 switch (e
->entry
->type
) {
723 case OB_MENU_ENTRY_TYPE_NORMAL
:
724 text_a
->texture
[0].data
.text
.string
= e
->entry
->data
.normal
.label
;
725 tw
= RrMinWidth(text_a
);
726 tw
= MIN(tw
, MAX_MENU_WIDTH
);
727 th
= ob_rr_theme
->menu_font_height
;
729 if (e
->entry
->data
.normal
.icon
||
730 e
->entry
->data
.normal
.mask
)
733 case OB_MENU_ENTRY_TYPE_SUBMENU
:
734 sub
= e
->entry
->data
.submenu
.submenu
;
735 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
736 tw
= RrMinWidth(text_a
);
737 tw
= MIN(tw
, MAX_MENU_WIDTH
);
738 th
= ob_rr_theme
->menu_font_height
;
740 if (e
->entry
->data
.normal
.icon
||
741 e
->entry
->data
.normal
.mask
)
744 tw
+= ITEM_HEIGHT
- PADDING
;
746 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
747 if (e
->entry
->data
.separator
.label
!= NULL
) {
748 ob_rr_theme
->a_menu_text_title
->texture
[0].data
.text
.string
=
749 e
->entry
->data
.separator
.label
;
750 tw
= RrMinWidth(ob_rr_theme
->a_menu_text_title
) +
751 2*ob_rr_theme
->paddingx
;
752 tw
= MIN(tw
, MAX_MENU_WIDTH
);
753 th
= ob_rr_theme
->menu_title_height
+
754 (ob_rr_theme
->mbwidth
- PADDING
) *2;
757 th
= ob_rr_theme
->menu_sep_width
+
758 2*ob_rr_theme
->menu_sep_paddingy
- 2*PADDING
;
762 g_assert_not_reached();
770 /* if the last entry is a labeled separator, then make its border
771 overlap with the menu's outside border */
772 it
= g_list_last(self
->entries
);
773 e
= it
? it
->data
: NULL
;
774 if (e
&& e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
775 e
->entry
->data
.separator
.label
)
777 h
-= ob_rr_theme
->mbwidth
;
780 self
->text_x
= PADDING
;
785 w
+= ITEM_HEIGHT
+ PADDING
;
786 self
->text_x
+= ITEM_HEIGHT
+ PADDING
;
793 XResizeWindow(obt_display
, self
->window
, w
, h
);
797 RrPaint(self
->a_items
, self
->window
, w
, h
);
799 for (it
= self
->entries
; it
; it
= g_list_next(it
))
800 menu_entry_frame_render(it
->data
);
802 w
+= ob_rr_theme
->mbwidth
* 2;
803 h
+= ob_rr_theme
->mbwidth
* 2;
805 RECT_SET_SIZE(self
->area
, w
, h
);
810 static void menu_frame_update(ObMenuFrame
*self
)
816 menu_pipe_execute(self
->menu
);
817 menu_find_submenus(self
->menu
);
819 self
->selected
= NULL
;
821 /* start at show_from */
822 mit
= g_list_nth(self
->menu
->entries
, self
->show_from
);
824 /* go through the menu's and frame's entries and connect the frame entries
825 to the menu entries */
826 for (fit
= self
->entries
; mit
&& fit
;
827 mit
= g_list_next(mit
), fit
= g_list_next(fit
))
829 ObMenuEntryFrame
*f
= fit
->data
;
830 f
->entry
= mit
->data
;
833 /* if there are more menu entries than in the frame, add them */
835 ObMenuEntryFrame
*e
= menu_entry_frame_new(mit
->data
, self
);
836 self
->entries
= g_list_append(self
->entries
, e
);
837 mit
= g_list_next(mit
);
840 /* if there are more frame entries than menu entries then get rid of
843 GList
*n
= g_list_next(fit
);
844 menu_entry_frame_free(fit
->data
);
845 self
->entries
= g_list_delete_link(self
->entries
, fit
);
849 /* * make the menu fit on the screen */
851 /* calculate the height of the menu */
853 for (fit
= self
->entries
; fit
; fit
= g_list_next(fit
))
854 h
+= menu_entry_frame_get_height(fit
->data
,
855 fit
== self
->entries
,
856 g_list_next(fit
) == NULL
);
857 /* add the border at the top and bottom */
858 h
+= ob_rr_theme
->mbwidth
* 2;
860 a
= screen_physical_area_monitor(self
->monitor
);
864 gboolean last_entry
= TRUE
;
866 /* take the height of our More... entry into account */
867 h
+= menu_entry_frame_get_height(NULL
, FALSE
, TRUE
);
869 /* start at the end of the entries */
870 flast
= g_list_last(self
->entries
);
872 /* pull out all the entries from the frame that don't
873 fit on the screen, leaving at least 1 though */
874 while (h
> a
->height
&& g_list_previous(flast
) != NULL
) {
875 /* update the height, without this entry */
876 h
-= menu_entry_frame_get_height(flast
->data
, FALSE
, last_entry
);
878 /* destroy the entry we're not displaying */
880 flast
= g_list_previous(flast
);
881 menu_entry_frame_free(tmp
->data
);
882 self
->entries
= g_list_delete_link(self
->entries
, tmp
);
884 /* only the first one that we see is the last entry in the menu */
889 ObMenuEntry
*more_entry
;
890 ObMenuEntryFrame
*more_frame
;
891 /* make the More... menu entry frame which will display in this
893 if self->menu->more_menu is NULL that means that this is already
894 More... menu, so just use ourself.
896 more_entry
= menu_get_more((self
->menu
->more_menu
?
897 self
->menu
->more_menu
:
899 /* continue where we left off */
901 g_list_length(self
->entries
));
902 more_frame
= menu_entry_frame_new(more_entry
, self
);
903 /* make it get deleted when the menu frame goes away */
904 menu_entry_unref(more_entry
);
906 /* add our More... entry to the frame */
907 self
->entries
= g_list_append(self
->entries
, more_frame
);
913 menu_frame_render(self
);
916 static gboolean
menu_frame_is_visible(ObMenuFrame
*self
)
918 return !!(g_list_find(menu_frame_visible
, self
));
921 static gboolean
menu_frame_show(ObMenuFrame
*self
)
925 /* determine if the underlying menu is already visible */
926 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
927 ObMenuFrame
*f
= it
->data
;
928 if (f
->menu
== self
->menu
)
932 if (self
->menu
->update_func
)
933 if (!self
->menu
->update_func(self
, self
->menu
->data
))
937 if (menu_frame_visible
== NULL
) {
938 /* no menus shown yet */
940 /* grab the pointer in such a way as to pass through "owner events"
941 so that we can get enter/leave notifies in the menu. */
942 if (!grab_pointer(TRUE
, FALSE
, OB_CURSOR_POINTER
))
944 if (!grab_keyboard()) {
950 menu_frame_update(self
);
952 menu_frame_visible
= g_list_prepend(menu_frame_visible
, self
);
954 if (self
->menu
->show_func
)
955 self
->menu
->show_func(self
, self
->menu
->data
);
960 gboolean
menu_frame_show_topmenu(ObMenuFrame
*self
, gint x
, gint y
,
965 if (menu_frame_is_visible(self
))
967 if (!menu_frame_show(self
))
970 if (self
->menu
->place_func
)
971 self
->menu
->place_func(self
, &x
, &y
, mouse
, self
->menu
->data
);
973 menu_frame_place_topmenu(self
, &x
, &y
);
975 menu_frame_move(self
, x
, y
);
977 XMapWindow(obt_display
, self
->window
);
979 if (screen_pointer_pos(&px
, &py
)) {
980 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
981 if (e
&& e
->frame
== self
)
988 /*! Stop hiding an open submenu.
989 @child The OnMenuFrame of the submenu to be hidden
991 static void remove_submenu_hide_timeout(ObMenuFrame
*child
)
993 obt_main_loop_timeout_remove_data(ob_main_loop
, submenu_hide_timeout
,
997 gboolean
menu_frame_show_submenu(ObMenuFrame
*self
, ObMenuFrame
*parent
,
998 ObMenuEntryFrame
*parent_entry
)
1003 if (menu_frame_is_visible(self
))
1006 self
->monitor
= parent
->monitor
;
1007 self
->parent
= parent
;
1008 self
->parent_entry
= parent_entry
;
1010 /* set up parent's child to be us */
1011 if ((parent
->child
) != self
) {
1013 menu_frame_hide(parent
->child
);
1014 parent
->child
= self
;
1015 parent
->child_entry
= parent_entry
;
1018 if (!menu_frame_show(self
))
1021 menu_frame_place_submenu(self
, &x
, &y
);
1022 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1025 /*try the other side */
1026 self
->direction_right
= !self
->direction_right
;
1027 menu_frame_place_submenu(self
, &x
, &y
);
1028 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1030 menu_frame_move(self
, x
+ dx
, y
+ dy
);
1032 XMapWindow(obt_display
, self
->window
);
1034 if (screen_pointer_pos(&px
, &py
)) {
1035 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
1036 if (e
&& e
->frame
== self
)
1043 static void menu_frame_hide(ObMenuFrame
*self
)
1045 GList
*it
= g_list_find(menu_frame_visible
, self
);
1046 gulong ignore_start
;
1051 if (self
->menu
->hide_func
)
1052 self
->menu
->hide_func(self
, self
->menu
->data
);
1055 menu_frame_hide(self
->child
);
1058 remove_submenu_hide_timeout(self
);
1060 self
->parent
->child
= NULL
;
1061 self
->parent
->child_entry
= NULL
;
1063 self
->parent
= NULL
;
1064 self
->parent_entry
= NULL
;
1066 menu_frame_visible
= g_list_delete_link(menu_frame_visible
, it
);
1068 if (menu_frame_visible
== NULL
) {
1069 /* last menu shown */
1074 ignore_start
= event_start_ignore_all_enters();
1075 XUnmapWindow(obt_display
, self
->window
);
1076 event_end_ignore_all_enters(ignore_start
);
1078 menu_frame_free(self
);
1081 void menu_frame_hide_all(void)
1085 if (config_submenu_show_delay
) {
1086 /* remove any submenu open requests */
1087 obt_main_loop_timeout_remove(ob_main_loop
, submenu_show_timeout
);
1089 if ((it
= g_list_last(menu_frame_visible
)))
1090 menu_frame_hide(it
->data
);
1093 void menu_frame_hide_all_client(ObClient
*client
)
1095 GList
*it
= g_list_last(menu_frame_visible
);
1097 ObMenuFrame
*f
= it
->data
;
1098 if (f
->client
== client
) {
1099 if (config_submenu_show_delay
) {
1100 /* remove any submenu open requests */
1101 obt_main_loop_timeout_remove(ob_main_loop
,
1102 submenu_show_timeout
);
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
;
1267 /* release grabs before executing the shit */
1268 if (!(state
& ControlMask
)) {
1269 menu_frame_hide_all();
1274 func(entry
, frame
, client
, state
, data
);
1276 actions_run_acts(acts
, OB_USER_ACTION_MENU_SELECTION
,
1277 state
, -1, -1, 0, OB_FRAME_CONTEXT_NONE
, client
);
1281 void menu_frame_select_previous(ObMenuFrame
*self
)
1283 GList
*it
= NULL
, *start
;
1285 if (self
->entries
) {
1286 start
= it
= g_list_find(self
->entries
, self
->selected
);
1288 ObMenuEntryFrame
*e
;
1290 it
= it
? g_list_previous(it
) : g_list_last(self
->entries
);
1296 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1298 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1303 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);
1306 void menu_frame_select_next(ObMenuFrame
*self
)
1308 GList
*it
= NULL
, *start
;
1310 if (self
->entries
) {
1311 start
= it
= g_list_find(self
->entries
, self
->selected
);
1313 ObMenuEntryFrame
*e
;
1315 it
= it
? g_list_next(it
) : self
->entries
;
1321 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1323 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1328 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);