]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/menu.c
Turn off timed_menu by default
[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 GHashTable *menu_map = NULL;
12
13 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask | \
14                          LeaveWindowMask)
15 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
16 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
17                          ButtonPressMask | ButtonReleaseMask)
18
19 void menu_control_show(Menu *self, int x, int y, Client *client);
20
21 void menu_destroy_hash_key(gpointer data)
22 {
23     g_free(data);
24 }
25
26 void menu_destroy_hash_value(Menu *self)
27 {
28     GList *it;
29
30     for (it = self->entries; it; it = it->next)
31         menu_entry_free(it->data);
32     g_list_free(self->entries);
33
34     g_free(self->label);
35     g_free(self->name);
36
37     g_hash_table_remove(menu_map, &self->title);
38     g_hash_table_remove(menu_map, &self->frame);
39     g_hash_table_remove(menu_map, &self->items);
40
41     appearance_free(self->a_title);
42     XDestroyWindow(ob_display, self->title);
43     XDestroyWindow(ob_display, self->frame);
44     XDestroyWindow(ob_display, self->items);
45
46     g_free(self);
47 }
48
49 void menu_entry_free(MenuEntry *self)
50 {
51     g_free(self->label);
52     action_free(self->action);
53
54     g_hash_table_remove(menu_map, &self->item);
55
56     appearance_free(self->a_item);
57     appearance_free(self->a_disabled);
58     appearance_free(self->a_hilite);
59     XDestroyWindow(ob_display, self->item);
60
61     g_free(self);
62 }
63     
64 void menu_startup()
65 {
66     Menu *m;
67     Menu *s;
68     Menu *t;
69     Action *a;
70
71     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
72                                       menu_destroy_hash_key,
73                                       (GDestroyNotify)menu_destroy_hash_value);
74     menu_map = g_hash_table_new(g_int_hash, g_int_equal);
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     /*
94     t = (Menu *)plugin_create("timed_menu");
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("client menu", "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     g_hash_table_destroy(menu_map);
139 }
140
141 static Window createWindow(Window parent, unsigned long mask,
142                            XSetWindowAttributes *attrib)
143 {
144     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
145                          render_depth, InputOutput, render_visual,
146                          mask, attrib);
147                        
148 }
149
150 Menu *menu_new_full(char *label, char *name, Menu *parent, 
151                     menu_controller_show show, menu_controller_update update)
152 {
153     XSetWindowAttributes attrib;
154     Menu *self;
155
156     self = g_new0(Menu, 1);
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(menu_map, &self->frame, self);
196     g_hash_table_insert(menu_map, &self->title, self);
197     g_hash_table_insert(menu_map, &self->items, self);
198     g_hash_table_insert(menu_hash, g_strdup(name), self);
199     return self;
200 }
201
202 void menu_free(char *name)
203 {
204     g_hash_table_remove(menu_hash, name);
205 }
206
207 MenuEntry *menu_entry_new_full(char *label, Action *action,
208                                MenuEntryRenderType render_type,
209                                gpointer submenu)
210 {
211     MenuEntry *menu_entry = g_new0(MenuEntry, 1);
212
213     menu_entry->label = g_strdup(label);
214     menu_entry->render_type = render_type;
215     menu_entry->action = action;
216
217     menu_entry->hilite = FALSE;
218     menu_entry->enabled = TRUE;
219
220     menu_entry->submenu = submenu;
221
222     return menu_entry;
223 }
224
225 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
226 {
227     g_assert(entry != NULL);
228     
229     entry->submenu = submenu;
230
231     if(entry->parent != NULL)
232         entry->parent->invalid = TRUE;
233 }
234
235 void menu_add_entry(Menu *menu, MenuEntry *entry)
236 {
237     XSetWindowAttributes attrib;
238
239     g_assert(menu != NULL);
240     g_assert(entry != NULL);
241     g_assert(entry->item == None);
242
243     menu->entries = g_list_append(menu->entries, entry);
244     entry->parent = menu;
245
246     attrib.event_mask = ENTRY_EVENTMASK;
247     entry->item = createWindow(menu->items, CWEventMask, &attrib);
248     XMapWindow(ob_display, entry->item);
249     entry->a_item = appearance_copy(theme_a_menu_item);
250     entry->a_disabled = appearance_copy(theme_a_menu_disabled);
251     entry->a_hilite = appearance_copy(theme_a_menu_hilite);
252
253     menu->invalid = TRUE;
254
255     g_hash_table_insert(menu_map, &entry->item, menu);
256 }
257
258 void menu_show(char *name, int x, int y, Client *client)
259 {
260     Menu *self;
261   
262     self = g_hash_table_lookup(menu_hash, name);
263     if (!self) {
264         g_warning("Attempted to show menu '%s' but it does not exist.",
265                   name);
266         return;
267     }
268     
269     menu_show_full(self, x, y, client);
270 }  
271
272 void menu_show_full(Menu *self, int x, int y, Client *client)
273 {
274     g_assert(self != NULL);
275        
276     menu_render(self);
277     
278     self->client = client;
279
280     if (self->show) {
281         self->show(self, x, y, client);
282     } else {
283       menu_control_show(self, x, y, client);
284     }
285 }
286
287
288 void menu_hide(Menu *self) {
289     if (self->shown) {
290         XUnmapWindow(ob_display, self->frame);
291         self->shown = FALSE;
292         if (self->open_submenu)
293             menu_hide(self->open_submenu);
294         if (self->parent && self->parent->open_submenu == self)
295             self->parent->open_submenu = NULL;
296
297     }
298 }
299
300 void menu_clear(Menu *self) {
301     GList *it;
302   
303     for (it = self->entries; it; it = it->next) {
304         MenuEntry *entry = it->data;
305         menu_entry_free(entry);
306     }
307     self->entries = NULL;
308 }
309
310
311 MenuEntry *menu_find_entry(Menu *menu, Window win)
312 {
313     GList *it;
314
315     for (it = menu->entries; it; it = it->next) {
316         MenuEntry *entry = it->data;
317         if (entry->item == win)
318             return entry;
319     }
320     return NULL;
321 }
322
323 void menu_entry_fire(MenuEntry *self)
324 {
325     Menu *m;
326
327     if (self->action) {
328         self->action->data.any.c = self->parent->client;
329         self->action->func(&self->action->data);
330
331         /* hide the whole thing */
332         m = self->parent;
333         while (m->parent) m = m->parent;
334         menu_hide(m);
335     }
336 }
337
338 /* 
339    Default menu controller action for showing.
340 */
341
342 void menu_control_show(Menu *self, int x, int y, Client *client) {
343     g_assert(!self->invalid);
344     
345     XMoveWindow(ob_display, self->frame, 
346                 MIN(x, screen_physical_size.width - self->size.width), 
347                 MIN(y, screen_physical_size.height - self->size.height));
348     POINT_SET(self->location, 
349               MIN(x, screen_physical_size.width - self->size.width), 
350               MIN(y, screen_physical_size.height - self->size.height));
351
352     if (!self->shown) {
353         stacking_raise_internal(self->frame);
354         XMapWindow(ob_display, self->frame);
355         self->shown = TRUE;
356     } else if (self->shown && self->open_submenu) {
357         menu_hide(self->open_submenu);
358     }
359 }
360
361 void menu_control_mouseover(MenuEntry *self, gboolean enter) {
362     int x;
363     self->hilite = enter;
364   
365     if (enter) {
366         if (self->parent->open_submenu && self->submenu 
367             != self->parent->open_submenu)
368             menu_hide(self->parent->open_submenu);
369         
370         if (self->submenu) {
371             self->parent->open_submenu = self->submenu;
372
373             /* shouldn't be invalid since it must be displayed */
374             g_assert(!self->parent->invalid);
375             /* TODO: I don't understand why these bevels should be here.
376                Something must be wrong in the width calculation */
377             x = self->parent->location.x + self->parent->size.width + 
378                 theme_bevel;
379
380             /* need to get the width. is this bad?*/
381             menu_render(self->submenu);
382
383             if (self->submenu->size.width + x > screen_physical_size.width)
384                 x = self->parent->location.x - self->submenu->size.width - 
385                     theme_bevel;
386             
387             menu_show_full(self->submenu, x,
388                            self->parent->location.y + self->y, NULL);
389         } 
390     }
391 }