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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
/*
* Copyright (C) 2019 Purism SPC
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#include "config.h"
#include <glib/gi18n-lib.h>
#include "hdy-preferences-group-private.h"
#include "hdy-preferences-row.h"
/**
* SECTION:hdy-preferences-group
* @short_description: A group gathering preferences rows.
* @Title: HdyPreferencesGroup
*
* A #HdyPreferencesGroup represents a group or tightly related preferences,
* which in turn are represented by HdyPreferencesRow.
*
* To summarize the role of the preferences it gathers, a group can have both a
* title and a description. The title will be used by #HdyPreferencesWindow to
* let the user look for a preference.
*
* # CSS nodes
*
* #HdyPreferencesGroup has a single CSS node with name preferencesgroup.
*
* Since: 0.0.10
*/
typedef struct
{
GtkBox *box;
GtkLabel *description;
GtkListBox *listbox;
GtkBox *listbox_box;
GtkLabel *title;
} HdyPreferencesGroupPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (HdyPreferencesGroup, hdy_preferences_group, GTK_TYPE_BIN)
enum {
PROP_0,
PROP_DESCRIPTION,
PROP_TITLE,
LAST_PROP,
};
static GParamSpec *props[LAST_PROP];
static void
update_title_visibility (HdyPreferencesGroup *self)
{
HdyPreferencesGroupPrivate *priv = hdy_preferences_group_get_instance_private (self);
/* Show the listbox only if it has children to avoid having showing the
* listbox as an empty frame, parasiting the look of non-GtkListBoxRow
* children.
*/
gtk_widget_set_visible (GTK_WIDGET (priv->title),
gtk_label_get_text (priv->title) != NULL &&
g_strcmp0 (gtk_label_get_text (priv->title), "") != 0);
}
static void
update_description_visibility (HdyPreferencesGroup *self)
{
HdyPreferencesGroupPrivate *priv = hdy_preferences_group_get_instance_private (self);
gtk_widget_set_visible (GTK_WIDGET (priv->description),
gtk_label_get_text (priv->description) != NULL &&
g_strcmp0 (gtk_label_get_text (priv->description), "") != 0);
}
static void
update_listbox_visibility (HdyPreferencesGroup *self)
{
HdyPreferencesGroupPrivate *priv = hdy_preferences_group_get_instance_private (self);
g_autoptr(GList) children = NULL;
/* We must wait until listob has been built and added. */
if (priv->listbox == NULL)
return;
children = gtk_container_get_children (GTK_CONTAINER (priv->listbox));
gtk_widget_set_visible (GTK_WIDGET (priv->listbox), children != NULL);
}
typedef struct {
HdyPreferencesGroup *group;
GtkCallback callback;
gpointer callback_data;
} ForallData;
static void
for_non_internal_child (GtkWidget *widget,
gpointer callback_data)
{
ForallData *data = callback_data;
HdyPreferencesGroupPrivate *priv = hdy_preferences_group_get_instance_private (data->group);
if (widget != (GtkWidget *) priv->listbox)
data->callback (widget, data->callback_data);
}
static void
hdy_preferences_group_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data)
{
HdyPreferencesGroup *self = HDY_PREFERENCES_GROUP (container);
HdyPreferencesGroupPrivate *priv = hdy_preferences_group_get_instance_private (self);
ForallData data;
if (include_internals) {
GTK_CONTAINER_CLASS (hdy_preferences_group_parent_class)->forall (GTK_CONTAINER (self), include_internals, callback, callback_data);
return;
}
data.group = self;
data.callback = callback;
data.callback_data = callback_data;
if (priv->listbox)
GTK_CONTAINER_GET_CLASS (priv->listbox)->forall (GTK_CONTAINER (priv->listbox), include_internals, callback, callback_data);
if (priv->listbox_box)
GTK_CONTAINER_GET_CLASS (priv->listbox_box)->forall (GTK_CONTAINER (priv->listbox_box), include_internals, for_non_internal_child, &data);
}
static void
hdy_preferences_group_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
HdyPreferencesGroup *self = HDY_PREFERENCES_GROUP (object);
switch (prop_id) {
case PROP_DESCRIPTION:
g_value_set_string (value, hdy_preferences_group_get_description (self));
break;
case PROP_TITLE:
g_value_set_string (value, hdy_preferences_group_get_title (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
hdy_preferences_group_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
HdyPreferencesGroup *self = HDY_PREFERENCES_GROUP (object);
switch (prop_id) {
case PROP_DESCRIPTION:
hdy_preferences_group_set_description (self, g_value_get_string (value));
break;
case PROP_TITLE:
hdy_preferences_group_set_title (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
hdy_preferences_group_dispose (GObject *object)
{
HdyPreferencesGroup *self = HDY_PREFERENCES_GROUP (object);
HdyPreferencesGroupPrivate *priv = hdy_preferences_group_get_instance_private (self);
/*
* Since we overload forall(), the inherited destroy() won't work as normal.
* Remove internal widgets ourself.
*/
g_clear_pointer ((GtkWidget **) &priv->description, gtk_widget_destroy);
g_clear_pointer ((GtkWidget **) &priv->listbox, gtk_widget_destroy);
g_clear_pointer ((GtkWidget **) &priv->listbox_box, gtk_widget_destroy);
g_clear_pointer ((GtkWidget **) &priv->title, gtk_widget_destroy);
G_OBJECT_CLASS (hdy_preferences_group_parent_class)->dispose (object);
}
static void
hdy_preferences_group_add (GtkContainer *container,
GtkWidget *child)
{
HdyPreferencesGroup *self = HDY_PREFERENCES_GROUP (container);
HdyPreferencesGroupPrivate *priv = hdy_preferences_group_get_instance_private (self);
if (priv->title == NULL || priv->description == NULL || priv->listbox_box == NULL) {
GTK_CONTAINER_CLASS (hdy_preferences_group_parent_class)->add (container, child);
return;
}
if (HDY_IS_PREFERENCES_ROW (child))
gtk_container_add (GTK_CONTAINER (priv->listbox), child);
else
gtk_container_add (GTK_CONTAINER (priv->listbox_box), child);
}
static void
hdy_preferences_group_remove (GtkContainer *container,
GtkWidget *child)
{
HdyPreferencesGroup *self = HDY_PREFERENCES_GROUP (container);
HdyPreferencesGroupPrivate *priv = hdy_preferences_group_get_instance_private (self);
if (child == GTK_WIDGET (priv->box))
GTK_CONTAINER_CLASS (hdy_preferences_group_parent_class)->remove (container, child);
else if (HDY_IS_PREFERENCES_ROW (child))
gtk_container_remove (GTK_CONTAINER (priv->listbox), child);
else if (child != GTK_WIDGET (priv->listbox))
gtk_container_remove (GTK_CONTAINER (priv->listbox_box), child);
}
static void
hdy_preferences_group_class_init (HdyPreferencesGroupClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
object_class->get_property = hdy_preferences_group_get_property;
object_class->set_property = hdy_preferences_group_set_property;
object_class->dispose = hdy_preferences_group_dispose;
container_class->add = hdy_preferences_group_add;
container_class->remove = hdy_preferences_group_remove;
container_class->forall = hdy_preferences_group_forall;
/**
* HdyPreferencesGroup:description:
*
* The description for this group of preferences.
*
* Since: 0.0.10
*/
props[PROP_DESCRIPTION] =
g_param_spec_string ("description",
_("Description"),
_("Description"),
"",
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
/**
* HdyPreferencesGroup:title:
*
* The title for this group of preferences.
*
* Since: 0.0.10
*/
props[PROP_TITLE] =
g_param_spec_string ("title",
_("Title"),
_("Title"),
"",
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, LAST_PROP, props);
gtk_widget_class_set_css_name (widget_class, "preferencesgroup");
gtk_widget_class_set_template_from_resource (widget_class,
"/sm/puri/handy/ui/hdy-preferences-group.ui");
gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesGroup, box);
gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesGroup, description);
gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesGroup, listbox);
gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesGroup, listbox_box);
gtk_widget_class_bind_template_child_private (widget_class, HdyPreferencesGroup, title);
gtk_widget_class_bind_template_callback (widget_class, update_listbox_visibility);
}
static void
hdy_preferences_group_init (HdyPreferencesGroup *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
update_description_visibility (self);
update_title_visibility (self);
update_listbox_visibility (self);
}
/**
* hdy_preferences_group_new:
*
* Creates a new #HdyPreferencesGroup.
*
* Returns: a new #HdyPreferencesGroup
*
* Since: 0.0.10
*/
GtkWidget *
hdy_preferences_group_new (void)
{
return g_object_new (HDY_TYPE_PREFERENCES_GROUP, NULL);
}
/**
* hdy_preferences_group_get_title:
* @self: a #HdyPreferencesGroup
*
* Gets the title of @self.
*
* Returns: the title of @self.
*
* Since: 0.0.10
*/
const gchar *
hdy_preferences_group_get_title (HdyPreferencesGroup *self)
{
HdyPreferencesGroupPrivate *priv;
g_return_val_if_fail (HDY_IS_PREFERENCES_GROUP (self), NULL);
priv = hdy_preferences_group_get_instance_private (self);
return gtk_label_get_text (priv->title);
}
/**
* hdy_preferences_group_set_title:
* @self: a #HdyPreferencesGroup
* @title: the title
*
* Sets the title for @self.
*
* Since: 0.0.10
*/
void
hdy_preferences_group_set_title (HdyPreferencesGroup *self,
const gchar *title)
{
HdyPreferencesGroupPrivate *priv;
g_return_if_fail (HDY_IS_PREFERENCES_GROUP (self));
priv = hdy_preferences_group_get_instance_private (self);
if (g_strcmp0 (gtk_label_get_label (priv->title), title) == 0)
return;
gtk_label_set_label (priv->title, title);
update_title_visibility (self);
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_TITLE]);
}
/**
* hdy_preferences_group_get_description:
* @self: a #HdyPreferencesGroup
*
*
* Returns: the description of @self.
*
* Since: 0.0.10
*/
const gchar *
hdy_preferences_group_get_description (HdyPreferencesGroup *self)
{
HdyPreferencesGroupPrivate *priv;
g_return_val_if_fail (HDY_IS_PREFERENCES_GROUP (self), NULL);
priv = hdy_preferences_group_get_instance_private (self);
return gtk_label_get_text (priv->description);
}
/**
* hdy_preferences_group_set_description:
* @self: a #HdyPreferencesGroup
* @description: the description
*
* Sets the description for @self.
*
* Since: 0.0.10
*/
void
hdy_preferences_group_set_description (HdyPreferencesGroup *self,
const gchar *description)
{
HdyPreferencesGroupPrivate *priv;
g_return_if_fail (HDY_IS_PREFERENCES_GROUP (self));
priv = hdy_preferences_group_get_instance_private (self);
if (g_strcmp0 (gtk_label_get_label (priv->description), description) == 0)
return;
gtk_label_set_label (priv->description, description);
update_description_visibility (self);
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_DESCRIPTION]);
}
static void
add_preferences_to_model (HdyPreferencesRow *row,
GListStore *model)
{
const gchar *title;
g_assert (HDY_IS_PREFERENCES_ROW (row));
g_assert (G_IS_LIST_STORE (model));
if (!gtk_widget_get_visible (GTK_WIDGET (row)))
return;
title = hdy_preferences_row_get_title (row);
if (!title || !*title)
return;
g_list_store_append (model, row);
}
/**
* hdy_preferences_group_add_preferences_to_model: (skip)
* @self: a #HdyPreferencesGroup
* @model: the model
*
* Add preferences from @self to the model.
*
* Since: 0.0.10
*/
void
hdy_preferences_group_add_preferences_to_model (HdyPreferencesGroup *self,
GListStore *model)
{
HdyPreferencesGroupPrivate *priv = hdy_preferences_group_get_instance_private (self);
g_return_if_fail (HDY_IS_PREFERENCES_GROUP (self));
g_return_if_fail (G_IS_LIST_STORE (model));
if (!gtk_widget_get_visible (GTK_WIDGET (self)))
return;
gtk_container_foreach (GTK_CONTAINER (priv->listbox), (GtkCallback) add_preferences_to_model, model);
}
|