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
;
49 if (plugin_open_reopen(plugin
))
50 parent
= plugin_create(plugin
, &data
);
53 menu
= menu_new(title
, id
, data
? *((ObMenu
**)data
) : NULL
);
56 *((ObMenu
**)data
) = menu
;
58 menu
= (ObMenu
*)data
;
61 node
= node
->xmlChildrenNode
;
64 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "menu")) {
65 if (parse_attr_string("plugin", node
, &plugin
)) {
66 PluginMenuCreateData data
;
70 if (plugin_open_reopen(plugin
))
71 parent
= plugin_create(plugin
, &data
);
75 parse_menu(doc
, node
, &parent
);
76 menu_add_entry(menu
, menu_entry_new_submenu(parent
->label
,
81 else if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "item")) {
82 if (parse_attr_string("label", node
, &label
)) {
83 if ((nact
= parse_find_node("action", node
->xmlChildrenNode
)))
84 act
= action_parse(doc
, nact
);
88 menu_add_entry(menu
, menu_entry_new(label
, act
));
90 menu_add_entry(menu
, menu_entry_new_separator(label
));
102 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
);
104 void menu_destroy_hash_key(ObMenu
*menu
)
109 void menu_destroy_hash_value(ObMenu
*self
)
113 for (it
= self
->entries
; it
; it
= it
->next
)
114 menu_entry_free(it
->data
);
115 g_list_free(self
->entries
);
120 g_hash_table_remove(window_map
, &self
->title
);
121 g_hash_table_remove(window_map
, &self
->frame
);
122 g_hash_table_remove(window_map
, &self
->items
);
124 stacking_remove(self
);
126 RrAppearanceFree(self
->a_title
);
127 RrAppearanceFree(self
->a_items
);
128 XDestroyWindow(ob_display
, self
->title
);
129 XDestroyWindow(ob_display
, self
->frame
);
130 XDestroyWindow(ob_display
, self
->items
);
135 void menu_entry_free(ObMenuEntry
*self
)
138 action_free(self
->action
);
140 g_hash_table_remove(window_map
, &self
->item
);
142 RrAppearanceFree(self
->a_item
);
143 RrAppearanceFree(self
->a_disabled
);
144 RrAppearanceFree(self
->a_hilite
);
145 RrAppearanceFree(self
->a_submenu
);
146 XDestroyWindow(ob_display
, self
->item
);
147 XDestroyWindow(ob_display
, self
->submenu_pic
);
153 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
154 (GDestroyNotify
)menu_destroy_hash_key
,
155 (GDestroyNotify
)menu_destroy_hash_value
);
157 parse_register("menu", parse_menu
, NULL
);
163 g_hash_table_destroy(menu_hash
);
166 static Window
createWindow(Window parent
, unsigned long mask
,
167 XSetWindowAttributes
*attrib
)
169 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
170 RrDepth(ob_rr_inst
), InputOutput
,
171 RrVisual(ob_rr_inst
), mask
, attrib
);
175 ObMenu
*menu_new_full(char *label
, char *name
, ObMenu
*parent
,
176 menu_controller_show show
, menu_controller_update update
,
177 menu_controller_selected selected
,
178 menu_controller_hide hide
,
179 menu_controller_mouseover mouseover
)
181 XSetWindowAttributes attrib
;
184 self
= g_new0(ObMenu
, 1);
185 self
->obwin
.type
= Window_Menu
;
186 self
->label
= g_strdup(label
);
187 self
->name
= g_strdup(name
);
188 self
->parent
= parent
;
189 self
->open_submenu
= NULL
;
192 self
->entries
= NULL
;
194 self
->invalid
= TRUE
;
196 /* default controllers */
197 self
->show
= (show
!= NULL
? show
: menu_show_full
);
198 self
->hide
= (hide
!= NULL
? hide
: menu_hide
);
199 self
->update
= (update
!= NULL
? update
: menu_render
);
200 self
->mouseover
= (mouseover
!= NULL
? mouseover
:
201 menu_control_mouseover
);
202 self
->selected
= (selected
!= NULL
? selected
: menu_entry_fire
);
205 self
->plugin_data
= NULL
;
207 attrib
.override_redirect
= TRUE
;
208 attrib
.event_mask
= FRAME_EVENTMASK
;
209 self
->frame
= createWindow(RootWindow(ob_display
, ob_screen
),
210 CWOverrideRedirect
|CWEventMask
, &attrib
);
211 attrib
.event_mask
= TITLE_EVENTMASK
;
212 self
->title
= createWindow(self
->frame
, CWEventMask
, &attrib
);
213 self
->items
= createWindow(self
->frame
, 0, &attrib
);
215 self
->a_title
= self
->a_items
= NULL
;
217 XMapWindow(ob_display
, self
->title
);
218 XMapWindow(ob_display
, self
->items
);
220 g_hash_table_insert(window_map
, &self
->frame
, self
);
221 g_hash_table_insert(window_map
, &self
->title
, self
);
222 g_hash_table_insert(window_map
, &self
->items
, self
);
223 g_hash_table_insert(menu_hash
, g_strdup(name
), self
);
225 stacking_add(MENU_AS_WINDOW(self
));
226 stacking_raise(MENU_AS_WINDOW(self
));
231 void menu_free(char *name
)
233 g_hash_table_remove(menu_hash
, name
);
236 ObMenuEntry
*menu_entry_new_full(char *label
, ObAction
*action
,
237 ObMenuEntryRenderType render_type
,
240 ObMenuEntry
*menu_entry
= g_new0(ObMenuEntry
, 1);
242 menu_entry
->label
= g_strdup(label
);
243 menu_entry
->render_type
= render_type
;
244 menu_entry
->action
= action
;
246 menu_entry
->hilite
= FALSE
;
247 menu_entry
->enabled
= TRUE
;
249 menu_entry
->submenu
= submenu
;
254 void menu_entry_set_submenu(ObMenuEntry
*entry
, ObMenu
*submenu
)
256 g_assert(entry
!= NULL
);
258 entry
->submenu
= submenu
;
260 if(entry
->parent
!= NULL
)
261 entry
->parent
->invalid
= TRUE
;
264 void menu_add_entry(ObMenu
*menu
, ObMenuEntry
*entry
)
266 XSetWindowAttributes attrib
;
268 g_assert(menu
!= NULL
);
269 g_assert(entry
!= NULL
);
270 g_assert(entry
->item
== None
);
272 menu
->entries
= g_list_append(menu
->entries
, entry
);
273 entry
->parent
= menu
;
275 attrib
.event_mask
= ENTRY_EVENTMASK
;
276 entry
->item
= createWindow(menu
->items
, CWEventMask
, &attrib
);
277 entry
->submenu_pic
= createWindow(menu
->items
, CWEventMask
, &attrib
);
278 XMapWindow(ob_display
, entry
->item
);
279 XMapWindow(ob_display
, entry
->submenu_pic
);
281 entry
->a_item
= entry
->a_disabled
= entry
->a_hilite
= entry
->a_submenu
284 menu
->invalid
= TRUE
;
286 g_hash_table_insert(window_map
, &entry
->item
, menu
);
287 g_hash_table_insert(window_map
, &entry
->submenu_pic
, menu
);
290 void menu_show(char *name
, int x
, int y
, ObClient
*client
)
294 self
= g_hash_table_lookup(menu_hash
, name
);
296 g_warning("Attempted to show menu '%s' but it does not exist.",
301 menu_show_full(self
, x
, y
, client
);
304 void menu_show_full(ObMenu
*self
, int x
, int y
, ObClient
*client
)
306 g_assert(self
!= NULL
);
310 self
->client
= client
;
313 if (!(self
->parent
&& self
->parent
->shown
)) {
314 grab_pointer(TRUE
, None
);
317 menu_visible
= g_list_append(menu_visible
, self
);
320 menu_control_show(self
, x
, y
, client
);
323 void menu_hide(ObMenu
*self
) {
325 XUnmapWindow(ob_display
, self
->frame
);
327 if (self
->open_submenu
)
328 self
->open_submenu
->hide(self
->open_submenu
);
329 if (self
->parent
&& self
->parent
->open_submenu
== self
) {
330 self
->parent
->open_submenu
= NULL
;
333 if (!(self
->parent
&& self
->parent
->shown
)) {
334 grab_keyboard(FALSE
);
335 grab_pointer(FALSE
, None
);
337 menu_visible
= g_list_remove(menu_visible
, self
);
339 ((ObMenuEntry
*)self
->over
->data
)->hilite
= FALSE
;
340 menu_entry_render(self
->over
->data
);
346 void menu_clear(ObMenu
*self
) {
349 for (it
= self
->entries
; it
; it
= it
->next
) {
350 ObMenuEntry
*entry
= it
->data
;
351 menu_entry_free(entry
);
353 self
->entries
= NULL
;
354 self
->invalid
= TRUE
;
358 ObMenuEntry
*menu_find_entry(ObMenu
*menu
, Window win
)
362 for (it
= menu
->entries
; it
; it
= it
->next
) {
363 ObMenuEntry
*entry
= it
->data
;
364 if (entry
->item
== win
)
370 ObMenuEntry
*menu_find_entry_by_submenu(ObMenu
*menu
, ObMenu
*submenu
)
374 for (it
= menu
->entries
; it
; it
= it
->next
) {
375 ObMenuEntry
*entry
= it
->data
;
376 if (entry
->submenu
== submenu
)
382 ObMenuEntry
*menu_find_entry_by_pos(ObMenu
*menu
, int x
, int y
)
384 if (x
< 0 || x
>= menu
->size
.width
|| y
< 0 || y
>= menu
->size
.height
)
387 y
-= menu
->title_h
+ ob_rr_theme
->bwidth
;
388 if (y
< 0) return NULL
;
390 ob_debug("%d %p\n", y
/menu
->item_h
,
391 g_list_nth_data(menu
->entries
, y
/ menu
->item_h
));
392 return g_list_nth_data(menu
->entries
, y
/ menu
->item_h
);
395 void menu_entry_fire(ObMenuEntry
*self
, unsigned int button
, unsigned int x
,
400 /* ignore wheel scrolling */
401 if (button
== 4 || button
== 5) return;
404 self
->action
->data
.any
.c
= self
->parent
->client
;
405 self
->action
->func(&self
->action
->data
);
407 /* hide the whole thing */
409 while (m
->parent
) m
= m
->parent
;
415 Default menu controller action for showing.
418 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
)
423 g_assert(!self
->invalid
);
425 for (i
= 0; i
< screen_num_monitors
; ++i
) {
426 a
= screen_physical_area_monitor(i
);
427 if (RECT_CONTAINS(*a
, x
, y
))
433 POINT_SET(self
->location
,
434 MIN(x
, a
->x
+ a
->width
- 1 - self
->size
.width
),
435 MIN(y
, a
->y
+ a
->height
- 1 - self
->size
.height
));
436 XMoveWindow(ob_display
, self
->frame
, self
->location
.x
, self
->location
.y
);
439 XMapWindow(ob_display
, self
->frame
);
440 stacking_raise(MENU_AS_WINDOW(self
));
442 } else if (self
->shown
&& self
->open_submenu
) {
443 self
->open_submenu
->hide(self
->open_submenu
);
447 void menu_control_mouseover(ObMenuEntry
*self
, gboolean enter
)
453 g_assert(self
!= NULL
);
456 /* TODO: we prolly don't need open_submenu */
457 if (self
->parent
->open_submenu
&& self
->submenu
458 != self
->parent
->open_submenu
)
460 e
= (ObMenuEntry
*) self
->parent
->over
->data
;
462 menu_entry_render(e
);
463 self
->parent
->open_submenu
->hide(self
->parent
->open_submenu
);
466 if (self
->submenu
&& self
->parent
->open_submenu
!= self
->submenu
) {
467 self
->parent
->open_submenu
= self
->submenu
;
469 /* shouldn't be invalid since it must be displayed */
470 g_assert(!self
->parent
->invalid
);
471 /* TODO: I don't understand why these bevels should be here.
472 Something must be wrong in the width calculation */
473 x
= self
->parent
->location
.x
+ self
->parent
->size
.width
+
474 ob_rr_theme
->bwidth
- ob_rr_theme
->menu_overlap
;
476 /* need to get the width. is this bad?*/
477 self
->parent
->update(self
->submenu
);
479 a
= screen_physical_area_monitor(self
->parent
->xin_area
);
481 if (self
->submenu
->size
.width
+ x
>= a
->x
+ a
->width
) {
482 int newparentx
= a
->x
+ a
->width
483 - self
->submenu
->size
.width
484 - self
->parent
->size
.width
485 - ob_rr_theme
->bwidth
486 - ob_rr_theme
->menu_overlap
;
488 x
= a
->x
+ a
->width
- self
->submenu
->size
.width
489 - ob_rr_theme
->menu_overlap
;
490 XWarpPointer(ob_display
, None
, None
, 0, 0, 0, 0,
491 newparentx
- self
->parent
->location
.x
, 0);
493 menu_show_full(self
->parent
, newparentx
,
494 self
->parent
->location
.y
, self
->parent
->client
);
497 menu_show_full(self
->submenu
, x
,
498 self
->parent
->location
.y
+ self
->y
,
499 self
->parent
->client
);
502 self
->parent
->over
= g_list_find(self
->parent
->entries
, self
);
505 self
->hilite
= FALSE
;
507 menu_entry_render(self
);
510 void menu_control_keyboard_nav(unsigned int key
)
512 static ObMenu
*current_menu
= NULL
;
513 ObMenuEntry
*e
= NULL
;
515 ObKey obkey
= OB_NUM_KEYS
;
517 /* hrmm. could be fixed */
518 if (key
== ob_keycode(OB_KEY_DOWN
))
520 else if (key
== ob_keycode(OB_KEY_UP
))
522 else if (key
== ob_keycode(OB_KEY_RIGHT
)) /* fuck */
523 obkey
= OB_KEY_RIGHT
;
524 else if (key
== ob_keycode(OB_KEY_LEFT
)) /* users */
526 else if (key
== ob_keycode(OB_KEY_RETURN
))
527 obkey
= OB_KEY_RETURN
;
530 if (current_menu
== NULL
)
531 current_menu
= menu_visible
->data
;
535 if (current_menu
->over
) {
536 current_menu
->mouseover(current_menu
->over
->data
, FALSE
);
537 current_menu
->over
= (current_menu
->over
->next
!= NULL
?
538 current_menu
->over
->next
:
539 current_menu
->entries
);
542 current_menu
->over
= current_menu
->entries
;
544 if (current_menu
->over
)
545 current_menu
->mouseover(current_menu
->over
->data
, TRUE
);
550 if (current_menu
->over
) {
551 current_menu
->mouseover(current_menu
->over
->data
, FALSE
);
552 current_menu
->over
= (current_menu
->over
->prev
!= NULL
?
553 current_menu
->over
->prev
:
554 g_list_last(current_menu
->entries
));
556 current_menu
->over
= g_list_last(current_menu
->entries
);
558 if (current_menu
->over
)
559 current_menu
->mouseover(current_menu
->over
->data
, TRUE
);
564 if (current_menu
->over
== NULL
)
566 e
= (ObMenuEntry
*)current_menu
->over
->data
;
568 current_menu
->mouseover(e
, TRUE
);
569 current_menu
= e
->submenu
;
570 current_menu
->over
= current_menu
->entries
;
571 if (current_menu
->over
)
572 current_menu
->mouseover(current_menu
->over
->data
, TRUE
);
577 case OB_KEY_RETURN
: {
578 if (current_menu
->over
== NULL
)
580 e
= (ObMenuEntry
*)current_menu
->over
->data
;
582 current_menu
->mouseover(e
, FALSE
);
583 current_menu
->over
= NULL
;
585 menu_entry_fire(e
, 0, 0, 0);
589 if (current_menu
->over
!= NULL
) {
590 current_menu
->mouseover(current_menu
->over
->data
, FALSE
);
591 current_menu
->over
= NULL
;
594 current_menu
->hide(current_menu
);
596 if (current_menu
->parent
)
597 current_menu
= current_menu
->parent
;
602 ((ObMenu
*)menu_visible
->data
)->hide(menu_visible
->data
);
610 /* This noop brought to you by OLS 2003 Email Garden. */