* Boston, MA 02111-1307, USA.
*/
+#include <string.h>
+#include <gobject/gvaluecollector.h>
#include "gtktreemodel.h"
#include "gtktreestore.h"
#include "gtktreedatalist.h"
#include "gtktreednd.h"
-#include <string.h>
-#include <gobject/gvaluecollector.h>
#define G_NODE(node) ((GNode *)node)
#define GTK_TREE_STORE_IS_SORTED(tree) (GTK_TREE_STORE (tree)->sort_column_id != -2)
static void gtk_tree_store_drag_dest_init (GtkTreeDragDestIface *iface);
static void gtk_tree_store_sortable_init (GtkTreeSortableIface *iface);
static void gtk_tree_store_finalize (GObject *object);
-static guint gtk_tree_store_get_flags (GtkTreeModel *tree_model);
+static GtkTreeModelFlags gtk_tree_store_get_flags (GtkTreeModel *tree_model);
static gint gtk_tree_store_get_n_columns (GtkTreeModel *tree_model);
static GType gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
gint index);
static void validate_gnode (GNode *node);
+static void gtk_tree_store_move (GtkTreeStore *tree_store,
+ GtkTreeIter *iter,
+ GtkTreeIter *position,
+ gboolean before);
+
static GObjectClass *parent_class = NULL;
}
}
-GtkType
+GType
gtk_tree_store_get_type (void)
{
static GType tree_store_type = 0;
NULL
};
- tree_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkTreeStore", &tree_store_info, 0);
+ tree_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkTreeStore",
+ &tree_store_info, 0);
g_type_add_interface_static (tree_store_type,
GTK_TYPE_TREE_MODEL,
g_return_val_if_fail (n_columns > 0, NULL);
- retval = GTK_TREE_STORE (g_object_new (GTK_TYPE_TREE_STORE, NULL));
+ retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
gtk_tree_store_set_n_columns (retval, n_columns);
va_start (args, n_columns);
GType type = va_arg (args, GType);
if (! _gtk_tree_data_list_check_type (type))
{
- g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n", G_STRLOC, g_type_name (type));
- g_object_unref (G_OBJECT (retval));
+ g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n",
+ G_STRLOC, g_type_name (type));
+ g_object_unref (retval);
return NULL;
}
gtk_tree_store_set_column_type (retval, i, type);
g_return_val_if_fail (n_columns > 0, NULL);
- retval = GTK_TREE_STORE (g_object_new (GTK_TYPE_TREE_STORE, NULL));
+ retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
gtk_tree_store_set_n_columns (retval, n_columns);
for (i = 0; i < n_columns; i++)
{
if (! _gtk_tree_data_list_check_type (types[i]))
{
- g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n", G_STRLOC, g_type_name (types[i]));
- g_object_unref (G_OBJECT (retval));
+ g_warning ("%s: Invalid type %s passed to gtk_tree_store_new_with_types\n",
+ G_STRLOC, g_type_name (types[i]));
+ g_object_unref (retval);
return NULL;
}
gtk_tree_store_set_column_type (retval, i, types[i]);
tree_store->column_headers[column] = type;
}
-static void
+static gboolean
node_free (GNode *node, gpointer data)
{
- _gtk_tree_data_list_free (node->data, (GType*)data);
+ if (node->data)
+ _gtk_tree_data_list_free (node->data, (GType*)data);
+ node->data = NULL;
+
+ return FALSE;
}
static void
{
GtkTreeStore *tree_store = GTK_TREE_STORE (object);
- g_node_children_foreach (tree_store->root, G_TRAVERSE_LEAFS, node_free, tree_store->column_headers);
+ g_node_traverse (tree_store->root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
+ node_free, tree_store->column_headers);
+ g_node_destroy (tree_store->root);
_gtk_tree_data_list_header_free (tree_store->sort_list);
g_free (tree_store->column_headers);
tree_store->default_sort_data = NULL;
}
+ /* must chain up */
(* parent_class->finalize) (object);
}
*/
-static guint
+static GtkTreeModelFlags
gtk_tree_store_get_flags (GtkTreeModel *tree_model)
{
g_return_val_if_fail (GTK_IS_TREE_STORE (tree_model), 0);
{
GtkTreeDataList *list;
GtkTreeDataList *prev;
- GtkTreePath *path = NULL;
+ gint old_column = column;
GValue real_value = {0, };
gboolean converted = FALSE;
gboolean retval = FALSE;
+ g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
+ g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
+ g_return_val_if_fail (column >= 0 && column < tree_store->n_columns, FALSE);
+ g_return_val_if_fail (G_IS_VALUE (value), FALSE);
+
if (! g_type_is_a (G_VALUE_TYPE (value), tree_store->column_headers[column]))
{
if (! (g_value_type_compatible (G_VALUE_TYPE (value), tree_store->column_headers[column]) &&
prev = list = G_NODE (iter->user_data)->data;
- path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
-
while (list != NULL)
{
if (column == 0)
else
_gtk_tree_data_list_value_to_node (list, value);
retval = TRUE;
- gtk_tree_path_free (path);
if (converted)
g_value_unset (&real_value);
return retval;
list->next = NULL;
column --;
}
+
if (converted)
_gtk_tree_data_list_value_to_node (list, &real_value);
else
_gtk_tree_data_list_value_to_node (list, value);
retval = TRUE;
- gtk_tree_path_free (path);
if (converted)
g_value_unset (&real_value);
+ if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
+ gtk_tree_store_sort_iter_changed (tree_store, iter, old_column);
+
return retval;
}
* Removes @iter from @tree_store. After being removed, @iter is set to the
* next valid row at that level, or invalidated if it previously pointed to the
* last one.
+ *
+ * Return value: %TRUE if @iter is still valid, %FALSE if not.
**/
-void
+gboolean
gtk_tree_store_remove (GtkTreeStore *tree_store,
GtkTreeIter *iter)
{
GNode *parent;
GNode *next_node;
- g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
- g_return_if_fail (VALID_ITER (iter, tree_store));
+ g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
+ g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
parent = G_NODE (iter->user_data)->parent;
next_node = G_NODE (iter->user_data)->next;
if (G_NODE (iter->user_data)->data)
- _gtk_tree_data_list_free ((GtkTreeDataList *) G_NODE (iter->user_data)->data,
- tree_store->column_headers);
+ g_node_traverse (G_NODE (iter->user_data), G_POST_ORDER, G_TRAVERSE_ALL,
+ -1, node_free, tree_store->column_headers);
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
g_node_destroy (G_NODE (iter->user_data));
{
iter->stamp = tree_store->stamp;
iter->user_data = next_node;
+ return TRUE;
}
else
{
iter->stamp = 0;
iter->user_data = NULL;
}
+
+ return FALSE;
}
/**
* @sibling: A valid #GtkTreeIter, or %NULL
*
* Inserts a new row before @sibling. If @sibling is %NULL, then the row will
- * be appended to the beginning of the @parent 's children. If @parent and
- * @sibling are %NULL, then the row will be appended to the toplevel. If both
- * @sibling and @parent are set, then @parent must be the parent of @sibling.
- * When @sibling is set, @parent is optional.
+ * be appended to @parent 's children. If @parent and @sibling are %NULL, then
+ * the row will be appended to the toplevel. If both @sibling and @parent are
+ * set, then @parent must be the parent of @sibling. When @sibling is set,
+ * @parent is optional.
*
* @iter will be changed to point to this new row. The row will be empty after
* this function is called. To fill in values, you need to call
* @sibling: A valid #GtkTreeIter, or %NULL
*
* Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
- * prepended to the beginning of the @parent 's children. If @parent and
- * @sibling are %NULL, then the row will be prepended to the toplevel. If both
- * @sibling and @parent are set, then @parent must be the parent of @sibling.
- * When @sibling is set, @parent is optional.
+ * prepended to @parent 's children. If @parent and @sibling are %NULL, then
+ * the row will be prepended to the toplevel. If both @sibling and @parent are
+ * set, then @parent must be the parent of @sibling. When @sibling is set,
+ * @parent is optional.
*
* @iter will be changed to point to this new row. The row will be empty after
* this function is called. To fill in values, you need to call
gtk_tree_store_clear_traverse (tree_store->root, tree_store);
}
+static gboolean
+gtk_tree_store_iter_is_valid_helper (GtkTreeIter *iter,
+ GNode *first)
+{
+ GNode *node;
+
+ node = first;
+
+ do
+ {
+ if (node == iter->user_data)
+ return TRUE;
+
+ if (node->children)
+ if (gtk_tree_store_iter_is_valid_helper (iter, node->children))
+ return TRUE;
+
+ node = node->next;
+ }
+ while (node);
+
+ return FALSE;
+}
+
+/**
+ * gtk_tree_store_iter_is_valid:
+ * @tree_store: A #GtkTreeStore.
+ * @iter: A #GtkTreeIter.
+ *
+ * WARNING: This function is slow. Only use it for debugging and/or testing
+ * purposes.
+ *
+ * Checks if the given iter is a valid iter for this #GtkTreeStore.
+ *
+ * Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
+ **/
+gboolean
+gtk_tree_store_iter_is_valid (GtkTreeStore *tree_store,
+ GtkTreeIter *iter)
+{
+ g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
+ g_return_val_if_fail (iter != NULL, FALSE);
+
+ if (!VALID_ITER (iter, tree_store))
+ return FALSE;
+
+ return gtk_tree_store_iter_is_valid_helper (iter, tree_store->root);
+}
+
/* DND */
prev))
{
GtkTreeIter tmp_iter = dest_iter;
- gtk_tree_store_insert_after (GTK_TREE_STORE (tree_model),
- &dest_iter,
- NULL,
- &tmp_iter);
+
+ if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_model), "gtk-tree-model-drop-append")))
+ {
+ GtkTreeIter parent;
+
+ if (gtk_tree_model_iter_parent (GTK_TREE_MODEL (tree_model), &parent, &tmp_iter))
+ gtk_tree_store_append (GTK_TREE_STORE (tree_model),
+ &dest_iter, &parent);
+ else
+ gtk_tree_store_append (GTK_TREE_STORE (tree_model),
+ &dest_iter, NULL);
+ }
+ else
+ gtk_tree_store_insert_after (GTK_TREE_STORE (tree_model),
+ &dest_iter,
+ NULL,
+ &tmp_iter);
retval = TRUE;
}
}
+ g_object_set_data (G_OBJECT (tree_model), "gtk-tree-model-drop-append",
+ NULL);
+
gtk_tree_path_free (prev);
/* If we succeeded in creating dest_iter, walk src_iter tree branch,
GtkTreePath *tmp = NULL;
gboolean retval = FALSE;
+ g_return_val_if_fail (GTK_IS_TREE_STORE (drag_dest), FALSE);
+
+ /* don't accept drops if the tree has been sorted */
+ if (GTK_TREE_STORE_IS_SORTED (drag_dest))
+ return FALSE;
+
if (!gtk_tree_get_row_drag_data (selection_data,
&src_model,
&src_path))
return retval;
}
-/* Sorting */
+/* Sorting and reordering */
typedef struct _SortTuple
{
gint offset;
GNode *node;
} SortTuple;
+/* Reordering */
+static gint
+gtk_tree_store_reorder_func (gconstpointer a,
+ gconstpointer b,
+ gpointer user_data)
+{
+ SortTuple *a_reorder;
+ SortTuple *b_reorder;
+
+ a_reorder = (SortTuple *)a;
+ b_reorder = (SortTuple *)b;
+
+ if (a_reorder->offset < b_reorder->offset)
+ return -1;
+ if (a_reorder->offset > b_reorder->offset)
+ return 1;
+
+ return 0;
+}
+
+/**
+ * gtk_tree_store_reorder:
+ * @tree_store: A #GtkTreeStore.
+ * @parent: A #GtkTreeIter.
+ * @new_order: An integer array indication the new order for the list.
+ *
+ * Reorders the children of @parent in @tree_store to follow the order
+ * indicated by @new_order. Note that this function only works with
+ * unsorted stores.
+ **/
+void
+gtk_tree_store_reorder (GtkTreeStore *tree_store,
+ GtkTreeIter *parent,
+ gint *new_order)
+{
+ gint i, length = 0;
+ GNode *level, *node;
+ GtkTreePath *path;
+ SortTuple *sort_array;
+
+ g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
+ g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
+ g_return_if_fail (parent == NULL || VALID_ITER (parent, tree_store));
+ g_return_if_fail (new_order != NULL);
+
+ if (!parent)
+ level = G_NODE (tree_store->root)->children;
+ else
+ level = G_NODE (parent->user_data)->children;
+
+ /* count nodes */
+ node = level;
+ while (node)
+ {
+ length++;
+ node = node->next;
+ }
+
+ /* set up sortarray */
+ sort_array = g_new (SortTuple, length);
+
+ node = level;
+ for (i = 0; i < length; i++)
+ {
+ sort_array[i].offset = new_order[i];
+ sort_array[i].node = node;
+
+ node = node->next;
+ }
+
+ g_qsort_with_data (sort_array,
+ length,
+ sizeof (SortTuple),
+ gtk_tree_store_reorder_func,
+ NULL);
+
+ /* fix up level */
+ for (i = 0; i < length - 1; i++)
+ {
+ sort_array[i].node->next = sort_array[i+1].node;
+ sort_array[i+1].node->prev = sort_array[i].node;
+ }
+
+ sort_array[length-1].node->next = NULL;
+ sort_array[0].node->prev = NULL;
+ if (parent)
+ G_NODE (parent->user_data)->children = sort_array[0].node;
+ else
+ G_NODE (tree_store->root)->children = sort_array[0].node;
+
+ /* emit signal */
+ if (parent)
+ path = gtk_tree_model_get_path (GTK_TREE_MODEL (tree_store), parent);
+ else
+ path = gtk_tree_path_new ();
+ gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path,
+ parent, new_order);
+ gtk_tree_path_free (path);
+ g_free (sort_array);
+}
+
+/**
+ * gtk_tree_store_swap:
+ * @tree_store: A #GtkTreeStore.
+ * @a: A #GtkTreeIter.
+ * @b: Another #GtkTreeIter.
+ *
+ * Swaps @a and @b in the same level of @tree_store. Note that this function
+ * only works with unsorted stores.
+ **/
+void
+gtk_tree_store_swap (GtkTreeStore *tree_store,
+ GtkTreeIter *a,
+ GtkTreeIter *b)
+{
+ GNode *tmp, *node_a, *node_b, *parent_node;
+ GNode *a_prev, *a_next, *b_prev, *b_next;
+ gint i, a_count, b_count, length, *order;
+ GtkTreePath *path_a, *path_b;
+ GtkTreeIter parent;
+
+ g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
+ g_return_if_fail (VALID_ITER (a, tree_store));
+ g_return_if_fail (VALID_ITER (b, tree_store));
+
+ node_a = G_NODE (a->user_data);
+ node_b = G_NODE (b->user_data);
+
+ /* basic sanity checking */
+ if (node_a == node_b)
+ return;
+
+ path_a = gtk_tree_model_get_path (GTK_TREE_MODEL (tree_store), a);
+ path_b = gtk_tree_model_get_path (GTK_TREE_MODEL (tree_store), b);
+
+ g_return_if_fail (path_a && path_b);
+
+ gtk_tree_path_up (path_a);
+ gtk_tree_path_up (path_b);
+
+ if (gtk_tree_path_compare (path_a, path_b))
+ {
+ gtk_tree_path_free (path_a);
+ gtk_tree_path_free (path_b);
+
+ g_warning ("Given childs are not in the same level\n");
+ return;
+ }
+
+ gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_store), &parent, path_a);
+ parent_node = G_NODE (parent.user_data);
+
+ gtk_tree_path_free (path_b);
+
+ /* old links which we have to keep around */
+ a_prev = node_a->prev;
+ a_next = node_a->next;
+
+ b_prev = node_b->prev;
+ b_next = node_b->next;
+
+ /* fix up links if the nodes are next to eachother */
+ if (a_prev == node_b)
+ a_prev = node_a;
+ if (a_next == node_b)
+ a_next = node_a;
+
+ if (b_prev == node_a)
+ b_prev = node_b;
+ if (b_next == node_a)
+ b_next = node_b;
+
+ /* counting nodes */
+ tmp = parent_node->children;
+ i = a_count = b_count = 0;
+ while (tmp)
+ {
+ if (tmp == node_a)
+ a_count = i;
+ if (tmp == node_b)
+ b_count = i;
+
+ tmp = tmp->next;
+ i++;
+ }
+ length = i;
+
+ /* hacking the tree */
+ if (!a_prev)
+ parent_node->children = node_b;
+ else
+ a_prev->next = node_b;
+
+ if (a_next)
+ a_next->prev = node_b;
+
+ if (!b_prev)
+ parent_node->children = node_a;
+ else
+ b_prev->next = node_a;
+
+ if (b_next)
+ b_next->prev = node_a;
+
+ node_a->prev = b_prev;
+ node_a->next = b_next;
+
+ node_b->prev = a_prev;
+ node_b->next = a_next;
+
+ /* emit signal */
+ order = g_new (gint, length);
+ for (i = 0; i < length; i++)
+ if (i == a_count)
+ order[i] = b_count;
+ else if (i == b_count)
+ order[i] = a_count;
+ else
+ order[i] = i;
+
+ gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path_a,
+ &parent, order);
+ gtk_tree_path_free (path_a);
+ g_free (order);
+}
+
+/* WARNING: this function is *incredibly* fragily. Please smashtest after
+ * making changes here.
+ * -Kris
+ */
+static void
+gtk_tree_store_move (GtkTreeStore *tree_store,
+ GtkTreeIter *iter,
+ GtkTreeIter *position,
+ gboolean before)
+{
+ GNode *parent, *node, *a, *b, *tmp, *tmp_a, *tmp_b;
+ gint old_pos, new_pos, length, i, *order;
+ GtkTreePath *path = NULL, *tmppath, *pos_path = NULL;
+ GtkTreeIter parent_iter, dst_a, dst_b;
+ gint depth = 0;
+ gboolean handle_b = TRUE;
+
+ g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
+ g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
+ g_return_if_fail (VALID_ITER (iter, tree_store));
+ if (position)
+ g_return_if_fail (VALID_ITER (position, tree_store));
+
+ a = b = NULL;
+
+ /* sanity checks */
+ if (position)
+ {
+ path = gtk_tree_model_get_path (GTK_TREE_MODEL (tree_store), iter);
+ pos_path = gtk_tree_model_get_path (GTK_TREE_MODEL (tree_store),
+ position);
+
+ /* if before:
+ * moving the iter before path or "path + 1" doesn't make sense
+ * else
+ * moving the iter before path or "path - 1" doesn't make sense
+ */
+ if (!gtk_tree_path_compare (path, pos_path))
+ goto free_paths_and_out;
+
+ if (before)
+ gtk_tree_path_next (path);
+ else
+ gtk_tree_path_prev (path);
+
+ if (!gtk_tree_path_compare (path, pos_path))
+ goto free_paths_and_out;
+
+ if (before)
+ gtk_tree_path_prev (path);
+ else
+ gtk_tree_path_next (path);
+
+ if (gtk_tree_path_get_depth (path) != gtk_tree_path_get_depth (pos_path))
+ {
+ g_warning ("Given childs are not in the same level\n");
+
+ goto free_paths_and_out;
+ }
+
+ tmppath = gtk_tree_path_copy (pos_path);
+ gtk_tree_path_up (path);
+ gtk_tree_path_up (tmppath);
+
+ if (gtk_tree_path_get_depth (path) > 0 &&
+ gtk_tree_path_compare (path, tmppath))
+ {
+ g_warning ("Given childs are not in the same level\n");
+
+ gtk_tree_path_free (tmppath);
+ goto free_paths_and_out;
+ }
+
+ gtk_tree_path_free (tmppath);
+ }
+
+ if (!path)
+ {
+ path = gtk_tree_model_get_path (GTK_TREE_MODEL (tree_store), iter);
+ gtk_tree_path_up (path);
+ }
+
+ depth = gtk_tree_path_get_depth (path);
+
+ if (depth)
+ {
+ gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_store), &parent_iter, path);
+ gtk_tree_path_free (path);
+
+ parent = G_NODE (parent_iter.user_data);
+ }
+ else
+ parent = G_NODE (tree_store->root);
+
+ /* yes, I know that this can be done shorter, but I'm doing it this way
+ * so the code is also maintainable
+ */
+
+ if (before && position)
+ {
+ b = G_NODE (position->user_data);
+
+ if (gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path) - 1] > 0)
+ {
+ gtk_tree_path_prev (pos_path);
+ if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_store), &dst_a, pos_path))
+ a = G_NODE (dst_a.user_data);
+ else
+ a = NULL;
+ gtk_tree_path_next (pos_path);
+ }
+
+ /* if b is NULL, a is NULL too -- we are at the beginning of the list
+ * yes and we leak memory here ...
+ */
+ g_return_if_fail (b);
+ }
+ else if (before && !position)
+ {
+ /* move before without position is appending */
+ a = NULL;
+ b = NULL;
+ }
+ else /* !before */
+ {
+ if (position)
+ a = G_NODE (position->user_data);
+ else
+ a = NULL;
+
+ if (position)
+ {
+ gtk_tree_path_next (pos_path);
+ if (gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_store), &dst_b, pos_path))
+ b = G_NODE (dst_b.user_data);
+ else
+ b = NULL;
+ gtk_tree_path_prev (pos_path);
+ }
+ else
+ {
+ /* move after without position is prepending */
+ if (depth)
+ gtk_tree_model_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
+ &parent_iter);
+ else
+ gtk_tree_model_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
+ NULL);
+
+ b = G_NODE (dst_b.user_data);
+ }
+
+ /* if a is NULL, a is NULL too -- we are at the end of the list
+ * yes and we leak memory here ...
+ */
+ if (position)
+ g_return_if_fail (a);
+ }
+
+ /* counting nodes */
+ tmp = parent->children;
+
+ length = old_pos = 0;
+ while (tmp)
+ {
+ if (tmp == iter->user_data)
+ old_pos = length;
+
+ tmp = tmp->next;
+ length++;
+ }
+
+ /* remove node from list */
+ node = G_NODE (iter->user_data);
+ tmp_a = node->prev;
+ tmp_b = node->next;
+
+ if (tmp_a)
+ tmp_a->next = tmp_b;
+ else
+ parent->children = tmp_b;
+
+ if (tmp_b)
+ tmp_b->prev = tmp_a;
+
+ /* and reinsert the node */
+ if (a)
+ {
+ tmp = a->next;
+
+ a->next = node;
+ node->next = tmp;
+ node->prev = a;
+ }
+ else if (!a && !before)
+ {
+ tmp = parent->children;
+
+ node->prev = NULL;
+ parent->children = node;
+
+ node->next = tmp;
+ tmp->prev = node;
+
+ handle_b = FALSE;
+ }
+ else if (!a && before)
+ {
+ if (!position)
+ {
+ node->parent = NULL;
+ node->next = node->prev = NULL;
+
+ /* before with sibling = NULL appends */
+ g_node_insert_before (parent, NULL, node);
+ }
+ else
+ {
+ node->parent = NULL;
+ node->next = node->prev = NULL;
+
+ /* after with sibling = NULL prepends */
+ g_node_insert_after (parent, NULL, node);
+ }
+ }
+
+ if (handle_b)
+ {
+ if (b)
+ {
+ tmp = b->prev;
+
+ b->prev = node;
+ node->prev = tmp;
+ node->next = b;
+ }
+ else if (!(!a && before)) /* !a && before is completely handled above */
+ node->next = NULL;
+ }
+
+ /* emit signal */
+ if (position)
+ new_pos = gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path)-1];
+ else if (before)
+ {
+ if (depth)
+ new_pos = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (tree_store),
+ &parent_iter) - 1;
+ else
+ new_pos = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (tree_store),
+ NULL) - 1;
+ }
+ else
+ new_pos = 0;
+
+ if (new_pos > old_pos)
+ {
+ if (before && position)
+ new_pos--;
+ }
+ else
+ {
+ if (!before && position)
+ new_pos++;
+ }
+
+ order = g_new (gint, length);
+ if (new_pos > old_pos)
+ {
+ for (i = 0; i < length; i++)
+ if (i < old_pos)
+ order[i] = i;
+ else if (i >= old_pos && i < new_pos)
+ order[i] = i + 1;
+ else if (i == new_pos)
+ order[i] = old_pos;
+ else
+ order[i] = i;
+ }
+ else
+ {
+ for (i = 0; i < length; i++)
+ if (i == new_pos)
+ order[i] = old_pos;
+ else if (i > new_pos && i <= old_pos)
+ order[i] = i - 1;
+ else
+ order[i] = i;
+ }
+
+ path = gtk_tree_path_new ();
+ gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
+ path, NULL, order);
+
+ for (i = 0; i < length; i++)
+ g_print ("%2d ", order[i]);
+ g_print ("\n");
+
+ gtk_tree_path_free (path);
+ if (position)
+ gtk_tree_path_free (pos_path);
+ g_free (order);
+
+ return;
+
+free_paths_and_out:
+ gtk_tree_path_free (path);
+ gtk_tree_path_free (pos_path);
+}
+
+/**
+ * gtk_tree_store_move_before:
+ * @tree_store: A #GtkTreeStore.
+ * @iter: A #GtkTreeIter.
+ * @position: A #GtkTreeIter or %NULL.
+ *
+ * Moves @iter in @tree_store to the position before @position. @iter and
+ * @position should be in the same level. Note that this function only
+ * works with unsorted stores. If @position is %NULL, @iter will be
+ * moved to the end of the level.
+ **/
+void
+gtk_tree_store_move_before (GtkTreeStore *tree_store,
+ GtkTreeIter *iter,
+ GtkTreeIter *position)
+{
+ gtk_tree_store_move (tree_store, iter, position, TRUE);
+}
+
+/**
+ * gtk_tree_store_move_after:
+ * @tree_store: A #GtkTreeStore.
+ * @iter: A #GtkTreeIter.
+ * @position: A #GtkTreeIter.
+ *
+ * Moves @iter in @tree_store to the position after @position. @iter and
+ * @position should be in the same level. Note that this function only
+ * works with unsorted stores. If @position is %NULL, @iter will be moved
+ * to the start of the level.
+ **/
+void
+gtk_tree_store_move_after (GtkTreeStore *tree_store,
+ GtkTreeIter *iter,
+ GtkTreeIter *position)
+{
+ gtk_tree_store_move (tree_store, iter, position, FALSE);
+}
+
+/* Sorting */
static gint
gtk_tree_store_compare_func (gconstpointer a,
gconstpointer b,
cmp_b = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
}
-
if (tree_store->order == GTK_SORT_DESCENDING)
{
if (cmp_a < 0)
prev->next = next;
else
node->parent->children = next;
+
if (next)
next->prev = prev;
else
{
G_NODE (iter->user_data)->next = G_NODE (iter->user_data)->parent->children;
+ G_NODE (iter->user_data)->next->prev = G_NODE (iter->user_data);
G_NODE (iter->user_data)->parent->children = G_NODE (iter->user_data);
}
for (list = tree_store->sort_list; list; list = list->next)
{
- header = (GtkTreeDataSortHeader*) list->data;
- if (header->sort_column_id == sort_column_id)
- break;
+ GtkTreeDataSortHeader *list_header;
+
+ list_header = (GtkTreeDataSortHeader*) list->data;
+ if (list_header->sort_column_id == sort_column_id)
+ {
+ header = list_header;
+ break;
+ }
}
if (header == NULL)