Copy a branch


To copy a branch, we use recursion and the CopyItem() function to accomplish our task.
 
// CopyBranch           - Copies all items in a branch to a new location
// Returns              - The new branch node
// htiBranch            - The node that starts the branch
// htiNewParent - Handle of the parent for new branch
// htiAfter             - Item after which the new branch should be created
HTREEITEM CTreeCtrlX::CopyBranch( HTREEITEM htiBranch, HTREEITEM htiNewParent, 
                                                HTREEITEM htiAfter /*= TVI_LAST*/ )
{
        HTREEITEM hChild;

        HTREEITEM hNewItem = CopyItem( htiBranch, htiNewParent, htiAfter );
        hChild = GetChildItem(htiBranch);
        while( hChild != NULL)
        {
                // recursively transfer all the items
                CopyBranch(hChild, hNewItem);  
                hChild = GetNextSiblingItem( hChild );
        }
        return hNewItem;
}



Comments

  • Another solution..

    Posted by Legacy on 12/08/2003 12:00am

    Originally posted by: S�ren M�ller

    I got a crash every time I tried to copy at branch, which contained children..
    
    

    One thing I noticed was, that though the branch was copied, the old branch and cildren was not deleted.

    I modified the copyBranch function a bit, and no problems here anymore..

    HTREEITEM ENaviPlotTreeCtrl::CopyBranch( HTREEITEM htiBranch, HTREEITEM htiNewParent, HTREEITEM htiAfter /*= TVI_LAST*/ )
    {
    HTREEITEM hChild;

    HTREEITEM hNewItem = CopyItem( htiBranch, htiNewParent, htiAfter );
    hChild = GetChildItem(htiBranch);
    while( hChild != NULL)
    {
    // recursively transfer all the items
    CopyBranch(hChild, hNewItem,TVI_FIRST);
    hChild = GetNextSiblingItem( hChild );
    }


    // Don't delete item data only treeitem..
    SetItemData(htiBranch, NULL);
    DeleteItem(htiBranch);

    return hNewItem;
    }

    Hope this help someone..

    Reply
  • One solution

    Posted by Legacy on 11/08/2002 12:00am

    Originally posted by: Matt Spaulding

    Here is one simple implementation that uses a single
    
    recursive call to copy a branch between two arbitrary WTL
    treeview controls. Note that it uses some custom #defines
    (buffer size, null string, etc) that will prevent direct
    compilation, and also makes some assumptions including the
    item mask and insertion position which could be easily
    parameterized.

    static LRESULT CopyTreeNodes( CTreeViewCtrl &srctree, HTREEITEM srcitem, CTreeViewCtrl &desttree, HTREEITEM destparent )
    {
    TVITEM fromitem;
    TVINSERTSTRUCT toitem;
    TCHAR itemtext[ BUFFER_SIZE ] = NULL_STRING;
    HTREEITEM child, newitem;

    fromitem.mask = TVIF_HANDLE | TVIF_TEXT | TVIF_PARAM;
    fromitem.pszText = itemtext;
    fromitem.cchTextMax = BUFFER_SIZE;
    toitem.hInsertAfter = TVI_LAST;
    toitem.item = fromitem;

    while( srcitem )
    {
    fromitem.hItem = srcitem;
    toitem.hParent = destparent;
    srctree.GetItem( &fromitem );
    newitem = desttree.InsertItem( &toitem );

    if(( child = srctree.GetNextItem( srcitem, TVGN_CHILD )))
    {
    CopyTreeNodes( srctree, child, desttree, newitem );
    }

    srcitem = srctree.GetNextItem( srcitem, TVGN_NEXT );
    }

    return 0;
    }

    Reply
  • Flawed logic

    Posted by Legacy on 11/08/2002 12:00am

    Originally posted by: Matt Spaulding

    This algorithm is fundamentally flawed. To be more specific, it only checks for siblings if an item has children.

    Reply
  • Compiler error

    Posted by Legacy on 11/13/2001 12:00am

    Originally posted by: Jim Weiss

    I'm getting a compiler error because the CopyBranch function takes 3 parameters, but there's a call to CopyBranch within CopyBranch that takes 2 parameters. My compiler complains that there's a fatal error because CopyBranch doesn't take 2 parameters. What's up with this?

    Reply
  • Need Clipboard copy

    Posted by Legacy on 07/08/1999 12:00am

    Originally posted by: Channaveeraya

    I want to copy the entire tree control contents as a image, to clipboard & I want to paste it in the word document.

    Reply
  • This routine can hang and crash

    Posted by Legacy on 02/24/1999 12:00am

    Originally posted by: Gene Sewell

    I used this routine when building a tree which showed a geneology tree
    (for Autocad xref files.)

    The routine hung when trying to copy nested leaves (A->B->A...)
    Since it was recursive, when I stopped it, there was an endless series
    of calls to itself.

    I haven't got a good fix yet - I'll probably build a linked list of
    leaves copied, then stop the copy if I encounter a leaf that's already
    copied.

    Gene

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds