merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / plugin / base / multiplx.cxx
blob1703a56a3962c530b00733c893161633c2c32bbc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: multiplx.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include <vos/diagnose.hxx>
34 #include <plugin/multiplx.hxx>
36 // ---------------------------------------------------------------------
37 // class MRCListenerMultiplexerHelper
38 // ---------------------------------------------------------------------
39 MRCListenerMultiplexerHelper::MRCListenerMultiplexerHelper
41 const Reference< ::com::sun::star::awt::XWindow > & rControl
42 , const Reference< ::com::sun::star::awt::XWindow > & rPeer
44 : xPeer( rPeer )
45 , xControl( Reference< ::com::sun::star::awt::XControl >( rControl, UNO_QUERY ) )
46 , aListenerHolder( aMutex )
51 void MRCListenerMultiplexerHelper::setPeer( const Reference< ::com::sun::star::awt::XWindow > & rPeer )
53 ::osl::Guard< ::osl::Mutex > aGuard( aMutex );
54 if( xPeer != rPeer )
56 if( xPeer.is() )
58 // get all uiks from the listener added to the peer
59 Sequence<Type> aContainedTypes = aListenerHolder.getContainedTypes();
60 const Type* pArray = aContainedTypes.getConstArray();
61 sal_Int32 nCount = aContainedTypes.getLength();
62 // loop over all listener types and remove the listeners from the peer
63 for( sal_Int32 i = 0; i < nCount; i++ )
64 unadviseFromPeer( xPeer, pArray[i] );
66 xPeer = rPeer;
67 if( xPeer.is() )
69 // get all uiks from the listener added to the peer
70 Sequence<Type> aContainedTypes = aListenerHolder.getContainedTypes();
71 const Type * pArray = aContainedTypes.getConstArray();
72 sal_Int32 nCount = aContainedTypes.getLength();
73 // loop over all listener types and add the listeners to the peer
74 for( sal_Int32 i = 0; i < nCount; i++ )
75 adviseToPeer( xPeer, pArray[i] );
80 // MRCListenerMultiplexerHelper
81 void MRCListenerMultiplexerHelper::disposeAndClear()
83 ::com::sun::star::lang::EventObject aEvt;
84 aEvt.Source = xControl;
85 aListenerHolder.disposeAndClear( aEvt );
88 // MRCListenerMultiplexerHelper
89 void MRCListenerMultiplexerHelper::adviseToPeer( const Reference< ::com::sun::star::awt::XWindow > & rPeer, const Type & type )
91 // add a listener to the source (peer)
92 if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XWindowListener >*)0) )
93 rPeer->addWindowListener( this );
94 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XKeyListener >*)0) )
95 rPeer->addKeyListener( this );
96 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XFocusListener >*)0) )
97 rPeer->addFocusListener( this );
98 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XMouseListener >*)0) )
99 rPeer->addMouseListener( this );
100 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XMouseMotionListener >*)0) )
101 rPeer->addMouseMotionListener( this );
102 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XPaintListener >*)0) )
103 rPeer->addPaintListener( this );
104 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XTopWindowListener >*)0) )
106 Reference< ::com::sun::star::awt::XTopWindow > xTop( rPeer, UNO_QUERY );
107 if( xTop.is() )
108 xTop->addTopWindowListener( this );
110 else
112 VOS_ENSHURE( sal_False, "unknown listener" );
116 // MRCListenerMultiplexerHelper
117 void MRCListenerMultiplexerHelper::unadviseFromPeer( const Reference< ::com::sun::star::awt::XWindow > & rPeer, const Type & type )
119 // the last listener is removed, remove the listener from the source (peer)
120 if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XWindowListener >*)0) )
121 rPeer->removeWindowListener( this );
122 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XKeyListener >*)0) )
123 rPeer->removeKeyListener( this );
124 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XFocusListener >*)0) )
125 rPeer->removeFocusListener( this );
126 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XMouseListener >*)0) )
127 rPeer->removeMouseListener( this );
128 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XMouseMotionListener >*)0) )
129 rPeer->removeMouseMotionListener( this );
130 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XPaintListener >*)0) )
131 rPeer->removePaintListener( this );
132 else if( type == ::getCppuType((const Reference< ::com::sun::star::awt::XTopWindowListener >*)0) )
134 Reference< ::com::sun::star::awt::XTopWindow > xTop( rPeer, UNO_QUERY );
135 if( xTop.is() )
136 xTop->removeTopWindowListener( this );
138 else
140 VOS_ENSHURE( sal_False, "unknown listener" );
144 // MRCListenerMultiplexerHelper
145 void MRCListenerMultiplexerHelper::advise( const Type & type, const Reference< XInterface > & listener)
147 ::osl::Guard< ::osl::Mutex > aGuard( aMutex );
148 if( 1 == aListenerHolder.addInterface( type, listener ) )
150 // the first listener is added
151 if( xPeer.is() )
152 adviseToPeer( xPeer, type );
156 // MRCListenerMultiplexerHelper
157 void MRCListenerMultiplexerHelper::unadvise(const Type & type, const Reference< XInterface > & listener)
159 ::osl::Guard< ::osl::Mutex > aGuard( aMutex );
160 ::cppu::OInterfaceContainerHelper * pCont = aListenerHolder.getContainer( type );
161 if( pCont )
163 if( 0 == pCont->removeInterface( listener ) && xPeer.is() )
164 // the last listener is removed
165 unadviseFromPeer( xPeer, type );
169 // ::com::sun::star::lang::XEventListener
170 void MRCListenerMultiplexerHelper::disposing(const ::com::sun::star::lang::EventObject& ) throw()
172 ::osl::Guard< ::osl::Mutex > aGuard( aMutex );
173 // peer is disposed, clear the reference
174 xPeer = Reference< ::com::sun::star::awt::XWindow > ();
177 #define MULTIPLEX( InterfaceName, MethodName, EventName ) \
178 ::cppu::OInterfaceContainerHelper * pCont; \
179 pCont = aListenerHolder.getContainer( ::getCppuType((const Reference< InterfaceName >*)0) ); \
180 if( pCont ) \
182 ::cppu::OInterfaceIteratorHelper aIt( *pCont ); \
183 EventName aEvt = e; \
184 /* Remark: The control is the event source not the peer. We must change */ \
185 /* the source of the event */ \
186 aEvt.Source = xControl;\
187 /*.is the control not destroyed */ \
188 if( aEvt.Source.is() ) \
190 if( aIt.hasMoreElements() ) \
192 InterfaceName * pListener = (InterfaceName *)aIt.next(); \
193 try \
195 pListener->MethodName( aEvt ); \
197 catch( RuntimeException& ) \
199 /* ignore all usr system exceptions from the listener */ \
205 // ::com::sun::star::awt::XFocusListener
206 void MRCListenerMultiplexerHelper::focusGained(const ::com::sun::star::awt::FocusEvent& e) throw()
208 MULTIPLEX( ::com::sun::star::awt::XFocusListener, focusGained, ::com::sun::star::awt::FocusEvent )
211 // ::com::sun::star::awt::XFocusListener
212 void MRCListenerMultiplexerHelper::focusLost(const ::com::sun::star::awt::FocusEvent& e) throw()
214 MULTIPLEX( ::com::sun::star::awt::XFocusListener, focusLost, ::com::sun::star::awt::FocusEvent )
217 // ::com::sun::star::awt::XWindowListener
218 void MRCListenerMultiplexerHelper::windowResized(const ::com::sun::star::awt::WindowEvent& e) throw()
220 MULTIPLEX( ::com::sun::star::awt::XWindowListener, windowResized, ::com::sun::star::awt::WindowEvent )
223 // ::com::sun::star::awt::XWindowListener
224 void MRCListenerMultiplexerHelper::windowMoved(const ::com::sun::star::awt::WindowEvent& e) throw()
226 MULTIPLEX( ::com::sun::star::awt::XWindowListener, windowMoved, ::com::sun::star::awt::WindowEvent )
229 // ::com::sun::star::awt::XWindowListener
230 void MRCListenerMultiplexerHelper::windowShown(const ::com::sun::star::lang::EventObject& e) throw()
232 MULTIPLEX( ::com::sun::star::awt::XWindowListener, windowShown, ::com::sun::star::lang::EventObject )
235 // ::com::sun::star::awt::XWindowListener
236 void MRCListenerMultiplexerHelper::windowHidden(const ::com::sun::star::lang::EventObject& e) throw()
238 MULTIPLEX( ::com::sun::star::awt::XWindowListener, windowHidden, ::com::sun::star::lang::EventObject )
241 // ::com::sun::star::awt::XKeyListener
242 void MRCListenerMultiplexerHelper::keyPressed(const ::com::sun::star::awt::KeyEvent& e) throw()
244 MULTIPLEX( ::com::sun::star::awt::XKeyListener, keyPressed, ::com::sun::star::awt::KeyEvent )
247 // ::com::sun::star::awt::XKeyListener
248 void MRCListenerMultiplexerHelper::keyReleased(const ::com::sun::star::awt::KeyEvent& e) throw()
250 MULTIPLEX( ::com::sun::star::awt::XKeyListener, keyReleased, ::com::sun::star::awt::KeyEvent )
253 // ::com::sun::star::awt::XMouseListener
254 void MRCListenerMultiplexerHelper::mousePressed(const ::com::sun::star::awt::MouseEvent& e) throw()
256 MULTIPLEX( ::com::sun::star::awt::XMouseListener, mousePressed, ::com::sun::star::awt::MouseEvent )
259 // ::com::sun::star::awt::XMouseListener
260 void MRCListenerMultiplexerHelper::mouseReleased(const ::com::sun::star::awt::MouseEvent& e) throw()
262 MULTIPLEX( ::com::sun::star::awt::XMouseListener, mouseReleased, ::com::sun::star::awt::MouseEvent )
265 // ::com::sun::star::awt::XMouseListener
266 void MRCListenerMultiplexerHelper::mouseEntered(const ::com::sun::star::awt::MouseEvent& e) throw()
268 MULTIPLEX( ::com::sun::star::awt::XMouseListener, mouseEntered, ::com::sun::star::awt::MouseEvent )
271 // ::com::sun::star::awt::XMouseListener
272 void MRCListenerMultiplexerHelper::mouseExited(const ::com::sun::star::awt::MouseEvent& e) throw()
274 MULTIPLEX( ::com::sun::star::awt::XMouseListener, mouseExited, ::com::sun::star::awt::MouseEvent )
277 // ::com::sun::star::awt::XMouseMotionListener
278 void MRCListenerMultiplexerHelper::mouseDragged(const ::com::sun::star::awt::MouseEvent& e) throw()
280 MULTIPLEX( ::com::sun::star::awt::XMouseMotionListener, mouseDragged, ::com::sun::star::awt::MouseEvent )
283 // ::com::sun::star::awt::XMouseMotionListener
284 void MRCListenerMultiplexerHelper::mouseMoved(const ::com::sun::star::awt::MouseEvent& e) throw()
286 MULTIPLEX( ::com::sun::star::awt::XMouseMotionListener, mouseMoved, ::com::sun::star::awt::MouseEvent )
289 // ::com::sun::star::awt::XPaintListener
290 void MRCListenerMultiplexerHelper::windowPaint(const ::com::sun::star::awt::PaintEvent& e) throw()
292 MULTIPLEX( ::com::sun::star::awt::XPaintListener, windowPaint, ::com::sun::star::awt::PaintEvent )
295 // ::com::sun::star::awt::XTopWindowListener
296 void MRCListenerMultiplexerHelper::windowOpened(const ::com::sun::star::lang::EventObject& e) throw()
298 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener, windowOpened, ::com::sun::star::lang::EventObject )
301 // ::com::sun::star::awt::XTopWindowListener
302 void MRCListenerMultiplexerHelper::windowClosing( const ::com::sun::star::lang::EventObject& e ) throw()
304 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener, windowClosing, ::com::sun::star::lang::EventObject )
307 // ::com::sun::star::awt::XTopWindowListener
308 void MRCListenerMultiplexerHelper::windowClosed( const ::com::sun::star::lang::EventObject& e ) throw()
310 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener, windowClosed, ::com::sun::star::lang::EventObject )
313 // ::com::sun::star::awt::XTopWindowListener
314 void MRCListenerMultiplexerHelper::windowMinimized( const ::com::sun::star::lang::EventObject& e ) throw()
316 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener, windowMinimized, ::com::sun::star::lang::EventObject )
319 // ::com::sun::star::awt::XTopWindowListener
320 void MRCListenerMultiplexerHelper::windowNormalized( const ::com::sun::star::lang::EventObject& e ) throw()
322 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener, windowNormalized, ::com::sun::star::lang::EventObject )
325 // ::com::sun::star::awt::XTopWindowListener
326 void MRCListenerMultiplexerHelper::windowActivated( const ::com::sun::star::lang::EventObject& e ) throw()
328 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener, windowActivated, ::com::sun::star::lang::EventObject )
331 // ::com::sun::star::awt::XTopWindowListener
332 void MRCListenerMultiplexerHelper::windowDeactivated( const ::com::sun::star::lang::EventObject& e ) throw()
334 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener, windowDeactivated, ::com::sun::star::lang::EventObject )