2 * GTK - The GIMP Toolkit
3 * Copyright (C) 1998, 1999 Red Hat, Inc.
6 * This Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 * Author: James Henstridge <james@daa.com.au>
23 * Modified by the GTK+ Team and others 2003. See the AUTHORS
24 * file for a list of people on the GTK+ Team. See the ChangeLog
25 * files for a list of changes. These files are distributed with
26 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 * SECTION:gtkactiongroup
31 * @Short_description: A group of actions
32 * @Title: GtkActionGroup
34 * Actions are organised into groups. An action group is essentially a
35 * map from names to #GtkAction objects.
37 * All actions that would make sense to use in a particular context
38 * should be in a single group. Multiple action groups may be used for a
39 * particular user interface. In fact, it is expected that most nontrivial
40 * applications will make use of multiple groups. For example, in an
41 * application that can edit multiple documents, one group holding global
42 * actions (e.g. quit, about, new), and one group per document holding
43 * actions that act on that document (eg. save, cut/copy/paste, etc). Each
44 * window's menus would be constructed from a combination of two action
47 * <para id="Action-Accel">
48 * Accelerators are handled by the GTK+ accelerator map. All actions are
49 * assigned an accelerator path (which normally has the form
50 * <literal><Actions>/group-name/action-name</literal>)
51 * and a shortcut is associated with this accelerator path. All menuitems
52 * and toolitems take on this accelerator path. The GTK+ accelerator map
53 * code makes sure that the correct shortcut is displayed next to the menu
56 * <refsect2 id="GtkActionGroup-BUILDER-UI">
57 * <title>GtkActionGroup as GtkBuildable</title>
59 * The #GtkActionGroup implementation of the #GtkBuildable interface accepts
60 * #GtkAction objects as <child> elements in UI definitions.
62 * Note that it is probably more common to define actions and action groups
63 * in the code, since they are directly related to what the code can do.
65 * The GtkActionGroup implementation of the GtkBuildable interface supports
66 * a custom <accelerator> element, which has attributes named key and
67 * modifiers and allows to specify accelerators. This is similar to the
68 * <accelerator> element of #GtkWidget, the main difference is that
69 * it doesn't allow you to specify a signal.
72 * <title>A #GtkDialog UI definition fragment.</title>
73 * <programlisting><![CDATA[
74 * <object class="GtkActionGroup" id="actiongroup">
76 * <object class="GtkAction" id="About">
77 * <property name="name">About</property>
78 * <property name="stock_id">gtk-about</property>
79 * <signal handler="about_activate" name="activate"/>
81 * <accelerator key="F1" modifiers="GDK_CONTROL_MASK | GDK_SHIFT_MASK"/>
84 * ]]></programlisting>
92 #include "gtkactiongroup.h"
93 #include "gtkbuildable.h"
94 #include "gtkiconfactory.h"
95 #include "gtkicontheme.h"
97 #include "gtktoggleaction.h"
98 #include "gtkradioaction.h"
99 #include "gtkaccelmap.h"
100 #include "gtkmarshalers.h"
101 #include "gtkbuilderprivate.h"
102 #include "gtkprivate.h"
106 struct _GtkActionGroupPrivate
112 GtkAccelGroup *accel_group;
114 GtkTranslateFunc translate_func;
115 gpointer translate_data;
116 GDestroyNotify translate_notify;
137 static void gtk_action_group_init (GtkActionGroup *self);
138 static void gtk_action_group_class_init (GtkActionGroupClass *class);
139 static void gtk_action_group_finalize (GObject *object);
140 static void gtk_action_group_set_property (GObject *object,
144 static void gtk_action_group_get_property (GObject *object,
148 static GtkAction *gtk_action_group_real_get_action (GtkActionGroup *self,
152 static void gtk_action_group_buildable_init (GtkBuildableIface *iface);
153 static void gtk_action_group_buildable_add_child (GtkBuildable *buildable,
157 static void gtk_action_group_buildable_set_name (GtkBuildable *buildable,
159 static const gchar* gtk_action_group_buildable_get_name (GtkBuildable *buildable);
160 static gboolean gtk_action_group_buildable_custom_tag_start (GtkBuildable *buildable,
163 const gchar *tagname,
164 GMarkupParser *parser,
166 static void gtk_action_group_buildable_custom_tag_end (GtkBuildable *buildable,
169 const gchar *tagname,
170 gpointer *user_data);
173 gtk_action_group_get_type (void)
175 static GType type = 0;
179 const GTypeInfo type_info =
181 sizeof (GtkActionGroupClass),
182 NULL, /* base_init */
183 NULL, /* base_finalize */
184 (GClassInitFunc) gtk_action_group_class_init,
185 NULL, /* class_finalize */
186 NULL, /* class_data */
187 sizeof (GtkActionGroup),
189 (GInstanceInitFunc) gtk_action_group_init,
192 const GInterfaceInfo buildable_info =
194 (GInterfaceInitFunc) gtk_action_group_buildable_init,
199 type = g_type_register_static (G_TYPE_OBJECT, I_("GtkActionGroup"),
202 g_type_add_interface_static (type,
209 static GObjectClass *parent_class = NULL;
210 static guint action_group_signals[LAST_SIGNAL] = { 0 };
213 gtk_action_group_class_init (GtkActionGroupClass *klass)
215 GObjectClass *gobject_class;
217 gobject_class = G_OBJECT_CLASS (klass);
218 parent_class = g_type_class_peek_parent (klass);
220 gobject_class->finalize = gtk_action_group_finalize;
221 gobject_class->set_property = gtk_action_group_set_property;
222 gobject_class->get_property = gtk_action_group_get_property;
223 klass->get_action = gtk_action_group_real_get_action;
225 g_object_class_install_property (gobject_class,
227 g_param_spec_string ("name",
229 P_("A name for the action group."),
231 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
232 g_object_class_install_property (gobject_class,
234 g_param_spec_boolean ("sensitive",
236 P_("Whether the action group is enabled."),
238 GTK_PARAM_READWRITE));
239 g_object_class_install_property (gobject_class,
241 g_param_spec_boolean ("visible",
243 P_("Whether the action group is visible."),
245 GTK_PARAM_READWRITE));
246 g_object_class_install_property (gobject_class,
248 g_param_spec_object ("accel-group",
249 P_("Accelerator Group"),
250 P_("The accelerator group the actions of this group should use."),
251 GTK_TYPE_ACCEL_GROUP,
252 GTK_PARAM_READWRITE));
255 * GtkActionGroup::connect-proxy:
256 * @action_group: the group
257 * @action: the action
260 * The ::connect-proxy signal is emitted after connecting a proxy to
261 * an action in the group. Note that the proxy may have been connected
262 * to a different action before.
264 * This is intended for simple customizations for which a custom action
265 * class would be too clumsy, e.g. showing tooltips for menuitems in the
268 * #GtkUIManager proxies the signal and provides global notification
269 * just before any action is connected to a proxy, which is probably more
274 action_group_signals[CONNECT_PROXY] =
275 g_signal_new (I_("connect-proxy"),
276 G_OBJECT_CLASS_TYPE (klass),
278 _gtk_marshal_VOID__OBJECT_OBJECT,
280 GTK_TYPE_ACTION, GTK_TYPE_WIDGET);
283 * GtkActionGroup::disconnect-proxy:
284 * @action_group: the group
285 * @action: the action
288 * The ::disconnect-proxy signal is emitted after disconnecting a proxy
289 * from an action in the group.
291 * #GtkUIManager proxies the signal and provides global notification
292 * just before any action is connected to a proxy, which is probably more
297 action_group_signals[DISCONNECT_PROXY] =
298 g_signal_new (I_("disconnect-proxy"),
299 G_OBJECT_CLASS_TYPE (klass),
301 _gtk_marshal_VOID__OBJECT_OBJECT,
303 GTK_TYPE_ACTION, GTK_TYPE_WIDGET);
306 * GtkActionGroup::pre-activate:
307 * @action_group: the group
308 * @action: the action
310 * The ::pre-activate signal is emitted just before the @action in the
311 * @action_group is activated
313 * This is intended for #GtkUIManager to proxy the signal and provide global
314 * notification just before any action is activated.
318 action_group_signals[PRE_ACTIVATE] =
319 g_signal_new (I_("pre-activate"),
320 G_OBJECT_CLASS_TYPE (klass),
322 _gtk_marshal_VOID__OBJECT,
327 * GtkActionGroup::post-activate:
328 * @action_group: the group
329 * @action: the action
331 * The ::post-activate signal is emitted just after the @action in the
332 * @action_group is activated
334 * This is intended for #GtkUIManager to proxy the signal and provide global
335 * notification just after any action is activated.
339 action_group_signals[POST_ACTIVATE] =
340 g_signal_new (I_("post-activate"),
341 G_OBJECT_CLASS_TYPE (klass),
343 _gtk_marshal_VOID__OBJECT,
347 g_type_class_add_private (gobject_class, sizeof (GtkActionGroupPrivate));
352 remove_action (GtkAction *action)
354 g_object_set (action, I_("action-group"), NULL, NULL);
355 g_object_unref (action);
359 gtk_action_group_init (GtkActionGroup *action_group)
361 GtkActionGroupPrivate *private;
363 action_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (action_group,
364 GTK_TYPE_ACTION_GROUP,
365 GtkActionGroupPrivate);
366 private = action_group->priv;
368 private->name = NULL;
369 private->sensitive = TRUE;
370 private->visible = TRUE;
371 private->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
373 (GDestroyNotify) remove_action);
374 private->translate_func = NULL;
375 private->translate_data = NULL;
376 private->translate_notify = NULL;
380 gtk_action_group_buildable_init (GtkBuildableIface *iface)
382 iface->add_child = gtk_action_group_buildable_add_child;
383 iface->set_name = gtk_action_group_buildable_set_name;
384 iface->get_name = gtk_action_group_buildable_get_name;
385 iface->custom_tag_start = gtk_action_group_buildable_custom_tag_start;
386 iface->custom_tag_end = gtk_action_group_buildable_custom_tag_end;
390 gtk_action_group_buildable_add_child (GtkBuildable *buildable,
395 gtk_action_group_add_action_with_accel (GTK_ACTION_GROUP (buildable),
396 GTK_ACTION (child), NULL);
400 gtk_action_group_buildable_set_name (GtkBuildable *buildable,
403 GtkActionGroup *self = GTK_ACTION_GROUP (buildable);
404 GtkActionGroupPrivate *private = self->priv;
406 private->name = g_strdup (name);
410 gtk_action_group_buildable_get_name (GtkBuildable *buildable)
412 GtkActionGroup *self = GTK_ACTION_GROUP (buildable);
413 GtkActionGroupPrivate *private = self->priv;
415 return private->name;
421 GdkModifierType modifiers;
422 } AcceleratorParserData;
425 accelerator_start_element (GMarkupParseContext *context,
426 const gchar *element_name,
428 const gchar **values,
434 GdkModifierType modifiers = 0;
435 AcceleratorParserData *parser_data = (AcceleratorParserData*)user_data;
437 if (strcmp (element_name, "accelerator") != 0)
438 g_warning ("Unknown <accelerator> tag: %s", element_name);
440 for (i = 0; names[i]; i++)
442 if (strcmp (names[i], "key") == 0)
443 key = gdk_keyval_from_name (values[i]);
444 else if (strcmp (names[i], "modifiers") == 0)
446 if (!_gtk_builder_flags_from_string (GDK_TYPE_MODIFIER_TYPE,
456 g_warning ("<accelerator> requires a key attribute");
459 parser_data->key = key;
460 parser_data->modifiers = modifiers;
463 static const GMarkupParser accelerator_parser =
465 accelerator_start_element
469 gtk_action_group_buildable_custom_tag_start (GtkBuildable *buildable,
472 const gchar *tagname,
473 GMarkupParser *parser,
476 AcceleratorParserData *parser_data;
478 if (child && strcmp (tagname, "accelerator") == 0)
480 parser_data = g_slice_new0 (AcceleratorParserData);
481 parser_data->child = child;
482 *user_data = parser_data;
483 *parser = accelerator_parser;
491 gtk_action_group_buildable_custom_tag_end (GtkBuildable *buildable,
494 const gchar *tagname,
497 AcceleratorParserData *data;
499 if (strcmp (tagname, "accelerator") == 0)
501 GtkActionGroup *action_group;
502 GtkActionGroupPrivate *private;
506 data = (AcceleratorParserData*)user_data;
507 action_group = GTK_ACTION_GROUP (buildable);
508 private = action_group->priv;
509 action = GTK_ACTION (child);
511 accel_path = g_strconcat ("<Actions>/",
513 gtk_action_get_name (action), NULL);
515 if (gtk_accel_map_lookup_entry (accel_path, NULL))
516 gtk_accel_map_change_entry (accel_path, data->key, data->modifiers, TRUE);
518 gtk_accel_map_add_entry (accel_path, data->key, data->modifiers);
520 gtk_action_set_accel_path (action, accel_path);
523 g_slice_free (AcceleratorParserData, data);
528 * gtk_action_group_new:
529 * @name: the name of the action group.
531 * Creates a new #GtkActionGroup object. The name of the action group
532 * is used when associating <link linkend="Action-Accel">keybindings</link>
535 * Returns: the new #GtkActionGroup
540 gtk_action_group_new (const gchar *name)
542 GtkActionGroup *self;
543 GtkActionGroupPrivate *private;
545 self = g_object_new (GTK_TYPE_ACTION_GROUP, NULL);
546 private = self->priv;
547 private->name = g_strdup (name);
553 gtk_action_group_finalize (GObject *object)
555 GtkActionGroup *self;
556 GtkActionGroupPrivate *private;
558 self = GTK_ACTION_GROUP (object);
559 private = self->priv;
561 g_free (private->name);
562 private->name = NULL;
564 g_hash_table_destroy (private->actions);
565 private->actions = NULL;
567 g_clear_object (&private->accel_group);
569 if (private->translate_notify)
570 private->translate_notify (private->translate_data);
572 parent_class->finalize (object);
576 gtk_action_group_set_property (GObject *object,
581 GtkActionGroup *self;
582 GtkActionGroupPrivate *private;
585 self = GTK_ACTION_GROUP (object);
586 private = self->priv;
592 private->name = g_value_dup_string (value);
596 gtk_action_group_set_sensitive (self, g_value_get_boolean (value));
599 gtk_action_group_set_visible (self, g_value_get_boolean (value));
601 case PROP_ACCEL_GROUP:
602 gtk_action_group_set_accel_group (self, g_value_get_object (value));
605 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
611 gtk_action_group_get_property (GObject *object,
616 GtkActionGroup *self;
617 GtkActionGroupPrivate *private;
619 self = GTK_ACTION_GROUP (object);
620 private = self->priv;
625 g_value_set_string (value, private->name);
628 g_value_set_boolean (value, private->sensitive);
631 g_value_set_boolean (value, private->visible);
633 case PROP_ACCEL_GROUP:
634 g_value_set_object (value, private->accel_group);
637 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
643 gtk_action_group_real_get_action (GtkActionGroup *self,
644 const gchar *action_name)
646 GtkActionGroupPrivate *private;
648 private = self->priv;
650 return g_hash_table_lookup (private->actions, action_name);
654 * gtk_action_group_get_name:
655 * @action_group: the action group
657 * Gets the name of the action group.
659 * Returns: the name of the action group.
664 gtk_action_group_get_name (GtkActionGroup *action_group)
666 GtkActionGroupPrivate *private;
668 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
670 private = action_group->priv;
672 return private->name;
676 * gtk_action_group_get_sensitive:
677 * @action_group: the action group
679 * Returns %TRUE if the group is sensitive. The constituent actions
680 * can only be logically sensitive (see gtk_action_is_sensitive()) if
681 * they are sensitive (see gtk_action_get_sensitive()) and their group
684 * Return value: %TRUE if the group is sensitive.
689 gtk_action_group_get_sensitive (GtkActionGroup *action_group)
691 GtkActionGroupPrivate *private;
693 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
695 private = action_group->priv;
697 return private->sensitive;
701 cb_set_action_sensitivity (const gchar *name,
704 /* Minor optimization, the action_groups state only affects actions
705 * that are themselves sensitive */
706 g_object_notify (G_OBJECT (action), "sensitive");
711 * gtk_action_group_set_sensitive:
712 * @action_group: the action group
713 * @sensitive: new sensitivity
715 * Changes the sensitivity of @action_group
720 gtk_action_group_set_sensitive (GtkActionGroup *action_group,
723 GtkActionGroupPrivate *private;
725 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
727 private = action_group->priv;
728 sensitive = sensitive != FALSE;
730 if (private->sensitive != sensitive)
732 private->sensitive = sensitive;
733 g_hash_table_foreach (private->actions,
734 (GHFunc) cb_set_action_sensitivity, NULL);
736 g_object_notify (G_OBJECT (action_group), "sensitive");
741 * gtk_action_group_get_visible:
742 * @action_group: the action group
744 * Returns %TRUE if the group is visible. The constituent actions
745 * can only be logically visible (see gtk_action_is_visible()) if
746 * they are visible (see gtk_action_get_visible()) and their group
749 * Return value: %TRUE if the group is visible.
754 gtk_action_group_get_visible (GtkActionGroup *action_group)
756 GtkActionGroupPrivate *private;
758 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
760 private = action_group->priv;
762 return private->visible;
766 * gtk_action_group_get_accel_group:
767 * @action_group: a #GtkActionGroup
769 * Gets the accelerator group.
771 * Returns: (transfer none): the accelerator group associated with this action
772 * group or %NULL if there is none.
777 gtk_action_group_get_accel_group (GtkActionGroup *action_group)
779 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
781 return action_group->priv->accel_group;
785 cb_set_action_visiblity (const gchar *name,
788 /* Minor optimization, the action_groups state only affects actions
789 * that are themselves visible */
790 g_object_notify (G_OBJECT (action), "visible");
794 * gtk_action_group_set_visible:
795 * @action_group: the action group
796 * @visible: new visiblity
798 * Changes the visible of @action_group.
803 gtk_action_group_set_visible (GtkActionGroup *action_group,
806 GtkActionGroupPrivate *private;
808 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
810 private = action_group->priv;
811 visible = visible != FALSE;
813 if (private->visible != visible)
815 private->visible = visible;
816 g_hash_table_foreach (private->actions,
817 (GHFunc) cb_set_action_visiblity, NULL);
819 g_object_notify (G_OBJECT (action_group), "visible");
824 gtk_action_group_accel_group_foreach (gpointer key, gpointer val, gpointer data)
826 gtk_action_set_accel_group (val, data);
830 * gtk_action_group_set_accel_group:
831 * @action_group: a #GtkActionGroup
832 * @accel_group: (allow-none): a #GtkAccelGroup to set or %NULL
834 * Sets the accelerator group to be used by every action in this group.
839 gtk_action_group_set_accel_group (GtkActionGroup *action_group,
840 GtkAccelGroup *accel_group)
842 GtkActionGroupPrivate *private;
844 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
846 private = action_group->priv;
848 if (private->accel_group == accel_group)
851 g_clear_object (&private->accel_group);
854 private->accel_group = g_object_ref (accel_group);
856 /* Set the new accel group on every action */
857 g_hash_table_foreach (private->actions,
858 gtk_action_group_accel_group_foreach,
861 g_object_notify (G_OBJECT (action_group), "accel-group");
865 * gtk_action_group_get_action:
866 * @action_group: the action group
867 * @action_name: the name of the action
869 * Looks up an action in the action group by name.
871 * Returns: (transfer none): the action, or %NULL if no action by that name exists
876 gtk_action_group_get_action (GtkActionGroup *action_group,
877 const gchar *action_name)
879 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
880 g_return_val_if_fail (GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action != NULL, NULL);
882 return GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action (action_group,
887 check_unique_action (GtkActionGroup *action_group,
888 const gchar *action_name)
890 if (gtk_action_group_get_action (action_group, action_name) != NULL)
892 GtkActionGroupPrivate *private;
894 private = action_group->priv;
896 g_warning ("Refusing to add non-unique action '%s' to action group '%s'",
906 * gtk_action_group_add_action:
907 * @action_group: the action group
910 * Adds an action object to the action group. Note that this function
911 * does not set up the accel path of the action, which can lead to problems
912 * if a user tries to modify the accelerator of a menuitem associated with
913 * the action. Therefore you must either set the accel path yourself with
914 * gtk_action_set_accel_path(), or use
915 * <literal>gtk_action_group_add_action_with_accel (..., NULL)</literal>.
920 gtk_action_group_add_action (GtkActionGroup *action_group,
923 GtkActionGroupPrivate *private;
926 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
927 g_return_if_fail (GTK_IS_ACTION (action));
929 name = gtk_action_get_name (action);
930 g_return_if_fail (name != NULL);
932 if (!check_unique_action (action_group, name))
935 private = action_group->priv;
937 g_hash_table_insert (private->actions,
939 g_object_ref (action));
940 g_object_set (action, I_("action-group"), action_group, NULL);
942 if (private->accel_group)
943 gtk_action_set_accel_group (action, private->accel_group);
947 * gtk_action_group_add_action_with_accel:
948 * @action_group: the action group
949 * @action: the action to add
950 * @accelerator: (allow-none): the accelerator for the action, in
951 * the format understood by gtk_accelerator_parse(), or "" for no accelerator, or
952 * %NULL to use the stock accelerator
954 * Adds an action object to the action group and sets up the accelerator.
956 * If @accelerator is %NULL, attempts to use the accelerator associated
957 * with the stock_id of the action.
959 * Accel paths are set to
960 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
965 gtk_action_group_add_action_with_accel (GtkActionGroup *action_group,
967 const gchar *accelerator)
969 GtkActionGroupPrivate *private;
972 GdkModifierType accel_mods;
975 name = gtk_action_get_name (action);
976 if (!check_unique_action (action_group, name))
979 private = action_group->priv;
980 accel_path = g_strconcat ("<Actions>/",
981 private->name, "/", name, NULL);
985 if (accelerator[0] == 0)
989 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
991 g_warning ("Unable to parse accelerator '%s' for action '%s'",
998 GtkStockItem stock_item;
1000 g_object_get (action, "stock-id", &stock_id, NULL);
1002 if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
1004 accel_key = stock_item.keyval;
1005 accel_mods = stock_item.modifier;
1012 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
1014 gtk_action_set_accel_path (action, accel_path);
1015 gtk_action_group_add_action (action_group, action);
1017 g_free (accel_path);
1021 * gtk_action_group_remove_action:
1022 * @action_group: the action group
1023 * @action: an action
1025 * Removes an action object from the action group.
1030 gtk_action_group_remove_action (GtkActionGroup *action_group,
1033 GtkActionGroupPrivate *private;
1036 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1037 g_return_if_fail (GTK_IS_ACTION (action));
1039 name = gtk_action_get_name (action);
1040 g_return_if_fail (name != NULL);
1042 private = action_group->priv;
1044 g_hash_table_remove (private->actions, name);
1048 add_single_action (gpointer key,
1052 GList **list = user_data;
1054 *list = g_list_prepend (*list, value);
1058 * gtk_action_group_list_actions:
1059 * @action_group: the action group
1061 * Lists the actions in the action group.
1063 * Returns: (element-type GtkAction) (transfer container): an allocated list of the action objects in the action group
1068 gtk_action_group_list_actions (GtkActionGroup *action_group)
1070 GtkActionGroupPrivate *private;
1071 GList *actions = NULL;
1073 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
1075 private = action_group->priv;
1077 g_hash_table_foreach (private->actions, add_single_action, &actions);
1079 return g_list_reverse (actions);
1084 * gtk_action_group_add_actions: (skip)
1085 * @action_group: the action group
1086 * @entries: (array length=n_entries): an array of action descriptions
1087 * @n_entries: the number of entries
1088 * @user_data: data to pass to the action callbacks
1090 * This is a convenience function to create a number of actions and add them
1091 * to the action group.
1093 * The "activate" signals of the actions are connected to the callbacks and
1094 * their accel paths are set to
1095 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
1100 gtk_action_group_add_actions (GtkActionGroup *action_group,
1101 const GtkActionEntry *entries,
1105 gtk_action_group_add_actions_full (action_group,
1110 typedef struct _SharedData SharedData;
1112 struct _SharedData {
1115 GDestroyNotify destroy;
1119 shared_data_unref (gpointer data)
1121 SharedData *shared_data = (SharedData *)data;
1123 shared_data->ref_count--;
1124 if (shared_data->ref_count == 0)
1126 if (shared_data->destroy)
1127 shared_data->destroy (shared_data->data);
1129 g_slice_free (SharedData, shared_data);
1135 * gtk_action_group_add_actions_full: (skip)
1136 * @action_group: the action group
1137 * @entries: (array length=n_entries): an array of action descriptions
1138 * @n_entries: the number of entries
1139 * @user_data: data to pass to the action callbacks
1140 * @destroy: destroy notification callback for @user_data
1142 * This variant of gtk_action_group_add_actions() adds a #GDestroyNotify
1143 * callback for @user_data.
1148 gtk_action_group_add_actions_full (GtkActionGroup *action_group,
1149 const GtkActionEntry *entries,
1152 GDestroyNotify destroy)
1155 /* Keep this in sync with the other
1156 * gtk_action_group_add_..._actions_full() functions.
1159 SharedData *shared_data;
1161 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1163 shared_data = g_slice_new0 (SharedData);
1164 shared_data->ref_count = 1;
1165 shared_data->data = user_data;
1166 shared_data->destroy = destroy;
1168 for (i = 0; i < n_entries; i++)
1172 const gchar *tooltip;
1174 if (!check_unique_action (action_group, entries[i].name))
1177 label = gtk_action_group_translate_string (action_group, entries[i].label);
1178 tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1180 action = gtk_action_new (entries[i].name,
1185 if (entries[i].stock_id)
1187 g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1188 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
1189 entries[i].stock_id))
1190 g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1193 if (entries[i].callback)
1197 closure = g_cclosure_new (entries[i].callback, user_data, NULL);
1198 g_closure_add_finalize_notifier (closure, shared_data,
1199 (GClosureNotify)shared_data_unref);
1200 shared_data->ref_count++;
1202 g_signal_connect_closure (action, "activate", closure, FALSE);
1205 gtk_action_group_add_action_with_accel (action_group,
1207 entries[i].accelerator);
1208 g_object_unref (action);
1211 shared_data_unref (shared_data);
1215 * gtk_action_group_add_toggle_actions: (skip)
1216 * @action_group: the action group
1217 * @entries: (array length=n_entries): an array of toggle action descriptions
1218 * @n_entries: the number of entries
1219 * @user_data: data to pass to the action callbacks
1221 * This is a convenience function to create a number of toggle actions and add them
1222 * to the action group.
1224 * The "activate" signals of the actions are connected to the callbacks and
1225 * their accel paths are set to
1226 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
1231 gtk_action_group_add_toggle_actions (GtkActionGroup *action_group,
1232 const GtkToggleActionEntry *entries,
1236 gtk_action_group_add_toggle_actions_full (action_group,
1243 * gtk_action_group_add_toggle_actions_full: (skip)
1244 * @action_group: the action group
1245 * @entries: (array length=n_entries): an array of toggle action descriptions
1246 * @n_entries: the number of entries
1247 * @user_data: data to pass to the action callbacks
1248 * @destroy: destroy notification callback for @user_data
1250 * This variant of gtk_action_group_add_toggle_actions() adds a
1251 * #GDestroyNotify callback for @user_data.
1256 gtk_action_group_add_toggle_actions_full (GtkActionGroup *action_group,
1257 const GtkToggleActionEntry *entries,
1260 GDestroyNotify destroy)
1262 /* Keep this in sync with the other
1263 * gtk_action_group_add_..._actions_full() functions.
1266 SharedData *shared_data;
1268 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1270 shared_data = g_slice_new0 (SharedData);
1271 shared_data->ref_count = 1;
1272 shared_data->data = user_data;
1273 shared_data->destroy = destroy;
1275 for (i = 0; i < n_entries; i++)
1277 GtkToggleAction *action;
1279 const gchar *tooltip;
1281 if (!check_unique_action (action_group, entries[i].name))
1284 label = gtk_action_group_translate_string (action_group, entries[i].label);
1285 tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1287 action = gtk_toggle_action_new (entries[i].name,
1292 if (entries[i].stock_id)
1294 if (gtk_icon_factory_lookup_default (entries[i].stock_id))
1295 g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1297 g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1300 gtk_toggle_action_set_active (action, entries[i].is_active);
1302 if (entries[i].callback)
1306 closure = g_cclosure_new (entries[i].callback, user_data, NULL);
1307 g_closure_add_finalize_notifier (closure, shared_data,
1308 (GClosureNotify)shared_data_unref);
1309 shared_data->ref_count++;
1311 g_signal_connect_closure (action, "activate", closure, FALSE);
1314 gtk_action_group_add_action_with_accel (action_group,
1315 GTK_ACTION (action),
1316 entries[i].accelerator);
1317 g_object_unref (action);
1320 shared_data_unref (shared_data);
1324 * gtk_action_group_add_radio_actions: (skip)
1325 * @action_group: the action group
1326 * @entries: (array length=n_entries): an array of radio action descriptions
1327 * @n_entries: the number of entries
1328 * @value: the value of the action to activate initially, or -1 if
1329 * no action should be activated
1330 * @on_change: the callback to connect to the changed signal
1331 * @user_data: data to pass to the action callbacks
1333 * This is a convenience routine to create a group of radio actions and
1334 * add them to the action group.
1336 * The "changed" signal of the first radio action is connected to the
1337 * @on_change callback and the accel paths of the actions are set to
1338 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
1343 gtk_action_group_add_radio_actions (GtkActionGroup *action_group,
1344 const GtkRadioActionEntry *entries,
1347 GCallback on_change,
1350 gtk_action_group_add_radio_actions_full (action_group,
1353 on_change, user_data, NULL);
1357 * gtk_action_group_add_radio_actions_full: (skip)
1358 * @action_group: the action group
1359 * @entries: (array length=n_entries): an array of radio action descriptions
1360 * @n_entries: the number of entries
1361 * @value: the value of the action to activate initially, or -1 if
1362 * no action should be activated
1363 * @on_change: the callback to connect to the changed signal
1364 * @user_data: data to pass to the action callbacks
1365 * @destroy: destroy notification callback for @user_data
1367 * This variant of gtk_action_group_add_radio_actions() adds a
1368 * #GDestroyNotify callback for @user_data.
1373 gtk_action_group_add_radio_actions_full (GtkActionGroup *action_group,
1374 const GtkRadioActionEntry *entries,
1377 GCallback on_change,
1379 GDestroyNotify destroy)
1381 /* Keep this in sync with the other
1382 * gtk_action_group_add_..._actions_full() functions.
1385 GSList *group = NULL;
1386 GtkRadioAction *first_action = NULL;
1388 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1390 for (i = 0; i < n_entries; i++)
1392 GtkRadioAction *action;
1394 const gchar *tooltip;
1396 if (!check_unique_action (action_group, entries[i].name))
1399 label = gtk_action_group_translate_string (action_group, entries[i].label);
1400 tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1402 action = gtk_radio_action_new (entries[i].name,
1408 if (entries[i].stock_id)
1410 if (gtk_icon_factory_lookup_default (entries[i].stock_id))
1411 g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1413 g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1417 first_action = action;
1419 gtk_radio_action_set_group (action, group);
1420 group = gtk_radio_action_get_group (action);
1422 if (value == entries[i].value)
1423 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
1425 gtk_action_group_add_action_with_accel (action_group,
1426 GTK_ACTION (action),
1427 entries[i].accelerator);
1428 g_object_unref (action);
1431 if (on_change && first_action)
1432 g_signal_connect_data (first_action, "changed",
1433 on_change, user_data,
1434 (GClosureNotify)destroy, 0);
1438 * gtk_action_group_set_translate_func:
1439 * @action_group: a #GtkActionGroup
1440 * @func: a #GtkTranslateFunc
1441 * @data: data to be passed to @func and @notify
1442 * @notify: a #GDestroyNotify function to be called when @action_group is
1443 * destroyed and when the translation function is changed again
1445 * Sets a function to be used for translating the @label and @tooltip of
1446 * #GtkActionEntry<!-- -->s added by gtk_action_group_add_actions().
1448 * If you're using gettext(), it is enough to set the translation domain
1449 * with gtk_action_group_set_translation_domain().
1454 gtk_action_group_set_translate_func (GtkActionGroup *action_group,
1455 GtkTranslateFunc func,
1457 GDestroyNotify notify)
1459 GtkActionGroupPrivate *private;
1461 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1463 private = action_group->priv;
1465 if (private->translate_notify)
1466 private->translate_notify (private->translate_data);
1468 private->translate_func = func;
1469 private->translate_data = data;
1470 private->translate_notify = notify;
1474 dgettext_swapped (const gchar *msgid,
1475 const gchar *domainname)
1477 /* Pass through g_dgettext if and only if msgid is nonempty. */
1478 if (msgid && *msgid)
1479 return (gchar*) g_dgettext (domainname, msgid);
1481 return (gchar*) msgid;
1485 * gtk_action_group_set_translation_domain:
1486 * @action_group: a #GtkActionGroup
1487 * @domain: (allow-none): the translation domain to use for g_dgettext()
1488 * calls, or %NULL to use the domain set with textdomain()
1490 * Sets the translation domain and uses g_dgettext() for translating the
1491 * @label and @tooltip of #GtkActionEntry<!-- -->s added by
1492 * gtk_action_group_add_actions().
1494 * If you're not using gettext() for localization, see
1495 * gtk_action_group_set_translate_func().
1500 gtk_action_group_set_translation_domain (GtkActionGroup *action_group,
1501 const gchar *domain)
1503 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1505 gtk_action_group_set_translate_func (action_group,
1506 (GtkTranslateFunc)dgettext_swapped,
1513 * gtk_action_group_translate_string:
1514 * @action_group: a #GtkActionGroup
1517 * Translates a string using the function set with
1518 * gtk_action_group_set_translate_func(). This
1519 * is mainly intended for language bindings.
1521 * Returns: the translation of @string
1526 gtk_action_group_translate_string (GtkActionGroup *action_group,
1527 const gchar *string)
1529 GtkActionGroupPrivate *private;
1530 GtkTranslateFunc translate_func;
1531 gpointer translate_data;
1533 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), string);
1538 private = action_group->priv;
1540 translate_func = private->translate_func;
1541 translate_data = private->translate_data;
1544 return translate_func (string, translate_data);
1549 /* Protected for use by GtkAction */
1551 _gtk_action_group_emit_connect_proxy (GtkActionGroup *action_group,
1555 g_signal_emit (action_group, action_group_signals[CONNECT_PROXY], 0,
1560 _gtk_action_group_emit_disconnect_proxy (GtkActionGroup *action_group,
1564 g_signal_emit (action_group, action_group_signals[DISCONNECT_PROXY], 0,
1569 _gtk_action_group_emit_pre_activate (GtkActionGroup *action_group,
1572 g_signal_emit (action_group, action_group_signals[PRE_ACTIVATE], 0, action);
1576 _gtk_action_group_emit_post_activate (GtkActionGroup *action_group,
1579 g_signal_emit (action_group, action_group_signals[POST_ACTIVATE], 0, action);