]> Pileus Git - ~andy/gtk/blob - gtk/gtkstock.c
Move documentation to inline comments: GtkStock
[~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 "gtkprivate.h"
31 #include "gtkstock.h"
32 #include "gtkiconfactory.h"
33 #include "gtkintl.h"
34 #include <gdk/gdkkeysyms.h>
35 #include "gtkalias.h"
36
37 /**
38  * SECTION:gtkstock
39  * @Short_description:
40 Prebuilt common menu/toolbar items and corresponding icons
41  * @Title: Stock Items
42  *
43  * Stock items represent commonly-used menu or toolbar items such as
44  * "Open" or "Exit". Each stock item is identified by a stock ID;
45  * stock IDs are just strings, but macros such as #GTK_STOCK_OPEN are
46  * provided to avoid typing mistakes in the strings.
47  * Applications can register their own stock items in addition to those
48  * built-in to GTK+.
49  *
50  * Each stock ID can be associated with a #GtkStockItem, which contains
51  * the user-visible label, keyboard accelerator, and translation domain
52  * of the menu or toolbar item; and/or with an icon stored in a
53  * #GtkIconFactory. See <link
54  * linkend="gtk-Themeable-Stock-Images">GtkIconFactory</link> for
55  * more information on stock icons. The connection between a
56  * #GtkStockItem and stock icons is purely conventional (by virtue of
57  * using the same stock ID); it's possible to register a stock item but
58  * no icon, and vice versa. Stock icons may have a RTL variant which gets
59  * used for right-to-left locales.
60  */
61
62 static GHashTable *translate_hash = NULL;
63 static GHashTable *stock_hash = NULL;
64 static void init_stock_hash (void);
65
66 /* We use an unused modifier bit to mark stock items which
67  * must be freed when they are removed from the hash table.
68  */
69 #define NON_STATIC_MASK (1 << 29)
70
71 typedef struct _GtkStockTranslateFunc GtkStockTranslateFunc;
72 struct _GtkStockTranslateFunc
73 {
74   GtkTranslateFunc func;
75   gpointer data;
76   GDestroyNotify notify;
77 };
78
79 static void
80 real_add (const GtkStockItem *items,
81           guint               n_items,
82           gboolean            copy)
83 {
84   int i;
85
86   init_stock_hash ();
87
88   if (n_items == 0)
89     return;
90
91   i = 0;
92   while (i < n_items)
93     {
94       gpointer old_key, old_value;
95       const GtkStockItem *item = &items[i];
96
97       if (item->modifier & NON_STATIC_MASK)
98         {
99           g_warning ("Bit 29 set in stock accelerator.\n");
100           copy = TRUE;
101         }
102
103       if (copy)
104         {
105           item = gtk_stock_item_copy (item);
106           ((GtkStockItem *)item)->modifier |= NON_STATIC_MASK;
107         }
108
109       if (g_hash_table_lookup_extended (stock_hash, item->stock_id,
110                                         &old_key, &old_value))
111         {
112           g_hash_table_remove (stock_hash, old_key);
113           if (((GtkStockItem *)old_value)->modifier & NON_STATIC_MASK)
114             gtk_stock_item_free (old_value);
115         }
116       
117       g_hash_table_insert (stock_hash,
118                            (gchar*)item->stock_id, (GtkStockItem*)item);
119
120       ++i;
121     }
122 }
123
124 /**
125  * gtk_stock_add:
126  * @items: a #GtkStockItem or array of items
127  * @n_items: number of #GtkStockItem in @items
128  *
129  * Registers each of the stock items in @items. If an item already
130  * exists with the same stock ID as one of the @items, the old item
131  * gets replaced. The stock items are copied, so GTK+ does not hold
132  * any pointer into @items and @items can be freed. Use
133  * gtk_stock_add_static() if @items is persistent and GTK+ need not
134  * copy the array.
135  * 
136  **/
137 void
138 gtk_stock_add (const GtkStockItem *items,
139                guint               n_items)
140 {
141   g_return_if_fail (items != NULL);
142
143   real_add (items, n_items, TRUE);
144 }
145
146 /**
147  * gtk_stock_add_static:
148  * @items: a #GtkStockItem or array of #GtkStockItem
149  * @n_items: number of items
150  *
151  * Same as gtk_stock_add(), but doesn't copy @items, so
152  * @items must persist until application exit.
153  * 
154  **/
155 void
156 gtk_stock_add_static (const GtkStockItem *items,
157                       guint               n_items)
158 {
159   g_return_if_fail (items != NULL);
160
161   real_add (items, n_items, FALSE);
162 }
163
164 /**
165  * gtk_stock_lookup:
166  * @stock_id: a stock item name
167  * @item: stock item to initialize with values
168  * 
169  * Fills @item with the registered values for @stock_id, returning %TRUE
170  * if @stock_id was known.
171  * 
172  * 
173  * Return value: %TRUE if @item was initialized
174  **/
175 gboolean
176 gtk_stock_lookup (const gchar  *stock_id,
177                   GtkStockItem *item)
178 {
179   const GtkStockItem *found;
180
181   g_return_val_if_fail (stock_id != NULL, FALSE);
182   g_return_val_if_fail (item != NULL, FALSE);
183
184   init_stock_hash ();
185
186   found = g_hash_table_lookup (stock_hash, stock_id);
187
188   if (found)
189     {
190       *item = *found;
191       item->modifier &= ~NON_STATIC_MASK;
192       if (item->label)
193         {
194           GtkStockTranslateFunc *translate;
195           
196           if (item->translation_domain)
197             translate = (GtkStockTranslateFunc *) 
198               g_hash_table_lookup (translate_hash, item->translation_domain);
199           else
200             translate = NULL;
201           
202           if (translate != NULL && translate->func != NULL)
203             item->label = (* translate->func) (item->label, translate->data);
204           else
205             item->label = (gchar *) g_dgettext (item->translation_domain, item->label);
206         }
207     }
208
209   return found != NULL;
210 }
211
212 /**
213  * gtk_stock_list_ids:
214  * 
215  * Retrieves a list of all known stock IDs added to a #GtkIconFactory
216  * or registered with gtk_stock_add(). The list must be freed with g_slist_free(),
217  * and each string in the list must be freed with g_free().
218  * 
219  * Return value: a list of known stock IDs
220  **/
221 GSList*
222 gtk_stock_list_ids (void)
223 {
224   GList *ids;
225   GList *icon_ids;
226   GSList *retval;
227   const gchar *last_id;
228   
229   init_stock_hash ();
230
231   ids = g_hash_table_get_keys (stock_hash);
232   icon_ids = _gtk_icon_factory_list_ids ();
233   ids = g_list_concat (ids, icon_ids);
234
235   ids = g_list_sort (ids, (GCompareFunc)strcmp);
236
237   last_id = NULL;
238   retval = NULL;
239   while (ids != NULL)
240     {
241       GList *next;
242
243       next = g_list_next (ids);
244
245       if (last_id && strcmp (ids->data, last_id) == 0)
246         {
247           /* duplicate, ignore */
248         }
249       else
250         {
251           retval = g_slist_prepend (retval, g_strdup (ids->data));
252           last_id = ids->data;
253         }
254
255       g_list_free_1 (ids);
256       
257       ids = next;
258     }
259
260   return retval;
261 }
262
263 /**
264  * gtk_stock_item_copy:
265  * @item: a #GtkStockItem
266  * 
267  * Copies a stock item, mostly useful for language bindings and not in applications.
268  * 
269  * Return value: a new #GtkStockItem
270  **/
271 GtkStockItem *
272 gtk_stock_item_copy (const GtkStockItem *item)
273 {
274   GtkStockItem *copy;
275
276   g_return_val_if_fail (item != NULL, NULL);
277
278   copy = g_new (GtkStockItem, 1);
279
280   *copy = *item;
281
282   copy->stock_id = g_strdup (item->stock_id);
283   copy->label = g_strdup (item->label);
284   copy->translation_domain = g_strdup (item->translation_domain);
285
286   return copy;
287 }
288
289 /**
290  * gtk_stock_item_free:
291  * @item: a #GtkStockItem
292  *
293  * Frees a stock item allocated on the heap, such as one returned by
294  * gtk_stock_item_copy(). Also frees the fields inside the stock item,
295  * if they are not %NULL.
296  * 
297  **/
298 void
299 gtk_stock_item_free (GtkStockItem *item)
300 {
301   g_return_if_fail (item != NULL);
302
303   g_free ((gchar*)item->stock_id);
304   g_free ((gchar*)item->label);
305   g_free ((gchar*)item->translation_domain);
306
307   g_free (item);
308 }
309
310 static const GtkStockItem builtin_items [] =
311 {
312   /* KEEP IN SYNC with gtkiconfactory.c stock icons, when appropriate */ 
313  
314   { GTK_STOCK_DIALOG_INFO, NC_("Stock label", "Information"), 0, 0, GETTEXT_PACKAGE },
315   { GTK_STOCK_DIALOG_WARNING, NC_("Stock label", "Warning"), 0, 0, GETTEXT_PACKAGE },
316   { GTK_STOCK_DIALOG_ERROR, NC_("Stock label", "Error"), 0, 0, GETTEXT_PACKAGE },
317   { GTK_STOCK_DIALOG_QUESTION, NC_("Stock label", "Question"), 0, 0, GETTEXT_PACKAGE },
318
319   /*  FIXME these need accelerators when appropriate, and
320    * need the mnemonics to be rationalized
321    */
322   { GTK_STOCK_ABOUT, NC_("Stock label", "_About"), 0, 0, GETTEXT_PACKAGE },
323   { GTK_STOCK_ADD, NC_("Stock label", "_Add"), 0, 0, GETTEXT_PACKAGE },
324   { GTK_STOCK_APPLY, NC_("Stock label", "_Apply"), 0, 0, GETTEXT_PACKAGE },
325   { GTK_STOCK_BOLD, NC_("Stock label", "_Bold"), 0, 0, GETTEXT_PACKAGE },
326   { GTK_STOCK_CANCEL, NC_("Stock label", "_Cancel"), 0, 0, GETTEXT_PACKAGE },
327   { GTK_STOCK_CDROM, NC_("Stock label", "_CD-Rom"), 0, 0, GETTEXT_PACKAGE },
328   { GTK_STOCK_CLEAR, NC_("Stock label", "_Clear"), 0, 0, GETTEXT_PACKAGE },
329   { GTK_STOCK_CLOSE, NC_("Stock label", "_Close"), GDK_CONTROL_MASK, 'w', GETTEXT_PACKAGE },
330   { GTK_STOCK_CONNECT, NC_("Stock label", "C_onnect"), 0, 0, GETTEXT_PACKAGE },
331   { GTK_STOCK_CONVERT, NC_("Stock label", "_Convert"), 0, 0, GETTEXT_PACKAGE },
332    { GTK_STOCK_COPY, NC_("Stock label", "_Copy"), GDK_CONTROL_MASK, 'c', GETTEXT_PACKAGE },
333   { GTK_STOCK_CUT, NC_("Stock label", "Cu_t"), GDK_CONTROL_MASK, 'x', GETTEXT_PACKAGE },
334   { GTK_STOCK_DELETE, NC_("Stock label", "_Delete"), 0, 0, GETTEXT_PACKAGE },
335   { GTK_STOCK_DISCARD, NC_("Stock label", "_Discard"), 0, 0, GETTEXT_PACKAGE },
336   { GTK_STOCK_DISCONNECT, NC_("Stock label", "_Disconnect"), 0, 0, GETTEXT_PACKAGE },
337   { GTK_STOCK_EXECUTE, NC_("Stock label", "_Execute"), 0, 0, GETTEXT_PACKAGE },
338   { GTK_STOCK_EDIT, NC_("Stock label", "_Edit"), 0, 0, GETTEXT_PACKAGE },
339   { GTK_STOCK_FIND, NC_("Stock label", "_Find"), GDK_CONTROL_MASK, 'f', GETTEXT_PACKAGE },
340   { GTK_STOCK_FIND_AND_REPLACE, NC_("Stock label", "Find and _Replace"), GDK_CONTROL_MASK, 'r', GETTEXT_PACKAGE },
341   { GTK_STOCK_FLOPPY, NC_("Stock label", "_Floppy"), 0, 0, GETTEXT_PACKAGE },
342   { GTK_STOCK_FULLSCREEN, NC_("Stock label", "_Fullscreen"), 0, 0, GETTEXT_PACKAGE },
343   { GTK_STOCK_LEAVE_FULLSCREEN, NC_("Stock label", "_Leave Fullscreen"), 0, 0, GETTEXT_PACKAGE },
344   /* This is a navigation label as in "go to the bottom of the page" */
345   { GTK_STOCK_GOTO_BOTTOM, NC_("Stock label, navigation", "_Bottom"), 0, 0, GETTEXT_PACKAGE "-navigation" },
346   /* This is a navigation label as in "go to the first page" */
347   { GTK_STOCK_GOTO_FIRST, NC_("Stock label, navigation", "_First"), 0, 0, GETTEXT_PACKAGE "-navigation" },
348   /* This is a navigation label as in "go to the last page" */
349   { GTK_STOCK_GOTO_LAST, NC_("Stock label, navigation", "_Last"), 0, 0, GETTEXT_PACKAGE "-navigation" },
350   /* This is a navigation label as in "go to the top of the page" */
351   { GTK_STOCK_GOTO_TOP, NC_("Stock label, navigation", "_Top"), 0, 0, GETTEXT_PACKAGE "-navigation" },
352   /* This is a navigation label as in "go back" */
353   { GTK_STOCK_GO_BACK, NC_("Stock label, navigation", "_Back"), 0, 0, GETTEXT_PACKAGE "-navigation" },
354   /* This is a navigation label as in "go down" */
355   { GTK_STOCK_GO_DOWN, NC_("Stock label, navigation", "_Down"), 0, 0, GETTEXT_PACKAGE "-navigation" },
356   /* This is a navigation label as in "go forward" */
357   { GTK_STOCK_GO_FORWARD, NC_("Stock label, navigation", "_Forward"), 0, 0, GETTEXT_PACKAGE "-navigation" },
358   /* This is a navigation label as in "go up" */
359   { GTK_STOCK_GO_UP, NC_("Stock label, navigation", "_Up"), 0, 0, GETTEXT_PACKAGE "-navigation" },
360   { GTK_STOCK_HARDDISK, NC_("Stock label", "_Harddisk"), 0, 0, GETTEXT_PACKAGE },
361   { GTK_STOCK_HELP, NC_("Stock label", "_Help"), GDK_CONTROL_MASK, 'h', GETTEXT_PACKAGE },
362   { GTK_STOCK_HOME, NC_("Stock label", "_Home"), 0, 0, GETTEXT_PACKAGE },
363   { GTK_STOCK_INDENT, NC_("Stock label", "Increase Indent"), 0, 0, GETTEXT_PACKAGE },
364   { GTK_STOCK_UNINDENT, NC_("Stock label", "Decrease Indent"), 0, 0, GETTEXT_PACKAGE },
365   { GTK_STOCK_INDEX, NC_("Stock label", "_Index"), 0, 0, GETTEXT_PACKAGE },
366   { GTK_STOCK_INFO, NC_("Stock label", "_Information"), 0, 0, GETTEXT_PACKAGE },
367   { GTK_STOCK_ITALIC, NC_("Stock label", "_Italic"), 0, 0, GETTEXT_PACKAGE },
368   { GTK_STOCK_JUMP_TO, NC_("Stock label", "_Jump to"), 0, 0, GETTEXT_PACKAGE },
369   /* This is about text justification, "centered text" */
370   { GTK_STOCK_JUSTIFY_CENTER, NC_("Stock label", "_Center"), 0, 0, GETTEXT_PACKAGE },
371   /* This is about text justification */
372   { GTK_STOCK_JUSTIFY_FILL, NC_("Stock label", "_Fill"), 0, 0, GETTEXT_PACKAGE },
373   /* This is about text justification, "left-justified text" */
374   { GTK_STOCK_JUSTIFY_LEFT, NC_("Stock label", "_Left"), 0, 0, GETTEXT_PACKAGE },
375   /* This is about text justification, "right-justified text" */
376   { GTK_STOCK_JUSTIFY_RIGHT, NC_("Stock label", "_Right"), 0, 0, GETTEXT_PACKAGE },
377
378   /* Media label, as in "fast forward" */
379   { GTK_STOCK_MEDIA_FORWARD, NC_("Stock label, media", "_Forward"), 0, 0, GETTEXT_PACKAGE "-media" },
380   /* Media label, as in "next song" */
381   { GTK_STOCK_MEDIA_NEXT, NC_("Stock label, media", "_Next"), 0, 0, GETTEXT_PACKAGE "-media" },
382   /* Media label, as in "pause music" */
383   { GTK_STOCK_MEDIA_PAUSE, NC_("Stock label, media", "P_ause"), 0, 0, GETTEXT_PACKAGE "-media" },
384   /* Media label, as in "play music" */
385   { GTK_STOCK_MEDIA_PLAY, NC_("Stock label, media", "_Play"), 0, 0, GETTEXT_PACKAGE "-media" },
386   /* Media label, as in  "previous song" */
387   { GTK_STOCK_MEDIA_PREVIOUS, NC_("Stock label, media", "Pre_vious"), 0, 0, GETTEXT_PACKAGE "-media" },
388   /* Media label */
389   { GTK_STOCK_MEDIA_RECORD, NC_("Stock label, media", "_Record"), 0, 0, GETTEXT_PACKAGE "-media" },
390   /* Media label */
391   { GTK_STOCK_MEDIA_REWIND, NC_("Stock label, media", "R_ewind"), 0, 0, GETTEXT_PACKAGE "-media" },
392   /* Media label */
393   { GTK_STOCK_MEDIA_STOP, NC_("Stock label, media", "_Stop"), 0, 0, GETTEXT_PACKAGE "-media" },
394   { GTK_STOCK_NETWORK, NC_("Stock label", "_Network"), 0, 0, GETTEXT_PACKAGE },
395   { GTK_STOCK_NEW, NC_("Stock label", "_New"), GDK_CONTROL_MASK, 'n', GETTEXT_PACKAGE },
396   { GTK_STOCK_NO, NC_("Stock label", "_No"), 0, 0, GETTEXT_PACKAGE },
397   { GTK_STOCK_OK, NC_("Stock label", "_OK"), 0, 0, GETTEXT_PACKAGE },
398   { GTK_STOCK_OPEN, NC_("Stock label", "_Open"), GDK_CONTROL_MASK, 'o', GETTEXT_PACKAGE },
399   /* Page orientation */
400   { GTK_STOCK_ORIENTATION_LANDSCAPE, NC_("Stock label", "Landscape"), 0, 0, GETTEXT_PACKAGE },
401   /* Page orientation */
402   { GTK_STOCK_ORIENTATION_PORTRAIT, NC_("Stock label", "Portrait"), 0, 0, GETTEXT_PACKAGE },
403   /* Page orientation */
404   { GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE, NC_("Stock label", "Reverse landscape"), 0, 0, GETTEXT_PACKAGE },
405   /* Page orientation */
406   { GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT, NC_("Stock label", "Reverse portrait"), 0, 0, GETTEXT_PACKAGE },
407   { GTK_STOCK_PAGE_SETUP, NC_("Stock label", "Page Set_up"), 0, 0, GETTEXT_PACKAGE },
408   { GTK_STOCK_PASTE, NC_("Stock label", "_Paste"), GDK_CONTROL_MASK, 'v', GETTEXT_PACKAGE },
409   { GTK_STOCK_PREFERENCES, NC_("Stock label", "_Preferences"), 0, 0, GETTEXT_PACKAGE },
410   { GTK_STOCK_PRINT, NC_("Stock label", "_Print"), 0, 0, GETTEXT_PACKAGE },
411   { GTK_STOCK_PRINT_PREVIEW, NC_("Stock label", "Print Pre_view"), 0, 0, GETTEXT_PACKAGE },
412   { GTK_STOCK_PROPERTIES, NC_("Stock label", "_Properties"), 0, 0, GETTEXT_PACKAGE },
413   { GTK_STOCK_QUIT, NC_("Stock label", "_Quit"), GDK_CONTROL_MASK, 'q', GETTEXT_PACKAGE },
414   { GTK_STOCK_REDO, NC_("Stock label", "_Redo"), 0, 0, GETTEXT_PACKAGE },
415   { GTK_STOCK_REFRESH, NC_("Stock label", "_Refresh"), 0, 0, GETTEXT_PACKAGE },
416   { GTK_STOCK_REMOVE, NC_("Stock label", "_Remove"), 0, 0, GETTEXT_PACKAGE },
417   { GTK_STOCK_REVERT_TO_SAVED, NC_("Stock label", "_Revert"), 0, 0, GETTEXT_PACKAGE },
418   { GTK_STOCK_SAVE, NC_("Stock label", "_Save"), GDK_CONTROL_MASK, 's', GETTEXT_PACKAGE },
419   { GTK_STOCK_SAVE_AS, NC_("Stock label", "Save _As"), 0, 0, GETTEXT_PACKAGE },
420   { GTK_STOCK_SELECT_ALL, NC_("Stock label", "Select _All"), 0, 0, GETTEXT_PACKAGE },
421   { GTK_STOCK_SELECT_COLOR, NC_("Stock label", "_Color"), 0, 0, GETTEXT_PACKAGE },
422   { GTK_STOCK_SELECT_FONT, NC_("Stock label", "_Font"), 0, 0, GETTEXT_PACKAGE },
423   /* Sorting direction */
424   { GTK_STOCK_SORT_ASCENDING, NC_("Stock label", "_Ascending"), 0, 0, GETTEXT_PACKAGE },
425   /* Sorting direction */
426   { GTK_STOCK_SORT_DESCENDING, NC_("Stock label", "_Descending"), 0, 0, GETTEXT_PACKAGE },
427   { GTK_STOCK_SPELL_CHECK, NC_("Stock label", "_Spell Check"), 0, 0, GETTEXT_PACKAGE },
428   { GTK_STOCK_STOP, NC_("Stock label", "_Stop"), 0, 0, GETTEXT_PACKAGE },
429   /* Font variant */
430   { GTK_STOCK_STRIKETHROUGH, NC_("Stock label", "_Strikethrough"), 0, 0, GETTEXT_PACKAGE },
431   { GTK_STOCK_UNDELETE, NC_("Stock label", "_Undelete"), 0, 0, GETTEXT_PACKAGE },
432   /* Font variant */
433   { GTK_STOCK_UNDERLINE, NC_("Stock label", "_Underline"), 0, 0, GETTEXT_PACKAGE },
434   { GTK_STOCK_UNDO, NC_("Stock label", "_Undo"), 0, 0, GETTEXT_PACKAGE },
435   { GTK_STOCK_YES, NC_("Stock label", "_Yes"), 0, 0, GETTEXT_PACKAGE },
436   /* Zoom */
437   { GTK_STOCK_ZOOM_100, NC_("Stock label", "_Normal Size"), 0, 0, GETTEXT_PACKAGE },
438   /* Zoom */
439   { GTK_STOCK_ZOOM_FIT, NC_("Stock label", "Best _Fit"), 0, 0, GETTEXT_PACKAGE },
440   { GTK_STOCK_ZOOM_IN, NC_("Stock label", "Zoom _In"), 0, 0, GETTEXT_PACKAGE },
441   { GTK_STOCK_ZOOM_OUT, NC_("Stock label", "Zoom _Out"), 0, 0, GETTEXT_PACKAGE }
442 };
443
444 /**
445  * gtk_stock_set_translate_func: 
446  * @domain: the translation domain for which @func shall be used
447  * @func: a #GtkTranslateFunc 
448  * @data: data to pass to @func
449  * @notify: a #GDestroyNotify that is called when @data is
450  *   no longer needed
451  *
452  * Sets a function to be used for translating the @label of 
453  * a stock item.
454  * 
455  * If no function is registered for a translation domain,
456  * g_dgettext() is used.
457  * 
458  * The function is used for all stock items whose
459  * @translation_domain matches @domain. Note that it is possible
460  * to use strings different from the actual gettext translation domain
461  * of your application for this, as long as your #GtkTranslateFunc uses
462  * the correct domain when calling dgettext(). This can be useful, e.g.
463  * when dealing with message contexts:
464  *
465  * |[
466  * GtkStockItem items[] = { 
467  *  { MY_ITEM1, NC_("odd items", "Item 1"), 0, 0, "odd-item-domain" },
468  *  { MY_ITEM2, NC_("even items", "Item 2"), 0, 0, "even-item-domain" },
469  * };
470  *
471  * gchar *
472  * my_translate_func (const gchar *msgid,
473  *                    gpointer     data)
474  * {
475  *   gchar *msgctxt = data;
476  * 
477  *   return (gchar*)g_dpgettext2 (GETTEXT_PACKAGE, msgctxt, msgid);
478  * }
479  *
480  * /&ast; ... &ast;/
481  *
482  * gtk_stock_add (items, G_N_ELEMENTS (items));
483  * gtk_stock_set_translate_func ("odd-item-domain", my_translate_func, "odd items"); 
484  * gtk_stock_set_translate_func ("even-item-domain", my_translate_func, "even items"); 
485  * ]|
486  * 
487  * Since: 2.8
488  */
489 void
490 gtk_stock_set_translate_func (const gchar      *domain,
491                               GtkTranslateFunc  func,
492                               gpointer          data,
493                               GDestroyNotify    notify)
494 {
495   GtkStockTranslateFunc *translate;
496   gchar *domainname;
497  
498   domainname = g_strdup (domain);
499
500   translate = (GtkStockTranslateFunc *) 
501     g_hash_table_lookup (translate_hash, domainname);
502
503   if (translate)
504     {
505       if (translate->notify)
506         (* translate->notify) (translate->data);
507     }
508   else
509     translate = g_new0 (GtkStockTranslateFunc, 1);
510     
511   translate->func = func;
512   translate->data = data;
513   translate->notify = notify;
514       
515   g_hash_table_insert (translate_hash, domainname, translate);
516 }
517
518 static gchar *
519 sgettext_swapped (const gchar *msgid, 
520                   gpointer     data)
521 {
522   gchar *msgctxt = data;
523
524   return (gchar *)g_dpgettext2 (GETTEXT_PACKAGE, msgctxt, msgid);
525 }
526
527 static void
528 init_stock_hash (void)
529 {
530   if (stock_hash == NULL)
531     {
532       stock_hash = g_hash_table_new (g_str_hash, g_str_equal);
533
534       gtk_stock_add_static (builtin_items, G_N_ELEMENTS (builtin_items));
535     }
536
537   if (translate_hash == NULL)
538     {
539       translate_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
540                                               g_free, NULL);
541
542       gtk_stock_set_translate_func (GETTEXT_PACKAGE, 
543                                     sgettext_swapped,
544                                     "Stock label",
545                                     NULL);
546       gtk_stock_set_translate_func (GETTEXT_PACKAGE "-navigation", 
547                                     sgettext_swapped,
548                                     "Stock label, navigation",
549                                     NULL);
550       gtk_stock_set_translate_func (GETTEXT_PACKAGE "-media", 
551                                     sgettext_swapped,
552                                     "Stock label, media",
553                                     NULL);
554     }
555 }
556
557 #define __GTK_STOCK_C__
558 #include "gtkaliasdef.c"