Commit 7a2e1974 authored by Jonathan Blandford's avatar Jonathan Blandford Committed by Jonathan Blandford

Add convertion functions to let you convert to and from relative path's

2000-11-20  Jonathan Blandford <jrb@redhat.com>
	* gtk/gtktreemodelsort.h: Add convertion functions to let you
	  convert to and from relative path's and iters.

	* gtk/treestoretest.c: move to having a sorted/unsorted view.  Start
	  testing sorting functions a bit better.  Currently broken -- will
	  fix in morning.
parent 18dd625f
2000-11-20 Jonathan Blandford <jrb@redhat.com>
* gtk/gtktreemodelsort.h: Add convertion functions to let you
convert to and from relative path's and iters.
* gtk/treestoretest.c: move to having a sorted/unsorted view. Start
testing sorting functions a bit better. Currently broken -- will
fix in morning.
2000-11-20 Havoc Pennington <hp@redhat.com>
* gtk/gtkwidget.c: Documented a bunch of functions in here
......
2000-11-20 Jonathan Blandford <jrb@redhat.com>
* gtk/gtktreemodelsort.h: Add convertion functions to let you
convert to and from relative path's and iters.
* gtk/treestoretest.c: move to having a sorted/unsorted view. Start
testing sorting functions a bit better. Currently broken -- will
fix in morning.
2000-11-20 Havoc Pennington <hp@redhat.com>
* gtk/gtkwidget.c: Documented a bunch of functions in here
......
2000-11-20 Jonathan Blandford <jrb@redhat.com>
* gtk/gtktreemodelsort.h: Add convertion functions to let you
convert to and from relative path's and iters.
* gtk/treestoretest.c: move to having a sorted/unsorted view. Start
testing sorting functions a bit better. Currently broken -- will
fix in morning.
2000-11-20 Havoc Pennington <hp@redhat.com>
* gtk/gtkwidget.c: Documented a bunch of functions in here
......
2000-11-20 Jonathan Blandford <jrb@redhat.com>
* gtk/gtktreemodelsort.h: Add convertion functions to let you
convert to and from relative path's and iters.
* gtk/treestoretest.c: move to having a sorted/unsorted view. Start
testing sorting functions a bit better. Currently broken -- will
fix in morning.
2000-11-20 Havoc Pennington <hp@redhat.com>
* gtk/gtkwidget.c: Documented a bunch of functions in here
......
2000-11-20 Jonathan Blandford <jrb@redhat.com>
* gtk/gtktreemodelsort.h: Add convertion functions to let you
convert to and from relative path's and iters.
* gtk/treestoretest.c: move to having a sorted/unsorted view. Start
testing sorting functions a bit better. Currently broken -- will
fix in morning.
2000-11-20 Havoc Pennington <hp@redhat.com>
* gtk/gtkwidget.c: Documented a bunch of functions in here
......
2000-11-20 Jonathan Blandford <jrb@redhat.com>
* gtk/gtktreemodelsort.h: Add convertion functions to let you
convert to and from relative path's and iters.
* gtk/treestoretest.c: move to having a sorted/unsorted view. Start
testing sorting functions a bit better. Currently broken -- will
fix in morning.
2000-11-20 Havoc Pennington <hp@redhat.com>
* gtk/gtkwidget.c: Documented a bunch of functions in here
......
2000-11-20 Jonathan Blandford <jrb@redhat.com>
* gtk/gtktreemodelsort.h: Add convertion functions to let you
convert to and from relative path's and iters.
* gtk/treestoretest.c: move to having a sorted/unsorted view. Start
testing sorting functions a bit better. Currently broken -- will
fix in morning.
2000-11-20 Havoc Pennington <hp@redhat.com>
* gtk/gtkwidget.c: Documented a bunch of functions in here
......
......@@ -19,8 +19,8 @@
/* NOTE: There is a potential for confusion in this code as to whether an iter,
* path or value refers to the GtkTreeModelSort model, or the model being
* sorted. As a convention, variables referencing the sorted model will have an
* path or value refers to the GtkTreeModelSort model, or the child model being
* sorted. As a convention, variables referencing the child model will have an
* s_ prefix before them (ie. s_iter, s_value, s_path);
*/
......@@ -228,14 +228,14 @@ gtk_tree_model_sort_new (void)
}
GtkTreeModel *
gtk_tree_model_sort_new_with_model (GtkTreeModel *model,
gtk_tree_model_sort_new_with_model (GtkTreeModel *child_model,
GValueCompareFunc func,
gint sort_col)
{
GtkTreeModel *retval;
retval = gtk_tree_model_sort_new ();
gtk_tree_model_sort_set_model (GTK_TREE_MODEL_SORT (retval), model);
gtk_tree_model_sort_set_model (GTK_TREE_MODEL_SORT (retval), child_model);
GTK_TREE_MODEL_SORT (retval)->func = func;
GTK_TREE_MODEL_SORT (retval)->sort_col = sort_col;
......@@ -245,79 +245,78 @@ gtk_tree_model_sort_new_with_model (GtkTreeModel *model,
/**
* gtk_tree_model_sort_set_model:
* @tree_model_sort: The #GtkTreeModelSort.
* @model: A #GtkTreeModel, or NULL.
* @child_model: A #GtkTreeModel, or NULL.
*
* Sets the model of @tree_model_sort to be @model. If @model is NULL, then the
* old model is unset.
**/
void
gtk_tree_model_sort_set_model (GtkTreeModelSort *tree_model_sort,
GtkTreeModel *model)
GtkTreeModel *child_model)
{
g_return_if_fail (tree_model_sort != NULL);
g_return_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model_sort));
if (model)
g_object_ref (G_OBJECT (model));
if (child_model)
g_object_ref (G_OBJECT (child_model));
if (tree_model_sort->model)
if (tree_model_sort->child_model)
{
gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->model),
gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->child_model),
gtk_tree_model_sort_changed,
tree_model_sort);
gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->model),
gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->child_model),
gtk_tree_model_sort_inserted,
tree_model_sort);
gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->model),
gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->child_model),
gtk_tree_model_sort_child_toggled,
tree_model_sort);
gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->model),
gtk_signal_disconnect_by_func (GTK_OBJECT (tree_model_sort->child_model),
gtk_tree_model_sort_deleted,
tree_model_sort);
g_object_unref (G_OBJECT (tree_model_sort->model));
g_object_unref (G_OBJECT (tree_model_sort->child_model));
}
tree_model_sort->model = model;
tree_model_sort->child_model = child_model;
if (model)
if (child_model)
{
gtk_signal_connect (GTK_OBJECT (model),
gtk_signal_connect (GTK_OBJECT (child_model),
"changed",
gtk_tree_model_sort_changed,
tree_model_sort);
gtk_signal_connect (GTK_OBJECT (model),
gtk_signal_connect (GTK_OBJECT (child_model),
"inserted",
gtk_tree_model_sort_inserted,
tree_model_sort);
gtk_signal_connect (GTK_OBJECT (model),
gtk_signal_connect (GTK_OBJECT (child_model),
"child_toggled",
gtk_tree_model_sort_child_toggled,
tree_model_sort);
gtk_signal_connect (GTK_OBJECT (model),
gtk_signal_connect (GTK_OBJECT (child_model),
"deleted",
gtk_tree_model_sort_deleted,
tree_model_sort);
tree_model_sort->flags = gtk_tree_model_get_flags (model);
tree_model_sort->flags = gtk_tree_model_get_flags (child_model);
}
}
/**
* gtk_tree_model_sort_convert_path:
* @tree_model_sort: The #GtkTreeModelSort.
* @path: A #GtkTreePath, relative to the @tree_model_sort 's model.
* @path: A #GtkTreePath, relative to the child model.
*
* Converts the @path to a new path, relative to the sorted position. In other
* words, the value found in the @tree_model_sort ->model at the @path, is
* words, the value found in the @tree_model_sort ->child_model at the @path, is
* identical to that found in the @tree_model_sort and the return value.
*
* Return value: A new path, or NULL if @path does not exist in @tree_model_sort
* ->model.
* ->child_model.
**/
GtkTreePath *
gtk_tree_model_sort_convert_path (GtkTreeModelSort *tree_model_sort,
GtkTreePath *path)
GtkTreePath *child_path)
{
GtkTreePath *retval;
GArray *array;
......@@ -329,7 +328,7 @@ gtk_tree_model_sort_convert_path (GtkTreeModelSort *tree_model_sort,
retval = gtk_tree_path_new ();
array = (GArray *) tree_model_sort->root;
indices = gtk_tree_path_get_indices (path);
indices = gtk_tree_path_get_indices (child_path);
do
{
......@@ -339,7 +338,7 @@ gtk_tree_model_sort_convert_path (GtkTreeModelSort *tree_model_sort,
if ((array->len < indices[i]) || (array == NULL))
{
gtk_tree_path_free (path);
gtk_tree_path_free (retval);
return NULL;
}
......@@ -354,7 +353,7 @@ gtk_tree_model_sort_convert_path (GtkTreeModelSort *tree_model_sort,
}
if (! found)
{
gtk_tree_path_free (path);
gtk_tree_path_free (retval);
return NULL;
}
......@@ -363,7 +362,7 @@ gtk_tree_model_sort_convert_path (GtkTreeModelSort *tree_model_sort,
gtk_tree_model_sort_build_level (tree_model_sort, elt);
i++;
}
while (i < gtk_tree_path_get_depth (path));
while (i < gtk_tree_path_get_depth (child_path));
return retval;
}
......@@ -376,7 +375,7 @@ gtk_tree_model_sort_finalize (GObject *object)
if (tree_model_sort->root)
gtk_tree_model_sort_free_level (tree_model_sort->root);
g_object_unref (G_OBJECT (tree_model_sort->model));
g_object_unref (G_OBJECT (tree_model_sort->child_model));
}
static void
......@@ -458,7 +457,7 @@ gtk_tree_model_sort_insert_value (GtkTreeModelSort *sort,
func = sort->func;
else
{
switch (gtk_tree_model_get_column_type (sort->model, sort->sort_col))
switch (gtk_tree_model_get_column_type (sort->child_model, sort->sort_col))
{
case G_TYPE_STRING:
func = &g_value_string_compare_func;
......@@ -469,12 +468,12 @@ gtk_tree_model_sort_insert_value (GtkTreeModelSort *sort,
default:
g_warning ("No comparison function for row %d (Type %s)\n",
sort->sort_col,
g_type_name (gtk_tree_model_get_column_type (sort->model, sort->sort_col)));
g_type_name (gtk_tree_model_get_column_type (sort->child_model, sort->sort_col)));
return;
}
}
gtk_tree_model_get_value (sort->model, s_iter, sort->sort_col, &s_value);
gtk_tree_model_get_value (sort->child_model, s_iter, sort->sort_col, &s_value);
#if 0
/* FIXME: we can, as we are an array, do binary search to find the correct
......@@ -490,7 +489,7 @@ gtk_tree_model_sort_insert_value (GtkTreeModelSort *sort,
{
gint cmp;
tmp_elt = &(g_array_index (array, SortElt, middle));
gtk_tree_model_get_value (sort->model,
gtk_tree_model_get_value (sort->child_model,
(GtkTreeIter *) tmp_elt,
sort->sort_col,
&tmp_value);
......@@ -512,7 +511,7 @@ gtk_tree_model_sort_insert_value (GtkTreeModelSort *sort,
gint cmp;
tmp_elt = &(g_array_index (array, SortElt, middle));
gtk_tree_model_get_value (sort->model,
gtk_tree_model_get_value (sort->child_model,
(GtkTreeIter *) tmp_elt,
sort->sort_col,
&tmp_value);
......@@ -673,9 +672,9 @@ static gint
gtk_tree_model_sort_get_n_columns (GtkTreeModel *tree_model)
{
g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), 0);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, 0);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, 0);
return gtk_tree_model_get_n_columns (GTK_TREE_MODEL_SORT (tree_model)->model);
return gtk_tree_model_get_n_columns (GTK_TREE_MODEL_SORT (tree_model)->child_model);
}
static GType
......@@ -683,9 +682,9 @@ gtk_tree_model_sort_get_column_type (GtkTreeModel *tree_model,
gint index)
{
g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), G_TYPE_INVALID);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, G_TYPE_INVALID);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, G_TYPE_INVALID);
return gtk_tree_model_get_column_type (GTK_TREE_MODEL_SORT (tree_model)->model, index);
return gtk_tree_model_get_column_type (GTK_TREE_MODEL_SORT (tree_model)->child_model, index);
}
static gboolean
......@@ -719,7 +718,7 @@ gtk_tree_model_sort_get_iter_helper (GtkTreeModelSort *tree_model_sort,
depth + 1,
path);
if (gtk_tree_model_iter_has_child (tree_model_sort->model,
if (gtk_tree_model_iter_has_child (tree_model_sort->child_model,
&(elt->iter)))
gtk_tree_model_sort_build_level (tree_model_sort, elt);
......@@ -737,7 +736,7 @@ gtk_tree_model_sort_get_iter (GtkTreeModel *tree_model,
GtkTreePath *path)
{
g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, FALSE);
if (GTK_TREE_MODEL_SORT (tree_model)->root == NULL)
gtk_tree_model_sort_build_level (GTK_TREE_MODEL_SORT (tree_model), NULL);
......@@ -752,9 +751,9 @@ gtk_tree_model_sort_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), NULL);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, NULL);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, NULL);
return gtk_tree_model_get_path (GTK_TREE_MODEL_SORT (tree_model)->model, iter);
return gtk_tree_model_get_path (GTK_TREE_MODEL_SORT (tree_model)->child_model, iter);
}
static void
......@@ -766,12 +765,12 @@ gtk_tree_model_sort_get_value (GtkTreeModel *tree_model,
SortElt *elt;
g_return_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model));
g_return_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL);
g_return_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL);
g_return_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp);
elt = iter->tree_node;
gtk_tree_model_get_value (GTK_TREE_MODEL_SORT (tree_model)->model, (GtkTreeIter *)elt, column, value);
gtk_tree_model_get_value (GTK_TREE_MODEL_SORT (tree_model)->child_model, (GtkTreeIter *)elt, column, value);
}
static gboolean
......@@ -782,7 +781,7 @@ gtk_tree_model_sort_iter_next (GtkTreeModel *tree_model,
SortElt *elt;
g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp, FALSE);
elt = iter->tree_node;
......@@ -805,7 +804,7 @@ gtk_tree_model_sort_iter_children (GtkTreeModel *tree_model,
SortElt *elt;
g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, FALSE);
if (parent)
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == parent->stamp, FALSE);
......@@ -818,7 +817,7 @@ gtk_tree_model_sort_iter_children (GtkTreeModel *tree_model,
return FALSE;
if (elt->children == NULL &&
gtk_tree_model_iter_has_child (GTK_TREE_MODEL_SORT (tree_model)->model, (GtkTreeIter *)elt))
gtk_tree_model_iter_has_child (GTK_TREE_MODEL_SORT (tree_model)->child_model, (GtkTreeIter *)elt))
gtk_tree_model_sort_build_level (GTK_TREE_MODEL_SORT (tree_model), elt);
if (elt->children == NULL)
......@@ -837,14 +836,14 @@ gtk_tree_model_sort_iter_has_child (GtkTreeModel *tree_model,
SortElt *elt;
g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp, FALSE);
elt = iter->tree_node;
if (elt->children)
return TRUE;
return gtk_tree_model_iter_has_child (GTK_TREE_MODEL_SORT (tree_model)->model, (GtkTreeIter *) elt);
return gtk_tree_model_iter_has_child (GTK_TREE_MODEL_SORT (tree_model)->child_model, (GtkTreeIter *) elt);
}
static gint
......@@ -854,14 +853,14 @@ gtk_tree_model_sort_iter_n_children (GtkTreeModel *tree_model,
SortElt *elt;
g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), 0);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, 0);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, 0);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == iter->stamp, 0);
elt = iter->tree_node;
if (elt->children)
return elt->children->len;
return gtk_tree_model_iter_n_children (GTK_TREE_MODEL_SORT (tree_model)->model, (GtkTreeIter *) elt);
return gtk_tree_model_iter_n_children (GTK_TREE_MODEL_SORT (tree_model)->child_model, (GtkTreeIter *) elt);
}
......@@ -874,7 +873,7 @@ gtk_tree_model_sort_iter_nth_child (GtkTreeModel *tree_model,
SortElt *elt;
g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, FALSE);
if (parent)
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == parent->stamp, FALSE);
......@@ -883,7 +882,7 @@ gtk_tree_model_sort_iter_nth_child (GtkTreeModel *tree_model,
if (elt->children == NULL)
{
if (gtk_tree_model_iter_has_child (GTK_TREE_MODEL_SORT (tree_model)->model, (GtkTreeIter *)elt))
if (gtk_tree_model_iter_has_child (GTK_TREE_MODEL_SORT (tree_model)->child_model, (GtkTreeIter *)elt))
gtk_tree_model_sort_build_level (GTK_TREE_MODEL_SORT (tree_model), elt);
else
return FALSE;
......@@ -909,7 +908,7 @@ gtk_tree_model_sort_iter_parent (GtkTreeModel *tree_model,
SortElt *elt;
g_return_val_if_fail (GTK_IS_TREE_MODEL_SORT (tree_model), FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->model != NULL, FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->child_model != NULL, FALSE);
g_return_val_if_fail (GTK_TREE_MODEL_SORT (tree_model)->stamp == child->stamp, FALSE);
elt = iter->tree_node;
......@@ -951,7 +950,7 @@ gtk_tree_model_sort_build_level (GtkTreeModelSort *tree_model_sort,
parent_iter = & (place->iter);
n = gtk_tree_model_iter_n_children (tree_model_sort->model, parent_iter);
n = gtk_tree_model_iter_n_children (tree_model_sort->child_model, parent_iter);
if (n == 0)
return;
......@@ -963,7 +962,7 @@ gtk_tree_model_sort_build_level (GtkTreeModelSort *tree_model_sort,
else
tree_model_sort->root = children;
gtk_tree_model_iter_children (tree_model_sort->model,
gtk_tree_model_iter_children (tree_model_sort->child_model,
&iter,
parent_iter);
......@@ -978,7 +977,7 @@ gtk_tree_model_sort_build_level (GtkTreeModelSort *tree_model_sort,
g_array_append_vals (children, &elt, 1);
i++;
}
while (gtk_tree_model_iter_next (tree_model_sort->model, &iter));
while (gtk_tree_model_iter_next (tree_model_sort->child_model, &iter));
}
......
......@@ -46,7 +46,7 @@ struct _GtkTreeModelSort
gpointer root;
gint stamp;
guint flags;
GtkTreeModel *model;
GtkTreeModel *child_model;
gint sort_col;
GValueCompareFunc func;
};
......@@ -73,7 +73,7 @@ struct _GtkTreeModelSortClass
GtkType gtk_tree_model_sort_get_type (void);
GtkTreeModel *gtk_tree_model_sort_new (void);
GtkTreeModel *gtk_tree_model_sort_new_with_model (GtkTreeModel *model,
GtkTreeModel *gtk_tree_model_sort_new_with_model (GtkTreeModel *child_model,
GValueCompareFunc func,
gint sort_col);
void gtk_tree_model_sort_set_model (GtkTreeModelSort *tree_model_sort,
......@@ -84,7 +84,10 @@ void gtk_tree_model_sort_set_compare (GtkTreeModelSort *tree_model_
GValueCompareFunc *func);
void gtk_tree_model_sort_resort (GtkTreeModelSort *tree_model_sort);
GtkTreePath *gtk_tree_model_sort_convert_path (GtkTreeModelSort *tree_model_sort,
GtkTreePath *path);
GtkTreePath *child_path);
void gtk_tree_model_sort_convert_iter (GtkTreeModelSort *tree_model_sort,
GtkTreeIter *sort_iter,
GtkTreeIter *child_iter);
#ifdef __cplusplus
}
......
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
GtkTreeStore *model;
GtkTreeStore *base_model;
static void
selection_changed (GtkTreeSelection *selection,
......@@ -17,23 +18,35 @@ static void
node_set (GtkTreeIter *iter)
{
static gint i = 0;
gchar *str;
str = g_strdup_printf ("FOO: %d", i++);
gtk_tree_store_set (model, iter, 0, str, -1);
gtk_tree_store_set (base_model, iter, 0, str, -1);
g_free (str);
str = g_strdup_printf ("%d", g_random_int ());
gtk_tree_store_set (base_model, iter, 1, str, -1);
g_free (str);
}
static void
iter_remove (GtkWidget *button, GtkTreeView *tree_view)
{
GtkTreeIter selected;
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
GtkTreeIter s_selected;
GtkTreeModel *model;
model = gtk_tree_view_get_model (tree_view);
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (tree_view),
NULL,
&selected))
{
gtk_tree_store_remove (model, &selected);
if (GTK_IS_TREE_STORE (model))
{
gtk_tree_store_remove (GTK_TREE_STORE (model), &selected);
}
}
}
......@@ -43,6 +56,7 @@ iter_insert (GtkWidget *button, GtkTreeView *tree_view)
GtkWidget *entry;
GtkTreeIter iter;
GtkTreeIter selected;
GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
entry = gtk_object_get_user_data (GTK_OBJECT (button));
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
......@@ -70,6 +84,7 @@ iter_insert_before (GtkWidget *button, GtkTreeView *tree_view)
{
GtkTreeIter iter;
GtkTreeIter selected;
GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
NULL,
......@@ -96,6 +111,7 @@ iter_insert_after (GtkWidget *button, GtkTreeView *tree_view)
{
GtkTreeIter iter;
GtkTreeIter selected;
GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
NULL,
......@@ -122,23 +138,29 @@ iter_prepend (GtkWidget *button, GtkTreeView *tree_view)
{
GtkTreeIter iter;
GtkTreeIter selected;
GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
NULL,
&selected))
if (gtk_tree_selection_get_selected (selection, NULL, &selected))
{
gtk_tree_store_prepend (model,
&iter,
&selected);
if (GTK_IS_TREE_STORE (model))
{
gtk_tree_store_prepend (GTK_TREE_STORE (model),
&iter,
&selected);
node_set (&iter);
}
}
else
{
gtk_tree_store_prepend (model,
&iter,
NULL);
if (GTK_IS_TREE_STORE (model))
{
gtk_tree_store_prepend (GTK_TREE_STORE (model),
&iter,
NULL);
node_set (&iter);
}
}
node_set (&iter);
}
static void
......@@ -146,25 +168,31 @@ iter_append (GtkWidget *button, GtkTreeView *tree_view)
{
GtkTreeIter iter;
GtkTreeIter selected;
GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
NULL,
&selected))
{
gtk_tree_store_append (model, &iter, &selected);
if (GTK_IS_TREE_STORE (model))
{
gtk_tree_store_append (model, &iter, &selected);
node_set (&iter);
}
}
else
{
gtk_tree_store_append (model, &iter, NULL);
if (GTK_IS_TREE_STORE (model))
{
gtk_tree_store_append (model, &iter, NULL);
node_set (&iter);
}
}
node_set (&iter);
}
static void
make_window ()
make_window (gboolean use_sort)
{
GtkTreeModel *sort_model;
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *hbox, *entry;
......@@ -177,13 +205,28 @@ make_window ()
/* Make the Widgets/Objects */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
if (use_sort)
gtk_window_set_title (GTK_WINDOW (window), "Sorted list");
else
gtk_window_set_title (GTK_WINDOW (window), "Unsorted list");
vbox = gtk_vbox_new (FALSE, 8);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
gtk_window_set_default_size (GTK_WINDOW (window), 300, 350);
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
sort_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (model),
NULL, 0);
tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (sort_model));
if (use_sort)
{
GtkTreeModel *sort_model;
sort_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (base_model),
NULL, 0);
tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (sort_model));
}
else
{
tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (base_model));
}
selection = GTK_OBJECT (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)));
gtk_tree_selection_set_type (GTK_TREE_SELECTION (selection), GTK_TREE_SELECTION_SINGLE);
......@@ -244,14 +287,20 @@ make_window ()
/* The selected column */
cell = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("nodes", cell, "text", 0, NULL);
column = gtk_tree_view_column_new_with_attributes ("Node ID", cell, "text", 0, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
/* A few to start */
iter_prepend (NULL, GTK_TREE_VIEW (tree_view));
iter_prepend (NULL, GTK_TREE_VIEW (tree_view));
iter_prepend (NULL, GTK_TREE_VIEW (tree_view));
cell = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Random Number", cell, "text", 1, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
/* A few to start */
if (!use_sort)
{
iter_prepend (NULL, GTK_TREE_VIEW (tree_view));
iter_prepend (NULL, GTK_TREE_VIEW (tree_view));
iter_prepend (NULL, GTK_TREE_VIEW (tree_view));
}
/* Show it all */
gtk_widget_show_all (window);
}
......@@ -261,10 +310,11 @@ main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
model = gtk_tree_store_new_with_types (2, G_TYPE_STRING, G_TYPE_STRING);
make_window ();
make_window ();
base_model = gtk_tree_store_new_with_types (2, G_TYPE_STRING, G_TYPE_STRING);
/* FIXME: reverse this */
make_window (FALSE);
make_window (TRUE);
gtk_main ();
......
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
GtkTreeStore *model;
GtkTreeStore *base_model;
static void
selection_changed (GtkTreeSelection *selection,
......@@ -17,23 +18,35 @@ static void
node_set (GtkTreeIter *iter)
{
static gint i = 0;
gchar *str;
str = g_strdup_printf ("FOO: %d", i++);
gtk_tree_store_set (model, iter, 0, str, -1);
gtk_tree_store_set (base_model, iter, 0, str, -1);
g_free (str);
str = g_strdup_printf ("%d", g_random_int ());
gtk_tree_store_set (base_model, iter, 1, str, -1);
g_free (str);
}
static void
iter_remove (GtkWidget *button, GtkTreeView *tree_view)
{
GtkTreeIter selected;