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

  • It's time high-level executives and IT compliance officers recognize and acknowledge the danger of malicious insiders, an increased attack surface and the potential for breaches caused by employee error or negligence. See why there is extra emphasis on insider threats.

  • Given today's threat environment and the increasing connectivity of digital infrastructures, security teams now realize that they must assume their IT environments are subject to periodic compromise. Gone are the days when preventive measures to secure the perimeter or trying to detect malware problems using signature-match technologies were enough. New practices based on an understanding of the phases of an attack, continuous threat monitoring, and rapid attack detection and remediation are necessary. This …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds