]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
GtkTreeModel copy chain. gtktreemodel_copy_chain
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Tue, 4 Oct 2011 08:28:21 +0000 (10:28 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Tue, 4 Oct 2011 08:28:21 +0000 (10:28 +0200)
src/utils.c
tests/utils_gtktreemodel_copy.c

index 31735985cee568f6d4492d7644fea88f50b39781..16c810716ee6a5d45a6cc1eeb01128588db04679 100644 (file)
@@ -512,8 +512,9 @@ GType
 }
 
 static GtkTreeIter
-*solipa_gtktreemodel_get_iter_child (GtkTreeIter *iter, GtkTreeModel **models, guint n_models)
+*solipa_gtktreemodel_get_iter_child (GtkTreeIter *iter, GPtrArray *models)
 {
+       GtkTreeModel *model;
        GtkTreeIter *iter_parent;
        GtkTreeIter *iter_child;
 
@@ -522,18 +523,19 @@ static GtkTreeIter
        iter_parent = iter;
        iter_child = (GtkTreeIter *)g_malloc0 (sizeof (GtkTreeIter));
 
-       for (n = 0; n < (n_models - 1); n++)
+       for (n = 0; n < (models->len - 1); n++)
                {
-                       if (GTK_IS_TREE_MODEL_SORT (models[n]))
+                       model = (GtkTreeModel *)g_ptr_array_index (models, n);
+                       if (GTK_IS_TREE_MODEL_SORT (model))
                                {
-                                       gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (models[n]),
+                                       gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model),
                                                                                        iter_child, iter_parent);
                                        iter_parent = iter_child;
                                        iter_child = (GtkTreeIter *)g_malloc0 (sizeof (GtkTreeIter));
                                }
-                       else if (GTK_IS_TREE_MODEL_FILTER (models[n]))
+                       else if (GTK_IS_TREE_MODEL_FILTER (model))
                                {
-                                       gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (models[n]),
+                                       gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
                                                                                          iter_child, iter_parent);
                                        iter_parent = iter_child;
                                        iter_child = (GtkTreeIter *)g_malloc0 (sizeof (GtkTreeIter));
@@ -547,8 +549,7 @@ static GtkTreeIter
 static void
 solipa_gtktreemodel_copy_iter (GtkTreeModel *ret,
                                GtkTreeModel *model,
-                               GtkTreeModel **models,
-                               guint n_models,
+                               GPtrArray *models,
                                gboolean is_list_store,
                                guint cols,
                                GtkTreeIter *iter,
@@ -569,13 +570,13 @@ solipa_gtktreemodel_copy_iter (GtkTreeModel *ret,
                                {
                                        gtk_tree_store_append (GTK_TREE_STORE (ret), &iter_new, iter_parent);
                                }
-continue;
-                       iter_child = solipa_gtktreemodel_get_iter_child (iter, models, n_models);
+
+                       iter_child = solipa_gtktreemodel_get_iter_child (iter, models);
                        for (col = 0; col < cols; col++)
                                {
                                        GValue gval = {0};
 
-                                       gtk_tree_model_get_value (models[n_models - 1],
+                                       gtk_tree_model_get_value ((GtkTreeModel *)g_ptr_array_index (models, models->len - 1),
                                                                  iter_child,
                                                                  col, &gval);
 
@@ -598,32 +599,30 @@ continue;
 
                                        if (gtk_tree_model_iter_children (model, &iter_children, iter_child))
                                                {
-                                                       solipa_gtktreemodel_copy_iter (ret, model, models, n_models, is_list_store, cols, &iter_children, &iter_new);
+                                                       solipa_gtktreemodel_copy_iter (ret, model, models, is_list_store, cols, &iter_children, &iter_new);
                                                }
                                }
                } while (gtk_tree_model_iter_next (model, iter));
 }
 
 static void
-solipa_gtktreemodel_get_models_chain (GtkTreeModel *model, GtkTreeModel **models, guint *n_models)
+solipa_gtktreemodel_get_models_chain (GtkTreeModel *model, GPtrArray *models)
 {
+       GtkTreeModel *my_model;
+
        if (GTK_IS_TREE_MODEL_SORT (model))
                {
-                       *n_models = *n_models + 1;
-                       models = (GtkTreeModel **)g_realloc (models, *n_models * sizeof (GtkTreeModel *));
-
-                       models[*n_models - 1] = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model));
+                       my_model = (gpointer)gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model));
+                       g_ptr_array_add (models, my_model);
 
-                       solipa_gtktreemodel_get_models_chain (models[*n_models - 1], models, n_models);
+                       solipa_gtktreemodel_get_models_chain (my_model, models);
                }
        else if (GTK_IS_TREE_MODEL_FILTER (model))
                {
-                       *n_models = *n_models + 1;
-                       models = (GtkTreeModel **)g_realloc (models, *n_models * sizeof (GtkTreeModel *));
+                       my_model = (gpointer)gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
+                       g_ptr_array_add (models, my_model);
 
-                       models[*n_models - 1] = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
-
-                       solipa_gtktreemodel_get_models_chain (models[*n_models - 1], models, n_models);
+                       solipa_gtktreemodel_get_models_chain (my_model, models);
                }
 }
 
@@ -635,29 +634,26 @@ GtkTreeModel
        GType *gtypes;
 
        guint cols;
-       guint col;
 
-       guint n_models;
-       GtkTreeModel **models;
+       GPtrArray *models; /* GtkTreeModel */
 
        GtkTreeIter iter;
 
        ret = NULL;
 
        gtypes = solipa_gtktreemodel_get_gtypes (model, &cols);
-       if (gtypes != NULL)
+       if (gtypes != NULL && cols > 0)
                {
-                       n_models = 1;
-                       models = (GtkTreeModel **)g_malloc0 (sizeof (GtkTreeModel *));
-                       models[0] = model;
+                       models = g_ptr_array_new ();
+                       g_ptr_array_add (models, (gpointer)model);
 
                        if (GTK_IS_TREE_MODEL_SORT (model) ||
                            GTK_IS_TREE_MODEL_FILTER (model))
                                {
-                                       solipa_gtktreemodel_get_models_chain (model, models, &n_models);
+                                       solipa_gtktreemodel_get_models_chain (model, models);
                                }
 
-                       if (GTK_IS_LIST_STORE (models[n_models - 1]))
+                       if (GTK_IS_LIST_STORE ((GtkTreeModel *)g_ptr_array_index (models, models->len - 1)))
                                {
                                        ret = GTK_TREE_MODEL (gtk_list_store_newv (cols, gtypes));
 
@@ -665,11 +661,11 @@ GtkTreeModel
                                                {
                                                        if (gtk_tree_model_get_iter_first (model, &iter))
                                                                {
-                                                                       solipa_gtktreemodel_copy_iter (ret, model, models, n_models, TRUE, cols, &iter, NULL);
+                                                                       solipa_gtktreemodel_copy_iter (ret, model, models, TRUE, cols, &iter, NULL);
                                                                }
                                                }
                                }
-                       else if (GTK_IS_TREE_STORE (models[n_models - 1]))
+                       else if (GTK_IS_TREE_STORE ((GtkTreeModel *)g_ptr_array_index (models, models->len - 1)))
                                {
                                        ret = GTK_TREE_MODEL (gtk_tree_store_newv (cols, gtypes));
 
@@ -677,7 +673,7 @@ GtkTreeModel
                                                {
                                                        if (gtk_tree_model_get_iter_first (model, &iter))
                                                                {
-                                                                       solipa_gtktreemodel_copy_iter (ret, model, models, n_models, FALSE, cols, &iter, NULL);
+                                                                       solipa_gtktreemodel_copy_iter (ret, model, models, FALSE, cols, &iter, NULL);
                                                                }
                                                }
                                }
@@ -685,6 +681,8 @@ GtkTreeModel
                                {
                                        g_warning ("GtkTreeModel type not expected.");
                                }
+
+                       g_ptr_array_free (models, TRUE);
                }
 
        return ret;
index f1b1ea0f7e244e99ca35eb206c8ac2eea1d70c4e..fea02fb05909f2ca049cb75aa43b13cc0fa00e1d 100644 (file)
@@ -103,12 +103,13 @@ main (int argc, char *argv[])
                {
                        g_error ("Copia del GTK_LIST_STORE vuota.");
                }
+       g_free (copia);
 
        g_printf ("\n\nCOPIA DEL LIST_STORE SORT\n"
                  "=========================\n");
 
        sort = gtk_tree_model_sort_new_with_model (model);
-       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort), 1, GTK_SORT_ASCENDING);
+       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort), 2, GTK_SORT_ASCENDING);
 
        copia = solipa_gtktreemodel_copy (sort, FALSE);
 
@@ -128,6 +129,7 @@ main (int argc, char *argv[])
                {
                        g_error ("Copia del GTK_LIST_STORE vuota.");
                }
+       g_free (copia);
 
        /* copia di un TREE_STORE */
        g_printf ("\n\nCOPIA DEL TREE_STORE\n"