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 XDestroyWindow(ob_display
, self
->item
);
152 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
153 (GDestroyNotify
)menu_destroy_hash_key
,
154 (GDestroyNotify
)menu_destroy_hash_value
);
156 parse_register("menu", parse_menu
, NULL
);
162 g_hash_table_destroy(menu_hash
);
165 static Window
createWindow(Window parent
, unsigned long mask
,
166 XSetWindowAttributes
*attrib
)
168 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
169 RrDepth(ob_rr_inst
), InputOutput
,
170 RrVisual(ob_rr_inst
), mask
, attrib
);
174 ObMenu
*menu_new_full(char *label
, char *name
, ObMenu
*parent
,
175 menu_controller_show show
, menu_controller_update update
,
176 menu_controller_selected selected
,
177 menu_controller_hide hide
,
178 menu_controller_mouseover mouseover
)
180 XSetWindowAttributes attrib
;
183 self
= g_new0(ObMenu
, 1);
184 self
->obwin
.type
= Window_Menu
;
185 self
->label
= g_strdup(label
);
186 self
->name
= g_strdup(name
);
187 self
->parent
= parent
;
188 self
->open_submenu
= NULL
;
191 self
->entries
= NULL
;
193 self
->invalid
= TRUE
;
195 /* default controllers */
196 self
->show
= (show
!= NULL
? show
: menu_show_full
);
197 self
->hide
= (hide
!= NULL
? hide
: menu_hide
);
198 self
->update
= (update
!= NULL
? update
: menu_render
);
199 self
->mouseover
= (mouseover
!= NULL
? mouseover
:
200 menu_control_mouseover
);
201 self
->selected
= (selected
!= NULL
? selected
: menu_entry_fire
);
204 self
->plugin_data
= NULL
;
206 attrib
.override_redirect
= TRUE
;
207 attrib
.event_mask
= FRAME_EVENTMASK
;
208 self
->frame
= createWindow(RootWindow(ob_display
, ob_screen
),
209 CWOverrideRedirect
|CWEventMask
, &attrib
);
210 attrib
.event_mask
= TITLE_EVENTMASK
;
211 self
->title
= createWindow(self
->frame
, CWEventMask
, &attrib
);
212 self
->items
= createWindow(self
->frame
, 0, &attrib
);
214 self
->a_title
= self
->a_items
= NULL
;
216 XMapWindow(ob_display
, self
->title
);
217 XMapWindow(ob_display
, self
->items
);
219 g_hash_table_insert(window_map
, &self
->frame
, self
);
220 g_hash_table_insert(window_map
, &self
->title
, self
);
221 g_hash_table_insert(window_map
, &self
->items
, self
);
222 g_hash_table_insert(menu_hash
, g_strdup(name
), self
);
224 stacking_add(MENU_AS_WINDOW(self
));
225 stacking_raise(MENU_AS_WINDOW(self
));
230 void menu_free(char *name
)
232 g_hash_table_remove(menu_hash
, name
);
235 ObMenuEntry
*menu_entry_new_full(char *label
, Action
*action
,
236 ObMenuEntryRenderType render_type
,
239 ObMenuEntry
*menu_entry
= g_new0(ObMenuEntry
, 1);
241 menu_entry
->label
= g_strdup(label
);
242 menu_entry
->render_type
= render_type
;
243 menu_entry
->action
= action
;
245 menu_entry
->hilite
= FALSE
;
246 menu_entry
->enabled
= TRUE
;
248 menu_entry
->submenu
= submenu
;
253 void menu_entry_set_submenu(ObMenuEntry
*entry
, ObMenu
*submenu
)
255 g_assert(entry
!= NULL
);
257 entry
->submenu
= submenu
;
259 if(entry
->parent
!= NULL
)
260 entry
->parent
->invalid
= TRUE
;
263 void menu_add_entry(ObMenu
*menu
, ObMenuEntry
*entry
)
265 XSetWindowAttributes attrib
;
267 g_assert(menu
!= NULL
);
268 g_assert(entry
!= NULL
);
269 g_assert(entry
->item
== None
);
271 menu
->entries
= g_list_append(menu
->entries
, entry
);
272 entry
->parent
= menu
;
274 attrib
.event_mask
= ENTRY_EVENTMASK
;
275 entry
->item
= createWindow(menu
->items
, CWEventMask
, &attrib
);
276 XMapWindow(ob_display
, entry
->item
);
278 entry
->a_item
= entry
->a_disabled
= entry
->a_hilite
= NULL
;
280 menu
->invalid
= TRUE
;
282 g_hash_table_insert(window_map
, &entry
->item
, menu
);
285 void menu_show(char *name
, int x
, int y
, ObClient
*client
)
289 self
= g_hash_table_lookup(menu_hash
, name
);
291 g_warning("Attempted to show menu '%s' but it does not exist.",
296 menu_show_full(self
, x
, y
, client
);
299 void menu_show_full(ObMenu
*self
, int x
, int y
, ObClient
*client
)
301 g_assert(self
!= NULL
);
305 self
->client
= client
;
308 if (!(self
->parent
&& self
->parent
->shown
)) {
309 grab_pointer(TRUE
, None
);
312 menu_visible
= g_list_append(menu_visible
, self
);
315 menu_control_show(self
, x
, y
, client
);
318 void menu_hide(ObMenu
*self
) {
320 XUnmapWindow(ob_display
, self
->frame
);
322 if (self
->open_submenu
)
323 self
->open_submenu
->hide(self
->open_submenu
);
324 if (self
->parent
&& self
->parent
->open_submenu
== self
) {
325 self
->parent
->open_submenu
= NULL
;
328 if (!(self
->parent
&& self
->parent
->shown
)) {
329 grab_keyboard(FALSE
);
330 grab_pointer(FALSE
, None
);
332 menu_visible
= g_list_remove(menu_visible
, self
);
334 ((ObMenuEntry
*)self
->over
->data
)->hilite
= FALSE
;
335 menu_entry_render(self
->over
->data
);
341 void menu_clear(ObMenu
*self
) {
344 for (it
= self
->entries
; it
; it
= it
->next
) {
345 ObMenuEntry
*entry
= it
->data
;
346 menu_entry_free(entry
);
348 self
->entries
= NULL
;
349 self
->invalid
= TRUE
;
353 ObMenuEntry
*menu_find_entry(ObMenu
*menu
, Window win
)
357 for (it
= menu
->entries
; it
; it
= it
->next
) {
358 ObMenuEntry
*entry
= it
->data
;
359 if (entry
->item
== win
)
365 ObMenuEntry
*menu_find_entry_by_submenu(ObMenu
*menu
, ObMenu
*submenu
)
369 for (it
= menu
->entries
; it
; it
= it
->next
) {
370 ObMenuEntry
*entry
= it
->data
;
371 if (entry
->submenu
== submenu
)
377 ObMenuEntry
*menu_find_entry_by_pos(ObMenu
*menu
, int x
, int y
)
379 if (x
< 0 || x
>= menu
->size
.width
|| y
< 0 || y
>= menu
->size
.height
)
382 y
-= menu
->title_h
+ ob_rr_theme
->bwidth
;
383 if (y
< 0) return NULL
;
385 ob_debug("%d %p\n", y
/menu
->item_h
,
386 g_list_nth_data(menu
->entries
, y
/ menu
->item_h
));
387 return g_list_nth_data(menu
->entries
, y
/ menu
->item_h
);
390 void menu_entry_fire(ObMenuEntry
*self
, unsigned int button
, unsigned int x
,
395 /* ignore wheel scrolling */
396 if (button
== 4 || button
== 5) return;
399 self
->action
->data
.any
.c
= self
->parent
->client
;
400 self
->action
->func(&self
->action
->data
);
402 /* hide the whole thing */
404 while (m
->parent
) m
= m
->parent
;
410 Default menu controller action for showing.
413 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
)
418 g_assert(!self
->invalid
);
420 for (i
= 0; i
< screen_num_monitors
; ++i
) {
421 a
= screen_physical_area_monitor(i
);
422 if (RECT_CONTAINS(*a
, x
, y
))
428 POINT_SET(self
->location
,
429 MIN(x
, a
->x
+ a
->width
- 1 - self
->size
.width
),
430 MIN(y
, a
->y
+ a
->height
- 1 - self
->size
.height
));
431 XMoveWindow(ob_display
, self
->frame
, self
->location
.x
, self
->location
.y
);
434 XMapWindow(ob_display
, self
->frame
);
435 stacking_raise(MENU_AS_WINDOW(self
));
437 } else if (self
->shown
&& self
->open_submenu
) {
438 self
->open_submenu
->hide(self
->open_submenu
);
442 void menu_control_mouseover(ObMenuEntry
*self
, gboolean enter
)
448 g_assert(self
!= NULL
);
451 /* TODO: we prolly don't need open_submenu */
452 if (self
->parent
->open_submenu
&& self
->submenu
453 != self
->parent
->open_submenu
)
455 e
= (ObMenuEntry
*) self
->parent
->over
->data
;
457 menu_entry_render(e
);
458 self
->parent
->open_submenu
->hide(self
->parent
->open_submenu
);
461 if (self
->submenu
&& self
->parent
->open_submenu
!= self
->submenu
) {
462 self
->parent
->open_submenu
= self
->submenu
;
464 /* shouldn't be invalid since it must be displayed */
465 g_assert(!self
->parent
->invalid
);
466 /* TODO: I don't understand why these bevels should be here.
467 Something must be wrong in the width calculation */
468 x
= self
->parent
->location
.x
+ self
->parent
->size
.width
+
469 ob_rr_theme
->bwidth
- ob_rr_theme
->menu_overlap
;
471 /* need to get the width. is this bad?*/
472 self
->parent
->update(self
->submenu
);
474 a
= screen_physical_area_monitor(self
->parent
->xin_area
);
476 if (self
->submenu
->size
.width
+ x
>= a
->x
+ a
->width
)
477 x
= self
->parent
->location
.x
- self
->submenu
->size
.width
-
478 ob_rr_theme
->bwidth
+ ob_rr_theme
->menu_overlap
;
480 menu_show_full(self
->submenu
, x
,
481 self
->parent
->location
.y
+ self
->y
,
482 self
->parent
->client
);
485 self
->parent
->over
= g_list_find(self
->parent
->entries
, self
);
488 self
->hilite
= FALSE
;
490 menu_entry_render(self
);
493 void menu_control_keyboard_nav(unsigned int key
)
495 static ObMenu
*current_menu
= NULL
;
496 ObMenuEntry
*e
= NULL
;
498 ObKey obkey
= OB_NUM_KEYS
;
500 /* hrmm. could be fixed */
501 if (key
== ob_keycode(OB_KEY_DOWN
))
503 else if (key
== ob_keycode(OB_KEY_UP
))
505 else if (key
== ob_keycode(OB_KEY_RIGHT
)) /* fuck */
506 obkey
= OB_KEY_RIGHT
;
507 else if (key
== ob_keycode(OB_KEY_LEFT
)) /* users */
509 else if (key
== ob_keycode(OB_KEY_RETURN
))
510 obkey
= OB_KEY_RETURN
;
513 if (current_menu
== NULL
)
514 current_menu
= menu_visible
->data
;
518 if (current_menu
->over
) {
519 current_menu
->mouseover(current_menu
->over
->data
, FALSE
);
520 current_menu
->over
= (current_menu
->over
->next
!= NULL
?
521 current_menu
->over
->next
:
522 current_menu
->entries
);
525 current_menu
->over
= current_menu
->entries
;
527 if (current_menu
->over
)
528 current_menu
->mouseover(current_menu
->over
->data
, TRUE
);
533 if (current_menu
->over
) {
534 current_menu
->mouseover(current_menu
->over
->data
, FALSE
);
535 current_menu
->over
= (current_menu
->over
->prev
!= NULL
?
536 current_menu
->over
->prev
:
537 g_list_last(current_menu
->entries
));
539 current_menu
->over
= g_list_last(current_menu
->entries
);
541 if (current_menu
->over
)
542 current_menu
->mouseover(current_menu
->over
->data
, TRUE
);
547 if (current_menu
->over
== NULL
)
549 e
= (ObMenuEntry
*)current_menu
->over
->data
;
551 current_menu
->mouseover(e
, TRUE
);
552 current_menu
= e
->submenu
;
553 current_menu
->over
= current_menu
->entries
;
554 if (current_menu
->over
)
555 current_menu
->mouseover(current_menu
->over
->data
, TRUE
);
560 case OB_KEY_RETURN
: {
561 if (current_menu
->over
== NULL
)
563 e
= (ObMenuEntry
*)current_menu
->over
->data
;
565 current_menu
->mouseover(e
, FALSE
);
566 current_menu
->over
= NULL
;
568 menu_entry_fire(e
, 0, 0, 0);
572 if (current_menu
->over
!= NULL
) {
573 current_menu
->mouseover(current_menu
->over
->data
, FALSE
);
574 current_menu
->over
= NULL
;
577 current_menu
->hide(current_menu
);
579 if (current_menu
->parent
)
580 current_menu
= current_menu
->parent
;
585 ((ObMenu
*)menu_visible
->data
)->hide(menu_visible
->data
);
593 /* This noop brought to you by OLS 2003 Email Garden. */