Click here to Skip to main content
15,894,362 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.9K   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_ClearDrawing.cpp $ 

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


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


using namespace kis;
using namespace std;


namespace
{


class C_DemoAction_ClearDrawing : public C_DemoAction
{

public:
  C_DemoAction_ClearDrawing( CKisActionDemoDoc* a_pDoc )
  :
  m_pDoc( a_pDoc )
  {
    // nop
  }

  C_DemoAction_ClearDrawing( const C_DemoAction_ClearDrawing& a_Src )
  :
  m_pDoc( a_Src.m_pDoc ),
  m_Stream( a_Src.m_Stream )
  {
    // nop
  }

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

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

  /*override*/ bool IsNull() const
  {
    return m_pDoc->m_Drawing.IsEmpty();
  }

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

  /*override*/ bool AcquireInput()
  {
    return true;
  }

  /*override*/ bool Execute()
  {
    if ( ! m_Stream )
      return false;
    m_pDoc->m_Drawing.Write( m_Stream );
    m_pDoc->m_Drawing.Delete();
    m_pDoc->UpdateAllViews( 0 );
    return true;
  }

  /*override*/ bool HasUnexecute() const
  {
    return true;//m_Stream.good();
  }

  /*override*/ void Unexecute()
  {
    using namespace std;
    m_Stream.clear();
    m_Stream.seekg( 0 );
    m_pDoc->m_Drawing.Read( m_Stream );
    m_pDoc->UpdateAllViews( 0 );
  }

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

  /*override*/ void Reexecute()
  {
    m_pDoc->m_Drawing.Delete();
    m_pDoc->UpdateAllViews( 0 );
  }

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

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

protected:
  CKisActionDemoDoc* m_pDoc;
  C_TmpFStream m_Stream;

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

};


} // namespace


SP_Action C_Factory::CreateAction_ClearDrawing( CKisActionDemoDoc* a_pDoc )
{
  return SP_Action( new C_DemoAction_ClearDrawing( a_pDoc ) );
}

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