gtktreemodelfilter.c 148 KB
Newer Older
1 2 3 4
/* gtktreemodelfilter.c
 * Copyright (C) 2000,2001  Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
 * Copyright (C) 2001-2003  Kristian Rietveld <kris@gtk.org>
 *
5 6 7 8
 * 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.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Library General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18
 */

19
#include "config.h"
20
#include "gtktreemodelfilter.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
21
#include "gtkintl.h"
22
#include "gtktreednd.h"
23
#include "gtkprivate.h"
24 25
#include <string.h>

26 27 28 29 30 31 32 33 34

/**
 * SECTION:gtktreemodelfilter
 * @Short_description: A GtkTreeModel which hides parts of an underlying tree model
 * @Title: GtkTreeModelFilter
 * @See_also:#GtkTreeModelSort
 *
 * A #GtkTreeModelFilter is a tree model which wraps another tree model,
 * and can do the following things:
35
 * 
36
 * - Filter specific rows, based on data from a “visible column”, a column
37
 *   storing booleans indicating whether the row should be filtered or not,
38
 *   or based on the return value of a “visible function”, which gets a
39 40 41
 *   model, iter and user_data and returns a boolean indicating whether the
 *   row should be filtered or not.
 * 
42
 * - Modify the “appearance” of the model, using a modify function.
43 44 45 46
 *   This is extremely powerful and allows for just changing some
 *   values and also for creating a completely different model based
 *   on the given child model.
 *
47
 * - Set a different root node, also known as a “virtual root”. You can pass
48 49
 *   in a #GtkTreePath indicating the root node for the filter at construction
 *   time.
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
 * The basic API is similar to #GtkTreeModelSort. For an example on its usage,
 * see the section on #GtkTreeModelSort.
 *
 * When using #GtkTreeModelFilter, it is important to realize that
 * #GtkTreeModelFilter maintains an internal cache of all nodes which are
 * visible in its clients. The cache is likely to be a subtree of the tree
 * exposed by the child model. #GtkTreeModelFilter will not cache the entire
 * child model when unnecessary to not compromise the caching mechanism
 * that is exposed by the reference counting scheme. If the child model
 * implements reference counting, unnecessary signals may not be emitted
 * because of reference counting rule 3, see the #GtkTreeModel
 * documentation. (Note that e.g. #GtkTreeStore does not implement
 * reference counting and will always emit all signals, even when
 * the receiving node is not visible).
 *
 * Because of this, limitations for possible visible functions do apply.
 * In general, visible functions should only use data or properties from
 * the node for which the visibility state must be determined, its siblings
 * or its parents. Usually, having a dependency on the state of any child
 * node is not possible, unless references are taken on these explicitly.
 * When no such reference exists, no signals may be received for these child
 * nodes (see reference couting rule number 3 in the #GtkTreeModel section).
 *
 * Determining the visibility state of a given node based on the state
 * of its child nodes is a frequently occurring use case. Therefore,
 * #GtkTreeModelFilter explicitly supports this. For example, when a node
 * does not have any children, you might not want the node to be visible.
78 79
 * As soon as the first row is added to the node’s child level (or the
 * last row removed), the node’s visibility should be updated.
80 81
 *
 * This introduces a dependency from the node on its child nodes. In order
82
 * to accommodate this, #GtkTreeModelFilter must make sure the necessary
83 84 85 86 87 88 89 90 91 92
 * signals are received from the child model. This is achieved by building,
 * for all nodes which are exposed as visible nodes to #GtkTreeModelFilter's
 * clients, the child level (if any) and take a reference on the first node
 * in this level. Furthermore, for every row-inserted, row-changed or
 * row-deleted signal (also these which were not handled because the node
 * was not cached), #GtkTreeModelFilter will check if the visibility state
 * of any parent node has changed.
 *
 * Beware, however, that this explicit support is limited to these two
 * cases. For example, if you want a node to be visible only if two nodes
93
 * in a child’s child level (2 levels deeper) are visible, you are on your
94 95 96 97
 * own. In this case, either rely on #GtkTreeStore to emit all signals
 * because it does not implement reference counting, or for models that
 * do implement reference counting, obtain references on these child levels
 * yourself.
98 99
 */

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/* Notes on this implementation of GtkTreeModelFilter
 * ==================================================
 *
 * Warnings
 * --------
 *
 * In this code there is a potential for confusion as to whether an iter,
 * path or value refers to the GtkTreeModelFilter model, or to the child
 * model that has been set. As a convention, variables referencing the
 * child model will have a c_ prefix before them (ie. c_iter, c_value,
 * c_path). In case the c_ prefixed names are already in use, an f_
 * prefix is used. Conversion of iterators and paths between
 * GtkTreeModelFilter and the child model is done through the various
 * gtk_tree_model_filter_convert_* functions.
 *
 * Even though the GtkTreeModelSort and GtkTreeModelFilter have very
 * similar data structures, many assumptions made in the GtkTreeModelSort
 * code do *not* apply in the GtkTreeModelFilter case. Reference counting
 * in particular is more complicated in GtkTreeModelFilter, because
119
 * we explicitly support reliance on the state of a node’s children as
120 121 122 123 124 125 126 127 128
 * outlined in the public API documentation. Because of these differences,
 * you are strongly recommended to first read through these notes before
 * making any modification to the code.
 *
 * Iterator format
 * ---------------
 *
 * The iterator format of iterators handed out by GtkTreeModelFilter is
 * as follows:
129
 *
130 131 132
 *     iter->stamp = filter->priv->stamp
 *     iter->user_data = FilterLevel
 *     iter->user_data2 = FilterElt
133
 *
134 135 136
 * Internal data structure
 * -----------------------
 *
137
 * Using FilterLevel and FilterElt, GtkTreeModelFilter maintains a “cache”
138 139 140 141 142 143 144
 * of the mapping from GtkTreeModelFilter nodes to nodes in the child model.
 * This is to avoid re-creating a level each time (which involves computing
 * visibility for each node in that level) an operation is requested on
 * GtkTreeModelFilter, such as get iter, get path and get value.
 *
 * A FilterElt corresponds to a single node. The FilterElt can either be
 * visible or invisible in the model that is exposed to the clients of this
145
 * GtkTreeModelFilter. The visibility state is stored in the “visible_siter”
146 147
 * field, which is NULL when the node is not visible. The FilterLevel keeps
 * a reference to the parent FilterElt and its FilterLevel (if any). The
148
 * FilterElt can have a “children” pointer set, which points at a child
149 150 151 152 153 154 155 156 157 158 159 160 161 162
 * level (a sub level).
 *
 * In a FilterLevel, two separate GSequences are maintained. One contains
 * all nodes of this FilterLevel, regardless of the visibility state of
 * the node. Another contains only visible nodes. A visible FilterElt
 * is thus present in both the full and the visible GSequence. The
 * GSequence allows for fast access, addition and removal of nodes.
 *
 * It is important to recognize the two different mappings that play
 * a part in this code:
 *   I.  The mapping from the client to this model. The order in which
 *       nodes are stored in the *visible* GSequence is the order in
 *       which the nodes are exposed to clients of the GtkTreeModelFilter.
 *   II. The mapping from this model to its child model. Each FilterElt
163
 *       contains an “offset” field which is the offset of the
164 165 166 167 168
 *       corresponding node in the child model.
 *
 * Throughout the code, two kinds of paths relative to the GtkTreeModelFilter
 * (those generated from the sequence positions) are used. There are paths
 * which take non-visible nodes into account (generated from the full
169
 * sequences) and paths which don’t (generated from the visible sequences).
170 171 172 173 174 175 176 177 178 179
 * Paths which have been generated from the full sequences should only be
 * used internally and NEVER be passed along with a signal emisson.
 *
 * Reference counting
 * ------------------
 *
 * GtkTreeModelFilter forwards all reference and unreference operations
 * to the corresponding node in the child model. In addition,
 * GtkTreeModelFilter will also add references of its own. The full reference
 * count of each node (i.e. all forwarded references and these by the
180
 * filter model) is maintained internally in the “ref_count” fields in
181 182
 * FilterElt and FilterLevel. Because there is a need to determine whether
 * a node should be visible for the client, the reference count of only
183
 * the forwarded references is maintained as well, in the “ext_ref_count”
184 185 186 187 188 189 190 191 192 193 194
 * fields.
 *
 * In a few cases, GtkTreeModelFilter takes additional references on
 * nodes. The first case is that a reference is taken on the parent
 * (if any) of each level. This happens in gtk_tree_model_filter_build_level()
 * and the reference is released again in gtk_tree_model_filter_free_level().
 * This ensures that for all references which are taken by the filter
 * model, all parent nodes are referenced according to reference counting
 * rule 1 in the GtkTreeModel documentation.
 *
 * A second case is required to support visible functions which depend on
195
 * the state of a node’s children (see the public API documentation for
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
 * GtkTreeModelFilter above). We build the child level of each node that
 * could be visible in the client (i.e. the level has an ext_ref_count > 0;
 * not the elt, because the elt might be invisible and thus unreferenced
 * by the client). For each node that becomes visible, due to insertion or
 * changes in visibility state, it is checked whether node has children, if
 * so the child level is built.
 *
 * A reference is taken on the first node of each level so that the child
 * model will emit all signals for this level, due to reference counting
 * rule 3 in the GtkTreeModel documentation. If due to changes in the level,
 * another node becomes the first node (e.g. due to insertion or reordering),
 * this reference is transferred from the old to the new first node.
 *
 * When a level has an *external* reference count of zero (which means that
 * none of the nodes in the level is referenced by the clients), the level
211
 * has a “zero ref count” on all its parents. As soon as the level reaches
212 213 214 215 216 217 218 219 220 221
 * an *external* reference count of zero, the zero ref count value is
 * incremented by one for all parents of this level. Due to the additional
 * references taken by the filter model, it is important to base the
 * zero ref count on the external reference count instead of on the full
 * reference count of the node.
 *
 * The zero ref count value aids in determining which portions of the
 * cache are possibly unused and could be removed. If a FilterElt has
 * a zero ref count of one, then its child level is unused. However, the
 * child level can only be removed from the cache if the FilterElt's
222 223 224 225
 * parent level has an external ref count of zero. (Not the parent elt,
 * because an invisible parent elt with external ref count == 0 might still
 * become visible because of a state change in its child level!).  Otherwise,
 * monitoring this level is necessary to possibly update the visibility state
226 227 228 229 230 231 232 233 234
 * of the parent. This is an important difference from GtkTreeModelSort!
 *
 * Signals are only required for levels with an external ref count > 0.
 * This due to reference counting rule 3, see the GtkTreeModel
 * documentation. In the GtkTreeModelFilter we try hard to stick to this
 * rule and not emit redundant signals (though redundant emissions of
 * row-has-child-toggled could appear frequently; it does happen that
 * we simply forward the signal emitted by e.g. GtkTreeStore but also
 * emit our own copy).
235 236
 */

