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
= {
48 parent
= plugin_create(plugin
, &data
);
51 menu
= menu_new(title
, id
, data
? *((ObMenu
**)data
) : NULL
);
54 *((ObMenu
**)data
) = menu
;
56 menu
= (ObMenu
*)data
;
59 node
= node
->xmlChildrenNode
;
62 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "menu")) {
63 if (parse_attr_string("plugin", node
, &plugin
)) {
64 PluginMenuCreateData data
;
68 parent
= plugin_create(plugin
, &data
);
72 parse_menu(doc
, node
, &parent
);
73 menu_add_entry(menu
, menu_entry_new_submenu(parent
->label
,
78 else if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "item")) {
79 if (parse_attr_string("label", node
, &label
)) {
80 if ((nact
= parse_find_node("action", node
->xmlChildrenNode
)))
81 act
= action_parse(doc
, nact
);
85 menu_add_entry(menu
, menu_entry_new(label
, act
));
87 menu_add_entry(menu
, menu_entry_new_separator(label
));
99 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
);
101 void menu_destroy_hash_key(ObMenu
*menu
)
106 void menu_destroy_hash_value(ObMenu
*self
)
110 for (it
= self
->entries
; it
; it
= it
->next
)
111 menu_entry_free(it
->data
);
112 g_list_free(self
->entries
);
117 g_hash_table_remove(window_map
, &self
->title
);
118 g_hash_table_remove(window_map
, &self
->frame
);
119 g_hash_table_remove(window_map
, &self
->items
);
121 stacking_remove(self
);
123 RrAppearanceFree(self
->a_title
);
124 RrAppearanceFree(self
->a_items
);
125 XDestroyWindow(ob_display
, self
->title
);
126 XDestroyWindow(ob_display
, self
->frame
);
127 XDestroyWindow(ob_display
, self
->items
);
132 void menu_entry_free(ObMenuEntry
*self
)
135 action_free(self
->action
);
137 g_hash_table_remove(window_map
, &self
->item
);
139 RrAppearanceFree(self
->a_item
);
140 RrAppearanceFree(self
->a_disabled
);
141 RrAppearanceFree(self
->a_hilite
);
142 XDestroyWindow(ob_display
, self
->item
);
156 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
157 (GDestroyNotify
)menu_destroy_hash_key
,
158 (GDestroyNotify
)menu_destroy_hash_value
);
160 parse_register("menu", parse_menu
, NULL
);
163 m = menu_new("sex menu", "root", NULL);
165 a = action_from_string("execute");
166 a->data.execute.path = g_strdup("xterm");
167 menu_add_entry(m, menu_entry_new("xterm", a));
168 a = action_from_string("restart");
169 menu_add_entry(m, menu_entry_new("restart", a));
170 menu_add_entry(m, menu_entry_new_separator("--"));
171 a = action_from_string("exit");
172 menu_add_entry(m, menu_entry_new("exit", a));
176 s = menu_new("subsex menu", "submenu", m);
177 a = action_from_string("execute");
178 a->data.execute.path = g_strdup("xclock");
179 menu_add_entry(s, menu_entry_new("xclock", a));
181 menu_add_entry(m, menu_entry_new_submenu("subz", s));
183 s = menu_new("empty", "chub", m);
184 menu_add_entry(m, menu_entry_new_submenu("empty", s));
186 s = menu_new("", "s-club", m);
187 menu_add_entry(m, menu_entry_new_submenu("empty", s));
189 s = menu_new(NULL, "h-club", m);
190 menu_add_entry(m, menu_entry_new_submenu("empty", s));
192 s = menu_new(NULL, "g-club", m);
194 a = action_from_string("execute");
195 a->data.execute.path = g_strdup("xterm");
196 menu_add_entry(s, menu_entry_new("xterm", a));
197 a = action_from_string("restart");
198 menu_add_entry(s, menu_entry_new("restart", a));
199 menu_add_entry(s, menu_entry_new_separator("--"));
200 a = action_from_string("exit");
201 menu_add_entry(s, menu_entry_new("exit", a));
203 menu_add_entry(m, menu_entry_new_submenu("long", s));
209 g_hash_table_destroy(menu_hash
);
212 static Window
createWindow(Window parent
, unsigned long mask
,
213 XSetWindowAttributes
*attrib
)
215 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
216 RrDepth(ob_rr_inst
), InputOutput
,
217 RrVisual(ob_rr_inst
), mask
, attrib
);
221 ObMenu
*menu_new_full(char *label
, char *name
, ObMenu
*parent
,
222 menu_controller_show show
, menu_controller_update update
)
224 XSetWindowAttributes attrib
;
227 self
= g_new0(ObMenu
, 1);
228 self
->obwin
.type
= Window_Menu
;
229 self
->label
= g_strdup(label
);
230 self
->name
= g_strdup(name
);
231 self
->parent
= parent
;
232 self
->open_submenu
= NULL
;
234 self
->entries
= NULL
;
236 self
->invalid
= TRUE
;
238 /* default controllers */
241 self
->update
= update
;
242 self
->mouseover
= NULL
;
243 self
->selected
= NULL
;
246 self
->plugin_data
= NULL
;
248 attrib
.override_redirect
= TRUE
;
249 attrib
.event_mask
= FRAME_EVENTMASK
;
250 self
->frame
= createWindow(RootWindow(ob_display
, ob_screen
),
251 CWOverrideRedirect
|CWEventMask
, &attrib
);
252 attrib
.event_mask
= TITLE_EVENTMASK
;
253 self
->title
= createWindow(self
->frame
, CWEventMask
, &attrib
);
254 self
->items
= createWindow(self
->frame
, 0, &attrib
);
256 self
->a_title
= self
->a_items
= NULL
;
258 XMapWindow(ob_display
, self
->title
);
259 XMapWindow(ob_display
, self
->items
);
261 g_hash_table_insert(window_map
, &self
->frame
, self
);
262 g_hash_table_insert(window_map
, &self
->title
, self
);
263 g_hash_table_insert(window_map
, &self
->items
, self
);
264 g_hash_table_insert(menu_hash
, g_strdup(name
), self
);
266 stacking_add(MENU_AS_WINDOW(self
));
267 stacking_raise(MENU_AS_WINDOW(self
));
272 void menu_free(char *name
)
274 g_hash_table_remove(menu_hash
, name
);
277 ObMenuEntry
*menu_entry_new_full(char *label
, Action
*action
,
278 ObMenuEntryRenderType render_type
,
281 ObMenuEntry
*menu_entry
= g_new0(ObMenuEntry
, 1);
283 menu_entry
->label
= g_strdup(label
);
284 menu_entry
->render_type
= render_type
;
285 menu_entry
->action
= action
;
287 menu_entry
->hilite
= FALSE
;
288 menu_entry
->enabled
= TRUE
;
290 menu_entry
->submenu
= submenu
;
295 void menu_entry_set_submenu(ObMenuEntry
*entry
, ObMenu
*submenu
)
297 g_assert(entry
!= NULL
);
299 entry
->submenu
= submenu
;
301 if(entry
->parent
!= NULL
)
302 entry
->parent
->invalid
= TRUE
;
305 void menu_add_entry(ObMenu
*menu
, ObMenuEntry
*entry
)
307 XSetWindowAttributes attrib
;
309 g_assert(menu
!= NULL
);
310 g_assert(entry
!= NULL
);
311 g_assert(entry
->item
== None
);
313 menu
->entries
= g_list_append(menu
->entries
, entry
);
314 entry
->parent
= menu
;
316 attrib
.event_mask
= ENTRY_EVENTMASK
;
317 entry
->item
= createWindow(menu
->items
, CWEventMask
, &attrib
);
318 XMapWindow(ob_display
, entry
->item
);
320 entry
->a_item
= entry
->a_disabled
= entry
->a_hilite
= NULL
;
322 menu
->invalid
= TRUE
;
324 g_hash_table_insert(window_map
, &entry
->item
, menu
);
327 void menu_show(char *name
, int x
, int y
, ObClient
*client
)
331 self
= g_hash_table_lookup(menu_hash
, name
);
333 g_warning("Attempted to show menu '%s' but it does not exist.",
338 menu_show_full(self
, x
, y
, client
);
341 void menu_show_full(ObMenu
*self
, int x
, int y
, ObClient
*client
)
343 g_assert(self
!= NULL
);
347 self
->client
= client
;
350 if (!(self
->parent
&& self
->parent
->shown
)) {
351 grab_pointer(TRUE
, None
);
354 menu_visible
= g_list_append(menu_visible
, self
);
358 self
->show(self
, x
, y
, client
);
360 menu_control_show(self
, x
, y
, client
);
364 void menu_hide(ObMenu
*self
) {
366 XUnmapWindow(ob_display
, self
->frame
);
368 if (self
->open_submenu
)
369 menu_hide(self
->open_submenu
);
370 if (self
->parent
&& self
->parent
->open_submenu
== self
)
371 self
->parent
->open_submenu
= NULL
;
373 if (!(self
->parent
&& self
->parent
->shown
)) {
374 grab_keyboard(FALSE
);
375 grab_pointer(FALSE
, None
);
377 menu_visible
= g_list_remove(menu_visible
, self
);
381 void menu_clear(ObMenu
*self
) {
384 for (it
= self
->entries
; it
; it
= it
->next
) {
385 ObMenuEntry
*entry
= it
->data
;
386 menu_entry_free(entry
);
388 self
->entries
= NULL
;
389 self
->invalid
= TRUE
;
393 ObMenuEntry
*menu_find_entry(ObMenu
*menu
, Window win
)
397 for (it
= menu
->entries
; it
; it
= it
->next
) {
398 ObMenuEntry
*entry
= it
->data
;
399 if (entry
->item
== win
)
405 ObMenuEntry
*menu_find_entry_by_submenu(ObMenu
*menu
, ObMenu
*submenu
)
409 for (it
= menu
->entries
; it
; it
= it
->next
) {
410 ObMenuEntry
*entry
= it
->data
;
411 if (entry
->submenu
== submenu
)
417 ObMenuEntry
*menu_find_entry_by_pos(ObMenu
*menu
, int x
, int y
)
419 if (x
< 0 || x
>= menu
->size
.width
|| y
< 0 || y
>= menu
->size
.height
)
422 y
-= menu
->title_h
+ ob_rr_theme
->bwidth
;
423 if (y
< 0) return NULL
;
425 g_message ("%d %p", y
/menu
->item_h
, g_list_nth_data(menu
->entries
, y
/ menu
->item_h
));
426 return g_list_nth_data(menu
->entries
, y
/ menu
->item_h
);
429 void menu_entry_fire(ObMenuEntry
*self
)
434 self
->action
->data
.any
.c
= self
->parent
->client
;
435 self
->action
->func(&self
->action
->data
);
437 /* hide the whole thing */
439 while (m
->parent
) m
= m
->parent
;
445 Default menu controller action for showing.
448 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
)
453 g_assert(!self
->invalid
);
455 for (i
= 0; i
< screen_num_monitors
; ++i
) {
456 a
= screen_physical_area_monitor(i
);
457 if (RECT_CONTAINS(*a
, x
, y
))
463 POINT_SET(self
->location
,
464 MIN(x
, a
->x
+ a
->width
- 1 - self
->size
.width
),
465 MIN(y
, a
->y
+ a
->height
- 1 - self
->size
.height
));
466 XMoveWindow(ob_display
, self
->frame
, self
->location
.x
, self
->location
.y
);
469 XMapWindow(ob_display
, self
->frame
);
470 stacking_raise(MENU_AS_WINDOW(self
));
472 } else if (self
->shown
&& self
->open_submenu
) {
473 menu_hide(self
->open_submenu
);
477 void menu_control_mouseover(ObMenuEntry
*self
, gboolean enter
)
484 if (self
->parent
->open_submenu
&& self
->submenu
485 != self
->parent
->open_submenu
)
487 e
= menu_find_entry_by_submenu(self
->parent
,
488 self
->parent
->open_submenu
);
490 menu_entry_render(e
);
491 menu_hide(self
->parent
->open_submenu
);
494 if (self
->submenu
&& self
->parent
->open_submenu
!= self
->submenu
) {
495 self
->parent
->open_submenu
= self
->submenu
;
497 /* shouldn't be invalid since it must be displayed */
498 g_assert(!self
->parent
->invalid
);
499 /* TODO: I don't understand why these bevels should be here.
500 Something must be wrong in the width calculation */
501 x
= self
->parent
->location
.x
+ self
->parent
->size
.width
+
502 ob_rr_theme
->bwidth
- ob_rr_theme
->menu_overlap
;
504 /* need to get the width. is this bad?*/
505 menu_render(self
->submenu
);
507 a
= screen_physical_area_monitor(self
->parent
->xin_area
);
509 if (self
->submenu
->size
.width
+ x
>= a
->x
+ a
->width
)
510 x
= self
->parent
->location
.x
- self
->submenu
->size
.width
-
511 ob_rr_theme
->bwidth
+ ob_rr_theme
->menu_overlap
;
513 menu_show_full(self
->submenu
, x
,
514 self
->parent
->location
.y
+ self
->y
,
515 self
->parent
->client
);
519 if (enter
|| !self
->submenu
||
520 menu_find_entry_by_submenu(self
->parent
,
521 self
->parent
->open_submenu
) != self
)
522 self
->hilite
= enter
;
525 ObMenuEntry
*menu_control_keyboard_nav(ObMenuEntry
*over
, ObKey key
)
532 if (over
->parent
->mouseover
)
533 over
->parent
->mouseover(over
, FALSE
);
535 menu_control_mouseover(over
, FALSE
);
536 menu_entry_render(over
);
538 it
= over
->parent
->entries
;
539 while (it
!= NULL
&& it
->data
!= over
)
544 over
= (ObMenuEntry
*)it
->next
->data
;
545 else if (over
== NULL
) {
546 if (menu_visible
&& ((ObMenu
*)menu_visible
->data
)->entries
)
547 over
= (ObMenuEntry
*)
548 (((ObMenu
*)menu_visible
->data
)->entries
)->data
;
552 over
= (over
->parent
->entries
!= NULL
?
553 over
->parent
->entries
->data
: NULL
);
557 if (over
->parent
->mouseover
)
558 over
->parent
->mouseover(over
, TRUE
);
560 menu_control_mouseover(over
, TRUE
);
561 menu_entry_render(over
);
568 if (over
->parent
->mouseover
)
569 over
->parent
->mouseover(over
, FALSE
);
571 menu_control_mouseover(over
, FALSE
);
572 menu_entry_render(over
);
574 it
= g_list_last(over
->parent
->entries
);
575 while (it
!= NULL
&& it
->data
!= over
)
580 over
= (ObMenuEntry
*)it
->prev
->data
;
581 else if (over
== NULL
) {
582 it
= g_list_last(menu_visible
);
584 it
= g_list_last(((ObMenu
*)it
->data
)->entries
);
585 over
= (ObMenuEntry
*)(it
!= NULL
? it
->data
: NULL
);
588 over
= (over
->parent
->entries
!= NULL
?
589 g_list_last(over
->parent
->entries
)->data
:
592 if (over
->parent
->mouseover
)
593 over
->parent
->mouseover(over
, TRUE
);
595 menu_control_mouseover(over
, TRUE
);
596 menu_entry_render(over
);
599 case OB_KEY_RETURN
: {
604 if (over
->parent
->mouseover
)
605 over
->parent
->mouseover(over
, FALSE
);
607 menu_control_mouseover(over
, FALSE
);
608 menu_entry_render(over
);
610 if (over
->submenu
->entries
)
611 over
= over
->submenu
->entries
->data
;
613 if (over
->parent
->mouseover
)
614 over
->parent
->mouseover(over
, TRUE
);
616 menu_control_mouseover(over
, TRUE
);
617 menu_entry_render(over
);
620 if (over
->parent
->mouseover
)
621 over
->parent
->mouseover(over
, FALSE
);
623 menu_control_mouseover(over
, FALSE
);
624 menu_entry_render(over
);
626 menu_entry_fire(over
);
630 case OB_KEY_ESCAPE
: {
632 if (over
->parent
->mouseover
)
633 over
->parent
->mouseover(over
, FALSE
);
635 menu_control_mouseover(over
, FALSE
);
636 menu_entry_render(over
);
638 menu_hide(over
->parent
);
640 it
= g_list_last(menu_visible
);
642 menu_hide((ObMenu
*)it
->data
);
650 g_error("Unknown key");