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
|
/*
* 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 "gtksorterprivate.h"
#include <glib/gi18n-lib.h>
#include "gtktypebuiltins.h"
#include "gtkprivate.h"
/**
* GtkSorter:
*
* Describes sorting criteria for a [class@Gtk.SortListModel].
*
* Its primary user is [class@Gtk.SortListModel]
*
* The model will use a sorter to determine the order in which
* its items should appear by calling [method@Gtk.Sorter.compare]
* for pairs of items.
*
* Sorters may change their sorting behavior through their lifetime.
* In that case, they will emit the [signal@Gtk.Sorter::changed] signal
* to notify that the sort order is no longer valid and should be updated
* by calling gtk_sorter_compare() again.
*
* GTK provides various pre-made sorter implementations for common sorting
* operations. [class@Gtk.ColumnView] has built-in support for sorting lists
* via the [property@Gtk.ColumnViewColumn:sorter] property, where the user can
* change the sorting by clicking on list headers.
*
* Of course, in particular for large lists, it is also possible to subclass
* `GtkSorter` and provide one's own sorter.
*/
typedef struct _GtkSorterPrivate GtkSorterPrivate;
typedef struct _GtkDefaultSortKeys GtkDefaultSortKeys;
struct _GtkSorterPrivate
{
GtkSortKeys *keys;
};
struct _GtkDefaultSortKeys
{
GtkSortKeys keys;
GtkSorter *sorter;
};
enum {
CHANGED,
LAST_SIGNAL
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkSorter, gtk_sorter, G_TYPE_OBJECT)
static guint signals[LAST_SIGNAL] = { 0 };
static GtkOrdering
gtk_sorter_default_compare (GtkSorter *self,
gpointer item1,
gpointer item2)
{
g_critical ("Sorter of type '%s' does not implement GtkSorter::compare", G_OBJECT_TYPE_NAME (self));
return GTK_ORDERING_EQUAL;
}
static GtkSorterOrder
gtk_sorter_default_get_order (GtkSorter *self)
{
return GTK_SORTER_ORDER_PARTIAL;
}
static void
gtk_sorter_dispose (GObject *object)
{
GtkSorter *self = GTK_SORTER (object);
GtkSorterPrivate *priv = gtk_sorter_get_instance_private (self);
g_clear_pointer (&priv->keys, gtk_sort_keys_unref);
G_OBJECT_CLASS (gtk_sorter_parent_class)->dispose (object);
}
static void
gtk_sorter_class_init (GtkSorterClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->dispose = gtk_sorter_dispose;
class->compare = gtk_sorter_default_compare;
class->get_order = gtk_sorter_default_get_order;
/**
* GtkSorter::changed:
* @self: The `GtkSorter`
* @change: how the sorter changed
*
* Emitted whenever the sorter changed.
*
* Users of the sorter should then update the sort order
* again via gtk_sorter_compare().
*
* [class@Gtk.SortListModel] handles this signal automatically.
*
* Depending on the @change parameter, it may be possible to update
* the sort order without a full resorting. Refer to the
* [enum@Gtk.SorterChange] documentation for details.
*/
signals[CHANGED] =
g_signal_new (I_("changed"),
G_TYPE_FROM_CLASS (class),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__ENUM,
G_TYPE_NONE, 1,
GTK_TYPE_SORTER_CHANGE);
g_signal_set_va_marshaller (signals[CHANGED],
G_TYPE_FROM_CLASS (class),
g_cclosure_marshal_VOID__ENUMv);
}
static void
gtk_sorter_init (GtkSorter *self)
{
}
/**
* gtk_sorter_compare:
* @self: a `GtkSorter`
* @item1: (type GObject) (transfer none): first item to compare
* @item2: (type GObject) (transfer none): second item to compare
*
* Compares two given items according to the sort order implemented
* by the sorter.
*
* Sorters implement a partial order:
*
* * It is reflexive, ie a = a
* * It is antisymmetric, ie if a < b and b < a, then a = b
* * It is transitive, ie given any 3 items with a ≤ b and b ≤ c,
* then a ≤ c
*
* The sorter may signal it conforms to additional constraints
* via the return value of [method@Gtk.Sorter.get_order].
*
* Returns: %GTK_ORDERING_EQUAL if @item1 == @item2,
* %GTK_ORDERING_SMALLER if @item1 < @item2,
* %GTK_ORDERING_LARGER if @item1 > @item2
*/
GtkOrdering
gtk_sorter_compare (GtkSorter *self,
gpointer item1,
gpointer item2)
{
GtkOrdering result;
/* We turn this off because gtk_sorter_compare() is called so much that it's too expensive */
/* g_return_val_if_fail (GTK_IS_SORTER (self), GTK_ORDERING_EQUAL); */
g_return_val_if_fail (item1 && item2, GTK_ORDERING_EQUAL);
if (item1 == item2)
return GTK_ORDERING_EQUAL;
result = GTK_SORTER_GET_CLASS (self)->compare (self, item1, item2);
#ifdef G_ENABLE_DEBUG
if (result < -1 || result > 1)
{
g_critical ("A sorter of type \"%s\" returned %d, which is not a valid GtkOrdering result.\n"
"Did you forget to call gtk_ordering_from_cmpfunc()?",
G_OBJECT_TYPE_NAME (self), (int) result);
}
#endif
return result;
}
/**
* gtk_sorter_get_order:
* @self: a `GtkSorter`
*
* Gets the order that @self conforms to.
*
* See [enum@Gtk.SorterOrder] for details
* of the possible return values.
*
* This function is intended to allow optimizations.
*
* Returns: The order
*/
GtkSorterOrder
gtk_sorter_get_order (GtkSorter *self)
{
g_return_val_if_fail (GTK_IS_SORTER (self), GTK_SORTER_ORDER_PARTIAL);
return GTK_SORTER_GET_CLASS (self)->get_order (self);
}
static int
gtk_default_sort_keys_compare (gconstpointer a,
gconstpointer b,
gpointer data)
{
GtkDefaultSortKeys *self = data;
gpointer *key_a = (gpointer *) a;
gpointer *key_b = (gpointer *) b;
return gtk_sorter_compare (self->sorter, *key_a, *key_b);
}
static void
gtk_default_sort_keys_free (GtkSortKeys *keys)
{
GtkDefaultSortKeys *self = (GtkDefaultSortKeys *) keys;
g_object_unref (self->sorter);
g_free (self);
}
static gboolean
gtk_default_sort_keys_is_compatible (GtkSortKeys *keys,
GtkSortKeys *other)
{
if (keys->klass != other->klass)
return FALSE;
return TRUE;
}
static void
gtk_default_sort_keys_init_key (GtkSortKeys *self,
gpointer item,
gpointer key_memory)
{
gpointer *key = (gpointer *) key_memory;
*key = g_object_ref (item);
}
static void
gtk_default_sort_keys_clear_key (GtkSortKeys *self,
gpointer key_memory)
{
gpointer *key = (gpointer *) key_memory;
g_object_unref (*key);
}
static const GtkSortKeysClass GTK_DEFAULT_SORT_KEYS_CLASS =
{
gtk_default_sort_keys_free,
gtk_default_sort_keys_compare,
gtk_default_sort_keys_is_compatible,
gtk_default_sort_keys_init_key,
gtk_default_sort_keys_clear_key,
};
/*<private>
* gtk_sorter_get_keys:
* @self: a `GtkSorter`
*
* Gets a `GtkSortKeys` that can be used as an alternative to
* @self for faster sorting.
*
* The sort keys can change every time [signal@Gtk.Sorter::changed]
* is emitted. When the keys change, you should redo all comparisons
* with the new keys.
*
* When [method@Gtk.SortKeys.is_compatible] for the old and new keys
* returns %TRUE, you can reuse keys you generated previously.
*
* Returns: (transfer full): the sort keys to sort with
*/
GtkSortKeys *
gtk_sorter_get_keys (GtkSorter *self)
{
GtkSorterPrivate *priv = gtk_sorter_get_instance_private (self);
GtkDefaultSortKeys *fallback;
g_return_val_if_fail (GTK_IS_SORTER (self), NULL);
if (priv->keys)
return gtk_sort_keys_ref (priv->keys);
fallback = gtk_sort_keys_new (GtkDefaultSortKeys, >K_DEFAULT_SORT_KEYS_CLASS, sizeof (gpointer), G_ALIGNOF (gpointer));
fallback->sorter = g_object_ref (self);
return (GtkSortKeys *) fallback;
}
/**
* gtk_sorter_changed:
* @self: a `GtkSorter`
* @change: How the sorter changed
*
* Notifies all users of the sorter that it has changed.
*
* This emits the [signal@Gtk.Sorter::changed] signal. Users
* of the sorter should then update the sort order via
* [method@Gtk.Sorter.compare].
*
* Depending on the @change parameter, it may be possible to
* update the sort order without a full resorting. Refer to
* the [enum@Gtk.SorterChange] documentation for details.
*
* This function is intended for implementers of `GtkSorter`
* subclasses and should not be called from other functions.
*/
void
gtk_sorter_changed (GtkSorter *self,
GtkSorterChange change)
{
g_return_if_fail (GTK_IS_SORTER (self));
g_signal_emit (self, signals[CHANGED], 0, change);
}
/*<private>
* gtk_sorter_changed_with_keys:
* @self: a `GtkSorter`
* @change: How the sorter changed
* @keys: (not nullable) (transfer full): New keys to use
*
* Updates the sorter's keys to @keys and then calls gtk_sorter_changed().
*
* If you do not want to update the keys, call that function instead.
*
* This function should also be called in your_sorter_init() to initialize
* the keys to use with your sorter.
*/
void
gtk_sorter_changed_with_keys (GtkSorter *self,
GtkSorterChange change,
GtkSortKeys *keys)
{
GtkSorterPrivate *priv = gtk_sorter_get_instance_private (self);
g_return_if_fail (GTK_IS_SORTER (self));
g_return_if_fail (keys != NULL);
g_clear_pointer (&priv->keys, gtk_sort_keys_unref);
priv->keys = keys;
gtk_sorter_changed (self, change);
}
/* See the comment in gtkenums.h as to why we need to play
* games with the introspection scanner for static inline
* functions
*/
#ifdef __GI_SCANNER__
/**
* gtk_ordering_from_cmpfunc: (skip)
* @cmpfunc_result: Result of a comparison function
*
* Converts the result of a `GCompareFunc` like strcmp() to a
* `GtkOrdering` value.
*
* Returns: the corresponding `GtkOrdering`
*
* Since: 4.2
**/
GtkOrdering
gtk_ordering_from_cmpfunc (int cmpfunc_result)
{
return (GtkOrdering) ((cmpfunc_result > 0) - (cmpfunc_result < 0));
}
#endif
|