ActivatableIF

Activatable widgets can be connected to a gtk.Action and reflects the state of its action. A GtkActivatable can also provide feedback through its action, as they are responsible for activating their related actions.

Implementing GtkActivatable

When extending a class that is already GtkActivatable; it is only necessary to implement the [GtkActivatable-|GtkActivatable-]>Action.properties and [GtkActivatable-|GtkActivatable-]>update() methods and chain up to the parent implementation, however when introducing a new GtkActivatable class; the related-action and use-action-appearance properties need to be handled by the implementor. Handling these properties is mostly a matter of installing the action pointer and boolean flag on your instance, and calling gtk_activatable_do_set_related_action() and gtk_activatable_sync_action_properties() at the appropriate times.

A class fragment implementing GtkActivatable

1 
2 
3 enum {
4 ...
5 
6 PROP_ACTIVATABLE_RELATED_ACTION,
7 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
8 }
9 
10 struct _FooBarPrivate
11 {
12 
13 ...
14 
15 GtkAction      *action;
16 gboolean        use_action_appearance;
17 };
18 
19 ...
20 
21 static void foo_bar_activatable_interface_init         (GtkActivatableIface  *iface);
22 static void foo_bar_activatable_update                 (GtkActivatable       *activatable,
23 GtkAction            *action,
24 const gchar          *property_name);
25 static void foo_bar_activatable_sync_action_properties (GtkActivatable       *activatable,
26 GtkAction            *action);
27 ...
28 
29 
30 static void
31 foo_bar_class_init (FooBarClass *klass)
32 {
33 
34 ...
35 
36 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
37 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
38 
39 ...
40 }
41 
42 
43 static void
44 foo_bar_activatable_interface_init (GtkActivatableIface  *iface)
45 {
46 iface->update = foo_bar_activatable_update;
47 iface->sync_action_properties = foo_bar_activatable_sync_action_properties;
48 }
49 
50 ... Break the reference using `gtk_activatable_do_set_related_action()`...
51 
52 static void
53 foo_bar_dispose (GObject *object)
54 {
55 FooBar *bar = FOO_BAR (object);
56 FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
57 
58 ...
59 
60 if (priv->action)
61 {
62 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (bar), NULL);
63 priv->action = NULL;
64 }
65 G_OBJECT_CLASS (foo_bar_parent_class)->dispose (object);
66 }
67 
68 ... Handle the “related-action” and “use-action-appearance” properties ...
69 
70 static void
71 foo_bar_set_property (GObject         *object,
72 guint            prop_id,
73 const GValue    *value,
74 GParamSpec      *pspec)
75 {
76 FooBar *bar = FOO_BAR (object);
77 FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
78 
79 switch (prop_id)
80 {
81 
82 ...
83 
84 case PROP_ACTIVATABLE_RELATED_ACTION:
85 foo_bar_set_related_action (bar, g_value_get_object (value));
86 break;
87 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
88 foo_bar_set_use_action_appearance (bar, g_value_get_boolean (value));
89 break;
90 default:
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92 break;
93 }
94 }
95 
96 static void
97 foo_bar_get_property (GObject         *object,
98 guint            prop_id,
99 GValue          *value,
100 GParamSpec      *pspec)
101 {
102 FooBar *bar = FOO_BAR (object);
103 FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
104 
105 switch (prop_id)
106 {
107 
108 ...
109 
110 case PROP_ACTIVATABLE_RELATED_ACTION:
111 g_value_set_object (value, priv->action);
112 break;
113 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
114 g_value_set_boolean (value, priv->use_action_appearance);
115 break;
116 default:
117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118 break;
119 }
120 }
121 
122 
123 static void
124 foo_bar_set_use_action_appearance (FooBar   *bar,
125 gboolean  use_appearance)
126 {
127 FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
128 
129 if (priv->use_action_appearance != use_appearance)
130 {
131 priv->use_action_appearance = use_appearance;
132 
133 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (bar), priv->action);
134 }
135 }
136 
137 ... call `gtk_activatable_do_set_related_action()` and then assign the action pointer,
138 no need to reference the action here since `gtk_activatable_do_set_related_action()` already
139 holds a reference here for you...
140 static void
141 foo_bar_set_related_action (FooBar    *bar,
142 GtkAction *action)
143 {
144 FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);
145 
146 if (priv->action == action)
147 return;
148 
149 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (bar), action);
150 
151 priv->action = action;
152 }
153 
154 ... Selectively reset and update activatable depending on the use-action-appearance property ...
155 static void
156 gtk_button_activatable_sync_action_properties (GtkActivatable       *activatable,
157 GtkAction            *action)
158 {
159 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
160 
161 if (!action)
162 return;
163 
164 if (gtk_action_is_visible (action))
165 gtk_widget_show (GTK_WIDGET (activatable));
166 else
167 gtk_widget_hide (GTK_WIDGET (activatable));
168 
169 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
170 
171 ...
172 
173 if (priv->use_action_appearance)
174 {
175 if (gtk_action_get_stock_id (action))
176 foo_bar_set_stock (button, gtk_action_get_stock_id (action));
177 else if (gtk_action_get_label (action))
178 foo_bar_set_label (button, gtk_action_get_label (action));
179 
180 ...
181 
182 }
183 }
184 
185 static void
186 foo_bar_activatable_update (GtkActivatable       *activatable,
187 GtkAction            *action,
188 const gchar          *property_name)
189 {
190 FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (activatable);
191 
192 if (strcmp (property_name, "visible") == 0)
193 {
194 if (gtk_action_is_visible (action))
195 gtk_widget_show (GTK_WIDGET (activatable));
196 else
197 gtk_widget_hide (GTK_WIDGET (activatable));
198 }
199 else if (strcmp (property_name, "sensitive") == 0)
200 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
201 
202 ...
203 
204 if (!priv->use_action_appearance)
205 return;
206 
207 if (strcmp (property_name, "stock-id") == 0)
208 foo_bar_set_stock (button, gtk_action_get_stock_id (action));
209 else if (strcmp (property_name, "label") == 0)
210 foo_bar_set_label (button, gtk_action_get_label (action));
211 
212 ...
213 }

Members

Functions

doSetRelatedAction
void doSetRelatedAction(Action action)

This is a utility function for GtkActivatable implementors.

getActivatableStruct
GtkActivatable* getActivatableStruct(bool transferOwnership)

Get the main Gtk struct

getRelatedAction
Action getRelatedAction()

Gets the related gtk.Action for activatable.

getStruct
void* getStruct()

the main Gtk struct as a void*

getUseActionAppearance
bool getUseActionAppearance()

Gets whether this activatable should reset its layout and appearance when setting the related action or when the action changes appearance.

setRelatedAction
void setRelatedAction(Action action)

Sets the related action on the activatable object.

setUseActionAppearance
void setUseActionAppearance(bool useAppearance)

Sets whether this activatable should reset its layout and appearance when setting the related action or when the action changes appearance

syncActionProperties
void syncActionProperties(Action action)

This is called to update the activatable completely, this is called internally when the related-action property is set or unset and by the implementing class when use-action-appearance changes.

Static functions

getType
GType getType()