237

238 239 240 241 242 243 244 245 246
typedef struct _FilterElt FilterElt;
typedef struct _FilterLevel FilterLevel;

struct _FilterElt
{
  GtkTreeIter iter;
  FilterLevel *children;
  gint offset;
  gint ref_count;
247
  gint ext_ref_count;
248
  gint zero_ref_count;
249
  GSequenceIter *visible_siter; /* iter into visible_seq */
250 251 252 253
};

struct _FilterLevel
{
254 255
  GSequence *seq;
  GSequence *visible_seq;
256
  gint ref_count;
257
  gint ext_ref_count;
258

259
  FilterElt *parent_elt;
260 261 262
  FilterLevel *parent_level;
};

263

264 265
struct _GtkTreeModelFilterPrivate
{
266
  GtkTreeModel *child_model;
267
  gpointer root;
268 269
  GtkTreePath *virtual_root;

270 271 272
  gint stamp;
  guint child_flags;
  gint zero_ref_count;
273
  gint visible_column;
274 275 276

  GtkTreeModelFilterVisibleFunc visible_func;
  gpointer visible_data;
277
  GDestroyNotify visible_destroy;
278 279 280 281

  GType *modify_types;
  GtkTreeModelFilterModifyFunc modify_func;
  gpointer modify_data;
282
  GDestroyNotify modify_destroy;
283
  gint modify_n_columns;
284

285 286
  guint visible_method_set   : 1;
  guint modify_func_set      : 1;
287

288 289
  guint in_row_deleted       : 1;
  guint virtual_root_deleted : 1;
290

291
  /* signal ids */
292 293 294 295 296
  gulong changed_id;
  gulong inserted_id;
  gulong has_child_toggled_id;
  gulong deleted_id;
  gulong reordered_id;
297 298 299 300 301 302 303 304 305 306
};

/* properties */
enum
{
  PROP_0,
  PROP_CHILD_MODEL,
  PROP_VIRTUAL_ROOT
};

307 308 309 310 311 312 313
/* Set this to 0 to disable caching of child iterators.  This
 * allows for more stringent testing.  It is recommended to set this
 * to one when refactoring this code and running the unit tests to
 * catch more errors.
 */
#if 1
#  define GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS(filter) \
314
        (((GtkTreeModelFilter *)filter)->priv->child_flags & GTK_TREE_MODEL_ITERS_PERSIST)
315 316 317
#else
#  define GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS(filter) (FALSE)
#endif
318

319 320 321 322 323
/* Defining this constant enables more assertions, which will be
 * helpful when debugging the code.
 */
#undef MODEL_FILTER_DEBUG

324 325
#define FILTER_ELT(filter_elt) ((FilterElt *)filter_elt)
#define FILTER_LEVEL(filter_level) ((FilterLevel *)filter_level)
326
#define GET_ELT(siter) ((FilterElt*) (siter ? g_sequence_get (siter) : NULL))
327 328 329

/* general code (object/interface init, properties, etc) */
static void         gtk_tree_model_filter_tree_model_init                 (GtkTreeModelIface       *iface);
330
static void         gtk_tree_model_filter_drag_source_init                (GtkTreeDragSourceIface  *iface);
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
static void         gtk_tree_model_filter_finalize                        (GObject                 *object);
static void         gtk_tree_model_filter_set_property                    (GObject                 *object,
                                                                           guint                    prop_id,
                                                                           const GValue            *value,
                                                                           GParamSpec              *pspec);
static void         gtk_tree_model_filter_get_property                    (GObject                 *object,
                                                                           guint                    prop_id,
                                                                           GValue                 *value,
                                                                           GParamSpec             *pspec);

/* signal handlers */
static void         gtk_tree_model_filter_row_changed                     (GtkTreeModel           *c_model,
                                                                           GtkTreePath            *c_path,
                                                                           GtkTreeIter            *c_iter,
                                                                           gpointer                data);
static void         gtk_tree_model_filter_row_inserted                    (GtkTreeModel           *c_model,
                                                                           GtkTreePath            *c_path,
                                                                           GtkTreeIter            *c_iter,
                                                                           gpointer                data);
static void         gtk_tree_model_filter_row_has_child_toggled           (GtkTreeModel           *c_model,
                                                                           GtkTreePath            *c_path,
                                                                           GtkTreeIter            *c_iter,
                                                                           gpointer                data);
