Adding Command Handlers for Copy and Paste

AppWizard has already added the Copy and Paste menu items to Container’s Edit menu, but these commands still need to be implemented. The COleDocument implementation already provides an UPDATE_COMMAND_UI handler for the Paste command. This handler enables the Paste command if there is anything on the Clipboard.

To implement the Copy command on the Edit menu

  1. On WizardBar, select CContainerView and ID_EDIT_COPY. Click the action button arrow located on the right end of WizardBar and then click Add Windows Message Handler.

  2. In the New Windows Message and Event Handlers dialog box, click Add Handler for both the COMMAND and UPDATE_COMMAND_UI handlers for ID_EDIT_COPY and accept the default function names, OnEditCopy and OnUpdateEditCopy, respectively, in the Add Member Function dialog box.

  3. To implement the Copy command on the Edit menu, fill in the handler that WizardBar creates with the code below :
    if (m_pSelection != NULL)
    m_pSelection->CopyToClipboard();
    

    The Copy command on the Edit menu copies the contents of the current selection to the Clipboard. Implementing the Copy command is easy because the framework function COleClientItem::CopyToClipboard does all the work.

  4. Fill in the handler for OnUpdateEditCopy with the following code:
    pCmdUI->Enable(m_pSelection != NULL);
    

    The UPDATE_COMMAND_UI handler for the Copy command enables the command if there is an active selection; otherwise, the command is disabled.

To implement the Paste command on the Edit menu

  1. On WizardBar, select CContainerView and ID_EDIT_PASTE. Click the action button arrow located on the right end of WizardBar and then click Add Windows Message Handler.

  2. In the New Windows Message and Event Handlers dialog box, click Add and Edit for the COMMAND handler for ID_EDIT_PASTE and accept the default function name, OnEditPaste, in the Add Member Function dialog box.

  3. Implement the Paste command with the following code:
    CContainerItem* pItem = NULL;
    
    TRY
    {
    // Create new item connected to this document.
    CContainerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    pItem = new CContainerItem(pDoc);
    ASSERT_VALID(pItem);
    
    // Initialize the item from clipboard data
    if (!pItem->CreateFromClipboard())
    AfxThrowMemoryException();    // any exception will do
    ASSERT_VALID(pItem);
    
    // update the size before displaying
    pItem->UpdateFromServerExtent();
    
    // set selection to newly pasted item
    SetSelection(pItem);    
    pItem->InvalidateItem();    
    }
    CATCH(CException, e)
    {
    if (pItem != NULL)
    {
    ASSERT_VALID(pItem);
    pItem->Delete();
    }    
    AfxMessageBox(IDP_FAILED_TO_CREATE);
    }
    END_CATCH
    

The Paste command on the Edit menu is somewhat like the Insert New Object command on the Edit menu in that it creates a new COleClientItem object. Compare the above implementation of OnEditPaste with the one that AppWizard provided for OnInsertObject. Both share some code for constructing a new CContainerItem.

The difference is that OnInsertObject initializes the item based on information requested from the user by means of a COleInsertDialog object as shown here:

   // Initialize the item from the dialog data.
   if (!dlg.CreateItem(pItem))
      AfxThrowMemoryException();   // any exception will do
   ASSERT_VALID(pItem);
         
   // If item created from class list (not from file) then launch
   //   the server to edit the item.
   if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
      pItem->DoVerb(OLEIVERB_SHOW, this);

The OnEditPaste function initializes the item from the Clipboard, using COleClientItem::CreateFromClipboard as shown below:

   if (!pItem->CreateFromClipboard())
      AfxThrowMemoryException();