[Gtk-sharp-list] Sorting a TreeView with a filter

Adam Tauno Williams awilliam at whitemice.org
Fri Sep 12 08:13:46 EDT 2008


I have a rather complicated TreeView using a ListStore.  Filtering the
view works,  but I'm so far unable to get sorting to work.  I have a
sort object connected to the model and the filter object connected to
the sort object;  I'm found very similair constructs in other projects
(including Galaxium
<http://code.google.com/p/galaxium/source/browse/trunk/src/Galaxium.Gui.Gtk/NewTreeView/GalaxiumTreeView.cs?r=551>) but it just doesn't sort.  My column-header-click happens when a column gets clicked but it doesn't sort,  the actual sort method never gets pinged.  Despite beng connect-before Gtk seems to be doing something before my column-header-clicked gets called.   Any pointers appreciated. 

What happens when a column header is clicked:
(Whitemice.Consonance:20431): Gtk-CRITICAL **:
gtk_tree_sortable_get_sort_column_id: assertion `GTK_IS_TREE_SORTABLE
(sortable)' failed
(Whitemice.Consonance:20431): Gtk-CRITICAL **:
gtk_tree_sortable_has_default_sort_func: assertion `GTK_IS_TREE_SORTABLE
(sortable)' failed
(Whitemice.Consonance:20431): Gtk-CRITICAL **:
gtk_tree_sortable_set_sort_column_id: assertion `GTK_IS_TREE_SORTABLE
(sortable)' failed
ColumnClicked
Sort Column:4 Order:Ascending

The actual sort method never gets called

<object>

      TreeView view;
      ListStore taskStore;
      TreeModelSort sortAdapter;
      TreeModelFilter  taskFilter;
		  int             sortColumn;
		  SortType        sortOrder;

    <ctor>
			taskStore = new Gtk.ListStore();
		  ...
			objectIdColumn = new Gtk.TreeViewColumn ();
			objectIdColumn.Title = "Object Id";
			objectIdColumn.Reorderable = true;
			objectIdColumn.Resizable = true;
			objectIdColumn.Clickable = true;
			cell = new Gtk.CellRendererText ();
			objectIdColumn.PackStart(cell, true);
			objectIdColumn.SetCellDataFunc(cell, new
Gtk.TreeCellDataFunc(RenderObjectId));
			objectIdColumn.SortColumnId = 0;
			objectIdColumn.Clicked += new EventHandler(ColumnClicked);
			view.AppendColumn(objectIdColumn);
      ...
		  sortAdapter = new TreeModelSort(taskStore);
		  sortAdapter.SetSortFunc(-2, OrderTasks);
			taskFilter = new TreeModelFilter(sortAdapter, null);
			taskFilter.VisibleFunc = new
Gtk.TreeModelFilterVisibleFunc(TaskFilter);
			view.Model = taskFilter;
    </ctor>

		[GLib.ConnectBefore]
		protected void ColumnClicked(object sender, EventArgs args)
		{
		  TreeViewColumn column;

      Console.WriteLine("ColumnClicked");
		  if (sender is Gtk.TreeViewColumn)
		  {
		    column = (sender as Gtk.TreeViewColumn);
		    if (column.SortColumnId == sortColumn)
		    {
		      if (sortOrder == SortType.Ascending)
		        sortOrder = SortType.Descending;
		      else sortOrder = SortType.Ascending;
		      column.SortOrder = sortOrder;
		      column.SortIndicator = true;
		    } else 
		      {
		        sortColumn = column.SortColumnId;
		        sortOrder = SortType.Ascending;
		        column.SortIndicator = true;
		      }
		    Console.WriteLine("Sort Column:{0} Order:{1}", sortColumn,
sortOrder);
		    sortAdapter.SetSortColumnId(sortColumn, sortOrder);
		    sortAdapter.ChangeSortColumn ();
		  }
		} // end ColumnClicked
		
protected int OrderTasks(TreeModel model, TreeIter t1, TreeIter t2)
{ 
//  ObjectId	0   Title 1     Project	2   Start	3
//  Creator	4   Status 5 Priority 6 End 7
//  Executant	8 Completed	9
Console.WriteLine("SimpleTaskList.OrderTasks");
 return 0;
}
    ...
</object>



More information about the Gtk-sharp-list mailing list