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

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds