GtkTreeView sorting triggered by cell renderer while retaining row selection



Hello,

I've stumbled upon a peculiar problem with GtkTreeView and selections
when combined with sorting. Please download and see the simple test
case code[1] and associated GtkBuilder UI[2] for reference.

(Compile with: gcc $(pkg-config --libs --cflags gtk+-2.0) treeview.c
-o treeview)

So I have a tree view with two columns - in one column there is a
GtkCellRendererToggle which is mapped to a boolean field in my
GtkListStore. I'd like the user to be able to modify this boolean
field by clicking the toggle button, so I connect to the "toggled"
signal and from the handler I update the appropriate row in the model.

So far so good.

Now I'd also like to have the rows sorted so that the toggled rows are
on the top of the list (the ones where the boolean field is TRUE). I
set up my sort function appropriately and test it.

Select the last row in the list and click on the toggle button. The
toggle state changes and the row moves to the top of the list, as
expected of the sorting. However, the GtkTreeView *selection* is now
still at the last row! I would expect it to be retained on the row we
just clicked (and which happened to move to the top).

Note that the selection retaining works as expected if the model is
changed in some other way that does not involve clicks on the tree
view.

Here's the output of the program after clicking the toggle button:

** (treeview:1538): DEBUG: toggle_cb
** (treeview:1538): DEBUG: sort_iter_compare
** (treeview:1538): DEBUG: sort_iter_compare
** (treeview:1538): DEBUG: sort_iter_compare
** (treeview:1538): DEBUG: selection_changed

First the toggle callback is called, which in turn updates the model.
Then GtkTreeSortable does its magic and calls sort_iter_compare to
re-order the rows. What I'm curious about is the last line, where the
"changed" signal of the tree views GtkTreeSelection is emitted. Why is
the selection changed?

My guess is that the click event on the toggle button is handled
before the click on the row. Thus the click on the row is only handled
*after* the row order has changed and the row under the mouse is
selected instead.

Am I doing something wrong here to achieve both sorting and editable
cells? Or is this a bug in GTK? How can I make sure the selection is
retained?


Best regards,

Johannes H. Jensen

[1] http://dl.dropbox.com/u/190333/tmp/treeview.c
[2] http://dl.dropbox.com/u/190333/tmp/treeview.ui



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