12 #include "parser/parse.h"
14 GHashTable
*menu_hash
= NULL
;
15 GList
*menu_visible
= NULL
;
17 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
19 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
20 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
21 ButtonPressMask | ButtonReleaseMask)
23 static void parse_menu(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
26 g_message("%s", __FUNCTION__
);
27 parse_menu_full(i
, doc
, node
, data
, TRUE
);
31 void parse_menu_full(ObParseInst
*i
, xmlDocPtr doc
, xmlNodePtr node
,
32 gpointer data
, gboolean newmenu
)
37 gchar
*id
= NULL
, *title
= NULL
, *label
= NULL
, *plugin
;
38 ObMenu
*menu
= NULL
, *parent
;
40 if (newmenu
== TRUE
) {
41 if (!parse_attr_string("id", node
, &id
))
43 if (!parse_attr_string("label", node
, &title
))
45 ob_debug("menu label %s\n", title
);
47 if (parse_attr_string("plugin", node
, &plugin
)) {
48 PluginMenuCreateData data
;
54 if (plugin_open_reopen(plugin
, i
))
55 parent
= plugin_create(plugin
, &data
);
58 menu
= menu_new(title
, id
, data
? *((ObMenu
**)data
) : NULL
);
61 *((ObMenu
**)data
) = menu
;
63 menu
= (ObMenu
*)data
;
66 node
= node
->xmlChildrenNode
;
69 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "menu")) {
70 if (parse_attr_string("plugin", node
, &plugin
)) {
71 PluginMenuCreateData data
;
75 if (plugin_open_reopen(plugin
, i
))
76 parent
= plugin_create(plugin
, &data
);
80 parse_menu(i
, doc
, node
, &parent
);
81 menu_add_entry(menu
, menu_entry_new_submenu(parent
->label
,
86 else if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "item")) {
87 if (parse_attr_string("label", node
, &label
)) {
88 if ((nact
= parse_find_node("action", node
->xmlChildrenNode
)))
89 act
= action_parse(doc
, nact
);
93 menu_add_entry(menu
, menu_entry_new(label
, act
));
95 menu_add_entry(menu
, menu_entry_new_separator(label
));
107 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
);
109 void menu_destroy_hash_key(ObMenu
*menu
)
114 void menu_destroy_hash_value(ObMenu
*self
)
118 for (it
= self
->entries
; it
; it
= it
->next
)
119 menu_entry_free(it
->data
);
120 g_list_free(self
->entries
);
125 g_hash_table_remove(window_map
, &self
->title
);
126 g_hash_table_remove(window_map
, &self
->frame
);
127 g_hash_table_remove(window_map
, &self
->items
);
129 stacking_remove(self
);
131 RrAppearanceFree(self
->a_title
);
132 RrAppearanceFree(self
->a_items
);
133 XDestroyWindow(ob_display
, self
->title
);
134 XDestroyWindow(ob_display
, self
->frame
);
135 XDestroyWindow(ob_display
, self
->items
);
140 void menu_entry_free(ObMenuEntry
*self
)
143 action_free(self
->action
);
145 g_hash_table_remove(window_map
, &self
->item
);
147 RrAppearanceFree(self
->a_item
);
148 RrAppearanceFree(self
->a_disabled
);
149 RrAppearanceFree(self
->a_hilite
);
150 RrAppearanceFree(self
->a_submenu
);
151 XDestroyWindow(ob_display
, self
->item
);
152 XDestroyWindow(ob_display
, self
->submenu_pic
);
156 void menu_startup(ObParseInst
*i
)
158 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
159 (GDestroyNotify
)menu_destroy_hash_key
,
160 (GDestroyNotify
)menu_destroy_hash_value
);
165 g_hash_table_destroy(menu_hash
);
174 gboolean loaded
= FALSE
;
178 if (config_menu_path
)
180 parse_load(config_menu_path
, "openbox_menu", &doc
, &node
)))
181 g_warning("Failed to load menu from '%s'", config_menu_path
);
183 p
= g_build_filename(g_get_home_dir(), ".openbox", "menu", NULL
);
185 parse_load(p
, "openbox_menu", &doc
, &node
)))
186 g_warning("Failed to load menu from '%s'", p
);
190 p
= g_build_filename(RCDIR
, "menu", NULL
);
192 parse_load(p
, "openbox_menu", &doc
, &node
)))
193 g_warning("Failed to load menu from '%s'", p
);
198 parse_register(i
, "menu", parse_menu
, NULL
);
199 parse_tree(i
, doc
, node
->xmlChildrenNode
);
205 static Window
createWindow(Window parent
, unsigned long mask
,
206 XSetWindowAttributes
*attrib
)
208 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
209 RrDepth(ob_rr_inst
), InputOutput
,
210 RrVisual(ob_rr_inst
), mask
, attrib
);
214 ObMenu
*menu_new_full(char *label
, char *name
, ObMenu
*parent
,
215 menu_controller_show show
, menu_controller_update update
,
216 menu_controller_selected selected
,
217 menu_controller_hide hide
,
218 menu_controller_mouseover mouseover
)
220 XSetWindowAttributes attrib
;
223 self
= g_new0(ObMenu
, 1);
224 self
->obwin
.type
= Window_Menu
;
225 self
->label
= g_strdup(label
);
226 self
->name
= g_strdup(name
);
227 self
->parent
= parent
;
228 self
->open_submenu
= NULL
;
231 self
->entries
= NULL
;
233 self
->invalid
= TRUE
;
235 /* default controllers */
236 self
->show
= (show
!= NULL
? show
: menu_show_full
);
237 self
->hide
= (hide
!= NULL
? hide
: menu_hide
);
238 self
->update
= (update
!= NULL
? update
: menu_render
);
239 self
->mouseover
= (mouseover
!= NULL
? mouseover
:
240 menu_control_mouseover
);
241 self
->selected
= (selected
!= NULL
? selected
: menu_entry_fire
);
244 self
->plugin_data
= NULL
;
246 attrib
.override_redirect
= TRUE
;
247 attrib
.event_mask
= FRAME_EVENTMASK
;
248 self
->frame
= createWindow(RootWindow(ob_display
, ob_screen
),
249 CWOverrideRedirect
|CWEventMask
, &attrib
);
250 attrib
.event_mask
= TITLE_EVENTMASK
;
251 self
->title
= createWindow(self
->frame
, CWEventMask
, &attrib
);
252 self
->items
= createWindow(self
->frame
, 0, &attrib
);
254 self
->a_title
= self
->a_items
= NULL
;
256 XMapWindow(ob_display
, self
->title
);
257 XMapWindow(ob_display
, self
->items
);
259 g_hash_table_insert(window_map
, &self
->frame
, self
);
260 g_hash_table_insert(window_map
, &self
->title
, self
);
261 g_hash_table_insert(window_map
, &self
->items
, self
);
262 g_hash_table_insert(menu_hash
, g_strdup(name
), self
);
264 stacking_add(MENU_AS_WINDOW(self
));
265 stacking_raise(MENU_AS_WINDOW(self
));
270 void menu_free(char *name
)
272 g_hash_table_remove(menu_hash
, name
);
275 ObMenuEntry
*menu_entry_new_full(char *label
, ObAction
*action
,
276 ObMenuEntryRenderType render_type
,
279 ObMenuEntry
*menu_entry
= g_new0(ObMenuEntry
, 1);
281 menu_entry
->label
= g_strdup(label
);
282 menu_entry
->render_type
= render_type
;
283 menu_entry
->action
= action
;
285 menu_entry
->hilite
= FALSE
;
286 menu_entry
->enabled
= TRUE
;
288 menu_entry
->submenu
= submenu
;
293 void menu_entry_set_submenu(ObMenuEntry
*entry
, ObMenu
*submenu
)
295 g_assert(entry
!= NULL
);
297 entry
->submenu
= submenu
;
299 if(entry
->parent
!= NULL
)
300 entry
->parent
->invalid
= TRUE
;
303 void menu_add_entry(ObMenu
*menu
, ObMenuEntry
*entry
)
305 XSetWindowAttributes attrib
;
307 g_assert(menu
!= NULL
);
308 g_assert(entry
!= NULL
);
309 g_assert(entry
->item
== None
);
311 menu
->entries
= g_list_append(menu
->entries
, entry
);
312 entry
->parent
= menu
;
314 attrib
.event_mask
= ENTRY_EVENTMASK
;
315 entry
->item
= createWindow(menu
->items
, CWEventMask
, &attrib
);
316 entry
->submenu_pic
= createWindow(menu
->items
, CWEventMask
, &attrib
);
317 XMapWindow(ob_display
, entry
->item
);
318 XMapWindow(ob_display
, entry
->submenu_pic
);
320 entry
->a_item
= entry
->a_disabled
= entry
->a_hilite
= entry
->a_submenu
323 menu
->invalid
= TRUE
;
325 g_hash_table_insert(window_map
, &entry
->item
, menu
);
326 g_hash_table_insert(window_map
, &entry
->submenu_pic
, menu
);
329 void menu_show(char *name
, int x
, int y
, ObClient
*client
)
333 self
= g_hash_table_lookup(menu_hash
, name
);
335 g_warning("Attempted to show menu '%s' but it does not exist.",
340 menu_show_full(self
, x
, y
, client
);
343 void menu_show_full(ObMenu
*self
, int x
, int y
, ObClient
*client
)
345 g_assert(self
!= NULL
);
349 self
->client
= client
;
352 if (!(self
->parent
&& self
->parent
->shown
)) {
353 grab_pointer(TRUE
, None
);
356 menu_visible
= g_list_append(menu_visible
, self
);
359 menu_control_show(self
, x
, y
, client
);
362 void menu_hide(ObMenu
*self
) {
364 XUnmapWindow(ob_display
, self
->frame
);
366 if (self
->open_submenu
)
367 self
->open_submenu
->hide(self
->open_submenu
);
368 if (self
->parent
&& self
->parent
->open_submenu
== self
) {
369 self
->parent
->open_submenu
= NULL
;
372 if (!(self
->parent
&& self
->parent
->shown
)) {
373 grab_keyboard(FALSE
);
374 grab_pointer(FALSE
, None
);
376 menu_visible
= g_list_remove(menu_visible
, self
);
378 ((ObMenuEntry
*)self
->over
->data
)->hilite
= FALSE
;
379 menu_entry_render(self
->over
->data
);
385 void menu_clear(ObMenu
*self
) {
388 for (it
= self
->entries
; it
; it
= it
->next
) {
389 ObMenuEntry
*entry
= it
->data
;
390 menu_entry_free(entry
);
392 self
->entries
= NULL
;
393 self
->invalid
= TRUE
;
397 ObMenuEntry
*menu_find_entry(ObMenu
*menu
, Window win
)
401 for (it
= menu
->entries
; it
; it
= it
->next
) {
402 ObMenuEntry
*entry
= it
->data
;
403 if (entry
->item
== win
)
409 ObMenuEntry
*menu_find_entry_by_submenu(ObMenu
*menu
, ObMenu
*submenu
)
413 for (it
= menu
->entries
; it
; it
= it
->next
) {
414 ObMenuEntry
*entry
= it
->data
;
415 if (entry
->submenu
== submenu
)
421 ObMenuEntry
*menu_find_entry_by_pos(ObMenu
*menu
, int x
, int y
)
423 if (x
< 0 || x
>= menu
->size
.width
|| y
< 0 || y
>= menu
->size
.height
)
426 y
-= menu
->title_h
+ ob_rr_theme
->bwidth
;
427 if (y
< 0) return NULL
;
429 ob_debug("%d %p\n", y
/menu
->item_h
,
430 g_list_nth_data(menu
->entries
, y
/ menu
->item_h
));
431 return g_list_nth_data(menu
->entries
, y
/ menu
->item_h
);
434 void menu_entry_fire(ObMenuEntry
*self
, unsigned int button
, unsigned int x
,
439 /* ignore wheel scrolling */
440 if (button
== 4 || button
== 5) return;
443 self
->action
->data
.any
.c
= self
->parent
->client
;
444 self
->action
->func(&self
->action
->data
);
446 /* hide the whole thing */
448 while (m
->parent
) m
= m
->parent
;
454 Default menu controller action for showing.
457 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
)
462 g_assert(!self
->invalid
);
464 for (i
= 0; i
< screen_num_monitors
; ++i
) {
465 a
= screen_physical_area_monitor(i
);
466 if (RECT_CONTAINS(*a
, x
, y
))
472 POINT_SET(self
->location
,
473 MIN(x
, a
->x
+ a
->width
- 1 - self
->size
.width
),
474 MIN(y
, a
->y
+ a
->height
- 1 - self
->size
.height
));
475 XMoveWindow(ob_display
, self
->frame
, self
->location
.x
, self
->location
.y
);
478 XMapWindow(ob_display
, self
->frame
);
479 stacking_raise(MENU_AS_WINDOW(self
));
481 } else if (self
->shown
&& self
->open_submenu
) {
482 self
->open_submenu
->hide(self
->open_submenu
);
486 void menu_control_mouseover(ObMenuEntry
*self
, gboolean enter
)
492 g_assert(self
!= NULL
);
495 /* TODO: we prolly don't need open_submenu */
496 if (self
->parent
->open_submenu
&& self
->submenu
497 != self
->parent
->open_submenu
)
499 e
= (ObMenuEntry
*) self
->parent
->over
->data
;
501 menu_entry_render(e
);
502 self
->parent
->open_submenu
->hide(self
->parent
->open_submenu
);
505 if (self
->submenu
&& self
->parent
->open_submenu
!= self
->submenu
) {
506 self
->parent
->open_submenu
= self
->submenu
;
508 /* shouldn't be invalid since it must be displayed */
509 g_assert(!self
->parent
->invalid
);
510 /* TODO: I don't understand why these bevels should be here.
511 Something must be wrong in the width calculation */
512 x
= self
->parent
->location
.x
+ self
->parent
->size
.width
+
513 ob_rr_theme
->bwidth
- ob_rr_theme
->menu_overlap
;
515 /* need to get the width. is this bad?*/
516 self
->parent
->update(self
->submenu
);
518 a
= screen_physical_area_monitor(self
->parent
->xin_area
);
520 if (self
->submenu
->size
.width
+ x
>= a
->x
+ a
->width
) {
521 int newparentx
= a
->x
+ a
->width
522 - self
->submenu
->size
.width
523 - self
->parent
->size
.width
524 - ob_rr_theme
->bwidth
525 - ob_rr_theme
->menu_overlap
;
527 x
= a
->x
+ a
->width
- self
->submenu
->size
.width
528 - ob_rr_theme
->menu_overlap
;
529 XWarpPointer(ob_display
, None
, None
, 0, 0, 0, 0,
530 newparentx
- self
->parent
->location
.x
, 0);
532 menu_show_full(self
->parent
, newparentx
,
533 self
->parent
->location
.y
, self
->parent
->client
);
536 menu_show_full(self
->submenu
, x
,
537 self
->parent
->location
.y
+ self
->y
,
538 self
->parent
->client
);
541 self
->parent
->over
= g_list_find(self
->parent
->entries
, self
);
544 self
->hilite
= FALSE
;
546 menu_entry_render(self
);
549 void menu_control_keyboard_nav(unsigned int key
)
551 static ObMenu
*current_menu
= NULL
;
552 ObMenuEntry
*e
= NULL
;
554 ObKey obkey
= OB_NUM_KEYS
;
556 /* hrmm. could be fixed */
557 if (key
== ob_keycode(OB_KEY_DOWN
))
559 else if (key
== ob_keycode(OB_KEY_UP
))
561 else if (key
== ob_keycode(OB_KEY_RIGHT
)) /* fuck */
562 obkey
= OB_KEY_RIGHT
;
563 else if (key
== ob_keycode(OB_KEY_LEFT
)) /* users */
565 else if (key
== ob_keycode(OB_KEY_RETURN
))
566 obkey
= OB_KEY_RETURN
;
569 if (current_menu
== NULL
)
570 current_menu
= menu_visible
->data
;
574 if (current_menu
->over
) {
575 current_menu
->mouseover(current_menu
->over
->data
, FALSE
);
576 current_menu
->over
= (current_menu
->over
->next
!= NULL
?
577 current_menu
->over
->next
:
578 current_menu
->entries
);
581 current_menu
->over
= current_menu
->entries
;
583 if (current_menu
->over
)
584 current_menu
->mouseover(current_menu
->over
->data
, TRUE
);
589 if (current_menu
->over
) {
590 current_menu
->mouseover(current_menu
->over
->data
, FALSE
);
591 current_menu
->over
= (current_menu
->over
->prev
!= NULL
?
592 current_menu
->over
->prev
:
593 g_list_last(current_menu
->entries
));
595 current_menu
->over
= g_list_last(current_menu
->entries
);
597 if (current_menu
->over
)
598 current_menu
->mouseover(current_menu
->over
->data
, TRUE
);
603 if (current_menu
->over
== NULL
)
605 e
= (ObMenuEntry
*)current_menu
->over
->data
;
607 current_menu
->mouseover(e
, TRUE
);
608 current_menu
= e
->submenu
;
609 current_menu
->over
= current_menu
->entries
;
610 if (current_menu
->over
)
611 current_menu
->mouseover(current_menu
->over
->data
, TRUE
);
616 case OB_KEY_RETURN
: {
617 if (current_menu
->over
== NULL
)
619 e
= (ObMenuEntry
*)current_menu
->over
->data
;
621 current_menu
->mouseover(e
, FALSE
);
622 current_menu
->over
= NULL
;
624 menu_entry_fire(e
, 0, 0, 0);
628 if (current_menu
->over
!= NULL
) {
629 current_menu
->mouseover(current_menu
->over
->data
, FALSE
);
630 current_menu
->over
= NULL
;
633 current_menu
->hide(current_menu
);
635 if (current_menu
->parent
)
636 current_menu
= current_menu
->parent
;
641 ((ObMenu
*)menu_visible
->data
)->hide(menu_visible
->data
);
649 /* This noop brought to you by OLS 2003 Email Garden. */