1 /* gtkmnemonichash.c: Sets of mnemonics with cycling
3 * GTK - The GIMP Toolkit
4 * Copyright (C) 2002, Red Hat Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 #include "gtkmnemonichash.h"
22 struct _GtkMnemnonicHash
29 _gtk_mnemonic_hash_new (void)
31 GtkMnemonicHash *mnemonic_hash = g_new (GtkMnemonicHash, 1);
33 mnemonic_hash->hash = g_hash_table_new (g_direct_hash, NULL);
39 mnemonic_hash_free_foreach (gpointer key,
43 guint keyval = GPOINTER_TO_UINT (key);
44 GSList *targets = value;
46 gchar *name = gtk_accelerator_name (keyval, 0);
48 g_warning ("mnemonic \"%s\" wasn't removed for widget (%p)",
52 g_slist_free (targets);
56 _gtk_mnemonic_hash_free (GtkMnemonicHash *mnemonic_hash)
58 g_hash_table_foreach (mnemonic_hash->hash,
59 mnemonic_hash_free_foreach,
62 g_hash_table_destroy (mnemonic_hash->hash);
63 g_free (mnemonic_hash);
67 _gtk_mnemonic_hash_add (GtkMnemonicHash *mnemonic_hash,
71 gpointer key = GUINT_TO_POINTER (keyval);
72 GSList *targets, *new_targets;
74 g_return_if_fail (GTK_IS_WIDGET (target));
76 targets = g_hash_table_lookup (mnemonic_hash->hash, key);
77 g_return_if_fail (g_slist_find (targets, target) == NULL);
79 new_targets = g_slist_append (targets, target);
80 if (new_targets != targets)
81 g_hash_table_insert (mnemonic_hash->hash, key, new_targets);
85 _gtk_mnemonic_hash_remove (GtkMnemonicHash *mnemonic_hash,
89 gpointer key = GUINT_TO_POINTER (keyval);
90 GSList *targets, *new_targets;
92 g_return_if_fail (GTK_IS_WIDGET (target));
94 targets = g_hash_table_lookup (mnemonic_hash->hash, key);
96 g_return_if_fail (targets && g_slist_find (targets, target) != NULL);
98 new_targets = g_slist_remove (targets, target);
99 if (new_targets != targets)
101 if (new_targets == NULL)
102 g_hash_table_remove (mnemonic_hash->hash, key);
104 g_hash_table_insert (mnemonic_hash->hash, key, new_targets);
109 _gtk_mnemonic_hash_activate (GtkMnemonicHash *mnemonic_hash,
112 GSList *list, *targets;
113 GtkWidget *widget, *chosen_widget;
117 targets = g_hash_table_lookup (mnemonic_hash->hash,
118 GUINT_TO_POINTER (keyval));
123 chosen_widget = NULL;
124 for (list = targets; list; list = list->next)
126 widget = GTK_WIDGET (list->data);
127 window = gtk_widget_get_window (widget);
129 if (gtk_widget_is_sensitive (widget) &&
130 gtk_widget_get_mapped (widget) &&
131 window && gdk_window_is_viewable (window))
139 chosen_widget = widget;
145 /* For round robin we put the activated entry on
146 * the end of the list after activation
148 targets = g_slist_remove (targets, chosen_widget);
149 targets = g_slist_append (targets, chosen_widget);
150 g_hash_table_insert (mnemonic_hash->hash,
151 GUINT_TO_POINTER (keyval),
154 return gtk_widget_mnemonic_activate (chosen_widget, overloaded);
160 _gtk_mnemonic_hash_lookup (GtkMnemonicHash *mnemonic_hash,
163 return g_hash_table_lookup (mnemonic_hash->hash, GUINT_TO_POINTER (keyval));
167 mnemonic_hash_foreach_func (gpointer key,
172 GtkMnemonicHashForeach func;
176 guint keyval = GPOINTER_TO_UINT (key);
177 GSList *targets = value;
179 (*info->func) (keyval, targets, info->func_data);
183 _gtk_mnemonic_hash_foreach (GtkMnemonicHash *mnemonic_hash,
184 GtkMnemonicHashForeach func,
188 GtkMnemonicHashForeach func;
193 info.func_data = func_data;
195 g_hash_table_foreach (mnemonic_hash->hash,
196 mnemonic_hash_foreach_func,