1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 menuframe.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "menuframe.h"
30 #include "obt/keyboard.h"
31 #include "obrender/theme.h"
34 #define MAX_MENU_WIDTH 400
36 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
38 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
40 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
41 ButtonPressMask | ButtonReleaseMask | \
44 GList
*menu_frame_visible
;
45 GHashTable
*menu_frame_map
;
47 static RrAppearance
*a_sep
;
48 static guint submenu_show_timer
= 0;
49 static guint submenu_hide_timer
= 0;
51 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
53 static void menu_entry_frame_free(ObMenuEntryFrame
*self
);
54 static void menu_frame_update(ObMenuFrame
*self
);
55 static gboolean
submenu_show_timeout(gpointer data
);
56 static void menu_frame_hide(ObMenuFrame
*self
);
58 static gboolean
submenu_hide_timeout(gpointer data
);
60 static Window
createWindow(Window parent
, gulong mask
,
61 XSetWindowAttributes
*attrib
)
63 return XCreateWindow(obt_display
, parent
, 0, 0, 1, 1, 0,
64 RrDepth(ob_rr_inst
), InputOutput
,
65 RrVisual(ob_rr_inst
), mask
, attrib
);
68 static void client_dest(ObClient
*client
, gpointer data
)
72 /* menus can be associated with a client, so null those refs since
73 we are disappearing now */
74 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
75 ObMenuFrame
*f
= it
->data
;
76 if (f
->client
== client
)
81 void menu_frame_startup(gboolean reconfig
)
85 a_sep
= RrAppearanceCopy(ob_rr_theme
->a_clear
);
86 RrAppearanceAddTextures(a_sep
, ob_rr_theme
->menu_sep_width
);
87 for (i
= 0; i
< ob_rr_theme
->menu_sep_width
; ++i
) {
88 a_sep
->texture
[i
].type
= RR_TEXTURE_LINE_ART
;
89 a_sep
->texture
[i
].data
.lineart
.color
=
90 ob_rr_theme
->menu_sep_color
;
95 client_add_destroy_notify(client_dest
, NULL
);
96 menu_frame_map
= g_hash_table_new(g_int_hash
, g_int_equal
);
99 void menu_frame_shutdown(gboolean reconfig
)
101 RrAppearanceFree(a_sep
);
103 if (reconfig
) return;
105 client_remove_destroy_notify(client_dest
);
106 g_hash_table_destroy(menu_frame_map
);
109 ObMenuFrame
* menu_frame_new(ObMenu
*menu
, guint show_from
, ObClient
*client
)
112 XSetWindowAttributes attr
;
114 self
= g_slice_new0(ObMenuFrame
);
115 self
->obwin
.type
= OB_WINDOW_CLASS_MENUFRAME
;
117 self
->selected
= NULL
;
118 self
->client
= client
;
119 self
->direction_right
= TRUE
;
120 self
->show_from
= show_from
;
122 attr
.event_mask
= FRAME_EVENTMASK
;
123 self
->window
= createWindow(obt_root(ob_screen
),
126 /* make it a popup menu type window */
127 OBT_PROP_SET32(self
->window
, NET_WM_WINDOW_TYPE
, ATOM
,
128 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_POPUP_MENU
));
130 XSetWindowBorderWidth(obt_display
, self
->window
, ob_rr_theme
->mbwidth
);
131 XSetWindowBorder(obt_display
, self
->window
,
132 RrColorPixel(ob_rr_theme
->menu_border_color
));
134 self
->a_items
= RrAppearanceCopy(ob_rr_theme
->a_menu
);
136 window_add(&self
->window
, MENUFRAME_AS_WINDOW(self
));
137 stacking_add(MENUFRAME_AS_WINDOW(self
));
142 void menu_frame_free(ObMenuFrame
*self
)
145 while (self
->entries
) {
146 menu_entry_frame_free(self
->entries
->data
);
147 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
150 stacking_remove(MENUFRAME_AS_WINDOW(self
));
151 window_remove(self
->window
);
153 RrAppearanceFree(self
->a_items
);
155 XDestroyWindow(obt_display
, self
->window
);
157 g_slice_free(ObMenuFrame
, self
);
161 ObtIC
* menu_frame_ic(ObMenuFrame
*self
)
163 /* menus are always used through a grab right now, so they can always use
164 the grab input context */
165 return grab_input_context();
168 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
171 ObMenuEntryFrame
*self
;
172 XSetWindowAttributes attr
;
174 self
= g_slice_new0(ObMenuEntryFrame
);
178 menu_entry_ref(entry
);
180 attr
.event_mask
= ENTRY_EVENTMASK
;
181 self
->window
= createWindow(self
->frame
->window
, CWEventMask
, &attr
);
182 self
->text
= createWindow(self
->window
, 0, NULL
);
183 g_hash_table_insert(menu_frame_map
, &self
->window
, self
);
184 g_hash_table_insert(menu_frame_map
, &self
->text
, self
);
185 if ((entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) ||
186 (entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)) {
187 self
->icon
= createWindow(self
->window
, 0, NULL
);
188 g_hash_table_insert(menu_frame_map
, &self
->icon
, self
);
190 if (entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
191 self
->bullet
= createWindow(self
->window
, 0, NULL
);
192 g_hash_table_insert(menu_frame_map
, &self
->bullet
, self
);
195 XMapWindow(obt_display
, self
->window
);
196 XMapWindow(obt_display
, self
->text
);
198 window_add(&self
->window
, MENUFRAME_AS_WINDOW(self
->frame
));
203 static void menu_entry_frame_free(ObMenuEntryFrame
*self
)
206 menu_entry_unref(self
->entry
);
208 window_remove(self
->window
);
210 XDestroyWindow(obt_display
, self
->text
);
211 XDestroyWindow(obt_display
, self
->window
);
212 g_hash_table_remove(menu_frame_map
, &self
->text
);
213 g_hash_table_remove(menu_frame_map
, &self
->window
);
214 if ((self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) ||
215 (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)) {
216 XDestroyWindow(obt_display
, self
->icon
);
217 g_hash_table_remove(menu_frame_map
, &self
->icon
);
219 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
220 XDestroyWindow(obt_display
, self
->bullet
);
221 g_hash_table_remove(menu_frame_map
, &self
->bullet
);
224 g_slice_free(ObMenuEntryFrame
, self
);
228 void menu_frame_move(ObMenuFrame
*self
, gint x
, gint y
)
230 RECT_SET_POINT(self
->area
, x
, y
);
231 self
->monitor
= screen_find_monitor_point(x
, y
);
232 XMoveWindow(obt_display
, self
->window
, self
->area
.x
, self
->area
.y
);
235 static void menu_frame_place_topmenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
239 if (config_menu_middle
) {
243 *y
-= self
->area
.height
/ 2;
245 /* try to the right of the cursor */
246 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
247 self
->direction_right
= TRUE
;
249 /* try to the left of the cursor */
250 myx
= *x
- self
->area
.width
;
251 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
252 self
->direction_right
= FALSE
;
255 /* if didnt fit on either side so just use what it says */
257 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
258 self
->direction_right
= TRUE
;
268 /* try to the bottom right of the cursor */
269 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
270 self
->direction_right
= TRUE
;
271 if (dx
!= 0 || dy
!= 0) {
272 /* try to the bottom left of the cursor */
273 myx
= *x
- self
->area
.width
;
275 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
276 self
->direction_right
= FALSE
;
278 if (dx
!= 0 || dy
!= 0) {
279 /* try to the top right of the cursor */
281 myy
= *y
- self
->area
.height
;
282 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
283 self
->direction_right
= TRUE
;
285 if (dx
!= 0 || dy
!= 0) {
286 /* try to the top left of the cursor */
287 myx
= *x
- self
->area
.width
;
288 myy
= *y
- self
->area
.height
;
289 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
290 self
->direction_right
= FALSE
;
292 if (dx
!= 0 || dy
!= 0) {
293 /* if didnt fit on either side so just use what it says */
296 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
297 self
->direction_right
= TRUE
;
304 static void menu_frame_place_submenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
306 gint overlapx
, overlapy
;
309 overlapx
= ob_rr_theme
->menu_overlap_x
;
310 overlapy
= ob_rr_theme
->menu_overlap_y
;
311 bwidth
= ob_rr_theme
->mbwidth
;
313 if (self
->direction_right
)
314 *x
= self
->parent
->area
.x
+ self
->parent
->area
.width
-
317 *x
= self
->parent
->area
.x
- self
->area
.width
+ overlapx
+ bwidth
;
319 *y
= self
->parent
->area
.y
+ self
->parent_entry
->area
.y
;
320 if (config_menu_middle
)
321 *y
-= (self
->area
.height
- (bwidth
* 2) - ITEM_HEIGHT
) / 2;
326 void menu_frame_move_on_screen(ObMenuFrame
*self
, gint x
, gint y
,
329 const Rect
*a
= NULL
;
330 Rect search
= self
->area
;
331 gint pos
, half
, monitor
;
334 RECT_SET_POINT(search
, x
, y
);
337 monitor
= self
->parent
->monitor
;
339 monitor
= screen_find_monitor(&search
);
341 a
= screen_physical_area_monitor(monitor
);
343 half
= g_list_length(self
->entries
) / 2;
344 pos
= g_list_index(self
->entries
, self
->selected
);
346 /* if in the bottom half then check this stuff first, will keep the bottom
347 edge of the menu visible */
349 *dx
= MAX(*dx
, a
->x
- x
);
350 *dy
= MAX(*dy
, a
->y
- y
);
352 *dx
= MIN(*dx
, (a
->x
+ a
->width
) - (x
+ self
->area
.width
));
353 *dy
= MIN(*dy
, (a
->y
+ a
->height
) - (y
+ self
->area
.height
));
354 /* if in the top half then check this stuff last, will keep the top
355 edge of the menu visible */
357 *dx
= MAX(*dx
, a
->x
- x
);
358 *dy
= MAX(*dy
, a
->y
- y
);
362 static void menu_entry_frame_render(ObMenuEntryFrame
*self
)
364 RrAppearance
*item_a
, *text_a
;
367 ObMenuFrame
*frame
= self
->frame
;
369 switch (self
->entry
->type
) {
370 case OB_MENU_ENTRY_TYPE_NORMAL
:
371 case OB_MENU_ENTRY_TYPE_SUBMENU
:
372 item_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
373 !self
->entry
->data
.normal
.enabled
?
375 (self
== self
->frame
->selected
?
376 ob_rr_theme
->a_menu_disabled_selected
:
377 ob_rr_theme
->a_menu_disabled
) :
379 (self
== self
->frame
->selected
?
380 ob_rr_theme
->a_menu_selected
:
381 ob_rr_theme
->a_menu_normal
));
384 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
385 if (self
->entry
->data
.separator
.label
) {
386 item_a
= ob_rr_theme
->a_menu_title
;
387 th
= ob_rr_theme
->menu_title_height
;
389 item_a
= ob_rr_theme
->a_menu_normal
;
390 th
= ob_rr_theme
->menu_sep_width
+
391 2*ob_rr_theme
->menu_sep_paddingy
;
395 g_assert_not_reached();
398 RECT_SET_SIZE(self
->area
, self
->frame
->inner_w
, th
);
399 XResizeWindow(obt_display
, self
->window
,
400 self
->area
.width
, self
->area
.height
);
401 item_a
->surface
.parent
= self
->frame
->a_items
;
402 item_a
->surface
.parentx
= self
->area
.x
;
403 item_a
->surface
.parenty
= self
->area
.y
;
404 RrPaint(item_a
, self
->window
, self
->area
.width
, self
->area
.height
);
406 switch (self
->entry
->type
) {
407 case OB_MENU_ENTRY_TYPE_NORMAL
:
408 text_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
409 !self
->entry
->data
.normal
.enabled
?
411 (self
== self
->frame
->selected
?
412 ob_rr_theme
->a_menu_text_disabled_selected
:
413 ob_rr_theme
->a_menu_text_disabled
) :
415 (self
== self
->frame
->selected
?
416 ob_rr_theme
->a_menu_text_selected
:
417 ob_rr_theme
->a_menu_text_normal
));
418 text_a
->texture
[0].data
.text
.string
= self
->entry
->data
.normal
.label
;
419 if (self
->entry
->data
.normal
.shortcut
&&
420 (self
->frame
->menu
->show_all_shortcuts
||
421 self
->entry
->data
.normal
.shortcut_always_show
||
422 self
->entry
->data
.normal
.shortcut_position
> 0))
424 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
425 text_a
->texture
[0].data
.text
.shortcut_pos
=
426 self
->entry
->data
.normal
.shortcut_position
;
428 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
430 case OB_MENU_ENTRY_TYPE_SUBMENU
:
431 text_a
= (self
== self
->frame
->selected
?
432 ob_rr_theme
->a_menu_text_selected
:
433 ob_rr_theme
->a_menu_text_normal
);
434 sub
= self
->entry
->data
.submenu
.submenu
;
435 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
436 if (sub
&& sub
->shortcut
&& (self
->frame
->menu
->show_all_shortcuts
||
437 sub
->shortcut_always_show
||
438 sub
->shortcut_position
> 0))
440 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
441 text_a
->texture
[0].data
.text
.shortcut_pos
= sub
->shortcut_position
;
443 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
445 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
446 if (self
->entry
->data
.separator
.label
!= NULL
) {
447 text_a
= ob_rr_theme
->a_menu_text_title
;
448 text_a
->texture
[0].data
.text
.string
=
449 self
->entry
->data
.separator
.label
;
452 text_a
= ob_rr_theme
->a_menu_text_normal
;
455 g_assert_not_reached();
458 switch (self
->entry
->type
) {
459 case OB_MENU_ENTRY_TYPE_NORMAL
:
460 XMoveResizeWindow(obt_display
, self
->text
,
461 self
->frame
->text_x
, PADDING
,
463 ITEM_HEIGHT
- 2*PADDING
);
464 text_a
->surface
.parent
= item_a
;
465 text_a
->surface
.parentx
= self
->frame
->text_x
;
466 text_a
->surface
.parenty
= PADDING
;
467 RrPaint(text_a
, self
->text
, self
->frame
->text_w
,
468 ITEM_HEIGHT
- 2*PADDING
);
470 case OB_MENU_ENTRY_TYPE_SUBMENU
:
471 XMoveResizeWindow(obt_display
, self
->text
,
472 self
->frame
->text_x
, PADDING
,
473 self
->frame
->text_w
- ITEM_HEIGHT
,
474 ITEM_HEIGHT
- 2*PADDING
);
475 text_a
->surface
.parent
= item_a
;
476 text_a
->surface
.parentx
= self
->frame
->text_x
;
477 text_a
->surface
.parenty
= PADDING
;
478 RrPaint(text_a
, self
->text
, self
->frame
->text_w
- ITEM_HEIGHT
,
479 ITEM_HEIGHT
- 2*PADDING
);
481 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
482 if (self
->entry
->data
.separator
.label
!= NULL
) {
483 /* labeled separator */
484 XMoveResizeWindow(obt_display
, self
->text
,
485 ob_rr_theme
->paddingx
, ob_rr_theme
->paddingy
,
486 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
487 ob_rr_theme
->menu_title_height
-
488 2*ob_rr_theme
->paddingy
);
489 text_a
->surface
.parent
= item_a
;
490 text_a
->surface
.parentx
= ob_rr_theme
->paddingx
;
491 text_a
->surface
.parenty
= ob_rr_theme
->paddingy
;
492 RrPaint(text_a
, self
->text
,
493 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
494 ob_rr_theme
->menu_title_height
-
495 2*ob_rr_theme
->paddingy
);
499 /* unlabeled separator */
500 XMoveResizeWindow(obt_display
, self
->text
, 0, 0,
502 ob_rr_theme
->menu_sep_width
+
503 2*ob_rr_theme
->menu_sep_paddingy
);
505 a_sep
->surface
.parent
= item_a
;
506 a_sep
->surface
.parentx
= 0;
507 a_sep
->surface
.parenty
= 0;
508 for (i
= 0; i
< ob_rr_theme
->menu_sep_width
; ++i
) {
509 a_sep
->texture
[i
].data
.lineart
.x1
=
510 ob_rr_theme
->menu_sep_paddingx
;
511 a_sep
->texture
[i
].data
.lineart
.y1
=
512 ob_rr_theme
->menu_sep_paddingy
+ i
;
513 a_sep
->texture
[i
].data
.lineart
.x2
=
514 self
->area
.width
- ob_rr_theme
->menu_sep_paddingx
- 1;
515 a_sep
->texture
[i
].data
.lineart
.y2
=
516 ob_rr_theme
->menu_sep_paddingy
+ i
;
519 RrPaint(a_sep
, self
->text
, self
->area
.width
,
520 ob_rr_theme
->menu_sep_width
+
521 2*ob_rr_theme
->menu_sep_paddingy
);
525 g_assert_not_reached();
528 if (((self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) ||
529 (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)) &&
530 self
->entry
->data
.normal
.icon
)
534 XMoveResizeWindow(obt_display
, self
->icon
,
535 PADDING
, frame
->item_margin
.top
,
536 ITEM_HEIGHT
- frame
->item_margin
.top
537 - frame
->item_margin
.bottom
,
538 ITEM_HEIGHT
- frame
->item_margin
.top
539 - frame
->item_margin
.bottom
);
541 clear
= ob_rr_theme
->a_clear_tex
;
542 RrAppearanceClearTextures(clear
);
543 clear
->texture
[0].type
= RR_TEXTURE_IMAGE
;
544 clear
->texture
[0].data
.image
.image
=
545 self
->entry
->data
.normal
.icon
;
546 clear
->texture
[0].data
.image
.alpha
=
547 self
->entry
->data
.normal
.icon_alpha
;
548 clear
->surface
.parent
= item_a
;
549 clear
->surface
.parentx
= PADDING
;
550 clear
->surface
.parenty
= frame
->item_margin
.top
;
551 RrPaint(clear
, self
->icon
,
552 ITEM_HEIGHT
- frame
->item_margin
.top
553 - frame
->item_margin
.bottom
,
554 ITEM_HEIGHT
- frame
->item_margin
.top
555 - frame
->item_margin
.bottom
);
556 XMapWindow(obt_display
, self
->icon
);
557 } else if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
558 self
->entry
->data
.normal
.mask
)
563 XMoveResizeWindow(obt_display
, self
->icon
,
564 PADDING
, frame
->item_margin
.top
,
565 ITEM_HEIGHT
- frame
->item_margin
.top
566 - frame
->item_margin
.bottom
,
567 ITEM_HEIGHT
- frame
->item_margin
.top
568 - frame
->item_margin
.bottom
);
570 clear
= ob_rr_theme
->a_clear_tex
;
571 RrAppearanceClearTextures(clear
);
572 clear
->texture
[0].type
= RR_TEXTURE_MASK
;
573 clear
->texture
[0].data
.mask
.mask
=
574 self
->entry
->data
.normal
.mask
;
576 c
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
577 !self
->entry
->data
.normal
.enabled
?
579 (self
== self
->frame
->selected
?
580 self
->entry
->data
.normal
.mask_disabled_selected_color
:
581 self
->entry
->data
.normal
.mask_disabled_color
) :
583 (self
== self
->frame
->selected
?
584 self
->entry
->data
.normal
.mask_selected_color
:
585 self
->entry
->data
.normal
.mask_normal_color
));
586 clear
->texture
[0].data
.mask
.color
= c
;
588 clear
->surface
.parent
= item_a
;
589 clear
->surface
.parentx
= PADDING
;
590 clear
->surface
.parenty
= frame
->item_margin
.top
;
591 RrPaint(clear
, self
->icon
,
592 ITEM_HEIGHT
- frame
->item_margin
.top
593 - frame
->item_margin
.bottom
,
594 ITEM_HEIGHT
- frame
->item_margin
.top
595 - frame
->item_margin
.bottom
);
596 XMapWindow(obt_display
, self
->icon
);
598 XUnmapWindow(obt_display
, self
->icon
);
600 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
601 RrAppearance
*bullet_a
;
602 XMoveResizeWindow(obt_display
, self
->bullet
,
603 self
->frame
->text_x
+ self
->frame
->text_w
-
604 ITEM_HEIGHT
+ PADDING
, PADDING
,
605 ITEM_HEIGHT
- 2*PADDING
,
606 ITEM_HEIGHT
- 2*PADDING
);
607 bullet_a
= (self
== self
->frame
->selected
?
608 ob_rr_theme
->a_menu_bullet_selected
:
609 ob_rr_theme
->a_menu_bullet_normal
);
610 bullet_a
->surface
.parent
= item_a
;
611 bullet_a
->surface
.parentx
=
612 self
->frame
->text_x
+ self
->frame
->text_w
- ITEM_HEIGHT
+ PADDING
;
613 bullet_a
->surface
.parenty
= PADDING
;
614 RrPaint(bullet_a
, self
->bullet
,
615 ITEM_HEIGHT
- 2*PADDING
,
616 ITEM_HEIGHT
- 2*PADDING
);
617 XMapWindow(obt_display
, self
->bullet
);
619 XUnmapWindow(obt_display
, self
->bullet
);
624 /*! this code is taken from the menu_frame_render. if that changes, this won't
626 static gint
menu_entry_frame_get_height(ObMenuEntryFrame
*self
,
627 gboolean first_entry
,
636 t
= self
->entry
->type
;
638 /* this is the More... entry, it's NORMAL type */
639 t
= OB_MENU_ENTRY_TYPE_NORMAL
;
642 case OB_MENU_ENTRY_TYPE_NORMAL
:
643 case OB_MENU_ENTRY_TYPE_SUBMENU
:
644 h
+= ob_rr_theme
->menu_font_height
;
646 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
647 if (self
->entry
->data
.separator
.label
!= NULL
) {
648 h
+= ob_rr_theme
->menu_title_height
+
649 (ob_rr_theme
->mbwidth
- PADDING
) * 2;
651 /* if the first entry is a labeled separator, then make its border
652 overlap with the menu's outside border */
654 h
-= ob_rr_theme
->mbwidth
;
655 /* if the last entry is a labeled separator, then make its border
656 overlap with the menu's outside border */
658 h
-= ob_rr_theme
->mbwidth
;
660 h
+= ob_rr_theme
->menu_sep_width
+
661 2*ob_rr_theme
->menu_sep_paddingy
- PADDING
* 2;
669 void menu_frame_render(ObMenuFrame
*self
)
672 gint tw
, th
; /* temps */
674 gboolean has_icon
= FALSE
;
678 /* find text dimensions */
680 STRUT_SET(self
->item_margin
, 0, 0, 0, 0);
685 e
= self
->entries
->data
;
686 ob_rr_theme
->a_menu_text_normal
->texture
[0].data
.text
.string
= "";
687 tw
= RrMinWidth(ob_rr_theme
->a_menu_text_normal
);
692 RrMargins(ob_rr_theme
->a_menu_normal
, &l
, &t
, &r
, &b
);
693 STRUT_SET(self
->item_margin
,
694 MAX(self
->item_margin
.left
, l
),
695 MAX(self
->item_margin
.top
, t
),
696 MAX(self
->item_margin
.right
, r
),
697 MAX(self
->item_margin
.bottom
, b
));
698 RrMargins(ob_rr_theme
->a_menu_selected
, &l
, &t
, &r
, &b
);
699 STRUT_SET(self
->item_margin
,
700 MAX(self
->item_margin
.left
, l
),
701 MAX(self
->item_margin
.top
, t
),
702 MAX(self
->item_margin
.right
, r
),
703 MAX(self
->item_margin
.bottom
, b
));
704 RrMargins(ob_rr_theme
->a_menu_disabled
, &l
, &t
, &r
, &b
);
705 STRUT_SET(self
->item_margin
,
706 MAX(self
->item_margin
.left
, l
),
707 MAX(self
->item_margin
.top
, t
),
708 MAX(self
->item_margin
.right
, r
),
709 MAX(self
->item_margin
.bottom
, b
));
710 RrMargins(ob_rr_theme
->a_menu_disabled_selected
, &l
, &t
, &r
, &b
);
711 STRUT_SET(self
->item_margin
,
712 MAX(self
->item_margin
.left
, l
),
713 MAX(self
->item_margin
.top
, t
),
714 MAX(self
->item_margin
.right
, r
),
715 MAX(self
->item_margin
.bottom
, b
));
718 /* render the entries */
720 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
721 RrAppearance
*text_a
;
724 /* if the first entry is a labeled separator, then make its border
725 overlap with the menu's outside border */
726 if (it
== self
->entries
&&
727 e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
728 e
->entry
->data
.separator
.label
)
730 h
-= ob_rr_theme
->mbwidth
;
733 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
734 e
->entry
->data
.separator
.label
)
736 e
->border
= ob_rr_theme
->mbwidth
;
739 RECT_SET_POINT(e
->area
, 0, h
+e
->border
);
740 XMoveWindow(obt_display
, e
->window
,
741 e
->area
.x
-e
->border
, e
->area
.y
-e
->border
);
742 XSetWindowBorderWidth(obt_display
, e
->window
, e
->border
);
743 XSetWindowBorder(obt_display
, e
->window
,
744 RrColorPixel(ob_rr_theme
->menu_border_color
));
746 text_a
= (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
747 !e
->entry
->data
.normal
.enabled
?
749 (e
== self
->selected
?
750 ob_rr_theme
->a_menu_text_disabled_selected
:
751 ob_rr_theme
->a_menu_text_disabled
) :
753 (e
== self
->selected
?
754 ob_rr_theme
->a_menu_text_selected
:
755 ob_rr_theme
->a_menu_text_normal
));
756 switch (e
->entry
->type
) {
757 case OB_MENU_ENTRY_TYPE_NORMAL
:
758 text_a
->texture
[0].data
.text
.string
= e
->entry
->data
.normal
.label
;
759 tw
= RrMinWidth(text_a
);
760 tw
= MIN(tw
, MAX_MENU_WIDTH
);
761 th
= ob_rr_theme
->menu_font_height
;
763 if (e
->entry
->data
.normal
.icon
||
764 e
->entry
->data
.normal
.mask
)
767 case OB_MENU_ENTRY_TYPE_SUBMENU
:
768 sub
= e
->entry
->data
.submenu
.submenu
;
769 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
770 tw
= RrMinWidth(text_a
);
771 tw
= MIN(tw
, MAX_MENU_WIDTH
);
772 th
= ob_rr_theme
->menu_font_height
;
774 if (e
->entry
->data
.normal
.icon
||
775 e
->entry
->data
.normal
.mask
)
778 tw
+= ITEM_HEIGHT
- PADDING
;
780 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
781 if (e
->entry
->data
.separator
.label
!= NULL
) {
782 ob_rr_theme
->a_menu_text_title
->texture
[0].data
.text
.string
=
783 e
->entry
->data
.separator
.label
;
784 tw
= RrMinWidth(ob_rr_theme
->a_menu_text_title
) +
785 2*ob_rr_theme
->paddingx
;
786 tw
= MIN(tw
, MAX_MENU_WIDTH
);
787 th
= ob_rr_theme
->menu_title_height
+
788 (ob_rr_theme
->mbwidth
- PADDING
) *2;
791 th
= ob_rr_theme
->menu_sep_width
+
792 2*ob_rr_theme
->menu_sep_paddingy
- 2*PADDING
;
796 g_assert_not_reached();
804 /* if the last entry is a labeled separator, then make its border
805 overlap with the menu's outside border */
806 it
= g_list_last(self
->entries
);
807 e
= it
? it
->data
: NULL
;
808 if (e
&& e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
809 e
->entry
->data
.separator
.label
)
811 h
-= ob_rr_theme
->mbwidth
;
814 self
->text_x
= PADDING
;
819 w
+= ITEM_HEIGHT
+ PADDING
;
820 self
->text_x
+= ITEM_HEIGHT
+ PADDING
;
827 XResizeWindow(obt_display
, self
->window
, w
, h
);
831 RrPaint(self
->a_items
, self
->window
, w
, h
);
833 for (it
= self
->entries
; it
; it
= g_list_next(it
))
834 menu_entry_frame_render(it
->data
);
836 w
+= ob_rr_theme
->mbwidth
* 2;
837 h
+= ob_rr_theme
->mbwidth
* 2;
839 RECT_SET_SIZE(self
->area
, w
, h
);
844 static void menu_frame_update(ObMenuFrame
*self
)
850 menu_pipe_execute(self
->menu
);
851 menu_find_submenus(self
->menu
);
853 self
->selected
= NULL
;
855 /* start at show_from */
856 mit
= g_list_nth(self
->menu
->entries
, self
->show_from
);
858 /* go through the menu's and frame's entries and connect the frame entries
859 to the menu entries */
860 for (fit
= self
->entries
; mit
&& fit
;
861 mit
= g_list_next(mit
), fit
= g_list_next(fit
))
863 ObMenuEntryFrame
*f
= fit
->data
;
864 f
->entry
= mit
->data
;
867 /* if there are more menu entries than in the frame, add them */
869 ObMenuEntryFrame
*e
= menu_entry_frame_new(mit
->data
, self
);
870 self
->entries
= g_list_append(self
->entries
, e
);
871 mit
= g_list_next(mit
);
874 /* if there are more frame entries than menu entries then get rid of
877 GList
*n
= g_list_next(fit
);
878 menu_entry_frame_free(fit
->data
);
879 self
->entries
= g_list_delete_link(self
->entries
, fit
);
883 /* * make the menu fit on the screen */
885 /* calculate the height of the menu */
887 for (fit
= self
->entries
; fit
; fit
= g_list_next(fit
))
888 h
+= menu_entry_frame_get_height(fit
->data
,
889 fit
== self
->entries
,
890 g_list_next(fit
) == NULL
);
891 /* add the border at the top and bottom */
892 h
+= ob_rr_theme
->mbwidth
* 2;
894 a
= screen_physical_area_monitor(self
->monitor
);
898 gboolean last_entry
= TRUE
;
900 /* take the height of our More... entry into account */
901 h
+= menu_entry_frame_get_height(NULL
, FALSE
, TRUE
);
903 /* start at the end of the entries */
904 flast
= g_list_last(self
->entries
);
906 /* pull out all the entries from the frame that don't
907 fit on the screen, leaving at least 1 though */
908 while (h
> a
->height
&& g_list_previous(flast
) != NULL
) {
909 /* update the height, without this entry */
910 h
-= menu_entry_frame_get_height(flast
->data
, FALSE
, last_entry
);
912 /* destroy the entry we're not displaying */
914 flast
= g_list_previous(flast
);
915 menu_entry_frame_free(tmp
->data
);
916 self
->entries
= g_list_delete_link(self
->entries
, tmp
);
918 /* only the first one that we see is the last entry in the menu */
923 ObMenuEntry
*more_entry
;
924 ObMenuEntryFrame
*more_frame
;
925 /* make the More... menu entry frame which will display in this
927 if self->menu->more_menu is NULL that means that this is already
928 More... menu, so just use ourself.
930 more_entry
= menu_get_more((self
->menu
->more_menu
?
931 self
->menu
->more_menu
:
933 /* continue where we left off */
935 g_list_length(self
->entries
));
936 more_frame
= menu_entry_frame_new(more_entry
, self
);
937 /* make it get deleted when the menu frame goes away */
938 menu_entry_unref(more_entry
);
940 /* add our More... entry to the frame */
941 self
->entries
= g_list_append(self
->entries
, more_frame
);
945 menu_frame_render(self
);
948 static gboolean
menu_frame_is_visible(ObMenuFrame
*self
)
950 return !!(g_list_find(menu_frame_visible
, self
));
953 static gboolean
menu_frame_show(ObMenuFrame
*self
)
957 /* determine if the underlying menu is already visible */
958 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
959 ObMenuFrame
*f
= it
->data
;
960 if (f
->menu
== self
->menu
)
964 if (self
->menu
->update_func
)
965 if (!self
->menu
->update_func(self
, self
->menu
->data
))
969 if (menu_frame_visible
== NULL
) {
970 /* no menus shown yet */
972 /* grab the pointer in such a way as to pass through "owner events"
973 so that we can get enter/leave notifies in the menu. */
974 if (!grab_pointer(TRUE
, FALSE
, OB_CURSOR_POINTER
))
976 if (!grab_keyboard()) {
982 menu_frame_update(self
);
984 menu_frame_visible
= g_list_prepend(menu_frame_visible
, self
);
986 if (self
->menu
->show_func
)
987 self
->menu
->show_func(self
, self
->menu
->data
);
992 gboolean
menu_frame_show_topmenu(ObMenuFrame
*self
, gint x
, gint y
,
997 if (menu_frame_is_visible(self
))
999 if (!menu_frame_show(self
))
1002 if (self
->menu
->place_func
)
1003 self
->menu
->place_func(self
, &x
, &y
, mouse
, self
->menu
->data
);
1005 menu_frame_place_topmenu(self
, &x
, &y
);
1007 menu_frame_move(self
, x
, y
);
1009 XMapWindow(obt_display
, self
->window
);
1011 if (screen_pointer_pos(&px
, &py
)) {
1012 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
1013 if (e
&& e
->frame
== self
)
1020 /*! Stop hiding an open submenu.
1021 @child The OnMenuFrame of the submenu to be hidden
1023 static void remove_submenu_hide_timeout(ObMenuFrame
*child
)
1025 if (submenu_hide_timer
) g_source_remove(submenu_hide_timer
);
1026 submenu_hide_timer
= 0;
1029 gboolean
menu_frame_show_submenu(ObMenuFrame
*self
, ObMenuFrame
*parent
,
1030 ObMenuEntryFrame
*parent_entry
)
1035 if (menu_frame_is_visible(self
))
1038 self
->monitor
= parent
->monitor
;
1039 self
->parent
= parent
;
1040 self
->parent_entry
= parent_entry
;
1042 /* set up parent's child to be us */
1043 if ((parent
->child
) != self
) {
1045 menu_frame_hide(parent
->child
);
1046 parent
->child
= self
;
1047 parent
->child_entry
= parent_entry
;
1050 if (!menu_frame_show(self
)) {
1051 parent
->child
= NULL
;
1052 parent
->child_entry
= NULL
;
1056 menu_frame_place_submenu(self
, &x
, &y
);
1057 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1060 /*try the other side */
1061 self
->direction_right
= !self
->direction_right
;
1062 menu_frame_place_submenu(self
, &x
, &y
);
1063 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1065 menu_frame_move(self
, x
+ dx
, y
+ dy
);
1067 XMapWindow(obt_display
, self
->window
);
1069 if (screen_pointer_pos(&px
, &py
)) {
1070 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
1071 if (e
&& e
->frame
== self
)
1078 static void menu_frame_hide(ObMenuFrame
*self
)
1080 ObMenu
*const menu
= self
->menu
;
1081 GList
*it
= g_list_find(menu_frame_visible
, self
);
1082 gulong ignore_start
;
1087 if (menu
->hide_func
)
1088 menu
->hide_func(self
, menu
->data
);
1091 menu_frame_hide(self
->child
);
1094 remove_submenu_hide_timeout(self
);
1096 self
->parent
->child
= NULL
;
1097 self
->parent
->child_entry
= NULL
;
1099 self
->parent
= NULL
;
1100 self
->parent_entry
= NULL
;
1102 menu_frame_visible
= g_list_delete_link(menu_frame_visible
, it
);
1104 if (menu_frame_visible
== NULL
) {
1105 /* last menu shown */
1110 ignore_start
= event_start_ignore_all_enters();
1111 XUnmapWindow(obt_display
, self
->window
);
1112 event_end_ignore_all_enters(ignore_start
);
1114 menu_frame_free(self
);
1116 if (menu
->cleanup_func
)
1117 menu
->cleanup_func(menu
, menu
->data
);
1120 void menu_frame_hide_all(void)
1124 if (config_submenu_show_delay
) {
1125 /* remove any submenu open requests */
1126 if (submenu_show_timer
) g_source_remove(submenu_show_timer
);
1127 submenu_show_timer
= 0;
1129 if ((it
= g_list_last(menu_frame_visible
)))
1130 menu_frame_hide(it
->data
);
1133 ObMenuFrame
* menu_frame_under(gint x
, gint y
)
1135 ObMenuFrame
*ret
= NULL
;
1138 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
1139 ObMenuFrame
*f
= it
->data
;
1141 if (RECT_CONTAINS(f
->area
, x
, y
)) {
1149 ObMenuEntryFrame
* menu_entry_frame_under(gint x
, gint y
)
1152 ObMenuEntryFrame
*ret
= NULL
;
1155 if ((frame
= menu_frame_under(x
, y
))) {
1156 x
-= ob_rr_theme
->mbwidth
+ frame
->area
.x
;
1157 y
-= ob_rr_theme
->mbwidth
+ frame
->area
.y
;
1159 for (it
= frame
->entries
; it
; it
= g_list_next(it
)) {
1160 ObMenuEntryFrame
*e
= it
->data
;
1162 if (RECT_CONTAINS(e
->area
, x
, y
)) {
1171 static gboolean
submenu_show_timeout(gpointer data
)
1173 g_assert(menu_frame_visible
);
1174 menu_entry_frame_show_submenu((ObMenuEntryFrame
*)data
);
1178 static gboolean
submenu_hide_timeout(gpointer data
)
1180 g_assert(menu_frame_visible
);
1181 menu_frame_hide((ObMenuFrame
*)data
);
1185 void menu_frame_select(ObMenuFrame
*self
, ObMenuEntryFrame
*entry
,
1188 ObMenuEntryFrame
*old
= self
->selected
;
1189 ObMenuFrame
*oldchild
= self
->child
;
1190 ObMenuEntryFrame
*oldchild_entry
= self
->child_entry
;
1192 /* if the user selected a separator, ignore it and reselect what we had
1194 if (entry
&& entry
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
1198 (!old
|| old
->entry
->type
!= OB_MENU_ENTRY_TYPE_SUBMENU
))
1201 /* if the user left this menu but we have a submenu open, move the
1202 selection back to that submenu */
1203 if (!entry
&& oldchild_entry
)
1204 entry
= oldchild_entry
;
1206 if (config_submenu_show_delay
) {
1207 /* remove any submenu open requests */
1208 if (submenu_show_timer
) g_source_remove(submenu_show_timer
);
1209 submenu_show_timer
= 0;
1212 self
->selected
= entry
;
1215 menu_entry_frame_render(old
);
1217 if (oldchild_entry
) {
1218 /* There is an open submenu */
1219 if (oldchild_entry
== self
->selected
) {
1220 /* The open submenu has been reselected, so stop hiding the
1222 remove_submenu_hide_timeout(oldchild
);
1224 else if (oldchild_entry
== old
) {
1225 /* The open submenu was selected and is no longer, so hide the
1227 if (immediate
|| config_submenu_hide_delay
== 0)
1228 menu_frame_hide(oldchild
);
1229 else if (config_submenu_hide_delay
> 0) {
1230 if (submenu_hide_timer
) g_source_remove(submenu_hide_timer
);
1231 submenu_hide_timer
=
1232 g_timeout_add_full(G_PRIORITY_DEFAULT
,
1233 config_submenu_hide_delay
,
1234 submenu_hide_timeout
, oldchild
, NULL
);
1239 if (self
->selected
) {
1240 menu_entry_frame_render(self
->selected
);
1242 if (self
->selected
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
1243 /* only show if the submenu isn't already showing */
1244 if (oldchild_entry
!= self
->selected
) {
1245 if (immediate
|| config_submenu_hide_delay
== 0)
1246 menu_entry_frame_show_submenu(self
->selected
);
1247 else if (config_submenu_hide_delay
> 0) {
1248 if (submenu_show_timer
)
1249 g_source_remove(submenu_show_timer
);
1250 submenu_show_timer
=
1251 g_timeout_add_full(G_PRIORITY_DEFAULT
,
1252 config_submenu_show_delay
,
1253 submenu_show_timeout
,
1254 self
->selected
, NULL
);
1257 /* hide the grandchildren of this menu. and move the cursor to
1259 else if (immediate
&& self
->child
&& self
->child
->child
) {
1260 menu_frame_hide(self
->child
->child
);
1261 menu_frame_select(self
->child
, NULL
, TRUE
);
1267 void menu_entry_frame_show_submenu(ObMenuEntryFrame
*self
)
1271 if (!self
->entry
->data
.submenu
.submenu
) return;
1273 f
= menu_frame_new(self
->entry
->data
.submenu
.submenu
,
1274 self
->entry
->data
.submenu
.show_from
,
1275 self
->frame
->client
);
1276 /* pass our direction on to our child */
1277 f
->direction_right
= self
->frame
->direction_right
;
1279 if (!menu_frame_show_submenu(f
, self
->frame
, self
))
1283 void menu_entry_frame_execute(ObMenuEntryFrame
*self
, guint state
)
1285 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1286 self
->entry
->data
.normal
.enabled
)
1288 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1290 ObMenuEntry
*entry
= self
->entry
;
1291 ObMenuExecuteFunc func
= self
->frame
->menu
->execute_func
;
1292 gpointer data
= self
->frame
->menu
->data
;
1293 GSList
*acts
= self
->entry
->data
.normal
.actions
;
1294 ObClient
*client
= self
->frame
->client
;
1295 ObMenuFrame
*frame
= self
->frame
;
1296 guint mods
= obt_keyboard_only_modmasks(state
);
1298 /* release grabs before executing the shit */
1299 if (!(mods
& ControlMask
)) {
1300 event_cancel_all_key_grabs();
1305 func(entry
, frame
, client
, state
, data
);
1307 actions_run_acts(acts
, OB_USER_ACTION_MENU_SELECTION
,
1308 state
, -1, -1, 0, OB_FRAME_CONTEXT_NONE
, client
);
1312 void menu_frame_select_previous(ObMenuFrame
*self
)
1314 GList
*it
= NULL
, *start
;
1316 if (self
->entries
) {
1317 start
= it
= g_list_find(self
->entries
, self
->selected
);
1319 ObMenuEntryFrame
*e
;
1321 it
= it
? g_list_previous(it
) : g_list_last(self
->entries
);
1327 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1329 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1334 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);
1337 void menu_frame_select_next(ObMenuFrame
*self
)
1339 GList
*it
= NULL
, *start
;
1341 if (self
->entries
) {
1342 start
= it
= g_list_find(self
->entries
, self
->selected
);
1344 ObMenuEntryFrame
*e
;
1346 it
= it
? g_list_next(it
) : self
->entries
;
1352 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1354 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1359 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);
1362 void menu_frame_select_first(ObMenuFrame
*self
)
1366 if (self
->entries
) {
1367 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
1368 ObMenuEntryFrame
*e
= it
->data
;
1369 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1371 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1375 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);
1378 void menu_frame_select_last(ObMenuFrame
*self
)
1382 if (self
->entries
) {
1383 for (it
= g_list_last(self
->entries
); it
; it
= g_list_previous(it
)) {
1384 ObMenuEntryFrame
*e
= it
->data
;
1385 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1387 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1391 menu_frame_select(self
, it
? it
->data
: NULL
, FALSE
);