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>
27 #include <sfx2/sfxsids.hrc>
28 #include <com/sun/star/lang/NoSupportException.hpp>
29 #include <com/sun/star/lang/NotInitializedException.hpp>
30 #include <svl/undo.hxx>
31 #include <tools/diagnose_ex.h>
32 #include <framework/undomanagerhelper.hxx>
33 #include <framework/imutex.hxx>
40 using ::com::sun::star::uno::Reference
;
41 using ::com::sun::star::uno::XInterface
;
42 using ::com::sun::star::uno::RuntimeException
;
43 using ::com::sun::star::uno::Sequence
;
44 using ::com::sun::star::document::XUndoAction
;
45 using ::com::sun::star::lang::NotInitializedException
;
46 using ::com::sun::star::document::XUndoManagerListener
;
47 using ::com::sun::star::document::XUndoManager
;
48 using ::com::sun::star::lang::NoSupportException
;
49 using ::com::sun::star::frame::XModel
;
51 //= DocumentUndoManager_Impl
53 struct DocumentUndoManager_Impl
: public ::framework::IUndoManagerImplementation
55 DocumentUndoManager
& rAntiImpl
;
56 SfxUndoManager
* pUndoManager
;
57 ::framework::UndoManagerHelper aUndoHelper
;
59 explicit DocumentUndoManager_Impl( DocumentUndoManager
& i_antiImpl
)
60 :rAntiImpl( i_antiImpl
)
61 ,pUndoManager( impl_retrieveUndoManager( i_antiImpl
.getBaseModel() ) )
62 // do this *before* the construction of aUndoHelper (which actually means: put pUndoManager before
63 // aUndoHelper in the member list)!
68 virtual ~DocumentUndoManager_Impl()
72 // IUndoManagerImplementation
73 virtual SfxUndoManager
& getImplUndoManager() override
;
74 virtual Reference
< XUndoManager
> getThis() override
;
78 aUndoHelper
.disposing();
79 ENSURE_OR_RETURN_VOID( pUndoManager
, "DocumentUndoManager_Impl::disposing: already disposed!" );
80 pUndoManager
= nullptr;
83 void invalidateXDo_nolck();
86 static SfxUndoManager
* impl_retrieveUndoManager( SfxBaseModel
& i_baseModel
)
88 SfxUndoManager
* pUndoManager( nullptr );
89 SfxObjectShell
* pObjectShell
= i_baseModel
.GetObjectShell();
90 if ( pObjectShell
!= nullptr )
91 pUndoManager
= pObjectShell
->GetUndoManager();
93 throw NotInitializedException( OUString(), i_baseModel
);
99 SfxUndoManager
& DocumentUndoManager_Impl::getImplUndoManager()
101 ENSURE_OR_THROW( pUndoManager
!= nullptr, "DocumentUndoManager_Impl::getImplUndoManager: no access to the doc's UndoManager implementation!" );
103 #if OSL_DEBUG_LEVEL > 0
104 // in a non-product build, assert if the current UndoManager at the shell is not the same we obtained
105 // (and cached) at construction time
106 SfxObjectShell
* pObjectShell
= rAntiImpl
.getBaseModel().GetObjectShell();
107 OSL_ENSURE( ( pObjectShell
!= nullptr ) && ( pUndoManager
== pObjectShell
->GetUndoManager() ),
108 "DocumentUndoManager_Impl::getImplUndoManager: the UndoManager changed meanwhile - what about our listener?" );
111 return *pUndoManager
;
115 Reference
< XUndoManager
> DocumentUndoManager_Impl::getThis()
117 return static_cast< XUndoManager
* >( &rAntiImpl
);
121 void DocumentUndoManager_Impl::invalidateXDo_nolck()
123 SfxModelGuard
aGuard( rAntiImpl
);
125 const SfxObjectShell
* pDocShell
= rAntiImpl
.getBaseModel().GetObjectShell();
126 ENSURE_OR_THROW( pDocShell
!= nullptr, "lcl_invalidateUndo: no access to the doc shell!" );
127 SfxViewFrame
* pViewFrame
= SfxViewFrame::GetFirst( pDocShell
);
130 pViewFrame
->GetBindings().Invalidate( SID_UNDO
);
131 pViewFrame
->GetBindings().Invalidate( SID_REDO
);
132 pViewFrame
= SfxViewFrame::GetNext( *pViewFrame
, pDocShell
);
141 /** a facade for the SolarMutex, implementing ::framework::IMutex
143 class SolarMutexFacade
: public ::framework::IMutex
150 virtual ~SolarMutexFacade() {}
152 virtual void acquire() override
154 Application::GetSolarMutex().acquire();
157 virtual void release() override
159 Application::GetSolarMutex().release();
166 class UndoManagerGuard
:public ::framework::IMutexGuard
169 explicit UndoManagerGuard( DocumentUndoManager
& i_undoManager
)
170 :m_guard( i_undoManager
)
171 ,m_solarMutexFacade()
175 virtual ~UndoManagerGuard()
179 UndoManagerGuard(const UndoManagerGuard
&) = delete;
180 UndoManagerGuard
& operator=(const UndoManagerGuard
&) = delete;
182 virtual void clear() override
187 virtual ::framework::IMutex
& getGuardedMutex() override
189 // note that this means that we *know* that SfxModelGuard also locks the SolarMutex (nothing more, nothing less).
190 // If this ever changes, we need to adjust this code here, too.
191 return m_solarMutexFacade
;
195 SfxModelGuard m_guard
;
196 SolarMutexFacade m_solarMutexFacade
;
201 //= DocumentUndoManager
204 DocumentUndoManager::DocumentUndoManager( SfxBaseModel
& i_document
)
205 :SfxModelSubComponent( i_document
)
206 ,m_pImpl( new DocumentUndoManager_Impl( *this ) )
210 DocumentUndoManager::~DocumentUndoManager()
214 void DocumentUndoManager::disposing()
216 m_pImpl
->disposing();
220 bool DocumentUndoManager::isInContext() const
222 // No mutex locking within this method, no disposal check - this is the responsibility of the owner.
223 return m_pImpl
->getImplUndoManager().IsInListAction();
227 void SAL_CALL
DocumentUndoManager::acquire() throw()
229 OWeakObject::acquire();
230 SfxModelSubComponent::acquireModel();
234 void SAL_CALL
DocumentUndoManager::release() throw()
236 SfxModelSubComponent::releaseModel();
237 OWeakObject::release();
241 void SAL_CALL
DocumentUndoManager::enterUndoContext( const OUString
& i_title
)
244 UndoManagerGuard
aGuard( *this );
245 m_pImpl
->aUndoHelper
.enterUndoContext( i_title
, aGuard
);
247 m_pImpl
->invalidateXDo_nolck();
251 void SAL_CALL
DocumentUndoManager::enterHiddenUndoContext( )
254 UndoManagerGuard
aGuard( *this );
255 m_pImpl
->aUndoHelper
.enterHiddenUndoContext( aGuard
);
257 m_pImpl
->invalidateXDo_nolck();
261 void SAL_CALL
DocumentUndoManager::leaveUndoContext( )
264 UndoManagerGuard
aGuard( *this );
265 m_pImpl
->aUndoHelper
.leaveUndoContext( aGuard
);
267 m_pImpl
->invalidateXDo_nolck();
271 void SAL_CALL
DocumentUndoManager::addUndoAction( const Reference
< XUndoAction
>& i_action
)
274 UndoManagerGuard
aGuard( *this );
275 m_pImpl
->aUndoHelper
.addUndoAction( i_action
, aGuard
);
277 m_pImpl
->invalidateXDo_nolck();
281 void SAL_CALL
DocumentUndoManager::undo( )
284 UndoManagerGuard
aGuard( *this );
285 m_pImpl
->aUndoHelper
.undo( aGuard
);
287 m_pImpl
->invalidateXDo_nolck();
291 void SAL_CALL
DocumentUndoManager::redo( )
294 UndoManagerGuard
aGuard( *this );
295 m_pImpl
->aUndoHelper
.redo( aGuard
);
297 m_pImpl
->invalidateXDo_nolck();
301 sal_Bool SAL_CALL
DocumentUndoManager::isUndoPossible( )
303 UndoManagerGuard
aGuard( *this );
304 return m_pImpl
->aUndoHelper
.isUndoPossible();
308 sal_Bool SAL_CALL
DocumentUndoManager::isRedoPossible( )
310 UndoManagerGuard
aGuard( *this );
311 return m_pImpl
->aUndoHelper
.isRedoPossible();
315 OUString SAL_CALL
DocumentUndoManager::getCurrentUndoActionTitle( )
317 UndoManagerGuard
aGuard( *this );
318 return m_pImpl
->aUndoHelper
.getCurrentUndoActionTitle();
322 OUString SAL_CALL
DocumentUndoManager::getCurrentRedoActionTitle( )
324 UndoManagerGuard
aGuard( *this );
325 return m_pImpl
->aUndoHelper
.getCurrentRedoActionTitle();
329 Sequence
< OUString
> SAL_CALL
DocumentUndoManager::getAllUndoActionTitles( )
331 UndoManagerGuard
aGuard( *this );
332 return m_pImpl
->aUndoHelper
.getAllUndoActionTitles();
336 Sequence
< OUString
> SAL_CALL
DocumentUndoManager::getAllRedoActionTitles( )
338 UndoManagerGuard
aGuard( *this );
339 return m_pImpl
->aUndoHelper
.getAllRedoActionTitles();
343 void SAL_CALL
DocumentUndoManager::clear( )
346 UndoManagerGuard
aGuard( *this );
347 m_pImpl
->aUndoHelper
.clear( aGuard
);
349 m_pImpl
->invalidateXDo_nolck();
353 void SAL_CALL
DocumentUndoManager::clearRedo( )
356 UndoManagerGuard
aGuard( *this );
357 m_pImpl
->aUndoHelper
.clearRedo( aGuard
);
359 m_pImpl
->invalidateXDo_nolck();
363 void SAL_CALL
DocumentUndoManager::reset()
366 UndoManagerGuard
aGuard( *this );
367 m_pImpl
->aUndoHelper
.reset( aGuard
);
369 m_pImpl
->invalidateXDo_nolck();
373 void SAL_CALL
DocumentUndoManager::lock( )
375 UndoManagerGuard
aGuard( *this );
376 m_pImpl
->aUndoHelper
.lock();
380 void SAL_CALL
DocumentUndoManager::unlock( )
382 UndoManagerGuard
aGuard( *this );
383 m_pImpl
->aUndoHelper
.unlock();
387 sal_Bool SAL_CALL
DocumentUndoManager::isLocked( )
389 UndoManagerGuard
aGuard( *this );
390 return m_pImpl
->aUndoHelper
.isLocked();
394 void SAL_CALL
DocumentUndoManager::addUndoManagerListener( const Reference
< XUndoManagerListener
>& i_listener
)
396 UndoManagerGuard
aGuard( *this );
397 return m_pImpl
->aUndoHelper
.addUndoManagerListener( i_listener
);
401 void SAL_CALL
DocumentUndoManager::removeUndoManagerListener( const Reference
< XUndoManagerListener
>& i_listener
)
403 UndoManagerGuard
aGuard( *this );
404 return m_pImpl
->aUndoHelper
.removeUndoManagerListener( i_listener
);
408 Reference
< XInterface
> SAL_CALL
DocumentUndoManager::getParent( )
410 UndoManagerGuard
aGuard( *this );
411 return static_cast< XModel
* >( &getBaseModel() );
415 void SAL_CALL
DocumentUndoManager::setParent( const Reference
< XInterface
>& )
417 throw NoSupportException( OUString(), m_pImpl
->getThis() );
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */