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
);
49 static Window
createWindow(Window parent
, gulong mask
,
50 XSetWindowAttributes
*attrib
)
52 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
53 RrDepth(ob_rr_inst
), InputOutput
,
54 RrVisual(ob_rr_inst
), mask
, attrib
);
57 GHashTable
*menu_frame_map
;
59 void menu_frame_startup(gboolean reconfig
)
63 menu_frame_map
= g_hash_table_new(g_int_hash
, g_int_equal
);
66 void menu_frame_shutdown(gboolean reconfig
)
70 g_hash_table_destroy(menu_frame_map
);
73 ObMenuFrame
* menu_frame_new(ObMenu
*menu
, guint show_from
, ObClient
*client
)
76 XSetWindowAttributes attr
;
78 self
= g_new0(ObMenuFrame
, 1);
79 self
->type
= Window_Menu
;
81 self
->selected
= NULL
;
82 self
->client
= client
;
83 self
->direction_right
= TRUE
;
84 self
->show_from
= show_from
;
86 attr
.event_mask
= FRAME_EVENTMASK
;
87 self
->window
= createWindow(RootWindow(ob_display
, ob_screen
),
90 XSetWindowBorderWidth(ob_display
, self
->window
, ob_rr_theme
->mbwidth
);
91 XSetWindowBorder(ob_display
, self
->window
,
92 RrColorPixel(ob_rr_theme
->menu_b_color
));
94 self
->a_title
= RrAppearanceCopy(ob_rr_theme
->a_menu_title
);
95 self
->a_items
= RrAppearanceCopy(ob_rr_theme
->a_menu
);
97 stacking_add(MENU_AS_WINDOW(self
));
102 void menu_frame_free(ObMenuFrame
*self
)
105 while (self
->entries
) {
106 menu_entry_frame_free(self
->entries
->data
);
107 self
->entries
= g_list_delete_link(self
->entries
, self
->entries
);
110 stacking_remove(MENU_AS_WINDOW(self
));
112 XDestroyWindow(ob_display
, self
->window
);
114 RrAppearanceFree(self
->a_items
);
115 RrAppearanceFree(self
->a_title
);
121 static ObMenuEntryFrame
* menu_entry_frame_new(ObMenuEntry
*entry
,
124 ObMenuEntryFrame
*self
;
125 XSetWindowAttributes attr
;
127 self
= g_new0(ObMenuEntryFrame
, 1);
131 menu_entry_ref(entry
);
133 attr
.event_mask
= ENTRY_EVENTMASK
;
134 self
->window
= createWindow(self
->frame
->window
, CWEventMask
, &attr
);
135 self
->text
= createWindow(self
->window
, 0, NULL
);
136 g_hash_table_insert(menu_frame_map
, &self
->window
, self
);
137 g_hash_table_insert(menu_frame_map
, &self
->text
, self
);
138 if (entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
139 self
->icon
= createWindow(self
->window
, 0, NULL
);
140 g_hash_table_insert(menu_frame_map
, &self
->icon
, self
);
142 if (entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
143 self
->bullet
= createWindow(self
->window
, 0, NULL
);
144 g_hash_table_insert(menu_frame_map
, &self
->bullet
, self
);
147 XMapWindow(ob_display
, self
->window
);
148 XMapWindow(ob_display
, self
->text
);
150 self
->a_normal
= RrAppearanceCopy(ob_rr_theme
->a_menu_normal
);
151 self
->a_selected
= RrAppearanceCopy(ob_rr_theme
->a_menu_selected
);
152 self
->a_disabled
= RrAppearanceCopy(ob_rr_theme
->a_menu_disabled
);
153 self
->a_disabled_selected
=
154 RrAppearanceCopy(ob_rr_theme
->a_menu_disabled_selected
);
156 if (entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
) {
157 self
->a_separator
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
158 self
->a_separator
->texture
[0].type
= RR_TEXTURE_LINE_ART
;
160 self
->a_icon
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
161 self
->a_icon
->texture
[0].type
= RR_TEXTURE_RGBA
;
162 self
->a_mask
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
163 self
->a_mask
->texture
[0].type
= RR_TEXTURE_MASK
;
164 self
->a_bullet_normal
=
165 RrAppearanceCopy(ob_rr_theme
->a_menu_bullet_normal
);
166 self
->a_bullet_selected
=
167 RrAppearanceCopy(ob_rr_theme
->a_menu_bullet_selected
);
170 self
->a_text_normal
=
171 RrAppearanceCopy(ob_rr_theme
->a_menu_text_normal
);
172 self
->a_text_selected
=
173 RrAppearanceCopy(ob_rr_theme
->a_menu_text_selected
);
174 self
->a_text_disabled
=
175 RrAppearanceCopy(ob_rr_theme
->a_menu_text_disabled
);
176 self
->a_text_disabled_selected
=
177 RrAppearanceCopy(ob_rr_theme
->a_menu_text_disabled_selected
);
179 RrAppearanceCopy(ob_rr_theme
->a_menu_text_title
);
184 static void menu_entry_frame_free(ObMenuEntryFrame
*self
)
187 menu_entry_unref(self
->entry
);
189 XDestroyWindow(ob_display
, self
->text
);
190 XDestroyWindow(ob_display
, self
->window
);
191 g_hash_table_remove(menu_frame_map
, &self
->text
);
192 g_hash_table_remove(menu_frame_map
, &self
->window
);
193 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
) {
194 XDestroyWindow(ob_display
, self
->icon
);
195 g_hash_table_remove(menu_frame_map
, &self
->icon
);
197 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
198 XDestroyWindow(ob_display
, self
->bullet
);
199 g_hash_table_remove(menu_frame_map
, &self
->bullet
);
202 RrAppearanceFree(self
->a_normal
);
203 RrAppearanceFree(self
->a_selected
);
204 RrAppearanceFree(self
->a_disabled
);
205 RrAppearanceFree(self
->a_disabled_selected
);
207 RrAppearanceFree(self
->a_separator
);
208 RrAppearanceFree(self
->a_icon
);
209 RrAppearanceFree(self
->a_mask
);
210 RrAppearanceFree(self
->a_text_normal
);
211 RrAppearanceFree(self
->a_text_selected
);
212 RrAppearanceFree(self
->a_text_disabled
);
213 RrAppearanceFree(self
->a_text_disabled_selected
);
214 RrAppearanceFree(self
->a_text_title
);
215 RrAppearanceFree(self
->a_bullet_normal
);
216 RrAppearanceFree(self
->a_bullet_selected
);
222 void menu_frame_move(ObMenuFrame
*self
, gint x
, gint y
)
224 RECT_SET_POINT(self
->area
, x
, y
);
225 XMoveWindow(ob_display
, self
->window
, self
->area
.x
, self
->area
.y
);
228 static void menu_frame_place_topmenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
232 if (config_menu_middle
) {
236 *y
-= self
->area
.height
/ 2;
238 /* try to the right of the cursor */
239 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
240 self
->direction_right
= TRUE
;
242 /* try to the left of the cursor */
243 myx
= *x
- self
->area
.width
;
244 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
245 self
->direction_right
= FALSE
;
248 /* if didnt fit on either side so just use what it says */
250 menu_frame_move_on_screen(self
, myx
, *y
, &dx
, &dy
);
251 self
->direction_right
= TRUE
;
261 /* try to the bottom right of the cursor */
262 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
263 self
->direction_right
= TRUE
;
264 if (dx
!= 0 || dy
!= 0) {
265 /* try to the bottom left of the cursor */
266 myx
= *x
- self
->area
.width
;
268 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
269 self
->direction_right
= FALSE
;
271 if (dx
!= 0 || dy
!= 0) {
272 /* try to the top right of the cursor */
274 myy
= *y
- self
->area
.height
;
275 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
276 self
->direction_right
= TRUE
;
278 if (dx
!= 0 || dy
!= 0) {
279 /* try to the top left of the cursor */
280 myx
= *x
- self
->area
.width
;
281 myy
= *y
- self
->area
.height
;
282 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
283 self
->direction_right
= FALSE
;
285 if (dx
!= 0 || dy
!= 0) {
286 /* if didnt fit on either side so just use what it says */
289 menu_frame_move_on_screen(self
, myx
, myy
, &dx
, &dy
);
290 self
->direction_right
= TRUE
;
297 static void menu_frame_place_submenu(ObMenuFrame
*self
, gint
*x
, gint
*y
)
302 overlap
= ob_rr_theme
->menu_overlap
;
303 bwidth
= ob_rr_theme
->mbwidth
;
305 if (self
->direction_right
)
306 *x
= self
->parent
->area
.x
+ self
->parent
->area
.width
-
309 *x
= self
->parent
->area
.x
- self
->area
.width
+ overlap
+ bwidth
;
311 *y
= self
->parent
->area
.y
+ self
->parent_entry
->area
.y
;
312 if (config_menu_middle
)
313 *y
-= (self
->area
.height
- (bwidth
* 2) - ITEM_HEIGHT
) / 2;
318 void menu_frame_move_on_screen(ObMenuFrame
*self
, gint x
, gint y
,
326 a
= screen_physical_area_monitor(self
->monitor
);
328 half
= g_list_length(self
->entries
) / 2;
329 pos
= g_list_index(self
->entries
, self
->selected
);
331 /* if in the bottom half then check this stuff first, will keep the bottom
332 edge of the menu visible */
334 *dx
= MAX(*dx
, a
->x
- x
);
335 *dy
= MAX(*dy
, a
->y
- y
);
337 *dx
= MIN(*dx
, (a
->x
+ a
->width
) - (x
+ self
->area
.width
));
338 *dy
= MIN(*dy
, (a
->y
+ a
->height
) - (y
+ self
->area
.height
));
339 /* if in the top half then check this stuff last, will keep the top
340 edge of the menu visible */
342 *dx
= MAX(*dx
, a
->x
- x
);
343 *dy
= MAX(*dy
, a
->y
- y
);
347 static void menu_entry_frame_render(ObMenuEntryFrame
*self
)
349 RrAppearance
*item_a
, *text_a
;
352 ObMenuFrame
*frame
= self
->frame
;
354 switch (self
->entry
->type
) {
355 case OB_MENU_ENTRY_TYPE_NORMAL
:
356 case OB_MENU_ENTRY_TYPE_SUBMENU
:
357 item_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
358 !self
->entry
->data
.normal
.enabled
?
360 (self
== self
->frame
->selected
?
361 self
->a_disabled_selected
: self
->a_disabled
) :
363 (self
== self
->frame
->selected
?
364 self
->a_selected
: self
->a_normal
));
367 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
368 if (self
->entry
->data
.separator
.label
) {
369 item_a
= self
->frame
->a_title
;
370 th
= ob_rr_theme
->menu_title_height
;
372 item_a
= self
->a_normal
;
373 th
= SEPARATOR_HEIGHT
+ 2*PADDING
;
377 g_assert_not_reached();
379 RECT_SET_SIZE(self
->area
, self
->frame
->inner_w
, th
);
380 XResizeWindow(ob_display
, self
->window
,
381 self
->area
.width
, self
->area
.height
);
382 item_a
->surface
.parent
= self
->frame
->a_items
;
383 item_a
->surface
.parentx
= self
->area
.x
;
384 item_a
->surface
.parenty
= self
->area
.y
;
385 RrPaint(item_a
, self
->window
, self
->area
.width
, self
->area
.height
);
387 switch (self
->entry
->type
) {
388 case OB_MENU_ENTRY_TYPE_NORMAL
:
389 text_a
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
390 !self
->entry
->data
.normal
.enabled
?
392 (self
== self
->frame
->selected
?
393 self
->a_text_disabled_selected
: self
->a_text_disabled
) :
395 (self
== self
->frame
->selected
?
396 self
->a_text_selected
: self
->a_text_normal
));
397 text_a
->texture
[0].data
.text
.string
= self
->entry
->data
.normal
.label
;
398 if (self
->entry
->data
.normal
.shortcut
&&
399 (self
->frame
->menu
->show_all_shortcuts
||
400 self
->entry
->data
.normal
.shortcut_position
> 0))
402 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
403 text_a
->texture
[0].data
.text
.shortcut_pos
=
404 self
->entry
->data
.normal
.shortcut_position
;
406 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
408 case OB_MENU_ENTRY_TYPE_SUBMENU
:
409 text_a
= (self
== self
->frame
->selected
?
410 self
->a_text_selected
:
411 self
->a_text_normal
);
412 sub
= self
->entry
->data
.submenu
.submenu
;
413 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
414 if (sub
->shortcut
&& (self
->frame
->menu
->show_all_shortcuts
||
415 sub
->shortcut_position
> 0))
417 text_a
->texture
[0].data
.text
.shortcut
= TRUE
;
418 text_a
->texture
[0].data
.text
.shortcut_pos
= sub
->shortcut_position
;
420 text_a
->texture
[0].data
.text
.shortcut
= FALSE
;
422 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
423 if (self
->entry
->data
.separator
.label
!= NULL
)
424 text_a
= self
->a_text_title
;
426 text_a
= self
->a_text_normal
;
430 switch (self
->entry
->type
) {
431 case OB_MENU_ENTRY_TYPE_NORMAL
:
432 XMoveResizeWindow(ob_display
, self
->text
,
433 self
->frame
->text_x
, PADDING
,
435 ITEM_HEIGHT
- 2*PADDING
);
436 text_a
->surface
.parent
= item_a
;
437 text_a
->surface
.parentx
= self
->frame
->text_x
;
438 text_a
->surface
.parenty
= PADDING
;
439 RrPaint(text_a
, self
->text
, self
->frame
->text_w
,
440 ITEM_HEIGHT
- 2*PADDING
);
442 case OB_MENU_ENTRY_TYPE_SUBMENU
:
443 XMoveResizeWindow(ob_display
, self
->text
,
444 self
->frame
->text_x
, PADDING
,
445 self
->frame
->text_w
- ITEM_HEIGHT
,
446 ITEM_HEIGHT
- 2*PADDING
);
447 text_a
->surface
.parent
= item_a
;
448 text_a
->surface
.parentx
= self
->frame
->text_x
;
449 text_a
->surface
.parenty
= PADDING
;
450 RrPaint(text_a
, self
->text
, self
->frame
->text_w
- ITEM_HEIGHT
,
451 ITEM_HEIGHT
- 2*PADDING
);
453 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
454 if (self
->entry
->data
.separator
.label
!= NULL
) {
455 /* labeled separator */
456 XMoveResizeWindow(ob_display
, self
->text
,
457 ob_rr_theme
->paddingx
, ob_rr_theme
->paddingy
,
458 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
459 ob_rr_theme
->menu_title_height
-
460 2*ob_rr_theme
->paddingy
);
461 text_a
->surface
.parent
= item_a
;
462 text_a
->surface
.parentx
= ob_rr_theme
->paddingx
;
463 text_a
->surface
.parenty
= ob_rr_theme
->paddingy
;
464 RrPaint(text_a
, self
->text
,
465 self
->area
.width
- 2*ob_rr_theme
->paddingx
,
466 ob_rr_theme
->menu_title_height
-
467 2*ob_rr_theme
->paddingy
);
469 /* unlabeled separaator */
470 XMoveResizeWindow(ob_display
, self
->text
, PADDING
, PADDING
,
471 self
->area
.width
- 2*PADDING
, SEPARATOR_HEIGHT
);
472 self
->a_separator
->surface
.parent
= item_a
;
473 self
->a_separator
->surface
.parentx
= PADDING
;
474 self
->a_separator
->surface
.parenty
= PADDING
;
475 self
->a_separator
->texture
[0].data
.lineart
.color
=
476 text_a
->texture
[0].data
.text
.color
;
477 self
->a_separator
->texture
[0].data
.lineart
.x1
= 2*PADDING
;
478 self
->a_separator
->texture
[0].data
.lineart
.y1
= SEPARATOR_HEIGHT
/2;
479 self
->a_separator
->texture
[0].data
.lineart
.x2
=
480 self
->area
.width
- 4*PADDING
;
481 self
->a_separator
->texture
[0].data
.lineart
.y2
= SEPARATOR_HEIGHT
/2;
482 RrPaint(self
->a_separator
, self
->text
,
483 self
->area
.width
- 2*PADDING
, SEPARATOR_HEIGHT
);
488 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
489 self
->entry
->data
.normal
.icon_data
)
491 XMoveResizeWindow(ob_display
, self
->icon
,
492 PADDING
, frame
->item_margin
.top
,
493 ITEM_HEIGHT
- frame
->item_margin
.top
494 - frame
->item_margin
.bottom
,
495 ITEM_HEIGHT
- frame
->item_margin
.top
496 - frame
->item_margin
.bottom
);
497 self
->a_icon
->texture
[0].data
.rgba
.width
=
498 self
->entry
->data
.normal
.icon_width
;
499 self
->a_icon
->texture
[0].data
.rgba
.height
=
500 self
->entry
->data
.normal
.icon_height
;
501 self
->a_icon
->texture
[0].data
.rgba
.data
=
502 self
->entry
->data
.normal
.icon_data
;
503 self
->a_icon
->surface
.parent
= item_a
;
504 self
->a_icon
->surface
.parentx
= PADDING
;
505 self
->a_icon
->surface
.parenty
= frame
->item_margin
.top
;
506 RrPaint(self
->a_icon
, self
->icon
,
507 ITEM_HEIGHT
- frame
->item_margin
.top
508 - frame
->item_margin
.bottom
,
509 ITEM_HEIGHT
- frame
->item_margin
.top
510 - frame
->item_margin
.bottom
);
511 XMapWindow(ob_display
, self
->icon
);
512 } else if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
513 self
->entry
->data
.normal
.mask
)
517 XMoveResizeWindow(ob_display
, self
->icon
,
518 PADDING
, frame
->item_margin
.top
,
519 ITEM_HEIGHT
- frame
->item_margin
.top
520 - frame
->item_margin
.bottom
,
521 ITEM_HEIGHT
- frame
->item_margin
.top
522 - frame
->item_margin
.bottom
);
523 self
->a_mask
->texture
[0].data
.mask
.mask
=
524 self
->entry
->data
.normal
.mask
;
526 c
= (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
527 !self
->entry
->data
.normal
.enabled
?
529 (self
== self
->frame
->selected
?
530 self
->entry
->data
.normal
.mask_disabled_selected_color
:
531 self
->entry
->data
.normal
.mask_disabled_color
) :
533 (self
== self
->frame
->selected
?
534 self
->entry
->data
.normal
.mask_selected_color
:
535 self
->entry
->data
.normal
.mask_normal_color
));
536 self
->a_mask
->texture
[0].data
.mask
.color
= c
;
538 self
->a_mask
->surface
.parent
= item_a
;
539 self
->a_mask
->surface
.parentx
= PADDING
;
540 self
->a_mask
->surface
.parenty
= frame
->item_margin
.top
;
541 RrPaint(self
->a_mask
, self
->icon
,
542 ITEM_HEIGHT
- frame
->item_margin
.top
543 - frame
->item_margin
.bottom
,
544 ITEM_HEIGHT
- frame
->item_margin
.top
545 - frame
->item_margin
.bottom
);
546 XMapWindow(ob_display
, self
->icon
);
548 XUnmapWindow(ob_display
, self
->icon
);
550 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
551 RrAppearance
*bullet_a
;
552 XMoveResizeWindow(ob_display
, self
->bullet
,
553 self
->frame
->text_x
+ self
->frame
->text_w
-
554 ITEM_HEIGHT
+ PADDING
, PADDING
,
555 ITEM_HEIGHT
- 2*PADDING
,
556 ITEM_HEIGHT
- 2*PADDING
);
557 bullet_a
= (self
== self
->frame
->selected
?
558 self
->a_bullet_selected
:
559 self
->a_bullet_normal
);
560 bullet_a
->surface
.parent
= item_a
;
561 bullet_a
->surface
.parentx
=
562 self
->frame
->text_x
+ self
->frame
->text_w
- ITEM_HEIGHT
+ PADDING
;
563 bullet_a
->surface
.parenty
= PADDING
;
564 RrPaint(bullet_a
, self
->bullet
,
565 ITEM_HEIGHT
- 2*PADDING
,
566 ITEM_HEIGHT
- 2*PADDING
);
567 XMapWindow(ob_display
, self
->bullet
);
569 XUnmapWindow(ob_display
, self
->bullet
);
574 /*! this code is taken from the menu_frame_render. if that changes, this won't
576 static gint
menu_entry_frame_get_height(ObMenuEntryFrame
*self
,
577 gboolean first_entry
,
586 t
= self
->entry
->type
;
588 /* this is the More... entry, it's NORMAL type */
589 t
= OB_MENU_ENTRY_TYPE_NORMAL
;
592 case OB_MENU_ENTRY_TYPE_NORMAL
:
593 case OB_MENU_ENTRY_TYPE_SUBMENU
:
594 h
+= ob_rr_theme
->menu_font_height
;
596 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
597 if (self
->entry
->data
.separator
.label
!= NULL
) {
598 h
+= ob_rr_theme
->menu_title_height
+
599 (ob_rr_theme
->mbwidth
- PADDING
) * 2;
601 /* if the first entry is a labeled separator, then make its border
602 overlap with the menu's outside border */
604 h
-= ob_rr_theme
->mbwidth
;
605 /* if the last entry is a labeled separator, then make its border
606 overlap with the menu's outside border */
608 h
-= ob_rr_theme
->mbwidth
;
610 h
+= SEPARATOR_HEIGHT
;
618 void menu_frame_render(ObMenuFrame
*self
)
621 gint tw
, th
; /* temps */
623 gboolean has_icon
= FALSE
;
627 /* find text dimensions */
629 STRUT_SET(self
->item_margin
, 0, 0, 0, 0);
632 ObMenuEntryFrame
*e
= self
->entries
->data
;
635 e
->a_text_normal
->texture
[0].data
.text
.string
= "";
636 tw
= RrMinWidth(e
->a_text_normal
);
641 RrMargins(e
->a_normal
, &l
, &t
, &r
, &b
);
642 STRUT_SET(self
->item_margin
,
643 MAX(self
->item_margin
.left
, l
),
644 MAX(self
->item_margin
.top
, t
),
645 MAX(self
->item_margin
.right
, r
),
646 MAX(self
->item_margin
.bottom
, b
));
647 RrMargins(e
->a_selected
, &l
, &t
, &r
, &b
);
648 STRUT_SET(self
->item_margin
,
649 MAX(self
->item_margin
.left
, l
),
650 MAX(self
->item_margin
.top
, t
),
651 MAX(self
->item_margin
.right
, r
),
652 MAX(self
->item_margin
.bottom
, b
));
653 RrMargins(e
->a_disabled
, &l
, &t
, &r
, &b
);
654 STRUT_SET(self
->item_margin
,
655 MAX(self
->item_margin
.left
, l
),
656 MAX(self
->item_margin
.top
, t
),
657 MAX(self
->item_margin
.right
, r
),
658 MAX(self
->item_margin
.bottom
, b
));
659 RrMargins(e
->a_disabled_selected
, &l
, &t
, &r
, &b
);
660 STRUT_SET(self
->item_margin
,
661 MAX(self
->item_margin
.left
, l
),
662 MAX(self
->item_margin
.top
, t
),
663 MAX(self
->item_margin
.right
, r
),
664 MAX(self
->item_margin
.bottom
, b
));
667 /* render the entries */
669 for (it
= self
->entries
; it
; it
= g_list_next(it
)) {
670 RrAppearance
*text_a
;
673 /* if the first entry is a labeled separator, then make its border
674 overlap with the menu's outside border */
675 if (it
== self
->entries
&&
676 e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
677 e
->entry
->data
.separator
.label
)
679 h
-= ob_rr_theme
->mbwidth
;
682 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
683 e
->entry
->data
.separator
.label
)
685 e
->border
= ob_rr_theme
->mbwidth
;
688 RECT_SET_POINT(e
->area
, 0, h
+e
->border
);
689 XMoveWindow(ob_display
, e
->window
, e
->area
.x
-e
->border
, e
->area
.y
-e
->border
);
690 XSetWindowBorderWidth(ob_display
, e
->window
, e
->border
);
691 XSetWindowBorder(ob_display
, e
->window
,
692 RrColorPixel(ob_rr_theme
->menu_b_color
));
695 text_a
= (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
696 !e
->entry
->data
.normal
.enabled
?
698 (e
== self
->selected
?
699 e
->a_text_disabled_selected
: e
->a_text_disabled
) :
701 (e
== self
->selected
?
702 e
->a_text_selected
: e
->a_text_normal
));
703 switch (e
->entry
->type
) {
704 case OB_MENU_ENTRY_TYPE_NORMAL
:
705 text_a
->texture
[0].data
.text
.string
= e
->entry
->data
.normal
.label
;
706 tw
= RrMinWidth(text_a
);
707 tw
= MIN(tw
, MAX_MENU_WIDTH
);
708 th
= ob_rr_theme
->menu_font_height
;
710 if (e
->entry
->data
.normal
.icon_data
||
711 e
->entry
->data
.normal
.mask
)
714 case OB_MENU_ENTRY_TYPE_SUBMENU
:
715 sub
= e
->entry
->data
.submenu
.submenu
;
716 text_a
->texture
[0].data
.text
.string
= sub
? sub
->title
: "";
717 tw
= RrMinWidth(text_a
);
718 tw
= MIN(tw
, MAX_MENU_WIDTH
);
719 th
= ob_rr_theme
->menu_font_height
;
721 if (e
->entry
->data
.normal
.icon_data
||
722 e
->entry
->data
.normal
.mask
)
725 tw
+= ITEM_HEIGHT
- PADDING
;
727 case OB_MENU_ENTRY_TYPE_SEPARATOR
:
728 if (e
->entry
->data
.separator
.label
!= NULL
) {
729 e
->a_text_title
->texture
[0].data
.text
.string
=
730 e
->entry
->data
.separator
.label
;
731 tw
= RrMinWidth(text_a
);
732 tw
= MIN(tw
, MAX_MENU_WIDTH
);
733 th
= ob_rr_theme
->menu_title_height
+
734 (ob_rr_theme
->mbwidth
- PADDING
) *2;
737 th
= SEPARATOR_HEIGHT
;
747 /* if the last entry is a labeled separator, then make its border
748 overlap with the menu's outside border */
749 it
= g_list_last(self
->entries
);
750 e
= it
? it
->data
: NULL
;
751 if (e
&& e
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
&&
752 e
->entry
->data
.separator
.label
)
754 h
-= ob_rr_theme
->mbwidth
;
757 self
->text_x
= PADDING
;
762 w
+= ITEM_HEIGHT
+ PADDING
;
763 self
->text_x
+= ITEM_HEIGHT
+ PADDING
;
770 XResizeWindow(ob_display
, self
->window
, w
, h
);
774 RrPaint(self
->a_items
, self
->window
, w
, h
);
776 for (it
= self
->entries
; it
; it
= g_list_next(it
))
777 menu_entry_frame_render(it
->data
);
779 w
+= ob_rr_theme
->mbwidth
* 2;
780 h
+= ob_rr_theme
->mbwidth
* 2;
782 RECT_SET_SIZE(self
->area
, w
, h
);
787 static void menu_frame_update(ObMenuFrame
*self
)
793 menu_pipe_execute(self
->menu
);
794 menu_find_submenus(self
->menu
);
796 self
->selected
= NULL
;
798 /* start at show_from */
799 mit
= g_list_nth(self
->menu
->entries
, self
->show_from
);
801 /* go through the menu's and frame's entries and connect the frame entries
802 to the menu entries */
803 for (fit
= self
->entries
; mit
&& fit
;
804 mit
= g_list_next(mit
), fit
= g_list_next(fit
))
806 ObMenuEntryFrame
*f
= fit
->data
;
807 f
->entry
= mit
->data
;
810 /* if there are more menu entries than in the frame, add them */
812 ObMenuEntryFrame
*e
= menu_entry_frame_new(mit
->data
, self
);
813 self
->entries
= g_list_append(self
->entries
, e
);
814 mit
= g_list_next(mit
);
817 /* if there are more frame entries than menu entries then get rid of
820 GList
*n
= g_list_next(fit
);
821 menu_entry_frame_free(fit
->data
);
822 self
->entries
= g_list_delete_link(self
->entries
, fit
);
826 /* * make the menu fit on the screen */
828 /* calculate the height of the menu */
830 for (fit
= self
->entries
; fit
; fit
= g_list_next(fit
))
831 h
+= menu_entry_frame_get_height(fit
->data
,
832 fit
== self
->entries
,
833 g_list_next(fit
) == NULL
);
834 /* add the border at the top and bottom */
835 h
+= ob_rr_theme
->mbwidth
* 2;
837 a
= screen_physical_area_monitor(self
->monitor
);
841 gboolean last_entry
= TRUE
;
843 /* take the height of our More... entry into account */
844 h
+= menu_entry_frame_get_height(NULL
, FALSE
, TRUE
);
846 /* start at the end of the entries */
847 flast
= g_list_last(self
->entries
);
849 /* pull out all the entries from the frame that don't
850 fit on the screen, leaving at least 1 though */
851 while (h
> a
->height
&& g_list_previous(flast
) != NULL
) {
852 /* update the height, without this entry */
853 h
-= menu_entry_frame_get_height(flast
->data
, FALSE
, last_entry
);
855 /* destroy the entry we're not displaying */
857 flast
= g_list_previous(flast
);
858 menu_entry_frame_free(tmp
->data
);
859 self
->entries
= g_list_delete_link(self
->entries
, tmp
);
861 /* only the first one that we see is the last entry in the menu */
866 ObMenuEntry
*more_entry
;
867 ObMenuEntryFrame
*more_frame
;
868 /* make the More... menu entry frame which will display in this
870 if self->menu->more_menu is NULL that means that this is already
871 More... menu, so just use ourself.
873 more_entry
= menu_get_more((self
->menu
->more_menu
?
874 self
->menu
->more_menu
:
876 /* continue where we left off */
878 g_list_length(self
->entries
));
879 more_frame
= menu_entry_frame_new(more_entry
, self
);
880 /* make it get deleted when the menu frame goes away */
881 menu_entry_unref(more_entry
);
883 /* add our More... entry to the frame */
884 self
->entries
= g_list_append(self
->entries
, more_frame
);
888 menu_frame_render(self
);
891 static gboolean
menu_frame_is_visible(ObMenuFrame
*self
)
893 return !!(g_list_find(menu_frame_visible
, self
));
896 static gboolean
menu_frame_show(ObMenuFrame
*self
)
900 /* determine if the underlying menu is already visible */
901 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
902 ObMenuFrame
*f
= it
->data
;
903 if (f
->menu
== self
->menu
)
907 if (self
->menu
->update_func
)
908 if (!self
->menu
->update_func(self
, self
->menu
->data
))
912 if (menu_frame_visible
== NULL
) {
913 /* no menus shown yet */
914 if (!grab_pointer(TRUE
, TRUE
, OB_CURSOR_POINTER
))
916 if (!grab_keyboard(TRUE
)) {
917 grab_pointer(FALSE
, TRUE
, OB_CURSOR_POINTER
);
922 menu_frame_update(self
);
924 menu_frame_visible
= g_list_prepend(menu_frame_visible
, self
);
926 if (self
->menu
->show_func
)
927 self
->menu
->show_func(self
, self
->menu
->data
);
932 gboolean
menu_frame_show_topmenu(ObMenuFrame
*self
, gint x
, gint y
,
937 if (menu_frame_is_visible(self
))
939 if (!menu_frame_show(self
))
942 /* find the monitor the menu is on */
943 for (i
= 0; i
< screen_num_monitors
; ++i
) {
944 Rect
*a
= screen_physical_area_monitor(i
);
945 if (RECT_CONTAINS(*a
, x
, y
)) {
951 if (self
->menu
->place_func
)
952 self
->menu
->place_func(self
, &x
, &y
, button
, self
->menu
->data
);
954 menu_frame_place_topmenu(self
, &x
, &y
);
956 menu_frame_move(self
, x
, y
);
958 XMapWindow(ob_display
, self
->window
);
963 gboolean
menu_frame_show_submenu(ObMenuFrame
*self
, ObMenuFrame
*parent
,
964 ObMenuEntryFrame
*parent_entry
)
969 if (menu_frame_is_visible(self
))
972 self
->monitor
= parent
->monitor
;
973 self
->parent
= parent
;
974 self
->parent_entry
= parent_entry
;
976 /* set up parent's child to be us */
978 menu_frame_hide(parent
->child
);
979 parent
->child
= self
;
981 if (!menu_frame_show(self
))
984 menu_frame_place_submenu(self
, &x
, &y
);
985 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
988 /*try the other side */
989 self
->direction_right
= !self
->direction_right
;
990 menu_frame_place_submenu(self
, &x
, &y
);
991 menu_frame_move_on_screen(self
, x
, y
, &dx
, &dy
);
993 menu_frame_move(self
, x
+ dx
, y
+ dy
);
995 XMapWindow(ob_display
, self
->window
);
997 if (screen_pointer_pos(&dx
, &dy
) && (e
= menu_entry_frame_under(dx
, dy
)) &&
1004 void menu_frame_hide(ObMenuFrame
*self
)
1006 GList
*it
= g_list_find(menu_frame_visible
, self
);
1011 if (self
->menu
->hide_func
)
1012 self
->menu
->hide_func(self
, self
->menu
->data
);
1015 menu_frame_hide(self
->child
);
1018 self
->parent
->child
= NULL
;
1019 self
->parent
= NULL
;
1020 self
->parent_entry
= NULL
;
1022 menu_frame_visible
= g_list_delete_link(menu_frame_visible
, it
);
1024 if (menu_frame_visible
== NULL
) {
1025 /* last menu shown */
1026 grab_pointer(FALSE
, TRUE
, OB_CURSOR_NONE
);
1027 grab_keyboard(FALSE
);
1030 XUnmapWindow(ob_display
, self
->window
);
1032 menu_frame_free(self
);
1035 void menu_frame_hide_all()
1039 if (config_submenu_show_delay
) {
1040 /* remove any submenu open requests */
1041 ob_main_loop_timeout_remove(ob_main_loop
,
1042 menu_entry_frame_submenu_timeout
);
1044 if ((it
= g_list_last(menu_frame_visible
)))
1045 menu_frame_hide(it
->data
);
1048 void menu_frame_hide_all_client(ObClient
*client
)
1050 GList
*it
= g_list_last(menu_frame_visible
);
1052 ObMenuFrame
*f
= it
->data
;
1053 if (f
->client
== client
)
1059 ObMenuFrame
* menu_frame_under(gint x
, gint y
)
1061 ObMenuFrame
*ret
= NULL
;
1064 for (it
= menu_frame_visible
; it
; it
= g_list_next(it
)) {
1065 ObMenuFrame
*f
= it
->data
;
1067 if (RECT_CONTAINS(f
->area
, x
, y
)) {
1075 ObMenuEntryFrame
* menu_entry_frame_under(gint x
, gint y
)
1078 ObMenuEntryFrame
*ret
= NULL
;
1081 if ((frame
= menu_frame_under(x
, y
))) {
1082 x
-= ob_rr_theme
->mbwidth
+ frame
->area
.x
;
1083 y
-= ob_rr_theme
->mbwidth
+ frame
->area
.y
;
1085 for (it
= frame
->entries
; it
; it
= g_list_next(it
)) {
1086 ObMenuEntryFrame
*e
= it
->data
;
1088 if (RECT_CONTAINS(e
->area
, x
, y
)) {
1097 static gboolean
menu_entry_frame_submenu_timeout(gpointer data
)
1099 menu_entry_frame_show_submenu((ObMenuEntryFrame
*)data
);
1103 void menu_frame_select(ObMenuFrame
*self
, ObMenuEntryFrame
*entry
,
1106 ObMenuEntryFrame
*old
= self
->selected
;
1107 ObMenuFrame
*oldchild
= self
->child
;
1109 if (entry
&& entry
->entry
->type
== OB_MENU_ENTRY_TYPE_SEPARATOR
)
1112 if (old
== entry
) return;
1114 if (config_submenu_show_delay
) {
1115 /* remove any submenu open requests */
1116 ob_main_loop_timeout_remove(ob_main_loop
,
1117 menu_entry_frame_submenu_timeout
);
1120 self
->selected
= entry
;
1123 menu_entry_frame_render(old
);
1125 menu_frame_hide(oldchild
);
1127 if (self
->selected
) {
1128 menu_entry_frame_render(self
->selected
);
1130 if (self
->selected
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
) {
1131 if (config_submenu_show_delay
&& !immediate
) {
1132 /* initiate a new submenu open request */
1133 ob_main_loop_timeout_add(ob_main_loop
,
1134 config_submenu_show_delay
* 1000,
1135 menu_entry_frame_submenu_timeout
,
1136 self
->selected
, g_direct_equal
,
1139 menu_entry_frame_show_submenu(self
->selected
);
1145 void menu_entry_frame_show_submenu(ObMenuEntryFrame
*self
)
1149 if (!self
->entry
->data
.submenu
.submenu
) return;
1151 f
= menu_frame_new(self
->entry
->data
.submenu
.submenu
,
1152 self
->entry
->data
.submenu
.show_from
,
1153 self
->frame
->client
);
1154 /* pass our direction on to our child */
1155 f
->direction_right
= self
->frame
->direction_right
;
1157 menu_frame_show_submenu(f
, self
->frame
, self
);
1160 void menu_entry_frame_execute(ObMenuEntryFrame
*self
, guint state
, Time time
)
1162 if (self
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
&&
1163 self
->entry
->data
.normal
.enabled
)
1165 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1167 ObMenuEntry
*entry
= self
->entry
;
1168 ObMenuExecuteFunc func
= self
->frame
->menu
->execute_func
;
1169 gpointer data
= self
->frame
->menu
->data
;
1170 GSList
*acts
= self
->entry
->data
.normal
.actions
;
1171 ObClient
*client
= self
->frame
->client
;
1172 ObMenuFrame
*frame
= self
->frame
;
1174 /* release grabs before executing the shit */
1175 if (!(state
& ControlMask
)) {
1176 menu_frame_hide_all();
1181 func(entry
, frame
, client
, state
, data
, time
);
1183 action_run(acts
, client
, state
, time
);
1187 void menu_frame_select_previous(ObMenuFrame
*self
)
1189 GList
*it
= NULL
, *start
;
1191 if (self
->entries
) {
1192 start
= it
= g_list_find(self
->entries
, self
->selected
);
1194 ObMenuEntryFrame
*e
;
1196 it
= it
? g_list_previous(it
) : g_list_last(self
->entries
);
1202 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1204 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1209 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);
1212 void menu_frame_select_next(ObMenuFrame
*self
)
1214 GList
*it
= NULL
, *start
;
1216 if (self
->entries
) {
1217 start
= it
= g_list_find(self
->entries
, self
->selected
);
1219 ObMenuEntryFrame
*e
;
1221 it
= it
? g_list_next(it
) : self
->entries
;
1227 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_SUBMENU
)
1229 if (e
->entry
->type
== OB_MENU_ENTRY_TYPE_NORMAL
)
1234 menu_frame_select(self
, it
? it
->data
: NULL
, TRUE
);