gtktreemodelfilter.c 107 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 "gtkalias.h"
26
#include "gtkprivate.h"
27 28 29 30 31 32 33 34 35 36 37 38 39
#include <string.h>

/* ITER FORMAT:
 *
 * iter->stamp = filter->priv->stamp
 * iter->user_data = FilterLevel
 * iter->user_data2 = FilterElt
 */

/* all paths, iters, etc prefixed with c_ are paths, iters, etc relative to the
 * child model.
 */

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/* A few notes:
 *  There are three model/views involved, so there are two mappings:
 *    * this model -> child model: mapped via offset in FilterElt.
 *    * this model -> parent model (or view): mapped via the array index
 *                                            of FilterElt.
 *
 *  Note that there are two kinds of paths relative to the filter model
 *  (those generated from the array indices): paths taking non-visible
 *  nodes into account, and paths which don't.  Paths which take
 *  non-visible nodes into account should only be used internally and
 *  NEVER be passed along with a signal emission.
 *
 *  The filter model has a reference on every node that is not in the root
 *  level and has a parent with ref_count > 1.  Exception is a virtual root
 *  level; all nodes in the virtual root level are referenced too.
 */

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
typedef struct _FilterElt FilterElt;
typedef struct _FilterLevel FilterLevel;

struct _FilterElt
{
  GtkTreeIter iter;
  FilterLevel *children;
  gint offset;
  gint ref_count;
  gint zero_ref_count;
  gboolean visible;
};

struct _FilterLevel
{
  GArray *array;
  gint ref_count;
74
  gint visible_nodes;
75 76 77 78 79

  FilterElt *parent_elt;
  FilterLevel *parent_level;
};

80 81
#define GTK_TREE_MODEL_FILTER_GET_PRIVATE(obj)  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TREE_MODEL_FILTER, GtkTreeModelFilterPrivate))

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
struct _GtkTreeModelFilterPrivate
{
  gpointer root;
  gint stamp;
  guint child_flags;
  GtkTreeModel *child_model;
  gint zero_ref_count;

  GtkTreePath *virtual_root;

  GtkTreeModelFilterVisibleFunc visible_func;
  gpointer visible_data;
  GtkDestroyNotify visible_destroy;

  gint modify_n_columns;
  GType *modify_types;
  GtkTreeModelFilterModifyFunc modify_func;
  gpointer modify_data;
  gpointer modify_destroy;

  gint visible_column;

  gboolean visible_method_set;
  gboolean modify_func_set;

107 108
  gboolean in_row_deleted;

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
  /* signal ids */
  guint changed_id;
  guint inserted_id;
  guint has_child_toggled_id;
  guint deleted_id;
  guint reordered_id;
};

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

#define GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS(filter) \
        (((GtkTreeModelFilter *)filter)->priv->child_flags & GTK_TREE_MODEL_ITERS_PERSIST)

#define FILTER_ELT(filter_elt) ((FilterElt *)filter_elt)
#define FILTER_LEVEL(filter_level) ((FilterLevel *)filter_level)

/* general code (object/interface init, properties, etc) */
static void         gtk_tree_model_filter_init                            (GtkTreeModelFilter      *filter);
static void         gtk_tree_model_filter_class_init                      (GtkTreeModelFilterClass *filter_class);
static void         gtk_tree_model_filter_tree_model_init                 (GtkTreeModelIface       *iface);
135
static void         gtk_tree_model_filter_drag_source_init                (GtkTreeDragSourceIface  *iface);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
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 */
169
static GtkTreeModelFlags gtk_tree_model_filter_get_flags                       (GtkTreeModel           *model);
170 171 172
static gint         gtk_tree_model_filter_get_n_columns                   (GtkTreeModel           *model);
static GType        gtk_tree_model_filter_get_column_type                 (GtkTreeModel           *model,
                                                                           gint                    index);
173 174 175
static gboolean     gtk_tree_model_filter_get_iter_full                   (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
                                                                           GtkTreePath            *path);
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
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);
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);

206 207 208 209 210 211 212 213
/* 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);
214 215 216 217

/* private functions */
static void        gtk_tree_model_filter_build_level                      (GtkTreeModelFilter     *filter,
                                                                           FilterLevel            *parent_level,
218 219 220
                                                                           FilterElt              *parent_elt,
                                                                           gboolean                emit_inserted);

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
static void        gtk_tree_model_filter_free_level                       (GtkTreeModelFilter     *filter,
                                                                           FilterLevel            *filter_level);

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

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

