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 <comphelper/diagnose_ex.hxx>
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::Sequence
;
43 using ::com::sun::star::document::XUndoAction
;
44 using ::com::sun::star::lang::NotInitializedException
;
45 using ::com::sun::star::document::XUndoManagerListener
;
46 using ::com::sun::star::document::XUndoManager
;
47 using ::com::sun::star::lang::NoSupportException
;
48 using ::com::sun::star::frame::XModel
;
50 //= DocumentUndoManager_Impl
52 struct DocumentUndoManager_Impl
: public ::framework::IUndoManagerImplementation
54 DocumentUndoManager
& rAntiImpl
;
55 SfxUndoManager
* pUndoManager
;
56 ::framework::UndoManagerHelper aUndoHelper
;
58 explicit DocumentUndoManager_Impl( DocumentUndoManager
& i_antiImpl
)
59 :rAntiImpl( i_antiImpl
)
60 ,pUndoManager( impl_retrieveUndoManager( i_antiImpl
.getBaseModel() ) )
61 // do this *before* the construction of aUndoHelper (which actually means: put pUndoManager before
62 // aUndoHelper in the member list)!
67 virtual ~DocumentUndoManager_Impl()
71 // IUndoManagerImplementation
72 virtual SfxUndoManager
& getImplUndoManager() override
;
73 virtual Reference
< XUndoManager
> getThis() override
;
77 aUndoHelper
.disposing();
78 ENSURE_OR_RETURN_VOID( pUndoManager
, "DocumentUndoManager_Impl::disposing: already disposed!" );
79 pUndoManager
= nullptr;
82 void invalidateXDo_nolck();
85 static SfxUndoManager
* impl_retrieveUndoManager( SfxBaseModel
& i_baseModel
)
87 SfxUndoManager
* pUndoManager( nullptr );
88 SfxObjectShell
* pObjectShell
= i_baseModel
.GetObjectShell();
89 if ( pObjectShell
!= nullptr )
90 pUndoManager
= pObjectShell
->GetUndoManager();
92 throw NotInitializedException( OUString(), i_baseModel
);
98 SfxUndoManager
& DocumentUndoManager_Impl::getImplUndoManager()
100 ENSURE_OR_THROW( pUndoManager
!= nullptr, "DocumentUndoManager_Impl::getImplUndoManager: no access to the doc's UndoManager implementation!" );
102 #if OSL_DEBUG_LEVEL > 0
103 // in a non-product build, assert if the current UndoManager at the shell is not the same we obtained
104 // (and cached) at construction time
105 SfxObjectShell
* pObjectShell
= rAntiImpl
.getBaseModel().GetObjectShell();
106 OSL_ENSURE( ( pObjectShell
!= nullptr ) && ( pUndoManager
== pObjectShell
->GetUndoManager() ),
107 "DocumentUndoManager_Impl::getImplUndoManager: the UndoManager changed meanwhile - what about our listener?" );
110 return *pUndoManager
;
114 Reference
< XUndoManager
> DocumentUndoManager_Impl::getThis()
116 return static_cast< XUndoManager
* >( &rAntiImpl
);
120 void DocumentUndoManager_Impl::invalidateXDo_nolck()
122 SfxModelGuard
aGuard( rAntiImpl
);
124 const SfxObjectShell
* pDocShell
= rAntiImpl
.getBaseModel().GetObjectShell();
125 ENSURE_OR_THROW( pDocShell
!= nullptr, "lcl_invalidateUndo: no access to the doc shell!" );
126 SfxViewFrame
* pViewFrame
= SfxViewFrame::GetFirst( pDocShell
);
129 pViewFrame
->GetBindings().Invalidate( SID_UNDO
);
130 pViewFrame
->GetBindings().Invalidate( SID_REDO
);
131 pViewFrame
= SfxViewFrame::GetNext( *pViewFrame
, pDocShell
);
140 /** a facade for the SolarMutex, implementing ::framework::IMutex
142 class SolarMutexFacade
: public ::framework::IMutex
149 virtual ~SolarMutexFacade() {}
151 virtual void acquire() override
153 Application::GetSolarMutex().acquire();
156 virtual void release() override
158 Application::GetSolarMutex().release();
165 class UndoManagerGuard
:public ::framework::IMutexGuard
168 explicit UndoManagerGuard( DocumentUndoManager
& i_undoManager
)
169 :m_guard( i_undoManager
)
173 virtual ~UndoManagerGuard()
177 UndoManagerGuard(const UndoManagerGuard
&) = delete;
178 UndoManagerGuard
& operator=(const UndoManagerGuard
&) = delete;
180 virtual void clear() override
185 virtual ::framework::IMutex
& getGuardedMutex() override
187 // note that this means that we *know* that SfxModelGuard also locks the SolarMutex (nothing more, nothing less).
188 // If this ever changes, we need to adjust this code here, too.
189 return m_solarMutexFacade
;
193 SfxModelGuard m_guard
;
194 SolarMutexFacade m_solarMutexFacade
;
199 //= DocumentUndoManager
202 DocumentUndoManager::DocumentUndoManager( SfxBaseModel
& i_document
)
203 :SfxModelSubComponent( i_document
)
204 ,m_pImpl( new DocumentUndoManager_Impl( *this ) )
208 DocumentUndoManager::~DocumentUndoManager()
212 void DocumentUndoManager::disposing()
214 m_pImpl
->disposing();
218 bool DocumentUndoManager::isInContext() const
220 // No mutex locking within this method, no disposal check - this is the responsibility of the owner.
221 return m_pImpl
->getImplUndoManager().IsInListAction();
225 void SAL_CALL
DocumentUndoManager::acquire() noexcept
227 OWeakObject::acquire();
228 SfxModelSubComponent::acquireModel();
232 void SAL_CALL
DocumentUndoManager::release() noexcept
234 SfxModelSubComponent::releaseModel();
235 OWeakObject::release();
239 void SAL_CALL
DocumentUndoManager::enterUndoContext( const OUString
& i_title
)
242 UndoManagerGuard
aGuard( *this );
243 m_pImpl
->aUndoHelper
.enterUndoContext( i_title
, aGuard
);
245 m_pImpl
->invalidateXDo_nolck();
249 void SAL_CALL
DocumentUndoManager::enterHiddenUndoContext( )
252 UndoManagerGuard
aGuard( *this );
253 m_pImpl
->aUndoHelper
.enterHiddenUndoContext( aGuard
);
255 m_pImpl
->invalidateXDo_nolck();
259 void SAL_CALL
DocumentUndoManager::leaveUndoContext( )
262 UndoManagerGuard
aGuard( *this );
263 m_pImpl
->aUndoHelper
.leaveUndoContext( aGuard
);
265 m_pImpl
->invalidateXDo_nolck();
269 void SAL_CALL
DocumentUndoManager::addUndoAction( const Reference
< XUndoAction
>& i_action
)
272 UndoManagerGuard
aGuard( *this );
273 m_pImpl
->aUndoHelper
.addUndoAction( i_action
, aGuard
);
275 m_pImpl
->invalidateXDo_nolck();
279 void SAL_CALL
DocumentUndoManager::undo( )
282 UndoManagerGuard
aGuard( *this );
283 m_pImpl
->aUndoHelper
.undo( aGuard
);
285 m_pImpl
->invalidateXDo_nolck();
289 void SAL_CALL
DocumentUndoManager::redo( )
292 UndoManagerGuard
aGuard( *this );
293 m_pImpl
->aUndoHelper
.redo( aGuard
);
295 m_pImpl
->invalidateXDo_nolck();
299 sal_Bool SAL_CALL
DocumentUndoManager::isUndoPossible( )
301 UndoManagerGuard
aGuard( *this );
302 return m_pImpl
->aUndoHelper
.isUndoPossible();
306 sal_Bool SAL_CALL
DocumentUndoManager::isRedoPossible( )
308 UndoManagerGuard
aGuard( *this );
309 return m_pImpl
->aUndoHelper
.isRedoPossible();
313 OUString SAL_CALL
DocumentUndoManager::getCurrentUndoActionTitle( )
315 UndoManagerGuard
aGuard( *this );
316 return m_pImpl
->aUndoHelper
.getCurrentUndoActionTitle();
320 OUString SAL_CALL
DocumentUndoManager::getCurrentRedoActionTitle( )
322 UndoManagerGuard
aGuard( *this );
323 return m_pImpl
->aUndoHelper
.getCurrentRedoActionTitle();
327 Sequence
< OUString
> SAL_CALL
DocumentUndoManager::getAllUndoActionTitles( )
329 UndoManagerGuard
aGuard( *this );
330 return m_pImpl
->aUndoHelper
.getAllUndoActionTitles();
334 Sequence
< OUString
> SAL_CALL
DocumentUndoManager::getAllRedoActionTitles( )
336 UndoManagerGuard
aGuard( *this );
337 return m_pImpl
->aUndoHelper
.getAllRedoActionTitles();
341 void SAL_CALL
DocumentUndoManager::clear( )
344 UndoManagerGuard
aGuard( *this );
345 m_pImpl
->aUndoHelper
.clear( aGuard
);
347 m_pImpl
->invalidateXDo_nolck();
351 void SAL_CALL
DocumentUndoManager::clearRedo( )
354 UndoManagerGuard
aGuard( *this );
355 m_pImpl
->aUndoHelper
.clearRedo( aGuard
);
357 m_pImpl
->invalidateXDo_nolck();
361 void SAL_CALL
DocumentUndoManager::reset()
364 UndoManagerGuard
aGuard( *this );
365 m_pImpl
->aUndoHelper
.reset( aGuard
);
367 m_pImpl
->invalidateXDo_nolck();
371 void SAL_CALL
DocumentUndoManager::lock( )
373 UndoManagerGuard
aGuard( *this );
374 m_pImpl
->aUndoHelper
.lock();
378 void SAL_CALL
DocumentUndoManager::unlock( )
380 UndoManagerGuard
aGuard( *this );
381 m_pImpl
->aUndoHelper
.unlock();
385 sal_Bool SAL_CALL
DocumentUndoManager::isLocked( )
387 UndoManagerGuard
aGuard( *this );
388 return m_pImpl
->aUndoHelper
.isLocked();
392 void SAL_CALL
DocumentUndoManager::addUndoManagerListener( const Reference
< XUndoManagerListener
>& i_listener
)
394 UndoManagerGuard
aGuard( *this );
395 return m_pImpl
->aUndoHelper
.addUndoManagerListener( i_listener
);
399 void SAL_CALL
DocumentUndoManager::removeUndoManagerListener( const Reference
< XUndoManagerListener
>& i_listener
)
401 UndoManagerGuard
aGuard( *this );
402 return m_pImpl
->aUndoHelper
.removeUndoManagerListener( i_listener
);
406 Reference
< XInterface
> SAL_CALL
DocumentUndoManager::getParent( )
408 UndoManagerGuard
aGuard( *this );
409 return static_cast< XModel
* >( &getBaseModel() );
413 void SAL_CALL
DocumentUndoManager::setParent( const Reference
< XInterface
>& )
415 throw NoSupportException( OUString(), m_pImpl
->getThis() );
422 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */