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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/*
* Copyright (c) 2014 Intel Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Ikey Doherty <michael.i.doherty@intel.com>
*/
#include "config.h"
#include "gtkstacksidebar.h"
#include "gtkbinlayout.h"
#include "gtklabel.h"
#include "gtklistbox.h"
#include "gtkscrolledwindow.h"
#include "gtkseparator.h"
#include "gtkstylecontext.h"
#include "gtkselectionmodel.h"
#include "gtkstack.h"
#include "gtkprivate.h"
#include "gtkwidgetprivate.h"
#include "gtkintl.h"
/**
* SECTION:gtkstacksidebar
* @Title: GtkStackSidebar
* @Short_description: An automatic sidebar widget
*
* A GtkStackSidebar enables you to quickly and easily provide a
* consistent "sidebar" object for your user interface.
*
* In order to use a GtkStackSidebar, you simply use a GtkStack to
* organize your UI flow, and add the sidebar to your sidebar area. You
* can use gtk_stack_sidebar_set_stack() to connect the #GtkStackSidebar
* to the #GtkStack.
*
* # CSS nodes
*
* GtkStackSidebar has a single CSS node with name stacksidebar and
* style class .sidebar.
*
* When circumstances require it, GtkStackSidebar adds the
* .needs-attention style class to the widgets representing the stack
* pages.
*/
typedef struct _GtkStackSidebarClass GtkStackSidebarClass;
struct _GtkStackSidebar
{
GtkWidget parent_instance;
GtkListBox *list;
GtkStack *stack;
GtkSelectionModel *pages;
GHashTable *rows;
};
struct _GtkStackSidebarClass
{
GtkWidgetClass parent_class;
};
G_DEFINE_TYPE (GtkStackSidebar, gtk_stack_sidebar, GTK_TYPE_WIDGET)
enum
{
PROP_0,
PROP_STACK,
N_PROPERTIES
};
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
static void
gtk_stack_sidebar_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
case PROP_STACK:
gtk_stack_sidebar_set_stack (GTK_STACK_SIDEBAR (object), g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_stack_sidebar_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkStackSidebar *self = GTK_STACK_SIDEBAR (object);
switch (prop_id)
{
case PROP_STACK:
g_value_set_object (value, self->stack);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
update_header (GtkListBoxRow *row,
GtkListBoxRow *before,
gpointer userdata)
{
GtkWidget *ret = NULL;
if (before && !gtk_list_box_row_get_header (row))
{
ret = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
gtk_list_box_row_set_header (row, ret);
}
}
static void
gtk_stack_sidebar_row_selected (GtkListBox *box,
GtkListBoxRow *row,
gpointer userdata)
{
GtkStackSidebar *self = GTK_STACK_SIDEBAR (userdata);
guint index;
if (row == NULL)
return;
index = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (row), "child-index"));
gtk_selection_model_select_item (self->pages, index, TRUE);
}
static void
gtk_stack_sidebar_init (GtkStackSidebar *self)
{
GtkWidget *sw;
sw = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
GTK_POLICY_NEVER,
GTK_POLICY_AUTOMATIC);
gtk_widget_set_parent (sw, GTK_WIDGET (self));
self->list = GTK_LIST_BOX (gtk_list_box_new ());
gtk_container_add (GTK_CONTAINER (sw), GTK_WIDGET (self->list));
gtk_list_box_set_header_func (self->list, update_header, self, NULL);
g_signal_connect (self->list, "row-selected",
G_CALLBACK (gtk_stack_sidebar_row_selected), self);
gtk_widget_add_css_class (GTK_WIDGET (self), "sidebar");
self->rows = g_hash_table_new (NULL, NULL);
}
static void
update_row (GtkStackSidebar *self,
GtkStackPage *page,
GtkWidget *row)
{
GtkWidget *item;
gchar *title;
gboolean needs_attention;
gboolean visible;
g_object_get (page,
"title", &title,
"needs-attention", &needs_attention,
"visible", &visible,
NULL);
item = gtk_bin_get_child (GTK_BIN (row));
gtk_label_set_text (GTK_LABEL (item), title);
gtk_widget_set_visible (row, visible && title != NULL);
if (needs_attention)
gtk_widget_add_css_class (row, GTK_STYLE_CLASS_NEEDS_ATTENTION);
else
gtk_widget_remove_css_class (row, GTK_STYLE_CLASS_NEEDS_ATTENTION);
g_free (title);
}
static void
on_page_updated (GtkStackPage *page,
GParamSpec *pspec,
GtkStackSidebar *self)
{
GtkWidget *row;
row = g_hash_table_lookup (self->rows, page);
update_row (self, page, row);
}
static void
add_child (guint position,
GtkStackSidebar *self)
{
GtkWidget *item;
GtkWidget *row;
GtkStackPage *page;
/* Make a pretty item when we add kids */
item = gtk_label_new ("");
gtk_widget_set_halign (item, GTK_ALIGN_START);
gtk_widget_set_valign (item, GTK_ALIGN_CENTER);
row = gtk_list_box_row_new ();
gtk_container_add (GTK_CONTAINER (row), item);
page = g_list_model_get_item (G_LIST_MODEL (self->pages), position);
update_row (self, page, row);
gtk_container_add (GTK_CONTAINER (self->list), row);
g_object_set_data (G_OBJECT (row), "child-index", GUINT_TO_POINTER (position));
if (gtk_selection_model_is_selected (self->pages, position))
gtk_list_box_select_row (self->list, GTK_LIST_BOX_ROW (row));
else
gtk_list_box_unselect_row (self->list, GTK_LIST_BOX_ROW (row));
g_signal_connect (page, "notify", G_CALLBACK (on_page_updated), self);
g_hash_table_insert (self->rows, page, row);
g_object_unref (page);
}
static void
populate_sidebar (GtkStackSidebar *self)
{
guint i;
for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (self->pages)); i++)
add_child (i, self);
}
static void
clear_sidebar (GtkStackSidebar *self)
{
GHashTableIter iter;
GtkStackPage *page;
GtkWidget *row;
g_hash_table_iter_init (&iter, self->rows);
while (g_hash_table_iter_next (&iter, (gpointer *)&page, (gpointer *)&row))
{
gtk_container_remove (GTK_CONTAINER (self->list), row);
g_hash_table_iter_remove (&iter);
g_signal_handlers_disconnect_by_func (page, on_page_updated, self);
}
}
static void
items_changed_cb (GListModel *model,
guint position,
guint removed,
guint added,
GtkStackSidebar *self)
{
/* FIXME: we can do better */
clear_sidebar (self);
populate_sidebar (self);
}
static void
selection_changed_cb (GtkSelectionModel *model,
guint position,
guint n_items,
GtkStackSidebar *self)
{
guint i;
for (i = position; i < position + n_items; i++)
{
GtkStackPage *page;
GtkWidget *row;
page = g_list_model_get_item (G_LIST_MODEL (self->pages), i);
row = g_hash_table_lookup (self->rows, page);
if (gtk_selection_model_is_selected (self->pages, i))
gtk_list_box_select_row (self->list, GTK_LIST_BOX_ROW (row));
else
gtk_list_box_unselect_row (self->list, GTK_LIST_BOX_ROW (row));
g_object_unref (page);
}
}
static void
disconnect_stack_signals (GtkStackSidebar *self)
{
g_signal_handlers_disconnect_by_func (self->pages, items_changed_cb, self);
g_signal_handlers_disconnect_by_func (self->pages, selection_changed_cb, self);
}
static void
connect_stack_signals (GtkStackSidebar *self)
{
g_signal_connect (self->pages, "items-changed", G_CALLBACK (items_changed_cb), self);
g_signal_connect (self->pages, "selection-changed", G_CALLBACK (selection_changed_cb), self);
}
static void
set_stack (GtkStackSidebar *self,
GtkStack *stack)
{
if (stack)
{
self->stack = g_object_ref (stack);
self->pages = gtk_stack_get_pages (stack);
populate_sidebar (self);
connect_stack_signals (self);
}
}
static void
unset_stack (GtkStackSidebar *self)
{
if (self->stack)
{
disconnect_stack_signals (self);
clear_sidebar (self);
g_clear_object (&self->stack);
g_clear_object (&self->pages);
}
}
static void
gtk_stack_sidebar_dispose (GObject *object)
{
GtkStackSidebar *self = GTK_STACK_SIDEBAR (object);
GtkWidget *child;
unset_stack (self);
/* The scrolled window */
child = gtk_widget_get_first_child (GTK_WIDGET (self));
if (child)
{
gtk_widget_unparent (child);
self->list = NULL;
}
G_OBJECT_CLASS (gtk_stack_sidebar_parent_class)->dispose (object);
}
static void
gtk_stack_sidebar_finalize (GObject *object)
{
GtkStackSidebar *self = GTK_STACK_SIDEBAR (object);
g_hash_table_destroy (self->rows);
G_OBJECT_CLASS (gtk_stack_sidebar_parent_class)->finalize (object);
}
static void
gtk_stack_sidebar_class_init (GtkStackSidebarClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->dispose = gtk_stack_sidebar_dispose;
object_class->finalize = gtk_stack_sidebar_finalize;
object_class->set_property = gtk_stack_sidebar_set_property;
object_class->get_property = gtk_stack_sidebar_get_property;
widget_class->grab_focus = gtk_widget_grab_focus_none;
widget_class->focus = gtk_widget_focus_child;
obj_properties[PROP_STACK] =
g_param_spec_object (I_("stack"), P_("Stack"),
P_("Associated stack for this GtkStackSidebar"),
GTK_TYPE_STACK,
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties);
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
gtk_widget_class_set_css_name (widget_class, I_("stacksidebar"));
}
/**
* gtk_stack_sidebar_new:
*
* Creates a new sidebar.
*
* Returns: the new #GtkStackSidebar
*/
GtkWidget *
gtk_stack_sidebar_new (void)
{
return GTK_WIDGET (g_object_new (GTK_TYPE_STACK_SIDEBAR, NULL));
}
/**
* gtk_stack_sidebar_set_stack:
* @self: a #GtkStackSidebar
* @stack: a #GtkStack
*
* Set the #GtkStack associated with this #GtkStackSidebar.
*
* The sidebar widget will automatically update according to the order
* (packing) and items within the given #GtkStack.
*/
void
gtk_stack_sidebar_set_stack (GtkStackSidebar *self,
GtkStack *stack)
{
g_return_if_fail (GTK_IS_STACK_SIDEBAR (self));
g_return_if_fail (GTK_IS_STACK (stack) || stack == NULL);
if (self->stack == stack)
return;
unset_stack (self);
set_stack (self, stack);
gtk_widget_queue_resize (GTK_WIDGET (self));
g_object_notify (G_OBJECT (self), "stack");
}
/**
* gtk_stack_sidebar_get_stack:
* @self: a #GtkStackSidebar
*
* Retrieves the stack.
* See gtk_stack_sidebar_set_stack().
*
* Returns: (nullable) (transfer none): the associated #GtkStack or
* %NULL if none has been set explicitly
*/
GtkStack *
gtk_stack_sidebar_get_stack (GtkStackSidebar *self)
{
g_return_val_if_fail (GTK_IS_STACK_SIDEBAR (self), NULL);
return GTK_STACK (self->stack);
}
|