10 GHashTable
*menu_hash
= NULL
;
11 GSList
*menu_visible
= NULL
;
13 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask | \
15 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
16 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
17 ButtonPressMask | ButtonReleaseMask)
19 static void parse_menu(xmlDocPtr doc
, xmlNodePtr node
, void *data
)
21 parse_menu_full(doc
, node
, data
, TRUE
);
25 void parse_menu_full(xmlDocPtr doc
, xmlNodePtr node
, void *data
,
31 gchar
*id
= NULL
, *title
= NULL
, *label
= NULL
, *plugin
;
32 ObMenu
*menu
= NULL
, *parent
;
34 if (newmenu
== TRUE
) {
35 if (!parse_attr_string("id", node
->parent
, &id
))
37 if (!parse_attr_string("label", node
->parent
, &title
))
40 g_message("menu label %s", title
);
42 menu
= menu_new(title
, id
, data
? *((ObMenu
**)data
) : NULL
);
45 *((ObMenu
**)data
) = menu
;
47 menu
= (ObMenu
*)data
;
51 if (!xmlStrcasecmp(node
->name
, (const xmlChar
*) "menu")) {
52 if (parse_attr_string("plugin", node
, &plugin
)) {
53 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
;
341 grab_pointer(TRUE
, None
);
344 menu_visible
= g_slist_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
;
364 grab_keyboard(FALSE
);
365 grab_pointer(FALSE
, None
);
367 menu_visible
= g_slist_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_pos(ObMenu
*menu
, int x
, int y
)
397 if (x
< 0 || x
>= menu
->size
.width
|| y
< 0 || y
>= menu
->size
.height
)
400 y
-= menu
->title_h
+ ob_rr_theme
->bwidth
;
401 if (y
< 0) return NULL
;
403 g_message ("%d %p", y
/menu
->item_h
, g_list_nth_data(menu
->entries
, y
/ menu
->item_h
));
404 return g_list_nth_data(menu
->entries
, y
/ menu
->item_h
);
407 void menu_entry_fire(ObMenuEntry
*self
)
412 self
->action
->data
.any
.c
= self
->parent
->client
;
413 self
->action
->func(&self
->action
->data
);
415 /* hide the whole thing */
417 while (m
->parent
) m
= m
->parent
;
423 Default menu controller action for showing.
426 void menu_control_show(ObMenu
*self
, int x
, int y
, ObClient
*client
) {
430 g_assert(!self
->invalid
);
432 for (i
= 0; i
< screen_num_monitors
; ++i
) {
433 a
= screen_physical_area_monitor(i
);
434 if (RECT_CONTAINS(*a
, x
, y
))
440 POINT_SET(self
->location
,
441 MIN(x
, a
->x
+ a
->width
- 1 - self
->size
.width
),
442 MIN(y
, a
->y
+ a
->height
- 1 - self
->size
.height
));
443 XMoveWindow(ob_display
, self
->frame
, self
->location
.x
, self
->location
.y
);
446 XMapWindow(ob_display
, self
->frame
);
447 stacking_raise(MENU_AS_WINDOW(self
));
449 } else if (self
->shown
&& self
->open_submenu
) {
450 menu_hide(self
->open_submenu
);
454 void menu_control_mouseover(ObMenuEntry
*self
, gboolean enter
) {
458 self
->hilite
= enter
;
461 if (self
->parent
->open_submenu
&& self
->submenu
462 != self
->parent
->open_submenu
)
463 menu_hide(self
->parent
->open_submenu
);
465 if (self
->submenu
&& self
->parent
->open_submenu
!= self
->submenu
) {
466 self
->parent
->open_submenu
= self
->submenu
;
468 /* shouldn't be invalid since it must be displayed */
469 g_assert(!self
->parent
->invalid
);
470 /* TODO: I don't understand why these bevels should be here.
471 Something must be wrong in the width calculation */
472 x
= self
->parent
->location
.x
+ self
->parent
->size
.width
+
475 /* need to get the width. is this bad?*/
476 menu_render(self
->submenu
);
478 a
= screen_physical_area_monitor(self
->parent
->xin_area
);
480 if (self
->submenu
->size
.width
+ x
>= a
->x
+ a
->width
)
481 x
= self
->parent
->location
.x
- self
->submenu
->size
.width
-
484 menu_show_full(self
->submenu
, x
,
485 self
->parent
->location
.y
+ self
->y
,
486 self
->parent
->client
);