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