xref: /AOO41X/main/framework/source/fwe/helper/documentundoguard.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
11*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
12*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
13*cdf0e10cSrcweir  *
14*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
15*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
18*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
19*cdf0e10cSrcweir  *
20*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
21*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
22*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
23*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
24*cdf0e10cSrcweir  *
25*cdf0e10cSrcweir  ************************************************************************/
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir #include "precompiled_framework.hxx"
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include "framework/documentundoguard.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir /** === begin UNO includes === **/
32*cdf0e10cSrcweir #include <com/sun/star/document/XUndoManagerSupplier.hpp>
33*cdf0e10cSrcweir /** === end UNO includes === **/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
36*cdf0e10cSrcweir #include <rtl/ref.hxx>
37*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //......................................................................................................................
40*cdf0e10cSrcweir namespace framework
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir //......................................................................................................................
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 	/** === begin UNO using === **/
45*cdf0e10cSrcweir 	using ::com::sun::star::uno::Reference;
46*cdf0e10cSrcweir 	using ::com::sun::star::uno::XInterface;
47*cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY;
48*cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY_THROW;
49*cdf0e10cSrcweir 	using ::com::sun::star::uno::Exception;
50*cdf0e10cSrcweir 	using ::com::sun::star::uno::RuntimeException;
51*cdf0e10cSrcweir 	using ::com::sun::star::uno::Any;
52*cdf0e10cSrcweir 	using ::com::sun::star::uno::makeAny;
53*cdf0e10cSrcweir 	using ::com::sun::star::uno::Sequence;
54*cdf0e10cSrcweir 	using ::com::sun::star::uno::Type;
55*cdf0e10cSrcweir     using ::com::sun::star::document::XUndoManagerSupplier;
56*cdf0e10cSrcweir     using ::com::sun::star::document::XUndoManager;
57*cdf0e10cSrcweir     using ::com::sun::star::document::XUndoManagerListener;
58*cdf0e10cSrcweir     using ::com::sun::star::document::UndoManagerEvent;
59*cdf0e10cSrcweir     using ::com::sun::star::lang::EventObject;
60*cdf0e10cSrcweir 	/** === end UNO using === **/
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 	//==================================================================================================================
63*cdf0e10cSrcweir 	//= UndoManagerContextListener
64*cdf0e10cSrcweir 	//==================================================================================================================
65*cdf0e10cSrcweir     typedef ::cppu::WeakImplHelper1 <   XUndoManagerListener
66*cdf0e10cSrcweir                                     >   UndoManagerContextListener_Base;
67*cdf0e10cSrcweir     class UndoManagerContextListener : public UndoManagerContextListener_Base
68*cdf0e10cSrcweir     {
69*cdf0e10cSrcweir     public:
70*cdf0e10cSrcweir         UndoManagerContextListener( const Reference< XUndoManager >& i_undoManager )
71*cdf0e10cSrcweir             :m_xUndoManager( i_undoManager, UNO_QUERY_THROW )
72*cdf0e10cSrcweir             ,m_nRelativeContextDepth( 0 )
73*cdf0e10cSrcweir             ,m_documentDisposed( false )
74*cdf0e10cSrcweir         {
75*cdf0e10cSrcweir             osl_incrementInterlockedCount( &m_refCount );
76*cdf0e10cSrcweir             {
77*cdf0e10cSrcweir                 m_xUndoManager->addUndoManagerListener( this );
78*cdf0e10cSrcweir             }
79*cdf0e10cSrcweir             osl_decrementInterlockedCount( &m_refCount );
80*cdf0e10cSrcweir         }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir         UndoManagerContextListener()
83*cdf0e10cSrcweir         {
84*cdf0e10cSrcweir         }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir         void finish()
87*cdf0e10cSrcweir         {
88*cdf0e10cSrcweir             OSL_ENSURE( m_nRelativeContextDepth >= 0, "UndoManagerContextListener: more contexts left than entered?" );
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir             if ( m_documentDisposed )
91*cdf0e10cSrcweir                 return;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir             // work with a copy of m_nRelativeContextDepth, to be independent from possible bugs in the
94*cdf0e10cSrcweir             // listener notifications (where it would be decremented with every leaveUndoContext)
95*cdf0e10cSrcweir             sal_Int32 nDepth = m_nRelativeContextDepth;
96*cdf0e10cSrcweir             while ( nDepth-- > 0 )
97*cdf0e10cSrcweir             {
98*cdf0e10cSrcweir                 m_xUndoManager->leaveUndoContext();
99*cdf0e10cSrcweir             }
100*cdf0e10cSrcweir             m_xUndoManager->removeUndoManagerListener( this );
101*cdf0e10cSrcweir         }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         // XUndoManagerListener
104*cdf0e10cSrcweir         virtual void SAL_CALL undoActionAdded( const UndoManagerEvent& i_event ) throw (RuntimeException);
105*cdf0e10cSrcweir         virtual void SAL_CALL actionUndone( const UndoManagerEvent& i_event ) throw (RuntimeException);
106*cdf0e10cSrcweir         virtual void SAL_CALL actionRedone( const UndoManagerEvent& i_event ) throw (RuntimeException);
107*cdf0e10cSrcweir         virtual void SAL_CALL allActionsCleared( const EventObject& i_event ) throw (RuntimeException);
108*cdf0e10cSrcweir         virtual void SAL_CALL redoActionsCleared( const EventObject& i_event ) throw (RuntimeException);
109*cdf0e10cSrcweir         virtual void SAL_CALL resetAll( const EventObject& i_event ) throw (RuntimeException);
110*cdf0e10cSrcweir         virtual void SAL_CALL enteredContext( const UndoManagerEvent& i_event ) throw (RuntimeException);
111*cdf0e10cSrcweir         virtual void SAL_CALL enteredHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException);
112*cdf0e10cSrcweir         virtual void SAL_CALL leftContext( const UndoManagerEvent& i_event ) throw (RuntimeException);
113*cdf0e10cSrcweir         virtual void SAL_CALL leftHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException);
114*cdf0e10cSrcweir         virtual void SAL_CALL cancelledContext( const UndoManagerEvent& i_event ) throw (RuntimeException);
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir         // XEventListener
117*cdf0e10cSrcweir         virtual void SAL_CALL disposing( const EventObject& i_event ) throw (RuntimeException);
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     private:
120*cdf0e10cSrcweir         Reference< XUndoManager > const m_xUndoManager;
121*cdf0e10cSrcweir         oslInterlockedCount             m_nRelativeContextDepth;
122*cdf0e10cSrcweir         bool                            m_documentDisposed;
123*cdf0e10cSrcweir     };
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
126*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::undoActionAdded( const UndoManagerEvent& i_event ) throw (RuntimeException)
127*cdf0e10cSrcweir     {
128*cdf0e10cSrcweir         (void)i_event;
129*cdf0e10cSrcweir         // not interested in
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
133*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::actionUndone( const UndoManagerEvent& i_event ) throw (RuntimeException)
134*cdf0e10cSrcweir     {
135*cdf0e10cSrcweir         (void)i_event;
136*cdf0e10cSrcweir         // not interested in
137*cdf0e10cSrcweir     }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
140*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::actionRedone( const UndoManagerEvent& i_event ) throw (RuntimeException)
141*cdf0e10cSrcweir     {
142*cdf0e10cSrcweir         (void)i_event;
143*cdf0e10cSrcweir         // not interested in
144*cdf0e10cSrcweir     }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
147*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::allActionsCleared( const EventObject& i_event ) throw (RuntimeException)
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir         (void)i_event;
150*cdf0e10cSrcweir         // not interested in
151*cdf0e10cSrcweir     }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
154*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::redoActionsCleared( const EventObject& i_event ) throw (RuntimeException)
155*cdf0e10cSrcweir     {
156*cdf0e10cSrcweir         (void)i_event;
157*cdf0e10cSrcweir         // not interested in
158*cdf0e10cSrcweir     }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
161*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::resetAll( const EventObject& i_event ) throw (RuntimeException)
162*cdf0e10cSrcweir     {
163*cdf0e10cSrcweir         (void)i_event;
164*cdf0e10cSrcweir         m_nRelativeContextDepth = 0;
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
168*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::enteredContext( const UndoManagerEvent& i_event ) throw (RuntimeException)
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir         (void)i_event;
171*cdf0e10cSrcweir         osl_incrementInterlockedCount( &m_nRelativeContextDepth );
172*cdf0e10cSrcweir     }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
175*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::enteredHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException)
176*cdf0e10cSrcweir     {
177*cdf0e10cSrcweir         (void)i_event;
178*cdf0e10cSrcweir         osl_incrementInterlockedCount( &m_nRelativeContextDepth );
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
182*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::leftContext( const UndoManagerEvent& i_event ) throw (RuntimeException)
183*cdf0e10cSrcweir     {
184*cdf0e10cSrcweir         (void)i_event;
185*cdf0e10cSrcweir         osl_decrementInterlockedCount( &m_nRelativeContextDepth );
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
189*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::leftHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException)
190*cdf0e10cSrcweir     {
191*cdf0e10cSrcweir         (void)i_event;
192*cdf0e10cSrcweir         osl_decrementInterlockedCount( &m_nRelativeContextDepth );
193*cdf0e10cSrcweir     }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
196*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::cancelledContext( const UndoManagerEvent& i_event ) throw (RuntimeException)
197*cdf0e10cSrcweir     {
198*cdf0e10cSrcweir         (void)i_event;
199*cdf0e10cSrcweir         osl_decrementInterlockedCount( &m_nRelativeContextDepth );
200*cdf0e10cSrcweir     }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     //------------------------------------------------------------------------------------------------------------------
203*cdf0e10cSrcweir     void SAL_CALL UndoManagerContextListener::disposing( const EventObject& i_event ) throw (RuntimeException)
204*cdf0e10cSrcweir     {
205*cdf0e10cSrcweir         (void)i_event;
206*cdf0e10cSrcweir         m_documentDisposed = true;
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 	//==================================================================================================================
210*cdf0e10cSrcweir 	//= DocumentUndoGuard_Data
211*cdf0e10cSrcweir 	//==================================================================================================================
212*cdf0e10cSrcweir     struct DocumentUndoGuard_Data
213*cdf0e10cSrcweir     {
214*cdf0e10cSrcweir         Reference< XUndoManager >                       xUndoManager;
215*cdf0e10cSrcweir         ::rtl::Reference< UndoManagerContextListener >  pContextListener;
216*cdf0e10cSrcweir     };
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     namespace
219*cdf0e10cSrcweir     {
220*cdf0e10cSrcweir 	    //--------------------------------------------------------------------------------------------------------------
221*cdf0e10cSrcweir         void lcl_init( DocumentUndoGuard_Data& i_data, const Reference< XInterface >& i_undoSupplierComponent )
222*cdf0e10cSrcweir         {
223*cdf0e10cSrcweir             try
224*cdf0e10cSrcweir             {
225*cdf0e10cSrcweir                 Reference< XUndoManagerSupplier > xUndoSupplier( i_undoSupplierComponent, UNO_QUERY );
226*cdf0e10cSrcweir                 if ( xUndoSupplier.is() )
227*cdf0e10cSrcweir                     i_data.xUndoManager.set( xUndoSupplier->getUndoManager(), UNO_QUERY_THROW );
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir                 if ( i_data.xUndoManager.is() )
230*cdf0e10cSrcweir                     i_data.pContextListener.set( new UndoManagerContextListener( i_data.xUndoManager ) );
231*cdf0e10cSrcweir             }
232*cdf0e10cSrcweir             catch( const Exception& )
233*cdf0e10cSrcweir             {
234*cdf0e10cSrcweir         	    DBG_UNHANDLED_EXCEPTION();
235*cdf0e10cSrcweir             }
236*cdf0e10cSrcweir         }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir         //--------------------------------------------------------------------------------------------------------------
239*cdf0e10cSrcweir         void lcl_restore( DocumentUndoGuard_Data& i_data )
240*cdf0e10cSrcweir         {
241*cdf0e10cSrcweir             try
242*cdf0e10cSrcweir             {
243*cdf0e10cSrcweir                 if ( i_data.pContextListener.is() )
244*cdf0e10cSrcweir                     i_data.pContextListener->finish();
245*cdf0e10cSrcweir                 i_data.pContextListener.clear();
246*cdf0e10cSrcweir             }
247*cdf0e10cSrcweir             catch( const Exception& )
248*cdf0e10cSrcweir             {
249*cdf0e10cSrcweir             	DBG_UNHANDLED_EXCEPTION();
250*cdf0e10cSrcweir             }
251*cdf0e10cSrcweir         }
252*cdf0e10cSrcweir     }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 	//==================================================================================================================
255*cdf0e10cSrcweir 	//= DocumentUndoGuard
256*cdf0e10cSrcweir 	//==================================================================================================================
257*cdf0e10cSrcweir 	//------------------------------------------------------------------------------------------------------------------
258*cdf0e10cSrcweir     DocumentUndoGuard::DocumentUndoGuard( const Reference< XInterface >& i_undoSupplierComponent )
259*cdf0e10cSrcweir         :m_pData( new DocumentUndoGuard_Data )
260*cdf0e10cSrcweir     {
261*cdf0e10cSrcweir         lcl_init( *m_pData, i_undoSupplierComponent );
262*cdf0e10cSrcweir     }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir     DocumentUndoGuard::~DocumentUndoGuard()
265*cdf0e10cSrcweir     {
266*cdf0e10cSrcweir         lcl_restore( *m_pData );
267*cdf0e10cSrcweir     }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir //......................................................................................................................
270*cdf0e10cSrcweir } // namespace framework
271*cdf0e10cSrcweir //......................................................................................................................
272