1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 Red Hat, Inc.
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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include <gdk/gdkkeysyms.h>
31 static GHashTable *stock_hash = NULL;
32 static void init_stock_hash (void);
35 real_add (const GtkStockItem *items,
49 gpointer old_key, old_value;
50 const GtkStockItem *item = &items[i];
52 item = gtk_stock_item_copy (item);
54 if (g_hash_table_lookup_extended (stock_hash, item->stock_id,
55 &old_key, &old_value))
57 g_hash_table_remove (stock_hash, old_key);
58 gtk_stock_item_free (old_value);
61 g_hash_table_insert (stock_hash,
62 (gchar*)item->stock_id, (GtkStockItem*)item);
70 * @items: a #GtkStockItem or array of items
71 * @n_items: number of #GtkStockItem in @items
73 * Registers each of the stock items in @items. If an item already
74 * exists with the same stock ID as one of the @items, the old item
75 * gets replaced. The stock items are copied, so GTK+ does not hold
76 * any pointer into @items and @items can be freed. Use
77 * gtk_stock_add_static() if @items is persistent and GTK+ need not
82 gtk_stock_add (const GtkStockItem *items,
85 g_return_if_fail (items != NULL);
87 real_add (items, n_items, TRUE);
91 * gtk_stock_add_static:
92 * @items: a #GtkStockItem or array of #GtkStockItem
93 * @n_items: number of items
95 * Same as gtk_stock_add(), but doesn't copy @items, so
96 * @items must persist until application exit.
100 gtk_stock_add_static (const GtkStockItem *items,
103 g_return_if_fail (items != NULL);
105 real_add (items, n_items, FALSE);
110 * @stock_id: a stock item name
111 * @item: stock item to initialize with values
113 * Fills @item with the registered values for @stock_id, returning %TRUE
114 * if @stock_id was known.
117 * Return value: %TRUE if @item was initialized
120 gtk_stock_lookup (const gchar *stock_id,
123 const GtkStockItem *found;
125 g_return_val_if_fail (stock_id != NULL, FALSE);
126 g_return_val_if_fail (item != NULL, FALSE);
130 found = g_hash_table_lookup (stock_hash, stock_id);
136 item->label = dgettext (item->translation_domain, item->label);
139 return found != NULL;
143 listify_foreach (gpointer key, gpointer value, gpointer data)
145 GSList **list = data;
147 *list = g_slist_prepend (*list, value);
151 g_hash_table_get_values (GHashTable *table)
155 g_hash_table_foreach (table, listify_foreach, &list);
161 * gtk_stock_list_items:
163 * Retrieves a list of all known #GtkStockItem. The items are not copied;
164 * they should not be freed. However, the list itself must be freed
165 * with g_slist_free().
167 * Return value: a list of registered stock items
170 gtk_stock_list_items (void)
174 return g_hash_table_get_values (stock_hash);
178 * gtk_stock_item_copy:
179 * @item: a #GtkStockItem
181 * Copies a stock item, mostly useful for language bindings and not in applications.
183 * Return value: a new #GtkStockItem
186 gtk_stock_item_copy (const GtkStockItem *item)
190 g_return_val_if_fail (item != NULL, NULL);
192 copy = g_new (GtkStockItem, 1);
196 copy->stock_id = g_strdup (item->stock_id);
197 copy->label = g_strdup (item->label);
198 copy->translation_domain = g_strdup (item->translation_domain);
204 * gtk_stock_item_free:
205 * @item: a #GtkStockItem
207 * Frees a stock item allocated on the heap, such as one returned by
208 * gtk_stock_item_copy(). Also frees the fields inside the stock item,
209 * if they are not %NULL.
213 gtk_stock_item_free (GtkStockItem *item)
215 g_return_if_fail (item != NULL);
217 g_free ((gchar*)item->stock_id);
218 g_free ((gchar*)item->label);
219 g_free ((gchar*)item->translation_domain);
224 static GtkStockItem builtin_items [] =
226 /* KEEP IN SYNC with gtkiconfactory.c stock icons */
228 { GTK_STOCK_DIALOG_INFO, N_("Information"), 0, 0, GETTEXT_PACKAGE },
229 { GTK_STOCK_DIALOG_WARNING, N_("Warning"), 0, 0, GETTEXT_PACKAGE },
230 { GTK_STOCK_DIALOG_ERROR, N_("Error"), 0, 0, GETTEXT_PACKAGE },
231 { GTK_STOCK_DIALOG_QUESTION, N_("Question"), 0, 0, GETTEXT_PACKAGE },
233 { GTK_STOCK_BUTTON_APPLY, N_("_Apply"), 0, 0, GETTEXT_PACKAGE },
234 { GTK_STOCK_BUTTON_OK, N_("OK"), 0, 0, GETTEXT_PACKAGE },
235 { GTK_STOCK_BUTTON_CANCEL, N_("Cancel"), 0, 0, GETTEXT_PACKAGE },
236 { GTK_STOCK_BUTTON_CLOSE, N_("_Close"), 0, 0, GETTEXT_PACKAGE },
237 { GTK_STOCK_BUTTON_YES, N_("_Yes"), 0, 0, GETTEXT_PACKAGE },
238 { GTK_STOCK_BUTTON_NO, N_("_No"), 0, 0, GETTEXT_PACKAGE },
240 { GTK_STOCK_CLOSE, N_("Close"), GDK_CONTROL_MASK, 'w', GETTEXT_PACKAGE },
241 { GTK_STOCK_QUIT, N_("Quit"), GDK_CONTROL_MASK, 'q', GETTEXT_PACKAGE },
242 { GTK_STOCK_HELP, N_("Help"), GDK_CONTROL_MASK, 'h', GETTEXT_PACKAGE },
243 { GTK_STOCK_NEW, N_("New"), GDK_CONTROL_MASK, 'n', GETTEXT_PACKAGE },
244 { GTK_STOCK_OPEN, N_("Open"), GDK_CONTROL_MASK, 'o', GETTEXT_PACKAGE },
245 { GTK_STOCK_SAVE, N_("Save"), GDK_CONTROL_MASK, 's', GETTEXT_PACKAGE }
249 init_stock_hash (void)
251 if (stock_hash == NULL)
253 stock_hash = g_hash_table_new (g_str_hash, g_str_equal);
255 gtk_stock_add_static (builtin_items, G_N_ELEMENTS (builtin_items));