Re: GtkTreeView status report



Jonathan Blandford wrote:
> Comments on the actual features would be wonderful, as well as any
needed features that aren't listed.

I recently tried writing some code that dealt with multiple selections and was pretty disappointed by the API for this.

Consider a button that removes the selected rows from the TreeView.
Two situations:
1) The button should be disabled when there are no rows selected. Ok, connect to the "changed" signal on the GtkTreeSelection and... well, how do you determine when there are no rows selected? gtk_tree_selection_get_selected() claims it doesn't work on multi-select list. I ended up writing this abomination:

static void
tree_multi_selected_cb(GtkTreeModel *source, GtkTreePath *path, GtkTreeIter *iter, gpointer data) {
	*(gboolean*)data = TRUE;
}

static gboolean
tree_multi_selected(GtkTreeSelection *sel) {
	gboolean ret = FALSE;
	gtk_tree_selection_selected_foreach(sel, tree_multi_selected_cb, &ret);
	return ret;
}

This is clearly not optimal, but it was the best I could see.

2) Now, when the button is clicked, I need to remove all of the selected rows. Again, the only way of getting at the selection is the foreach function. I don't expect modifying the list from within the foreach to work (though I tried it, and it didn't work), so there are two options: 2a) Build some sort of list using the foreach. You can't rely on TreeIters existing through a delete so you could build a list of TreePaths, but then those involve row numbers so you can delete the elements from the list in reverse...
2b) pseudocode:
do {
  found_selected = FALSE
  get_first(iter)
  do {
    if (iter is_selected) {
      found_selected = TRUE
      remove iter
      break    // to outer while loop
    }
  } while (get_next(iter))
} while (found_selected == FALSE)

You have to restart your search for selected elements again from the top because each time you remove an element you kill your TreeIter. This is O(n^2)ish.

Both of those options are pretty sub-optimal.





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