]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkstock.c
filechooserbutton: Duh, remove all the timeouts after tests
[~andy/gtk] / gtk / gtkstock.c
index dd3c8be802da26fec12d5d383379cc099d1aa123..7f5155fea0a3e95986de88911231e0132cf3c263 100644 (file)
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
  */
 
 #include "config.h"
+
 #include <string.h>
 
+#include "gtkprivate.h"
 #include "gtkstock.h"
 #include "gtkiconfactory.h"
 #include "gtkintl.h"
-#include <gdk/gdkkeysyms.h>
-#include "gtkalias.h"
+
+/**
+ * SECTION:gtkstock
+ * @Short_description:
+Prebuilt common menu/toolbar items and corresponding icons
+ * @Title: Stock Items
+ *
+ * Stock items represent commonly-used menu or toolbar items such as
+ * "Open" or "Exit". Each stock item is identified by a stock ID;
+ * stock IDs are just strings, but macros such as #GTK_STOCK_OPEN are
+ * provided to avoid typing mistakes in the strings.
+ * Applications can register their own stock items in addition to those
+ * built-in to GTK+.
+ *
+ * Each stock ID can be associated with a #GtkStockItem, which contains
+ * the user-visible label, keyboard accelerator, and translation domain
+ * of the menu or toolbar item; and/or with an icon stored in a
+ * #GtkIconFactory. See <link
+ * linkend="gtk3-Themeable-Stock-Images">GtkIconFactory</link> for
+ * more information on stock icons. The connection between a
+ * #GtkStockItem and stock icons is purely conventional (by virtue of
+ * using the same stock ID); it's possible to register a stock item but
+ * no icon, and vice versa. Stock icons may have a RTL variant which gets
+ * used for right-to-left locales.
+ */
 
 static GHashTable *translate_hash = NULL;
 static GHashTable *stock_hash = NULL;
@@ -42,6 +65,9 @@ static void init_stock_hash (void);
  */
 #define NON_STATIC_MASK (1 << 29)
 