static void         gtk_tree_model_filter_real_unref_node                 (GtkTreeModel           *model,
                                                                           GtkTreeIter            *iter,
                                                                           gboolean                propagate_unref);

static void         gtk_tree_model_filter_set_model                       (GtkTreeModelFilter     *filter,
                                                                           GtkTreeModel           *child_model);
246 247 248 249
static void         gtk_tree_model_filter_ref_path                        (GtkTreeModelFilter     *filter,
                                                                           GtkTreePath            *path);
static void         gtk_tree_model_filter_unref_path                      (GtkTreeModelFilter     *filter,
                                                                           GtkTreePath            *path);
250 251 252 253 254 255
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
256
                                                                           gboolean                fetch_children);
257

258 259 260 261 262 263 264
static FilterElt   *gtk_tree_model_filter_get_nth                         (GtkTreeModelFilter     *filter,
                                                                           FilterLevel            *level,
                                                                           int                     n);
static FilterElt   *gtk_tree_model_filter_get_nth_visible                 (GtkTreeModelFilter     *filter,
                                                                           FilterLevel            *level,
                                                                           int                     n);

265 266 267 268 269
static FilterElt   *gtk_tree_model_filter_fetch_child                     (GtkTreeModelFilter     *filter,
                                                                           FilterLevel            *level,
                                                                           gint                    offset,
                                                                           gint                   *index);
static void         gtk_tree_model_filter_remove_node                     (GtkTreeModelFilter     *filter,
270
                                                                           GtkTreeIter            *iter);
Matthias Clasen's avatar
Matthias Clasen committed
271
static void         gtk_tree_model_filter_update_children                 (GtkTreeModelFilter     *filter,
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
                                                                           FilterLevel            *level,
                                                                           FilterElt              *elt);
static FilterElt   *bsearch_elt_with_offset                               (GArray                 *array,
                                                                           gint                   offset,
                                                                           gint                  *index);


static GObjectClass *parent_class = NULL;

GType
gtk_tree_model_filter_get_type (void)
{
  static GType tree_model_filter_type = 0;

  if (!tree_model_filter_type)
    {
      static const GTypeInfo tree_model_filter_info =
        {
          sizeof (GtkTreeModelFilterClass),
          NULL, /* base_init */
          NULL, /* base_finalize */
          (GClassInitFunc) gtk_tree_model_filter_class_init,
          NULL, /* class_finalize */
          NULL, /* class_data */
          sizeof (GtkTreeModelFilter),
          0, /* n_preallocs */
          (GInstanceInitFunc) gtk_tree_model_filter_init
        };

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

308 309 310 311 312 313 314
      static const GInterfaceInfo drag_source_info =
        {
          (GInterfaceInitFunc) gtk_tree_model_filter_drag_source_init,
          NULL,
          NULL
        };

315
      tree_model_filter_type = g_type_register_static (G_TYPE_OBJECT,
Matthias Clasen's avatar
Matthias Clasen committed
316
                                                       I_("GtkTreeModelFilter"),
317 318 319 320 321
                                                       &tree_model_filter_info, 0);

      g_type_add_interface_static (tree_model_filter_type,
                                   GTK_TYPE_TREE_MODEL,
                                   &tree_model_info);
322 323 324 325

      g_type_add_interface_static (tree_model_filter_type,
                                   GTK_TYPE_TREE_DRAG_SOURCE,
                                   &drag_source_info);
326 327 328 329 330 331 332 333 334 335 336 337 338 339
    }

  return tree_model_filter_type;
}

static void
gtk_tree_model_filter_init (GtkTreeModelFilter *filter)
{
  filter->priv = GTK_TREE_MODEL_FILTER_GET_PRIVATE (filter);

  filter->priv->visible_column = -1;
  filter->priv->zero_ref_count = 0;
  filter->priv->visible_method_set = FALSE;
  filter->priv->modify_func_set = FALSE;
340
  filter->priv->in_row_deleted = FALSE;
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
}

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

  object_class = (GObjectClass *) filter_class;
  parent_class = g_type_class_peek_parent (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;

  /* 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,
361
                                   g_param_spec_object ("child-model",
362 363 364
                                                        ("The child model"),
                                                        ("The model for the filtermodel to filter"),
                                                        GTK_TYPE_TREE_MODEL,
365
                                                        GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
366 367 368

  g_object_class_install_property (object_class,
                                   PROP_VIRTUAL_ROOT,
369
                                   g_param_spec_boxed ("virtual-root",
370 371 372
                                                       ("The virtual root"),
                                                       ("The virtual root (relative to the child model) for this filtermodel"),
                                                       GTK_TYPE_TREE_PATH,
373
                                                       GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

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

397 398 399 400 401 402 403 404
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;
}

405 406 407 408 409 410

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

411 412 413
  if (filter->priv->virtual_root)
    gtk_tree_model_filter_unref_path (filter, filter->priv->virtual_root);

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
  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)
    gtk_tree_model_filter_free_level (filter, filter->priv->root);

  if (filter->priv->modify_types)
    g_free (filter->priv->modify_types);

  /* must chain up */
  parent_class->finalize (object);
}

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

static void
gtk_tree_model_filter_build_level (GtkTreeModelFilter *filter,
                                   FilterLevel        *parent_level,
478 479
                                   FilterElt          *parent_elt,
                                   gboolean            emit_inserted)
480 481
{
  GtkTreeIter iter;
482
  GtkTreeIter first_node;
483 484 485 486 487 488 489
  GtkTreeIter root;
  FilterLevel *new_level;
  gint length = 0;
  gint i;

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

490 491 492
  if (filter->priv->in_row_deleted)
    return;

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
  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);
    }

  g_return_if_fail (length > 0);

  new_level = g_new (FilterLevel, 1);
  new_level->array = g_array_sized_new (FALSE, FALSE,
                                        sizeof (FilterElt),
                                        length);
  new_level->ref_count = 0;
540
  new_level->visible_nodes = 0;
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  new_level->parent_elt = parent_elt;
  new_level->parent_level = parent_level;

  if (parent_elt)
    parent_elt->children = new_level;
  else
    filter->priv->root = new_level;

  /* increase the count of zero ref_counts */
  while (parent_level)
    {
      parent_elt->zero_ref_count++;

      parent_elt = parent_level->parent_elt;
      parent_level = parent_level->parent_level;
    }
557 558
  if (new_level != filter->priv->root)
    filter->priv->zero_ref_count++;
559 560 561

  i = 0;

562
  first_node = iter;
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

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

          filter_elt.offset = i;
          filter_elt.zero_ref_count = 0;
          filter_elt.ref_count = 0;
          filter_elt.children = NULL;
          filter_elt.visible = TRUE;

          if (GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS (filter))
            filter_elt.iter = iter;

          g_array_append_val (new_level->array, filter_elt);
580 581 582 583 584 585 586 587 588
          new_level->visible_nodes++;

          if (new_level->parent_level || filter->priv->virtual_root)
            {
              GtkTreeIter f_iter;

              f_iter.stamp = filter->priv->stamp;
              f_iter.user_data = new_level;
              f_iter.user_data2 = &(g_array_index (new_level->array, FilterElt, new_level->array->len - 1));
589

590 591 592 593 594 595 596 597 598 599 600 601 602
              gtk_tree_model_filter_ref_node (GTK_TREE_MODEL (filter), &f_iter);

              if (emit_inserted)
                {
                  GtkTreePath *f_path;

                  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);
                }
            }
603 604 605 606
        }
      i++;
    }
  while (gtk_tree_model_iter_next (filter->priv->child_model, &iter));
607 608 609 610 611 612 613 614 615

  if (new_level->array->len == 0
      && (new_level != filter->priv->root || filter->priv->virtual_root))
    {
      /* If none of the nodes are visible, we will just pull in the
       * first node of the level and keep a reference on it.  We need this
       * to make sure that we get all signals for this level.
       */
      FilterElt filter_elt;
616
      GtkTreeIter f_iter;
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636

      filter_elt.offset = 0;
      filter_elt.zero_ref_count = 0;
      filter_elt.ref_count = 0;
      filter_elt.children = NULL;
      filter_elt.visible = FALSE;

      if (GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS (filter))
        filter_elt.iter = first_node;

      g_array_append_val (new_level->array, filter_elt);

      f_iter.stamp = filter->priv->stamp;
      f_iter.user_data = new_level;
      f_iter.user_data2 = &(g_array_index (new_level->array, FilterElt, new_level->array->len - 1));

      gtk_tree_model_filter_ref_node (GTK_TREE_MODEL (filter), &f_iter);
    }
  else if (new_level->array->len == 0)
    gtk_tree_model_filter_free_level (filter, new_level);
637 638 639 640 641 642 643 644 645 646
}

