Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / framework / source / fwe / helper / documentundoguard.cxx
blobc4aac1619b1e23558a4fdaa59f22c6b759389dce
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <framework/documentundoguard.hxx>
22 #include <com/sun/star/document/XUndoManagerSupplier.hpp>
24 #include <cppuhelper/implbase.hxx>
25 #include <rtl/ref.hxx>
26 #include <tools/diagnose_ex.h>
28 namespace framework
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::XInterface;
33 using ::com::sun::star::uno::UNO_QUERY;
34 using ::com::sun::star::uno::UNO_QUERY_THROW;
35 using ::com::sun::star::uno::Exception;
36 using ::com::sun::star::document::XUndoManagerSupplier;
37 using ::com::sun::star::document::XUndoManager;
38 using ::com::sun::star::document::XUndoManagerListener;
39 using ::com::sun::star::document::UndoManagerEvent;
40 using ::com::sun::star::lang::EventObject;
42 //= UndoManagerContextListener
44 typedef ::cppu::WeakImplHelper < XUndoManagerListener
45 > UndoManagerContextListener_Base;
46 class UndoManagerContextListener : public UndoManagerContextListener_Base
48 public:
49 explicit UndoManagerContextListener( const Reference< XUndoManager >& i_undoManager )
50 :m_xUndoManager( i_undoManager )
51 ,m_nRelativeContextDepth( 0 )
52 ,m_documentDisposed( false )
54 osl_atomic_increment( &m_refCount );
56 m_xUndoManager->addUndoManagerListener( this );
58 osl_atomic_decrement( &m_refCount );
61 void finish()
63 OSL_ENSURE( m_nRelativeContextDepth >= 0, "UndoManagerContextListener: more contexts left than entered?" );
65 if ( m_documentDisposed )
66 return;
68 // work with a copy of m_nRelativeContextDepth, to be independent from possible bugs in the
69 // listener notifications (where it would be decremented with every leaveUndoContext)
70 sal_Int32 nDepth = m_nRelativeContextDepth;
71 while ( nDepth-- > 0 )
73 m_xUndoManager->leaveUndoContext();
75 m_xUndoManager->removeUndoManagerListener( this );
78 // XUndoManagerListener
79 virtual void SAL_CALL undoActionAdded( const UndoManagerEvent& i_event ) override;
80 virtual void SAL_CALL actionUndone( const UndoManagerEvent& i_event ) override;
81 virtual void SAL_CALL actionRedone( const UndoManagerEvent& i_event ) override;
82 virtual void SAL_CALL allActionsCleared( const EventObject& i_event ) override;
83 virtual void SAL_CALL redoActionsCleared( const EventObject& i_event ) override;
84 virtual void SAL_CALL resetAll( const EventObject& i_event ) override;
85 virtual void SAL_CALL enteredContext( const UndoManagerEvent& i_event ) override;
86 virtual void SAL_CALL enteredHiddenContext( const UndoManagerEvent& i_event ) override;
87 virtual void SAL_CALL leftContext( const UndoManagerEvent& i_event ) override;
88 virtual void SAL_CALL leftHiddenContext( const UndoManagerEvent& i_event ) override;
89 virtual void SAL_CALL cancelledContext( const UndoManagerEvent& i_event ) override;
91 // XEventListener
92 virtual void SAL_CALL disposing( const EventObject& i_event ) override;
94 private:
95 Reference< XUndoManager > const m_xUndoManager;
96 oslInterlockedCount m_nRelativeContextDepth;
97 bool m_documentDisposed;
100 void SAL_CALL UndoManagerContextListener::undoActionAdded( const UndoManagerEvent& )
102 // not interested in
105 void SAL_CALL UndoManagerContextListener::actionUndone( const UndoManagerEvent& )
107 // not interested in
110 void SAL_CALL UndoManagerContextListener::actionRedone( const UndoManagerEvent& )
112 // not interested in
115 void SAL_CALL UndoManagerContextListener::allActionsCleared( const EventObject& )
117 // not interested in
120 void SAL_CALL UndoManagerContextListener::redoActionsCleared( const EventObject& )
122 // not interested in
125 void SAL_CALL UndoManagerContextListener::resetAll( const EventObject& )
127 m_nRelativeContextDepth = 0;
130 void SAL_CALL UndoManagerContextListener::enteredContext( const UndoManagerEvent& )
132 osl_atomic_increment( &m_nRelativeContextDepth );
135 void SAL_CALL UndoManagerContextListener::enteredHiddenContext( const UndoManagerEvent& )
137 osl_atomic_increment( &m_nRelativeContextDepth );
140 void SAL_CALL UndoManagerContextListener::leftContext( const UndoManagerEvent& )
142 osl_atomic_decrement( &m_nRelativeContextDepth );
145 void SAL_CALL UndoManagerContextListener::leftHiddenContext( const UndoManagerEvent& )
147 osl_atomic_decrement( &m_nRelativeContextDepth );
150 void SAL_CALL UndoManagerContextListener::cancelledContext( const UndoManagerEvent& )
152 osl_atomic_decrement( &m_nRelativeContextDepth );
155 void SAL_CALL UndoManagerContextListener::disposing( const EventObject& )
157 m_documentDisposed = true;
160 //= DocumentUndoGuard_Data
162 struct DocumentUndoGuard_Data
164 Reference< XUndoManager > xUndoManager;
165 ::rtl::Reference< UndoManagerContextListener > pContextListener;
168 namespace
171 void lcl_init( DocumentUndoGuard_Data& i_data, const Reference< XInterface >& i_undoSupplierComponent )
175 Reference< XUndoManagerSupplier > xUndoSupplier( i_undoSupplierComponent, UNO_QUERY );
176 if ( xUndoSupplier.is() )
177 i_data.xUndoManager.set( xUndoSupplier->getUndoManager(), css::uno::UNO_SET_THROW );
179 if ( i_data.xUndoManager.is() )
180 i_data.pContextListener.set( new UndoManagerContextListener( i_data.xUndoManager ) );
182 catch( const Exception& )
184 DBG_UNHANDLED_EXCEPTION("fwk");
188 void lcl_restore( DocumentUndoGuard_Data& i_data )
192 if ( i_data.pContextListener.is() )
193 i_data.pContextListener->finish();
194 i_data.pContextListener.clear();
196 catch( const Exception& )
198 DBG_UNHANDLED_EXCEPTION("fwk");
203 //= DocumentUndoGuard
205 DocumentUndoGuard::DocumentUndoGuard( const Reference< XInterface >& i_undoSupplierComponent )
206 :m_xData( new DocumentUndoGuard_Data )
208 lcl_init( *m_xData, i_undoSupplierComponent );
211 DocumentUndoGuard::~DocumentUndoGuard()
213 lcl_restore( *m_xData );
216 } // namespace framework
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */