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