]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/menu.c
add a simple menu parser.
[mikachu/openbox.git] / openbox / menu.c
1 #include "menu.h"
2 #include "openbox.h"
3 #include "stacking.h"
4 #include "grab.h"
5 #include "render/theme.h"
6 #include "screen.h"
7 #include "geom.h"
8 #include "plugin.h"
9
10 GHashTable *menu_hash = NULL;
11
12 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask | \
13                          LeaveWindowMask)
14 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
15 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
16                          ButtonPressMask | ButtonReleaseMask)
17
18 static void parse_menu(xmlDocPtr doc, xmlNodePtr node, void *data)
19 {
20     Action *act;
21     xmlNodePtr nact;
22     gchar *id = NULL, *title = NULL, *label = NULL;
23     Menu *menu, *parent;
24
25     if (!parse_attr_string("id", node->parent, &id))
26         goto parse_menu_fail;
27     if (!parse_attr_string("label", node->parent, &title))
28         goto parse_menu_fail;
29
30     g_message("menu label %s", title);
31
32     menu = menu_new(title, id, data ? *((Menu**)data) : NULL);
33     if (data)
34         *((Menu**)data) = menu;
35
36     while (node) {
37         if (!xmlStrcasecmp(node->name, (const xmlChar*) "menu")) {
38             parent = menu;
39             parse_menu(doc, node->xmlChildrenNode, &parent);
40             menu_add_entry(menu, menu_entry_new_submenu(parent->label,
41                                                         parent));
42         }
43         else if (!xmlStrcasecmp(node->name, (const xmlChar*) "item")) {
44             if (parse_attr_string("label", node, &label)) {
45                 if ((nact = parse_find_node("action", node->xmlChildrenNode)))
46                     act = action_parse(doc, nact);
47                 else
48                     act = NULL;
49                 if (act)
50                     menu_add_entry(menu, menu_entry_new(label, act));
51                 else
52                     menu_add_entry(menu, menu_entry_new_separator(label));
53                 g_free(label);
54             }
55         }
56         node = node->next;
57     }
58
59 parse_menu_fail:
60     g_free(id);
61     g_free(title);
62 }
63
64 void menu_control_show(Menu *self, int x, int y, Client *client);
65
66 void menu_destroy_hash_key(Menu *menu)
67 {
68     g_free(menu);
69 }
70
71 void menu_destroy_hash_value(Menu *self)
72 {
73     GList *it;
74
75     for (it = self->entries; it; it = it->next)
76         menu_entry_free(it->data);
77     g_list_free(self->entries);
78
79     g_free(self->label);
80     g_free(self->name);
81
82     g_hash_table_remove(window_map, &self->title);
83     g_hash_table_remove(window_map, &self->frame);
84     g_hash_table_remove(window_map, &self->items);
85
86     stacking_remove(self);
87
88     RrAppearanceFree(self->a_title);
89     XDestroyWindow(ob_display, self->title);
90     XDestroyWindow(ob_display, self->frame);
91     XDestroyWindow(ob_display, self->items);
92
93     g_free(self);
94 }
95
96 void menu_entry_free(MenuEntry *self)
97 {
98     g_free(self->label);
99     action_free(self->action);
100
101     g_hash_table_remove(window_map, &self->item);
102
103     RrAppearanceFree(self->a_item);
104     RrAppearanceFree(self->a_disabled);
105     RrAppearanceFree(self->a_hilite);
106     XDestroyWindow(ob_display, self->item);
107
108     g_free(self);
109 }
110     
111 void menu_startup()
112 {
113 /*
114     Menu *m;
115     Menu *s;
116     Menu *t;
117     Action *a;
118 */
119
120     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
121                                       (GDestroyNotify)menu_destroy_hash_key,
122                                       (GDestroyNotify)menu_destroy_hash_value);
123
124     parse_register("menu", parse_menu, NULL);
125
126 /*
127     m = menu_new("sex menu", "root", NULL);
128  
129     a = action_from_string("execute");
130     a->data.execute.path = g_strdup("xterm");
131     menu_add_entry(m, menu_entry_new("xterm", a));
132     a = action_from_string("restart");
133     menu_add_entry(m, menu_entry_new("restart", a));
134     menu_add_entry(m, menu_entry_new_separator("--"));
135     a = action_from_string("exit");
136     menu_add_entry(m, menu_entry_new("exit", a));
137 */
138
139     /*
140     s = menu_new("subsex menu", "submenu", m);
141     a = action_from_string("execute");
142     a->data.execute.path = g_strdup("xclock");
143     menu_add_entry(s, menu_entry_new("xclock", a));
144
145     menu_add_entry(m, menu_entry_new_submenu("subz", s));
146
147     s = menu_new("empty", "chub", m);
148     menu_add_entry(m, menu_entry_new_submenu("empty", s));
149
150     s = menu_new("", "s-club", m);
151     menu_add_entry(m, menu_entry_new_submenu("empty", s));
152
153     s = menu_new(NULL, "h-club", m);
154     menu_add_entry(m, menu_entry_new_submenu("empty", s));
155
156     s = menu_new(NULL, "g-club", m);
157
158     a = action_from_string("execute");
159     a->data.execute.path = g_strdup("xterm");
160     menu_add_entry(s, menu_entry_new("xterm", a));
161     a = action_from_string("restart");
162     menu_add_entry(s, menu_entry_new("restart", a));
163     menu_add_entry(s, menu_entry_new_separator("--"));
164     a = action_from_string("exit");
165     menu_add_entry(s, menu_entry_new("exit", a));
166
167     menu_add_entry(m, menu_entry_new_submenu("long", s));
168     */
169 }
170
171 void menu_shutdown()
172 {
173     g_hash_table_destroy(menu_hash);
174 }
175
176 static Window createWindow(Window parent, unsigned long mask,
177                            XSetWindowAttributes *attrib)
178 {
179     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
180                          RrDepth(ob_rr_inst), InputOutput,
181                          RrVisual(ob_rr_inst), mask, attrib);
182                        
183 }
184
185 Menu *menu_new_full(char *label, char *name, Menu *parent, 
186                     menu_controller_show show, menu_controller_update update)
187 {
188     XSetWindowAttributes attrib;
189     Menu *self;
190
191     self = g_new0(Menu, 1);
192     self->obwin.type = Window_Menu;
193     self->label = g_strdup(label);
194     self->name = g_strdup(name);
195     self->parent = parent;
196     self->open_submenu = NULL;
197
198     self->entries = NULL;
199     self->shown = FALSE;
200     self->invalid = TRUE;
201
202     /* default controllers */
203     self->show = show;
204     self->hide = NULL;
205     self->update = update;
206     self->mouseover = NULL;
207     self->selected = NULL;
208
209     self->plugin = NULL;
210     self->plugin_data = NULL;
211
212     attrib.override_redirect = TRUE;
213     attrib.event_mask = FRAME_EVENTMASK;
214     self->frame = createWindow(ob_root,
215                                CWOverrideRedirect|CWEventMask, &attrib);
216     attrib.event_mask = TITLE_EVENTMASK;
217     self->title = createWindow(self->frame, CWEventMask, &attrib);
218     self->items = createWindow(self->frame, 0, &attrib);
219
220     self->a_title = self->a_items = NULL;
221
222     XMapWindow(ob_display, self->title);
223     XMapWindow(ob_display, self->items);
224
225     g_hash_table_insert(window_map, &self->frame, self);
226     g_hash_table_insert(window_map, &self->title, self);
227     g_hash_table_insert(window_map, &self->items, self);
228     g_hash_table_insert(menu_hash, g_strdup(name), self);
229
230     stacking_add(MENU_AS_WINDOW(self));
231     stacking_raise(MENU_AS_WINDOW(self));
232
233     return self;
234 }
235
236 void menu_free(char *name)
237 {
238     g_hash_table_remove(menu_hash, name);
239 }
240
241 MenuEntry *menu_entry_new_full(char *label, Action *action,
242                                MenuEntryRenderType render_type,
243                                gpointer submenu)
244 {
245     MenuEntry *menu_entry = g_new0(MenuEntry, 1);
246
247     menu_entry->label = g_strdup(label);
248     menu_entry->render_type = render_type;
249     menu_entry->action = action;
250
251     menu_entry->hilite = FALSE;
252     menu_entry->enabled = TRUE;
253
254     menu_entry->submenu = submenu;
255
256     return menu_entry;
257 }
258
259 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
260 {
261     g_assert(entry != NULL);
262     
263     entry->submenu = submenu;
264
265     if(entry->parent != NULL)
266         entry->parent->invalid = TRUE;
267 }
268
269 void menu_add_entry(Menu *menu, MenuEntry *entry)
270 {
271     XSetWindowAttributes attrib;
272
273     g_assert(menu != NULL);
274     g_assert(entry != NULL);
275     g_assert(entry->item == None);
276
277     menu->entries = g_list_append(menu->entries, entry);
278     entry->parent = menu;
279
280     attrib.event_mask = ENTRY_EVENTMASK;
281     entry->item = createWindow(menu->items, CWEventMask, &attrib);
282     XMapWindow(ob_display, entry->item);
283
284     entry->a_item = entry->a_disabled = entry->a_hilite = NULL;
285
286     menu->invalid = TRUE;
287
288     g_hash_table_insert(window_map, &entry->item, menu);
289 }
290
291 void menu_show(char *name, int x, int y, Client *client)
292 {
293     Menu *self;
294   
295     self = g_hash_table_lookup(menu_hash, name);
296     if (!self) {
297         g_warning("Attempted to show menu '%s' but it does not exist.",
298                   name);
299         return;
300     }
301
302     menu_show_full(self, x, y, client);
303 }  
304
305 void menu_show_full(Menu *self, int x, int y, Client *client)
306 {
307     g_assert(self != NULL);
308        
309     menu_render(self);
310     
311     self->client = client;
312
313     if (self->show) {
314         self->show(self, x, y, client);
315     } else {
316       menu_control_show(self, x, y, client);
317     }
318 }
319
320 void menu_hide(Menu *self) {
321     if (self->shown) {
322         XUnmapWindow(ob_display, self->frame);
323         self->shown = FALSE;
324         if (self->open_submenu)
325             menu_hide(self->open_submenu);
326         if (self->parent && self->parent->open_submenu == self)
327             self->parent->open_submenu = NULL;
328
329     }
330 }
331
332 void menu_clear(Menu *self) {
333     GList *it;
334   
335     for (it = self->entries; it; it = it->next) {
336         MenuEntry *entry = it->data;
337         menu_entry_free(entry);
338     }
339     self->entries = NULL;
340     self->invalid = TRUE;
341 }
342
343
344 MenuEntry *menu_find_entry(Menu *menu, Window win)
345 {
346     GList *it;
347
348     for (it = menu->entries; it; it = it->next) {
349         MenuEntry *entry = it->data;
350         if (entry->item == win)
351             return entry;
352     }
353     return NULL;
354 }
355
356 void menu_entry_fire(MenuEntry *self)
357 {
358     Menu *m;
359
360     if (self->action) {
361         self->action->data.any.c = self->parent->client;
362         self->action->func(&self->action->data);
363
364         /* hide the whole thing */
365         m = self->parent;
366         while (m->parent) m = m->parent;
367         menu_hide(m);
368     }
369 }
370
371 /* 
372    Default menu controller action for showing.
373 */
374
375 void menu_control_show(Menu *self, int x, int y, Client *client) {
376     g_assert(!self->invalid);
377     
378     XMoveWindow(ob_display, self->frame, 
379                 MIN(x, screen_physical_size.width - self->size.width), 
380                 MIN(y, screen_physical_size.height - self->size.height));
381     POINT_SET(self->location, 
382               MIN(x, screen_physical_size.width - self->size.width), 
383               MIN(y, screen_physical_size.height - self->size.height));
384
385     if (!self->shown) {
386         XMapWindow(ob_display, self->frame);
387         stacking_raise(MENU_AS_WINDOW(self));
388         self->shown = TRUE;
389     } else if (self->shown && self->open_submenu) {
390         menu_hide(self->open_submenu);
391     }
392 }
393
394 void menu_control_mouseover(MenuEntry *self, gboolean enter) {
395     int x;
396     self->hilite = enter;
397   
398     if (enter) {
399         if (self->parent->open_submenu && self->submenu 
400             != self->parent->open_submenu)
401             menu_hide(self->parent->open_submenu);
402         
403         if (self->submenu) {
404             self->parent->open_submenu = self->submenu;
405
406             /* shouldn't be invalid since it must be displayed */
407             g_assert(!self->parent->invalid);
408             /* TODO: I don't understand why these bevels should be here.
409                Something must be wrong in the width calculation */
410             x = self->parent->location.x + self->parent->size.width + 
411                 ob_rr_theme->bevel;
412
413             /* need to get the width. is this bad?*/
414             menu_render(self->submenu);
415
416             if (self->submenu->size.width + x > screen_physical_size.width)
417                 x = self->parent->location.x - self->submenu->size.width - 
418                     ob_rr_theme->bevel;
419             
420             menu_show_full(self->submenu, x,
421                            self->parent->location.y + self->y,
422                            self->parent->client);
423         } 
424     }
425 }