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 the Gnome Library; see the file COPYING.LIB. If not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * Author: James Henstridge <james@daa.com.au>
25 * Modified by the GTK+ Team and others 2003. See the AUTHORS
26 * file for a list of people on the GTK+ Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 * SECTION:gtkactiongroup
33 * @Short_description: A group of actions
34 * @Title: GtkActionGroup
36 * Actions are organised into groups. An action group is essentially a
37 * map from names to #GtkAction objects.
39 * All actions that would make sense to use in a particular context
40 * should be in a single group. Multiple action groups may be used for a
41 * particular user interface. In fact, it is expected that most nontrivial
42 * applications will make use of multiple groups. For example, in an
43 * application that can edit multiple documents, one group holding global
44 * actions (e.g. quit, about, new), and one group per document holding
45 * actions that act on that document (eg. save, cut/copy/paste, etc). Each
46 * window's menus would be constructed from a combination of two action
49 * <para id="Action-Accel">
50 * Accelerators are handled by the GTK+ accelerator map. All actions are
51 * assigned an accelerator path (which normally has the form
52 * <literal><Actions>/group-name/action-name</literal>)
53 * and a shortcut is associated with this accelerator path. All menuitems
54 * and toolitems take on this accelerator path. The GTK+ accelerator map
55 * code makes sure that the correct shortcut is displayed next to the menu
58 * <refsect2 id="GtkActionGroup-BUILDER-UI">
59 * <title>GtkActionGroup as GtkBuildable</title>
61 * The #GtkActionGroup implementation of the #GtkBuildable interface accepts
62 * #GtkAction objects as <child> elements in UI definitions.
64 * Note that it is probably more common to define actions and action groups
65 * in the code, since they are directly related to what the code can do.
67 * The GtkActionGroup implementation of the GtkBuildable interface supports
68 * a custom <accelerator> element, which has attributes named key and
69 * modifiers and allows to specify accelerators. This is similar to the
70 * <accelerator> element of #GtkWidget, the main difference is that
71 * it doesn't allow you to specify a signal.
74 * <title>A #GtkDialog UI definition fragment.</title>
75 * <programlisting><![CDATA[
76 * <object class="GtkActionGroup" id="actiongroup">
78 * <object class="GtkAction" id="About">
79 * <property name="name">About</property>
80 * <property name="stock_id">gtk-about</property>
81 * <signal handler="about_activate" name="activate"/>
83 * <accelerator key="F1" modifiers="GDK_CONTROL_MASK | GDK_SHIFT_MASK"/>
86 * ]]></programlisting>
94 #include "gtkactiongroup.h"
95 #include "gtkbuildable.h"
96 #include "gtkiconfactory.h"
97 #include "gtkicontheme.h"
99 #include "gtktoggleaction.h"
100 #include "gtkradioaction.h"
101 #include "gtkaccelmap.h"
102 #include "gtkmarshalers.h"
103 #include "gtkbuilderprivate.h"
104 #include "gtkprivate.h"
108 struct _GtkActionGroupPrivate
115 GtkTranslateFunc translate_func;
116 gpointer translate_data;
117 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));
248 * GtkActionGroup::connect-proxy:
249 * @action_group: the group
250 * @action: the action
253 * The ::connect-proxy signal is emitted after connecting a proxy to
254 * an action in the group. Note that the proxy may have been connected
255 * to a different action before.
257 * This is intended for simple customizations for which a custom action
258 * class would be too clumsy, e.g. showing tooltips for menuitems in the
261 * #GtkUIManager proxies the signal and provides global notification
262 * just before any action is connected to a proxy, which is probably more
267 action_group_signals[CONNECT_PROXY] =
268 g_signal_new (I_("connect-proxy"),
269 G_OBJECT_CLASS_TYPE (klass),
271 _gtk_marshal_VOID__OBJECT_OBJECT,
273 GTK_TYPE_ACTION, GTK_TYPE_WIDGET);
276 * GtkActionGroup::disconnect-proxy:
277 * @action_group: the group
278 * @action: the action
281 * The ::disconnect-proxy signal is emitted after disconnecting a proxy
282 * from an action in the group.
284 * #GtkUIManager proxies the signal and provides global notification
285 * just before any action is connected to a proxy, which is probably more
290 action_group_signals[DISCONNECT_PROXY] =
291 g_signal_new (I_("disconnect-proxy"),
292 G_OBJECT_CLASS_TYPE (klass),
294 _gtk_marshal_VOID__OBJECT_OBJECT,
296 GTK_TYPE_ACTION, GTK_TYPE_WIDGET);
299 * GtkActionGroup::pre-activate:
300 * @action_group: the group
301 * @action: the action
303 * The ::pre-activate signal is emitted just before the @action in the
304 * @action_group is activated
306 * This is intended for #GtkUIManager to proxy the signal and provide global
307 * notification just before any action is activated.
311 action_group_signals[PRE_ACTIVATE] =
312 g_signal_new (I_("pre-activate"),
313 G_OBJECT_CLASS_TYPE (klass),
315 _gtk_marshal_VOID__OBJECT,
320 * GtkActionGroup::post-activate:
321 * @action_group: the group
322 * @action: the action
324 * The ::post-activate signal is emitted just after the @action in the
325 * @action_group is activated
327 * This is intended for #GtkUIManager to proxy the signal and provide global
328 * notification just after any action is activated.
332 action_group_signals[POST_ACTIVATE] =
333 g_signal_new (I_("post-activate"),
334 G_OBJECT_CLASS_TYPE (klass),
336 _gtk_marshal_VOID__OBJECT,
340 g_type_class_add_private (gobject_class, sizeof (GtkActionGroupPrivate));
345 remove_action (GtkAction *action)
347 g_object_set (action, I_("action-group"), NULL, NULL);
348 g_object_unref (action);
352 gtk_action_group_init (GtkActionGroup *action_group)
354 GtkActionGroupPrivate *private;
356 action_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (action_group,
357 GTK_TYPE_ACTION_GROUP,
358 GtkActionGroupPrivate);
359 private = action_group->priv;
361 private->name = NULL;
362 private->sensitive = TRUE;
363 private->visible = TRUE;
364 private->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
366 (GDestroyNotify) remove_action);
367 private->translate_func = NULL;
368 private->translate_data = NULL;
369 private->translate_notify = NULL;
373 gtk_action_group_buildable_init (GtkBuildableIface *iface)
375 iface->add_child = gtk_action_group_buildable_add_child;
376 iface->set_name = gtk_action_group_buildable_set_name;
377 iface->get_name = gtk_action_group_buildable_get_name;
378 iface->custom_tag_start = gtk_action_group_buildable_custom_tag_start;
379 iface->custom_tag_end = gtk_action_group_buildable_custom_tag_end;
383 gtk_action_group_buildable_add_child (GtkBuildable *buildable,
388 gtk_action_group_add_action_with_accel (GTK_ACTION_GROUP (buildable),
389 GTK_ACTION (child), NULL);
393 gtk_action_group_buildable_set_name (GtkBuildable *buildable,
396 GtkActionGroup *self = GTK_ACTION_GROUP (buildable);
397 GtkActionGroupPrivate *private = self->priv;
399 private->name = g_strdup (name);
403 gtk_action_group_buildable_get_name (GtkBuildable *buildable)
405 GtkActionGroup *self = GTK_ACTION_GROUP (buildable);
406 GtkActionGroupPrivate *private = self->priv;
408 return private->name;
414 GdkModifierType modifiers;
415 } AcceleratorParserData;
418 accelerator_start_element (GMarkupParseContext *context,
419 const gchar *element_name,
421 const gchar **values,
427 GdkModifierType modifiers = 0;
428 AcceleratorParserData *parser_data = (AcceleratorParserData*)user_data;
430 if (strcmp (element_name, "accelerator") != 0)
431 g_warning ("Unknown <accelerator> tag: %s", element_name);
433 for (i = 0; names[i]; i++)
435 if (strcmp (names[i], "key") == 0)
436 key = gdk_keyval_from_name (values[i]);
437 else if (strcmp (names[i], "modifiers") == 0)
439 if (!_gtk_builder_flags_from_string (GDK_TYPE_MODIFIER_TYPE,
449 g_warning ("<accelerator> requires a key attribute");
452 parser_data->key = key;
453 parser_data->modifiers = modifiers;
456 static const GMarkupParser accelerator_parser =
458 accelerator_start_element
462 gtk_action_group_buildable_custom_tag_start (GtkBuildable *buildable,
465 const gchar *tagname,
466 GMarkupParser *parser,
469 AcceleratorParserData *parser_data;
471 if (child && strcmp (tagname, "accelerator") == 0)
473 parser_data = g_slice_new0 (AcceleratorParserData);
474 parser_data->child = child;
475 *user_data = parser_data;
476 *parser = accelerator_parser;
484 gtk_action_group_buildable_custom_tag_end (GtkBuildable *buildable,
487 const gchar *tagname,
490 AcceleratorParserData *data;
492 if (strcmp (tagname, "accelerator") == 0)
494 GtkActionGroup *action_group;
495 GtkActionGroupPrivate *private;
499 data = (AcceleratorParserData*)user_data;
500 action_group = GTK_ACTION_GROUP (buildable);
501 private = action_group->priv;
502 action = GTK_ACTION (child);
504 accel_path = g_strconcat ("<Actions>/",
506 gtk_action_get_name (action), NULL);
508 if (gtk_accel_map_lookup_entry (accel_path, NULL))
509 gtk_accel_map_change_entry (accel_path, data->key, data->modifiers, TRUE);
511 gtk_accel_map_add_entry (accel_path, data->key, data->modifiers);
513 gtk_action_set_accel_path (action, accel_path);
516 g_slice_free (AcceleratorParserData, data);
521 * gtk_action_group_new:
522 * @name: the name of the action group.
524 * Creates a new #GtkActionGroup object. The name of the action group
525 * is used when associating <link linkend="Action-Accel">keybindings</link>
528 * Returns: the new #GtkActionGroup
533 gtk_action_group_new (const gchar *name)
535 GtkActionGroup *self;
536 GtkActionGroupPrivate *private;
538 self = g_object_new (GTK_TYPE_ACTION_GROUP, NULL);
539 private = self->priv;
540 private->name = g_strdup (name);
546 gtk_action_group_finalize (GObject *object)
548 GtkActionGroup *self;
549 GtkActionGroupPrivate *private;
551 self = GTK_ACTION_GROUP (object);
552 private = self->priv;
554 g_free (private->name);
555 private->name = NULL;
557 g_hash_table_destroy (private->actions);
558 private->actions = NULL;
560 if (private->translate_notify)
561 private->translate_notify (private->translate_data);
563 parent_class->finalize (object);
567 gtk_action_group_set_property (GObject *object,
572 GtkActionGroup *self;
573 GtkActionGroupPrivate *private;
576 self = GTK_ACTION_GROUP (object);
577 private = self->priv;
583 private->name = g_value_dup_string (value);
587 gtk_action_group_set_sensitive (self, g_value_get_boolean (value));
590 gtk_action_group_set_visible (self, g_value_get_boolean (value));
593 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
599 gtk_action_group_get_property (GObject *object,
604 GtkActionGroup *self;
605 GtkActionGroupPrivate *private;
607 self = GTK_ACTION_GROUP (object);
608 private = self->priv;
613 g_value_set_string (value, private->name);
616 g_value_set_boolean (value, private->sensitive);
619 g_value_set_boolean (value, private->visible);
622 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
628 gtk_action_group_real_get_action (GtkActionGroup *self,
629 const gchar *action_name)
631 GtkActionGroupPrivate *private;
633 private = self->priv;
635 return g_hash_table_lookup (private->actions, action_name);
639 * gtk_action_group_get_name:
640 * @action_group: the action group
642 * Gets the name of the action group.
644 * Returns: the name of the action group.
649 gtk_action_group_get_name (GtkActionGroup *action_group)
651 GtkActionGroupPrivate *private;
653 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
655 private = action_group->priv;
657 return private->name;
661 * gtk_action_group_get_sensitive:
662 * @action_group: the action group
664 * Returns %TRUE if the group is sensitive. The constituent actions
665 * can only be logically sensitive (see gtk_action_is_sensitive()) if
666 * they are sensitive (see gtk_action_get_sensitive()) and their group
669 * Return value: %TRUE if the group is sensitive.
674 gtk_action_group_get_sensitive (GtkActionGroup *action_group)
676 GtkActionGroupPrivate *private;
678 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
680 private = action_group->priv;
682 return private->sensitive;
686 cb_set_action_sensitivity (const gchar *name,
689 /* Minor optimization, the action_groups state only affects actions
690 * that are themselves sensitive */
691 g_object_notify (G_OBJECT (action), "sensitive");
696 * gtk_action_group_set_sensitive:
697 * @action_group: the action group
698 * @sensitive: new sensitivity
700 * Changes the sensitivity of @action_group
705 gtk_action_group_set_sensitive (GtkActionGroup *action_group,
708 GtkActionGroupPrivate *private;
710 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
712 private = action_group->priv;
713 sensitive = sensitive != FALSE;
715 if (private->sensitive != sensitive)
717 private->sensitive = sensitive;
718 g_hash_table_foreach (private->actions,
719 (GHFunc) cb_set_action_sensitivity, NULL);
721 g_object_notify (G_OBJECT (action_group), "sensitive");
726 * gtk_action_group_get_visible:
727 * @action_group: the action group
729 * Returns %TRUE if the group is visible. The constituent actions
730 * can only be logically visible (see gtk_action_is_visible()) if
731 * they are visible (see gtk_action_get_visible()) and their group
734 * Return value: %TRUE if the group is visible.
739 gtk_action_group_get_visible (GtkActionGroup *action_group)
741 GtkActionGroupPrivate *private;
743 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
745 private = action_group->priv;
747 return private->visible;
751 cb_set_action_visiblity (const gchar *name,
754 /* Minor optimization, the action_groups state only affects actions
755 * that are themselves visible */
756 g_object_notify (G_OBJECT (action), "visible");
760 * gtk_action_group_set_visible:
761 * @action_group: the action group
762 * @visible: new visiblity
764 * Changes the visible of @action_group.
769 gtk_action_group_set_visible (GtkActionGroup *action_group,
772 GtkActionGroupPrivate *private;
774 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
776 private = action_group->priv;
777 visible = visible != FALSE;
779 if (private->visible != visible)
781 private->visible = visible;
782 g_hash_table_foreach (private->actions,
783 (GHFunc) cb_set_action_visiblity, NULL);
785 g_object_notify (G_OBJECT (action_group), "visible");
790 * gtk_action_group_get_action:
791 * @action_group: the action group
792 * @action_name: the name of the action
794 * Looks up an action in the action group by name.
796 * Returns: (transfer none): the action, or %NULL if no action by that name exists
801 gtk_action_group_get_action (GtkActionGroup *action_group,
802 const gchar *action_name)
804 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
805 g_return_val_if_fail (GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action != NULL, NULL);
807 return GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action (action_group,
812 check_unique_action (GtkActionGroup *action_group,
813 const gchar *action_name)
815 if (gtk_action_group_get_action (action_group, action_name) != NULL)
817 GtkActionGroupPrivate *private;
819 private = action_group->priv;
821 g_warning ("Refusing to add non-unique action '%s' to action group '%s'",
831 * gtk_action_group_add_action:
832 * @action_group: the action group
835 * Adds an action object to the action group. Note that this function
836 * does not set up the accel path of the action, which can lead to problems
837 * if a user tries to modify the accelerator of a menuitem associated with
838 * the action. Therefore you must either set the accel path yourself with
839 * gtk_action_set_accel_path(), or use
840 * <literal>gtk_action_group_add_action_with_accel (..., NULL)</literal>.
845 gtk_action_group_add_action (GtkActionGroup *action_group,
848 GtkActionGroupPrivate *private;
851 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
852 g_return_if_fail (GTK_IS_ACTION (action));
854 name = gtk_action_get_name (action);
855 g_return_if_fail (name != NULL);
857 if (!check_unique_action (action_group, name))
860 private = action_group->priv;
862 g_hash_table_insert (private->actions,
864 g_object_ref (action));
865 g_object_set (action, I_("action-group"), action_group, NULL);
869 * gtk_action_group_add_action_with_accel:
870 * @action_group: the action group
871 * @action: the action to add
872 * @accelerator: (allow-none): the accelerator for the action, in
873 * the format understood by gtk_accelerator_parse(), or "" for no accelerator, or
874 * %NULL to use the stock accelerator
876 * Adds an action object to the action group and sets up the accelerator.
878 * If @accelerator is %NULL, attempts to use the accelerator associated
879 * with the stock_id of the action.
881 * Accel paths are set to
882 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
887 gtk_action_group_add_action_with_accel (GtkActionGroup *action_group,
889 const gchar *accelerator)
891 GtkActionGroupPrivate *private;
894 GdkModifierType accel_mods;
897 name = gtk_action_get_name (action);
898 if (!check_unique_action (action_group, name))
901 private = action_group->priv;
902 accel_path = g_strconcat ("<Actions>/",
903 private->name, "/", name, NULL);
907 if (accelerator[0] == 0)
911 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
913 g_warning ("Unable to parse accelerator '%s' for action '%s'",
920 GtkStockItem stock_item;
922 g_object_get (action, "stock-id", &stock_id, NULL);
924 if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
926 accel_key = stock_item.keyval;
927 accel_mods = stock_item.modifier;
934 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
936 gtk_action_set_accel_path (action, accel_path);
937 gtk_action_group_add_action (action_group, action);
943 * gtk_action_group_remove_action:
944 * @action_group: the action group
947 * Removes an action object from the action group.
952 gtk_action_group_remove_action (GtkActionGroup *action_group,
955 GtkActionGroupPrivate *private;
958 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
959 g_return_if_fail (GTK_IS_ACTION (action));
961 name = gtk_action_get_name (action);
962 g_return_if_fail (name != NULL);
964 private = action_group->priv;
966 g_hash_table_remove (private->actions, name);
970 add_single_action (gpointer key,
974 GList **list = user_data;
976 *list = g_list_prepend (*list, value);
980 * gtk_action_group_list_actions:
981 * @action_group: the action group
983 * Lists the actions in the action group.
985 * Returns: (element-type GtkAction) (transfer container): an allocated list of the action objects in the action group
990 gtk_action_group_list_actions (GtkActionGroup *action_group)
992 GtkActionGroupPrivate *private;
993 GList *actions = NULL;
995 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
997 private = action_group->priv;
999 g_hash_table_foreach (private->actions, add_single_action, &actions);
1001 return g_list_reverse (actions);
1006 * gtk_action_group_add_actions: (skip)
1007 * @action_group: the action group
1008 * @entries: (array length=n_entries): an array of action descriptions
1009 * @n_entries: the number of entries
1010 * @user_data: data to pass to the action callbacks
1012 * This is a convenience function to create a number of actions and add them
1013 * to the action group.
1015 * The "activate" signals of the actions are connected to the callbacks and
1016 * their accel paths are set to
1017 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
1022 gtk_action_group_add_actions (GtkActionGroup *action_group,
1023 const GtkActionEntry *entries,
1027 gtk_action_group_add_actions_full (action_group,
1032 typedef struct _SharedData SharedData;
1034 struct _SharedData {
1037 GDestroyNotify destroy;
1041 shared_data_unref (gpointer data)
1043 SharedData *shared_data = (SharedData *)data;
1045 shared_data->ref_count--;
1046 if (shared_data->ref_count == 0)
1048 if (shared_data->destroy)
1049 shared_data->destroy (shared_data->data);
1051 g_slice_free (SharedData, shared_data);
1057 * gtk_action_group_add_actions_full: (skip)
1058 * @action_group: the action group
1059 * @entries: (array length=n_entries): an array of action descriptions
1060 * @n_entries: the number of entries
1061 * @user_data: data to pass to the action callbacks
1062 * @destroy: destroy notification callback for @user_data
1064 * This variant of gtk_action_group_add_actions() adds a #GDestroyNotify
1065 * callback for @user_data.
1070 gtk_action_group_add_actions_full (GtkActionGroup *action_group,
1071 const GtkActionEntry *entries,
1074 GDestroyNotify destroy)
1077 /* Keep this in sync with the other
1078 * gtk_action_group_add_..._actions_full() functions.
1081 SharedData *shared_data;
1083 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1085 shared_data = g_slice_new0 (SharedData);
1086 shared_data->ref_count = 1;
1087 shared_data->data = user_data;
1088 shared_data->destroy = destroy;
1090 for (i = 0; i < n_entries; i++)
1094 const gchar *tooltip;
1096 if (!check_unique_action (action_group, entries[i].name))
1099 label = gtk_action_group_translate_string (action_group, entries[i].label);
1100 tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1102 action = gtk_action_new (entries[i].name,
1107 if (entries[i].stock_id)
1109 g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1110 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
1111 entries[i].stock_id))
1112 g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1115 if (entries[i].callback)
1119 closure = g_cclosure_new (entries[i].callback, user_data, NULL);
1120 g_closure_add_finalize_notifier (closure, shared_data,
1121 (GClosureNotify)shared_data_unref);
1122 shared_data->ref_count++;
1124 g_signal_connect_closure (action, "activate", closure, FALSE);
1127 gtk_action_group_add_action_with_accel (action_group,
1129 entries[i].accelerator);
1130 g_object_unref (action);
1133 shared_data_unref (shared_data);
1137 * gtk_action_group_add_toggle_actions: (skip)
1138 * @action_group: the action group
1139 * @entries: (array length=n_entries): an array of toggle action descriptions
1140 * @n_entries: the number of entries
1141 * @user_data: data to pass to the action callbacks
1143 * This is a convenience function to create a number of toggle actions and add them
1144 * to the action group.
1146 * The "activate" signals of the actions are connected to the callbacks and
1147 * their accel paths are set to
1148 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
1153 gtk_action_group_add_toggle_actions (GtkActionGroup *action_group,
1154 const GtkToggleActionEntry *entries,
1158 gtk_action_group_add_toggle_actions_full (action_group,
1165 * gtk_action_group_add_toggle_actions_full: (skip)
1166 * @action_group: the action group
1167 * @entries: (array length=n_entries): an array of toggle action descriptions
1168 * @n_entries: the number of entries
1169 * @user_data: data to pass to the action callbacks
1170 * @destroy: destroy notification callback for @user_data
1172 * This variant of gtk_action_group_add_toggle_actions() adds a
1173 * #GDestroyNotify callback for @user_data.
1178 gtk_action_group_add_toggle_actions_full (GtkActionGroup *action_group,
1179 const GtkToggleActionEntry *entries,
1182 GDestroyNotify destroy)
1184 /* Keep this in sync with the other
1185 * gtk_action_group_add_..._actions_full() functions.
1188 SharedData *shared_data;
1190 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1192 shared_data = g_slice_new0 (SharedData);
1193 shared_data->ref_count = 1;
1194 shared_data->data = user_data;
1195 shared_data->destroy = destroy;
1197 for (i = 0; i < n_entries; i++)
1199 GtkToggleAction *action;
1201 const gchar *tooltip;
1203 if (!check_unique_action (action_group, entries[i].name))
1206 label = gtk_action_group_translate_string (action_group, entries[i].label);
1207 tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1209 action = gtk_toggle_action_new (entries[i].name,
1214 if (entries[i].stock_id)
1216 if (gtk_icon_factory_lookup_default (entries[i].stock_id))
1217 g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1219 g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1222 gtk_toggle_action_set_active (action, entries[i].is_active);
1224 if (entries[i].callback)
1228 closure = g_cclosure_new (entries[i].callback, user_data, NULL);
1229 g_closure_add_finalize_notifier (closure, shared_data,
1230 (GClosureNotify)shared_data_unref);
1231 shared_data->ref_count++;
1233 g_signal_connect_closure (action, "activate", closure, FALSE);
1236 gtk_action_group_add_action_with_accel (action_group,
1237 GTK_ACTION (action),
1238 entries[i].accelerator);
1239 g_object_unref (action);
1242 shared_data_unref (shared_data);
1246 * gtk_action_group_add_radio_actions: (skip)
1247 * @action_group: the action group
1248 * @entries: (array length=n_entries): an array of radio action descriptions
1249 * @n_entries: the number of entries
1250 * @value: the value of the action to activate initially, or -1 if
1251 * no action should be activated
1252 * @on_change: the callback to connect to the changed signal
1253 * @user_data: data to pass to the action callbacks
1255 * This is a convenience routine to create a group of radio actions and
1256 * add them to the action group.
1258 * The "changed" signal of the first radio action is connected to the
1259 * @on_change callback and the accel paths of the actions are set to
1260 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
1265 gtk_action_group_add_radio_actions (GtkActionGroup *action_group,
1266 const GtkRadioActionEntry *entries,
1269 GCallback on_change,
1272 gtk_action_group_add_radio_actions_full (action_group,
1275 on_change, user_data, NULL);
1279 * gtk_action_group_add_radio_actions_full: (skip)
1280 * @action_group: the action group
1281 * @entries: (array length=n_entries): an array of radio action descriptions
1282 * @n_entries: the number of entries
1283 * @value: the value of the action to activate initially, or -1 if
1284 * no action should be activated
1285 * @on_change: the callback to connect to the changed signal
1286 * @user_data: data to pass to the action callbacks
1287 * @destroy: destroy notification callback for @user_data
1289 * This variant of gtk_action_group_add_radio_actions() adds a
1290 * #GDestroyNotify callback for @user_data.
1295 gtk_action_group_add_radio_actions_full (GtkActionGroup *action_group,
1296 const GtkRadioActionEntry *entries,
1299 GCallback on_change,
1301 GDestroyNotify destroy)
1303 /* Keep this in sync with the other
1304 * gtk_action_group_add_..._actions_full() functions.
1307 GSList *group = NULL;
1308 GtkRadioAction *first_action = NULL;
1310 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1312 for (i = 0; i < n_entries; i++)
1314 GtkRadioAction *action;
1316 const gchar *tooltip;
1318 if (!check_unique_action (action_group, entries[i].name))
1321 label = gtk_action_group_translate_string (action_group, entries[i].label);
1322 tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1324 action = gtk_radio_action_new (entries[i].name,
1330 if (entries[i].stock_id)
1332 if (gtk_icon_factory_lookup_default (entries[i].stock_id))
1333 g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1335 g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1339 first_action = action;
1341 gtk_radio_action_set_group (action, group);
1342 group = gtk_radio_action_get_group (action);
1344 if (value == entries[i].value)
1345 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
1347 gtk_action_group_add_action_with_accel (action_group,
1348 GTK_ACTION (action),
1349 entries[i].accelerator);
1350 g_object_unref (action);
1353 if (on_change && first_action)
1354 g_signal_connect_data (first_action, "changed",
1355 on_change, user_data,
1356 (GClosureNotify)destroy, 0);
1360 * gtk_action_group_set_translate_func:
1361 * @action_group: a #GtkActionGroup
1362 * @func: a #GtkTranslateFunc
1363 * @data: data to be passed to @func and @notify
1364 * @notify: a #GDestroyNotify function to be called when @action_group is
1365 * destroyed and when the translation function is changed again
1367 * Sets a function to be used for translating the @label and @tooltip of
1368 * #GtkActionGroupEntry<!-- -->s added by gtk_action_group_add_actions().
1370 * If you're using gettext(), it is enough to set the translation domain
1371 * with gtk_action_group_set_translation_domain().
1376 gtk_action_group_set_translate_func (GtkActionGroup *action_group,
1377 GtkTranslateFunc func,
1379 GDestroyNotify notify)
1381 GtkActionGroupPrivate *private;
1383 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1385 private = action_group->priv;
1387 if (private->translate_notify)
1388 private->translate_notify (private->translate_data);
1390 private->translate_func = func;
1391 private->translate_data = data;
1392 private->translate_notify = notify;
1396 dgettext_swapped (const gchar *msgid,
1397 const gchar *domainname)
1399 /* Pass through g_dgettext if and only if msgid is nonempty. */
1400 if (msgid && *msgid)
1401 return (gchar*) g_dgettext (domainname, msgid);
1403 return (gchar*) msgid;
1407 * gtk_action_group_set_translation_domain:
1408 * @action_group: a #GtkActionGroup
1409 * @domain: the translation domain to use for g_dgettext() calls
1411 * Sets the translation domain and uses g_dgettext() for translating the
1412 * @label and @tooltip of #GtkActionEntry<!-- -->s added by
1413 * gtk_action_group_add_actions().
1415 * If you're not using gettext() for localization, see
1416 * gtk_action_group_set_translate_func().
1421 gtk_action_group_set_translation_domain (GtkActionGroup *action_group,
1422 const gchar *domain)
1424 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1426 gtk_action_group_set_translate_func (action_group,
1427 (GtkTranslateFunc)dgettext_swapped,
1434 * gtk_action_group_translate_string:
1435 * @action_group: a #GtkActionGroup
1438 * Translates a string using the specified translate_func(). This
1439 * is mainly intended for language bindings.
1441 * Returns: the translation of @string
1446 gtk_action_group_translate_string (GtkActionGroup *action_group,
1447 const gchar *string)
1449 GtkActionGroupPrivate *private;
1450 GtkTranslateFunc translate_func;
1451 gpointer translate_data;
1453 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), string);
1458 private = action_group->priv;
1460 translate_func = private->translate_func;
1461 translate_data = private->translate_data;
1464 return translate_func (string, translate_data);
1469 /* Protected for use by GtkAction */
1471 _gtk_action_group_emit_connect_proxy (GtkActionGroup *action_group,
1475 g_signal_emit (action_group, action_group_signals[CONNECT_PROXY], 0,
1480 _gtk_action_group_emit_disconnect_proxy (GtkActionGroup *action_group,
1484 g_signal_emit (action_group, action_group_signals[DISCONNECT_PROXY], 0,
1489 _gtk_action_group_emit_pre_activate (GtkActionGroup *action_group,
1492 g_signal_emit (action_group, action_group_signals[PRE_ACTIVATE], 0, action);
1496 _gtk_action_group_emit_post_activate (GtkActionGroup *action_group,
1499 g_signal_emit (action_group, action_group_signals[POST_ACTIVATE], 0, action);