gtktreemodelsort.c 66.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* gtktreemodelsort.c
 * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
/* NOTE: There is a potential for confusion in this code as to whether an iter,
21 22
 * path or value refers to the GtkTreeModelSort model, or the child model being
 * sorted.  As a convention, variables referencing the child model will have an
23 24 25
 * s_ prefix before them (ie. s_iter, s_value, s_path);
 */

26 27 28 29 30 31 32
/* ITER FORMAT:
 *
 * iter->stamp = tree_model_sort->stamp
 * iter->user_data = SortLevel
 * iter->user_data2 = SortElt
 */

33
#include "gtktreemodelsort.h"
34
#include "gtktreesortable.h"
35
#include "gtktreestore.h"
36
#include "gtksignal.h"
37
#include "gtktreedatalist.h"
38
#include <string.h>
39 40

typedef struct _SortElt SortElt;
41 42 43 44
typedef struct _SortLevel SortLevel;
typedef struct _SortData SortData;
typedef struct _SortTuple SortTuple;

45 46
struct _SortElt
{
47 48 49 50 51 52 53 54 55 56 57 58 59
  GtkTreeIter  iter;
  SortLevel   *children;
  gint         offset;
  gint         ref_count;
  gint         zero_ref_count;
};

struct _SortLevel
{
  GArray    *array;
  gint       ref_count;
  SortElt   *parent_elt;
  SortLevel *parent_level;
60 61
};

Jonathan Blandford's avatar
Jonathan Blandford committed
62 63
struct _SortData
{
64 65 66
  GtkTreeModelSort *tree_model_sort;
  GtkTreePath *parent_a;
  GtkTreePath *parent_b;
Jonathan Blandford's avatar
Jonathan Blandford committed
67 68
};

69 70 71
struct _SortTuple
{
  SortElt   *elt;
72 73
  SortLevel *level;
  SortLevel *children;
74 75
  gint       offset;  
};
76

77 78 79 80 81 82
#define GTK_TREE_MODEL_SORT_CACHE_CHILD_ITERS(tree_model_sort) \
	(((GtkTreeModelSort *)tree_model_sort)->child_flags&GTK_TREE_MODEL_ITERS_PERSIST)
#define SORT_ELT(sort_elt) ((SortElt *)sort_elt)
#define SORT_LEVEL(sort_level) ((SortLevel *)sort_level)

#define GET_CHILD_ITER(tree_model_sort,child_iter,sort_iter) gtk_tree_model_sort_convert_iter_to_child_iter(GTK_TREE_MODEL_SORT (tree_model_sort), child_iter, sort_iter);
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
static void gtk_tree_model_sort_init                  (GtkTreeModelSort      *tree_model_sort);
static void gtk_tree_model_sort_class_init            (GtkTreeModelSortClass *tree_model_sort_class);
static void gtk_tree_model_sort_tree_model_init       (GtkTreeModelIface     *iface);
static void gtk_tree_model_sort_tree_sortable_init    (GtkTreeSortableIface  *iface);
static void gtk_tree_model_sort_finalize              (GObject               *object);
static void gtk_tree_model_sort_row_changed           (GtkTreeModel          *model,
						       GtkTreePath           *start_path,
						       GtkTreeIter           *start_iter,
						       gpointer               data);
static void gtk_tree_model_sort_row_inserted          (GtkTreeModel          *model,
						       GtkTreePath           *path,
						       GtkTreeIter           *iter,
						       gpointer               data);
static void gtk_tree_model_sort_row_has_child_toggled (GtkTreeModel          *model,
						       GtkTreePath           *path,
						       GtkTreeIter           *iter,
						       gpointer               data);
static void gtk_tree_model_sort_row_deleted           (GtkTreeModel          *model,
						       GtkTreePath           *path,
						       gpointer               data);
static void gtk_tree_model_sort_rows_reordered        (GtkTreeModel          *s_model,
						       GtkTreePath           *s_path,
						       GtkTreeIter           *s_iter,
						       gint                  *new_order,
						       gpointer               data);
109
static void gtk_tree_model_sort_destroy               (GtkObject             *gobject);
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static void gtk_tree_model_sort_row_changed           (GtkTreeModel          *model,
						       GtkTreePath           *start_path,
						       GtkTreeIter           *start_iter,
						       gpointer               data);
static void gtk_tree_model_sort_row_inserted          (GtkTreeModel          *model,
						       GtkTreePath           *path,
						       GtkTreeIter           *iter,
						       gpointer               data);
static void gtk_tree_model_sort_row_has_child_toggled (GtkTreeModel          *model,
						       GtkTreePath           *path,
						       GtkTreeIter           *iter,
						       gpointer               data);
static void gtk_tree_model_sort_row_deleted           (GtkTreeModel          *model,
						       GtkTreePath           *path,
						       gpointer               data);
static void gtk_tree_model_sort_rows_reordered        (GtkTreeModel          *s_model,
						       GtkTreePath           *s_path,
						       GtkTreeIter           *s_iter,
						       gint                  *new_order,
						       gpointer               data);
131 132

/* TreeModel interface */
133
static guint        gtk_tree_model_sort_get_flags          (GtkTreeModel          *tree_model);
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
static gint         gtk_tree_model_sort_get_n_columns      (GtkTreeModel          *tree_model);
static GType        gtk_tree_model_sort_get_column_type    (GtkTreeModel          *tree_model,
                                                            gint                   index);
static gboolean     gtk_tree_model_sort_get_iter           (GtkTreeModel          *tree_model,
                                                            GtkTreeIter           *iter,
                                                            GtkTreePath           *path);
static GtkTreePath *gtk_tree_model_sort_get_path           (GtkTreeModel          *tree_model,
                                                            GtkTreeIter           *iter);
static void         gtk_tree_model_sort_get_value          (GtkTreeModel          *tree_model,
                                                            GtkTreeIter           *iter,
                                                            gint                   column,
                                                            GValue                *value);
static gboolean     gtk_tree_model_sort_iter_next          (GtkTreeModel          *tree_model,
                                                            GtkTreeIter           *iter);