static void
gtk_tree_model_filter_free_level (GtkTreeModelFilter *filter,
                                  FilterLevel        *filter_level)
{
  gint i;

  g_assert (filter_level);

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
  for (i = 0; i < filter_level->array->len; i++)
    {
      if (g_array_index (filter_level->array, FilterElt, i).children)
        gtk_tree_model_filter_free_level (filter,
                                          FILTER_LEVEL (g_array_index (filter_level->array, FilterElt, i).children));

      if (filter_level->parent_level || filter->priv->virtual_root)
        {
          GtkTreeIter f_iter;

          f_iter.stamp = filter->priv->stamp;
          f_iter.user_data = filter_level;
          f_iter.user_data2 = &(g_array_index (filter_level->array, FilterElt, i));

          gtk_tree_model_filter_unref_node (GTK_TREE_MODEL (filter), &f_iter);
        }
    }

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
  if (filter_level->ref_count == 0)
    {
      FilterLevel *parent_level = filter_level->parent_level;
      FilterElt *parent_elt = filter_level->parent_elt;

      do
        {
          if (parent_elt)
            parent_elt->zero_ref_count--;

          if (parent_level)
            {
              parent_elt = parent_level->parent_elt;
              parent_level = parent_level->parent_level;
            }
        }
      while (parent_level);
682 683
      if (filter_level != filter->priv->root)
        filter->priv->zero_ref_count--;
684 685 686 687 688 689 690 691 692 693 694 695 696 697
    }

  if (filter_level->parent_elt)
    filter_level->parent_elt->children = NULL;
  else
    filter->priv->root = NULL;

  g_array_free (filter_level->array, TRUE);
  filter_level->array = NULL;

  g_free (filter_level);
  filter_level = NULL;
}

698
/* Creates paths suitable for accessing the child model. */
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
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);

      walker2 = walker->parent_elt;
      walker = walker->parent_level;
    }

  if (root)
    {
724
      real_path = gtk_tree_model_filter_add_root (path, root);
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
      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++)
    gtk_tree_path_append_index (retval, gtk_tree_path_get_indices (src)[i]);

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

  for (i = 0; i < gtk_tree_path_get_depth (root); i++)
    if (indices[i] != gtk_tree_path_get_indices (root)[i])
      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
gtk_tree_model_filter_visible (GtkTreeModelFilter *filter,
                               GtkTreeIter        *child_iter)
{
  if (filter->priv->visible_func)
    {
Matthias Clasen's avatar
Matthias Clasen committed
792 793 794
      return filter->priv->visible_func (filter->priv->child_model,
					 child_iter,
					 filter->priv->visible_data)
Matthias Clasen's avatar
Matthias Clasen committed
795
	? TRUE : FALSE;
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
    }
  else if (filter->priv->visible_column >= 0)
   {
     GValue val = {0, };

     gtk_tree_model_get_value (filter->priv->child_model, child_iter,
                               filter->priv->visible_column, &val);

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

     g_value_unset (&val);
     return FALSE;
   }

814
  /* no visible function set, so always visible */
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
  return TRUE;
}

static void
gtk_tree_model_filter_clear_cache_helper (GtkTreeModelFilter *filter,
                                          FilterLevel        *level)
{
  gint i;

  g_assert (level);

  for (i = 0; i < level->array->len; i++)
    {
      if (g_array_index (level->array, FilterElt, i).zero_ref_count > 0)
        gtk_tree_model_filter_clear_cache_helper (filter, g_array_index (level->array, FilterElt, i).children);
     }

  if (level->ref_count == 0 && level != filter->priv->root)
    {
      gtk_tree_model_filter_free_level (filter, level);
      return;
    }
}

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
static FilterElt *
gtk_tree_model_filter_get_nth (GtkTreeModelFilter *filter,
                               FilterLevel        *level,
                               int                 n)
{
  if (level->array->len <= n)
    return NULL;

  return &g_array_index (level->array, FilterElt, n);
}

