Click here to Skip to main content
15,884,388 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.h $ 

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


#pragma once


//-------------------------------------------------------------------------
/** C_TmpFStream class is a temporary file stream. The file
    is created and opened on construction. On destruction
    the stream is closed and deleted. */
class C_TmpFStream : public std::fstream
{

public:

  C_TmpFStream()
  {
    _CreateTmpFile();
    open( m_FullPath, ios_base::out | ios_base::in );
  }

  C_TmpFStream( const C_TmpFStream& a_Src )
  {
    _CreateTmpFile();
    VERIFY( ::CopyFile( a_Src.m_FullPath, m_FullPath, FALSE ) );
    open( m_FullPath, ios_base::out | ios_base::in );
  }

  ~C_TmpFStream()
  {
    close();
    VERIFY( ::DeleteFile( m_FullPath ) );
  }
  

protected:

  TCHAR m_FullPath[MAX_PATH];

  void _CreateTmpFile()
  {
    TCHAR Path[MAX_PATH];
    VERIFY( GetTempPath( sizeof(Path)/sizeof(Path[0]), Path ) );
    VERIFY( GetTempFileName( Path, _T("kis"), 0, m_FullPath ) );
  }

};


//-------------------------------------------------------------------------
/** C_DemoAction is the base class for all actions in this 
    application. */
class C_DemoAction : public kis::C_Action
{

public:

  C_DemoAction()
  :
  m_RefCount( 0 )
  {
    // nop
  }

  /*override*/ unsigned long AddRef()
  {
    return ++m_RefCount;
  }

  /*override*/ unsigned long Release()
  {
    if ( ! --m_RefCount )
    {
      delete this;
      return 0;
    }
    return m_RefCount;
  }

protected:

  unsigned int m_RefCount;

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

};


//-------------------------------------------------------------------------
/** C_DemoActionWithMouseInput is the base class of all mouse driven
    actions  */
class C_DemoActionWithMouseInput : public C_DemoAction
{

protected:
  
  class CView* m_pView;

  C_DemoActionWithMouseInput( class CView* a_pView );
  C_DemoActionWithMouseInput( const C_DemoActionWithMouseInput& a_Src );

  /*override*/ bool AcquireInput();

  void _DPToHimetric( CPoint* a_pPoint );

  virtual void _OnFirstPoint( const CPoint& a_Point, bool a_Alt ) = 0;
  virtual void _OnNextPoint( const CPoint& a_Point, bool a_Alt ) = 0;
  virtual void _OnLastPoint( const CPoint& a_Point, bool a_Alt ) = 0;
  virtual void _OnCancelled() = 0;

};

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