static gboolean     gtk_tree_model_sort_iter_children      (GtkTreeModel          *tree_model,
                                                            GtkTreeIter           *iter,
                                                            GtkTreeIter           *parent);
static gboolean     gtk_tree_model_sort_iter_has_child     (GtkTreeModel          *tree_model,
                                                            GtkTreeIter           *iter);
static gint         gtk_tree_model_sort_iter_n_children    (GtkTreeModel          *tree_model,
                                                            GtkTreeIter           *iter);
static gboolean     gtk_tree_model_sort_iter_nth_child     (GtkTreeModel          *tree_model,
                                                            GtkTreeIter           *iter,
                                                            GtkTreeIter           *parent,
                                                            gint                   n);
static gboolean     gtk_tree_model_sort_iter_parent        (GtkTreeModel          *tree_model,
                                                            GtkTreeIter           *iter,
                                                            GtkTreeIter           *child);
static void         gtk_tree_model_sort_ref_node           (GtkTreeModel          *tree_model,
                                                            GtkTreeIter           *iter);
static void         gtk_tree_model_sort_unref_node         (GtkTreeModel          *tree_model,
                                                            GtkTreeIter           *iter);

/* TreeSortable interface */
168 169 170 171 172 173 174 175 176 177 178
static gboolean     gtk_tree_model_sort_get_sort_column_id    (GtkTreeSortable        *sortable,
							       gint                   *sort_column_id,
							       GtkSortType            *order);
static void         gtk_tree_model_sort_set_sort_column_id    (GtkTreeSortable        *sortable,
							       gint                    sort_column_id,
							       GtkSortType        order);
static void         gtk_tree_model_sort_set_sort_func         (GtkTreeSortable        *sortable,
							       gint                    sort_column_id,
							       GtkTreeIterCompareFunc  func,
							       gpointer                data,
							       GtkDestroyNotify        destroy);
179 180 181 182 183
static void         gtk_tree_model_sort_set_default_sort_func (GtkTreeSortable        *sortable,
							       GtkTreeIterCompareFunc  func,
							       gpointer                data,
							       GtkDestroyNotify        destroy);
static gboolean     gtk_tree_model_sort_has_default_sort_func (GtkTreeSortable     *sortable);
184

185
/* Private functions */
186 187 188 189 190 191 192 193 194 195 196
static void         gtk_tree_model_sort_build_level       (GtkTreeModelSort *tree_model_sort,
							   SortLevel        *parent_level,
							   SortElt          *parent_elt);
static void         gtk_tree_model_sort_free_level        (GtkTreeModelSort *tree_model_sort,
							   SortLevel        *sort_level);
static void         gtk_tree_model_sort_increment_stamp   (GtkTreeModelSort *tree_model_sort);
static void         gtk_tree_model_sort_sort_level        (GtkTreeModelSort *tree_model_sort,
							   SortLevel        *level,
							   gboolean          recurse,
							   gboolean          emit_reordered);
static void         gtk_tree_model_sort_sort              (GtkTreeModelSort *tree_model_sort);
197 198 199 200 201
static gint         gtk_tree_model_sort_level_find_insert (GtkTreeModelSort *tree_model_sort,
							   SortLevel        *level,
							   GtkTreeIter      *iter,
							   gboolean          skip_sort_elt);
static gboolean     gtk_tree_model_sort_insert_value      (GtkTreeModelSort *tree_model_sort,
202
							   SortLevel        *level,
203 204
							   GtkTreePath      *s_path,
							   GtkTreeIter      *s_iter);
205 206 207 208 209 210 211 212 213 214 215 216
static GtkTreePath *gtk_tree_model_sort_elt_get_path      (SortLevel        *level,
							   SortElt          *elt);
static void         get_child_iter_from_elt_no_cache      (GtkTreeModelSort *tree_model_sort,
							   GtkTreeIter      *child_iter,
							   SortLevel        *level,
							   SortElt          *elt);
static void         get_child_iter_from_elt               (GtkTreeModelSort *tree_model_sort,
							   GtkTreeIter      *child_iter,
							   SortLevel        *level,
							   SortElt          *elt);
static void         gtk_tree_model_sort_set_model         (GtkTreeModelSort *tree_model_sort,
							   GtkTreeModel     *child_model);
217 218


219
GType
220 221
gtk_tree_model_sort_get_type (void)
{
222
  static GType tree_model_sort_type = 0;
223
  
224 225 226 227 228
  if (!tree_model_sort_type)
    {
      static const GTypeInfo tree_model_sort_info =
      {
        sizeof (GtkTreeModelSortClass),
229 230
        NULL,           /* base_init */
        NULL,           /* base_finalize */
231
        (GClassInitFunc) gtk_tree_model_sort_class_init,
232 233
        NULL,           /* class_finalize */
        NULL,           /* class_data */
234
        sizeof (GtkTreeModelSort),
235
        0,              /* n_preallocs */
236 237 238 239 240
        (GInstanceInitFunc) gtk_tree_model_sort_init
      };

      static const GInterfaceInfo tree_model_info =
      {
241 242 243
        (GInterfaceInitFunc) gtk_tree_model_sort_tree_model_init,
        NULL,
        NULL
244 245
      };

246 247
      static const GInterfaceInfo sortable_info =
      {
248 249 250
        (GInterfaceInitFunc) gtk_tree_model_sort_tree_sortable_init,
        NULL,
        NULL
251 252
      };

253 254 255
      tree_model_sort_type = g_type_register_static (G_TYPE_OBJECT,
						     "GtkTreeModelSort",
						     &tree_model_sort_info, 0);
256
      g_type_add_interface_static (tree_model_sort_type,
257 258
                                   GTK_TYPE_TREE_MODEL,
                                   &tree_model_info);
259
      
260
      g_type_add_interface_static (tree_model_sort_type,
261 262
                                   GTK_TYPE_TREE_SORTABLE,
                                   &sortable_info);
263 264 265 266 267 268
    }

  return tree_model_sort_type;
}

