1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 menuframe.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "menuframe.h"
29 #include "render/theme.h"
32 #define SEPARATOR_HEIGHT 3
33 #define MAX_MENU_WIDTH 400
35 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
37 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
39 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
40 ButtonPressMask | ButtonReleaseMask)
42 GList
*menu_frame_visible
;
44 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
46 static void menu_entry_frame_free(ObMenuEntryFrame
*self
);
47 static void menu_frame_update(ObMenuFrame
*self
);
48 static gboolean
menu_entry_frame_submenu_timeout(gpointer data
);
49 static void menu_frame_hide(ObMenuFrame
*self
);
51 static Window
createWindow(Window parent
, gulong mask
,
52 XSetWindowAttributes
*attrib
)
54 return XCreateWindow(obt_display
, parent
, 0, 0, 1, 1, 0,
55 RrDepth(ob_rr_inst
), InputOutput
,
56 RrVisual(ob_rr_inst
), mask
, attrib
);
59 GHashTable
*menu_frame_map
;
61 void menu_frame_startup(gboolean reconfig
)
65 menu_frame_map
= g_hash_table_new(g_int_hash
, g_int_equal
);
68 void menu_frame_shutdown(gboolean reconfig
)
72 g_hash_table_destroy(menu_frame_map
);
75 ObMenuFrame
* menu_frame_new(ObMenu
*menu
, guint show_from
, ObClient
*client
)
78 XSetWindowAttributes attr
;
80 self
= g_new0(ObMenuFrame
, 1);
81 self
->obwin
.type
= OB_WINDOW_CLASS_MENUFRAME
;
83 self
->selected
= NULL
;
84 self
->client
= client
;
85 self
->direction_right
= TRUE
;
86 self
->show_from
= show_from
;
88 attr
.event_mask
= FRAME_EVENTMASK
;
89 self
->window
= createWindow(obt_root(ob_screen
),
92 /* make it a popup menu type window */
93 OBT_PROP_SET32(self
->window
, NET_WM_WINDOW_TYPE
, ATOM
,
94 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU
));
96 XSetWindowBorderWidth(obt_display
, self
->window
, ob_rr_theme
->mbwidth
);
97 XSetWindowBorder(obt_display
, self
->window
,
98 RrColorPixel(ob_rr_theme
->menu_border_color
));
100 self
->a_items
= RrAppearanceCopy(ob_rr_theme
->a_menu
);
102 window_add(&self
->window
, MENUFRAME_AS_WINDOW(self
));
103 stacking_add(MENUFRAME_AS_WINDOW(self
));
108 void menu_frame_free(ObMenuFrame
*self
)
111 while (self
->entries
) {
112 menu_entry_frame_free(self
->entries
->data
);
113 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
116 stacking_remove(MENUFRAME_AS_WINDOW(self
));
117 window_remove(self
->window
);
119 RrAppearanceFree(self
->a_items
);
121 XDestroyWindow(obt_display
, self
->window
);
127 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
130 ObMenuEntryFrame
*self
;
131 XSetWindowAttributes attr
;
133 self
= g_new0(ObMenuEntryFrame
, 1);
137 menu_entry_ref(entry
);
139 attr
.event_mask
= ENTRY_EVENTMASK
;
140 self
->window
= createWindow(self
->frame
->window
, CWEventMask
, &attr
);
141 self
->text
= createWindow(self
->window
, 0, NULL
);
142 g_hash_table_insert(menu_frame_map
, &self
->window
, self
);
143 g_hash_table_insert(menu_frame_map
, &self
->text
, self
);
144 if (entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
145 self
->icon
= createWindow(self
->window
, 0, NULL
);
146 g_hash_table_insert(menu_frame_map
, &self
->icon
, self
);
148 if (entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
149 self
->bullet
= createWindow(self
->window
, 0, NULL
);
150 g_hash_table_insert(menu_frame_map
, &self
->bullet
, self
);
153 XMapWindow(obt_display
, self
->window
);
154 XMapWindow(obt_display
, self
->text
);
156 window_add(&self
->window
, MENUFRAME_AS_WINDOW(self
->frame
));
161 static void menu_entry_frame_free(ObMenuEntryFrame
*self
)
164 menu_entry_unref(self
->entry
);
166 window_remove(self
->window
);
168 XDestroyWindow(obt_display
, self
->text
);
169 XDestroyWindow(obt_display
, self
->window
);
170 g_hash_table_remove(menu_frame_map
, &self
->text
);
171 g_hash_table_remove(menu_frame_map
, &self
->window
);
172 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
173 XDestroyWindow(obt_display
, self
->icon
);
174 g_hash_table_remove(menu_frame_map
, &self
->icon
);
176 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
177 XDestroyWindow(obt_display
, self
->bullet
);
178 g_hash_table_remove(menu_frame_map
, &self
->bullet
);
185 void menu_frame_move(ObMenuFrame
*self
, gint x
, gint y
)
187 RECT_SET_POINT(self
->area
, x
, y
);
188 XMoveWindow(obt_display
, self
->window
, self
->area
.x
, self
->area
.y
);
191 static void menu_frame_place_topmenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
195 if (config_menu_middle
) {
199 *y
-= self
->area
.height
/ 2;
201 /* try to the right of the cursor */
202 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
203 self
->direction_right
= TRUE
;
205 /* try to the left of the cursor */
206 myx
= *x
- self
->area
.width
;
207 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
208 self
->direction_right
= FALSE
;
211 /* if didnt fit on either side so just use what it says */
213 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
214 self
->direction_right
= TRUE
;
224 /* try to the bottom right of the cursor */
225 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
226 self
->direction_right
= TRUE
;
227 if (dx
!= 0 || dy
!= 0) {
228 /* try to the bottom left of the cursor */
229 myx
= *x
- self
->area
.width
;
231 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
232 self
->direction_right
= FALSE
;
234 if (dx
!= 0 || dy
!= 0) {
235 /* try to the top right of the cursor */
237 myy
= *y
- self
->area
.height
;
238 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
239 self
->direction_right
= TRUE
;
241 if (dx
!= 0 || dy
!= 0) {
242 /* try to the top left of the cursor */
243 myx
= *x
- self
->area
.width
;
244 myy
= *y
- self
->area
.height
;
245 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
246 self
->direction_right
= FALSE
;
248 if (dx
!= 0 || dy
!= 0) {
249 /* if didnt fit on either side so just use what it says */
252 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
253 self
->direction_right
= TRUE
;
260 static void menu_frame_place_submenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
262 gint overlapx
, overlapy
;
265 overlapx
= ob_rr_theme
->menu_overlap_x
;
266 overlapy
= ob_rr_theme
->menu_overlap_y
;
267 bwidth
= ob_rr_theme
->mbwidth
;
269 if (self
->direction_right
)
270 *x
= self
->parent
->area
.x
+ self
->parent
->area
.width
-
273 *x
= self
->parent
->area
.x
- self
->area
.width
+ overlapx
+ bwidth
;
275 *y
= self
->parent
->area
.y
+ self
->parent_entry
->area
.y
;
276 if (config_menu_middle
)
277 *y
-= (self
->area
.height
- (bwidth
* 2) - ITEM_HEIGHT
) / 2;
282 void menu_frame_move_on_screen(ObMenuFrame
*self
, gint x
, gint y
,
290 a
= screen_physical_area_monitor(self
->monitor
);
292 half
= g_list_length(self
->entries
) / 2;
293 pos
= g_list_index(self
->entries
, self
->selected
);
295 /* if in the bottom half then check this stuff first, will keep the bottom
296 edge of the menu visible */
298 *dx
= MAX(*dx
, a
->x
- x
);
299 *dy
= MAX(*dy
, a
->y
- y
);
301 *dx
= MIN(*dx
, (a
->x
+ a
->width
) - (x
+ self
->area
.width
));
302 *dy
= MIN(*dy
, (a
->y
+ a
->height
) - (y
+ self
->area
.height
));
303 /* if in the top half then check this stuff last, will keep the top
304 edge of the menu visible */
306 *dx
= MAX(*dx
, a
->x
- x
);
307 *dy
= MAX(*dy
, a
->y
- y
);
313 static void menu_entry_frame_render(ObMenuEntryFrame
*self
)
315 RrAppearance
*item_a
, *text_a
;
318 ObMenuFrame
*frame
= self
->frame
;
320 switch (self
->entry
->type
) {
321 case OB_MENU_ENTRY_TYPE_NORMAL
:
322 case OB_MENU_ENTRY_TYPE_SUBMENU
:
323 item_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
324 !self
->entry
->data
.normal
.enabled
?
326 (self
== self
->frame
->selected
?
327 ob_rr_theme
->a_menu_disabled_selected
:
328 ob_rr_theme
->a_menu_disabled
) :
330 (self
== self
->frame
->selected
?
331 ob_rr_theme
->a_menu_selected
:
332 ob_rr_theme
->a_menu_normal
));
335 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
336 if (self
->entry
->data
.separator
.label
) {
337 item_a
= ob_rr_theme
->a_menu_title
;
338 th
= ob_rr_theme
->menu_title_height
;
340 item_a
= ob_rr_theme
->a_menu_normal
;
341 th
= SEPARATOR_HEIGHT
+ 2*PADDING
;
345 g_assert_not_reached();
348 RECT_SET_SIZE(self
->area
, self
->frame
->inner_w
, th
);
349 XResizeWindow(obt_display
, self
->window
,
350 self
->area
.width
, self
->area
.height
);
351 item_a
->surface
.parent
= self
->frame
->a_items
;
352 item_a
->surface
.parentx
= self
->area
.x
;
353 item_a
->surface
.parenty
= self
->area
.y
;
354 RrPaint(item_a
, self
->window
, self
->area
.width
, self
->area
.height
);
356 switch (self
->entry
->type
) {
357 case OB_MENU_ENTRY_TYPE_NORMAL
:
358 text_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
359 !self
->entry
->data
.normal
.enabled
?
361 (self
== self
->frame
->selected
?
362 ob_rr_theme
->a_menu_text_disabled_selected
:
363 ob_rr_theme
->a_menu_text_disabled
) :
365 (self
== self
->frame
->selected
?
366 ob_rr_theme
->a_menu_text_selected
:
367 ob_rr_theme
->a_menu_text_normal
));
368 text_a
->texture
[0].data
.text
.string
= self
->entry
->data
.normal
.label
;
369 if (self
->entry
->data
.normal
.shortcut
&&
370 (self
->frame
->menu
->show_all_shortcuts
||
371 self
->entry
->data
.normal
.shortcut_always_show
||
372 self
->entry
->data
.normal
.shortcut_position
> 0))
374 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
375 text_a
->texture
[0].data
.text
.shortcut_pos
=
376 self
->entry
->data
.normal
.shortcut_position
;
378 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
380 case OB_MENU_ENTRY_TYPE_SUBMENU
:
381 text_a
= (self
== self
->frame
->selected
?
382 ob_rr_theme
->a_menu_text_selected
:
383 ob_rr_theme
->a_menu_text_normal
);
384 sub
= self
->entry
->data
.submenu
.submenu
;
385 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
386 if (sub
->shortcut
&& (self
->frame
->menu
->show_all_shortcuts
||
387 sub
->shortcut_always_show
||
388 sub
->shortcut_position
> 0))
390 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
391 text_a
->texture
[0].data
.text
.shortcut_pos
= sub
->shortcut_position
;
393 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
395 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
396 if (self
->entry
->data
.separator
.label
!= NULL
) {
397 text_a
= ob_rr_theme
->a_menu_text_title
;
398 text_a
->texture
[0].data
.text
.string
=
399 self
->entry
->data
.separator
.label
;
402 text_a
= ob_rr_theme
->a_menu_text_normal
;
406 switch (self
->entry
->type
) {
407 case OB_MENU_ENTRY_TYPE_NORMAL
:
408 XMoveResizeWindow(obt_display
, self
->text
,
409 self
->frame
->text_x
, PADDING
,
411 ITEM_HEIGHT
- 2*PADDING
);
412 text_a
->surface
.parent
= item_a
;
413 text_a
->surface
.parentx
= self
->frame
->text_x
;
414 text_a
->surface
.parenty
= PADDING
;
415 RrPaint(text_a
, self
->text
, self
->frame
->text_w
,
416 ITEM_HEIGHT
- 2*PADDING
);
418 case OB_MENU_ENTRY_TYPE_SUBMENU
:
419 XMoveResizeWindow(obt_display
, self
->text
,
420 self
->frame
->text_x
, PADDING
,
421 self
->frame
->text_w
- ITEM_HEIGHT
,
422 ITEM_HEIGHT
- 2*PADDING
);
423 text_a
->surface
.parent
= item_a
;
424 text_a
->surface
.parentx
= self
->frame
->text_x
;
425 text_a
->surface
.parenty
= PADDING
;
426 RrPaint(text_a
, self
->text
, self
->frame
->text_w
- ITEM_HEIGHT
,
427 ITEM_HEIGHT
- 2*PADDING
);
429 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
430 if (self
->entry
->data
.separator
.label
!= NULL
) {
431 /* labeled separator */
432 XMoveResizeWindow(obt_display
, self
->text
,
433 ob_rr_theme
->paddingx
, ob_rr_theme
->paddingy
,
434 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
435 ob_rr_theme
->menu_title_height
-
436 2*ob_rr_theme
->paddingy
);
437 text_a
->surface
.parent
= item_a
;
438 text_a
->surface
.parentx
= ob_rr_theme
->paddingx
;
439 text_a
->surface
.parenty
= ob_rr_theme
->paddingy
;
440 RrPaint(text_a
, self
->text
,
441 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
442 ob_rr_theme
->menu_title_height
-
443 2*ob_rr_theme
->paddingy
);
447 /* unlabeled separaator */
448 XMoveResizeWindow(obt_display
, self
->text
, PADDING
, PADDING
,
449 self
->area
.width
- 2*PADDING
, SEPARATOR_HEIGHT
);
451 clear
= ob_rr_theme
->a_clear_tex
;
452 RrAppearanceClearTextures(clear
);
453 clear
->texture
[0].type
= RR_TEXTURE_LINE_ART
;
454 clear
->surface
.parent
= item_a
;
455 clear
->surface
.parentx
= PADDING
;
456 clear
->surface
.parenty
= PADDING
;
457 clear
->texture
[0].data
.lineart
.color
=
458 text_a
->texture
[0].data
.text
.color
;
459 clear
->texture
[0].data
.lineart
.x1
= 2*PADDING
;
460 clear
->texture
[0].data
.lineart
.y1
= SEPARATOR_HEIGHT
/2;
461 clear
->texture
[0].data
.lineart
.x2
= self
->area
.width
- 4*PADDING
;
462 clear
->texture
[0].data
.lineart
.y2
= SEPARATOR_HEIGHT
/2;
463 RrPaint(clear
, self
->text
,
464 self
->area
.width
- 2*PADDING
, SEPARATOR_HEIGHT
);
469 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
470 self
->entry
->data
.normal
.icon
)
474 XMoveResizeWindow(obt_display
, self
->icon
,
475 PADDING
, frame
->item_margin
.top
,
476 ITEM_HEIGHT
- frame
->item_margin
.top
477 - frame
->item_margin
.bottom
,
478 ITEM_HEIGHT
- frame
->item_margin
.top
479 - frame
->item_margin
.bottom
);
481 clear
= ob_rr_theme
->a_clear_tex
;
482 RrAppearanceClearTextures(clear
);
483 clear
->texture
[0].type
= RR_TEXTURE_IMAGE
;
484 clear
->texture
[0].data
.image
.image
=
485 self
->entry
->data
.normal
.icon
;
486 clear
->texture
[0].data
.image
.alpha
=
487 self
->entry
->data
.normal
.icon_alpha
;
488 clear
->surface
.parent
= item_a
;
489 clear
->surface
.parentx
= PADDING
;
490 clear
->surface
.parenty
= frame
->item_margin
.top
;
491 RrPaint(clear
, self
->icon
,
492 ITEM_HEIGHT
- frame
->item_margin
.top
493 - frame
->item_margin
.bottom
,
494 ITEM_HEIGHT
- frame
->item_margin
.top
495 - frame
->item_margin
.bottom
);
496 XMapWindow(obt_display
, self
->icon
);
497 } else if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
498 self
->entry
->data
.normal
.mask
)
503 XMoveResizeWindow(obt_display
, self
->icon
,
504 PADDING
, frame
->item_margin
.top
,
505 ITEM_HEIGHT
- frame
->item_margin
.top
506 - frame
->item_margin
.bottom
,
507 ITEM_HEIGHT
- frame
->item_margin
.top
508 - frame
->item_margin
.bottom
);
510 clear
= ob_rr_theme
->a_clear_tex
;
511 RrAppearanceClearTextures(clear
);
512 clear
->texture
[0].type
= RR_TEXTURE_MASK
;
513 clear
->texture
[0].data
.mask
.mask
=
514 self
->entry
->data
.normal
.mask
;
516 c
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
517 !self
->entry
->data
.normal
.enabled
?
519 (self
== self
->frame
->selected
?
520 self
->entry
->data
.normal
.mask_disabled_selected_color
:
521 self
->entry
->data
.normal
.mask_disabled_color
) :
523 (self
== self
->frame
->selected
?
524 self
->entry
->data
.normal
.mask_selected_color
:
525 self
->entry
->data
.normal
.mask_normal_color
));
526 clear
->texture
[0].data
.mask
.color
= c
;
528 clear
->surface
.parent
= item_a
;
529 clear
->surface
.parentx
= PADDING
;
530 clear
->surface
.parenty
= frame
->item_margin
.top
;
531 RrPaint(clear
, self
->icon
,
532 ITEM_HEIGHT
- frame
->item_margin
.top
533 - frame
->item_margin
.bottom
,
534 ITEM_HEIGHT
- frame
->item_margin
.top
535 - frame
->item_margin
.bottom
);
536 XMapWindow(obt_display
, self
->icon
);
538 XUnmapWindow(obt_display
, self
->icon
);
540 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
541 RrAppearance
*bullet_a
;
542 XMoveResizeWindow(obt_display
, self
->bullet
,
543 self
->frame
->text_x
+ self
->frame
->text_w
-
544 ITEM_HEIGHT
+ PADDING
, PADDING
,
545 ITEM_HEIGHT
- 2*PADDING
,
546 ITEM_HEIGHT
- 2*PADDING
);
547 bullet_a
= (self
== self
->frame
->selected
?
548 ob_rr_theme
->a_menu_bullet_selected
:
549 ob_rr_theme
->a_menu_bullet_normal
);
550 bullet_a
->surface
.parent
= item_a
;
551 bullet_a
->surface
.parentx
=
552 self
->frame
->text_x
+ self
->frame
->text_w
- ITEM_HEIGHT
+ PADDING
;
553 bullet_a
->surface
.parenty
= PADDING
;
554 RrPaint(bullet_a
, self
->bullet
,
555 ITEM_HEIGHT
- 2*PADDING
,
556 ITEM_HEIGHT
- 2*PADDING
);
557 XMapWindow(obt_display
, self
->bullet
);
559 XUnmapWindow(obt_display
, self
->bullet
);
564 /*! this code is taken from the menu_frame_render. if that changes, this won't
566 static gint
menu_entry_frame_get_height(ObMenuEntryFrame
*self
,
567 gboolean first_entry
,
576 t
= self
->entry
->type
;
578 /* this is the More... entry, it's NORMAL type */
579 t
= OB_MENU_ENTRY_TYPE_NORMAL
;
582 case OB_MENU_ENTRY_TYPE_NORMAL
:
583 case OB_MENU_ENTRY_TYPE_SUBMENU
:
584 h
+= ob_rr_theme
->menu_font_height
;
586 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
587 if (self
->entry
->data
.separator
.label
!= NULL
) {
588 h
+= ob_rr_theme
->menu_title_height
+
589 (ob_rr_theme
->mbwidth
- PADDING
) * 2;
591 /* if the first entry is a labeled separator, then make its border
592 overlap with the menu's outside border */
594 h
-= ob_rr_theme
->mbwidth
;
595 /* if the last entry is a labeled separator, then make its border
596 overlap with the menu's outside border */
598 h
-= ob_rr_theme
->mbwidth
;
600 h
+= SEPARATOR_HEIGHT
;
608 void menu_frame_render(ObMenuFrame
*self
)
611 gint tw
, th
; /* temps */
613 gboolean has_icon
= FALSE
;
617 /* find text dimensions */
619 STRUT_SET(self
->item_margin
, 0, 0, 0, 0);
624 e
= self
->entries
->data
;
625 ob_rr_theme
->a_menu_text_normal
->texture
[0].data
.text
.string
= "";
626 tw
= RrMinWidth(ob_rr_theme
->a_menu_text_normal
);
631 RrMargins(ob_rr_theme
->a_menu_normal
, &l
, &t
, &r
, &b
);
632 STRUT_SET(self
->item_margin
,
633 MAX(self
->item_margin
.left
, l
),
634 MAX(self
->item_margin
.top
, t
),
635 MAX(self
->item_margin
.right
, r
),
636 MAX(self
->item_margin
.bottom
, b
));
637 RrMargins(ob_rr_theme
->a_menu_selected
, &l
, &t
, &r
, &b
);
638 STRUT_SET(self
->item_margin
,
639 MAX(self
->item_margin
.left
, l
),
640 MAX(self
->item_margin
.top
, t
),
641 MAX(self
->item_margin
.right
, r
),
642 MAX(self
->item_margin
.bottom
, b
));
643 RrMargins(ob_rr_theme
->a_menu_disabled
, &l
, &t
, &r
, &b
);
644 STRUT_SET(self
->item_margin
,
645 MAX(self
->item_margin
.left
, l
),
646 MAX(self
->item_margin
.top
, t
),
647 MAX(self
->item_margin
.right
, r
),
648 MAX(self
->item_margin
.bottom
, b
));
649 RrMargins(ob_rr_theme
->a_menu_disabled_selected
, &l
, &t
, &r
, &b
);
650 STRUT_SET(self
->item_margin
,
651 MAX(self
->item_margin
.left
, l
),
652 MAX(self
->item_margin
.top
, t
),
653 MAX(self
->item_margin
.right
, r
),
654 MAX(self
->item_margin
.bottom
, b
));
657 /* render the entries */
659 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
660 RrAppearance
*text_a
;
663 /* if the first entry is a labeled separator, then make its border
664 overlap with the menu's outside border */
665 if (it
== self
->entries
&&
666 e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
667 e
->entry
->data
.separator
.label
)
669 h
-= ob_rr_theme
->mbwidth
;
672 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
673 e
->entry
->data
.separator
.label
)
675 e
->border
= ob_rr_theme
->mbwidth
;
678 RECT_SET_POINT(e
->area
, 0, h
+e
->border
);
679 XMoveWindow(obt_display
, e
->window
,
680 e
->area
.x
-e
->border
, e
->area
.y
-e
->border
);
681 XSetWindowBorderWidth(obt_display
, e
->window
, e
->border
);
682 XSetWindowBorder(obt_display
, e
->window
,
683 RrColorPixel(ob_rr_theme
->menu_border_color
));
686 text_a
= (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
687 !e
->entry
->data
.normal
.enabled
?
689 (e
== self
->selected
?
690 ob_rr_theme
->a_menu_text_disabled_selected
:
691 ob_rr_theme
->a_menu_text_disabled
) :
693 (e
== self
->selected
?
694 ob_rr_theme
->a_menu_text_selected
:
695 ob_rr_theme
->a_menu_text_normal
));
696 switch (e
->entry
->type
) {
697 case OB_MENU_ENTRY_TYPE_NORMAL
:
698 text_a
->texture
[0].data
.text
.string
= e
->entry
->data
.normal
.label
;
699 tw
= RrMinWidth(text_a
);
700 tw
= MIN(tw
, MAX_MENU_WIDTH
);
701 th
= ob_rr_theme
->menu_font_height
;
703 if (e
->entry
->data
.normal
.icon
||
704 e
->entry
->data
.normal
.mask
)
707 case OB_MENU_ENTRY_TYPE_SUBMENU
:
708 sub
= e
->entry
->data
.submenu
.submenu
;
709 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
710 tw
= RrMinWidth(text_a
);
711 tw
= MIN(tw
, MAX_MENU_WIDTH
);
712 th
= ob_rr_theme
->menu_font_height
;
714 if (e
->entry
->data
.normal
.icon
||
715 e
->entry
->data
.normal
.mask
)
718 tw
+= ITEM_HEIGHT
- PADDING
;
720 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
721 if (e
->entry
->data
.separator
.label
!= NULL
) {
722 ob_rr_theme
->a_menu_text_title
->texture
[0].data
.text
.string
=
723 e
->entry
->data
.separator
.label
;
724 tw
= RrMinWidth(ob_rr_theme
->a_menu_text_title
) +
725 2*ob_rr_theme
->paddingx
;
726 tw
= MIN(tw
, MAX_MENU_WIDTH
);
727 th
= ob_rr_theme
->menu_title_height
+
728 (ob_rr_theme
->mbwidth
- PADDING
) *2;
731 th
= SEPARATOR_HEIGHT
;
741 /* if the last entry is a labeled separator, then make its border
742 overlap with the menu's outside border */
743 it
= g_list_last(self
->entries
);
744 e
= it
? it
->data
: NULL
;
745 if (e
&& e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
746 e
->entry
->data
.separator
.label
)
748 h
-= ob_rr_theme
->mbwidth
;
751 self
->text_x
= PADDING
;
756 w
+= ITEM_HEIGHT
+ PADDING
;
757 self
->text_x
+= ITEM_HEIGHT
+ PADDING
;
764 XResizeWindow(obt_display
, self
->window
, w
, h
);
768 RrPaint(self
->a_items
, self
->window
, w
, h
);
770 for (it
= self
->entries
; it
; it
= g_list_next(it
))
771 menu_entry_frame_render(it
->data
);
773 w
+= ob_rr_theme
->mbwidth
* 2;
774 h
+= ob_rr_theme
->mbwidth
* 2;
776 RECT_SET_SIZE(self
->area
, w
, h
);
781 static void menu_frame_update(ObMenuFrame
*self
)
787 menu_pipe_execute(self
->menu
);
788 menu_find_submenus(self
->menu
);
790 self
->selected
= NULL
;
792 /* start at show_from */
793 mit
= g_list_nth(self
->menu
->entries
, self
->show_from
);
795 /* go through the menu's and frame's entries and connect the frame entries
796 to the menu entries */
797 for (fit
= self
->entries
; mit
&& fit
;
798 mit
= g_list_next(mit
), fit
= g_list_next(fit
))
800 ObMenuEntryFrame
*f
= fit
->data
;
801 f
->entry
= mit
->data
;
804 /* if there are more menu entries than in the frame, add them */
806 ObMenuEntryFrame
*e
= menu_entry_frame_new(mit
->data
, self
);
807 self
->entries
= g_list_append(self
->entries
, e
);
808 mit
= g_list_next(mit
);
811 /* if there are more frame entries than menu entries then get rid of
814 GList
*n
= g_list_next(fit
);
815 menu_entry_frame_free(fit
->data
);
816 self
->entries
= g_list_delete_link(self
->entries
, fit
);
820 /* * make the menu fit on the screen */
822 /* calculate the height of the menu */
824 for (fit
= self
->entries
; fit
; fit
= g_list_next(fit
))
825 h
+= menu_entry_frame_get_height(fit
->data
,
826 fit
== self
->entries
,
827 g_list_next(fit
) == NULL
);
828 /* add the border at the top and bottom */
829 h
+= ob_rr_theme
->mbwidth
* 2;
831 a
= screen_physical_area_monitor(self
->monitor
);
835 gboolean last_entry
= TRUE
;
837 /* take the height of our More... entry into account */
838 h
+= menu_entry_frame_get_height(NULL
, FALSE
, TRUE
);
840 /* start at the end of the entries */
841 flast
= g_list_last(self
->entries
);
843 /* pull out all the entries from the frame that don't
844 fit on the screen, leaving at least 1 though */
845 while (h
> a
->height
&& g_list_previous(flast
) != NULL
) {
846 /* update the height, without this entry */
847 h
-= menu_entry_frame_get_height(flast
->data
, FALSE
, last_entry
);
849 /* destroy the entry we're not displaying */
851 flast
= g_list_previous(flast
);
852 menu_entry_frame_free(tmp
->data
);
853 self
->entries
= g_list_delete_link(self
->entries
, tmp
);
855 /* only the first one that we see is the last entry in the menu */
860 ObMenuEntry
*more_entry
;
861 ObMenuEntryFrame
*more_frame
;
862 /* make the More... menu entry frame which will display in this
864 if self->menu->more_menu is NULL that means that this is already
865 More... menu, so just use ourself.
867 more_entry
= menu_get_more((self
->menu
->more_menu
?
868 self
->menu
->more_menu
:
870 /* continue where we left off */
872 g_list_length(self
->entries
));
873 more_frame
= menu_entry_frame_new(more_entry
, self
);
874 /* make it get deleted when the menu frame goes away */
875 menu_entry_unref(more_entry
);
877 /* add our More... entry to the frame */
878 self
->entries
= g_list_append(self
->entries
, more_frame
);
884 menu_frame_render(self
);
887 static gboolean
menu_frame_is_visible(ObMenuFrame
*self
)
889 return !!(g_list_find(menu_frame_visible
, self
));
892 static gboolean
menu_frame_show(ObMenuFrame
*self
)
896 /* determine if the underlying menu is already visible */
897 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
898 ObMenuFrame
*f
= it
->data
;
899 if (f
->menu
== self
->menu
)
903 if (self
->menu
->update_func
)
904 if (!self
->menu
->update_func(self
, self
->menu
->data
))
908 if (menu_frame_visible
== NULL
) {
909 /* no menus shown yet */
911 /* grab the pointer in such a way as to pass through "owner events"
912 so that we can get enter/leave notifies in the menu. */
913 if (!grab_pointer(TRUE
, FALSE
, OB_CURSOR_POINTER
))
915 if (!grab_keyboard()) {
921 menu_frame_update(self
);
923 menu_frame_visible
= g_list_prepend(menu_frame_visible
, self
);
925 if (self
->menu
->show_func
)
926 self
->menu
->show_func(self
, self
->menu
->data
);
931 gboolean
menu_frame_show_topmenu(ObMenuFrame
*self
, gint x
, gint y
,
937 if (menu_frame_is_visible(self
))
939 if (!menu_frame_show(self
))
942 /* find the monitor the menu is on */
943 for (i
= 0; i
< screen_num_monitors
; ++i
) {
944 Rect
*a
= screen_physical_area_monitor(i
);
945 gboolean contains
= RECT_CONTAINS(*a
, x
, y
);
953 if (self
->menu
->place_func
)
954 self
->menu
->place_func(self
, &x
, &y
, mouse
, self
->menu
->data
);
956 menu_frame_place_topmenu(self
, &x
, &y
);
958 menu_frame_move(self
, x
, y
);
960 XMapWindow(obt_display
, self
->window
);
962 if (screen_pointer_pos(&px
, &py
)) {
963 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
964 if (e
&& e
->frame
== self
)
971 gboolean
menu_frame_show_submenu(ObMenuFrame
*self
, ObMenuFrame
*parent
,
972 ObMenuEntryFrame
*parent_entry
)
977 if (menu_frame_is_visible(self
))
980 self
->monitor
= parent
->monitor
;
981 self
->parent
= parent
;
982 self
->parent_entry
= parent_entry
;
984 /* set up parent's child to be us */
986 menu_frame_hide(parent
->child
);
987 parent
->child
= self
;
989 if (!menu_frame_show(self
))
992 menu_frame_place_submenu(self
, &x
, &y
);
993 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
996 /*try the other side */
997 self
->direction_right
= !self
->direction_right
;
998 menu_frame_place_submenu(self
, &x
, &y
);
999 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1001 menu_frame_move(self
, x
+ dx
, y
+ dy
);
1003 XMapWindow(obt_display
, self
->window
);
1005 if (screen_pointer_pos(&px
, &py
)) {
1006 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
1007 if (e
&& e
->frame
== self
)
1014 static void menu_frame_hide(ObMenuFrame
*self
)
1016 GList
*it
= g_list_find(menu_frame_visible
, self
);
1021 if (self
->menu
->hide_func
)
1022 self
->menu
->hide_func(self
, self
->menu
->data
);
1025 menu_frame_hide(self
->child
);
1028 self
->parent
->child
= NULL
;
1029 self
->parent
= NULL
;
1030 self
->parent_entry
= NULL
;
1032 menu_frame_visible
= g_list_delete_link(menu_frame_visible
, it
);
1034 if (menu_frame_visible
== NULL
) {
1035 /* last menu shown */
1040 XUnmapWindow(obt_display
, self
->window
);
1042 menu_frame_free(self
);
1045 void menu_frame_hide_all(void)
1049 if (config_submenu_show_delay
) {
1050 /* remove any submenu open requests */
1051 obt_main_loop_timeout_remove(ob_main_loop
,
1052 menu_entry_frame_submenu_timeout
);
1054 if ((it
= g_list_last(menu_frame_visible
)))
1055 menu_frame_hide(it
->data
);
1058 void menu_frame_hide_all_client(ObClient
*client
)
1060 GList
*it
= g_list_last(menu_frame_visible
);
1062 ObMenuFrame
*f
= it
->data
;
1063 if (f
->client
== client
) {
1064 if (config_submenu_show_delay
) {
1065 /* remove any submenu open requests */
1066 obt_main_loop_timeout_remove(ob_main_loop
,
1067 menu_entry_frame_submenu_timeout
);
1075 ObMenuFrame
* menu_frame_under(gint x
, gint y
)
1077 ObMenuFrame
*ret
= NULL
;
1080 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
1081 ObMenuFrame
*f
= it
->data
;
1083 if (RECT_CONTAINS(f
->area
, x
, y
)) {
1091 ObMenuEntryFrame
* menu_entry_frame_under(gint x
, gint y
)
1094 ObMenuEntryFrame
*ret
= NULL
;
1097 if ((frame
= menu_frame_under(x
, y
))) {
1098 x
-= ob_rr_theme
->mbwidth
+ frame
->area
.x
;
1099 y
-= ob_rr_theme
->mbwidth
+ frame
->area
.y
;
1101 for (it
= frame
->entries
; it
; it
= g_list_next(it
)) {
1102 ObMenuEntryFrame
*e
= it
->data
;
1104 if (RECT_CONTAINS(e
->area
, x
, y
)) {
1113 static gboolean
menu_entry_frame_submenu_timeout(gpointer data
)
1115 g_assert(menu_frame_visible
);
1116 menu_entry_frame_show_submenu((ObMenuEntryFrame
*)data
);
1120 void menu_frame_select(ObMenuFrame
*self
, ObMenuEntryFrame
*entry
,
1123 ObMenuEntryFrame
*old
= self
->selected
;
1124 ObMenuFrame
*oldchild
= self
->child
;
1126 if (entry
&& entry
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
1129 if (old
== entry
) return;
1131 if (config_submenu_show_delay
) {
1132 /* remove any submenu open requests */
1133 obt_main_loop_timeout_remove(ob_main_loop
,
1134 menu_entry_frame_submenu_timeout
);
1137 self
->selected
= entry
;
1140 menu_entry_frame_render(old
);
1142 menu_frame_hide(oldchild
);
1144 if (self
->selected
) {
1145 menu_entry_frame_render(self
->selected
);
1147 if (self
->selected
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
1148 if (config_submenu_show_delay
&& !immediate
) {
1149 /* initiate a new submenu open request */
1150 obt_main_loop_timeout_add(ob_main_loop
,
1151 config_submenu_show_delay
* 1000,
1152 menu_entry_frame_submenu_timeout
,
1153 self
->selected
, g_direct_equal
,
1156 menu_entry_frame_show_submenu(self
->selected
);
1162 void menu_entry_frame_show_submenu(ObMenuEntryFrame
*self
)
1166 if (!self
->entry
->data
.submenu
.submenu
) return;
1168 f
= menu_frame_new(self
->entry
->data
.submenu
.submenu
,
1169 self
->entry
->data
.submenu
.show_from
,
1170 self
->frame
->client
);
1171 /* pass our direction on to our child */
1172 f
->direction_right
= self
->frame
->direction_right
;
1174 menu_frame_show_submenu(f
, self
->frame
, self
);
1177 void menu_entry_frame_execute(ObMenuEntryFrame
*self
, guint state
)
1179 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1180 self
->entry
->data
.normal
.enabled
)
1182 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1184 ObMenuEntry
*entry
= self
->entry
;
1185 ObMenuExecuteFunc func
= self
->frame
->menu
->execute_func
;
1186 gpointer data
= self
->frame
->menu
->data
;
1187 GSList
*acts
= self
->entry
->data
.normal
.actions
;
1188 ObClient
*client
= self
->frame
->client
;
1189 ObMenuFrame
*frame
= self
->frame
;
1191 /* release grabs before executing the shit */
1192 if (!(state
& ControlMask
)) {
1193 menu_frame_hide_all();
1198 func(entry
, frame
, client
, state
, data
);
1200 actions_run_acts(acts
, OB_USER_ACTION_MENU_SELECTION
,
1201 state
, -1, -1, 0, OB_FRAME_CONTEXT_NONE
, client
);
1205 void menu_frame_select_previous(ObMenuFrame
*self
)
1207 GList
*it
= NULL
, *start
;
1209 if (self
->entries
) {
1210 start
= it
= g_list_find(self
->entries
, self
->selected
);
1212 ObMenuEntryFrame
*e
;
1214 it
= it
? g_list_previous(it
) : g_list_last(self
->entries
);
1220 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1222 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1227 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);
1230 void menu_frame_select_next(ObMenuFrame
*self
)
1232 GList
*it
= NULL
, *start
;
1234 if (self
->entries
) {
1235 start
= it
= g_list_find(self
->entries
, self
->selected
);
1237 ObMenuEntryFrame
*e
;
1239 it
= it
? g_list_next(it
) : self
->entries
;
1245 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1247 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1252 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);