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
->parent
, &id
))
38 if (!parse_attr_string("label", node
->parent
, &title
))
41 g_message("menu label %s", title
);
43 menu
= menu_new(title
, id
, data
? *((ObMenu
**)data
) : NULL
);
46 *((ObMenu
**)data
) = menu
;
48 menu
= (ObMenu
*)data
;
52 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "menu")) {
53 if (parse_attr_string("plugin", node
, &plugin
)) {
54 PluginMenuCreateData data
;
58 parent
= plugin_create(plugin
, &data
);
62 parse_menu(doc
, node
->xmlChildrenNode
, &parent
);
63 menu_add_entry(menu
, menu_entry_new_submenu(parent
->label
,
68 else if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "item")) {
69 if (parse_attr_string("label", node
, &label
)) {
70 if ((nact
= parse_find_node("action", node
->xmlChildrenNode
)))
71 act
= action_parse(doc
, nact
);
75 menu_add_entry(menu
, menu_entry_new(label
, act
));
77 menu_add_entry(menu
, menu_entry_new_separator(label
));
89 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
);
91 void menu_destroy_hash_key(ObMenu
*menu
)
96 void menu_destroy_hash_value(ObMenu
*self
)
100 for (it
= self
->entries
; it
; it
= it
->next
)
101 menu_entry_free(it
->data
);
102 g_list_free(self
->entries
);
107 g_hash_table_remove(window_map
, &self
->title
);
108 g_hash_table_remove(window_map
, &self
->frame
);
109 g_hash_table_remove(window_map
, &self
->items
);
111 stacking_remove(self
);
113 RrAppearanceFree(self
->a_title
);
114 RrAppearanceFree(self
->a_items
);
115 XDestroyWindow(ob_display
, self
->title
);
116 XDestroyWindow(ob_display
, self
->frame
);
117 XDestroyWindow(ob_display
, self
->items
);
122 void menu_entry_free(ObMenuEntry
*self
)
125 action_free(self
->action
);
127 g_hash_table_remove(window_map
, &self
->item
);
129 RrAppearanceFree(self
->a_item
);
130 RrAppearanceFree(self
->a_disabled
);
131 RrAppearanceFree(self
->a_hilite
);
132 XDestroyWindow(ob_display
, self
->item
);
146 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
147 (GDestroyNotify
)menu_destroy_hash_key
,
148 (GDestroyNotify
)menu_destroy_hash_value
);
150 parse_register("menu", parse_menu
, NULL
);
153 m = menu_new("sex menu", "root", NULL);
155 a = action_from_string("execute");
156 a->data.execute.path = g_strdup("xterm");
157 menu_add_entry(m, menu_entry_new("xterm", a));
158 a = action_from_string("restart");
159 menu_add_entry(m, menu_entry_new("restart", a));
160 menu_add_entry(m, menu_entry_new_separator("--"));
161 a = action_from_string("exit");
162 menu_add_entry(m, menu_entry_new("exit", a));
166 s = menu_new("subsex menu", "submenu", m);
167 a = action_from_string("execute");
168 a->data.execute.path = g_strdup("xclock");
169 menu_add_entry(s, menu_entry_new("xclock", a));
171 menu_add_entry(m, menu_entry_new_submenu("subz", s));
173 s = menu_new("empty", "chub", m);
174 menu_add_entry(m, menu_entry_new_submenu("empty", s));
176 s = menu_new("", "s-club", m);
177 menu_add_entry(m, menu_entry_new_submenu("empty", s));
179 s = menu_new(NULL, "h-club", m);
180 menu_add_entry(m, menu_entry_new_submenu("empty", s));
182 s = menu_new(NULL, "g-club", m);
184 a = action_from_string("execute");
185 a->data.execute.path = g_strdup("xterm");
186 menu_add_entry(s, menu_entry_new("xterm", a));
187 a = action_from_string("restart");
188 menu_add_entry(s, menu_entry_new("restart", a));
189 menu_add_entry(s, menu_entry_new_separator("--"));
190 a = action_from_string("exit");
191 menu_add_entry(s, menu_entry_new("exit", a));
193 menu_add_entry(m, menu_entry_new_submenu("long", s));
199 g_hash_table_destroy(menu_hash
);
202 static Window
createWindow(Window parent
, unsigned long mask
,
203 XSetWindowAttributes
*attrib
)
205 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
206 RrDepth(ob_rr_inst
), InputOutput
,
207 RrVisual(ob_rr_inst
), mask
, attrib
);
211 ObMenu
*menu_new_full(char *label
, char *name
, ObMenu
*parent
,
212 menu_controller_show show
, menu_controller_update update
)
214 XSetWindowAttributes attrib
;
217 self
= g_new0(ObMenu
, 1);
218 self
->obwin
.type
= Window_Menu
;
219 self
->label
= g_strdup(label
);
220 self
->name
= g_strdup(name
);
221 self
->parent
= parent
;
222 self
->open_submenu
= NULL
;
224 self
->entries
= NULL
;
226 self
->invalid
= TRUE
;
228 /* default controllers */
231 self
->update
= update
;
232 self
->mouseover
= NULL
;
233 self
->selected
= NULL
;
236 self
->plugin_data
= NULL
;
238 attrib
.override_redirect
= TRUE
;
239 attrib
.event_mask
= FRAME_EVENTMASK
;
240 self
->frame
= createWindow(RootWindow(ob_display
, ob_screen
),
241 CWOverrideRedirect
|CWEventMask
, &attrib
);
242 attrib
.event_mask
= TITLE_EVENTMASK
;
243 self
->title
= createWindow(self
->frame
, CWEventMask
, &attrib
);
244 self
->items
= createWindow(self
->frame
, 0, &attrib
);
246 self
->a_title
= self
->a_items
= NULL
;
248 XMapWindow(ob_display
, self
->title
);
249 XMapWindow(ob_display
, self
->items
);
251 g_hash_table_insert(window_map
, &self
->frame
, self
);
252 g_hash_table_insert(window_map
, &self
->title
, self
);
253 g_hash_table_insert(window_map
, &self
->items
, self
);
254 g_hash_table_insert(menu_hash
, g_strdup(name
), self
);
256 stacking_add(MENU_AS_WINDOW(self
));
257 stacking_raise(MENU_AS_WINDOW(self
));
262 void menu_free(char *name
)
264 g_hash_table_remove(menu_hash
, name
);
267 ObMenuEntry
*menu_entry_new_full(char *label
, Action
*action
,
268 ObMenuEntryRenderType render_type
,
271 ObMenuEntry
*menu_entry
= g_new0(ObMenuEntry
, 1);
273 menu_entry
->label
= g_strdup(label
);
274 menu_entry
->render_type
= render_type
;
275 menu_entry
->action
= action
;
277 menu_entry
->hilite
= FALSE
;
278 menu_entry
->enabled
= TRUE
;
280 menu_entry
->submenu
= submenu
;
285 void menu_entry_set_submenu(ObMenuEntry
*entry
, ObMenu
*submenu
)
287 g_assert(entry
!= NULL
);
289 entry
->submenu
= submenu
;
291 if(entry
->parent
!= NULL
)
292 entry
->parent
->invalid
= TRUE
;
295 void menu_add_entry(ObMenu
*menu
, ObMenuEntry
*entry
)
297 XSetWindowAttributes attrib
;
299 g_assert(menu
!= NULL
);
300 g_assert(entry
!= NULL
);
301 g_assert(entry
->item
== None
);
303 menu
->entries
= g_list_append(menu
->entries
, entry
);
304 entry
->parent
= menu
;
306 attrib
.event_mask
= ENTRY_EVENTMASK
;
307 entry
->item
= createWindow(menu
->items
, CWEventMask
, &attrib
);
308 XMapWindow(ob_display
, entry
->item
);
310 entry
->a_item
= entry
->a_disabled
= entry
->a_hilite
= NULL
;
312 menu
->invalid
= TRUE
;
314 g_hash_table_insert(window_map
, &entry
->item
, menu
);
317 void menu_show(char *name
, int x
, int y
, ObClient
*client
)
321 self
= g_hash_table_lookup(menu_hash
, name
);
323 g_warning("Attempted to show menu '%s' but it does not exist.",
328 menu_show_full(self
, x
, y
, client
);
331 void menu_show_full(ObMenu
*self
, int x
, int y
, ObClient
*client
)
333 g_assert(self
!= NULL
);
337 self
->client
= client
;
340 if (!(self
->parent
&& self
->parent
->shown
)) {
341 grab_pointer(TRUE
, None
);
344 menu_visible
= g_list_append(menu_visible
, self
);
348 self
->show(self
, x
, y
, client
);
350 menu_control_show(self
, x
, y
, client
);
354 void menu_hide(ObMenu
*self
) {
356 XUnmapWindow(ob_display
, self
->frame
);
358 if (self
->open_submenu
)
359 menu_hide(self
->open_submenu
);
360 if (self
->parent
&& self
->parent
->open_submenu
== self
)
361 self
->parent
->open_submenu
= NULL
;
363 if (!(self
->parent
&& self
->parent
->shown
)) {
364 grab_keyboard(FALSE
);
365 grab_pointer(FALSE
, None
);
367 menu_visible
= g_list_remove(menu_visible
, self
);
371 void menu_clear(ObMenu
*self
) {
374 for (it
= self
->entries
; it
; it
= it
->next
) {
375 ObMenuEntry
*entry
= it
->data
;
376 menu_entry_free(entry
);
378 self
->entries
= NULL
;
379 self
->invalid
= TRUE
;
383 ObMenuEntry
*menu_find_entry(ObMenu
*menu
, Window win
)
387 for (it
= menu
->entries
; it
; it
= it
->next
) {
388 ObMenuEntry
*entry
= it
->data
;
389 if (entry
->item
== win
)
395 ObMenuEntry
*menu_find_entry_by_submenu(ObMenu
*menu
, ObMenu
*submenu
)
399 for (it
= menu
->entries
; it
; it
= it
->next
) {
400 ObMenuEntry
*entry
= it
->data
;
401 if (entry
->submenu
== submenu
)
407 ObMenuEntry
*menu_find_entry_by_pos(ObMenu
*menu
, int x
, int y
)
409 if (x
< 0 || x
>= menu
->size
.width
|| y
< 0 || y
>= menu
->size
.height
)
412 y
-= menu
->title_h
+ ob_rr_theme
->bwidth
;
413 if (y
< 0) return NULL
;
415 g_message ("%d %p", y
/menu
->item_h
, g_list_nth_data(menu
->entries
, y
/ menu
->item_h
));
416 return g_list_nth_data(menu
->entries
, y
/ menu
->item_h
);
419 void menu_entry_fire(ObMenuEntry
*self
)
424 self
->action
->data
.any
.c
= self
->parent
->client
;
425 self
->action
->func(&self
->action
->data
);
427 /* hide the whole thing */
429 while (m
->parent
) m
= m
->parent
;
435 Default menu controller action for showing.
438 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
)
443 g_assert(!self
->invalid
);
445 for (i
= 0; i
< screen_num_monitors
; ++i
) {
446 a
= screen_physical_area_monitor(i
);
447 if (RECT_CONTAINS(*a
, x
, y
))
453 POINT_SET(self
->location
,
454 MIN(x
, a
->x
+ a
->width
- 1 - self
->size
.width
),
455 MIN(y
, a
->y
+ a
->height
- 1 - self
->size
.height
));
456 XMoveWindow(ob_display
, self
->frame
, self
->location
.x
, self
->location
.y
);
459 XMapWindow(ob_display
, self
->frame
);
460 stacking_raise(MENU_AS_WINDOW(self
));
462 } else if (self
->shown
&& self
->open_submenu
) {
463 menu_hide(self
->open_submenu
);
467 void menu_control_mouseover(ObMenuEntry
*self
, gboolean enter
)
474 if (self
->parent
->open_submenu
&& self
->submenu
475 != self
->parent
->open_submenu
)
477 e
= menu_find_entry_by_submenu(self
->parent
,
478 self
->parent
->open_submenu
);
480 menu_entry_render(e
);
481 menu_hide(self
->parent
->open_submenu
);
484 if (self
->submenu
&& self
->parent
->open_submenu
!= self
->submenu
) {
485 self
->parent
->open_submenu
= self
->submenu
;
487 /* shouldn't be invalid since it must be displayed */
488 g_assert(!self
->parent
->invalid
);
489 /* TODO: I don't understand why these bevels should be here.
490 Something must be wrong in the width calculation */
491 x
= self
->parent
->location
.x
+ self
->parent
->size
.width
+
492 ob_rr_theme
->bwidth
- ob_rr_theme
->menu_overlap
;
494 /* need to get the width. is this bad?*/
495 menu_render(self
->submenu
);
497 a
= screen_physical_area_monitor(self
->parent
->xin_area
);
499 if (self
->submenu
->size
.width
+ x
>= a
->x
+ a
->width
)
500 x
= self
->parent
->location
.x
- self
->submenu
->size
.width
-
501 ob_rr_theme
->bwidth
+ ob_rr_theme
->menu_overlap
;
503 menu_show_full(self
->submenu
, x
,
504 self
->parent
->location
.y
+ self
->y
,
505 self
->parent
->client
);
509 if (enter
|| !self
->submenu
||
510 menu_find_entry_by_submenu(self
->parent
,
511 self
->parent
->open_submenu
) != self
)
512 self
->hilite
= enter
;
515 ObMenuEntry
*menu_control_keyboard_nav(ObMenuEntry
*over
, ObKey key
)
522 if (over
->parent
->mouseover
)
523 over
->parent
->mouseover(over
, FALSE
);
525 menu_control_mouseover(over
, FALSE
);
526 menu_entry_render(over
);
528 it
= over
->parent
->entries
;
529 while (it
!= NULL
&& it
->data
!= over
)
534 over
= (ObMenuEntry
*)it
->next
->data
;
535 else if (over
== NULL
) {
536 if (menu_visible
&& ((ObMenu
*)menu_visible
->data
)->entries
)
537 over
= (ObMenuEntry
*)
538 (((ObMenu
*)menu_visible
->data
)->entries
)->data
;
542 over
= (over
->parent
->entries
!= NULL
?
543 over
->parent
->entries
->data
: NULL
);
547 if (over
->parent
->mouseover
)
548 over
->parent
->mouseover(over
, TRUE
);
550 menu_control_mouseover(over
, TRUE
);
551 menu_entry_render(over
);
558 if (over
->parent
->mouseover
)
559 over
->parent
->mouseover(over
, FALSE
);
561 menu_control_mouseover(over
, FALSE
);
562 menu_entry_render(over
);
564 it
= g_list_last(over
->parent
->entries
);
565 while (it
!= NULL
&& it
->data
!= over
)
570 over
= (ObMenuEntry
*)it
->prev
->data
;
571 else if (over
== NULL
) {
572 it
= g_list_last(menu_visible
);
574 it
= g_list_last(((ObMenu
*)it
->data
)->entries
);
575 over
= (ObMenuEntry
*)(it
!= NULL
? it
->data
: NULL
);
578 over
= (over
->parent
->entries
!= NULL
?
579 g_list_last(over
->parent
->entries
)->data
:
582 if (over
->parent
->mouseover
)
583 over
->parent
->mouseover(over
, TRUE
);
585 menu_control_mouseover(over
, TRUE
);
586 menu_entry_render(over
);
589 case OB_KEY_RETURN
: {
594 if (over
->parent
->mouseover
)
595 over
->parent
->mouseover(over
, FALSE
);
597 menu_control_mouseover(over
, FALSE
);
598 menu_entry_render(over
);
600 if (over
->submenu
->entries
)
601 over
= over
->submenu
->entries
->data
;
603 if (over
->parent
->mouseover
)
604 over
->parent
->mouseover(over
, TRUE
);
606 menu_control_mouseover(over
, TRUE
);
607 menu_entry_render(over
);
610 if (over
->parent
->mouseover
)
611 over
->parent
->mouseover(over
, FALSE
);
613 menu_control_mouseover(over
, FALSE
);
614 menu_entry_render(over
);
616 menu_entry_fire(over
);
620 case OB_KEY_ESCAPE
: {
622 if (over
->parent
->mouseover
)
623 over
->parent
->mouseover(over
, FALSE
);
625 menu_control_mouseover(over
, FALSE
);
626 menu_entry_render(over
);
628 menu_hide(over
->parent
);
630 it
= g_list_last(menu_visible
);
632 menu_hide((ObMenu
*)it
->data
);
640 g_error("Unknown key");