static FilterElt *
gtk_tree_model_filter_get_nth_visible (GtkTreeModelFilter *filter,
                                       FilterLevel        *level,
                                       int                 n)
{
  int i = 0;
  FilterElt *elt;

  if (level->visible_nodes <= n)
    return NULL;

  elt = FILTER_ELT (level->array->data);
  while (!elt->visible)
    elt++;

  while (i < n)
    {
      if (elt->visible)
        i++;
      elt++;
    }

  while (!elt->visible)
    elt++;

  return elt;
}

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
static FilterElt *
gtk_tree_model_filter_fetch_child (GtkTreeModelFilter *filter,
                                   FilterLevel        *level,
                                   gint                offset,
                                   gint               *index)
{
  gint i = 0;
  gint start, middle, end;
  gint len;
  GtkTreePath *c_path = NULL;
  GtkTreeIter c_iter;
  GtkTreePath *c_parent_path = NULL;
  GtkTreeIter c_parent_iter;
  FilterElt elt;

  /* check if child exists and is visible */
  if (level->parent_elt)
    {
      c_parent_path =
        gtk_tree_model_filter_elt_get_path (level->parent_level,
                                            level->parent_elt,
                                            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;

  /* add child */
  elt.offset = offset;
  elt.zero_ref_count = 0;
  elt.ref_count = 0;
  elt.children = NULL;
  /* visibility should be FALSE as we don't emit row_inserted */
  elt.visible = FALSE;

  if (GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS (filter))
    elt.iter = c_iter;

  /* find index (binary search on offset) */
  start = 0;
  end = level->array->len;

  if (start != end)
    {
      while (start != end)
        {
          middle = (start + end) / 2;

          if (g_array_index (level->array, FilterElt, middle).offset <= offset)
            start = middle + 1;
          else
            end = middle;
        }

      if (g_array_index (level->array, FilterElt, middle).offset <= offset)
        i = middle + 1;
      else
        i = middle;
    }
  else
    i = 0;

  g_array_insert_val (level->array, i, elt);
  *index = i;

973 974 975 976
  if (i > 0)
    i--;

  for ( ; i < level->array->len; i++)
977 978 979 980 981 982
    {
      FilterElt *e = &(g_array_index (level->array, FilterElt, i));
      if (e->children)
        e->children->parent_elt = e;
    }

983 984 985 986 987 988 989
  c_iter.stamp = filter->priv->stamp;
  c_iter.user_data = level;
  c_iter.user_data2 = &g_array_index (level->array, FilterElt, *index);

  if (level->parent_level || filter->priv->virtual_root)
    gtk_tree_model_filter_ref_node (GTK_TREE_MODEL (filter), &c_iter);

990 991 992 993 994
  return &g_array_index (level->array, FilterElt, *index);
}

static void
gtk_tree_model_filter_remove_node (GtkTreeModelFilter *filter,
995
                                   GtkTreeIter        *iter)
996 997 998
{
  FilterElt *elt, *parent;
  FilterLevel *level, *parent_level;
999
  gint offset, i, length;
1000

1001
  gboolean emit_child_toggled = FALSE;
1002 1003 1004 1005 1006 1007

  level = FILTER_LEVEL (iter->user_data);
  elt = FILTER_ELT (iter->user_data2);

  parent = level->parent_elt;
  parent_level = level->parent_level;
1008

1009
  length = level->array->len;
1010
  offset = elt->offset;
1011

1012 1013 1014 1015 1016 1017 1018 1019 1020
  /* we distinguish a couple of cases:
   *  - root level, length > 1: emit row-deleted and remove.
   *  - root level, length == 1: emit row-deleted and keep in cache.
   *  - level, length == 1: parent->ref_count > 1: emit row-deleted and keep.
   *  - level, length > 1: emit row-deleted and remove.
   *  - else, remove level.
   *
   *  if level != root level and visible nodes == 0, emit row-has-child-toggled.
   */
1021

1022 1023
  if (level != filter->priv->root && level->visible_nodes == 0)
    emit_child_toggled = TRUE;
1024

1025
  if (length > 1)
1026 1027
    {
      GtkTreePath *path;
1028 1029 1030 1031
      FilterElt *tmp;

      /* we emit row-deleted, and remove the node from the cache.
       */
1032 1033

      path = gtk_tree_model_get_path (GTK_TREE_MODEL (filter), iter);
1034
      elt->visible = FALSE;
1035
      gtk_tree_model_filter_increment_stamp (filter);
1036
      iter->stamp = filter->priv->stamp;
1037 1038 1039
      gtk_tree_model_row_deleted (GTK_TREE_MODEL (filter), path);
      gtk_tree_path_free (path);

1040 1041 1042
      while (elt->ref_count > 1)
        gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
                                               iter, FALSE);
1043

1044 1045 1046 1047 1048
      if (parent_level || filter->priv->virtual_root)
        gtk_tree_model_filter_unref_node (GTK_TREE_MODEL (filter), iter);
      else
        gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
                                               iter, FALSE);
1049 1050 1051 1052 1053 1054 1055 1056

      /* remove the node */
      tmp = bsearch_elt_with_offset (level->array, elt->offset, &i);

      if (tmp)
        {
          g_array_remove_index (level->array, i);

1057 1058
	  i--;
          for (i = MAX (i, 0); i < level->array->len; i++)
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
            {
              /* NOTE: here we do *not* decrease offsets, because the node was
               * not removed from the child model
               */
              elt = &g_array_index (level->array, FilterElt, i);
              if (elt->children)
                elt->children->parent_elt = elt;
            }
        }
    }
1069 1070 1071 1072
  else if ((length == 1 && parent && parent->ref_count > 1)
           || (length == 1 && level == filter->priv->root))
    {
      GtkTreePath *path;
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
      /* we emit row-deleted, but keep the node in the cache and
       * referenced.
       */

      path = gtk_tree_model_get_path (GTK_TREE_MODEL (filter), iter);
      elt->visible = FALSE;
      gtk_tree_model_filter_increment_stamp (filter);
      gtk_tree_model_row_deleted (GTK_TREE_MODEL (filter), path);
      gtk_tree_path_free (path);
    }
  else
    {
      GtkTreePath *path;

      /* blow level away */

      path = gtk_tree_model_get_path (GTK_TREE_MODEL (filter), iter);
      elt->visible = FALSE;
      gtk_tree_model_filter_increment_stamp (filter);
      iter->stamp = filter->priv->stamp;
      gtk_tree_model_row_deleted (GTK_TREE_MODEL (filter), path);
      gtk_tree_path_free (path);

      while (elt->ref_count > 1)
        gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
                                               iter, FALSE);

      gtk_tree_model_filter_free_level (filter, level);
    }

  if (emit_child_toggled)
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
    {
      GtkTreeIter piter;
      GtkTreePath *ppath;

      piter.stamp = filter->priv->stamp;
      piter.user_data = parent_level;
      piter.user_data2 = parent;

      ppath = gtk_tree_model_get_path (GTK_TREE_MODEL (filter), &piter);

      gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (filter),
                                            ppath, &piter);
      gtk_tree_path_free (ppath);
    }
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1122 1123 1124
gtk_tree_model_filter_update_children (GtkTreeModelFilter *filter,
				       FilterLevel        *level,
				       FilterElt          *elt)
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
{
  GtkTreeIter c_iter;
  GtkTreeIter iter;

  if (!elt->visible)
    return;

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

  gtk_tree_model_filter_convert_iter_to_child_iter (filter, &c_iter, &iter);

  if (gtk_tree_model_iter_has_child (filter->priv->child_model, &c_iter))
    {
      GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (filter),
                                                   &iter);
      gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (filter),
                                            path,
                                            &iter);
      if (path)
        gtk_tree_path_free (path);
    }
}

