Re: Removing selected items from a list



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

On 7/21/2006 2:32 PM, Christopher Backhouse wrote:
I want to remove all the items in a list that the user has highlighted
After discovering that I'm not allowed to use 
gtk_tree_selection_selected_foreach I am trying it like this

GList* 
seln=gtk_tree_selection_get_selected_rows(gtk_tree_view_get_selection(view),&model);
do
{
      GtkTreeIter iter;
      gtk_tree_model_get_iter(model,&iter,(GtkTreePath*)seln->data);
      gtk_list_store_remove(store,&iter);
}
while(seln=seln->next);

The effect of this seems to be to remove only alternate items in 
continuous selections and do something complicated when the selection is 
not continuous.

It looks like the GtkTreePaths that gtk_tree_selection_get_selected_rows 
returns aren't happy about the TreeStore being edited under them. What 
is the correct way to do this?

Yep, you've hit on the problem.  GtkTreePaths aren't valid if the
underlying model is altered.  The safest way is to create
GtkTreeRowReferences, which doesn't care if the model is modified.  For
some model types, GtkTreeIters are also safe to use in this manner, but
the row references are *always* safe.

Probably relevant to this: why are there paths,iters and references? 
wouldn't one have done? I guess there are important differences in their 
behaviour and intended usage that the docs don't make clear.

- From my understanding:

* GtkTreePath:
These are just indices, which can be extended for actual trees.  For a
simple GtkListStore, 0 is the first row, 1 is the second row, etc.
Obviously, if you have a GtkTreePath pointing to item #45, and then
reomve item #22, #45 isn't the same item as it used to be.  For other
kinds of models, like GtkTreeStore, GtkTreePaths can actually point to a
hierarchy.  So the path might be 0:2:4, which is the fifth child of the
third child of the first row in the list.  This tree aspect is why you
can't simply replace the GtkTreePath concept with an index.

One thing to note is that the GtkTreePath is not bound to a particular
GtkTreeModel.  That is, you can create a GtkTreePath without having a
model to associate with it.  Whether or not that path is valid for any
particular model is another matter.  A single GtkTreePath with index 0
can be used to get iters for the first item in two different models, if
you want.

* GtkTreeIter:
Unlike paths, iterators are associated with a particular GtkTreeModel.
You can easily move backwards and forwards through a model, and know if
you're moving to a valid point in the model.  GtkTreeIters are not
necessarily resistant to modification of the underlying model; i.e., an
iter created before modifying the model may not be valid afterwards.
There's a way to check to see if a particular model supports
modification-persistent iters, but I don't remember how offhand.

*GtkTreeRowReference:
These guys are pretty hardy.  If you create one, it'll always point to
the same row in your model, no matter what.  I don't quite remember
exactly, but it may also grab a reference to the row itself, so it'll
still be valid even if the row is removed from the model.

At least, this is how I've used and looked at these three classes in my
apps, so it seems to work for me.  I probably don't have the whole
picture, though, as I haven't messed with GtkTreeModel's internals all
that much.

        -brian

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)

iD8DBQFEwUx+6XyW6VEeAnsRApuiAJ4xFMtycSrXcQ6Ti4bG9DEREEl2lgCfe10e
E7NXlaU8D2JQempxGGTuIqE=
=httb
-----END PGP SIGNATURE-----



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