gtktreemodelfilter.c 146 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 16
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
17 18 19 20
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

/**
 * 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:
 * <itemizedlist>
 * <listitem><para>
 * Filter specific rows, based on data from a "visible column", a column
 * storing booleans indicating whether the row should be filtered or not,
 * or based on the return value of a "visible function", which gets a
 * model, iter and user_data and returns a boolean indicating whether the
 * row should be filtered or not.
 * </para></listitem>
 * <listitem><para>
 * Modify the "appearance" of the model, using a modify function.
 * 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.
 * </para></listitem>
 * <listitem><para>
 * Set a different root node, also known as a "virtual root". You can pass in
 * a #GtkTreePath indicating the root node for the filter at construction time.
 * </para></listitem>
 * </itemizedlist>
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
 * 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.
 * 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.
 *
 * This introduces a dependency from the node on its child nodes. In order
 * to accommodate this, #GtkTreeModelFilter must make sure the necesary
 * 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
 * in a child's child level (2 levels deeper) are visible, you are on your
 * 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.
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
/* 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
 * we explicitly support reliance on the state of a node's children as
 * 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:
135
 *
136 137 138
 *     iter->stamp = filter->priv->stamp
 *     iter->user_data = FilterLevel
 *     iter->user_data2 = FilterElt
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
 * Internal data structure
 * -----------------------
 *
 * Using FilterLevel and FilterElt, GtkTreeModelFilter maintains a "cache"
 * 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
 * GtkTreeModelFilter. The visibility state is stored in the "visible_siter"
 * 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
 * FilterElt can have a "children" pointer set, which points at a child
 * 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
 *       contains an "offset" field which is the offset of the
 *       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
 * sequences) and paths which don't (generated from the visible sequences).
 * 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
 * filter model) is maintained internally in the "ref_count" fields in
 * FilterElt and FilterLevel. Because there is a need to determine whether
 * a node should be visible for the client, the reference count of only
 * the forwarded references is maintained as well, in the "ext_ref_count"
 * 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
 * the state of a node's children (see the public API documentation for
 * 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
 * has a "zero ref count" on all its parents. As soon as the level reaches
 * 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
228 229 230 231
 * 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
232 233 234 235 236 237 238 239 240
 * 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).
241 242
 */

243

244 245 246 247 248 249 250 251 252
typedef struct _FilterElt FilterElt;
typedef struct _FilterLevel FilterLevel;

struct _FilterElt
{
  GtkTreeIter iter;
  FilterLevel *children;
  gint offset;
  gint ref_count;
253
  gint ext_ref_count;
254
  gint zero_ref_count;
255
  GSequenceIter *visible_siter; /* iter into visible_seq */
256 257 258 259
};

struct _FilterLevel
{
260 261
  GSequence *seq;
  GSequence *visible_seq;
262
  gint ref_count;
263
  gint ext_ref_count;
264

265
  FilterElt *parent_elt;
266 267 268
  FilterLevel *parent_level;
};

269

270 271
struct _GtkTreeModelFilterPrivate
{
272
  GtkTreeModel *child_model;
273
  gpointer root;
274 275
  GtkTreePath *virtual_root;

276 277 278
  gint stamp;
  guint child_flags;
  gint zero_ref_count;
279
  gint visible_column;
280 281 282

  GtkTreeModelFilterVisibleFunc visible_func;
  gpointer visible_data;
283
  GDestroyNotify visible_destroy;
284 285 286 287

  GType *modify_types;
  GtkTreeModelFilterModifyFunc modify_func;
  gpointer modify_data;
288
  GDestroyNotify modify_destroy;
289
  gint modify_n_columns;
290

291 292
  guint visible_method_set   : 1;
  guint modify_func_set      : 1;
293

294 295
  guint in_row_deleted       : 1;
  guint virtual_root_deleted : 1;
296

297
  /* signal ids */
298 299 300 301 302
  gulong changed_id;
  gulong inserted_id;
  gulong has_child_toggled_id;
  gulong deleted_id;
  gulong reordered_id;
303 304 305 306 307 308 309 310 311 312
};

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

