]> Pileus Git - ~andy/gtk/blob - gtk/gtkactionable.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkactionable.c
1 /*
2  * Copyright © 2012 Canonical Limited
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Authors: Ryan Lortie <desrt@desrt.ca>
18  */
19
20 #include "config.h"
21
22 #include "gtkactionable.h"
23
24 #include "gtkwidget.h"
25 #include "gtkintl.h"
26
27 /**
28  * SECTION:gtkactionable
29  * @title: GtkActionable
30  * @short_description: An interface for widgets that can be associated
31  *     with actions
32  *
33  * This interface provides a convenient way of associating widgets with
34  * actions on a #GtkApplicationWindow or #GtkApplication.
35  *
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.
39  *
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.
44  *
45  * Since: 3.4
46  **/
47
48 /**
49  * GtkActionable:
50  *
51  * An opaque pointer type.
52  **/
53
54 /**
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
60  *
61  * The interface vtable for #GtkActionable.
62  **/
63
64 G_DEFINE_INTERFACE (GtkActionable, gtk_actionable, GTK_TYPE_WIDGET)
65
66 static void
67 gtk_actionable_default_init (GtkActionableInterface *iface)
68 {
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));
73
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));
78 }
79
80 /**
81  * gtk_actionable_get_action_name:
82  * @actionable: a #GtkActionable widget
83  *
84  * Gets the action name for @actionable.
85  *
86  * See gtk_actionable_set_action_name() for more information.
87  *
88  * Returns: the action name, or %NULL if none is set
89  *
90  * Since: 3.4
91  **/
92 const gchar *
93 gtk_actionable_get_action_name (GtkActionable *actionable)
94 {
95   g_return_val_if_fail (GTK_IS_ACTIONABLE (actionable), NULL);
96
97   return GTK_ACTIONABLE_GET_IFACE (actionable)
98     ->get_action_name (actionable);
99 }
100
101 /**
102  * gtk_actionable_set_action_name:
103  * @actionable: a #GtkActionable widget
104  * @action_name: an action name, or %NULL
105  *
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.
109  *
110  * Usually this function is used when the widget is located (or will be
111  * located) within the hierarchy of a #GtkApplicationWindow.
112  *
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.
117  *
118  * Since: 3.4
119  **/
120 void
121 gtk_actionable_set_action_name (GtkActionable *actionable,
122                                 const gchar   *action_name)
123 {
124   g_return_if_fail (GTK_IS_ACTIONABLE (actionable));
125
126   GTK_ACTIONABLE_GET_IFACE (actionable)
127     ->set_action_name (actionable, action_name);
128 }
129
130 /**
131  * gtk_actionable_get_action_target_value:
132  * @actionable: a #GtkActionable widget
133  *
134  * Gets the current target value of @actionabe.
135  *
136  * See gtk_actionable_set_action_target_value() for more information.
137  *
138  * Returns: (transfer none): the current target value
139  *
140  * Since: 3.4
141  **/
142 GVariant *
143 gtk_actionable_get_action_target_value (GtkActionable *actionable)
144 {
145   g_return_val_if_fail (GTK_IS_ACTIONABLE (actionable), NULL);
146
147   return GTK_ACTIONABLE_GET_IFACE (actionable)
148     ->get_action_target_value (actionable);
149 }
150
151 /**
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
155  *
156  * Sets the target value of an actionable widget.
157  *
158  * If @target_value is %NULL then the target value is unset.
159  *
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.
165  *
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).
175  *
176  * Since: 3.4
177  **/
178 void
179 gtk_actionable_set_action_target_value (GtkActionable *actionable,
180                                         GVariant      *target_value)
181 {
182   g_return_if_fail (GTK_IS_ACTIONABLE (actionable));
183
184   GTK_ACTIONABLE_GET_IFACE (actionable)
185     ->set_action_target_value (actionable, target_value);
186 }
187
188 /**
189  * gtk_actionable_set_action_target:
190  * @actionable: a #GtkActionable widget
191  * @format_string: a GVariant format string
192  * @...: arguments appropriate for @format_string
193  *
194  * Sets the target of an actionable widget.
195  *
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().
199  *
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 ().
203  *
204  * Since: 3.4
205  **/
206 void
207 gtk_actionable_set_action_target (GtkActionable *actionable,
208                                   const gchar   *format_string,
209                                   ...)
210 {
211   va_list ap;
212
213   va_start (ap, format_string);
214   gtk_actionable_set_action_target_value (actionable, g_variant_new_va (format_string, NULL, &ap));
215   va_end (ap);
216 }
217
218 /**
219  * gtk_actionable_set_detailed_action_name:
220  * @actionable: a #GtkActionable widget
221  * @detailed_action_name: the detailed action name
222  *
223  * Sets the action-name and associated string target value of an
224  * actionable widget.
225  *
226  * This allows for the effect of both gtk_actionable_set_action_name()
227  * and gtk_actionable_set_action_target_value() in the common case that
228  * the target is string-valued.
229  *
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
233  * as the target.
234  *
235  * Since: 3.4
236  **/
237 void
238 gtk_actionable_set_detailed_action_name (GtkActionable *actionable,
239                                          const gchar   *detailed_action_name)
240 {
241   gchar **parts;
242
243   g_return_if_fail (GTK_IS_ACTIONABLE (actionable));
244
245   if (detailed_action_name == NULL)
246     {
247       gtk_actionable_set_action_name (actionable, NULL);
248       gtk_actionable_set_action_target_value (actionable, NULL);
249       return;
250     }
251
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]);
256   else
257     gtk_actionable_set_action_target_value (actionable, NULL);
258   g_strfreev (parts);
259 }