Re: Q: GtkTree - how to get parent and grandparent text?



I had this same problem a while ago.  The reason is that the parent of GtkTreeItem "Version 1.1" is the GtkTree it is in.  This makes sense.  But the parent of that GtkTree is NOT the GtkTreeItem "File A", but instead is the GtkTree that "File A" is in.  I was using the gtk-- wrappers, and this is what I had to do to get the parent's text:


	// the selected item's data
	Gtk::Tree::SelectionList& list( building_tree->selection() ) ;

	if ( list.empty() )
	{
		// nothing is selected,
		return ;
	}
		
	Gtk::TreeItem* item( *list.begin() ) ;
	Gtk::Tree* tree( dynamic_cast<Gtk::Tree*>(item->get_parent()) ) ;
	Gtk::Label* label( dynamic_cast<Gtk::Label*>(item->get_child()) ) ;

	string number( label->get_text() ) ;
	int level( tree->get_level() ) ;

	// the selected item's parent data ;
	Gtk::TreeItem* parent_item = NULL ; 
	// apparently, a tree's parent is a Tree, not a TreeItem
	Gtk::Tree* parent_tree( dynamic_cast<Gtk::Tree*>(tree->get_parent()) ) ;
	Gtk::Label* parent_label = NULL ;
	string parent_number ;

	if( parent_tree != NULL ) 
	{
		// find the TreeItem that has the label of the dept/course
		Gtk::Tree::ItemList& plist( parent_tree->tree() ) ;
		Gtk::Tree::ItemList::iterator piter( plist.begin() ) ;

		parent_item = *piter ;
		while( parent_item->get_subtree() != tree )
		{
			piter++ ;
			parent_item = *piter ;
		}

		parent_label = dynamic_cast<Gtk::Label*>( 
			parent_item->get_child() ) ;
		parent_number = str::split( 
			parent_label->get_text(), ' ' ).first ;
	}


Basically, I get the selected tree item and get the text from it.  Then I get it's tree and the tree's parent tree.  Then I loop through all the TreeItem's in the parent_tree until I find the one whose subtree is the parent of the tree item that was selected.  I haven't had enough experience with the C api to give you an example,  but I imagine that you can get a GList of GtkTreeItems from the parent tree and then loop through them.

This is rather non-intuitive and causes kludgy workarounds like this.  Will/Can this be fixed in 1.4?  This "feature" of having a GtkTree's parent be a tree also causes this kind of code to segfault:

GtkWidget* tree = gtk_tree_new() ;
GtkWidget* item = gtk_tree_item_new() ;
GtkWidget* subtree = gtk_tree_new() ;

gtk_tree_item_set_subtree( GTK_TREEITEM( item ), GTK_TREE( subtree ) ) ;
gtk_tree_append( GTK_TREE( tree ), GTK_TREEITEM( item ) ) ;


But this will work:

gtk_tree_append( GTK_TREE( tree ), GTK_TREEITEM( item ) ) ;
gtk_tree_item_set_subtree( GTK_TREEITEM( item ), GTK_TREE( subtree ) ) ;


( I apologize if I made any really stupid typos in the above C code,  as I mentioned I was using the gtk-- wrappers so I am converting it from memory without looking at any docs.  You're smarter than the compiler, though, and I think you get what I mean :-) )



-D



> 
> I have had this problem: I when a child is selected I can get its
> label with the get_label_or_whatever routine, but I have difficulty
> getting the value of the parents label. It gets upset when I cast the
> parent to GTK_BIN because it has more than one child (or something - I
> think).
> 
> Tree:
> 
> -Root
> |---File A
> |  |--Version 1.1
>  ..
> |---File B
> |  |--Version 1.1
>  ..
> 
> So how do I get the label/text or any data I myself have added from a
> parent tree in a GtkTree? Say I have version 1.1 selected I want to know
> if it is version 1.1 of File A or B.
> 
> Hope this isn't too much of a newbie question..
> 
> rgds,
> A.
> 




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