CtkActivatable

CtkActivatable — An interface for activatable widgets

Functions

Properties

CtkAction * related-action Read / Write
gboolean use-action-appearance Read / Write

Types and Values

Object Hierarchy

    GInterface
    ╰── CtkActivatable

Prerequisites

CtkActivatable requires GObject.

Known Implementations

CtkActivatable is implemented by CtkButton, CtkCheckButton, CtkCheckMenuItem, CtkColorButton, CtkFontButton, CtkImageMenuItem, CtkLinkButton, CtkLockButton, CtkMenuButton, CtkMenuItem, CtkMenuToolButton, CtkModelButton, CtkRadioButton, CtkRadioMenuItem, CtkRadioToolButton, CtkRecentChooserMenu, CtkScaleButton, CtkSeparatorMenuItem, CtkSeparatorToolItem, CtkSwitch, CtkTearoffMenuItem, CtkToggleButton, CtkToggleToolButton, CtkToolButton, CtkToolItem and CtkVolumeButton.

Includes

#include <ctk/ctk.h>

Description

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

Implementing CtkActivatable

When extending a class that is already CtkActivatable; it is only necessary to implement the CtkActivatable->sync_action_properties() and CtkActivatable->update() methods and chain up to the parent implementation, however when introducing a new CtkActivatable 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 ctk_activatable_do_set_related_action() and ctk_activatable_sync_action_properties() at the appropriate times.

A class fragment implementing CtkActivatable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
enum {
...

PROP_ACTIVATABLE_RELATED_ACTION,
PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
}

struct _FooBarPrivate
{

  ...

  CtkAction      *action;
  gboolean        use_action_appearance;
};

...

static void foo_bar_activatable_interface_init         (CtkActivatableIface  *iface);
static void foo_bar_activatable_update                 (CtkActivatable       *activatable,
						           CtkAction            *action,
						           const gchar          *property_name);
static void foo_bar_activatable_sync_action_properties (CtkActivatable       *activatable,
						           CtkAction            *action);
...


static void
foo_bar_class_init (FooBarClass *klass)
{

  ...

  g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
  g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");

  ...
}


static void
foo_bar_activatable_interface_init (CtkActivatableIface  *iface)
{
  iface->update = foo_bar_activatable_update;
  iface->sync_action_properties = foo_bar_activatable_sync_action_properties;
}

... Break the reference using ctk_activatable_do_set_related_action()...

static void 
foo_bar_dispose (GObject *object)
{
  FooBar *bar = FOO_BAR (object);
  FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);

  ...

  if (priv->action)
    {
      ctk_activatable_do_set_related_action (CTK_ACTIVATABLE (bar), NULL);
      priv->action = NULL;
    }
  G_OBJECT_CLASS (foo_bar_parent_class)->dispose (object);
}

... Handle the related-action and use-action-appearance properties ...