static void
269 270 271 272 273 274 275 276 277 278 279
gtk_tree_model_sort_init (GtkTreeModelSort *tree_model_sort)
{
  tree_model_sort->sort_column_id = GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID;
  tree_model_sort->stamp = 0;
  tree_model_sort->zero_ref_count = 0;
  tree_model_sort->root = NULL;
  tree_model_sort->sort_list = NULL;
}

static void
gtk_tree_model_sort_class_init (GtkTreeModelSortClass *class)
280 281
{
  GObjectClass *object_class;
282
  GtkObjectClass *gobject_class;
283

284 285
  object_class = (GObjectClass *) class;
  gobject_class = (GtkObjectClass *) class;
286

287
  object_class->finalize = gtk_tree_model_sort_finalize;
288
  gobject_class->destroy = gtk_tree_model_sort_destroy;
289 290 291 292 293
}

static void
gtk_tree_model_sort_tree_model_init (GtkTreeModelIface *iface)
{
294
  iface->get_flags = gtk_tree_model_sort_get_flags;
295 296 297 298 299 300 301 302 303 304 305
  iface->get_n_columns = gtk_tree_model_sort_get_n_columns;
  iface->get_column_type = gtk_tree_model_sort_get_column_type;
  iface->get_iter = gtk_tree_model_sort_get_iter;
  iface->get_path = gtk_tree_model_sort_get_path;
  iface->get_value = gtk_tree_model_sort_get_value;
  iface->iter_next = gtk_tree_model_sort_iter_next;
  iface->iter_children = gtk_tree_model_sort_iter_children;
  iface->iter_has_child = gtk_tree_model_sort_iter_has_child;
  iface->iter_n_children = gtk_tree_model_sort_iter_n_children;
  iface->iter_nth_child = gtk_tree_model_sort_iter_nth_child;
  iface->iter_parent = gtk_tree_model_sort_iter_parent;
306 307
  iface->ref_node = gtk_tree_model_sort_ref_node;
  iface->unref_node = gtk_tree_model_sort_unref_node;
308 309
}

310 311 312 313 314
static void
gtk_tree_model_sort_tree_sortable_init (GtkTreeSortableIface *iface)
{
  iface->get_sort_column_id = gtk_tree_model_sort_get_sort_column_id;
  iface->set_sort_column_id = gtk_tree_model_sort_set_sort_column_id;
315
  iface->set_sort_func = gtk_tree_model_sort_set_sort_func;
316 317
  iface->set_default_sort_func = gtk_tree_model_sort_set_default_sort_func;
  iface->has_default_sort_func = gtk_tree_model_sort_has_default_sort_func;
318 319
}

320 321 322 323 324 325 326 327
/**
 * gtk_tree_model_sort_new_with_model:
 * @child_model: A #GtkTreeModel
 *
 * Creates a new #GtkTreeModel, with @child_model as the child_model.
 *
 * Return value: A new #GtkTreeModel.
 */
328
GtkTreeModel *
329
gtk_tree_model_sort_new_with_model (GtkTreeModel      *child_model)
330 331 332
{
  GtkTreeModel *retval;

333 334 335 336
  g_return_val_if_fail (GTK_IS_TREE_MODEL (child_model), NULL);

  retval = GTK_TREE_MODEL (g_object_new (gtk_tree_model_sort_get_type (), NULL));

337
  gtk_tree_model_sort_set_model (GTK_TREE_MODEL_SORT (retval), child_model);
338 339 340 341

  return retval;
}

342 343 344
/* GObject callbacks */
static void
gtk_tree_model_sort_finalize (GObject *object)
345
{
346
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *) object;
347

348
  if (tree_model_sort->root)
349
    gtk_tree_model_sort_free_level (tree_model_sort, tree_model_sort->root);
350 351 352 353 354 355

  if (tree_model_sort->sort_list)
    {
      _gtk_tree_data_list_header_free (tree_model_sort->sort_list);
      tree_model_sort->sort_list = NULL;
    }
356 357
}

358
/* GtkObject callbacks */
359
static void
360
gtk_tree_model_sort_destroy (GtkObject *gobject)
361
{
362
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *) gobject;
363

364
  if (tree_model_sort->child_model)
365
    gtk_tree_model_sort_set_model (tree_model_sort, NULL);
366 367 368
}

static void
369
gtk_tree_model_sort_row_changed (GtkTreeModel *s_model,
370 371
				 GtkTreePath  *start_s_path,
				 GtkTreeIter  *start_s_iter,
372
				 gpointer      data)