+/* Magic value which is automatically replaced by the primary accel modifier */
+#define PRIMARY_MODIFIER 0xffffffff
+
 typedef struct _GtkStockTranslateFunc GtkStockTranslateFunc;
 struct _GtkStockTranslateFunc
 {
@@ -53,7 +79,8 @@ struct _GtkStockTranslateFunc
 static void
 real_add (const GtkStockItem *items,
           guint               n_items,
-          gboolean            copy)
+          gboolean            copy,
+          gboolean            replace_primary)
 {
   int i;
 
@@ -68,17 +95,26 @@ real_add (const GtkStockItem *items,
       gpointer old_key, old_value;
       const GtkStockItem *item = &items[i];
 
-      if (item->modifier & NON_STATIC_MASK)
-       {
-         g_warning ("Bit 29 set in stock accelerator.\n");
-         copy = TRUE;
-       }
-
-      if (copy)
-       {
-         item = gtk_stock_item_copy (item);
-         ((GtkStockItem *)item)->modifier |= NON_STATIC_MASK;
-       }
+      if (replace_primary && item->modifier == PRIMARY_MODIFIER)
+        {
+          item = gtk_stock_item_copy (item);
+          ((GtkStockItem *)item)->modifier = (NON_STATIC_MASK |
+                                              _gtk_get_primary_accel_mod ());
+        }
+      else
+        {
+          if (item->modifier & NON_STATIC_MASK)
+            {
+              g_warning ("Bit 29 set in stock accelerator.\n");
+              copy = TRUE;
+            }
+
+          if (copy)
+            {
+              item = gtk_stock_item_copy (item);
+              ((GtkStockItem *)item)->modifier |= NON_STATIC_MASK;
+            }
+        }
 
       if (g_hash_table_lookup_extended (stock_hash, item->stock_id,
                                         &old_key, &old_value))
@@ -97,7 +133,7 @@ real_add (const GtkStockItem *items,
 
 /**
  * gtk_stock_add:
- * @items: a #GtkStockItem or array of items
+ * @items: (array length=n_items): a #GtkStockItem or array of items
  * @n_items: number of #GtkStockItem in @items
  *
  * Registers each of the stock items in @items. If an item already
@@ -114,12 +150,12 @@ gtk_stock_add (const GtkStockItem *items,
 {
   g_return_if_fail (items != NULL);
 
-  real_add (items, n_items, TRUE);
+  real_add (items, n_items, TRUE, FALSE);
 }
 
 /**
  * gtk_stock_add_static:
- * @items: a #GtkStockItem or array of #GtkStockItem
+ * @items: (array length=n_items): a #GtkStockItem or array of #GtkStockItem
  * @n_items: number of items
  *
  * Same as gtk_stock_add(), but doesn't copy @items, so
@@ -132,13 +168,13 @@ gtk_stock_add_static (const GtkStockItem *items,
 {
   g_return_if_fail (items != NULL);
 
-  real_add (items, n_items, FALSE);
+  real_add (items, n_items, FALSE, FALSE);
 }
 
 /**
  * gtk_stock_lookup:
  * @stock_id: a stock item name
- * @item: stock item to initialize with values
+ * @item: (out): stock item to initialize with values
  * 
  * Fills @item with the registered values for @stock_id, returning %TRUE
  * if @stock_id was known.
@@ -189,8 +225,8 @@ gtk_stock_lookup (const gchar  *stock_id,
  * Retrieves a list of all known stock IDs added to a #GtkIconFactory
  * or registered with gtk_stock_add(). The list must be freed with g_slist_free(),
  * and each string in the list must be freed with g_free().
- * 
- * Return value: a list of known stock IDs
+ *
+ * Return value: (element-type utf8) (transfer full): a list of known stock IDs
  **/
 GSList*
 gtk_stock_list_ids (void)
@@ -235,7 +271,7 @@ gtk_stock_list_ids (void)
 }
 
 /**
- * gtk_stock_item_copy:
+ * gtk_stock_item_copy: (skip)
  * @item: a #GtkStockItem
  * 
  * Copies a stock item, mostly useful for language bindings and not in applications.
@@ -298,41 +334,42 @@ static const GtkStockItem builtin_items [] =
   { GTK_STOCK_APPLY, NC_("Stock label", "_Apply"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_BOLD, NC_("Stock label", "_Bold"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_CANCEL, NC_("Stock label", "_Cancel"), 0, 0, GETTEXT_PACKAGE },
-  { GTK_STOCK_CDROM, NC_("Stock label", "_CD-Rom"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_CDROM, NC_("Stock label", "_CD-ROM"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_CLEAR, NC_("Stock label", "_Clear"), 0, 0, GETTEXT_PACKAGE },
-  { GTK_STOCK_CLOSE, NC_("Stock label", "_Close"), GDK_CONTROL_MASK, 'w', GETTEXT_PACKAGE },
+  { GTK_STOCK_CLOSE, NC_("Stock label", "_Close"), PRIMARY_MODIFIER, 'w', GETTEXT_PACKAGE },
   { GTK_STOCK_CONNECT, NC_("Stock label", "C_onnect"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_CONVERT, NC_("Stock label", "_Convert"), 0, 0, GETTEXT_PACKAGE },
-   { GTK_STOCK_COPY, NC_("Stock label", "_Copy"), GDK_CONTROL_MASK, 'c', GETTEXT_PACKAGE },
-  { GTK_STOCK_CUT, NC_("Stock label", "Cu_t"), GDK_CONTROL_MASK, 'x', GETTEXT_PACKAGE },
+   { GTK_STOCK_COPY, NC_("Stock label", "_Copy"), PRIMARY_MODIFIER, 'c', GETTEXT_PACKAGE },
+  { GTK_STOCK_CUT, NC_("Stock label", "Cu_t"), PRIMARY_MODIFIER, 'x', GETTEXT_PACKAGE },
   { GTK_STOCK_DELETE, NC_("Stock label", "_Delete"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_DISCARD, NC_("Stock label", "_Discard"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_DISCONNECT, NC_("Stock label", "_Disconnect"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_EXECUTE, NC_("Stock label", "_Execute"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_EDIT, NC_("Stock label", "_Edit"), 0, 0, GETTEXT_PACKAGE },
-  { GTK_STOCK_FIND, NC_("Stock label", "_Find"), GDK_CONTROL_MASK, 'f', GETTEXT_PACKAGE },
-  { GTK_STOCK_FIND_AND_REPLACE, NC_("Stock label", "Find and _Replace"), GDK_CONTROL_MASK, 'r', GETTEXT_PACKAGE },
+  { GTK_STOCK_FILE, NC_("Stock label", "_File"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_FIND, NC_("Stock label", "_Find"), PRIMARY_MODIFIER, 'f', GETTEXT_PACKAGE },
+  { GTK_STOCK_FIND_AND_REPLACE, NC_("Stock label", "Find and _Replace"), PRIMARY_MODIFIER, 'r', GETTEXT_PACKAGE },
   { GTK_STOCK_FLOPPY, NC_("Stock label", "_Floppy"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_FULLSCREEN, NC_("Stock label", "_Fullscreen"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_LEAVE_FULLSCREEN, NC_("Stock label", "_Leave Fullscreen"), 0, 0, GETTEXT_PACKAGE },
   /* This is a navigation label as in "go to the bottom of the page" */
-  { GTK_STOCK_GOTO_BOTTOM, NC_("Stock label", "_Bottom"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_GOTO_BOTTOM, NC_("Stock label, navigation", "_Bottom"), 0, 0, GETTEXT_PACKAGE "-navigation" },
   /* This is a navigation label as in "go to the first page" */
-  { GTK_STOCK_GOTO_FIRST, NC_("Stock label", "_First"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_GOTO_FIRST, NC_("Stock label, navigation", "_First"), 0, 0, GETTEXT_PACKAGE "-navigation" },
   /* This is a navigation label as in "go to the last page" */
-  { GTK_STOCK_GOTO_LAST, NC_("Stock label", "_Last"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_GOTO_LAST, NC_("Stock label, navigation", "_Last"), 0, 0, GETTEXT_PACKAGE "-navigation" },
   /* This is a navigation label as in "go to the top of the page" */
-  { GTK_STOCK_GOTO_TOP, NC_("Stock label", "_Top"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_GOTO_TOP, NC_("Stock label, navigation", "_Top"), 0, 0, GETTEXT_PACKAGE "-navigation" },
   /* This is a navigation label as in "go back" */
-  { GTK_STOCK_GO_BACK, NC_("Stock label", "_Back"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_GO_BACK, NC_("Stock label, navigation", "_Back"), 0, 0, GETTEXT_PACKAGE "-navigation" },
   /* This is a navigation label as in "go down" */
-  { GTK_STOCK_GO_DOWN, NC_("Stock label", "_Down"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_GO_DOWN, NC_("Stock label, navigation", "_Down"), 0, 0, GETTEXT_PACKAGE "-navigation" },
   /* This is a navigation label as in "go forward" */
-  { GTK_STOCK_GO_FORWARD, NC_("Stock label", "_Forward"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_GO_FORWARD, NC_("Stock label, navigation", "_Forward"), 0, 0, GETTEXT_PACKAGE "-navigation" },
   /* This is a navigation label as in "go up" */
-  { GTK_STOCK_GO_UP, NC_("Stock label", "_Up"), 0, 0, GETTEXT_PACKAGE },
-  { GTK_STOCK_HARDDISK, NC_("Stock label", "_Harddisk"), 0, 0, GETTEXT_PACKAGE },
-  { GTK_STOCK_HELP, NC_("Stock label", "_Help"), GDK_CONTROL_MASK, 'h', GETTEXT_PACKAGE },
+  { GTK_STOCK_GO_UP, NC_("Stock label, navigation", "_Up"), 0, 0, GETTEXT_PACKAGE "-navigation" },
+  { GTK_STOCK_HARDDISK, NC_("Stock label", "_Hard Disk"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_HELP, NC_("Stock label", "_Help"), 0, GDK_KEY_F1, GETTEXT_PACKAGE },
   { GTK_STOCK_HOME, NC_("Stock label", "_Home"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_INDENT, NC_("Stock label", "Increase Indent"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_UNINDENT, NC_("Stock label", "Decrease Indent"), 0, 0, GETTEXT_PACKAGE },
@@ -350,26 +387,26 @@ static const GtkStockItem builtin_items [] =
   { GTK_STOCK_JUSTIFY_RIGHT, NC_("Stock label", "_Right"), 0, 0, GETTEXT_PACKAGE },
 
   /* Media label, as in "fast forward" */
-  { GTK_STOCK_MEDIA_FORWARD, NC_("Stock label", "_Forward"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_MEDIA_FORWARD, NC_("Stock label, media", "_Forward"), 0, 0, GETTEXT_PACKAGE "-media" },
   /* Media label, as in "next song" */
-  { GTK_STOCK_MEDIA_NEXT, NC_("Stock label", "_Next"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_MEDIA_NEXT, NC_("Stock label, media", "_Next"), 0, 0, GETTEXT_PACKAGE "-media" },
   /* Media label, as in "pause music" */
-  { GTK_STOCK_MEDIA_PAUSE, NC_("Stock label", "P_ause"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_MEDIA_PAUSE, NC_("Stock label, media", "P_ause"), 0, 0, GETTEXT_PACKAGE "-media" },
   /* Media label, as in "play music" */
-  { GTK_STOCK_MEDIA_PLAY, NC_("Stock label", "_Play"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_MEDIA_PLAY, NC_("Stock label, media", "_Play"), 0, 0, GETTEXT_PACKAGE "-media" },
   /* Media label, as in  "previous song" */
-  { GTK_STOCK_MEDIA_PREVIOUS, NC_("Stock label", "Pre_vious"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_MEDIA_PREVIOUS, NC_("Stock label, media", "Pre_vious"), 0, 0, GETTEXT_PACKAGE "-media" },
   /* Media label */
-  { GTK_STOCK_MEDIA_RECORD, NC_("Stock label", "_Record"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_MEDIA_RECORD, NC_("Stock label, media", "_Record"), 0, 0, GETTEXT_PACKAGE "-media" },
   /* Media label */
-  { GTK_STOCK_MEDIA_REWIND, NC_("Stock label", "R_ewind"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_MEDIA_REWIND, NC_("Stock label, media", "R_ewind"), 0, 0, GETTEXT_PACKAGE "-media" },
   /* Media label */
-  { GTK_STOCK_MEDIA_STOP, NC_("Stock label", "_Stop"), 0, 0, GETTEXT_PACKAGE },
+  { GTK_STOCK_MEDIA_STOP, NC_("Stock label, media", "_Stop"), 0, 0, GETTEXT_PACKAGE "-media" },
   { GTK_STOCK_NETWORK, NC_("Stock label", "_Network"), 0, 0, GETTEXT_PACKAGE },
-  { GTK_STOCK_NEW, NC_("Stock label", "_New"), GDK_CONTROL_MASK, 'n', GETTEXT_PACKAGE },
+  { GTK_STOCK_NEW, NC_("Stock label", "_New"), PRIMARY_MODIFIER, 'n', GETTEXT_PACKAGE },
   { GTK_STOCK_NO, NC_("Stock label", "_No"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_OK, NC_("Stock label", "_OK"), 0, 0, GETTEXT_PACKAGE },
-  { GTK_STOCK_OPEN, NC_("Stock label", "_Open"), GDK_CONTROL_MASK, 'o', GETTEXT_PACKAGE },
+  { GTK_STOCK_OPEN, NC_("Stock label", "_Open"), PRIMARY_MODIFIER, 'o', GETTEXT_PACKAGE },
   /* Page orientation */
   { GTK_STOCK_ORIENTATION_LANDSCAPE, NC_("Stock label", "Landscape"), 0, 0, GETTEXT_PACKAGE },
   /* Page orientation */
@@ -379,17 +416,17 @@ static const GtkStockItem builtin_items [] =
   /* Page orientation */
   { GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT, NC_("Stock label", "Reverse portrait"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_PAGE_SETUP, NC_("Stock label", "Page Set_up"), 0, 0, GETTEXT_PACKAGE },
-  { GTK_STOCK_PASTE, NC_("Stock label", "_Paste"), GDK_CONTROL_MASK, 'v', GETTEXT_PACKAGE },
+  { GTK_STOCK_PASTE, NC_("Stock label", "_Paste"), PRIMARY_MODIFIER, 'v', GETTEXT_PACKAGE },
   { GTK_STOCK_PREFERENCES, NC_("Stock label", "_Preferences"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_PRINT, NC_("Stock label", "_Print"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_PRINT_PREVIEW, NC_("Stock label", "Print Pre_view"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_PROPERTIES, NC_("Stock label", "_Properties"), 0, 0, GETTEXT_PACKAGE },
-  { GTK_STOCK_QUIT, NC_("Stock label", "_Quit"), GDK_CONTROL_MASK, 'q', GETTEXT_PACKAGE },
+  { GTK_STOCK_QUIT, NC_("Stock label", "_Quit"), PRIMARY_MODIFIER, 'q', GETTEXT_PACKAGE },
   { GTK_STOCK_REDO, NC_("Stock label", "_Redo"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_REFRESH, NC_("Stock label", "_Refresh"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_REMOVE, NC_("Stock label", "_Remove"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_REVERT_TO_SAVED, NC_("Stock label", "_Revert"), 0, 0, GETTEXT_PACKAGE },
-  { GTK_STOCK_SAVE, NC_("Stock label", "_Save"), GDK_CONTROL_MASK, 's', GETTEXT_PACKAGE },
+  { GTK_STOCK_SAVE, NC_("Stock label", "_Save"), PRIMARY_MODIFIER, 's', GETTEXT_PACKAGE },
   { GTK_STOCK_SAVE_AS, NC_("Stock label", "Save _As"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_SELECT_ALL, NC_("Stock label", "Select _All"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_SELECT_COLOR, NC_("Stock label", "_Color"), 0, 0, GETTEXT_PACKAGE },
@@ -425,12 +462,40 @@ static const GtkStockItem builtin_items [] =
  *
  * Sets a function to be used for translating the @label of 
  * a stock item.
- *
+ * 
  * If no function is registered for a translation domain,
  * g_dgettext() is used.
+ * 
+ * The function is used for all stock items whose
+ * @translation_domain matches @domain. Note that it is possible
+ * to use strings different from the actual gettext translation domain
+ * of your application for this, as long as your #GtkTranslateFunc uses
+ * the correct domain when calling dgettext(). This can be useful, e.g.
+ * when dealing with message contexts:
  *
- * Since: 2.8
+ * |[
+ * GtkStockItem items[] = { 
+ *  { MY_ITEM1, NC_("odd items", "Item 1"), 0, 0, "odd-item-domain" },
+ *  { MY_ITEM2, NC_("even items", "Item 2"), 0, 0, "even-item-domain" },
+ * };
+ *
+ * gchar *
+ * my_translate_func (const gchar *msgid,
+ *                    gpointer     data)
+ * {
+ *   gchar *msgctxt = data;
+ * 
+ *   return (gchar*)g_dpgettext2 (GETTEXT_PACKAGE, msgctxt, msgid);
+ * }
+ *
+ * /&ast; ... &ast;/
+ *
+ * gtk_stock_add (items, G_N_ELEMENTS (items));
+ * gtk_stock_set_translate_func ("odd-item-domain", my_translate_func, "odd items"); 
+ * gtk_stock_set_translate_func ("even-item-domain", my_translate_func, "even items"); 
+ * ]|
  * 
+ * Since: 2.8
  */
 void
 gtk_stock_set_translate_func (const gchar      *domain,
@@ -465,9 +530,10 @@ static gchar *
 sgettext_swapped (const gchar *msgid, 
                  gpointer     data)
 {
-  return (gchar *)g_dpgettext2 ((const char *)data, "Stock label", msgid);
-}
+  gchar *msgctxt = data;
 
+  return (gchar *)g_dpgettext2 (GETTEXT_PACKAGE, msgctxt, msgid);
+}
 
 static void
 init_stock_hash (void)
@@ -476,7 +542,7 @@ init_stock_hash (void)
     {
       stock_hash = g_hash_table_new (g_str_hash, g_str_equal);
 
-      gtk_stock_add_static (builtin_items, G_N_ELEMENTS (builtin_items));
+      real_add (builtin_items, G_N_ELEMENTS (builtin_items), FALSE, TRUE);
     }
 
   if (translate_hash == NULL)
@@ -486,10 +552,15 @@ init_stock_hash (void)
 
       gtk_stock_set_translate_func (GETTEXT_PACKAGE, 
                                    sgettext_swapped,
-                                   GETTEXT_PACKAGE,
+                                   "Stock label",
+                                   NULL);
+      gtk_stock_set_translate_func (GETTEXT_PACKAGE "-navigation", 
+                                   sgettext_swapped,
+                                   "Stock label, navigation",
+                                   NULL);
+      gtk_stock_set_translate_func (GETTEXT_PACKAGE "-media", 
+                                   sgettext_swapped,
+                                   "Stock label, media",
                                    NULL);
     }
 }
-
-#define __GTK_STOCK_C__
-#include "gtkaliasdef.c"