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/.
33 #include "gtkactiongroup.h"
35 #include "gtktoggleaction.h"
36 #include "gtkradioaction.h"
37 #include "gtkaccelmap.h"
40 #define GTK_ACTION_GROUP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ACTION_GROUP, GtkActionGroupPrivate))
42 struct _GtkActionGroupPrivate
47 GtkTranslateFunc translate_func;
48 gpointer translate_data;
49 GtkDestroyNotify translate_notify;
58 static void gtk_action_group_init (GtkActionGroup *self);
59 static void gtk_action_group_class_init (GtkActionGroupClass *class);
60 static void gtk_action_group_finalize (GObject *object);
61 static void gtk_action_group_set_property (GObject *object,
65 static void gtk_action_group_get_property (GObject *object,
69 static GtkAction *gtk_action_group_real_get_action (GtkActionGroup *self,
74 gtk_action_group_get_type (void)
76 static GType type = 0;
80 static const GTypeInfo type_info =
82 sizeof (GtkActionGroupClass),
84 NULL, /* base_finalize */
85 (GClassInitFunc) gtk_action_group_class_init,
86 NULL, /* class_finalize */
87 NULL, /* class_data */
88 sizeof (GtkActionGroup),
90 (GInstanceInitFunc) gtk_action_group_init,
93 type = g_type_register_static (G_TYPE_OBJECT, "GtkActionGroup",
100 static GObjectClass *parent_class = NULL;
103 gtk_action_group_class_init (GtkActionGroupClass *klass)
105 GObjectClass *gobject_class;
107 gobject_class = G_OBJECT_CLASS (klass);
108 parent_class = g_type_class_peek_parent (klass);
110 gobject_class->finalize = gtk_action_group_finalize;
111 gobject_class->set_property = gtk_action_group_set_property;
112 gobject_class->get_property = gtk_action_group_get_property;
113 klass->get_action = gtk_action_group_real_get_action;
115 g_object_class_install_property (gobject_class,
117 g_param_spec_string ("name",
119 _("A name for the action group."),
122 G_PARAM_CONSTRUCT_ONLY));
123 g_type_class_add_private (gobject_class, sizeof (GtkActionGroupPrivate));
127 gtk_action_group_init (GtkActionGroup *self)
129 self->private_data = GTK_ACTION_GROUP_GET_PRIVATE (self);
130 self->private_data->name = NULL;
131 self->private_data->actions = g_hash_table_new_full (g_str_hash, g_str_equal,
132 (GDestroyNotify) g_free,
133 (GDestroyNotify) g_object_unref);
134 self->private_data->translate_func = NULL;
135 self->private_data->translate_data = NULL;
136 self->private_data->translate_notify = NULL;
140 * gtk_action_group_new:
141 * @name: the name of the action group.
143 * Creates a new #GtkActionGroup object. The name of the action group
144 * is used when associating <link linkend="Action-Accel">keybindings</link>
147 * Returns: the new #GtkActionGroup
152 gtk_action_group_new (const gchar *name)
154 GtkActionGroup *self;
156 self = g_object_new (GTK_TYPE_ACTION_GROUP, NULL);
157 self->private_data->name = g_strdup (name);
163 gtk_action_group_finalize (GObject *object)
165 GtkActionGroup *self;
167 self = GTK_ACTION_GROUP (object);
169 g_free (self->private_data->name);
170 self->private_data->name = NULL;
172 g_hash_table_destroy (self->private_data->actions);
173 self->private_data->actions = NULL;
175 if (self->private_data->translate_notify)
176 self->private_data->translate_notify (self->private_data->translate_data);
178 if (parent_class->finalize)
179 (* parent_class->finalize) (object);
183 gtk_action_group_set_property (GObject *object,
188 GtkActionGroup *self;
191 self = GTK_ACTION_GROUP (object);
196 tmp = self->private_data->name;
197 self->private_data->name = g_value_dup_string (value);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 gtk_action_group_get_property (GObject *object,
212 GtkActionGroup *self;
214 self = GTK_ACTION_GROUP (object);
219 g_value_set_string (value, self->private_data->name);
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228 gtk_action_group_real_get_action (GtkActionGroup *self,
229 const gchar *action_name)
231 return g_hash_table_lookup (self->private_data->actions, action_name);
235 * gtk_action_group_get_name:
236 * @action_group: the action group
238 * Gets the name of the action group.
240 * Returns: the name of the action group.
245 gtk_action_group_get_name (GtkActionGroup *action_group)
247 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
249 return action_group->private_data->name;
253 * gtk_action_group_get_action:
254 * @action_group: the action group
255 * @action_name: the name of the action
257 * Looks up an action in the action group by name.
259 * Returns: the action, or %NULL if no action by that name exists
264 gtk_action_group_get_action (GtkActionGroup *action_group,
265 const gchar *action_name)
267 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
268 g_return_val_if_fail (GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action != NULL, NULL);
270 return (* GTK_ACTION_GROUP_GET_CLASS (action_group)->get_action)
271 (action_group, action_name);
275 * gtk_action_group_add_action:
276 * @action_group: the action group
279 * Adds an action object to the action group.
284 gtk_action_group_add_action (GtkActionGroup *action_group,
287 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
288 g_return_if_fail (GTK_IS_ACTION (action));
289 g_return_if_fail (gtk_action_get_name (action) != NULL);
291 g_hash_table_insert (action_group->private_data->actions,
292 g_strdup (gtk_action_get_name (action)),
293 g_object_ref (action));
297 * gtk_action_group_remove_action:
298 * @action_group: the action group
301 * Removes an action object from the action group.
306 gtk_action_group_remove_action (GtkActionGroup *action_group,
309 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
310 g_return_if_fail (GTK_IS_ACTION (action));
311 g_return_if_fail (gtk_action_get_name (action) != NULL);
313 /* extra protection to make sure action->name is valid */
314 g_object_ref (action);
315 g_hash_table_remove (action_group->private_data->actions, gtk_action_get_name (action));
316 g_object_unref (action);
320 add_single_action (gpointer key,
324 GList **list = user_data;
326 *list = g_list_prepend (*list, value);
330 * gtk_action_group_list_actions:
331 * @action_group: the action group
333 * Lists the actions in the action group.
335 * Returns: an allocated list of the action objects in the action group
340 gtk_action_group_list_actions (GtkActionGroup *action_group)
342 GList *actions = NULL;
343 g_return_val_if_fail (GTK_IS_ACTION_GROUP (action_group), NULL);
345 g_hash_table_foreach (action_group->private_data->actions, add_single_action, &actions);
347 return g_list_reverse (actions);
352 * gtk_action_group_add_actions:
353 * @action_group: the action group
354 * @entries: an array of action descriptions
355 * @n_entries: the number of entries
356 * @user_data: data to pass to the action callbacks
358 * This is a convenience function to create a number of actions and add them
359 * to the action group.
361 * The "activate" signals of the actions are connected to the callbacks and
362 * their accel paths are set to
363 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
368 gtk_action_group_add_actions (GtkActionGroup *action_group,
369 GtkActionEntry *entries,
373 gtk_action_group_add_actions_full (action_group,
380 * gtk_action_group_add_actions_full:
381 * @action_group: the action group
382 * @entries: an array of action descriptions
383 * @n_entries: the number of entries
384 * @user_data: data to pass to the action callbacks
385 * @destroy: destroy notification callback for @user_data
387 * This variant of gtk_action_group_add_actions() adds a #GDestroyNotify
388 * callback for @user_data.
393 gtk_action_group_add_actions_full (GtkActionGroup *action_group,
394 GtkActionEntry *entries,
397 GDestroyNotify destroy)
400 /* Keep this in sync with the other
401 * gtk_action_group_add_..._actions_full() functions.
404 GtkTranslateFunc translate_func;
405 gpointer translate_data;
407 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
409 translate_func = action_group->private_data->translate_func;
410 translate_data = action_group->private_data->translate_data;
412 for (i = 0; i < n_entries; i++)
417 GdkModifierType accel_mods;
418 GtkStockItem stock_item;
424 label = translate_func (entries[i].label, translate_data);
425 tooltip = translate_func (entries[i].tooltip, translate_data);
429 label = entries[i].label;
430 tooltip = entries[i].tooltip;
433 action = g_object_new (GTK_TYPE_ACTION,
434 "name", entries[i].name,
437 "stock_id", entries[i].stock_id,
440 if (entries[i].callback)
441 g_signal_connect_data (action, "activate",
443 user_data, (GClosureNotify)destroy, 0);
445 /* set the accel path for the menu item */
446 accel_path = g_strconcat ("<Actions>/", action_group->private_data->name, "/",
447 entries[i].name, NULL);
449 if (entries[i].accelerator)
450 gtk_accelerator_parse (entries[i].accelerator, &accel_key, &accel_mods);
451 else if (entries[i].stock_id &&
452 gtk_stock_lookup (entries[i].stock_id, &stock_item))
454 accel_key = stock_item.keyval;
455 accel_mods = stock_item.modifier;
459 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
461 gtk_action_set_accel_path (action, accel_path);
464 gtk_action_group_add_action (action_group, action);
465 g_object_unref (action);
470 * gtk_action_group_add_toggle_actions:
471 * @action_group: the action group
472 * @entries: an array of toggle action descriptions
473 * @n_entries: the number of entries
474 * @user_data: data to pass to the action callbacks
476 * This is a convenience function to create a number of toggle actions and add them
477 * to the action group.
479 * The "activate" signals of the actions are connected to the callbacks and
480 * their accel paths are set to
481 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
486 gtk_action_group_add_toggle_actions (GtkActionGroup *action_group,
487 GtkToggleActionEntry *entries,
491 gtk_action_group_add_toggle_actions_full (action_group,
498 * gtk_action_group_add_toggle_actions_full:
499 * @action_group: the action group
500 * @entries: an array of toggle action descriptions
501 * @n_entries: the number of entries
502 * @user_data: data to pass to the action callbacks
503 * @destroy: destroy notification callback for @user_data
505 * This variant of gtk_action_group_add_toggle_actions() adds a
506 * #GDestroyNotify callback for @user_data.
511 gtk_action_group_add_toggle_actions_full (GtkActionGroup *action_group,
512 GtkToggleActionEntry *entries,
515 GDestroyNotify destroy)
517 /* Keep this in sync with the other
518 * gtk_action_group_add_..._actions_full() functions.
521 GtkTranslateFunc translate_func;
522 gpointer translate_data;
524 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
526 translate_func = action_group->private_data->translate_func;
527 translate_data = action_group->private_data->translate_data;
529 for (i = 0; i < n_entries; i++)
534 GdkModifierType accel_mods;
535 GtkStockItem stock_item;
541 label = translate_func (entries[i].label, translate_data);
542 tooltip = translate_func (entries[i].tooltip, translate_data);
546 label = entries[i].label;
547 tooltip = entries[i].tooltip;
550 action = g_object_new (GTK_TYPE_TOGGLE_ACTION,
551 "name", entries[i].name,
554 "stock_id", entries[i].stock_id,
557 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
558 entries[i].is_active);
560 if (entries[i].callback)
561 g_signal_connect_data (action, "activate",
563 user_data, (GClosureNotify)destroy, 0);
565 /* set the accel path for the menu item */
566 accel_path = g_strconcat ("<Actions>/", action_group->private_data->name, "/",
567 entries[i].name, NULL);
569 if (entries[i].accelerator)
570 gtk_accelerator_parse (entries[i].accelerator, &accel_key, &accel_mods);
571 else if (entries[i].stock_id &&
572 gtk_stock_lookup (entries[i].stock_id, &stock_item))
574 accel_key = stock_item.keyval;
575 accel_mods = stock_item.modifier;
579 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
581 gtk_action_set_accel_path (action, accel_path);
584 gtk_action_group_add_action (action_group, action);
585 g_object_unref (action);
590 * gtk_action_group_add_radio_actions:
591 * @action_group: the action group
592 * @entries: an array of radio action descriptions
593 * @n_entries: the number of entries
594 * @value: the value of the action to activate initially, or -1 if
595 * no action should be activated
596 * @on_change: the callback to connect to the changed signal
597 * @user_data: data to pass to the action callbacks
599 * This is a convenience routine to create a group of radio actions and
600 * add them to the action group.
602 * The "changed" signal of the first radio action is connected to the
603 * @on_change callback and the accel paths of the actions are set to
604 * <literal><Actions>/<replaceable>group-name</replaceable>/<replaceable>action-name</replaceable></literal>.
609 gtk_action_group_add_radio_actions (GtkActionGroup *action_group,
610 GtkRadioActionEntry *entries,
616 gtk_action_group_add_radio_actions_full (action_group,
619 on_change, user_data, NULL);
623 * gtk_action_group_add_radio_actions_full:
624 * @action_group: the action group
625 * @entries: an array of radio action descriptions
626 * @n_entries: the number of entries
627 * @value: the value of the action to activate initially, or -1 if
628 * no action should be activated
629 * @on_change: the callback to connect to the changed signal
630 * @user_data: data to pass to the action callbacks
631 * @destroy: destroy notification callback for @user_data
633 * This variant of gtk_action_group_add_radio_actions() adds a
634 * #GDestroyNotify callback for @user_data.
639 gtk_action_group_add_radio_actions_full (GtkActionGroup *action_group,
640 GtkRadioActionEntry *entries,
645 GDestroyNotify destroy)
647 /* Keep this in sync with the other
648 * gtk_action_group_add_..._actions_full() functions.
651 GtkTranslateFunc translate_func;
652 gpointer translate_data;
653 GSList *group = NULL;
654 GtkAction *first_action = NULL;
656 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
658 translate_func = action_group->private_data->translate_func;
659 translate_data = action_group->private_data->translate_data;
661 for (i = 0; i < n_entries; i++)
666 GdkModifierType accel_mods;
667 GtkStockItem stock_item;
673 label = translate_func (entries[i].label, translate_data);
674 tooltip = translate_func (entries[i].tooltip, translate_data);
678 label = entries[i].label;
679 tooltip = entries[i].tooltip;
682 action = g_object_new (GTK_TYPE_RADIO_ACTION,
683 "name", entries[i].name,
686 "stock_id", entries[i].stock_id,
687 "value", entries[i].value,
691 first_action = action;
693 gtk_radio_action_set_group (GTK_RADIO_ACTION (action), group);
694 group = gtk_radio_action_get_group (GTK_RADIO_ACTION (action));
696 if (value == entries[i].value)
697 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
699 /* set the accel path for the menu item */
700 accel_path = g_strconcat ("<Actions>/",
701 action_group->private_data->name, "/",
702 entries[i].name, NULL);
704 if (entries[i].accelerator)
705 gtk_accelerator_parse (entries[i].accelerator, &accel_key, &accel_mods);
706 else if (entries[i].stock_id &&
707 gtk_stock_lookup (entries[i].stock_id, &stock_item))
709 accel_key = stock_item.keyval;
710 accel_mods = stock_item.modifier;
714 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
716 gtk_action_set_accel_path (action, accel_path);
719 gtk_action_group_add_action (action_group, action);
720 g_object_unref (action);
723 if (on_change && first_action)
724 g_signal_connect_data (first_action, "changed",
725 on_change, user_data,
726 (GClosureNotify)destroy, 0);
730 * gtk_action_group_set_translate_func:
731 * @action_group: a #GtkActionGroup
732 * @func: a #GtkTranslateFunc
733 * @data: data to be passed to @func and @notify
734 * @notify: a #GtkDestroyNotify function to be called when @action_group is
735 * destroyed and when the translation function is changed again
737 * Sets a function to be used for translating the @label and @tooltip of
738 * #GtkActionGroupEntry<!-- -->s added by gtk_action_group_add_actions().
740 * If you're using gettext(), it is enough to set the translation domain
741 * with gtk_action_group_set_translation_domain().
746 gtk_action_group_set_translate_func (GtkActionGroup *action_group,
747 GtkTranslateFunc func,
749 GtkDestroyNotify notify)
751 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
753 if (action_group->private_data->translate_notify)
754 action_group->private_data->translate_notify (action_group->private_data->translate_data);
756 action_group->private_data->translate_func = func;
757 action_group->private_data->translate_data = data;
758 action_group->private_data->translate_notify = notify;
762 dgettext_swapped (const gchar *msgid,
763 const gchar *domainname)
765 return dgettext (domainname, msgid);
769 * gtk_action_group_set_translation_domain:
770 * @action_group: a #GtkActionGroup
771 * @domain: the translation domain to use for dgettext() calls
773 * Sets the translation domain and uses dgettext() for translating the
774 * @label and @tooltip of #GtkActionEntry<!-- -->s added by
775 * gtk_action_group_add_actions().
777 * If you're not using gettext() for localization, see
778 * gtk_action_group_set_translate_func().
783 gtk_action_group_set_translation_domain (GtkActionGroup *action_group,
786 g_return_if_fail (GTK_IS_ACTION_GROUP (action_group));
788 gtk_action_group_set_translate_func (action_group,
789 (GtkTranslateFunc)dgettext_swapped,