373 374
{
  GtkTreeModelSort *tree_model_sort = GTK_TREE_MODEL_SORT (data);
375
  GtkTreePath *path = NULL;
376
  GtkTreeIter iter;
377
  GtkTreeIter tmpiter;
378
  
379
  SortElt tmp;
380 381
  SortElt *elt;
  SortLevel *level;
382

383 384 385
  gboolean free_s_path = FALSE;

  gint offset, index = 0, i;
386 387

  g_return_if_fail (start_s_path != NULL || start_s_iter != NULL);
388

389
  if (!start_s_path)
390 391
    {
      free_s_path = TRUE;
392
      start_s_path = gtk_tree_model_get_path (s_model, start_s_iter);
393
    }
394

395 396 397
  path = gtk_tree_model_sort_convert_child_path_to_path (tree_model_sort,
							 start_s_path);
  if (!path)
Jonathan Blandford's avatar
Jonathan Blandford committed
398 399
    {
      if (free_s_path)
400
	gtk_tree_path_free (start_s_path);
Jonathan Blandford's avatar
Jonathan Blandford committed
401 402
      return;
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
403

404
  gtk_tree_model_get_iter (GTK_TREE_MODEL (data), &iter, path);
405 406 407 408
  
  level = iter.user_data;
  elt = iter.user_data2;
  
409
  if (level->array->len < 2 || tree_model_sort->sort_column_id == -1)
410 411
    {
      if (free_s_path)
412
	gtk_tree_path_free (start_s_path);
413
      
414 415
      gtk_tree_model_row_changed (GTK_TREE_MODEL (data), path, &iter);
      
416
      gtk_tree_path_free (path);
417
      
418 419 420
      return;
    }

421
  if (!GTK_TREE_MODEL_SORT_CACHE_CHILD_ITERS (tree_model_sort))
422 423 424 425
    {
      gtk_tree_model_get_iter (tree_model_sort->child_model,
			       &tmpiter, start_s_path);
    }
426
  
427 428 429 430 431
  offset = elt->offset;

  for (i = 0; i < level->array->len; i++)
    if (elt->offset == g_array_index (level->array, SortElt, i).offset)
      index = i;
432 433
  
  memcpy (&tmp, elt, sizeof (SortElt));
434
  g_array_remove_index (level->array, index);
435
  
436 437 438
  if (GTK_TREE_MODEL_SORT_CACHE_CHILD_ITERS (tree_model_sort))
    index = gtk_tree_model_sort_level_find_insert (tree_model_sort,
						   level,
439 440 441
						   &tmp.iter,
						   TRUE);
  else
442 443
    index = gtk_tree_model_sort_level_find_insert (tree_model_sort,
						   level,
444 445
						   &tmpiter,
						   TRUE);
446
  
447
  g_array_insert_val (level->array, index, tmp);
448 449 450 451 452 453 454 455 456 457

  gtk_tree_model_sort_increment_stamp (tree_model_sort);

  if (path)
    gtk_tree_path_free (path);

  path = gtk_tree_model_sort_elt_get_path (level, &g_array_index (level->array, SortElt, index));
  g_return_if_fail (path != NULL);

  gtk_tree_model_get_iter (GTK_TREE_MODEL (data), &iter, path);
458
  
459 460
  gtk_tree_model_row_changed (GTK_TREE_MODEL (data), path, &iter);

461 462
  gtk_tree_path_free (path);
  if (free_s_path)
463
    gtk_tree_path_free (start_s_path);
464 465
}

466 467 468 469 470
static void
gtk_tree_model_sort_row_inserted (GtkTreeModel          *s_model,
				  GtkTreePath           *s_path,
				  GtkTreeIter           *s_iter,
				  gpointer               data)
471
{
472 473
  GtkTreeModelSort *tree_model_sort = GTK_TREE_MODEL_SORT (data);
  GtkTreePath *path;
474
  GtkTreeIter iter;
475 476 477 478 479 480 481 482 483 484 485
  GtkTreeIter real_s_iter;

  gint i = 0;

  gboolean free_s_path = FALSE;

  SortElt *elt;
  SortLevel *level;
  SortLevel *parent_level = NULL;

  parent_level = level = SORT_LEVEL (tree_model_sort->root);
486

487
  g_return_if_fail (s_path != NULL || s_iter != NULL);
488
  
489
  if (!s_path)
490
    {
491 492
      s_path = gtk_tree_model_get_path (s_model, s_iter);
      free_s_path = TRUE;
493
    }
494 495 496
  
  if (!s_iter)
    gtk_tree_model_get_iter (s_model, &real_s_iter, s_path);
497
  else
498
    real_s_iter = *s_iter;
499

500 501 502
  if (!tree_model_sort->root)
    {
      gtk_tree_model_sort_build_level (tree_model_sort, NULL, NULL);
503

504 505 506 507
      /* the build level already put the inserted iter in the level,
	 so no need to handle this signal anymore */
      
      goto done_and_submit;
508
    }
509 510 511 512 513 514 515 516 517
  
  /* find the parent level */
  while (i < gtk_tree_path_get_depth (s_path) - 1)
    {
      if (!level)
	{
	  /* level not yet build, we won't cover this signal */
	  goto done;
	}
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
      if (level->array->len < gtk_tree_path_get_indices (s_path)[i])
	{
	  g_warning ("A node was inserted with a parent that's not in the tree.\n"
		     "This possibly means that a GtkTreeModel inserted a child node\n"
		     "before the parent was inserted.");
	  goto done;
	}

      elt = SORT_ELT (&g_array_index (level->array, SortElt, gtk_tree_path_get_indices (s_path)[i]));
      if (!elt->children)
	{
	  /* FIXME: emit has_child_toggled here? like the treeview? */

	  GtkTreePath *tmppath;
	  GtkTreeIter  tmpiter;
	  
	  tmppath = gtk_tree_model_sort_elt_get_path (level, elt);
	  if (tmppath)
	    {
	      gtk_tree_model_get_iter (GTK_TREE_MODEL (data), &tmpiter,
				       tmppath);
	      gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (data),
						    tmppath,
						    &tmpiter);
	      gtk_tree_path_free (tmppath);
	    }
545

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
	  /* not covering this signal */
	  goto done;
	}

      level = elt->children;
      parent_level = level;
      i++;
    }
  
  if (!parent_level)
    {
      goto done;
    }
  
  if (!gtk_tree_model_sort_insert_value (tree_model_sort,
					 parent_level,
					 s_path,
					 &real_s_iter))
    goto done;
  
 done_and_submit:
567 568
  path = gtk_tree_model_sort_convert_child_path_to_path (tree_model_sort,
							 s_path);
569
  
570
  if (!path)
Jonathan Blandford's avatar
Jonathan Blandford committed
571
    return;
572

573
  gtk_tree_model_sort_increment_stamp (tree_model_sort);
574
  
575
  gtk_tree_model_get_iter (GTK_TREE_MODEL (data), &iter, path);
576
  gtk_tree_model_row_inserted (GTK_TREE_MODEL (data), path, &iter);
577
  gtk_tree_path_free (path);
578 579 580 581 582 583 584

 done:

  if (free_s_path)
    gtk_tree_path_free (s_path);
  
  return;
585 586 587
}

static void
588 589 590 591
gtk_tree_model_sort_row_has_child_toggled (GtkTreeModel *s_model,
					   GtkTreePath  *s_path,
					   GtkTreeIter  *s_iter,
					   gpointer      data)