313 314 315 316 317 318 319
/* 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) \
320
        (((GtkTreeModelFilter *)filter)->priv->child_flags & GTK_TREE_MODEL_ITERS_PERSIST)
321 322 323
#else
#  define GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS(filter) (FALSE)
#endif
324

325 326 327 328 329
/* Defining this constant enables more assertions, which will be
 * helpful when debugging the code.
 */
#undef MODEL_FILTER_DEBUG

330 331
#define FILTER_ELT(filter_elt) ((FilterElt *)filter_elt)
#define FILTER_LEVEL(filter_level) ((FilterLevel *)filter_level)
332
#define GET_ELT(siter) ((FilterElt*) (siter ? g_sequence_get (siter) : NULL))
333 334 335

/* general code (object/interface init, properties, etc) */
static void         gtk_tree_model_filter_tree_model_init                 (GtkTreeModelIface       *iface);
336
static void         gtk_tree_model_filter_drag_source_init                (GtkTreeDragSourceIface  *iface);
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
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 */
370
static GtkTreeModelFlags gtk_tree_model_filter_get_flags                       (GtkTreeModel           *model);
371 372 373
static gint         gtk_tree_model_filter_get_n_columns                   (GtkTreeModel           *model);
static GType        gtk_tree_model_filter_get_column_type                 (GtkTreeModel           *model,
                                                                           gint                    index);
374 375 376
static gboolean     gtk_tree_model_filter_get_iter_full                   (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
                                                                           GtkTreePath            *path);
377 378 379 380 381 382 383 384 385 386 387
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);
388 389
static gboolean     gtk_tree_model_filter_iter_previous                   (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter);
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
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);

409 410 411 412 413 414 415 416
/* 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);
417 418 419 420

/* private functions */
static void        gtk_tree_model_filter_build_level                      (GtkTreeModelFilter     *filter,
                                                                           FilterLevel            *parent_level,
421
                                                                           FilterElt              *parent_elt,
422 423
                                                                           gboolean                emit_inserted);

424
static void        gtk_tree_model_filter_free_level                       (GtkTreeModelFilter     *filter,
425
                                                                           FilterLevel            *filter_level,
426 427
                                                                           gboolean                unref,
                                                                           gboolean                unref_external);
428 429 430 431 432 433 434 435 436 437 438 439

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);

440 441 442 443 444 445 446 447
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);
448 449 450 451 452
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);

453 454 455
static void         gtk_tree_model_filter_real_ref_node                   (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
                                                                           gboolean                external);
456 457
static void         gtk_tree_model_filter_real_unref_node                 (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
458
                                                                           gboolean                external,
459 460 461 462
                                                                           gboolean                propagate_unref);

static void         gtk_tree_model_filter_set_model                       (GtkTreeModelFilter     *filter,
                                                                           GtkTreeModel           *child_model);
463 464 465
static void         gtk_tree_model_filter_ref_path                        (GtkTreeModelFilter     *filter,
                                                                           GtkTreePath            *path);
static void         gtk_tree_model_filter_unref_path                      (GtkTreeModelFilter     *filter,
466 467
                                                                           GtkTreePath            *path,
                                                                           int                     depth);
468 469 470 471 472 473
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
474
                                                                           gboolean                fetch_children);
475

476 477
static gboolean    gtk_tree_model_filter_elt_is_visible_in_target         (FilterLevel            *level,
                                                                           FilterElt              *elt);
478

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


Matthias Clasen's avatar
Matthias Clasen committed
500 501 502 503
G_DEFINE_TYPE_WITH_CODE (GtkTreeModelFilter, gtk_tree_model_filter, G_TYPE_OBJECT,
			 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
504
						gtk_tree_model_filter_drag_source_init))
505 506 507 508

static void
gtk_tree_model_filter_init (GtkTreeModelFilter *filter)
{
509 510 511
  filter->priv = G_TYPE_INSTANCE_GET_PRIVATE (filter,
                                              GTK_TYPE_TREE_MODEL_FILTER,
                                              GtkTreeModelFilterPrivate);
512 513 514 515 516

  filter->priv->visible_column = -1;
  filter->priv->zero_ref_count = 0;
  filter->priv->visible_method_set = FALSE;
  filter->priv->modify_func_set = FALSE;
517
  filter->priv->in_row_deleted = FALSE;
518
  filter->priv->virtual_root_deleted = FALSE;
519 520 521 522 523 524 525 526 527 528 529 530 531 532
}

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;

533 534 535
  filter_class->visible = gtk_tree_model_filter_real_visible;
  filter_class->modify  = gtk_tree_model_filter_real_modify;

536 537 538 539 540
  /* 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,
541
                                   g_param_spec_object ("child-model",
542 543 544
                                                        ("The child model"),
                                                        ("The model for the filtermodel to filter"),
                                                        GTK_TYPE_TREE_MODEL,
545
                                                        GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
546 547 548

  g_object_class_install_property (object_class,
                                   PROP_VIRTUAL_ROOT,
549
                                   g_param_spec_boxed ("virtual-root",
550 551 552
                                                       ("The virtual root"),
                                                       ("The virtual root (relative to the child model) for this filtermodel"),
                                                       GTK_TYPE_TREE_PATH,
553
                                                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
554 555 556 557 558 559 560 561 562 563 564 565 566 567

  g_type_class_add_private (object_class, sizeof (GtkTreeModelFilterPrivate));
}

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;
568
  iface->iter_previous = gtk_tree_model_filter_iter_previous;
569 570 571 572 573 574 575 576 577
  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;
}

578 579 580 581 582 583 584 585
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;
}

586 587 588 589 590 591

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

592 593
  if (filter->priv->virtual_root && !filter->priv->virtual_root_deleted)
    {
594 595
      gtk_tree_model_filter_unref_path (filter, filter->priv->virtual_root,
                                        -1);
596 597
      filter->priv->virtual_root_deleted = TRUE;
    }
598

599 600 601 602 603 604
  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)
605
    gtk_tree_model_filter_free_level (filter, filter->priv->root, TRUE, FALSE);
606

607
  g_free (filter->priv->modify_types);
608 609 610 611 612 613
  
  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);
614 615

  /* must chain up */
Matthias Clasen's avatar
Matthias Clasen committed
616
  G_OBJECT_CLASS (gtk_tree_model_filter_parent_class)->finalize (object);
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 656 657 658 659 660 661 662 663 664
}

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 */

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 723 724 725 726 727 728 729 730 731
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--;
}

732 733 734
static void
gtk_tree_model_filter_build_level (GtkTreeModelFilter *filter,
                                   FilterLevel        *parent_level,
735
                                   FilterElt          *parent_elt,
736
                                   gboolean            emit_inserted)
737 738
{
  GtkTreeIter iter;
739
  GtkTreeIter first_node;
740 741
  GtkTreeIter root;
  FilterLevel *new_level;
742
  FilterLevel *tmp_level;
743
  FilterElt *tmp_elt;
744
  GtkTreeIter f_iter;
745 746
  gint length = 0;
  gint i;
747
  gboolean empty = TRUE;
748 749 750

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

751 752
  /* Avoid building a level that already exists */
  if (parent_level)
753
    g_assert (parent_elt->children == NULL);
754 755 756
  else
    g_assert (filter->priv->root == NULL);

757 758 759
  if (filter->priv->in_row_deleted)
    return;

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 796 797
  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);
798 799 800 801

      /* Take a reference on the parent */
      gtk_tree_model_filter_real_ref_node (GTK_TREE_MODEL (filter),
                                           &parent_iter, FALSE);
802 803 804 805 806
    }

  g_return_if_fail (length > 0);

  new_level = g_new (FilterLevel, 1);
