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(ob_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(ob_display
, ob_screen
),
91 XSetWindowBorderWidth(ob_display
, self
->window
, ob_rr_theme
->mbwidth
);
92 XSetWindowBorder(ob_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(ob_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(ob_display
, self
->window
);
149 XMapWindow(ob_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(ob_display
, self
->text
);
191 XDestroyWindow(ob_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(ob_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(ob_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(ob_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
);
348 static void menu_entry_frame_render(ObMenuEntryFrame
*self
)
350 RrAppearance
*item_a
, *text_a
;
353 ObMenuFrame
*frame
= self
->frame
;
355 switch (self
->entry
->type
) {
356 case OB_MENU_ENTRY_TYPE_NORMAL
:
357 case OB_MENU_ENTRY_TYPE_SUBMENU
:
358 item_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
359 !self
->entry
->data
.normal
.enabled
?
361 (self
== self
->frame
->selected
?
362 self
->a_disabled_selected
: self
->a_disabled
) :
364 (self
== self
->frame
->selected
?
365 self
->a_selected
: self
->a_normal
));
368 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
369 if (self
->entry
->data
.separator
.label
) {
370 item_a
= self
->frame
->a_title
;
371 th
= ob_rr_theme
->menu_title_height
;
373 item_a
= self
->a_normal
;
374 th
= SEPARATOR_HEIGHT
+ 2*PADDING
;
378 g_assert_not_reached();
380 RECT_SET_SIZE(self
->area
, self
->frame
->inner_w
, th
);
381 XResizeWindow(ob_display
, self
->window
,
382 self
->area
.width
, self
->area
.height
);
383 item_a
->surface
.parent
= self
->frame
->a_items
;
384 item_a
->surface
.parentx
= self
->area
.x
;
385 item_a
->surface
.parenty
= self
->area
.y
;
386 RrPaint(item_a
, self
->window
, self
->area
.width
, self
->area
.height
);
388 switch (self
->entry
->type
) {
389 case OB_MENU_ENTRY_TYPE_NORMAL
:
390 text_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
391 !self
->entry
->data
.normal
.enabled
?
393 (self
== self
->frame
->selected
?
394 self
->a_text_disabled_selected
: self
->a_text_disabled
) :
396 (self
== self
->frame
->selected
?
397 self
->a_text_selected
: self
->a_text_normal
));
398 text_a
->texture
[0].data
.text
.string
= self
->entry
->data
.normal
.label
;
399 if (self
->entry
->data
.normal
.shortcut
&&
400 (self
->frame
->menu
->show_all_shortcuts
||
401 self
->entry
->data
.normal
.shortcut_position
> 0))
403 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
404 text_a
->texture
[0].data
.text
.shortcut_pos
=
405 self
->entry
->data
.normal
.shortcut_position
;
407 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
409 case OB_MENU_ENTRY_TYPE_SUBMENU
:
410 text_a
= (self
== self
->frame
->selected
?
411 self
->a_text_selected
:
412 self
->a_text_normal
);
413 sub
= self
->entry
->data
.submenu
.submenu
;
414 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
415 if (sub
->shortcut
&& (self
->frame
->menu
->show_all_shortcuts
||
416 sub
->shortcut_position
> 0))
418 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
419 text_a
->texture
[0].data
.text
.shortcut_pos
= sub
->shortcut_position
;
421 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
423 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
424 if (self
->entry
->data
.separator
.label
!= NULL
)
425 text_a
= self
->a_text_title
;
427 text_a
= self
->a_text_normal
;
431 switch (self
->entry
->type
) {
432 case OB_MENU_ENTRY_TYPE_NORMAL
:
433 XMoveResizeWindow(ob_display
, self
->text
,
434 self
->frame
->text_x
, PADDING
,
436 ITEM_HEIGHT
- 2*PADDING
);
437 text_a
->surface
.parent
= item_a
;
438 text_a
->surface
.parentx
= self
->frame
->text_x
;
439 text_a
->surface
.parenty
= PADDING
;
440 RrPaint(text_a
, self
->text
, self
->frame
->text_w
,
441 ITEM_HEIGHT
- 2*PADDING
);
443 case OB_MENU_ENTRY_TYPE_SUBMENU
:
444 XMoveResizeWindow(ob_display
, self
->text
,
445 self
->frame
->text_x
, PADDING
,
446 self
->frame
->text_w
- ITEM_HEIGHT
,
447 ITEM_HEIGHT
- 2*PADDING
);
448 text_a
->surface
.parent
= item_a
;
449 text_a
->surface
.parentx
= self
->frame
->text_x
;
450 text_a
->surface
.parenty
= PADDING
;
451 RrPaint(text_a
, self
->text
, self
->frame
->text_w
- ITEM_HEIGHT
,
452 ITEM_HEIGHT
- 2*PADDING
);
454 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
455 if (self
->entry
->data
.separator
.label
!= NULL
) {
456 /* labeled separator */
457 XMoveResizeWindow(ob_display
, self
->text
,
458 ob_rr_theme
->paddingx
, ob_rr_theme
->paddingy
,
459 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
460 ob_rr_theme
->menu_title_height
-
461 2*ob_rr_theme
->paddingy
);
462 text_a
->surface
.parent
= item_a
;
463 text_a
->surface
.parentx
= ob_rr_theme
->paddingx
;
464 text_a
->surface
.parenty
= ob_rr_theme
->paddingy
;
465 RrPaint(text_a
, self
->text
,
466 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
467 ob_rr_theme
->menu_title_height
-
468 2*ob_rr_theme
->paddingy
);
470 /* unlabeled separaator */
471 XMoveResizeWindow(ob_display
, self
->text
, PADDING
, PADDING
,
472 self
->area
.width
- 2*PADDING
, SEPARATOR_HEIGHT
);
473 self
->a_separator
->surface
.parent
= item_a
;
474 self
->a_separator
->surface
.parentx
= PADDING
;
475 self
->a_separator
->surface
.parenty
= PADDING
;
476 self
->a_separator
->texture
[0].data
.lineart
.color
=
477 text_a
->texture
[0].data
.text
.color
;
478 self
->a_separator
->texture
[0].data
.lineart
.x1
= 2*PADDING
;
479 self
->a_separator
->texture
[0].data
.lineart
.y1
= SEPARATOR_HEIGHT
/2;
480 self
->a_separator
->texture
[0].data
.lineart
.x2
=
481 self
->area
.width
- 4*PADDING
;
482 self
->a_separator
->texture
[0].data
.lineart
.y2
= SEPARATOR_HEIGHT
/2;
483 RrPaint(self
->a_separator
, self
->text
,
484 self
->area
.width
- 2*PADDING
, SEPARATOR_HEIGHT
);
489 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
490 self
->entry
->data
.normal
.icon_data
)
492 XMoveResizeWindow(ob_display
, self
->icon
,
493 PADDING
, frame
->item_margin
.top
,
494 ITEM_HEIGHT
- frame
->item_margin
.top
495 - frame
->item_margin
.bottom
,
496 ITEM_HEIGHT
- frame
->item_margin
.top
497 - frame
->item_margin
.bottom
);
498 self
->a_icon
->texture
[0].data
.rgba
.width
=
499 self
->entry
->data
.normal
.icon_width
;
500 self
->a_icon
->texture
[0].data
.rgba
.height
=
501 self
->entry
->data
.normal
.icon_height
;
502 self
->a_icon
->texture
[0].data
.rgba
.alpha
=
503 self
->entry
->data
.normal
.icon_alpha
;
504 self
->a_icon
->texture
[0].data
.rgba
.data
=
505 self
->entry
->data
.normal
.icon_data
;
506 self
->a_icon
->surface
.parent
= item_a
;
507 self
->a_icon
->surface
.parentx
= PADDING
;
508 self
->a_icon
->surface
.parenty
= frame
->item_margin
.top
;
509 RrPaint(self
->a_icon
, self
->icon
,
510 ITEM_HEIGHT
- frame
->item_margin
.top
511 - frame
->item_margin
.bottom
,
512 ITEM_HEIGHT
- frame
->item_margin
.top
513 - frame
->item_margin
.bottom
);
514 XMapWindow(ob_display
, self
->icon
);
515 } else if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
516 self
->entry
->data
.normal
.mask
)
520 XMoveResizeWindow(ob_display
, self
->icon
,
521 PADDING
, frame
->item_margin
.top
,
522 ITEM_HEIGHT
- frame
->item_margin
.top
523 - frame
->item_margin
.bottom
,
524 ITEM_HEIGHT
- frame
->item_margin
.top
525 - frame
->item_margin
.bottom
);
526 self
->a_mask
->texture
[0].data
.mask
.mask
=
527 self
->entry
->data
.normal
.mask
;
529 c
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
530 !self
->entry
->data
.normal
.enabled
?
532 (self
== self
->frame
->selected
?
533 self
->entry
->data
.normal
.mask_disabled_selected_color
:
534 self
->entry
->data
.normal
.mask_disabled_color
) :
536 (self
== self
->frame
->selected
?
537 self
->entry
->data
.normal
.mask_selected_color
:
538 self
->entry
->data
.normal
.mask_normal_color
));
539 self
->a_mask
->texture
[0].data
.mask
.color
= c
;
541 self
->a_mask
->surface
.parent
= item_a
;
542 self
->a_mask
->surface
.parentx
= PADDING
;
543 self
->a_mask
->surface
.parenty
= frame
->item_margin
.top
;
544 RrPaint(self
->a_mask
, self
->icon
,
545 ITEM_HEIGHT
- frame
->item_margin
.top
546 - frame
->item_margin
.bottom
,
547 ITEM_HEIGHT
- frame
->item_margin
.top
548 - frame
->item_margin
.bottom
);
549 XMapWindow(ob_display
, self
->icon
);
551 XUnmapWindow(ob_display
, self
->icon
);
553 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
554 RrAppearance
*bullet_a
;
555 XMoveResizeWindow(ob_display
, self
->bullet
,
556 self
->frame
->text_x
+ self
->frame
->text_w
-
557 ITEM_HEIGHT
+ PADDING
, PADDING
,
558 ITEM_HEIGHT
- 2*PADDING
,
559 ITEM_HEIGHT
- 2*PADDING
);
560 bullet_a
= (self
== self
->frame
->selected
?
561 self
->a_bullet_selected
:
562 self
->a_bullet_normal
);
563 bullet_a
->surface
.parent
= item_a
;
564 bullet_a
->surface
.parentx
=
565 self
->frame
->text_x
+ self
->frame
->text_w
- ITEM_HEIGHT
+ PADDING
;
566 bullet_a
->surface
.parenty
= PADDING
;
567 RrPaint(bullet_a
, self
->bullet
,
568 ITEM_HEIGHT
- 2*PADDING
,
569 ITEM_HEIGHT
- 2*PADDING
);
570 XMapWindow(ob_display
, self
->bullet
);
572 XUnmapWindow(ob_display
, self
->bullet
);
577 /*! this code is taken from the menu_frame_render. if that changes, this won't
579 static gint
menu_entry_frame_get_height(ObMenuEntryFrame
*self
,
580 gboolean first_entry
,
589 t
= self
->entry
->type
;
591 /* this is the More... entry, it's NORMAL type */
592 t
= OB_MENU_ENTRY_TYPE_NORMAL
;
595 case OB_MENU_ENTRY_TYPE_NORMAL
:
596 case OB_MENU_ENTRY_TYPE_SUBMENU
:
597 h
+= ob_rr_theme
->menu_font_height
;
599 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
600 if (self
->entry
->data
.separator
.label
!= NULL
) {
601 h
+= ob_rr_theme
->menu_title_height
+
602 (ob_rr_theme
->mbwidth
- PADDING
) * 2;
604 /* if the first entry is a labeled separator, then make its border
605 overlap with the menu's outside border */
607 h
-= ob_rr_theme
->mbwidth
;
608 /* if the last entry is a labeled separator, then make its border
609 overlap with the menu's outside border */
611 h
-= ob_rr_theme
->mbwidth
;
613 h
+= SEPARATOR_HEIGHT
;
621 void menu_frame_render(ObMenuFrame
*self
)
624 gint tw
, th
; /* temps */
626 gboolean has_icon
= FALSE
;
630 /* find text dimensions */
632 STRUT_SET(self
->item_margin
, 0, 0, 0, 0);
635 ObMenuEntryFrame
*e
= self
->entries
->data
;
638 e
->a_text_normal
->texture
[0].data
.text
.string
= "";
639 tw
= RrMinWidth(e
->a_text_normal
);
644 RrMargins(e
->a_normal
, &l
, &t
, &r
, &b
);
645 STRUT_SET(self
->item_margin
,
646 MAX(self
->item_margin
.left
, l
),
647 MAX(self
->item_margin
.top
, t
),
648 MAX(self
->item_margin
.right
, r
),
649 MAX(self
->item_margin
.bottom
, b
));
650 RrMargins(e
->a_selected
, &l
, &t
, &r
, &b
);
651 STRUT_SET(self
->item_margin
,
652 MAX(self
->item_margin
.left
, l
),
653 MAX(self
->item_margin
.top
, t
),
654 MAX(self
->item_margin
.right
, r
),
655 MAX(self
->item_margin
.bottom
, b
));
656 RrMargins(e
->a_disabled
, &l
, &t
, &r
, &b
);
657 STRUT_SET(self
->item_margin
,
658 MAX(self
->item_margin
.left
, l
),
659 MAX(self
->item_margin
.top
, t
),
660 MAX(self
->item_margin
.right
, r
),
661 MAX(self
->item_margin
.bottom
, b
));
662 RrMargins(e
->a_disabled_selected
, &l
, &t
, &r
, &b
);
663 STRUT_SET(self
->item_margin
,
664 MAX(self
->item_margin
.left
, l
),
665 MAX(self
->item_margin
.top
, t
),
666 MAX(self
->item_margin
.right
, r
),
667 MAX(self
->item_margin
.bottom
, b
));
670 /* render the entries */
672 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
673 RrAppearance
*text_a
;
676 /* if the first entry is a labeled separator, then make its border
677 overlap with the menu's outside border */
678 if (it
== self
->entries
&&
679 e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
680 e
->entry
->data
.separator
.label
)
682 h
-= ob_rr_theme
->mbwidth
;
685 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
686 e
->entry
->data
.separator
.label
)
688 e
->border
= ob_rr_theme
->mbwidth
;
691 RECT_SET_POINT(e
->area
, 0, h
+e
->border
);
692 XMoveWindow(ob_display
, e
->window
, e
->area
.x
-e
->border
, e
->area
.y
-e
->border
);
693 XSetWindowBorderWidth(ob_display
, e
->window
, e
->border
);
694 XSetWindowBorder(ob_display
, e
->window
,
695 RrColorPixel(ob_rr_theme
->menu_border_color
));
698 text_a
= (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
699 !e
->entry
->data
.normal
.enabled
?
701 (e
== self
->selected
?
702 e
->a_text_disabled_selected
: e
->a_text_disabled
) :
704 (e
== self
->selected
?
705 e
->a_text_selected
: e
->a_text_normal
));
706 switch (e
->entry
->type
) {
707 case OB_MENU_ENTRY_TYPE_NORMAL
:
708 text_a
->texture
[0].data
.text
.string
= e
->entry
->data
.normal
.label
;
709 tw
= RrMinWidth(text_a
);
710 tw
= MIN(tw
, MAX_MENU_WIDTH
);
711 th
= ob_rr_theme
->menu_font_height
;
713 if (e
->entry
->data
.normal
.icon_data
||
714 e
->entry
->data
.normal
.mask
)
717 case OB_MENU_ENTRY_TYPE_SUBMENU
:
718 sub
= e
->entry
->data
.submenu
.submenu
;
719 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
720 tw
= RrMinWidth(text_a
);
721 tw
= MIN(tw
, MAX_MENU_WIDTH
);
722 th
= ob_rr_theme
->menu_font_height
;
724 if (e
->entry
->data
.normal
.icon_data
||
725 e
->entry
->data
.normal
.mask
)
728 tw
+= ITEM_HEIGHT
- PADDING
;
730 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
731 if (e
->entry
->data
.separator
.label
!= NULL
) {
732 e
->a_text_title
->texture
[0].data
.text
.string
=
733 e
->entry
->data
.separator
.label
;
734 tw
= RrMinWidth(e
->a_text_title
);
735 tw
= MIN(tw
, MAX_MENU_WIDTH
);
736 th
= ob_rr_theme
->menu_title_height
+
737 (ob_rr_theme
->mbwidth
- PADDING
) *2;
740 th
= SEPARATOR_HEIGHT
;
750 /* if the last entry is a labeled separator, then make its border
751 overlap with the menu's outside border */
752 it
= g_list_last(self
->entries
);
753 e
= it
? it
->data
: NULL
;
754 if (e
&& e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
755 e
->entry
->data
.separator
.label
)
757 h
-= ob_rr_theme
->mbwidth
;
760 self
->text_x
= PADDING
;
765 w
+= ITEM_HEIGHT
+ PADDING
;
766 self
->text_x
+= ITEM_HEIGHT
+ PADDING
;
773 XResizeWindow(ob_display
, self
->window
, w
, h
);
777 RrPaint(self
->a_items
, self
->window
, w
, h
);
779 for (it
= self
->entries
; it
; it
= g_list_next(it
))
780 menu_entry_frame_render(it
->data
);
782 w
+= ob_rr_theme
->mbwidth
* 2;
783 h
+= ob_rr_theme
->mbwidth
* 2;
785 RECT_SET_SIZE(self
->area
, w
, h
);
790 static void menu_frame_update(ObMenuFrame
*self
)
796 menu_pipe_execute(self
->menu
);
797 menu_find_submenus(self
->menu
);
799 self
->selected
= NULL
;
801 /* start at show_from */
802 mit
= g_list_nth(self
->menu
->entries
, self
->show_from
);
804 /* go through the menu's and frame's entries and connect the frame entries
805 to the menu entries */
806 for (fit
= self
->entries
; mit
&& fit
;
807 mit
= g_list_next(mit
), fit
= g_list_next(fit
))
809 ObMenuEntryFrame
*f
= fit
->data
;
810 f
->entry
= mit
->data
;
813 /* if there are more menu entries than in the frame, add them */
815 ObMenuEntryFrame
*e
= menu_entry_frame_new(mit
->data
, self
);
816 self
->entries
= g_list_append(self
->entries
, e
);
817 mit
= g_list_next(mit
);
820 /* if there are more frame entries than menu entries then get rid of
823 GList
*n
= g_list_next(fit
);
824 menu_entry_frame_free(fit
->data
);
825 self
->entries
= g_list_delete_link(self
->entries
, fit
);
829 /* * make the menu fit on the screen */
831 /* calculate the height of the menu */
833 for (fit
= self
->entries
; fit
; fit
= g_list_next(fit
))
834 h
+= menu_entry_frame_get_height(fit
->data
,
835 fit
== self
->entries
,
836 g_list_next(fit
) == NULL
);
837 /* add the border at the top and bottom */
838 h
+= ob_rr_theme
->mbwidth
* 2;
840 a
= screen_physical_area_monitor(self
->monitor
);
844 gboolean last_entry
= TRUE
;
846 /* take the height of our More... entry into account */
847 h
+= menu_entry_frame_get_height(NULL
, FALSE
, TRUE
);
849 /* start at the end of the entries */
850 flast
= g_list_last(self
->entries
);
852 /* pull out all the entries from the frame that don't
853 fit on the screen, leaving at least 1 though */
854 while (h
> a
->height
&& g_list_previous(flast
) != NULL
) {
855 /* update the height, without this entry */
856 h
-= menu_entry_frame_get_height(flast
->data
, FALSE
, last_entry
);
858 /* destroy the entry we're not displaying */
860 flast
= g_list_previous(flast
);
861 menu_entry_frame_free(tmp
->data
);
862 self
->entries
= g_list_delete_link(self
->entries
, tmp
);
864 /* only the first one that we see is the last entry in the menu */
869 ObMenuEntry
*more_entry
;
870 ObMenuEntryFrame
*more_frame
;
871 /* make the More... menu entry frame which will display in this
873 if self->menu->more_menu is NULL that means that this is already
874 More... menu, so just use ourself.
876 more_entry
= menu_get_more((self
->menu
->more_menu
?
877 self
->menu
->more_menu
:
879 /* continue where we left off */
881 g_list_length(self
->entries
));
882 more_frame
= menu_entry_frame_new(more_entry
, self
);
883 /* make it get deleted when the menu frame goes away */
884 menu_entry_unref(more_entry
);
886 /* add our More... entry to the frame */
887 self
->entries
= g_list_append(self
->entries
, more_frame
);
891 menu_frame_render(self
);
894 static gboolean
menu_frame_is_visible(ObMenuFrame
*self
)
896 return !!(g_list_find(menu_frame_visible
, self
));
899 static gboolean
menu_frame_show(ObMenuFrame
*self
)
903 /* determine if the underlying menu is already visible */
904 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
905 ObMenuFrame
*f
= it
->data
;
906 if (f
->menu
== self
->menu
)
910 if (self
->menu
->update_func
)
911 if (!self
->menu
->update_func(self
, self
->menu
->data
))
915 if (menu_frame_visible
== NULL
) {
916 /* no menus shown yet */
918 /* grab the pointer in such a way as to pass through "owner events"
919 so that we can get enter/leave notifies in the menu. */
920 if (!grab_pointer(TRUE
, FALSE
, OB_CURSOR_POINTER
))
922 if (!grab_keyboard()) {
928 menu_frame_update(self
);
930 menu_frame_visible
= g_list_prepend(menu_frame_visible
, self
);
932 if (self
->menu
->show_func
)
933 self
->menu
->show_func(self
, self
->menu
->data
);
938 gboolean
menu_frame_show_topmenu(ObMenuFrame
*self
, gint x
, gint y
,
944 if (menu_frame_is_visible(self
))
946 if (!menu_frame_show(self
))
949 /* find the monitor the menu is on */
950 for (i
= 0; i
< screen_num_monitors
; ++i
) {
951 Rect
*a
= screen_physical_area_monitor(i
);
952 if (RECT_CONTAINS(*a
, x
, y
)) {
958 if (self
->menu
->place_func
)
959 self
->menu
->place_func(self
, &x
, &y
, button
, self
->menu
->data
);
961 menu_frame_place_topmenu(self
, &x
, &y
);
963 menu_frame_move(self
, x
, y
);
965 XMapWindow(ob_display
, self
->window
);
967 if (screen_pointer_pos(&px
, &py
)) {
968 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
969 if (e
&& e
->frame
== self
)
976 gboolean
menu_frame_show_submenu(ObMenuFrame
*self
, ObMenuFrame
*parent
,
977 ObMenuEntryFrame
*parent_entry
)
982 if (menu_frame_is_visible(self
))
985 self
->monitor
= parent
->monitor
;
986 self
->parent
= parent
;
987 self
->parent_entry
= parent_entry
;
989 /* set up parent's child to be us */
991 menu_frame_hide(parent
->child
);
992 parent
->child
= self
;
994 if (!menu_frame_show(self
))
997 menu_frame_place_submenu(self
, &x
, &y
);
998 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1001 /*try the other side */
1002 self
->direction_right
= !self
->direction_right
;
1003 menu_frame_place_submenu(self
, &x
, &y
);
1004 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
1006 menu_frame_move(self
, x
+ dx
, y
+ dy
);
1008 XMapWindow(ob_display
, self
->window
);
1010 if (screen_pointer_pos(&px
, &py
)) {
1011 ObMenuEntryFrame
*e
= menu_entry_frame_under(px
, py
);
1012 if (e
&& e
->frame
== self
)
1019 static void menu_frame_hide(ObMenuFrame
*self
)
1021 GList
*it
= g_list_find(menu_frame_visible
, self
);
1026 if (self
->menu
->hide_func
)
1027 self
->menu
->hide_func(self
, self
->menu
->data
);
1030 menu_frame_hide(self
->child
);
1033 self
->parent
->child
= NULL
;
1034 self
->parent
= NULL
;
1035 self
->parent_entry
= NULL
;
1037 menu_frame_visible
= g_list_delete_link(menu_frame_visible
, it
);
1039 if (menu_frame_visible
== NULL
) {
1040 /* last menu shown */
1045 XUnmapWindow(ob_display
, self
->window
);
1047 menu_frame_free(self
);
1050 void menu_frame_hide_all()
1054 if (config_submenu_show_delay
) {
1055 /* remove any submenu open requests */
1056 ob_main_loop_timeout_remove(ob_main_loop
,
1057 menu_entry_frame_submenu_timeout
);
1059 if ((it
= g_list_last(menu_frame_visible
)))
1060 menu_frame_hide(it
->data
);
1063 void menu_frame_hide_all_client(ObClient
*client
)
1065 GList
*it
= g_list_last(menu_frame_visible
);
1067 ObMenuFrame
*f
= it
->data
;
1068 if (f
->client
== client
)
1074 ObMenuFrame
* menu_frame_under(gint x
, gint y
)
1076 ObMenuFrame
*ret
= NULL
;
1079 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
1080 ObMenuFrame
*f
= it
->data
;
1082 if (RECT_CONTAINS(f
->area
, x
, y
)) {
1090 ObMenuEntryFrame
* menu_entry_frame_under(gint x
, gint y
)
1093 ObMenuEntryFrame
*ret
= NULL
;
1096 if ((frame
= menu_frame_under(x
, y
))) {
1097 x
-= ob_rr_theme
->mbwidth
+ frame
->area
.x
;
1098 y
-= ob_rr_theme
->mbwidth
+ frame
->area
.y
;
1100 for (it
= frame
->entries
; it
; it
= g_list_next(it
)) {
1101 ObMenuEntryFrame
*e
= it
->data
;
1103 if (RECT_CONTAINS(e
->area
, x
, y
)) {
1112 static gboolean
menu_entry_frame_submenu_timeout(gpointer data
)
1114 menu_entry_frame_show_submenu((ObMenuEntryFrame
*)data
);
1118 void menu_frame_select(ObMenuFrame
*self
, ObMenuEntryFrame
*entry
,
1121 ObMenuEntryFrame
*old
= self
->selected
;
1122 ObMenuFrame
*oldchild
= self
->child
;
1124 if (entry
&& entry
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
1127 if (old
== entry
) return;
1129 if (config_submenu_show_delay
) {
1130 /* remove any submenu open requests */
1131 ob_main_loop_timeout_remove(ob_main_loop
,
1132 menu_entry_frame_submenu_timeout
);
1135 self
->selected
= entry
;
1138 menu_entry_frame_render(old
);
1140 menu_frame_hide(oldchild
);
1142 if (self
->selected
) {
1143 menu_entry_frame_render(self
->selected
);
1145 if (self
->selected
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
1146 if (config_submenu_show_delay
&& !immediate
) {
1147 /* initiate a new submenu open request */
1148 ob_main_loop_timeout_add(ob_main_loop
,
1149 config_submenu_show_delay
* 1000,
1150 menu_entry_frame_submenu_timeout
,
1151 self
->selected
, g_direct_equal
,
1154 menu_entry_frame_show_submenu(self
->selected
);
1160 void menu_entry_frame_show_submenu(ObMenuEntryFrame
*self
)
1164 if (!self
->entry
->data
.submenu
.submenu
) return;
1166 f
= menu_frame_new(self
->entry
->data
.submenu
.submenu
,
1167 self
->entry
->data
.submenu
.show_from
,
1168 self
->frame
->client
);
1169 /* pass our direction on to our child */
1170 f
->direction_right
= self
->frame
->direction_right
;
1172 menu_frame_show_submenu(f
, self
->frame
, self
);
1175 void menu_entry_frame_execute(ObMenuEntryFrame
*self
, guint state
, Time time
)
1177 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1178 self
->entry
->data
.normal
.enabled
)
1180 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1182 ObMenuEntry
*entry
= self
->entry
;
1183 ObMenuExecuteFunc func
= self
->frame
->menu
->execute_func
;
1184 gpointer data
= self
->frame
->menu
->data
;
1185 GSList
*acts
= self
->entry
->data
.normal
.actions
;
1186 ObClient
*client
= self
->frame
->client
;
1187 ObMenuFrame
*frame
= self
->frame
;
1189 /* release grabs before executing the shit */
1190 if (!(state
& ControlMask
)) {
1191 menu_frame_hide_all();
1196 func(entry
, frame
, client
, state
, data
, time
);
1198 action_run(acts
, client
, state
, time
);
1202 void menu_frame_select_previous(ObMenuFrame
*self
)
1204 GList
*it
= NULL
, *start
;
1206 if (self
->entries
) {
1207 start
= it
= g_list_find(self
->entries
, self
->selected
);
1209 ObMenuEntryFrame
*e
;
1211 it
= it
? g_list_previous(it
) : g_list_last(self
->entries
);
1217 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1219 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1224 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);
1227 void menu_frame_select_next(ObMenuFrame
*self
)
1229 GList
*it
= NULL
, *start
;
1231 if (self
->entries
) {
1232 start
= it
= g_list_find(self
->entries
, self
->selected
);
1234 ObMenuEntryFrame
*e
;
1236 it
= it
? g_list_next(it
) : self
->entries
;
1242 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1244 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1249 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);