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 ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
36 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
38 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
39 ButtonPressMask | ButtonReleaseMask)
41 GList
*menu_frame_visible
;
43 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
45 static void menu_entry_frame_free(ObMenuEntryFrame
*self
);
46 static void menu_frame_update(ObMenuFrame
*self
);
47 static gboolean
menu_entry_frame_submenu_timeout(gpointer data
);
48 static void menu_frame_hide(ObMenuFrame
*self
);
50 static Window
createWindow(Window parent
, gulong mask
,
51 XSetWindowAttributes
*attrib
)
53 return XCreateWindow(obt_display
, parent
, 0, 0, 1, 1, 0,
54 RrDepth(ob_rr_inst
), InputOutput
,
55 RrVisual(ob_rr_inst
), mask
, attrib
);
58 GHashTable
*menu_frame_map
;
60 void menu_frame_startup(gboolean reconfig
)
64 menu_frame_map
= g_hash_table_new(g_int_hash
, g_int_equal
);
67 void menu_frame_shutdown(gboolean reconfig
)
71 g_hash_table_destroy(menu_frame_map
);
74 ObMenuFrame
* menu_frame_new(ObMenu
*menu
, guint show_from
, ObClient
*client
)
77 XSetWindowAttributes attr
;
79 self
= g_new0(ObMenuFrame
, 1);
80 self
->type
= Window_Menu
;
82 self
->selected
= NULL
;
83 self
->client
= client
;
84 self
->direction_right
= TRUE
;
85 self
->show_from
= show_from
;
87 attr
.event_mask
= FRAME_EVENTMASK
;
88 self
->window
= createWindow(RootWindow(obt_display
, ob_screen
),
91 XSetWindowBorderWidth(obt_display
, self
->window
, ob_rr_theme
->mbwidth
);
92 XSetWindowBorder(obt_display
, self
->window
,
93 RrColorPixel(ob_rr_theme
->menu_border_color
));
95 self
->a_title
= RrAppearanceCopy(ob_rr_theme
->a_menu_title
);
96 self
->a_items
= RrAppearanceCopy(ob_rr_theme
->a_menu
);
98 stacking_add(MENU_AS_WINDOW(self
));
103 void menu_frame_free(ObMenuFrame
*self
)
106 while (self
->entries
) {
107 menu_entry_frame_free(self
->entries
->data
);
108 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
111 stacking_remove(MENU_AS_WINDOW(self
));
113 XDestroyWindow(obt_display
, self
->window
);
115 RrAppearanceFree(self
->a_items
);
116 RrAppearanceFree(self
->a_title
);
122 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
125 ObMenuEntryFrame
*self
;
126 XSetWindowAttributes attr
;
128 self
= g_new0(ObMenuEntryFrame
, 1);
132 menu_entry_ref(entry
);
134 attr
.event_mask
= ENTRY_EVENTMASK
;
135 self
->window
= createWindow(self
->frame
->window
, CWEventMask
, &attr
);
136 self
->text
= createWindow(self
->window
, 0, NULL
);
137 g_hash_table_insert(menu_frame_map
, &self
->window
, self
);
138 g_hash_table_insert(menu_frame_map
, &self
->text
, self
);
139 if (entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
140 self
->icon
= createWindow(self
->window
, 0, NULL
);
141 g_hash_table_insert(menu_frame_map
, &self
->icon
, self
);
143 if (entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
144 self
->bullet
= createWindow(self
->window
, 0, NULL
);
145 g_hash_table_insert(menu_frame_map
, &self
->bullet
, self
);
148 XMapWindow(obt_display
, self
->window
);
149 XMapWindow(obt_display
, self
->text
);
151 self
->a_normal
= RrAppearanceCopy(ob_rr_theme
->a_menu_normal
);
152 self
->a_selected
= RrAppearanceCopy(ob_rr_theme
->a_menu_selected
);
153 self
->a_disabled
= RrAppearanceCopy(ob_rr_theme
->a_menu_disabled
);
154 self
->a_disabled_selected
=
155 RrAppearanceCopy(ob_rr_theme
->a_menu_disabled_selected
);
157 if (entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
) {
158 self
->a_separator
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
159 self
->a_separator
->texture
[0].type
= RR_TEXTURE_LINE_ART
;
161 self
->a_icon
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
162 self
->a_icon
->texture
[0].type
= RR_TEXTURE_RGBA
;
163 self
->a_mask
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
164 self
->a_mask
->texture
[0].type
= RR_TEXTURE_MASK
;
165 self
->a_bullet_normal
=
166 RrAppearanceCopy(ob_rr_theme
->a_menu_bullet_normal
);
167 self
->a_bullet_selected
=
168 RrAppearanceCopy(ob_rr_theme
->a_menu_bullet_selected
);
171 self
->a_text_normal
=
172 RrAppearanceCopy(ob_rr_theme
->a_menu_text_normal
);
173 self
->a_text_selected
=
174 RrAppearanceCopy(ob_rr_theme
->a_menu_text_selected
);
175 self
->a_text_disabled
=
176 RrAppearanceCopy(ob_rr_theme
->a_menu_text_disabled
);
177 self
->a_text_disabled_selected
=
178 RrAppearanceCopy(ob_rr_theme
->a_menu_text_disabled_selected
);
180 RrAppearanceCopy(ob_rr_theme
->a_menu_text_title
);
185 static void menu_entry_frame_free(ObMenuEntryFrame
*self
)
188 menu_entry_unref(self
->entry
);
190 XDestroyWindow(obt_display
, self
->text
);
191 XDestroyWindow(obt_display
, self
->window
);
192 g_hash_table_remove(menu_frame_map
, &self
->text
);
193 g_hash_table_remove(menu_frame_map
, &self
->window
);
194 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
195 XDestroyWindow(obt_display
, self
->icon
);
196 g_hash_table_remove(menu_frame_map
, &self
->icon
);
198 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
199 XDestroyWindow(obt_display
, self
->bullet
);
200 g_hash_table_remove(menu_frame_map
, &self
->bullet
);
203 RrAppearanceFree(self
->a_normal
);
204 RrAppearanceFree(self
->a_selected
);
205 RrAppearanceFree(self
->a_disabled
);
206 RrAppearanceFree(self
->a_disabled_selected
);
208 RrAppearanceFree(self
->a_separator
);
209 RrAppearanceFree(self
->a_icon
);
210 RrAppearanceFree(self
->a_mask
);
211 RrAppearanceFree(self
->a_text_normal
);
212 RrAppearanceFree(self
->a_text_selected
);
213 RrAppearanceFree(self
->a_text_disabled
);
214 RrAppearanceFree(self
->a_text_disabled_selected
);
215 RrAppearanceFree(self
->a_text_title
);
216 RrAppearanceFree(self
->a_bullet_normal
);
217 RrAppearanceFree(self
->a_bullet_selected
);
223 void menu_frame_move(ObMenuFrame
*self
, gint x
, gint y
)
225 RECT_SET_POINT(self
->area
, x
, y
);
226 XMoveWindow(obt_display
, self
->window
, self
->area
.x
, self
->area
.y
);
229 static void menu_frame_place_topmenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
233 if (config_menu_middle
) {
237 *y
-= self
->area
.height
/ 2;
239 /* try to the right of the cursor */
240 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
241 self
->direction_right
= TRUE
;
243 /* try to the left of the cursor */
244 myx
= *x
- self
->area
.width
;
245 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
246 self
->direction_right
= FALSE
;
249 /* if didnt fit on either side so just use what it says */
251 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
252 self
->direction_right
= TRUE
;
262 /* try to the bottom right of the cursor */
263 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
264 self
->direction_right
= TRUE
;
265 if (dx
!= 0 || dy
!= 0) {
266 /* try to the bottom left of the cursor */
267 myx
= *x
- self
->area
.width
;
269 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
270 self
->direction_right
= FALSE
;
272 if (dx
!= 0 || dy
!= 0) {
273 /* try to the top right of the cursor */
275 myy
= *y
- self
->area
.height
;
276 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
277 self
->direction_right
= TRUE
;
279 if (dx
!= 0 || dy
!= 0) {
280 /* try to the top left of the cursor */
281 myx
= *x
- self
->area
.width
;
282 myy
= *y
- self
->area
.height
;
283 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
284 self
->direction_right
= FALSE
;
286 if (dx
!= 0 || dy
!= 0) {
287 /* if didnt fit on either side so just use what it says */
290 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
291 self
->direction_right
= TRUE
;
298 static void menu_frame_place_submenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
303 overlap
= ob_rr_theme
->menu_overlap
;
304 bwidth
= ob_rr_theme
->mbwidth
;
306 if (self
->direction_right
)
307 *x
= self
->parent
->area
.x
+ self
->parent
->area
.width
-
310 *x
= self
->parent
->area
.x
- self
->area
.width
+ overlap
+ bwidth
;
312 *y
= self
->parent
->area
.y
+ self
->parent_entry
->area
.y
;
313 if (config_menu_middle
)
314 *y
-= (self
->area
.height
- (bwidth
* 2) - ITEM_HEIGHT
) / 2;
319 void menu_frame_move_on_screen(ObMenuFrame
*self
, gint x
, gint y
,
327 a
= screen_physical_area_monitor(self
->monitor
);
329 half
= g_list_length(self
->entries
) / 2;
330 pos
= g_list_index(self
->entries
, self
->selected
);
332 /* if in the bottom half then check this stuff first, will keep the bottom
333 edge of the menu visible */
335 *dx
= MAX(*dx
, a
->x
- x
);
336 *dy
= MAX(*dy
, a
->y
- y
);
338 *dx
= MIN(*dx
, (a
->x
+ a
->width
) - (x
+ self
->area
.width
));
339 *dy
= MIN(*dy
, (a
->y
+ a
->height
) - (y
+ self
->area
.height
));
340 /* if in the top half then check this stuff last, will keep the top
341 edge of the menu visible */
343 *dx
= MAX(*dx
, a
->x
- x
);
344 *dy
= MAX(*dy
, a
->y
- y
);
350 static void menu_entry_frame_render(ObMenuEntryFrame
*self
)
352 RrAppearance
*item_a
, *text_a
;
355 ObMenuFrame
*frame
= self
->frame
;
357 switch (self
->entry
->type
) {
358 case OB_MENU_ENTRY_TYPE_NORMAL
:
359 case OB_MENU_ENTRY_TYPE_SUBMENU
:
360 item_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
361 !self
->entry
->data
.normal
.enabled
?
363 (self
== self
->frame
->selected
?
364 self
->a_disabled_selected
: self
->a_disabled
) :
366 (self
== self
->frame
->selected
?
367 self
->a_selected
: self
->a_normal
));
370 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
371 if (self
->entry
->data
.separator
.label
) {
372 item_a
= self
->frame
->a_title
;
373 th
= ob_rr_theme
->menu_title_height
;
375 item_a
= self
->a_normal
;
376 th
= SEPARATOR_HEIGHT
+ 2*PADDING
;
380 g_assert_not_reached();
382 RECT_SET_SIZE(self
->area
, self
->frame
->inner_w
, th
);
383 XResizeWindow(obt_display
, self
->window
,
384 self
->area
.width
, self
->area
.height
);
385 item_a
->surface
.parent
= self
->frame
->a_items
;
386 item_a
->surface
.parentx
= self
->area
.x
;
387 item_a
->surface
.parenty
= self
->area
.y
;
388 RrPaint(item_a
, self
->window
, self
->area
.width
, self
->area
.height
);
390 switch (self
->entry
->type
) {
391 case OB_MENU_ENTRY_TYPE_NORMAL
:
392 text_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
393 !self
->entry
->data
.normal
.enabled
?
395 (self
== self
->frame
->selected
?
396 self
->a_text_disabled_selected
: self
->a_text_disabled
) :
398 (self
== self
->frame
->selected
?
399 self
->a_text_selected
: self
->a_text_normal
));
400 text_a
->texture
[0].data
.text
.string
= self
->entry
->data
.normal
.label
;
401 if (self
->entry
->data
.normal
.shortcut
&&
402 (self
->frame
->menu
->show_all_shortcuts
||
403 self
->entry
->data
.normal
.shortcut_always_show
||
404 self
->entry
->data
.normal
.shortcut_position
> 0))
406 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
407 text_a
->texture
[0].data
.text
.shortcut_pos
=
408 self
->entry
->data
.normal
.shortcut_position
;
410 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
412 case OB_MENU_ENTRY_TYPE_SUBMENU
:
413 text_a
= (self
== self
->frame
->selected
?
414 self
->a_text_selected
:
415 self
->a_text_normal
);
416 sub
= self
->entry
->data
.submenu
.submenu
;
417 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
418 if (sub
->shortcut
&& (self
->frame
->menu
->show_all_shortcuts
||
419 sub
->shortcut_always_show
||
420 sub
->shortcut_position
> 0))
422 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
423 text_a
->texture
[0].data
.text
.shortcut_pos
= sub
->shortcut_position
;
425 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
427 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
428 if (self
->entry
->data
.separator
.label
!= NULL
)
429 text_a
= self
->a_text_title
;
431 text_a
= self
->a_text_normal
;
435 switch (self
->entry
->type
) {
436 case OB_MENU_ENTRY_TYPE_NORMAL
:
437 XMoveResizeWindow(obt_display
, self
->text
,
438 self
->frame
->text_x
, PADDING
,
440 ITEM_HEIGHT
- 2*PADDING
);
441 text_a
->surface
.parent
= item_a
;
442 text_a
->surface
.parentx
= self
->frame
->text_x
;
443 text_a
->surface
.parenty
= PADDING
;
444 RrPaint(text_a
, self
->text
, self
->frame
->text_w
,
445 ITEM_HEIGHT
- 2*PADDING
);
447 case OB_MENU_ENTRY_TYPE_SUBMENU
:
448 XMoveResizeWindow(obt_display
, self
->text
,
449 self
->frame
->text_x
, PADDING
,
450 self
->frame
->text_w
- ITEM_HEIGHT
,
451 ITEM_HEIGHT
- 2*PADDING
);
452 text_a
->surface
.parent
= item_a
;
453 text_a
->surface
.parentx
= self
->frame
->text_x
;
454 text_a
->surface
.parenty
= PADDING
;
455 RrPaint(text_a
, self
->text
, self
->frame
->text_w
- ITEM_HEIGHT
,
456 ITEM_HEIGHT
- 2*PADDING
);
458 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
459 if (self
->entry
->data
.separator
.label
!= NULL
) {
460 /* labeled separator */
461 XMoveResizeWindow(obt_display
, self
->text
,
462 ob_rr_theme
->paddingx
, ob_rr_theme
->paddingy
,
463 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
464 ob_rr_theme
->menu_title_height
-
465 2*ob_rr_theme
->paddingy
);
466 text_a
->surface
.parent
= item_a
;
467 text_a
->surface
.parentx
= ob_rr_theme
->paddingx
;
468 text_a
->surface
.parenty
= ob_rr_theme
->paddingy
;
469 RrPaint(text_a
, self
->text
,
470 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
471 ob_rr_theme
->menu_title_height
-
472 2*ob_rr_theme
->paddingy
);
474 /* unlabeled separaator */
475 XMoveResizeWindow(obt_display
, self
->text
, PADDING
, PADDING
,
476 self
->area
.width
- 2*PADDING
, SEPARATOR_HEIGHT
);
477 self
->a_separator
->surface
.parent
= item_a
;
478 self
->a_separator
->surface
.parentx
= PADDING
;
479 self
->a_separator
->surface
.parenty
= PADDING
;
480 self
->a_separator
->texture
[0].data
.lineart
.color
=
481 text_a
->texture
[0].data
.text
.color
;
482 self
->a_separator
->texture
[0].data
.lineart
.x1
= 2*PADDING
;
483 self
->a_separator
->texture
[0].data
.lineart
.y1
= SEPARATOR_HEIGHT
/2;
484 self
->a_separator
->texture
[0].data
.lineart
.x2
=
485 self
->area
.width
- 4*PADDING
;
486 self
->a_separator
->texture
[0].data
.lineart
.y2
= SEPARATOR_HEIGHT
/2;
487 RrPaint(self
->a_separator
, self
->text
,
488 self
->area
.width
- 2*PADDING
, SEPARATOR_HEIGHT
);
493 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
494 self
->entry
->data
.normal
.icon_data
)
496 XMoveResizeWindow(obt_display
, self
->icon
,
497 PADDING
, frame
->item_margin
.top
,
498 ITEM_HEIGHT
- frame
->item_margin
.top
499 - frame
->item_margin
.bottom
,
500 ITEM_HEIGHT
- frame
->item_margin
.top
501 - frame
->item_margin
.bottom
);
502 self
->a_icon
->texture
[0].data
.rgba
.width
=
503 self
->entry
->data
.normal
.icon_width
;
504 self
->a_icon
->texture
[0].data
.rgba
.height
=
505 self
->entry
->data
.normal
.icon_height
;
506 self
->a_icon
->texture
[0].data
.rgba
.alpha
=
507 self
->entry
->data
.normal
.icon_alpha
;
508 self
->a_icon
->texture
[0].data
.rgba
.data
=
509 self
->entry
->data
.normal
.icon_data
;
510 self
->a_icon
->surface
.parent
= item_a
;
511 self
->a_icon
->surface
.parentx
= PADDING
;
512 self
->a_icon
->surface
.parenty
= frame
->item_margin
.top
;
513 RrPaint(self
->a_icon
, self
->icon
,
514 ITEM_HEIGHT
- frame
->item_margin
.top
515 - frame
->item_margin
.bottom
,
516 ITEM_HEIGHT
- frame
->item_margin
.top
517 - frame
->item_margin
.bottom
);
518 XMapWindow(obt_display
, self
->icon
);
519 } else if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
520 self
->entry
->data
.normal
.mask
)
524 XMoveResizeWindow(obt_display
, self
->icon
,
525 PADDING
, frame
->item_margin
.top
,
526 ITEM_HEIGHT
- frame
->item_margin
.top
527 - frame
->item_margin
.bottom
,
528 ITEM_HEIGHT
- frame
->item_margin
.top
529 - frame
->item_margin
.bottom
);
530 self
->a_mask
->texture
[0].data
.mask
.mask
=
531 self
->entry
->data
.normal
.mask
;
533 c
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
534 !self
->entry
->data
.normal
.enabled
?
536 (self
== self
->frame
->selected
?
537 self
->entry
->data
.normal
.mask_disabled_selected_color
:
538 self
->entry
->data
.normal
.mask_disabled_color
) :
540 (self
== self
->frame
->selected
?
541 self
->entry
->data
.normal
.mask_selected_color
:
542 self
->entry
->data
.normal
.mask_normal_color
));
543 self
->a_mask
->texture
[0].data
.mask
.color
= c
;
545 self
->a_mask
->surface
.parent
= item_a
;
546 self
->a_mask
->surface
.parentx
= PADDING
;
547 self
->a_mask
->surface
.parenty
= frame
->item_margin
.top
;
548 RrPaint(self
->a_mask
, self
->icon
,
549 ITEM_HEIGHT
- frame
->item_margin
.top
550 - frame
->item_margin
.bottom
,
551 ITEM_HEIGHT
- frame
->item_margin
.top
552 - frame
->item_margin
.bottom
);
553 XMapWindow(obt_display
, self
->icon
);
555 XUnmapWindow(obt_display
, self
->icon
);
557 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
558 RrAppearance
*bullet_a
;
559 XMoveResizeWindow(obt_display
, self
->bullet
,
560 self
->frame
->text_x
+ self
->frame
->text_w
-
561 ITEM_HEIGHT
+ PADDING
, PADDING
,
562 ITEM_HEIGHT
- 2*PADDING
,
563 ITEM_HEIGHT
- 2*PADDING
);
564 bullet_a
= (self
== self
->frame
->selected
?
565 self
->a_bullet_selected
:
566 self
->a_bullet_normal
);
567 bullet_a
->surface
.parent
= item_a
;
568 bullet_a
->surface
.parentx
=
569 self
->frame
->text_x
+ self
->frame
->text_w
- ITEM_HEIGHT
+ PADDING
;
570 bullet_a
->surface
.parenty
= PADDING
;
571 RrPaint(bullet_a
, self
->bullet
,
572 ITEM_HEIGHT
- 2*PADDING
,
573 ITEM_HEIGHT
- 2*PADDING
);
574 XMapWindow(obt_display
, self
->bullet
);
576 XUnmapWindow(obt_display
, self
->bullet
);
581 /*! this code is taken from the menu_frame_render. if that changes, this won't
583 static gint
menu_entry_frame_get_height(ObMenuEntryFrame
*self
,
584 gboolean first_entry
,
593 t
= self
->entry
->type
;
595 /* this is the More... entry, it's NORMAL type */
596 t
= OB_MENU_ENTRY_TYPE_NORMAL
;
599 case OB_MENU_ENTRY_TYPE_NORMAL
:
600 case OB_MENU_ENTRY_TYPE_SUBMENU
:
601 h
+= ob_rr_theme
->menu_font_height
;
603 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
604 if (self
->entry
->data
.separator
.label
!= NULL
) {
605 h
+= ob_rr_theme
->menu_title_height
+
606 (ob_rr_theme
->mbwidth
- PADDING
) * 2;
608 /* if the first entry is a labeled separator, then make its border
609 overlap with the menu's outside border */
611 h
-= ob_rr_theme
->mbwidth
;
612 /* if the last entry is a labeled separator, then make its border
613 overlap with the menu's outside border */
615 h
-= ob_rr_theme
->mbwidth
;
617 h
+= SEPARATOR_HEIGHT
;
625 void menu_frame_render(ObMenuFrame
*self
)
628 gint tw
, th
; /* temps */
630 gboolean has_icon
= FALSE
;
634 /* find text dimensions */
636 STRUT_SET(self
->item_margin
, 0, 0, 0, 0);
641 e
= self
->entries
->data
;
642 e
->a_text_normal
->texture
[0].data
.text
.string
= "";
643 tw
= RrMinWidth(e
->a_text_normal
);
648 RrMargins(e
->a_normal
, &l
, &t
, &r
, &b
);
649 STRUT_SET(self
->item_margin
,
650 MAX(self
->item_margin
.left
, l
),
651 MAX(self
->item_margin
.top
, t
),
652 MAX(self
->item_margin
.right
, r
),
653 MAX(self
->item_margin
.bottom
, b
));
654 RrMargins(e
->a_selected
, &l
, &t
, &r
, &b
);
655 STRUT_SET(self
->item_margin
,
656 MAX(self
->item_margin
.left
, l
),
657 MAX(self
->item_margin
.top
, t
),
658 MAX(self
->item_margin
.right
, r
),
659 MAX(self
->item_margin
.bottom
, b
));
660 RrMargins(e
->a_disabled
, &l
, &t
, &r
, &b
);
661 STRUT_SET(self
->item_margin
,
662 MAX(self
->item_margin
.left
, l
),
663 MAX(self
->item_margin
.top
, t
),
664 MAX(self
->item_margin
.right
, r
),
665 MAX(self
->item_margin
.bottom
, b
));
666 RrMargins(e
->a_disabled_selected
, &l
, &t
, &r
, &b
);
667 STRUT_SET(self
->item_margin
,
668 MAX(self
->item_margin
.left
, l
),
669 MAX(self
->item_margin
.top
, t
),
670 MAX(self
->item_margin
.right
, r
),
671 MAX(self
->item_margin
.bottom
, b
));
674 /* render the entries */
676 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
677 RrAppearance
*text_a
;
680 /* if the first entry is a labeled separator, then make its border
681 overlap with the menu's outside border */
682 if (it
== self
->entries
&&
683 e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
684 e
->entry
->data
.separator
.label
)
686 h
-= ob_rr_theme
->mbwidth
;
689 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
690 e
->entry
->data
.separator
.label
)
692 e
->border
= ob_rr_theme
->mbwidth
;
695 RECT_SET_POINT(e
->area
, 0, h
+e
->border
);
696 XMoveWindow(obt_display
, e
->window
,
697 e
->area
.x
-e
->border
, e
->area
.y
-e
->border
);
698 XSetWindowBorderWidth(obt_display
, e
->window
, e
->border
);
699 XSetWindowBorder(obt_display
, e
->window
,
700 RrColorPixel(ob_rr_theme
->menu_border_color
));
703 text_a
= (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
704 !e
->entry
->data
.normal
.enabled
?
706 (e
== self
->selected
?
707 e
->a_text_disabled_selected
: e
->a_text_disabled
) :
709 (e
== self
->selected
?
710 e
->a_text_selected
: e
->a_text_normal
));
711 switch (e
->entry
->type
) {
712 case OB_MENU_ENTRY_TYPE_NORMAL
:
713 text_a
->texture
[0].data
.text
.string
= e
->entry
->data
.normal
.label
;
714 tw
= RrMinWidth(text_a
);
715 tw
= MIN(tw
, MAX_MENU_WIDTH
);
716 th
= ob_rr_theme
->menu_font_height
;
718 if (e
->entry
->data
.normal
.icon_data
||
719 e
->entry
->data
.normal
.mask
)
722 case OB_MENU_ENTRY_TYPE_SUBMENU
:
723 sub
= e
->entry
->data
.submenu
.submenu
;
724 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
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_data
||
730 e
->entry
->data
.normal
.mask
)
733 tw
+= ITEM_HEIGHT
- PADDING
;
735 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
736 if (e
->entry
->data
.separator
.label
!= NULL
) {
737 e
->a_text_title
->texture
[0].data
.text
.string
=
738 e
->entry
->data
.separator
.label
;
739 tw
= RrMinWidth(e
->a_text_title
) + 2*ob_rr_theme
->paddingx
;
740 tw
= MIN(tw
, MAX_MENU_WIDTH
);
741 th
= ob_rr_theme
->menu_title_height
+
742 (ob_rr_theme
->mbwidth
- PADDING
) *2;
745 th
= SEPARATOR_HEIGHT
;
755 /* if the last entry is a labeled separator, then make its border
756 overlap with the menu's outside border */
757 it
= g_list_last(self
->entries
);
758 e
= it
? it
->data
: NULL
;
759 if (e
&& e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
760 e
->entry
->data
.separator
.label
)
762 h
-= ob_rr_theme
->mbwidth
;
765 self
->text_x
= PADDING
;
770 w
+= ITEM_HEIGHT
+ PADDING
;
771 self
->text_x
+= ITEM_HEIGHT
+ PADDING
;
778 XResizeWindow(obt_display
, self
->window
, w
, h
);
782 RrPaint(self
->a_items
, self
->window
, w
, h
);
784 for (it
= self
->entries
; it
; it
= g_list_next(it
))
785 menu_entry_frame_render(it
->data
);
787 w
+= ob_rr_theme
->mbwidth
* 2;
788 h
+= ob_rr_theme
->mbwidth
* 2;
790 RECT_SET_SIZE(self
->area
, w
, h
);
795 static void menu_frame_update(ObMenuFrame
*self
)
801 menu_pipe_execute(self
->menu
);
802 menu_find_submenus(self
->menu
);
804 self
->selected
= NULL
;
806 /* start at show_from */
807 mit
= g_list_nth(self
->menu
->entries
, self
->show_from
);
809 /* go through the menu's and frame's entries and connect the frame entries
810 to the menu entries */
811 for (fit
= self
->entries
; mit
&& fit
;
812 mit
= g_list_next(mit
), fit
= g_list_next(fit
))
814 ObMenuEntryFrame
*f
= fit
->data
;
815 f
->entry
= mit
->data
;
818 /* if there are more menu entries than in the frame, add them */
820 ObMenuEntryFrame
*e
= menu_entry_frame_new(mit
->data
, self
);
821 self
->entries
= g_list_append(self
->entries
, e
);
822 mit
= g_list_next(mit
);
825 /* if there are more frame entries than menu entries then get rid of
828 GList
*n
= g_list_next(fit
);
829 menu_entry_frame_free(fit
->data
);
830 self
->entries
= g_list_delete_link(self
->entries
, fit
);
834 /* * make the menu fit on the screen */
836 /* calculate the height of the menu */
838 for (fit
= self
->entries
; fit
; fit
= g_list_next(fit
))
839 h
+= menu_entry_frame_get_height(fit
->data
,
840 fit
== self
->entries
,
841 g_list_next(fit
) == NULL
);
842 /* add the border at the top and bottom */
843 h
+= ob_rr_theme
->mbwidth
* 2;
845 a
= screen_physical_area_monitor(self
->monitor
);
849 gboolean last_entry
= TRUE
;
851 /* take the height of our More... entry into account */
852 h
+= menu_entry_frame_get_height(NULL
, FALSE
, TRUE
);
854 /* start at the end of the entries */
855 flast
= g_list_last(self
->entries
);
857 /* pull out all the entries from the frame that don't
858 fit on the screen, leaving at least 1 though */
859 while (h
> a
->height
&& g_list_previous(flast
) != NULL
) {
860 /* update the height, without this entry */
861 h
-= menu_entry_frame_get_height(flast
->data
, FALSE
, last_entry
);
863 /* destroy the entry we're not displaying */
865 flast
= g_list_previous(flast
);
866 menu_entry_frame_free(tmp
->data
);
867 self
->entries
= g_list_delete_link(self
->entries
, tmp
);
869 /* only the first one that we see is the last entry in the menu */
874 ObMenuEntry
*more_entry
;
875 ObMenuEntryFrame
*more_frame
;
876 /* make the More... menu entry frame which will display in this
878 if self->menu->more_menu is NULL that means that this is already
879 More... menu, so just use ourself.
881 more_entry
= menu_get_more((self
->menu
->more_menu
?
882 self
->menu
->more_menu
:
884 /* continue where we left off */
886 g_list_length(self
->entries
));
887 more_frame
= menu_entry_frame_new(more_entry
, self
);
888 /* make it get deleted when the menu frame goes away */
889 menu_entry_unref(more_entry
);
891 /* add our More... entry to the frame */
892 self
->entries
= g_list_append(self
->entries
, more_frame
);
898 menu_frame_render(self
);
901 static gboolean
menu_frame_is_visible(ObMenuFrame
*self
)
903 return !!(g_list_find(menu_frame_visible
, self
));
906 static gboolean
menu_frame_show(ObMenuFrame
*self
)
910 /* determine if the underlying menu is already visible */
911 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
912 ObMenuFrame
*f
= it
->data
;
913 if (f
->menu
== self
->menu
)
917 if (self
->menu
->update_func
)
918 if (!self
->menu
->update_func(self
, self
->menu
->data
))
922 if (menu_frame_visible
== NULL
) {
923 /* no menus shown yet */
925 /* grab the pointer in such a way as to pass through "owner events"
926 so that we can get enter/leave notifies in the menu. */
927 if (!grab_pointer(TRUE
, FALSE
, OB_CURSOR_POINTER
))
929 if (!grab_keyboard()) {
935 menu_frame_update(self
);
937 menu_frame_visible
= g_list_prepend(menu_frame_visible
, self
);
939 if (self
->menu
->show_func
)
940 self
->menu
->show_func(self
, self
->menu
->data
);
945 gboolean
menu_frame_show_topmenu(ObMenuFrame
*self
, gint x
, gint y
,
951 if (menu_frame_is_visible(self
))
953 if (!menu_frame_show(self
))
956 /* find the monitor the menu is on */
957 for (i
= 0; i
< screen_num_monitors
; ++i
) {
958 Rect
*a
= screen_physical_area_monitor(i
);
959 gboolean contains
= RECT_CONTAINS(*a
, x
, y
);
967 if (self
->menu
->place_func
)
968 self
->menu
->place_func(self
, &x
, &y
, mouse
, self
->menu
->data
);
970 menu_frame_place_topmenu(self
, &x
, &y
);
972 menu_frame_move(self
, x
, y
);
974 XMapWindow(obt_display
, self
->window
);
976 if (screen_pointer_pos(&px
, &py
)) {
977 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
978 if (e
&& e
->frame
== self
)
985 gboolean
menu_frame_show_submenu(ObMenuFrame
*self
, ObMenuFrame
*parent
,
986 ObMenuEntryFrame
*parent_entry
)
991 if (menu_frame_is_visible(self
))
994 self
->monitor
= parent
->monitor
;
995 self
->parent
= parent
;
996 self
->parent_entry
= parent_entry
;
998 /* set up parent's child to be us */
1000 menu_frame_hide(parent
->child
);
1001 parent
->child
= self
;
1003 if (!menu_frame_show(self
))
1006 menu_frame_place_submenu(self
, &x
, &y
);
1007 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1010 /*try the other side */
1011 self
->direction_right
= !self
->direction_right
;
1012 menu_frame_place_submenu(self
, &x
, &y
);
1013 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1015 menu_frame_move(self
, x
+ dx
, y
+ dy
);
1017 XMapWindow(obt_display
, self
->window
);
1019 if (screen_pointer_pos(&px
, &py
)) {
1020 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
1021 if (e
&& e
->frame
== self
)
1028 static void menu_frame_hide(ObMenuFrame
*self
)
1030 GList
*it
= g_list_find(menu_frame_visible
, self
);
1035 if (self
->menu
->hide_func
)
1036 self
->menu
->hide_func(self
, self
->menu
->data
);
1039 menu_frame_hide(self
->child
);
1042 self
->parent
->child
= NULL
;
1043 self
->parent
= NULL
;
1044 self
->parent_entry
= NULL
;
1046 menu_frame_visible
= g_list_delete_link(menu_frame_visible
, it
);
1048 if (menu_frame_visible
== NULL
) {
1049 /* last menu shown */
1054 XUnmapWindow(obt_display
, self
->window
);
1056 menu_frame_free(self
);
1059 void menu_frame_hide_all(void)
1063 if (config_submenu_show_delay
) {
1064 /* remove any submenu open requests */
1065 obt_main_loop_timeout_remove(ob_main_loop
,
1066 menu_entry_frame_submenu_timeout
);
1068 if ((it
= g_list_last(menu_frame_visible
)))
1069 menu_frame_hide(it
->data
);
1072 void menu_frame_hide_all_client(ObClient
*client
)
1074 GList
*it
= g_list_last(menu_frame_visible
);
1076 ObMenuFrame
*f
= it
->data
;
1077 if (f
->client
== client
) {
1078 if (config_submenu_show_delay
) {
1079 /* remove any submenu open requests */
1080 obt_main_loop_timeout_remove(ob_main_loop
,
1081 menu_entry_frame_submenu_timeout
);
1089 ObMenuFrame
* menu_frame_under(gint x
, gint y
)
1091 ObMenuFrame
*ret
= NULL
;
1094 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
1095 ObMenuFrame
*f
= it
->data
;
1097 if (RECT_CONTAINS(f
->area
, x
, y
)) {
1105 ObMenuEntryFrame
* menu_entry_frame_under(gint x
, gint y
)
1108 ObMenuEntryFrame
*ret
= NULL
;
1111 if ((frame
= menu_frame_under(x
, y
))) {
1112 x
-= ob_rr_theme
->mbwidth
+ frame
->area
.x
;
1113 y
-= ob_rr_theme
->mbwidth
+ frame
->area
.y
;
1115 for (it
= frame
->entries
; it
; it
= g_list_next(it
)) {
1116 ObMenuEntryFrame
*e
= it
->data
;
1118 if (RECT_CONTAINS(e
->area
, x
, y
)) {
1127 static gboolean
menu_entry_frame_submenu_timeout(gpointer data
)
1129 g_assert(menu_frame_visible
);
1130 menu_entry_frame_show_submenu((ObMenuEntryFrame
*)data
);
1134 void menu_frame_select(ObMenuFrame
*self
, ObMenuEntryFrame
*entry
,
1137 ObMenuEntryFrame
*old
= self
->selected
;
1138 ObMenuFrame
*oldchild
= self
->child
;
1140 if (entry
&& entry
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
1143 if (old
== entry
) return;
1145 if (config_submenu_show_delay
) {
1146 /* remove any submenu open requests */
1147 obt_main_loop_timeout_remove(ob_main_loop
,
1148 menu_entry_frame_submenu_timeout
);
1151 self
->selected
= entry
;
1154 menu_entry_frame_render(old
);
1156 menu_frame_hide(oldchild
);
1158 if (self
->selected
) {
1159 menu_entry_frame_render(self
->selected
);
1161 if (self
->selected
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
1162 if (config_submenu_show_delay
&& !immediate
) {
1163 /* initiate a new submenu open request */
1164 obt_main_loop_timeout_add(ob_main_loop
,
1165 config_submenu_show_delay
* 1000,
1166 menu_entry_frame_submenu_timeout
,
1167 self
->selected
, g_direct_equal
,
1170 menu_entry_frame_show_submenu(self
->selected
);
1176 void menu_entry_frame_show_submenu(ObMenuEntryFrame
*self
)
1180 if (!self
->entry
->data
.submenu
.submenu
) return;
1182 f
= menu_frame_new(self
->entry
->data
.submenu
.submenu
,
1183 self
->entry
->data
.submenu
.show_from
,
1184 self
->frame
->client
);
1185 /* pass our direction on to our child */
1186 f
->direction_right
= self
->frame
->direction_right
;
1188 menu_frame_show_submenu(f
, self
->frame
, self
);
1191 void menu_entry_frame_execute(ObMenuEntryFrame
*self
, guint state
)
1193 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1194 self
->entry
->data
.normal
.enabled
)
1196 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1198 ObMenuEntry
*entry
= self
->entry
;
1199 ObMenuExecuteFunc func
= self
->frame
->menu
->execute_func
;
1200 gpointer data
= self
->frame
->menu
->data
;
1201 GSList
*acts
= self
->entry
->data
.normal
.actions
;
1202 ObClient
*client
= self
->frame
->client
;
1203 ObMenuFrame
*frame
= self
->frame
;
1205 /* release grabs before executing the shit */
1206 if (!(state
& ControlMask
)) {
1207 menu_frame_hide_all();
1212 func(entry
, frame
, client
, state
, data
);
1214 actions_run_acts(acts
, OB_USER_ACTION_MENU_SELECTION
,
1215 state
, -1, -1, 0, OB_FRAME_CONTEXT_NONE
, client
);
1219 void menu_frame_select_previous(ObMenuFrame
*self
)
1221 GList
*it
= NULL
, *start
;
1223 if (self
->entries
) {
1224 start
= it
= g_list_find(self
->entries
, self
->selected
);
1226 ObMenuEntryFrame
*e
;
1228 it
= it
? g_list_previous(it
) : g_list_last(self
->entries
);
1234 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1236 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1241 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);
1244 void menu_frame_select_next(ObMenuFrame
*self
)
1246 GList
*it
= NULL
, *start
;
1248 if (self
->entries
) {
1249 start
= it
= g_list_find(self
->entries
, self
->selected
);
1251 ObMenuEntryFrame
*e
;
1253 it
= it
? g_list_next(it
) : self
->entries
;
1259 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1261 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1266 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);