Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

A Simple, Action Based, Undo/Redo Framework

, 16 Feb 2013
How to use a simple, action based, undo/redo framework
kiswinbin_v_1.zip
kis
kis.dll
kis.lib
KisWinBin_v_1_r_1.zip
KisWinBin_v_1_r_1
kis
kis.dll
kis.lib
kiswinsrc_v_1.zip
KisActionDemo
res
KisActionDemo.ico
KisActionDemoDoc.ico
Toolbar.bmp
vssver.scc
KisWinSrc_v_1_r_1.zip
KisWinSrc_v_1_r_1
kis
KisActionDemo
res
KisActionDemo.ico
KisActionDemoDoc.ico
Toolbar.bmp
vssver.scc
/*
***************************************************************************

kis v 1

(c) 2006, Florin DUMITRESCU

mailto: fdproxy@gmail.com

$Workfile: KisActionDemoDoc.cpp $ 

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


#include "stdafx.h"
#include "KisActionDemo.h"
#include "KisActionDemoDoc.h"
#include "_Factory.h"
#include ".\kisactiondemodoc.h"
#include "resource.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


#ifdef _DEBUG

void CKisActionDemoDoc::AssertValid() const
{
	CDocument::AssertValid();
}


void CKisActionDemoDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}

#endif _DEBUG

IMPLEMENT_DYNCREATE(CKisActionDemoDoc, CDocument)

BEGIN_MESSAGE_MAP(CKisActionDemoDoc, CDocument)
  
  ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
  ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)

  ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
  ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)

  ON_COMMAND(ID_ACTION_ADDRANDOMSEGMENT, OnActionAddRandomSegment)

  ON_COMMAND(ID_ACTION_ADDSEGMENT, OnActionAddsegment)
  ON_UPDATE_COMMAND_UI(ID_ACTION_ADDSEGMENT, OnUpdateActionAddsegment)

  ON_COMMAND(ID_ACTION_ADDRECTANGLE, OnActionAddrectangle)
  ON_UPDATE_COMMAND_UI(ID_ACTION_ADDRECTANGLE, OnUpdateActionAddrectangle)

  ON_COMMAND(ID_ACTION_ADDELLIPSE, OnActionAddellipse)
  ON_UPDATE_COMMAND_UI(ID_ACTION_ADDELLIPSE, OnUpdateActionAddellipse)

  ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnTipText)
  ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnTipText)

  ON_COMMAND(ID_ACTION_CLEARDRAWING, OnActionClearDrawing)

END_MESSAGE_MAP()

CKisActionDemoDoc::CKisActionDemoDoc()
{
  m_spActionExecutor = C_Factory().CreateActionExecutor();
  m_spActionExecutor->SetMaxBytesCount( theApp.m_UndoRedoMemorySize );
  m_ActiveTool = ID_ACTION_ADDSEGMENT;
}

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

void CKisActionDemoDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

BOOL CKisActionDemoDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
  //
	return TRUE;
}

BOOL CKisActionDemoDoc::OnTipText( UINT id, NMHDR* pNMHDR, LRESULT* /*pResult*/ )
{
	ASSERT( pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW );

	TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
	TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
	CString TipText;

	UINT_PTR Id = pNMHDR->idFrom;
	if ( pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) 
    || pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND) )
    Id = ((UINT)(WORD)::GetDlgCtrlID((HWND)Id));

  switch ( Id )
  {
  case ID_EDIT_REDO:
    TipText = _GetRedoText();
    break;
  case ID_EDIT_UNDO:
    TipText = _GetUndoText();
    break;
  default:
    return FALSE;
  }

#ifndef _UNICODE
	if ( pNMHDR->code == TTN_NEEDTEXTA )
		lstrcpyn(pTTTA->szText, TipText, sizeof(pTTTA->szText)/sizeof(pTTTA->szText[0]));
	else
		_mbstowcsz(pTTTW->szText, TipText, sizeof(pTTTW->szText)/sizeof(pTTTW->szText[0]));
#else
	if (pNMHDR->code == TTN_NEEDTEXTA)
		_wcstombsz(pTTTA->szText, TipText, sizeof(pTTTA->szText)/sizeof(pTTTA->szText[0]));
	else
		lstrcpyn(pTTTW->szText, TipText, sizeof(pTTTW->szText)/sizeof(pTTTW->szText[0]));
#endif

	::SetWindowPos
    (
    pNMHDR->hwndFrom, 
    HWND_TOP, 
    0, 
    0, 
    0, 
    0,
		SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE
    );

	return TRUE;
}


//-------------------------------------------------------------------------
// UNDO
//-------------------------------------------------------------------------


CString CKisActionDemoDoc::_GetUndoText(void)
{
  TCHAR Text[ c_MaxUndoRedoTextLen ] = _T("");
  if ( m_spActionExecutor->GetUnexecuteCount() > 0 )
    m_spActionExecutor->GetUnexecuteName( (unsigned char*)Text, sizeof(Text)/sizeof(Text[0]), 0 );
  CString Result;
  Result.Format( IDS_FMT_UNDO, Text );
  return Result;
}

void CKisActionDemoDoc::OnEditUndo()
{
  m_spActionExecutor->Unexecute( 1 );
  UpdateAllViews( 0 );
}

void CKisActionDemoDoc::OnUpdateEditUndo(CCmdUI *pCmdUI)
{
  pCmdUI->SetText( _GetUndoText() );
  pCmdUI->Enable( m_spActionExecutor->GetUnexecuteCount() != 0 );
}


//-------------------------------------------------------------------------
// REDO
//-------------------------------------------------------------------------


CString CKisActionDemoDoc::_GetRedoText(void)
{
  TCHAR Text[ c_MaxUndoRedoTextLen ] = _T("");
  if ( m_spActionExecutor->GetReexecuteCount() > 0 )
    m_spActionExecutor->GetReexecuteName( (unsigned char*)Text, sizeof(Text)/sizeof(Text[0]), 0 );
  CString Result;
  Result.Format( IDS_FMT_REDO, Text );
  return Result;
}

void CKisActionDemoDoc::OnEditRedo()
{
  m_spActionExecutor->Reexecute( 1 );
  UpdateAllViews( 0 );
}

void CKisActionDemoDoc::OnUpdateEditRedo(CCmdUI *pCmdUI)
{
  pCmdUI->SetText( _GetRedoText() );
  pCmdUI->Enable( m_spActionExecutor->GetReexecuteCount() != 0 );
}


//-------------------------------------------------------------------------
// DRAWING ACTIONS
//-------------------------------------------------------------------------


void CKisActionDemoDoc::OnActionAddRandomSegment()
{
  m_spActionExecutor->Execute( C_Factory().CreateAction_AddRandomSegment( this ) );
}

void CKisActionDemoDoc::OnLButtonDown( CKisActionDemoView* a_pView )
{
  m_spActionExecutor->Execute( C_Factory().CreateAction( m_ActiveTool, this, a_pView ) );
}

void CKisActionDemoDoc::OnActionAddsegment()
{
  m_ActiveTool = ID_ACTION_ADDSEGMENT;
}

void CKisActionDemoDoc::OnUpdateActionAddsegment(CCmdUI *pCmdUI)
{
  pCmdUI->SetCheck( pCmdUI->m_nID == m_ActiveTool );
}

void CKisActionDemoDoc::OnActionAddrectangle()
{
  m_ActiveTool = ID_ACTION_ADDRECTANGLE;
}

void CKisActionDemoDoc::OnUpdateActionAddrectangle(CCmdUI *pCmdUI)
{
  pCmdUI->SetCheck( pCmdUI->m_nID == m_ActiveTool );
}

void CKisActionDemoDoc::OnActionAddellipse()
{
  m_ActiveTool = ID_ACTION_ADDELLIPSE;
}

void CKisActionDemoDoc::OnUpdateActionAddellipse(CCmdUI *pCmdUI)
{
  pCmdUI->SetCheck( pCmdUI->m_nID == m_ActiveTool );
}

void CKisActionDemoDoc::OnActionClearDrawing()
{
  m_spActionExecutor->Execute( C_Factory().CreateAction_ClearDrawing( this ) );
}

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)

About the Author

zdf

Romania Romania
Just a humble programmer.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 16 Feb 2013
Article Copyright 2006 by zdf
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid