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
|
/*
* Copyright © 2019 Matthias Clasen
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Matthias Clasen <mclasen@redhat.com>
*/
#include "config.h"
#include "gtkmultisorter.h"
#include "gtkbuildable.h"
#include "gtkintl.h"
#include "gtksorterprivate.h"
#include "gtktypebuiltins.h"
#define GDK_ARRAY_TYPE_NAME GtkSorters
#define GDK_ARRAY_NAME gtk_sorters
#define GDK_ARRAY_ELEMENT_TYPE GtkSorter *
#define GDK_ARRAY_FREE_FUNC g_object_unref
#include "gdk/gdkarrayimpl.c"
/**
* SECTION:gtkmultisorter
* @Title: GtkMultiSorter
* @Short_description: Combining multiple sorters
*
* GtkMultiSorter combines multiple sorters by trying them
* in turn. If the first sorter compares two items as equal,
* the second is tried next, and so on.
*/
struct _GtkMultiSorter
{
GtkSorter parent_instance;
GtkSorters sorters;
};
typedef struct _GtkMultiSortKey GtkMultiSortKey;
typedef struct _GtkMultiSortKeys GtkMultiSortKeys;
struct _GtkMultiSortKey
{
gsize offset;
GtkSortKeys *keys;
};
struct _GtkMultiSortKeys
{
GtkSortKeys parent_keys;
guint n_keys;
GtkMultiSortKey keys[];
};
static void
gtk_multi_sort_keys_free (GtkSortKeys *keys)
{
GtkMultiSortKeys *self = (GtkMultiSortKeys *) keys;
gsize i;
for (i = 0; i < self->n_keys; i++)
gtk_sort_keys_unref (self->keys[i].keys);
g_slice_free1 (sizeof (GtkMultiSortKeys) + self->n_keys * sizeof (GtkMultiSortKey), self);
}
static int
gtk_multi_sort_keys_compare (gconstpointer a,
gconstpointer b,
gpointer data)
{
GtkMultiSortKeys *self = (GtkMultiSortKeys *) data;
gsize i;
for (i = 0; i < self->n_keys; i++)
{
GtkOrdering result = gtk_sort_keys_compare (self->keys[i].keys,
((const char *) a) + self->keys[i].offset,
((const char *) b) + self->keys[i].offset);
if (result != GTK_ORDERING_EQUAL)
return result;
}
return GTK_ORDERING_EQUAL;
}
static gboolean
gtk_multi_sort_keys_is_compatible (GtkSortKeys *keys,
GtkSortKeys *other)
{
GtkMultiSortKeys *self = (GtkMultiSortKeys *) keys;
GtkMultiSortKeys *compare = (GtkMultiSortKeys *) other;
gsize i;
if (keys->klass != other->klass)
return FALSE;
if (self->n_keys != compare->n_keys)
return FALSE;
for (i = 0; i < self->n_keys; i++)
{
if (!gtk_sort_keys_is_compatible (self->keys[i].keys, compare->keys[i].keys))
return FALSE;
}
return TRUE;
}
static void
gtk_multi_sort_keys_init_key (GtkSortKeys *keys,
gpointer item,
gpointer key_memory)
{
GtkMultiSortKeys *self = (GtkMultiSortKeys *) keys;
char *key = (char *) key_memory;
gsize i;
for (i = 0; i < self->n_keys; i++)
gtk_sort_keys_init_key (self->keys[i].keys, item, key + self->keys[i].offset);
}
static void
gtk_multi_sort_keys_clear_key (GtkSortKeys *keys,
gpointer key_memory)
{
GtkMultiSortKeys *self = (GtkMultiSortKeys *) keys;
char *key = (char *) key_memory;
gsize i;
for (i = 0; i < self->n_keys; i++)
gtk_sort_keys_clear_key (self->keys[i].keys, key + self->keys[i].offset);
}
static const GtkSortKeysClass GTK_MULTI_SORT_KEYS_CLASS =
{
gtk_multi_sort_keys_free,
gtk_multi_sort_keys_compare,
gtk_multi_sort_keys_is_compatible,
gtk_multi_sort_keys_init_key,
gtk_multi_sort_keys_clear_key,
};
static GtkSortKeys *
gtk_multi_sort_keys_new (GtkMultiSorter *self)
{
GtkMultiSortKeys *result;
GtkSortKeys *keys;
gsize i;
if (gtk_sorters_get_size (&self->sorters) == 0)
return gtk_sort_keys_new_equal ();
else if (gtk_sorters_get_size (&self->sorters) == 1)
return gtk_sorter_get_keys (gtk_sorters_get (&self->sorters, 0));
keys = gtk_sort_keys_alloc (>K_MULTI_SORT_KEYS_CLASS,
sizeof (GtkMultiSortKeys) + gtk_sorters_get_size (&self->sorters) * sizeof (GtkMultiSortKey),
0, 1);
result = (GtkMultiSortKeys *) keys;
result->n_keys = gtk_sorters_get_size (&self->sorters);
for (i = 0; i < result->n_keys; i++)
{
result->keys[i].keys = gtk_sorter_get_keys (gtk_sorters_get (&self->sorters, i));
result->keys[i].offset = GTK_SORT_KEYS_ALIGN (keys->key_size, gtk_sort_keys_get_key_align (result->keys[i].keys));
keys->key_size = result->keys[i].offset + gtk_sort_keys_get_key_size (result->keys[i].keys);
keys->key_align = MAX (keys->key_align, gtk_sort_keys_get_key_align (result->keys[i].keys));
}
return keys;
}
static GType
gtk_multi_sorter_get_item_type (GListModel *list)
{
return GTK_TYPE_SORTER;
}
static guint
gtk_multi_sorter_get_n_items (GListModel *list)
{
GtkMultiSorter *self = GTK_MULTI_SORTER (list);
return gtk_sorters_get_size (&self->sorters);
}
static gpointer
gtk_multi_sorter_get_item (GListModel *list,
guint position)
{
GtkMultiSorter *self = GTK_MULTI_SORTER (list);
if (position < gtk_sorters_get_size (&self->sorters))
return g_object_ref (gtk_sorters_get (&self->sorters, position));
else
return NULL;
}
static void
gtk_multi_sorter_list_model_init (GListModelInterface *iface)
{
iface->get_item_type = gtk_multi_sorter_get_item_type;
iface->get_n_items = gtk_multi_sorter_get_n_items;
iface->get_item = gtk_multi_sorter_get_item;
}
static GtkBuildableIface *parent_buildable_iface;
static void
gtk_multi_sorter_buildable_add_child (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const char *type)
{
if (GTK_IS_SORTER (child))
gtk_multi_sorter_append (GTK_MULTI_SORTER (buildable), g_object_ref (GTK_SORTER (child)));
else
parent_buildable_iface->add_child (buildable, builder, child, type);
}
static void
gtk_multi_sorter_buildable_init (GtkBuildableIface *iface)
{
parent_buildable_iface = g_type_interface_peek_parent (iface);
iface->add_child = gtk_multi_sorter_buildable_add_child;
}
G_DEFINE_TYPE_WITH_CODE (GtkMultiSorter, gtk_multi_sorter, GTK_TYPE_SORTER,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, gtk_multi_sorter_list_model_init)
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, gtk_multi_sorter_buildable_init))
static GtkOrdering
gtk_multi_sorter_compare (GtkSorter *sorter,
gpointer item1,
gpointer item2)
{
GtkMultiSorter *self = GTK_MULTI_SORTER (sorter);
GtkOrdering result = GTK_ORDERING_EQUAL;
guint i;
for (i = 0; i < gtk_sorters_get_size (&self->sorters); i++)
{
GtkSorter *child = gtk_sorters_get (&self->sorters, i);
result = gtk_sorter_compare (child, item1, item2);
if (result != GTK_ORDERING_EQUAL)
break;
}
return result;
}
static GtkSorterOrder
gtk_multi_sorter_get_order (GtkSorter *sorter)
{
GtkMultiSorter *self = GTK_MULTI_SORTER (sorter);
GtkSorterOrder result = GTK_SORTER_ORDER_NONE;
guint i;
for (i = 0; i < gtk_sorters_get_size (&self->sorters); i++)
{
GtkSorter *child = gtk_sorters_get (&self->sorters, i);
GtkSorterOrder child_order;
child_order = gtk_sorter_get_order (child);
switch (child_order)
{
case GTK_SORTER_ORDER_PARTIAL:
result = GTK_SORTER_ORDER_PARTIAL;
break;
case GTK_SORTER_ORDER_NONE:
break;
case GTK_SORTER_ORDER_TOTAL:
return GTK_SORTER_ORDER_TOTAL;
default:
g_assert_not_reached ();
break;
}
}
return result;
}
static void
gtk_multi_sorter_changed_cb (GtkSorter *sorter,
GtkSorterChange change,
GtkMultiSorter *self)
{
/* Using an enum on purpose, so gcc complains about this case if
* new values are added to the enum
*/
switch (change)
{
case GTK_SORTER_CHANGE_INVERTED:
/* This could do a lot better with change handling, in particular in
* cases where self->n_sorters == 1 or if sorter == self->sorters[0]
*/
change = GTK_SORTER_CHANGE_DIFFERENT;
break;
case GTK_SORTER_CHANGE_DIFFERENT:
case GTK_SORTER_CHANGE_LESS_STRICT:
case GTK_SORTER_CHANGE_MORE_STRICT:
break;
default:
g_assert_not_reached ();
change = GTK_SORTER_CHANGE_DIFFERENT;
}
gtk_sorter_changed_with_keys (GTK_SORTER (self),
change,
gtk_multi_sort_keys_new (self));
}
static void
gtk_multi_sorter_dispose (GObject *object)
{
GtkMultiSorter *self = GTK_MULTI_SORTER (object);
guint i;
for (i = 0; i < gtk_sorters_get_size (&self->sorters); i++)
{
GtkSorter *sorter = gtk_sorters_get (&self->sorters, i);
g_signal_handlers_disconnect_by_func (sorter, gtk_multi_sorter_changed_cb, self);
}
gtk_sorters_clear (&self->sorters);
G_OBJECT_CLASS (gtk_multi_sorter_parent_class)->dispose (object);
}
static void
gtk_multi_sorter_class_init (GtkMultiSorterClass *class)
{
GtkSorterClass *sorter_class = GTK_SORTER_CLASS (class);
GObjectClass *object_class = G_OBJECT_CLASS (class);
sorter_class->compare = gtk_multi_sorter_compare;
sorter_class->get_order = gtk_multi_sorter_get_order;
object_class->dispose = gtk_multi_sorter_dispose;
}
static void
gtk_multi_sorter_init (GtkMultiSorter *self)
{
gtk_sorters_init (&self->sorters);
gtk_sorter_changed_with_keys (GTK_SORTER (self),
GTK_SORTER_CHANGE_DIFFERENT,
gtk_multi_sort_keys_new (self));
}
/**
* gtk_multi_sorter_new:
*
* Creates a new multi sorter.
*
* This sorter compares items by trying each of the sorters
* in turn, until one returns non-zero. In particular, if
* no sorter has been added to it, it will always compare
* items as equal.
*
* Returns: a new #GtkMultiSorter
*/
GtkMultiSorter *
gtk_multi_sorter_new (void)
{
return g_object_new (GTK_TYPE_MULTI_SORTER, NULL);
}
/**
* gtk_multi_sorter_append:
* @self: a #GtkMultiSorter
* @sorter: (transfer full): a sorter to add
*
* Add @sorter to @self to use for sorting at the end. @self
* will consult all existing sorters before it will sort with
* the given @sorter.
*/
void
gtk_multi_sorter_append (GtkMultiSorter *self,
GtkSorter *sorter)
{
g_return_if_fail (GTK_IS_MULTI_SORTER (self));
g_return_if_fail (GTK_IS_SORTER (sorter));
g_signal_connect (sorter, "changed", G_CALLBACK (gtk_multi_sorter_changed_cb), self);
gtk_sorters_append (&self->sorters, sorter);
gtk_sorter_changed_with_keys (GTK_SORTER (self),
GTK_SORTER_CHANGE_MORE_STRICT,
gtk_multi_sort_keys_new (self));
}
/**
* gtk_multi_sorter_remove:
* @self: a #GtkMultiSorter
* @position: position of sorter to remove
*
* Removes the sorter at the given @position from the list of sorter
* used by @self.
*
* If @position is larger than the number of sorters, nothing happens.
*/
void
gtk_multi_sorter_remove (GtkMultiSorter *self,
guint position)
{
guint length;
GtkSorter *sorter;
g_return_if_fail (GTK_IS_MULTI_SORTER (self));
length = gtk_sorters_get_size (&self->sorters);
if (position >= length)
return;
sorter = gtk_sorters_get (&self->sorters, position);
g_signal_handlers_disconnect_by_func (sorter, gtk_multi_sorter_changed_cb, self);
gtk_sorters_splice (&self->sorters, position, 1, NULL, 0);
gtk_sorter_changed_with_keys (GTK_SORTER (self),
GTK_SORTER_CHANGE_LESS_STRICT,
gtk_multi_sort_keys_new (self));
}
|