gtktreemodelsort.c 29 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
21
22
23
24
25
26

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

27
28
#include "gtktreemodelsort.h"
#include "gtksignal.h"
29
#include <string.h>
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

enum {
  CHANGED,
  INSERTED,
  CHILD_TOGGLED,
  DELETED,
  LAST_SIGNAL
};

typedef struct _SortElt SortElt;
struct _SortElt
{
  GtkTreeIter iter;
  SortElt *parent;
  GArray *children;
  gint ref;
  gint offset;
};

static guint tree_model_sort_signals[LAST_SIGNAL] = { 0 };


#define get_array(e,t) ((GArray *)((e)->parent?(e)->parent->children:GTK_TREE_MODEL_SORT(t)->root))

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_finalize        (GObject               *object);
static void         gtk_tree_model_sort_changed         (GtkTreeModel          *model,
							 GtkTreePath           *path,
							 GtkTreeIter           *iter,
							 gpointer               data);
static void         gtk_tree_model_sort_inserted        (GtkTreeModel          *model,
							 GtkTreePath           *path,
							 GtkTreeIter           *iter,
							 gpointer               data);
static void         gtk_tree_model_sort_child_toggled   (GtkTreeModel          *model,
							 GtkTreePath           *path,
							 GtkTreeIter           *iter,
							 gpointer               data);
static void         gtk_tree_model_sort_deleted         (GtkTreeModel          *model,
							 GtkTreePath           *path,
							 gpointer               data);
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_iter        (GtkTreeModel          *tree_model,
							 GtkTreeIter           *iter);
static void         gtk_tree_model_sort_unref_iter      (GtkTreeModel          *tree_model,
							 GtkTreeIter           *iter);

/* Internal functions */
static void         gtk_tree_model_sort_build_level     (GtkTreeModelSort      *tree_model_sort,
							 SortElt               *place);
static void         gtk_tree_model_sort_free_level      (GArray                *array);
gint                g_value_string_compare_func         (const GValue          *a,
							 const GValue          *b);
gint                g_value_int_compare_func            (const GValue          *a,
							 const GValue          *b);


GtkType
gtk_tree_model_sort_get_type (void)
{
  static GtkType tree_model_sort_type = 0;

  if (!tree_model_sort_type)
    {
      static const GTypeInfo tree_model_sort_info =
      {
        sizeof (GtkTreeModelSortClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
        (GClassInitFunc) gtk_tree_model_sort_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
        sizeof (GtkTreeModelSort),
	0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_tree_model_sort_init
      };

      static const GInterfaceInfo tree_model_info =
      {
	(GInterfaceInitFunc) gtk_tree_model_sort_tree_model_init,
	NULL,
	NULL
      };

      tree_model_sort_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkTreeModelSort", &tree_model_sort_info, 0);
      g_type_add_interface_static (tree_model_sort_type,
				   GTK_TYPE_TREE_MODEL,
				   &tree_model_info);
    }

  return tree_model_sort_type;
}

static void
gtk_tree_model_sort_class_init (GtkTreeModelSortClass *tree_model_sort_class)
{
  GObjectClass *object_class;

  object_class = (GObjectClass *) tree_model_sort_class;

  tree_model_sort_signals[CHANGED] =
    gtk_signal_new ("changed",
                    GTK_RUN_FIRST,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkTreeModelSortClass, changed),
                    gtk_marshal_VOID__POINTER_POINTER,
                    GTK_TYPE_NONE, 2,
		    GTK_TYPE_POINTER,
		    GTK_TYPE_POINTER);
  tree_model_sort_signals[INSERTED] =
    gtk_signal_new ("inserted",
                    GTK_RUN_FIRST,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkTreeModelSortClass, inserted),
                    gtk_marshal_VOID__POINTER_POINTER,
                    GTK_TYPE_NONE, 2,
		    GTK_TYPE_POINTER,
		    GTK_TYPE_POINTER);
  tree_model_sort_signals[CHILD_TOGGLED] =
    gtk_signal_new ("child_toggled",
                    GTK_RUN_FIRST,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkTreeModelSortClass, child_toggled),
                    gtk_marshal_VOID__POINTER_POINTER,
                    GTK_TYPE_NONE, 2,
		    GTK_TYPE_POINTER,
		    GTK_TYPE_POINTER);
  tree_model_sort_signals[DELETED] =
    gtk_signal_new ("deleted",
                    GTK_RUN_FIRST,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkTreeModelSortClass, deleted),
                    gtk_marshal_VOID__POINTER,
                    GTK_TYPE_NONE, 1,
		    GTK_TYPE_POINTER);

  object_class->finalize = gtk_tree_model_sort_finalize;

  gtk_object_class_add_signals (GTK_OBJECT_CLASS (object_class), tree_model_sort_signals, LAST_SIGNAL);
}

static void
gtk_tree_model_sort_tree_model_init (GtkTreeModelIface *iface)
{
  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;
  iface->ref_iter = gtk_tree_model_sort_ref_iter;
  iface->unref_iter = gtk_tree_model_sort_unref_iter;
}

static void
gtk_tree_model_sort_init (GtkTreeModelSort *tree_model_sort)
{
  tree_model_sort->stamp = g_random_int ();
}

GtkTreeModel *
gtk_tree_model_sort_new (void)
{
  return GTK_TREE_MODEL (gtk_type_new (gtk_tree_model_sort_get_type ()));
}

GtkTreeModel *
gtk_tree_model_sort_new_with_model (GtkTreeModel      *model,
232
				    GValueCompareFunc  func,
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
				    gint               sort_col)
{
  GtkTreeModel *retval;

  retval = gtk_tree_model_sort_new ();
  gtk_tree_model_sort_set_model (GTK_TREE_MODEL_SORT (retval), model);

  GTK_TREE_MODEL_SORT (retval)->func = func;
  GTK_TREE_MODEL_SORT (retval)->sort_col = sort_col;
  return retval;
}

/**
 * gtk_tree_model_sort_set_model:
 * @tree_model_sort: The #GtkTreeModelSort.
 * @model: A #GtkTreeModel, or NULL.
 * 
 * Sets the model of @tree_model_sort to be @model.  If @model is NULL, then the
 * old model is unset.
 **/
void
gtk_tree_model_sort_set_model (GtkTreeModelSort *tree_model_sort,
			       GtkTreeModel     *model)
{
  g_return_if_fail (tree_model_sort != NULL);
  g_return_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model_sort));

  if (model)
    g_object_ref (G_OBJECT (model));

  if (tree_model_sort->model)
    {
      gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->model),
				     gtk_tree_model_sort_changed,
				     tree_model_sort);
      gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->model),
				     gtk_tree_model_sort_inserted,
				     tree_model_sort);
      gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->model),
				     gtk_tree_model_sort_child_toggled,
				     tree_model_sort);
      gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->model),
				     gtk_tree_model_sort_deleted,
				     tree_model_sort);

      g_object_unref (G_OBJECT (tree_model_sort->model));
    }

  tree_model_sort->model = model;

  if (model)
    {
      gtk_signal_connect (GTK_OBJECT (model),
			  "changed",
			  gtk_tree_model_sort_changed,
			  tree_model_sort);
      gtk_signal_connect (GTK_OBJECT (model),
			  "inserted",
			  gtk_tree_model_sort_inserted,
			  tree_model_sort);
      gtk_signal_connect (GTK_OBJECT (model),
			  "child_toggled",
			  gtk_tree_model_sort_child_toggled,
			  tree_model_sort);
      gtk_signal_connect (GTK_OBJECT (model),
			  "deleted",
			  gtk_tree_model_sort_deleted,
			  tree_model_sort);

      tree_model_sort->flags = gtk_tree_model_get_flags (model);
    }
}

/**
 * gtk_tree_model_sort_convert_path:
 * @tree_model_sort: The #GtkTreeModelSort.
 * @path: A #GtkTreePath, relative to the @tree_model_sort 's model.
 * 
 * Converts the @path to a new path, relative to the sorted position.  In other
 * words, the value found in the @tree_model_sort ->model at the @path, is
 * identical to that found in the @tree_model_sort and the return value.
 * 
 * Return value: A new path, or NULL if @path does not exist in @tree_model_sort
 * ->model.
 **/
GtkTreePath *
gtk_tree_model_sort_convert_path (GtkTreeModelSort *tree_model_sort,
				  GtkTreePath      *path)
{
  GtkTreePath *retval;
  GArray *array;
  gint *indices;
  gint i = 0;

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

  retval = gtk_tree_path_new ();
  array = (GArray *) tree_model_sort->root;
  indices = gtk_tree_path_get_indices (path);

  do
    {
      SortElt *elt;
      gboolean found = FALSE;
      gint j;

      if ((array->len < indices[i]) || (array == NULL))
	{
	  gtk_tree_path_free (path);
	  return NULL;
	}

      elt = (SortElt *) array->data;
      for (j = 0; j < array->len; j++, elt++)
	{
	  if (elt->offset == indices[i])
	    {
	      found = TRUE;
	      break;
	    }
	}
      if (! found)
	{
	  gtk_tree_path_free (path);
	  return NULL;
	}

      gtk_tree_path_prepend_index (retval, j);
      if (elt->children == NULL)
	gtk_tree_model_sort_build_level (tree_model_sort, elt);
      i++;
    }
  while (i < gtk_tree_path_get_depth (path));

  return retval;
}

static void
gtk_tree_model_sort_finalize (GObject *object)
{
  GtkTreeModelSort *tree_model_sort = (GtkTreeModelSort *) object;

  if (tree_model_sort->root)
    gtk_tree_model_sort_free_level (tree_model_sort->root);

  g_object_unref (G_OBJECT (tree_model_sort->model));
}

static void
383
384
385
gtk_tree_model_sort_changed (GtkTreeModel *s_model,
			     GtkTreePath  *s_path,
			     GtkTreeIter  *s_iter,
386
387
388
			     gpointer      data)
{
  GtkTreeModelSort *tree_model_sort = GTK_TREE_MODEL_SORT (data);
389
390
391
392
393
  GtkTreePath *path;
  GtkTreeIter iter;
  gboolean free_s_path = FALSE;

  g_return_if_fail (s_path != NULL || s_iter != NULL);
394

395
396
397
398
399
  if (s_path == NULL)
    {
      free_s_path = TRUE;
      s_path = gtk_tree_model_get_path (s_model, s_iter);
    }
400

401
402
403
  path = gtk_tree_model_sort_convert_path (tree_model_sort, s_path);
  gtk_tree_model_get_iter (GTK_TREE_MODEL (data), &iter, path);
  gtk_signal_emit_by_name (GTK_OBJECT (data), "changed", path, &iter);
404

405
406
407
  gtk_tree_path_free (path);
  if (free_s_path)
    gtk_tree_path_free (s_path);
408
409
410
411
}

static void
gtk_tree_model_sort_insert_value (GtkTreeModelSort *sort,
412
413
				  GtkTreePath      *s_path,
				  GtkTreeIter      *s_iter)
