File: tepl-stack-helper.c

package info (click to toggle)
tepl 6.4.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,692 kB
  • sloc: ansic: 16,879; xml: 542; sh: 20; makefile: 9
file content (208 lines) | stat: -rw-r--r-- 5,198 bytes parent folder | download
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
/* SPDX-FileCopyrightText: 2022 - Sébastien Wilmet <swilmet@gnome.org>
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include "tepl-stack-helper.h"
#include "tepl-signal-group.h"

/*
 * #TeplStackHelper adds convenience API to #GtkStack.
 */

/* Another nice benefit is that unit tests can be written. For a #GtkStack
 * switcher widget, only interactive tests are possible.
 */

struct _TeplStackHelperPrivate
{
	TeplSignalGroup *stack_signal_group;
};

enum
{
	SIGNAL_CHANGED,
	N_SIGNALS
};

static guint signals[N_SIGNALS];

G_DEFINE_TYPE_WITH_PRIVATE (TeplStackHelper, _tepl_stack_helper, G_TYPE_OBJECT)

static void
_tepl_stack_helper_dispose (GObject *object)
{
	TeplStackHelper *helper = TEPL_STACK_HELPER (object);

	tepl_signal_group_clear (&helper->priv->stack_signal_group);

	G_OBJECT_CLASS (_tepl_stack_helper_parent_class)->dispose (object);
}

static void
_tepl_stack_helper_class_init (TeplStackHelperClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = _tepl_stack_helper_dispose;

	/*
	 * TeplStackHelper::changed:
	 * @helper: the #TeplStackHelper emitting the signal.
	 *
	 * ::changed is a convenience signal that is emitted whenever the
	 * content of a #GtkStack switcher potentially needs to be re-created.
	 *
	 * There are two possible approaches to implement a #GtkStack switcher:
	 * 1. Listen to this signal and re-create each time the whole content of
	 *    the switcher.
	 * 2. Don't use this class, and listen instead to the individual
	 *    finer-grained signals (listed below) and update the minimum of the
	 *    switcher content. This approach is more difficult (and
	 *    error-prone), but has normally better performances.
	 *
	 * This signal is emitted on these finer-grained signals:
	 * - #GtkContainer::add
	 * - #GtkContainer::remove
	 * - #GObject::notify for the #GtkStack:visible-child property.
	 * - For all children part of the container:
	 *     - #GtkWidget::child-notify
	 *     - #GObject::notify for the #GtkWidget:visible property.
	 */
	signals[SIGNAL_CHANGED] =
		g_signal_new ("changed",
			      G_TYPE_FROM_CLASS (klass),
			      G_SIGNAL_RUN_FIRST,
			      0, NULL, NULL, NULL,
			      G_TYPE_NONE, 0);
}

static void
_tepl_stack_helper_init (TeplStackHelper *helper)
{
	helper->priv = _tepl_stack_helper_get_instance_private (helper);
}

static void
emit_changed (TeplStackHelper *helper)
{
	g_signal_emit (helper, signals[SIGNAL_CHANGED], 0);
}

static void
child_notify_cb (GtkWidget       *stack_child,
                 GParamSpec      *child_property,
                 TeplStackHelper *helper)
{
	emit_changed (helper);
}

static void
visible_notify_cb (GtkWidget       *stack_child,
		   GParamSpec      *pspec,
		   TeplStackHelper *helper)
{
	emit_changed (helper);
}

static void
connect_child (TeplStackHelper *helper,
               GtkWidget       *stack_child)
{
	g_signal_connect (stack_child,
			  "child-notify",
			  G_CALLBACK (child_notify_cb),
			  helper);

	g_signal_connect (stack_child,
			  "notify::visible",
			  G_CALLBACK (visible_notify_cb),
			  helper);
}

static void
disconnect_child (TeplStackHelper *helper,
		  GtkWidget       *stack_child)
{
	g_signal_handlers_disconnect_by_func (stack_child, child_notify_cb, helper);
	g_signal_handlers_disconnect_by_func (stack_child, visible_notify_cb, helper);
}

static void
connect_all_children (TeplStackHelper *helper,
		      GtkStack        *stack)
{
	GList *children;
	GList *l;

	children = gtk_container_get_children (GTK_CONTAINER (stack));
	for (l = children; l != NULL; l = l->next)
	{
		GtkWidget *stack_child = GTK_WIDGET (l->data);
		connect_child (helper, stack_child);
	}
	g_list_free (children);
}

static void
add_cb (GtkStack        *stack,
	GtkWidget       *stack_child,
	TeplStackHelper *helper)
{
	connect_child (helper, stack_child);
	emit_changed (helper);
}

static void
remove_cb (GtkStack        *stack,
	   GtkWidget       *stack_child,
	   TeplStackHelper *helper)
{
	disconnect_child (helper, stack_child);
	emit_changed (helper);
}

static void
visible_child_notify_cb (GtkStack        *stack,
			 GParamSpec      *pspec,
			 TeplStackHelper *helper)
{
	emit_changed (helper);
}

/* Doesn't hold a reference to @stack. */
TeplStackHelper *
_tepl_stack_helper_new (GtkStack *stack)
{
	TeplStackHelper *helper;

	g_return_val_if_fail (GTK_IS_STACK (stack), NULL);

	/* In an ideal world, a :stack construct-only property should be added
	 * instead of doing the work in new().
	 */
	helper = g_object_new (TEPL_TYPE_STACK_HELPER, NULL);

	connect_all_children (helper, stack);

	helper->priv->stack_signal_group = tepl_signal_group_new (G_OBJECT (stack));

	tepl_signal_group_add (helper->priv->stack_signal_group,
			       g_signal_connect (stack,
						 "add",
						 G_CALLBACK (add_cb),
						 helper));

	tepl_signal_group_add (helper->priv->stack_signal_group,
			       g_signal_connect (stack,
						 "remove",
						 G_CALLBACK (remove_cb),
						 helper));

	tepl_signal_group_add (helper->priv->stack_signal_group,
			       g_signal_connect (stack,
						 "notify::visible-child",
						 G_CALLBACK (visible_child_notify_cb),
						 helper));

	return helper;
}