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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:gtkactivatable
22 * @Short_Description: An interface for activatable widgets
23 * @Title: GtkActivatable
25 * Activatable widgets can be connected to a #GtkAction and reflects
26 * the state of its action. A #GtkActivatable can also provide feedback
27 * through its action, as they are responsible for activating their
31 * <title>Implementing GtkActivatable</title>
33 * When extending a class that is already #GtkActivatable; it is only
34 * necessary to implement the #GtkActivatable->sync_action_properties()
35 * and #GtkActivatable->update() methods and chain up to the parent
36 * implementation, however when introducing
37 * a new #GtkActivatable class; the #GtkActivatable:related-action and
38 * #GtkActivatable:use-action-appearance properties need to be handled by
39 * the implementor. Handling these properties is mostly a matter of installing
40 * the action pointer and boolean flag on your instance, and calling
41 * gtk_activatable_do_set_related_action() and
42 * gtk_activatable_sync_action_properties() at the appropriate times.
45 * <title>A class fragment implementing #GtkActivatable</title>
46 * <programlisting><![CDATA[
51 * PROP_ACTIVATABLE_RELATED_ACTION,
52 * PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
55 * struct _FooBarPrivate
61 * gboolean use_action_appearance;
66 * static void foo_bar_activatable_interface_init (GtkActivatableIface *iface);
67 * static void foo_bar_activatable_update (GtkActivatable *activatable,
69 * const gchar *property_name);
70 * static void foo_bar_activatable_sync_action_properties (GtkActivatable *activatable,
76 * foo_bar_class_init (FooBarClass *klass)
81 * g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
82 * g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
89 * foo_bar_activatable_interface_init (GtkActivatableIface *iface)
91 * iface->update = foo_bar_activatable_update;
92 * iface->sync_action_properties = foo_bar_activatable_sync_action_properties;
95 * ... Break the reference using gtk_activatable_do_set_related_action()...
98 * foo_bar_dispose (GObject *object)
100 * FooBar *bar = FOO_BAR (object);
101 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
107 * gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (bar), NULL);
108 * priv->action = NULL;
110 * G_OBJECT_CLASS (foo_bar_parent_class)->dispose (object);
113 * ... Handle the "related-action" and "use-action-appearance" properties ...
116 * foo_bar_set_property (GObject *object,
118 * const GValue *value,
121 * FooBar *bar = FOO_BAR (object);
122 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
129 * case PROP_ACTIVATABLE_RELATED_ACTION:
130 * foo_bar_set_related_action (bar, g_value_get_object (value));
132 * case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
133 * foo_bar_set_use_action_appearance (bar, g_value_get_boolean (value));
136 * G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 * foo_bar_get_property (GObject *object,
147 * FooBar *bar = FOO_BAR (object);
148 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
155 * case PROP_ACTIVATABLE_RELATED_ACTION:
156 * g_value_set_object (value, priv->action);
158 * case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
159 * g_value_set_boolean (value, priv->use_action_appearance);
162 * G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169 * foo_bar_set_use_action_appearance (FooBar *bar,
170 * gboolean use_appearance)
172 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
174 * if (priv->use_action_appearance != use_appearance)
176 * priv->use_action_appearance = use_appearance;
178 * gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (bar), priv->action);
182 * ... call gtk_activatable_do_set_related_action() and then assign the action pointer,
183 * no need to reference the action here since gtk_activatable_do_set_related_action() already
184 * holds a reference here for you...
186 * foo_bar_set_related_action (FooBar *bar,
189 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
191 * if (priv->action == action)
194 * gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (bar), action);
196 * priv->action = action;
199 * ... Selectively reset and update activatable depending on the use-action-appearance property ...
201 * gtk_button_activatable_sync_action_properties (GtkActivatable *activatable,
204 * GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
209 * if (gtk_action_is_visible (action))
210 * gtk_widget_show (GTK_WIDGET (activatable));
212 * gtk_widget_hide (GTK_WIDGET (activatable));
214 * gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
218 * if (priv->use_action_appearance)
220 * if (gtk_action_get_stock_id (action))
221 * foo_bar_set_stock (button, gtk_action_get_stock_id (action));
222 * else if (gtk_action_get_label (action))
223 * foo_bar_set_label (button, gtk_action_get_label (action));
231 * foo_bar_activatable_update (GtkActivatable *activatable,
233 * const gchar *property_name)
235 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (activatable);
237 * if (strcmp (property_name, "visible") == 0)
239 * if (gtk_action_is_visible (action))
240 * gtk_widget_show (GTK_WIDGET (activatable));
242 * gtk_widget_hide (GTK_WIDGET (activatable));
244 * else if (strcmp (property_name, "sensitive") == 0)
245 * gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
249 * if (!priv->use_action_appearance)
252 * if (strcmp (property_name, "stock-id") == 0)
253 * foo_bar_set_stock (button, gtk_action_get_stock_id (action));
254 * else if (strcmp (property_name, "label") == 0)
255 * foo_bar_set_label (button, gtk_action_get_label (action));
258 * }]]></programlisting>
264 #include "gtkactivatable.h"
265 #include "gtkactiongroup.h"
266 #include "gtktypeutils.h"
267 #include "gtkprivate.h"
269 #include "gtkalias.h"
272 static void gtk_activatable_class_init (gpointer g_iface);
275 gtk_activatable_get_type (void)
277 static GType activatable_type = 0;
279 if (!activatable_type)
281 g_type_register_static_simple (G_TYPE_INTERFACE, I_("GtkActivatable"),
282 sizeof (GtkActivatableIface),
283 (GClassInitFunc) gtk_activatable_class_init,
286 return activatable_type;
290 gtk_activatable_class_init (gpointer g_iface)
293 * GtkActivatable:related-action:
295 * The action that this activatable will activate and receive
296 * updates from for various states and possibly appearance.
298 * <note><para>#GtkActivatable implementors need to handle the this property and
299 * call gtk_activatable_do_set_related_action() when it changes.</para></note>
303 g_object_interface_install_property (g_iface,
304 g_param_spec_object ("related-action",
305 P_("Related Action"),
306 P_("The action this activatable will activate and receive updates from"),
308 GTK_PARAM_READWRITE));
311 * GtkActivatable:use-action-appearance:
313 * Whether this activatable should reset its layout
314 * and appearance when setting the related action or when
315 * the action changes appearance.
317 * See the #GtkAction documentation directly to find which properties
318 * should be ignored by the #GtkActivatable when this property is %FALSE.
320 * <note><para>#GtkActivatable implementors need to handle this property
321 * and call gtk_activatable_sync_action_properties() on the activatable
322 * widget when it changes.</para></note>
326 g_object_interface_install_property (g_iface,
327 g_param_spec_boolean ("use-action-appearance",
328 P_("Use Action Appearance"),
329 P_("Whether to use the related actions appearance properties"),
331 GTK_PARAM_READWRITE));
337 gtk_activatable_update (GtkActivatable *activatable,
339 const gchar *property_name)
341 GtkActivatableIface *iface;
343 g_return_if_fail (GTK_IS_ACTIVATABLE (activatable));
345 iface = GTK_ACTIVATABLE_GET_IFACE (activatable);
347 iface->update (activatable, action, property_name);
349 g_critical ("GtkActivatable->update() unimplemented for type %s",
350 g_type_name (G_OBJECT_TYPE (activatable)));
354 * gtk_activatable_sync_action_properties:
355 * @activatable: a #GtkActivatable
356 * @action: the related #GtkAction or %NULL
358 * This is called to update the activatable completely, this is called
359 * internally when the #GtkActivatable::related-action property is set
360 * or unset and by the implementing class when
361 * #GtkActivatable::use-action-appearance changes.
366 gtk_activatable_sync_action_properties (GtkActivatable *activatable,
369 GtkActivatableIface *iface;
371 g_return_if_fail (GTK_IS_ACTIVATABLE (activatable));
373 iface = GTK_ACTIVATABLE_GET_IFACE (activatable);
374 if (iface->sync_action_properties)
375 iface->sync_action_properties (activatable, action);
377 g_critical ("GtkActivatable->sync_action_properties() unimplemented for type %s",
378 g_type_name (G_OBJECT_TYPE (activatable)));
383 * gtk_activatable_set_related_action:
384 * @activatable: a #GtkActivatable
385 * @action: the #GtkAction to set
387 * Sets the related action on the @activatable object.
389 * <note><para>#GtkActivatable implementors need to handle the #GtkActivatable:related-action
390 * property and call gtk_activatable_do_set_related_action() when it changes.</para></note>
395 gtk_activatable_set_related_action (GtkActivatable *activatable,
398 g_return_if_fail (GTK_IS_ACTIVATABLE (activatable));
399 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
401 g_object_set (activatable, "related-action", action, NULL);
405 gtk_activatable_action_notify (GtkAction *action,
407 GtkActivatable *activatable)
409 gtk_activatable_update (activatable, action, pspec->name);
413 * gtk_activatable_do_set_related_action:
414 * @activatable: a #GtkActivatable
415 * @action: the #GtkAction to set
417 * This is a utility function for #GtkActivatable implementors.
419 * When implementing #GtkActivatable you must call this when
420 * handling changes of the #GtkActivatable:related-action, and
421 * you must also use this to break references in #GObject->dispose().
423 * This function adds a reference to the currently set related
424 * action for you, it also makes sure the #GtkActivatable->update()
425 * method is called when the related #GtkAction properties change
426 * and registers to the action's proxy list.
428 * <note><para>Be careful to call this before setting the local
429 * copy of the #GtkAction property, since this function uses
430 * gtk_activatable_get_action() to retrieve the previous action</para></note>
435 gtk_activatable_do_set_related_action (GtkActivatable *activatable,
438 GtkAction *prev_action;
440 prev_action = gtk_activatable_get_related_action (activatable);
442 if (prev_action != action)
446 g_signal_handlers_disconnect_by_func (prev_action, gtk_activatable_action_notify, activatable);
448 _gtk_action_remove_from_proxy_list (prev_action, GTK_WIDGET (activatable));
450 /* Some apps are using the object data directly...
451 * so continue to set it for a bit longer
453 g_object_set_data (G_OBJECT (activatable), "gtk-action", NULL);
456 * We don't want prev_action to be activated
457 * during the sync_action_properties() call when syncing "active".
459 gtk_action_block_activate (prev_action);
462 /* Some applications rely on their proxy UI to be set up
463 * before they receive the ::connect-proxy signal, so we
464 * need to call sync_action_properties() before add_to_proxy_list().
466 gtk_activatable_sync_action_properties (activatable, action);
470 gtk_action_unblock_activate (prev_action);
471 g_object_unref (prev_action);
476 g_object_ref (action);
478 g_signal_connect (G_OBJECT (action), "notify", G_CALLBACK (gtk_activatable_action_notify), activatable);
480 _gtk_action_add_to_proxy_list (action, GTK_WIDGET (activatable));
482 g_object_set_data (G_OBJECT (activatable), "gtk-action", action);
488 * gtk_activatable_get_related_action:
489 * @activatable: a #GtkActivatable
491 * Gets the related #GtkAction for @activatable.
493 * Returns: the related #GtkAction if one is set.
498 gtk_activatable_get_related_action (GtkActivatable *activatable)
502 g_return_val_if_fail (GTK_IS_ACTIVATABLE (activatable), NULL);
504 g_object_get (activatable, "related-action", &action, NULL);
506 /* g_object_get() gives us a ref... */
508 g_object_unref (action);
514 * gtk_activatable_set_use_action_appearance:
515 * @activatable: a #GtkActivatable
516 * @use_appearance: whether to use the actions appearance
518 * Sets whether this activatable should reset its layout and appearance
519 * when setting the related action or when the action changes appearance
521 * <note><para>#GtkActivatable implementors need to handle the
522 * #GtkActivatable:use-action-appearance property and call
523 * gtk_activatable_sync_action_properties() to update @activatable
524 * if needed.</para></note>
529 gtk_activatable_set_use_action_appearance (GtkActivatable *activatable,
530 gboolean use_appearance)
532 g_object_set (activatable, "use-action-appearance", use_appearance, NULL);
536 * gtk_activatable_get_use_action_appearance:
537 * @activatable: a #GtkActivatable
539 * Gets whether this activatable should reset its layout
540 * and appearance when setting the related action or when
541 * the action changes appearance.
543 * Returns: whether @activatable uses its actions appearance.
548 gtk_activatable_get_use_action_appearance (GtkActivatable *activatable)
550 gboolean use_appearance;
552 g_object_get (activatable, "use-action-appearance", &use_appearance, NULL);
554 return use_appearance;
557 #define __GTK_ACTIVATABLE_C__
558 #include "gtkaliasdef.c"