414
{
415
  GtkTreePath *tmp_path;
416
417
  GArray *array;
  GtkTreeIter iter;
418
  SortElt elt;
419
  SortElt *tmp_elt;
420
421
  gint offset;
  gint high, low, middle, j;
422
423
  GValueCompareFunc func;
  GValue s_value = {0, };
424
425
  GValue tmp_value = {0, };

426
427
  offset = gtk_tree_path_get_indices (s_path)[gtk_tree_path_get_depth (s_path) - 1];

428
429
430
  elt.iter = *s_iter;
  elt.ref = 0;
  elt.children = NULL;
431
  elt.offset = offset;
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
  tmp_path = gtk_tree_path_copy (s_path);
  if (gtk_tree_path_up (tmp_path))
    {
      GtkTreePath *parent_path;

      parent_path = gtk_tree_model_sort_convert_path (sort, tmp_path);
      gtk_tree_model_get_iter (GTK_TREE_MODEL (sort), &iter, parent_path);
      elt.parent = ((SortElt *) iter.tree_node);
      array = ((SortElt *) iter.tree_node)->children;
      if (array == NULL)
	{
	  ((SortElt *) iter.tree_node)->children = g_array_sized_new (FALSE, FALSE, sizeof (SortElt), 1);
	  array = ((SortElt *) iter.tree_node)->children;
	}
      gtk_tree_path_free (parent_path);
    }
  else
    {
      if (sort->root == NULL)
	sort->root = g_array_sized_new (FALSE, FALSE, sizeof (SortElt), 1);
      array = sort->root;
      elt.parent = NULL;
    }
  gtk_tree_path_free (tmp_path);
456
457
458
459
460
461
462
463
464
465
466
467
468
469

  if (sort->func)
    func = sort->func;
  else
    {
      switch (gtk_tree_model_get_column_type (sort->model, sort->sort_col))
	{
	case G_TYPE_STRING:
	  func = &g_value_string_compare_func;
	  break;
	case G_TYPE_INT:
	  func = &g_value_int_compare_func;
	  break;
	default:
470
471
472
	  g_warning ("No comparison function for row %d (Type %s)\n",
		     sort->sort_col,
		     g_type_name (gtk_tree_model_get_column_type (sort->model, sort->sort_col)));
473
474
475
476
	  return;
	}
    }

477
  gtk_tree_model_get_value (sort->model, s_iter, sort->sort_col, &s_value);
478

479
480
481
482
483
#if 0
  /* FIXME: we can, as we are an array, do binary search to find the correct
   * location to insert the element.  However, I'd rather get it working.  The
   * below is quite wrong, but a step in the right direction.
   */
484
485
486
487
488
  low = 0;
  high = array->len;
  middle = (low + high)/2;

  /* Insert the value into the array */
489
  while (low != high)
490
491
    {
      gint cmp;
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
      tmp_elt = &(g_array_index (array, SortElt, middle));
      gtk_tree_model_get_value (sort->model,
				(GtkTreeIter *) tmp_elt,
				sort->sort_col,
				&tmp_value);

      cmp = ((func) (&tmp_value, &s_value));
      g_value_unset (&tmp_value);

      if (cmp < 0)
	high = middle;
      else if (cmp > 0)
	low = middle;
      else if (cmp == 0)
	break;
      middle = (low + high)/2;
508
    }
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#endif
  for (middle = 0; middle < array->len; middle++)
    {
      gint cmp;

      tmp_elt = &(g_array_index (array, SortElt, middle));
      gtk_tree_model_get_value (sort->model,
				(GtkTreeIter *) tmp_elt,
				sort->sort_col,
				&tmp_value);

      cmp = ((func) (&s_value, &tmp_value));
      g_value_unset (&tmp_value);

      if (cmp >= 0)
	break;
    }
526
527

  g_array_insert_vals (array, middle, &elt, 1);
528

529
  g_value_unset (&s_value);
530
531
532
533
534
535
536
537
538

  /* update all the larger offsets */
  tmp_elt = (SortElt *) array->data;
  for (j = 0; j < array->len; j++, tmp_elt++)
	{
	  if ((tmp_elt->offset >= offset) &&
	      j != middle)
	    tmp_elt->offset ++;
	}
539
540
541
}

static void
542
543
544
gtk_tree_model_sort_inserted (GtkTreeModel *s_model,
			      GtkTreePath  *s_path,
			      GtkTreeIter  *s_iter,
545
546
547
			      gpointer      data)
{
  GtkTreeModelSort *tree_model_sort = GTK_TREE_MODEL_SORT (data);
548
549
  GtkTreePath *path;
  GtkTreeIter iter;
550

551
  g_return_if_fail (s_path != NULL || s_iter != NULL);
552

553
554
555
  if (s_path == NULL)
    s_path = gtk_tree_model_get_path (s_model, s_iter);

556
  if (!(tree_model_sort->flags & GTK_TREE_MODEL_ITERS_PERSIST))
557
    {
558
      gtk_tree_model_sort_free_level ((GArray *) tree_model_sort->root);
559
560
      tree_model_sort->root = NULL;
    }
561
562
  else
    {
563
      GtkTreeIter real_s_iter;
564

565
566
567
      if (s_iter == NULL)
	gtk_tree_model_get_iter (s_model, &real_s_iter, s_path);
      else
568
	real_s_iter = (* s_iter);
569
570
571

      gtk_tree_model_sort_insert_value (tree_model_sort, s_path, &real_s_iter);
    }
572

573
  path = gtk_tree_model_sort_convert_path (tree_model_sort, s_path);
574

575
576
  g_return_if_fail (path != NULL);

577
  gtk_tree_model_get_iter (GTK_TREE_MODEL (data), &iter, path);
578
  gtk_signal_emit_by_name (GTK_OBJECT (data), "inserted", path, &iter);
579
  gtk_tree_path_free (path);
580
581
582
}

static void
583
584
585
gtk_tree_model_sort_child_toggled (GtkTreeModel *s_model,
				   GtkTreePath  *s_path,
				   GtkTreeIter  *s_iter,
586
587
588
				   gpointer      data)
{
  GtkTreeModelSort *tree_model_sort = GTK_TREE_MODEL_SORT (data);
589
590
591
  GtkTreePath *path;
  GtkTreeIter iter;
  gboolean free_s_path = FALSE;
592

593
  g_return_if_fail (s_path != NULL || s_iter != NULL);
594

595
  if (!(tree_model_sort->flags & GTK_TREE_MODEL_ITERS_PERSIST))
596
597
598
599
600
    {
      gtk_tree_model_sort_free_level ((GArray *)tree_model_sort->root);
      tree_model_sort->root = NULL;
    }

601
602
603
604
605
  if (s_path == NULL)
    {
      s_path = gtk_tree_model_get_path (s_model, s_iter);
      free_s_path = TRUE;
    }
606

607
608
  path = gtk_tree_model_sort_convert_path (tree_model_sort, s_path);
  gtk_tree_model_get_iter (GTK_TREE_MODEL (data), &iter, path);
609
610
  gtk_signal_emit_by_name (GTK_OBJECT (data),
			   "child_toggled",
611
612
613
614
			   path, &iter);
  gtk_tree_path_free (path);
  if (free_s_path)
    gtk_tree_path_free (s_path);
615
616
617
}

static void
618
619
gtk_tree_model_sort_deleted (GtkTreeModel *s_model,
			     GtkTreePath  *s_path,
620
621
622
			     gpointer      data)
{
  GtkTreeModelSort *tree_model_sort = GTK_TREE_MODEL_SORT (data);
623
  GtkTreePath *path;
624

625
626
  g_return_if_fail (s_path != NULL);
  path = gtk_tree_model_sort_convert_path (tree_model_sort, s_path);
627
628
  g_return_if_fail (path != NULL);

629
  if (!(tree_model_sort->flags & GTK_TREE_MODEL_ITERS_PERSIST))
630
631
632
633
    {
      gtk_tree_model_sort_free_level ((GArray *)tree_model_sort->root);
      tree_model_sort->root = NULL;
    }
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
  else
    {
      GArray *array;
      GtkTreeIter iter;
      SortElt *elt;
      gint offset;
      gint i;

      gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_model_sort), &iter, path);
      elt = (SortElt *) iter.tree_node;
      offset = elt->offset;
      array = get_array (elt, tree_model_sort);
      if (array->len == 1)
	{
	  if (((SortElt *)array->data)->parent == NULL)
	    tree_model_sort->root = NULL;
	  else
	    (((SortElt *)array->data)->parent)->children = NULL;
	  gtk_tree_model_sort_free_level (array);
	}
      else
	{
	  g_array_remove_index (array, elt - ((SortElt *) array->data));
657

658
659
660
661
662
663
664
665
666
667
668
669
	  for (i = 0; i < array->len; i++)
	    {
	      elt = & (g_array_index (array, SortElt, i));
	      if (elt->offset > offset)
		elt->offset--;
	    }
	}
    }

  tree_model_sort->stamp++;
  gtk_signal_emit_by_name (GTK_OBJECT (data), "deleted", path);
  gtk_tree_path_free (path);
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
}

static gint
gtk_tree_model_sort_get_n_columns (GtkTreeModel *tree_model)
{
  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), 0);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, 0);

  return gtk_tree_model_get_n_columns (GTK_TREE_MODEL_SORT (tree_model)->model);
}

static GType
gtk_tree_model_sort_get_column_type (GtkTreeModel *tree_model,
				     gint          index)
{
  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), G_TYPE_INVALID);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, G_TYPE_INVALID);

  return gtk_tree_model_get_column_type (GTK_TREE_MODEL_SORT (tree_model)->model, index);
}

static gboolean
gtk_tree_model_sort_get_iter_helper (GtkTreeModelSort *tree_model_sort,
				     GArray       *array,
				     GtkTreeIter  *iter,
				     gint          depth,
				     GtkTreePath  *path)
{
  SortElt *elt;

  if (array == NULL)
    return FALSE;

  if (gtk_tree_path_get_indices (path)[depth] > array->len)
    return FALSE;

  elt = & (g_array_index (array, SortElt, gtk_tree_path_get_indices (path)[depth]));

  if (depth == gtk_tree_path_get_depth (path) - 1)
    {
      iter->stamp = tree_model_sort->stamp;
      iter->tree_node = elt;
      return TRUE;
    }

  if (elt->children != NULL)
    return gtk_tree_model_sort_get_iter_helper (tree_model_sort,
						elt->children,
						iter,
						depth + 1,
						path);

  if (gtk_tree_model_iter_has_child (tree_model_sort->model,
				     &(elt->iter)))

    gtk_tree_model_sort_build_level (tree_model_sort, elt);

  return gtk_tree_model_sort_get_iter_helper (tree_model_sort,
					      elt->children,
					      iter,
					      depth + 1,
					      path);
}

static gboolean
gtk_tree_model_sort_get_iter (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter,
			      GtkTreePath  *path)
{
  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);

  if (GTK_TREE_MODEL_SORT (tree_model)->root == NULL)
    gtk_tree_model_sort_build_level (GTK_TREE_MODEL_SORT (tree_model), NULL);

  return gtk_tree_model_sort_get_iter_helper (GTK_TREE_MODEL_SORT (tree_model),
					      GTK_TREE_MODEL_SORT (tree_model)->root,
					      iter, 0, path);
}

static GtkTreePath *
gtk_tree_model_sort_get_path (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter)
{
  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), NULL);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, NULL);

  return gtk_tree_model_get_path (GTK_TREE_MODEL_SORT (tree_model)->model, iter);
}

static void
gtk_tree_model_sort_get_value (GtkTreeModel *tree_model,
			       GtkTreeIter  *iter,
			       gint          column,
			       GValue        *value)
{
  SortElt *elt;

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

  elt = iter->tree_node;

  gtk_tree_model_get_value (GTK_TREE_MODEL_SORT (tree_model)->model, (GtkTreeIter *)elt, column, value);
}

static gboolean
gtk_tree_model_sort_iter_next (GtkTreeModel *tree_model,
			       GtkTreeIter  *iter)
{
  GArray *array;
  SortElt *elt;

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp, FALSE);

  elt = iter->tree_node;
  array = get_array (elt, tree_model);

  if (elt - ((SortElt*) array->data) >=  array->len - 1)
    {
      iter->stamp = 0;
      return FALSE;
    }
796
  iter->tree_node = elt + 1;
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
  return TRUE;
}

static gboolean
gtk_tree_model_sort_iter_children (GtkTreeModel *tree_model,
				   GtkTreeIter  *iter,
				   GtkTreeIter  *parent)
{
  SortElt *elt;

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);
  if (parent)
    g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == parent->stamp, FALSE);

  if (parent)
    elt = parent->tree_node;
  else
    elt = (SortElt *) ((GArray *)GTK_TREE_MODEL_SORT (tree_model)->root)->data;

  if (elt == NULL)
    return FALSE;

  if (elt->children == NULL &&
      gtk_tree_model_iter_has_child (GTK_TREE_MODEL_SORT (tree_model)->model, (GtkTreeIter *)elt))
    gtk_tree_model_sort_build_level (GTK_TREE_MODEL_SORT (tree_model), elt);

  if (elt->children == NULL)
    return FALSE;

  iter->stamp = GTK_TREE_MODEL_SORT (tree_model)->stamp;
  iter->tree_node = elt->children->data;

  return TRUE;
}

static gboolean
gtk_tree_model_sort_iter_has_child (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter)
{
  SortElt *elt;

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp, FALSE);

  elt = iter->tree_node;
  if (elt->children)
    return TRUE;

  return gtk_tree_model_iter_has_child (GTK_TREE_MODEL_SORT (tree_model)->model, (GtkTreeIter *) elt);
}

