]> Pileus Git - ~andy/gtk/blob - gtk/gtkaccelmap.c
Practically everything changed.
[~andy/gtk] / gtk / gtkaccelmap.c
1 /* GTK - The GTK+ Toolkit
2  * Copyright (C) 1998, 2001 Tim Janik
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include "gtkaccelmap.h"
23
24 #include "gtkmarshalers.h"
25 #include "gtkwindow.h"  /* in lack of GtkAcceleratable */
26 #include "gtkintl.h" 
27 #include "gtkalias.h"
28
29 #include <glib/gstdio.h>
30
31 #include <string.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #ifdef G_OS_WIN32
38 #include <io.h>
39 #endif
40
41
42 /* --- structures --- */
43 struct _GtkAccelMap
44 {
45   GObject parent_instance;
46 };
47
48 struct _GtkAccelMapClass
49 {
50   GObjectClass parent_class;
51 };
52
53 typedef struct {
54   const gchar *accel_path;
55   guint        accel_key;
56   guint        accel_mods;
57   guint        std_accel_key;
58   guint        std_accel_mods;
59   guint        changed    :  1;
60   guint        lock_count : 15;
61   GSList      *groups;
62 } AccelEntry;
63
64 /* --- signals --- */
65 enum {
66   CHANGED,
67   LAST_SIGNAL
68 };
69
70 /* --- variables --- */
71
72 static GHashTable  *accel_entry_ht = NULL;      /* accel_path -> AccelEntry */
73 static GSList      *accel_filters = NULL;
74 static gulong       accel_map_signals[LAST_SIGNAL] = { 0, };
75 static GtkAccelMap *accel_map;
76
77 /* --- prototypes --- */
78 static void do_accel_map_changed (AccelEntry *entry);
79
80 /* --- functions --- */
81 static guint
82 accel_entry_hash (gconstpointer key)
83 {
84   const AccelEntry *entry = key;
85
86   return g_str_hash (entry->accel_path);
87 }
88
89 static gboolean
90 accel_entry_equal (gconstpointer key1,
91                    gconstpointer key2)
92 {
93   const AccelEntry *entry1 = key1;
94   const AccelEntry *entry2 = key2;
95
96   return g_str_equal (entry1->accel_path, entry2->accel_path);
97 }
98
99 static inline AccelEntry*
100 accel_path_lookup (const gchar *accel_path)
101 {
102   AccelEntry ekey;
103
104   ekey.accel_path = accel_path;
105
106   /* safety NULL check for return_if_fail()s */
107   return accel_path ? g_hash_table_lookup (accel_entry_ht, &ekey) : NULL;
108 }
109
110 void
111 _gtk_accel_map_init (void)
112 {
113   g_assert (accel_entry_ht == NULL);
114
115   accel_entry_ht = g_hash_table_new (accel_entry_hash, accel_entry_equal);
116 }
117
118 gboolean
119 _gtk_accel_path_is_valid (const gchar *accel_path)
120 {
121   gchar *p;
122
123   if (!accel_path || accel_path[0] != '<' ||
124       accel_path[1] == '<' || accel_path[1] == '>' || !accel_path[1])
125     return FALSE;
126   p = strchr (accel_path, '>');
127   if (!p || (p[1] != 0 && p[1] != '/'))
128     return FALSE;
129   return TRUE;
130 }
131
132 /**
133  * gtk_accel_map_add_entry:
134  * @accel_path: valid accelerator path
135  * @accel_key:  the accelerator key
136  * @accel_mods: the accelerator modifiers
137  *
138  * Registers a new accelerator with the global accelerator map.
139  * This function should only be called once per @accel_path
140  * with the canonical @accel_key and @accel_mods for this path.
141  * To change the accelerator during runtime programatically, use
142  * gtk_accel_map_change_entry().
143  * The accelerator path must consist of "&lt;WINDOWTYPE&gt;/Category1/Category2/.../Action",
144  * where &lt;WINDOWTYPE&gt; should be a unique application-specific identifier, that
145  * corresponds to the kind of window the accelerator is being used in, e.g. "Gimp-Image",
146  * "Abiword-Document" or "Gnumeric-Settings".
147  * The Category1/.../Action portion is most appropriately chosen by the action the
148  * accelerator triggers, i.e. for accelerators on menu items, choose the item's menu path,
149  * e.g. "File/Save As", "Image/View/Zoom" or "Edit/Select All".
150  * So a full valid accelerator path may look like:
151  * "&lt;Gimp-Toolbox&gt;/File/Dialogs/Tool Options...".
152  * 
153  * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
154  * pass a static string, you can save some memory by interning it first with 
155  * g_intern_static_string().
156  */
157 void
158 gtk_accel_map_add_entry (const gchar    *accel_path,
159                          guint           accel_key,
160                          GdkModifierType accel_mods)
161 {
162   AccelEntry *entry;
163
164   g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
165
166   if (!accel_key)
167     accel_mods = 0;
168   else
169     accel_mods &= gtk_accelerator_get_default_mod_mask ();
170
171   entry = accel_path_lookup (accel_path);
172   if (entry)
173     {
174       if (!entry->std_accel_key && !entry->std_accel_mods &&
175           (accel_key || accel_mods))
176         {
177           entry->std_accel_key = accel_key;
178           entry->std_accel_mods = accel_mods;
179           if (!entry->changed)
180             gtk_accel_map_change_entry (entry->accel_path, accel_key, accel_mods, TRUE);
181         }
182     }
183   else
184     {
185       entry = g_slice_new0 (AccelEntry);
186       entry->accel_path = g_intern_string (accel_path);
187       entry->std_accel_key = accel_key;
188       entry->std_accel_mods = accel_mods;
189       entry->accel_key = accel_key;
190       entry->accel_mods = accel_mods;
191       entry->changed = FALSE;
192       entry->lock_count = 0;
193       g_hash_table_insert (accel_entry_ht, entry, entry);
194
195       do_accel_map_changed (entry);
196     }
197 }
198
199 /**
200  * gtk_accel_map_lookup_entry:
201  * @accel_path:  a valid accelerator path
202  * @key:         the accelerator key to be filled in (optional)
203  * @returns:     %TRUE if @accel_path is known, %FALSE otherwise
204  *
205  * Looks up the accelerator entry for @accel_path and fills in @key.
206  */
207 gboolean
208 gtk_accel_map_lookup_entry (const gchar *accel_path,
209                             GtkAccelKey *key)
210 {
211   AccelEntry *entry;
212
213   g_return_val_if_fail (_gtk_accel_path_is_valid (accel_path), FALSE);
214
215   entry = accel_path_lookup (accel_path);
216   if (entry && key)
217     {
218       key->accel_key = entry->accel_key;
219       key->accel_mods = entry->accel_mods;
220       key->accel_flags = 0;
221     }
222
223   return entry ? TRUE : FALSE;
224 }
225
226 static void
227 hash2slist_foreach (gpointer  key,
228                     gpointer  value,
229                     gpointer  user_data)
230 {
231   GSList **slist_p = user_data;
232
233   *slist_p = g_slist_prepend (*slist_p, value);
234 }
235
236 static GSList*
237 g_hash_table_slist_values (GHashTable *hash_table)
238 {
239   GSList *slist = NULL;
240
241   g_return_val_if_fail (hash_table != NULL, NULL);
242
243   g_hash_table_foreach (hash_table, hash2slist_foreach, &slist);
244
245   return slist;
246 }
247
248 /* if simulate==TRUE, return whether accel_path can be changed to
249  * accel_key && accel_mods. otherwise, return whether accel_path
250  * was actually changed.
251  */
252 static gboolean
253 internal_change_entry (const gchar    *accel_path,
254                        guint           accel_key,
255                        GdkModifierType accel_mods,
256                        gboolean        replace,
257                        gboolean        simulate)
258 {
259   GSList *node, *slist, *win_list, *group_list, *replace_list = NULL;
260   GHashTable *group_hm, *window_hm;
261   gboolean change_accel, removable, can_change = TRUE, seen_accel = FALSE;
262   GQuark entry_quark;
263   AccelEntry *entry = accel_path_lookup (accel_path);
264
265   /* not much todo if there's no entry yet */
266   if (!entry)
267     {
268       if (!simulate)
269         {
270           gtk_accel_map_add_entry (accel_path, 0, 0);
271           entry = accel_path_lookup (accel_path);
272           entry->accel_key = accel_key;
273           entry->accel_mods = accel_mods;
274           entry->changed = TRUE;
275
276           do_accel_map_changed (entry);
277         }
278       return TRUE;
279     }
280
281   /* if there's nothing to change, not much todo either */
282   if (entry->accel_key == accel_key && entry->accel_mods == accel_mods)
283     {
284       if (!simulate)
285         entry->changed = TRUE;
286       return simulate ? TRUE : FALSE;
287     }
288
289   /* The no-change case has already been handled, so 
290    * simulate doesn't make a difference here.
291    */
292   if (entry->lock_count > 0)
293     return FALSE;
294
295   /* nobody's interested, easy going */
296   if (!entry->groups)
297     {
298       if (!simulate)
299         {
300           entry->accel_key = accel_key;
301           entry->accel_mods = accel_mods;
302           entry->changed = TRUE;
303
304           do_accel_map_changed (entry);
305         }
306       return TRUE;
307     }
308
309   /* 1) fetch all accel groups affected by this entry */
310   entry_quark = g_quark_try_string (entry->accel_path);
311   group_hm = g_hash_table_new (NULL, NULL);
312   window_hm = g_hash_table_new (NULL, NULL);
313   for (slist = entry->groups; slist; slist = slist->next)
314     g_hash_table_insert (group_hm, slist->data, slist->data);
315
316   /* 2) collect acceleratables affected */
317   group_list = g_hash_table_slist_values (group_hm);
318   for (slist = group_list; slist; slist = slist->next)
319     {
320       GtkAccelGroup *group = slist->data;
321
322       for (node = group->acceleratables; node; node = node->next)
323         g_hash_table_insert (window_hm, node->data, node->data);
324     }
325   g_slist_free (group_list);
326
327   /* 3) include all accel groups used by acceleratables */
328   win_list = g_hash_table_slist_values (window_hm);
329   g_hash_table_destroy (window_hm);
330   for (slist = win_list; slist; slist = slist->next)
331     for (node = gtk_accel_groups_from_object (slist->data); node; node = node->next)
332       g_hash_table_insert (group_hm, node->data, node->data);
333   group_list = g_hash_table_slist_values (group_hm);
334   g_hash_table_destroy (group_hm);
335   
336   /* 4) walk the acceleratables and figure whether they occupy accel_key&accel_mods */
337   if (accel_key)
338     for (slist = win_list; slist; slist = slist->next)
339       if (GTK_IS_WINDOW (slist->data))  /* bad kludge in lack of a GtkAcceleratable */
340         if (_gtk_window_query_nonaccels (slist->data, accel_key, accel_mods))
341           {
342             seen_accel = TRUE;
343             break;
344           }
345   removable = !seen_accel;
346   
347   /* 5) walk all accel groups and search for locks */
348   if (removable)
349     for (slist = group_list; slist; slist = slist->next)
350       {
351         GtkAccelGroup *group = slist->data;
352         GtkAccelGroupEntry *ag_entry;
353         guint i, n;
354         
355         n = 0;
356         ag_entry = entry->accel_key ? gtk_accel_group_query (group, entry->accel_key, entry->accel_mods, &n) : NULL;
357         for (i = 0; i < n; i++)
358           if (ag_entry[i].accel_path_quark == entry_quark)
359             {
360               can_change = !(ag_entry[i].key.accel_flags & GTK_ACCEL_LOCKED);
361               if (!can_change)
362                 goto break_loop_step5;
363             }
364         
365         n = 0;
366         ag_entry = accel_key ? gtk_accel_group_query (group, accel_key, accel_mods, &n) : NULL;
367         for (i = 0; i < n; i++)
368           {
369             seen_accel = TRUE;
370             removable = !group->lock_count && !(ag_entry[i].key.accel_flags & GTK_ACCEL_LOCKED);
371             if (!removable)
372               goto break_loop_step5;
373             if (ag_entry[i].accel_path_quark)
374               replace_list = g_slist_prepend (replace_list, GUINT_TO_POINTER (ag_entry[i].accel_path_quark));
375           }
376       }
377  break_loop_step5:
378   
379   /* 6) check whether we can remove existing accelerators */
380   if (removable && can_change)
381     for (slist = replace_list; slist; slist = slist->next)
382       if (!internal_change_entry (g_quark_to_string (GPOINTER_TO_UINT (slist->data)), 0, 0, FALSE, TRUE))
383         {
384           removable = FALSE;
385           break;
386         }
387   
388   /* 7) check conditions and proceed if possible */
389   change_accel = can_change && (!seen_accel || (removable && replace));
390   
391   if (change_accel && !simulate)
392     {
393       /* ref accel groups */
394       for (slist = group_list; slist; slist = slist->next)
395         g_object_ref (slist->data);
396
397       /* 8) remove existing accelerators */
398       for (slist = replace_list; slist; slist = slist->next)
399         internal_change_entry (g_quark_to_string (GPOINTER_TO_UINT (slist->data)), 0, 0, FALSE, FALSE);
400
401       /* 9) install new accelerator */
402       entry->accel_key = accel_key;
403       entry->accel_mods = accel_mods;
404       entry->changed = TRUE;
405
406       for (slist = group_list; slist; slist = slist->next)
407         _gtk_accel_group_reconnect (slist->data, g_quark_from_string (entry->accel_path));
408
409       /* unref accel groups */
410       for (slist = group_list; slist; slist = slist->next)
411         g_object_unref (slist->data);
412
413       do_accel_map_changed (entry);
414     }
415   g_slist_free (replace_list);
416   g_slist_free (group_list);
417   g_slist_free (win_list);
418
419   return change_accel;
420 }
421
422 /**
423  * gtk_accel_map_change_entry:
424  * @accel_path:  a valid accelerator path
425  * @accel_key:   the new accelerator key
426  * @accel_mods:  the new accelerator modifiers
427  * @replace:     %TRUE if other accelerators may be deleted upon conflicts
428  * @returns:     %TRUE if the accelerator could be changed, %FALSE otherwise
429  *
430  * Changes the @accel_key and @accel_mods currently associated with @accel_path.
431  * Due to conflicts with other accelerators, a change may not always be possible,
432  * @replace indicates whether other accelerators may be deleted to resolve such
433  * conflicts. A change will only occur if all conflicts could be resolved (which
434  * might not be the case if conflicting accelerators are locked). Successful
435  * changes are indicated by a %TRUE return value.
436  * 
437  * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
438  * pass a static string, you can save some memory by interning it first with 
439  * g_intern_static_string().
440  */
441 gboolean
442 gtk_accel_map_change_entry (const gchar    *accel_path,
443                             guint           accel_key,
444                             GdkModifierType accel_mods,
445                             gboolean        replace)
446 {
447   g_return_val_if_fail (_gtk_accel_path_is_valid (accel_path), FALSE);
448
449   return internal_change_entry (accel_path, accel_key, accel_key ? accel_mods : 0, replace, FALSE);
450 }
451
452 static guint
453 accel_map_parse_accel_path (GScanner *scanner)
454 {
455   guint accel_key = 0;
456   GdkModifierType accel_mods = 0;
457   gchar *path, *accel;
458   
459   /* parse accel path */
460   g_scanner_get_next_token (scanner);
461   if (scanner->token != G_TOKEN_STRING)
462     return G_TOKEN_STRING;
463
464   /* test if the next token is an accelerator */
465   g_scanner_peek_next_token (scanner);
466   if (scanner->next_token != G_TOKEN_STRING)
467     {
468       /* if not so, eat that token and error out */
469       g_scanner_get_next_token (scanner);
470       return G_TOKEN_STRING;
471     }
472
473   /* get the full accelerator specification */
474   path = g_strdup (scanner->value.v_string);
475   g_scanner_get_next_token (scanner);
476   accel = g_strdup (scanner->value.v_string);
477
478   /* ensure the entry is present */
479   gtk_accel_map_add_entry (path, 0, 0);
480
481   /* and propagate it */
482   gtk_accelerator_parse (accel, &accel_key, &accel_mods);
483   gtk_accel_map_change_entry (path, accel_key, accel_mods, TRUE);
484
485   g_free (accel);
486   g_free (path);
487
488   /* check correct statement end */
489   g_scanner_get_next_token (scanner);
490   if (scanner->token != ')')
491     return ')';
492   else
493     return G_TOKEN_NONE;
494 }
495
496 static void
497 accel_map_parse_statement (GScanner *scanner)
498 {
499   guint expected_token;
500
501   g_scanner_get_next_token (scanner);
502
503   if (scanner->token == G_TOKEN_SYMBOL)
504     {
505       guint (*parser_func) (GScanner*);
506
507       parser_func = (guint (*) (GScanner *))scanner->value.v_symbol;
508
509       expected_token = parser_func (scanner);
510     }
511   else
512     expected_token = G_TOKEN_SYMBOL;
513
514   /* skip rest of statement on errrors
515    */
516   if (expected_token != G_TOKEN_NONE)
517     {
518       register guint level;
519
520       level = 1;
521       if (scanner->token == ')')
522         level--;
523       if (scanner->token == '(')
524         level++;
525
526       while (!g_scanner_eof (scanner) && level > 0)
527         {
528           g_scanner_get_next_token (scanner);
529
530           if (scanner->token == '(')
531             level++;
532           else if (scanner->token == ')')
533             level--;
534         }
535     }
536 }
537
538 /**
539  * gtk_accel_map_load_scanner:
540  * @scanner: a #GScanner which has already been provided with an input file
541  *
542  * #GScanner variant of gtk_accel_map_load().
543  */
544 void
545 gtk_accel_map_load_scanner (GScanner *scanner)
546 {
547   gboolean skip_comment_single;
548   gboolean symbol_2_token;
549   gchar *cpair_comment_single;
550   gpointer saved_symbol;
551   
552   g_return_if_fail (scanner != NULL);
553
554   /* configure scanner */
555   skip_comment_single = scanner->config->skip_comment_single;
556   scanner->config->skip_comment_single = TRUE;
557   cpair_comment_single = scanner->config->cpair_comment_single;
558   scanner->config->cpair_comment_single = ";\n";
559   symbol_2_token = scanner->config->symbol_2_token;
560   scanner->config->symbol_2_token = FALSE;
561   saved_symbol = g_scanner_lookup_symbol (scanner, "gtk_accel_path");
562   g_scanner_scope_add_symbol (scanner, 0, "gtk_accel_path", 
563                               accel_map_parse_accel_path);
564
565   /* outer parsing loop
566    */
567   g_scanner_peek_next_token (scanner);
568   while (scanner->next_token == '(')
569     {
570       g_scanner_get_next_token (scanner);
571
572       accel_map_parse_statement (scanner);
573
574       g_scanner_peek_next_token (scanner);
575     }
576
577   /* restore config */
578   scanner->config->skip_comment_single = skip_comment_single;
579   scanner->config->cpair_comment_single = cpair_comment_single;
580   scanner->config->symbol_2_token = symbol_2_token;
581   g_scanner_scope_remove_symbol (scanner, 0, "gtk_accel_path");
582   if (saved_symbol)
583     g_scanner_scope_add_symbol (scanner, 0, "gtk_accel_path", saved_symbol);
584 }
585
586 /**
587  * gtk_accel_map_load_fd:
588  * @fd: a valid readable file descriptor
589  *
590  * Filedescriptor variant of gtk_accel_map_load().
591  *
592  * Note that the file descriptor will not be closed by this function.
593  */
594 void
595 gtk_accel_map_load_fd (gint fd)
596 {
597   GScanner *scanner;
598
599   g_return_if_fail (fd >= 0);
600
601   /* create and setup scanner */
602   scanner = g_scanner_new (NULL);
603   g_scanner_input_file (scanner, fd);
604
605   gtk_accel_map_load_scanner (scanner);
606
607   g_scanner_destroy (scanner);
608 }
609
610 /**
611  * gtk_accel_map_load:
612  * @file_name: a file containing accelerator specifications,
613  *   in the GLib file name encoding
614  *
615  * Parses a file previously saved with gtk_accel_map_save() for
616  * accelerator specifications, and propagates them accordingly.
617  */
618 void
619 gtk_accel_map_load (const gchar *file_name)
620 {
621   gint fd;
622
623   g_return_if_fail (file_name != NULL);
624
625   if (!g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
626     return;
627
628   fd = g_open (file_name, O_RDONLY, 0);
629   if (fd < 0)
630     return;
631
632   gtk_accel_map_load_fd (fd);
633
634   close (fd);
635 }
636
637 static gboolean
638 write_all (gint   fd,
639            gchar *buf,
640            gsize  to_write)
641 {
642   while (to_write > 0)
643     {
644       gssize count = write (fd, buf, to_write);
645       if (count < 0)
646         {
647           if (errno != EINTR)
648             return FALSE;
649         }
650       else
651         {
652           to_write -= count;
653           buf += count;
654         }
655     }
656
657   return TRUE;
658 }
659
660 static void
661 accel_map_print (gpointer        data,
662                  const gchar    *accel_path,
663                  guint           accel_key,
664                  GdkModifierType accel_mods,
665                  gboolean        changed)
666 {
667   GString *gstring = g_string_new (changed ? NULL : "; ");
668   gint fd = GPOINTER_TO_INT (data);
669   gchar *tmp, *name;
670
671   g_string_append (gstring, "(gtk_accel_path \"");
672
673   tmp = g_strescape (accel_path, NULL);
674   g_string_append (gstring, tmp);
675   g_free (tmp);
676
677   g_string_append (gstring, "\" \"");
678
679   name = gtk_accelerator_name (accel_key, accel_mods);
680   tmp = g_strescape (name, NULL);
681   g_free (name);
682   g_string_append (gstring, tmp);
683   g_free (tmp);
684
685   g_string_append (gstring, "\")\n");
686
687   write_all (fd, gstring->str, gstring->len);
688
689   g_string_free (gstring, TRUE);
690 }
691
692 /**
693  * gtk_accel_map_save_fd:
694  * @fd: a valid writable file descriptor
695  *
696  * Filedescriptor variant of gtk_accel_map_save().
697  *
698  * Note that the file descriptor will not be closed by this function.
699  */
700 void
701 gtk_accel_map_save_fd (gint fd)
702 {
703   GString *gstring;
704
705   g_return_if_fail (fd >= 0);
706
707   gstring = g_string_new ("; ");
708   if (g_get_prgname ())
709     g_string_append (gstring, g_get_prgname ());
710   g_string_append (gstring, " GtkAccelMap rc-file         -*- scheme -*-\n");
711   g_string_append (gstring, "; this file is an automated accelerator map dump\n");
712   g_string_append (gstring, ";\n");
713
714   write_all (fd, gstring->str, gstring->len);
715   
716   g_string_free (gstring, TRUE);
717
718   gtk_accel_map_foreach (GINT_TO_POINTER (fd), accel_map_print);
719 }
720
721 /**
722  * gtk_accel_map_save:
723  * @file_name: the name of the file to contain accelerator specifications,
724  *   in the GLib file name encoding
725  *
726  * Saves current accelerator specifications (accelerator path, key
727  * and modifiers) to @file_name.
728  * The file is written in a format suitable to be read back in by
729  * gtk_accel_map_load().
730  */
731 void
732 gtk_accel_map_save (const gchar *file_name)
733 {
734   gint fd;
735
736   g_return_if_fail (file_name != NULL);
737
738   fd = g_open (file_name, O_CREAT | O_TRUNC | O_WRONLY, 0644);
739   if (fd < 0)
740     return;
741
742   gtk_accel_map_save_fd (fd);
743
744   close (fd);
745 }
746
747 /**
748  * gtk_accel_map_foreach:
749  * @data:         data to be passed into @foreach_func
750  * @foreach_func: function to be executed for each accel map entry which
751  *                is not filtered out
752  *
753  * Loops over the entries in the accelerator map whose accel path 
754  * doesn't match any of the filters added with gtk_accel_map_add_filter(), 
755  * and execute @foreach_func on each. The signature of @foreach_func is 
756  * that of #GtkAccelMapForeach, the @changed parameter indicates whether
757  * this accelerator was changed during runtime (thus, would need
758  * saving during an accelerator map dump).
759  */
760 void
761 gtk_accel_map_foreach (gpointer           data,
762                        GtkAccelMapForeach foreach_func)
763 {
764   GSList *entries, *slist, *node;
765
766   g_return_if_fail (foreach_func != NULL);
767
768   entries = g_hash_table_slist_values (accel_entry_ht);
769   for (slist = entries; slist; slist = slist->next)
770     {
771       AccelEntry *entry = slist->data;
772       gboolean changed = entry->accel_key != entry->std_accel_key || entry->accel_mods != entry->std_accel_mods;
773
774       for (node = accel_filters; node; node = node->next)
775         if (g_pattern_match_string (node->data, entry->accel_path))
776           goto skip_accel;
777       foreach_func (data, entry->accel_path, entry->accel_key, entry->accel_mods, changed);
778     skip_accel:
779       /* noop */;
780     }
781   g_slist_free (entries);
782 }
783
784 /**
785  * gtk_accel_map_foreach_unfiltered:
786  * @data:         data to be passed into @foreach_func
787  * @foreach_func: function to be executed for each accel map entry
788  *
789  * Loops over all entries in the accelerator map, and execute
790  * @foreach_func on each. The signature of @foreach_func is that of
791  * #GtkAccelMapForeach, the @changed parameter indicates whether
792  * this accelerator was changed during runtime (thus, would need
793  * saving during an accelerator map dump).
794  */
795 void
796 gtk_accel_map_foreach_unfiltered (gpointer           data,
797                                   GtkAccelMapForeach foreach_func)
798 {
799   GSList *entries, *slist;
800
801   g_return_if_fail (foreach_func != NULL);
802
803   entries = g_hash_table_slist_values (accel_entry_ht);
804   for (slist = entries; slist; slist = slist->next)
805     {
806       AccelEntry *entry = slist->data;
807       gboolean changed = entry->accel_key != entry->std_accel_key || entry->accel_mods != entry->std_accel_mods;
808
809       foreach_func (data, entry->accel_path, entry->accel_key, entry->accel_mods, changed);
810     }
811   g_slist_free (entries);
812 }
813
814 /**
815  * gtk_accel_map_add_filter:
816  * @filter_pattern: a pattern (see #GPatternSpec)
817  *
818  * Adds a filter to the global list of accel path filters.
819  *
820  * Accel map entries whose accel path matches one of the filters
821  * are skipped by gtk_accel_map_foreach().
822  *
823  * This function is intended for GTK+ modules that create their own
824  * menus, but don't want them to be saved into the applications accelerator
825  * map dump.
826  */
827 void
828 gtk_accel_map_add_filter (const gchar *filter_pattern)
829 {
830   GPatternSpec *pspec;
831   GSList *slist;
832
833   g_return_if_fail (filter_pattern != NULL);
834
835   pspec = g_pattern_spec_new (filter_pattern);
836   for (slist = accel_filters; slist; slist = slist->next)
837     if (g_pattern_spec_equal (pspec, slist->data))
838       {
839         g_pattern_spec_free (pspec);
840         return;
841       }
842   accel_filters = g_slist_prepend (accel_filters, pspec);
843 }
844
845 void
846 _gtk_accel_map_add_group (const gchar   *accel_path,
847                           GtkAccelGroup *accel_group)
848 {
849   AccelEntry *entry;
850
851   g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
852   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
853
854   entry = accel_path_lookup (accel_path);
855   if (!entry)
856     {
857       gtk_accel_map_add_entry (accel_path, 0, 0);
858       entry = accel_path_lookup (accel_path);
859     }
860   entry->groups = g_slist_prepend (entry->groups, accel_group);
861 }
862
863 void
864 _gtk_accel_map_remove_group (const gchar   *accel_path,
865                              GtkAccelGroup *accel_group)
866 {
867   AccelEntry *entry;
868
869   entry = accel_path_lookup (accel_path);
870   g_return_if_fail (entry != NULL);
871   g_return_if_fail (g_slist_find (entry->groups, accel_group));
872
873   entry->groups = g_slist_remove (entry->groups, accel_group);
874 }
875
876
877 /**
878  * gtk_accel_map_lock_path:
879  * @accel_path: a valid accelerator path
880  * 
881  * Locks the given accelerator path. If the accelerator map doesn't yet contain
882  * an entry for @accel_path, a new one is created.
883  *
884  * Locking an accelerator path prevents its accelerator from being changed 
885  * during runtime. A locked accelerator path can be unlocked by 
886  * gtk_accel_map_unlock_path(). Refer to gtk_accel_map_change_entry() 
887  * for information about runtime accelerator changes.
888  *
889  * If called more than once, @accel_path remains locked until
890  * gtk_accel_map_unlock_path() has been called an equivalent number
891  * of times.
892  *
893  * Note that locking of individual accelerator paths is independent from 
894  * locking the #GtkAccelGroup containing them. For runtime accelerator
895  * changes to be possible both the accelerator path and its #GtkAccelGroup
896  * have to be unlocked. 
897  *
898  * Since: 2.4
899  **/
900 void 
901 gtk_accel_map_lock_path (const gchar *accel_path)
902 {
903   AccelEntry *entry;
904
905   g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
906
907   entry = accel_path_lookup (accel_path);
908   
909   if (!entry)
910     {
911       gtk_accel_map_add_entry (accel_path, 0, 0);
912       entry = accel_path_lookup (accel_path);
913     }
914
915   entry->lock_count += 1;
916 }
917
918 /**
919  * gtk_accel_map_unlock_path:
920  * @accel_path: a valid accelerator path
921  * 
922  * Undoes the last call to gtk_accel_map_lock_path() on this @accel_path.
923  * Refer to gtk_accel_map_lock_path() for information about accelerator path locking.
924  *
925  * Since: 2.4
926  **/
927 void 
928 gtk_accel_map_unlock_path (const gchar *accel_path)
929 {
930   AccelEntry *entry;
931
932   g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
933
934   entry = accel_path_lookup (accel_path);
935
936   g_return_if_fail (entry != NULL && entry->lock_count > 0);
937
938   entry->lock_count -= 1;  
939 }
940
941 G_DEFINE_TYPE (GtkAccelMap, gtk_accel_map, G_TYPE_OBJECT)
942
943 static void
944 gtk_accel_map_class_init (GtkAccelMapClass *accel_map_class)
945 {
946   /**
947    * GtkAccelMap::changed:
948    * @object: the global accel map object
949    * @accel_path: the path of the accelerator that changed
950    * @accel_key: the key value for the new accelerator
951    * @accel_mods: the modifier mask for the new accelerator
952    *
953    * Notifies of a change in the global accelerator map.
954    * The path is also used as the detail for the signal,
955    * so it is possible to connect to
956    * changed::<replaceable>accel_path</replaceable>.
957    *
958    * Since: 2.4
959    */
960   accel_map_signals[CHANGED] = g_signal_new (I_("changed"),
961                                              G_TYPE_FROM_CLASS (accel_map_class),
962                                              G_SIGNAL_DETAILED|G_SIGNAL_RUN_LAST,
963                                              0,
964                                              NULL, NULL,
965                                              _gtk_marshal_VOID__STRING_UINT_FLAGS,
966                                              G_TYPE_NONE, 3,
967                                              G_TYPE_STRING, G_TYPE_UINT, GDK_TYPE_MODIFIER_TYPE);
968 }
969
970 static void
971 gtk_accel_map_init (GtkAccelMap *accel_map)
972 {
973 }
974
975 /**
976  * gtk_accel_map_get:
977  * 
978  * Gets the singleton global #GtkAccelMap object. This object
979  * is useful only for notification of changes to the accelerator
980  * map via the ::changed signal; it isn't a parameter to the
981  * other accelerator map functions.
982  * 
983  * Return value: the global #GtkAccelMap object
984  *
985  * Since: 2.4
986  **/
987 GtkAccelMap *
988 gtk_accel_map_get (void)
989 {
990   if (!accel_map)
991     accel_map = g_object_new (GTK_TYPE_ACCEL_MAP, NULL);
992
993   return accel_map;
994 }
995
996 static void
997 do_accel_map_changed (AccelEntry *entry)
998 {
999   if (accel_map)
1000     g_signal_emit (accel_map,
1001                    accel_map_signals[CHANGED],
1002                    g_quark_from_string (entry->accel_path),
1003                    entry->accel_path,
1004                    entry->accel_key,
1005                    entry->accel_mods);
1006 }
1007
1008 #ifdef G_OS_WIN32
1009
1010 #undef gtk_accel_map_load
1011
1012 void
1013 gtk_accel_map_load (const gchar *file_name)
1014 {
1015   gchar *utf8_file_name = g_locale_to_utf8 (file_name, -1, NULL, NULL, NULL);
1016
1017   gtk_accel_map_load_utf8 (utf8_file_name);
1018
1019   g_free (utf8_file_name);
1020 }
1021
1022 #undef gtk_accel_map_save
1023
1024 void
1025 gtk_accel_map_save (const gchar *file_name)
1026 {
1027   gchar *utf8_file_name = g_locale_to_utf8 (file_name, -1, NULL, NULL, NULL);
1028
1029   gtk_accel_map_save_utf8 (utf8_file_name);
1030
1031   g_free (utf8_file_name);
1032 }
1033
1034 #endif
1035
1036 #define __GTK_ACCEL_MAP_C__
1037 #include "gtkaliasdef.c"