1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1998, 2001 Tim Janik
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 #include "gtkaccelmapprivate.h"
22 #include "gtkmarshalers.h"
23 #include "gtkwindowprivate.h"
26 #include <glib/gstdio.h>
41 * @Short_description: Loadable keyboard accelerator specifications
42 * @Title: Accelerator Maps
43 * @See_also: #GtkAccelGroup, #GtkAccelKey, #GtkUIManager, gtk_widget_set_accel_path(), gtk_menu_item_set_accel_path(), #GtkSettings:gtk-can-change-accels
45 * Accelerator maps are used to define runtime configurable accelerators.
46 * Functions for manipulating them are are usually used by higher level
47 * convenience mechanisms like #GtkUIManager and are thus considered
48 * "low-level". You'll want to use them if you're manually creating menus that
49 * should have user-configurable accelerators.
51 * Accelerator is uniquely defined by:
54 * <listitem><para>accelerator path</para></listitem>
55 * <listitem><para>accelerator key</para></listitem>
56 * <listitem><para>accelerator modifiers</para></listitem>
59 * The accelerator path must consist of
60 * "<WINDOWTYPE>/Category1/Category2/.../Action", where WINDOWTYPE
61 * should be a unique application-specific identifier that corresponds to the
62 * kind of window the accelerator is being used in, e.g. "Gimp-Image",
63 * "Abiword-Document" or "Gnumeric-Settings".
64 * The "Category1/.../Action" portion is most appropriately chosen by the action
65 * the accelerator triggers, i.e. for accelerators on menu items, choose the
66 * item's menu path, e.g. "File/Save As", "Image/View/Zoom" or
67 * "Edit/Select All". So a full valid accelerator path may look like:
68 * "<Gimp-Toolbox>/File/Dialogs/Tool Options...".
70 * All accelerators are stored inside one global #GtkAccelMap that can be
71 * obtained using gtk_accel_map_get(). See <link
72 * linkend="monitoring-changes">Monitoring changes</link> for additional
75 * <refsect2 id="manipulating-accelerators">
76 * <title>Manipulating accelerators</title>
78 * New accelerators can be added using gtk_accel_map_add_entry(). To search for
79 * specific accelerator, use gtk_accel_map_lookup_entry(). Modifications of
80 * existing accelerators should be done using gtk_accel_map_change_entry().
82 * In order to avoid having some accelerators changed, they can be locked using
83 * gtk_accel_map_lock_path(). Unlocking is done using
84 * gtk_accel_map_unlock_path().
87 * <refsect2 id="saving-and-loading">
88 * <title>Saving and loading accelerator maps</title>
90 * Accelerator maps can be saved to and loaded from some external resource. For
91 * simple saving and loading from file, gtk_accel_map_save() and
92 * gtk_accel_map_load() are provided. Saving and loading can also be done by
93 * providing file descriptor to gtk_accel_map_save_fd() and
94 * gtk_accel_map_load_fd().
97 * <refsect2 id="monitoring-changes">
98 * <title>Monitoring changes</title>
100 * #GtkAccelMap object is only useful for monitoring changes of accelerators. By
101 * connecting to #GtkAccelMap::changed signal, one can monitor changes of all
102 * accelerators. It is also possible to monitor only single accelerator path by
103 * using it as a detail of the #GtkAccelMap::changed signal.
109 /* --- structures --- */
112 GObject parent_instance;
115 struct _GtkAccelMapClass
117 GObjectClass parent_class;
121 const gchar *accel_path;
125 guint std_accel_mods;
127 guint lock_count : 15;
131 /* --- signals --- */
137 /* --- variables --- */
139 static GHashTable *accel_entry_ht = NULL; /* accel_path -> AccelEntry */
140 static GSList *accel_filters = NULL;
141 static gulong accel_map_signals[LAST_SIGNAL] = { 0, };
142 static GtkAccelMap *accel_map;
144 /* --- prototypes --- */
145 static void do_accel_map_changed (AccelEntry *entry);
147 /* --- functions --- */
149 accel_entry_hash (gconstpointer key)
151 const AccelEntry *entry = key;
153 return g_str_hash (entry->accel_path);
157 accel_entry_equal (gconstpointer key1,
160 const AccelEntry *entry1 = key1;
161 const AccelEntry *entry2 = key2;
163 return g_str_equal (entry1->accel_path, entry2->accel_path);
166 static inline AccelEntry*
167 accel_path_lookup (const gchar *accel_path)
171 ekey.accel_path = accel_path;
173 /* safety NULL check for return_if_fail()s */
174 return accel_path ? g_hash_table_lookup (accel_entry_ht, &ekey) : NULL;
178 _gtk_accel_map_init (void)
180 if (accel_entry_ht == NULL)
181 accel_entry_ht = g_hash_table_new (accel_entry_hash, accel_entry_equal);
185 _gtk_accel_path_is_valid (const gchar *accel_path)
189 if (!accel_path || accel_path[0] != '<' ||
190 accel_path[1] == '<' || accel_path[1] == '>' || !accel_path[1])
192 p = strchr (accel_path, '>');
193 if (!p || (p[1] != 0 && p[1] != '/'))
199 * gtk_accel_map_add_entry:
200 * @accel_path: valid accelerator path
201 * @accel_key: the accelerator key
202 * @accel_mods: the accelerator modifiers
204 * Registers a new accelerator with the global accelerator map.
205 * This function should only be called once per @accel_path
206 * with the canonical @accel_key and @accel_mods for this path.
207 * To change the accelerator during runtime programatically, use
208 * gtk_accel_map_change_entry().
210 * Set @accel_key and @accel_mods to 0 to request a removal of
213 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
214 * pass a static string, you can save some memory by interning it first with
215 * g_intern_static_string().
218 gtk_accel_map_add_entry (const gchar *accel_path,
220 GdkModifierType accel_mods)
224 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
229 accel_mods &= gtk_accelerator_get_default_mod_mask ();
231 entry = accel_path_lookup (accel_path);
234 if (!entry->std_accel_key && !entry->std_accel_mods &&
235 (accel_key || accel_mods))
237 entry->std_accel_key = accel_key;
238 entry->std_accel_mods = accel_mods;
240 gtk_accel_map_change_entry (entry->accel_path, accel_key, accel_mods, TRUE);
245 entry = g_slice_new0 (AccelEntry);
246 entry->accel_path = g_intern_string (accel_path);
247 entry->std_accel_key = accel_key;
248 entry->std_accel_mods = accel_mods;
249 entry->accel_key = accel_key;
250 entry->accel_mods = accel_mods;
251 entry->changed = FALSE;
252 entry->lock_count = 0;
253 g_hash_table_insert (accel_entry_ht, entry, entry);
255 do_accel_map_changed (entry);
260 * gtk_accel_map_lookup_entry:
261 * @accel_path: a valid accelerator path
262 * @key: (allow-none) (out): the accelerator key to be filled in (optional)
264 * Looks up the accelerator entry for @accel_path and fills in @key.
266 * Returns: %TRUE if @accel_path is known, %FALSE otherwise
269 gtk_accel_map_lookup_entry (const gchar *accel_path,
274 g_return_val_if_fail (_gtk_accel_path_is_valid (accel_path), FALSE);
276 entry = accel_path_lookup (accel_path);
279 key->accel_key = entry->accel_key;
280 key->accel_mods = entry->accel_mods;
281 key->accel_flags = 0;
284 return entry ? TRUE : FALSE;
288 hash2slist_foreach (gpointer key,
292 GSList **slist_p = user_data;
294 *slist_p = g_slist_prepend (*slist_p, value);
298 g_hash_table_slist_values (GHashTable *hash_table)
300 GSList *slist = NULL;
302 g_return_val_if_fail (hash_table != NULL, NULL);
304 g_hash_table_foreach (hash_table, hash2slist_foreach, &slist);
309 /* if simulate==TRUE, return whether accel_path can be changed to
310 * accel_key && accel_mods. otherwise, return whether accel_path
311 * was actually changed.
314 internal_change_entry (const gchar *accel_path,
316 GdkModifierType accel_mods,
320 GSList *node, *slist, *win_list, *group_list, *replace_list = NULL;
321 GHashTable *group_hm, *window_hm;
322 gboolean change_accel, removable, can_change = TRUE, seen_accel = FALSE;
324 AccelEntry *entry = accel_path_lookup (accel_path);
326 /* not much todo if there's no entry yet */
331 gtk_accel_map_add_entry (accel_path, 0, 0);
332 entry = accel_path_lookup (accel_path);
333 entry->accel_key = accel_key;
334 entry->accel_mods = accel_mods;
335 entry->changed = TRUE;
337 do_accel_map_changed (entry);
342 /* if there's nothing to change, not much todo either */
343 if (entry->accel_key == accel_key && entry->accel_mods == accel_mods)
346 entry->changed = TRUE;
347 return simulate ? TRUE : FALSE;
350 /* The no-change case has already been handled, so
351 * simulate doesn't make a difference here.
353 if (entry->lock_count > 0)
356 /* nobody's interested, easy going */
361 entry->accel_key = accel_key;
362 entry->accel_mods = accel_mods;
363 entry->changed = TRUE;
365 do_accel_map_changed (entry);
370 /* 1) fetch all accel groups affected by this entry */
371 entry_quark = g_quark_try_string (entry->accel_path);
372 group_hm = g_hash_table_new (NULL, NULL);
373 window_hm = g_hash_table_new (NULL, NULL);
374 for (slist = entry->groups; slist; slist = slist->next)
375 g_hash_table_insert (group_hm, slist->data, slist->data);
377 /* 2) collect acceleratables affected */
378 group_list = g_hash_table_slist_values (group_hm);
379 for (slist = group_list; slist; slist = slist->next)
381 GtkAccelGroup *group = slist->data;
383 for (node = _gtk_accel_group_get_accelerables (group); node; node = node->next)
384 g_hash_table_insert (window_hm, node->data, node->data);
386 g_slist_free (group_list);
388 /* 3) include all accel groups used by acceleratables */
389 win_list = g_hash_table_slist_values (window_hm);
390 g_hash_table_destroy (window_hm);
391 for (slist = win_list; slist; slist = slist->next)
392 for (node = gtk_accel_groups_from_object (slist->data); node; node = node->next)
393 g_hash_table_insert (group_hm, node->data, node->data);
394 group_list = g_hash_table_slist_values (group_hm);
395 g_hash_table_destroy (group_hm);
397 /* 4) walk the acceleratables and figure whether they occupy accel_key&accel_mods */
399 for (slist = win_list; slist; slist = slist->next)
400 if (GTK_IS_WINDOW (slist->data)) /* bad kludge in lack of a GtkAcceleratable */
401 if (_gtk_window_query_nonaccels (slist->data, accel_key, accel_mods))
406 removable = !seen_accel;
408 /* 5) walk all accel groups and search for locks */
410 for (slist = group_list; slist; slist = slist->next)
412 GtkAccelGroup *group = slist->data;
413 GtkAccelGroupEntry *ag_entry;
417 ag_entry = entry->accel_key ? gtk_accel_group_query (group, entry->accel_key, entry->accel_mods, &n) : NULL;
418 for (i = 0; i < n; i++)
419 if (ag_entry[i].accel_path_quark == entry_quark)
421 can_change = !(ag_entry[i].key.accel_flags & GTK_ACCEL_LOCKED);
423 goto break_loop_step5;
427 ag_entry = accel_key ? gtk_accel_group_query (group, accel_key, accel_mods, &n) : NULL;
428 for (i = 0; i < n; i++)
431 removable = !gtk_accel_group_get_is_locked (group) && !(ag_entry[i].key.accel_flags & GTK_ACCEL_LOCKED);
433 goto break_loop_step5;
434 if (ag_entry[i].accel_path_quark)
435 replace_list = g_slist_prepend (replace_list, GUINT_TO_POINTER (ag_entry[i].accel_path_quark));
440 /* 6) check whether we can remove existing accelerators */
441 if (removable && can_change)
442 for (slist = replace_list; slist; slist = slist->next)
443 if (!internal_change_entry (g_quark_to_string (GPOINTER_TO_UINT (slist->data)), 0, 0, FALSE, TRUE))
449 /* 7) check conditions and proceed if possible */
450 change_accel = can_change && (!seen_accel || (removable && replace));
452 if (change_accel && !simulate)
454 /* ref accel groups */
455 for (slist = group_list; slist; slist = slist->next)
456 g_object_ref (slist->data);
458 /* 8) remove existing accelerators */
459 for (slist = replace_list; slist; slist = slist->next)
460 internal_change_entry (g_quark_to_string (GPOINTER_TO_UINT (slist->data)), 0, 0, FALSE, FALSE);
462 /* 9) install new accelerator */
463 entry->accel_key = accel_key;
464 entry->accel_mods = accel_mods;
465 entry->changed = TRUE;
467 for (slist = group_list; slist; slist = slist->next)
468 _gtk_accel_group_reconnect (slist->data, g_quark_from_string (entry->accel_path));
470 /* unref accel groups */
471 for (slist = group_list; slist; slist = slist->next)
472 g_object_unref (slist->data);
474 do_accel_map_changed (entry);
476 g_slist_free (replace_list);
477 g_slist_free (group_list);
478 g_slist_free (win_list);
484 * gtk_accel_map_change_entry:
485 * @accel_path: a valid accelerator path
486 * @accel_key: the new accelerator key
487 * @accel_mods: the new accelerator modifiers
488 * @replace: %TRUE if other accelerators may be deleted upon conflicts
490 * Changes the @accel_key and @accel_mods currently associated with @accel_path.
491 * Due to conflicts with other accelerators, a change may not always be possible,
492 * @replace indicates whether other accelerators may be deleted to resolve such
493 * conflicts. A change will only occur if all conflicts could be resolved (which
494 * might not be the case if conflicting accelerators are locked). Successful
495 * changes are indicated by a %TRUE return value.
497 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
498 * pass a static string, you can save some memory by interning it first with
499 * g_intern_static_string().
501 * Returns: %TRUE if the accelerator could be changed, %FALSE otherwise
504 gtk_accel_map_change_entry (const gchar *accel_path,
506 GdkModifierType accel_mods,
509 g_return_val_if_fail (_gtk_accel_path_is_valid (accel_path), FALSE);
511 return internal_change_entry (accel_path, accel_key, accel_key ? accel_mods : 0, replace, FALSE);
515 accel_map_parse_accel_path (GScanner *scanner)
518 GdkModifierType accel_mods = 0;
521 /* parse accel path */
522 g_scanner_get_next_token (scanner);
523 if (scanner->token != G_TOKEN_STRING)
524 return G_TOKEN_STRING;
526 /* test if the next token is an accelerator */
527 g_scanner_peek_next_token (scanner);
528 if (scanner->next_token != G_TOKEN_STRING)
530 /* if not so, eat that token and error out */
531 g_scanner_get_next_token (scanner);
532 return G_TOKEN_STRING;
535 /* get the full accelerator specification */
536 path = g_strdup (scanner->value.v_string);
537 g_scanner_get_next_token (scanner);
538 accel = g_strdup (scanner->value.v_string);
540 /* ensure the entry is present */
541 gtk_accel_map_add_entry (path, 0, 0);
543 /* and propagate it */
544 gtk_accelerator_parse (accel, &accel_key, &accel_mods);
545 gtk_accel_map_change_entry (path, accel_key, accel_mods, TRUE);
550 /* check correct statement end */
551 g_scanner_get_next_token (scanner);
552 if (scanner->token != ')')
559 accel_map_parse_statement (GScanner *scanner)
561 guint expected_token;
563 g_scanner_get_next_token (scanner);
565 if (scanner->token == G_TOKEN_SYMBOL)
567 guint (*parser_func) (GScanner*);
569 parser_func = (guint (*) (GScanner *))scanner->value.v_symbol;
571 expected_token = parser_func (scanner);
574 expected_token = G_TOKEN_SYMBOL;
576 /* skip rest of statement on errrors
578 if (expected_token != G_TOKEN_NONE)
580 register guint level;
583 if (scanner->token == ')')
585 if (scanner->token == '(')
588 while (!g_scanner_eof (scanner) && level > 0)
590 g_scanner_get_next_token (scanner);
592 if (scanner->token == '(')
594 else if (scanner->token == ')')
601 * gtk_accel_map_load_scanner:
602 * @scanner: a #GScanner which has already been provided with an input file
604 * #GScanner variant of gtk_accel_map_load().
607 gtk_accel_map_load_scanner (GScanner *scanner)
609 gboolean skip_comment_single;
610 gboolean symbol_2_token;
611 gchar *cpair_comment_single;
612 gpointer saved_symbol;
614 g_return_if_fail (scanner != NULL);
616 /* configure scanner */
617 skip_comment_single = scanner->config->skip_comment_single;
618 scanner->config->skip_comment_single = TRUE;
619 cpair_comment_single = scanner->config->cpair_comment_single;
620 scanner->config->cpair_comment_single = ";\n";
621 symbol_2_token = scanner->config->symbol_2_token;
622 scanner->config->symbol_2_token = FALSE;
623 saved_symbol = g_scanner_lookup_symbol (scanner, "gtk_accel_path");
624 g_scanner_scope_add_symbol (scanner, 0, "gtk_accel_path",
625 accel_map_parse_accel_path);
627 /* outer parsing loop
629 g_scanner_peek_next_token (scanner);
630 while (scanner->next_token == '(')
632 g_scanner_get_next_token (scanner);
634 accel_map_parse_statement (scanner);
636 g_scanner_peek_next_token (scanner);
640 scanner->config->skip_comment_single = skip_comment_single;
641 scanner->config->cpair_comment_single = cpair_comment_single;
642 scanner->config->symbol_2_token = symbol_2_token;
643 g_scanner_scope_remove_symbol (scanner, 0, "gtk_accel_path");
645 g_scanner_scope_add_symbol (scanner, 0, "gtk_accel_path", saved_symbol);
649 * gtk_accel_map_load_fd:
650 * @fd: a valid readable file descriptor
652 * Filedescriptor variant of gtk_accel_map_load().
654 * Note that the file descriptor will not be closed by this function.
657 gtk_accel_map_load_fd (gint fd)
661 g_return_if_fail (fd >= 0);
663 /* create and setup scanner */
664 scanner = g_scanner_new (NULL);
665 g_scanner_input_file (scanner, fd);
667 gtk_accel_map_load_scanner (scanner);
669 g_scanner_destroy (scanner);
673 * gtk_accel_map_load:
674 * @file_name: (type filename): a file containing accelerator specifications,
675 * in the GLib file name encoding
677 * Parses a file previously saved with gtk_accel_map_save() for
678 * accelerator specifications, and propagates them accordingly.
681 gtk_accel_map_load (const gchar *file_name)
685 g_return_if_fail (file_name != NULL);
687 if (!g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
690 fd = g_open (file_name, O_RDONLY, 0);
694 gtk_accel_map_load_fd (fd);
706 gssize count = write (fd, buf, to_write);
723 accel_map_print (gpointer data,
724 const gchar *accel_path,
726 GdkModifierType accel_mods,
729 GString *gstring = g_string_new (changed ? NULL : "; ");
730 gint fd = GPOINTER_TO_INT (data);
733 g_string_append (gstring, "(gtk_accel_path \"");
735 tmp = g_strescape (accel_path, NULL);
736 g_string_append (gstring, tmp);
739 g_string_append (gstring, "\" \"");
741 name = gtk_accelerator_name (accel_key, accel_mods);
742 tmp = g_strescape (name, NULL);
744 g_string_append (gstring, tmp);
747 g_string_append (gstring, "\")\n");
749 write_all (fd, gstring->str, gstring->len);
751 g_string_free (gstring, TRUE);
755 * gtk_accel_map_save_fd:
756 * @fd: a valid writable file descriptor
758 * Filedescriptor variant of gtk_accel_map_save().
760 * Note that the file descriptor will not be closed by this function.
763 gtk_accel_map_save_fd (gint fd)
767 g_return_if_fail (fd >= 0);
769 gstring = g_string_new ("; ");
770 if (g_get_prgname ())
771 g_string_append (gstring, g_get_prgname ());
772 g_string_append (gstring, " GtkAccelMap rc-file -*- scheme -*-\n");
773 g_string_append (gstring, "; this file is an automated accelerator map dump\n");
774 g_string_append (gstring, ";\n");
776 write_all (fd, gstring->str, gstring->len);
778 g_string_free (gstring, TRUE);
780 gtk_accel_map_foreach (GINT_TO_POINTER (fd), accel_map_print);
784 * gtk_accel_map_save:
785 * @file_name: (type filename): the name of the file to contain
786 * accelerator specifications, in the GLib file name encoding
788 * Saves current accelerator specifications (accelerator path, key
789 * and modifiers) to @file_name.
790 * The file is written in a format suitable to be read back in by
791 * gtk_accel_map_load().
794 gtk_accel_map_save (const gchar *file_name)
798 g_return_if_fail (file_name != NULL);
800 fd = g_open (file_name, O_CREAT | O_TRUNC | O_WRONLY, 0644);
804 gtk_accel_map_save_fd (fd);
810 * gtk_accel_map_foreach:
811 * @data: (allow-none): data to be passed into @foreach_func
812 * @foreach_func: (scope call): function to be executed for each accel
813 * map entry which is not filtered out
815 * Loops over the entries in the accelerator map whose accel path
816 * doesn't match any of the filters added with gtk_accel_map_add_filter(),
817 * and execute @foreach_func on each. The signature of @foreach_func is
818 * that of #GtkAccelMapForeach, the @changed parameter indicates whether
819 * this accelerator was changed during runtime (thus, would need
820 * saving during an accelerator map dump).
823 gtk_accel_map_foreach (gpointer data,
824 GtkAccelMapForeach foreach_func)
826 GSList *entries, *slist, *node;
828 g_return_if_fail (foreach_func != NULL);
830 entries = g_hash_table_slist_values (accel_entry_ht);
831 for (slist = entries; slist; slist = slist->next)
833 AccelEntry *entry = slist->data;
834 gboolean changed = entry->accel_key != entry->std_accel_key || entry->accel_mods != entry->std_accel_mods;
836 for (node = accel_filters; node; node = node->next)
837 if (g_pattern_match_string (node->data, entry->accel_path))
839 foreach_func (data, entry->accel_path, entry->accel_key, entry->accel_mods, changed);
843 g_slist_free (entries);
847 * gtk_accel_map_foreach_unfiltered:
848 * @data: data to be passed into @foreach_func
849 * @foreach_func: (scope call): function to be executed for each accel
852 * Loops over all entries in the accelerator map, and execute
853 * @foreach_func on each. The signature of @foreach_func is that of
854 * #GtkAccelMapForeach, the @changed parameter indicates whether
855 * this accelerator was changed during runtime (thus, would need
856 * saving during an accelerator map dump).
859 gtk_accel_map_foreach_unfiltered (gpointer data,
860 GtkAccelMapForeach foreach_func)
862 GSList *entries, *slist;
864 g_return_if_fail (foreach_func != NULL);
866 entries = g_hash_table_slist_values (accel_entry_ht);
867 for (slist = entries; slist; slist = slist->next)
869 AccelEntry *entry = slist->data;
870 gboolean changed = entry->accel_key != entry->std_accel_key || entry->accel_mods != entry->std_accel_mods;
872 foreach_func (data, entry->accel_path, entry->accel_key, entry->accel_mods, changed);
874 g_slist_free (entries);
878 * gtk_accel_map_add_filter:
879 * @filter_pattern: a pattern (see #GPatternSpec)
881 * Adds a filter to the global list of accel path filters.
883 * Accel map entries whose accel path matches one of the filters
884 * are skipped by gtk_accel_map_foreach().
886 * This function is intended for GTK+ modules that create their own
887 * menus, but don't want them to be saved into the applications accelerator
891 gtk_accel_map_add_filter (const gchar *filter_pattern)
896 g_return_if_fail (filter_pattern != NULL);
898 pspec = g_pattern_spec_new (filter_pattern);
899 for (slist = accel_filters; slist; slist = slist->next)
900 if (g_pattern_spec_equal (pspec, slist->data))
902 g_pattern_spec_free (pspec);
905 accel_filters = g_slist_prepend (accel_filters, pspec);
909 _gtk_accel_map_add_group (const gchar *accel_path,
910 GtkAccelGroup *accel_group)
914 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
915 g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
917 entry = accel_path_lookup (accel_path);
920 gtk_accel_map_add_entry (accel_path, 0, 0);
921 entry = accel_path_lookup (accel_path);
923 entry->groups = g_slist_prepend (entry->groups, accel_group);
927 _gtk_accel_map_remove_group (const gchar *accel_path,
928 GtkAccelGroup *accel_group)
932 entry = accel_path_lookup (accel_path);
933 g_return_if_fail (entry != NULL);
934 g_return_if_fail (g_slist_find (entry->groups, accel_group));
936 entry->groups = g_slist_remove (entry->groups, accel_group);
941 * gtk_accel_map_lock_path:
942 * @accel_path: a valid accelerator path
944 * Locks the given accelerator path. If the accelerator map doesn't yet contain
945 * an entry for @accel_path, a new one is created.
947 * Locking an accelerator path prevents its accelerator from being changed
948 * during runtime. A locked accelerator path can be unlocked by
949 * gtk_accel_map_unlock_path(). Refer to gtk_accel_map_change_entry()
950 * for information about runtime accelerator changes.
952 * If called more than once, @accel_path remains locked until
953 * gtk_accel_map_unlock_path() has been called an equivalent number
956 * Note that locking of individual accelerator paths is independent from
957 * locking the #GtkAccelGroup containing them. For runtime accelerator
958 * changes to be possible, both the accelerator path and its #GtkAccelGroup
959 * have to be unlocked.
964 gtk_accel_map_lock_path (const gchar *accel_path)
968 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
970 entry = accel_path_lookup (accel_path);
974 gtk_accel_map_add_entry (accel_path, 0, 0);
975 entry = accel_path_lookup (accel_path);
978 entry->lock_count += 1;
982 * gtk_accel_map_unlock_path:
983 * @accel_path: a valid accelerator path
985 * Undoes the last call to gtk_accel_map_lock_path() on this @accel_path.
986 * Refer to gtk_accel_map_lock_path() for information about accelerator path locking.
991 gtk_accel_map_unlock_path (const gchar *accel_path)
995 g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
997 entry = accel_path_lookup (accel_path);
999 g_return_if_fail (entry != NULL && entry->lock_count > 0);
1001 entry->lock_count -= 1;
1004 G_DEFINE_TYPE (GtkAccelMap, gtk_accel_map, G_TYPE_OBJECT)
1007 gtk_accel_map_class_init (GtkAccelMapClass *accel_map_class)
1010 * GtkAccelMap::changed:
1011 * @object: the global accel map object
1012 * @accel_path: the path of the accelerator that changed
1013 * @accel_key: the key value for the new accelerator
1014 * @accel_mods: the modifier mask for the new accelerator
1016 * Notifies of a change in the global accelerator map.
1017 * The path is also used as the detail for the signal,
1018 * so it is possible to connect to
1019 * changed::<replaceable>accel_path</replaceable>.
1023 accel_map_signals[CHANGED] = g_signal_new (I_("changed"),
1024 G_TYPE_FROM_CLASS (accel_map_class),
1025 G_SIGNAL_DETAILED|G_SIGNAL_RUN_LAST,
1028 _gtk_marshal_VOID__STRING_UINT_FLAGS,
1030 G_TYPE_STRING, G_TYPE_UINT, GDK_TYPE_MODIFIER_TYPE);
1034 gtk_accel_map_init (GtkAccelMap *accel_map)
1039 * gtk_accel_map_get:
1041 * Gets the singleton global #GtkAccelMap object. This object
1042 * is useful only for notification of changes to the accelerator
1043 * map via the ::changed signal; it isn't a parameter to the
1044 * other accelerator map functions.
1046 * Return value: (transfer none): the global #GtkAccelMap object
1051 gtk_accel_map_get (void)
1054 accel_map = g_object_new (GTK_TYPE_ACCEL_MAP, NULL);
1060 do_accel_map_changed (AccelEntry *entry)
1063 g_signal_emit (accel_map,
1064 accel_map_signals[CHANGED],
1065 g_quark_from_string (entry->accel_path),
1072 _gtk_accel_path_for_action (const gchar *action_name,
1073 GVariant *parameter)
1077 s = g_string_new ("<GAction>/");
1078 g_string_append (s, action_name);
1081 g_string_append_c (s, '/');
1082 g_variant_print_string (parameter, s, FALSE);
1084 return g_string_free (s, FALSE);