1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "docundomanager.hxx"
22 #include "sfx2/sfxbasemodel.hxx"
23 #include "sfx2/objsh.hxx"
24 #include "sfx2/viewfrm.hxx"
25 #include "sfx2/viewsh.hxx"
26 #include "sfx2/bindings.hxx"
28 #include <com/sun/star/lang/XComponent.hpp>
30 #include <comphelper/anytostring.hxx>
31 #include <comphelper/flagguard.hxx>
32 #include <svl/undo.hxx>
33 #include <tools/diagnose_ex.h>
34 #include <framework/undomanagerhelper.hxx>
36 #include <boost/noncopyable.hpp>
39 //......................................................................................................................
42 //......................................................................................................................
44 using ::com::sun::star::uno::Reference
;
45 using ::com::sun::star::uno::XInterface
;
46 using ::com::sun::star::uno::UNO_QUERY
;
47 using ::com::sun::star::uno::UNO_QUERY_THROW
;
48 using ::com::sun::star::uno::UNO_SET_THROW
;
49 using ::com::sun::star::uno::Exception
;
50 using ::com::sun::star::uno::RuntimeException
;
51 using ::com::sun::star::uno::Any
;
52 using ::com::sun::star::uno::makeAny
;
53 using ::com::sun::star::uno::Sequence
;
54 using ::com::sun::star::uno::Type
;
55 using ::com::sun::star::util::InvalidStateException
;
56 using ::com::sun::star::document::EmptyUndoStackException
;
57 using ::com::sun::star::util::NotLockedException
;
58 using ::com::sun::star::document::UndoContextNotClosedException
;
59 using ::com::sun::star::document::XUndoAction
;
60 using ::com::sun::star::document::XUndoManagerSupplier
;
61 using ::com::sun::star::lang::XComponent
;
62 using ::com::sun::star::lang::IllegalArgumentException
;
63 using ::com::sun::star::lang::NotInitializedException
;
64 using ::com::sun::star::lang::EventObject
;
65 using ::com::sun::star::document::UndoManagerEvent
;
66 using ::com::sun::star::document::XUndoManagerListener
;
67 using ::com::sun::star::document::UndoFailedException
;
68 using ::com::sun::star::document::XUndoManager
;
69 using ::com::sun::star::lang::NoSupportException
;
70 using ::com::sun::star::frame::XModel
;
72 using ::svl::IUndoManager
;
74 //==================================================================================================================
75 //= DocumentUndoManager_Impl
76 //==================================================================================================================
77 struct DocumentUndoManager_Impl
: public ::framework::IUndoManagerImplementation
79 DocumentUndoManager
& rAntiImpl
;
80 IUndoManager
* pUndoManager
;
81 ::framework::UndoManagerHelper aUndoHelper
;
83 DocumentUndoManager_Impl( DocumentUndoManager
& i_antiImpl
)
84 :rAntiImpl( i_antiImpl
)
85 ,pUndoManager( impl_retrieveUndoManager( i_antiImpl
.getBaseModel() ) )
86 // do this *before* the construction of aUndoHelper (which actually means: put pUndoManager before
87 // aUndoHelper in the member list)!
92 virtual ~DocumentUndoManager_Impl()
96 const SfxObjectShell
* getObjectShell() const { return rAntiImpl
.getBaseModel().GetObjectShell(); }
97 SfxObjectShell
* getObjectShell() { return rAntiImpl
.getBaseModel().GetObjectShell(); }
99 // IUndoManagerImplementation
100 virtual ::svl::IUndoManager
& getImplUndoManager();
101 virtual Reference
< XUndoManager
> getThis();
105 aUndoHelper
.disposing();
106 ENSURE_OR_RETURN_VOID( pUndoManager
, "DocumentUndoManager_Impl::disposing: already disposed!" );
110 void invalidateXDo_nolck();
113 static IUndoManager
* impl_retrieveUndoManager( SfxBaseModel
& i_baseModel
)
115 IUndoManager
* pUndoManager( NULL
);
116 SfxObjectShell
* pObjectShell
= i_baseModel
.GetObjectShell();
117 if ( pObjectShell
!= NULL
)
118 pUndoManager
= pObjectShell
->GetUndoManager();
120 throw NotInitializedException( OUString(), *&i_baseModel
);
125 //------------------------------------------------------------------------------------------------------------------
126 ::svl::IUndoManager
& DocumentUndoManager_Impl::getImplUndoManager()
128 ENSURE_OR_THROW( pUndoManager
!= NULL
, "DocumentUndoManager_Impl::getImplUndoManager: no access to the doc's UndoManager implementation!" );
130 #if OSL_DEBUG_LEVEL > 0
131 // in a non-product build, assert if the current UndoManager at the shell is not the same we obtained
132 // (and cached) at construction time
133 SfxObjectShell
* pObjectShell
= rAntiImpl
.getBaseModel().GetObjectShell();
134 OSL_ENSURE( ( pObjectShell
!= NULL
) && ( pUndoManager
== pObjectShell
->GetUndoManager() ),
135 "DocumentUndoManager_Impl::getImplUndoManager: the UndoManager changed meanwhile - what about our listener?" );
138 return *pUndoManager
;
141 //------------------------------------------------------------------------------------------------------------------
142 Reference
< XUndoManager
> DocumentUndoManager_Impl::getThis()
144 return static_cast< XUndoManager
* >( &rAntiImpl
);
147 //------------------------------------------------------------------------------------------------------------------
148 void DocumentUndoManager_Impl::invalidateXDo_nolck()
150 SfxModelGuard
aGuard( rAntiImpl
);
152 const SfxObjectShell
* pDocShell
= getObjectShell();
153 ENSURE_OR_THROW( pDocShell
!= NULL
, "lcl_invalidateUndo: no access to the doc shell!" );
154 SfxViewFrame
* pViewFrame
= SfxViewFrame::GetFirst( pDocShell
);
157 pViewFrame
->GetBindings().Invalidate( SID_UNDO
);
158 pViewFrame
->GetBindings().Invalidate( SID_REDO
);
159 pViewFrame
= SfxViewFrame::GetNext( *pViewFrame
, pDocShell
);
163 //==================================================================================================================
165 //==================================================================================================================
166 /** a facade for the SolarMutex, implementing ::framework::IMutex (as opposed to ::vos::IMutex)
168 class SolarMutexFacade
: public ::framework::IMutex
175 virtual ~SolarMutexFacade() {}
177 virtual void acquire()
179 Application::GetSolarMutex().acquire();
182 virtual void release()
184 Application::GetSolarMutex().release();
188 //==================================================================================================================
190 //==================================================================================================================
191 class UndoManagerGuard
:public ::framework::IMutexGuard
192 ,public ::boost::noncopyable
195 UndoManagerGuard( DocumentUndoManager
& i_undoManager
)
196 :m_guard( i_undoManager
)
197 ,m_solarMutexFacade()
201 virtual ~UndoManagerGuard()
215 virtual ::framework::IMutex
& getGuardedMutex()
217 // note that this means that we *know* that SfxModelGuard also locks the SolarMutex (nothing more, nothing less).
218 // If this ever changes, we need to adjust this code here, too.
219 return m_solarMutexFacade
;
223 SfxModelGuard m_guard
;
224 SolarMutexFacade m_solarMutexFacade
;
227 //==================================================================================================================
228 //= DocumentUndoManager
229 //==================================================================================================================
230 //------------------------------------------------------------------------------------------------------------------
231 DocumentUndoManager::DocumentUndoManager( SfxBaseModel
& i_document
)
232 :SfxModelSubComponent( i_document
)
233 ,m_pImpl( new DocumentUndoManager_Impl( *this ) )
237 //------------------------------------------------------------------------------------------------------------------
238 DocumentUndoManager::~DocumentUndoManager()
242 //------------------------------------------------------------------------------------------------------------------
243 void DocumentUndoManager::disposing()
245 m_pImpl
->disposing();
248 //------------------------------------------------------------------------------------------------------------------
249 bool DocumentUndoManager::isInContext() const
251 // No mutex locking within this method, no disposal check - this is the responsibility of the owner.
252 return m_pImpl
->getImplUndoManager().IsInListAction();
255 //------------------------------------------------------------------------------------------------------------------
256 void SAL_CALL
DocumentUndoManager::acquire( ) throw ()
258 SfxModelSubComponent::acquire();
261 //------------------------------------------------------------------------------------------------------------------
262 void SAL_CALL
DocumentUndoManager::release( ) throw ()
264 SfxModelSubComponent::release();
267 //------------------------------------------------------------------------------------------------------------------
268 void SAL_CALL
DocumentUndoManager::enterUndoContext( const OUString
& i_title
) throw (RuntimeException
)
271 UndoManagerGuard
aGuard( *this );
272 m_pImpl
->aUndoHelper
.enterUndoContext( i_title
, aGuard
);
274 m_pImpl
->invalidateXDo_nolck();
277 //------------------------------------------------------------------------------------------------------------------
278 void SAL_CALL
DocumentUndoManager::enterHiddenUndoContext( ) throw (EmptyUndoStackException
, RuntimeException
)
281 UndoManagerGuard
aGuard( *this );
282 m_pImpl
->aUndoHelper
.enterHiddenUndoContext( aGuard
);
284 m_pImpl
->invalidateXDo_nolck();
287 //------------------------------------------------------------------------------------------------------------------
288 void SAL_CALL
DocumentUndoManager::leaveUndoContext( ) throw (InvalidStateException
, RuntimeException
)
291 UndoManagerGuard
aGuard( *this );
292 m_pImpl
->aUndoHelper
.leaveUndoContext( aGuard
);
294 m_pImpl
->invalidateXDo_nolck();
297 //------------------------------------------------------------------------------------------------------------------
298 void SAL_CALL
DocumentUndoManager::addUndoAction( const Reference
< XUndoAction
>& i_action
) throw (RuntimeException
, IllegalArgumentException
)
301 UndoManagerGuard
aGuard( *this );
302 m_pImpl
->aUndoHelper
.addUndoAction( i_action
, aGuard
);
304 m_pImpl
->invalidateXDo_nolck();
307 //------------------------------------------------------------------------------------------------------------------
308 void SAL_CALL
DocumentUndoManager::undo( ) throw (EmptyUndoStackException
, UndoContextNotClosedException
, UndoFailedException
, RuntimeException
)
311 UndoManagerGuard
aGuard( *this );
312 m_pImpl
->aUndoHelper
.undo( aGuard
);
314 m_pImpl
->invalidateXDo_nolck();
317 //------------------------------------------------------------------------------------------------------------------
318 void SAL_CALL
DocumentUndoManager::redo( ) throw (EmptyUndoStackException
, UndoContextNotClosedException
, UndoFailedException
, RuntimeException
)
321 UndoManagerGuard
aGuard( *this );
322 m_pImpl
->aUndoHelper
.redo( aGuard
);
324 m_pImpl
->invalidateXDo_nolck();
327 //------------------------------------------------------------------------------------------------------------------
328 ::sal_Bool SAL_CALL
DocumentUndoManager::isUndoPossible( ) throw (RuntimeException
)
330 UndoManagerGuard
aGuard( *this );
331 return m_pImpl
->aUndoHelper
.isUndoPossible();
334 //------------------------------------------------------------------------------------------------------------------
335 ::sal_Bool SAL_CALL
DocumentUndoManager::isRedoPossible( ) throw (RuntimeException
)
337 UndoManagerGuard
aGuard( *this );
338 return m_pImpl
->aUndoHelper
.isRedoPossible();
341 //------------------------------------------------------------------------------------------------------------------
342 OUString SAL_CALL
DocumentUndoManager::getCurrentUndoActionTitle( ) throw (EmptyUndoStackException
, RuntimeException
)
344 UndoManagerGuard
aGuard( *this );
345 return m_pImpl
->aUndoHelper
.getCurrentUndoActionTitle();
348 //------------------------------------------------------------------------------------------------------------------
349 OUString SAL_CALL
DocumentUndoManager::getCurrentRedoActionTitle( ) throw (EmptyUndoStackException
, RuntimeException
)
351 UndoManagerGuard
aGuard( *this );
352 return m_pImpl
->aUndoHelper
.getCurrentRedoActionTitle();
355 //------------------------------------------------------------------------------------------------------------------
356 Sequence
< OUString
> SAL_CALL
DocumentUndoManager::getAllUndoActionTitles( ) throw (RuntimeException
)
358 UndoManagerGuard
aGuard( *this );
359 return m_pImpl
->aUndoHelper
.getAllUndoActionTitles();
362 //------------------------------------------------------------------------------------------------------------------
363 Sequence
< OUString
> SAL_CALL
DocumentUndoManager::getAllRedoActionTitles( ) throw (RuntimeException
)
365 UndoManagerGuard
aGuard( *this );
366 return m_pImpl
->aUndoHelper
.getAllRedoActionTitles();
369 //------------------------------------------------------------------------------------------------------------------
370 void SAL_CALL
DocumentUndoManager::clear( ) throw (UndoContextNotClosedException
, RuntimeException
)
373 UndoManagerGuard
aGuard( *this );
374 m_pImpl
->aUndoHelper
.clear( aGuard
);
376 m_pImpl
->invalidateXDo_nolck();
379 //------------------------------------------------------------------------------------------------------------------
380 void SAL_CALL
DocumentUndoManager::clearRedo( ) throw (UndoContextNotClosedException
, RuntimeException
)
383 UndoManagerGuard
aGuard( *this );
384 m_pImpl
->aUndoHelper
.clearRedo( aGuard
);
386 m_pImpl
->invalidateXDo_nolck();
389 //------------------------------------------------------------------------------------------------------------------
390 void SAL_CALL
DocumentUndoManager::reset() throw (RuntimeException
)
393 UndoManagerGuard
aGuard( *this );
394 m_pImpl
->aUndoHelper
.reset( aGuard
);
396 m_pImpl
->invalidateXDo_nolck();
399 //------------------------------------------------------------------------------------------------------------------
400 void SAL_CALL
DocumentUndoManager::lock( ) throw (RuntimeException
)
402 UndoManagerGuard
aGuard( *this );
403 m_pImpl
->aUndoHelper
.lock();
406 //------------------------------------------------------------------------------------------------------------------
407 void SAL_CALL
DocumentUndoManager::unlock( ) throw (RuntimeException
, NotLockedException
)
409 UndoManagerGuard
aGuard( *this );
410 m_pImpl
->aUndoHelper
.unlock();
413 //------------------------------------------------------------------------------------------------------------------
414 ::sal_Bool SAL_CALL
DocumentUndoManager::isLocked( ) throw (RuntimeException
)
416 UndoManagerGuard
aGuard( *this );
417 return m_pImpl
->aUndoHelper
.isLocked();
420 //------------------------------------------------------------------------------------------------------------------
421 void SAL_CALL
DocumentUndoManager::addUndoManagerListener( const Reference
< XUndoManagerListener
>& i_listener
) throw (RuntimeException
)
423 UndoManagerGuard
aGuard( *this );
424 return m_pImpl
->aUndoHelper
.addUndoManagerListener( i_listener
);
427 //------------------------------------------------------------------------------------------------------------------
428 void SAL_CALL
DocumentUndoManager::removeUndoManagerListener( const Reference
< XUndoManagerListener
>& i_listener
) throw (RuntimeException
)
430 UndoManagerGuard
aGuard( *this );
431 return m_pImpl
->aUndoHelper
.removeUndoManagerListener( i_listener
);
434 //------------------------------------------------------------------------------------------------------------------
435 Reference
< XInterface
> SAL_CALL
DocumentUndoManager::getParent( ) throw (RuntimeException
)
437 UndoManagerGuard
aGuard( *this );
438 return static_cast< XModel
* >( &getBaseModel() );
441 //------------------------------------------------------------------------------------------------------------------
442 void SAL_CALL
DocumentUndoManager::setParent( const Reference
< XInterface
>& i_parent
) throw (NoSupportException
, RuntimeException
)
445 throw NoSupportException( OUString(), m_pImpl
->getThis() );
448 //......................................................................................................................
450 //......................................................................................................................
452 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */