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