Re: gtk_tree_foreach() and gtk_tree_find_item()



Tim Janik wrote:
> 
> On Fri, 18 Feb 2000, Fritz Jetzek wrote:
> 
> > hi there,
> >
> > i implemented the above mentioned functions and would like to hand them
> > over to discussion; may be they could be part of gtk+ someday.. see
> > details + example code at
> > http://www.buggerz.com/index.proposals.gtk.html
> 

first of all, thanks a lot for the detailed feedback - this really
motivates =)

> please post patches like this to this list directly.

ok..

> a few notes on the patch,
> - _first_ update you list pointer, then invoke the callback,
>   so you don't access invalid memory if the callback deletes
>   the item currently traversed.
> - call gtk_tree_forech() soemthing else, that name is already taken
>   by gtk_container_foreach(). best bet is probably to model them after
>   clist's pre_recursive() and post_recursive() functions.

alright, i looked at the code of those functions and remodelled mine;
the only difference is actually only the place where i map CTree style
nodes to GtkTreeItems (you suggested reimplementation after GtkClist's
functions; however, i didn't find those functions there so i took those
of GtkCTree. As the CTree is based on GtkCList, that shouldn't matter
really).

> - honour the coding style of gtk, specifically:
>   * put { braces after newlines
>   * use a single line for each statement, so if (foo) return FALSE; becomes:
>     if (foo)
>       return FALSE;
>   * don't use inline assignments, so if ((w = expr())) becomes:
>     w = expr ();
>     if (w)
> 

ok, changed. 

new code (also in my example code at
http://www.buggerz.com/gtk/tree_funcs.c.gz
which is based on the GtkTree example from the GTK Tutorial):

1  void
2  gtk_tree_post_recursive(GtkTree* tree,
3                       GtkTreeItem* item,
4                          GFunc func,
5                          gpointer data)
6  {
7    GList      *children;
8    GList      *tmp;
9  
10    g_return_if_fail (tree != NULL);
11    g_return_if_fail (GTK_IS_TREE (tree));
12    g_return_if_fail (func != NULL);
13  
14    if (item)
15      children = item->subtree?GTK_TREE(item->subtree)->children:NULL;
16    else
17      children = tree->children;
18  
19    while (children)
20      {
21        tmp = children->next;
22        gtk_tree_post_recursive(tree, children->data, func, data);
23        children = tmp;
24      }
25  
26    if (item)
27      (*func) (item, data);
28  }
29  
30  void
31  gtk_tree_pre_recursive(GtkTree* tree,
32                         GtkTreeItem* item,
33                         GFunc func,
34                         gpointer data)
35  {
36    GList     *children;
37    GList     *tmp;
38  
39    g_return_if_fail (tree != NULL);
40    g_return_if_fail (GTK_IS_TREE (tree));
41    g_return_if_fail (func != NULL);
42  
43    if (item)
44      {
45        children =
item->subtree?GTK_TREE(item->subtree)->children:NULL;
46        (*func) (item, data);
47      }
48    else
49      children = tree->children;
50  
51    while (children)
52      {
53        tmp = children->next;
54        gtk_tree_post_recursive(tree, children->data, func, data);
55        children = tmp;
56      }
57  }


> - make the callback function return TRUE to continue traversal and
>   FALSE to abort traversal, then gtk_tree_find_item() can be left to an
>   appropriate callback function, and doesn't need to be implemented by
>   gtk itself. e.g. use
>   typedef gboolean (GtkTraverseFunc) (GtkWidget *widget,
>                                       gpointer   data);


i'm not quite satisfied with this one. As the callback function would
then be passed to gtk_tree_post/pre_recursive, it would require the
*recursive function to return the result of the callback (in case that
an item is found). This in turn would require to deviate from modelling
the recursive functions after those of GtkCTree since they wouldn't be
of type void but of the return type of the passed function. 

If i'm right, there are two ways to proceed: either live with the
redundancy of code and implement a function gtk_tree_find_item, or
implement the *recursive function to stop traversal as soon as a passed
function returns FALSE; the latter would also require a flag saying
whether traversal should be conditional on the callback or not. could
someone comment on this? i'm still not what i'd call a GTK expert ;)

fritz



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