2 * Copyright © 2012 Canonical Limited
4 * This library 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, see <http://www.gnu.org/licenses/>.
17 * Authors: Ryan Lortie <desrt@desrt.ca>
22 #include "gtkactionable.h"
24 #include "gtkwidget.h"
28 * SECTION:gtkactionable
29 * @title: GtkActionable
30 * @short_description: An interface for widgets that can be associated
33 * This interface provides a convenient way of associating widgets with
34 * actions on a #GtkApplicationWindow or #GtkApplication.
36 * It primarily consists of two properties: #GtkActionable:action-name
37 * and #GtkActionable:action-target. There are also some convenience APIs
38 * for setting these properties.
40 * This interface is presently only meaningful if used on a widget that
41 * is (or will be) located inside of a #GtkApplicationWindow and can
42 * only be used to associate the widget with actions on that window, or
43 * its associated #GtkApplication.
51 * An opaque pointer type.
55 * GtkActionableInterface:
56 * @get_action_name: virtual pointer for gtk_actionable_get_action_name()
57 * @set_action_name: virtual pointer for gtk_actionable_set_action_name()
58 * @get_action_target_value: virtual pointer for gtk_actionable_get_action_target_value()
59 * @set_action_target_value: virtual pointer for gtk_actionable_set_action_target_value
61 * The interface vtable for #GtkActionable.
64 G_DEFINE_INTERFACE (GtkActionable, gtk_actionable, GTK_TYPE_WIDGET)
67 gtk_actionable_default_init (GtkActionableInterface *iface)
69 g_object_interface_install_property (iface,
70 g_param_spec_string ("action-name", P_("action name"),
71 P_("The name of the associated action, like 'app.quit'"),
72 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
74 g_object_interface_install_property (iface,
75 g_param_spec_variant ("action-target", P_("action target value"),
76 P_("The parameter for action invocations"),
77 G_VARIANT_TYPE_ANY, NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
81 * gtk_actionable_get_action_name:
82 * @actionable: a #GtkActionable widget
84 * Gets the action name for @actionable.
86 * See gtk_actionable_set_action_name() for more information.
88 * Returns: the action name, or %NULL if none is set
93 gtk_actionable_get_action_name (GtkActionable *actionable)
95 g_return_val_if_fail (GTK_IS_ACTIONABLE (actionable), NULL);
97 return GTK_ACTIONABLE_GET_IFACE (actionable)
98 ->get_action_name (actionable);
102 * gtk_actionable_set_action_name:
103 * @actionable: a #GtkActionable widget
104 * @action_name: an action name, or %NULL
106 * Specifies the name of the action with which this widget should be
107 * associated. If @action_name is %NULL then the widget will be
108 * unassociated from any previous action.
110 * Usually this function is used when the widget is located (or will be
111 * located) within the hierarchy of a #GtkApplicationWindow.
113 * Names are of the form "win.save" or "app.quit" for actions on the
114 * containing #GtkApplicationWindow or its associated #GtkApplication,
115 * respectively. This is the same form used for actions in the #GMenu
116 * associated with the window.
121 gtk_actionable_set_action_name (GtkActionable *actionable,
122 const gchar *action_name)
124 g_return_if_fail (GTK_IS_ACTIONABLE (actionable));
126 GTK_ACTIONABLE_GET_IFACE (actionable)
127 ->set_action_name (actionable, action_name);
131 * gtk_actionable_get_action_target_value:
132 * @actionable: a #GtkActionable widget
134 * Gets the current target value of @actionabe.
136 * See gtk_actionable_set_target_value() for more information.
138 * Returns: (transfer none): the current target value
143 gtk_actionable_get_action_target_value (GtkActionable *actionable)
145 g_return_val_if_fail (GTK_IS_ACTIONABLE (actionable), NULL);
147 return GTK_ACTIONABLE_GET_IFACE (actionable)
148 ->get_action_target_value (actionable);
152 * gtk_actionable_set_action_target_value:
153 * @actionable: a #GtkActionable widget
154 * @target_value: a #GVariant to set as the target value, or %NULL
156 * Sets the target value of an actionable widget.
158 * If @target_value is %NULL then the target value is unset.
160 * The target value has two purposes. First, it is used as the
161 * parameter to activation of the action associated with the
162 * #GtkActionable widget. Second, it is used to determine if the widget
163 * should be rendered as "active" - the widget is active if the state
164 * is equal to the given target.
166 * Consider the example of associating a set of buttons with a #GAction
167 * with string state in a typical "radio button" situation. Each button
168 * will be associated with the same action, but with a different target
169 * value for that action. Clicking on a particular button will activate
170 * the action with the target of that button, which will typically cause
171 * the action's state to change to that value. Since the action's state
172 * is now equal to the target value of the button, the button will now
173 * be rendered as active (and the other buttons, with different targets,
174 * rendered inactive).
179 gtk_actionable_set_action_target_value (GtkActionable *actionable,
180 GVariant *target_value)
182 g_return_if_fail (GTK_IS_ACTIONABLE (actionable));
184 GTK_ACTIONABLE_GET_IFACE (actionable)
185 ->set_action_target_value (actionable, target_value);
189 * gtk_actionable_set_action_target:
190 * @actionable: a #GtkActionable widget
191 * @format_string: a GVariant format string
192 * @...: arguments appropriate for @format_string
194 * Sets the target of an actionable widget.
196 * This is a convenience function that calls g_variant_new() for
197 * @format_string and uses the result to call
198 * gtk_actionable_set_action_target_value().
200 * If you are setting a string-valued target and want to set the action
201 * name at the same time, you can use
202 * gtk_actionable_set_detailed_action_name ().
207 gtk_actionable_set_action_target (GtkActionable *actionable,
208 const gchar *format_string,
213 va_start (ap, format_string);
214 gtk_actionable_set_action_target_value (actionable, g_variant_new_va (format_string, NULL, &ap));
219 * gtk_actionable_set_detailed_action_name:
220 * @actionable: a #GtkActionable widget
221 * @detailed_action_name: the detailed action name
223 * Sets the action-name and associated string target value of an
226 * This allows for the effect of both gtk_actionable_set_action_name()
227 * and gtk_actionable_set_target() in the common case that the target is
230 * @detailed_action_name is a string of the form
231 * <literal>"action::target"</literal> where <literal>action</literal>
232 * is the action name and <literal>target</literal> is the string to use
238 gtk_actionable_set_detailed_action_name (GtkActionable *actionable,
239 const gchar *detailed_action_name)
243 g_return_if_fail (GTK_IS_ACTIONABLE (actionable));
245 if (detailed_action_name == NULL)
247 gtk_actionable_set_action_name (actionable, NULL);
248 gtk_actionable_set_action_target_value (actionable, NULL);
252 parts = g_strsplit (detailed_action_name, "::", 2);
253 gtk_actionable_set_action_name (actionable, parts[0]);
254 if (parts[0] && parts[1])
255 gtk_actionable_set_action_target (actionable, "s", parts[1]);
257 gtk_actionable_set_action_target_value (actionable, NULL);