807 808
  new_level->seq = g_sequence_new (filter_elt_free);
  new_level->visible_seq = g_sequence_new (NULL);
809
  new_level->ref_count = 0;
810
  new_level->ext_ref_count = 0;
811
  new_level->parent_elt = parent_elt;
812 813
  new_level->parent_level = parent_level;

814
  if (parent_elt)
815 816 817 818 819
    parent_elt->children = new_level;
  else
    filter->priv->root = new_level;

  /* increase the count of zero ref_counts */
820
  tmp_level = parent_level;
821
  tmp_elt = parent_elt;
822 823

  while (tmp_level)
824
    {
825
      tmp_elt->zero_ref_count++;
826

827
      tmp_elt = tmp_level->parent_elt;
828
      tmp_level = tmp_level->parent_level;
829
    }
830 831
  if (new_level != filter->priv->root)
    filter->priv->zero_ref_count++;
832 833 834

  i = 0;

835
  first_node = iter;
836 837 838 839 840

  do
    {
      if (gtk_tree_model_filter_visible (filter, &iter))
        {
841
          FilterElt *filter_elt;
842

843 844 845 846 847 848 849
          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;
850 851

          if (GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS (filter))
852
            filter_elt->iter = iter;
853

854 855 856
          g_sequence_append (new_level->seq, filter_elt);
          filter_elt->visible_siter = g_sequence_append (new_level->visible_seq, filter_elt);
          empty = FALSE;
857

858 859 860
          if (emit_inserted)
            {
              GtkTreePath *f_path;
861
              GtkTreeIter f_iter;
862 863
              GtkTreeIter children;

864 865
              f_iter.stamp = filter->priv->stamp;
              f_iter.user_data = new_level;
866
              f_iter.user_data2 = filter_elt;
867

868 869 870 871 872 873 874 875 876 877 878
              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));
879
            }
880 881 882 883
        }
      i++;
    }
  while (gtk_tree_model_iter_next (filter->priv->child_model, &iter));
884

885 886 887
  /* 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,
888 889 890
   * 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.
891
   */
892
  if (empty &&
893
       (parent_level && parent_level->ext_ref_count == 0))
894
    {
895
      gtk_tree_model_filter_free_level (filter, new_level, FALSE, FALSE);
896 897 898 899 900 901
      return;
    }

  /* If none of the nodes are visible, we will just pull in the
   * first node of the level.
   */
902
  if (empty)
903
    {
904
      FilterElt *filter_elt;
905

906 907 908 909 910 911 912
      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;
913 914

      if (GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS (filter))
915
        filter_elt->iter = first_node;
916

917
      g_sequence_append (new_level->seq, filter_elt);
918
    }
919

920 921 922 923 924
  /* 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;
925
  f_iter.user_data2 = g_sequence_get (g_sequence_get_begin_iter (new_level->seq));
926

927 928
  gtk_tree_model_filter_real_ref_node (GTK_TREE_MODEL (filter), &f_iter,
                                       FALSE);
929 930 931 932
}

static void
gtk_tree_model_filter_free_level (GtkTreeModelFilter *filter,
933
                                  FilterLevel        *filter_level,
934 935
                                  gboolean            unref,
                                  gboolean            unref_external)
936
{
937 938
  GSequenceIter *siter;
  GSequenceIter *end_siter;
939 940 941

  g_assert (filter_level);

942 943 944 945
  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))
946
    {
947 948 949
      FilterElt *elt = g_sequence_get (siter);

      if (elt->children)
950
        gtk_tree_model_filter_free_level (filter,
951
                                          FILTER_LEVEL (elt->children),
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
                                          unref, unref_external);

      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,
                                                   TRUE, unref);
        }
967
    }
968

969 970 971 972 973
  /* Release the reference on the first item.
   */
  if (unref)
    {
      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
  if (filter_level->parent_elt)
1001 1002 1003 1004 1005 1006 1007 1008
    {
      /* Release reference on parent */
      if (unref)
        {
          GtkTreeIter parent_iter;

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

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

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

1020 1021
  g_sequence_free (filter_level->seq);
  g_sequence_free (filter_level->visible_seq);
1022 1023 1024
  g_free (filter_level);
}

1025 1026 1027 1028 1029 1030 1031 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 1057 1058 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 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
/* 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
 * this function is lacking a "gboolean unref" argument.
 */
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,
                                          FILTER_LEVEL (elt->children), TRUE,
                                          TRUE);
    }

  /* 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);

      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
}

1113 1114 1115
static void
gtk_tree_model_filter_level_transfer_first_ref (GtkTreeModelFilter *filter,
                                                FilterLevel        *level,
1116 1117
                                                GSequenceIter      *from_iter,
                                                GSequenceIter      *to_iter)
1118 1119 1120 1121 1122
{
  GtkTreeIter f_iter;

  f_iter.stamp = filter->priv->stamp;
  f_iter.user_data = level;
1123
  f_iter.user_data2 = g_sequence_get (to_iter);
1124 1125 1126 1127 1128 1129

  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;
1130
  f_iter.user_data2 = g_sequence_get (from_iter);
1131 1132 1133 1134 1135

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

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
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));
}
1146

1147
/* Creates paths suitable for accessing the child model. */
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
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);

