140df464eSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 340df464eSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 440df464eSAndrew Rist * or more contributor license agreements. See the NOTICE file 540df464eSAndrew Rist * distributed with this work for additional information 640df464eSAndrew Rist * regarding copyright ownership. The ASF licenses this file 740df464eSAndrew Rist * to you under the Apache License, Version 2.0 (the 840df464eSAndrew Rist * "License"); you may not use this file except in compliance 940df464eSAndrew Rist * with the License. You may obtain a copy of the License at 1040df464eSAndrew Rist * 1140df464eSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 1240df464eSAndrew Rist * 1340df464eSAndrew Rist * Unless required by applicable law or agreed to in writing, 1440df464eSAndrew Rist * software distributed under the License is distributed on an 1540df464eSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1640df464eSAndrew Rist * KIND, either express or implied. See the License for the 1740df464eSAndrew Rist * specific language governing permissions and limitations 1840df464eSAndrew Rist * under the License. 1940df464eSAndrew Rist * 2040df464eSAndrew Rist *************************************************************/ 2140df464eSAndrew Rist 2240df464eSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_svl.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <com/sun/star/uno/Exception.hpp> 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include <comphelper/flagguard.hxx> 30cdf0e10cSrcweir #include <tools/debug.hxx> 31cdf0e10cSrcweir #include <tools/diagnose_ex.h> 32cdf0e10cSrcweir 33cdf0e10cSrcweir #include <svl/undo.hxx> 34cdf0e10cSrcweir 35cdf0e10cSrcweir #include <vector> 36cdf0e10cSrcweir #include <list> 37cdf0e10cSrcweir #include <limits> 38cdf0e10cSrcweir 39cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 40cdf0e10cSrcweir 41cdf0e10cSrcweir // STATIC DATA ----------------------------------------------------------- 42cdf0e10cSrcweir 43cdf0e10cSrcweir DBG_NAME(SfxUndoAction) 44cdf0e10cSrcweir 45cdf0e10cSrcweir //======================================================================== 46cdf0e10cSrcweir 47cdf0e10cSrcweir TYPEINIT0(SfxUndoAction); 48cdf0e10cSrcweir TYPEINIT0(SfxListUndoAction); 49cdf0e10cSrcweir TYPEINIT0(SfxLinkUndoAction); 50cdf0e10cSrcweir TYPEINIT0(SfxRepeatTarget); 51cdf0e10cSrcweir 52cdf0e10cSrcweir //------------------------------------------------------------------------ 53cdf0e10cSrcweir 54cdf0e10cSrcweir SfxRepeatTarget::~SfxRepeatTarget() 55cdf0e10cSrcweir { 56cdf0e10cSrcweir } 57cdf0e10cSrcweir 58cdf0e10cSrcweir //------------------------------------------------------------------------ 59cdf0e10cSrcweir 60cdf0e10cSrcweir SfxUndoContext::~SfxUndoContext() 61cdf0e10cSrcweir { 62cdf0e10cSrcweir } 63cdf0e10cSrcweir 64cdf0e10cSrcweir //------------------------------------------------------------------------ 65cdf0e10cSrcweir 66*01300968SArmin Le Grand void SfxUndoAction::SetLinkToSfxLinkUndoAction(SfxLinkUndoAction* pSfxLinkUndoAction) 67cdf0e10cSrcweir { 68*01300968SArmin Le Grand mpSfxLinkUndoAction = pSfxLinkUndoAction; 69cdf0e10cSrcweir } 70cdf0e10cSrcweir 71cdf0e10cSrcweir //------------------------------------------------------------------------ 72cdf0e10cSrcweir 73cdf0e10cSrcweir SfxUndoAction::~SfxUndoAction() 74cdf0e10cSrcweir { 75*01300968SArmin Le Grand DBG_DTOR(SfxUndoAction, 0); 76*01300968SArmin Le Grand 77*01300968SArmin Le Grand if(mpSfxLinkUndoAction) 78*01300968SArmin Le Grand { 79*01300968SArmin Le Grand mpSfxLinkUndoAction->LinkedSfxUndoActionDestructed(*this); 80*01300968SArmin Le Grand mpSfxLinkUndoAction = 0; 81*01300968SArmin Le Grand } 82cdf0e10cSrcweir } 83cdf0e10cSrcweir 84cdf0e10cSrcweir 85cdf0e10cSrcweir SfxUndoAction::SfxUndoAction() 86*01300968SArmin Le Grand : mpSfxLinkUndoAction(0) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir DBG_CTOR(SfxUndoAction, 0); 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir //------------------------------------------------------------------------ 92cdf0e10cSrcweir 93cdf0e10cSrcweir sal_Bool SfxUndoAction::Merge( SfxUndoAction * ) 94cdf0e10cSrcweir { 95cdf0e10cSrcweir DBG_CHKTHIS(SfxUndoAction, 0); 96cdf0e10cSrcweir return sal_False; 97cdf0e10cSrcweir } 98cdf0e10cSrcweir 99cdf0e10cSrcweir //------------------------------------------------------------------------ 100cdf0e10cSrcweir 101cdf0e10cSrcweir XubString SfxUndoAction::GetComment() const 102cdf0e10cSrcweir { 103cdf0e10cSrcweir DBG_CHKTHIS(SfxUndoAction, 0); 104cdf0e10cSrcweir return XubString(); 105cdf0e10cSrcweir } 106cdf0e10cSrcweir 107cdf0e10cSrcweir //------------------------------------------------------------------------ 108cdf0e10cSrcweir 109cdf0e10cSrcweir 110cdf0e10cSrcweir sal_uInt16 SfxUndoAction::GetId() const 111cdf0e10cSrcweir { 112cdf0e10cSrcweir DBG_CHKTHIS(SfxUndoAction, 0); 113cdf0e10cSrcweir return 0; 114cdf0e10cSrcweir } 115cdf0e10cSrcweir 116cdf0e10cSrcweir //------------------------------------------------------------------------ 117cdf0e10cSrcweir 118cdf0e10cSrcweir XubString SfxUndoAction::GetRepeatComment(SfxRepeatTarget&) const 119cdf0e10cSrcweir { 120cdf0e10cSrcweir DBG_CHKTHIS(SfxUndoAction, 0); 121cdf0e10cSrcweir return GetComment(); 122cdf0e10cSrcweir } 123cdf0e10cSrcweir 124cdf0e10cSrcweir //------------------------------------------------------------------------ 125cdf0e10cSrcweir 126cdf0e10cSrcweir void SfxUndoAction::Undo() 127cdf0e10cSrcweir { 128cdf0e10cSrcweir // die sind nur konzeptuell pure virtual 129cdf0e10cSrcweir DBG_ERROR( "pure virtual function called: SfxUndoAction::Undo()" ); 130cdf0e10cSrcweir } 131cdf0e10cSrcweir 132cdf0e10cSrcweir //------------------------------------------------------------------------ 133cdf0e10cSrcweir 134cdf0e10cSrcweir void SfxUndoAction::UndoWithContext( SfxUndoContext& i_context ) 135cdf0e10cSrcweir { 136cdf0e10cSrcweir (void)i_context; 137cdf0e10cSrcweir Undo(); 138cdf0e10cSrcweir } 139cdf0e10cSrcweir 140cdf0e10cSrcweir //------------------------------------------------------------------------ 141cdf0e10cSrcweir 142cdf0e10cSrcweir void SfxUndoAction::Redo() 143cdf0e10cSrcweir { 144cdf0e10cSrcweir // die sind nur konzeptuell pure virtual 145cdf0e10cSrcweir DBG_ERROR( "pure virtual function called: SfxUndoAction::Redo()" ); 146cdf0e10cSrcweir } 147cdf0e10cSrcweir 148cdf0e10cSrcweir //------------------------------------------------------------------------ 149cdf0e10cSrcweir 150cdf0e10cSrcweir void SfxUndoAction::RedoWithContext( SfxUndoContext& i_context ) 151cdf0e10cSrcweir { 152cdf0e10cSrcweir (void)i_context; 153cdf0e10cSrcweir Redo(); 154cdf0e10cSrcweir } 155cdf0e10cSrcweir 156cdf0e10cSrcweir //------------------------------------------------------------------------ 157cdf0e10cSrcweir 158cdf0e10cSrcweir void SfxUndoAction::Repeat(SfxRepeatTarget&) 159cdf0e10cSrcweir { 160cdf0e10cSrcweir // die sind nur konzeptuell pure virtual 161cdf0e10cSrcweir DBG_ERROR( "pure virtual function called: SfxUndoAction::Repeat()" ); 162cdf0e10cSrcweir } 163cdf0e10cSrcweir 164cdf0e10cSrcweir //------------------------------------------------------------------------ 165cdf0e10cSrcweir 166cdf0e10cSrcweir 167cdf0e10cSrcweir sal_Bool SfxUndoAction::CanRepeat(SfxRepeatTarget&) const 168cdf0e10cSrcweir { 169cdf0e10cSrcweir return sal_True; 170cdf0e10cSrcweir } 171cdf0e10cSrcweir 172cdf0e10cSrcweir //======================================================================== 173cdf0e10cSrcweir 174cdf0e10cSrcweir typedef ::std::vector< SfxUndoListener* > UndoListeners; 175cdf0e10cSrcweir 176cdf0e10cSrcweir struct SVL_DLLPRIVATE SfxUndoManager_Data 177cdf0e10cSrcweir { 178cdf0e10cSrcweir ::osl::Mutex aMutex; 179cdf0e10cSrcweir SfxUndoArray* pUndoArray; 180cdf0e10cSrcweir SfxUndoArray* pActUndoArray; 181cdf0e10cSrcweir SfxUndoArray* pFatherUndoArray; 182cdf0e10cSrcweir 183cdf0e10cSrcweir sal_Int32 mnMarks; 184cdf0e10cSrcweir sal_Int32 mnEmptyMark; 185cdf0e10cSrcweir bool mbUndoEnabled; 186cdf0e10cSrcweir bool mbDoing; 187cdf0e10cSrcweir bool mbClearUntilTopLevel; 188cdf0e10cSrcweir 189cdf0e10cSrcweir UndoListeners aListeners; 190cdf0e10cSrcweir 191cdf0e10cSrcweir SfxUndoManager_Data( size_t i_nMaxUndoActionCount ) 192cdf0e10cSrcweir :pUndoArray( new SfxUndoArray( i_nMaxUndoActionCount ) ) 193cdf0e10cSrcweir ,pActUndoArray( NULL ) 194cdf0e10cSrcweir ,pFatherUndoArray( NULL ) 195cdf0e10cSrcweir ,mnMarks( 0 ) 196cdf0e10cSrcweir ,mnEmptyMark(MARK_INVALID) 197cdf0e10cSrcweir ,mbUndoEnabled( true ) 198cdf0e10cSrcweir ,mbDoing( false ) 199cdf0e10cSrcweir ,mbClearUntilTopLevel( false ) 200cdf0e10cSrcweir { 201cdf0e10cSrcweir pActUndoArray = pUndoArray; 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir ~SfxUndoManager_Data() 205cdf0e10cSrcweir { 206cdf0e10cSrcweir delete pUndoArray; 207cdf0e10cSrcweir } 208cdf0e10cSrcweir }; 209cdf0e10cSrcweir 210cdf0e10cSrcweir //======================================================================== 211cdf0e10cSrcweir 212cdf0e10cSrcweir namespace svl { namespace undo { namespace impl 213cdf0e10cSrcweir { 214cdf0e10cSrcweir //-------------------------------------------------------------------- 215cdf0e10cSrcweir class SVL_DLLPRIVATE LockGuard 216cdf0e10cSrcweir { 217cdf0e10cSrcweir public: 218cdf0e10cSrcweir LockGuard( SfxUndoManager& i_manager ) 219cdf0e10cSrcweir :m_manager( i_manager ) 220cdf0e10cSrcweir { 221cdf0e10cSrcweir m_manager.ImplEnableUndo_Lock( false ); 222cdf0e10cSrcweir } 223cdf0e10cSrcweir 224cdf0e10cSrcweir ~LockGuard() 225cdf0e10cSrcweir { 226cdf0e10cSrcweir m_manager.ImplEnableUndo_Lock( true ); 227cdf0e10cSrcweir } 228cdf0e10cSrcweir 229cdf0e10cSrcweir private: 230cdf0e10cSrcweir SfxUndoManager& m_manager; 231cdf0e10cSrcweir }; 232cdf0e10cSrcweir 233cdf0e10cSrcweir //-------------------------------------------------------------------- 234cdf0e10cSrcweir typedef void ( SfxUndoListener::*UndoListenerVoidMethod )(); 235cdf0e10cSrcweir typedef void ( SfxUndoListener::*UndoListenerStringMethod )( const String& ); 236cdf0e10cSrcweir 237cdf0e10cSrcweir //-------------------------------------------------------------------- 238cdf0e10cSrcweir struct SVL_DLLPRIVATE NotifyUndoListener : public ::std::unary_function< SfxUndoListener*, void > 239cdf0e10cSrcweir { 240cdf0e10cSrcweir NotifyUndoListener() 241cdf0e10cSrcweir :m_notificationMethod( NULL ) 242cdf0e10cSrcweir ,m_altNotificationMethod( NULL ) 243cdf0e10cSrcweir ,m_sActionComment() 244cdf0e10cSrcweir { 245cdf0e10cSrcweir } 246cdf0e10cSrcweir 247cdf0e10cSrcweir NotifyUndoListener( UndoListenerVoidMethod i_notificationMethod ) 248cdf0e10cSrcweir :m_notificationMethod( i_notificationMethod ) 249cdf0e10cSrcweir ,m_altNotificationMethod( NULL ) 250cdf0e10cSrcweir ,m_sActionComment() 251cdf0e10cSrcweir { 252cdf0e10cSrcweir } 253cdf0e10cSrcweir 254cdf0e10cSrcweir NotifyUndoListener( UndoListenerStringMethod i_notificationMethod, const String& i_actionComment ) 255cdf0e10cSrcweir :m_notificationMethod( NULL ) 256cdf0e10cSrcweir ,m_altNotificationMethod( i_notificationMethod ) 257cdf0e10cSrcweir ,m_sActionComment( i_actionComment ) 258cdf0e10cSrcweir { 259cdf0e10cSrcweir } 260cdf0e10cSrcweir 261cdf0e10cSrcweir bool is() const 262cdf0e10cSrcweir { 263cdf0e10cSrcweir return ( m_notificationMethod != NULL ) || ( m_altNotificationMethod != NULL ); 264cdf0e10cSrcweir } 265cdf0e10cSrcweir 266cdf0e10cSrcweir void operator()( SfxUndoListener* i_listener ) const 267cdf0e10cSrcweir { 268cdf0e10cSrcweir OSL_PRECOND( is(), "NotifyUndoListener: this will crash!" ); 269cdf0e10cSrcweir if ( m_altNotificationMethod != NULL ) 270cdf0e10cSrcweir { 271cdf0e10cSrcweir ( i_listener->*m_altNotificationMethod )( m_sActionComment ); 272cdf0e10cSrcweir } 273cdf0e10cSrcweir else 274cdf0e10cSrcweir { 275cdf0e10cSrcweir ( i_listener->*m_notificationMethod )(); 276cdf0e10cSrcweir } 277cdf0e10cSrcweir } 278cdf0e10cSrcweir 279cdf0e10cSrcweir private: 280cdf0e10cSrcweir UndoListenerVoidMethod m_notificationMethod; 281cdf0e10cSrcweir UndoListenerStringMethod m_altNotificationMethod; 282cdf0e10cSrcweir String m_sActionComment; 283cdf0e10cSrcweir }; 284cdf0e10cSrcweir 285cdf0e10cSrcweir //-------------------------------------------------------------------- 286cdf0e10cSrcweir class SVL_DLLPRIVATE UndoManagerGuard 287cdf0e10cSrcweir { 288cdf0e10cSrcweir public: 289cdf0e10cSrcweir UndoManagerGuard( SfxUndoManager_Data& i_managerData ) 290cdf0e10cSrcweir :m_rManagerData( i_managerData ) 291cdf0e10cSrcweir ,m_aGuard( i_managerData.aMutex ) 292cdf0e10cSrcweir ,m_notifiers() 293cdf0e10cSrcweir { 294cdf0e10cSrcweir } 295cdf0e10cSrcweir 296cdf0e10cSrcweir ~UndoManagerGuard(); 297cdf0e10cSrcweir 298cdf0e10cSrcweir void clear() 299cdf0e10cSrcweir { 300cdf0e10cSrcweir m_aGuard.clear(); 301cdf0e10cSrcweir } 302cdf0e10cSrcweir 303cdf0e10cSrcweir void reset() 304cdf0e10cSrcweir { 305cdf0e10cSrcweir m_aGuard.reset(); 306cdf0e10cSrcweir } 307cdf0e10cSrcweir 308cdf0e10cSrcweir void cancelNotifications() 309cdf0e10cSrcweir { 310cdf0e10cSrcweir m_notifiers.clear(); 311cdf0e10cSrcweir } 312cdf0e10cSrcweir 313cdf0e10cSrcweir /** marks the given Undo action for deletion 314cdf0e10cSrcweir 315cdf0e10cSrcweir The Undo action will be put into a list, whose members will be deleted from within the destructor of the 316cdf0e10cSrcweir UndoManagerGuard. This deletion will happen without the UndoManager's mutex locked. 317cdf0e10cSrcweir */ 318cdf0e10cSrcweir void markForDeletion( SfxUndoAction* i_action ) 319cdf0e10cSrcweir { 320cdf0e10cSrcweir // remember 321cdf0e10cSrcweir if ( i_action ) 322cdf0e10cSrcweir m_aUndoActionsCleanup.push_back( i_action ); 323cdf0e10cSrcweir } 324cdf0e10cSrcweir 325cdf0e10cSrcweir /** schedules the given SfxUndoListener method to be called for all registered listeners. 326cdf0e10cSrcweir 327cdf0e10cSrcweir The notification will happen after the Undo manager's mutex has been released, and after all pending 328cdf0e10cSrcweir deletions of Undo actions are done. 329cdf0e10cSrcweir */ 330cdf0e10cSrcweir void scheduleNotification( UndoListenerVoidMethod i_notificationMethod ) 331cdf0e10cSrcweir { 332cdf0e10cSrcweir m_notifiers.push_back( NotifyUndoListener( i_notificationMethod ) ); 333cdf0e10cSrcweir } 334cdf0e10cSrcweir 335cdf0e10cSrcweir void scheduleNotification( UndoListenerStringMethod i_notificationMethod, const String& i_actionComment ) 336cdf0e10cSrcweir { 337cdf0e10cSrcweir m_notifiers.push_back( NotifyUndoListener( i_notificationMethod, i_actionComment ) ); 338cdf0e10cSrcweir } 339cdf0e10cSrcweir 340cdf0e10cSrcweir private: 341cdf0e10cSrcweir SfxUndoManager_Data& m_rManagerData; 342cdf0e10cSrcweir ::osl::ResettableMutexGuard m_aGuard; 343cdf0e10cSrcweir ::std::list< SfxUndoAction* > m_aUndoActionsCleanup; 344cdf0e10cSrcweir ::std::list< NotifyUndoListener > m_notifiers; 345cdf0e10cSrcweir }; 346cdf0e10cSrcweir 347cdf0e10cSrcweir UndoManagerGuard::~UndoManagerGuard() 348cdf0e10cSrcweir { 349cdf0e10cSrcweir // copy members 350cdf0e10cSrcweir UndoListeners aListenersCopy( m_rManagerData.aListeners ); 351cdf0e10cSrcweir 352cdf0e10cSrcweir // release mutex 353cdf0e10cSrcweir m_aGuard.clear(); 354cdf0e10cSrcweir 355cdf0e10cSrcweir // delete all actions 356cdf0e10cSrcweir while ( !m_aUndoActionsCleanup.empty() ) 357cdf0e10cSrcweir { 358cdf0e10cSrcweir SfxUndoAction* pAction = m_aUndoActionsCleanup.front(); 359cdf0e10cSrcweir m_aUndoActionsCleanup.pop_front(); 360cdf0e10cSrcweir try 361cdf0e10cSrcweir { 362cdf0e10cSrcweir delete pAction; 363cdf0e10cSrcweir } 364cdf0e10cSrcweir catch( const Exception& ) 365cdf0e10cSrcweir { 366cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 367cdf0e10cSrcweir } 368cdf0e10cSrcweir } 369cdf0e10cSrcweir 370cdf0e10cSrcweir // handle scheduled notification 371cdf0e10cSrcweir for ( ::std::list< NotifyUndoListener >::const_iterator notifier = m_notifiers.begin(); 372cdf0e10cSrcweir notifier != m_notifiers.end(); 373cdf0e10cSrcweir ++notifier 374cdf0e10cSrcweir ) 375cdf0e10cSrcweir { 376cdf0e10cSrcweir if ( notifier->is() ) 377cdf0e10cSrcweir ::std::for_each( aListenersCopy.begin(), aListenersCopy.end(), *notifier ); 378cdf0e10cSrcweir } 379cdf0e10cSrcweir } 380cdf0e10cSrcweir } } } 381cdf0e10cSrcweir 382cdf0e10cSrcweir using namespace ::svl::undo::impl; 383cdf0e10cSrcweir 384cdf0e10cSrcweir //======================================================================== 385cdf0e10cSrcweir 386cdf0e10cSrcweir SfxUndoManager::SfxUndoManager( size_t nMaxUndoActionCount ) 387cdf0e10cSrcweir :m_pData( new SfxUndoManager_Data( nMaxUndoActionCount ) ) 388cdf0e10cSrcweir { 389cdf0e10cSrcweir } 390cdf0e10cSrcweir 391cdf0e10cSrcweir //------------------------------------------------------------------------ 392cdf0e10cSrcweir 393cdf0e10cSrcweir SfxUndoManager::~SfxUndoManager() 394cdf0e10cSrcweir { 395cdf0e10cSrcweir UndoListeners aListenersCopy; 396cdf0e10cSrcweir { 397cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 398cdf0e10cSrcweir aListenersCopy = m_pData->aListeners; 399cdf0e10cSrcweir } 400cdf0e10cSrcweir 401cdf0e10cSrcweir ::std::for_each( aListenersCopy.begin(), aListenersCopy.end(), 402cdf0e10cSrcweir NotifyUndoListener( &SfxUndoListener::undoManagerDying ) ); 403cdf0e10cSrcweir } 404cdf0e10cSrcweir 405cdf0e10cSrcweir //------------------------------------------------------------------------ 406cdf0e10cSrcweir 407cdf0e10cSrcweir void SfxUndoManager::EnableUndo( bool i_enable ) 408cdf0e10cSrcweir { 409cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 410cdf0e10cSrcweir ImplEnableUndo_Lock( i_enable ); 411cdf0e10cSrcweir 412cdf0e10cSrcweir } 413cdf0e10cSrcweir 414cdf0e10cSrcweir //------------------------------------------------------------------------ 415cdf0e10cSrcweir 416cdf0e10cSrcweir void SfxUndoManager::ImplEnableUndo_Lock( bool const i_enable ) 417cdf0e10cSrcweir { 418cdf0e10cSrcweir if ( m_pData->mbUndoEnabled == i_enable ) 419cdf0e10cSrcweir return; 420cdf0e10cSrcweir m_pData->mbUndoEnabled = i_enable; 421cdf0e10cSrcweir } 422cdf0e10cSrcweir 423cdf0e10cSrcweir //------------------------------------------------------------------------ 424cdf0e10cSrcweir 425cdf0e10cSrcweir bool SfxUndoManager::IsUndoEnabled() const 426cdf0e10cSrcweir { 427cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 428cdf0e10cSrcweir return ImplIsUndoEnabled_Lock(); 429cdf0e10cSrcweir } 430cdf0e10cSrcweir 431cdf0e10cSrcweir //------------------------------------------------------------------------ 432cdf0e10cSrcweir 433cdf0e10cSrcweir bool SfxUndoManager::ImplIsUndoEnabled_Lock() const 434cdf0e10cSrcweir { 435cdf0e10cSrcweir return m_pData->mbUndoEnabled; 436cdf0e10cSrcweir } 437cdf0e10cSrcweir 438cdf0e10cSrcweir //------------------------------------------------------------------------ 439cdf0e10cSrcweir 440cdf0e10cSrcweir void SfxUndoManager::SetMaxUndoActionCount( size_t nMaxUndoActionCount ) 441cdf0e10cSrcweir { 442cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 443cdf0e10cSrcweir 444cdf0e10cSrcweir // Remove entries from the pActUndoArray when we have to reduce 445cdf0e10cSrcweir // the number of entries due to a lower nMaxUndoActionCount. 446cdf0e10cSrcweir // Both redo and undo action entries will be removed until we reached the 447cdf0e10cSrcweir // new nMaxUndoActionCount. 448cdf0e10cSrcweir 449cdf0e10cSrcweir long nNumToDelete = m_pData->pActUndoArray->aUndoActions.size() - nMaxUndoActionCount; 450cdf0e10cSrcweir while ( nNumToDelete > 0 ) 451cdf0e10cSrcweir { 452cdf0e10cSrcweir size_t nPos = m_pData->pActUndoArray->aUndoActions.size(); 453cdf0e10cSrcweir if ( nPos > m_pData->pActUndoArray->nCurUndoAction ) 454cdf0e10cSrcweir { 455cdf0e10cSrcweir SfxUndoAction* pAction = m_pData->pActUndoArray->aUndoActions[nPos-1].pAction; 456*01300968SArmin Le Grand aGuard.markForDeletion( pAction ); 457*01300968SArmin Le Grand m_pData->pActUndoArray->aUndoActions.Remove( nPos-1 ); 458*01300968SArmin Le Grand --nNumToDelete; 459cdf0e10cSrcweir } 460cdf0e10cSrcweir 461cdf0e10cSrcweir if ( nNumToDelete > 0 && m_pData->pActUndoArray->nCurUndoAction > 0 ) 462cdf0e10cSrcweir { 463cdf0e10cSrcweir SfxUndoAction* pAction = m_pData->pActUndoArray->aUndoActions[0].pAction; 464*01300968SArmin Le Grand aGuard.markForDeletion( pAction ); 465*01300968SArmin Le Grand m_pData->pActUndoArray->aUndoActions.Remove(0); 466*01300968SArmin Le Grand --m_pData->pActUndoArray->nCurUndoAction; 467*01300968SArmin Le Grand --nNumToDelete; 468cdf0e10cSrcweir } 469cdf0e10cSrcweir 470cdf0e10cSrcweir if ( nPos == m_pData->pActUndoArray->aUndoActions.size() ) 471cdf0e10cSrcweir break; // Cannot delete more entries 472cdf0e10cSrcweir } 473cdf0e10cSrcweir 474cdf0e10cSrcweir m_pData->pActUndoArray->nMaxUndoActions = nMaxUndoActionCount; 475cdf0e10cSrcweir } 476cdf0e10cSrcweir 477cdf0e10cSrcweir //------------------------------------------------------------------------ 478cdf0e10cSrcweir 479cdf0e10cSrcweir size_t SfxUndoManager::GetMaxUndoActionCount() const 480cdf0e10cSrcweir { 481cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 482cdf0e10cSrcweir return m_pData->pActUndoArray->nMaxUndoActions; 483cdf0e10cSrcweir } 484cdf0e10cSrcweir 485cdf0e10cSrcweir //------------------------------------------------------------------------ 486cdf0e10cSrcweir 487cdf0e10cSrcweir void SfxUndoManager::ImplClearCurrentLevel_NoNotify( UndoManagerGuard& i_guard ) 488cdf0e10cSrcweir { 489cdf0e10cSrcweir // clear array 490cdf0e10cSrcweir while ( !m_pData->pActUndoArray->aUndoActions.empty() ) 491cdf0e10cSrcweir { 492cdf0e10cSrcweir size_t deletePos = m_pData->pActUndoArray->aUndoActions.size() - 1; 493cdf0e10cSrcweir SfxUndoAction* pAction = m_pData->pActUndoArray->aUndoActions[ deletePos ].pAction; 494cdf0e10cSrcweir i_guard.markForDeletion( pAction ); 495cdf0e10cSrcweir m_pData->pActUndoArray->aUndoActions.Remove( deletePos ); 496cdf0e10cSrcweir } 497cdf0e10cSrcweir 498cdf0e10cSrcweir m_pData->pActUndoArray->nCurUndoAction = 0; 499cdf0e10cSrcweir 500cdf0e10cSrcweir m_pData->mnMarks = 0; 501cdf0e10cSrcweir m_pData->mnEmptyMark = MARK_INVALID; 502cdf0e10cSrcweir } 503cdf0e10cSrcweir 504cdf0e10cSrcweir //------------------------------------------------------------------------ 505cdf0e10cSrcweir 506cdf0e10cSrcweir void SfxUndoManager::Clear() 507cdf0e10cSrcweir { 508cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 509cdf0e10cSrcweir 510cdf0e10cSrcweir OSL_ENSURE( !ImplIsInListAction_Lock(), "SfxUndoManager::Clear: suspicious call - do you really wish to clear the current level?" ); 511cdf0e10cSrcweir ImplClearCurrentLevel_NoNotify( aGuard ); 512cdf0e10cSrcweir 513cdf0e10cSrcweir // notify listeners 514cdf0e10cSrcweir aGuard.scheduleNotification( &SfxUndoListener::cleared ); 515cdf0e10cSrcweir } 516cdf0e10cSrcweir 517cdf0e10cSrcweir //------------------------------------------------------------------------ 518cdf0e10cSrcweir 519cdf0e10cSrcweir void SfxUndoManager::ClearAllLevels() 520cdf0e10cSrcweir { 521cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 522cdf0e10cSrcweir ImplClearCurrentLevel_NoNotify( aGuard ); 523cdf0e10cSrcweir 524cdf0e10cSrcweir if ( ImplIsInListAction_Lock() ) 525cdf0e10cSrcweir { 526cdf0e10cSrcweir m_pData->mbClearUntilTopLevel = true; 527cdf0e10cSrcweir } 528cdf0e10cSrcweir else 529cdf0e10cSrcweir { 530cdf0e10cSrcweir aGuard.scheduleNotification( &SfxUndoListener::cleared ); 531cdf0e10cSrcweir } 532cdf0e10cSrcweir } 533cdf0e10cSrcweir 534cdf0e10cSrcweir //------------------------------------------------------------------------ 535cdf0e10cSrcweir 536cdf0e10cSrcweir void SfxUndoManager::ImplClearRedo_NoLock( bool const i_currentLevel ) 537cdf0e10cSrcweir { 538cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 539cdf0e10cSrcweir ImplClearRedo( aGuard, i_currentLevel ); 540cdf0e10cSrcweir } 541cdf0e10cSrcweir 542cdf0e10cSrcweir //------------------------------------------------------------------------ 543cdf0e10cSrcweir 544cdf0e10cSrcweir void SfxUndoManager::ClearRedo() 545cdf0e10cSrcweir { 546cdf0e10cSrcweir OSL_ENSURE( !IsInListAction(), "SfxUndoManager::ClearRedo: suspicious call - do you really wish to clear the current level?" ); 547cdf0e10cSrcweir ImplClearRedo_NoLock( CurrentLevel ); 548cdf0e10cSrcweir } 549cdf0e10cSrcweir 550cdf0e10cSrcweir //------------------------------------------------------------------------ 551cdf0e10cSrcweir 552cdf0e10cSrcweir void SfxUndoManager::Reset() 553cdf0e10cSrcweir { 554cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 555cdf0e10cSrcweir 556cdf0e10cSrcweir // clear all locks 557cdf0e10cSrcweir while ( !ImplIsUndoEnabled_Lock() ) 558cdf0e10cSrcweir ImplEnableUndo_Lock( true ); 559cdf0e10cSrcweir 560cdf0e10cSrcweir // cancel all list actions 561cdf0e10cSrcweir while ( IsInListAction() ) 562cdf0e10cSrcweir ImplLeaveListAction( false, aGuard ); 563cdf0e10cSrcweir 564cdf0e10cSrcweir // clear both stacks 565cdf0e10cSrcweir ImplClearCurrentLevel_NoNotify( aGuard ); 566cdf0e10cSrcweir 567cdf0e10cSrcweir // cancel the notifications scheduled by ImplLeaveListAction, 568cdf0e10cSrcweir // as we want to do an own, dedicated notification 569cdf0e10cSrcweir aGuard.cancelNotifications(); 570cdf0e10cSrcweir 571cdf0e10cSrcweir // schedule notification 572cdf0e10cSrcweir aGuard.scheduleNotification( &SfxUndoListener::resetAll ); 573cdf0e10cSrcweir } 574cdf0e10cSrcweir 575cdf0e10cSrcweir //------------------------------------------------------------------------ 576cdf0e10cSrcweir 577cdf0e10cSrcweir void SfxUndoManager::ImplClearUndo( UndoManagerGuard& i_guard ) 578cdf0e10cSrcweir { 579cdf0e10cSrcweir while ( m_pData->pActUndoArray->nCurUndoAction > 0 ) 580cdf0e10cSrcweir { 581cdf0e10cSrcweir SfxUndoAction* pUndoAction = m_pData->pActUndoArray->aUndoActions[0].pAction; 582cdf0e10cSrcweir m_pData->pActUndoArray->aUndoActions.Remove( 0 ); 583cdf0e10cSrcweir i_guard.markForDeletion( pUndoAction ); 584cdf0e10cSrcweir --m_pData->pActUndoArray->nCurUndoAction; 585cdf0e10cSrcweir } 586cdf0e10cSrcweir // TODO: notifications? We don't have clearedUndo, only cleared and clearedRedo at the SfxUndoListener 587cdf0e10cSrcweir } 588cdf0e10cSrcweir 589cdf0e10cSrcweir //------------------------------------------------------------------------ 590cdf0e10cSrcweir 591cdf0e10cSrcweir void SfxUndoManager::ImplClearRedo( UndoManagerGuard& i_guard, bool const i_currentLevel ) 592cdf0e10cSrcweir { 593cdf0e10cSrcweir SfxUndoArray* pUndoArray = ( i_currentLevel == IUndoManager::CurrentLevel ) ? m_pData->pActUndoArray : m_pData->pUndoArray; 594cdf0e10cSrcweir 595cdf0e10cSrcweir // clearance 596cdf0e10cSrcweir while ( pUndoArray->aUndoActions.size() > pUndoArray->nCurUndoAction ) 597cdf0e10cSrcweir { 598cdf0e10cSrcweir size_t deletePos = pUndoArray->aUndoActions.size() - 1; 599cdf0e10cSrcweir SfxUndoAction* pAction = pUndoArray->aUndoActions[ deletePos ].pAction; 600cdf0e10cSrcweir pUndoArray->aUndoActions.Remove( deletePos ); 601cdf0e10cSrcweir i_guard.markForDeletion( pAction ); 602cdf0e10cSrcweir } 603cdf0e10cSrcweir 604cdf0e10cSrcweir // notification - only if the top level's stack was cleared 605cdf0e10cSrcweir if ( i_currentLevel == IUndoManager::TopLevel ) 606cdf0e10cSrcweir i_guard.scheduleNotification( &SfxUndoListener::clearedRedo ); 607cdf0e10cSrcweir } 608cdf0e10cSrcweir 609cdf0e10cSrcweir //------------------------------------------------------------------------ 610cdf0e10cSrcweir 611cdf0e10cSrcweir bool SfxUndoManager::ImplAddUndoAction_NoNotify( SfxUndoAction *pAction, bool bTryMerge, bool bClearRedo, UndoManagerGuard& i_guard ) 612cdf0e10cSrcweir { 613cdf0e10cSrcweir if ( !ImplIsUndoEnabled_Lock() || ( m_pData->pActUndoArray->nMaxUndoActions == 0 ) ) 614cdf0e10cSrcweir { 615cdf0e10cSrcweir i_guard.markForDeletion( pAction ); 616cdf0e10cSrcweir return false; 617cdf0e10cSrcweir } 618cdf0e10cSrcweir 619cdf0e10cSrcweir // merge, if required 620cdf0e10cSrcweir SfxUndoAction* pMergeWithAction = m_pData->pActUndoArray->nCurUndoAction ? 621cdf0e10cSrcweir m_pData->pActUndoArray->aUndoActions[m_pData->pActUndoArray->nCurUndoAction-1].pAction : NULL; 622cfbf046dSOliver-Rainer Wittmann if ( bTryMerge && ( pMergeWithAction && pMergeWithAction->Merge( pAction ) ) ) 623cdf0e10cSrcweir { 624cdf0e10cSrcweir i_guard.markForDeletion( pAction ); 625cdf0e10cSrcweir return false; 626cdf0e10cSrcweir } 627cdf0e10cSrcweir 628cdf0e10cSrcweir // clear redo stack, if requested 629cdf0e10cSrcweir if ( bClearRedo && ( ImplGetRedoActionCount_Lock( CurrentLevel ) > 0 ) ) 630cdf0e10cSrcweir ImplClearRedo( i_guard, IUndoManager::CurrentLevel ); 631cdf0e10cSrcweir 632cdf0e10cSrcweir // respect max number 633cdf0e10cSrcweir if( m_pData->pActUndoArray == m_pData->pUndoArray ) 634cdf0e10cSrcweir { 635*01300968SArmin Le Grand while(m_pData->pActUndoArray->aUndoActions.size() >= m_pData->pActUndoArray->nMaxUndoActions) 636cdf0e10cSrcweir { 637cdf0e10cSrcweir i_guard.markForDeletion( m_pData->pActUndoArray->aUndoActions[0].pAction ); 638cdf0e10cSrcweir m_pData->pActUndoArray->aUndoActions.Remove(0); 639cdf0e10cSrcweir --m_pData->pActUndoArray->nCurUndoAction; 640cdf0e10cSrcweir } 641cdf0e10cSrcweir } 642cdf0e10cSrcweir 643cdf0e10cSrcweir // append new action 644cdf0e10cSrcweir m_pData->pActUndoArray->aUndoActions.Insert( pAction, m_pData->pActUndoArray->nCurUndoAction++ ); 645cdf0e10cSrcweir return true; 646cdf0e10cSrcweir } 647cdf0e10cSrcweir 648cdf0e10cSrcweir //------------------------------------------------------------------------ 649cdf0e10cSrcweir 650cdf0e10cSrcweir void SfxUndoManager::AddUndoAction( SfxUndoAction *pAction, sal_Bool bTryMerge ) 651cdf0e10cSrcweir { 652cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 653cdf0e10cSrcweir 654cdf0e10cSrcweir // add 655cdf0e10cSrcweir if ( ImplAddUndoAction_NoNotify( pAction, bTryMerge, true, aGuard ) ) 656cdf0e10cSrcweir { 657cdf0e10cSrcweir // notify listeners 658cdf0e10cSrcweir aGuard.scheduleNotification( &SfxUndoListener::undoActionAdded, pAction->GetComment() ); 659cdf0e10cSrcweir } 660cdf0e10cSrcweir } 661cdf0e10cSrcweir 662cdf0e10cSrcweir //------------------------------------------------------------------------ 663cdf0e10cSrcweir 664cdf0e10cSrcweir size_t SfxUndoManager::GetUndoActionCount( bool const i_currentLevel ) const 665cdf0e10cSrcweir { 666cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 667cdf0e10cSrcweir const SfxUndoArray* pUndoArray = i_currentLevel ? m_pData->pActUndoArray : m_pData->pUndoArray; 668cdf0e10cSrcweir return pUndoArray->nCurUndoAction; 669cdf0e10cSrcweir } 670cdf0e10cSrcweir 671cdf0e10cSrcweir //------------------------------------------------------------------------ 672cdf0e10cSrcweir 673cdf0e10cSrcweir XubString SfxUndoManager::GetUndoActionComment( size_t nNo, bool const i_currentLevel ) const 674cdf0e10cSrcweir { 675cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 676cdf0e10cSrcweir 677cdf0e10cSrcweir String sComment; 678cdf0e10cSrcweir const SfxUndoArray* pUndoArray = i_currentLevel ? m_pData->pActUndoArray : m_pData->pUndoArray; 679cdf0e10cSrcweir DBG_ASSERT( nNo < pUndoArray->nCurUndoAction, "svl::SfxUndoManager::GetUndoActionComment: illegal index!" ); 680cdf0e10cSrcweir if( nNo < pUndoArray->nCurUndoAction ) 681cdf0e10cSrcweir { 682cdf0e10cSrcweir sComment = pUndoArray->aUndoActions[ pUndoArray->nCurUndoAction - 1 - nNo ].pAction->GetComment(); 683cdf0e10cSrcweir } 684cdf0e10cSrcweir return sComment; 685cdf0e10cSrcweir } 686cdf0e10cSrcweir 687cdf0e10cSrcweir //------------------------------------------------------------------------ 688cdf0e10cSrcweir 689cdf0e10cSrcweir sal_uInt16 SfxUndoManager::GetUndoActionId() const 690cdf0e10cSrcweir { 691cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 692cdf0e10cSrcweir 693cdf0e10cSrcweir DBG_ASSERT( m_pData->pActUndoArray->nCurUndoAction > 0, "svl::SfxUndoManager::GetUndoActionId(), illegal id!" ); 694cdf0e10cSrcweir if ( m_pData->pActUndoArray->nCurUndoAction == 0 ) 695cdf0e10cSrcweir return 0; 696cdf0e10cSrcweir return m_pData->pActUndoArray->aUndoActions[m_pData->pActUndoArray->nCurUndoAction-1].pAction->GetId(); 697cdf0e10cSrcweir } 698cdf0e10cSrcweir 699cdf0e10cSrcweir //------------------------------------------------------------------------ 700cdf0e10cSrcweir 701cdf0e10cSrcweir SfxUndoAction* SfxUndoManager::GetUndoAction( size_t nNo ) const 702cdf0e10cSrcweir { 703cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 704cdf0e10cSrcweir 705cdf0e10cSrcweir DBG_ASSERT( nNo < m_pData->pActUndoArray->nCurUndoAction, "svl::SfxUndoManager::GetUndoAction(), illegal id!" ); 706cdf0e10cSrcweir if( nNo >= m_pData->pActUndoArray->nCurUndoAction ) 707cdf0e10cSrcweir return NULL; 708cdf0e10cSrcweir return m_pData->pActUndoArray->aUndoActions[m_pData->pActUndoArray->nCurUndoAction-1-nNo].pAction; 709cdf0e10cSrcweir } 710cdf0e10cSrcweir 711cdf0e10cSrcweir //------------------------------------------------------------------------ 712cdf0e10cSrcweir 713cdf0e10cSrcweir /** clears the redo stack and removes the top undo action */ 714cdf0e10cSrcweir void SfxUndoManager::RemoveLastUndoAction() 715cdf0e10cSrcweir { 716cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 717cdf0e10cSrcweir 718cdf0e10cSrcweir ENSURE_OR_RETURN_VOID( m_pData->pActUndoArray->nCurUndoAction, "svl::SfxUndoManager::RemoveLastUndoAction(), no action to remove?!" ); 719cdf0e10cSrcweir 720cdf0e10cSrcweir m_pData->pActUndoArray->nCurUndoAction--; 721cdf0e10cSrcweir 722cdf0e10cSrcweir // delete redo-actions and top action 723cdf0e10cSrcweir for ( size_t nPos = m_pData->pActUndoArray->aUndoActions.size(); nPos > m_pData->pActUndoArray->nCurUndoAction; --nPos ) 724cdf0e10cSrcweir { 725cdf0e10cSrcweir aGuard.markForDeletion( m_pData->pActUndoArray->aUndoActions[nPos-1].pAction ); 726cdf0e10cSrcweir } 727cdf0e10cSrcweir 728cdf0e10cSrcweir m_pData->pActUndoArray->aUndoActions.Remove( 729cdf0e10cSrcweir m_pData->pActUndoArray->nCurUndoAction, 730cdf0e10cSrcweir m_pData->pActUndoArray->aUndoActions.size() - m_pData->pActUndoArray->nCurUndoAction ); 731cdf0e10cSrcweir } 732cdf0e10cSrcweir 733cdf0e10cSrcweir //------------------------------------------------------------------------ 734cdf0e10cSrcweir 735cdf0e10cSrcweir bool SfxUndoManager::IsDoing() const 736cdf0e10cSrcweir { 737cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 738cdf0e10cSrcweir return m_pData->mbDoing; 739cdf0e10cSrcweir } 740cdf0e10cSrcweir 741cdf0e10cSrcweir //------------------------------------------------------------------------ 742cdf0e10cSrcweir 743cdf0e10cSrcweir sal_Bool SfxUndoManager::Undo() 744cdf0e10cSrcweir { 745cdf0e10cSrcweir return ImplUndo( NULL ); 746cdf0e10cSrcweir } 747cdf0e10cSrcweir 748cdf0e10cSrcweir //------------------------------------------------------------------------ 749cdf0e10cSrcweir 750cdf0e10cSrcweir sal_Bool SfxUndoManager::UndoWithContext( SfxUndoContext& i_context ) 751cdf0e10cSrcweir { 752cdf0e10cSrcweir return ImplUndo( &i_context ); 753cdf0e10cSrcweir } 754cdf0e10cSrcweir 755cdf0e10cSrcweir //------------------------------------------------------------------------ 756cdf0e10cSrcweir 757cdf0e10cSrcweir sal_Bool SfxUndoManager::ImplUndo( SfxUndoContext* i_contextOrNull ) 758cdf0e10cSrcweir { 759cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 760cdf0e10cSrcweir OSL_ENSURE( !IsDoing(), "SfxUndoManager::Undo: *nested* Undo/Redo actions? How this?" ); 761cdf0e10cSrcweir 762cdf0e10cSrcweir ::comphelper::FlagGuard aDoingGuard( m_pData->mbDoing ); 763cdf0e10cSrcweir LockGuard aLockGuard( *this ); 764cdf0e10cSrcweir 765cdf0e10cSrcweir if ( ImplIsInListAction_Lock() ) 766cdf0e10cSrcweir { 767cdf0e10cSrcweir OSL_ENSURE( false, "SfxUndoManager::Undo: not possible when within a list action!" ); 768cdf0e10cSrcweir return sal_False; 769cdf0e10cSrcweir } 770cdf0e10cSrcweir 771cdf0e10cSrcweir if ( m_pData->pActUndoArray->nCurUndoAction == 0 ) 772cdf0e10cSrcweir { 773cdf0e10cSrcweir OSL_ENSURE( false, "SfxUndoManager::Undo: undo stack is empty!" ); 774cdf0e10cSrcweir return sal_False; 775cdf0e10cSrcweir } 776cdf0e10cSrcweir 777cdf0e10cSrcweir SfxUndoAction* pAction = m_pData->pActUndoArray->aUndoActions[ --m_pData->pActUndoArray->nCurUndoAction ].pAction; 778cdf0e10cSrcweir const String sActionComment = pAction->GetComment(); 779cdf0e10cSrcweir try 780cdf0e10cSrcweir { 781cdf0e10cSrcweir // clear the guard/mutex before calling into the SfxUndoAction - this can be an extension-implemented UNO component 782cdf0e10cSrcweir // nowadays ... 783cdf0e10cSrcweir aGuard.clear(); 784cdf0e10cSrcweir if ( i_contextOrNull != NULL ) 785cdf0e10cSrcweir pAction->UndoWithContext( *i_contextOrNull ); 786cdf0e10cSrcweir else 787cdf0e10cSrcweir pAction->Undo(); 788cdf0e10cSrcweir aGuard.reset(); 789cdf0e10cSrcweir } 790cdf0e10cSrcweir catch( ... ) 791cdf0e10cSrcweir { 792cdf0e10cSrcweir aGuard.reset(); 793cdf0e10cSrcweir 794cdf0e10cSrcweir // in theory, somebody might have tampered with all of *m_pData while the mutex was unlocked. So, see if 795cdf0e10cSrcweir // we still find pAction in our current Undo array 796cdf0e10cSrcweir size_t nCurAction = 0; 797cdf0e10cSrcweir while ( nCurAction < m_pData->pActUndoArray->aUndoActions.size() ) 798cdf0e10cSrcweir { 799cdf0e10cSrcweir if ( m_pData->pActUndoArray->aUndoActions[ nCurAction++ ].pAction == pAction ) 800cdf0e10cSrcweir { 801cdf0e10cSrcweir // the Undo action is still there ... 802cdf0e10cSrcweir // assume the error is a permanent failure, and clear the Undo stack 803cdf0e10cSrcweir ImplClearUndo( aGuard ); 804cdf0e10cSrcweir throw; 805cdf0e10cSrcweir } 806cdf0e10cSrcweir } 807cdf0e10cSrcweir OSL_ENSURE( false, "SfxUndoManager::Undo: can't clear the Undo stack after the failure - some other party was faster ..." ); 808cdf0e10cSrcweir throw; 809cdf0e10cSrcweir } 810cdf0e10cSrcweir 811cdf0e10cSrcweir aGuard.scheduleNotification( &SfxUndoListener::actionUndone, sActionComment ); 812cdf0e10cSrcweir 813cdf0e10cSrcweir return sal_True; 814cdf0e10cSrcweir } 815cdf0e10cSrcweir 816cdf0e10cSrcweir //------------------------------------------------------------------------ 817cdf0e10cSrcweir 818cdf0e10cSrcweir size_t SfxUndoManager::GetRedoActionCount( bool const i_currentLevel ) const 819cdf0e10cSrcweir { 820cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 821cdf0e10cSrcweir return ImplGetRedoActionCount_Lock( i_currentLevel ); 822cdf0e10cSrcweir } 823cdf0e10cSrcweir 824cdf0e10cSrcweir //------------------------------------------------------------------------ 825cdf0e10cSrcweir 826cdf0e10cSrcweir size_t SfxUndoManager::ImplGetRedoActionCount_Lock( bool const i_currentLevel ) const 827cdf0e10cSrcweir { 828cdf0e10cSrcweir const SfxUndoArray* pUndoArray = i_currentLevel ? m_pData->pActUndoArray : m_pData->pUndoArray; 829cdf0e10cSrcweir return pUndoArray->aUndoActions.size() - pUndoArray->nCurUndoAction; 830cdf0e10cSrcweir } 831cdf0e10cSrcweir 832cdf0e10cSrcweir //------------------------------------------------------------------------ 833cdf0e10cSrcweir 834cdf0e10cSrcweir XubString SfxUndoManager::GetRedoActionComment( size_t nNo, bool const i_currentLevel ) const 835cdf0e10cSrcweir { 836cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 837cdf0e10cSrcweir const SfxUndoArray* pUndoArray = i_currentLevel ? m_pData->pActUndoArray : m_pData->pUndoArray; 838cdf0e10cSrcweir return pUndoArray->aUndoActions[ pUndoArray->nCurUndoAction + nNo ].pAction->GetComment(); 839cdf0e10cSrcweir } 840cdf0e10cSrcweir 841cdf0e10cSrcweir //------------------------------------------------------------------------ 842cdf0e10cSrcweir 843cdf0e10cSrcweir sal_Bool SfxUndoManager::Redo() 844cdf0e10cSrcweir { 845cdf0e10cSrcweir return ImplRedo( NULL ); 846cdf0e10cSrcweir } 847cdf0e10cSrcweir 848cdf0e10cSrcweir //------------------------------------------------------------------------ 849cdf0e10cSrcweir 850cdf0e10cSrcweir sal_Bool SfxUndoManager::RedoWithContext( SfxUndoContext& i_context ) 851cdf0e10cSrcweir { 852cdf0e10cSrcweir return ImplRedo( &i_context ); 853cdf0e10cSrcweir } 854cdf0e10cSrcweir 855cdf0e10cSrcweir //------------------------------------------------------------------------ 856cdf0e10cSrcweir 857cdf0e10cSrcweir sal_Bool SfxUndoManager::ImplRedo( SfxUndoContext* i_contextOrNull ) 858cdf0e10cSrcweir { 859cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 860cdf0e10cSrcweir OSL_ENSURE( !IsDoing(), "SfxUndoManager::Redo: *nested* Undo/Redo actions? How this?" ); 861cdf0e10cSrcweir 862cdf0e10cSrcweir ::comphelper::FlagGuard aDoingGuard( m_pData->mbDoing ); 863cdf0e10cSrcweir LockGuard aLockGuard( *this ); 864cdf0e10cSrcweir 865cdf0e10cSrcweir if ( ImplIsInListAction_Lock() ) 866cdf0e10cSrcweir { 867cdf0e10cSrcweir OSL_ENSURE( false, "SfxUndoManager::Redo: not possible when within a list action!" ); 868cdf0e10cSrcweir return sal_False; 869cdf0e10cSrcweir } 870cdf0e10cSrcweir 871cdf0e10cSrcweir if ( m_pData->pActUndoArray->nCurUndoAction >= m_pData->pActUndoArray->aUndoActions.size() ) 872cdf0e10cSrcweir { 873cdf0e10cSrcweir OSL_ENSURE( false, "SfxUndoManager::Redo: redo stack is empty!" ); 874cdf0e10cSrcweir return sal_False; 875cdf0e10cSrcweir } 876cdf0e10cSrcweir 877cdf0e10cSrcweir SfxUndoAction* pAction = m_pData->pActUndoArray->aUndoActions[ m_pData->pActUndoArray->nCurUndoAction++ ].pAction; 878cdf0e10cSrcweir const String sActionComment = pAction->GetComment(); 879cdf0e10cSrcweir try 880cdf0e10cSrcweir { 881cdf0e10cSrcweir // clear the guard/mutex before calling into the SfxUndoAction - this can be a extension-implemented UNO component 882cdf0e10cSrcweir // nowadays ... 883cdf0e10cSrcweir aGuard.clear(); 884cdf0e10cSrcweir if ( i_contextOrNull != NULL ) 885cdf0e10cSrcweir pAction->RedoWithContext( *i_contextOrNull ); 886cdf0e10cSrcweir else 887cdf0e10cSrcweir pAction->Redo(); 888cdf0e10cSrcweir aGuard.reset(); 889cdf0e10cSrcweir } 890cdf0e10cSrcweir catch( ... ) 891cdf0e10cSrcweir { 892cdf0e10cSrcweir aGuard.reset(); 893cdf0e10cSrcweir 894cdf0e10cSrcweir // in theory, somebody might have tampered with all of *m_pData while the mutex was unlocked. So, see if 895cdf0e10cSrcweir // we still find pAction in our current Undo array 896cdf0e10cSrcweir size_t nCurAction = 0; 897cdf0e10cSrcweir while ( nCurAction < m_pData->pActUndoArray->aUndoActions.size() ) 898cdf0e10cSrcweir { 899cdf0e10cSrcweir if ( m_pData->pActUndoArray->aUndoActions[ nCurAction ].pAction == pAction ) 900cdf0e10cSrcweir { 901cdf0e10cSrcweir // the Undo action is still there ... 902cdf0e10cSrcweir // assume the error is a permanent failure, and clear the Undo stack 903cdf0e10cSrcweir ImplClearRedo( aGuard, IUndoManager::CurrentLevel ); 904cdf0e10cSrcweir throw; 905cdf0e10cSrcweir } 906cdf0e10cSrcweir ++nCurAction; 907cdf0e10cSrcweir } 908cdf0e10cSrcweir OSL_ENSURE( false, "SfxUndoManager::Redo: can't clear the Undo stack after the failure - some other party was faster ..." ); 909cdf0e10cSrcweir throw; 910cdf0e10cSrcweir } 911cdf0e10cSrcweir 912cdf0e10cSrcweir aGuard.scheduleNotification( &SfxUndoListener::actionRedone, sActionComment ); 913cdf0e10cSrcweir 914cdf0e10cSrcweir return sal_True; 915cdf0e10cSrcweir } 916cdf0e10cSrcweir 917cdf0e10cSrcweir //------------------------------------------------------------------------ 918cdf0e10cSrcweir 919cdf0e10cSrcweir size_t SfxUndoManager::GetRepeatActionCount() const 920cdf0e10cSrcweir { 921cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 922cdf0e10cSrcweir return m_pData->pActUndoArray->aUndoActions.size(); 923cdf0e10cSrcweir } 924cdf0e10cSrcweir 925cdf0e10cSrcweir //------------------------------------------------------------------------ 926cdf0e10cSrcweir 927cdf0e10cSrcweir XubString SfxUndoManager::GetRepeatActionComment( SfxRepeatTarget &rTarget) const 928cdf0e10cSrcweir { 929cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 930cdf0e10cSrcweir return m_pData->pActUndoArray->aUndoActions[ m_pData->pActUndoArray->aUndoActions.size() - 1 ].pAction 931cdf0e10cSrcweir ->GetRepeatComment(rTarget); 932cdf0e10cSrcweir } 933cdf0e10cSrcweir 934cdf0e10cSrcweir //------------------------------------------------------------------------ 935cdf0e10cSrcweir 936cdf0e10cSrcweir sal_Bool SfxUndoManager::Repeat( SfxRepeatTarget &rTarget ) 937cdf0e10cSrcweir { 938cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 939cdf0e10cSrcweir if ( !m_pData->pActUndoArray->aUndoActions.empty() ) 940cdf0e10cSrcweir { 941cdf0e10cSrcweir SfxUndoAction* pAction = m_pData->pActUndoArray->aUndoActions[ m_pData->pActUndoArray->aUndoActions.size() - 1 ].pAction; 942cdf0e10cSrcweir aGuard.clear(); 943cdf0e10cSrcweir if ( pAction->CanRepeat( rTarget ) ) 944cdf0e10cSrcweir pAction->Repeat( rTarget ); 945cdf0e10cSrcweir return sal_True; 946cdf0e10cSrcweir } 947cdf0e10cSrcweir 948cdf0e10cSrcweir return sal_False; 949cdf0e10cSrcweir } 950cdf0e10cSrcweir 951cdf0e10cSrcweir //------------------------------------------------------------------------ 952cdf0e10cSrcweir 953cdf0e10cSrcweir sal_Bool SfxUndoManager::CanRepeat( SfxRepeatTarget &rTarget ) const 954cdf0e10cSrcweir { 955cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 956cdf0e10cSrcweir if ( !m_pData->pActUndoArray->aUndoActions.empty() ) 957cdf0e10cSrcweir { 958cdf0e10cSrcweir size_t nActionNo = m_pData->pActUndoArray->aUndoActions.size() - 1; 959cdf0e10cSrcweir return m_pData->pActUndoArray->aUndoActions[nActionNo].pAction->CanRepeat(rTarget); 960cdf0e10cSrcweir } 961cdf0e10cSrcweir return sal_False; 962cdf0e10cSrcweir } 963cdf0e10cSrcweir 964cdf0e10cSrcweir //------------------------------------------------------------------------ 965cdf0e10cSrcweir 966cdf0e10cSrcweir void SfxUndoManager::AddUndoListener( SfxUndoListener& i_listener ) 967cdf0e10cSrcweir { 968cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 969cdf0e10cSrcweir m_pData->aListeners.push_back( &i_listener ); 970cdf0e10cSrcweir } 971cdf0e10cSrcweir 972cdf0e10cSrcweir //------------------------------------------------------------------------ 973cdf0e10cSrcweir 974cdf0e10cSrcweir void SfxUndoManager::RemoveUndoListener( SfxUndoListener& i_listener ) 975cdf0e10cSrcweir { 976cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 977cdf0e10cSrcweir for ( UndoListeners::iterator lookup = m_pData->aListeners.begin(); 978cdf0e10cSrcweir lookup != m_pData->aListeners.end(); 979cdf0e10cSrcweir ++lookup 980cdf0e10cSrcweir ) 981cdf0e10cSrcweir { 982cdf0e10cSrcweir if ( (*lookup) == &i_listener ) 983cdf0e10cSrcweir { 984cdf0e10cSrcweir m_pData->aListeners.erase( lookup ); 985cdf0e10cSrcweir break; 986cdf0e10cSrcweir } 987cdf0e10cSrcweir } 988cdf0e10cSrcweir } 989cdf0e10cSrcweir 990cdf0e10cSrcweir //------------------------------------------------------------------------ 991cdf0e10cSrcweir 992cdf0e10cSrcweir void SfxUndoManager::EnterListAction( 993cdf0e10cSrcweir const XubString& rComment, const XubString &rRepeatComment, sal_uInt16 nId ) 994cdf0e10cSrcweir 995cdf0e10cSrcweir /* [Beschreibung] 996cdf0e10cSrcweir 997cdf0e10cSrcweir Fuegt eine ListUndoAction ein und setzt dessen UndoArray als aktuelles. 998cdf0e10cSrcweir */ 999cdf0e10cSrcweir 1000cdf0e10cSrcweir { 1001cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 1002cdf0e10cSrcweir 1003cdf0e10cSrcweir if( !ImplIsUndoEnabled_Lock() ) 1004cdf0e10cSrcweir return; 1005cdf0e10cSrcweir 1006cdf0e10cSrcweir if ( !m_pData->pUndoArray->nMaxUndoActions ) 1007cdf0e10cSrcweir return; 1008cdf0e10cSrcweir 1009cdf0e10cSrcweir m_pData->pFatherUndoArray = m_pData->pActUndoArray; 1010cdf0e10cSrcweir SfxListUndoAction* pAction = new SfxListUndoAction( rComment, rRepeatComment, nId, m_pData->pActUndoArray ); 1011cdf0e10cSrcweir OSL_VERIFY( ImplAddUndoAction_NoNotify( pAction, false, false, aGuard ) ); 1012cdf0e10cSrcweir // expected to succeed: all conditions under which it could fail should have been checked already 1013cdf0e10cSrcweir m_pData->pActUndoArray = pAction; 1014cdf0e10cSrcweir 1015cdf0e10cSrcweir // notification 1016cdf0e10cSrcweir aGuard.scheduleNotification( &SfxUndoListener::listActionEntered, rComment ); 1017cdf0e10cSrcweir } 1018cdf0e10cSrcweir 1019cdf0e10cSrcweir //------------------------------------------------------------------------ 1020cdf0e10cSrcweir 1021cdf0e10cSrcweir bool SfxUndoManager::IsInListAction() const 1022cdf0e10cSrcweir { 1023cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 1024cdf0e10cSrcweir return ImplIsInListAction_Lock(); 1025cdf0e10cSrcweir } 1026cdf0e10cSrcweir 1027cdf0e10cSrcweir //------------------------------------------------------------------------ 1028cdf0e10cSrcweir 1029cdf0e10cSrcweir bool SfxUndoManager::ImplIsInListAction_Lock() const 1030cdf0e10cSrcweir { 1031cdf0e10cSrcweir return ( m_pData->pActUndoArray != m_pData->pUndoArray ); 1032cdf0e10cSrcweir } 1033cdf0e10cSrcweir 1034cdf0e10cSrcweir //------------------------------------------------------------------------ 1035cdf0e10cSrcweir 1036cdf0e10cSrcweir size_t SfxUndoManager::GetListActionDepth() const 1037cdf0e10cSrcweir { 1038cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 1039cdf0e10cSrcweir size_t nDepth(0); 1040cdf0e10cSrcweir 1041cdf0e10cSrcweir SfxUndoArray* pLookup( m_pData->pActUndoArray ); 1042cdf0e10cSrcweir while ( pLookup != m_pData->pUndoArray ) 1043cdf0e10cSrcweir { 1044cdf0e10cSrcweir pLookup = pLookup->pFatherUndoArray; 1045cdf0e10cSrcweir ++nDepth; 1046cdf0e10cSrcweir } 1047cdf0e10cSrcweir 1048cdf0e10cSrcweir return nDepth; 1049cdf0e10cSrcweir } 1050cdf0e10cSrcweir 1051cdf0e10cSrcweir //------------------------------------------------------------------------ 1052cdf0e10cSrcweir 1053cdf0e10cSrcweir size_t SfxUndoManager::LeaveListAction() 1054cdf0e10cSrcweir { 1055cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 1056cdf0e10cSrcweir size_t nCount = ImplLeaveListAction( false, aGuard ); 1057cdf0e10cSrcweir 1058cdf0e10cSrcweir if ( m_pData->mbClearUntilTopLevel ) 1059cdf0e10cSrcweir { 1060cdf0e10cSrcweir ImplClearCurrentLevel_NoNotify( aGuard ); 1061cdf0e10cSrcweir if ( !ImplIsInListAction_Lock() ) 1062cdf0e10cSrcweir { 1063cdf0e10cSrcweir m_pData->mbClearUntilTopLevel = false; 1064cdf0e10cSrcweir aGuard.scheduleNotification( &SfxUndoListener::cleared ); 1065cdf0e10cSrcweir } 1066cdf0e10cSrcweir nCount = 0; 1067cdf0e10cSrcweir } 1068cdf0e10cSrcweir 1069cdf0e10cSrcweir return nCount; 1070cdf0e10cSrcweir } 1071cdf0e10cSrcweir 1072cdf0e10cSrcweir //------------------------------------------------------------------------ 1073cdf0e10cSrcweir 1074cdf0e10cSrcweir size_t SfxUndoManager::LeaveAndMergeListAction() 1075cdf0e10cSrcweir { 1076cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 1077cdf0e10cSrcweir return ImplLeaveListAction( true, aGuard ); 1078cdf0e10cSrcweir } 1079cdf0e10cSrcweir 1080cdf0e10cSrcweir //------------------------------------------------------------------------ 1081cdf0e10cSrcweir 1082cdf0e10cSrcweir size_t SfxUndoManager::ImplLeaveListAction( const bool i_merge, UndoManagerGuard& i_guard ) 1083cdf0e10cSrcweir { 1084cdf0e10cSrcweir if ( !ImplIsUndoEnabled_Lock() ) 1085cdf0e10cSrcweir return 0; 1086cdf0e10cSrcweir 1087cdf0e10cSrcweir if ( !m_pData->pUndoArray->nMaxUndoActions ) 1088cdf0e10cSrcweir return 0; 1089cdf0e10cSrcweir 1090cdf0e10cSrcweir if( !ImplIsInListAction_Lock() ) 1091cdf0e10cSrcweir { 1092cdf0e10cSrcweir DBG_ERROR( "svl::SfxUndoManager::ImplLeaveListAction, called without calling EnterListAction()!" ); 1093cdf0e10cSrcweir return 0; 1094cdf0e10cSrcweir } 1095cdf0e10cSrcweir 1096cdf0e10cSrcweir DBG_ASSERT( m_pData->pActUndoArray->pFatherUndoArray, "SfxUndoManager::ImplLeaveListAction, no father undo array!?" ); 1097cdf0e10cSrcweir 1098cdf0e10cSrcweir // the array/level which we're about to leave 1099cdf0e10cSrcweir SfxUndoArray* pArrayToLeave = m_pData->pActUndoArray; 1100cdf0e10cSrcweir // one step up 1101cdf0e10cSrcweir m_pData->pActUndoArray = m_pData->pActUndoArray->pFatherUndoArray; 1102cdf0e10cSrcweir 1103cdf0e10cSrcweir // If no undo actions were added to the list, delete the list action 1104cdf0e10cSrcweir const size_t nListActionElements = pArrayToLeave->nCurUndoAction; 1105cdf0e10cSrcweir if ( nListActionElements == 0 ) 1106cdf0e10cSrcweir { 1107cdf0e10cSrcweir SfxUndoAction* pCurrentAction= m_pData->pActUndoArray->aUndoActions[ m_pData->pActUndoArray->nCurUndoAction-1 ].pAction; 1108cdf0e10cSrcweir m_pData->pActUndoArray->aUndoActions.Remove( --m_pData->pActUndoArray->nCurUndoAction ); 1109cdf0e10cSrcweir i_guard.markForDeletion( pCurrentAction ); 1110cdf0e10cSrcweir 1111cdf0e10cSrcweir i_guard.scheduleNotification( &SfxUndoListener::listActionCancelled ); 1112cdf0e10cSrcweir return 0; 1113cdf0e10cSrcweir } 1114cdf0e10cSrcweir 1115cdf0e10cSrcweir // now that it is finally clear the list action is non-trivial, and does participate in the Undo stack, clear 1116cdf0e10cSrcweir // the redo stack 1117cdf0e10cSrcweir ImplClearRedo( i_guard, IUndoManager::CurrentLevel ); 1118cdf0e10cSrcweir 1119cdf0e10cSrcweir SfxUndoAction* pCurrentAction= m_pData->pActUndoArray->aUndoActions[ m_pData->pActUndoArray->nCurUndoAction-1 ].pAction; 1120cdf0e10cSrcweir SfxListUndoAction* pListAction = dynamic_cast< SfxListUndoAction * >( pCurrentAction ); 1121cdf0e10cSrcweir ENSURE_OR_RETURN( pListAction, "SfxUndoManager::ImplLeaveListAction: list action expected at this position!", nListActionElements ); 1122cdf0e10cSrcweir 1123cdf0e10cSrcweir if ( i_merge ) 1124cdf0e10cSrcweir { 1125cdf0e10cSrcweir // merge the list action with its predecessor on the same level 1126cdf0e10cSrcweir OSL_ENSURE( m_pData->pActUndoArray->nCurUndoAction > 1, 1127cdf0e10cSrcweir "SfxUndoManager::ImplLeaveListAction: cannot merge the list action if there's no other action on the same level - check this beforehand!" ); 1128cdf0e10cSrcweir if ( m_pData->pActUndoArray->nCurUndoAction > 1 ) 1129cdf0e10cSrcweir { 1130cdf0e10cSrcweir SfxUndoAction* pPreviousAction = m_pData->pActUndoArray->aUndoActions[ m_pData->pActUndoArray->nCurUndoAction - 2 ].pAction; 1131cdf0e10cSrcweir m_pData->pActUndoArray->aUndoActions.Remove( m_pData->pActUndoArray->nCurUndoAction - 2 ); 1132cdf0e10cSrcweir --m_pData->pActUndoArray->nCurUndoAction; 1133cdf0e10cSrcweir pListAction->aUndoActions.Insert( pPreviousAction, 0 ); 1134cdf0e10cSrcweir ++pListAction->nCurUndoAction; 1135cdf0e10cSrcweir 1136cdf0e10cSrcweir pListAction->SetComment( pPreviousAction->GetComment() ); 1137cdf0e10cSrcweir } 1138cdf0e10cSrcweir } 1139cdf0e10cSrcweir 1140cdf0e10cSrcweir // if the undo array has no comment, try to get it from its children 1141cdf0e10cSrcweir if ( pListAction->GetComment().Len() == 0 ) 1142cdf0e10cSrcweir { 1143cdf0e10cSrcweir for( size_t n = 0; n < pListAction->aUndoActions.size(); n++ ) 1144cdf0e10cSrcweir { 1145cdf0e10cSrcweir if( pListAction->aUndoActions[n].pAction->GetComment().Len() ) 1146cdf0e10cSrcweir { 1147cdf0e10cSrcweir pListAction->SetComment( pListAction->aUndoActions[n].pAction->GetComment() ); 1148cdf0e10cSrcweir break; 1149cdf0e10cSrcweir } 1150cdf0e10cSrcweir } 1151cdf0e10cSrcweir } 1152cdf0e10cSrcweir 1153cdf0e10cSrcweir // notify listeners 1154cdf0e10cSrcweir i_guard.scheduleNotification( &SfxUndoListener::listActionLeft, pListAction->GetComment() ); 1155cdf0e10cSrcweir 1156cdf0e10cSrcweir // outta here 1157cdf0e10cSrcweir return nListActionElements; 1158cdf0e10cSrcweir } 1159cdf0e10cSrcweir 1160cdf0e10cSrcweir //------------------------------------------------------------------------ 1161cdf0e10cSrcweir UndoStackMark SfxUndoManager::MarkTopUndoAction() 1162cdf0e10cSrcweir { 1163cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 1164cdf0e10cSrcweir 1165cdf0e10cSrcweir OSL_ENSURE( !IsInListAction(), 1166cdf0e10cSrcweir "SfxUndoManager::MarkTopUndoAction(): suspicious call!" ); 1167cdf0e10cSrcweir OSL_ENSURE((m_pData->mnMarks + 1) < (m_pData->mnEmptyMark - 1), 1168cdf0e10cSrcweir "SfxUndoManager::MarkTopUndoAction(): mark overflow!"); 1169cdf0e10cSrcweir 1170cdf0e10cSrcweir size_t const nActionPos = m_pData->pUndoArray->nCurUndoAction; 1171cdf0e10cSrcweir if (0 == nActionPos) 1172cdf0e10cSrcweir { 1173cdf0e10cSrcweir --m_pData->mnEmptyMark; 1174cdf0e10cSrcweir return m_pData->mnEmptyMark; 1175cdf0e10cSrcweir } 1176cdf0e10cSrcweir 1177cdf0e10cSrcweir m_pData->pUndoArray->aUndoActions[ nActionPos-1 ].aMarks.push_back( 1178cdf0e10cSrcweir ++m_pData->mnMarks ); 1179cdf0e10cSrcweir return m_pData->mnMarks; 1180cdf0e10cSrcweir } 1181cdf0e10cSrcweir 1182cdf0e10cSrcweir //------------------------------------------------------------------------ 1183cdf0e10cSrcweir void SfxUndoManager::RemoveMark( UndoStackMark const i_mark ) 1184cdf0e10cSrcweir { 1185cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 1186cdf0e10cSrcweir 1187cdf0e10cSrcweir if ((m_pData->mnEmptyMark < i_mark) || (MARK_INVALID == i_mark)) 1188cdf0e10cSrcweir { 1189cdf0e10cSrcweir return; // nothing to remove 1190cdf0e10cSrcweir } 1191cdf0e10cSrcweir else if (i_mark == m_pData->mnEmptyMark) 1192cdf0e10cSrcweir { 1193cdf0e10cSrcweir --m_pData->mnEmptyMark; // never returned from MarkTop => invalid 1194cdf0e10cSrcweir return; 1195cdf0e10cSrcweir } 1196cdf0e10cSrcweir 1197cdf0e10cSrcweir for ( size_t i=0; i<m_pData->pUndoArray->aUndoActions.size(); ++i ) 1198cdf0e10cSrcweir { 1199cdf0e10cSrcweir MarkedUndoAction& rAction = m_pData->pUndoArray->aUndoActions[i]; 1200cdf0e10cSrcweir for ( ::std::vector< UndoStackMark >::iterator markPos = rAction.aMarks.begin(); 1201cdf0e10cSrcweir markPos != rAction.aMarks.end(); 1202cdf0e10cSrcweir ++markPos 1203cdf0e10cSrcweir ) 1204cdf0e10cSrcweir { 1205cdf0e10cSrcweir if ( *markPos == i_mark ) 1206cdf0e10cSrcweir { 1207cdf0e10cSrcweir rAction.aMarks.erase( markPos ); 1208cdf0e10cSrcweir return; 1209cdf0e10cSrcweir } 1210cdf0e10cSrcweir } 1211cdf0e10cSrcweir } 1212cdf0e10cSrcweir OSL_ENSURE( false, "SfxUndoManager::RemoveMark: mark not found!" ); 1213cdf0e10cSrcweir // TODO: this might be too offensive. There are situations where we implicitly remove marks 1214cdf0e10cSrcweir // without our clients, in particular the client which created the mark, having a chance to know 1215cdf0e10cSrcweir // about this. 1216cdf0e10cSrcweir } 1217cdf0e10cSrcweir 1218cdf0e10cSrcweir //------------------------------------------------------------------------ 1219cdf0e10cSrcweir bool SfxUndoManager::HasTopUndoActionMark( UndoStackMark const i_mark ) 1220cdf0e10cSrcweir { 1221cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 1222cdf0e10cSrcweir 1223cdf0e10cSrcweir size_t nActionPos = m_pData->pUndoArray->nCurUndoAction; 1224cdf0e10cSrcweir if ( nActionPos == 0 ) 1225cdf0e10cSrcweir { 1226cdf0e10cSrcweir return (i_mark == m_pData->mnEmptyMark); 1227cdf0e10cSrcweir } 1228cdf0e10cSrcweir 1229cdf0e10cSrcweir const MarkedUndoAction& rAction = 1230cdf0e10cSrcweir m_pData->pUndoArray->aUndoActions[ nActionPos-1 ]; 1231cdf0e10cSrcweir for ( ::std::vector< UndoStackMark >::const_iterator markPos = rAction.aMarks.begin(); 1232cdf0e10cSrcweir markPos != rAction.aMarks.end(); 1233cdf0e10cSrcweir ++markPos 1234cdf0e10cSrcweir ) 1235cdf0e10cSrcweir { 1236cdf0e10cSrcweir if ( *markPos == i_mark ) 1237cdf0e10cSrcweir return true; 1238cdf0e10cSrcweir } 1239cdf0e10cSrcweir 1240cdf0e10cSrcweir return false; 1241cdf0e10cSrcweir } 1242cdf0e10cSrcweir 1243cdf0e10cSrcweir //------------------------------------------------------------------------ 1244cdf0e10cSrcweir 1245cdf0e10cSrcweir void SfxUndoManager::RemoveOldestUndoActions( size_t const i_count ) 1246cdf0e10cSrcweir { 1247cdf0e10cSrcweir UndoManagerGuard aGuard( *m_pData ); 1248cdf0e10cSrcweir 1249cdf0e10cSrcweir size_t nActionsToRemove = i_count; 1250cdf0e10cSrcweir while ( nActionsToRemove ) 1251cdf0e10cSrcweir { 1252cdf0e10cSrcweir SfxUndoAction* pActionToRemove = m_pData->pUndoArray->aUndoActions[0].pAction; 1253cdf0e10cSrcweir 1254cdf0e10cSrcweir if ( IsInListAction() && ( m_pData->pUndoArray->nCurUndoAction == 1 ) ) 1255cdf0e10cSrcweir { 1256cdf0e10cSrcweir OSL_ENSURE( false, "SfxUndoManager::RemoveOldestUndoActions: cannot remove a not-yet-closed list action!" ); 1257cdf0e10cSrcweir return; 1258cdf0e10cSrcweir } 1259cdf0e10cSrcweir 1260cdf0e10cSrcweir aGuard.markForDeletion( pActionToRemove ); 1261cdf0e10cSrcweir m_pData->pUndoArray->aUndoActions.Remove( 0 ); 1262cdf0e10cSrcweir --m_pData->pUndoArray->nCurUndoAction; 1263cdf0e10cSrcweir --nActionsToRemove; 1264cdf0e10cSrcweir } 1265cdf0e10cSrcweir } 1266cdf0e10cSrcweir 1267cdf0e10cSrcweir //------------------------------------------------------------------------ 1268cdf0e10cSrcweir 1269cdf0e10cSrcweir sal_uInt16 SfxListUndoAction::GetId() const 1270cdf0e10cSrcweir { 1271cdf0e10cSrcweir return nId; 1272cdf0e10cSrcweir } 1273cdf0e10cSrcweir 1274cdf0e10cSrcweir //------------------------------------------------------------------------ 1275cdf0e10cSrcweir 1276cdf0e10cSrcweir XubString SfxListUndoAction::GetComment() const 1277cdf0e10cSrcweir { 1278cdf0e10cSrcweir return aComment; 1279cdf0e10cSrcweir } 1280cdf0e10cSrcweir 1281cdf0e10cSrcweir //------------------------------------------------------------------------ 1282cdf0e10cSrcweir 1283cdf0e10cSrcweir void SfxListUndoAction::SetComment( const UniString& rComment ) 1284cdf0e10cSrcweir { 1285cdf0e10cSrcweir aComment = rComment; 1286cdf0e10cSrcweir } 1287cdf0e10cSrcweir 1288cdf0e10cSrcweir //------------------------------------------------------------------------ 1289cdf0e10cSrcweir 1290cdf0e10cSrcweir XubString SfxListUndoAction::GetRepeatComment(SfxRepeatTarget &) const 1291cdf0e10cSrcweir { 1292cdf0e10cSrcweir return aRepeatComment; 1293cdf0e10cSrcweir } 1294cdf0e10cSrcweir 1295cdf0e10cSrcweir 1296cdf0e10cSrcweir //------------------------------------------------------------------------ 1297cdf0e10cSrcweir 1298cdf0e10cSrcweir SfxListUndoAction::SfxListUndoAction 1299cdf0e10cSrcweir ( 1300cdf0e10cSrcweir const XubString &rComment, 1301cdf0e10cSrcweir const XubString rRepeatComment, 1302cdf0e10cSrcweir sal_uInt16 Id, 1303cdf0e10cSrcweir SfxUndoArray *pFather 1304cdf0e10cSrcweir ) 1305cdf0e10cSrcweir : nId(Id), aComment(rComment), aRepeatComment(rRepeatComment) 1306cdf0e10cSrcweir { 1307cdf0e10cSrcweir pFatherUndoArray = pFather; 1308cdf0e10cSrcweir nMaxUndoActions = USHRT_MAX; 1309cdf0e10cSrcweir } 1310cdf0e10cSrcweir 1311cdf0e10cSrcweir //------------------------------------------------------------------------ 1312cdf0e10cSrcweir 1313cdf0e10cSrcweir void SfxListUndoAction::Undo() 1314cdf0e10cSrcweir { 1315cdf0e10cSrcweir for(size_t i=nCurUndoAction;i>0;) 1316cdf0e10cSrcweir aUndoActions[--i].pAction->Undo(); 1317cdf0e10cSrcweir nCurUndoAction=0; 1318cdf0e10cSrcweir } 1319cdf0e10cSrcweir 1320cdf0e10cSrcweir //------------------------------------------------------------------------ 1321cdf0e10cSrcweir 1322cdf0e10cSrcweir void SfxListUndoAction::UndoWithContext( SfxUndoContext& i_context ) 1323cdf0e10cSrcweir { 1324cdf0e10cSrcweir for(size_t i=nCurUndoAction;i>0;) 1325cdf0e10cSrcweir aUndoActions[--i].pAction->UndoWithContext( i_context ); 1326cdf0e10cSrcweir nCurUndoAction=0; 1327cdf0e10cSrcweir } 1328cdf0e10cSrcweir 1329cdf0e10cSrcweir //------------------------------------------------------------------------ 1330cdf0e10cSrcweir 1331cdf0e10cSrcweir void SfxListUndoAction::Redo() 1332cdf0e10cSrcweir { 1333cdf0e10cSrcweir for(size_t i=nCurUndoAction;i<aUndoActions.size();i++) 1334cdf0e10cSrcweir aUndoActions[i].pAction->Redo(); 1335cdf0e10cSrcweir nCurUndoAction = aUndoActions.size(); 1336cdf0e10cSrcweir } 1337cdf0e10cSrcweir 1338cdf0e10cSrcweir //------------------------------------------------------------------------ 1339cdf0e10cSrcweir 1340cdf0e10cSrcweir void SfxListUndoAction::RedoWithContext( SfxUndoContext& i_context ) 1341cdf0e10cSrcweir { 1342cdf0e10cSrcweir for(size_t i=nCurUndoAction;i<aUndoActions.size();i++) 1343cdf0e10cSrcweir aUndoActions[i].pAction->RedoWithContext( i_context ); 1344cdf0e10cSrcweir nCurUndoAction = aUndoActions.size(); 1345cdf0e10cSrcweir } 1346cdf0e10cSrcweir 1347cdf0e10cSrcweir //------------------------------------------------------------------------ 1348cdf0e10cSrcweir 1349cdf0e10cSrcweir void SfxListUndoAction::Repeat(SfxRepeatTarget&rTarget) 1350cdf0e10cSrcweir { 1351cdf0e10cSrcweir for(size_t i=0;i<nCurUndoAction;i++) 1352cdf0e10cSrcweir aUndoActions[i].pAction->Repeat(rTarget); 1353cdf0e10cSrcweir } 1354cdf0e10cSrcweir 1355cdf0e10cSrcweir //------------------------------------------------------------------------ 1356cdf0e10cSrcweir 1357cdf0e10cSrcweir sal_Bool SfxListUndoAction::CanRepeat(SfxRepeatTarget&r) const 1358cdf0e10cSrcweir { 1359cdf0e10cSrcweir for(size_t i=0;i<nCurUndoAction;i++) 1360cdf0e10cSrcweir if(!aUndoActions[i].pAction->CanRepeat(r)) 1361cdf0e10cSrcweir return sal_False; 1362cdf0e10cSrcweir return sal_True; 1363cdf0e10cSrcweir } 1364cdf0e10cSrcweir 1365cdf0e10cSrcweir //------------------------------------------------------------------------ 1366cdf0e10cSrcweir 1367cdf0e10cSrcweir sal_Bool SfxListUndoAction::Merge( SfxUndoAction *pNextAction ) 1368cdf0e10cSrcweir { 1369cdf0e10cSrcweir return !aUndoActions.empty() && aUndoActions[aUndoActions.size()-1].pAction->Merge( pNextAction ); 1370cdf0e10cSrcweir } 1371cdf0e10cSrcweir 1372cdf0e10cSrcweir //------------------------------------------------------------------------ 1373cdf0e10cSrcweir 1374cdf0e10cSrcweir SfxLinkUndoAction::SfxLinkUndoAction(::svl::IUndoManager *pManager) 1375cdf0e10cSrcweir /* [Beschreibung] 1376cdf0e10cSrcweir 1377cdf0e10cSrcweir Richtet eine LinkAction ein, die auf einen weiteren UndoManager zeigt. 1378cdf0e10cSrcweir Holt sich als zugehoerige Action des weiteren UndoManagers dessen 1379cdf0e10cSrcweir aktuelle Action. 1380cdf0e10cSrcweir */ 1381cdf0e10cSrcweir 1382cdf0e10cSrcweir { 1383cdf0e10cSrcweir pUndoManager = pManager; 1384cdf0e10cSrcweir SfxUndoManager* pUndoManagerImplementation = dynamic_cast< SfxUndoManager* >( pManager ); 1385cdf0e10cSrcweir ENSURE_OR_THROW( pUndoManagerImplementation != NULL, "unsupported undo manager implementation!" ); 1386cdf0e10cSrcweir // yes, this cast is dirty. But reaching into the the SfxUndoManager's implementation, 1387cdf0e10cSrcweir // directly accessing its internal stack, and tampering with an action on that stack 1388cdf0e10cSrcweir // is dirty, too. 1389cdf0e10cSrcweir if ( pManager->GetMaxUndoActionCount() ) 1390cdf0e10cSrcweir { 1391cdf0e10cSrcweir size_t nPos = pManager->GetUndoActionCount()-1; 1392cdf0e10cSrcweir pAction = pUndoManagerImplementation->m_pData->pActUndoArray->aUndoActions[nPos].pAction; 1393*01300968SArmin Le Grand pAction->SetLinkToSfxLinkUndoAction(this); 1394cdf0e10cSrcweir } 1395cdf0e10cSrcweir else 1396cdf0e10cSrcweir pAction = 0; 1397cdf0e10cSrcweir } 1398cdf0e10cSrcweir 1399cdf0e10cSrcweir //------------------------------------------------------------------------ 1400cdf0e10cSrcweir 1401cdf0e10cSrcweir void SfxLinkUndoAction::Undo() 1402cdf0e10cSrcweir { 1403cdf0e10cSrcweir if ( pAction ) 1404cdf0e10cSrcweir pUndoManager->Undo(); 1405cdf0e10cSrcweir } 1406cdf0e10cSrcweir 1407cdf0e10cSrcweir //------------------------------------------------------------------------ 1408cdf0e10cSrcweir 1409cdf0e10cSrcweir void SfxLinkUndoAction::Redo() 1410cdf0e10cSrcweir { 1411cdf0e10cSrcweir if ( pAction ) 1412cdf0e10cSrcweir pUndoManager->Redo(); 1413cdf0e10cSrcweir } 1414cdf0e10cSrcweir 1415cdf0e10cSrcweir //------------------------------------------------------------------------ 1416cdf0e10cSrcweir 1417cdf0e10cSrcweir 1418cdf0e10cSrcweir sal_Bool SfxLinkUndoAction::CanRepeat(SfxRepeatTarget& r) const 1419cdf0e10cSrcweir { 1420cdf0e10cSrcweir return pAction && pAction->CanRepeat(r); 1421cdf0e10cSrcweir } 1422cdf0e10cSrcweir 1423cdf0e10cSrcweir 1424cdf0e10cSrcweir //------------------------------------------------------------------------ 1425cdf0e10cSrcweir 1426cdf0e10cSrcweir 1427cdf0e10cSrcweir void SfxLinkUndoAction::Repeat(SfxRepeatTarget&r) 1428cdf0e10cSrcweir { 1429cdf0e10cSrcweir if ( pAction && pAction->CanRepeat( r ) ) 1430cdf0e10cSrcweir pAction->Repeat( r ); 1431cdf0e10cSrcweir } 1432cdf0e10cSrcweir 1433cdf0e10cSrcweir 1434cdf0e10cSrcweir //------------------------------------------------------------------------ 1435cdf0e10cSrcweir 1436cdf0e10cSrcweir XubString SfxLinkUndoAction::GetComment() const 1437cdf0e10cSrcweir { 1438cdf0e10cSrcweir if ( pAction ) 1439cdf0e10cSrcweir return pAction->GetComment(); 1440cdf0e10cSrcweir else 1441cdf0e10cSrcweir return XubString(); 1442cdf0e10cSrcweir } 1443cdf0e10cSrcweir 1444cdf0e10cSrcweir 1445cdf0e10cSrcweir //------------------------------------------------------------------------ 1446cdf0e10cSrcweir 1447cdf0e10cSrcweir XubString SfxLinkUndoAction::GetRepeatComment(SfxRepeatTarget&r) const 1448cdf0e10cSrcweir { 1449cdf0e10cSrcweir if ( pAction ) 1450cdf0e10cSrcweir return pAction->GetRepeatComment(r); 1451cdf0e10cSrcweir else 1452cdf0e10cSrcweir return XubString(); 1453cdf0e10cSrcweir } 1454cdf0e10cSrcweir 1455cdf0e10cSrcweir //------------------------------------------------------------------------ 1456cdf0e10cSrcweir 1457cdf0e10cSrcweir SfxLinkUndoAction::~SfxLinkUndoAction() 1458cdf0e10cSrcweir { 1459cdf0e10cSrcweir if( pAction ) 1460*01300968SArmin Le Grand pAction->SetLinkToSfxLinkUndoAction(0); 1461cdf0e10cSrcweir } 1462cdf0e10cSrcweir 1463*01300968SArmin Le Grand //------------------------------------------------------------------------ 1464*01300968SArmin Le Grand 1465*01300968SArmin Le Grand void SfxLinkUndoAction::LinkedSfxUndoActionDestructed(const SfxUndoAction& rCandidate) 1466*01300968SArmin Le Grand { 1467*01300968SArmin Le Grand OSL_ENSURE(0 != pAction, "OOps, we have no linked SfxUndoAction (!)"); 1468*01300968SArmin Le Grand OSL_ENSURE(pAction == &rCandidate, "OOps, the destroyed and linked UndoActions differ (!)"); 1469*01300968SArmin Le Grand (void)rCandidate; 1470*01300968SArmin Le Grand pAction = 0; 1471*01300968SArmin Le Grand } 1472cdf0e10cSrcweir 1473cdf0e10cSrcweir //------------------------------------------------------------------------ 1474cdf0e10cSrcweir 1475cdf0e10cSrcweir SfxUndoArray::~SfxUndoArray() 1476cdf0e10cSrcweir { 1477cdf0e10cSrcweir while ( !aUndoActions.empty() ) 1478cdf0e10cSrcweir { 1479cdf0e10cSrcweir SfxUndoAction *pAction = aUndoActions[ aUndoActions.size() - 1 ].pAction; 1480cdf0e10cSrcweir aUndoActions.Remove( aUndoActions.size() - 1 ); 1481cdf0e10cSrcweir delete pAction; 1482cdf0e10cSrcweir } 1483cdf0e10cSrcweir } 1484cdf0e10cSrcweir 1485cdf0e10cSrcweir 1486cdf0e10cSrcweir sal_uInt16 SfxLinkUndoAction::GetId() const 1487cdf0e10cSrcweir { 1488cdf0e10cSrcweir return pAction ? pAction->GetId() : 0; 1489cdf0e10cSrcweir } 1490