12 GHashTable
*menu_hash
= NULL
;
13 GList
*menu_visible
= NULL
;
15 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
17 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
18 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
19 ButtonPressMask | ButtonReleaseMask)
21 static void parse_menu(xmlDocPtr doc
, xmlNodePtr node
, void *data
)
23 parse_menu_full(doc
, node
, data
, TRUE
);
27 void parse_menu_full(xmlDocPtr doc
, xmlNodePtr node
, void *data
,
33 gchar
*id
= NULL
, *title
= NULL
, *label
= NULL
, *plugin
;
34 ObMenu
*menu
= NULL
, *parent
;
36 if (newmenu
== TRUE
) {
37 if (!parse_attr_string("id", node
, &id
))
39 if (!parse_attr_string("label", node
, &title
))
41 ob_debug("menu label %s\n", title
);
43 if (parse_attr_string("plugin", node
, &plugin
)) {
44 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
);
149 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
150 (GDestroyNotify
)menu_destroy_hash_key
,
151 (GDestroyNotify
)menu_destroy_hash_value
);
153 parse_register("menu", parse_menu
, NULL
);
159 g_hash_table_destroy(menu_hash
);
162 static Window
createWindow(Window parent
, unsigned long mask
,
163 XSetWindowAttributes
*attrib
)
165 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
166 RrDepth(ob_rr_inst
), InputOutput
,
167 RrVisual(ob_rr_inst
), mask
, attrib
);
171 ObMenu
*menu_new_full(char *label
, char *name
, ObMenu
*parent
,
172 menu_controller_show show
, menu_controller_update update
,
173 menu_controller_selected selected
,
174 menu_controller_hide hide
,
175 menu_controller_mouseover mouseover
)
177 XSetWindowAttributes attrib
;
180 self
= g_new0(ObMenu
, 1);
181 self
->obwin
.type
= Window_Menu
;
182 self
->label
= g_strdup(label
);
183 self
->name
= g_strdup(name
);
184 self
->parent
= parent
;
185 self
->open_submenu
= NULL
;
187 self
->entries
= NULL
;
189 self
->invalid
= TRUE
;
191 /* default controllers */
192 self
->show
= (show
!= NULL
? show
: menu_show_full
);
193 self
->hide
= (hide
!= NULL
? hide
: menu_hide
);
194 self
->update
= (update
!= NULL
? update
: menu_render
);
195 self
->mouseover
= (mouseover
!= NULL
? mouseover
:
196 menu_control_mouseover
);
197 self
->selected
= (selected
!= NULL
? selected
: menu_entry_fire
);
200 self
->plugin_data
= NULL
;
202 attrib
.override_redirect
= TRUE
;
203 attrib
.event_mask
= FRAME_EVENTMASK
;
204 self
->frame
= createWindow(RootWindow(ob_display
, ob_screen
),
205 CWOverrideRedirect
|CWEventMask
, &attrib
);
206 attrib
.event_mask
= TITLE_EVENTMASK
;
207 self
->title
= createWindow(self
->frame
, CWEventMask
, &attrib
);
208 self
->items
= createWindow(self
->frame
, 0, &attrib
);
210 self
->a_title
= self
->a_items
= NULL
;
212 XMapWindow(ob_display
, self
->title
);
213 XMapWindow(ob_display
, self
->items
);
215 g_hash_table_insert(window_map
, &self
->frame
, self
);
216 g_hash_table_insert(window_map
, &self
->title
, self
);
217 g_hash_table_insert(window_map
, &self
->items
, self
);
218 g_hash_table_insert(menu_hash
, g_strdup(name
), self
);
220 stacking_add(MENU_AS_WINDOW(self
));
221 stacking_raise(MENU_AS_WINDOW(self
));
226 void menu_free(char *name
)
228 g_hash_table_remove(menu_hash
, name
);
231 ObMenuEntry
*menu_entry_new_full(char *label
, Action
*action
,
232 ObMenuEntryRenderType render_type
,
235 ObMenuEntry
*menu_entry
= g_new0(ObMenuEntry
, 1);
237 menu_entry
->label
= g_strdup(label
);
238 menu_entry
->render_type
= render_type
;
239 menu_entry
->action
= action
;
241 menu_entry
->hilite
= FALSE
;
242 menu_entry
->enabled
= TRUE
;
244 menu_entry
->submenu
= submenu
;
249 void menu_entry_set_submenu(ObMenuEntry
*entry
, ObMenu
*submenu
)
251 g_assert(entry
!= NULL
);
253 entry
->submenu
= submenu
;
255 if(entry
->parent
!= NULL
)
256 entry
->parent
->invalid
= TRUE
;
259 void menu_add_entry(ObMenu
*menu
, ObMenuEntry
*entry
)
261 XSetWindowAttributes attrib
;
263 g_assert(menu
!= NULL
);
264 g_assert(entry
!= NULL
);
265 g_assert(entry
->item
== None
);
267 menu
->entries
= g_list_append(menu
->entries
, entry
);
268 entry
->parent
= menu
;
270 attrib
.event_mask
= ENTRY_EVENTMASK
;
271 entry
->item
= createWindow(menu
->items
, CWEventMask
, &attrib
);
272 XMapWindow(ob_display
, entry
->item
);
274 entry
->a_item
= entry
->a_disabled
= entry
->a_hilite
= NULL
;
276 menu
->invalid
= TRUE
;
278 g_hash_table_insert(window_map
, &entry
->item
, menu
);
281 void menu_show(char *name
, int x
, int y
, ObClient
*client
)
285 self
= g_hash_table_lookup(menu_hash
, name
);
287 g_warning("Attempted to show menu '%s' but it does not exist.",
292 menu_show_full(self
, x
, y
, client
);
295 void menu_show_full(ObMenu
*self
, int x
, int y
, ObClient
*client
)
297 g_assert(self
!= NULL
);
301 self
->client
= client
;
304 if (!(self
->parent
&& self
->parent
->shown
)) {
305 grab_pointer(TRUE
, None
);
308 menu_visible
= g_list_append(menu_visible
, self
);
311 menu_control_show(self
, x
, y
, client
);
314 void menu_hide(ObMenu
*self
) {
316 XUnmapWindow(ob_display
, self
->frame
);
318 if (self
->open_submenu
)
319 menu_hide(self
->open_submenu
);
320 if (self
->parent
&& self
->parent
->open_submenu
== self
) {
323 self
->parent
->open_submenu
= NULL
;
325 e
= menu_find_entry_by_submenu(self
->parent
, self
);
326 self
->parent
->mouseover(e
, FALSE
);
329 if (!(self
->parent
&& self
->parent
->shown
)) {
330 grab_keyboard(FALSE
);
331 grab_pointer(FALSE
, None
);
333 menu_visible
= g_list_remove(menu_visible
, self
);
337 void menu_clear(ObMenu
*self
) {
340 for (it
= self
->entries
; it
; it
= it
->next
) {
341 ObMenuEntry
*entry
= it
->data
;
342 menu_entry_free(entry
);
344 self
->entries
= NULL
;
345 self
->invalid
= TRUE
;
349 ObMenuEntry
*menu_find_entry(ObMenu
*menu
, Window win
)
353 for (it
= menu
->entries
; it
; it
= it
->next
) {
354 ObMenuEntry
*entry
= it
->data
;
355 if (entry
->item
== win
)
361 ObMenuEntry
*menu_find_entry_by_submenu(ObMenu
*menu
, ObMenu
*submenu
)
365 for (it
= menu
->entries
; it
; it
= it
->next
) {
366 ObMenuEntry
*entry
= it
->data
;
367 if (entry
->submenu
== submenu
)
373 ObMenuEntry
*menu_find_entry_by_pos(ObMenu
*menu
, int x
, int y
)
375 if (x
< 0 || x
>= menu
->size
.width
|| y
< 0 || y
>= menu
->size
.height
)
378 y
-= menu
->title_h
+ ob_rr_theme
->bwidth
;
379 if (y
< 0) return NULL
;
381 ob_debug("%d %p\n", y
/menu
->item_h
,
382 g_list_nth_data(menu
->entries
, y
/ menu
->item_h
));
383 return g_list_nth_data(menu
->entries
, y
/ menu
->item_h
);
386 void menu_entry_fire(ObMenuEntry
*self
, unsigned int button
, unsigned int x
,
391 if (button
> 3) return;
394 self
->action
->data
.any
.c
= self
->parent
->client
;
395 self
->action
->func(&self
->action
->data
);
397 /* hide the whole thing */
399 while (m
->parent
) m
= m
->parent
;
405 Default menu controller action for showing.
408 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
)
413 g_assert(!self
->invalid
);
415 for (i
= 0; i
< screen_num_monitors
; ++i
) {
416 a
= screen_physical_area_monitor(i
);
417 if (RECT_CONTAINS(*a
, x
, y
))
423 POINT_SET(self
->location
,
424 MIN(x
, a
->x
+ a
->width
- 1 - self
->size
.width
),
425 MIN(y
, a
->y
+ a
->height
- 1 - self
->size
.height
));
426 XMoveWindow(ob_display
, self
->frame
, self
->location
.x
, self
->location
.y
);
429 XMapWindow(ob_display
, self
->frame
);
430 stacking_raise(MENU_AS_WINDOW(self
));
432 } else if (self
->shown
&& self
->open_submenu
) {
433 menu_hide(self
->open_submenu
);
437 void menu_control_mouseover(ObMenuEntry
*self
, gboolean enter
)
444 if (self
->parent
->open_submenu
&& self
->submenu
445 != self
->parent
->open_submenu
)
447 e
= menu_find_entry_by_submenu(self
->parent
,
448 self
->parent
->open_submenu
);
450 menu_entry_render(e
);
451 menu_hide(self
->parent
->open_submenu
);
454 if (self
->submenu
&& self
->parent
->open_submenu
!= self
->submenu
) {
455 self
->parent
->open_submenu
= self
->submenu
;
457 /* shouldn't be invalid since it must be displayed */
458 g_assert(!self
->parent
->invalid
);
459 /* TODO: I don't understand why these bevels should be here.
460 Something must be wrong in the width calculation */
461 x
= self
->parent
->location
.x
+ self
->parent
->size
.width
+
462 ob_rr_theme
->bwidth
- ob_rr_theme
->menu_overlap
;
464 /* need to get the width. is this bad?*/
465 menu_render(self
->submenu
);
467 a
= screen_physical_area_monitor(self
->parent
->xin_area
);
469 if (self
->submenu
->size
.width
+ x
>= a
->x
+ a
->width
)
470 x
= self
->parent
->location
.x
- self
->submenu
->size
.width
-
471 ob_rr_theme
->bwidth
+ ob_rr_theme
->menu_overlap
;
473 menu_show_full(self
->submenu
, x
,
474 self
->parent
->location
.y
+ self
->y
,
475 self
->parent
->client
);
479 if (enter
|| !self
->submenu
||
480 menu_find_entry_by_submenu(self
->parent
,
481 self
->parent
->open_submenu
) != self
)
482 self
->hilite
= enter
;
484 menu_entry_render(self
);
487 ObMenuEntry
*menu_control_keyboard_nav(ObMenuEntry
*over
, ObKey key
)
494 over
->parent
->mouseover(over
, FALSE
);
496 it
= over
->parent
->entries
;
497 while (it
!= NULL
&& it
->data
!= over
)
502 over
= (ObMenuEntry
*)it
->next
->data
;
503 else if (over
== NULL
) {
504 if (menu_visible
&& ((ObMenu
*)menu_visible
->data
)->entries
)
505 over
= (ObMenuEntry
*)
506 (((ObMenu
*)menu_visible
->data
)->entries
)->data
;
510 over
= (over
->parent
->entries
!= NULL
?
511 over
->parent
->entries
->data
: NULL
);
515 over
->parent
->mouseover(over
, TRUE
);
521 over
->parent
->mouseover(over
, FALSE
);
523 it
= g_list_last(over
->parent
->entries
);
524 while (it
!= NULL
&& it
->data
!= over
)
529 over
= (ObMenuEntry
*)it
->prev
->data
;
530 else if (over
== NULL
) {
531 it
= g_list_last(menu_visible
);
533 it
= g_list_last(((ObMenu
*)it
->data
)->entries
);
534 over
= (ObMenuEntry
*)(it
!= NULL
? it
->data
: NULL
);
537 over
= (over
->parent
->entries
!= NULL
?
538 g_list_last(over
->parent
->entries
)->data
:
541 over
->parent
->mouseover(over
, TRUE
);
549 over
->parent
->mouseover(over
, FALSE
);
551 if (over
->submenu
->entries
)
552 over
= over
->submenu
->entries
->data
;
554 over
->parent
->mouseover(over
, TRUE
);
557 over
->parent
->mouseover(over
, FALSE
);
560 menu_entry_fire(over
, 0, 0, 0);
566 over
->parent
->mouseover(over
, FALSE
);
567 menu_hide(over
->parent
);
569 it
= g_list_last(menu_visible
);
571 menu_hide((ObMenu
*)it
->data
);
579 g_error("Unknown key");
587 /* This noop brought to you by OLS 2003 Email Garden. */