Re: CTreeCtrls
M.P. wrote:
Hi all,
I was trying to copy one tree control into another, but soon found an
error
CTreeCtrl x;
CTreeCtrl y;
//.. load x with stuff
y = x; // ERROR - this cannot be done (life is not so simple)
Is there a way to do this easily?
No, but I thought it would be fun to try and it may go something like
this. Note, I have not tested this other than to see it compiles. And
something doesn't look quite right:
//start post
/*
Here is where I'd use the CTreeCursor class as it has two members:
HTREEITEM m_hTreeItem;
CTreeCtrlEx *m_pTree;
But this would work with
call( ctrl& a, ctrl& b, HTREEITEM a, HTREEITEM, b )
I'd write it to copy any tree object to any other tree object.
It means you can copy parts of a tree even to the same tree
Note that image lists are handled elsewhere if different trees.
It copies below the from and to, so roots can be used.
*/
void CopyTreeItems( CTreeCursor from, CTreeCursor to )
{
//Assert that the to is not a child of the from
//That could get messy
//As if recursive, so use a stack
std::stack< CTreeCursor > stack;
for( CTreeCursor at= from.GetChild( ); ! stack.empty( ); )
{
if( ! at.IsValid( ) )
{
if( stack.size( ) )
{
at= stack.top( );
stack.pop( );
to= to.GetParent( );
}
continue;
}
//create/copy item
to.AddTail( at.GetText( ), at.GetImageID( ), at.GetImageIDSel( ) );
to.SetData( at.GetData( ) );
if( at.HasChildren( ) )
{
stack.push( at.GetNextSibling( ) );
at= at.GetChild( );
to= to.GetChild( );
}
else
{
at= at.GetNextSibling( );
}
}
}
//end post
Best, Dan.