merge the formfield patch from ooo-build
[ooovba.git] / UnoControls / source / base / multiplexer.cxx
blobd3e9efc9a01c9518200233e67a489b47bfdec0b1
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: multiplexer.cxx,v $
10 * $Revision: 1.8 $
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 ************************************************************************/
30 //____________________________________________________________________________________________________________
31 // my own include
32 //____________________________________________________________________________________________________________
34 #include "multiplexer.hxx"
36 //____________________________________________________________________________________________________________
37 // includes of other projects
38 //____________________________________________________________________________________________________________
39 #include <vos/diagnose.hxx>
41 //____________________________________________________________________________________________________________
42 // includes of my own project
43 //____________________________________________________________________________________________________________
45 //____________________________________________________________________________________________________________
46 // namespaces
47 //____________________________________________________________________________________________________________
49 using namespace ::cppu ;
50 using namespace ::osl ;
51 using namespace ::com::sun::star::uno ;
52 using namespace ::com::sun::star::awt ;
53 using namespace ::com::sun::star::lang ;
55 namespace unocontrols{
57 //____________________________________________________________________________________________________________
58 // macros
59 //____________________________________________________________________________________________________________
61 #define MULTIPLEX( INTERFACE, METHOD, EVENTTYP, EVENT ) \
63 /* First get all interfaces from container with right type.*/ \
64 OInterfaceContainerHelper* pContainer = m_aListenerHolder.getContainer( ::getCppuType((const Reference< INTERFACE >*)0) ); \
65 /* Do the follow only, if elements in container exist.*/ \
66 if( pContainer != NULL ) \
67 { \
68 OInterfaceIteratorHelper aIterator( *pContainer ); \
69 EVENTTYP aLocalEvent = EVENT; \
70 /* Remark: The control is the event source not the peer.*/ \
71 /* We must change the source of the event. */ \
72 aLocalEvent.Source = m_xControl ; \
73 /* Is the control not destroyed? */ \
74 if( aLocalEvent.Source.is() == sal_True ) \
75 { \
76 if( aIterator.hasMoreElements() ) \
77 { \
78 INTERFACE * pListener = (INTERFACE *)aIterator.next(); \
79 try \
80 { \
81 pListener->METHOD( aLocalEvent ); \
82 } \
83 catch( RuntimeException& ) \
84 { \
85 /* Ignore all system exceptions from the listener! */ \
86 } \
87 } \
88 } \
91 //____________________________________________________________________________________________________________
92 // construct/destruct
93 //____________________________________________________________________________________________________________
95 OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const Reference< XWindow >& xControl ,
96 const Reference< XWindow >& xPeer )
97 : m_xPeer ( xPeer )
98 , m_xControl ( xControl )
99 , m_aListenerHolder ( m_aMutex )
103 OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const OMRCListenerMultiplexerHelper& /*aCopyInstance*/ )
104 : XFocusListener()
105 , XWindowListener()
106 , XKeyListener()
107 , XMouseListener()
108 , XMouseMotionListener()
109 , XPaintListener()
110 , XTopWindowListener()
111 , OWeakObject()
112 , m_aListenerHolder ( m_aMutex )
116 OMRCListenerMultiplexerHelper::~OMRCListenerMultiplexerHelper()
120 //____________________________________________________________________________________________________________
121 // XInterface
122 //____________________________________________________________________________________________________________
124 Any SAL_CALL OMRCListenerMultiplexerHelper::queryInterface( const Type& rType ) throw( RuntimeException )
126 // Attention:
127 // Don't use mutex or guard in this method!!! Is a method of XInterface.
129 // Ask for my own supported interfaces ...
130 // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
131 Any aReturn ( ::cppu::queryInterface( rType ,
132 static_cast< XWindowListener* > ( this ) ,
133 static_cast< XKeyListener* > ( this ) ,
134 static_cast< XFocusListener* > ( this ) ,
135 static_cast< XMouseListener* > ( this ) ,
136 static_cast< XMouseMotionListener* > ( this ) ,
137 static_cast< XPaintListener* > ( this ) ,
138 static_cast< XTopWindowListener* > ( this ) ,
139 static_cast< XTopWindowListener* > ( this )
143 // If searched interface supported by this class ...
144 if ( aReturn.hasValue() == sal_True )
146 // ... return this information.
147 return aReturn ;
149 else
151 // Else; ... ask baseclass for interfaces!
152 return OWeakObject::queryInterface( rType );
156 //____________________________________________________________________________________________________________
157 // XInterface
158 //____________________________________________________________________________________________________________
160 void SAL_CALL OMRCListenerMultiplexerHelper::acquire() throw()
162 // Attention:
163 // Don't use mutex or guard in this method!!! Is a method of XInterface.
165 // Forward to baseclass
166 OWeakObject::acquire();
169 //____________________________________________________________________________________________________________
170 // XInterface
171 //____________________________________________________________________________________________________________
173 void SAL_CALL OMRCListenerMultiplexerHelper::release() throw()
175 // Attention:
176 // Don't use mutex or guard in this method!!! Is a method of XInterface.
178 // Forward to baseclass
179 OWeakObject::release();
182 //____________________________________________________________________________________________________________
183 // operator
184 //____________________________________________________________________________________________________________
186 OMRCListenerMultiplexerHelper::operator Reference< XInterface >() const
188 return ((OWeakObject*)this) ;
191 //____________________________________________________________________________________________________________
192 // operator
193 //____________________________________________________________________________________________________________
195 //OMRCListenerMultiplexerHelper& OMRCListenerMultiplexerHelper::operator= ( const OMRCListenerMultiplexerHelper& aCopyInstance )
197 // return this ;
200 //____________________________________________________________________________________________________________
201 // container method
202 //____________________________________________________________________________________________________________
204 void OMRCListenerMultiplexerHelper::setPeer( const Reference< XWindow >& xPeer )
206 MutexGuard aGuard( m_aMutex );
207 if( m_xPeer != xPeer )
209 if( m_xPeer.is() )
211 // get all types from the listener added to the peer
212 Sequence< Type > aContainedTypes = m_aListenerHolder.getContainedTypes();
213 const Type* pArray = aContainedTypes.getConstArray();
214 sal_Int32 nCount = aContainedTypes.getLength();
215 // loop over all listener types and remove the listeners from the peer
216 for( sal_Int32 i=0; i<nCount; i++ )
217 impl_unadviseFromPeer( m_xPeer, pArray[i] );
219 m_xPeer = xPeer;
220 if( m_xPeer.is() )
222 // get all types from the listener added to the peer
223 Sequence< Type > aContainedTypes = m_aListenerHolder.getContainedTypes();
224 const Type* pArray = aContainedTypes.getConstArray();
225 sal_Int32 nCount = aContainedTypes.getLength();
226 // loop over all listener types and add the listeners to the peer
227 for( sal_Int32 i = 0; i < nCount; i++ )
228 impl_adviseToPeer( m_xPeer, pArray[i] );
233 //____________________________________________________________________________________________________________
234 // container method
235 //____________________________________________________________________________________________________________
237 void OMRCListenerMultiplexerHelper::disposeAndClear()
239 EventObject aEvent ;
240 aEvent.Source = m_xControl ;
241 m_aListenerHolder.disposeAndClear( aEvent );
244 //____________________________________________________________________________________________________________
245 // container method
246 //____________________________________________________________________________________________________________
248 void OMRCListenerMultiplexerHelper::advise( const Type& aType ,
249 const Reference< XInterface >& xListener )
251 MutexGuard aGuard( m_aMutex );
252 if( m_aListenerHolder.addInterface( aType, xListener ) == 1 )
254 // the first listener is added
255 if( m_xPeer.is() )
257 impl_adviseToPeer( m_xPeer, aType );
262 //____________________________________________________________________________________________________________
263 // container method
264 //____________________________________________________________________________________________________________
266 void OMRCListenerMultiplexerHelper::unadvise( const Type& aType ,
267 const Reference< XInterface >& xListener )
269 MutexGuard aGuard( m_aMutex );
270 if( m_aListenerHolder.removeInterface( aType, xListener ) == 0 )
272 // the last listener is removed
273 if ( m_xPeer.is() )
275 impl_unadviseFromPeer( m_xPeer, aType );
280 //____________________________________________________________________________________________________________
281 // XEventListener
282 //____________________________________________________________________________________________________________
284 void SAL_CALL OMRCListenerMultiplexerHelper::disposing( const EventObject& /*aSource*/ ) throw( RuntimeException )
286 MutexGuard aGuard( m_aMutex );
287 // peer is disposed, clear the reference
288 m_xPeer = Reference< XWindow >();
291 //____________________________________________________________________________________________________________
292 // XFcousListener
293 //____________________________________________________________________________________________________________
295 void OMRCListenerMultiplexerHelper::focusGained(const FocusEvent& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
298 OInterfaceContainerHelper * pCont = aListenerHolder.getContainer( ::getCppuType((const Reference< XFocusListener >*)0) );
299 if( pCont )
301 OInterfaceIteratorHelper aIt( *pCont );
302 FocusEvent aEvt = e;
303 // Reamark: The control is the event source not the peer. We must change
304 // the source of the event
305 xControl.queryHardRef( ((XInterface*)NULL)->getSmartUik(), aEvt.Source );
306 //.is the control not destroyed
307 if( aEvt.Source.is() )
309 if( aIt.hasMoreElements() )
311 XFocusListener * pListener = (XFocusListener *)aIt.next();
314 pListener->focusGained( aEvt );
316 catch( RuntimeException, e )
318 // ignore all usr system exceptions from the listener
324 MULTIPLEX( XFocusListener, focusGained, FocusEvent, aEvent )
327 //____________________________________________________________________________________________________________
328 // XFcousListener
329 //____________________________________________________________________________________________________________
331 void OMRCListenerMultiplexerHelper::focusLost(const FocusEvent& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
333 MULTIPLEX( XFocusListener, focusLost, FocusEvent, aEvent )
336 //____________________________________________________________________________________________________________
337 // XWindowListener
338 //____________________________________________________________________________________________________________
340 void OMRCListenerMultiplexerHelper::windowResized(const WindowEvent& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
342 MULTIPLEX( XWindowListener, windowResized, WindowEvent, aEvent )
345 //____________________________________________________________________________________________________________
346 // XWindowListener
347 //____________________________________________________________________________________________________________
349 void OMRCListenerMultiplexerHelper::windowMoved(const WindowEvent& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
351 MULTIPLEX( XWindowListener, windowMoved, WindowEvent, aEvent )
354 //____________________________________________________________________________________________________________
355 // XWindowListener
356 //____________________________________________________________________________________________________________
358 void OMRCListenerMultiplexerHelper::windowShown(const EventObject& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
360 MULTIPLEX( XWindowListener, windowShown, EventObject, aEvent )
363 //____________________________________________________________________________________________________________
364 // XWindowListener
365 //____________________________________________________________________________________________________________
367 void OMRCListenerMultiplexerHelper::windowHidden(const EventObject& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
369 MULTIPLEX( XWindowListener, windowHidden, EventObject, aEvent )
372 //____________________________________________________________________________________________________________
373 // XKeyListener
374 //____________________________________________________________________________________________________________
376 void OMRCListenerMultiplexerHelper::keyPressed(const KeyEvent& aEvent) throw( UNO3_RUNTIMEEXCEPTION )
378 MULTIPLEX( XKeyListener, keyPressed, KeyEvent, aEvent )
381 //____________________________________________________________________________________________________________
382 // XKeyListener
383 //____________________________________________________________________________________________________________
385 void OMRCListenerMultiplexerHelper::keyReleased(const KeyEvent& aEvent) throw( UNO3_RUNTIMEEXCEPTION )
387 MULTIPLEX( XKeyListener, keyReleased, KeyEvent, aEvent )
390 //____________________________________________________________________________________________________________
391 // XMouseListener
392 //____________________________________________________________________________________________________________
394 void OMRCListenerMultiplexerHelper::mousePressed(const MouseEvent& aEvent) throw( UNO3_RUNTIMEEXCEPTION )
396 MULTIPLEX( XMouseListener, mousePressed, MouseEvent, aEvent )
399 //____________________________________________________________________________________________________________
400 // XMouseListener
401 //____________________________________________________________________________________________________________
403 void OMRCListenerMultiplexerHelper::mouseReleased(const MouseEvent& aEvent) throw( UNO3_RUNTIMEEXCEPTION )
405 MULTIPLEX( XMouseListener, mouseReleased, MouseEvent, aEvent )
408 //____________________________________________________________________________________________________________
409 // XMouseListener
410 //____________________________________________________________________________________________________________
412 void OMRCListenerMultiplexerHelper::mouseEntered(const MouseEvent& aEvent) throw( UNO3_RUNTIMEEXCEPTION )
414 MULTIPLEX( XMouseListener, mouseEntered, MouseEvent, aEvent )
417 //____________________________________________________________________________________________________________
418 // XMouseListener
419 //____________________________________________________________________________________________________________
421 void OMRCListenerMultiplexerHelper::mouseExited(const MouseEvent& aEvent) throw( UNO3_RUNTIMEEXCEPTION )
423 MULTIPLEX( XMouseListener, mouseExited, MouseEvent, aEvent )
426 //____________________________________________________________________________________________________________
427 // XMouseMotionListener
428 //____________________________________________________________________________________________________________
430 void OMRCListenerMultiplexerHelper::mouseDragged(const MouseEvent& aEvent) throw( UNO3_RUNTIMEEXCEPTION )
432 MULTIPLEX( XMouseMotionListener, mouseDragged, MouseEvent, aEvent )
435 //____________________________________________________________________________________________________________
436 // XMouseMotionListener
437 //____________________________________________________________________________________________________________
439 void OMRCListenerMultiplexerHelper::mouseMoved(const MouseEvent& aEvent) throw( UNO3_RUNTIMEEXCEPTION )
441 MULTIPLEX( XMouseMotionListener, mouseMoved, MouseEvent, aEvent )
444 //____________________________________________________________________________________________________________
445 // XPaintListener
446 //____________________________________________________________________________________________________________
448 void OMRCListenerMultiplexerHelper::windowPaint(const PaintEvent& aEvent) throw( UNO3_RUNTIMEEXCEPTION )
450 MULTIPLEX( XPaintListener, windowPaint, PaintEvent, aEvent )
453 //____________________________________________________________________________________________________________
454 // XTopWindowListener
455 //____________________________________________________________________________________________________________
457 void OMRCListenerMultiplexerHelper::windowOpened(const EventObject& aEvent) throw( UNO3_RUNTIMEEXCEPTION )
459 MULTIPLEX( XTopWindowListener, windowOpened, EventObject, aEvent )
462 //____________________________________________________________________________________________________________
463 // XTopWindowListener
464 //____________________________________________________________________________________________________________
466 void OMRCListenerMultiplexerHelper::windowClosing( const EventObject& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
468 MULTIPLEX( XTopWindowListener, windowClosing, EventObject, aEvent )
471 //____________________________________________________________________________________________________________
472 // XTopWindowListener
473 //____________________________________________________________________________________________________________
475 void OMRCListenerMultiplexerHelper::windowClosed( const EventObject& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
477 MULTIPLEX( XTopWindowListener, windowClosed, EventObject, aEvent )
480 //____________________________________________________________________________________________________________
481 // XTopWindowListener
482 //____________________________________________________________________________________________________________
484 void OMRCListenerMultiplexerHelper::windowMinimized( const EventObject& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
486 MULTIPLEX( XTopWindowListener, windowMinimized, EventObject, aEvent )
489 //____________________________________________________________________________________________________________
490 // XTopWindowListener
491 //____________________________________________________________________________________________________________
493 void OMRCListenerMultiplexerHelper::windowNormalized( const EventObject& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
495 MULTIPLEX( XTopWindowListener, windowNormalized, EventObject, aEvent )
498 //____________________________________________________________________________________________________________
499 // XTopWindowListener
500 //____________________________________________________________________________________________________________
502 void OMRCListenerMultiplexerHelper::windowActivated( const EventObject& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
504 MULTIPLEX( XTopWindowListener, windowActivated, EventObject, aEvent )
507 //____________________________________________________________________________________________________________
508 // XTopWindowListener
509 //____________________________________________________________________________________________________________
511 void OMRCListenerMultiplexerHelper::windowDeactivated( const EventObject& aEvent ) throw( UNO3_RUNTIMEEXCEPTION )
513 MULTIPLEX( XTopWindowListener, windowDeactivated, EventObject, aEvent )
516 //____________________________________________________________________________________________________________
517 // protected method
518 //____________________________________________________________________________________________________________
520 void OMRCListenerMultiplexerHelper::impl_adviseToPeer( const Reference< XWindow >& xPeer ,
521 const Type& aType )
523 // add a listener to the source (peer)
524 if( aType == ::getCppuType((const Reference< XWindowListener >*)0) )
525 xPeer->addWindowListener( this );
526 else if( aType == ::getCppuType((const Reference< XKeyListener >*)0) )
527 xPeer->addKeyListener( this );
528 else if( aType == ::getCppuType((const Reference< XFocusListener >*)0) )
529 xPeer->addFocusListener( this );
530 else if( aType == ::getCppuType((const Reference< XMouseListener >*)0) )
531 xPeer->addMouseListener( this );
532 else if( aType == ::getCppuType((const Reference< XMouseMotionListener >*)0) )
533 xPeer->addMouseMotionListener( this );
534 else if( aType == ::getCppuType((const Reference< XPaintListener >*)0) )
535 xPeer->addPaintListener( this );
536 else if( aType == ::getCppuType((const Reference< XTopWindowListener >*)0) )
538 Reference< XTopWindow > xTop( xPeer, UNO_QUERY );
539 if( xTop.is() )
540 xTop->addTopWindowListener( this );
542 else
544 VOS_ENSHURE( sal_False, "unknown listener" );
548 //____________________________________________________________________________________________________________
549 // protected method
550 //____________________________________________________________________________________________________________
552 void OMRCListenerMultiplexerHelper::impl_unadviseFromPeer( const Reference< XWindow >& xPeer ,
553 const Type& aType )
555 // the last listener is removed, remove the listener from the source (peer)
556 if( aType == ::getCppuType((const Reference< XWindowListener >*)0) )
557 xPeer->removeWindowListener( this );
558 else if( aType == ::getCppuType((const Reference< XKeyListener >*)0) )
559 xPeer->removeKeyListener( this );
560 else if( aType == ::getCppuType((const Reference< XFocusListener >*)0) )
561 xPeer->removeFocusListener( this );
562 else if( aType == ::getCppuType((const Reference< XMouseListener >*)0) )
563 xPeer->removeMouseListener( this );
564 else if( aType == ::getCppuType((const Reference< XMouseMotionListener >*)0) )
565 xPeer->removeMouseMotionListener( this );
566 else if( aType == ::getCppuType((const Reference< XPaintListener >*)0) )
567 xPeer->removePaintListener( this );
568 else if( aType == ::getCppuType((const Reference< XTopWindowListener >*)0) )
570 Reference< XTopWindow > xTop( xPeer, UNO_QUERY );
571 if( xTop.is() )
572 xTop->removeTopWindowListener( this );
574 else
576 VOS_ENSHURE( sal_False, "unknown listener" );
580 } // namespace unocontrols