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/.
31 #include "gtkiconfactory.h"
33 #include <gdk/gdkkeysyms.h>
36 static GHashTable *translate_hash = NULL;
37 static GHashTable *stock_hash = NULL;
38 static void init_stock_hash (void);
40 /* We use an unused modifier bit to mark stock items which
41 * must be freed when they are removed from the hash table.
43 #define NON_STATIC_MASK (1 << 29)
45 typedef struct _GtkStockTranslateFunc GtkStockTranslateFunc;
46 struct _GtkStockTranslateFunc
48 GtkTranslateFunc func;
50 GtkDestroyNotify notify;
54 real_add (const GtkStockItem *items,
68 gpointer old_key, old_value;
69 const GtkStockItem *item = &items[i];
71 if (item->modifier & NON_STATIC_MASK)
73 g_warning ("Bit 29 set in stock accelerator.\n");
79 item = gtk_stock_item_copy (item);
80 ((GtkStockItem *)item)->modifier |= NON_STATIC_MASK;
83 if (g_hash_table_lookup_extended (stock_hash, item->stock_id,
84 &old_key, &old_value))
86 g_hash_table_remove (stock_hash, old_key);
87 if (((GtkStockItem *)old_value)->modifier & NON_STATIC_MASK)
88 gtk_stock_item_free (old_value);
91 g_hash_table_insert (stock_hash,
92 (gchar*)item->stock_id, (GtkStockItem*)item);
100 * @items: a #GtkStockItem or array of items
101 * @n_items: number of #GtkStockItem in @items
103 * Registers each of the stock items in @items. If an item already
104 * exists with the same stock ID as one of the @items, the old item
105 * gets replaced. The stock items are copied, so GTK+ does not hold
106 * any pointer into @items and @items can be freed. Use
107 * gtk_stock_add_static() if @items is persistent and GTK+ need not
112 gtk_stock_add (const GtkStockItem *items,
115 g_return_if_fail (items != NULL);
117 real_add (items, n_items, TRUE);
121 * gtk_stock_add_static:
122 * @items: a #GtkStockItem or array of #GtkStockItem
123 * @n_items: number of items
125 * Same as gtk_stock_add(), but doesn't copy @items, so
126 * @items must persist until application exit.
130 gtk_stock_add_static (const GtkStockItem *items,
133 g_return_if_fail (items != NULL);
135 real_add (items, n_items, FALSE);
140 * @stock_id: a stock item name
141 * @item: stock item to initialize with values
143 * Fills @item with the registered values for @stock_id, returning %TRUE
144 * if @stock_id was known.
147 * Return value: %TRUE if @item was initialized
150 gtk_stock_lookup (const gchar *stock_id,
153 const GtkStockItem *found;
155 g_return_val_if_fail (stock_id != NULL, FALSE);
156 g_return_val_if_fail (item != NULL, FALSE);
160 found = g_hash_table_lookup (stock_hash, stock_id);
165 item->modifier &= ~NON_STATIC_MASK;
168 GtkStockTranslateFunc *translate;
170 if (item->translation_domain)
171 translate = (GtkStockTranslateFunc *)
172 g_hash_table_lookup (translate_hash, item->translation_domain);
176 if (translate != NULL && translate->func != NULL)
177 item->label = (* translate->func) (item->label, translate->data);
179 item->label = dgettext (item->translation_domain, item->label);
183 return found != NULL;
187 listify_foreach (gpointer key, gpointer value, gpointer data)
189 GSList **list = data;
191 *list = g_slist_prepend (*list, key);
195 g_hash_table_get_keys (GHashTable *table)
199 g_hash_table_foreach (table, listify_foreach, &list);
205 * gtk_stock_list_ids:
207 * Retrieves a list of all known stock IDs added to a #GtkIconFactory
208 * or registered with gtk_stock_add(). The list must be freed with g_slist_free(),
209 * and each string in the list must be freed with g_free().
211 * Return value: a list of known stock IDs
214 gtk_stock_list_ids (void)
220 const gchar *last_id;
224 ids = g_hash_table_get_keys (stock_hash);
225 icon_ids = _gtk_icon_factory_list_ids ();
226 ids = g_slist_concat (ids, icon_ids);
228 ids = g_slist_sort (ids, (GCompareFunc)strcmp);
233 while (tmp_list != NULL)
237 next = g_slist_next (tmp_list);
239 if (last_id && strcmp (tmp_list->data, last_id) == 0)
241 /* duplicate, ignore */
245 retval = g_slist_prepend (retval, g_strdup (tmp_list->data));
246 last_id = tmp_list->data;
249 g_slist_free_1 (tmp_list);
258 * gtk_stock_item_copy:
259 * @item: a #GtkStockItem
261 * Copies a stock item, mostly useful for language bindings and not in applications.
263 * Return value: a new #GtkStockItem
266 gtk_stock_item_copy (const GtkStockItem *item)
270 g_return_val_if_fail (item != NULL, NULL);
272 copy = g_new (GtkStockItem, 1);
276 copy->stock_id = g_strdup (item->stock_id);
277 copy->label = g_strdup (item->label);
278 copy->translation_domain = g_strdup (item->translation_domain);
284 * gtk_stock_item_free:
285 * @item: a #GtkStockItem
287 * Frees a stock item allocated on the heap, such as one returned by
288 * gtk_stock_item_copy(). Also frees the fields inside the stock item,
289 * if they are not %NULL.
293 gtk_stock_item_free (GtkStockItem *item)
295 g_return_if_fail (item != NULL);
297 g_free ((gchar*)item->stock_id);
298 g_free ((gchar*)item->label);
299 g_free ((gchar*)item->translation_domain);
304 static const GtkStockItem builtin_items [] =
306 /* KEEP IN SYNC with gtkiconfactory.c stock icons, when appropriate */
308 { GTK_STOCK_DIALOG_INFO, N_("Information"), 0, 0, GETTEXT_PACKAGE },
309 { GTK_STOCK_DIALOG_WARNING, N_("Warning"), 0, 0, GETTEXT_PACKAGE },
310 { GTK_STOCK_DIALOG_ERROR, N_("Error"), 0, 0, GETTEXT_PACKAGE },
311 { GTK_STOCK_DIALOG_QUESTION, N_("Question"), 0, 0, GETTEXT_PACKAGE },
313 /* FIXME these need accelerators when appropriate, and
314 * need the mnemonics to be rationalized
316 { GTK_STOCK_ABOUT, N_("_About"), 0, 0, GETTEXT_PACKAGE },
317 { GTK_STOCK_ADD, N_("_Add"), 0, 0, GETTEXT_PACKAGE },
318 { GTK_STOCK_APPLY, N_("_Apply"), 0, 0, GETTEXT_PACKAGE },
319 { GTK_STOCK_BOLD, N_("_Bold"), 0, 0, GETTEXT_PACKAGE },
320 { GTK_STOCK_CANCEL, N_("_Cancel"), 0, 0, GETTEXT_PACKAGE },
321 { GTK_STOCK_CDROM, N_("_CD-Rom"), 0, 0, GETTEXT_PACKAGE },
322 { GTK_STOCK_CLEAR, N_("_Clear"), 0, 0, GETTEXT_PACKAGE },
323 { GTK_STOCK_CLOSE, N_("_Close"), GDK_CONTROL_MASK, 'w', GETTEXT_PACKAGE },
324 { GTK_STOCK_CONVERT, N_("_Convert"), 0, 0, GETTEXT_PACKAGE },
325 { GTK_STOCK_COPY, N_("_Copy"), GDK_CONTROL_MASK, 'c', GETTEXT_PACKAGE },
326 { GTK_STOCK_CUT, N_("Cu_t"), GDK_CONTROL_MASK, 'x', GETTEXT_PACKAGE },
327 { GTK_STOCK_DELETE, N_("_Delete"), 0, 0, GETTEXT_PACKAGE },
328 { GTK_STOCK_EXECUTE, N_("_Execute"), 0, 0, GETTEXT_PACKAGE },
329 { GTK_STOCK_EDIT, N_("_Edit"), 0, 0, GETTEXT_PACKAGE },
330 { GTK_STOCK_FIND, N_("_Find"), GDK_CONTROL_MASK, 'f', GETTEXT_PACKAGE },
331 { GTK_STOCK_FIND_AND_REPLACE, N_("Find and _Replace"), GDK_CONTROL_MASK, 'r', GETTEXT_PACKAGE },
332 { GTK_STOCK_FLOPPY, N_("_Floppy"), 0, 0, GETTEXT_PACKAGE },
333 /* translators, strip the prefix up to and including the first | */
334 { GTK_STOCK_GOTO_BOTTOM, N_("Navigation|_Bottom"), 0, 0, GETTEXT_PACKAGE },
335 /* translators, strip the prefix up to and including the first | */
336 { GTK_STOCK_GOTO_FIRST, N_("Navigation|_First"), 0, 0, GETTEXT_PACKAGE },
337 /* translators, strip the prefix up to and including the first | */
338 { GTK_STOCK_GOTO_LAST, N_("Navigation|_Last"), 0, 0, GETTEXT_PACKAGE },
339 /* translators, strip the prefix up to and including the first | */
340 { GTK_STOCK_GOTO_TOP, N_("Navigation|_Top"), 0, 0, GETTEXT_PACKAGE },
341 /* translators, strip the prefix up to and including the first | */
342 { GTK_STOCK_GO_BACK, N_("Navigation|_Back"), 0, 0, GETTEXT_PACKAGE },
343 /* translators, strip the prefix up to and including the first | */
344 { GTK_STOCK_GO_DOWN, N_("Navigation|_Down"), 0, 0, GETTEXT_PACKAGE },
345 /* translators, strip the prefix up to and including the first | */
346 { GTK_STOCK_GO_FORWARD, N_("Navigation|_Forward"), 0, 0, GETTEXT_PACKAGE },
347 /* translators, strip the prefix up to and including the first | */
348 { GTK_STOCK_GO_UP, N_("Navigation|_Up"), 0, 0, GETTEXT_PACKAGE },
349 { GTK_STOCK_HARDDISK, N_("_Harddisk"), 0, 0, GETTEXT_PACKAGE },
350 { GTK_STOCK_HELP, N_("_Help"), GDK_CONTROL_MASK, 'h', GETTEXT_PACKAGE },
351 { GTK_STOCK_HOME, N_("_Home"), 0, 0, GETTEXT_PACKAGE },
352 { GTK_STOCK_INDENT, N_("Increase Indent"), 0, 0, GETTEXT_PACKAGE },
353 { GTK_STOCK_UNINDENT, N_("Decrease Indent"), 0, 0, GETTEXT_PACKAGE },
354 { GTK_STOCK_INDEX, N_("_Index"), 0, 0, GETTEXT_PACKAGE },
355 { GTK_STOCK_ITALIC, N_("_Italic"), 0, 0, GETTEXT_PACKAGE },
356 { GTK_STOCK_JUMP_TO, N_("_Jump to"), 0, 0, GETTEXT_PACKAGE },
357 /* translators, strip the prefix up to and including the first | */
358 { GTK_STOCK_JUSTIFY_CENTER, N_("Justify|_Center"), 0, 0, GETTEXT_PACKAGE },
359 /* translators, strip the prefix up to and including the first | */
360 { GTK_STOCK_JUSTIFY_FILL, N_("Justify|_Fill"), 0, 0, GETTEXT_PACKAGE },
361 /* translators, strip the prefix up to and including the first | */
362 { GTK_STOCK_JUSTIFY_LEFT, N_("Justify|_Left"), 0, 0, GETTEXT_PACKAGE },
363 /* translators, strip the prefix up to and including the first | */
364 { GTK_STOCK_JUSTIFY_RIGHT, N_("Justify|_Right"), 0, 0, GETTEXT_PACKAGE },
366 /* translators, strip the prefix up to and including the first | */
367 { GTK_STOCK_MEDIA_FORWARD, N_("Media|_Forward"), 0, 0, GETTEXT_PACKAGE },
368 /* translators, strip the prefix up to and including the first | */
369 { GTK_STOCK_MEDIA_NEXT, N_("Media|_Next"), 0, 0, GETTEXT_PACKAGE },
370 /* translators, strip the prefix up to and including the first | */
371 { GTK_STOCK_MEDIA_PAUSE, N_("Media|P_ause"), 0, 0, GETTEXT_PACKAGE },
372 /* translators, strip the prefix up to and including the first | */
373 { GTK_STOCK_MEDIA_PLAY, N_("Media|_Play"), 0, 0, GETTEXT_PACKAGE },
374 /* translators, strip the prefix up to and including the first | */
375 { GTK_STOCK_MEDIA_PREVIOUS, N_("Media|Pre_vious"), 0, 0, GETTEXT_PACKAGE },
376 /* translators, strip the prefix up to and including the first | */
377 { GTK_STOCK_MEDIA_RECORD, N_("Media|_Record"), 0, 0, GETTEXT_PACKAGE },
378 /* translators, strip the prefix up to and including the first | */
379 { GTK_STOCK_MEDIA_REWIND, N_("Media|R_ewind"), 0, 0, GETTEXT_PACKAGE },
380 /* translators, strip the prefix up to and including the first | */
381 { GTK_STOCK_MEDIA_STOP, N_("Media|_Stop"), 0, 0, GETTEXT_PACKAGE },
382 { GTK_STOCK_NETWORK, N_("_Network"), 0, 0, GETTEXT_PACKAGE },
383 { GTK_STOCK_NEW, N_("_New"), GDK_CONTROL_MASK, 'n', GETTEXT_PACKAGE },
384 { GTK_STOCK_NO, N_("_No"), 0, 0, GETTEXT_PACKAGE },
385 { GTK_STOCK_OK, N_("_OK"), 0, 0, GETTEXT_PACKAGE },
386 { GTK_STOCK_OPEN, N_("_Open"), GDK_CONTROL_MASK, 'o', GETTEXT_PACKAGE },
387 { GTK_STOCK_PASTE, N_("_Paste"), GDK_CONTROL_MASK, 'v', GETTEXT_PACKAGE },
388 { GTK_STOCK_PREFERENCES, N_("_Preferences"), 0, 0, GETTEXT_PACKAGE },
389 { GTK_STOCK_PRINT, N_("_Print"), 0, 0, GETTEXT_PACKAGE },
390 { GTK_STOCK_PRINT_PREVIEW, N_("Print Pre_view"), 0, 0, GETTEXT_PACKAGE },
391 { GTK_STOCK_PROPERTIES, N_("_Properties"), 0, 0, GETTEXT_PACKAGE },
392 { GTK_STOCK_QUIT, N_("_Quit"), GDK_CONTROL_MASK, 'q', GETTEXT_PACKAGE },
393 { GTK_STOCK_REDO, N_("_Redo"), 0, 0, GETTEXT_PACKAGE },
394 { GTK_STOCK_REFRESH, N_("_Refresh"), 0, 0, GETTEXT_PACKAGE },
395 { GTK_STOCK_REMOVE, N_("_Remove"), 0, 0, GETTEXT_PACKAGE },
396 { GTK_STOCK_REVERT_TO_SAVED, N_("_Revert"), 0, 0, GETTEXT_PACKAGE },
397 { GTK_STOCK_SAVE, N_("_Save"), GDK_CONTROL_MASK, 's', GETTEXT_PACKAGE },
398 { GTK_STOCK_SAVE_AS, N_("Save _As"), 0, 0, GETTEXT_PACKAGE },
399 { GTK_STOCK_SELECT_COLOR, N_("_Color"), 0, 0, GETTEXT_PACKAGE },
400 { GTK_STOCK_SELECT_FONT, N_("_Font"), 0, 0, GETTEXT_PACKAGE },
401 { GTK_STOCK_SORT_ASCENDING, N_("_Ascending"), 0, 0, GETTEXT_PACKAGE },
402 { GTK_STOCK_SORT_DESCENDING, N_("_Descending"), 0, 0, GETTEXT_PACKAGE },
403 { GTK_STOCK_SPELL_CHECK, N_("_Spell Check"), 0, 0, GETTEXT_PACKAGE },
404 { GTK_STOCK_STOP, N_("_Stop"), 0, 0, GETTEXT_PACKAGE },
405 { GTK_STOCK_STRIKETHROUGH, N_("_Strikethrough"), 0, 0, GETTEXT_PACKAGE },
406 { GTK_STOCK_UNDELETE, N_("_Undelete"), 0, 0, GETTEXT_PACKAGE },
407 { GTK_STOCK_UNDERLINE, N_("_Underline"), 0, 0, GETTEXT_PACKAGE },
408 { GTK_STOCK_UNDO, N_("_Undo"), 0, 0, GETTEXT_PACKAGE },
409 { GTK_STOCK_YES, N_("_Yes"), 0, 0, GETTEXT_PACKAGE },
410 { GTK_STOCK_ZOOM_100, N_("_Normal Size"), 0, 0, GETTEXT_PACKAGE },
411 { GTK_STOCK_ZOOM_FIT, N_("Best _Fit"), 0, 0, GETTEXT_PACKAGE },
412 { GTK_STOCK_ZOOM_IN, N_("Zoom _In"), 0, 0, GETTEXT_PACKAGE },
413 { GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), 0, 0, GETTEXT_PACKAGE }
417 * gtk_stock_set_translate_func:
418 * @func: a #GtkTranslateFunc
419 * @data: data to pass to @func
420 * @notify: a #GtkDestroyNotify that is called when @data is
423 * Sets a function to be used for translating the @label of
426 * If no function is registered for a translation domain,
427 * dgettext() is used.
433 gtk_stock_set_translate_func (const gchar *domain,
434 GtkTranslateFunc func,
436 GtkDestroyNotify notify)
438 GtkStockTranslateFunc *translate;
441 domainname = g_strdup (domain);
443 translate = (GtkStockTranslateFunc *)
444 g_hash_table_lookup (translate_hash, domainname);
448 if (translate->notify)
449 (* translate->notify) (translate->data);
452 translate = g_new0 (GtkStockTranslateFunc, 1);
454 translate->func = func;
455 translate->data = data;
456 translate->notify = notify;
458 g_hash_table_insert (translate_hash, domainname, translate);
462 sgettext_swapped (const gchar *msgid,
465 gchar *domainname = data;
467 return (gchar *)g_strip_context (msgid, dgettext (domainname, msgid));
472 init_stock_hash (void)
474 if (stock_hash == NULL)
476 stock_hash = g_hash_table_new (g_str_hash, g_str_equal);
478 gtk_stock_add_static (builtin_items, G_N_ELEMENTS (builtin_items));
481 if (translate_hash == NULL)
483 translate_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
486 gtk_stock_set_translate_func (GETTEXT_PACKAGE,
493 #define __GTK_STOCK_C__
494 #include "gtkaliasdef.c"