static void         gtk_tree_model_filter_row_deleted                     (GtkTreeModel           *c_model,
                                                                           GtkTreePath            *c_path,
                                                                           gpointer                data);
static void         gtk_tree_model_filter_rows_reordered                  (GtkTreeModel           *c_model,
                                                                           GtkTreePath            *c_path,
                                                                           GtkTreeIter            *c_iter,
                                                                           gint                   *new_order,
                                                                           gpointer                data);

/* GtkTreeModel interface */
364
static GtkTreeModelFlags gtk_tree_model_filter_get_flags                       (GtkTreeModel           *model);
365 366 367
static gint         gtk_tree_model_filter_get_n_columns                   (GtkTreeModel           *model);
static GType        gtk_tree_model_filter_get_column_type                 (GtkTreeModel           *model,
                                                                           gint                    index);
368 369 370
static gboolean     gtk_tree_model_filter_get_iter_full                   (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
                                                                           GtkTreePath            *path);
371 372 373 374 375 376 377 378 379 380 381
static gboolean     gtk_tree_model_filter_get_iter                        (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
                                                                           GtkTreePath            *path);
static GtkTreePath *gtk_tree_model_filter_get_path                        (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter);
static void         gtk_tree_model_filter_get_value                       (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
                                                                           gint                    column,
                                                                           GValue                 *value);
static gboolean     gtk_tree_model_filter_iter_next                       (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter);
382 383
static gboolean     gtk_tree_model_filter_iter_previous                   (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
static gboolean     gtk_tree_model_filter_iter_children                   (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
                                                                           GtkTreeIter            *parent);
static gboolean     gtk_tree_model_filter_iter_has_child                  (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter);
static gint         gtk_tree_model_filter_iter_n_children                 (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter);
static gboolean     gtk_tree_model_filter_iter_nth_child                  (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
                                                                           GtkTreeIter            *parent,
                                                                           gint                    n);
static gboolean     gtk_tree_model_filter_iter_parent                     (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
                                                                           GtkTreeIter            *child);
static void         gtk_tree_model_filter_ref_node                        (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter);
static void         gtk_tree_model_filter_unref_node                      (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter);

403 404 405 406 407 408 409 410
/* TreeDragSource interface */
static gboolean    gtk_tree_model_filter_row_draggable                    (GtkTreeDragSource      *drag_source,
                                                                           GtkTreePath            *path);
static gboolean    gtk_tree_model_filter_drag_data_get                    (GtkTreeDragSource      *drag_source,
                                                                           GtkTreePath            *path,
                                                                           GtkSelectionData       *selection_data);
static gboolean    gtk_tree_model_filter_drag_data_delete                 (GtkTreeDragSource      *drag_source,
                                                                           GtkTreePath            *path);
411 412 413 414

/* private functions */
static void        gtk_tree_model_filter_build_level                      (GtkTreeModelFilter     *filter,
                                                                           FilterLevel            *parent_level,
415
                                                                           FilterElt              *parent_elt,
416 417
                                                                           gboolean                emit_inserted);

418
static void        gtk_tree_model_filter_free_level                       (GtkTreeModelFilter     *filter,
419
                                                                           FilterLevel            *filter_level,
420 421
                                                                           gboolean                unref_self,
                                                                           gboolean                unref_parent,
422
                                                                           gboolean                unref_external);
423 424 425 426 427 428 429 430 431 432 433 434

static GtkTreePath *gtk_tree_model_filter_elt_get_path                    (FilterLevel            *level,
                                                                           FilterElt              *elt,
                                                                           GtkTreePath            *root);

static GtkTreePath *gtk_tree_model_filter_add_root                        (GtkTreePath            *src,
                                                                           GtkTreePath            *root);
static GtkTreePath *gtk_tree_model_filter_remove_root                     (GtkTreePath            *src,
                                                                           GtkTreePath            *root);

static void         gtk_tree_model_filter_increment_stamp                 (GtkTreeModelFilter     *filter);

435 436 437 438 439 440 441 442
static void         gtk_tree_model_filter_real_modify                     (GtkTreeModelFilter     *self,
                                                                           GtkTreeModel           *child_model,
                                                                           GtkTreeIter            *iter,
                                                                           GValue                 *value,
                                                                           gint                    column);
static gboolean     gtk_tree_model_filter_real_visible                    (GtkTreeModelFilter     *filter,
                                                                           GtkTreeModel           *child_model,
                                                                           GtkTreeIter            *child_iter);
443 444 445 446 447
static gboolean     gtk_tree_model_filter_visible                         (GtkTreeModelFilter     *filter,
                                                                           GtkTreeIter            *child_iter);
static void         gtk_tree_model_filter_clear_cache_helper              (GtkTreeModelFilter     *filter,
                                                                           FilterLevel            *level);

448 449 450
static void         gtk_tree_model_filter_real_ref_node                   (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
                                                                           gboolean                external);
451 452
static void         gtk_tree_model_filter_real_unref_node                 (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
453
                                                                           gboolean                external,
454 455 456 457
                                                                           gboolean                propagate_unref);

static void         gtk_tree_model_filter_set_model                       (GtkTreeModelFilter     *filter,
                                                                           GtkTreeModel           *child_model);
458 459 460
static void         gtk_tree_model_filter_ref_path                        (GtkTreeModelFilter     *filter,
                                                                           GtkTreePath            *path);
static void         gtk_tree_model_filter_unref_path                      (GtkTreeModelFilter     *filter,
461 462
                                                                           GtkTreePath            *path,
                                                                           int                     depth);
463 464 465 466 467 468
static void         gtk_tree_model_filter_set_root                        (GtkTreeModelFilter     *filter,
                                                                           GtkTreePath            *root);

static GtkTreePath *gtk_real_tree_model_filter_convert_child_path_to_path (GtkTreeModelFilter     *filter,
                                                                           GtkTreePath            *child_path,
                                                                           gboolean                build_levels,
Matthias Clasen's avatar
Matthias Clasen committed
469
                                                                           gboolean                fetch_children);
470

471 472
static gboolean    gtk_tree_model_filter_elt_is_visible_in_target         (FilterLevel            *level,
                                                                           FilterElt              *elt);
473

474 475 476 477 478
static FilterElt   *gtk_tree_model_filter_insert_elt_in_level             (GtkTreeModelFilter     *filter,
                                                                           GtkTreeIter            *c_iter,
                                                                           FilterLevel            *level,
                                                                           gint                    offset,
                                                                           gint                   *index);
479 480 481 482
static FilterElt   *gtk_tree_model_filter_fetch_child                     (GtkTreeModelFilter     *filter,
                                                                           FilterLevel            *level,
                                                                           gint                    offset,
                                                                           gint                   *index);
483
static void         gtk_tree_model_filter_remove_elt_from_level           (GtkTreeModelFilter     *filter,
484 485
                                                                           FilterLevel            *level,
                                                                           FilterElt              *elt);
Matthias Clasen's avatar
Matthias Clasen committed
486
static void         gtk_tree_model_filter_update_children                 (GtkTreeModelFilter     *filter,
487 488
                                                                           FilterLevel            *level,
                                                                           FilterElt              *elt);
489 490 491 492
static void         gtk_tree_model_filter_emit_row_inserted_for_path      (GtkTreeModelFilter     *filter,
                                                                           GtkTreeModel           *c_model,
                                                                           GtkTreePath            *c_path,
                                                                           GtkTreeIter            *c_iter);
493 494


Matthias Clasen's avatar
Matthias Clasen committed
495
G_DEFINE_TYPE_WITH_CODE (GtkTreeModelFilter, gtk_tree_model_filter, G_TYPE_OBJECT,
496
                         G_ADD_PRIVATE (GtkTreeModelFilter)
Matthias Clasen's avatar
Matthias Clasen committed
497 498 499
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
						gtk_tree_model_filter_tree_model_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
Matthias Clasen's avatar
Matthias Clasen committed
500
						gtk_tree_model_filter_drag_source_init))
