Start with the project you created in Tutorial 4: Setting Vector Automation Properties.
Take the following steps to add Undo/Redo capabilites to your project.:
1. |
Open VecAutDoc.cpp and add the following lines after #include "VecAutDoc.h": |
extern CVecAutApp theApp;
2. |
Click on the "Resource View" tab of the project workspace. |
|
Click to open the "VecAut resources" branch. |
|
Click to open the "Menu" branch. |
|
Double click "IDR_MAINFRAME". |
3. |
Click on Edit menu to expand it, then right click on the Copy and choose "Add Event Handler" |
4. |
For the "Class Name" drop down box, select "CVecAutDoc". |
5. |
For "Messages" select "COMMAND". |
6. |
Click the "Add And Edit" button. |
7. |
Return to the menu window, then right click again on the Copy and choose "Add Event Handler" |
8. |
For "Messages" select "UPDATE_COMMAND_UI". |
8. |
Click the "Add Function" button. |
9. |
Repeat the previous five steps for ID_EDIT_CUT, ID_EDIT_PASTE, ID_EDIT_UNDO |
10. |
Add the code to the following eight functions: |
void CVecAutDoc::OnEditCopy()
{
theApp.m_Automation.Copy ();
}
void CVecAutDoc::OnUpdateEditCopy(CCmdUI* pCmdUI)
{
L_BOOL bSelected = FALSE;
m_VectorWindow. IsObjectSelected (&bSelected);
pCmdUI->Enable(bSelected);
}
void CVecAutDoc::OnEditCut()
{
theApp.m_Automation.Cut ();
}
void CVecAutDoc::OnUpdateEditCut(CCmdUI* pCmdUI)
{
L_BOOL bSelected = FALSE;
m_VectorWindow. IsObjectSelected (&bSelected);
pCmdUI->Enable(bSelected);
}
void CVecAutDoc::OnEditPaste()
{
theApp.m_Automation.Paste ();
}
void CVecAutDoc::OnUpdateEditPaste(CCmdUI* pCmdUI)
{
pCmdUI->Enable(theApp.m_Automation.ClipboardDataReady () == TRUE);
}
void CVecAutDoc::OnEditUndo()
{
theApp.m_Automation.Undo ();
}
void CVecAutDoc::OnUpdateEditUndo(CCmdUI* pCmdUI)
{
pCmdUI->Enable((theApp.m_Automation.CanUndo () == TRUE));
}
11. |
The OnNewDocument() and OnOpenDocument() functions make calls to vector functions that get added to the "Undo" stack. For this tutorial, opening and document and creating a new document will not be "undoable". To accomplish this, any code that should not be added to the "undo" stack needs to be wrapped in the following calls: |
theApp.m_Automation.SetUndoEnabled (FALSE);
//...code not added to undo stack
theApp.m_Automation.SetUndoEnabled (TRUE);
12. |
In the Class View branch, click to open the CVecAutDoc branch. |
13. |
Double click to edit the OnNewDocument() member function. Add the SetUndoEnabled(FALSE) and SetUndoEnabled(TRUE) calls as follows: |
BOOL CVecAutDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
//Empty the vector of all objects and layers
m_VectorWindow. Empty ();
//Create an empty vector layer
VECTORLAYERDESC VectorLayerDesc;
VectorLayerDesc.nSize = sizeof(VECTORLAYERDESC);
lstrcpy(VectorLayerDesc.szName, TEXT("First Layer"));
VectorLayerDesc.bVisible= TRUE;
VectorLayerDesc.bLocked = FALSE;
VectorLayerDesc.dwTag = 0;
LVectorLayer MyLayer(&VectorLayerDesc);
m_VectorWindow. AddLayer (&MyLayer);
m_VectorWindow. SetActiveLayer (&MyLayer);
m_VectorWindow. EnableAutoScroll (TRUE);
VECTORPOINT min, max;
min.x = min.y = min.z = max.z = 0.0;
max.x = max.y = 100.0;
m_VectorWindow. SetParallelogram (&min,&max);
m_VectorWindow. Reset ();
theApp.m_Automation.SetUndoEnabled (TRUE);
return TRUE;
}
14. |
Do the same for OnOpenDocument(). The function should look like this: |
BOOL CVecAutDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
L_TCHAR szTemp[_MAX_PATH];
L_BOOL bRet;
L_INT nRet;
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
theApp.m_Automation.SetUndoEnabled (FALSE);
lstrcpy(szTemp, lpszPathName);
nRet = m_VectorWindow. Load (szTemp);
if (nRet == SUCCESS)
{
m_VectorWindow. EnableAutoScroll (TRUE);
m_VectorWindow. Reset ();
}
else
{
CString strTmp;
strTmp.Format(TEXT("Error Loading File[%d]"), nRet);
AfxMessageBox(strTmp);
bRet = FALSE;
}
theApp.m_Automation.SetUndoEnabled (TRUE);
return bRet;
}
15. |
Compile and run the demo. |
16. |
From the program menu, choose View->Vector Toolbar. |
17. |
Select the line tool and draw three lines. |
18. |
Choose the select tool, and select one of the lines. |
19. |
Choose Edit->Cut to remove the line. |
20. |
Choose Edit-Paste to paste the line. |
21. |
Using the select tool, select all of the lines. |
22. |
Hit the "Del" key to delete the lines. |
23. |
Choose Edit->Undo to bring back the lines. |