592 593
{
  GtkTreeModelSort *tree_model_sort = GTK_TREE_MODEL_SORT (data);
594 595 596
  GtkTreePath *path;
  GtkTreeIter iter;
  gboolean free_s_path = FALSE;
597

598
  g_return_if_fail (s_path != NULL || s_iter != NULL);
599

600 601 602 603 604
  /* we don't handle signals which we don't cover */
  if (!tree_model_sort->root)
    return;

  if (!s_path)
605 606 607 608
    {
      s_path = gtk_tree_model_get_path (s_model, s_iter);
      free_s_path = TRUE;
    }
609

610 611 612
  path = gtk_tree_model_sort_convert_child_path_to_path (tree_model_sort,
							 s_path);
  if (!path)
Jonathan Blandford's avatar
Jonathan Blandford committed
613
    return;
614
  
615
  gtk_tree_model_get_iter (GTK_TREE_MODEL (data), &iter, path);
616 617
  gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (data), path,
					&iter);
618
  gtk_tree_path_free (path);
619 620 621 622 623 624 625

  if (!GTK_TREE_MODEL_SORT_CACHE_CHILD_ITERS (tree_model_sort))
    {
      gtk_tree_model_sort_free_level (tree_model_sort,
				      SORT_LEVEL (tree_model_sort->root));
      tree_model_sort->root = NULL;
    }
626
  
627 628
  if (free_s_path)
    gtk_tree_path_free (s_path);
629 630 631
}

static void
632 633 634
gtk_tree_model_sort_row_deleted (GtkTreeModel *s_model,
				 GtkTreePath  *s_path,
				 gpointer      data)
635 636
{
  GtkTreeModelSort *tree_model_sort = GTK_TREE_MODEL_SORT (data);
637
  GtkTreePath *path = NULL;
638

639 640 641 642
  /* we don't handle signals which we don't cover */
  if (!tree_model_sort->root)
    return;

643
  g_return_if_fail (s_path != NULL);
644 645
  path = gtk_tree_model_sort_convert_child_path_to_path (tree_model_sort,
							 s_path);
646

647 648 649 650 651 652
  if (!path)
    {
      /* we don't cover this signal */
      return;
    }
  
653
  if (!GTK_TREE_MODEL_SORT_CACHE_CHILD_ITERS (tree_model_sort))
654
    {
655 656
      gtk_tree_model_sort_free_level (tree_model_sort,
				      SORT_LEVEL (tree_model_sort->root));
657 658
      tree_model_sort->root = NULL;
    }
659 660 661
  else
    {
      SortElt *elt;
662 663
      SortLevel *level;
      GtkTreeIter iter;
664 665
      gint offset;
      gint i;
666
      
667
      gtk_tree_model_get_iter (GTK_TREE_MODEL (data),
668
			       &iter, path);
669
      g_return_if_fail (tree_model_sort->stamp == iter.stamp);
670 671
      level = SORT_LEVEL (iter.user_data);
      elt = SORT_ELT (iter.user_data2);
672
      offset = elt->offset;
673

674 675 676 677 678 679 680 681 682
      for (i = 0; i < level->array->len; i++)
	if (elt->offset == g_array_index (level->array, SortElt, i).offset)
	  break;
      
      g_array_remove_index (level->array, i);
      level->ref_count--;
      
      /* update all offsets */
      for (i = 0; i < level->array->len; i++)
683 684 685 686 687
	    {
	      elt = & (g_array_index (level->array, SortElt, i));
	      if (elt->offset > offset)
		elt->offset--;
	    }
688
    }
689
  
690
  gtk_tree_model_sort_increment_stamp (tree_model_sort);
691 692 693 694
  gtk_tree_model_row_deleted (GTK_TREE_MODEL (data), path);

  if (path)
    gtk_tree_path_free (path);
695 696
}

697
static void
698 699 700 701 702
gtk_tree_model_sort_rows_reordered (GtkTreeModel *s_model,
				    GtkTreePath  *s_path,
				    GtkTreeIter  *s_iter,
				    gint         *new_order,
				    gpointer      data)
703
{
704 705 706 707 708 709 710
  int i;
  int len;
  SortElt *elt;
  SortLevel *level;
  gboolean free_s_path = FALSE;
  
  GtkTreeIter  iter;
711 712
  GtkTreePath *path;

713 714
  GtkTreeModelSort *tree_model_sort = GTK_TREE_MODEL_SORT (data);
  
715 716
  g_return_if_fail (s_path != NULL || s_iter != NULL);
  g_return_if_fail (new_order != NULL);
717

718
  if (!s_path)
719 720 721 722 723
    {
      s_path = gtk_tree_model_get_path (s_model, s_iter);
      free_s_path = TRUE;
    }

724 725 726 727 728 729
  if (!gtk_tree_path_get_indices (s_path))
    len = gtk_tree_model_iter_n_children (s_model, NULL);
  else
    len = gtk_tree_model_iter_n_children (s_model, s_iter);

  if (len < 2)
730 731 732 733 734 735 736
    {
      if (free_s_path)
	gtk_tree_path_free (s_path);
      return;
    }

  /** get correct sort level **/
737

738
  if (!gtk_tree_path_get_indices (s_path))
739 740
    level = SORT_LEVEL (tree_model_sort->root);
  else
741
    {
742 743 744 745
      path = gtk_tree_model_sort_convert_child_path_to_path (tree_model_sort,
							     s_path);
      
      if (!path)
746
	{
747 748
	  if (free_s_path)
	    gtk_tree_path_free (s_path);
749 750
	  return;
	}
751

752
      if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (data), &iter, path))
753
	{
754 755 756 757 758
	  /* no iter for me */
	  if (free_s_path)
	    gtk_tree_path_free (s_path);
	  gtk_tree_path_free (path);
	  return;
759 760
	}
      
761 762
      level = SORT_LEVEL (iter.user_data);
      elt = SORT_ELT (iter.user_data2);
763 764
      gtk_tree_path_free (path);

765
      /* FIXME: is this needed ? */
766 767 768 769 770 771
      if (!s_iter)
	gtk_tree_model_get_iter (s_model, s_iter, s_path);

      if (!elt->children)
	return;

772 773 774 775 776 777 778 779 780 781 782
      level = elt->children;
    }

  if (!level)
    {
      if (free_s_path)
	gtk_tree_path_free (s_path);

      /* ignore signal */
      return;
    }
783

784 785 786 787
  if (len != level->array->len)
    {
      if (free_s_path)
	gtk_tree_path_free (s_path);
788

789 790 791 792 793 794 795 796 797 798
      /* length mismatch, pretty bad, shouldn't happen */
      
      return;
    }
  
  /** unsorted: set offsets, resort without reordered emission **/
  if (tree_model_sort->sort_column_id == -1)
    {
      path = gtk_tree_model_sort_convert_child_path_to_path (tree_model_sort,
							     s_path);
799

800 801 802 803
      if (!path)
	{
	  if (free_s_path)
	    gtk_tree_path_free (s_path);
804
	  
805
	  return;
806
	}
807
      
808
      for (i = 0; i < level->array->len; i++)
809 810 811
	{
	  g_array_index (level->array, SortElt, i).offset = new_order[i];
	  
812 813
	  if (GTK_TREE_MODEL_SORT_CACHE_CHILD_ITERS (tree_model_sort)
	      && tree_model_sort->sort_column_id == -1)
814 815 816 817 818
	    {
	      get_child_iter_from_elt_no_cache (tree_model_sort,
						&(g_array_index (level->array, SortElt, i).iter), level, &g_array_index (level->array, SortElt, i));
	    }
	}
819

820 821 822 823 824 825
      gtk_tree_model_sort_increment_stamp (tree_model_sort);
      
      gtk_tree_model_sort_sort_level (tree_model_sort, level,
				      FALSE, FALSE);

      if (gtk_tree_path_get_depth (path))
826
	{
827 828 829 830 831
	  gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model_sort),
				   &iter,
				   path);
	  gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_model_sort),
					 path, &iter, new_order);
832
	}
833
      else
834 835 836 837
	{
	  gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_model_sort),
					 path, NULL, new_order);
	}
838 839
      
      gtk_tree_path_free (path);
840
      
841 842
      if (free_s_path)
	gtk_tree_path_free (s_path);
843

844
      return;
845 846
    }

847 848 849 850 851 852
  /** sorted: update offsets, no emission of reordered signal **/
  for (i = 0; i < level->array->len; i++)
    g_array_index (level->array, SortElt, i).offset =
      new_order[g_array_index (level->array, SortElt, i).offset];

  gtk_tree_model_sort_increment_stamp (tree_model_sort);
853

854 855 856 857 858 859 860 861 862 863 864
  if (free_s_path)
    gtk_tree_path_free (s_path);
}

/* Fulfill our model requirements */
static guint
gtk_tree_model_sort_get_flags (GtkTreeModel *tree_model)
{
  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), 0);

  return 0;
865 866
}

867 868 869
static gint
gtk_tree_model_sort_get_n_columns (GtkTreeModel *tree_model)
{
870 871
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *) tree_model;

872
  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), 0);
873 874 875

  if (tree_model_sort->child_model == 0)
    return 0;
876

877
  return gtk_tree_model_get_n_columns (GTK_TREE_MODEL_SORT (tree_model)->child_model);
878 879 880 881
}

static GType
gtk_tree_model_sort_get_column_type (GtkTreeModel *tree_model,
882
                                     gint          index)
883 884
{
  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), G_TYPE_INVALID);
885
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, G_TYPE_INVALID);
886

887
  return gtk_tree_model_get_column_type (GTK_TREE_MODEL_SORT (tree_model)->child_model, index);
888 889 890
}

static gboolean
891 892 893
gtk_tree_model_sort_get_iter (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter,
			      GtkTreePath  *path)
894
{
895 896 897 898
  GtkTreeModelSort *tree_model_sort;
  gint *indices;
  SortLevel *level;
  gint depth, i;
899

900 901
  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, FALSE);
902

903 904 905 906 907 908 909 910 911
  tree_model_sort = (GtkTreeModelSort *) tree_model;
  indices = gtk_tree_path_get_indices (path);

  if (tree_model_sort->root == NULL)
    gtk_tree_model_sort_build_level (tree_model_sort, NULL, NULL);
  level = SORT_LEVEL (tree_model_sort->root);

  depth = gtk_tree_path_get_depth (path);
  if (depth == 0)
912
    return FALSE;
913

914
  for (i = 0; i < depth - 1; i++)
915
    {
916 917 918
      if ((level == NULL) ||
	  (level->array->len < indices[i]))
	return FALSE;
919

920 921 922
      if (g_array_index (level->array, SortElt, indices[i]).children == NULL)
	gtk_tree_model_sort_build_level (tree_model_sort, level, &g_array_index (level->array, SortElt, indices[i]));
      level = g_array_index (level->array, SortElt, indices[i]).children;
923 924
    }

925 926 927 928 929
  if (level == NULL)
    return FALSE;
  iter->stamp = tree_model_sort->stamp;
  iter->user_data = level;
  iter->user_data2 = &g_array_index (level->array, SortElt, indices[depth - 1]);
930

931
  return TRUE;
932 933 934 935
}

static GtkTreePath *
gtk_tree_model_sort_get_path (GtkTreeModel *tree_model,
936
			      GtkTreeIter  *iter)
937
{
938 939 940 941
  GtkTreePath *retval;
  SortLevel *level;
  SortElt *elt;

942
  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), NULL);
943
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, NULL);
944
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp, NULL);
945

946 947 948 949
  retval = gtk_tree_path_new ();
  level = iter->user_data;
  elt = iter->user_data2;
  while (level != NULL)
950
    {
951 952 953 954
      gtk_tree_path_prepend_index (retval, elt - (SortElt *)level->array->data);

      elt = level->parent_elt;
      level = level->parent_level;
955
    }
956 957

  return retval;
958 959 960 961
}