static gint
gtk_tree_model_sort_iter_n_children (GtkTreeModel *tree_model,
				     GtkTreeIter  *iter)
{
  SortElt *elt;

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), 0);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, 0);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp, 0);

  elt = iter->tree_node;
  if (elt->children)
    return elt->children->len;

  return gtk_tree_model_iter_n_children (GTK_TREE_MODEL_SORT (tree_model)->model, (GtkTreeIter *) elt);
}


static gboolean
gtk_tree_model_sort_iter_nth_child (GtkTreeModel *tree_model,
				    GtkTreeIter  *iter,
				    GtkTreeIter  *parent,
				    gint          n)
{
  SortElt *elt;

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);
  if (parent)
    g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == parent->stamp, FALSE);

  elt = iter->tree_node;


  if (elt->children == NULL)
    {
      if (gtk_tree_model_iter_has_child (GTK_TREE_MODEL_SORT (tree_model)->model, (GtkTreeIter *)elt))
	gtk_tree_model_sort_build_level (GTK_TREE_MODEL_SORT (tree_model), elt);
      else
	return FALSE;
    }

  if (elt->children == NULL)
    return FALSE;

  if (n >= elt->children->len)
    return FALSE;

  iter->stamp = GTK_TREE_MODEL_SORT (tree_model)->stamp;
  iter->tree_node = &g_array_index (elt->children, SortElt, n);

  return TRUE;
}

static gboolean
gtk_tree_model_sort_iter_parent (GtkTreeModel *tree_model,
				 GtkTreeIter  *iter,
				 GtkTreeIter  *child)
{
  SortElt *elt;

  g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);
  g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == child->stamp, FALSE);

  elt = iter->tree_node;
  if (elt->parent)
    {
      iter->stamp = GTK_TREE_MODEL_SORT (tree_model)->stamp;
      iter->tree_node = elt->parent;

      return TRUE;
    }
  return FALSE;
}

static void
gtk_tree_model_sort_ref_iter (GtkTreeModel *tree_model,
			      GtkTreeIter  *iter)
{
}

static void
gtk_tree_model_sort_unref_iter (GtkTreeModel *tree_model,
				GtkTreeIter  *iter)
{

}

/* Internal functions */
static void
gtk_tree_model_sort_build_level (GtkTreeModelSort *tree_model_sort,
				 SortElt          *place)
{
944
  gint n, i = 0;
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
  GArray *children;
  GtkTreeIter *parent_iter = NULL;
  GtkTreeIter iter;
  SortElt elt;

  if (place)
    parent_iter = & (place->iter);

      
  n = gtk_tree_model_iter_n_children (tree_model_sort->model, parent_iter);

  if (n == 0)
    return;

  children = g_array_sized_new (FALSE, FALSE, sizeof (SortElt), n);

  if (place)
    place->children = children;
  else
    tree_model_sort->root = children;

  gtk_tree_model_iter_children (tree_model_sort->model,
				&iter,
				parent_iter);

  do
    {
      elt.iter = iter;
      elt.parent = place;
      elt.children = NULL;
      elt.ref = 0;
      elt.offset = i;

      g_array_append_vals (children, &elt, 1);
      i++;
    }
  while (gtk_tree_model_iter_next (tree_model_sort->model, &iter));

}

static void
gtk_tree_model_sort_free_level (GArray *array)
{
  gint i;

990
991
992
  if (array == NULL)
    return;

993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
  for (i = 0; i < array->len; i++)
    {
      SortElt *elt;

      elt = &g_array_index (array, SortElt, i);
      if (elt->children)
	gtk_tree_model_sort_free_level (array);
    }

  g_array_free (array, TRUE);
}

gint
g_value_string_compare_func (const GValue *a,
			     const GValue *b)
{
1009
1010
1011
1012
1013
1014
1015
1016
1017
  gchar *a_str = g_value_get_string (a);
  gchar *b_str = g_value_get_string (b);

  if (b_str == NULL)
    return a_str == NULL;
  if (a_str == NULL)
    return -1;

  return strcmp (a_str, b_str);
1018
1019
1020
1021
1022
1023
1024
1025
}

gint
g_value_int_compare_func (const GValue *a,
			  const GValue *b)
{
  return g_value_get_int (a) < g_value_get_int (b);
}