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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
/*
* 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 "gtknumericsorter.h"
#include "gtkintl.h"
#include "gtksorterprivate.h"
#include "gtktypebuiltins.h"
#include <math.h>
/**
* GtkNumericSorter:
*
* `GtkNumericSorter` is a `GtkSorter` that compares numbers.
*
* To obtain the numbers to compare, this sorter evaluates a
* [class@Gtk.Expression].
*/
struct _GtkNumericSorter
{
GtkSorter parent_instance;
GtkSortType sort_order;
GtkExpression *expression;
};
enum {
PROP_0,
PROP_EXPRESSION,
PROP_SORT_ORDER,
NUM_PROPERTIES
};
G_DEFINE_TYPE (GtkNumericSorter, gtk_numeric_sorter, GTK_TYPE_SORTER)
static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
#define DO_COMPARISON(_result, _type, _getter, _order) G_STMT_START{ \
_type num1 = _getter (&value1); \
_type num2 = _getter (&value2); \
\
if (num1 < num2) \
_result = _order == GTK_SORT_ASCENDING ? GTK_ORDERING_SMALLER : GTK_ORDERING_LARGER; \
else if (num1 > num2) \
_result = _order == GTK_SORT_ASCENDING ? GTK_ORDERING_LARGER : GTK_ORDERING_SMALLER; \
else \
_result = GTK_ORDERING_EQUAL; \
}G_STMT_END
typedef struct _GtkNumericSortKeys GtkNumericSortKeys;
struct _GtkNumericSortKeys
{
GtkSortKeys keys;
GtkExpression *expression;
};
static void
gtk_numeric_sort_keys_free (GtkSortKeys *keys)
{
GtkNumericSortKeys *self = (GtkNumericSortKeys *) keys;
gtk_expression_unref (self->expression);
g_slice_free (GtkNumericSortKeys, self);
}
#define COMPARE_FUNC(type, name, _a, _b) \
static int \
gtk_ ## type ## _sort_keys_compare_ ## name (gconstpointer a, \
gconstpointer b, \
gpointer unused) \
{ \
type num1 = *(type *) _a; \
type num2 = *(type *) _b; \
\
if (num1 < num2) \
return GTK_ORDERING_SMALLER; \
else if (num1 > num2) \
return GTK_ORDERING_LARGER; \
else \
return GTK_ORDERING_EQUAL; \
}
#define COMPARE_FUNCS(type) \
COMPARE_FUNC(type, ascending, a, b) \
COMPARE_FUNC(type, descending, b, a)
#define FLOAT_COMPARE_FUNC(type, name, _a, _b) \
static int \
gtk_ ## type ## _sort_keys_compare_ ## name (gconstpointer a, \
gconstpointer b, \
gpointer unused) \
{ \
type num1 = *(type *) _a; \
type num2 = *(type *) _b; \
\
if (isnan (num1) && isnan (num2)) \
return GTK_ORDERING_EQUAL; \
else if (isnan (num1)) \
return GTK_ORDERING_LARGER; \
else if (isnan (num2)) \
return GTK_ORDERING_SMALLER; \
else if (num1 < num2) \
return GTK_ORDERING_SMALLER; \
else if (num1 > num2) \
return GTK_ORDERING_LARGER; \
else \
return GTK_ORDERING_EQUAL; \
}
#define FLOAT_COMPARE_FUNCS(type) \
FLOAT_COMPARE_FUNC(type, ascending, a, b) \
FLOAT_COMPARE_FUNC(type, descending, b, a)
COMPARE_FUNCS(char)
COMPARE_FUNCS(guchar)
COMPARE_FUNCS(int)
COMPARE_FUNCS(guint)
FLOAT_COMPARE_FUNCS(float)
FLOAT_COMPARE_FUNCS(double)
COMPARE_FUNCS(long)
COMPARE_FUNCS(gulong)
COMPARE_FUNCS(gint64)
COMPARE_FUNCS(guint64)
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
#define NUMERIC_SORT_KEYS(TYPE, key_type, type, default_value) \
static void \
gtk_ ## type ## _sort_keys_init_key (GtkSortKeys *keys, \
gpointer item, \
gpointer key_memory) \
{ \
GtkNumericSortKeys *self = (GtkNumericSortKeys *) keys; \
key_type *key = (key_type *) key_memory; \
GValue value = G_VALUE_INIT; \
\
if (gtk_expression_evaluate (self->expression, item, &value)) \
*key = g_value_get_ ## type (&value); \
else \
*key = default_value; \
\
g_value_unset (&value); \
} \
\
static gboolean \
gtk_ ## type ## _sort_keys_is_compatible (GtkSortKeys *keys, \
GtkSortKeys *other); \
\
static const GtkSortKeysClass GTK_ASCENDING_ ## TYPE ## _SORT_KEYS_CLASS = \
{ \
gtk_numeric_sort_keys_free, \
gtk_ ## key_type ## _sort_keys_compare_ascending, \
gtk_ ## type ## _sort_keys_is_compatible, \
gtk_ ## type ## _sort_keys_init_key, \
NULL \
}; \
\
static const GtkSortKeysClass GTK_DESCENDING_ ## TYPE ## _SORT_KEYS_CLASS = \
{ \
gtk_numeric_sort_keys_free, \
gtk_ ## key_type ## _sort_keys_compare_descending, \
gtk_ ## type ## _sort_keys_is_compatible, \
gtk_ ## type ## _sort_keys_init_key, \
NULL \
}; \
\
static gboolean \
gtk_ ## type ## _sort_keys_is_compatible (GtkSortKeys *keys, \
GtkSortKeys *other) \
{ \
GtkNumericSorter *self = (GtkNumericSorter *) keys; \
GtkNumericSorter *compare = (GtkNumericSorter *) other; \
\
if (other->klass != >K_ASCENDING_ ## TYPE ## _SORT_KEYS_CLASS && \
other->klass != >K_DESCENDING_ ## TYPE ## _SORT_KEYS_CLASS) \
return FALSE; \
\
return self->expression == compare->expression; \
}
NUMERIC_SORT_KEYS(BOOLEAN, char, boolean, FALSE)
NUMERIC_SORT_KEYS(CHAR, char, char, G_MININT8)
NUMERIC_SORT_KEYS(UCHAR, guchar, uchar, G_MAXUINT8)
NUMERIC_SORT_KEYS(INT, int, int, G_MININT)
NUMERIC_SORT_KEYS(UINT, guint, uint, G_MAXUINT)
NUMERIC_SORT_KEYS(FLOAT, float, float, NAN)
NUMERIC_SORT_KEYS(DOUBLE, double, double, NAN)
NUMERIC_SORT_KEYS(LONG, long, long, G_MINLONG)
NUMERIC_SORT_KEYS(ULONG, gulong, ulong, G_MAXLONG)
NUMERIC_SORT_KEYS(INT64, gint64, int64, G_MININT64)
NUMERIC_SORT_KEYS(UINT64, guint64, uint64, G_MAXUINT64)
G_GNUC_END_IGNORE_DEPRECATIONS
static GtkSortKeys *
gtk_numeric_sort_keys_new (GtkNumericSorter *self)
{
GtkNumericSortKeys *result;
if (self->expression == NULL)
return gtk_sort_keys_new_equal ();
switch (gtk_expression_get_value_type (self->expression))
{
case G_TYPE_BOOLEAN:
result = gtk_sort_keys_new (GtkNumericSortKeys,
self->sort_order == GTK_SORT_ASCENDING
? >K_ASCENDING_BOOLEAN_SORT_KEYS_CLASS
: >K_DESCENDING_BOOLEAN_SORT_KEYS_CLASS,
sizeof (char),
sizeof (char));
break;
case G_TYPE_CHAR:
result = gtk_sort_keys_new (GtkNumericSortKeys,
self->sort_order == GTK_SORT_ASCENDING
? >K_ASCENDING_CHAR_SORT_KEYS_CLASS
: >K_DESCENDING_CHAR_SORT_KEYS_CLASS,
sizeof (char),
sizeof (char));
break;
case G_TYPE_UCHAR:
result = gtk_sort_keys_new (GtkNumericSortKeys,
self->sort_order == GTK_SORT_ASCENDING
? >K_ASCENDING_UCHAR_SORT_KEYS_CLASS
: >K_DESCENDING_UCHAR_SORT_KEYS_CLASS,
sizeof (guchar),
sizeof (guchar));
break;
case G_TYPE_INT:
result = gtk_sort_keys_new (GtkNumericSortKeys,
self->sort_order == GTK_SORT_ASCENDING
? >K_ASCENDING_INT_SORT_KEYS_CLASS
: >K_DESCENDING_INT_SORT_KEYS_CLASS,
sizeof (int),
sizeof (int));
break;
case G_TYPE_UINT:
result = gtk_sort_keys_new (GtkNumericSortKeys,
self->sort_order == GTK_SORT_ASCENDING
? >K_ASCENDING_UINT_SORT_KEYS_CLASS
: >K_DESCENDING_UINT_SORT_KEYS_CLASS,
sizeof (guint),
sizeof (guint));
break;
case G_TYPE_FLOAT:
result = gtk_sort_keys_new (GtkNumericSortKeys,
self->sort_order == GTK_SORT_ASCENDING
? >K_ASCENDING_FLOAT_SORT_KEYS_CLASS
: >K_DESCENDING_FLOAT_SORT_KEYS_CLASS,
sizeof (float),
sizeof (float));
break;
case G_TYPE_DOUBLE:
result = gtk_sort_keys_new (GtkNumericSortKeys,
self->sort_order == GTK_SORT_ASCENDING
? >K_ASCENDING_DOUBLE_SORT_KEYS_CLASS
: >K_DESCENDING_DOUBLE_SORT_KEYS_CLASS,
sizeof (double),
sizeof (double));
break;
case G_TYPE_LONG:
result = gtk_sort_keys_new (GtkNumericSortKeys,
self->sort_order == GTK_SORT_ASCENDING
? >K_ASCENDING_LONG_SORT_KEYS_CLASS
: >K_DESCENDING_LONG_SORT_KEYS_CLASS,
sizeof (long),
sizeof (long));
break;
case G_TYPE_ULONG:
result = gtk_sort_keys_new (GtkNumericSortKeys,
self->sort_order == GTK_SORT_ASCENDING
? >K_ASCENDING_ULONG_SORT_KEYS_CLASS
: >K_DESCENDING_ULONG_SORT_KEYS_CLASS,
sizeof (gulong),
sizeof (gulong));
break;
case G_TYPE_INT64:
result = gtk_sort_keys_new (GtkNumericSortKeys,
self->sort_order == GTK_SORT_ASCENDING
? >K_ASCENDING_INT64_SORT_KEYS_CLASS
: >K_DESCENDING_INT64_SORT_KEYS_CLASS,
sizeof (gint64),
sizeof (gint64));
break;
case G_TYPE_UINT64:
result = gtk_sort_keys_new (GtkNumericSortKeys,
self->sort_order == GTK_SORT_ASCENDING
? >K_ASCENDING_UINT64_SORT_KEYS_CLASS
: >K_DESCENDING_UINT64_SORT_KEYS_CLASS,
sizeof (guint64),
sizeof (guint64));
break;
default:
g_critical ("Invalid value type %s for expression\n", g_type_name (gtk_expression_get_value_type (self->expression)));
return gtk_sort_keys_new_equal ();
}
result->expression = gtk_expression_ref (self->expression);
return (GtkSortKeys *) result;
}
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
static GtkOrdering
gtk_numeric_sorter_compare (GtkSorter *sorter,
gpointer item1,
gpointer item2)
{
GtkNumericSorter *self = GTK_NUMERIC_SORTER (sorter);
GValue value1 = G_VALUE_INIT;
GValue value2 = G_VALUE_INIT;
gboolean res1, res2;
GtkOrdering result;
if (self->expression == NULL)
return GTK_ORDERING_EQUAL;
res1 = gtk_expression_evaluate (self->expression, item1, &value1);
res2 = gtk_expression_evaluate (self->expression, item2, &value2);
/* If items don't evaluate, order them at the end, so they aren't
* in the way. */
if (!res1)
{
result = res2 ? GTK_ORDERING_LARGER : GTK_ORDERING_EQUAL;
goto out;
}
else if (!res2)
{
result = GTK_ORDERING_SMALLER;
goto out;
}
switch (g_type_fundamental (G_VALUE_TYPE (&value1)))
{
case G_TYPE_BOOLEAN:
DO_COMPARISON (result, gboolean, g_value_get_boolean, self->sort_order);
break;
case G_TYPE_CHAR:
DO_COMPARISON (result, char, g_value_get_char, self->sort_order);
break;
case G_TYPE_UCHAR:
DO_COMPARISON (result, guchar, g_value_get_uchar, self->sort_order);
break;
case G_TYPE_INT:
DO_COMPARISON (result, int, g_value_get_int, self->sort_order);
break;
case G_TYPE_UINT:
DO_COMPARISON (result, guint, g_value_get_uint, self->sort_order);
break;
case G_TYPE_FLOAT:
{
float num1 = g_value_get_float (&value1);
float num2 = g_value_get_float (&value2);
if (isnan (num1) && isnan (num2))
result = GTK_ORDERING_EQUAL;
else if (isnan (num1))
result = self->sort_order == GTK_SORT_ASCENDING ? GTK_ORDERING_LARGER : GTK_ORDERING_SMALLER;
else if (isnan (num2))
result = self->sort_order == GTK_SORT_ASCENDING ? GTK_ORDERING_SMALLER : GTK_ORDERING_LARGER;
else if (num1 < num2)
result = self->sort_order == GTK_SORT_ASCENDING ? GTK_ORDERING_SMALLER : GTK_ORDERING_LARGER;
else if (num1 > num2)
result = self->sort_order == GTK_SORT_ASCENDING ? GTK_ORDERING_LARGER : GTK_ORDERING_SMALLER;
else
result = GTK_ORDERING_EQUAL;
break;
}
case G_TYPE_DOUBLE:
{
double num1 = g_value_get_double (&value1);
double num2 = g_value_get_double (&value2);
if (isnan (num1) && isnan (num2))
result = GTK_ORDERING_EQUAL;
else if (isnan (num1))
result = self->sort_order == GTK_SORT_ASCENDING ? GTK_ORDERING_LARGER : GTK_ORDERING_SMALLER;
else if (isnan (num2))
result = self->sort_order == GTK_SORT_ASCENDING ? GTK_ORDERING_SMALLER : GTK_ORDERING_LARGER;
else if (num1 < num2)
result = self->sort_order == GTK_SORT_ASCENDING ? GTK_ORDERING_SMALLER : GTK_ORDERING_LARGER;
else if (num1 > num2)
result = self->sort_order == GTK_SORT_ASCENDING ? GTK_ORDERING_LARGER : GTK_ORDERING_SMALLER;
else
result = GTK_ORDERING_EQUAL;
break;
}
case G_TYPE_LONG:
DO_COMPARISON (result, long, g_value_get_long, self->sort_order);
break;
case G_TYPE_ULONG:
DO_COMPARISON (result, gulong, g_value_get_ulong, self->sort_order);
break;
case G_TYPE_INT64:
DO_COMPARISON (result, gint64, g_value_get_int64, self->sort_order);
break;
case G_TYPE_UINT64:
DO_COMPARISON (result, guint64, g_value_get_uint64, self->sort_order);
break;
default:
g_critical ("Invalid value type %s for expression\n", g_type_name (gtk_expression_get_value_type (self->expression)));
result = GTK_ORDERING_EQUAL;
break;
}
out:
g_value_unset (&value1);
g_value_unset (&value2);
return result;
}
G_GNUC_END_IGNORE_DEPRECATIONS
static GtkSorterOrder
gtk_numeric_sorter_get_order (GtkSorter *sorter)
{
GtkNumericSorter *self = GTK_NUMERIC_SORTER (sorter);
if (self->expression == NULL)
return GTK_SORTER_ORDER_NONE;
return GTK_SORTER_ORDER_PARTIAL;
}
static void
gtk_numeric_sorter_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkNumericSorter *self = GTK_NUMERIC_SORTER (object);
switch (prop_id)
{
case PROP_EXPRESSION:
gtk_numeric_sorter_set_expression (self, gtk_value_get_expression (value));
break;
case PROP_SORT_ORDER:
gtk_numeric_sorter_set_sort_order (self, g_value_get_enum (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_numeric_sorter_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkNumericSorter *self = GTK_NUMERIC_SORTER (object);
switch (prop_id)
{
case PROP_EXPRESSION:
gtk_value_set_expression (value, self->expression);
break;
case PROP_SORT_ORDER:
g_value_set_enum (value, self->sort_order);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_numeric_sorter_dispose (GObject *object)
{
GtkNumericSorter *self = GTK_NUMERIC_SORTER (object);
g_clear_pointer (&self->expression, gtk_expression_unref);
G_OBJECT_CLASS (gtk_numeric_sorter_parent_class)->dispose (object);
}
static void
gtk_numeric_sorter_class_init (GtkNumericSorterClass *class)
{
GtkSorterClass *sorter_class = GTK_SORTER_CLASS (class);
GObjectClass *object_class = G_OBJECT_CLASS (class);
sorter_class->compare = gtk_numeric_sorter_compare;
sorter_class->get_order = gtk_numeric_sorter_get_order;
object_class->get_property = gtk_numeric_sorter_get_property;
object_class->set_property = gtk_numeric_sorter_set_property;
object_class->dispose = gtk_numeric_sorter_dispose;
/**
* GtkNumericSorter:expression: (type GtkExpression) (attributes org.gtk.Property.get=gtk_numeric_sorter_get_expression org.gtk.Property.set=gtk_numeric_sorter_set_expression)
*
* The expression to evaluate on items to get a number to compare with.
*/
properties[PROP_EXPRESSION] =
gtk_param_spec_expression ("expression", NULL, NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkNumericSorter:sort-order: (attributes org.gtk.Property.get=gtk_numeric_sorter_get_sort_order org.gtk.Property.set=gtk_numeric_sorter_set_sort_order)
*
* Whether the sorter will sort smaller numbers first.
*/
properties[PROP_SORT_ORDER] =
g_param_spec_enum ("sort-order", NULL, NULL,
GTK_TYPE_SORT_TYPE,
GTK_SORT_ASCENDING,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
}
static void
gtk_numeric_sorter_init (GtkNumericSorter *self)
{
self->sort_order = GTK_SORT_ASCENDING;
gtk_sorter_changed_with_keys (GTK_SORTER (self),
GTK_SORTER_CHANGE_DIFFERENT,
gtk_numeric_sort_keys_new (self));
}
/**
* gtk_numeric_sorter_new:
* @expression: (transfer full) (nullable): The expression to evaluate
*
* Creates a new numeric sorter using the given @expression.
*
* Smaller numbers will be sorted first. You can call
* [method@Gtk.NumericSorter.set_sort_order] to change this.
*
* Returns: a new `GtkNumericSorter`
*/
GtkNumericSorter *
gtk_numeric_sorter_new (GtkExpression *expression)
{
GtkNumericSorter *result;
result = g_object_new (GTK_TYPE_NUMERIC_SORTER,
"expression", expression,
NULL);
g_clear_pointer (&expression, gtk_expression_unref);
return result;
}
/**
* gtk_numeric_sorter_get_expression: (attributes org.gtk.Method.get_property=expression)
* @self: a `GtkNumericSorter`
*
* Gets the expression that is evaluated to obtain numbers from items.
*
* Returns: (transfer none) (nullable): a `GtkExpression`
*/
GtkExpression *
gtk_numeric_sorter_get_expression (GtkNumericSorter *self)
{
g_return_val_if_fail (GTK_IS_NUMERIC_SORTER (self), NULL);
return self->expression;
}
/**
* gtk_numeric_sorter_set_expression: (attributes org.gtk.Method.set_property=expression)
* @self: a `GtkNumericSorter`
* @expression: (nullable) (transfer none): a `GtkExpression`
*
* Sets the expression that is evaluated to obtain numbers from items.
*
* Unless an expression is set on @self, the sorter will always
* compare items as invalid.
*
* The expression must have a return type that can be compared
* numerically, such as %G_TYPE_INT or %G_TYPE_DOUBLE.
*/
void
gtk_numeric_sorter_set_expression (GtkNumericSorter *self,
GtkExpression *expression)
{
g_return_if_fail (GTK_IS_NUMERIC_SORTER (self));
if (self->expression == expression)
return;
g_clear_pointer (&self->expression, gtk_expression_unref);
if (expression)
self->expression = gtk_expression_ref (expression);
gtk_sorter_changed_with_keys (GTK_SORTER (self),
GTK_SORTER_CHANGE_DIFFERENT,
gtk_numeric_sort_keys_new (self));
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_EXPRESSION]);
}
/**
* gtk_numeric_sorter_set_sort_order: (attributes org.gtk.Method.set_property=sort-order)
* @self: a `GtkNumericSorter`
* @sort_order: whether to sort smaller numbers first
*
* Sets whether to sort smaller numbers before larger ones.
*/
void
gtk_numeric_sorter_set_sort_order (GtkNumericSorter *self,
GtkSortType sort_order)
{
g_return_if_fail (GTK_IS_NUMERIC_SORTER (self));
if (self->sort_order == sort_order)
return;
self->sort_order = sort_order;
gtk_sorter_changed_with_keys (GTK_SORTER (self),
GTK_SORTER_CHANGE_INVERTED,
gtk_numeric_sort_keys_new (self));
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SORT_ORDER]);
}
/**
* gtk_numeric_sorter_get_sort_order: (attributes org.gtk.Method.get_property=sort-order)
* @self: a `GtkNumericSorter`
*
* Gets whether this sorter will sort smaller numbers first.
*
* Returns: the order of the numbers
*/
GtkSortType
gtk_numeric_sorter_get_sort_order (GtkNumericSorter *self)
{
g_return_val_if_fail (GTK_IS_NUMERIC_SORTER (self), GTK_SORT_ASCENDING);
return self->sort_order;
}
|