11 GHashTable
*menu_hash
= NULL
;
12 GList
*menu_visible
= NULL
;
14 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
16 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
17 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
18 ButtonPressMask | ButtonReleaseMask)
20 static void parse_menu(xmlDocPtr doc
, xmlNodePtr node
, void *data
)
22 parse_menu_full(doc
, node
, data
, TRUE
);
26 void parse_menu_full(xmlDocPtr doc
, xmlNodePtr node
, void *data
,
32 gchar
*id
= NULL
, *title
= NULL
, *label
= NULL
, *plugin
;
33 ObMenu
*menu
= NULL
, *parent
;
35 if (newmenu
== TRUE
) {
36 if (!parse_attr_string("id", node
, &id
))
38 if (!parse_attr_string("label", node
, &title
))
40 g_message("menu label %s", title
);
42 if (parse_attr_string("plugin", node
, &plugin
)) {
43 PluginMenuCreateData data
;
47 parent
= plugin_create(plugin
, &data
);
50 menu
= menu_new(title
, id
, data
? *((ObMenu
**)data
) : NULL
);
53 *((ObMenu
**)data
) = menu
;
55 menu
= (ObMenu
*)data
;
58 node
= node
->xmlChildrenNode
;
61 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "menu")) {
62 if (parse_attr_string("plugin", node
, &plugin
)) {
63 PluginMenuCreateData data
;
67 parent
= plugin_create(plugin
, &data
);
71 parse_menu(doc
, node
, &parent
);
72 menu_add_entry(menu
, menu_entry_new_submenu(parent
->label
,
77 else if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "item")) {
78 if (parse_attr_string("label", node
, &label
)) {
79 if ((nact
= parse_find_node("action", node
->xmlChildrenNode
)))
80 act
= action_parse(doc
, nact
);
84 menu_add_entry(menu
, menu_entry_new(label
, act
));
86 menu_add_entry(menu
, menu_entry_new_separator(label
));
98 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
);
100 void menu_destroy_hash_key(ObMenu
*menu
)
105 void menu_destroy_hash_value(ObMenu
*self
)
109 for (it
= self
->entries
; it
; it
= it
->next
)
110 menu_entry_free(it
->data
);
111 g_list_free(self
->entries
);
116 g_hash_table_remove(window_map
, &self
->title
);
117 g_hash_table_remove(window_map
, &self
->frame
);
118 g_hash_table_remove(window_map
, &self
->items
);
120 stacking_remove(self
);
122 RrAppearanceFree(self
->a_title
);
123 RrAppearanceFree(self
->a_items
);
124 XDestroyWindow(ob_display
, self
->title
);
125 XDestroyWindow(ob_display
, self
->frame
);
126 XDestroyWindow(ob_display
, self
->items
);
131 void menu_entry_free(ObMenuEntry
*self
)
134 action_free(self
->action
);
136 g_hash_table_remove(window_map
, &self
->item
);
138 RrAppearanceFree(self
->a_item
);
139 RrAppearanceFree(self
->a_disabled
);
140 RrAppearanceFree(self
->a_hilite
);
141 XDestroyWindow(ob_display
, self
->item
);
155 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
156 (GDestroyNotify
)menu_destroy_hash_key
,
157 (GDestroyNotify
)menu_destroy_hash_value
);
159 parse_register("menu", parse_menu
, NULL
);
162 m = menu_new("sex menu", "root", NULL);
164 a = action_from_string("execute");
165 a->data.execute.path = g_strdup("xterm");
166 menu_add_entry(m, menu_entry_new("xterm", a));
167 a = action_from_string("restart");
168 menu_add_entry(m, menu_entry_new("restart", a));
169 menu_add_entry(m, menu_entry_new_separator("--"));
170 a = action_from_string("exit");
171 menu_add_entry(m, menu_entry_new("exit", a));
175 s = menu_new("subsex menu", "submenu", m);
176 a = action_from_string("execute");
177 a->data.execute.path = g_strdup("xclock");
178 menu_add_entry(s, menu_entry_new("xclock", a));
180 menu_add_entry(m, menu_entry_new_submenu("subz", s));
182 s = menu_new("empty", "chub", m);
183 menu_add_entry(m, menu_entry_new_submenu("empty", s));
185 s = menu_new("", "s-club", m);
186 menu_add_entry(m, menu_entry_new_submenu("empty", s));
188 s = menu_new(NULL, "h-club", m);
189 menu_add_entry(m, menu_entry_new_submenu("empty", s));
191 s = menu_new(NULL, "g-club", m);
193 a = action_from_string("execute");
194 a->data.execute.path = g_strdup("xterm");
195 menu_add_entry(s, menu_entry_new("xterm", a));
196 a = action_from_string("restart");
197 menu_add_entry(s, menu_entry_new("restart", a));
198 menu_add_entry(s, menu_entry_new_separator("--"));
199 a = action_from_string("exit");
200 menu_add_entry(s, menu_entry_new("exit", a));
202 menu_add_entry(m, menu_entry_new_submenu("long", s));
208 g_hash_table_destroy(menu_hash
);
211 static Window
createWindow(Window parent
, unsigned long mask
,
212 XSetWindowAttributes
*attrib
)
214 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
215 RrDepth(ob_rr_inst
), InputOutput
,
216 RrVisual(ob_rr_inst
), mask
, attrib
);
220 ObMenu
*menu_new_full(char *label
, char *name
, ObMenu
*parent
,
221 menu_controller_show show
, menu_controller_update update
)
223 XSetWindowAttributes attrib
;
226 self
= g_new0(ObMenu
, 1);
227 self
->obwin
.type
= Window_Menu
;
228 self
->label
= g_strdup(label
);
229 self
->name
= g_strdup(name
);
230 self
->parent
= parent
;
231 self
->open_submenu
= NULL
;
233 self
->entries
= NULL
;
235 self
->invalid
= TRUE
;
237 /* default controllers */
240 self
->update
= update
;
241 self
->mouseover
= NULL
;
242 self
->selected
= NULL
;
245 self
->plugin_data
= NULL
;
247 attrib
.override_redirect
= TRUE
;
248 attrib
.event_mask
= FRAME_EVENTMASK
;
249 self
->frame
= createWindow(RootWindow(ob_display
, ob_screen
),
250 CWOverrideRedirect
|CWEventMask
, &attrib
);
251 attrib
.event_mask
= TITLE_EVENTMASK
;
252 self
->title
= createWindow(self
->frame
, CWEventMask
, &attrib
);
253 self
->items
= createWindow(self
->frame
, 0, &attrib
);
255 self
->a_title
= self
->a_items
= NULL
;
257 XMapWindow(ob_display
, self
->title
);
258 XMapWindow(ob_display
, self
->items
);
260 g_hash_table_insert(window_map
, &self
->frame
, self
);
261 g_hash_table_insert(window_map
, &self
->title
, self
);
262 g_hash_table_insert(window_map
, &self
->items
, self
);
263 g_hash_table_insert(menu_hash
, g_strdup(name
), self
);
265 stacking_add(MENU_AS_WINDOW(self
));
266 stacking_raise(MENU_AS_WINDOW(self
));
271 void menu_free(char *name
)
273 g_hash_table_remove(menu_hash
, name
);
276 ObMenuEntry
*menu_entry_new_full(char *label
, Action
*action
,
277 ObMenuEntryRenderType render_type
,
280 ObMenuEntry
*menu_entry
= g_new0(ObMenuEntry
, 1);
282 menu_entry
->label
= g_strdup(label
);
283 menu_entry
->render_type
= render_type
;
284 menu_entry
->action
= action
;
286 menu_entry
->hilite
= FALSE
;
287 menu_entry
->enabled
= TRUE
;
289 menu_entry
->submenu
= submenu
;
294 void menu_entry_set_submenu(ObMenuEntry
*entry
, ObMenu
*submenu
)
296 g_assert(entry
!= NULL
);
298 entry
->submenu
= submenu
;
300 if(entry
->parent
!= NULL
)
301 entry
->parent
->invalid
= TRUE
;
304 void menu_add_entry(ObMenu
*menu
, ObMenuEntry
*entry
)
306 XSetWindowAttributes attrib
;
308 g_assert(menu
!= NULL
);
309 g_assert(entry
!= NULL
);
310 g_assert(entry
->item
== None
);
312 menu
->entries
= g_list_append(menu
->entries
, entry
);
313 entry
->parent
= menu
;
315 attrib
.event_mask
= ENTRY_EVENTMASK
;
316 entry
->item
= createWindow(menu
->items
, CWEventMask
, &attrib
);
317 XMapWindow(ob_display
, entry
->item
);
319 entry
->a_item
= entry
->a_disabled
= entry
->a_hilite
= NULL
;
321 menu
->invalid
= TRUE
;
323 g_hash_table_insert(window_map
, &entry
->item
, menu
);
326 void menu_show(char *name
, int x
, int y
, ObClient
*client
)
330 self
= g_hash_table_lookup(menu_hash
, name
);
332 g_warning("Attempted to show menu '%s' but it does not exist.",
337 menu_show_full(self
, x
, y
, client
);
340 void menu_show_full(ObMenu
*self
, int x
, int y
, ObClient
*client
)
342 g_assert(self
!= NULL
);
346 self
->client
= client
;
349 if (!(self
->parent
&& self
->parent
->shown
)) {
350 grab_pointer(TRUE
, None
);
353 menu_visible
= g_list_append(menu_visible
, self
);
357 self
->show(self
, x
, y
, client
);
359 menu_control_show(self
, x
, y
, client
);
363 void menu_hide(ObMenu
*self
) {
365 XUnmapWindow(ob_display
, self
->frame
);
367 if (self
->open_submenu
)
368 menu_hide(self
->open_submenu
);
369 if (self
->parent
&& self
->parent
->open_submenu
== self
) {
372 self
->parent
->open_submenu
= NULL
;
374 e
= menu_find_entry_by_submenu(self
->parent
, self
);
375 if (self
->parent
->mouseover
)
376 self
->parent
->mouseover(e
, FALSE
);
378 menu_control_mouseover(e
, FALSE
);
379 menu_entry_render(e
);
382 if (!(self
->parent
&& self
->parent
->shown
)) {
383 grab_keyboard(FALSE
);
384 grab_pointer(FALSE
, None
);
386 menu_visible
= g_list_remove(menu_visible
, self
);
390 void menu_clear(ObMenu
*self
) {
393 for (it
= self
->entries
; it
; it
= it
->next
) {
394 ObMenuEntry
*entry
= it
->data
;
395 menu_entry_free(entry
);
397 self
->entries
= NULL
;
398 self
->invalid
= TRUE
;
402 ObMenuEntry
*menu_find_entry(ObMenu
*menu
, Window win
)
406 for (it
= menu
->entries
; it
; it
= it
->next
) {
407 ObMenuEntry
*entry
= it
->data
;
408 if (entry
->item
== win
)
414 ObMenuEntry
*menu_find_entry_by_submenu(ObMenu
*menu
, ObMenu
*submenu
)
418 for (it
= menu
->entries
; it
; it
= it
->next
) {
419 ObMenuEntry
*entry
= it
->data
;
420 if (entry
->submenu
== submenu
)
426 ObMenuEntry
*menu_find_entry_by_pos(ObMenu
*menu
, int x
, int y
)
428 if (x
< 0 || x
>= menu
->size
.width
|| y
< 0 || y
>= menu
->size
.height
)
431 y
-= menu
->title_h
+ ob_rr_theme
->bwidth
;
432 if (y
< 0) return NULL
;
434 g_message ("%d %p", y
/menu
->item_h
, g_list_nth_data(menu
->entries
, y
/ menu
->item_h
));
435 return g_list_nth_data(menu
->entries
, y
/ menu
->item_h
);
438 void menu_entry_fire(ObMenuEntry
*self
)
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 menu_hide(self
->open_submenu
);
486 void menu_control_mouseover(ObMenuEntry
*self
, gboolean enter
)
493 if (self
->parent
->open_submenu
&& self
->submenu
494 != self
->parent
->open_submenu
)
496 e
= menu_find_entry_by_submenu(self
->parent
,
497 self
->parent
->open_submenu
);
499 menu_entry_render(e
);
500 menu_hide(self
->parent
->open_submenu
);
503 if (self
->submenu
&& self
->parent
->open_submenu
!= self
->submenu
) {
504 self
->parent
->open_submenu
= self
->submenu
;
506 /* shouldn't be invalid since it must be displayed */
507 g_assert(!self
->parent
->invalid
);
508 /* TODO: I don't understand why these bevels should be here.
509 Something must be wrong in the width calculation */
510 x
= self
->parent
->location
.x
+ self
->parent
->size
.width
+
511 ob_rr_theme
->bwidth
- ob_rr_theme
->menu_overlap
;
513 /* need to get the width. is this bad?*/
514 menu_render(self
->submenu
);
516 a
= screen_physical_area_monitor(self
->parent
->xin_area
);
518 if (self
->submenu
->size
.width
+ x
>= a
->x
+ a
->width
)
519 x
= self
->parent
->location
.x
- self
->submenu
->size
.width
-
520 ob_rr_theme
->bwidth
+ ob_rr_theme
->menu_overlap
;
522 menu_show_full(self
->submenu
, x
,
523 self
->parent
->location
.y
+ self
->y
,
524 self
->parent
->client
);
528 if (enter
|| !self
->submenu
||
529 menu_find_entry_by_submenu(self
->parent
,
530 self
->parent
->open_submenu
) != self
)
531 self
->hilite
= enter
;
534 ObMenuEntry
*menu_control_keyboard_nav(ObMenuEntry
*over
, ObKey key
)
541 if (over
->parent
->mouseover
)
542 over
->parent
->mouseover(over
, FALSE
);
544 menu_control_mouseover(over
, FALSE
);
545 menu_entry_render(over
);
547 it
= over
->parent
->entries
;
548 while (it
!= NULL
&& it
->data
!= over
)
553 over
= (ObMenuEntry
*)it
->next
->data
;
554 else if (over
== NULL
) {
555 if (menu_visible
&& ((ObMenu
*)menu_visible
->data
)->entries
)
556 over
= (ObMenuEntry
*)
557 (((ObMenu
*)menu_visible
->data
)->entries
)->data
;
561 over
= (over
->parent
->entries
!= NULL
?
562 over
->parent
->entries
->data
: NULL
);
566 if (over
->parent
->mouseover
)
567 over
->parent
->mouseover(over
, TRUE
);
569 menu_control_mouseover(over
, TRUE
);
570 menu_entry_render(over
);
577 if (over
->parent
->mouseover
)
578 over
->parent
->mouseover(over
, FALSE
);
580 menu_control_mouseover(over
, FALSE
);
581 menu_entry_render(over
);
583 it
= g_list_last(over
->parent
->entries
);
584 while (it
!= NULL
&& it
->data
!= over
)
589 over
= (ObMenuEntry
*)it
->prev
->data
;
590 else if (over
== NULL
) {
591 it
= g_list_last(menu_visible
);
593 it
= g_list_last(((ObMenu
*)it
->data
)->entries
);
594 over
= (ObMenuEntry
*)(it
!= NULL
? it
->data
: NULL
);
597 over
= (over
->parent
->entries
!= NULL
?
598 g_list_last(over
->parent
->entries
)->data
:
601 if (over
->parent
->mouseover
)
602 over
->parent
->mouseover(over
, TRUE
);
604 menu_control_mouseover(over
, TRUE
);
605 menu_entry_render(over
);
608 case OB_KEY_RETURN
: {
613 if (over
->parent
->mouseover
)
614 over
->parent
->mouseover(over
, FALSE
);
616 menu_control_mouseover(over
, FALSE
);
617 menu_entry_render(over
);
619 if (over
->submenu
->entries
)
620 over
= over
->submenu
->entries
->data
;
622 if (over
->parent
->mouseover
)
623 over
->parent
->mouseover(over
, TRUE
);
625 menu_control_mouseover(over
, TRUE
);
626 menu_entry_render(over
);
629 if (over
->parent
->mouseover
)
630 over
->parent
->mouseover(over
, FALSE
);
632 menu_control_mouseover(over
, FALSE
);
633 menu_entry_render(over
);
635 menu_entry_fire(over
);
639 case OB_KEY_ESCAPE
: {
641 if (over
->parent
->mouseover
)
642 over
->parent
->mouseover(over
, FALSE
);
644 menu_control_mouseover(over
, FALSE
);
645 menu_entry_render(over
);
647 menu_hide(over
->parent
);
649 it
= g_list_last(menu_visible
);
651 menu_hide((ObMenu
*)it
->data
);
659 g_error("Unknown key");