* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
* a #GtkTreePath indicating the root node for the filter at construction time.
* </para></listitem>
* </itemizedlist>
- */
-
-
-/* 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.
+ * 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.
*/
-/* 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 position
- * of FilterElt in the sequence.
+/* 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:
+ *
+ * iter->stamp = filter->priv->stamp
+ * iter->user_data = FilterLevel
+ * iter->user_data2 = FilterElt
+ *
+ * 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.
*
- * Note that there are two kinds of paths relative to the filter model
- * (those generated from the sequence positions): 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.
+ * 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 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.
+ * 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
+ * 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
+ * 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).
*/
+
typedef struct _FilterElt FilterElt;
typedef struct _FilterLevel FilterLevel;
# define GTK_TREE_MODEL_FILTER_CACHE_CHILD_ITERS(filter) (FALSE)
#endif
+/* Defining this constant enables more assertions, which will be
+ * helpful when debugging the code.
+ */
+#undef MODEL_FILTER_DEBUG
+
#define FILTER_ELT(filter_elt) ((FilterElt *)filter_elt)
#define FILTER_LEVEL(filter_level) ((FilterLevel *)filter_level)
#define GET_ELT(siter) ((FilterElt*) (siter ? g_sequence_get (siter) : NULL))
static void gtk_tree_model_filter_free_level (GtkTreeModelFilter *filter,
FilterLevel *filter_level,
- gboolean unref);
+ gboolean unref_self,
+ gboolean unref_parent,
+ gboolean unref_external);
static GtkTreePath *gtk_tree_model_filter_elt_get_path (FilterLevel *level,
FilterElt *elt,
gtk_tree_path_free (filter->priv->virtual_root);
if (filter->priv->root)
- gtk_tree_model_filter_free_level (filter, filter->priv->root, TRUE);
+ gtk_tree_model_filter_free_level (filter, filter->priv->root, TRUE, TRUE, FALSE);
g_free (filter->priv->modify_types);
while (tmp_level)
{
- parent_elt->zero_ref_count++;
+ tmp_elt->zero_ref_count++;
tmp_elt = tmp_level->parent_elt;
tmp_level = tmp_level->parent_level;
/* 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,
- * if the parent of the parent node is not visible. In that case,
- * possible changes in state of the parent are not requested.
+ * 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.
*/
if (empty &&
- (parent_level && parent_level->parent_level &&
- parent_level->parent_elt->ext_ref_count == 0))
+ (parent_level && parent_level->ext_ref_count == 0))
{
- gtk_tree_model_filter_free_level (filter, new_level, FALSE);
+ gtk_tree_model_filter_free_level (filter, new_level, FALSE, TRUE, FALSE);
return;
}
static void
gtk_tree_model_filter_free_level (GtkTreeModelFilter *filter,
FilterLevel *filter_level,
- gboolean unref)
+ gboolean unref_self,
+ gboolean unref_parent,
+ gboolean unref_external)
{
GSequenceIter *siter;
GSequenceIter *end_siter;
FilterElt *elt = g_sequence_get (siter);
if (elt->children)
- gtk_tree_model_filter_free_level (filter,
- FILTER_LEVEL (elt->children),
- unref);
+ {
+ /* If we recurse and unref_self == FALSE, then unref_parent
+ * must also be FALSE (otherwise a still unref a node in this
+ * level).
+ */
+ gtk_tree_model_filter_free_level (filter,
+ FILTER_LEVEL (elt->children),
+ unref_self,
+ unref_self == FALSE ? FALSE : unref_parent,
+ unref_external);
+ }
+
+ 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_self);
+ }
}
/* Release the reference on the first item.
*/
- if (unref)
+ if (unref_self)
{
GtkTreeIter f_iter;
filter->priv->zero_ref_count--;
}
+#ifdef MODEL_FILTER_DEBUG
+ if (filter_level == filter->priv->root)
+ g_assert (filter->priv->zero_ref_count == 0);
+#endif
+
if (filter_level->parent_elt)
{
/* Release reference on parent */
- if (unref)
- {
- GtkTreeIter parent_iter;
+ GtkTreeIter parent_iter;
- parent_iter.stamp = filter->priv->stamp;
- parent_iter.user_data = filter_level->parent_level;
- parent_iter.user_data2 = filter_level->parent_elt;
+ parent_iter.stamp = filter->priv->stamp;
+ parent_iter.user_data = filter_level->parent_level;
+ parent_iter.user_data2 = filter_level->parent_elt;
- gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
- &parent_iter, FALSE, TRUE);
- }
+ gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
+ &parent_iter, FALSE, unref_parent);
filter_level->parent_elt->children = NULL;
}
g_free (filter_level);
}
+/* 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, 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);
+ /* In this case, we do remove reference counts we've added ourselves,
+ * since the node will be removed from the data structures.
+ */
+ while (elt->ref_count > 0)
+ gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
+ &f_iter, FALSE, TRUE);
+
+ 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
+}
+
static void
gtk_tree_model_filter_level_transfer_first_ref (GtkTreeModelFilter *filter,
FilterLevel *level,
}
else if (filter->priv->visible_column >= 0)
{
- GValue val = {0, };
+ GValue val = G_VALUE_INIT;
gtk_tree_model_get_value (child_model, child_iter,
filter->priv->visible_column, &val);
GtkTreeModelFilter *filter = user_data;
FilterElt *elt = data;
+#ifdef MODEL_FILTER_DEBUG
+ g_assert (elt->zero_ref_count >= 0);
+#endif
+
if (elt->zero_ref_count > 0)
gtk_tree_model_filter_clear_cache_helper (filter, elt->children);
}
/* 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
- * free the level if the parent's parent also has an external ref
+ * free the level if the parent level also has an external ref
* count of zero. In that case, changes concerning our parent are
* not requested.
+ *
+ * The root level is always visible, so an exception holds for levels
+ * with the root level as parent level: these have to remain cached.
*/
if (level->ext_ref_count == 0 && level != filter->priv->root &&
- level->parent_level && level->parent_elt &&
- level->parent_level->parent_level &&
- level->parent_level->parent_elt->ext_ref_count == 0)
+ level->parent_level && level->parent_level != filter->priv->root &&
+ level->parent_level->ext_ref_count == 0)
{
- gtk_tree_model_filter_free_level (filter, level, TRUE);
+ gtk_tree_model_filter_free_level (filter, level, TRUE, TRUE, FALSE);
return;
}
}
gboolean emit_child_toggled = FALSE;
+ /* 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;
+
iter.stamp = filter->priv->stamp;
iter.user_data = level;
iter.user_data2 = elt;
- path = gtk_tree_model_get_path (GTK_TREE_MODEL (filter), &iter);
-
parent = level->parent_elt;
parent_level = level->parent_level;
- length = g_sequence_get_length (level->seq);
+ if (!parent || 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;
- /* 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;
+ length = g_sequence_get_length (level->seq);
/* first register the node to be invisible */
g_sequence_remove (elt->visible_siter);
elt->visible_siter = NULL;
- /* 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->ext_ref_count > 0: emit row-deleted
- * and keep.
- * - level, length > 1: emit row-deleted and remove.
- * - else, remove level.
- *
- * 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.
+ /*
+ * 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.
*/
if (level != filter->priv->root
&& parent->visible_siter)
emit_child_toggled = TRUE;
+ /* 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.
+ */
if (length > 1)
{
GSequenceIter *siter;
* If it has any children, these will be removed here as well.
*/
+ /* 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.
+ */
if (elt->children)
- gtk_tree_model_filter_free_level (filter, elt->children, TRUE);
+ gtk_tree_model_filter_free_level (filter, elt->children, TRUE, TRUE, TRUE);
/* If the first node is being removed, transfer, the reference */
if (elt == g_sequence_get (g_sequence_get_begin_iter (level->seq)))
while (elt->ext_ref_count > 0)
gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
- &iter, TRUE, FALSE);
+ &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.
+ */
while (elt->ref_count > 0)
gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
- &iter, FALSE, FALSE);
+ &iter, FALSE, TRUE);
/* remove the node */
lookup_elt_with_offset (level->seq, elt->offset, &siter);
if (!parent || orig_level_ext_ref_count > 0)
gtk_tree_model_row_deleted (GTK_TREE_MODEL (filter), path);
}
- else if ((length == 1 && parent && parent->ext_ref_count > 0)
- || (length == 1 && level == filter->priv->root))
+ else
{
- /* We emit row-deleted, but keep the node in the cache and
- * referenced. Its children will be removed.
- */
-
- if (elt->children)
- {
- gtk_tree_model_filter_free_level (filter, elt->children, TRUE);
- elt->children = NULL;
- }
-
- gtk_tree_model_filter_increment_stamp (filter);
+ /* There is only one node left in this level */
+#ifdef MODEL_FILTER_DEBUG
+ g_assert (length == 1);
+#endif
- /* Only if the node is in the root level (parent == NULL) or
- * the level is visible, a row-deleted signal is necessary.
+ /* The row is signalled as deleted to the client. We have to
+ * drop the remaining external reference count here, the client
+ * will not do it.
+ *
+ * We keep the reference counts we've obtained ourselves.
*/
- if (!parent || orig_level_ext_ref_count > 0)
- gtk_tree_model_row_deleted (GTK_TREE_MODEL (filter), path);
- }
- else
- {
while (elt->ext_ref_count > 0)
gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
- &iter, TRUE, FALSE);
- while (elt->ref_count > 0)
- gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (filter),
- &iter, FALSE, FALSE);
-
- /* Blow level away, including any child levels */
- gtk_tree_model_filter_free_level (filter, level, TRUE);
+ &iter, TRUE, TRUE);
- gtk_tree_model_filter_increment_stamp (filter);
+ /* This level is still required if:
+ * - it is the root level
+ * - its parent level is the root level
+ * - its parent level has an external ref count > 0
+ */
+ if (! (level == filter->priv->root ||
+ level->parent_level == filter->priv->root ||
+ level->parent_level->ext_ref_count > 0))
+ {
+ /* Otherwise, the level can be removed */
+ gtk_tree_model_filter_free_level (filter, level, TRUE, TRUE, TRUE);
+ }
+ else
+ {
+ /* Level is kept, but we turn our attention to a child level.
+ *
+ * If level is not the root level, it is a child level with
+ * an ext ref count that is now 0. That means that any child level
+ * of elt can be removed.
+ */
+ if (level != filter->priv->root)
+ {
+#ifdef MODEL_FILTER_DEBUG
+ g_assert (level->ext_ref_count == 0);
+#endif
+ if (elt->children)
+ gtk_tree_model_filter_free_level (filter, elt->children,
+ TRUE, TRUE, TRUE);
+ }
+ else
+ {
+ /* In this case, we want to keep the level with the first
+ * node pulled in to monitor for signals.
+ */
+ if (elt->children)
+ gtk_tree_model_filter_prune_level (filter, elt->children);
+ }
+ }
if (!parent || orig_level_ext_ref_count > 0)
gtk_tree_model_row_deleted (GTK_TREE_MODEL (filter), path);
gtk_tree_model_filter_convert_iter_to_child_iter (filter, &c_iter, &iter);
- if ((!level->parent_level || level->parent_elt->ext_ref_count > 0) &&
+ if ((!level->parent_level || level->parent_level->ext_ref_count > 0) &&
gtk_tree_model_iter_has_child (filter->priv->child_model, &c_iter))
{
if (!elt->children)
if (current_state == TRUE && requested_state == TRUE)
{
- /* 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);
-
level = FILTER_LEVEL (iter.user_data);
elt = FILTER_ELT (iter.user_data2);
if (gtk_tree_model_filter_elt_is_visible_in_target (level, elt))
{
+ /* 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);
+
if (level->ext_ref_count > 0)
gtk_tree_model_row_changed (GTK_TREE_MODEL (filter), path, &iter);
}
done:
- gtk_tree_model_filter_check_ancestors (filter, real_path);
+ if (real_path)
+ gtk_tree_model_filter_check_ancestors (filter, real_path);
if (emit_row_inserted)
gtk_tree_model_filter_emit_row_inserted_for_path (filter, c_model,
* nodes will fail, since the respective nodes in the child model are
* no longer there.
*/
- gtk_tree_model_filter_free_level (filter, filter->priv->root, FALSE);
+ gtk_tree_model_filter_free_level (filter, filter->priv->root, FALSE, TRUE, FALSE);
gtk_tree_model_filter_increment_stamp (filter);
while (elt->ext_ref_count > 0)
gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (data), &iter,
TRUE, FALSE);
- while (elt->ref_count > 0)
- gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (data), &iter,
- FALSE, FALSE);
+
+ if (elt->children)
+ /* If this last node has children, then the recursion in free_level
+ * will release this reference.
+ */
+ while (elt->ref_count > 1)
+ gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (data), &iter,
+ FALSE, FALSE);
+ else
+ while (elt->ref_count > 0)
+ gtk_tree_model_filter_real_unref_node (GTK_TREE_MODEL (data), &iter,
+ FALSE, FALSE);
+
if (g_sequence_get_length (level->seq) == 1)
{
/* kill level */
- gtk_tree_model_filter_free_level (filter, level, FALSE);
+ gtk_tree_model_filter_free_level (filter, level, FALSE, TRUE, FALSE);
}
else
{
lookup_elt_with_offset (level->seq, elt->offset, &siter);
is_first = g_sequence_get_begin_iter (level->seq) == siter;
+ if (elt->children)
+ gtk_tree_model_filter_free_level (filter, elt->children,
+ FALSE, FALSE, FALSE);
+
/* remove the row */
- g_sequence_remove (elt->visible_siter);
+ if (elt->visible_siter)
+ g_sequence_remove (elt->visible_siter);
tmp = g_sequence_iter_next (siter);
g_sequence_remove (siter);
g_sequence_foreach_range (tmp, g_sequence_get_end_iter (level->seq),
GSequence *tmp_seq;
GSequenceIter *tmp_end_iter;
- GSequenceIter *old_first_elt = NULL;
+ GSequenceIter *old_first_siter = NULL;
gint *tmp_array;
gint i, elt_count;
gint length;
gtk_tree_model_filter_get_iter_full (GTK_TREE_MODEL (data),
&iter, path);
- level = FILTER_LEVEL (iter.user_data);
elt = FILTER_ELT (iter.user_data2);
if (!elt->children)
tmp_array = g_new (gint, g_sequence_get_length (level->visible_seq));
elt_count = 0;
+ old_first_siter = g_sequence_get_iter_at_pos (level->seq, 0);
+
for (i = 0; i < length; i++)
{
FilterElt *elt;
if (elt == NULL)
continue;
- /* Keep a reference if this elt has old_pos == 0 */
- if (new_order[i] == 0)
- old_first_elt = siter;
-
/* Only for visible items an entry should be present in the order array
* to be emitted.
*/
g_sequence_sort (level->visible_seq, filter_elt_cmp, NULL);
/* Transfer the reference from the old item at position 0 to the
- * new item at position 0.
+ * new item at position 0, unless the old item at position 0 is also
+ * at position 0 in the new sequence.
*/
- if (old_first_elt && g_sequence_iter_get_position (old_first_elt))
+ if (g_sequence_iter_get_position (old_first_siter) != 0)
gtk_tree_model_filter_level_transfer_first_ref (filter,
level,
- old_first_elt,
+ old_first_siter,
g_sequence_get_iter_at_pos (level->seq, 0));
-
/* emit rows_reordered */
if (g_sequence_get_length (level->visible_seq) > 0)
{
gtk_tree_model_filter_iter_next (GtkTreeModel *model,
GtkTreeIter *iter)
{
- FilterLevel *level;
FilterElt *elt;
GSequenceIter *siter;
g_return_val_if_fail (GTK_TREE_MODEL_FILTER (model)->priv->child_model != NULL, FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_FILTER (model)->priv->stamp == iter->stamp, FALSE);
- level = iter->user_data;
elt = iter->user_data2;
siter = g_sequence_iter_next (elt->visible_siter);
gtk_tree_model_filter_iter_previous (GtkTreeModel *model,
GtkTreeIter *iter)
{
- FilterLevel *level;
FilterElt *elt;
GSequenceIter *siter;
g_return_val_if_fail (GTK_TREE_MODEL_FILTER (model)->priv->child_model != NULL, FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_FILTER (model)->priv->stamp == iter->stamp, FALSE);
- level = iter->user_data;
elt = iter->user_data2;
- siter = g_sequence_iter_prev (elt->visible_siter);
- if (g_sequence_iter_is_begin (siter))
+ if (g_sequence_iter_is_begin (elt->visible_siter))
{
iter->stamp = 0;
return FALSE;
}
+ siter = g_sequence_iter_prev (elt->visible_siter);
iter->user_data2 = GET_ELT (siter);
if (filter->priv->root != level)
filter->priv->zero_ref_count--;
+
+#ifdef MODEL_FILTER_DEBUG
+ g_assert (filter->priv->zero_ref_count >= 0);
+ if (filter->priv->zero_ref_count > 0)
+ g_assert (filter->priv->root != NULL);
+#endif
}
}
+
+#ifdef MODEL_FILTER_DEBUG
+ g_assert (elt->ref_count >= elt->ext_ref_count);
+ g_assert (elt->ref_count >= 0);
+ g_assert (elt->ext_ref_count >= 0);
+#endif
}
static void
elt = iter->user_data2;
g_return_if_fail (elt->ref_count > 0);
+#ifdef MODEL_FILTER_DEBUG
+ g_assert (elt->ref_count >= elt->ext_ref_count);
+ g_assert (elt->ref_count >= 0);
+ g_assert (elt->ext_ref_count >= 0);
+#endif
elt->ref_count--;
level->ref_count--;
if (filter->priv->root != level)
filter->priv->zero_ref_count++;
+
+#ifdef MODEL_FILTER_DEBUG
+ g_assert (filter->priv->zero_ref_count >= 0);
+ if (filter->priv->zero_ref_count > 0)
+ g_assert (filter->priv->root != NULL);
+#endif
}
}
+
+#ifdef MODEL_FILTER_DEBUG
+ g_assert (elt->ref_count >= elt->ext_ref_count);
+ g_assert (elt->ref_count >= 0);
+ g_assert (elt->ext_ref_count >= 0);
+#endif
}
/* TreeDragSource interface implementation */
/* reset our state */
if (filter->priv->root)
- gtk_tree_model_filter_free_level (filter, filter->priv->root, TRUE);
+ gtk_tree_model_filter_free_level (filter, filter->priv->root,
+ TRUE, TRUE, FALSE);
filter->priv->root = NULL;
g_object_unref (filter->priv->child_model);
{
g_return_if_fail (GTK_IS_TREE_MODEL_FILTER (filter));
- if (!root)
- filter->priv->virtual_root = NULL;
+ if (root)
+ {
+ filter->priv->virtual_root = gtk_tree_path_copy (root);
+ gtk_tree_model_filter_ref_path (filter, filter->priv->virtual_root);
+ filter->priv->virtual_root_deleted = FALSE;
+ }
else
- filter->priv->virtual_root = gtk_tree_path_copy (root);
+ filter->priv->virtual_root = NULL;
}
/* public API */
gtk_tree_model_filter_new (GtkTreeModel *child_model,
GtkTreePath *root)
{
- GtkTreeModel *retval;
- GtkTreeModelFilter *filter;
-
g_return_val_if_fail (GTK_IS_TREE_MODEL (child_model), NULL);
- retval = g_object_new (GTK_TYPE_TREE_MODEL_FILTER,
- "child-model", child_model,
- "virtual-root", root,
- NULL);
-
- filter = GTK_TREE_MODEL_FILTER (retval);
- if (filter->priv->virtual_root)
- {
- gtk_tree_model_filter_ref_path (filter, filter->priv->virtual_root);
- filter->priv->virtual_root_deleted = FALSE;
- }
-
- return retval;
+ return g_object_new (GTK_TYPE_TREE_MODEL_FILTER,
+ "child-model", child_model,
+ "virtual-root", root,
+ NULL);
}
/**