Re: gtk_tree_view/store, best way to select all nodes of a branch



I played around with the gtk_tree_selection_select_range functions, but as far as I have seen this is not 
working for collapsed elements.
Also I found the handling very difficult for my purpose, and the operation can be distorted by other mouse 
movements.

I implemented this now by using the gtk_tree_model_foreach function, probably not the most efficient method, 
but at least a working one:

gtk_tree_model_foreach_hierarchy (GtkTreeModel *model,
                                  GtkTreeModelForeachFunc func,
                                  GtkTreePath * path,
                                  gpointer user_data)

The gtk_tree_model_foreach_hierarchy executes the GtkTreeModelForeachFunc func in a parent-child hierarchy 
for the parent and each child.
The parameter treepath has to point to one element in the desired hierarchy.


-- code --

typedef struct
{
  GtkTreeModelForeachFunc func;
  int index;
  gpointer user_data;
}t_gtmfh;

gboolean foreach_indices (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
{
  int ret = 0;
  t_gtmfh * gtmfh;
  int * ind;

  gtmfh = user_data;
  ind = gtk_tree_path_get_indices (path);

  if (*ind == gtmfh->index)
  {
    gtmfh->func(model, path, iter, gtmfh->user_data);
  }
  else if (*ind > gtmfh->index)
  {
    ret = 1;
  }

  return ret;
}


void gtk_tree_model_foreach_hierarchy (GtkTreeModel *model, GtkTreeModelForeachFunc func,GtkTreePath * 
path,gpointer user_data)
{
  t_gtmfh gtmfh;
  int *ind;

  ind = gtk_tree_path_get_indices (path);

  gtmfh.func = func;
  gtmfh.index = *ind;
  gtmfh.user_data = user_data;

  gtk_tree_model_foreach (model,foreach_indices,&gtmfh);
}




Am 12.09.2012 10:54, schrieb David NeÄas:
On Wed, Sep 12, 2012 at 10:40:11AM +0200, Arne Pagel wrote:
My current simple solution is as follows:
I use the gtk_tree_model_foreach() function and pass some user data with the current major number of the 
tree-path.
Inside the foreach-function I use gtk_tree_path_to_string, where I check if the first number is identical to 
the user data.

This works, but I have to use a lot of string functions.
Is there a smarter way to do that with less overhead?

Use gtk_tree_selection_select_range().

You already have the start path, so, how to get the end path?  Use
gtk_tree_model_iter_n_children() and gtk_tree_model_iter_nth_child()
recursively to always move to the last child in the branch.  If your
tree is only two-level (as it seems to be) you do not even need to
recurse.

Regards,

Yeti






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