static void
foo_bar_set_property (GObject         *object,
                      guint            prop_id,
                      const GValue    *value,
                      GParamSpec      *pspec)
{
  FooBar *bar = FOO_BAR (object);
  FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);

  switch (prop_id)
    {

      ...

    case PROP_ACTIVATABLE_RELATED_ACTION:
      foo_bar_set_related_action (bar, g_value_get_object (value));
      break;
    case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
      foo_bar_set_use_action_appearance (bar, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
foo_bar_get_property (GObject         *object,
                         guint            prop_id,
                         GValue          *value,
                         GParamSpec      *pspec)
{
  FooBar *bar = FOO_BAR (object);
  FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);

  switch (prop_id)
    { 

      ...

    case PROP_ACTIVATABLE_RELATED_ACTION:
      g_value_set_object (value, priv->action);
      break;
    case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
      g_value_set_boolean (value, priv->use_action_appearance);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}


static void
foo_bar_set_use_action_appearance (FooBar   *bar, 
				   gboolean  use_appearance)
{
  FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);

  if (priv->use_action_appearance != use_appearance)
    {
      priv->use_action_appearance = use_appearance;
      
      ctk_activatable_sync_action_properties (CTK_ACTIVATABLE (bar), priv->action);
    }
}

... call ctk_activatable_do_set_related_action() and then assign the action pointer, 
no need to reference the action here since ctk_activatable_do_set_related_action() already 
holds a reference here for you...
static void
foo_bar_set_related_action (FooBar    *bar, 
			    CtkAction *action)
{
  FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (bar);

  if (priv->action == action)
    return;

  ctk_activatable_do_set_related_action (CTK_ACTIVATABLE (bar), action);

  priv->action = action;
}

... Selectively reset and update activatable depending on the use-action-appearance property ...
static void
ctk_button_activatable_sync_action_properties (CtkActivatable       *activatable,
		                                  CtkAction            *action)
{
  CtkButtonPrivate *priv = CTK_BUTTON_GET_PRIVATE (activatable);

  if (!action)
    return;

  if (ctk_action_is_visible (action))
    ctk_widget_show (CTK_WIDGET (activatable));
  else
    ctk_widget_hide (CTK_WIDGET (activatable));
  
  ctk_widget_set_sensitive (CTK_WIDGET (activatable), ctk_action_is_sensitive (action));

  ...
  
  if (priv->use_action_appearance)
    {
      if (ctk_action_get_stock_id (action))
	foo_bar_set_stock (button, ctk_action_get_stock_id (action));
      else if (ctk_action_get_label (action))
	foo_bar_set_label (button, ctk_action_get_label (action));

      ...

    }
}

static void 
foo_bar_activatable_update (CtkActivatable       *activatable,
			       CtkAction            *action,
			       const gchar          *property_name)
{
  FooBarPrivate *priv = FOO_BAR_GET_PRIVATE (activatable);

  if (strcmp (property_name, "visible") == 0)
    {
      if (ctk_action_is_visible (action))
	ctk_widget_show (CTK_WIDGET (activatable));
      else
	ctk_widget_hide (CTK_WIDGET (activatable));
    }
  else if (strcmp (property_name, "sensitive") == 0)
    ctk_widget_set_sensitive (CTK_WIDGET (activatable), ctk_action_is_sensitive (action));

  ...

  if (!priv->use_action_appearance)
    return;

  if (strcmp (property_name, "stock-id") == 0)
    foo_bar_set_stock (button, ctk_action_get_stock_id (action));
  else if (strcmp (property_name, "label") == 0)
    foo_bar_set_label (button, ctk_action_get_label (action));

  ...
}

Functions

ctk_activatable_do_set_related_action ()

void
ctk_activatable_do_set_related_action (CtkActivatable *activatable,
                                       CtkAction *action);

This is a utility function for CtkActivatable implementors.

When implementing CtkActivatable you must call this when handling changes of the “related-action”, and you must also use this to break references in GObject->dispose().

This function adds a reference to the currently set related action for you, it also makes sure the CtkActivatable->update() method is called when the related CtkAction properties change and registers to the action’s proxy list.

Be careful to call this before setting the local copy of the CtkAction property, since this function uses ctk_activatable_get_related_action() to retrieve the previous action.

Parameters

activatable

a CtkActivatable

 

action

the CtkAction to set

 

Since: 2.16


ctk_activatable_get_related_action ()

CtkAction *
ctk_activatable_get_related_action (CtkActivatable *activatable);

Gets the related CtkAction for activatable .

Parameters

activatable

a CtkActivatable

 

Returns

the related CtkAction if one is set.

[transfer none]

Since: 2.16


ctk_activatable_get_use_action_appearance ()

gboolean
ctk_activatable_get_use_action_appearance
                               (CtkActivatable *activatable);

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

Parameters

activatable

a CtkActivatable

 

Returns

whether activatable uses its actions appearance.

Since: 2.16


ctk_activatable_sync_action_properties ()

void
ctk_activatable_sync_action_properties
                               (CtkActivatable *activatable,
                                CtkAction *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.

Parameters

activatable

a CtkActivatable

 

action

the related CtkAction or NULL.

[allow-none]

Since: 2.16


ctk_activatable_set_related_action ()

void
ctk_activatable_set_related_action (CtkActivatable *activatable,
                                    CtkAction *action);

Sets the related action on the activatable object.

CtkActivatable implementors need to handle the “related-action” property and call ctk_activatable_do_set_related_action() when it changes.

Parameters

activatable

a CtkActivatable

 

action

the CtkAction to set

 

Since: 2.16


ctk_activatable_set_use_action_appearance ()

void
ctk_activatable_set_use_action_appearance
                               (CtkActivatable *activatable,
                                gboolean use_appearance);

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

CtkActivatable implementors need to handle the “use-action-appearance” property and call ctk_activatable_sync_action_properties() to update activatable if needed.

Parameters

activatable

a CtkActivatable

 

use_appearance

whether to use the actions appearance

 

Since: 2.16

Types and Values

CtkActivatable

typedef struct _CtkActivatable CtkActivatable;

struct CtkActivatableIface

struct CtkActivatableIface {
  /* virtual table */
  void   (* update)                   (CtkActivatable *activatable,
		                       CtkAction      *action,
		                       const gchar    *property_name);
  void   (* sync_action_properties)   (CtkActivatable *activatable,
		                       CtkAction      *action);
};

This method can be called with a NULL action at times.

Members

update ()

Called to update the activatable when its related action’s properties change. You must check the “use-action-appearance” property only apply action properties that are meant to effect the appearance accordingly.

 

sync_action_properties ()

Called to update the activatable completely, this is called internally when “related-action” property is set or unset and by the implementor when “use-action-appearance” changes.

 

Since: 2.16

Property Details

The “related-action” property

  “related-action”           CtkAction *

The action that this activatable will activate and receive updates from for various states and possibly appearance.

CtkActivatable implementors need to handle the this property and call ctk_activatable_do_set_related_action() when it changes.

Owner: CtkActivatable

Flags: Read / Write

Since: 2.16


The “use-action-appearance” property

  “use-action-appearance”    gboolean

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

See the CtkAction documentation directly to find which properties should be ignored by the CtkActivatable when this property is FALSE.

CtkActivatable implementors need to handle this property and call ctk_activatable_sync_action_properties() on the activatable widget when it changes.

Owner: CtkActivatable

Flags: Read / Write

Default value: TRUE

Since: 2.16