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

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds