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 .
20 #include "UndoManager.hxx"
21 #include <ChartViewHelper.hxx>
23 #include <com/sun/star/frame/XModel.hpp>
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <com/sun/star/lang/NoSupportException.hpp>
27 #include <framework/undomanagerhelper.hxx>
28 #include <framework/imutex.hxx>
29 #include <officecfg/Office/Common.hxx>
30 #include <svl/undo.hxx>
35 using ::com::sun::star::uno::Reference
;
36 using ::com::sun::star::uno::XInterface
;
37 using ::com::sun::star::uno::UNO_QUERY
;
38 using ::com::sun::star::uno::Sequence
;
39 using ::com::sun::star::lang::DisposedException
;
40 using ::com::sun::star::document::XUndoManager
;
41 using ::com::sun::star::document::XUndoAction
;
42 using ::com::sun::star::document::XUndoManagerListener
;
43 using ::com::sun::star::lang::NoSupportException
;
44 using ::com::sun::star::util::XModifyListener
;
45 using ::com::sun::star::frame::XModel
;
49 class UndoManager_Impl
: public ::framework::IUndoManagerImplementation
52 UndoManager_Impl( UndoManager
& i_antiImpl
, ::cppu::OWeakObject
& i_parent
, ::osl::Mutex
& i_mutex
)
53 :m_rAntiImpl( i_antiImpl
)
54 ,m_rParent( i_parent
)
58 ,m_aUndoHelper( *this )
60 m_aUndoManager
.SetMaxUndoActionCount(
61 officecfg::Office::Common::Undo::Steps::get());
64 virtual ~UndoManager_Impl()
68 ::osl::Mutex
& getMutex();
69 // IUndoManagerImplementation
70 virtual SfxUndoManager
& getImplUndoManager() override
;
71 virtual Reference
< XUndoManager
> getThis() override
;
74 ::cppu::OWeakObject
& getParent() { return m_rParent
; }
75 ::framework::UndoManagerHelper
& getUndoHelper() { return m_aUndoHelper
; }
79 /// is called when the owner of the UndoManager is being disposed
82 /// checks whether we're already disposed, throws a DisposedException if so
83 void checkDisposed_lck();
86 UndoManager
& m_rAntiImpl
;
87 ::cppu::OWeakObject
& m_rParent
;
88 ::osl::Mutex
& m_rMutex
;
91 SfxUndoManager m_aUndoManager
;
92 ::framework::UndoManagerHelper m_aUndoHelper
;
95 ::osl::Mutex
& UndoManager_Impl::getMutex()
100 SfxUndoManager
& UndoManager_Impl::getImplUndoManager()
102 return m_aUndoManager
;
105 Reference
< XUndoManager
> UndoManager_Impl::getThis()
110 void UndoManager_Impl::disposing()
113 ::osl::MutexGuard
aGuard( m_rMutex
);
116 m_aUndoHelper
.disposing();
119 void UndoManager_Impl::checkDisposed_lck()
122 throw DisposedException( OUString(), getThis() );
125 /** guard for public UNO methods of the UndoManager
127 The only purpose of this guard is to check for the instance being disposed already. Everything else,
128 in particular the IMutexGuard functionality required by the UndoManagerHelper class, is a dummy only,
129 as all involved classes (means we ourselves, the UndoManagerHelper, the SfxUndoManager, and the Undo actions
130 we create) are inherently thread-safe, thus need no external lock (in particular no SolarMutex!).
132 class UndoManagerMethodGuard
: public ::framework::IMutexGuard
135 explicit UndoManagerMethodGuard( UndoManager_Impl
& i_impl
)
137 ::osl::MutexGuard
aGuard( i_impl
.getMutex() );
138 // throw if the instance is already disposed
139 i_impl
.checkDisposed_lck();
141 virtual ~UndoManagerMethodGuard()
146 virtual void clear() override
;
147 virtual ::framework::IMutex
& getGuardedMutex() override
;
150 class DummyMutex
: public ::framework::IMutex
153 virtual ~DummyMutex() {}
154 virtual void acquire() override
{ }
155 virtual void release() override
{ }
158 ::framework::IMutex
& UndoManagerMethodGuard::getGuardedMutex()
160 static DummyMutex s_aDummyMutex
;
161 return s_aDummyMutex
;
164 void UndoManagerMethodGuard::clear()
166 // nothing to do. This interface implementation is a dummy.
170 using impl::UndoManagerMethodGuard
;
172 UndoManager::UndoManager( ::cppu::OWeakObject
& i_parent
, ::osl::Mutex
& i_mutex
)
173 :m_pImpl( new impl::UndoManager_Impl( *this, i_parent
, i_mutex
) )
177 UndoManager::~UndoManager()
181 void SAL_CALL
UndoManager::acquire() throw ()
183 m_pImpl
->getParent().acquire();
186 void SAL_CALL
UndoManager::release() throw ()
188 m_pImpl
->getParent().release();
191 void UndoManager::disposing()
193 m_pImpl
->disposing();
196 void SAL_CALL
UndoManager::enterUndoContext( const OUString
& i_title
)
198 UndoManagerMethodGuard
aGuard( *m_pImpl
);
199 m_pImpl
->getUndoHelper().enterUndoContext( i_title
, aGuard
);
202 void SAL_CALL
UndoManager::enterHiddenUndoContext( )
204 UndoManagerMethodGuard
aGuard( *m_pImpl
);
205 m_pImpl
->getUndoHelper().enterHiddenUndoContext( aGuard
);
208 void SAL_CALL
UndoManager::leaveUndoContext( )
210 UndoManagerMethodGuard
aGuard( *m_pImpl
);
211 m_pImpl
->getUndoHelper().leaveUndoContext( aGuard
);
214 void SAL_CALL
UndoManager::addUndoAction( const Reference
< XUndoAction
>& i_action
)
216 UndoManagerMethodGuard
aGuard( *m_pImpl
);
217 m_pImpl
->getUndoHelper().addUndoAction( i_action
, aGuard
);
220 void SAL_CALL
UndoManager::undo( )
222 UndoManagerMethodGuard
aGuard( *m_pImpl
);
223 m_pImpl
->getUndoHelper().undo( aGuard
);
225 ChartViewHelper::setViewToDirtyState( Reference
< XModel
>( getParent(), UNO_QUERY
) );
228 void SAL_CALL
UndoManager::redo( )
230 UndoManagerMethodGuard
aGuard( *m_pImpl
);
231 m_pImpl
->getUndoHelper().redo( aGuard
);
233 ChartViewHelper::setViewToDirtyState( Reference
< XModel
>( getParent(), UNO_QUERY
) );
236 sal_Bool SAL_CALL
UndoManager::isUndoPossible( )
238 UndoManagerMethodGuard
aGuard( *m_pImpl
);
239 return m_pImpl
->getUndoHelper().isUndoPossible();
242 sal_Bool SAL_CALL
UndoManager::isRedoPossible( )
244 UndoManagerMethodGuard
aGuard( *m_pImpl
);
245 return m_pImpl
->getUndoHelper().isRedoPossible();
248 OUString SAL_CALL
UndoManager::getCurrentUndoActionTitle( )
250 UndoManagerMethodGuard
aGuard( *m_pImpl
);
251 return m_pImpl
->getUndoHelper().getCurrentUndoActionTitle();
254 OUString SAL_CALL
UndoManager::getCurrentRedoActionTitle( )
256 UndoManagerMethodGuard
aGuard( *m_pImpl
);
257 return m_pImpl
->getUndoHelper().getCurrentRedoActionTitle();
260 Sequence
< OUString
> SAL_CALL
UndoManager::getAllUndoActionTitles( )
262 UndoManagerMethodGuard
aGuard( *m_pImpl
);
263 return m_pImpl
->getUndoHelper().getAllUndoActionTitles();
266 Sequence
< OUString
> SAL_CALL
UndoManager::getAllRedoActionTitles( )
268 UndoManagerMethodGuard
aGuard( *m_pImpl
);
269 return m_pImpl
->getUndoHelper().getAllRedoActionTitles();
272 void SAL_CALL
UndoManager::clear( )
274 UndoManagerMethodGuard
aGuard( *m_pImpl
);
275 m_pImpl
->getUndoHelper().clear( aGuard
);
278 void SAL_CALL
UndoManager::clearRedo( )
280 UndoManagerMethodGuard
aGuard( *m_pImpl
);
281 m_pImpl
->getUndoHelper().clearRedo( aGuard
);
284 void SAL_CALL
UndoManager::reset( )
286 UndoManagerMethodGuard
aGuard( *m_pImpl
);
287 m_pImpl
->getUndoHelper().reset( aGuard
);
290 void SAL_CALL
UndoManager::addUndoManagerListener( const Reference
< XUndoManagerListener
>& i_listener
)
292 UndoManagerMethodGuard
aGuard( *m_pImpl
);
293 m_pImpl
->getUndoHelper().addUndoManagerListener( i_listener
);
296 void SAL_CALL
UndoManager::removeUndoManagerListener( const Reference
< XUndoManagerListener
>& i_listener
)
298 UndoManagerMethodGuard
aGuard( *m_pImpl
);
299 m_pImpl
->getUndoHelper().removeUndoManagerListener( i_listener
);
302 void SAL_CALL
UndoManager::lock( )
304 UndoManagerMethodGuard
aGuard( *m_pImpl
);
305 m_pImpl
->getUndoHelper().lock();
308 void SAL_CALL
UndoManager::unlock( )
310 UndoManagerMethodGuard
aGuard( *m_pImpl
);
311 m_pImpl
->getUndoHelper().unlock();
314 sal_Bool SAL_CALL
UndoManager::isLocked( )
316 UndoManagerMethodGuard
aGuard( *m_pImpl
);
317 return m_pImpl
->getUndoHelper().isLocked();
320 Reference
< XInterface
> SAL_CALL
UndoManager::getParent( )
322 UndoManagerMethodGuard
aGuard( *m_pImpl
);
323 return m_pImpl
->getParent();
326 void SAL_CALL
UndoManager::setParent( const Reference
< XInterface
>& )
328 UndoManagerMethodGuard
aGuard( *m_pImpl
);
329 throw NoSupportException( OUString(), m_pImpl
->getThis() );
332 void SAL_CALL
UndoManager::addModifyListener( const Reference
< XModifyListener
>& i_listener
)
334 UndoManagerMethodGuard
aGuard( *m_pImpl
);
335 m_pImpl
->getUndoHelper().addModifyListener( i_listener
);
338 void SAL_CALL
UndoManager::removeModifyListener( const Reference
< XModifyListener
>& i_listener
)
340 UndoManagerMethodGuard
aGuard( *m_pImpl
);
341 m_pImpl
->getUndoHelper().removeModifyListener( i_listener
);
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */