]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
from icccm:
[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 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 void menu_hide(Menu *self) {
276 if (self->shown) {
277 XUnmapWindow(ob_display, self->frame);
278 self->shown = FALSE;
279 if (self->open_submenu)
280 menu_hide(self->open_submenu);
281 if (self->parent && self->parent->open_submenu == self)
282 self->parent->open_submenu = NULL;
283
284 }
285 }
286
287 void menu_clear(Menu *self) {
288 GList *it;
289
290 for (it = self->entries; it; it = it->next) {
291 MenuEntry *entry = it->data;
292 menu_entry_free(entry);
293 }
294 self->entries = NULL;
295 self->invalid = TRUE;
296 }
297
298
299 MenuEntry *menu_find_entry(Menu *menu, Window win)
300 {
301 GList *it;
302
303 for (it = menu->entries; it; it = it->next) {
304 MenuEntry *entry = it->data;
305 if (entry->item == win)
306 return entry;
307 }
308 return NULL;
309 }
310
311 void menu_entry_fire(MenuEntry *self)
312 {
313 Menu *m;
314
315 if (self->action) {
316 self->action->data.any.c = self->parent->client;
317 self->action->func(&self->action->data);
318
319 /* hide the whole thing */
320 m = self->parent;
321 while (m->parent) m = m->parent;
322 menu_hide(m);
323 }
324 }
325
326 /*
327 Default menu controller action for showing.
328 */
329
330 void menu_control_show(Menu *self, int x, int y, Client *client) {
331 g_assert(!self->invalid);
332
333 XMoveWindow(ob_display, self->frame,
334 MIN(x, screen_physical_size.width - self->size.width),
335 MIN(y, screen_physical_size.height - self->size.height));
336 POINT_SET(self->location,
337 MIN(x, screen_physical_size.width - self->size.width),
338 MIN(y, screen_physical_size.height - self->size.height));
339
340 if (!self->shown) {
341 XMapWindow(ob_display, self->frame);
342 stacking_raise(MENU_AS_WINDOW(self));
343 self->shown = TRUE;
344 } else if (self->shown && self->open_submenu) {
345 menu_hide(self->open_submenu);
346 }
347 }
348
349 void menu_control_mouseover(MenuEntry *self, gboolean enter) {
350 int x;
351 self->hilite = enter;
352
353 if (enter) {
354 if (self->parent->open_submenu && self->submenu
355 != self->parent->open_submenu)
356 menu_hide(self->parent->open_submenu);
357
358 if (self->submenu) {
359 self->parent->open_submenu = self->submenu;
360
361 /* shouldn't be invalid since it must be displayed */
362 g_assert(!self->parent->invalid);
363 /* TODO: I don't understand why these bevels should be here.
364 Something must be wrong in the width calculation */
365 x = self->parent->location.x + self->parent->size.width +
366 theme_bevel;
367
368 /* need to get the width. is this bad?*/
369 menu_render(self->submenu);
370
371 if (self->submenu->size.width + x > screen_physical_size.width)
372 x = self->parent->location.x - self->submenu->size.width -
373 theme_bevel;
374
375 menu_show_full(self->submenu, x,
376 self->parent->location.y + self->y,
377 self->parent->client);
378 }
379 }
380 }
This page took 0.052885 seconds and 4 git commands to generate.