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
113 GtkTranslateFunc translate_func;
114 gpointer translate_data;
115 GDestroyNotify translate_notify;
135 static void gtk_action_group_init (GtkActionGroup *self);
136 static void gtk_action_group_class_init (GtkActionGroupClass *class);
137 static void gtk_action_group_finalize (GObject *object);
138 static void gtk_action_group_set_property (GObject *object,
142 static void gtk_action_group_get_property (GObject *object,
146 static GtkAction *gtk_action_group_real_get_action (GtkActionGroup *self,
150 static void gtk_action_group_buildable_init (GtkBuildableIface *iface);
151 static void gtk_action_group_buildable_add_child (GtkBuildable *buildable,
155 static void gtk_action_group_buildable_set_name (GtkBuildable *buildable,
157 static const gchar* gtk_action_group_buildable_get_name (GtkBuildable *buildable);
158 static gboolean gtk_action_group_buildable_custom_tag_start (GtkBuildable *buildable,
161 const gchar *tagname,
162 GMarkupParser *parser,
164 static void gtk_action_group_buildable_custom_tag_end (GtkBuildable *buildable,
167 const gchar *tagname,
168 gpointer *user_data);
171 gtk_action_group_get_type (void)
173 static GType type = 0;
177 const GTypeInfo type_info =
179 sizeof (GtkActionGroupClass),
180 NULL, /* base_init */
181 NULL, /* base_finalize */
182 (GClassInitFunc) gtk_action_group_class_init,
183 NULL, /* class_finalize */
184 NULL, /* class_data */
185 sizeof (GtkActionGroup),
187 (GInstanceInitFunc) gtk_action_group_init,
190 const GInterfaceInfo buildable_info =
192 (GInterfaceInitFunc) gtk_action_group_buildable_init,
197 type = g_type_register_static (G_TYPE_OBJECT, I_("GtkActionGroup"),
200 g_type_add_interface_static (type,
207 static GObjectClass *parent_class = NULL;
208 static guint action_group_signals[LAST_SIGNAL] = { 0 };
211 gtk_action_group_class_init (GtkActionGroupClass *klass)
213 GObjectClass *gobject_class;
215 gobject_class = G_OBJECT_CLASS (klass);
216 parent_class = g_type_class_peek_parent (klass);
218 gobject_class->finalize = gtk_action_group_finalize;
219 gobject_class->set_property = gtk_action_group_set_property;
220 gobject_class->get_property = gtk_action_group_get_property;
221 klass->get_action = gtk_action_group_real_get_action;
223 g_object_class_install_property (gobject_class,
225 g_param_spec_string ("name",
227 P_("A name for the action group."),
229 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
230 g_object_class_install_property (gobject_class,
232 g_param_spec_boolean ("sensitive",
234 P_("Whether the action group is enabled."),
236 GTK_PARAM_READWRITE));
237 g_object_class_install_property (gobject_class,
239 g_param_spec_boolean ("visible",
241 P_("Whether the action group is visible."),
243 GTK_PARAM_READWRITE));
246 * GtkActionGroup::connect-proxy:
247 * @action_group: the group
248 * @action: the action
251 * The ::connect-proxy signal is emitted after connecting a proxy to
252 * an action in the group. Note that the proxy may have been connected
253 * to a different action before.
255 * This is intended for simple customizations for which a custom action
256 * class would be too clumsy, e.g. showing tooltips for menuitems in the
259 * #GtkUIManager proxies the signal and provides global notification
260 * just before any action is connected to a proxy, which is probably more
265 action_group_signals[CONNECT_PROXY] =
266 g_signal_new (I_("connect-proxy"),
267 G_OBJECT_CLASS_TYPE (klass),
269 _gtk_marshal_VOID__OBJECT_OBJECT,
271 GTK_TYPE_ACTION, GTK_TYPE_WIDGET);
274 * GtkActionGroup::disconnect-proxy:
275 * @action_group: the group
276 * @action: the action
279 * The ::disconnect-proxy signal is emitted after disconnecting a proxy
280 * from an action in the group.
282 * #GtkUIManager proxies the signal and provides global notification
283 * just before any action is connected to a proxy, which is probably more
288 action_group_signals[DISCONNECT_PROXY] =
289 g_signal_new (I_("disconnect-proxy"),
290 G_OBJECT_CLASS_TYPE (klass),
292 _gtk_marshal_VOID__OBJECT_OBJECT,
294 GTK_TYPE_ACTION, GTK_TYPE_WIDGET);
297 * GtkActionGroup::pre-activate:
298 * @action_group: the group
299 * @action: the action
301 * The ::pre-activate signal is emitted just before the @action in the
302 * @action_group is activated
304 * This is intended for #GtkUIManager to proxy the signal and provide global
305 * notification just before any action is activated.
309 action_group_signals[PRE_ACTIVATE] =
310 g_signal_new (I_("pre-activate"),
311 G_OBJECT_CLASS_TYPE (klass),
313 _gtk_marshal_VOID__OBJECT,
318 * GtkActionGroup::post-activate:
319 * @action_group: the group
320 * @action: the action
322 * The ::post-activate signal is emitted just after the @action in the
323 * @action_group is activated
325 * This is intended for #GtkUIManager to proxy the signal and provide global
326 * notification just after any action is activated.
330 action_group_signals[POST_ACTIVATE] =
331 g_signal_new (I_("post-activate"),
332 G_OBJECT_CLASS_TYPE (klass),
334 _gtk_marshal_VOID__OBJECT,
338 g_type_class_add_private (gobject_class, sizeof (GtkActionGroupPrivate));
343 remove_action (GtkAction *action)
345 g_object_set (action, I_("action-group"), NULL, NULL);
346 g_object_unref (action);
350 gtk_action_group_init (GtkActionGroup *action_group)
352 GtkActionGroupPrivate *private;
354 action_group->priv = G_TYPE_INSTANCE_GET_PRIVATE (action_group,
355 GTK_TYPE_ACTION_GROUP,
356 GtkActionGroupPrivate);
357 private = action_group->priv;
359 private->name = NULL;
360 private->sensitive = TRUE;
361 private->visible = TRUE;
362 private->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
364 (GDestroyNotify) remove_action);
365 private->translate_func = NULL;
366 private->translate_data = NULL;
367 private->translate_notify = NULL;
371 gtk_action_group_buildable_init (GtkBuildableIface *iface)
373 iface->add_child = gtk_action_group_buildable_add_child;
374 iface->set_name = gtk_action_group_buildable_set_name;
375 iface->get_name = gtk_action_group_buildable_get_name;
376 iface->custom_tag_start = gtk_action_group_buildable_custom_tag_start;
377 iface->custom_tag_end = gtk_action_group_buildable_custom_tag_end;
381 gtk_action_group_buildable_add_child (GtkBuildable *buildable,
386 gtk_action_group_add_action_with_accel (GTK_ACTION_GROUP (buildable),
387 GTK_ACTION (child), NULL);
391 gtk_action_group_buildable_set_name (GtkBuildable *buildable,
394 GtkActionGroup *self = GTK_ACTION_GROUP (buildable);
395 GtkActionGroupPrivate *private = self->priv;
397 private->name = g_strdup (name);
401 gtk_action_group_buildable_get_name (GtkBuildable *buildable)
403 GtkActionGroup *self = GTK_ACTION_GROUP (buildable);
404 GtkActionGroupPrivate *private = self->priv;
406 return private->name;
412 GdkModifierType modifiers;
413 } AcceleratorParserData;
416 accelerator_start_element (GMarkupParseContext *context,
417 const gchar *element_name,
419 const gchar **values,
425 GdkModifierType modifiers = 0;
426 AcceleratorParserData *parser_data = (AcceleratorParserData*)user_data;
428 if (strcmp (element_name, "accelerator") != 0)
429 g_warning ("Unknown <accelerator> tag: %s", element_name);
431 for (i = 0; names[i]; i++)
433 if (strcmp (names[i], "key") == 0)
434 key = gdk_keyval_from_name (values[i]);
435 else if (strcmp (names[i], "modifiers") == 0)
437 if (!_gtk_builder_flags_from_string (GDK_TYPE_MODIFIER_TYPE,
447 g_warning ("<accelerator> requires a key attribute");
450 parser_data->key = key;
451 parser_data->modifiers = modifiers;
454 static const GMarkupParser accelerator_parser =
456 accelerator_start_element
460 gtk_action_group_buildable_custom_tag_start (GtkBuildable *buildable,
463 const gchar *tagname,
464 GMarkupParser *parser,
467 AcceleratorParserData *parser_data;
469 if (child && strcmp (tagname, "accelerator") == 0)
471 parser_data = g_slice_new0 (AcceleratorParserData);
472 parser_data->child = child;
473 *user_data = parser_data;
474 *parser = accelerator_parser;
482 gtk_action_group_buildable_custom_tag_end (GtkBuildable *buildable,
485 const gchar *tagname,
488 AcceleratorParserData *data;
490 if (strcmp (tagname, "accelerator") == 0)
492 GtkActionGroup *action_group;
493 GtkActionGroupPrivate *private;
497 data = (AcceleratorParserData*)user_data;
498 action_group = GTK_ACTION_GROUP (buildable);
499 private = action_group->priv;
500 action = GTK_ACTION (child);
502 accel_path = g_strconcat ("<Actions>/",
504 gtk_action_get_name (action), NULL);
506 if (gtk_accel_map_lookup_entry (accel_path, NULL))
507 gtk_accel_map_change_entry (accel_path, data->key, data->modifiers, TRUE);
509 gtk_accel_map_add_entry (accel_path, data->key, data->modifiers);
511 gtk_action_set_accel_path (action, accel_path);
514 g_slice_free (AcceleratorParserData, data);
519 * gtk_action_group_new:
520 * @name: the name of the action group.
522 * Creates a new #GtkActionGroup object. The name of the action group
523 * is used when associating <link linkend="Action-Accel">keybindings</link>
526 * Returns: the new #GtkActionGroup
531 gtk_action_group_new (const gchar *name)
533 GtkActionGroup *self;
534 GtkActionGroupPrivate *private;
536 self = g_object_new (GTK_TYPE_ACTION_GROUP, NULL);
537 private = self->priv;
538 private->name = g_strdup (name);
544 gtk_action_group_finalize (GObject *object)
546 GtkActionGroup *self;
547 GtkActionGroupPrivate *private;
549 self = GTK_ACTION_GROUP (object);
550 private = self->priv;
552 g_free (private->name);
553 private->name = NULL;
555 g_hash_table_destroy (private->actions);
556 private->actions = NULL;
558 if (private->translate_notify)
559 private->translate_notify (private->translate_data);
561 parent_class->finalize (object);
565 gtk_action_group_set_property (GObject *object,
570 GtkActionGroup *self;
571 GtkActionGroupPrivate *private;
574 self = GTK_ACTION_GROUP (object);
575 private = self->priv;
581 private->name = g_value_dup_string (value);
585 gtk_action_group_set_sensitive (self, g_value_get_boolean (value));
588 gtk_action_group_set_visible (self, g_value_get_boolean (value));
591 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
597 gtk_action_group_get_property (GObject *object,
602 GtkActionGroup *self;
603 GtkActionGroupPrivate *private;
605 self = GTK_ACTION_GROUP (object);
606 private = self->priv;
611 g_value_set_string (value, private->name);
614 g_value_set_boolean (value, private->sensitive);
617 g_value_set_boolean (value, private->visible);
620 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
626 gtk_action_group_real_get_action (GtkActionGroup *self,
627 const gchar *action_name)
629 GtkActionGroupPrivate *private;
631 private = self->priv;
633 return g_hash_table_lookup (private->actions, action_name);
637 * gtk_action_group_get_name:
638 * @action_group: the action group
640 * Gets the name of the action group.
642 * Returns: the name of the action group.
647 gtk_action_group_get_name (GtkActionGroup *action_group)
649 GtkActionGroupPrivate *private;
651 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
653 private = action_group->priv;
655 return private->name;
659 * gtk_action_group_get_sensitive:
660 * @action_group: the action group
662 * Returns %TRUE if the group is sensitive. The constituent actions
663 * can only be logically sensitive (see gtk_action_is_sensitive()) if
664 * they are sensitive (see gtk_action_get_sensitive()) and their group
667 * Return value: %TRUE if the group is sensitive.
672 gtk_action_group_get_sensitive (GtkActionGroup *action_group)
674 GtkActionGroupPrivate *private;
676 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
678 private = action_group->priv;
680 return private->sensitive;
684 cb_set_action_sensitivity (const gchar *name,
687 /* Minor optimization, the action_groups state only affects actions
688 * that are themselves sensitive */
689 g_object_notify (G_OBJECT (action), "sensitive");
694 * gtk_action_group_set_sensitive:
695 * @action_group: the action group
696 * @sensitive: new sensitivity
698 * Changes the sensitivity of @action_group
703 gtk_action_group_set_sensitive (GtkActionGroup *action_group,
706 GtkActionGroupPrivate *private;
708 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
710 private = action_group->priv;
711 sensitive = sensitive != FALSE;
713 if (private->sensitive != sensitive)
715 private->sensitive = sensitive;
716 g_hash_table_foreach (private->actions,
717 (GHFunc) cb_set_action_sensitivity, NULL);
719 g_object_notify (G_OBJECT (action_group), "sensitive");
724 * gtk_action_group_get_visible:
725 * @action_group: the action group
727 * Returns %TRUE if the group is visible. The constituent actions
728 * can only be logically visible (see gtk_action_is_visible()) if
729 * they are visible (see gtk_action_get_visible()) and their group
732 * Return value: %TRUE if the group is visible.
737 gtk_action_group_get_visible (GtkActionGroup *action_group)
739 GtkActionGroupPrivate *private;
741 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), FALSE);
743 private = action_group->priv;
745 return private->visible;
749 cb_set_action_visiblity (const gchar *name,
752 /* Minor optimization, the action_groups state only affects actions
753 * that are themselves visible */
754 g_object_notify (G_OBJECT (action), "visible");
758 * gtk_action_group_set_visible:
759 * @action_group: the action group
760 * @visible: new visiblity
762 * Changes the visible of @action_group.
767 gtk_action_group_set_visible (GtkActionGroup *action_group,
770 GtkActionGroupPrivate *private;
772 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
774 private = action_group->priv;
775 visible = visible != FALSE;
777 if (private->visible != visible)
779 private->visible = visible;
780 g_hash_table_foreach (private->actions,
781 (GHFunc) cb_set_action_visiblity, NULL);
783 g_object_notify (G_OBJECT (action_group), "visible");
788 * gtk_action_group_get_action:
789 * @action_group: the action group
790 * @action_name: the name of the action
792 * Looks up an action in the action group by name.
794 * Returns: (transfer none): the action, or %NULL if no action by that name exists
799 gtk_action_group_get_action (GtkActionGroup *action_group,
800 const gchar *action_name)
802 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
803 g_return_val_if_fail (GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action != NULL, NULL);
805 return GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action (action_group,
810 check_unique_action (GtkActionGroup *action_group,
811 const gchar *action_name)
813 if (gtk_action_group_get_action (action_group, action_name) != NULL)
815 GtkActionGroupPrivate *private;
817 private = action_group->priv;
819 g_warning ("Refusing to add non-unique action '%s' to action group '%s'",
829 * gtk_action_group_add_action:
830 * @action_group: the action group
833 * Adds an action object to the action group. Note that this function
834 * does not set up the accel path of the action, which can lead to problems
835 * if a user tries to modify the accelerator of a menuitem associated with
836 * the action. Therefore you must either set the accel path yourself with
837 * gtk_action_set_accel_path(), or use
838 * <literal>gtk_action_group_add_action_with_accel (..., NULL)</literal>.
843 gtk_action_group_add_action (GtkActionGroup *action_group,
846 GtkActionGroupPrivate *private;
849 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
850 g_return_if_fail (GTK_IS_ACTION (action));
852 name = gtk_action_get_name (action);
853 g_return_if_fail (name != NULL);
855 if (!check_unique_action (action_group, name))
858 private = action_group->priv;
860 g_hash_table_insert (private->actions,
862 g_object_ref (action));
863 g_object_set (action, I_("action-group"), action_group, NULL);
867 * gtk_action_group_add_action_with_accel:
868 * @action_group: the action group
869 * @action: the action to add
870 * @accelerator: (allow-none): the accelerator for the action, in
871 * the format understood by gtk_accelerator_parse(), or "" for no accelerator, or
872 * %NULL to use the stock accelerator
874 * Adds an action object to the action group and sets up the accelerator.
876 * If @accelerator is %NULL, attempts to use the accelerator associated
877 * with the stock_id of the action.
879 * Accel paths are set to
880 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
885 gtk_action_group_add_action_with_accel (GtkActionGroup *action_group,
887 const gchar *accelerator)
889 GtkActionGroupPrivate *private;
892 GdkModifierType accel_mods;
895 name = gtk_action_get_name (action);
896 if (!check_unique_action (action_group, name))
899 private = action_group->priv;
900 accel_path = g_strconcat ("<Actions>/",
901 private->name, "/", name, NULL);
905 if (accelerator[0] == 0)
909 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
911 g_warning ("Unable to parse accelerator '%s' for action '%s'",
918 GtkStockItem stock_item;
920 g_object_get (action, "stock-id", &stock_id, NULL);
922 if (stock_id && gtk_stock_lookup (stock_id, &stock_item))
924 accel_key = stock_item.keyval;
925 accel_mods = stock_item.modifier;
932 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
934 gtk_action_set_accel_path (action, accel_path);
935 gtk_action_group_add_action (action_group, action);
941 * gtk_action_group_remove_action:
942 * @action_group: the action group
945 * Removes an action object from the action group.
950 gtk_action_group_remove_action (GtkActionGroup *action_group,
953 GtkActionGroupPrivate *private;
956 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
957 g_return_if_fail (GTK_IS_ACTION (action));
959 name = gtk_action_get_name (action);
960 g_return_if_fail (name != NULL);
962 private = action_group->priv;
964 g_hash_table_remove (private->actions, name);
968 add_single_action (gpointer key,
972 GList **list = user_data;
974 *list = g_list_prepend (*list, value);
978 * gtk_action_group_list_actions:
979 * @action_group: the action group
981 * Lists the actions in the action group.
983 * Returns: (element-type GtkAction) (transfer container): an allocated list of the action objects in the action group
988 gtk_action_group_list_actions (GtkActionGroup *action_group)
990 GtkActionGroupPrivate *private;
991 GList *actions = NULL;
993 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
995 private = action_group->priv;
997 g_hash_table_foreach (private->actions, add_single_action, &actions);
999 return g_list_reverse (actions);
1004 * gtk_action_group_add_actions: (skip)
1005 * @action_group: the action group
1006 * @entries: (array length=n_entries): an array of action descriptions
1007 * @n_entries: the number of entries
1008 * @user_data: data to pass to the action callbacks
1010 * This is a convenience function to create a number of actions and add them
1011 * to the action group.
1013 * The "activate" signals of the actions are connected to the callbacks and
1014 * their accel paths are set to
1015 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
1020 gtk_action_group_add_actions (GtkActionGroup *action_group,
1021 const GtkActionEntry *entries,
1025 gtk_action_group_add_actions_full (action_group,
1030 typedef struct _SharedData SharedData;
1032 struct _SharedData {
1035 GDestroyNotify destroy;
1039 shared_data_unref (gpointer data)
1041 SharedData *shared_data = (SharedData *)data;
1043 shared_data->ref_count--;
1044 if (shared_data->ref_count == 0)
1046 if (shared_data->destroy)
1047 shared_data->destroy (shared_data->data);
1049 g_slice_free (SharedData, shared_data);
1055 * gtk_action_group_add_actions_full: (skip)
1056 * @action_group: the action group
1057 * @entries: (array length=n_entries): an array of action descriptions
1058 * @n_entries: the number of entries
1059 * @user_data: data to pass to the action callbacks
1060 * @destroy: destroy notification callback for @user_data
1062 * This variant of gtk_action_group_add_actions() adds a #GDestroyNotify
1063 * callback for @user_data.
1068 gtk_action_group_add_actions_full (GtkActionGroup *action_group,
1069 const GtkActionEntry *entries,
1072 GDestroyNotify destroy)
1075 /* Keep this in sync with the other
1076 * gtk_action_group_add_..._actions_full() functions.
1079 SharedData *shared_data;
1081 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1083 shared_data = g_slice_new0 (SharedData);
1084 shared_data->ref_count = 1;
1085 shared_data->data = user_data;
1086 shared_data->destroy = destroy;
1088 for (i = 0; i < n_entries; i++)
1092 const gchar *tooltip;
1094 if (!check_unique_action (action_group, entries[i].name))
1097 label = gtk_action_group_translate_string (action_group, entries[i].label);
1098 tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1100 action = gtk_action_new (entries[i].name,
1105 if (entries[i].stock_id)
1107 g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1108 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
1109 entries[i].stock_id))
1110 g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1113 if (entries[i].callback)
1117 closure = g_cclosure_new (entries[i].callback, user_data, NULL);
1118 g_closure_add_finalize_notifier (closure, shared_data,
1119 (GClosureNotify)shared_data_unref);
1120 shared_data->ref_count++;
1122 g_signal_connect_closure (action, "activate", closure, FALSE);
1125 gtk_action_group_add_action_with_accel (action_group,
1127 entries[i].accelerator);
1128 g_object_unref (action);
1131 shared_data_unref (shared_data);
1135 * gtk_action_group_add_toggle_actions: (skip)
1136 * @action_group: the action group
1137 * @entries: (array length=n_entries): an array of toggle action descriptions
1138 * @n_entries: the number of entries
1139 * @user_data: data to pass to the action callbacks
1141 * This is a convenience function to create a number of toggle actions and add them
1142 * to the action group.
1144 * The "activate" signals of the actions are connected to the callbacks and
1145 * their accel paths are set to
1146 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
1151 gtk_action_group_add_toggle_actions (GtkActionGroup *action_group,
1152 const GtkToggleActionEntry *entries,
1156 gtk_action_group_add_toggle_actions_full (action_group,
1163 * gtk_action_group_add_toggle_actions_full: (skip)
1164 * @action_group: the action group
1165 * @entries: (array length=n_entries): an array of toggle action descriptions
1166 * @n_entries: the number of entries
1167 * @user_data: data to pass to the action callbacks
1168 * @destroy: destroy notification callback for @user_data
1170 * This variant of gtk_action_group_add_toggle_actions() adds a
1171 * #GDestroyNotify callback for @user_data.
1176 gtk_action_group_add_toggle_actions_full (GtkActionGroup *action_group,
1177 const GtkToggleActionEntry *entries,
1180 GDestroyNotify destroy)
1182 /* Keep this in sync with the other
1183 * gtk_action_group_add_..._actions_full() functions.
1186 SharedData *shared_data;
1188 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1190 shared_data = g_slice_new0 (SharedData);
1191 shared_data->ref_count = 1;
1192 shared_data->data = user_data;
1193 shared_data->destroy = destroy;
1195 for (i = 0; i < n_entries; i++)
1197 GtkToggleAction *action;
1199 const gchar *tooltip;
1201 if (!check_unique_action (action_group, entries[i].name))
1204 label = gtk_action_group_translate_string (action_group, entries[i].label);
1205 tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1207 action = gtk_toggle_action_new (entries[i].name,
1212 if (entries[i].stock_id)
1214 if (gtk_icon_factory_lookup_default (entries[i].stock_id))
1215 g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1217 g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1220 gtk_toggle_action_set_active (action, entries[i].is_active);
1222 if (entries[i].callback)
1226 closure = g_cclosure_new (entries[i].callback, user_data, NULL);
1227 g_closure_add_finalize_notifier (closure, shared_data,
1228 (GClosureNotify)shared_data_unref);
1229 shared_data->ref_count++;
1231 g_signal_connect_closure (action, "activate", closure, FALSE);
1234 gtk_action_group_add_action_with_accel (action_group,
1235 GTK_ACTION (action),
1236 entries[i].accelerator);
1237 g_object_unref (action);
1240 shared_data_unref (shared_data);
1244 * gtk_action_group_add_radio_actions: (skip)
1245 * @action_group: the action group
1246 * @entries: (array length=n_entries): an array of radio action descriptions
1247 * @n_entries: the number of entries
1248 * @value: the value of the action to activate initially, or -1 if
1249 * no action should be activated
1250 * @on_change: the callback to connect to the changed signal
1251 * @user_data: data to pass to the action callbacks
1253 * This is a convenience routine to create a group of radio actions and
1254 * add them to the action group.
1256 * The "changed" signal of the first radio action is connected to the
1257 * @on_change callback and the accel paths of the actions are set to
1258 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
1263 gtk_action_group_add_radio_actions (GtkActionGroup *action_group,
1264 const GtkRadioActionEntry *entries,
1267 GCallback on_change,
1270 gtk_action_group_add_radio_actions_full (action_group,
1273 on_change, user_data, NULL);
1277 * gtk_action_group_add_radio_actions_full: (skip)
1278 * @action_group: the action group
1279 * @entries: (array length=n_entries): an array of radio action descriptions
1280 * @n_entries: the number of entries
1281 * @value: the value of the action to activate initially, or -1 if
1282 * no action should be activated
1283 * @on_change: the callback to connect to the changed signal
1284 * @user_data: data to pass to the action callbacks
1285 * @destroy: destroy notification callback for @user_data
1287 * This variant of gtk_action_group_add_radio_actions() adds a
1288 * #GDestroyNotify callback for @user_data.
1293 gtk_action_group_add_radio_actions_full (GtkActionGroup *action_group,
1294 const GtkRadioActionEntry *entries,
1297 GCallback on_change,
1299 GDestroyNotify destroy)
1301 /* Keep this in sync with the other
1302 * gtk_action_group_add_..._actions_full() functions.
1305 GSList *group = NULL;
1306 GtkRadioAction *first_action = NULL;
1308 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1310 for (i = 0; i < n_entries; i++)
1312 GtkRadioAction *action;
1314 const gchar *tooltip;
1316 if (!check_unique_action (action_group, entries[i].name))
1319 label = gtk_action_group_translate_string (action_group, entries[i].label);
1320 tooltip = gtk_action_group_translate_string (action_group, entries[i].tooltip);
1322 action = gtk_radio_action_new (entries[i].name,
1328 if (entries[i].stock_id)
1330 if (gtk_icon_factory_lookup_default (entries[i].stock_id))
1331 g_object_set (action, "stock-id", entries[i].stock_id, NULL);
1333 g_object_set (action, "icon-name", entries[i].stock_id, NULL);
1337 first_action = action;
1339 gtk_radio_action_set_group (action, group);
1340 group = gtk_radio_action_get_group (action);
1342 if (value == entries[i].value)
1343 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
1345 gtk_action_group_add_action_with_accel (action_group,
1346 GTK_ACTION (action),
1347 entries[i].accelerator);
1348 g_object_unref (action);
1351 if (on_change && first_action)
1352 g_signal_connect_data (first_action, "changed",
1353 on_change, user_data,
1354 (GClosureNotify)destroy, 0);
1358 * gtk_action_group_set_translate_func:
1359 * @action_group: a #GtkActionGroup
1360 * @func: a #GtkTranslateFunc
1361 * @data: data to be passed to @func and @notify
1362 * @notify: a #GDestroyNotify function to be called when @action_group is
1363 * destroyed and when the translation function is changed again
1365 * Sets a function to be used for translating the @label and @tooltip of
1366 * #GtkActionGroupEntry<!-- -->s added by gtk_action_group_add_actions().
1368 * If you're using gettext(), it is enough to set the translation domain
1369 * with gtk_action_group_set_translation_domain().
1374 gtk_action_group_set_translate_func (GtkActionGroup *action_group,
1375 GtkTranslateFunc func,
1377 GDestroyNotify notify)
1379 GtkActionGroupPrivate *private;
1381 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1383 private = action_group->priv;
1385 if (private->translate_notify)
1386 private->translate_notify (private->translate_data);
1388 private->translate_func = func;
1389 private->translate_data = data;
1390 private->translate_notify = notify;
1394 dgettext_swapped (const gchar *msgid,
1395 const gchar *domainname)
1397 /* Pass through g_dgettext if and only if msgid is nonempty. */
1398 if (msgid && *msgid)
1399 return (gchar*) g_dgettext (domainname, msgid);
1401 return (gchar*) msgid;
1405 * gtk_action_group_set_translation_domain:
1406 * @action_group: a #GtkActionGroup
1407 * @domain: (allow-none): the translation domain to use for g_dgettext()
1408 * calls, or %NULL to use the domain set with textdomain()
1410 * Sets the translation domain and uses g_dgettext() for translating the
1411 * @label and @tooltip of #GtkActionEntry<!-- -->s added by
1412 * gtk_action_group_add_actions().
1414 * If you're not using gettext() for localization, see
1415 * gtk_action_group_set_translate_func().
1420 gtk_action_group_set_translation_domain (GtkActionGroup *action_group,
1421 const gchar *domain)
1423 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
1425 gtk_action_group_set_translate_func (action_group,
1426 (GtkTranslateFunc)dgettext_swapped,
1433 * gtk_action_group_translate_string:
1434 * @action_group: a #GtkActionGroup
1437 * Translates a string using the specified translate_func(). This
1438 * is mainly intended for language bindings.
1440 * Returns: the translation of @string
1445 gtk_action_group_translate_string (GtkActionGroup *action_group,
1446 const gchar *string)
1448 GtkActionGroupPrivate *private;
1449 GtkTranslateFunc translate_func;
1450 gpointer translate_data;
1452 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), string);
1457 private = action_group->priv;
1459 translate_func = private->translate_func;
1460 translate_data = private->translate_data;
1463 return translate_func (string, translate_data);
1468 /* Protected for use by GtkAction */
1470 _gtk_action_group_emit_connect_proxy (GtkActionGroup *action_group,
1474 g_signal_emit (action_group, action_group_signals[CONNECT_PROXY], 0,
1479 _gtk_action_group_emit_disconnect_proxy (GtkActionGroup *action_group,
1483 g_signal_emit (action_group, action_group_signals[DISCONNECT_PROXY], 0,
1488 _gtk_action_group_emit_pre_activate (GtkActionGroup *action_group,
1491 g_signal_emit (action_group, action_group_signals[PRE_ACTIVATE], 0, action);
1495 _gtk_action_group_emit_post_activate (GtkActionGroup *action_group,
1498 g_signal_emit (action_group, action_group_signals[POST_ACTIVATE], 0, action);