]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
all my changes while i was offline.
[chaz/openbox] / 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 m = menu_new("client menu", "client", NULL);
81 a = action_from_string("iconify");
82 menu_add_entry(m, menu_entry_new("iconify", a));
83 a = action_from_string("toggleshade");
84 menu_add_entry(m, menu_entry_new("(un)shade", a));
85 a = action_from_string("togglemaximizefull");
86 menu_add_entry(m, menu_entry_new("(un)maximize", a));
87 a = action_from_string("close");
88 menu_add_entry(m, menu_entry_new("close", a));
89 }
90
91 void menu_shutdown()
92 {
93 g_hash_table_destroy(menu_hash);
94 g_hash_table_destroy(menu_map);
95 }
96
97 static Window createWindow(Window parent, unsigned long mask,
98 XSetWindowAttributes *attrib)
99 {
100 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
101 render_depth, InputOutput, render_visual,
102 mask, attrib);
103
104 }
105
106 Menu *menu_new_full(char *label, char *name, Menu *parent,
107 menu_controller_show show, menu_controller_update update)
108 {
109 XSetWindowAttributes attrib;
110 Menu *self;
111
112 self = g_new0(Menu, 1);
113 self->label = g_strdup(label);
114 self->name = g_strdup(name);
115 self->parent = parent;
116
117 self->entries = NULL;
118 self->shown = FALSE;
119 self->invalid = FALSE;
120 /* default controllers? */
121
122 self->show = show;
123 self->hide = NULL;
124 self->update = update;
125 self->mouseover = NULL;
126 self->selected = NULL;
127
128 attrib.override_redirect = TRUE;
129 attrib.event_mask = FRAME_EVENTMASK;
130 self->frame = createWindow(ob_root, CWOverrideRedirect|CWEventMask, &attrib);
131 attrib.event_mask = TITLE_EVENTMASK;
132 self->title = createWindow(self->frame, CWEventMask, &attrib);
133 self->items = createWindow(self->frame, 0, &attrib);
134
135 XSetWindowBorderWidth(ob_display, self->frame, theme_bwidth);
136 XSetWindowBorderWidth(ob_display, self->title, theme_bwidth);
137 XSetWindowBorder(ob_display, self->frame, theme_b_color->pixel);
138 XSetWindowBorder(ob_display, self->title, theme_b_color->pixel);
139
140 XMapWindow(ob_display, self->title);
141 XMapWindow(ob_display, self->items);
142
143 self->a_title = appearance_copy(theme_a_menu_title);
144 self->a_items = appearance_copy(theme_a_menu);
145
146 g_hash_table_insert(menu_map, &self->frame, self);
147 g_hash_table_insert(menu_map, &self->title, self);
148 g_hash_table_insert(menu_map, &self->items, self);
149 g_hash_table_insert(menu_hash, g_strdup(name), self);
150 return self;
151 }
152
153 void menu_free(char *name)
154 {
155 g_hash_table_remove(menu_hash, name);
156 }
157
158 MenuEntry *menu_entry_new_full(char *label, Action *action,
159 MenuEntryRenderType render_type,
160 gpointer submenu)
161 {
162 MenuEntry *menu_entry = g_new0(MenuEntry, 1);
163
164 menu_entry->label = g_strdup(label);
165 menu_entry->render_type = render_type;
166 menu_entry->action = action;
167
168 menu_entry->hilite = FALSE;
169 menu_entry->enabled = TRUE;
170
171 menu_entry->submenu = submenu;
172
173 return menu_entry;
174 }
175
176 void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
177 {
178 g_assert(entry != NULL);
179
180 entry->submenu = submenu;
181
182 if(entry->parent != NULL)
183 entry->parent->invalid = TRUE;
184 }
185
186 void menu_add_entry(Menu *menu, MenuEntry *entry)
187 {
188 XSetWindowAttributes attrib;
189
190 g_assert(menu != NULL && entry != NULL && entry->item == None);
191
192 menu->entries = g_list_append(menu->entries, entry);
193 entry->parent = menu;
194
195 attrib.event_mask = ENTRY_EVENTMASK;
196 entry->item = createWindow(menu->items, CWEventMask, &attrib);
197 XMapWindow(ob_display, entry->item);
198 entry->a_item = appearance_copy(theme_a_menu_item);
199 entry->a_disabled = appearance_copy(theme_a_menu_disabled);
200 entry->a_hilite = appearance_copy(theme_a_menu_hilite);
201
202 menu->invalid = TRUE;
203
204 g_hash_table_insert(menu_map, &entry->item, menu);
205 }
206
207 void menu_show(char *name, int x, int y, Client *client)
208 {
209 Menu *self;
210
211 self = g_hash_table_lookup(menu_hash, name);
212 if (!self) {
213 g_warning("Attempted to show menu '%s' but it does not exist.",
214 name);
215 return;
216 }
217
218 if (self->invalid) {
219 if (self->update) {
220 self->update(self);
221 } else {
222 menu_render(self);
223 }
224 }
225
226 self->client = client;
227
228 if (self->show) {
229 self->show(self, x, y, client);
230 } else {
231 menu_control_show(self, x, y, client);
232 }
233 }
234
235 void menu_hide(Menu *self) {
236 if (self->shown) {
237 XUnmapWindow(ob_display, self->frame);
238 self->shown = FALSE;
239 }
240 }
241
242 MenuEntry *menu_find_entry(Menu *menu, Window win)
243 {
244 GList *it;
245
246 for (it = menu->entries; it; it = it->next) {
247 MenuEntry *entry = it->data;
248 if (entry->item == win)
249 return entry;
250 }
251 return NULL;
252 }
253
254 void menu_entry_render(MenuEntry *self)
255 {
256 Menu *menu = self->parent;
257 Appearance *a;
258
259 a = !self->enabled ? self->a_disabled :
260 (self->hilite && self->action ? self->a_hilite : self->a_item);
261
262 RECT_SET(a->area, 0, 0, menu->width,
263 menu->item_h);
264 RECT_SET(a->texture[0].position, menu->bullet_w,
265 0, menu->width - 2 * menu->bullet_w,
266 menu->item_h);
267
268 XMoveResizeWindow(ob_display, self->item, 0, self->y,
269 menu->width, menu->item_h);
270 a->surface.data.planar.parent = menu->a_items;
271 a->surface.data.planar.parentx = 0;
272 a->surface.data.planar.parenty = self->y;
273
274 paint(self->item, a);
275 }
276
277 void menu_entry_fire(MenuEntry *self)
278 {
279 Menu *m;
280
281 if (self->action) {
282 self->action->data.any.c = self->parent->client;
283 self->action->func(&self->action->data);
284
285 /* hide the whole thing */
286 m = self->parent;
287 while (m->parent) m = m->parent;
288 menu_hide(m);
289 }
290 }
291
292 /*
293 Default menu controller action for showing.
294 */
295
296 void menu_control_show(Menu *self, int x, int y, Client *client) {
297 XMoveWindow(ob_display, self->frame, x, y);
298
299 if (!self->shown) {
300 stacking_raise_internal(self->frame);
301 XMapWindow(ob_display, self->frame);
302 self->shown = TRUE;
303 }
304 }
This page took 0.049928 seconds and 4 git commands to generate.