2 * Copyright (C) 2008 Tristan Van Berkom <tristan.van.berkom@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * SECTION:gtkactivatable
20 * @Short_Description: An interface for activatable widgets
21 * @Title: GtkActivatable
23 * Activatable widgets can be connected to a #GtkAction and reflects
24 * the state of its action. A #GtkActivatable can also provide feedback
25 * through its action, as they are responsible for activating their
29 * <title>Implementing GtkActivatable</title>
31 * When extending a class that is already #GtkActivatable; it is only
32 * necessary to implement the #GtkActivatable->sync_action_properties()
33 * and #GtkActivatable->update() methods and chain up to the parent
34 * implementation, however when introducing
35 * a new #GtkActivatable class; the #GtkActivatable:related-action and
36 * #GtkActivatable:use-action-appearance properties need to be handled by
37 * the implementor. Handling these properties is mostly a matter of installing
38 * the action pointer and boolean flag on your instance, and calling
39 * gtk_activatable_do_set_related_action() and
40 * gtk_activatable_sync_action_properties() at the appropriate times.
43 * <title>A class fragment implementing #GtkActivatable</title>
44 * <programlisting><![CDATA[
49 * PROP_ACTIVATABLE_RELATED_ACTION,
50 * PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
53 * struct _FooBarPrivate
59 * gboolean use_action_appearance;
64 * static void foo_bar_activatable_interface_init (GtkActivatableIface *iface);
65 * static void foo_bar_activatable_update (GtkActivatable *activatable,
67 * const gchar *property_name);
68 * static void foo_bar_activatable_sync_action_properties (GtkActivatable *activatable,
74 * foo_bar_class_init (FooBarClass *klass)
79 * g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
80 * g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
87 * foo_bar_activatable_interface_init (GtkActivatableIface *iface)
89 * iface->update = foo_bar_activatable_update;
90 * iface->sync_action_properties = foo_bar_activatable_sync_action_properties;
93 * ... Break the reference using gtk_activatable_do_set_related_action()...
96 * foo_bar_dispose (GObject *object)
98 * FooBar *bar = FOO_BAR (object);
99 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
105 * gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (bar), NULL);
106 * priv->action = NULL;
108 * G_OBJECT_CLASS (foo_bar_parent_class)->dispose (object);
111 * ... Handle the "related-action" and "use-action-appearance" properties ...
114 * foo_bar_set_property (GObject *object,
116 * const GValue *value,
119 * FooBar *bar = FOO_BAR (object);
120 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
127 * case PROP_ACTIVATABLE_RELATED_ACTION:
128 * foo_bar_set_related_action (bar, g_value_get_object (value));
130 * case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
131 * foo_bar_set_use_action_appearance (bar, g_value_get_boolean (value));
134 * G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140 * foo_bar_get_property (GObject *object,
145 * FooBar *bar = FOO_BAR (object);
146 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
153 * case PROP_ACTIVATABLE_RELATED_ACTION:
154 * g_value_set_object (value, priv->action);
156 * case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
157 * g_value_set_boolean (value, priv->use_action_appearance);
160 * G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 * foo_bar_set_use_action_appearance (FooBar *bar,
168 * gboolean use_appearance)
170 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
172 * if (priv->use_action_appearance != use_appearance)
174 * priv->use_action_appearance = use_appearance;
176 * gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (bar), priv->action);
180 * ... call gtk_activatable_do_set_related_action() and then assign the action pointer,
181 * no need to reference the action here since gtk_activatable_do_set_related_action() already
182 * holds a reference here for you...
184 * foo_bar_set_related_action (FooBar *bar,
187 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
189 * if (priv->action == action)
192 * gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (bar), action);
194 * priv->action = action;
197 * ... Selectively reset and update activatable depending on the use-action-appearance property ...
199 * gtk_button_activatable_sync_action_properties (GtkActivatable *activatable,
202 * GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
207 * if (gtk_action_is_visible (action))
208 * gtk_widget_show (GTK_WIDGET (activatable));
210 * gtk_widget_hide (GTK_WIDGET (activatable));
212 * gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
216 * if (priv->use_action_appearance)
218 * if (gtk_action_get_stock_id (action))
219 * foo_bar_set_stock (button, gtk_action_get_stock_id (action));
220 * else if (gtk_action_get_label (action))
221 * foo_bar_set_label (button, gtk_action_get_label (action));
229 * foo_bar_activatable_update (GtkActivatable *activatable,
231 * const gchar *property_name)
233 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (activatable);
235 * if (strcmp (property_name, "visible") == 0)
237 * if (gtk_action_is_visible (action))
238 * gtk_widget_show (GTK_WIDGET (activatable));
240 * gtk_widget_hide (GTK_WIDGET (activatable));
242 * else if (strcmp (property_name, "sensitive") == 0)
243 * gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
247 * if (!priv->use_action_appearance)
250 * if (strcmp (property_name, "stock-id") == 0)
251 * foo_bar_set_stock (button, gtk_action_get_stock_id (action));
252 * else if (strcmp (property_name, "label") == 0)
253 * foo_bar_set_label (button, gtk_action_get_label (action));
256 * }]]></programlisting>
262 #include "gtkactivatable.h"
263 #include "gtkactiongroup.h"
264 #include "gtkprivate.h"
268 typedef GtkActivatableIface GtkActivatableInterface;
269 G_DEFINE_INTERFACE (GtkActivatable, gtk_activatable, G_TYPE_OBJECT)
272 gtk_activatable_default_init (GtkActivatableInterface *iface)
275 * GtkActivatable:related-action:
277 * The action that this activatable will activate and receive
278 * updates from for various states and possibly appearance.
280 * <note><para>#GtkActivatable implementors need to handle the this property and
281 * call gtk_activatable_do_set_related_action() when it changes.</para></note>
285 g_object_interface_install_property (iface,
286 g_param_spec_object ("related-action",
287 P_("Related Action"),
288 P_("The action this activatable will activate and receive updates from"),
290 GTK_PARAM_READWRITE));
293 * GtkActivatable:use-action-appearance:
295 * Whether this activatable should reset its layout
296 * and appearance when setting the related action or when
297 * the action changes appearance.
299 * See the #GtkAction documentation directly to find which properties
300 * should be ignored by the #GtkActivatable when this property is %FALSE.
302 * <note><para>#GtkActivatable implementors need to handle this property
303 * and call gtk_activatable_sync_action_properties() on the activatable
304 * widget when it changes.</para></note>
308 g_object_interface_install_property (iface,
309 g_param_spec_boolean ("use-action-appearance",
310 P_("Use Action Appearance"),
311 P_("Whether to use the related actions appearance properties"),
313 GTK_PARAM_READWRITE));
319 gtk_activatable_update (GtkActivatable *activatable,
321 const gchar *property_name)
323 GtkActivatableIface *iface;
325 g_return_if_fail (GTK_IS_ACTIVATABLE (activatable));
327 iface = GTK_ACTIVATABLE_GET_IFACE (activatable);
329 iface->update (activatable, action, property_name);
331 g_critical ("GtkActivatable->update() unimplemented for type %s",
332 g_type_name (G_OBJECT_TYPE (activatable)));
336 * gtk_activatable_sync_action_properties:
337 * @activatable: a #GtkActivatable
338 * @action: (allow-none): the related #GtkAction or %NULL
340 * This is called to update the activatable completely, this is called
341 * internally when the #GtkActivatable:related-action property is set
342 * or unset and by the implementing class when
343 * #GtkActivatable:use-action-appearance changes.
348 gtk_activatable_sync_action_properties (GtkActivatable *activatable,
351 GtkActivatableIface *iface;
353 g_return_if_fail (GTK_IS_ACTIVATABLE (activatable));
355 iface = GTK_ACTIVATABLE_GET_IFACE (activatable);
356 if (iface->sync_action_properties)
357 iface->sync_action_properties (activatable, action);
359 g_critical ("GtkActivatable->sync_action_properties() unimplemented for type %s",
360 g_type_name (G_OBJECT_TYPE (activatable)));
365 * gtk_activatable_set_related_action:
366 * @activatable: a #GtkActivatable
367 * @action: the #GtkAction to set
369 * Sets the related action on the @activatable object.
371 * <note><para>#GtkActivatable implementors need to handle the #GtkActivatable:related-action
372 * property and call gtk_activatable_do_set_related_action() when it changes.</para></note>
377 gtk_activatable_set_related_action (GtkActivatable *activatable,
380 g_return_if_fail (GTK_IS_ACTIVATABLE (activatable));
381 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
383 g_object_set (activatable, "related-action", action, NULL);
387 gtk_activatable_action_notify (GtkAction *action,
389 GtkActivatable *activatable)
391 gtk_activatable_update (activatable, action, pspec->name);
395 * gtk_activatable_do_set_related_action:
396 * @activatable: a #GtkActivatable
397 * @action: the #GtkAction to set
399 * This is a utility function for #GtkActivatable implementors.
401 * When implementing #GtkActivatable you must call this when
402 * handling changes of the #GtkActivatable:related-action, and
403 * you must also use this to break references in #GObject->dispose().
405 * This function adds a reference to the currently set related
406 * action for you, it also makes sure the #GtkActivatable->update()
407 * method is called when the related #GtkAction properties change
408 * and registers to the action's proxy list.
410 * <note><para>Be careful to call this before setting the local
411 * copy of the #GtkAction property, since this function uses
412 * gtk_activatable_get_related_action() to retrieve the
413 * previous action</para></note>
418 gtk_activatable_do_set_related_action (GtkActivatable *activatable,
421 GtkAction *prev_action;
423 prev_action = gtk_activatable_get_related_action (activatable);
425 if (prev_action != action)
429 g_signal_handlers_disconnect_by_func (prev_action, gtk_activatable_action_notify, activatable);
431 /* Check the type so that actions can be activatable too. */
432 if (GTK_IS_WIDGET (activatable))
433 _gtk_action_remove_from_proxy_list (prev_action, GTK_WIDGET (activatable));
435 /* Some apps are using the object data directly...
436 * so continue to set it for a bit longer
438 g_object_set_data (G_OBJECT (activatable), "gtk-action", NULL);
441 * We don't want prev_action to be activated
442 * during the sync_action_properties() call when syncing "active".
444 gtk_action_block_activate (prev_action);
447 /* Some applications rely on their proxy UI to be set up
448 * before they receive the ::connect-proxy signal, so we
449 * need to call sync_action_properties() before add_to_proxy_list().
451 gtk_activatable_sync_action_properties (activatable, action);
455 gtk_action_unblock_activate (prev_action);
456 g_object_unref (prev_action);
461 g_object_ref (action);
463 g_signal_connect (G_OBJECT (action), "notify", G_CALLBACK (gtk_activatable_action_notify), activatable);
465 if (GTK_IS_WIDGET (activatable))
466 _gtk_action_add_to_proxy_list (action, GTK_WIDGET (activatable));
468 g_object_set_data (G_OBJECT (activatable), "gtk-action", action);
474 * gtk_activatable_get_related_action:
475 * @activatable: a #GtkActivatable
477 * Gets the related #GtkAction for @activatable.
479 * Returns: (transfer none): the related #GtkAction if one is set.
484 gtk_activatable_get_related_action (GtkActivatable *activatable)
488 g_return_val_if_fail (GTK_IS_ACTIVATABLE (activatable), NULL);
490 g_object_get (activatable, "related-action", &action, NULL);
492 /* g_object_get() gives us a ref... */
494 g_object_unref (action);
500 * gtk_activatable_set_use_action_appearance:
501 * @activatable: a #GtkActivatable
502 * @use_appearance: whether to use the actions appearance
504 * Sets whether this activatable should reset its layout and appearance
505 * when setting the related action or when the action changes appearance
507 * <note><para>#GtkActivatable implementors need to handle the
508 * #GtkActivatable:use-action-appearance property and call
509 * gtk_activatable_sync_action_properties() to update @activatable
510 * if needed.</para></note>
515 gtk_activatable_set_use_action_appearance (GtkActivatable *activatable,
516 gboolean use_appearance)
518 g_object_set (activatable, "use-action-appearance", use_appearance, NULL);
522 * gtk_activatable_get_use_action_appearance:
523 * @activatable: a #GtkActivatable
525 * Gets whether this activatable should reset its layout
526 * and appearance when setting the related action or when
527 * the action changes appearance.
529 * Returns: whether @activatable uses its actions appearance.
534 gtk_activatable_get_use_action_appearance (GtkActivatable *activatable)
536 gboolean use_appearance;
538 g_object_get (activatable, "use-action-appearance", &use_appearance, NULL);
540 return use_appearance;