Copy an item to new location


Copying an item is simple but Ive listed a function for copying anyway. This function is used by other functions discussed later and it also contains a component that we often forget - extensibility. At the end of the function, the OnItemCopied() function is called to give the derived class the oppurtunity to update any internal information.
 
// CopyItem             - Copies an item to a new location
// Returns              - Handle of the new item
// hItem                - Item to be copied
// htiNewParent         - Handle of the parent for new item
// htiAfter             - Item after which the new item should be created
HTREEITEM CTreeCtrlX::CopyItem( HTREEITEM hItem, HTREEITEM htiNewParent, 
                                        HTREEITEM htiAfter /*= TVI_LAST*/ )
{
        TV_INSERTSTRUCT         tvstruct;
        HTREEITEM                       hNewItem;
        CString                         sText;

        // get information of the source item
        tvstruct.item.hItem = hItem;
        tvstruct.item.mask = TVIF_CHILDREN | TVIF_HANDLE | 
                                TVIF_IMAGE | TVIF_SELECTEDIMAGE;
        GetItem(&tvstruct.item);  
        sText = GetItemText( hItem );
        
        tvstruct.item.cchTextMax = sText.GetLength();
        tvstruct.item.pszText = sText.LockBuffer();

        // Insert the item at proper location
        tvstruct.hParent = htiNewParent;
        tvstruct.hInsertAfter = htiAfter;
        tvstruct.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
        hNewItem = InsertItem(&tvstruct);
        sText.ReleaseBuffer();

        // Now copy item data and item state.
        SetItemData( hNewItem, GetItemData( hItem ));
        SetItemState( hNewItem, GetItemState( hItem, TVIS_STATEIMAGEMASK ), 
                                                        TVIS_STATEIMAGEMASK );

        // Call virtual function to allow further processing in derived class
        OnItemCopied( hItem, hNewItem );

        return hNewItem;
}

void CTreeCtrlX::OnItemCopied(HTREEITEM /*hItem*/, HTREEITEM /*hNewItem*/ )
{
        // Virtual function 
}


In the class declaration add the following.

public:
        HTREEITEM CopyItem( HTREEITEM hItem, HTREEITEM htiNewParent, 
                                        HTREEITEM htiAfter = TVI_LAST );

protected:
        virtual void OnItemCopied( HTREEITEM hItem, HTREEITEM hNewItem );




Comments

  • Copy to last node in the tree fails

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

    Originally posted by: Lawrence Perepolkin

    Copy item does not copy to last node in the tree. If one selects a leaf node in the tree control then drags it to the last node in the tree, copy item fails to copy the item.

    Reply
  • /treeview/copy_item.shtml

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

    Originally posted by: Lawrence Perepolkin

    Copy item does not copy to last node in the tree. If one selects a leaf node in the tree control then drags it to the last node in the tree, copy item fails to copy the item.

    Reply
  • Copy Item is really a move?

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

    Originally posted by: Lawrence Perepolkin

    To me copy means to copy a branch to a new location. One should have two instances of the branch in the tree. The copy function does not seem to do this. What it does is moves the branch to a new location?

    Reply
  • Copy tree "as is", without collapsing it.

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

    Originally posted by: Arnt Witteveen

    If you change the line(s):
    
    

    SetItemState( hNewItem, GetItemState( hItem, TVIS_STATEIMAGEMASK ),
    TVIS_STATEIMAGEMASK );

    to:

    SetItemState( hNewItem, GetItemState( hItem, TVIS_STATEIMAGEMASK | TVIS_EXPANDED ),
    TVIS_STATEIMAGEMASK | TVIS_EXPANDED );

    the CopyBranch() function will be copy a branch exactly like it was, with expanded items expanded (and not expanded ones not expanded ... duh).
    Maybe you can also add: TVIS_EXPANDEDONCE and/or TVIS_EXPANDPARTIAL according to your needs.

    I have not been able to copy the TVIS_SELECTED status, because this leaves both the selected item and the "hItem" selected.


    Reply
  • If using callbacks won't work

    Posted by Legacy on 11/10/1998 12:00am

    Originally posted by: John Armstrong

    //use something like
    
    

    tvstruct.item.pszText = LPSTR_TEXTCALLBACK;
    tvstruct.item.pszText = LPSTR_TEXTCALLBACK;
    tvstruct.item.iImage = I_IMAGECALLBACK;
    tvstruct.item.iSelectedImage = I_IMAGECALLBACK;
    tvstruct.item.state = INDEXTOSTATEIMAGEMASK(1);
    tvstruct.item.stateMask = TVIS_STATEIMAGEMASK;
    tvstruct.item.cChildren = I_CHILDRENCALLBACK;

    // instead of the existing code inside copyitem

    sText = Tree.GetItemText( hItem );

    tvstruct.item.cchTextMax = sText.GetLength();
    tvstruct.item.pszText = sText.LockBuffer();

    Reply
  • Re: Help

    Posted by Legacy on 11/05/1998 12:00am

    Originally posted by: Sergey

    Are you moooooooooooooron!

    Reply
  • Help

    Posted by Legacy on 10/06/1998 12:00am

    Originally posted by: Andrew Ardvark

    MOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!

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

Top White Papers and Webcasts

  • Wednesday, September 24, 2014 8:00 AM - 9:00 AM PDT According to a recent Forrester Research report, many companies are choosing low-code platforms over traditional programming platforms, due to the speed with which low-code apps can be assembled and tested. With customer-facing applications on the rise, traditional programming platforms simply can't keep up with the "short schedules and rapid change cycles" required to develop these applications. Check out this upcoming webinar and join Clay Richardson from …

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds