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
OnEditCopy
and OnUpdateEditCopy
, respectively, in the Add Member Function dialog box.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.
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
OnEditPaste
, in the Add Member Function dialog box.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();