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
24 * Activatable widgets can be connected to a #GtkAction and reflects
25 * the state of its action. A #GtkActivatable can also provide feedback
26 * through its action, as they are responsible for activating their
30 * <title>Implementing GtkActivatable</title>
32 * When extending a class that is already #GtkActivatable; it is only
33 * necessary to implement the #GtkActivatable->sync_action_properties()
34 * and #GtkActivatable->update() methods and chain up to the parent
35 * implementation, however when introducing
36 * a new #GtkActivatable class; the #GtkActivatable:related-action and
37 * #GtkActivatable:use-action-appearance properties need to be handled by
38 * the implementor. Handling these properties is mostly a matter of installing
39 * the action pointer and boolean flag on your instance, and calling
40 * gtk_activatable_do_set_related_action() and
41 * gtk_activatable_sync_action_properties() at the appropriate times.
44 * <title>A class fragment implementing #GtkActivatable</title>
45 * <programlisting><![CDATA[
50 * PROP_ACTIVATABLE_RELATED_ACTION,
51 * PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
54 * struct _FooBarPrivate
60 * gboolean use_action_appearance;
65 * static void foo_bar_activatable_interface_init (GtkActivatableIface *iface);
66 * static void foo_bar_activatable_update (GtkActivatable *activatable,
68 * const gchar *property_name);
69 * static void foo_bar_activatable_sync_action_properties (GtkActivatable *activatable,
75 * foo_bar_class_init (FooBarClass *klass)
80 * g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
81 * g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
88 * foo_bar_activatable_interface_init (GtkActivatableIface *iface)
90 * iface->update = foo_bar_activatable_update;
91 * iface->sync_action_properties = foo_bar_activatable_sync_action_properties;
94 * ... Break the reference using gtk_activatable_do_set_related_action()...
97 * foo_bar_dispose (GObject *object)
99 * FooBar *bar = FOO_BAR (object);
100 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
106 * gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (bar), NULL);
107 * priv->action = NULL;
109 * G_OBJECT_CLASS (foo_bar_parent_class)->dispose (object);
112 * ... Handle the "related-action" and "use-action-appearance" properties ...
115 * foo_bar_set_property (GObject *object,
117 * const GValue *value,
120 * FooBar *bar = FOO_BAR (object);
121 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
128 * case PROP_ACTIVATABLE_RELATED_ACTION:
129 * foo_bar_set_related_action (bar, g_value_get_object (value));
131 * case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
132 * foo_bar_set_use_action_appearance (bar, g_value_get_boolean (value));
135 * G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141 * foo_bar_get_property (GObject *object,
146 * FooBar *bar = FOO_BAR (object);
147 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
154 * case PROP_ACTIVATABLE_RELATED_ACTION:
155 * g_value_set_object (value, priv->action);
157 * case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
158 * g_value_set_boolean (value, priv->use_action_appearance);
161 * G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168 * foo_bar_set_use_action_appearance (FooBar *bar,
169 * gboolean use_appearance)
171 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
173 * if (priv->use_action_appearance != use_appearance)
175 * priv->use_action_appearance = use_appearance;
177 * gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (bar), priv->action);
181 * ... call gtk_activatable_do_set_related_action() and then assign the action pointer,
182 * no need to reference the action here since gtk_activatable_do_set_related_action() already
183 * holds a reference here for you...
185 * foo_bar_set_related_action (FooBar *bar,
188 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
190 * if (priv->action == action)
193 * gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (bar), action);
195 * priv->action = action;
198 * ... Selectively reset and update activatable depending on the use-action-appearance property ...
200 * gtk_button_activatable_sync_action_properties (GtkActivatable *activatable,
203 * GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
208 * if (gtk_action_is_visible (action))
209 * gtk_widget_show (GTK_WIDGET (activatable));
211 * gtk_widget_hide (GTK_WIDGET (activatable));
213 * gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
217 * if (priv->use_action_appearance)
219 * if (gtk_action_get_stock_id (action))
220 * foo_bar_set_stock (button, gtk_action_get_stock_id (action));
221 * else if (gtk_action_get_label (action))
222 * foo_bar_set_label (button, gtk_action_get_label (action));
230 * foo_bar_activatable_update (GtkActivatable *activatable,
232 * const gchar *property_name)
234 * FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (activatable);
236 * if (strcmp (property_name, "visible") == 0)
238 * if (gtk_action_is_visible (action))
239 * gtk_widget_show (GTK_WIDGET (activatable));
241 * gtk_widget_hide (GTK_WIDGET (activatable));
243 * else if (strcmp (property_name, "sensitive") == 0)
244 * gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
248 * if (!priv->use_action_appearance)
251 * if (strcmp (property_name, "stock-id") == 0)
252 * foo_bar_set_stock (button, gtk_action_get_stock_id (action));
253 * else if (strcmp (property_name, "label") == 0)
254 * foo_bar_set_label (button, gtk_action_get_label (action));
257 * }]]></programlisting>
263 #include "gtkactivatable.h"
264 #include "gtkactiongroup.h"
265 #include "gtktypeutils.h"
266 #include "gtkprivate.h"
268 #include "gtkalias.h"
271 static void gtk_activatable_class_init (gpointer g_iface);
274 gtk_activatable_get_type (void)
276 static GType activatable_type = 0;
278 if (!activatable_type)
280 g_type_register_static_simple (G_TYPE_INTERFACE, I_("GtkActivatable"),
281 sizeof (GtkActivatableIface),
282 (GClassInitFunc) gtk_activatable_class_init,
285 return activatable_type;
289 gtk_activatable_class_init (gpointer g_iface)
292 * GtkActivatable:related-action:
294 * The action that this activatable will activate and receive
295 * updates from for various states and possibly appearance.
297 * <note><para>#GtkActivatable implementors need to handle the this property and
298 * call gtk_activatable_do_set_related_action() when it changes.</para></note>
302 g_object_interface_install_property (g_iface,
303 g_param_spec_object ("related-action",
304 P_("Related Action"),
305 P_("The action this activatable will activate and receive updates from"),
307 GTK_PARAM_READWRITE));
310 * GtkActivatable:use-action-appearance:
312 * Whether this activatable should reset its layout
313 * and appearance when setting the related action or when
314 * the action changes appearance.
316 * See the #GtkAction documentation directly to find which properties
317 * should be ignored by the #GtkActivatable when this property is %FALSE.
319 * <note><para>#GtkActivatable implementors need to handle this property
320 * and call gtk_activatable_sync_action_properties() on the activatable
321 * widget when it changes.</para></note>
325 g_object_interface_install_property (g_iface,
326 g_param_spec_boolean ("use-action-appearance",
327 P_("Use Action Appearance"),
328 P_("Whether to use the related actions appearance properties"),
330 GTK_PARAM_READWRITE));
336 gtk_activatable_update (GtkActivatable *activatable,
338 const gchar *property_name)
340 GtkActivatableIface *iface;
342 g_return_if_fail (GTK_IS_ACTIVATABLE (activatable));
344 iface = GTK_ACTIVATABLE_GET_IFACE (activatable);
346 iface->update (activatable, action, property_name);
348 g_critical ("GtkActivatable->update() unimplemented for type %s",
349 g_type_name (G_OBJECT_TYPE (activatable)));
353 * gtk_activatable_sync_action_properties:
354 * @activatable: a #GtkActivatable
355 * @action: the related #GtkAction or %NULL
357 * This is called to update the activatable completely, this is called
358 * internally when the #GtkActivatable::related-action property is set
359 * or unset and by the implementing class when
360 * #GtkActivatable::use-action-appearance changes.
365 gtk_activatable_sync_action_properties (GtkActivatable *activatable,
368 GtkActivatableIface *iface;
370 g_return_if_fail (GTK_IS_ACTIVATABLE (activatable));
372 iface = GTK_ACTIVATABLE_GET_IFACE (activatable);
373 if (iface->sync_action_properties)
374 iface->sync_action_properties (activatable, action);
376 g_critical ("GtkActivatable->sync_action_properties() unimplemented for type %s",
377 g_type_name (G_OBJECT_TYPE (activatable)));
382 * gtk_activatable_set_related_action:
383 * @activatable: a #GtkActivatable
384 * @action: the #GtkAction to set
386 * Sets the related action on the @activatable object.
388 * <note><para>#GtkActivatable implementors need to handle the #GtkActivatable:related-action
389 * property and call gtk_activatable_do_set_related_action() when it changes.</para></note>
394 gtk_activatable_set_related_action (GtkActivatable *activatable,
397 g_return_if_fail (GTK_IS_ACTIVATABLE (activatable));
398 g_return_if_fail (action == NULL || GTK_IS_ACTION (action));
400 g_object_set (activatable, "related-action", action, NULL);
404 gtk_activatable_action_notify (GtkAction *action,
406 GtkActivatable *activatable)
408 gtk_activatable_update (activatable, action, pspec->name);
412 * gtk_activatable_do_set_related_action:
413 * @activatable: a #GtkActivatable
414 * @action: the #GtkAction to set
416 * This is a utility function for #GtkActivatable implementors.
418 * When implementing #GtkActivatable you must call this when
419 * handling changes of the #GtkActivatable:related-action, and
420 * you must also use this to break references in #GObject->dispose().
422 * This function adds a reference to the currently set related
423 * action for you, it also makes sure the #GtkActivatable->update()
424 * method is called when the related #GtkAction properties change
425 * and registers to the action's proxy list.
427 * <note><para>Be careful to call this before setting the local
428 * copy of the #GtkAction property, since this function uses
429 * gtk_activatable_get_action() to retrieve the previous action</para></note>
434 gtk_activatable_do_set_related_action (GtkActivatable *activatable,
437 GtkAction *prev_action;
439 prev_action = gtk_activatable_get_related_action (activatable);
441 if (prev_action != action)
445 g_signal_handlers_disconnect_by_func (prev_action, gtk_activatable_action_notify, activatable);
447 _gtk_action_remove_from_proxy_list (prev_action, GTK_WIDGET (activatable));
449 /* Some apps are using the object data directly...
450 * so continue to set it for a bit longer
452 g_object_set_data (G_OBJECT (activatable), "gtk-action", NULL);
455 * We don't want prev_action to be activated
456 * during the sync_action_properties() call when syncing "active".
458 gtk_action_block_activate (prev_action);
461 /* Some applications rely on their proxy UI to be set up
462 * before they receive the ::connect-proxy signal, so we
463 * need to call sync_action_properties() before add_to_proxy_list().
465 gtk_activatable_sync_action_properties (activatable, action);
469 gtk_action_unblock_activate (prev_action);
470 g_object_unref (prev_action);
475 g_object_ref (action);
477 g_signal_connect (G_OBJECT (action), "notify", G_CALLBACK (gtk_activatable_action_notify), activatable);
479 _gtk_action_add_to_proxy_list (action, GTK_WIDGET (activatable));
481 g_object_set_data (G_OBJECT (activatable), "gtk-action", action);
487 * gtk_activatable_get_related_action:
488 * @activatable: a #GtkActivatable
490 * Gets the related #GtkAction for @activatable.
492 * Returns: the related #GtkAction if one is set.
497 gtk_activatable_get_related_action (GtkActivatable *activatable)
501 g_return_val_if_fail (GTK_IS_ACTIVATABLE (activatable), NULL);
503 g_object_get (activatable, "related-action", &action, NULL);
505 /* g_object_get() gives us a ref... */
507 g_object_unref (action);
513 * gtk_activatable_set_use_action_appearance:
514 * @activatable: a #GtkActivatable
515 * @use_appearance: whether to use the actions appearance
517 * Sets whether this activatable should reset its layout and appearance
518 * when setting the related action or when the action changes appearance
520 * <note><para>#GtkActivatable implementors need to handle the
521 * #GtkActivatable:use-action-appearance property and call
522 * gtk_activatable_sync_action_properties() to update @activatable
523 * if needed.</para></note>
528 gtk_activatable_set_use_action_appearance (GtkActivatable *activatable,
529 gboolean use_appearance)
531 g_object_set (activatable, "use-action-appearance", use_appearance, NULL);
535 * gtk_activatable_get_use_action_appearance:
536 * @activatable: a #GtkActivatable
538 * Gets whether this activatable should reset its layout
539 * and appearance when setting the related action or when
540 * the action changes appearance.
542 * Returns: whether @activatable uses its actions appearance.
547 gtk_activatable_get_use_action_appearance (GtkActivatable *activatable)
549 gboolean use_appearance;
551 g_object_get (activatable, "use-action-appearance", &use_appearance, NULL);
553 return use_appearance;
556 #define __GTK_ACTIVATABLE_C__
557 #include "gtkaliasdef.c"