diff options
-rw-r--r-- | svl/inc/svl/undo.hxx | 44 | ||||
-rw-r--r-- | svl/source/undo/undo.cxx | 155 |
2 files changed, 69 insertions, 130 deletions
diff --git a/svl/inc/svl/undo.hxx b/svl/inc/svl/undo.hxx index e92e337dc056..03b6381ac255 100644 --- a/svl/inc/svl/undo.hxx +++ b/svl/inc/svl/undo.hxx @@ -161,31 +161,31 @@ namespace svl virtual void SetMaxUndoActionCount( USHORT nMaxUndoActionCount ) = 0; virtual USHORT GetMaxUndoActionCount() const = 0; - virtual void Clear() = 0; virtual void AddUndoAction( SfxUndoAction *pAction, BOOL bTryMerg=FALSE ) = 0; virtual USHORT GetUndoActionCount() const = 0; - virtual USHORT GetUndoActionId(USHORT nNo=0) const = 0; + virtual USHORT GetUndoActionId() const = 0; virtual UniString GetUndoActionComment( USHORT nNo=0 ) const = 0; - /** returns the nNo'th undo action from the top */ virtual SfxUndoAction* GetUndoAction( USHORT nNo=0 ) const = 0; - virtual BOOL Undo() = 0; - virtual USHORT GetRedoActionCount() const = 0; - virtual USHORT GetRedoActionId(USHORT nNo=0) const = 0; virtual UniString GetRedoActionComment( USHORT nNo=0 ) const = 0; + virtual BOOL Undo() = 0; virtual BOOL Redo() = 0; + + virtual void Clear() = 0; virtual void ClearRedo() = 0; + /** detrmines whether an Undo or Redo is currently running + */ + virtual bool IsDoing() const = 0; + virtual USHORT GetRepeatActionCount() const = 0; - virtual UniString GetRepeatActionComment( SfxRepeatTarget &rTarget, USHORT nNo = 0) const = 0; - virtual BOOL Repeat( SfxRepeatTarget &rTarget, USHORT nFrom=0, USHORT nCount=1 ) = 0; - virtual void Repeat( SfxRepeatTarget &rTarget, SfxUndoAction &rAction ) = 0; - virtual BOOL CanRepeat( SfxRepeatTarget &rTarget, USHORT nNo = 0 ) const = 0; - virtual BOOL CanRepeat( SfxRepeatTarget &rTarget, SfxUndoAction &rAction ) const = 0; + virtual UniString GetRepeatActionComment( SfxRepeatTarget &rTarget) const = 0; + virtual BOOL Repeat( SfxRepeatTarget &rTarget ) = 0; + virtual BOOL CanRepeat( SfxRepeatTarget &rTarget ) const = 0; virtual void EnterListAction(const UniString &rComment, const UniString& rRepeatComment, USHORT nId=0) = 0; /** leaves the list action entered with EnterListAction @@ -234,24 +234,22 @@ public: // IUndoManager overridables virtual void SetMaxUndoActionCount( USHORT nMaxUndoActionCount ); virtual USHORT GetMaxUndoActionCount() const; - virtual void Clear(); virtual void AddUndoAction( SfxUndoAction *pAction, BOOL bTryMerg=FALSE ); virtual USHORT GetUndoActionCount() const; - virtual USHORT GetUndoActionId(USHORT nNo=0) const; + virtual USHORT GetUndoActionId() const; virtual UniString GetUndoActionComment( USHORT nNo=0 ) const; - SfxUndoAction* GetUndoAction( USHORT nNo=0 ) const; - virtual BOOL Undo(); + virtual SfxUndoAction* GetUndoAction( USHORT nNo=0 ) const; virtual USHORT GetRedoActionCount() const; - virtual USHORT GetRedoActionId(USHORT nNo=0) const; virtual UniString GetRedoActionComment( USHORT nNo=0 ) const; + virtual BOOL Undo(); virtual BOOL Redo(); + virtual void Clear(); virtual void ClearRedo(); + virtual bool IsDoing() const; virtual USHORT GetRepeatActionCount() const; - virtual UniString GetRepeatActionComment( SfxRepeatTarget &rTarget, USHORT nNo) const; - virtual BOOL Repeat( SfxRepeatTarget &rTarget, USHORT nFrom=0, USHORT nCount=1 ); - virtual void Repeat( SfxRepeatTarget &rTarget, SfxUndoAction &rAction ); - virtual BOOL CanRepeat( SfxRepeatTarget &rTarget, USHORT nNo ) const; - virtual BOOL CanRepeat( SfxRepeatTarget &rTarget, SfxUndoAction &rAction ) const; + virtual UniString GetRepeatActionComment( SfxRepeatTarget &rTarget) const; + virtual BOOL Repeat( SfxRepeatTarget &rTarget ); + virtual BOOL CanRepeat( SfxRepeatTarget &rTarget ) const; virtual void EnterListAction(const UniString &rComment, const UniString& rRepeatComment, USHORT nId=0); virtual USHORT LeaveListAction(); virtual bool IsInListAction() const; @@ -261,10 +259,6 @@ public: virtual bool IsUndoEnabled() const; virtual void AddUndoListener( SfxUndoListener& i_listener ); virtual void RemoveUndoListener( SfxUndoListener& i_listener ); - -protected: - void ImplUndo( SfxUndoAction &rAction ); - void ImplRedo( SfxUndoAction &rAction ); }; //========================================================================= diff --git a/svl/source/undo/undo.cxx b/svl/source/undo/undo.cxx index a413d558f443..69f10829a992 100644 --- a/svl/source/undo/undo.cxx +++ b/svl/source/undo/undo.cxx @@ -30,6 +30,7 @@ #include <com/sun/star/uno/Exception.hpp> +#include <comphelper/scopeguard.hxx> #include <tools/debug.hxx> #include <tools/diagnose_ex.h> @@ -162,6 +163,7 @@ struct SfxUndoManager_Data SfxUndoArray* pFatherUndoArray; bool mbUndoEnabled; + bool mbDoing; UndoListeners aListeners; @@ -170,6 +172,7 @@ struct SfxUndoManager_Data ,pActUndoArray( NULL ) ,pFatherUndoArray( NULL ) ,mbUndoEnabled( true ) + ,mbDoing( false ) { pActUndoArray = pUndoArray; @@ -390,12 +393,12 @@ XubString SfxUndoManager::GetUndoActionComment( USHORT nNo ) const //------------------------------------------------------------------------ -USHORT SfxUndoManager::GetUndoActionId( USHORT nNo ) const +USHORT SfxUndoManager::GetUndoActionId() const { - DBG_ASSERT( nNo < m_pData->pActUndoArray->nCurUndoAction, "svl::SfxUndoManager::GetUndoActionId(), illegal id!" ); - if( nNo < m_pData->pActUndoArray->nCurUndoAction ) + DBG_ASSERT( m_pData->pActUndoArray->nCurUndoAction > 0, "svl::SfxUndoManager::GetUndoActionId(), illegal id!" ); + if( m_pData->pActUndoArray->nCurUndoAction > 0 ) { - return m_pData->pActUndoArray->aUndoActions[m_pData->pActUndoArray->nCurUndoAction-1-nNo]->GetId(); //! + return m_pData->pActUndoArray->aUndoActions[m_pData->pActUndoArray->nCurUndoAction-1]->GetId(); } else { @@ -441,55 +444,41 @@ void SfxUndoManager::RemoveLastUndoAction() //------------------------------------------------------------------------ -BOOL SfxUndoManager::Undo() +bool SfxUndoManager::IsDoing() const { - bool bUndoWasEnabled = m_pData->mbUndoEnabled; - m_pData->mbUndoEnabled = false; - - BOOL bRet = FALSE; - - try - { - DBG_ASSERT( m_pData->pActUndoArray == m_pData->pUndoArray, "svl::SfxUndoManager::Undo(), LeaveListAction() not yet called!" ); - if ( m_pData->pActUndoArray->nCurUndoAction ) - { - ImplUndo( *m_pData->pActUndoArray->aUndoActions[ --m_pData->pActUndoArray->nCurUndoAction ] ); - bRet = TRUE; - } - } - catch( const Exception& ) - { - m_pData->mbUndoEnabled = bUndoWasEnabled; - throw; - } - m_pData->mbUndoEnabled = bUndoWasEnabled; - return bRet; + DBG_TESTSOLARMUTEX(); + return m_pData->mbDoing; } //------------------------------------------------------------------------ -void SfxUndoManager::ImplUndo( SfxUndoAction &rAction ) +BOOL SfxUndoManager::Undo() { - bool bUndoWasEnabled = m_pData->mbUndoEnabled; - m_pData->mbUndoEnabled = false; - try + DBG_TESTSOLARMUTEX(); + OSL_ENSURE( !IsDoing(), "SfxUndoManager::Undo: *nested* Undo/Redo actions? How this?" ); + + ::comphelper::FlagGuard aGuard( m_pData->mbDoing ); + ::comphelper::FlagRestorationGuard aEnableGuard( m_pData->mbUndoEnabled, false ); + + BOOL bRet = FALSE; + + DBG_ASSERT( m_pData->pActUndoArray == m_pData->pUndoArray, "svl::SfxUndoManager::Undo(), LeaveListAction() not yet called!" ); + if ( m_pData->pActUndoArray->nCurUndoAction ) { - rAction.Undo(); + SfxUndoAction* pAction = m_pData->pActUndoArray->aUndoActions[ --m_pData->pActUndoArray->nCurUndoAction ]; + pAction->Undo(); + bRet = TRUE; + for ( UndoListeners::const_iterator listener = m_pData->aListeners.begin(); listener != m_pData->aListeners.end(); ++listener ) { - (*listener)->actionUndone( rAction ); + (*listener)->actionUndone( *pAction ); } } - catch( const Exception& ) - { - m_pData->mbUndoEnabled = bUndoWasEnabled; - throw; - } - m_pData->mbUndoEnabled = bUndoWasEnabled; + return bRet; } //------------------------------------------------------------------------ @@ -508,63 +497,32 @@ XubString SfxUndoManager::GetRedoActionComment( USHORT nNo ) const //------------------------------------------------------------------------ -USHORT SfxUndoManager::GetRedoActionId( USHORT nNo ) const -{ - return m_pData->pActUndoArray->aUndoActions[m_pData->pActUndoArray->nCurUndoAction+nNo]->GetId(); //! -} - -//------------------------------------------------------------------------ - BOOL SfxUndoManager::Redo() { - bool bUndoWasEnabled = m_pData->mbUndoEnabled; - m_pData->mbUndoEnabled = false; + DBG_TESTSOLARMUTEX(); + OSL_ENSURE( !IsDoing(), "SfxUndoManager::Redo: *nested* Undo/Redo actions? How this?" ); + + ::comphelper::FlagGuard aGuard( m_pData->mbDoing ); + ::comphelper::FlagRestorationGuard aEnableGuard( m_pData->mbUndoEnabled, false ); BOOL bRet = FALSE; - try + if ( m_pData->pActUndoArray->aUndoActions.Count() > m_pData->pActUndoArray->nCurUndoAction ) { - if ( m_pData->pActUndoArray->aUndoActions.Count() > m_pData->pActUndoArray->nCurUndoAction ) - { - ImplRedo( *m_pData->pActUndoArray->aUndoActions[m_pData->pActUndoArray->nCurUndoAction++] ); - bRet = TRUE; - } - } - catch( const Exception& ) - { - m_pData->mbUndoEnabled = bUndoWasEnabled; - throw; - } - - m_pData->mbUndoEnabled = bUndoWasEnabled; - return bRet; -} - -//------------------------------------------------------------------------ + SfxUndoAction* pAction = m_pData->pActUndoArray->aUndoActions[m_pData->pActUndoArray->nCurUndoAction++]; + pAction->Redo(); + bRet = TRUE; -void SfxUndoManager::ImplRedo( SfxUndoAction &rAction ) -{ - bool bUndoWasEnabled = m_pData->mbUndoEnabled; - m_pData->mbUndoEnabled = false; - - try - { - rAction.Redo(); for ( UndoListeners::const_iterator listener = m_pData->aListeners.begin(); listener != m_pData->aListeners.end(); ++listener ) { - (*listener)->actionRedone( rAction ); + (*listener)->actionRedone( *pAction ); } } - catch( const Exception& ) - { - m_pData->mbUndoEnabled = bUndoWasEnabled; - throw; - } - m_pData->mbUndoEnabled = bUndoWasEnabled; + return bRet; } //------------------------------------------------------------------------ @@ -576,19 +534,21 @@ USHORT SfxUndoManager::GetRepeatActionCount() const //------------------------------------------------------------------------ -XubString SfxUndoManager::GetRepeatActionComment( SfxRepeatTarget &rTarget, USHORT nNo ) const +XubString SfxUndoManager::GetRepeatActionComment( SfxRepeatTarget &rTarget) const { - return m_pData->pActUndoArray->aUndoActions[ m_pData->pActUndoArray->aUndoActions.Count() - 1 - nNo ] + return m_pData->pActUndoArray->aUndoActions[ m_pData->pActUndoArray->aUndoActions.Count() - 1 ] ->GetRepeatComment(rTarget); } //------------------------------------------------------------------------ -BOOL SfxUndoManager::Repeat( SfxRepeatTarget &rTarget, USHORT /*nFrom*/, USHORT /*nCount*/ ) +BOOL SfxUndoManager::Repeat( SfxRepeatTarget &rTarget ) { if ( m_pData->pActUndoArray->aUndoActions.Count() ) { - Repeat( rTarget, *m_pData->pActUndoArray->aUndoActions[ m_pData->pActUndoArray->aUndoActions.Count() - 1 ] ); + SfxUndoAction* pAction = m_pData->pActUndoArray->aUndoActions[ m_pData->pActUndoArray->aUndoActions.Count() - 1 ]; + if ( pAction->CanRepeat( rTarget ) ) + pAction->Repeat( rTarget ); return TRUE; } @@ -597,26 +557,11 @@ BOOL SfxUndoManager::Repeat( SfxRepeatTarget &rTarget, USHORT /*nFrom*/, USHORT //------------------------------------------------------------------------ -void SfxUndoManager::Repeat( SfxRepeatTarget &rTarget, SfxUndoAction &rAction ) -{ - if ( rAction.CanRepeat(rTarget) ) - rAction.Repeat(rTarget); -} - -//------------------------------------------------------------------------ - -BOOL SfxUndoManager::CanRepeat( SfxRepeatTarget &rTarget, SfxUndoAction &rAction ) const +BOOL SfxUndoManager::CanRepeat( SfxRepeatTarget &rTarget ) const { - return rAction.CanRepeat(rTarget); -} - -//------------------------------------------------------------------------ - -BOOL SfxUndoManager::CanRepeat( SfxRepeatTarget &rTarget, USHORT nNo ) const -{ - if ( m_pData->pActUndoArray->aUndoActions.Count() > nNo ) + if ( m_pData->pActUndoArray->aUndoActions.Count() > 0 ) { - USHORT nActionNo = m_pData->pActUndoArray->aUndoActions.Count() - 1 - nNo; + USHORT nActionNo = m_pData->pActUndoArray->aUndoActions.Count() - 1; return m_pData->pActUndoArray->aUndoActions[nActionNo]->CanRepeat(rTarget); } @@ -904,7 +849,7 @@ void SfxLinkUndoAction::Redo() BOOL SfxLinkUndoAction::CanRepeat(SfxRepeatTarget& r) const { - return pAction && pUndoManager->CanRepeat(r,*pAction); + return pAction && pAction->CanRepeat(r); } @@ -913,8 +858,8 @@ BOOL SfxLinkUndoAction::CanRepeat(SfxRepeatTarget& r) const void SfxLinkUndoAction::Repeat(SfxRepeatTarget&r) { - if ( pAction ) - pUndoManager->Repeat(r,*pAction); + if ( pAction && pAction->CanRepeat( r ) ) + pAction->Repeat( r ); } |