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