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

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds