Click here to Skip to main content
15,883,910 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_AddRandomSegment.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;


namespace
{


class C_DemoAction_AddRandomSegment : public C_DemoAction
{

public:
  C_DemoAction_AddRandomSegment( CKisActionDemoDoc* a_pDoc )
  :
  m_pDoc( a_pDoc ),
  m_Idx( -1 )
  {
    // nop
  }

  C_DemoAction_AddRandomSegment( const C_DemoAction_AddRandomSegment& a_Src )
  :
  m_pDoc( a_Src.m_pDoc ),
  m_Idx( a_Src.m_Idx ),
  m_Segment( a_Src.m_Segment )
  {
    // nop
  }

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

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

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

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

  /*override*/ bool AcquireInput()
  {
    while ( m_Segment.IsEmpty() )
    {
      m_Segment.SetStartPoint( CPoint( rand() % 10000, -rand() % 10000 ) );
      m_Segment.SetEndPoint( CPoint( rand() % 10000, -rand() % 10000 ) );
    }
    return true;
  }

  /*override*/ bool Execute()
  {
    m_Idx = m_pDoc->m_Drawing.Add( m_Segment );
    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_Segment );
    ASSERT( Idx == m_Idx );
  }

  /*override*/ void GetName( unsigned char* a_Name, const unsigned char a_MaxNameLen ) const
  {
    CString Name;
    Name.Format
      ( 
      IDS_ACTION_ADD_SEGMENT_P1_P2, 
      double(m_Segment.GetStartPoint().x) / 100., 
      - double(m_Segment.GetStartPoint().y) / 100.,
      double(m_Segment.GetEndPoint().x) / 100., 
      - double(m_Segment.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;
  C_Graphic_Segment m_Segment;
  unsigned int m_Idx;

  /*override*/ ~C_DemoAction_AddRandomSegment()
  {
    // nop
  }
};


} // namespace


SP_Action C_Factory::CreateAction_AddRandomSegment( CKisActionDemoDoc* a_pDoc )
{
  return SP_Action( new C_DemoAction_AddRandomSegment( 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