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