]> Pileus Git - ~andy/gtk/blob - gtk/gtkstock.c
Add hidden aliases for exported symbols which are used internally in order
[~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 <config.h>
28 #include <string.h>
29
30 #include "gtkalias.h"
31 #include "gtkstock.h"
32 #include "gtkiconfactory.h"
33 #include "gtkintl.h"
34 #include <gdk/gdkkeysyms.h>
35
36 static GHashTable *stock_hash = NULL;
37 static void init_stock_hash (void);
38
39 /* We use an unused modifier bit to mark stock items which
40  * must be freed when they are removed from the hash table.
41  */
42 #define NON_STATIC_MASK (1 << 29)
43
44 static void
45 real_add (const GtkStockItem *items,
46           guint               n_items,
47           gboolean            copy)
48 {
49   int i;
50
51   init_stock_hash ();
52
53   if (n_items == 0)
54     return;
55
56   i = 0;
57   while (i < n_items)
58     {
59       gpointer old_key, old_value;
60       const GtkStockItem *item = &items[i];
61
62       if (item->modifier & NON_STATIC_MASK)
63         {
64           g_warning ("Bit 29 set in stock accelerator.\n");
65           copy = TRUE;
66         }
67
68       if (copy)
69         {
70           item = gtk_stock_item_copy (item);
71           ((GtkStockItem *)item)->modifier |= NON_STATIC_MASK;
72         }
73
74       if (g_hash_table_lookup_extended (stock_hash, item->stock_id,
75                                         &old_key, &old_value))
76         {
77           g_hash_table_remove (stock_hash, old_key);
78           if (((GtkStockItem *)old_value)->modifier & NON_STATIC_MASK)
79             gtk_stock_item_free (old_value);
80         }
81       
82       g_hash_table_insert (stock_hash,
83                            (gchar*)item->stock_id, (GtkStockItem*)item);
84
85       ++i;
86     }
87 }
88
89 /**
90  * gtk_stock_add:
91  * @items: a #GtkStockItem or array of items
92  * @n_items: number of #GtkStockItem in @items
93  *
94  * Registers each of the stock items in @items. If an item already
95  * exists with the same stock ID as one of the @items, the old item
96  * gets replaced. The stock items are copied, so GTK+ does not hold
97  * any pointer into @items and @items can be freed. Use
98  * gtk_stock_add_static() if @items is persistent and GTK+ need not
99  * copy the array.
100  * 
101  **/
102 void
103 gtk_stock_add (const GtkStockItem *items,
104                guint               n_items)
105 {
106   g_return_if_fail (items != NULL);
107
108   real_add (items, n_items, TRUE);
109 }
110
111 /**
112  * gtk_stock_add_static:
113  * @items: a #GtkStockItem or array of #GtkStockItem
114  * @n_items: number of items
115  *
116  * Same as gtk_stock_add(), but doesn't copy @items, so
117  * @items must persist until application exit.
118  * 
119  **/
120 void
121 gtk_stock_add_static (const GtkStockItem *items,
122                       guint               n_items)
123 {
124   g_return_if_fail (items != NULL);
125
126   real_add (items, n_items, FALSE);
127 }
128
129 /**
130  * gtk_stock_lookup:
131  * @stock_id: a stock item name
132  * @item: stock item to initialize with values
133  * 
134  * Fills @item with the registered values for @stock_id, returning %TRUE
135  * if @stock_id was known.
136  * 
137  * 
138  * Return value: %TRUE if @item was initialized
139  **/
140 gboolean
141 gtk_stock_lookup (const gchar  *stock_id,
142                   GtkStockItem *item)
143 {
144   const GtkStockItem *found;
145
146   g_return_val_if_fail (stock_id != NULL, FALSE);
147   g_return_val_if_fail (item != NULL, FALSE);
148
149   init_stock_hash ();
150
151   found = g_hash_table_lookup (stock_hash, stock_id);
152
153   if (found)
154     {
155       *item = *found;
156       item->modifier &= ~NON_STATIC_MASK;
157       if (item->label)
158         item->label = dgettext (item->translation_domain, item->label);
159     }
160
161   return found != NULL;
162 }
163
164 static void
165 listify_foreach (gpointer key, gpointer value, gpointer data)
166 {
167   GSList **list = data;
168
169   *list = g_slist_prepend (*list, key);
170 }
171
172 static GSList *
173 g_hash_table_get_keys (GHashTable *table)
174 {
175   GSList *list = NULL;
176
177   g_hash_table_foreach (table, listify_foreach, &list);
178
179   return list;
180 }
181
182 /**
183  * gtk_stock_list_ids:
184  * 
185  * Retrieves a list of all known stock IDs added to a #GtkIconFactory
186  * or registered with gtk_stock_add(). The list must be freed with g_slist_free(),
187  * and each string in the list must be freed with g_free().
188  * 
189  * Return value: a list of known stock IDs
190  **/
191 GSList*
192 gtk_stock_list_ids (void)
193 {
194   GSList *ids;
195   GSList *icon_ids;
196   GSList *retval;
197   GSList *tmp_list;
198   const gchar *last_id;
199   
200   init_stock_hash ();
201
202   ids = g_hash_table_get_keys (stock_hash);
203   icon_ids = _gtk_icon_factory_list_ids ();
204   ids = g_slist_concat (ids, icon_ids);
205
206   ids = g_slist_sort (ids, (GCompareFunc)strcmp);
207
208   last_id = NULL;
209   retval = NULL;
210   tmp_list = ids;
211   while (tmp_list != NULL)
212     {
213       GSList *next;
214
215       next = g_slist_next (tmp_list);
216
217       if (last_id && strcmp (tmp_list->data, last_id) == 0)
218         {
219           /* duplicate, ignore */
220         }
221       else
222         {
223           retval = g_slist_prepend (retval, g_strdup (tmp_list->data));
224           last_id = tmp_list->data;
225         }
226
227       g_slist_free_1 (tmp_list);
228       
229       tmp_list = next;
230     }
231
232   return retval;
233 }
234
235 /**
236  * gtk_stock_item_copy:
237  * @item: a #GtkStockItem
238  * 
239  * Copies a stock item, mostly useful for language bindings and not in applications.
240  * 
241  * Return value: a new #GtkStockItem
242  **/
243 GtkStockItem *
244 gtk_stock_item_copy (const GtkStockItem *item)
245 {
246   GtkStockItem *copy;
247
248   g_return_val_if_fail (item != NULL, NULL);
249
250   copy = g_new (GtkStockItem, 1);
251
252   *copy = *item;
253
254   copy->stock_id = g_strdup (item->stock_id);
255   copy->label = g_strdup (item->label);
256   copy->translation_domain = g_strdup (item->translation_domain);
257
258   return copy;
259 }
260
261 /**
262  * gtk_stock_item_free:
263  * @item: a #GtkStockItem
264  *
265  * Frees a stock item allocated on the heap, such as one returned by
266  * gtk_stock_item_copy(). Also frees the fields inside the stock item,
267  * if they are not %NULL.
268  * 
269  **/
270 void
271 gtk_stock_item_free (GtkStockItem *item)
272 {
273   g_return_if_fail (item != NULL);
274
275   g_free ((gchar*)item->stock_id);
276   g_free ((gchar*)item->label);
277   g_free ((gchar*)item->translation_domain);
278
279   g_free (item);
280 }
281
282 static const GtkStockItem builtin_items [] =
283 {
284   /* KEEP IN SYNC with gtkiconfactory.c stock icons, when appropriate */ 
285  
286   { GTK_STOCK_DIALOG_INFO, N_("Information"), 0, 0, GETTEXT_PACKAGE },
287   { GTK_STOCK_DIALOG_WARNING, N_("Warning"), 0, 0, GETTEXT_PACKAGE },
288   { GTK_STOCK_DIALOG_ERROR, N_("Error"), 0, 0, GETTEXT_PACKAGE },
289   { GTK_STOCK_DIALOG_QUESTION, N_("Question"), 0, 0, GETTEXT_PACKAGE },
290
291   /*  FIXME these need accelerators when appropriate, and
292    * need the mnemonics to be rationalized
293    */
294   { GTK_STOCK_ABOUT, N_("_About"), 0, 0, GETTEXT_PACKAGE },
295   { GTK_STOCK_ADD, N_("_Add"), 0, 0, GETTEXT_PACKAGE },
296   { GTK_STOCK_APPLY, N_("_Apply"), 0, 0, GETTEXT_PACKAGE },
297   { GTK_STOCK_BOLD, N_("_Bold"), 0, 0, GETTEXT_PACKAGE },
298   { GTK_STOCK_CANCEL, N_("_Cancel"), 0, 0, GETTEXT_PACKAGE },
299   { GTK_STOCK_CDROM, N_("_CD-Rom"), 0, 0, GETTEXT_PACKAGE },
300   { GTK_STOCK_CLEAR, N_("_Clear"), 0, 0, GETTEXT_PACKAGE },
301   { GTK_STOCK_CLOSE, N_("_Close"), GDK_CONTROL_MASK, 'w', GETTEXT_PACKAGE },
302   { GTK_STOCK_CONVERT, N_("_Convert"), 0, 0, GETTEXT_PACKAGE },
303   { GTK_STOCK_COPY, N_("_Copy"), GDK_CONTROL_MASK, 'c', GETTEXT_PACKAGE },
304   { GTK_STOCK_CUT, N_("Cu_t"), GDK_CONTROL_MASK, 'x', GETTEXT_PACKAGE },
305   { GTK_STOCK_DELETE, N_("_Delete"), 0, 0, GETTEXT_PACKAGE },
306   { GTK_STOCK_EXECUTE, N_("_Execute"), 0, 0, GETTEXT_PACKAGE },
307   { GTK_STOCK_EDIT, N_("_Edit"), 0, 0, GETTEXT_PACKAGE },
308   { GTK_STOCK_FIND, N_("_Find"), GDK_CONTROL_MASK, 'f', GETTEXT_PACKAGE },
309   { GTK_STOCK_FIND_AND_REPLACE, N_("Find and _Replace"), GDK_CONTROL_MASK, 'r', GETTEXT_PACKAGE },
310   { GTK_STOCK_FLOPPY, N_("_Floppy"), 0, 0, GETTEXT_PACKAGE },
311   { GTK_STOCK_GOTO_BOTTOM, N_("_Bottom"), 0, 0, GETTEXT_PACKAGE },
312   { GTK_STOCK_GOTO_FIRST, N_("_First"), 0, 0, GETTEXT_PACKAGE },
313   { GTK_STOCK_GOTO_LAST, N_("_Last"), 0, 0, GETTEXT_PACKAGE },
314   { GTK_STOCK_GOTO_TOP, N_("_Top"), 0, 0, GETTEXT_PACKAGE },
315   { GTK_STOCK_GO_BACK, N_("_Back"), 0, 0, GETTEXT_PACKAGE },
316   { GTK_STOCK_GO_DOWN, N_("_Down"), 0, 0, GETTEXT_PACKAGE },
317   { GTK_STOCK_GO_FORWARD, N_("_Forward"), 0, 0, GETTEXT_PACKAGE },
318   { GTK_STOCK_GO_UP, N_("_Up"), 0, 0, GETTEXT_PACKAGE },
319   { GTK_STOCK_HARDDISK, N_("_Harddisk"), 0, 0, GETTEXT_PACKAGE },
320   { GTK_STOCK_HELP, N_("_Help"), GDK_CONTROL_MASK, 'h', GETTEXT_PACKAGE },
321   { GTK_STOCK_HOME, N_("_Home"), 0, 0, GETTEXT_PACKAGE },
322   { GTK_STOCK_INDENT, N_("Increase Indent"), 0, 0, GETTEXT_PACKAGE },
323   { GTK_STOCK_UNINDENT, N_("Decrease Indent"), 0, 0, GETTEXT_PACKAGE },
324   { GTK_STOCK_INDEX, N_("_Index"), 0, 0, GETTEXT_PACKAGE },
325   { GTK_STOCK_ITALIC, N_("_Italic"), 0, 0, GETTEXT_PACKAGE },
326   { GTK_STOCK_JUMP_TO, N_("_Jump to"), 0, 0, GETTEXT_PACKAGE },
327   { GTK_STOCK_JUSTIFY_CENTER, N_("_Center"), 0, 0, GETTEXT_PACKAGE },
328   { GTK_STOCK_JUSTIFY_FILL, N_("_Fill"), 0, 0, GETTEXT_PACKAGE },
329   { GTK_STOCK_JUSTIFY_LEFT, N_("_Left"), 0, 0, GETTEXT_PACKAGE },
330   { GTK_STOCK_JUSTIFY_RIGHT, N_("_Right"), 0, 0, GETTEXT_PACKAGE },
331   { GTK_STOCK_MEDIA_FORWARD, N_("_Forward"), 0, 0, GETTEXT_PACKAGE },
332   { GTK_STOCK_MEDIA_NEXT, N_("_Next"), 0, 0, GETTEXT_PACKAGE },
333   { GTK_STOCK_MEDIA_PAUSE, N_("P_ause"), 0, 0, GETTEXT_PACKAGE },
334   { GTK_STOCK_MEDIA_PLAY, N_("_Play"), 0, 0, GETTEXT_PACKAGE },
335   { GTK_STOCK_MEDIA_PREVIOUS, N_("Pre_vious"), 0, 0, GETTEXT_PACKAGE },
336   { GTK_STOCK_MEDIA_RECORD, N_("_Record"), 0, 0, GETTEXT_PACKAGE },
337   { GTK_STOCK_MEDIA_REWIND, N_("R_ewind"), 0, 0, GETTEXT_PACKAGE },
338   { GTK_STOCK_MEDIA_STOP, N_("_Stop"), 0, 0, GETTEXT_PACKAGE },
339   { GTK_STOCK_NETWORK, N_("_Network"), 0, 0, GETTEXT_PACKAGE },
340   { GTK_STOCK_NEW, N_("_New"), GDK_CONTROL_MASK, 'n', GETTEXT_PACKAGE },
341   { GTK_STOCK_NO, N_("_No"), 0, 0, GETTEXT_PACKAGE },
342   { GTK_STOCK_OK, N_("_OK"), 0, 0, GETTEXT_PACKAGE },
343   { GTK_STOCK_OPEN, N_("_Open"), GDK_CONTROL_MASK, 'o', GETTEXT_PACKAGE },
344   { GTK_STOCK_PASTE, N_("_Paste"), GDK_CONTROL_MASK, 'v', GETTEXT_PACKAGE },
345   { GTK_STOCK_PREFERENCES, N_("_Preferences"), 0, 0, GETTEXT_PACKAGE },
346   { GTK_STOCK_PRINT, N_("_Print"), 0, 0, GETTEXT_PACKAGE },
347   { GTK_STOCK_PRINT_PREVIEW, N_("Print Pre_view"), 0, 0, GETTEXT_PACKAGE },
348   { GTK_STOCK_PROPERTIES, N_("_Properties"), 0, 0, GETTEXT_PACKAGE },
349   { GTK_STOCK_QUIT, N_("_Quit"), GDK_CONTROL_MASK, 'q', GETTEXT_PACKAGE },
350   { GTK_STOCK_REDO, N_("_Redo"), 0, 0, GETTEXT_PACKAGE },
351   { GTK_STOCK_REFRESH, N_("_Refresh"), 0, 0, GETTEXT_PACKAGE },
352   { GTK_STOCK_REMOVE, N_("_Remove"), 0, 0, GETTEXT_PACKAGE },
353   { GTK_STOCK_REVERT_TO_SAVED, N_("_Revert"), 0, 0, GETTEXT_PACKAGE },
354   { GTK_STOCK_SAVE, N_("_Save"), GDK_CONTROL_MASK, 's', GETTEXT_PACKAGE },
355   { GTK_STOCK_SAVE_AS, N_("Save _As"), 0, 0, GETTEXT_PACKAGE },
356   { GTK_STOCK_SELECT_COLOR, N_("_Color"), 0, 0, GETTEXT_PACKAGE },
357   { GTK_STOCK_SELECT_FONT, N_("_Font"), 0, 0, GETTEXT_PACKAGE },
358   { GTK_STOCK_SORT_ASCENDING, N_("_Ascending"), 0, 0, GETTEXT_PACKAGE },
359   { GTK_STOCK_SORT_DESCENDING, N_("_Descending"), 0, 0, GETTEXT_PACKAGE },
360   { GTK_STOCK_SPELL_CHECK, N_("_Spell Check"), 0, 0, GETTEXT_PACKAGE },
361   { GTK_STOCK_STOP, N_("_Stop"), 0, 0, GETTEXT_PACKAGE },
362   { GTK_STOCK_STRIKETHROUGH, N_("_Strikethrough"), 0, 0, GETTEXT_PACKAGE },
363   { GTK_STOCK_UNDELETE, N_("_Undelete"), 0, 0, GETTEXT_PACKAGE },
364   { GTK_STOCK_UNDERLINE, N_("_Underline"), 0, 0, GETTEXT_PACKAGE },
365   { GTK_STOCK_UNDO, N_("_Undo"), 0, 0, GETTEXT_PACKAGE },
366   { GTK_STOCK_YES, N_("_Yes"), 0, 0, GETTEXT_PACKAGE },
367   { GTK_STOCK_ZOOM_100, N_("_Normal Size"), 0, 0, GETTEXT_PACKAGE },
368   { GTK_STOCK_ZOOM_FIT, N_("Best _Fit"), 0, 0, GETTEXT_PACKAGE },
369   { GTK_STOCK_ZOOM_IN, N_("Zoom _In"), 0, 0, GETTEXT_PACKAGE },
370   { GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), 0, 0, GETTEXT_PACKAGE }
371 };
372
373 static void
374 init_stock_hash (void)
375 {
376   if (stock_hash == NULL)
377     {
378       stock_hash = g_hash_table_new (g_str_hash, g_str_equal);
379
380       gtk_stock_add_static (builtin_items, G_N_ELEMENTS (builtin_items));
381     }
382 }