static FilterElt *
bsearch_elt_with_offset (GArray *array,
                         gint    offset,
                         gint   *index)
{
  gint start, middle, end;
  FilterElt *elt;

  start = 0;
  end = array->len;

  if (array->len < 1)
    return NULL;

  if (start == end)
    {
      elt = &g_array_index (array, FilterElt, 0);

      if (elt->offset == offset)
        {
          *index = 0;
          return elt;
        }
      else
        return NULL;
    }

1177
  do
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    {
      middle = (start + end) / 2;

      elt = &g_array_index (array, FilterElt, middle);

      if (elt->offset < offset)
        start = middle + 1;
      else if (elt->offset > offset)
        end = middle;
      else
        break;
    }
1190
  while (start != end);
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

  if (elt->offset == offset)
    {
      *index = middle;
      return elt;
    }

  return NULL;
}

/* TreeModel signals */
static void
gtk_tree_model_filter_row_changed (GtkTreeModel *c_model,
                                   GtkTreePath  *c_path,
                                   GtkTreeIter  *c_iter,
                                   gpointer      data)
{
  GtkTreeModelFilter *filter = GTK_TREE_MODEL_FILTER (data);
  GtkTreeIter iter;
Matthias Clasen's avatar
Matthias Clasen committed
1210
  GtkTreeIter children;
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
  GtkTreeIter real_c_iter;
  GtkTreePath *path = NULL;

  FilterElt *elt;
  FilterLevel *level;

  gboolean requested_state;
  gboolean current_state;
  gboolean free_c_path = FALSE;

  g_return_if_fail (c_path != NULL || c_iter != NULL);

  if (!c_path)
    {
      c_path = gtk_tree_model_get_path (c_model, c_iter);
      free_c_path = TRUE;
    }

  if (c_iter)
    real_c_iter = *c_iter;
  else
    gtk_tree_model_get_iter (c_model, &real_c_iter, c_path);

1234 1235 1236 1237 1238 1239
  /* is this node above the virtual root? */
  if (filter->priv->virtual_root
      && (gtk_tree_path_get_depth (filter->priv->virtual_root)
          >= gtk_tree_path_get_depth (c_path)))
    goto done;

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
  /* what's the requested state? */
  requested_state = gtk_tree_model_filter_visible (filter, &real_c_iter);

  /* now, let's see whether the item is there */
  path = gtk_real_tree_model_filter_convert_child_path_to_path (filter,
                                                                c_path,
                                                                FALSE,
                                                                FALSE);

  if (path)
    {
1251 1252
      gtk_tree_model_filter_get_iter_full (GTK_TREE_MODEL (filter),
                                           &iter, path);
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
      current_state = FILTER_ELT (iter.user_data2)->visible;
    }
  else
    current_state = FALSE;

  if (current_state == FALSE && requested_state == FALSE)
    /* no changes required */
    goto done;

  if (current_state == TRUE && requested_state == FALSE)
    {
      /* get rid of this node */
      level = FILTER_LEVEL (iter.user_data);
1266
      level->visible_nodes--;
1267

1268
      gtk_tree_model_filter_remove_node (filter, &iter);
1269

1270 1271 1272 1273 1274
      goto done;
    }

  if (current_state == TRUE && requested_state == TRUE)
    {
1275 1276 1277 1278 1279
      /* propagate the signal; also get a path taking only visible
       * nodes into account.
       */
      gtk_tree_path_free (path);
      path = gtk_tree_model_get_path (GTK_TREE_MODEL (filter), &iter);
1280 1281 1282 1283 1284
      gtk_tree_model_row_changed (GTK_TREE_MODEL (filter), path, &iter);

      level = FILTER_LEVEL (iter.user_data);
      elt = FILTER_ELT (iter.user_data2);

Matthias Clasen's avatar
Matthias Clasen committed
1285 1286 1287
      /* and update the children */
      if (gtk_tree_model_iter_children (c_model, &children, &real_c_iter))
        gtk_tree_model_filter_update_children (filter, level, elt);
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302

      goto done;
    }

  /* only current == FALSE and requested == TRUE is left,
   * pull in the child
   */
  g_return_if_fail (current_state == FALSE && requested_state == TRUE);

  /* make sure the new item has been pulled in */
  if (!filter->priv->root)
    {
      gint i;
      FilterLevel *root;

1303
      gtk_tree_model_filter_build_level (filter, NULL, NULL, FALSE);
1304 1305 1306 1307 1308 1309 1310

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

      if (root)
        {
          for (i = 0; i < root->array->len; i++)
            g_array_index (root->array, FilterElt, i).visible = FALSE;
1311
          root->visible_nodes = 0;
1312 1313 1314
        }
    }

1315 1316
  gtk_tree_model_filter_increment_stamp (filter);

1317 1318 1319
  if (!path)
    path = gtk_real_tree_model_filter_convert_child_path_to_path (filter,
                                                                  c_path,
1320
                                                                  TRUE,
1321 1322
                                                                  TRUE);

1323 1324 1325
  if (!path)
    /* parent is probably being filtered out */
    goto done;
1326

1327
  gtk_tree_model_filter_get_iter_full (GTK_TREE_MODEL (filter), &iter, path);
1328 1329 1330 1331

  level = FILTER_LEVEL (iter.user_data);
  elt = FILTER_ELT (iter.user_data2);

1332 1333 1334 1335 1336 1337
  /* elt->visible can be TRUE at this point if it was pulled in above */
  if (!elt->visible)
    {
      elt->visible = TRUE;
      level->visible_nodes++;
    }
1338

1339 1340 1341 1342 1343
  if ((level->parent_elt && level->parent_elt->visible) || !level->parent_elt)
    {
      /* visibility changed -- reget path */
      gtk_tree_path_free (path);
      path = gtk_tree_model_get_path (GTK_TREE_MODEL (filter), &iter);
1344

1345
      gtk_tree_model_row_inserted (GTK_TREE_MODEL (filter), path, &iter);
1346

1347 1348 1349
      if (gtk_tree_model_iter_children (c_model, &children, c_iter))
        gtk_tree_model_filter_update_children (filter, level, elt);
    }
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365

done:
  if (path)
    gtk_tree_path_free (path);

  if (free_c_path)
    gtk_tree_path_free (c_path);
}

static void
gtk_tree_model_filter_row_inserted (GtkTreeModel *c_model,
                                    GtkTreePath  *c_path,
                                    GtkTreeIter  *c_iter,
                                    gpointer      data)
{
  GtkTreeModelFilter *filter = GTK_TREE_MODEL_FILTER (data);
Christian Persch's avatar
Christian Persch committed
1366 1367
  GtkTreePath *path = NULL;
  GtkTreePath *real_path = NULL;
1368 1369 1370 1371 1372 1373 1374 1375
  GtkTreeIter iter;

  GtkTreeIter real_c_iter;

  FilterElt *elt;
  FilterLevel *level;
  FilterLevel *parent_level;

1376
  gint i = 0, offset;
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402

  gboolean free_c_path = FALSE;

  g_return_if_fail (c_path != NULL || c_iter != NULL);

  if (!c_path)
    {
      c_path = gtk_tree_model_get_path (c_model, c_iter);
      free_c_path = TRUE;
    }

  if (c_iter)
    real_c_iter = *c_iter;
  else
    gtk_tree_model_get_iter (c_model, &real_c_iter, c_path);

  /* the row has already been inserted. so we need to fixup the
   * virtual root here first
   */
  if (filter->priv->virtual_root)
    {
      if (gtk_tree_path_get_depth (filter->priv->virtual_root) >=
          gtk_tree_path_get_depth (c_path))
        {
          gint level;
          gint *v_indices, *c_indices;
1403
          gboolean common_prefix = TRUE;
1404 1405 1406 1407 1408

          level = gtk_tree_path_get_depth (c_path) - 1;
          v_indices = gtk_tree_path_get_indices (filter->priv->virtual_root);
          c_indices = gtk_tree_path_get_indices (c_path);

1409 1410 1411 1412 1413 1414 1415 1416
          for (i = 0; i < level; i++)
            if (v_indices[i] != c_indices[i])
              {
                common_prefix = FALSE;
                break;
              }

          if (common_prefix && v_indices[level] >= c_indices[level])
1417 1418 1419 1420 1421 1422
            (v_indices[level])++;
        }
    }

  if (!filter->priv->root)
    {
1423 1424
      /* build level will pull in the new child */
      gtk_tree_model_filter_build_level (filter, NULL, NULL, FALSE);
1425

1426 1427 1428 1429 1430
      if (filter->priv->root
          && FILTER_LEVEL (filter->priv->root)->visible_nodes)
        goto done_and_emit;
      else
        goto done;
1431 1432 1433 1434 1435 1436 1437 1438 1439
    }

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

  /* subtract virtual root if necessary */
  if (filter->priv->virtual_root)
    {
      real_path = gtk_tree_model_filter_remove_root (c_path,
                                                     filter->priv->virtual_root);
1440
      /* not our child */
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 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 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
      if (!real_path)
        goto done;
    }
  else
    real_path = gtk_tree_path_copy (c_path);

  if (gtk_tree_path_get_depth (real_path) - 1 >= 1)
    {
      /* find the parent level */
      while (i < gtk_tree_path_get_depth (real_path) - 1)
        {
          gint j;

          if (!level)
            /* we don't cover this signal */
            goto done;

          elt = bsearch_elt_with_offset (level->array,
                                         gtk_tree_path_get_indices (real_path)[i],
                                         &j);

          if (!elt)
            /* parent is probably being filtered out */
            goto done;

          if (!elt->children)
            {
              GtkTreePath *tmppath;
              GtkTreeIter  tmpiter;

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

              tmppath = gtk_tree_model_get_path (GTK_TREE_MODEL (data),
                                                 &tmpiter);

              if (tmppath)
                {
                  gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (data),
                                                        tmppath, &tmpiter);
                  gtk_tree_path_free (tmppath);
                }

              /* not covering this signal */
              goto done;
            }

          level = elt->children;
          parent_level = level;
          i++;
        }
    }

  if (!parent_level)
    goto done;

  /* let's try to insert the value */
  offset = gtk_tree_path_get_indices (real_path)[gtk_tree_path_get_depth (real_path) - 1];

  /* update the offsets, yes if we didn't insert the node above, there will
   * be a gap here. This will be filled with the node (via fetch_child) when
   * it becomes visible