1167
      walker2 = walker->parent_elt;
1168 1169 1170 1171 1172
      walker = walker->parent_level;
    }

  if (root)
    {
1173
      real_path = gtk_tree_model_filter_add_root (path, root);
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
      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++)
1191
    gtk_tree_path_append_index (retval, gtk_tree_path_get_indices (src)[i]);
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208

  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);
1209
  indices = gtk_tree_path_get_indices (src);
1210 1211

  for (i = 0; i < gtk_tree_path_get_depth (root); i++)
1212
    if (indices[i] != gtk_tree_path_get_indices (root)[i])
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
      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
1236 1237 1238
gtk_tree_model_filter_real_visible (GtkTreeModelFilter *filter,
                                    GtkTreeModel       *child_model,
                                    GtkTreeIter        *child_iter)
1239 1240 1241
{
  if (filter->priv->visible_func)
    {
1242
      return filter->priv->visible_func (child_model,
Matthias Clasen's avatar
Matthias Clasen committed
1243 1244
					 child_iter,
					 filter->priv->visible_data)
Matthias Clasen's avatar
Matthias Clasen committed
1245
	? TRUE : FALSE;
1246 1247 1248 1249 1250
    }
  else if (filter->priv->visible_column >= 0)
   {
     GValue val = {0, };

1251
     gtk_tree_model_get_value (child_model, child_iter,
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
                               filter->priv->visible_column, &val);

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

     g_value_unset (&val);
     return FALSE;
   }

1264
  /* no visible function set, so always visible */
1265 1266 1267
  return TRUE;
}

1268 1269 1270 1271 1272 1273 1274 1275
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);
}

1276 1277 1278 1279 1280 1281 1282
static void
gtk_tree_model_filter_clear_cache_helper_iter (gpointer data,
                                               gpointer user_data)
{
  GtkTreeModelFilter *filter = user_data;
  FilterElt *elt = data;

1283 1284 1285 1286
#ifdef MODEL_FILTER_DEBUG
  g_assert (elt->zero_ref_count >= 0);
#endif

1287 1288 1289 1290
  if (elt->zero_ref_count > 0)
    gtk_tree_model_filter_clear_cache_helper (filter, elt->children);
}

1291 1292 1293 1294 1295 1296
static void
gtk_tree_model_filter_clear_cache_helper (GtkTreeModelFilter *filter,
                                          FilterLevel        *level)
{
  g_assert (level);

1297
  g_sequence_foreach (level->seq, gtk_tree_model_filter_clear_cache_helper_iter, filter);
1298

1299 1300 1301
  /* 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
1302
   * free the level if the parent level also has an external ref
1303 1304 1305
   * count of zero.  In that case, changes concerning our parent are
   * not requested.
   */
1306
  if (level->ext_ref_count == 0 && level != filter->priv->root &&
1307
      level->parent_level && level->parent_level->ext_ref_count == 0)
1308
    {
1309
      gtk_tree_model_filter_free_level (filter, level, TRUE, FALSE);
1310 1311 1312 1313
      return;
    }
}

