save the window ids in a map for the menus
[mikachu/openbox.git] / openbox / menu.c
1 #include "menu.h"
2 #include "openbox.h"
3 #include "render/theme.h"
4
5 static GHashTable *menu_hash = NULL;
6 GHashTable *menu_map = NULL;
7
8 #define TITLE_EVENTMASK (ButtonMotionMask)
9 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
10                          ButtonPressMask | ButtonReleaseMask)
11
12 void menu_destroy_hash_key(gpointer data)
13 {
14     g_free(data);
15 }
16
17 void menu_destroy_hash_value(Menu *self)
18 {
19     GList *it;
20     MenuRenderData *data = self->render_data;
21
22     for (it = self->entries; it; it = it->next)
23         menu_entry_free(it->data);
24     g_list_free(self->entries);
25
26     g_free(self->label);
27     g_free(self->name);
28
29     g_hash_table_remove(menu_map, &data->title);
30     g_hash_table_remove(menu_map, &data->frame);
31     g_hash_table_remove(menu_map, &data->items);
32
33     appearance_free(data->a_title);
34     XDestroyWindow(ob_display, data->title);
35     XDestroyWindow(ob_display, data->frame);
36     XDestroyWindow(ob_display, data->items);
37
38     g_free(self);
39 }
40
41 void menu_entry_free(MenuEntry *self)
42 {
43     MenuEntryRenderData *data = self->render_data;
44
45     g_free(self->label);
46     g_free(self->render_data);
47     action_free(self->action);
48
49     g_hash_table_remove(menu_map, &data->item);
50
51     appearance_free(data->a_item);
52     XDestroyWindow(ob_display, data->item);
53
54     g_free(self);
55 }
56     
57 void menu_startup()
58 {
59     Menu *m;
60
61     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
62                                       menu_destroy_hash_key,
63                                       (GDestroyNotify)menu_destroy_hash_value);
64     menu_map = g_hash_table_new(g_int_hash, g_int_equal);
65
66     m = menu_new("sex menu", "root", NULL);
67     menu_add_entry(m, menu_entry_new("foo shit etc bleh",
68                                      action_from_string("restart")));
69     menu_add_entry(m, menu_entry_new("more shit",
70                                      action_from_string("restart")));
71     menu_add_entry(m, menu_entry_new("",
72                                      action_from_string("restart")));
73     menu_add_entry(m, menu_entry_new("and yet more",
74                                      action_from_string("restart")));
75 }
76
77 void menu_shutdown()
78 {
79     g_hash_table_destroy(menu_hash);
80     g_hash_table_destroy(menu_map);
81 }
82
83 static Window createWindow(Window parent, unsigned long mask,
84                            XSetWindowAttributes *attrib)
85 {
86     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
87                          render_depth, InputOutput, render_visual,
88                          mask, attrib);
89                        
90 }
91
92 Menu *menu_new(char *label, char *name, Menu *parent)
93 {
94     XSetWindowAttributes attrib;
95     Menu *self;
96     MenuRenderData *data;
97
98     self = g_new0(Menu, 1);
99     self->label = g_strdup(label);
100     self->name = g_strdup(name);
101     self->parent = parent;
102
103     self->entries = NULL;
104     self->shown = FALSE;
105     self->invalid = FALSE;
106     /* default controllers? */
107
108     data = g_new(MenuRenderData, 1);
109
110     attrib.override_redirect = TRUE;
111     data->frame = createWindow(ob_root, CWOverrideRedirect, &attrib);
112     attrib.event_mask = TITLE_EVENTMASK;
113     data->title = createWindow(data->frame, CWEventMask, &attrib);
114     data->items = createWindow(data->frame, 0, &attrib);
115
116     XSetWindowBorderWidth(ob_display, data->frame, theme_bwidth);
117     XSetWindowBorderWidth(ob_display, data->title, theme_bwidth);
118     XSetWindowBorder(ob_display, data->frame, theme_b_color->pixel);
119     XSetWindowBorder(ob_display, data->title, theme_b_color->pixel);
120
121     XMapWindow(ob_display, data->title);
122     XMapWindow(ob_display, data->items);
123
124     data->a_title = appearance_copy(theme_a_menu_title);
125     data->a_items = appearance_copy(theme_a_menu);
126
127     self->render_data = data;
128
129     g_hash_table_insert(menu_map, &data->frame, self);
130     g_hash_table_insert(menu_map, &data->title, self);
131     g_hash_table_insert(menu_map, &data->items, self);
132     g_hash_table_insert(menu_hash, g_strdup(name), self);
133     return self;
134 }
135
136 void menu_free(char *name)
137 {
138     g_hash_table_remove(menu_hash, name);
139 }
140
141 MenuEntry *menu_entry_new_full(char *label, Action *action,
142                                MenuEntryRenderType render_type,
143                                gpointer submenu)
144 {
145     MenuEntry *menu_entry = g_new(MenuEntry, 1);
146
147     menu_entry->label = g_strdup(label);
148     menu_entry->render_type = render_type;
149     menu_entry->action = action;
150
151     menu_entry->render_data = NULL;
152     menu_entry->submenu = submenu;
153
154     return menu_entry;
155 }
156
157 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
158 {
159     g_assert(entry != NULL);
160     
161     entry->submenu = submenu;
162
163     if(entry->parent != NULL)
164         entry->parent->invalid = TRUE;
165 }
166
167 void menu_add_entry(Menu *menu, MenuEntry *entry)
168 {
169     MenuEntryRenderData *data;
170     XSetWindowAttributes attrib;
171
172     g_assert(menu != NULL && entry != NULL && entry->render_data == NULL);
173
174     menu->entries = g_list_append(menu->entries, entry);
175     entry->parent = menu;
176
177     data = g_new(MenuEntryRenderData, 1);
178     data->item = createWindow(((MenuRenderData*)menu->render_data)->items,
179                               0, &attrib);
180     XMapWindow(ob_display, data->item);
181     data->a_item = appearance_copy(theme_a_menu_item);
182
183     entry->render_data = data;
184
185     menu->invalid = TRUE;
186
187     g_hash_table_insert(menu_map, &data->item, menu);
188 }
189
190 void menu_show(char *name, int x, int y, Client *client)
191 {
192     Menu *self;
193     MenuRenderData *data;
194     GList *it;
195     int w = 1;
196     int items_h;
197     int item_h = 0, nitems = 0; /* each item, only one is used */
198     int item_y;
199     int bullet_w;
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     data = self->render_data;
209
210     /* set texture data and size them mofos out */
211     data->a_title->texture[0].data.text.string = self->label;
212     appearance_minsize(data->a_title, &data->title_min_w, &data->title_h);
213     data->title_min_w += theme_bevel * 2;
214     data->title_h += theme_bevel * 2;
215     w = MAX(w, data->title_min_w);
216
217     for (it = self->entries; it; it = it->next) {
218         MenuEntryRenderData *r = ((MenuEntry*)it->data)->render_data;
219
220         r->a_item->texture[0].data.text.string = ((MenuEntry*)it->data)->label;
221         appearance_minsize(r->a_item, &r->min_w, &item_h);
222         r->min_w += theme_bevel * 2;
223         item_h += theme_bevel * 2;
224         w = MAX(w, r->min_w);
225         ++nitems;
226     }
227     bullet_w = item_h + theme_bevel;
228     w += 2 * bullet_w;
229     items_h = item_h * nitems;
230
231     /* size appearances */
232     RECT_SET(data->a_title->area, 0, 0, w, data->title_h);
233     RECT_SET(data->a_title->texture[0].position, 0, 0, w, data->title_h);
234     RECT_SET(data->a_items->area, 0, 0, w, items_h);
235     for (it = self->entries; it; it = it->next) {
236         MenuEntryRenderData *r = ((MenuEntry*)it->data)->render_data;
237         RECT_SET(r->a_item->area, 0, 0, w, item_h);
238         RECT_SET(r->a_item->texture[0].position, bullet_w, 0,
239                  w - 2 * bullet_w, item_h);
240     }
241
242     /* size windows and paint the suckers */
243     XMoveResizeWindow(ob_display, data->frame, x, y, w,
244                       data->title_h + items_h);
245     XMoveResizeWindow(ob_display, data->title, -theme_bwidth, -theme_bwidth,
246                       w, data->title_h);
247     paint(data->title, data->a_title);
248     XMoveResizeWindow(ob_display, data->items, 0, data->title_h + theme_bwidth,
249                       w, items_h);
250     paint(data->items, data->a_items);
251     for (item_y = 0, it = self->entries; it; item_y += item_h, it = it->next) {
252         MenuEntryRenderData *r = ((MenuEntry*)it->data)->render_data;
253         XMoveResizeWindow(ob_display, r->item, 0, item_y, w, item_h);
254         r->a_item->surface.data.planar.parent = data->a_items;
255         r->a_item->surface.data.planar.parentx = 0;
256         r->a_item->surface.data.planar.parenty = item_y;
257         paint(r->item, r->a_item);
258     }
259
260
261     XMapWindow(ob_display, data->frame);
262 }