|
//------------------------------------------------------------------------------
// $Workfile: UndoRedoMgr.cpp $
// $Header: /SbjDev/SbjCore/UndoRedoMgr.cpp 4 11/12/08 2:22p Steve $
//
// Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
// Steve Johnson
//
// $Revision: 4 $
//
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "UndoRedoMgr.h"
#include "UndoRedoHandler.h"
#include "ModelController.h"
namespace SbjCore
{
namespace UndoRedo
{
struct ManagerImpl
{
typedef std::stack<Handler*> HandlerStack;
CMFCRibbonUndoButton* pTheUndoButton;
CMFCRibbonUndoButton* pTheRedoButton;
CStringList theUndoNameList;
CStringList theRedoNameList;
HandlerStack theUndoStack;
HandlerStack theRedoStack;
int nDepth;
ManagerImpl() :
pTheUndoButton(NULL),
pTheRedoButton(NULL),
nDepth(1)
{
}
virtual ~ManagerImpl()
{
theUndoNameList.RemoveAll();
theRedoNameList.RemoveAll();
ClearUndo();
ClearRedo();
}
void Push(Handler* p)
{
theUndoStack.push(p);
theUndoNameList.AddHead(p->GetHandlerName());
ClearRedo();
}
void ClearUndo()
{
while (!theUndoStack.empty())
{
Handler* pTheHandler = theUndoStack.top();
theUndoStack.pop();
delete pTheHandler;
}
theUndoNameList.RemoveAll();
}
void ClearRedo()
{
while (!theRedoStack.empty())
{
Handler* pTheHandler = theRedoStack.top();
theRedoStack.pop();
delete pTheHandler;
}
theRedoNameList.RemoveAll();
}
bool Undo(int nCount)
{
bool bRslt = false;
for (int nIndex = 0; nIndex < nCount; nIndex++)
{
Handler* pTheHandler = theUndoStack.top();
theUndoStack.pop();
theUndoNameList.RemoveHead();
bRslt = pTheHandler->HandleUndo();
if (bRslt)
{
theRedoStack.push(pTheHandler);
theRedoNameList.AddHead(pTheHandler->GetHandlerName());
}
else
{
ClearUndo();
break;
}
}
return bRslt;
}
bool Redo(int nCount)
{
bool bRslt = false;
for (int nIndex = 0; nIndex < nCount; nIndex++)
{
Handler* pTheHandler = theRedoStack.top();
theRedoStack.pop();
theRedoNameList.RemoveHead();
bRslt = pTheHandler->HandleRedo();
if (bRslt)
{
theUndoStack.push(pTheHandler);
theUndoNameList.AddHead(pTheHandler->GetHandlerName());
}
else
{
ClearRedo();
break;
}
}
return bRslt;
}
};
///////////////////////////////////////////////////////////////////////////////////////
Manager::Manager() :
m_pImpl(new ManagerImpl)
{
}
Manager::~Manager()
{
try
{
delete m_pImpl;
}
catch (...)
{
ASSERT(FALSE);
}
}
void Manager::Push(Handler* p)
{
m_pImpl->Push(p);
}
bool Manager::Undo(int nCount)
{
return m_pImpl->Undo(nCount);
}
bool Manager::Redo(int nCount)
{
return m_pImpl->Redo(nCount);
}
void Manager::ClearUndo()
{
m_pImpl->ClearUndo();
}
void Manager::ClearRedo()
{
m_pImpl->ClearRedo();
}
CStringList& Manager::GetUndoList() const
{
return m_pImpl->theUndoNameList;
}
CStringList& Manager::GetRedoList() const
{
return m_pImpl->theRedoNameList;
}
bool Manager::EnableUndo() const
{
return m_pImpl->theUndoNameList.GetCount() > 0;
}
bool Manager::EnableRedo() const
{
return m_pImpl->theRedoNameList.GetCount() > 0;
}
void Manager::SetUndoButton( CMFCRibbonUndoButton* p )
{
m_pImpl->pTheUndoButton = p;
}
CMFCRibbonUndoButton* Manager::GetUndoButton() const
{
return m_pImpl->pTheUndoButton;
}
void Manager::SetRedoButton( CMFCRibbonUndoButton* p )
{
m_pImpl->pTheRedoButton = p;
}
CMFCRibbonUndoButton* Manager::GetRedoButton() const
{
return m_pImpl->pTheRedoButton;
}
}
}
//*** Modification History ***
// $Log: /SbjDev/SbjCore/UndoRedoMgr.cpp $
//
// 4 11/12/08 2:22p Steve
// Finished Generalization of Model v2.0.1
//
// 3 10/24/08 9:03a Steve
|
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.
Real name is Steve Johnson. Programming since 1979. Started on a Heathkit Micro with a DEC LSI-11 and UCSD Pascal. Moved to PCs & DOS as soon as Turbo Pascal became available. Did some Assembly, ISR, TSR etc. All this while working for a Manufacturing Co. for 8 years. Had my own solo Co. doing barcode labeling software for 4 years (terrible business man, all I wanted to do was code). Since then working for various software companies. Moved to Windows around the time of 3.1 with Borland C then C++. Then on to VC++ and MFC, and just about anything I could get my hands on or had to learn for my job, and been at it ever since. Of course recently I've been playing with .NET, ASP, C#, WPF etc.