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 <ChartModel.hxx>
22 #include <ChartViewHelper.hxx>
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::Sequence
;
38 using ::com::sun::star::lang::DisposedException
;
39 using ::com::sun::star::document::XUndoManager
;
40 using ::com::sun::star::document::XUndoAction
;
41 using ::com::sun::star::document::XUndoManagerListener
;
42 using ::com::sun::star::lang::NoSupportException
;
43 using ::com::sun::star::util::XModifyListener
;
47 class UndoManager_Impl
: public ::framework::IUndoManagerImplementation
50 UndoManager_Impl( UndoManager
& i_antiImpl
, ::chart::ChartModel
& i_parent
, ::osl::Mutex
& i_mutex
)
51 :m_rAntiImpl( i_antiImpl
)
52 ,m_rParent( i_parent
)
55 ,m_aUndoHelper( *this )
57 m_aUndoManager
.SetMaxUndoActionCount(
58 officecfg::Office::Common::Undo::Steps::get());
61 virtual ~UndoManager_Impl()
65 ::osl::Mutex
& getMutex();
66 // IUndoManagerImplementation
67 virtual SfxUndoManager
& getImplUndoManager() override
;
68 virtual Reference
< XUndoManager
> getThis() override
;
71 ::chart::ChartModel
& getParent() { return m_rParent
; }
72 ::framework::UndoManagerHelper
& getUndoHelper() { return m_aUndoHelper
; }
76 /// is called when the owner of the UndoManager is being disposed
79 /// checks whether we're already disposed, throws a DisposedException if so
80 void checkDisposed_lck();
83 UndoManager
& m_rAntiImpl
;
84 ::chart::ChartModel
& m_rParent
;
85 ::osl::Mutex
& m_rMutex
;
88 SfxUndoManager m_aUndoManager
;
89 ::framework::UndoManagerHelper m_aUndoHelper
;
92 ::osl::Mutex
& UndoManager_Impl::getMutex()
97 SfxUndoManager
& UndoManager_Impl::getImplUndoManager()
99 return m_aUndoManager
;
102 Reference
< XUndoManager
> UndoManager_Impl::getThis()
107 void UndoManager_Impl::disposing()
110 ::osl::MutexGuard
aGuard( m_rMutex
);
113 m_aUndoHelper
.disposing();
116 void UndoManager_Impl::checkDisposed_lck()
119 throw DisposedException( OUString(), getThis() );
124 /** guard for public UNO methods of the UndoManager
126 The only purpose of this guard is to check for the instance being disposed already. Everything else,
127 in particular the IMutexGuard functionality required by the UndoManagerHelper class, is a dummy only,
128 as all involved classes (means we ourselves, the UndoManagerHelper, the SfxUndoManager, and the Undo actions
129 we create) are inherently thread-safe, thus need no external lock (in particular no SolarMutex!).
131 class UndoManagerMethodGuard
: public ::framework::IMutexGuard
134 explicit UndoManagerMethodGuard( UndoManager_Impl
& i_impl
)
136 ::osl::MutexGuard
aGuard( i_impl
.getMutex() );
137 // throw if the instance is already disposed
138 i_impl
.checkDisposed_lck();
140 virtual ~UndoManagerMethodGuard()
145 virtual void clear() override
;
146 virtual ::framework::IMutex
& getGuardedMutex() override
;
149 class DummyMutex
: public ::framework::IMutex
152 virtual ~DummyMutex() {}
153 virtual void acquire() override
{ }
154 virtual void release() override
{ }
159 ::framework::IMutex
& UndoManagerMethodGuard::getGuardedMutex()
161 static DummyMutex s_aDummyMutex
;
162 return s_aDummyMutex
;
165 void UndoManagerMethodGuard::clear()
167 // nothing to do. This interface implementation is a dummy.
171 using impl::UndoManagerMethodGuard
;
173 UndoManager::UndoManager( ::chart::ChartModel
& i_parent
, ::osl::Mutex
& i_mutex
)
174 :m_pImpl( new impl::UndoManager_Impl( *this, i_parent
, i_mutex
) )
178 UndoManager::~UndoManager()
182 void SAL_CALL
UndoManager::acquire() noexcept
184 m_pImpl
->getParent().acquire();
187 void SAL_CALL
UndoManager::release() noexcept
189 m_pImpl
->getParent().release();
192 void UndoManager::disposing()
194 m_pImpl
->disposing();
197 void SAL_CALL
UndoManager::enterUndoContext( const OUString
& i_title
)
199 UndoManagerMethodGuard
aGuard( *m_pImpl
);
200 m_pImpl
->getUndoHelper().enterUndoContext( i_title
, aGuard
);
203 void SAL_CALL
UndoManager::enterHiddenUndoContext( )
205 UndoManagerMethodGuard
aGuard( *m_pImpl
);
206 m_pImpl
->getUndoHelper().enterHiddenUndoContext( aGuard
);
209 void SAL_CALL
UndoManager::leaveUndoContext( )
211 UndoManagerMethodGuard
aGuard( *m_pImpl
);
212 m_pImpl
->getUndoHelper().leaveUndoContext( aGuard
);
215 void SAL_CALL
UndoManager::addUndoAction( const Reference
< XUndoAction
>& i_action
)
217 UndoManagerMethodGuard
aGuard( *m_pImpl
);
218 m_pImpl
->getUndoHelper().addUndoAction( i_action
, aGuard
);
221 void SAL_CALL
UndoManager::undo( )
223 UndoManagerMethodGuard
aGuard( *m_pImpl
);
224 m_pImpl
->getUndoHelper().undo( aGuard
);
226 ChartViewHelper::setViewToDirtyState( &m_pImpl
->getParent() );
229 void SAL_CALL
UndoManager::redo( )
231 UndoManagerMethodGuard
aGuard( *m_pImpl
);
232 m_pImpl
->getUndoHelper().redo( aGuard
);
234 ChartViewHelper::setViewToDirtyState( &m_pImpl
->getParent() );
237 sal_Bool SAL_CALL
UndoManager::isUndoPossible( )
239 UndoManagerMethodGuard
aGuard( *m_pImpl
);
240 return m_pImpl
->getUndoHelper().isUndoPossible();
243 sal_Bool SAL_CALL
UndoManager::isRedoPossible( )
245 UndoManagerMethodGuard
aGuard( *m_pImpl
);
246 return m_pImpl
->getUndoHelper().isRedoPossible();
249 OUString SAL_CALL
UndoManager::getCurrentUndoActionTitle( )
251 UndoManagerMethodGuard
aGuard( *m_pImpl
);
252 return m_pImpl
->getUndoHelper().getCurrentUndoActionTitle();
255 OUString SAL_CALL
UndoManager::getCurrentRedoActionTitle( )
257 UndoManagerMethodGuard
aGuard( *m_pImpl
);
258 return m_pImpl
->getUndoHelper().getCurrentRedoActionTitle();
261 Sequence
< OUString
> SAL_CALL
UndoManager::getAllUndoActionTitles( )
263 UndoManagerMethodGuard
aGuard( *m_pImpl
);
264 return m_pImpl
->getUndoHelper().getAllUndoActionTitles();
267 Sequence
< OUString
> SAL_CALL
UndoManager::getAllRedoActionTitles( )
269 UndoManagerMethodGuard
aGuard( *m_pImpl
);
270 return m_pImpl
->getUndoHelper().getAllRedoActionTitles();
273 void SAL_CALL
UndoManager::clear( )
275 UndoManagerMethodGuard
aGuard( *m_pImpl
);
276 m_pImpl
->getUndoHelper().clear( aGuard
);
279 void SAL_CALL
UndoManager::clearRedo( )
281 UndoManagerMethodGuard
aGuard( *m_pImpl
);
282 m_pImpl
->getUndoHelper().clearRedo( aGuard
);
285 void SAL_CALL
UndoManager::reset( )
287 UndoManagerMethodGuard
aGuard( *m_pImpl
);
288 m_pImpl
->getUndoHelper().reset( aGuard
);
291 void SAL_CALL
UndoManager::addUndoManagerListener( const Reference
< XUndoManagerListener
>& i_listener
)
293 UndoManagerMethodGuard
aGuard( *m_pImpl
);
294 m_pImpl
->getUndoHelper().addUndoManagerListener( i_listener
);
297 void SAL_CALL
UndoManager::removeUndoManagerListener( const Reference
< XUndoManagerListener
>& i_listener
)
299 UndoManagerMethodGuard
aGuard( *m_pImpl
);
300 m_pImpl
->getUndoHelper().removeUndoManagerListener( i_listener
);
303 void SAL_CALL
UndoManager::lock( )
305 UndoManagerMethodGuard
aGuard( *m_pImpl
);
306 m_pImpl
->getUndoHelper().lock();
309 void SAL_CALL
UndoManager::unlock( )
311 UndoManagerMethodGuard
aGuard( *m_pImpl
);
312 m_pImpl
->getUndoHelper().unlock();
315 sal_Bool SAL_CALL
UndoManager::isLocked( )
317 UndoManagerMethodGuard
aGuard( *m_pImpl
);
318 return m_pImpl
->getUndoHelper().isLocked();
321 Reference
< XInterface
> SAL_CALL
UndoManager::getParent( )
323 UndoManagerMethodGuard
aGuard( *m_pImpl
);
324 return m_pImpl
->getParent();
327 void SAL_CALL
UndoManager::setParent( const Reference
< XInterface
>& )
329 UndoManagerMethodGuard
aGuard( *m_pImpl
);
330 throw NoSupportException( OUString(), m_pImpl
->getThis() );
333 void SAL_CALL
UndoManager::addModifyListener( const Reference
< XModifyListener
>& i_listener
)
335 UndoManagerMethodGuard
aGuard( *m_pImpl
);
336 m_pImpl
->getUndoHelper().addModifyListener( i_listener
);
339 void SAL_CALL
UndoManager::removeModifyListener( const Reference
< XModifyListener
>& i_listener
)
341 UndoManagerMethodGuard
aGuard( *m_pImpl
);
342 m_pImpl
->getUndoHelper().removeModifyListener( i_listener
);
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */