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