1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 menuframe.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "menuframe.h"
28 #include "render/theme.h"
31 #define SEPARATOR_HEIGHT 3
32 #define MAX_MENU_WIDTH 400
34 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
36 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
37 ButtonPressMask | ButtonReleaseMask)
39 GList
*menu_frame_visible
;
41 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
43 static void menu_entry_frame_free(ObMenuEntryFrame
*self
);
44 static void menu_frame_render(ObMenuFrame
*self
);
45 static void menu_frame_update(ObMenuFrame
*self
);
46 static gboolean
menu_entry_frame_submenu_timeout(gpointer data
);
48 static Window
createWindow(Window parent
, gulong mask
,
49 XSetWindowAttributes
*attrib
)
51 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
52 RrDepth(ob_rr_inst
), InputOutput
,
53 RrVisual(ob_rr_inst
), mask
, attrib
);
56 GHashTable
*menu_frame_map
;
58 void menu_frame_startup(gboolean reconfig
)
62 menu_frame_map
= g_hash_table_new(g_int_hash
, g_int_equal
);
65 void menu_frame_shutdown(gboolean reconfig
)
69 g_hash_table_destroy(menu_frame_map
);
72 ObMenuFrame
* menu_frame_new(ObMenu
*menu
, ObClient
*client
)
75 XSetWindowAttributes attr
;
77 self
= g_new0(ObMenuFrame
, 1);
78 self
->type
= Window_Menu
;
80 self
->selected
= NULL
;
81 self
->client
= client
;
82 self
->direction_right
= TRUE
;
84 attr
.event_mask
= FRAME_EVENTMASK
;
85 self
->window
= createWindow(RootWindow(ob_display
, ob_screen
),
88 self
->a_title
= RrAppearanceCopy(ob_rr_theme
->a_menu_title
);
89 self
->a_items
= RrAppearanceCopy(ob_rr_theme
->a_menu
);
91 stacking_add(MENU_AS_WINDOW(self
));
96 void menu_frame_free(ObMenuFrame
*self
)
99 while (self
->entries
) {
100 menu_entry_frame_free(self
->entries
->data
);
101 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
104 stacking_remove(MENU_AS_WINDOW(self
));
106 XDestroyWindow(ob_display
, self
->window
);
108 RrAppearanceFree(self
->a_items
);
109 RrAppearanceFree(self
->a_title
);
115 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
118 ObMenuEntryFrame
*self
;
119 XSetWindowAttributes attr
;
121 self
= g_new0(ObMenuEntryFrame
, 1);
125 attr
.event_mask
= ENTRY_EVENTMASK
;
126 self
->window
= createWindow(self
->frame
->window
, CWEventMask
, &attr
);
127 self
->text
= createWindow(self
->window
, 0, NULL
);
128 g_hash_table_insert(menu_frame_map
, &self
->window
, self
);
129 g_hash_table_insert(menu_frame_map
, &self
->text
, self
);
130 if (entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
131 self
->icon
= createWindow(self
->window
, 0, NULL
);
132 g_hash_table_insert(menu_frame_map
, &self
->icon
, self
);
134 if (entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
135 self
->bullet
= createWindow(self
->window
, 0, NULL
);
136 g_hash_table_insert(menu_frame_map
, &self
->bullet
, self
);
139 XMapWindow(ob_display
, self
->window
);
140 XMapWindow(ob_display
, self
->text
);
142 self
->a_normal
= RrAppearanceCopy(ob_rr_theme
->a_menu_normal
);
143 self
->a_disabled
= RrAppearanceCopy(ob_rr_theme
->a_menu_disabled
);
144 self
->a_selected
= RrAppearanceCopy(ob_rr_theme
->a_menu_selected
);
146 if (entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
) {
147 self
->a_separator
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
148 self
->a_separator
->texture
[0].type
= RR_TEXTURE_LINE_ART
;
150 self
->a_icon
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
151 self
->a_icon
->texture
[0].type
= RR_TEXTURE_RGBA
;
152 self
->a_mask
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
153 self
->a_mask
->texture
[0].type
= RR_TEXTURE_MASK
;
154 self
->a_bullet_normal
=
155 RrAppearanceCopy(ob_rr_theme
->a_menu_bullet_normal
);
156 self
->a_bullet_selected
=
157 RrAppearanceCopy(ob_rr_theme
->a_menu_bullet_selected
);
160 self
->a_text_normal
=
161 RrAppearanceCopy(ob_rr_theme
->a_menu_text_normal
);
162 self
->a_text_disabled
=
163 RrAppearanceCopy(ob_rr_theme
->a_menu_text_disabled
);
164 self
->a_text_selected
=
165 RrAppearanceCopy(ob_rr_theme
->a_menu_text_selected
);
167 RrAppearanceCopy(ob_rr_theme
->a_menu_text_title
);
172 static void menu_entry_frame_free(ObMenuEntryFrame
*self
)
175 XDestroyWindow(ob_display
, self
->text
);
176 XDestroyWindow(ob_display
, self
->window
);
177 g_hash_table_remove(menu_frame_map
, &self
->text
);
178 g_hash_table_remove(menu_frame_map
, &self
->window
);
179 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
180 XDestroyWindow(ob_display
, self
->icon
);
181 g_hash_table_remove(menu_frame_map
, &self
->icon
);
183 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
184 XDestroyWindow(ob_display
, self
->bullet
);
185 g_hash_table_remove(menu_frame_map
, &self
->bullet
);
188 RrAppearanceFree(self
->a_normal
);
189 RrAppearanceFree(self
->a_disabled
);
190 RrAppearanceFree(self
->a_selected
);
192 RrAppearanceFree(self
->a_separator
);
193 RrAppearanceFree(self
->a_icon
);
194 RrAppearanceFree(self
->a_mask
);
195 RrAppearanceFree(self
->a_text_normal
);
196 RrAppearanceFree(self
->a_text_disabled
);
197 RrAppearanceFree(self
->a_text_selected
);
198 RrAppearanceFree(self
->a_text_title
);
199 RrAppearanceFree(self
->a_bullet_normal
);
200 RrAppearanceFree(self
->a_bullet_selected
);
206 void menu_frame_move(ObMenuFrame
*self
, gint x
, gint y
)
208 RECT_SET_POINT(self
->area
, x
, y
);
209 XMoveWindow(ob_display
, self
->window
, self
->area
.x
, self
->area
.y
);
212 static void menu_frame_place_topmenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
216 if (config_menu_middle
) {
220 *y
-= self
->area
.height
/ 2;
222 /* try to the right of the cursor */
223 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
225 /* try to the left of the cursor */
226 myx
= *x
- self
->area
.width
;
227 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
230 /* if didnt fit on either side so just use what it says */
232 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
242 /* try to the bottom right of the cursor */
243 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
244 if (dx
!= 0 || dy
!= 0) {
245 /* try to the bottom left of the cursor */
246 myx
= *x
- self
->area
.width
;
248 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
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
, *y
, &dx
, &dy
);
256 if (dx
!= 0 || dy
!= 0) {
257 /* try to the top left of the cursor */
258 myx
= *x
- self
->area
.width
;
259 myy
= *y
- self
->area
.height
;
260 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
262 if (dx
!= 0 || dy
!= 0) {
263 /* if didnt fit on either side so just use what it says */
266 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
273 static void menu_frame_place_submenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
278 overlap
= ob_rr_theme
->menu_overlap
;
279 bwidth
= ob_rr_theme
->mbwidth
;
281 if (self
->direction_right
)
282 *x
= self
->parent
->area
.x
+ self
->parent
->area
.width
-
285 *x
= self
->parent
->area
.x
- self
->area
.width
+ overlap
+ bwidth
;
287 *y
= self
->parent
->area
.y
+ self
->parent_entry
->area
.y
;
288 if (config_menu_middle
)
289 *y
-= (self
->area
.height
- (bwidth
* 2) - self
->item_h
) / 2;
294 void menu_frame_move_on_screen(ObMenuFrame
*self
, gint x
, gint y
,
302 a
= screen_physical_area_monitor(self
->monitor
);
304 half
= g_list_length(self
->entries
) / 2;
305 pos
= g_list_index(self
->entries
, self
->selected
);
307 /* if in the bottom half then check this stuff first, will keep the bottom
308 edge of the menu visible */
310 *dx
= MAX(*dx
, a
->x
- x
);
311 *dy
= MAX(*dy
, a
->y
- y
);
313 *dx
= MIN(*dx
, (a
->x
+ a
->width
) - (x
+ self
->area
.width
));
314 *dy
= MIN(*dy
, (a
->y
+ a
->height
) - (y
+ self
->area
.height
));
315 /* if in the top half then check this stuff last, will keep the top
316 edge of the menu visible */
318 *dx
= MAX(*dx
, a
->x
- x
);
319 *dy
= MAX(*dy
, a
->y
- y
);
323 static void menu_entry_frame_render(ObMenuEntryFrame
*self
)
325 RrAppearance
*item_a
, *text_a
;
328 ObMenuFrame
*frame
= self
->frame
;
330 switch (self
->entry
->type
) {
331 case OB_MENU_ENTRY_TYPE_NORMAL
:
332 case OB_MENU_ENTRY_TYPE_SUBMENU
:
333 item_a
= ((self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
334 !self
->entry
->data
.normal
.enabled
) ?
336 (self
== self
->frame
->selected
?
339 th
= self
->frame
->item_h
;
341 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
342 if (self
->entry
->data
.separator
.label
) {
343 item_a
= self
->frame
->a_title
;
344 th
= ob_rr_theme
->menu_title_height
;
346 item_a
= self
->a_normal
;
347 th
= SEPARATOR_HEIGHT
+ 2*PADDING
;
351 g_assert_not_reached();
353 RECT_SET_SIZE(self
->area
, self
->frame
->inner_w
, th
);
354 XResizeWindow(ob_display
, self
->window
,
355 self
->area
.width
, self
->area
.height
);
356 item_a
->surface
.parent
= self
->frame
->a_items
;
357 item_a
->surface
.parentx
= self
->area
.x
;
358 item_a
->surface
.parenty
= self
->area
.y
;
359 RrPaint(item_a
, self
->window
, self
->area
.width
, self
->area
.height
);
361 switch (self
->entry
->type
) {
362 case OB_MENU_ENTRY_TYPE_NORMAL
:
363 text_a
= (!self
->entry
->data
.normal
.enabled
?
364 self
->a_text_disabled
:
365 (self
== self
->frame
->selected
?
366 self
->a_text_selected
:
367 self
->a_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_position
> 0))
373 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
374 text_a
->texture
[0].data
.text
.shortcut_pos
=
375 self
->entry
->data
.normal
.shortcut_position
;
377 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
379 case OB_MENU_ENTRY_TYPE_SUBMENU
:
380 text_a
= (self
== self
->frame
->selected
?
381 self
->a_text_selected
:
382 self
->a_text_normal
);
383 sub
= self
->entry
->data
.submenu
.submenu
;
384 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
385 if (sub
->shortcut
&& (self
->frame
->menu
->show_all_shortcuts
||
386 sub
->shortcut_position
> 0))
388 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
389 text_a
->texture
[0].data
.text
.shortcut_pos
= sub
->shortcut_position
;
391 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
393 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
394 if (self
->entry
->data
.separator
.label
!= NULL
)
395 text_a
= self
->a_text_title
;
397 text_a
= self
->a_text_normal
;
401 switch (self
->entry
->type
) {
402 case OB_MENU_ENTRY_TYPE_NORMAL
:
403 XMoveResizeWindow(ob_display
, self
->text
,
404 self
->frame
->text_x
, PADDING
,
406 self
->frame
->item_h
- 2*PADDING
);
407 text_a
->surface
.parent
= item_a
;
408 text_a
->surface
.parentx
= self
->frame
->text_x
;
409 text_a
->surface
.parenty
= PADDING
;
410 RrPaint(text_a
, self
->text
, self
->frame
->text_w
,
411 self
->frame
->item_h
- 2*PADDING
);
413 case OB_MENU_ENTRY_TYPE_SUBMENU
:
414 XMoveResizeWindow(ob_display
, self
->text
,
415 self
->frame
->text_x
, PADDING
,
416 self
->frame
->text_w
- self
->frame
->item_h
,
417 self
->frame
->item_h
- 2*PADDING
);
418 text_a
->surface
.parent
= item_a
;
419 text_a
->surface
.parentx
= self
->frame
->text_x
;
420 text_a
->surface
.parenty
= PADDING
;
421 RrPaint(text_a
, self
->text
, self
->frame
->text_w
- self
->frame
->item_h
,
422 self
->frame
->item_h
- 2*PADDING
);
424 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
425 if (self
->entry
->data
.separator
.label
!= NULL
) {
426 /* labeled separator */
427 XMoveResizeWindow(ob_display
, self
->text
,
428 ob_rr_theme
->paddingx
, ob_rr_theme
->paddingy
,
429 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
430 ob_rr_theme
->menu_title_height
-
431 2*ob_rr_theme
->paddingy
);
432 text_a
->surface
.parent
= item_a
;
433 text_a
->surface
.parentx
= ob_rr_theme
->paddingx
;
434 text_a
->surface
.parenty
= ob_rr_theme
->paddingy
;
435 RrPaint(text_a
, self
->text
,
436 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
437 ob_rr_theme
->menu_title_height
-
438 2*ob_rr_theme
->paddingy
);
440 /* unlabeled separaator */
441 XMoveResizeWindow(ob_display
, self
->text
, PADDING
, PADDING
,
442 self
->area
.width
- 2*PADDING
, SEPARATOR_HEIGHT
);
443 self
->a_separator
->surface
.parent
= item_a
;
444 self
->a_separator
->surface
.parentx
= PADDING
;
445 self
->a_separator
->surface
.parenty
= PADDING
;
446 self
->a_separator
->texture
[0].data
.lineart
.color
=
447 text_a
->texture
[0].data
.text
.color
;
448 self
->a_separator
->texture
[0].data
.lineart
.x1
= 2*PADDING
;
449 self
->a_separator
->texture
[0].data
.lineart
.y1
= SEPARATOR_HEIGHT
/2;
450 self
->a_separator
->texture
[0].data
.lineart
.x2
=
451 self
->area
.width
- 4*PADDING
;
452 self
->a_separator
->texture
[0].data
.lineart
.y2
= SEPARATOR_HEIGHT
/2;
453 RrPaint(self
->a_separator
, self
->text
,
454 self
->area
.width
- 2*PADDING
, SEPARATOR_HEIGHT
);
459 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
460 self
->entry
->data
.normal
.icon_data
)
462 XMoveResizeWindow(ob_display
, self
->icon
,
463 PADDING
, frame
->item_margin
.top
,
464 self
->frame
->item_h
- frame
->item_margin
.top
465 - frame
->item_margin
.bottom
,
466 self
->frame
->item_h
- frame
->item_margin
.top
467 - frame
->item_margin
.bottom
);
468 self
->a_icon
->texture
[0].data
.rgba
.width
=
469 self
->entry
->data
.normal
.icon_width
;
470 self
->a_icon
->texture
[0].data
.rgba
.height
=
471 self
->entry
->data
.normal
.icon_height
;
472 self
->a_icon
->texture
[0].data
.rgba
.data
=
473 self
->entry
->data
.normal
.icon_data
;
474 self
->a_icon
->surface
.parent
= item_a
;
475 self
->a_icon
->surface
.parentx
= PADDING
;
476 self
->a_icon
->surface
.parenty
= frame
->item_margin
.top
;
477 RrPaint(self
->a_icon
, self
->icon
,
478 self
->frame
->item_h
- frame
->item_margin
.top
479 - frame
->item_margin
.bottom
,
480 self
->frame
->item_h
- frame
->item_margin
.top
481 - frame
->item_margin
.bottom
);
482 XMapWindow(ob_display
, self
->icon
);
483 } else if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
484 self
->entry
->data
.normal
.mask
)
488 XMoveResizeWindow(ob_display
, self
->icon
,
489 PADDING
, frame
->item_margin
.top
,
490 self
->frame
->item_h
- frame
->item_margin
.top
491 - frame
->item_margin
.bottom
,
492 self
->frame
->item_h
- frame
->item_margin
.top
493 - frame
->item_margin
.bottom
);
494 self
->a_mask
->texture
[0].data
.mask
.mask
=
495 self
->entry
->data
.normal
.mask
;
497 c
= ((self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
498 !self
->entry
->data
.normal
.enabled
) ?
499 self
->entry
->data
.normal
.mask_disabled_color
:
500 (self
== self
->frame
->selected
?
501 self
->entry
->data
.normal
.mask_selected_color
:
502 self
->entry
->data
.normal
.mask_normal_color
));
503 self
->a_mask
->texture
[0].data
.mask
.color
= c
;
505 self
->a_mask
->surface
.parent
= item_a
;
506 self
->a_mask
->surface
.parentx
= PADDING
;
507 self
->a_mask
->surface
.parenty
= frame
->item_margin
.top
;
508 RrPaint(self
->a_mask
, self
->icon
,
509 self
->frame
->item_h
- frame
->item_margin
.top
510 - frame
->item_margin
.bottom
,
511 self
->frame
->item_h
- frame
->item_margin
.top
512 - frame
->item_margin
.bottom
);
513 XMapWindow(ob_display
, self
->icon
);
515 XUnmapWindow(ob_display
, self
->icon
);
517 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
518 RrAppearance
*bullet_a
;
519 XMoveResizeWindow(ob_display
, self
->bullet
,
520 self
->frame
->text_x
+ self
->frame
->text_w
521 - self
->frame
->item_h
+ PADDING
, PADDING
,
522 self
->frame
->item_h
- 2*PADDING
,
523 self
->frame
->item_h
- 2*PADDING
);
524 bullet_a
= (self
== self
->frame
->selected
?
525 self
->a_bullet_selected
:
526 self
->a_bullet_normal
);
527 bullet_a
->surface
.parent
= item_a
;
528 bullet_a
->surface
.parentx
=
529 self
->frame
->text_x
+ self
->frame
->text_w
- self
->frame
->item_h
531 bullet_a
->surface
.parenty
= PADDING
;
532 RrPaint(bullet_a
, self
->bullet
,
533 self
->frame
->item_h
- 2*PADDING
,
534 self
->frame
->item_h
- 2*PADDING
);
535 XMapWindow(ob_display
, self
->bullet
);
537 XUnmapWindow(ob_display
, self
->bullet
);
542 static void menu_frame_render(ObMenuFrame
*self
)
545 gint tw
, th
; /* temps */
547 gboolean has_icon
= FALSE
;
551 XSetWindowBorderWidth(ob_display
, self
->window
, ob_rr_theme
->mbwidth
);
552 XSetWindowBorder(ob_display
, self
->window
,
553 RrColorPixel(ob_rr_theme
->menu_b_color
));
555 /* find text dimensions */
557 STRUT_SET(self
->item_margin
, 0, 0, 0, 0);
560 ObMenuEntryFrame
*e
= self
->entries
->data
;
563 e
->a_text_normal
->texture
[0].data
.text
.string
= "";
564 RrMinsize(e
->a_text_normal
, &tw
, &th
);
569 RrMargins(e
->a_normal
, &l
, &t
, &r
, &b
);
570 STRUT_SET(self
->item_margin
,
571 MAX(self
->item_margin
.left
, l
),
572 MAX(self
->item_margin
.top
, t
),
573 MAX(self
->item_margin
.right
, r
),
574 MAX(self
->item_margin
.bottom
, b
));
575 RrMargins(e
->a_selected
, &l
, &t
, &r
, &b
);
576 STRUT_SET(self
->item_margin
,
577 MAX(self
->item_margin
.left
, l
),
578 MAX(self
->item_margin
.top
, t
),
579 MAX(self
->item_margin
.right
, r
),
580 MAX(self
->item_margin
.bottom
, b
));
581 RrMargins(e
->a_disabled
, &l
, &t
, &r
, &b
);
582 STRUT_SET(self
->item_margin
,
583 MAX(self
->item_margin
.left
, l
),
584 MAX(self
->item_margin
.top
, t
),
585 MAX(self
->item_margin
.right
, r
),
586 MAX(self
->item_margin
.bottom
, b
));
590 /* render the entries */
592 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
593 RrAppearance
*text_a
;
596 /* if the first entry is a labeled separator, then make its border
597 overlap with the menu's outside border */
598 if (it
== self
->entries
&&
599 e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
600 e
->entry
->data
.separator
.label
)
602 h
-= ob_rr_theme
->mbwidth
;
605 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
606 e
->entry
->data
.separator
.label
)
608 e
->border
= ob_rr_theme
->mbwidth
;
611 RECT_SET_POINT(e
->area
, 0, h
+e
->border
);
612 XMoveWindow(ob_display
, e
->window
, e
->area
.x
-e
->border
, e
->area
.y
-e
->border
);
613 XSetWindowBorderWidth(ob_display
, e
->window
, e
->border
);
614 XSetWindowBorder(ob_display
, e
->window
,
615 RrColorPixel(ob_rr_theme
->menu_b_color
));
617 text_a
= ((e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
618 !e
->entry
->data
.normal
.enabled
) ?
620 (e
== self
->selected
?
623 switch (e
->entry
->type
) {
624 case OB_MENU_ENTRY_TYPE_NORMAL
:
625 text_a
->texture
[0].data
.text
.string
= e
->entry
->data
.normal
.label
;
626 RrMinsize(text_a
, &tw
, &th
);
627 tw
= MIN(tw
, MAX_MENU_WIDTH
);
629 if (e
->entry
->data
.normal
.icon_data
||
630 e
->entry
->data
.normal
.mask
)
633 case OB_MENU_ENTRY_TYPE_SUBMENU
:
634 sub
= e
->entry
->data
.submenu
.submenu
;
635 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
636 RrMinsize(text_a
, &tw
, &th
);
637 tw
= MIN(tw
, MAX_MENU_WIDTH
);
639 if (e
->entry
->data
.normal
.icon_data
||
640 e
->entry
->data
.normal
.mask
)
643 tw
+= self
->item_h
- PADDING
;
645 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
646 if (e
->entry
->data
.separator
.label
!= NULL
) {
647 e
->a_text_title
->texture
[0].data
.text
.string
=
648 e
->entry
->data
.separator
.label
;
649 RrMinsize(e
->a_text_title
, &tw
, &th
);
650 tw
= MIN(tw
, MAX_MENU_WIDTH
);
651 th
= ob_rr_theme
->menu_title_height
+
652 (ob_rr_theme
->mbwidth
- PADDING
) *2;
655 th
= SEPARATOR_HEIGHT
;
665 /* if the last entry is a labeled separator, then make its border
666 overlap with the menu's outside border */
667 it
= g_list_last(self
->entries
);
668 e
= it
? it
->data
: NULL
;
669 if (e
&& e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
670 e
->entry
->data
.separator
.label
)
672 h
-= ob_rr_theme
->mbwidth
;
675 self
->text_x
= PADDING
;
680 w
+= self
->item_h
+ PADDING
;
681 self
->text_x
+= self
->item_h
+ PADDING
;
688 XResizeWindow(ob_display
, self
->window
, w
, h
);
692 RrPaint(self
->a_items
, self
->window
, w
, h
);
694 for (it
= self
->entries
; it
; it
= g_list_next(it
))
695 menu_entry_frame_render(it
->data
);
697 w
+= ob_rr_theme
->mbwidth
* 2;
698 h
+= ob_rr_theme
->mbwidth
* 2;
700 RECT_SET_SIZE(self
->area
, w
, h
);
705 static void menu_frame_update(ObMenuFrame
*self
)
709 menu_pipe_execute(self
->menu
);
710 menu_find_submenus(self
->menu
);
712 self
->selected
= NULL
;
714 for (mit
= self
->menu
->entries
, fit
= self
->entries
; mit
&& fit
;
715 mit
= g_list_next(mit
), fit
= g_list_next(fit
))
717 ObMenuEntryFrame
*f
= fit
->data
;
718 f
->entry
= mit
->data
;
722 ObMenuEntryFrame
*e
= menu_entry_frame_new(mit
->data
, self
);
723 self
->entries
= g_list_append(self
->entries
, e
);
724 mit
= g_list_next(mit
);
728 GList
*n
= g_list_next(fit
);
729 menu_entry_frame_free(fit
->data
);
730 self
->entries
= g_list_delete_link(self
->entries
, fit
);
734 menu_frame_render(self
);
737 static gboolean
menu_frame_is_visible(ObMenuFrame
*self
)
739 return !!(g_list_find(menu_frame_visible
, self
));
742 static gboolean
menu_frame_show(ObMenuFrame
*self
)
746 if (menu_frame_visible
== NULL
) {
747 /* no menus shown yet */
748 if (!grab_pointer(TRUE
, TRUE
, OB_CURSOR_POINTER
))
750 if (!grab_keyboard(TRUE
)) {
751 grab_pointer(FALSE
, TRUE
, OB_CURSOR_POINTER
);
756 /* determine if the underlying menu is already visible */
757 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
758 ObMenuFrame
*f
= it
->data
;
759 if (f
->menu
== self
->menu
)
763 if (self
->menu
->update_func
)
764 self
->menu
->update_func(self
, self
->menu
->data
);
767 menu_frame_update(self
);
769 menu_frame_visible
= g_list_prepend(menu_frame_visible
, self
);
774 gboolean
menu_frame_show_topmenu(ObMenuFrame
*self
, gint x
, gint y
,
779 if (menu_frame_is_visible(self
))
781 if (!menu_frame_show(self
))
784 /* find the monitor the menu is on */
785 for (i
= 0; i
< screen_num_monitors
; ++i
) {
786 Rect
*a
= screen_physical_area_monitor(i
);
787 if (RECT_CONTAINS(*a
, x
, y
)) {
793 if (self
->menu
->place_func
)
794 self
->menu
->place_func(self
, &x
, &y
, button
, self
->menu
->data
);
796 menu_frame_place_topmenu(self
, &x
, &y
);
798 menu_frame_move(self
, x
, y
);
800 XMapWindow(ob_display
, self
->window
);
805 gboolean
menu_frame_show_submenu(ObMenuFrame
*self
, ObMenuFrame
*parent
,
806 ObMenuEntryFrame
*parent_entry
)
811 if (menu_frame_is_visible(self
))
814 self
->monitor
= parent
->monitor
;
815 self
->parent
= parent
;
816 self
->parent_entry
= parent_entry
;
818 /* set up parent's child to be us */
820 menu_frame_hide(parent
->child
);
821 parent
->child
= self
;
823 if (!menu_frame_show(self
))
826 menu_frame_place_submenu(self
, &x
, &y
);
827 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
830 /*try the other side */
831 self
->direction_right
= !self
->direction_right
;
832 menu_frame_place_submenu(self
, &x
, &y
);
833 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
835 menu_frame_move(self
, x
+ dx
, y
+ dy
);
837 XMapWindow(ob_display
, self
->window
);
839 if (screen_pointer_pos(&dx
, &dy
) && (e
= menu_entry_frame_under(dx
, dy
)) &&
846 void menu_frame_hide(ObMenuFrame
*self
)
848 GList
*it
= g_list_find(menu_frame_visible
, self
);
854 menu_frame_hide(self
->child
);
857 self
->parent
->child
= NULL
;
859 self
->parent_entry
= NULL
;
861 menu_frame_visible
= g_list_delete_link(menu_frame_visible
, it
);
863 if (menu_frame_visible
== NULL
) {
864 /* last menu shown */
865 grab_pointer(FALSE
, TRUE
, OB_CURSOR_NONE
);
866 grab_keyboard(FALSE
);
869 XUnmapWindow(ob_display
, self
->window
);
871 menu_frame_free(self
);
874 void menu_frame_hide_all()
878 if (config_submenu_show_delay
) {
879 /* remove any submenu open requests */
880 ob_main_loop_timeout_remove(ob_main_loop
,
881 menu_entry_frame_submenu_timeout
);
883 if ((it
= g_list_last(menu_frame_visible
)))
884 menu_frame_hide(it
->data
);
887 void menu_frame_hide_all_client(ObClient
*client
)
889 GList
*it
= g_list_last(menu_frame_visible
);
891 ObMenuFrame
*f
= it
->data
;
892 if (f
->client
== client
)
898 ObMenuFrame
* menu_frame_under(gint x
, gint y
)
900 ObMenuFrame
*ret
= NULL
;
903 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
904 ObMenuFrame
*f
= it
->data
;
906 if (RECT_CONTAINS(f
->area
, x
, y
)) {
914 ObMenuEntryFrame
* menu_entry_frame_under(gint x
, gint y
)
917 ObMenuEntryFrame
*ret
= NULL
;
920 if ((frame
= menu_frame_under(x
, y
))) {
921 x
-= ob_rr_theme
->mbwidth
+ frame
->area
.x
;
922 y
-= ob_rr_theme
->mbwidth
+ frame
->area
.y
;
924 for (it
= frame
->entries
; it
; it
= g_list_next(it
)) {
925 ObMenuEntryFrame
*e
= it
->data
;
927 if (RECT_CONTAINS(e
->area
, x
, y
)) {
936 static gboolean
menu_entry_frame_submenu_timeout(gpointer data
)
938 menu_entry_frame_show_submenu((ObMenuEntryFrame
*)data
);
942 void menu_frame_select(ObMenuFrame
*self
, ObMenuEntryFrame
*entry
,
945 ObMenuEntryFrame
*old
= self
->selected
;
946 ObMenuFrame
*oldchild
= self
->child
;
948 if (entry
&& entry
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
951 if (old
== entry
) return;
953 if (config_submenu_show_delay
) {
954 /* remove any submenu open requests */
955 ob_main_loop_timeout_remove(ob_main_loop
,
956 menu_entry_frame_submenu_timeout
);
959 self
->selected
= entry
;
962 menu_entry_frame_render(old
);
964 menu_frame_hide(oldchild
);
966 if (self
->selected
) {
967 menu_entry_frame_render(self
->selected
);
969 if (self
->selected
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
970 if (config_submenu_show_delay
&& !immediate
) {
971 /* initiate a new submenu open request */
972 ob_main_loop_timeout_add(ob_main_loop
,
973 config_submenu_show_delay
* 1000,
974 menu_entry_frame_submenu_timeout
,
975 self
->selected
, g_direct_equal
,
978 menu_entry_frame_show_submenu(self
->selected
);
984 void menu_entry_frame_show_submenu(ObMenuEntryFrame
*self
)
988 if (!self
->entry
->data
.submenu
.submenu
) return;
990 f
= menu_frame_new(self
->entry
->data
.submenu
.submenu
,
991 self
->frame
->client
);
992 /* pass our direction on to our child */
993 f
->direction_right
= self
->frame
->direction_right
;
995 menu_frame_show_submenu(f
, self
->frame
, self
);
998 void menu_entry_frame_execute(ObMenuEntryFrame
*self
, guint state
, Time time
)
1000 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1001 self
->entry
->data
.normal
.enabled
)
1003 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1005 ObMenuEntry
*entry
= self
->entry
;
1006 ObMenuExecuteFunc func
= self
->frame
->menu
->execute_func
;
1007 gpointer data
= self
->frame
->menu
->data
;
1008 GSList
*acts
= self
->entry
->data
.normal
.actions
;
1009 ObClient
*client
= self
->frame
->client
;
1011 /* release grabs before executing the shit */
1012 if (!(state
& ControlMask
))
1013 menu_frame_hide_all();
1016 func(entry
, state
, data
, time
);
1018 action_run(acts
, client
, state
, time
);
1022 void menu_frame_select_previous(ObMenuFrame
*self
)
1024 GList
*it
= NULL
, *start
;
1026 if (self
->entries
) {
1027 start
= it
= g_list_find(self
->entries
, self
->selected
);
1029 ObMenuEntryFrame
*e
;
1031 it
= it
? g_list_previous(it
) : g_list_last(self
->entries
);
1037 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1039 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1040 e
->entry
->data
.normal
.enabled
)
1045 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);
1048 void menu_frame_select_next(ObMenuFrame
*self
)
1050 GList
*it
= NULL
, *start
;
1052 if (self
->entries
) {
1053 start
= it
= g_list_find(self
->entries
, self
->selected
);
1055 ObMenuEntryFrame
*e
;
1057 it
= it
? g_list_next(it
) : self
->entries
;
1063 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1065 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1066 e
->entry
->data
.normal
.enabled
)
1071 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);