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