]> Dogcows Code - chaz/openbox/blob - openbox/menu.c
add keyboard shortcuts to the menus. you can specify the shortcut key with & even...
[chaz/openbox] / openbox / menu.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 menu.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "debug.h"
21 #include "menu.h"
22 #include "openbox.h"
23 #include "stacking.h"
24 #include "client.h"
25 #include "config.h"
26 #include "screen.h"
27 #include "menuframe.h"
28 #include "keyboard.h"
29 #include "geom.h"
30 #include "misc.h"
31 #include "client_menu.h"
32 #include "client_list_menu.h"
33 #include "client_list_combined_menu.h"
34 #include "gettext.h"
35 #include "parser/parse.h"
36
37 typedef struct _ObMenuParseState ObMenuParseState;
38
39 struct _ObMenuParseState
40 {
41 ObMenu *parent;
42 ObMenu *pipe_creator;
43 };
44
45 static GHashTable *menu_hash = NULL;
46 static ObParseInst *menu_parse_inst;
47 static ObMenuParseState menu_parse_state;
48
49 static void menu_destroy_hash_value(ObMenu *self);
50 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
51 gpointer data);
52 static void parse_menu_separator(ObParseInst *i,
53 xmlDocPtr doc, xmlNodePtr node,
54 gpointer data);
55 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
56 gpointer data);
57 static gunichar parse_shortcut(const gchar *label, gchar **strippedlabel,
58 guint *position);
59
60
61 static void client_dest(ObClient *client, gpointer data)
62 {
63 /* menus can be associated with a client, so close any that are since
64 we are disappearing now */
65 menu_frame_hide_all_client(client);
66 }
67
68 void menu_startup(gboolean reconfig)
69 {
70 xmlDocPtr doc;
71 xmlNodePtr node;
72 gboolean loaded = FALSE;
73 GSList *it;
74
75 menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
76 (GDestroyNotify)menu_destroy_hash_value);
77
78 client_list_menu_startup(reconfig);
79 client_list_combined_menu_startup(reconfig);
80 client_menu_startup();
81
82 menu_parse_inst = parse_startup();
83
84 menu_parse_state.parent = NULL;
85 menu_parse_state.pipe_creator = NULL;
86 parse_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
87 parse_register(menu_parse_inst, "item", parse_menu_item,
88 &menu_parse_state);
89 parse_register(menu_parse_inst, "separator",
90 parse_menu_separator, &menu_parse_state);
91
92 for (it = config_menu_files; it; it = g_slist_next(it)) {
93 if (parse_load_menu(it->data, &doc, &node)) {
94 loaded = TRUE;
95 parse_tree(menu_parse_inst, doc, node->children);
96 xmlFreeDoc(doc);
97 } else
98 g_message(_("Unable to find a valid menu file '%s'"),
99 (const gchar*)it->data);
100 }
101 if (!loaded) {
102 if (parse_load_menu("menu.xml", &doc, &node)) {
103 parse_tree(menu_parse_inst, doc, node->children);
104 xmlFreeDoc(doc);
105 } else
106 g_message(_("Unable to find a valid menu file '%s'"),
107 "menu.xml");
108 }
109
110 g_assert(menu_parse_state.parent == NULL);
111
112 if (!reconfig)
113 client_add_destructor(client_dest, NULL);
114 }
115
116 void menu_shutdown(gboolean reconfig)
117 {
118 if (!reconfig)
119 client_remove_destructor(client_dest);
120
121 parse_shutdown(menu_parse_inst);
122 menu_parse_inst = NULL;
123
124 client_list_menu_shutdown(reconfig);
125 client_list_combined_menu_shutdown(reconfig);
126
127 menu_frame_hide_all();
128 g_hash_table_destroy(menu_hash);
129 menu_hash = NULL;
130 }
131
132 static gboolean menu_pipe_submenu(gpointer key, gpointer val, gpointer data)
133 {
134 ObMenu *menu = val;
135 return menu->pipe_creator == data;
136 }
137
138 void menu_pipe_execute(ObMenu *self)
139 {
140 xmlDocPtr doc;
141 xmlNodePtr node;
142 gchar *output;
143 GError *err = NULL;
144
145 if (!self->execute)
146 return;
147
148 if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
149 g_message(_("Failed to execute command for pipe-menu '%s': %s"),
150 self->execute, err->message);
151 g_error_free(err);
152 return;
153 }
154
155 if (parse_load_mem(output, strlen(output),
156 "openbox_pipe_menu", &doc, &node))
157 {
158 g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, self);
159 menu_clear_entries(self);
160
161 menu_parse_state.pipe_creator = self;
162 menu_parse_state.parent = self;
163 parse_tree(menu_parse_inst, doc, node->children);
164 xmlFreeDoc(doc);
165 } else {
166 g_message(_("Invalid output from pipe-menu '%s'"), self->execute);
167 }
168
169 g_free(output);
170 }
171
172 static ObMenu* menu_from_name(gchar *name)
173 {
174 ObMenu *self = NULL;
175
176 g_assert(name != NULL);
177
178 if (!(self = g_hash_table_lookup(menu_hash, name)))
179 g_message(_("Attempted to access menu '%s' but it does not exist"),
180 name);
181 return self;
182 }
183
184 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
185 ((c) >= 'A' && (c) <= 'Z') || \
186 ((c) >= 'a' && (c) <= 'z'))
187
188 static gunichar parse_shortcut(const gchar *label, gchar **strippedlabel,
189 guint *position)
190 {
191 gunichar shortcut = 0;
192
193 *position = 0;
194
195 g_assert(strippedlabel != NULL);
196
197 if (label == NULL) {
198 *strippedlabel = NULL;
199 } else {
200 gchar *i;
201
202 *strippedlabel = g_strdup(label);
203
204 i = strchr(*strippedlabel, '&');
205 if (i != NULL) {
206 /* there is an ampersand in the string */
207
208 /* you have to use a printable ascii character for shortcuts
209 don't allow space either, so you can have like "a & b"
210 */
211 if (VALID_SHORTCUT(*(i+1))) {
212 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
213 *position = i - *strippedlabel;
214
215 /* remove the & from the string */
216 for (; *i != '\0'; ++i)
217 *i = *(i+1);
218 }
219 } else {
220 /* there is no ampersand, so find the first valid character to use
221 instead */
222
223 for (i = *strippedlabel; *i != '\0'; ++i)
224 if (VALID_SHORTCUT(*i)) {
225 *position = i - *strippedlabel;
226 shortcut = g_unichar_tolower(g_utf8_get_char(i));
227 break;
228 }
229 }
230 }
231 return shortcut;
232 }
233
234 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
235 gpointer data)
236 {
237 ObMenuParseState *state = data;
238 gchar *label;
239
240 if (state->parent) {
241 if (parse_attr_string("label", node, &label)) {
242 GSList *acts = NULL;
243
244 for (node = node->children; node; node = node->next)
245 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
246 ObAction *a = action_parse
247 (i, doc, node, OB_USER_ACTION_MENU_SELECTION);
248 if (a)
249 acts = g_slist_append(acts, a);
250 }
251 menu_add_normal(state->parent, -1, label, acts);
252 g_free(label);
253 }
254 }
255 }
256
257 static void parse_menu_separator(ObParseInst *i,
258 xmlDocPtr doc, xmlNodePtr node,
259 gpointer data)
260 {
261 ObMenuParseState *state = data;
262
263 if (state->parent) {
264 gchar *label;
265
266 if (!parse_attr_string("label", node, &label))
267 label = NULL;
268
269 menu_add_separator(state->parent, -1, label);
270 g_free(label);
271 }
272 }
273
274 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
275 gpointer data)
276 {
277 ObMenuParseState *state = data;
278 gchar *name = NULL, *title = NULL, *script = NULL;
279 ObMenu *menu;
280
281 if (!parse_attr_string("id", node, &name))
282 goto parse_menu_fail;
283
284 if (!g_hash_table_lookup(menu_hash, name)) {
285 if (!parse_attr_string("label", node, &title))
286 goto parse_menu_fail;
287
288 if ((menu = menu_new(name, title, NULL))) {
289 menu->pipe_creator = state->pipe_creator;
290 if (parse_attr_string("execute", node, &script)) {
291 menu->execute = parse_expand_tilde(script);
292 } else {
293 ObMenu *old;
294
295 old = state->parent;
296 state->parent = menu;
297 parse_tree(i, doc, node->children);
298 state->parent = old;
299 }
300 }
301 }
302
303 if (state->parent)
304 menu_add_submenu(state->parent, -1, name);
305
306 parse_menu_fail:
307 g_free(name);
308 g_free(title);
309 g_free(script);
310 }
311
312 ObMenu* menu_new(const gchar *name, const gchar *title, gpointer data)
313 {
314 ObMenu *self;
315
316 self = g_new0(ObMenu, 1);
317 self->name = g_strdup(name);
318 self->data = data;
319
320 self->shortcut = parse_shortcut(title, &self->title,
321 &self->shortcut_position);
322
323 g_hash_table_replace(menu_hash, self->name, self);
324
325 return self;
326 }
327
328 static void menu_destroy_hash_value(ObMenu *self)
329 {
330 /* make sure its not visible */
331 {
332 GList *it;
333 ObMenuFrame *f;
334
335 for (it = menu_frame_visible; it; it = g_list_next(it)) {
336 f = it->data;
337 if (f->menu == self)
338 menu_frame_hide_all();
339 }
340 }
341
342 if (self->destroy_func)
343 self->destroy_func(self, self->data);
344
345 menu_clear_entries(self);
346 g_free(self->name);
347 g_free(self->title);
348 g_free(self->execute);
349
350 g_free(self);
351 }
352
353 void menu_free(ObMenu *menu)
354 {
355 g_hash_table_remove(menu_hash, menu->name);
356 }
357
358 void menu_show(gchar *name, gint x, gint y, ObClient *client)
359 {
360 ObMenu *self;
361 ObMenuFrame *frame;
362
363 if (!(self = menu_from_name(name))
364 || keyboard_interactively_grabbed()) return;
365
366 /* if the requested menu is already the top visible menu, then don't
367 bother */
368 if (menu_frame_visible) {
369 frame = menu_frame_visible->data;
370 if (frame->menu == self)
371 return;
372 }
373
374 menu_frame_hide_all();
375
376 frame = menu_frame_new(self, client);
377 if (!menu_frame_show_topmenu(frame, x, y))
378 menu_frame_free(frame);
379 else if (frame->entries) {
380 ObMenuEntryFrame *e = frame->entries->data;
381 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
382 e->entry->data.normal.enabled)
383 menu_frame_select(frame, e, FALSE);
384 }
385 }
386
387 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
388 {
389 ObMenuEntry *self;
390
391 g_assert(menu);
392
393 self = g_new0(ObMenuEntry, 1);
394 self->type = type;
395 self->menu = menu;
396 self->id = id;
397
398 switch (type) {
399 case OB_MENU_ENTRY_TYPE_NORMAL:
400 self->data.normal.enabled = TRUE;
401 break;
402 case OB_MENU_ENTRY_TYPE_SUBMENU:
403 case OB_MENU_ENTRY_TYPE_SEPARATOR:
404 break;
405 }
406
407 return self;
408 }
409
410 void menu_entry_free(ObMenuEntry *self)
411 {
412 if (self) {
413 switch (self->type) {
414 case OB_MENU_ENTRY_TYPE_NORMAL:
415 g_free(self->data.normal.label);
416 while (self->data.normal.actions) {
417 action_unref(self->data.normal.actions->data);
418 self->data.normal.actions =
419 g_slist_delete_link(self->data.normal.actions,
420 self->data.normal.actions);
421 }
422 break;
423 case OB_MENU_ENTRY_TYPE_SUBMENU:
424 g_free(self->data.submenu.name);
425 break;
426 case OB_MENU_ENTRY_TYPE_SEPARATOR:
427 break;
428 }
429
430 g_free(self);
431 }
432 }
433
434 void menu_clear_entries(ObMenu *self)
435 {
436 #ifdef DEBUG
437 /* assert that the menu isn't visible */
438 {
439 GList *it;
440 ObMenuFrame *f;
441
442 for (it = menu_frame_visible; it; it = g_list_next(it)) {
443 f = it->data;
444 g_assert(f->menu != self);
445 }
446 }
447 #endif
448
449 while (self->entries) {
450 menu_entry_free(self->entries->data);
451 self->entries = g_list_delete_link(self->entries, self->entries);
452 }
453 }
454
455 void menu_entry_remove(ObMenuEntry *self)
456 {
457 self->menu->entries = g_list_remove(self->menu->entries, self);
458 menu_entry_free(self);
459 }
460
461 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
462 GSList *actions)
463 {
464 ObMenuEntry *e;
465
466 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
467 e->data.normal.actions = actions;
468
469 menu_entry_set_label(e, label);
470
471 self->entries = g_list_append(self->entries, e);
472 return e;
473 }
474
475 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
476 {
477 ObMenuEntry *e;
478
479 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
480 e->data.submenu.name = g_strdup(submenu);
481
482 self->entries = g_list_append(self->entries, e);
483 return e;
484 }
485
486 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
487 {
488 ObMenuEntry *e;
489
490 e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
491
492 menu_entry_set_label(e, label);
493
494 self->entries = g_list_append(self->entries, e);
495 return e;
496 }
497
498 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
499 {
500 self->update_func = func;
501 }
502
503 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
504 {
505 self->execute_func = func;
506 }
507
508 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
509 {
510 self->destroy_func = func;
511 }
512
513 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
514 {
515 ObMenuEntry *ret = NULL;
516 GList *it;
517
518 for (it = self->entries; it; it = g_list_next(it)) {
519 ObMenuEntry *e = it->data;
520
521 if (e->id == id) {
522 ret = e;
523 break;
524 }
525 }
526 return ret;
527 }
528
529 void menu_find_submenus(ObMenu *self)
530 {
531 GList *it;
532
533 for (it = self->entries; it; it = g_list_next(it)) {
534 ObMenuEntry *e = it->data;
535
536 if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
537 e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
538 }
539 }
540
541 void menu_entry_set_label(ObMenuEntry *self, const gchar *label)
542 {
543 switch (self->type) {
544 case OB_MENU_ENTRY_TYPE_SEPARATOR:
545 g_free(self->data.separator.label);
546 self->data.separator.label = g_strdup(label);
547 break;
548 case OB_MENU_ENTRY_TYPE_NORMAL:
549 g_free(self->data.normal.label);
550 self->data.normal.shortcut =
551 parse_shortcut(label, &self->data.normal.label,
552 &self->data.normal.shortcut_position);
553 break;
554 default:
555 g_assert_not_reached();
556 }
557 }
558
559 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
560 {
561 self->show_all_shortcuts = show;
562 }
This page took 0.058486 seconds and 5 git commands to generate.