hilight the first normal entry also if there is a menuheader/separator before it
[dana/openbox.git] / 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, gboolean allow_shortcut,
58                                gchar **strippedlabel, 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_destroy_notify(client_dest, NULL);
114 }
115
116 void menu_shutdown(gboolean reconfig)
117 {
118     if (!reconfig)
119         client_remove_destroy_notify(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, gboolean allow_shortcut,
189                                gchar **strippedlabel, 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         /* if allow_shortcut is false, then you can't use the &, instead you
205            have to just use the first valid character
206         */
207
208         i = strchr(*strippedlabel, '&');
209         if (allow_shortcut && i != NULL) {
210             /* there is an ampersand in the string */
211
212             /* you have to use a printable ascii character for shortcuts
213                don't allow space either, so you can have like "a & b"
214             */
215             if (VALID_SHORTCUT(*(i+1))) {
216                 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
217                 *position = i - *strippedlabel;
218
219                 /* remove the & from the string */
220                 for (; *i != '\0'; ++i)
221                     *i = *(i+1);
222             }
223         } else {
224             /* there is no ampersand, so find the first valid character to use
225                instead */
226
227             for (i = *strippedlabel; *i != '\0'; ++i)
228                 if (VALID_SHORTCUT(*i)) {
229                     *position = i - *strippedlabel;
230                     shortcut = g_unichar_tolower(g_utf8_get_char(i));
231                     break;
232                 }
233         }
234     }
235     return shortcut;
236 }
237
238 static void parse_menu_item(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
239                             gpointer data)
240 {
241     ObMenuParseState *state = data;
242     gchar *label;
243     
244     if (state->parent) {
245         if (parse_attr_string("label", node, &label)) {
246             GSList *acts = NULL;
247
248             for (node = node->children; node; node = node->next)
249                 if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
250                     ObAction *a = action_parse
251                         (i, doc, node, OB_USER_ACTION_MENU_SELECTION);
252                     if (a)
253                         acts = g_slist_append(acts, a);
254                 }
255             menu_add_normal(state->parent, -1, label, acts, FALSE);
256             g_free(label);
257         }
258     }
259 }
260
261 static void parse_menu_separator(ObParseInst *i,
262                                  xmlDocPtr doc, xmlNodePtr node,
263                                  gpointer data)
264 {
265     ObMenuParseState *state = data;
266
267     if (state->parent) {
268         gchar *label;
269
270         if (!parse_attr_string("label", node, &label))
271             label = NULL;
272
273         menu_add_separator(state->parent, -1, label);
274         g_free(label);
275     }
276 }
277
278 static void parse_menu(ObParseInst *i, xmlDocPtr doc, xmlNodePtr node,
279                        gpointer data)
280 {
281     ObMenuParseState *state = data;
282     gchar *name = NULL, *title = NULL, *script = NULL;
283     ObMenu *menu;
284
285     if (!parse_attr_string("id", node, &name))
286         goto parse_menu_fail;
287
288     if (!g_hash_table_lookup(menu_hash, name)) {
289         if (!parse_attr_string("label", node, &title))
290             goto parse_menu_fail;
291
292         if ((menu = menu_new(name, title, FALSE, NULL))) {
293             menu->pipe_creator = state->pipe_creator;
294             if (parse_attr_string("execute", node, &script)) {
295                 menu->execute = parse_expand_tilde(script);
296             } else {
297                 ObMenu *old;
298
299                 old = state->parent;
300                 state->parent = menu;
301                 parse_tree(i, doc, node->children);
302                 state->parent = old;
303             }
304         }
305     }
306
307     if (state->parent)
308         menu_add_submenu(state->parent, -1, name);
309
310 parse_menu_fail:
311     g_free(name);
312     g_free(title);
313     g_free(script);
314 }
315
316 ObMenu* menu_new(const gchar *name, const gchar *title,
317                  gboolean allow_shortcut_selection, gpointer data)
318 {
319     ObMenu *self;
320
321     self = g_new0(ObMenu, 1);
322     self->name = g_strdup(name);
323     self->data = data;
324
325     self->shortcut = parse_shortcut(title, allow_shortcut_selection,
326                                     &self->title, &self->shortcut_position);
327
328     g_hash_table_replace(menu_hash, self->name, self);
329
330     self->more_menu = g_new0(ObMenu, 1);
331     self->more_menu->name = _("More...");
332     self->more_menu->title = _("More...");
333     self->more_menu->data = data;
334     self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
335
336     self->more_menu->show_func = self->show_func;
337     self->more_menu->hide_func = self->hide_func;
338     self->more_menu->update_func = self->update_func;
339     self->more_menu->execute_func = self->execute_func;
340     self->more_menu->destroy_func = self->destroy_func;
341     self->more_menu->place_func = self->place_func;
342
343     return self;
344 }
345
346 static void menu_destroy_hash_value(ObMenu *self)
347 {
348     /* make sure its not visible */
349     {
350         GList *it;
351         ObMenuFrame *f;
352
353         for (it = menu_frame_visible; it; it = g_list_next(it)) {
354             f = it->data;
355             if (f->menu == self)
356                 menu_frame_hide_all();
357         }
358     }
359
360     if (self->destroy_func)
361         self->destroy_func(self, self->data);
362
363     menu_clear_entries(self);
364     g_free(self->name);
365     g_free(self->title);
366     g_free(self->execute);
367     g_free(self->more_menu);
368
369     g_free(self);
370 }
371
372 void menu_free(ObMenu *menu)
373 {
374     if (menu)
375         g_hash_table_remove(menu_hash, menu->name);
376 }
377
378 void menu_show(gchar *name, gint x, gint y, gint button, ObClient *client)
379 {
380     ObMenu *self;
381     ObMenuFrame *frame;
382
383     if (!(self = menu_from_name(name))
384         || keyboard_interactively_grabbed()) return;
385
386     /* if the requested menu is already the top visible menu, then don't
387        bother */
388     if (menu_frame_visible) {
389         frame = menu_frame_visible->data;
390         if (frame->menu == self)
391             return;
392     }
393
394     menu_frame_hide_all();
395
396     frame = menu_frame_new(self, 0, client);
397     if (!menu_frame_show_topmenu(frame, x, y, button))
398         menu_frame_free(frame);
399     else {
400         /* select the first entry if it's not a submenu */
401         GList *it = frame->entries;
402         while (it) {
403             ObMenuEntryFrame *e = it->data;
404             if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
405                 menu_frame_select(frame, e, FALSE);
406                 break;
407             } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
408                 it = g_list_next(it);
409             else
410                 break;
411         }
412     }
413 }
414
415 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
416 {
417     ObMenuEntry *self;
418
419     g_assert(menu);
420
421     self = g_new0(ObMenuEntry, 1);
422     self->ref = 1;
423     self->type = type;
424     self->menu = menu;
425     self->id = id;
426
427     switch (type) {
428     case OB_MENU_ENTRY_TYPE_NORMAL:
429         self->data.normal.enabled = TRUE;
430         break;
431     case OB_MENU_ENTRY_TYPE_SUBMENU:
432     case OB_MENU_ENTRY_TYPE_SEPARATOR:
433         break;
434     }
435
436     return self;
437 }
438
439 void menu_entry_ref(ObMenuEntry *self)
440 {
441     ++self->ref;
442 }
443
444 void menu_entry_unref(ObMenuEntry *self)
445 {
446     if (self && --self->ref == 0) {
447         switch (self->type) {
448         case OB_MENU_ENTRY_TYPE_NORMAL:
449             g_free(self->data.normal.label);
450             while (self->data.normal.actions) {
451                 action_unref(self->data.normal.actions->data);
452                 self->data.normal.actions =
453                     g_slist_delete_link(self->data.normal.actions,
454                                         self->data.normal.actions);
455             }
456             break;
457         case OB_MENU_ENTRY_TYPE_SUBMENU:
458             g_free(self->data.submenu.name);
459             break;
460         case OB_MENU_ENTRY_TYPE_SEPARATOR:
461             break;
462         }
463
464         g_free(self);
465     }
466 }
467
468 void menu_clear_entries(ObMenu *self)
469 {
470 #ifdef DEBUG
471     /* assert that the menu isn't visible */
472     {
473         GList *it;
474         ObMenuFrame *f;
475
476         for (it = menu_frame_visible; it; it = g_list_next(it)) {
477             f = it->data;
478             g_assert(f->menu != self);
479         }
480     }
481 #endif
482
483     while (self->entries) {
484         menu_entry_unref(self->entries->data);
485         self->entries = g_list_delete_link(self->entries, self->entries);
486     }
487     self->more_menu->entries = self->entries; /* keep it in sync */
488 }
489
490 void menu_entry_remove(ObMenuEntry *self)
491 {
492     self->menu->entries = g_list_remove(self->menu->entries, self);
493     menu_entry_unref(self);
494 }
495
496 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
497                              GSList *actions, gboolean allow_shortcut)
498 {
499     ObMenuEntry *e;
500
501     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
502     e->data.normal.actions = actions;
503
504     menu_entry_set_label(e, label, allow_shortcut);
505
506     self->entries = g_list_append(self->entries, e);
507     self->more_menu->entries = self->entries; /* keep it in sync */
508     return e;
509 }
510
511 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
512 {
513     ObMenuEntry *e;
514     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
515     /* points to itself */
516     e->data.submenu.name = g_strdup(self->name);
517     e->data.submenu.submenu = self;
518     e->data.submenu.show_from = show_from;
519     return e;
520 }
521
522 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
523 {
524     ObMenuEntry *e;
525
526     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
527     e->data.submenu.name = g_strdup(submenu);
528
529     self->entries = g_list_append(self->entries, e);
530     self->more_menu->entries = self->entries; /* keep it in sync */
531     return e;
532 }
533
534 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
535 {
536     ObMenuEntry *e;
537
538     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
539
540     menu_entry_set_label(e, label, FALSE);
541
542     self->entries = g_list_append(self->entries, e);
543     self->more_menu->entries = self->entries; /* keep it in sync */
544     return e;
545 }
546
547 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
548 {
549     do {
550         self->show_func = func;
551         self = self->more_menu;
552     } while (self);
553 }
554
555 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
556 {
557     do {
558         self->hide_func = func;
559         self = self->more_menu;
560     } while (self);
561 }
562
563 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
564 {
565     do {
566         self->update_func = func;
567         self = self->more_menu;
568     } while (self);
569 }
570
571 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
572 {
573     do {
574         self->execute_func = func;
575         self = self->more_menu;
576     } while (self);
577 }
578
579 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
580 {
581     do {
582         self->destroy_func = func;
583         self = self->more_menu;
584     } while (self);
585 }
586
587 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
588 {
589     do {
590         self->place_func = func;
591         self = self->more_menu;
592     } while (self);
593 }
594
595 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
596 {
597     ObMenuEntry *ret = NULL;
598     GList *it;
599
600     for (it = self->entries; it; it = g_list_next(it)) {
601         ObMenuEntry *e = it->data;
602
603         if (e->id == id) {
604             ret = e;
605             break;
606         }
607     }
608     return ret;
609 }
610
611 void menu_find_submenus(ObMenu *self)
612 {
613     GList *it;
614
615     for (it = self->entries; it; it = g_list_next(it)) {
616         ObMenuEntry *e = it->data;
617
618         if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
619             e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
620     }
621 }
622
623 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
624                           gboolean allow_shortcut)
625 {
626     switch (self->type) {
627     case OB_MENU_ENTRY_TYPE_SEPARATOR:
628         g_free(self->data.separator.label);
629         self->data.separator.label = g_strdup(label);
630         break;
631     case OB_MENU_ENTRY_TYPE_NORMAL:
632         g_free(self->data.normal.label);
633         self->data.normal.shortcut =
634             parse_shortcut(label, allow_shortcut, &self->data.normal.label,
635                            &self->data.normal.shortcut_position);
636         break;
637     default:
638         g_assert_not_reached();
639     }
640 }
641
642 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
643 {
644     self->show_all_shortcuts = show;
645 }