Click here to Skip to main content
15,883,802 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_AddGraphic.cpp $ 

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


#include "StdAfx.h"
#include "_DemoAction.h"
#include "KisActionDemoDoc.h"
#include "KisActionDemoView.h"
#include "resource.h"
#include "_Factory.h"


using namespace kis;


namespace
{


template < class GRAPHIC, UINT IDS_FMT_P1_P2 >
class C_DemoAction_AddGraphic : public C_DemoActionWithMouseInput
{

public:

  C_DemoAction_AddGraphic( CKisActionDemoDoc* a_pDoc, CKisActionDemoView* a_pView )
  :
  C_DemoActionWithMouseInput( a_pView ),
  m_pDoc( a_pDoc ),
  m_Idx( -1 )
  {
    // nop
  }

  C_DemoAction_AddGraphic( const C_DemoAction_AddGraphic& a_Src )
  :
  C_DemoActionWithMouseInput( (const C_DemoActionWithMouseInput&) a_Src ),
  m_pDoc( a_Src.m_pDoc ),
  m_Idx( a_Src.m_Idx ),
  m_Graphic( a_Src.m_Graphic )
  {
    // nop
  }

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

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

  /*override*/ bool IsNull() const
  {
    return m_Graphic.IsEmpty();
  }

  /*override*/ bool HasInput() const
  {
    return ! m_Graphic.IsEmpty();
  }

  /*override*/ bool Execute()
  {
    m_Idx = m_pDoc->m_Drawing.Add( m_Graphic );
    m_pDoc->UpdateAllViews( 0 );
    return true;
  }

  /*override*/ bool HasUnexecute() const
  {
    return m_Idx != -1;
  }

  /*override*/ void Unexecute()
  {
    ASSERT( m_Idx != -1 );
    m_pDoc->m_Drawing.RemoveAt( m_Idx );
  }

  /*override*/ bool HasReexecute() const
  {
    return m_Idx != -1;
  }

  /*override*/ void Reexecute()
  {
    int Idx = m_pDoc->m_Drawing.Add( m_Graphic );
    ASSERT( Idx == m_Idx );
  }

  /*override*/ void GetName( unsigned char* a_Name, const unsigned char a_MaxNameLen ) const
  {
    CString Name;
    Name.Format
      ( 
      IDS_FMT_P1_P2, 
      double(m_Graphic.GetStartPoint().x) / 100., 
      - double(m_Graphic.GetStartPoint().y) / 100.,
      double(m_Graphic.GetEndPoint().x) / 100., 
      - double(m_Graphic.GetEndPoint().y) / 100.
      );
    _tcsncpy( reinterpret_cast<TCHAR*>(a_Name), Name, (a_MaxNameLen - 1) / sizeof( TCHAR ) );
  }

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

protected:

  CKisActionDemoDoc* m_pDoc;
  GRAPHIC m_Graphic;
  unsigned int m_Idx;

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

  /*override*/ void _OnFirstPoint( const CPoint& a_Point, bool a_Alt )
  {
    GRAPHIC Graphic;
    Graphic.SetStartPoint( a_Point );
    Graphic.SetEndPoint( a_Point );
    m_Idx = m_pDoc->m_Drawing.Add( Graphic );
    m_pView->RedrawWindow();
  }

  /*override*/ void _OnNextPoint( const CPoint& a_Point, bool a_Alt )
  {
    static_cast< GRAPHIC& >( m_pDoc->m_Drawing.At( m_Idx ) ).SetEndPoint( a_Point );
    m_pView->RedrawWindow();
  }

  /*override*/ void _OnLastPoint( const CPoint& a_Point, bool a_Alt )
  {
    m_Graphic = static_cast< GRAPHIC& >( m_pDoc->m_Drawing.At( m_Idx ) );
    m_Graphic.SetEndPoint( a_Point );
    m_pDoc->m_Drawing.RemoveAt( m_Idx );
    m_Idx = -1;
  }

  /*override*/ void _OnCancelled()
  {
    m_pDoc->m_Drawing.RemoveAt( m_Idx );
    m_Idx = -1;
    m_pView->RedrawWindow();
  }

};

typedef C_DemoAction_AddGraphic< C_Graphic_Segment, IDS_ACTION_ADD_SEGMENT_P1_P2 > C_DemoAction_AddSegment;

typedef C_DemoAction_AddGraphic< C_Graphic_Rectangle, IDS_ACTION_ADD_RECTANGLE_P1_P2 > C_DemoAction_AddRectangle;

typedef C_DemoAction_AddGraphic< C_Graphic_Ellipse, IDS_ACTION_ADD_ELLIPSE_P1_P2 > C_DemoAction_AddEllipse;


} // namespace


SP_Action C_Factory::CreateAction_AddSegment( CKisActionDemoDoc* a_pDoc, CKisActionDemoView* a_pView )
{
  return SP_Action( new C_DemoAction_AddSegment( a_pDoc, a_pView ) );
}


SP_Action C_Factory::CreateAction_AddRectangle( CKisActionDemoDoc* a_pDoc, CKisActionDemoView* a_pView )
{
  return SP_Action( new C_DemoAction_AddRectangle( a_pDoc, a_pView ) );
}


SP_Action C_Factory::CreateAction_AddEllipse( CKisActionDemoDoc* a_pDoc, CKisActionDemoView* a_pView )
{
  return SP_Action( new C_DemoAction_AddEllipse( a_pDoc, a_pView ) );
}

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