Re: A Question about GtkTreeSortable



On Thu, 2005-10-20 at 17:44 +0200, Iago Rubio wrote:
[snip]
gint       
your_tree_iter_compare_func  (GtkTreeModel *model,
                              GtkTreeIter *a,
                              GtkTreeIter *b,
                              gpointer user_data)
{
GtkTreePath* path_first;
GtkTreePath* path_a;
GtkTreePath* path_b;

first_path = gtk_tree_path_new_first();
path_a = gtk_tree_model_get_path (model,a);
path_b = gtk_tree_model_get_path (model,b);

if( gtk_tree_path_compare(path_first, path_a) == 0 ){
      return -1; // "a" is first so "a" always sorts before
}else if ( gtk_tree_path_compare(path_first, path_b) == 0 ){
      return 1;  // "b" is first so "a" always sorts after
}

Viewing searun full code I realized this implementation is bogus, as it
depends on GtkSortType.

A better implementation should be:

gint       
your_tree_iter_compare_func  (GtkTreeModel *model,
                              GtkTreeIter *a,
                              GtkTreeIter *b,
                              gpointer user_data)
{
GtkTreePath* path_first;
GtkTreePath* path_a;
GtkTreePath* path_b;
GtkSortType sort_type;
gint sortcol, retval = 0;
  
gtk_tree_sortable_get_sort_column_id (
                      GTK_TREE_SORTABLE(model), 
                      &sortcol,
                      &sort_type); 

first_path = gtk_tree_path_new_first();
path_a = gtk_tree_model_get_path (model,a);
path_b = gtk_tree_model_get_path (model,b);

if( sort_type ==  GTK_SORT_ASCENDING ){
   if( gtk_tree_path_compare(path_first, path_a) == 0 )
      retval = -1;
   else if ( gtk_tree_path_compare(path_first, path_b) == 0 )
      retval = 1; 
} else {
   if( gtk_tree_path_compare(path_first, path_a) == 0 ) 
      retval = 1;
   else if ( gtk_tree_path_compare(path_first, path_b) == 0 )
      retval = -1;  
}

if ( retval ){
  gtk_tree_path_free(path_first);
  gtk_tree_path_free(path_a);
  gtk_tree_path_free(path_b);
  return retval;
}

// none of both are first so compare them 

.....

gtk_tree_path_free(path_first);
gtk_tree_path_free(path_a);
gtk_tree_path_free(path_b);
}

-- 
Iago Rubio




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