]> Pileus Git - ~andy/gtk/blob - gtk/gtkstock.c
CVS is doing its broken pipe thing, this is more of the previous commit
[~andy/gtk] / gtk / gtkstock.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc. 
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 /*
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/. 
25  */
26
27 #include "gtkstock.h"
28 #include "gtkintl.h"
29 #include <gdk/gdkkeysyms.h>
30
31 static GHashTable *stock_hash = NULL;
32 static void init_stock_hash (void);
33
34 static void
35 real_add (const GtkStockItem *items,
36           guint               n_items,
37           gboolean            copy)
38 {
39   int i;
40
41   init_stock_hash ();
42
43   if (n_items == 0)
44     return;
45
46   i = 0;
47   while (i < n_items)
48     {
49       gpointer old_key, old_value;
50       const GtkStockItem *item = &items[i];
51       if (copy)
52         item = gtk_stock_item_copy (item);
53
54       if (g_hash_table_lookup_extended (stock_hash, item->stock_id,
55                                         &old_key, &old_value))
56         {
57           g_hash_table_remove (stock_hash, old_key);
58           gtk_stock_item_free (old_value);
59         }
60       
61       g_hash_table_insert (stock_hash,
62                            (gchar*)item->stock_id, (GtkStockItem*)item);
63
64       ++i;
65     }
66 }
67
68 /**
69  * gtk_stock_add:
70  * @items: a #GtkStockItem or array of items
71  * @n_items: number of #GtkStockItem in @items
72  *
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
78  * copy the array.
79  * 
80  **/
81 void
82 gtk_stock_add (const GtkStockItem *items,
83                guint               n_items)
84 {
85   g_return_if_fail (items != NULL);
86
87   real_add (items, n_items, TRUE);
88 }
89
90 /**
91  * gtk_stock_add_static:
92  * @items: a #GtkStockItem or array of #GtkStockItem
93  * @n_items: number of items
94  *
95  * Same as gtk_stock_add(), but doesn't copy @items, so
96  * @items must persist until application exit.
97  * 
98  **/
99 void
100 gtk_stock_add_static (const GtkStockItem *items,
101                       guint               n_items)
102 {
103   g_return_if_fail (items != NULL);
104
105   real_add (items, n_items, FALSE);
106 }
107
108 /**
109  * gtk_stock_lookup:
110  * @stock_id: a stock item name
111  * @item: stock item to initialize with values
112  * 
113  * Fills @item with the registered values for @stock_id, returning %TRUE
114  * if @stock_id was known.
115  * 
116  * 
117  * Return value: %TRUE if @item was initialized
118  **/
119 gboolean
120 gtk_stock_lookup (const gchar  *stock_id,
121                   GtkStockItem *item)
122 {
123   const GtkStockItem *found;
124
125   g_return_val_if_fail (stock_id != NULL, FALSE);
126   g_return_val_if_fail (item != NULL, FALSE);
127
128   init_stock_hash ();
129
130   found = g_hash_table_lookup (stock_hash, stock_id);
131
132   if (found)
133     {
134       *item = *found;
135       if (item->label)
136         item->label = dgettext (item->translation_domain, item->label);
137     }
138
139   return found != NULL;
140 }
141
142 static void
143 listify_foreach (gpointer key, gpointer value, gpointer data)
144 {
145   GSList **list = data;
146
147   *list = g_slist_prepend (*list, value);
148 }
149
150 static GSList *
151 g_hash_table_get_values (GHashTable *table)
152 {
153   GSList *list = NULL;
154
155   g_hash_table_foreach (table, listify_foreach, &list);
156
157   return list;
158 }
159
160 /**
161  * gtk_stock_list_items:
162  * 
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().
166  * 
167  * Return value: a list of registered stock items
168  **/
169 GSList *
170 gtk_stock_list_items (void)
171 {
172   init_stock_hash ();
173
174   return g_hash_table_get_values (stock_hash);
175 }
176
177 /**
178  * gtk_stock_item_copy:
179  * @item: a #GtkStockItem
180  * 
181  * Copies a stock item, mostly useful for language bindings and not in applications.
182  * 
183  * Return value: a new #GtkStockItem
184  **/
185 GtkStockItem *
186 gtk_stock_item_copy (const GtkStockItem *item)
187 {
188   GtkStockItem *copy;
189
190   g_return_val_if_fail (item != NULL, NULL);
191
192   copy = g_new (GtkStockItem, 1);
193
194   *copy = *item;
195
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);
199
200   return copy;
201 }
202
203 /**
204  * gtk_stock_item_free:
205  * @item: a #GtkStockItem
206  *
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.
210  * 
211  **/
212 void
213 gtk_stock_item_free (GtkStockItem *item)
214 {
215   g_return_if_fail (item != NULL);
216
217   g_free ((gchar*)item->stock_id);
218   g_free ((gchar*)item->label);
219   g_free ((gchar*)item->translation_domain);
220
221   g_free (item);
222 }
223
224 static GtkStockItem builtin_items [] =
225 {
226   /* KEEP IN SYNC with gtkiconfactory.c stock icons */ 
227  
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 },
232
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 },
239
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 }
246 };
247
248 static void
249 init_stock_hash (void)
250 {
251   if (stock_hash == NULL)
252     {
253       stock_hash = g_hash_table_new (g_str_hash, g_str_equal);
254
255       gtk_stock_add_static (builtin_items, G_N_ELEMENTS (builtin_items));
256     }
257 }