501 502 503 504

static void
gtk_tree_model_filter_init (GtkTreeModelFilter *filter)
{
505
  filter->priv = gtk_tree_model_filter_get_instance_private (filter);
506 507 508 509
  filter->priv->visible_column = -1;
  filter->priv->zero_ref_count = 0;
  filter->priv->visible_method_set = FALSE;
  filter->priv->modify_func_set = FALSE;
510
  filter->priv->in_row_deleted = FALSE;
511
  filter->priv->virtual_root_deleted = FALSE;
512 513 514 515 516 517 518 519 520 521 522 523 524 525
}

static void
gtk_tree_model_filter_class_init (GtkTreeModelFilterClass *filter_class)
{
  GObjectClass *object_class;

  object_class = (GObjectClass *) filter_class;

  object_class->set_property = gtk_tree_model_filter_set_property;
  object_class->get_property = gtk_tree_model_filter_get_property;

  object_class->finalize = gtk_tree_model_filter_finalize;

526 527 528
  filter_class->visible = gtk_tree_model_filter_real_visible;
  filter_class->modify  = gtk_tree_model_filter_real_modify;

529 530 531 532 533
  /* Properties -- FIXME: disabled translations for now, until I can come up with a
   * better description
   */
  g_object_class_install_property (object_class,
                                   PROP_CHILD_MODEL,
534
                                   g_param_spec_object ("child-model",
535 536 537
                                                        ("The child model"),
                                                        ("The model for the filtermodel to filter"),
                                                        GTK_TYPE_TREE_MODEL,
538
                                                        GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
539 540 541

  g_object_class_install_property (object_class,
                                   PROP_VIRTUAL_ROOT,
542
                                   g_param_spec_boxed ("virtual-root",
543 544 545
                                                       ("The virtual root"),
                                                       ("The virtual root (relative to the child model) for this filtermodel"),
                                                       GTK_TYPE_TREE_PATH,
546
                                                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
547 548 549 550 551 552 553 554 555 556 557 558
}

static void
gtk_tree_model_filter_tree_model_init (GtkTreeModelIface *iface)
{
  iface->get_flags = gtk_tree_model_filter_get_flags;
  iface->get_n_columns = gtk_tree_model_filter_get_n_columns;
  iface->get_column_type = gtk_tree_model_filter_get_column_type;
  iface->get_iter = gtk_tree_model_filter_get_iter;
  iface->get_path = gtk_tree_model_filter_get_path;
  iface->get_value = gtk_tree_model_filter_get_value;
  iface->iter_next = gtk_tree_model_filter_iter_next;
559
  iface->iter_previous = gtk_tree_model_filter_iter_previous;
560 561 562 563 564 565 566 567 568
  iface->iter_children = gtk_tree_model_filter_iter_children;
  iface->iter_has_child = gtk_tree_model_filter_iter_has_child;
  iface->iter_n_children = gtk_tree_model_filter_iter_n_children;
  iface->iter_nth_child = gtk_tree_model_filter_iter_nth_child;
  iface->iter_parent = gtk_tree_model_filter_iter_parent;
  iface->ref_node = gtk_tree_model_filter_ref_node;
  iface->unref_node = gtk_tree_model_filter_unref_node;
}

569 570 571 572 573 574 575 576
static void
gtk_tree_model_filter_drag_source_init (GtkTreeDragSourceIface *iface)
{
  iface->row_draggable = gtk_tree_model_filter_row_draggable;
  iface->drag_data_delete = gtk_tree_model_filter_drag_data_delete;
  iface->drag_data_get = gtk_tree_model_filter_drag_data_get;
}

577 578 579 580 581 582

static void
gtk_tree_model_filter_finalize (GObject *object)
{
  GtkTreeModelFilter *filter = (GtkTreeModelFilter *) object;

583 584
  if (filter->priv->virtual_root && !filter->priv->virtual_root_deleted)
    {
585 586
      gtk_tree_model_filter_unref_path (filter, filter->priv->virtual_root,
                                        -1);
587 588
      filter->priv->virtual_root_deleted = TRUE;
    }
589

590 591 592 593 594 595
  gtk_tree_model_filter_set_model (filter, NULL);

  if (filter->priv->virtual_root)
    gtk_tree_path_free (filter->priv->virtual_root);

  if (filter->priv->root)
596
    gtk_tree_model_filter_free_level (filter, filter->priv->root, TRUE, TRUE, FALSE);
597

598
  g_free (filter->priv->modify_types);
599 600 601 602 603 604
  
  if (filter->priv->modify_destroy)
    filter->priv->modify_destroy (filter->priv->modify_data);

  if (filter->priv->visible_destroy)
    filter->priv->visible_destroy (filter->priv->visible_data);
605 606

  /* must chain up */
Matthias Clasen's avatar
Matthias Clasen committed
607
  G_OBJECT_CLASS (gtk_tree_model_filter_parent_class)->finalize (object);
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
}

static void
gtk_tree_model_filter_set_property (GObject      *object,
                                    guint         prop_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
{
  GtkTreeModelFilter *filter = GTK_TREE_MODEL_FILTER (object);

  switch (prop_id)
    {
      case PROP_CHILD_MODEL:
        gtk_tree_model_filter_set_model (filter, g_value_get_object (value));
        break;
      case PROP_VIRTUAL_ROOT:
        gtk_tree_model_filter_set_root (filter, g_value_get_boxed (value));
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
gtk_tree_model_filter_get_property (GObject    *object,
                                    guint       prop_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
  GtkTreeModelFilter *filter = GTK_TREE_MODEL_FILTER (object);

  switch (prop_id)
    {
      case PROP_CHILD_MODEL:
        g_value_set_object (value, filter->priv->child_model);
        break;
      case PROP_VIRTUAL_ROOT:
        g_value_set_boxed (value, filter->priv->virtual_root);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

/* helpers */

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 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
static FilterElt *
filter_elt_new (void)
{
  return g_slice_new (FilterElt);
}

static void
filter_elt_free (gpointer elt)
{
  g_slice_free (FilterElt, elt);
}

static gint
filter_elt_cmp (gconstpointer a,
                gconstpointer b,
                gpointer      user_data)
{
  const FilterElt *elt_a = a;
  const FilterElt *elt_b = b;

  if (elt_a->offset > elt_b->offset)
    return +1;
  else if (elt_a->offset < elt_b->offset)
    return -1;
  else
    return 0;
}

static FilterElt *
lookup_elt_with_offset (GSequence      *seq,
                        gint            offset,
                        GSequenceIter **ret_siter)
{
  GSequenceIter *siter;
  FilterElt dummy;

  dummy.offset = offset;
  siter = g_sequence_lookup (seq, &dummy, filter_elt_cmp, NULL);

  if (ret_siter)
    *ret_siter = siter;

  return GET_ELT (siter);
}

static void
increase_offset_iter (gpointer data,
                      gpointer user_data)
{
  FilterElt *elt = data;
  gint offset = GPOINTER_TO_INT (user_data);

  if (elt->offset >= offset)
    elt->offset++;
}

static void
decrease_offset_iter (gpointer data,
                      gpointer user_data)
{
  FilterElt *elt = data;
  gint offset = GPOINTER_TO_INT (user_data);

  if (elt->offset > offset)
    elt->offset--;
}

723 724 725
static void
gtk_tree_model_filter_build_level (GtkTreeModelFilter *filter,
                                   FilterLevel        *parent_level,
726
                                   FilterElt          *parent_elt,
727
                                   gboolean            emit_inserted)
728 729
{
  GtkTreeIter iter;
730
  GtkTreeIter first_node;
731 732
  GtkTreeIter root;
  FilterLevel *new_level;
733
  FilterLevel *tmp_level;
734
  FilterElt *tmp_elt;
735
  GtkTreeIter f_iter;
736 737
  gint length = 0;
  gint i;
738
  gboolean empty = TRUE;
739 740 741

  g_assert (filter->priv->child_model != NULL);

742 743
  /* Avoid building a level that already exists */
  if (parent_level)
744
    g_assert (parent_elt->children == NULL);
745 746 747
  else
    g_assert (filter->priv->root == NULL);

748 749 750
  if (filter->priv->in_row_deleted)
    return;

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
  if (!parent_level)
    {
      if (filter->priv->virtual_root)
        {
          if (gtk_tree_model_get_iter (filter->priv->child_model, &root, filter->priv->virtual_root) == FALSE)
            return;
          length = gtk_tree_model_iter_n_children (filter->priv->child_model, &root);

          if (gtk_tree_model_iter_children (filter->priv->child_model, &iter, &root) == FALSE)
            return;
        }
      else
        {
          if (!gtk_tree_model_get_iter_first (filter->priv->child_model, &iter))
            return;
          length = gtk_tree_model_iter_n_children (filter->priv->child_model, NULL);
        }
    }
  else
    {
      GtkTreeIter parent_iter;
      GtkTreeIter child_parent_iter;

      parent_iter.stamp = filter->priv->stamp;
      parent_iter.user_data = parent_level;
      parent_iter.user_data2 = parent_elt;

      gtk_tree_model_filter_convert_iter_to_child_iter (filter,
                                                        &child_parent_iter,
                                                        &parent_iter);
      if (gtk_tree_model_iter_children (filter->priv->child_model, &iter, &child_parent_iter) == FALSE)
        return;

      /* stamp may have changed */
      gtk_tree_model_filter_convert_iter_to_child_iter (filter,
                                                        &child_parent_iter,
                                                        &parent_iter);
      length = gtk_tree_model_iter_n_children (filter->priv->child_model, &child_parent_iter);
789 790 791 792

      /* Take a reference on the parent */
      gtk_tree_model_filter_real_ref_node (GTK_TREE_MODEL (filter),
                                           &parent_iter, FALSE);
793 794 795 796 797
    }

  g_return_if_fail (length > 0);

  new_level = g_new (FilterLevel, 1);
798 799
  new_level->seq = g_sequence_new (filter_elt_free);
  new_level->visible_seq = g_sequence_new (NULL);
800
  new_level->ref_count = 0;
801
  new_level->ext_ref_count = 0;
802
  new_level->parent_elt = parent_elt;
803 804
  new_level->parent_level = parent_level;

805
  if (parent_elt)
806 807 808 809 810
    parent_elt->children = new_level;
  else
    filter->priv->root = new_level;

  /* increase the count of zero ref_counts */
811
  tmp_level = parent_level;
812
  tmp_elt = parent_elt;
813 814

  while (tmp_level)
815
    {
816
      tmp_elt->zero_ref_count++;
817

818
      tmp_elt = tmp_level->parent_elt;
819
      tmp_level = tmp_level->parent_level;
820
    }
821 822
  if (new_level != filter->priv->root)
    filter->priv->zero_ref_count++;
823 824 825

  i = 0;

826
  first_node = iter;
827 828 829 830 831

  do
    {
      if (gtk_tree_model_filter_visible (filter, &iter))
        {
832
          FilterElt *filter_elt;
833

834 835 836 837 838 839 840
          filter_elt = filter_elt_new ();
          filter_elt->offset = i;
          filter_elt->zero_ref_count = 0;
          filter_elt->ref_count = 0;
          filter_elt->ext_ref_count = 0;
          filter_elt->children = NULL;
          filter_elt->visible_siter = NULL;
841 842

          if (GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS (filter))
843
            filter_elt->iter = iter;
844

845 846 847
          g_sequence_append (new_level->seq, filter_elt);
          filter_elt->visible_siter = g_sequence_append (new_level->visible_seq, filter_elt);
          empty = FALSE;
848

849 850 851
          if (emit_inserted)
            {
              GtkTreePath *f_path;
852
              GtkTreeIter f_iter;
853 854
              GtkTreeIter children;

855 856
              f_iter.stamp = filter->priv->stamp;
              f_iter.user_data = new_level;
857
              f_iter.user_data2 = filter_elt;
858

859 860 861 862 863 864 865 866 867 868 869
              f_path = gtk_tree_model_get_path (GTK_TREE_MODEL (filter),
                                                &f_iter);
              gtk_tree_model_row_inserted (GTK_TREE_MODEL (filter),
                                           f_path, &f_iter);
              gtk_tree_path_free (f_path);

              if (gtk_tree_model_iter_children (filter->priv->child_model,
                                                &children, &iter))
                gtk_tree_model_filter_update_children (filter,
                                                       new_level,
                                                       FILTER_ELT (f_iter.user_data2));
870
            }
871 872 873 874
        }
      i++;
    }
  while (gtk_tree_model_iter_next (filter->priv->child_model, &iter));
875

876 877 878
  /* The level does not contain any visible nodes.  However, changes in
   * this level might affect the parent node, which can either be visible
   * or invisible.  Therefore, this level can only be removed again,
879 880 881
   * if the parent level has an external reference count of zero.  That is,
   * if this level changes state, no signals are required in the parent
   * level.
882
   */
883
  if (empty &&
884
       (parent_level && parent_level->ext_ref_count == 0))
885
    {
886
      gtk_tree_model_filter_free_level (filter, new_level, FALSE, TRUE, FALSE);
887 888 889 890 891 892
      return;
    }

  /* If none of the nodes are visible, we will just pull in the
   * first node of the level.
   */
893
  if (empty)
894
    {
895
      FilterElt *filter_elt;
896

897 898 899 900 901 902 903
      filter_elt = filter_elt_new ();
      filter_elt->offset = 0;
      filter_elt->zero_ref_count = 0;
      filter_elt->ref_count = 0;
      filter_elt->ext_ref_count = 0;
      filter_elt->children = NULL;
      filter_elt->visible_siter = NULL;
904 905

      if (GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS (filter))
906
        filter_elt->iter = first_node;
907

908
      g_sequence_append (new_level->seq, filter_elt);
909
    }
910

911 912 913 914 915
  /* Keep a reference on the first node of this level.  We need this
   * to make sure that we get all signals for this level.
   */
  f_iter.stamp = filter->priv->stamp;
  f_iter.user_data = new_level;
916
  f_iter.user_data2 = g_sequence_get (g_sequence_get_begin_iter (new_level->seq));
917

918 919
  gtk_tree_model_filter_real_ref_node (GTK_TREE_MODEL (filter), &f_iter,
                                       FALSE);
920 921 922 923
}

static void
gtk_tree_model_filter_free_level (GtkTreeModelFilter *filter,
924
                                  FilterLevel        *filter_level,
925 926
                                  gboolean            unref_self,
                                  gboolean            unref_parent,
927
                                  gboolean            unref_external)
928
{
929 930
  GSequenceIter *siter;
  GSequenceIter *end_siter;
931 932 933

  g_assert (filter_level);

934 935 936 937
  end_siter = g_sequence_get_end_iter (filter_level->seq);
  for (siter = g_sequence_get_begin_iter (filter_level->seq);
       siter != end_siter;
       siter = g_sequence_iter_next (siter))
938
    {
939 940 941
      FilterElt *elt = g_sequence_get (siter);

      if (elt->children)
942 943 944 945 946 947 948 949 950 951 952
        {
          /* If we recurse and unref_self == FALSE, then unref_parent
           * must also be FALSE (otherwise a still unref a node in this
           * level).
           */
          gtk_tree_model_filter_free_level (filter,
                                            FILTER_LEVEL (elt->children),
                                            unref_self,
                                            unref_self == FALSE ? FALSE : unref_parent,
                                            unref_external);
        }
953 954 955 956 957 958 959 960 961 962 963 964

      if (unref_external)
        {
          GtkTreeIter f_iter;

          f_iter.stamp = filter->priv->stamp;
          f_iter.user_data = filter_level;
          f_iter.user_data2 = elt;

          while (elt->ext_ref_count > 0)
            gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
                                                   &f_iter,
965
                                                   TRUE, unref_self);
966
        }
967
    }
968

969 970
  /* Release the reference on the first item.
   */
971
  if (unref_self)
972 973
    {
      GtkTreeIter f_iter;
974

975 976
      f_iter.stamp = filter->priv->stamp;
      f_iter.user_data = filter_level;
977
      f_iter.user_data2 = g_sequence_get (g_sequence_get_begin_iter (filter_level->seq));
978

979 980
      gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
                                             &f_iter, FALSE, TRUE);
981 982
    }

983
  if (filter_level->ext_ref_count == 0)
984 985
    {
      FilterLevel *parent_level = filter_level->parent_level;
986
      FilterElt *parent_elt = filter_level->parent_elt;
987

988
      while (parent_level)
989
        {
990
          parent_elt->zero_ref_count--;
991

992 993
          parent_elt = parent_level->parent_elt;
          parent_level = parent_level->parent_level;
994
        }
995

996 997
      if (filter_level != filter->priv->root)
        filter->priv->zero_ref_count--;
998 999
    }

1000 1001 1002 1003 1004
#ifdef MODEL_FILTER_DEBUG
  if (filter_level == filter->priv->root)
    g_assert (filter->priv->zero_ref_count == 0);
#endif

1005
  if (filter_level->parent_elt)
1006 1007
    {
      /* Release reference on parent */
1008
      GtkTreeIter parent_iter;
1009

1010 1011 1012
      parent_iter.stamp = filter->priv->stamp;
      parent_iter.user_data = filter_level->parent_level;
      parent_iter.user_data2 = filter_level->parent_elt;
1013

1014 1015
      gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
                                             &parent_iter, FALSE, unref_parent);
1016

1017
      filter_level->parent_elt->children = NULL;
1018
    }
1019 1020 1021
  else
    filter->priv->root = NULL;

1022 1023
  g_sequence_free (filter_level->seq);
  g_sequence_free (filter_level->visible_seq);
1024 1025 1026
  g_free (filter_level);
}

1027 1028 1029 1030
/* prune_level() is like free_level(), however instead of being fully
 * freed, the level is pruned to a level with only the first node used
 * for monitoring.  For now it is only being called from
 * gtk_tree_model_filter_remove_elt_from_level(), which is the reason
1031
 * this function is lacking a “gboolean unref” argument.
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
 */
static void
gtk_tree_model_filter_prune_level (GtkTreeModelFilter *filter,
                                   FilterLevel        *level)
{
  GSequenceIter *siter;
  GSequenceIter *end_siter;
  FilterElt *elt;
  GtkTreeIter f_iter;

  /* This function is called when the parent of level became invisible.
   * All external ref counts of the children need to be dropped.
   * All children except the first one can be removed.
   */

  /* Any child levels can be freed */
  end_siter = g_sequence_get_end_iter (level->seq);
  for (siter = g_sequence_get_begin_iter (level->seq);
       siter != end_siter;
       siter = g_sequence_iter_next (siter))
    {
      FilterElt *elt = g_sequence_get (siter);

      if (elt->children)
        gtk_tree_model_filter_free_level (filter,
1057 1058
                                          FILTER_LEVEL (elt->children),
                                          TRUE, TRUE, TRUE);
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
    }

  /* For the first item, only drop the external references */
  elt = g_sequence_get (g_sequence_get_begin_iter (level->seq));

  f_iter.stamp = filter->priv->stamp;
  f_iter.user_data = level;
  f_iter.user_data2 = elt;

  while (elt->ext_ref_count > 0)
    gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
                                           &f_iter, TRUE, TRUE);

  if (elt->visible_siter)
    {
      g_sequence_remove (elt->visible_siter);
      elt->visible_siter = NULL;
    }

  /* Remove the other elts */
  end_siter = g_sequence_get_end_iter (level->seq);
  siter = g_sequence_get_begin_iter (level->seq);
  siter = g_sequence_iter_next (siter);
  for (; siter != end_siter; siter = g_sequence_iter_next (siter))
    {
      elt = g_sequence_get (siter);

      f_iter.stamp = filter->priv->stamp;
      f_iter.user_data = level;
      f_iter.user_data2 = elt;

      while (elt->ext_ref_count > 0)
        gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
                                               &f_iter, TRUE, TRUE);
1093 1094 1095 1096 1097 1098
      /* In this case, we do remove reference counts we've added ourselves,
       * since the node will be removed from the data structures.
       */
      while (elt->ref_count > 0)
        gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
                                               &f_iter, FALSE, TRUE);
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120

      if (elt->visible_siter)
        {
          g_sequence_remove (elt->visible_siter);
          elt->visible_siter = NULL;
        }
    }

  /* Remove [begin + 1, end] */
  siter = g_sequence_get_begin_iter (level->seq);
  siter = g_sequence_iter_next (siter);

  g_sequence_remove_range (siter, end_siter);

  /* The level must have reached an ext ref count of zero by now, though
   * we only assert on this in debugging mode.
   */
#ifdef MODEL_FILTER_DEBUG
  g_assert (level->ext_ref_count == 0);
#endif
}

1121 1122 1123
static void
gtk_tree_model_filter_level_transfer_first_ref (GtkTreeModelFilter *filter,
                                                FilterLevel        *level,
1124 1125
                                                GSequenceIter      *from_iter,
                                                GSequenceIter      *to_iter)
1126 1127 1128 1129 1130
{
  GtkTreeIter f_iter;

  f_iter.stamp = filter->priv->stamp;
  f_iter.user_data = level;
1131
  f_iter.user_data2 = g_sequence_get (to_iter);
1132 1133 1134 1135 1136 1137

  gtk_tree_model_filter_real_ref_node (GTK_TREE_MODEL (filter),
                                       &f_iter, FALSE);

  f_iter.stamp = filter->priv->stamp;
  f_iter.user_data = level;
1138
  f_iter.user_data2 = g_sequence_get (from_iter);
1139 1140 1141 1142 1143

  gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
                                         &f_iter, FALSE, TRUE);
}

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
static void
gtk_tree_model_filter_level_transfer_first_ref_with_index (GtkTreeModelFilter *filter,
                                                           FilterLevel        *level,
                                                           gint                from_index,
                                                           gint                to_index)
{
  gtk_tree_model_filter_level_transfer_first_ref (filter, level,
                                                  g_sequence_get_iter_at_pos (level->seq, from_index),
                                                  g_sequence_get_iter_at_pos (level->seq, to_index));
}
1154

1155
/* Creates paths suitable for accessing the child model. */
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
static GtkTreePath *
gtk_tree_model_filter_elt_get_path (FilterLevel *level,
                                    FilterElt   *elt,
                                    GtkTreePath *root)
{
  FilterLevel *walker = level;
  FilterElt *walker2 = elt;
  GtkTreePath *path;
  GtkTreePath *real_path;

  g_return_val_if_fail (level != NULL, NULL);
  g_return_val_if_fail (elt != NULL, NULL);

  path = gtk_tree_path_new ();

  while (walker)
    {
      gtk_tree_path_prepend_index (path, walker2->offset);

1175
      walker2 = walker->parent_elt;
1176 1177 1178 1179 1180
      walker = walker->parent_level;
    }

  if (root)
    {
1181
      real_path = gtk_tree_model_filter_add_root (path, root);
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
      gtk_tree_path_free (path);
      return real_path;
    }

  return path;
}

static GtkTreePath *
gtk_tree_model_filter_add_root (GtkTreePath *src,
                                GtkTreePath *root)
{
  GtkTreePath *retval;
  gint i;

  retval = gtk_tree_path_copy (root);

  for (i = 0; i < gtk_tree_path_get_depth (src); i++)
1199
    gtk_tree_path_append_index (retval, gtk_tree_path_get_indices (src)[i]);
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

  return retval;
}

static GtkTreePath *
gtk_tree_model_filter_remove_root (GtkTreePath *src,
                                   GtkTreePath *root)
{
  GtkTreePath *retval;
  gint i;
  gint depth;
  gint *indices;

  if (gtk_tree_path_get_depth (src) <= gtk_tree_path_get_depth (root))
    return NULL;

  depth = gtk_tree_path_get_depth (src);
1217
  indices = gtk_tree_path_get_indices (src);
1218 1219

  for (i = 0; i < gtk_tree_path_get_depth (root); i++)
1220
    if (indices[i] != gtk_tree_path_get_indices (root)[i])
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
      return NULL;

  retval = gtk_tree_path_new ();

  for (; i < depth; i++)
    gtk_tree_path_append_index (retval, indices[i]);

  return retval;
}

static void
gtk_tree_model_filter_increment_stamp (GtkTreeModelFilter *filter)
{
  do
    {
      filter->priv->stamp++;
    }
  while (filter->priv->stamp == 0);

  gtk_tree_model_filter_clear_cache (filter);
}

static gboolean
1244 1245 1246
gtk_tree_model_filter_real_visible (GtkTreeModelFilter *filter,
                                    GtkTreeModel       *child_model,
                                    GtkTreeIter        *child_iter)
1247 1248 1249
{
  if (filter->priv->visible_func)
    {
1250
      return filter->priv->visible_func (child_model,
Matthias Clasen's avatar
Matthias Clasen committed
1251 1252
					 child_iter,
					 filter->priv->visible_data)
Matthias Clasen's avatar
Matthias Clasen committed
1253
	? TRUE : FALSE;
1254 1255 1256
    }
  else if (filter->priv->visible_column >= 0)
   {
Javier Jardón's avatar
Javier Jardón committed
1257
     GValue val = G_VALUE_INIT;
1258

1259
     gtk_tree_model_get_value (child_model, child_iter,
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
                               filter->priv->visible_column, &val);

     if (g_value_get_boolean (&val))
       {
         g_value_unset (&val);
         return TRUE;
       }

     g_value_unset (&val);
     return FALSE;
   }

1272
  /* no visible function set, so always visible */
1273 1274 1275
  return TRUE;
}

1276 1277 1278 1279 1280 1281 1282 1283
static gboolean
gtk_tree_model_filter_visible (GtkTreeModelFilter *self,
                               GtkTreeIter        *child_iter)
{
  return GTK_TREE_MODEL_FILTER_GET_CLASS (self)->visible (self,
      self->priv->child_model, child_iter);
}

1284 1285 1286 1287 1288 1289 1290
static void
gtk_tree_model_filter_clear_cache_helper_iter (gpointer data,
                                               gpointer user_data)
{
  GtkTreeModelFilter *filter = user_data;
  FilterElt *elt = data;

1291 1292 1293 1294
#ifdef MODEL_FILTER_DEBUG
  g_assert (elt->zero_ref_count >= 0);
#endif

1295 1296 1297 1298
  if (elt->zero_ref_count > 0)
    gtk_tree_model_filter_clear_cache_helper (filter, elt->children);
}

1299 1300 1301 1302 1303 1304
static void
gtk_tree_model_filter_clear_cache_helper (GtkTreeModelFilter *filter,
                                          FilterLevel        *level)
{
  g_assert (level);

1305
  g_sequence_foreach (level->seq, gtk_tree_model_filter_clear_cache_helper_iter, filter);
1306

1307 1308 1309
  /* If the level's ext_ref_count is zero, it means the level is not visible
   * and can be removed.  But, since we support monitoring a child level
   * of a parent for changes (these might affect the parent), we will only
1310
   * free the level if the parent level also has an external ref
1311 1312
   * count of zero.  In that case, changes concerning our parent are
   * not requested.
1313 1314 1315
   *
   * The root level is always visible, so an exception holds for levels
   * with the root level as parent level: these have to remain cached.
1316
   */
1317
  if (level->ext_ref_count == 0 && level != filter->priv->root &&
1318 1319
      level->parent_level && level->parent_level != filter->priv->root &&
      level->parent_level->ext_ref_count == 0)
1320
    {
1321
      gtk_tree_model_filter_free_level (filter, level, TRUE, TRUE, FALSE);
1322 1323 1324 1325
      return;
    }
}

1326 1327 1328 1329
static gboolean
gtk_tree_model_filter_elt_is_visible_in_target (FilterLevel *level,
                                                FilterElt   *elt)
{
1330
  if (!elt->visible_siter)
1331 1332
    return FALSE;

1333
  if (!level->parent_elt)
1334 1335 1336 1337
    return TRUE;

  do
    {
1338
      elt = level->parent_elt;
1339 1340
      level = level->parent_level;

1341
      if (elt && !elt->visible_siter)
1342 1343 1344 1345 1346 1347 1348
        return FALSE;
    }
  while (level);

  return TRUE;
}

1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
/* If a change has occurred in path (inserted, changed or deleted),
 * then this function is used to check all its ancestors.  An ancestor
 * could have changed state as a result and this needs to be propagated
 * to the objects monitoring the filter model.
 */
static void
gtk_tree_model_filter_check_ancestors (GtkTreeModelFilter *filter,
                                       GtkTreePath        *path)
{
  int i = 0;
  int *indices = gtk_tree_path_get_indices (path);
  FilterElt *elt;
  FilterLevel *level;
  GtkTreeIter c_iter, tmp_iter;

  level = FILTER_LEVEL (filter->priv->root);

  if (!level)
    return;

  if (filter->priv->virtual_root)
    gtk_tree_model_get_iter (filter->priv->child_model, &c_iter,
                             filter->priv->virtual_root);

  tmp_iter = c_iter;
  gtk_tree_model_iter_nth_child (filter->priv->child_model, &c_iter,
                                 filter->priv->virtual_root ? &tmp_iter : NULL,
                                 indices[i]);

  while (i < gtk_tree_path_get_depth (path) - 1)
    {
      gboolean requested_state;

1382 1383
      elt = lookup_elt_with_offset (level->seq,
                                    gtk_tree_path_get_indices (path)[i], NULL);
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405

      requested_state = gtk_tree_model_filter_visible (filter, &c_iter);

      if (!elt)
        {
          int index;
          GtkTreePath *c_path;

          if (requested_state == FALSE)
            return;

          /* The elt does not exist in this level (so it is not
           * visible), but should now be visible.  We emit the
           * row-inserted and row-has-child-toggled signals.
           */
          elt = gtk_tree_model_filter_insert_elt_in_level (filter,
                                                           &c_iter,
                                                           level,
                                                           indices[i],
                                                           &index);

          /* insert_elt_in_level defaults to FALSE */
1406 1407 1408
          elt->visible_siter = g_sequence_insert_sorted (level->visible_seq,
                                                         elt,
                                                         filter_elt_cmp, NULL);
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425

          c_path = gtk_tree_model_get_path (filter->priv->child_model,
                                            &c_iter);

          gtk_tree_model_filter_emit_row_inserted_for_path (filter,
                                                            filter->priv->child_model,
                                                            c_path,
                                                            &c_iter);

          gtk_tree_path_free (c_path);

          /* We can immediately return, because this node was not visible
           * before and its children will be checked for in response to
           * the emitted row-has-child-toggled signal.
           */
          return;
        }
1426
      else if (elt->visible_siter)
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
        {
          if (!requested_state)
            {
              /* A node has turned invisible.  Remove it from the level
               * and emit row-deleted.  Since this node is being
               * deleted. it makes no sense to look further up the
               * chain.
               */
              gtk_tree_model_filter_remove_elt_from_level (filter,
                                                           level, elt);
              return;
            }

          /* Otherwise continue up the chain */
        }
1442
      else if (!elt->visible_siter)
1443 1444 1445 1446 1447 1448 1449 1450 1451
        {
          if (requested_state)
            {
              /* A node is already in the cache, but invisible.  This
               * is usually a node on which a reference is kept by
               * the filter model, or a node fetched on the filter's
               * request, and thus not shown.  Therefore, we will
               * not emit row-inserted for this node.  Instead,
               * we signal to its parent that a change has occurred.
1452 1453 1454
               *
               * Exception: root level, in this case, we must emit
               * row-inserted.
1455
               */
1456 1457 1458 1459 1460
              if (level->parent_level)
                {
                  GtkTreeIter f_iter;
                  GtkTreePath *f_path;

1461 1462
                  elt->visible_siter = g_sequence_insert_sorted (level->visible_seq, elt,
                                                                 filter_elt_cmp, NULL);
1463 1464 1465

                  f_iter.stamp = filter->priv->stamp;
                  f_iter.user_data = level->parent_level;
1466
                  f_iter.user_data2 = level->parent_elt;
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477

                  f_path = gtk_tree_model_get_path (GTK_TREE_MODEL (filter),
                                                    &f_iter);
                  gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (filter),
                                                        f_path, &f_iter);
                  gtk_tree_path_free (f_path);
                }
              else
                {
                  GtkTreePath *c_path;

1478 1479
                  elt->visible_siter = g_sequence_insert_sorted (level->visible_seq, elt,
                                                                 filter_elt_cmp, NULL);
1480 1481 1482

                  c_path = gtk_tree_model_get_path (filter->priv->child_model,
                                                    &c_iter);
1483

1484 1485 1486 1487
                  gtk_tree_model_filter_emit_row_inserted_for_path (filter,
                                                                    filter->priv->child_model,
                                                                    c_path,
                                                                    &c_iter);
1488

1489 1490
                  gtk_tree_path_free (c_path);
                }
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521

              /* We can immediately return, because this node was not visible
               * before and the parent will check its children, including
               * this node, in response to the emitted row-has-child-toggled
               * signal.
               */
              return;
            }

          /* Not visible, so no need to continue. */
          return;
        }

      if (!elt->children)
        {
          /* If an elt does not have children, these are not visible.
           * Therefore, any signals emitted for these children will
           * be ignored, so we do not have to emit them.
           */
          return;
        }

      level = elt->children;
      i++;

      tmp_iter = c_iter;
      gtk_tree_model_iter_nth_child (filter->priv->child_model, &c_iter,
                                     &tmp_iter, indices[i]);
    }
}

1522 1523 1524 1525 1526 1527 1528
static FilterElt *
gtk_tree_model_filter_insert_elt_in_level (GtkTreeModelFilter *filter,
                                           GtkTreeIter        *c_iter,
                                           FilterLevel        *level,
                                           gint                offset,
                                           gint               *index)
{
1529 1530
  FilterElt *elt;
  GSequenceIter *siter;
1531

1532
  elt = filter_elt_new ();
1533

1534 1535
  if (GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS (filter))
    elt->iter = *c_iter;
1536

1537 1538 1539 1540 1541
  elt->offset = offset;
  elt->zero_ref_count = 0;
  elt->ref_count = 0;
  elt->ext_ref_count = 0;
  elt->children = NULL;
1542

1543 1544 1545 1546
  /* Because we don't emit row_inserted, the node is invisible and thus
   * not inserted in visible_seq
   */
  elt->visible_siter = NULL;
1547

1548 1549
  siter = g_sequence_insert_sorted (level->seq, elt, filter_elt_cmp, NULL);
  *index = g_sequence_iter_get_position (siter);
1550

1551 1552 1553 1554
  /* If the insert location is zero, we need to move our reference
   * on the old first node to the new first node.
   */
  if (*index == 0)
1555 1556
    gtk_tree_model_filter_level_transfer_first_ref_with_index (filter, level,
                                                               1, 0);
1557

1558
  return elt;
1559 1560
}

1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
static FilterElt *
gtk_tree_model_filter_fetch_child (GtkTreeModelFilter *filter,
                                   FilterLevel        *level,
                                   gint                offset,
                                   gint               *index)
{
  gint len;
  GtkTreePath *c_path = NULL;
  GtkTreeIter c_iter;
  GtkTreePath *c_parent_path = NULL;
  GtkTreeIter c_parent_iter;

  /* check if child exists and is visible */
1574
  if (level->parent_elt)
1575 1576 1577
    {
      c_parent_path =
        gtk_tree_model_filter_elt_get_path (level->parent_level,
1578
                                            level->parent_elt,
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
                                            filter->priv->virtual_root);
      if (!c_parent_path)
        return NULL;
    }
  else
    {
      if (filter->priv->virtual_root)
        c_parent_path = gtk_tree_path_copy (filter->priv->virtual_root);
      else
        c_parent_path = NULL;
    }

  if (c_parent_path)
    {
      gtk_tree_model_get_iter (filter->priv->child_model,
                               &c_parent_iter,
                               c_parent_path);
      len = gtk_tree_model_iter_n_children (filter->priv->child_model,
                                            &c_parent_iter);

      c_path = gtk_tree_path_copy (c_parent_path);
      gtk_tree_path_free (c_parent_path);
    }
  else
    {
      len = gtk_tree_model_iter_n_children (filter->priv->child_model, NULL);
      c_path = gtk_tree_path_new ();
    }

  gtk_tree_path_append_index (c_path, offset);
  gtk_tree_model_get_iter (filter->priv->child_model, &c_iter, c_path);
  gtk_tree_path_free (c_path);

  if (offset >= len || !gtk_tree_model_filter_visible (filter, &c_iter))
    return NULL;

1615 1616 1617
  return gtk_tree_model_filter_insert_elt_in_level (filter, &c_iter,
                                                    level, offset,
                                                    index);
1618 1619
}

1620 1621 1622 1623 1624 1625
/* Note that this function is never called from the row-deleted handler.
 * This means that this function is only used for removing elements
 * which are still present in the child model.  As a result, we must
 * take care to properly release the references the filter model has
 * on the child model nodes.
 */
1626
static void
1627 1628 1629
gtk_tree_model_filter_remove_elt_from_level (GtkTreeModelFilter *filter,
                                             FilterLevel        *level,
                                             FilterElt          *elt)
1630
{
1631 1632
  FilterElt *parent;
  FilterLevel *parent_level;
1633
  gint length, orig_level_ext_ref_count;
1634
  GtkTreeIter iter;
1635
  GtkTreePath *path = NULL;
1636

1637
  gboolean emit_child_toggled = FALSE;
1638

1639 1640 1641 1642 1643