1314 1315 1316 1317
static gboolean
gtk_tree_model_filter_elt_is_visible_in_target (FilterLevel *level,
                                                FilterElt   *elt)
{
1318
  if (!elt->visible_siter)
1319 1320
    return FALSE;

1321
  if (!level->parent_elt)
1322 1323 1324 1325
    return TRUE;

  do
    {
1326
      elt = level->parent_elt;
1327 1328
      level = level->parent_level;

1329
      if (elt && !elt->visible_siter)
1330 1331 1332 1333 1334 1335 1336
        return FALSE;
    }
  while (level);

  return TRUE;
}

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
/* 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;

1370 1371
      elt = lookup_elt_with_offset (level->seq,
                                    gtk_tree_path_get_indices (path)[i], NULL);
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393

      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 */
1394 1395 1396
          elt->visible_siter = g_sequence_insert_sorted (level->visible_seq,
                                                         elt,
                                                         filter_elt_cmp, NULL);
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413

          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;
        }
1414
      else if (elt->visible_siter)
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
        {
          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 */
        }
1430
      else if (!elt->visible_siter)
1431 1432 1433 1434 1435 1436 1437 1438 1439
        {
          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.
1440 1441 1442
               *
               * Exception: root level, in this case, we must emit
               * row-inserted.
1443
               */
1444 1445 1446 1447 1448
              if (level->parent_level)
                {
                  GtkTreeIter f_iter;
                  GtkTreePath *f_path;

1449 1450
                  elt->visible_siter = g_sequence_insert_sorted (level->visible_seq, elt,
                                                                 filter_elt_cmp, NULL);
1451 1452 1453

                  f_iter.stamp = filter->priv->stamp;
                  f_iter.user_data = level->parent_level;
1454
                  f_iter.user_data2 = level->parent_elt;
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

                  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;

1466 1467
                  elt->visible_siter = g_sequence_insert_sorted (level->visible_seq, elt,
                                                                 filter_elt_cmp, NULL);
1468 1469 1470

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

1472 1473 1474 1475
                  gtk_tree_model_filter_emit_row_inserted_for_path (filter,
                                                                    filter->priv->child_model,
                                                                    c_path,
                                                                    &c_iter);
1476

1477 1478
                  gtk_tree_path_free (c_path);
                }
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509

              /* 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]);
    }
}

1510 1511 1512 1513 1514 1515 1516
static FilterElt *
gtk_tree_model_filter_insert_elt_in_level (GtkTreeModelFilter *filter,
                                           GtkTreeIter        *c_iter,
                                           FilterLevel        *level,
                                           gint                offset,
                                           gint               *index)
{
1517 1518
  FilterElt *elt;
  GSequenceIter *siter;
1519

1520
  elt = filter_elt_new ();
1521

1522 1523
  if (GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS (filter))
    elt->iter = *c_iter;
1524

1525 1526 1527 1528 1529
  elt->offset = offset;
  elt->zero_ref_count = 0;
  elt->ref_count = 0;
  elt->ext_ref_count = 0;
  elt->children = NULL;
1530

1531 1532 1533 1534
  /* Because we don't emit row_inserted, the node is invisible and thus
   * not inserted in visible_seq
   */
  elt->visible_siter = NULL;
1535

1536 1537
  siter = g_sequence_insert_sorted (level->seq, elt, filter_elt_cmp, NULL);
  *index = g_sequence_iter_get_position (siter);
1538

1539 1540 1541 1542
  /* 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)
1543 1544
    gtk_tree_model_filter_level_transfer_first_ref_with_index (filter, level,
                                                               1, 0);
1545

1546
  return elt;
1547 1548
}

1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
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 */
1562
  if (level->parent_elt)
1563 1564 1565
    {
      c_parent_path =
        gtk_tree_model_filter_elt_get_path (level->parent_level,
1566
                                            level->parent_elt,
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
                                            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;

1603 1604 1605
  return gtk_tree_model_filter_insert_elt_in_level (filter, &c_iter,
                                                    level, offset,
                                                    index);
1606 1607
}

1608 1609 1610 1611 1612 1613
/* 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.
 */
1614
static void
1615 1616 1617
gtk_tree_model_filter_remove_elt_from_level (GtkTreeModelFilter *filter,
                                             FilterLevel        *level,
                                             FilterElt          *elt)
1618
{
1619 1620
  FilterElt *parent;
  FilterLevel *parent_level;
1621
  gint length, orig_level_ext_ref_count;
1622
  GtkTreeIter iter;
1623
  GtkTreePath *path = NULL;
1624

1625
  gboolean emit_child_toggled = FALSE;
1626

1627 1628 1629 1630 1631
  /* We need to know about the level's ext ref count before removal
   * of this node.
   */
  orig_level_ext_ref_count = level->ext_ref_count;

1632 1633 1634
  iter.stamp = filter->priv->stamp;
  iter.user_data = level;
  iter.user_data2 = elt;
1635

1636 1637 1638 1639 1640 1641 1642 1643
  if (orig_level_ext_ref_count > 0)
    path = gtk_tree_model_get_path (GTK_TREE_MODEL (filter), &iter);
  else
    /* If the level is not visible, the parent is potentially invisible
     * too.  Either way, as no signal will be emitted, there is no use
     * for a path.
     */
    path = NULL;
1644

1645
  parent = level->parent_elt;
1646
  parent_level = level->parent_level;
1647

1648
  length = g_sequence_get_length (level->seq);
1649

1650
  /* first register the node to be invisible */
1651 1652
  g_sequence_remove (elt->visible_siter);
  elt->visible_siter = NULL;
1653

1654 1655 1656 1657
  /*
   * If level != root level and the number of visible nodes is 0 (ie. this
   * is the last node to be removed from the level), emit
   * row-has-child-toggled.
1658
   */
1659

1660
  if (level != filter->priv->root
1661
      && g_sequence_get_length (level->visible_seq) == 0
1662
      && parent
1663
      && parent->visible_siter)
1664
    emit_child_toggled = TRUE;
1665

1666 1667 1668 1669 1670 1671
  /* Distinguish:
   *   - length > 1: in this case, the node is removed from the level
   *                 and row-deleted is emitted.
   *   - length == 1: in this case, we need to decide whether to keep
   *                  the level or to free it.
   */
1672
  if (length > 1)
1673
    {
1674
      GSequenceIter *siter;
1675

1676 1677
      /* We emit row-deleted, and remove the node from the cache.
       * If it has any children, these will be removed here as well.
1678
       */
1679

1680 1681 1682 1683 1684
      /* FIXME: I am not 100% sure it is always save to fully free the
       * level here.  Perhaps the state of the parent level, etc. has to
       * be checked to make the right decision, like is done below for
       * the case length == 1.
       */
1685
      if (elt->children)
1686
        gtk_tree_model_filter_free_level (filter, elt->children, TRUE, TRUE);
1687

1688
      /* If the first node is being removed, transfer, the reference */
1689
      if (elt == g_sequence_get (g_sequence_get_begin_iter (level->seq)))
1690
        {
1691 1692
          gtk_tree_model_filter_level_transfer_first_ref_with_index (filter, level,
                                                                     0, 1);
1693 1694
        }

1695 1696
      while (elt->ext_ref_count > 0)
        gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
1697 1698 1699 1700
                                               &iter, TRUE, TRUE);
      /* In this case, we do remove reference counts we've added ourselves,
       * since the node will be removed from the data structures.
       */
1701
      while (elt->ref_count > 0)
1702
        gtk_tree_model_filte