[Gtk-sharp-list] Getting the coordinates of a cell in a TreeCellDataFunc method.

Jonathan Pryor jonpryor@vt.edu
Tue, 17 Aug 2004 07:36:57 -0400


On Tue, 2004-08-17 at 05:01, tapia wrote:
<snip/>
> I have a dinamically created TreeView. So I don't know how much columns
> it have. The problem is when I have to populate it. I can't know how to
> get the column I'm drawing.

I don't know of a way offhand to determine which column to render.  I
don't believe this is possible to do after-the-fact.

Consequently, you need to design your app so that you know, in advance,
what the column is.  You can do that by introducing a new class:

	// 
	// much pseudo-code:
	//

	// This handles your column rendering.  It knows which column 
	// to render, as you tell it in the constructor
	class RenderInfo {
		int column;
		public RenderInfo (int column) {this.column = column;}
		public void GenericCellDataFunc (TreeViewColumn col,
			CellRenderer cell, TreeModel model, 
			TreeIter iter)
		{
			// Access this.column column
			Item item = (Item) model.GetValue (iter, column);
		
			if (item != null) {
				((CellRendererText)cell).Text = 
					item.Columns[column].ToString();
			}
	}

	// in your initialization code...
	int currentColumn = 0;
	foreach (Column toRender in myData) {
		TreeViewColumn column = new TreeViewColumn ();
		column.SetCellDataFunc (tr, new TreeCellDataFunc (
			new RenderInfo (currentColumn++).GenericCellDataFunc
		));
	}

A little abstraction can go a long way...

 - Jon