static void
gtk_tree_model_sort_get_value (GtkTreeModel *tree_model,
962 963 964
			       GtkTreeIter  *iter,
			       gint          column,
			       GValue       *value)
965
{
966
  GtkTreeIter child_iter;
967 968

  g_return_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model));
969
  g_return_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL);
970 971
  g_return_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp);

972
  GET_CHILD_ITER (tree_model, &child_iter, iter);
973 974
  gtk_tree_model_get_value (GTK_TREE_MODEL_SORT (tree_model)->child_model,
			    &child_iter, column, value);
975 976 977 978
}

static gboolean
gtk_tree_model_sort_iter_next (GtkTreeModel *tree_model,
979
			       GtkTreeIter  *iter)
980
{
981
  SortLevel *level;
982 983 984
  SortElt *elt;

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
985
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, FALSE);
986 987
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp, FALSE);

988 989
  level = iter->user_data;
  elt = iter->user_data2;
990

991
  if (elt - (SortElt *)level->array->data >= level->array->len - 1)
992 993 994 995
    {
      iter->stamp = 0;
      return FALSE;
    }
996
  iter->user_data2 = elt + 1;
997

998 999 1000 1001 1002
  return TRUE;
}

static gboolean
gtk_tree_model_sort_iter_children (GtkTreeModel *tree_model,
1003 1004
				   GtkTreeIter  *iter,
				   GtkTreeIter  *parent)
1005
{
1006 1007
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *) tree_model;
  SortLevel *level;
1008 1009

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
1010 1011
  g_return_val_if_fail (tree_model_sort->child_model != NULL, FALSE);
  if (parent) g_return_val_if_fail (tree_model_sort->stamp == parent->stamp, FALSE);
Jonathan Blandford's avatar
Jonathan Blandford committed
1012

1013 1014
  if (parent == NULL)
    {
1015

1016 1017 1018 1019
      if (tree_model_sort->root == NULL)
	gtk_tree_model_sort_build_level (tree_model_sort, NULL, NULL);
      if (tree_model_sort->root == NULL)
	return FALSE;
1020

1021 1022 1023 1024 1025
      level = tree_model_sort->root;
      iter->stamp = tree_model_sort->stamp;
      iter->user_data = level;
      iter->user_data2 = level->array->data;
    }
1026
  else
1027
    {
1028 1029 1030 1031 1032
      if (((SortElt *)parent->user_data2)->children == NULL)
	gtk_tree_model_sort_build_level (tree_model_sort,
					 (SortLevel *)parent->user_data,
					 (SortElt *)parent->user_data2);
      if (((SortElt *)parent->user_data2)->children == NULL)
1033
	return FALSE;
1034 1035 1036
      iter->stamp = tree_model_sort->stamp;
      iter->user_data = ((SortElt *)parent->user_data2)->children;
      iter->user_data2 = ((SortLevel *)iter->user_data)->array->data;
1037 1038
    }
  
1039 1040 1041 1042 1043
  return TRUE;
}

static gboolean
gtk_tree_model_sort_iter_has_child (GtkTreeModel *tree_model,
1044
				    GtkTreeIter  *iter)
1045
{
1046
  GtkTreeIter child_iter;
1047 1048

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
1049
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, FALSE);
1050 1051
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp, FALSE);

1052
  GET_CHILD_ITER (tree_model, &child_iter, iter);
1053

1054
  return gtk_tree_model_iter_has_child (GTK_TREE_MODEL_SORT (tree_model)->child_model, &child_iter);
1055 1056 1057 1058 1059 1060
}

static gint
gtk_tree_model_sort_iter_n_children (GtkTreeModel *tree_model,
				     GtkTreeIter  *iter)
{
1061
  GtkTreeIter child_iter;
1062 1063

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), 0);
1064
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, 0);
1065
  if (iter) g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp, 0);
1066

1067 1068
  if (iter == NULL)
    return gtk_tree_model_iter_n_children (GTK_TREE_MODEL_SORT (tree_model)->child_model, NULL);
1069

1070
  GET_CHILD_ITER (tree_model, &child_iter, iter);
1071

1072
  return gtk_tree_model_iter_n_children (GTK_TREE_MODEL_SORT (tree_model)->child_model, &child_iter);
1073
}
1074 1075 1076

static gboolean
gtk_tree_model_sort_iter_nth_child (GtkTreeModel *tree_model,
1077 1078 1079
				    GtkTreeIter  *iter,
				    GtkTreeIter  *parent,
				    gint          n)
1080
{
1081
  SortLevel *level;
1082 1083

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
1084
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, FALSE);
1085

1086 1087
  if (gtk_tree_model_sort_iter_children (tree_model, iter, parent) == FALSE)
    return FALSE;
1088

1089 1090
  level = iter->user_data;
  if (n >= level->array->len)
1091
    {
1092 1093
      iter->stamp = 0;
      return FALSE;
1094 1095
    }

1096
  iter->user_data2 = &g_array_index (level->array, SortElt, n);
1097 1098 1099 1100 1101
  return TRUE;
}

static gboolean
gtk_tree_model_sort_iter_parent (GtkTreeModel *tree_model,
1102 1103
				 GtkTreeIter  *iter,
				 GtkTreeIter  *child)
1104
{
1105
  SortLevel *level;
1106 1107

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
1108
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, FALSE);
1109 1110
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == child->stamp, FALSE);

1111
  level = child->user_data;
1112

1113
  if (level->parent_level)
1114 1115
    {
      iter->stamp = GTK_TREE_MODEL_SORT (tree_model)->stamp;
1116 1117 1118
      iter->user_data = level->parent_level;
      iter->user_data2 = level->parent_elt;

1119 1120 1121 1122 1123 1124
      return TRUE;
    }
  return FALSE;
}

static void
1125
gtk_tree_model_sort_ref_node (GtkTreeModel *tree_model,
1126
			      GtkTreeIter  *iter)
1127
{
1128 1129
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *) tree_model;
  SortLevel *level;
