]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/menu.c
Clients Menus and Slits are all 'ObWindow's now.
[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(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     return self;
201 }
202
203 void menu_free(char *name)
204 {
205     g_hash_table_remove(menu_hash, name);
206 }
207
208 MenuEntry *menu_entry_new_full(char *label, Action *action,
209                                MenuEntryRenderType render_type,
210                                gpointer submenu)
211 {
212     MenuEntry *menu_entry = g_new0(MenuEntry, 1);
213
214     menu_entry->label = g_strdup(label);
215     menu_entry->render_type = render_type;
216     menu_entry->action = action;
217
218     menu_entry->hilite = FALSE;
219     menu_entry->enabled = TRUE;
220
221     menu_entry->submenu = submenu;
222
223     return menu_entry;
224 }
225
226 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
227 {
228     g_assert(entry != NULL);
229     
230     entry->submenu = submenu;
231
232     if(entry->parent != NULL)
233         entry->parent->invalid = TRUE;
234 }
235
236 void menu_add_entry(Menu *menu, MenuEntry *entry)
237 {
238     XSetWindowAttributes attrib;
239
240     g_assert(menu != NULL);
241     g_assert(entry != NULL);
242     g_assert(entry->item == None);
243
244     menu->entries = g_list_append(menu->entries, entry);
245     entry->parent = menu;
246
247     attrib.event_mask = ENTRY_EVENTMASK;
248     entry->item = createWindow(menu->items, CWEventMask, &attrib);
249     XMapWindow(ob_display, entry->item);
250     entry->a_item = appearance_copy(theme_a_menu_item);
251     entry->a_disabled = appearance_copy(theme_a_menu_disabled);
252     entry->a_hilite = appearance_copy(theme_a_menu_hilite);
253
254     menu->invalid = TRUE;
255
256     g_hash_table_insert(menu_map, &entry->item, menu);
257 }
258
259 void menu_show(char *name, int x, int y, Client *client)
260 {
261     Menu *self;
262   
263     self = g_hash_table_lookup(menu_hash, name);
264     if (!self) {
265         g_warning("Attempted to show menu '%s' but it does not exist.",
266                   name);
267         return;
268     }
269     
270     menu_show_full(self, x, y, client);
271 }  
272
273 void menu_show_full(Menu *self, int x, int y, Client *client)
274 {
275     g_assert(self != NULL);
276        
277     menu_render(self);
278     
279     self->client = client;
280
281     if (self->show) {
282         self->show(self, x, y, client);
283     } else {
284       menu_control_show(self, x, y, client);
285     }
286 }
287
288
289 void menu_hide(Menu *self) {
290     if (self->shown) {
291         XUnmapWindow(ob_display, self->frame);
292         self->shown = FALSE;
293         if (self->open_submenu)
294             menu_hide(self->open_submenu);
295         if (self->parent && self->parent->open_submenu == self)
296             self->parent->open_submenu = NULL;
297
298     }
299 }
300
301 void menu_clear(Menu *self) {
302     GList *it;
303   
304     for (it = self->entries; it; it = it->next) {
305         MenuEntry *entry = it->data;
306         menu_entry_free(entry);
307     }
308     self->entries = NULL;
309 }
310
311
312 MenuEntry *menu_find_entry(Menu *menu, Window win)
313 {
314     GList *it;
315
316     for (it = menu->entries; it; it = it->next) {
317         MenuEntry *entry = it->data;
318         if (entry->item == win)
319             return entry;
320     }
321     return NULL;
322 }
323
324 void menu_entry_fire(MenuEntry *self)
325 {
326     Menu *m;
327
328     if (self->action) {
329         self->action->data.any.c = self->parent->client;
330         self->action->func(&self->action->data);
331
332         /* hide the whole thing */
333         m = self->parent;
334         while (m->parent) m = m->parent;
335         menu_hide(m);
336     }
337 }
338
339 /* 
340    Default menu controller action for showing.
341 */
342
343 void menu_control_show(Menu *self, int x, int y, Client *client) {
344     g_assert(!self->invalid);
345     
346     XMoveWindow(ob_display, self->frame, 
347                 MIN(x, screen_physical_size.width - self->size.width), 
348                 MIN(y, screen_physical_size.height - self->size.height));
349     POINT_SET(self->location, 
350               MIN(x, screen_physical_size.width - self->size.width), 
351               MIN(y, screen_physical_size.height - self->size.height));
352
353     if (!self->shown) {
354         /* XXX gotta add to the stacking list first!
355            stacking_raise(MENU_AS_WINDOW(self));
356         */
357         XMapWindow(ob_display, self->frame);
358         self->shown = TRUE;
359     } else if (self->shown && self->open_submenu) {
360         menu_hide(self->open_submenu);
361     }
362 }
363
364 void menu_control_mouseover(MenuEntry *self, gboolean enter) {
365     int x;
366     self->hilite = enter;
367   
368     if (enter) {
369         if (self->parent->open_submenu && self->submenu 
370             != self->parent->open_submenu)
371             menu_hide(self->parent->open_submenu);
372         
373         if (self->submenu) {
374             self->parent->open_submenu = self->submenu;
375
376             /* shouldn't be invalid since it must be displayed */
377             g_assert(!self->parent->invalid);
378             /* TODO: I don't understand why these bevels should be here.
379                Something must be wrong in the width calculation */
380             x = self->parent->location.x + self->parent->size.width + 
381                 theme_bevel;
382
383             /* need to get the width. is this bad?*/
384             menu_render(self->submenu);
385
386             if (self->submenu->size.width + x > screen_physical_size.width)
387                 x = self->parent->location.x - self->submenu->size.width - 
388                     theme_bevel;
389             
390             menu_show_full(self->submenu, x,
391                            self->parent->location.y + self->y, NULL);
392         } 
393     }
394 }