]> icculus.org git repositories - dana/openbox.git/blob - openbox/menu.c
wip: Add config_parser.c which will provide a nice means to specify config variables...
[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 "grab.h"
25 #include "client.h"
26 #include "config.h"
27 #include "action.h"
28 #include "action_list.h"
29 #include "action_parser.h"
30 #include "screen.h"
31 #include "menuframe.h"
32 #include "keyboard.h"
33 #include "geom.h"
34 #include "misc.h"
35 #include "client_menu.h"
36 #include "apps_menu.h"
37 #include "client_list_menu.h"
38 #include "client_list_combined_menu.h"
39 #include "gettext.h"
40 #include "obt/xml.h"
41 #include "obt/paths.h"
42
43 typedef struct _ObMenuParseState ObMenuParseState;
44
45 struct _ObMenuParseState
46 {
47     ObMenu *parent;
48     ObMenu *pipe_creator;
49 };
50
51 static GHashTable *menu_hash = NULL;
52 static ObtXmlInst *menu_parse_inst;
53 static ObMenuParseState menu_parse_state;
54 static gboolean menu_can_hide = FALSE;
55 static guint menu_timeout_id = 0;
56
57 static void menu_destroy_hash_value(ObMenu *self);
58 static void parse_menu_item(xmlNodePtr node, gpointer data);
59 static void parse_menu_separator(xmlNodePtr node, gpointer data);
60 static void parse_menu(xmlNodePtr node, gpointer data);
61 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
62                                gchar **strippedlabel, guint *position,
63                                gboolean *always_show);
64
65 void menu_startup(gboolean reconfig)
66 {
67     gboolean loaded = FALSE;
68     GSList *it;
69
70     menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
71                                       (GDestroyNotify)menu_destroy_hash_value);
72
73     apps_menu_startup(reconfig);
74     client_list_menu_startup(reconfig);
75     client_list_combined_menu_startup(reconfig);
76     client_menu_startup();
77
78     menu_parse_inst = obt_xml_instance_new();
79
80     menu_parse_state.parent = NULL;
81     menu_parse_state.pipe_creator = NULL;
82     obt_xml_register(menu_parse_inst, "menu", parse_menu, &menu_parse_state);
83     obt_xml_register(menu_parse_inst, "item", parse_menu_item,
84                      &menu_parse_state);
85     obt_xml_register(menu_parse_inst, "separator",
86                        parse_menu_separator, &menu_parse_state);
87
88     for (it = config_menu_files; it; it = g_slist_next(it)) {
89         if (obt_xml_load_config_file(menu_parse_inst,
90                                      "openbox",
91                                      it->data,
92                                      "openbox_menu"))
93         {
94             loaded = TRUE;
95             obt_xml_tree_from_root(menu_parse_inst);
96             obt_xml_close(menu_parse_inst);
97         } else
98             g_message(_("Unable to find a valid menu file \"%s\""),
99                       (const gchar*)it->data);
100     }
101     if (!loaded) {
102         if (obt_xml_load_config_file(menu_parse_inst,
103                                      "openbox",
104                                      "menu.xml",
105                                      "openbox_menu"))
106         {
107             obt_xml_tree_from_root(menu_parse_inst);
108             obt_xml_close(menu_parse_inst);
109         } else
110             g_message(_("Unable to find a valid menu file \"%s\""),
111                       "menu.xml");
112     }
113
114     g_assert(menu_parse_state.parent == NULL);
115 }
116
117 void menu_shutdown(gboolean reconfig)
118 {
119     obt_xml_instance_unref(menu_parse_inst);
120     menu_parse_inst = NULL;
121
122     menu_frame_hide_all();
123
124     client_list_combined_menu_shutdown(reconfig);
125     client_list_menu_shutdown(reconfig);
126     apps_menu_shutdown(reconfig);
127
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 != NULL;
136 }
137
138 static void clear_cache(gpointer key, gpointer val, gpointer data)
139 {
140     ObMenu *menu = val;
141     if (menu->execute)
142         menu_clear_entries(menu);
143 }
144
145 void menu_clear_pipe_caches(void)
146 {
147     /* delete any pipe menus' submenus */
148     g_hash_table_foreach_remove(menu_hash, menu_pipe_submenu, NULL);
149     /* empty the top level pipe menus */
150     g_hash_table_foreach(menu_hash, clear_cache, NULL);
151 }
152
153 void menu_pipe_execute(ObMenu *self)
154 {
155     gchar *output;
156     GError *err = NULL;
157
158     if (!self->execute)
159         return;
160     if (self->entries) /* the entries are already created and cached */
161         return;
162
163     if (!g_spawn_command_line_sync(self->execute, &output, NULL, NULL, &err)) {
164         g_message(_("Failed to execute command for pipe-menu \"%s\": %s"),
165                   self->execute, err->message);
166         g_error_free(err);
167         return;
168     }
169
170     if (obt_xml_load_mem(menu_parse_inst, output, strlen(output),
171                          "openbox_pipe_menu"))
172     {
173         menu_parse_state.pipe_creator = self;
174         menu_parse_state.parent = self;
175         obt_xml_tree_from_root(menu_parse_inst);
176         obt_xml_close(menu_parse_inst);
177     } else {
178         g_message(_("Invalid output from pipe-menu \"%s\""), self->execute);
179     }
180
181     g_free(output);
182 }
183
184 static ObMenu* menu_from_name(gchar *name)
185 {
186     ObMenu *self = NULL;
187
188     g_assert(name != NULL);
189
190     if (!(self = g_hash_table_lookup(menu_hash, name)))
191         g_message(_("Attempted to access menu \"%s\" but it does not exist"),
192                   name);
193     return self;
194 }
195
196 #define VALID_SHORTCUT(c) (((c) >= '0' && (c) <= '9') || \
197                            ((c) >= 'A' && (c) <= 'Z') || \
198                            ((c) >= 'a' && (c) <= 'z'))
199
200 static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
201                                gchar **strippedlabel, guint *position,
202                                gboolean *always_show)
203 {
204     gunichar shortcut = 0;
205
206     *position = 0;
207     *always_show = FALSE;
208
209     g_assert(strippedlabel != NULL);
210
211     if (label == NULL) {
212         *strippedlabel = NULL;
213     } else {
214         gchar *i;
215         gboolean escape;
216
217         *strippedlabel = g_strdup(label);
218
219         /* if allow_shortcut is false, then you can't use the '_', instead you
220            have to just use the first valid character
221         */
222
223         /* allow __ to escape an underscore */
224         i = *strippedlabel;
225         do {
226             escape = FALSE;
227             i = strchr(i, '_');
228             if (i && *(i+1) == '_') {
229                 gchar *j;
230
231                 /* remove the escape '_' from the string */
232                 for (j = i; *j != '\0'; ++j)
233                     *j = *(j+1);
234
235                 ++i;
236                 escape = TRUE;
237             }
238         } while (escape);
239
240         if (allow_shortcut && i != NULL) {
241             /* there is an underscore in the string */
242
243             /* you have to use a printable ascii character for shortcuts
244                don't allow space either, so you can have like "a _ b"
245             */
246             if (VALID_SHORTCUT(*(i+1))) {
247                 shortcut = g_unichar_tolower(g_utf8_get_char(i+1));
248                 *position = i - *strippedlabel;
249                 *always_show = TRUE;
250
251                 /* remove the '_' from the string */
252                 for (; *i != '\0'; ++i)
253                     *i = *(i+1);
254             } else if (*(i+1) == '\0') {
255                 /* no default shortcut if the '_' is the last character
256                    (eg. "Exit_") for menu entries that you don't want
257                    to be executed by mistake
258                 */
259                     *i = '\0';
260             }
261         } else {
262             /* there is no underscore, so find the first valid character to use
263                instead */
264
265             for (i = *strippedlabel; *i != '\0'; ++i)
266                 if (VALID_SHORTCUT(*i)) {
267                     *position = i - *strippedlabel;
268                     shortcut = g_unichar_tolower(g_utf8_get_char(i));
269                     break;
270                 }
271         }
272     }
273     return shortcut;
274 }
275
276 static void parse_menu_item(xmlNodePtr node,  gpointer data)
277 {
278     ObMenuParseState *state = data;
279     gchar *label;
280     gchar *icon;
281     ObMenuEntry *e;
282
283     if (state->parent) {
284         /* Don't try to extract "icon" attribute if icons in user-defined
285            menus are not enabled. */
286
287         if (obt_xml_attr_string(node, "label", &label)) {
288             xmlNodePtr c;
289             xmlChar *cc;
290             ObActionList *acts = NULL;
291             ObActionParser *p;
292
293             c = obt_xml_find_sibling(node->children, "action");
294             p = action_parser_new();
295             while (c) {
296                 ObActionList *al;
297
298                 cc = xmlNodeGetContent(c);
299                 al = action_parser_read_string(p, (gchar*)cc);
300                 xmlFree(cc);
301                 acts = action_list_concat(acts, al);
302
303                 c = obt_xml_find_sibling(node->next, "action");
304             }
305             e = menu_add_normal(state->parent, -1, label, acts, TRUE);
306             action_list_unref(acts);
307             
308             if (config_menu_show_icons &&
309                 obt_xml_attr_string(node, "icon", &icon))
310             {
311                 e->data.normal.icon = RrImageNewFromName(ob_rr_icons, icon);
312
313                 if (e->data.normal.icon)
314                     e->data.normal.icon_alpha = 0xff;
315
316                 g_free(icon);
317             }
318             g_free(label);
319         }
320     }
321 }
322
323 static void parse_menu_separator(xmlNodePtr node, gpointer data)
324 {
325     ObMenuParseState *state = data;
326
327     if (state->parent) {
328         gchar *label;
329
330         if (!obt_xml_attr_string(node, "label", &label))
331             label = NULL;
332
333         menu_add_separator(state->parent, -1, label);
334         g_free(label);
335     }
336 }
337
338 static void parse_menu(xmlNodePtr node, gpointer data)
339 {
340     ObMenuParseState *state = data;
341     gchar *name = NULL, *title = NULL, *script = NULL;
342     ObMenu *menu;
343     ObMenuEntry *e;
344     gchar *icon;
345
346     if (!obt_xml_attr_string(node, "id", &name))
347         goto parse_menu_fail;
348
349     if (!g_hash_table_lookup(menu_hash, name)) {
350         if (!obt_xml_attr_string(node, "label", &title))
351             goto parse_menu_fail;
352
353         if ((menu = menu_new(name, title, TRUE, NULL))) {
354             menu->pipe_creator = state->pipe_creator;
355             if (obt_xml_attr_string(node, "execute", &script)) {
356                 menu->execute = obt_paths_expand_tilde(script);
357             } else {
358                 ObMenu *old;
359
360                 old = state->parent;
361                 state->parent = menu;
362                 obt_xml_tree(menu_parse_inst, node->children);
363                 state->parent = old;
364             }
365         }
366     }
367
368     if (state->parent) {
369         e = menu_add_submenu(state->parent, -1, name);
370
371         if (config_menu_show_icons &&
372             obt_xml_attr_string(node, "icon", &icon))
373         {
374             e->data.submenu.icon = RrImageNewFromName(ob_rr_icons, icon);
375
376             if (e->data.submenu.icon)
377                 e->data.submenu.icon_alpha = 0xff;
378
379             g_free(icon);
380         }
381     }
382
383 parse_menu_fail:
384     g_free(name);
385     g_free(title);
386     g_free(script);
387 }
388
389 ObMenu* menu_new(const gchar *name, const gchar *title,
390                  gboolean allow_shortcut_selection, gpointer data)
391 {
392     ObMenu *self;
393
394     self = g_slice_new0(ObMenu);
395     self->name = g_strdup(name);
396     self->data = data;
397
398     self->shortcut = parse_shortcut(title, allow_shortcut_selection,
399                                     &self->title, &self->shortcut_position,
400                                     &self->shortcut_always_show);
401     self->collate_key = g_utf8_collate_key(self->title, -1);
402
403     g_hash_table_replace(menu_hash, self->name, self);
404
405     /* Each menu has a single more_menu.  When the menu spills past what
406        can fit on the screen, a new menu frame entry is created from this
407        more_menu, and a new menu frame for the submenu is created for this
408        menu, also pointing to the more_menu.
409
410        This can be done multiple times using the same more_menu.
411
412        more_menu->more_menu will always be NULL, since there is only 1 for
413        each menu. */
414     self->more_menu = g_slice_new0(ObMenu);
415     self->more_menu->name = _("More...");
416     self->more_menu->title = _("More...");
417     self->more_menu->collate_key = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
418     self->more_menu->data = data;
419     self->more_menu->shortcut = g_unichar_tolower(g_utf8_get_char("M"));
420
421     return self;
422 }
423
424 static void menu_destroy_hash_value(ObMenu *self)
425 {
426     /* make sure its not visible */
427     {
428         GList *it;
429         ObMenuFrame *f;
430
431         for (it = menu_frame_visible; it; it = g_list_next(it)) {
432             f = it->data;
433             if (f->menu == self)
434                 menu_frame_hide_all();
435         }
436     }
437
438     if (self->destroy_func)
439         self->destroy_func(self, self->data);
440
441     menu_clear_entries(self);
442     g_free(self->name);
443     g_free(self->title);
444     g_free(self->collate_key);
445     g_free(self->execute);
446     g_slice_free(ObMenu, self->more_menu);
447
448     g_slice_free(ObMenu, self);
449 }
450
451 void menu_free(ObMenu *menu)
452 {
453     if (menu)
454         g_hash_table_remove(menu_hash, menu->name);
455 }
456
457 static gboolean menu_hide_delay_func(gpointer data)
458 {
459     menu_can_hide = TRUE;
460     menu_timeout_id = 0;
461     return FALSE; /* no repeat */
462 }
463
464 void menu_show(gchar *name, gint x, gint y, gboolean mouse, ObClient *client)
465 {
466     ObMenu *self;
467     ObMenuFrame *frame;
468
469     if (!(self = menu_from_name(name)) ||
470         grab_on_keyboard() || grab_on_pointer()) return;
471
472     /* if the requested menu is already the top visible menu, then don't
473        bother */
474     if (menu_frame_visible) {
475         frame = menu_frame_visible->data;
476         if (frame->menu == self)
477             return;
478     }
479
480     menu_frame_hide_all();
481
482     /* clear the pipe menus when showing a new menu */
483     menu_clear_pipe_caches();
484
485     frame = menu_frame_new(self, 0, client);
486     if (!menu_frame_show_topmenu(frame, x, y, mouse))
487         menu_frame_free(frame);
488     else {
489         if (!mouse) {
490             /* select the first entry if it's not a submenu and we opened
491              * the menu with the keyboard, and skip all headers */
492             GList *it = frame->entries;
493             while (it) {
494                 ObMenuEntryFrame *e = it->data;
495                 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
496                     menu_frame_select(frame, e, FALSE);
497                     break;
498                 } else if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
499                     it = g_list_next(it);
500                 else
501                     break;
502             }
503         }
504
505         /* reset the hide timer */
506         if (!mouse)
507             menu_can_hide = TRUE;
508         else {
509             menu_can_hide = FALSE;
510             if (menu_timeout_id) g_source_remove(menu_timeout_id);
511             menu_timeout_id = g_timeout_add_full(G_PRIORITY_DEFAULT,
512                                                  config_menu_hide_delay,
513                                                  menu_hide_delay_func,
514                                                  NULL, NULL);
515         }
516     }
517 }
518
519 gboolean menu_hide_delay_reached(void)
520 {
521     return menu_can_hide;
522 }
523
524 static ObMenuEntry* menu_entry_new(ObMenu *menu, ObMenuEntryType type, gint id)
525 {
526     ObMenuEntry *self;
527
528     g_assert(menu);
529
530     self = g_slice_new0(ObMenuEntry);
531     self->ref = 1;
532     self->type = type;
533     self->menu = menu;
534     self->id = id;
535
536     switch (type) {
537     case OB_MENU_ENTRY_TYPE_NORMAL:
538         self->data.normal.enabled = TRUE;
539         break;
540     case OB_MENU_ENTRY_TYPE_SUBMENU:
541     case OB_MENU_ENTRY_TYPE_SEPARATOR:
542         break;
543     }
544
545     return self;
546 }
547
548 void menu_entry_ref(ObMenuEntry *self)
549 {
550     ++self->ref;
551 }
552
553 void menu_entry_unref(ObMenuEntry *self)
554 {
555     if (self && --self->ref == 0) {
556         switch (self->type) {
557         case OB_MENU_ENTRY_TYPE_NORMAL:
558             RrImageUnref(self->data.normal.icon);
559             g_free(self->data.normal.label);
560             g_free(self->data.normal.collate_key);
561             action_list_unref(self->data.normal.actions);
562             break;
563         case OB_MENU_ENTRY_TYPE_SUBMENU:
564             RrImageUnref(self->data.submenu.icon);
565             g_free(self->data.submenu.name);
566             break;
567         case OB_MENU_ENTRY_TYPE_SEPARATOR:
568             g_free(self->data.separator.label);
569             break;
570         }
571
572         g_slice_free(ObMenuEntry, self);
573     }
574 }
575
576 void menu_clear_entries(ObMenu *self)
577 {
578 #ifdef DEBUG
579     /* assert that the menu isn't visible */
580     {
581         GList *it;
582         ObMenuFrame *f;
583
584         for (it = menu_frame_visible; it; it = g_list_next(it)) {
585             f = it->data;
586             g_assert(f->menu != self);
587         }
588     }
589 #endif
590
591     while (self->entries) {
592         menu_entry_unref(self->entries->data);
593         self->entries = g_list_delete_link(self->entries, self->entries);
594     }
595     self->more_menu->entries = self->entries; /* keep it in sync */
596 }
597
598 void menu_entry_remove(ObMenuEntry *self)
599 {
600     self->menu->entries = g_list_remove(self->menu->entries, self);
601     menu_entry_unref(self);
602 }
603
604 ObMenuEntry* menu_add_normal(ObMenu *self, gint id, const gchar *label,
605                              ObActionList *actions, gboolean allow_shortcut)
606 {
607     ObMenuEntry *e;
608
609     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_NORMAL, id);
610     e->data.normal.actions = actions;
611     action_list_ref(actions);
612
613     menu_entry_set_label(e, label, allow_shortcut);
614
615     self->entries = g_list_append(self->entries, e);
616     self->more_menu->entries = self->entries; /* keep it in sync */
617     return e;
618 }
619
620 ObMenuEntry* menu_get_more(ObMenu *self, guint show_from)
621 {
622     ObMenuEntry *e;
623     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, -1);
624     /* points to itself */
625     e->data.submenu.name = g_strdup(self->name);
626     e->data.submenu.submenu = self;
627     e->data.submenu.show_from = show_from;
628     return e;
629 }
630
631 ObMenuEntry* menu_add_submenu(ObMenu *self, gint id, const gchar *submenu)
632 {
633     ObMenuEntry *e;
634
635     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SUBMENU, id);
636     e->data.submenu.name = g_strdup(submenu);
637
638     self->entries = g_list_append(self->entries, e);
639     self->more_menu->entries = self->entries; /* keep it in sync */
640     return e;
641 }
642
643 ObMenuEntry* menu_add_separator(ObMenu *self, gint id, const gchar *label)
644 {
645     ObMenuEntry *e;
646
647     e = menu_entry_new(self, OB_MENU_ENTRY_TYPE_SEPARATOR, id);
648
649     menu_entry_set_label(e, label, FALSE);
650
651     self->entries = g_list_append(self->entries, e);
652     self->more_menu->entries = self->entries; /* keep it in sync */
653     return e;
654 }
655
656 void menu_set_show_func(ObMenu *self, ObMenuShowFunc func)
657 {
658     self->show_func = func;
659 }
660
661 void menu_set_hide_func(ObMenu *self, ObMenuHideFunc func)
662 {
663     self->hide_func = func;
664 }
665
666 void menu_set_update_func(ObMenu *self, ObMenuUpdateFunc func)
667 {
668     self->update_func = func;
669 }
670
671 void menu_set_execute_func(ObMenu *self, ObMenuExecuteFunc func)
672 {
673     self->execute_func = func;
674     self->more_menu->execute_func = func; /* keep it in sync */
675 }
676
677 void menu_set_cleanup_func(ObMenu *self, ObMenuCleanupFunc func)
678 {
679     self->cleanup_func = func;
680 }
681
682 void menu_set_destroy_func(ObMenu *self, ObMenuDestroyFunc func)
683 {
684     self->destroy_func = func;
685 }
686
687 void menu_set_place_func(ObMenu *self, ObMenuPlaceFunc func)
688 {
689     self->place_func = func;
690 }
691
692 ObMenuEntry* menu_find_entry_id(ObMenu *self, gint id)
693 {
694     ObMenuEntry *ret = NULL;
695     GList *it;
696
697     for (it = self->entries; it; it = g_list_next(it)) {
698         ObMenuEntry *e = it->data;
699
700         if (e->id == id) {
701             ret = e;
702             break;
703         }
704     }
705     return ret;
706 }
707
708 void menu_find_submenus(ObMenu *self)
709 {
710     GList *it;
711
712     for (it = self->entries; it; it = g_list_next(it)) {
713         ObMenuEntry *e = it->data;
714
715         if (e->type == OB_MENU_ENTRY_TYPE_SUBMENU)
716             e->data.submenu.submenu = menu_from_name(e->data.submenu.name);
717     }
718 }
719
720 void menu_entry_set_label(ObMenuEntry *self, const gchar *label,
721                           gboolean allow_shortcut)
722 {
723     switch (self->type) {
724     case OB_MENU_ENTRY_TYPE_SEPARATOR:
725         g_free(self->data.separator.label);
726         self->data.separator.label = g_strdup(label);
727         break;
728     case OB_MENU_ENTRY_TYPE_NORMAL:
729         g_free(self->data.normal.label);
730         g_free(self->data.normal.collate_key);
731         self->data.normal.shortcut =
732             parse_shortcut(label, allow_shortcut, &self->data.normal.label,
733                            &self->data.normal.shortcut_position,
734                            &self->data.normal.shortcut_always_show);
735         self->data.normal.collate_key =
736             g_utf8_collate_key(self->data.normal.label, -1);
737         break;
738     default:
739         g_assert_not_reached();
740     }
741 }
742
743 void menu_show_all_shortcuts(ObMenu *self, gboolean show)
744 {
745     self->show_all_shortcuts = show;
746 }
747
748 static int sort_func(const void *a, const void *b) {
749     const ObMenuEntry *e[2] = {*(ObMenuEntry**)a, *(ObMenuEntry**)b};
750     const gchar *k[2];
751     gint i;
752
753     for (i = 0; i < 2; ++i) {
754         if (e[i]->type == OB_MENU_ENTRY_TYPE_NORMAL)
755             k[i] = e[i]->data.normal.collate_key;
756         else {
757             g_assert(e[i]->type == OB_MENU_ENTRY_TYPE_SUBMENU);
758             if (e[i]->data.submenu.submenu)
759                 k[i] = e[i]->data.submenu.submenu->collate_key;
760             else
761                 return -1; /* arbitrary really.. the submenu doesn't exist. */
762         }
763     }
764     return strcmp(k[0], k[1]);
765 }
766
767 /*!
768   @param start The first entry in the range to sort.
769   @param end The last entry in the range to sort.
770 */
771 static void sort_range(ObMenu *self, GList *start, GList *end, guint len)
772 {
773     ObMenuEntry **ar;
774     GList *it;
775     guint i;
776     if (!len) return;
777
778     ar = g_slice_alloc(sizeof(ObMenuEntry*) * len);
779     for (i = 0, it = start; it != g_list_next(end); ++i, it = g_list_next(it))
780         ar[i] = it->data;
781     qsort(ar, len, sizeof(ObMenuEntry*), sort_func);
782     for (i = 0, it = start; it != g_list_next(end); ++i, it = g_list_next(it))
783         it->data = ar[i];
784     g_slice_free1(sizeof(ObMenuEntry*) * len, ar);
785 }
786
787 void menu_sort_entries(ObMenu *self)
788 {
789     GList *it, *start, *end, *last;
790     guint len;
791
792     /* need the submenus to know their labels for sorting */
793     menu_find_submenus(self);
794
795     start = self->entries;
796     len = 0;
797     for (it = self->entries; it; it = g_list_next(it)) {
798         ObMenuEntry *e = it->data;
799         if (e->type == OB_MENU_ENTRY_TYPE_SEPARATOR) {
800             end = g_list_previous(it);
801             sort_range(self, start, end, len);
802
803             it = g_list_next(it); /* skip over the separator */
804             start = it;
805             len = 0;
806         }
807         else
808             len += 1;
809         last = it;
810     }
811     sort_range(self, start, last, len);
812 }