Re: New list/tree model questions



Maxim Koshelev <max krascoal ru> writes:
gtk_tree_model_get needs a iterator as parameter. So as far as I can
understand there is no
function to get  row by data (like gtk_clist_find_row_from_data) in
new tree model.
Or I'm wrong?


No you're right about that, I just wasn't sure what you were asking.

Look at how find_row_from_data works:

gint
gtk_clist_find_row_from_data (GtkCList *clist,
                              gpointer  data)
{
  GList *list;
  gint n;

  g_return_val_if_fail (GTK_IS_CLIST (clist), -1);

  for (n = 0, list = clist->row_list; list; n++, list = list->next)
    if (GTK_CLIST_ROW (list)->data == data)
      return n;

  return -1;
}

You can write exactly the same thing for tree view (this assumes a
list rather than a tree):

gboolean
find_row_from_data (GtkTreeModel *model,
                    GtkTreeIter  *iter,
                    int           column,
                    void         *data)
{
  GtkTreeIter tmp;
  void *this_data;

  if (!gtk_tree_model_get_iter_first (model, &tmp))
     return FALSE;

  do
  {
     gtk_tree_model_get (model, &tmp, column, &this_data, -1);
     if (this_data == data)
       {
           *iter = tmp;
           return TRUE;
       }
   } while (gtk_tree_model_iter_next (model, &tmp));

   return FALSE; /* FALSE return means not found */
}


Note that in both cases, this is a linear search and thus best
avoided. The tree model case only works for columns that contain
pointers, clearly, some columns can contain ints or strings or 
whatever. The returned data from gtk_tree_model_get() will need
to be freed if it's a string or boxed type rather than 
G_TYPE_POINTER.

To handle a tree in addition to a list, you need to recurse into the
model instead of just walking over it linearly.

Havoc



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