1/*
2 * Copyright © 2020 Benjamin Otte
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "config.h"
19
20#include "gtksortkeysprivate.h"
21
22#include "gtkcssstyleprivate.h"
23#include "gtkstyleproviderprivate.h"
24
25GtkSortKeys *
26gtk_sort_keys_alloc (const GtkSortKeysClass *klass,
27 gsize size,
28 gsize key_size,
29 gsize key_align)
30{
31 GtkSortKeys *self;
32
33 g_return_val_if_fail (key_align > 0, NULL);
34
35 self = g_slice_alloc0 (block_size: size);
36
37 self->klass = klass;
38 self->ref_count = 1;
39
40 self->key_size = key_size;
41 self->key_align = key_align;
42
43 return self;
44}
45
46GtkSortKeys *
47gtk_sort_keys_ref (GtkSortKeys *self)
48{
49 self->ref_count += 1;
50
51 return self;
52}
53
54void
55gtk_sort_keys_unref (GtkSortKeys *self)
56{
57 self->ref_count -= 1;
58 if (self->ref_count > 0)
59 return;
60
61 self->klass->free (self);
62}
63
64gsize
65gtk_sort_keys_get_key_size (GtkSortKeys *self)
66{
67 return self->key_size;
68}
69
70gsize
71gtk_sort_keys_get_key_align (GtkSortKeys *self)
72{
73 return self->key_align;
74}
75
76GCompareDataFunc
77gtk_sort_keys_get_key_compare_func (GtkSortKeys *self)
78{
79 return self->klass->key_compare;
80}
81
82gboolean
83gtk_sort_keys_is_compatible (GtkSortKeys *self,
84 GtkSortKeys *other)
85{
86 if (self == other)
87 return TRUE;
88
89 return self->klass->is_compatible (self, other);
90}
91
92gboolean
93gtk_sort_keys_needs_clear_key (GtkSortKeys *self)
94{
95 return self->klass->clear_key != NULL;
96}
97
98static void
99gtk_equal_sort_keys_free (GtkSortKeys *keys)
100{
101 g_slice_free (GtkSortKeys, keys);
102}
103
104static int
105gtk_equal_sort_keys_compare (gconstpointer a,
106 gconstpointer b,
107 gpointer unused)
108{
109 return GTK_ORDERING_EQUAL;
110}
111
112static gboolean
113gtk_equal_sort_keys_is_compatible (GtkSortKeys *keys,
114 GtkSortKeys *other)
115{
116 return keys->klass == other->klass;
117}
118
119static void
120gtk_equal_sort_keys_init_key (GtkSortKeys *keys,
121 gpointer item,
122 gpointer key_memory)
123{
124}
125
126static const GtkSortKeysClass GTK_EQUAL_SORT_KEYS_CLASS =
127{
128 gtk_equal_sort_keys_free,
129 gtk_equal_sort_keys_compare,
130 gtk_equal_sort_keys_is_compatible,
131 gtk_equal_sort_keys_init_key,
132 NULL
133};
134
135/*<private>
136 * gtk_sort_keys_new_equal:
137 *
138 * Creates a new GtkSortKeys that compares every element as equal.
139 * This is useful when sorters are in an invalid configuration.
140 *
141 * Returns: a new GtkSortKeys
142 **/
143GtkSortKeys *
144gtk_sort_keys_new_equal (void)
145{
146 return gtk_sort_keys_new (GtkSortKeys,
147 &GTK_EQUAL_SORT_KEYS_CLASS,
148 0, 1);
149}
150
151

source code of gtk/gtk/gtksortkeys.c