]> icculus.org git repositories - dana/openbox.git/blob - openbox/menu.c
fix brain damage in lineto
[dana/openbox.git] / openbox / menu.c
1 #include "menu.h"
2 #include "openbox.h"
3 #include "stacking.h"
4 #include "grab.h"
5 #include "screen.h"
6 #include "geom.h"
7 #include "plugin.h"
8
9 GHashTable *menu_hash = NULL;
10
11 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
12                          LeaveWindowMask | ExposureMask)
13 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask | ExposureMask)
14 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
15                          ButtonPressMask | ButtonReleaseMask | ExposureMask)
16
17 void menu_control_show(Menu *self, int x, int y, Client *client);
18
19 void menu_destroy_hash_key(Menu *menu)
20 {
21     g_free(menu);
22 }
23
24 void menu_destroy_hash_value(Menu *self)
25 {
26     GList *it;
27
28     for (it = self->entries; it; it = it->next)
29         menu_entry_free(it->data);
30     g_list_free(self->entries);
31
32     g_free(self->label);
33     g_free(self->name);
34
35     g_hash_table_remove(window_map, &self->w_title);
36     g_hash_table_remove(window_map, &self->w_frame);
37     g_hash_table_remove(window_map, &self->w_items);
38
39     stacking_remove(self);
40
41     RrSurfaceFree(self->s_title);
42     RrSurfaceFree(self->s_items);
43     RrSurfaceFree(self->s_frame);
44
45     XDestroyWindow(ob_display, self->w_frame);
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->w_item);
56     g_hash_table_remove(window_map, &self->w_disabled);
57     g_hash_table_remove(window_map, &self->w_hilite);
58
59     RrSurfaceFree(self->s_item);
60     RrSurfaceFree(self->s_disabled);
61     RrSurfaceFree(self->s_hilite);
62
63     g_free(self);
64 }
65     
66 void menu_startup()
67 {
68     Menu *m;
69 /*
70     Menu *s;
71     Menu *t;
72 */
73     Action *a;
74
75     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
76                                       (GDestroyNotify)menu_destroy_hash_key,
77                                       (GDestroyNotify)menu_destroy_hash_value);
78
79     m = menu_new("sex menu", "root", NULL);
80  
81     a = action_from_string("execute");
82     a->data.execute.path = g_strdup("xterm");
83     menu_add_entry(m, menu_entry_new("xterm", a));
84     a = action_from_string("restart");
85     menu_add_entry(m, menu_entry_new("restart", a));
86     menu_add_entry(m, menu_entry_new_separator("--"));
87     a = action_from_string("exit");
88     menu_add_entry(m, menu_entry_new("exit", a));
89
90     /*
91     s = menu_new("subsex menu", "submenu", m);
92     a = action_from_string("execute");
93     a->data.execute.path = g_strdup("xclock");
94     menu_add_entry(s, menu_entry_new("xclock", a));
95
96     menu_add_entry(m, menu_entry_new_submenu("subz", s));
97
98     s = menu_new("empty", "chub", m);
99     menu_add_entry(m, menu_entry_new_submenu("empty", s));
100
101     s = menu_new("", "s-club", m);
102     menu_add_entry(m, menu_entry_new_submenu("empty", s));
103
104     s = menu_new(NULL, "h-club", m);
105     menu_add_entry(m, menu_entry_new_submenu("empty", s));
106
107     s = menu_new(NULL, "g-club", m);
108
109     a = action_from_string("execute");
110     a->data.execute.path = g_strdup("xterm");
111     menu_add_entry(s, menu_entry_new("xterm", a));
112     a = action_from_string("restart");
113     menu_add_entry(s, menu_entry_new("restart", a));
114     menu_add_entry(s, menu_entry_new_separator("--"));
115     a = action_from_string("exit");
116     menu_add_entry(s, menu_entry_new("exit", a));
117
118     menu_add_entry(m, menu_entry_new_submenu("long", s));
119     */
120 }
121
122 void menu_shutdown()
123 {
124     g_hash_table_destroy(menu_hash);
125 }
126
127 static Window createWindow(Window parent, unsigned long mask,
128                            XSetWindowAttributes *attrib)
129 {
130     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
131                          RrInstanceDepth(ob_render_inst), InputOutput,
132                          RrInstanceVisual(ob_render_inst),
133                          mask, attrib);
134                        
135 }
136
137 Menu *menu_new_full(char *label, char *name, Menu *parent, 
138                     menu_controller_show show, menu_controller_update update)
139 {
140     XSetWindowAttributes attrib;
141     Menu *self;
142
143     self = g_new0(Menu, 1);
144     self->obwin.type = Window_Menu;
145     self->label = g_strdup(label);
146     self->name = g_strdup(name);
147     self->parent = parent;
148     self->open_submenu = NULL;
149
150     self->entries = NULL;
151     self->shown = FALSE;
152     self->invalid = TRUE;
153
154     /* default controllers */
155     self->show = show;
156     self->hide = NULL;
157     self->update = update;
158     self->mouseover = NULL;
159     self->selected = NULL;
160
161     self->plugin = NULL;
162     self->plugin_data = NULL;
163
164     attrib.override_redirect = TRUE;
165     attrib.event_mask = FRAME_EVENTMASK;
166     self->w_frame = createWindow(ob_root, CWOverrideRedirect|CWEventMask,
167                                  &attrib),
168     self->s_frame = RrSurfaceNew(ob_render_inst, RR_SURFACE_PLANAR,
169                                  self->w_frame, 0);
170                                  
171     /* XXX COPY THESE FROM THE THEME@&#*(&!# */
172     self->s_title = RrSurfaceNewChild(RR_SURFACE_PLANAR, self->s_frame, 1);
173     self->w_title = RrSurfaceWindow(self->s_title);
174     self->s_items = RrSurfaceNewChild(RR_SURFACE_PLANAR, self->s_frame, 0);
175     self->w_items = RrSurfaceWindow(self->s_items);
176     /* XXX LIKE THIS SORTA... 
177     self->a_title = appearance_copy(theme_a_menu_title);
178     self->a_items = appearance_copy(theme_a_menu);
179     */
180
181     /* event masks */
182     XSelectInput(ob_display, self->w_title, TITLE_EVENTMASK);
183
184     g_hash_table_insert(window_map, &self->w_frame, self);
185     g_hash_table_insert(window_map, &self->w_title, self);
186     g_hash_table_insert(window_map, &self->w_items, self);
187     g_hash_table_insert(menu_hash, g_strdup(name), self);
188
189     stacking_add(MENU_AS_WINDOW(self));
190     stacking_raise(MENU_AS_WINDOW(self));
191
192     return self;
193 }
194
195 void menu_free(char *name)
196 {
197     g_hash_table_remove(menu_hash, name);
198 }
199
200 MenuEntry *menu_entry_new_full(char *label, Action *action,
201                                MenuEntryRenderType render_type,
202                                gpointer submenu)
203 {
204     MenuEntry *menu_entry = g_new0(MenuEntry, 1);
205
206     menu_entry->label = g_strdup(label);
207     menu_entry->render_type = render_type;
208     menu_entry->action = action;
209
210     menu_entry->hilite = FALSE;
211     menu_entry->enabled = TRUE;
212
213     menu_entry->submenu = submenu;
214
215     return menu_entry;
216 }
217
218 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
219 {
220     g_assert(entry != NULL);
221     
222     entry->submenu = submenu;
223
224     if(entry->parent != NULL)
225         entry->parent->invalid = TRUE;
226 }
227
228 void menu_add_entry(Menu *menu, MenuEntry *entry)
229 {
230     XSetWindowAttributes attrib;
231
232     g_assert(menu != NULL);
233     g_assert(entry != NULL);
234     g_assert(entry->s_item == NULL);
235
236     menu->entries = g_list_append(menu->entries, entry);
237     entry->parent = menu;
238
239     attrib.event_mask = ENTRY_EVENTMASK;
240     /* XXX COPY FROM THEME SURFACES @#&!#&*!@ */
241     entry->s_item = RrSurfaceNewChild(RR_SURFACE_PLANAR, menu->s_items, 1);
242     entry->w_item = RrSurfaceWindow(entry->s_item);
243     entry->s_disabled = RrSurfaceNewChild(RR_SURFACE_PLANAR, menu->s_items,1);
244     entry->w_disabled = RrSurfaceWindow(entry->s_disabled);
245     entry->s_hilite = RrSurfaceNewChild(RR_SURFACE_PLANAR, menu->s_items, 1);
246     entry->w_hilite = RrSurfaceWindow(entry->s_hilite);
247     /* XXX LIKE THIS SORTA....
248     entry->a_item = appearance_copy(theme_a_menu_item);
249     entry->a_disabled = appearance_copy(theme_a_menu_disabled);
250     entry->a_hilite = appearance_copy(theme_a_menu_hilite);
251     */
252
253     /* event masks */
254     XSelectInput(ob_display, entry->w_item, ENTRY_EVENTMASK);
255     XSelectInput(ob_display, entry->w_disabled, ENTRY_EVENTMASK);
256     XSelectInput(ob_display, entry->w_hilite, ENTRY_EVENTMASK);
257
258     menu->invalid = TRUE;
259
260     g_hash_table_insert(window_map, &entry->w_item, menu);
261     g_hash_table_insert(window_map, &entry->w_disabled, menu);
262     g_hash_table_insert(window_map, &entry->w_hilite, menu);
263 }
264
265 void menu_show(char *name, int x, int y, Client *client)
266 {
267     Menu *self;
268   
269     self = g_hash_table_lookup(menu_hash, name);
270     if (!self) {
271         g_warning("Attempted to show menu '%s' but it does not exist.",
272                   name);
273         return;
274     }
275     
276     menu_show_full(self, x, y, client);
277 }  
278
279 void menu_show_full(Menu *self, int x, int y, Client *client)
280 {
281     g_assert(self != NULL);
282        
283     menu_render(self);
284     
285     self->client = client;
286
287     if (self->show) {
288         self->show(self, x, y, client);
289     } else {
290       menu_control_show(self, x, y, client);
291     }
292 }
293
294 void menu_hide(Menu *self) {
295     if (self->shown) {
296         RrSurfaceHide(self->s_frame);
297         self->shown = FALSE;
298         if (self->open_submenu)
299             menu_hide(self->open_submenu);
300         if (self->parent && self->parent->open_submenu == self)
301             self->parent->open_submenu = NULL;
302
303     }
304 }
305
306 void menu_clear(Menu *self) {
307     GList *it;
308   
309     for (it = self->entries; it; it = it->next) {
310         MenuEntry *entry = it->data;
311         menu_entry_free(entry);
312     }
313     self->entries = NULL;
314     self->invalid = TRUE;
315 }
316
317
318 MenuEntry *menu_find_entry(Menu *menu, Window win)
319 {
320     GList *it;
321
322     for (it = menu->entries; it; it = it->next) {
323         MenuEntry *entry = it->data;
324         if (entry->w_item == win ||
325             entry->w_disabled == win ||
326             entry->w_hilite == win)
327             return entry;
328     }
329     return NULL;
330 }
331
332 void menu_entry_fire(MenuEntry *self)
333 {
334     Menu *m;
335
336     if (self->action) {
337         self->action->data.any.c = self->parent->client;
338         self->action->func(&self->action->data);
339
340         /* hide the whole thing */
341         m = self->parent;
342         while (m->parent) m = m->parent;
343         menu_hide(m);
344     }
345 }
346
347 /* 
348    Default menu controller action for showing.
349 */
350
351 void menu_control_show(Menu *self, int x, int y, Client *client) {
352     g_assert(!self->invalid);
353
354     POINT_SET(self->location, x, y);
355     menu_render(self);
356
357     if (!self->shown) {
358         RrSurfaceShow(self->s_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
384             /* need to get the width. is this bad?*/
385             menu_render(self->submenu);
386
387             if (self->submenu->size.width + x > screen_physical_size.width)
388                 x = self->parent->location.x - self->submenu->size.width;
389             
390             menu_show_full(self->submenu, x,
391                            self->parent->location.y + self->y,
392                            self->parent->client);
393         } 
394     }
395 }