42a183aba775a3eb393d1fd3256f014b6c337dfa
[dana/openbox-history.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
7 static GHashTable *menu_hash = NULL;
8 GHashTable *menu_map = NULL;
9
10 #define FRAME_EVENTMASK (ButtonMotionMask | EnterWindowMask | LeaveWindowMask)
11 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
12 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
13                          ButtonPressMask | ButtonReleaseMask)
14
15 void menu_control_show(Menu *self, int x, int y, Client *client);
16
17 void menu_destroy_hash_key(gpointer data)
18 {
19     g_free(data);
20 }
21
22 void menu_destroy_hash_value(Menu *self)
23 {
24     GList *it;
25
26     for (it = self->entries; it; it = it->next)
27         menu_entry_free(it->data);
28     g_list_free(self->entries);
29
30     g_free(self->label);
31     g_free(self->name);
32
33     g_hash_table_remove(menu_map, &self->title);
34     g_hash_table_remove(menu_map, &self->frame);
35     g_hash_table_remove(menu_map, &self->items);
36
37     appearance_free(self->a_title);
38     XDestroyWindow(ob_display, self->title);
39     XDestroyWindow(ob_display, self->frame);
40     XDestroyWindow(ob_display, self->items);
41
42     g_free(self);
43 }
44
45 void menu_entry_free(MenuEntry *self)
46 {
47     g_free(self->label);
48     action_free(self->action);
49
50     g_hash_table_remove(menu_map, &self->item);
51
52     appearance_free(self->a_item);
53     appearance_free(self->a_disabled);
54     appearance_free(self->a_hilite);
55     XDestroyWindow(ob_display, self->item);
56
57     g_free(self);
58 }
59     
60 void menu_startup()
61 {
62     Menu *m;
63     Action *a;
64
65     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
66                                       menu_destroy_hash_key,
67                                       (GDestroyNotify)menu_destroy_hash_value);
68     menu_map = g_hash_table_new(g_int_hash, g_int_equal);
69
70     m = menu_new("sex menu", "root", NULL);
71     a = action_from_string("execute");
72     a->data.execute.path = g_strdup("xterm");
73     menu_add_entry(m, menu_entry_new("xterm", a));
74     a = action_from_string("restart");
75     menu_add_entry(m, menu_entry_new("restart", a));
76     menu_add_entry(m, menu_entry_new("--", NULL));
77     a = action_from_string("exit");
78     menu_add_entry(m, menu_entry_new("exit", a));
79 }
80
81 void menu_shutdown()
82 {
83     g_hash_table_destroy(menu_hash);
84     g_hash_table_destroy(menu_map);
85 }
86
87 static Window createWindow(Window parent, unsigned long mask,
88                            XSetWindowAttributes *attrib)
89 {
90     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
91                          render_depth, InputOutput, render_visual,
92                          mask, attrib);
93                        
94 }
95
96 Menu *menu_new_full(char *label, char *name, Menu *parent, 
97                     menu_controller_show show, menu_controller_update update)
98 {
99     XSetWindowAttributes attrib;
100     Menu *self;
101
102     self = g_new0(Menu, 1);
103     self->label = g_strdup(label);
104     self->name = g_strdup(name);
105     self->parent = parent;
106
107     self->entries = NULL;
108     self->shown = FALSE;
109     self->invalid = FALSE;
110     /* default controllers? */
111     
112     self->show = show;
113     self->hide = NULL;
114     self->update = update;
115     self->mouseover = NULL;
116     self->selected = NULL;
117
118     attrib.override_redirect = TRUE;
119     attrib.event_mask = FRAME_EVENTMASK;
120     self->frame = createWindow(ob_root, CWOverrideRedirect|CWEventMask, &attrib);
121     attrib.event_mask = TITLE_EVENTMASK;
122     self->title = createWindow(self->frame, CWEventMask, &attrib);
123     self->items = createWindow(self->frame, 0, &attrib);
124
125     XSetWindowBorderWidth(ob_display, self->frame, theme_bwidth);
126     XSetWindowBorderWidth(ob_display, self->title, theme_bwidth);
127     XSetWindowBorder(ob_display, self->frame, theme_b_color->pixel);
128     XSetWindowBorder(ob_display, self->title, theme_b_color->pixel);
129
130     XMapWindow(ob_display, self->title);
131     XMapWindow(ob_display, self->items);
132
133     self->a_title = appearance_copy(theme_a_menu_title);
134     self->a_items = appearance_copy(theme_a_menu);
135
136     g_hash_table_insert(menu_map, &self->frame, self);
137     g_hash_table_insert(menu_map, &self->title, self);
138     g_hash_table_insert(menu_map, &self->items, self);
139     g_hash_table_insert(menu_hash, g_strdup(name), self);
140     return self;
141 }
142
143 void menu_free(char *name)
144 {
145     g_hash_table_remove(menu_hash, name);
146 }
147
148 MenuEntry *menu_entry_new_full(char *label, Action *action,
149                                MenuEntryRenderType render_type,
150                                gpointer submenu)
151 {
152     MenuEntry *menu_entry = g_new0(MenuEntry, 1);
153
154     menu_entry->label = g_strdup(label);
155     menu_entry->render_type = render_type;
156     menu_entry->action = action;
157
158     menu_entry->hilite = FALSE;
159     menu_entry->enabled = TRUE;
160
161     menu_entry->submenu = submenu;
162
163     return menu_entry;
164 }
165
166 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
167 {
168     g_assert(entry != NULL);
169     
170     entry->submenu = submenu;
171
172     if(entry->parent != NULL)
173         entry->parent->invalid = TRUE;
174 }
175
176 void menu_add_entry(Menu *menu, MenuEntry *entry)
177 {
178     XSetWindowAttributes attrib;
179
180     g_assert(menu != NULL && entry != NULL && entry->item == None);
181
182     menu->entries = g_list_append(menu->entries, entry);
183     entry->parent = menu;
184
185     attrib.event_mask = ENTRY_EVENTMASK;
186     entry->item = createWindow(menu->items, CWEventMask, &attrib);
187     XMapWindow(ob_display, entry->item);
188     entry->a_item = appearance_copy(theme_a_menu_item);
189     entry->a_disabled = appearance_copy(theme_a_menu_disabled);
190     entry->a_hilite = appearance_copy(theme_a_menu_hilite);
191
192     menu->invalid = TRUE;
193
194     g_hash_table_insert(menu_map, &entry->item, menu);
195 }
196
197 void menu_show(char *name, int x, int y, Client *client)
198 {
199     Menu *self;
200
201     self = g_hash_table_lookup(menu_hash, name);
202     if (!self) {
203         g_warning("Attempted to show menu '%s' but it does not exist.",
204                   name);
205         return;
206     }
207
208     if (self->invalid) {
209       if (self->update) {
210         self->update(self);
211       } else {
212         menu_render(self);
213       }
214     }
215     
216     self->client = client;
217
218     if (self->show) {
219       self->show(self, x, y, client);
220     } else {
221       menu_control_show(self, x, y, client);
222     }
223 }
224
225 void menu_hide(Menu *self) {
226     if (self->shown) {
227         XUnmapWindow(ob_display, self->frame);
228         self->shown = FALSE;
229     }
230 }
231
232 MenuEntry *menu_find_entry(Menu *menu, Window win)
233 {
234     GList *it;
235
236     for (it = menu->entries; it; it = it->next) {
237         MenuEntry *entry = it->data;
238         if (entry->item == win)
239             return entry;
240     }
241     return NULL;
242 }
243
244 void menu_entry_render(MenuEntry *self)
245 {
246     Menu *menu = self->parent;
247     Appearance *a;
248
249     a = !self->enabled ? self->a_disabled :
250         (self->hilite && self->action ? self->a_hilite : self->a_item);
251
252     RECT_SET(a->area, 0, 0, menu->width,
253              menu->item_h);
254     RECT_SET(a->texture[0].position, menu->bullet_w,
255              0, menu->width - 2 * menu->bullet_w,
256              menu->item_h);
257
258     XMoveResizeWindow(ob_display, self->item, 0, self->y,
259                       menu->width, menu->item_h);
260     a->surface.data.planar.parent = menu->a_items;
261     a->surface.data.planar.parentx = 0;
262     a->surface.data.planar.parenty = self->y;
263
264     paint(self->item, a);
265 }
266
267 void menu_entry_fire(MenuEntry *self)
268 {
269     Menu *m;
270
271     if (self->action) {
272         self->action->data.any.c = self->parent->client;
273         self->action->func(&self->action->data);
274
275         /* hide the whole thing */
276         m = self->parent;
277         while (m->parent) m = m->parent;
278         menu_hide(m);
279     }
280 }
281
282 /* 
283    Default menu controller action for showing.
284 */
285
286 void menu_control_show(Menu *self, int x, int y, Client *client) {
287   XMoveWindow(ob_display, self->frame, x, y);
288
289   if (!self->shown) {
290     stacking_raise_internal(self->frame);
291     XMapWindow(ob_display, self->frame);
292     self->shown = TRUE;
293   }
294 }