Click here to Skip to main content
15,892,797 members
Articles / Programming Languages / C++

Undo and Redo the "Easy" Way

Rate me:
Please Sign up or sign in to vote.
4.95/5 (42 votes)
20 Jun 2004CPOL22 min read 290.4K   8.6K   114  
This article introduces a simple approach to in-memory transactions that can be used to implement Undo and Redo. The technique uses SEH and Virtual Memory and requires only STL and Win32.
// DrawIt.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#pragma warning (disable:4786)
#include "DrawIt.h"

#include "MainFrm.h"
#include "DrawItDoc.h"
#include "DrawItView.h"
#include ".\drawit.h"
#include "afxcmn.h"
#include "afxdhtml.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only CDrawItApp object

CDrawItApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDrawItApp

BEGIN_MESSAGE_MAP(CDrawItApp, CWinApp)
	//{{AFX_MSG_MAP(CDrawItApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDrawItApp construction

CDrawItApp::CDrawItApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// CDrawItApp initialization

BOOL CDrawItApp::InitInstance()
{
	BOOL retval = FALSE;

	__try {
		retval = DoInit();
	} __except(Mm::ExceptionFilter(GetExceptionInformation())) {
		ASSERT(0); // unreachable
	}

	return retval;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
public:
	CRichEditCtrl m_ctrlContent;
	virtual BOOL OnInitDialog();
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_CONTENT, m_ctrlContent);
}

BOOL CAboutDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CDrawItApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
//	CDHtmlDialog aboutDlg(IDD_ABOUTBOX);
//	aboutDlg.m_nHtmlResID = IDR_ABOUT_CONTENT;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CDrawItApp message handlers

DrawFunctionMap g_drawFunctions;

unsigned int AddDrawFunc(const CLIENTCALLBACKS* cb)
{
	unsigned int index = g_drawFunctions.size();
	g_drawFunctions[index] = *cb;
	return index;
}

BOOL CDrawItApp::DoInit()
{
	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("DrawIt"));

	LoadStdProfileSettings(5);  // Load standard INI file options (including MRU)

	// Load DLLs containing the code to draw and manipulate shapes, by default
	// this application doesn't have any shapes defined.
	//
//	Mm::InitSpaceZero();

	HMODULE h = ::LoadLibrary("DrawFunc.dll");
	if (h == NULL) {
		::MessageBox(NULL, "Failed to load DrawFunc.dll", "DrawIt.exe Cannot Start", MB_OK | MB_ICONEXCLAMATION);
	}

	PFNDLLINIT pfnInit = (PFNDLLINIT)::GetProcAddress(h, lpzDrawDLLInit);
	(*pfnInit)(AddDrawFunc);

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CDrawItDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CDrawItView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;


	// The one and only window has been initialized, so show and update it.
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	return TRUE;
}

int CDrawItApp::Run() 
{
	int retval = 0;
	__try {
		retval = CWinApp::Run();
	} __except(Mm::ExceptionFilter(GetExceptionInformation())) {
		ASSERT(0); // unreachable
	}	
	return retval;
}

int CDrawItApp::ExitInstance() 
{
	Mm::ClearAll();
	
	return CWinApp::ExitInstance();
}

void CDrawItApp::UpdateAllDocViews(void)
{
	POSITION posDT = GetFirstDocTemplatePosition();
	while (posDT) {
		CDocTemplate* pDT = GetNextDocTemplate(posDT);
		POSITION posD = pDT->GetFirstDocPosition();
		while (posD) {
			CDocument* pDoc = pDT->GetNextDoc(posD);
			pDoc->UpdateAllViews(NULL);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
A compiler warns of bogasity, ignore it at your peril. Unless you've done the compiler's job yourself, don't criticize it.

Comments and Discussions