1130 1131 1132 1133 1134 1135
  SortElt *elt;

  g_return_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model));
  g_return_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL);
  g_return_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp);

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
  level = iter->user_data;
  elt = iter->user_data2;

  elt->ref_count++;
  level->ref_count++;
  if (level->ref_count == 1)
    {
      SortLevel *parent_level = level->parent_level;
      SortElt *parent_elt = level->parent_elt;
      /* We were at zero -- time to decrement the zero_ref_count val */
      do
	{
	  if (parent_elt)
	    parent_elt->zero_ref_count--;
	  else
	    tree_model_sort->zero_ref_count--;
	  
	  if (parent_level)
	    {
	      parent_elt = parent_level->parent_elt;
	      parent_level = parent_level->parent_level;
	    }
	}
      while (parent_level);
    }
1161 1162 1163
}

static void
1164
gtk_tree_model_sort_unref_node (GtkTreeModel *tree_model,
1165
				GtkTreeIter  *iter)
1166
{
1167 1168
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *) tree_model;
  SortLevel *level;
1169
  SortElt *elt;
1170

1171 1172 1173
  g_return_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model));
  g_return_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL);
  g_return_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp);
1174 1175 1176 1177 1178 1179 1180

  level = iter->user_data;
  elt = iter->user_data2;

  elt->ref_count--;
  level->ref_count--;
  if (level->ref_count == 0)
1181
    {
1182 1183 1184 1185
      SortLevel *parent_level = level->parent_level;
      SortElt *parent_elt = level->parent_elt;
      /* We were at zero -- time to decrement the zero_ref_count val */
      do
1186
	{
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
	  if (parent_elt)
	    parent_elt->zero_ref_count++;
	  else
	    tree_model_sort->zero_ref_count++;
	  
	  if (parent_level)
	    {
	      parent_elt = parent_level->parent_elt;
	      parent_level = parent_level->parent_level;
	    }
1197
	}
1198
      while (parent_level);
1199
    }
1200 1201
}

1202
/* Sortable interface */
1203
static gboolean
1204 1205 1206
gtk_tree_model_sort_get_sort_column_id (GtkTreeSortable *sortable,
					gint            *sort_column_id,
					GtkSortType     *order)
1207
{
1208
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *)sortable;
1209 1210 1211

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (sortable), FALSE);

1212
  if (tree_model_sort->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
    return FALSE;

  if (sort_column_id)
    *sort_column_id = tree_model_sort->sort_column_id;
  if (order)
    *order = tree_model_sort->order;

  return TRUE;
}

static void
1224 1225 1226
gtk_tree_model_sort_set_sort_column_id (GtkTreeSortable *sortable,
					gint             sort_column_id,
					GtkSortType      order)
1227
{
1228 1229
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *)sortable;
  
1230
  g_return_if_fail (GTK_IS_TREE_MODEL_SORT (sortable));
1231 1232
  
  if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
    {
      GtkTreeDataSortHeader *header = NULL;
      
      header = _gtk_tree_data_list_get_header (tree_model_sort->sort_list,
					       sort_column_id);

      /* we want to make sure that we have a function */
      g_return_if_fail (header != NULL);
      g_return_if_fail (header->func != NULL);
    }
  else
    {
      g_return_if_fail (tree_model_sort->default_sort_func != NULL);
    }

1248
  if (tree_model_sort->sort_column_id == sort_column_id)
1249
    {
1250 1251 1252 1253 1254 1255 1256
      if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
	{
	  if (tree_model_sort->order == order)
	    return;
	}
      else
	return;
1257
    }
1258

1259 1260
  tree_model_sort->sort_column_id = sort_column_id;
  tree_model_sort->order = order;
1261 1262

  gtk_tree_model_sort_sort (tree_model_sort);
1263 1264 1265 1266
  gtk_tree_sortable_sort_column_changed (sortable);
}

static void
1267
gtk_tree_model_sort_set_sort_func (GtkTreeSortable        *sortable,
1268 1269 1270 1271
				   gint                    sort_column_id,
				   GtkTreeIterCompareFunc  func,
				   gpointer                data,
				   GtkDestroyNotify        destroy)
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
{
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *) sortable;
  GtkTreeDataSortHeader *header = NULL;
  GList *list;

  g_return_if_fail (GTK_IS_TREE_MODEL_SORT (sortable));
  g_return_if_fail (func != NULL);

  for (list = tree_model_sort->sort_list; list; list = list->next)
    {
1282 1283
      header = (GtkTreeDataSortHeader *) list->data;
      
1284
      if (header->sort_column_id == sort_column_id)
1285
	break;
1286
    }
1287

1288 1289 1290 1291
  if (header == NULL)
    {
      header = g_new0 (GtkTreeDataSortHeader, 1);
      header->sort_column_id = sort_column_id;
1292 1293
      tree_model_sort->sort_list = g_list_append (tree_model_sort->sort_list,
						  header);
1294
    }
1295

1296 1297
  if (header->destroy)
    (* header->destroy) (header->data);
1298

1299 1300 1301
  header->func = func;
  header->data = data;
  header->destroy = destroy;
1302 1303
}

1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
static void
gtk_tree_model_sort_set_default_sort_func (GtkTreeSortable        *sortable,
					   GtkTreeIterCompareFunc  func,
					   gpointer                data,
					   GtkDestroyNotify        destroy)
{
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *)sortable;
  
  g_return_if_fail (GTK_IS_TREE_MODEL_SORT (sortable));
  
  if (tree_model_sort->default_sort_destroy)
1315
    (* tree_model_sort->default_sort_destroy) (tree_model_sort->default_sort_data);
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331

  tree_model_sort->default_sort_func = func;
  tree_model_sort->default_sort_data = data;
  tree_model_sort->default_sort_destroy = destroy;
}

static gboolean
gtk_tree_model_sort_has_default_sort_func (GtkTreeSortable *sortable)
{
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *)sortable;

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (sortable), FALSE);

  return (tree_model_sort->default_sort_func != NULL);
}

1332
/* sorting code - private */
1333 1334
static gint
gtk_tree_model_sort_compare_func (gconstpointer a,