2 * Copyright © 2012 Canonical Limited
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * licence or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
19 * Authors: Ryan Lortie <desrt@desrt.ca>
24 #include "gtkactionable.h"
26 #include "gtkwidget.h"
30 * SECTION:gtkactionable
31 * @title: GtkActionable
32 * @short_description: An interface for widgets that can be associated
35 * This interface provides a convenient way of associating widgets with
36 * actions on a #GtkApplicationWindow or #GtkApplication.
38 * It primarily consists of two properties: #GtkActionable:action-name
39 * and #GtkActionable:action-target. There are also some convenience APIs
40 * for setting these properties.
42 * This interface is presently only meaningful if used on a widget that
43 * is (or will be) located inside of a #GtkApplicationWindow and can
44 * only be used to associate the widget with actions on that window, or
45 * its associated #GtkApplication.
53 * An opaque pointer type.
57 * GtkActionableInterface:
58 * @get_action_name: virtual pointer for gtk_actionable_get_action_name()
59 * @set_action_name: virtual pointer for gtk_actionable_set_action_name()
60 * @get_action_target_value: virtual pointer for gtk_actionable_get_action_target_value()
61 * @set_action_target_value: virtual pointer for gtk_actionable_set_action_target_value
63 * The interface vtable for #GtkActionable.
66 G_DEFINE_INTERFACE (GtkActionable, gtk_actionable, GTK_TYPE_WIDGET)
69 gtk_actionable_default_init (GtkActionableInterface *iface)
71 g_object_interface_install_property (iface,
72 g_param_spec_string ("action-name", P_("action name"),
73 P_("The name of the associated action, like 'app.quit'"),
74 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
76 g_object_interface_install_property (iface,
77 g_param_spec_variant ("action-target", P_("action target value"),
78 P_("The parameter for action invocations"),
79 G_VARIANT_TYPE_ANY, NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
83 * gtk_actionable_get_action_name:
84 * @actionable: a #GtkActionable widget
86 * Gets the action name for @actionable.
88 * See gtk_actionable_set_action_name() for more information.
90 * Returns: the action name, or %NULL if none is set
95 gtk_actionable_get_action_name (GtkActionable *actionable)
97 g_return_val_if_fail (GTK_IS_ACTIONABLE (actionable), NULL);
99 return GTK_ACTIONABLE_GET_IFACE (actionable)
100 ->get_action_name (actionable);
104 * gtk_actionable_set_action_name:
105 * @actionable: a #GtkActionable widget
106 * @action_name: an action name, or %NULL
108 * Specifies the name of the action with which this widget should be
109 * associated. If @action_name is %NULL then the widget will be
110 * unassociated from any previous action.
112 * Usually this function is used when the widget is located (or will be
113 * located) within the hierarchy of a #GtkApplicationWindow.
115 * Names are of the form "win.save" or "app.quit" for actions on the
116 * containing #GtkApplicationWindow or its associated #GtkApplication,
117 * respectively. This is the same form used for actions in the #GMenu
118 * associated with the window.
123 gtk_actionable_set_action_name (GtkActionable *actionable,
124 const gchar *action_name)
126 g_return_if_fail (GTK_IS_ACTIONABLE (actionable));
128 GTK_ACTIONABLE_GET_IFACE (actionable)
129 ->set_action_name (actionable, action_name);
133 * gtk_actionable_get_action_target_value:
134 * @actionable: a #GtkActionable widget
136 * Gets the current target value of @actionabe.
138 * See gtk_actionable_set_target_value() for more information.
140 * Returns: (transfer none): the current target value
145 gtk_actionable_get_action_target_value (GtkActionable *actionable)
147 g_return_val_if_fail (GTK_IS_ACTIONABLE (actionable), NULL);
149 return GTK_ACTIONABLE_GET_IFACE (actionable)
150 ->get_action_target_value (actionable);
154 * gtk_actionable_set_action_target_value:
155 * @actionable: a #GtkActionable widget
156 * @target_value: a #GVariant to set as the target value, or %NULL
158 * Sets the target value of an actionable widget.
160 * If @target_value is %NULL then the target value is unset.
162 * The target value has two purposes. First, it is used as the
163 * parameter to activation of the action associated with the
164 * #GtkActionable widget. Second, it is used to determine if the widget
165 * should be rendered as "active" - the widget is active if the state
166 * is equal to the given target.
168 * Consider the example of associating a set of buttons with a #GAction
169 * with string state in a typical "radio button" situation. Each button
170 * will be associated with the same action, but with a different target
171 * value for that action. Clicking on a particular button will activate
172 * the action with the target of that button, which will typically cause
173 * the action's state to change to that value. Since the action's state
174 * is now equal to the target value of the button, the button will now
175 * be rendered as active (and the other buttons, with different targets,
176 * rendered inactive).
181 gtk_actionable_set_action_target_value (GtkActionable *actionable,
182 GVariant *target_value)
184 g_return_if_fail (GTK_IS_ACTIONABLE (actionable));
186 GTK_ACTIONABLE_GET_IFACE (actionable)
187 ->set_action_target_value (actionable, target_value);
191 * gtk_actionable_set_action_target:
192 * @actionable: a #GtkActionable widget
193 * @format_string: a GVariant format string
194 * @...: arguments appropriate for @format_string
196 * Sets the target of an actionable widget.
198 * This is a convenience function that calls g_variant_new() for
199 * @format_string and uses the result to call
200 * gtk_actionable_set_action_target_value().
202 * If you are setting a string-valued target and want to set the action
203 * name at the same time, you can use
204 * gtk_actionable_set_detailed_action_name ().
209 gtk_actionable_set_action_target (GtkActionable *actionable,
210 const gchar *format_string,
215 va_start (ap, format_string);
216 gtk_actionable_set_action_target_value (actionable, g_variant_new_va (format_string, NULL, &ap));
221 * gtk_actionable_set_detailed_action_name:
222 * @actionable: a #GtkActionable widget
223 * @detailed_action_name: the detailed action name
225 * Sets the action-name and associated string target value of an
228 * This allows for the effect of both gtk_actionable_set_action_name()
229 * and gtk_actionable_set_target() in the common case that the target is
232 * @detailed_action_name is a string of the form
233 * <literal>"action::target"</literal> where <literal>action</literal>
234 * is the action name and <literal>target</literal> is the string to use
240 gtk_actionable_set_detailed_action_name (GtkActionable *actionable,
241 const gchar *detailed_action_name)
245 g_return_if_fail (GTK_IS_ACTIONABLE (actionable));
247 if (detailed_action_name == NULL)
249 gtk_actionable_set_action_name (actionable, NULL);
250 gtk_actionable_set_action_target_value (actionable, NULL);
254 parts = g_strsplit (detailed_action_name, "::", 2);
255 gtk_actionable_set_action_name (actionable, parts[0]);
256 if (parts[0] && parts[1])
257 gtk_actionable_set_action_target (actionable, "s", parts[1]);
259 gtk_actionable_set_action_target_value (actionable, NULL);