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
|
#include <gtk/gtk.h>
#include <string.h>
typedef struct _Row Row;
typedef struct _RowClass RowClass;
struct _Row
{
GtkListBoxRow parent_instance;
GtkWidget *label;
gint sort_id;
};
struct _RowClass
{
GtkListBoxRowClass parent_class;
};
const char *css =
"list row {"
" border-width: 1px;"
" border-style: solid;"
" border-color: blue;"
"}"
"list row:hover {"
"background-color: green;"
"}"
"list row:active {"
"background-color: red;"
"}";
G_DEFINE_TYPE (Row, row, GTK_TYPE_LIST_BOX_ROW)
static void
row_init (Row *row)
{
}
static void
row_class_init (RowClass *class)
{
}
GtkWidget *
row_new (const gchar* text, gint sort_id) {
Row *row;
row = g_object_new (row_get_type (), NULL);
if (text != NULL)
{
row->label = gtk_label_new (text);
gtk_container_add (GTK_CONTAINER (row), row->label);
gtk_widget_show (row->label);
}
row->sort_id = sort_id;
return GTK_WIDGET (row);
}
static void
update_header_cb (Row *row, Row *before, gpointer data)
{
GtkWidget *hbox, *l, *b;
GList *children;
if (before == NULL ||
(row->label != NULL &&
strcmp (gtk_label_get_text (GTK_LABEL (row->label)), "blah3") == 0))
{
/* Create header if needed */
if (gtk_list_box_row_get_header (GTK_LIST_BOX_ROW (row)) == NULL)
{
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
l = gtk_label_new ("Header");
gtk_container_add (GTK_CONTAINER (hbox), l);
b = gtk_button_new_with_label ("button");
gtk_container_add (GTK_CONTAINER (hbox), b);
gtk_widget_show (l);
gtk_widget_show (b);
gtk_list_box_row_set_header (GTK_LIST_BOX_ROW (row), hbox);
}
hbox = gtk_list_box_row_get_header(GTK_LIST_BOX_ROW (row));
children = gtk_container_get_children (GTK_CONTAINER (hbox));
l = children->data;
g_list_free (children);
gtk_label_set_text (GTK_LABEL (l), g_strdup_printf ("Header %d", row->sort_id));
}
else
{
gtk_list_box_row_set_header(GTK_LIST_BOX_ROW (row), NULL);
}
}
static int
sort_cb (Row *a, Row *b, gpointer data)
{
return a->sort_id - b->sort_id;
}
static int
reverse_sort_cb (Row *a, Row *b, gpointer data)
{
return b->sort_id - a->sort_id;
}
static gboolean
filter_cb (Row *row, gpointer data)
{
const char *text;
if (row->label != NULL)
{
text = gtk_label_get_text (GTK_LABEL (row->label));
return strcmp (text, "blah3") != 0;
}
return TRUE;
}
static void
row_activated_cb (GtkListBox *list_box,
GtkListBoxRow *row)
{
g_print ("activated row %p\n", row);
}
static void
row_selected_cb (GtkListBox *list_box,
GtkListBoxRow *row)
{
g_print ("selected row %p\n", row);
}
static void
sort_clicked_cb (GtkButton *button,
gpointer data)
{
GtkListBox *list = data;
gtk_list_box_set_sort_func (list, (GtkListBoxSortFunc)sort_cb, NULL, NULL);
}
static void
reverse_sort_clicked_cb (GtkButton *button,
gpointer data)
{
GtkListBox *list = data;
gtk_list_box_set_sort_func (list, (GtkListBoxSortFunc)reverse_sort_cb, NULL, NULL);
}
static void
filter_clicked_cb (GtkButton *button,
gpointer data)
{
GtkListBox *list = data;
gtk_list_box_set_filter_func (list, (GtkListBoxFilterFunc)filter_cb, NULL, NULL);
}
static void
unfilter_clicked_cb (GtkButton *button,
gpointer data)
{
GtkListBox *list = data;
gtk_list_box_set_filter_func (list, NULL, NULL, NULL);
}
static void
change_clicked_cb (GtkButton *button,
gpointer data)
{
Row *row = data;
if (strcmp (gtk_label_get_text (GTK_LABEL (row->label)), "blah3") == 0)
{
gtk_label_set_text (GTK_LABEL (row->label), "blah5");
row->sort_id = 5;
}
else
{
gtk_label_set_text (GTK_LABEL (row->label), "blah3");
row->sort_id = 3;
}
gtk_list_box_row_changed (GTK_LIST_BOX_ROW (row));
}
static void
add_clicked_cb (GtkButton *button,
gpointer data)
{
GtkListBox *list = data;
GtkWidget *new_row;
static int new_button_nr = 1;
new_row = row_new( g_strdup_printf ("blah2 new %d", new_button_nr), new_button_nr);
gtk_container_add (GTK_CONTAINER (list), new_row);
new_button_nr++;
}
static void
separate_clicked_cb (GtkButton *button,
gpointer data)
{
GtkListBox *list = data;
gtk_list_box_set_header_func (list, (GtkListBoxUpdateHeaderFunc)update_header_cb, NULL, NULL);
}
static void
unseparate_clicked_cb (GtkButton *button,
gpointer data)
{
GtkListBox *list = data;
gtk_list_box_set_header_func (list, NULL, NULL, NULL);
}
static void
visibility_clicked_cb (GtkButton *button,
gpointer data)
{
GtkWidget *row = data;
gtk_widget_set_visible (row, !gtk_widget_get_visible (row));
}
static void
selection_mode_changed (GtkComboBox *combo, gpointer data)
{
GtkListBox *list = data;
gtk_list_box_set_selection_mode (list, gtk_combo_box_get_active (combo));
}
static void
single_click_clicked (GtkButton *check, gpointer data)
{
GtkListBox *list = data;
g_print ("single: %d\n", gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check)));
gtk_list_box_set_activate_on_single_click (list, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check)));
}
int
main (int argc, char *argv[])
{
GtkCssProvider *provider;
GtkWidget *window, *hbox, *vbox, *list, *row, *row3, *row_vbox, *row_hbox, *l;
GtkWidget *check, *button, *combo, *scrolled;
gtk_init ();
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_container_add (GTK_CONTAINER (window), hbox);
provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (provider, css, -1);
gtk_style_context_add_provider_for_display (gtk_widget_get_display (window),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
list = gtk_list_box_new ();
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (hbox), vbox);
combo = gtk_combo_box_text_new ();
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo),
"GTK_SELECTION_NONE");
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo),
"GTK_SELECTION_SINGLE");
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo),
"GTK_SELECTION_BROWSE");
g_signal_connect (combo, "changed", G_CALLBACK (selection_mode_changed), list);
gtk_container_add (GTK_CONTAINER (vbox), combo);
gtk_combo_box_set_active (GTK_COMBO_BOX (combo), gtk_list_box_get_selection_mode (GTK_LIST_BOX (list)));
check = gtk_check_button_new_with_label ("single click mode");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), gtk_list_box_get_activate_on_single_click (GTK_LIST_BOX (list)));
g_signal_connect (check, "toggled", G_CALLBACK (single_click_clicked), list);
gtk_container_add (GTK_CONTAINER (vbox), check);
scrolled = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (scrolled), list);
gtk_container_add (GTK_CONTAINER (hbox), scrolled);
g_signal_connect (list, "row-activated", G_CALLBACK (row_activated_cb), NULL);
g_signal_connect (list, "row-selected", G_CALLBACK (row_selected_cb), NULL);
row = row_new ("blah4", 4);
gtk_container_add (GTK_CONTAINER (list), row);
row3 = row = row_new ("blah3", 3);
gtk_container_add (GTK_CONTAINER (list), row);
row = row_new ("blah1", 1);
gtk_container_add (GTK_CONTAINER (list), row);
row = row_new ("blah2", 2);
gtk_container_add (GTK_CONTAINER (list), row);
row = row_new (NULL, 0);
row_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
row_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
l = gtk_label_new ("da box for da man");
gtk_container_add (GTK_CONTAINER (row_hbox), l);
check = gtk_check_button_new ();
gtk_container_add (GTK_CONTAINER (row_hbox), check);
button = gtk_button_new_with_label ("ya!");
gtk_container_add (GTK_CONTAINER (row_hbox), button);
gtk_container_add (GTK_CONTAINER (row_vbox), row_hbox);
check = gtk_check_button_new ();
gtk_container_add (GTK_CONTAINER (row_vbox), check);
gtk_container_add (GTK_CONTAINER (row), row_vbox);
gtk_container_add (GTK_CONTAINER (list), row);
row = row_new (NULL, 0);
button = gtk_button_new_with_label ("focusable row");
gtk_widget_set_hexpand (button, FALSE);
gtk_widget_set_halign (button, GTK_ALIGN_START);
gtk_container_add (GTK_CONTAINER (row), button);
gtk_container_add (GTK_CONTAINER (list), row);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (hbox), vbox);
button = gtk_button_new_with_label ("sort");
gtk_container_add (GTK_CONTAINER (vbox), button);
g_signal_connect (button, "clicked", G_CALLBACK (sort_clicked_cb), list);
button = gtk_button_new_with_label ("reverse");
gtk_container_add (GTK_CONTAINER (vbox), button);
g_signal_connect (button, "clicked", G_CALLBACK (reverse_sort_clicked_cb), list);
button = gtk_button_new_with_label ("change");
gtk_container_add (GTK_CONTAINER (vbox), button);
g_signal_connect (button, "clicked", G_CALLBACK (change_clicked_cb), row3);
button = gtk_button_new_with_label ("filter");
gtk_container_add (GTK_CONTAINER (vbox), button);
g_signal_connect (button, "clicked", G_CALLBACK (filter_clicked_cb), list);
button = gtk_button_new_with_label ("unfilter");
gtk_container_add (GTK_CONTAINER (vbox), button);
g_signal_connect (button, "clicked", G_CALLBACK (unfilter_clicked_cb), list);
button = gtk_button_new_with_label ("add");
gtk_container_add (GTK_CONTAINER (vbox), button);
g_signal_connect (button, "clicked", G_CALLBACK (add_clicked_cb), list);
button = gtk_button_new_with_label ("separate");
gtk_container_add (GTK_CONTAINER (vbox), button);
g_signal_connect (button, "clicked", G_CALLBACK (separate_clicked_cb), list);
button = gtk_button_new_with_label ("unseparate");
gtk_container_add (GTK_CONTAINER (vbox), button);
g_signal_connect (button, "clicked", G_CALLBACK (unseparate_clicked_cb), list);
button = gtk_button_new_with_label ("visibility");
gtk_container_add (GTK_CONTAINER (vbox), button);
g_signal_connect (button, "clicked", G_CALLBACK (visibility_clicked_cb), row3);
gtk_widget_show (window);
while (TRUE)
g_main_context_iteration (NULL, TRUE);
return 0;
}
|