Click here to Skip to main content
15,884,425 members
Articles / Programming Languages / C++

A Simple, Action Based, Undo/Redo Framework

Rate me:
Please Sign up or sign in to vote.
4.51/5 (38 votes)
16 Feb 2013CPOL5 min read 97.3K   1.9K   104  
How to use a simple, action based, undo/redo framework
/*
***************************************************************************

kis v 1

(c) 2006, Florin DUMITRESCU

mailto: fdproxy@gmail.com

$Workfile: _DemoAction_ChangeAppOptions.cpp $ 

***************************************************************************
*/


#include "StdAfx.h"
#include "_Factory.h"
#include "_Graphic.h"
#include "KisActionDemoDoc.h"
#include "_DemoAction.h"
#include "Resource.h"
#include "MainFrm.h"
#include "KisActionDemo.h"

using namespace kis;
using namespace std;

namespace
{


class C_AppOptionsDlg : public CDialog
{

  DECLARE_DYNAMIC(C_AppOptionsDlg)
	DECLARE_MESSAGE_MAP()

public:

  enum { IDD = IDD_APP_OPTIONS };
  unsigned int m_UndoRedoMemorySize;
	C_AppOptionsDlg(CWnd* pParent = NULL);
	virtual ~C_AppOptionsDlg();
	virtual void DoDataExchange(CDataExchange* pDX);

};

IMPLEMENT_DYNAMIC(C_AppOptionsDlg, CDialog)

BEGIN_MESSAGE_MAP(C_AppOptionsDlg, CDialog)
END_MESSAGE_MAP()

C_AppOptionsDlg::C_AppOptionsDlg(CWnd* pParent /*=NULL*/)
: 
CDialog(C_AppOptionsDlg::IDD, pParent),
m_UndoRedoMemorySize(0)
{
  // nop
}

C_AppOptionsDlg::~C_AppOptionsDlg()
{
  // nop
}

void C_AppOptionsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
  DDX_Text( pDX, IDC_EDIT_UNDO_REDO_MEMORY_SIZE, m_UndoRedoMemorySize );
  DDV_MinMaxUInt( pDX, m_UndoRedoMemorySize, 0, 1024*1024-1 );
}


class C_DemoAction_ChangeAppOptions : public C_DemoAction
{

public:

  C_DemoAction_ChangeAppOptions()
  :
  m_UndoRedoMemorySize( 0 )
  {
    // nop
  }

  C_DemoAction_ChangeAppOptions( const C_DemoAction_ChangeAppOptions& a_Src )
  :
  m_UndoRedoMemorySize( a_Src.m_UndoRedoMemorySize )
  {
    // nop
  }

  /*override*/ SP_Action Clone() const
  {
    return SP_Action( new C_DemoAction_ChangeAppOptions( *this ) );
  }

  /*override*/ bool IsUnexecutable() const
  {
    return false;
  }

  /*override*/ bool IsNull() const
  {
    return false;
  }

  /*override*/ bool HasInput() const
  {
    return false;
  }

  /*override*/ bool AcquireInput()
  {
    C_AppOptionsDlg d;
    d.m_UndoRedoMemorySize = theApp.m_UndoRedoMemorySize;
    if ( d.DoModal() != IDOK )
      return false;
    m_UndoRedoMemorySize = d.m_UndoRedoMemorySize;
    return true;
  }

  /*override*/ bool Execute()
  {
    theApp.m_UndoRedoMemorySize = m_UndoRedoMemorySize;

    POSITION DocTemplatePos = theApp.GetFirstDocTemplatePosition();
    while ( DocTemplatePos )
    {
      CDocTemplate* pDocTemplate = theApp.GetNextDocTemplate( DocTemplatePos );
      POSITION DocPos = pDocTemplate->GetFirstDocPosition();
      while ( DocPos )
      {
        CDocument* pDoc = pDocTemplate->GetNextDoc( DocPos );
        if ( pDoc->IsKindOf( RUNTIME_CLASS( CKisActionDemoDoc ) ) )
          static_cast<CKisActionDemoDoc*>( pDoc )->m_spActionExecutor->SetMaxBytesCount( m_UndoRedoMemorySize );
      }
    }
    return true;
  }

  /*override*/ bool HasUnexecute() const
  {
    return false;
  }

  /*override*/ void Unexecute()
  {
    // nop
  }

  /*override*/ bool HasReexecute() const
  {
    return false;
  }

  /*override*/ void Reexecute()
  {
    // nop
  }

  /*override*/ void GetName( unsigned char* a_Name, const unsigned char a_MaxNameLen ) const
  {
    CString Name;
    _tcsncpy( reinterpret_cast<TCHAR*>(a_Name), Name, (a_MaxNameLen - 1) / sizeof( TCHAR ) );
  }

  /*override*/ unsigned int GetBytesCount() const
  {
    return sizeof( *this );
  }

protected:

  unsigned int m_UndoRedoMemorySize;

  /*override*/ ~C_DemoAction_ChangeAppOptions()
  {
    // nop
  }

};


} // namespace


SP_Action C_Factory::CreateAction_ClearDrawing()
{
  return SP_Action( new C_DemoAction_ChangeAppOptions() );
}

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
zdf
Romania Romania
Just a humble programmer.

Comments and Discussions