Re: How to remove selected items



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert Caryl wrote:
> Furthermore, I am going to assume that you have written a function in
> your class that will serve as the callback slot parameter for  
> Gtk::TreeSelection::selected_foreach_iter.  In this callback slot you
> should make a call to Gtk::TreeModel::erase using the iterator passed as
> its formal parameter, and voila!  Your selected rows will be deleted. 

The GTK+ documentation states the following for
gtk_tree_selection_selected_foreach: "Note that you cannot modify the
tree or selection from within this function."

This is the way I did it. It is plain C, but you probably get the idea.
It is perhaps even possible to avoid the conversion to iterators if you
delete the rows in the reverse order (from bottom to top).

static void
cg_element_editor_remove_button_clicked_cb (G_GNUC_UNUSED GtkButton *button,
                                            gpointer user_data)
{
	CgElementEditor *editor;
	CgElementEditorPrivate *priv;
	GtkTreeSelection *selection;
	GtkTreeIter *iter;
	GtkTreePath *path;
	GList *selected_rows;
	GList *selected_iters;
	GList *cur_item;
	
	editor = CG_ELEMENT_EDITOR (user_data);
	priv = CG_ELEMENT_EDITOR_PRIVATE (editor);
	selection = gtk_tree_view_get_selection (priv->view);
	selected_rows = gtk_tree_selection_get_selected_rows (selection, NULL);
	selected_iters = NULL;

	/* Convert paths to iters because changing the model borks offsets
	 * in paths, but since GtkListStore has GTK_TREE_MODEL_PERSIST set,
	 * iters continue to live after changing the model. */
	for (cur_item = selected_rows; cur_item != NULL; cur_item = cur_item->next)
	{
		path = cur_item->data;
		iter = g_new (GtkTreeIter, 1);

		gtk_tree_model_get_iter (priv->list, iter, path);
		selected_iters = g_list_prepend (selected_iters, iter);

		gtk_tree_path_free (path);
	}
	
	for (cur_item = selected_iters;
	     cur_item != NULL;
	     cur_item = cur_item->next)
	{
		iter = cur_item->data;
		gtk_list_store_remove (GTK_LIST_STORE (priv->list), iter);
		g_free (iter);
	}
	
	g_list_free (selected_rows);
	g_list_free (selected_iters);
}

Greetings,
Armin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGG3FvhOtxKlDYm6cRAj3UAJ9/8pamASk6e2sg3PK12dvH8xgnLwCbBceP
S7it2KyLcQv4DdDJp11yFog=
=5U4s
-----END PGP SIGNATURE-----



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]