bump product version to 4.1.6.2
[LibreOffice.git] / UnoControls / source / base / multiplexer.cxx
blob346836094e004e1d153427544c9d937f9fa46c9a
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 "multiplexer.hxx"
22 #include <osl/diagnose.h>
24 using namespace ::cppu ;
25 using namespace ::osl ;
26 using namespace ::com::sun::star::uno ;
27 using namespace ::com::sun::star::awt ;
28 using namespace ::com::sun::star::lang ;
30 namespace unocontrols{
32 //____________________________________________________________________________________________________________
33 // macros
34 //____________________________________________________________________________________________________________
36 #define MULTIPLEX( INTERFACE, METHOD, EVENTTYP, EVENT ) \
38 /* First get all interfaces from container with right type.*/ \
39 OInterfaceContainerHelper* pContainer = m_aListenerHolder.getContainer( ::getCppuType((const Reference< INTERFACE >*)0) ); \
40 /* Do the follow only, if elements in container exist.*/ \
41 if( pContainer != NULL ) \
42 { \
43 OInterfaceIteratorHelper aIterator( *pContainer ); \
44 EVENTTYP aLocalEvent = EVENT; \
45 /* Remark: The control is the event source not the peer.*/ \
46 /* We must change the source of the event. */ \
47 aLocalEvent.Source = m_xControl ; \
48 /* Is the control not destroyed? */ \
49 if( aLocalEvent.Source.is() == sal_True ) \
50 { \
51 if( aIterator.hasMoreElements() ) \
52 { \
53 INTERFACE * pListener = (INTERFACE *)aIterator.next(); \
54 try \
55 { \
56 pListener->METHOD( aLocalEvent ); \
57 } \
58 catch(const RuntimeException& ) \
59 { \
60 /* Ignore all system exceptions from the listener! */ \
61 } \
62 } \
63 } \
66 //____________________________________________________________________________________________________________
67 // construct/destruct
68 //____________________________________________________________________________________________________________
70 OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const Reference< XWindow >& xControl ,
71 const Reference< XWindow >& xPeer )
72 : m_xPeer ( xPeer )
73 , m_xControl ( xControl )
74 , m_aListenerHolder ( m_aMutex )
78 OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const OMRCListenerMultiplexerHelper& /*aCopyInstance*/ )
79 : XFocusListener()
80 , XWindowListener()
81 , XKeyListener()
82 , XMouseListener()
83 , XMouseMotionListener()
84 , XPaintListener()
85 , XTopWindowListener()
86 , OWeakObject()
87 , m_aListenerHolder ( m_aMutex )
91 OMRCListenerMultiplexerHelper::~OMRCListenerMultiplexerHelper()
95 //____________________________________________________________________________________________________________
96 // XInterface
97 //____________________________________________________________________________________________________________
99 Any SAL_CALL OMRCListenerMultiplexerHelper::queryInterface( const Type& rType ) throw( RuntimeException )
101 // Attention:
102 // Don't use mutex or guard in this method!!! Is a method of XInterface.
104 // Ask for my own supported interfaces ...
105 // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
106 Any aReturn ( ::cppu::queryInterface( rType ,
107 static_cast< XWindowListener* > ( this ) ,
108 static_cast< XKeyListener* > ( this ) ,
109 static_cast< XFocusListener* > ( this ) ,
110 static_cast< XMouseListener* > ( this ) ,
111 static_cast< XMouseMotionListener* > ( this ) ,
112 static_cast< XPaintListener* > ( this ) ,
113 static_cast< XTopWindowListener* > ( this ) ,
114 static_cast< XTopWindowListener* > ( this )
118 // If searched interface supported by this class ...
119 if ( aReturn.hasValue() == sal_True )
121 // ... return this information.
122 return aReturn ;
124 else
126 // Else; ... ask baseclass for interfaces!
127 return OWeakObject::queryInterface( rType );
131 //____________________________________________________________________________________________________________
132 // XInterface
133 //____________________________________________________________________________________________________________
135 void SAL_CALL OMRCListenerMultiplexerHelper::acquire() throw()
137 // Attention:
138 // Don't use mutex or guard in this method!!! Is a method of XInterface.
140 // Forward to baseclass
141 OWeakObject::acquire();
144 //____________________________________________________________________________________________________________
145 // XInterface
146 //____________________________________________________________________________________________________________
148 void SAL_CALL OMRCListenerMultiplexerHelper::release() throw()
150 // Attention:
151 // Don't use mutex or guard in this method!!! Is a method of XInterface.
153 // Forward to baseclass
154 OWeakObject::release();
157 //____________________________________________________________________________________________________________
158 // operator
159 //____________________________________________________________________________________________________________
161 OMRCListenerMultiplexerHelper::operator Reference< XInterface >() const
163 return ((OWeakObject*)this) ;
166 //____________________________________________________________________________________________________________
167 // container method
168 //____________________________________________________________________________________________________________
170 void OMRCListenerMultiplexerHelper::setPeer( const Reference< XWindow >& xPeer )
172 MutexGuard aGuard( m_aMutex );
173 if( m_xPeer != xPeer )
175 if( m_xPeer.is() )
177 // get all types from the listener added to the peer
178 Sequence< Type > aContainedTypes = m_aListenerHolder.getContainedTypes();
179 const Type* pArray = aContainedTypes.getConstArray();
180 sal_Int32 nCount = aContainedTypes.getLength();
181 // loop over all listener types and remove the listeners from the peer
182 for( sal_Int32 i=0; i<nCount; i++ )
183 impl_unadviseFromPeer( m_xPeer, pArray[i] );
185 m_xPeer = xPeer;
186 if( m_xPeer.is() )
188 // get all types from the listener added to the peer
189 Sequence< Type > aContainedTypes = m_aListenerHolder.getContainedTypes();
190 const Type* pArray = aContainedTypes.getConstArray();
191 sal_Int32 nCount = aContainedTypes.getLength();
192 // loop over all listener types and add the listeners to the peer
193 for( sal_Int32 i = 0; i < nCount; i++ )
194 impl_adviseToPeer( m_xPeer, pArray[i] );
199 //____________________________________________________________________________________________________________
200 // container method
201 //____________________________________________________________________________________________________________
203 void OMRCListenerMultiplexerHelper::disposeAndClear()
205 EventObject aEvent ;
206 aEvent.Source = m_xControl ;
207 m_aListenerHolder.disposeAndClear( aEvent );
210 //____________________________________________________________________________________________________________
211 // container method
212 //____________________________________________________________________________________________________________
214 void OMRCListenerMultiplexerHelper::advise( const Type& aType ,
215 const Reference< XInterface >& xListener )
217 MutexGuard aGuard( m_aMutex );
218 if( m_aListenerHolder.addInterface( aType, xListener ) == 1 )
220 // the first listener is added
221 if( m_xPeer.is() )
223 impl_adviseToPeer( m_xPeer, aType );
228 //____________________________________________________________________________________________________________
229 // container method
230 //____________________________________________________________________________________________________________
232 void OMRCListenerMultiplexerHelper::unadvise( const Type& aType ,
233 const Reference< XInterface >& xListener )
235 MutexGuard aGuard( m_aMutex );
236 if( m_aListenerHolder.removeInterface( aType, xListener ) == 0 )
238 // the last listener is removed
239 if ( m_xPeer.is() )
241 impl_unadviseFromPeer( m_xPeer, aType );
246 //____________________________________________________________________________________________________________
247 // XEventListener
248 //____________________________________________________________________________________________________________
250 void SAL_CALL OMRCListenerMultiplexerHelper::disposing( const EventObject& /*aSource*/ ) throw( RuntimeException )
252 MutexGuard aGuard( m_aMutex );
253 // peer is disposed, clear the reference
254 m_xPeer = Reference< XWindow >();
257 //____________________________________________________________________________________________________________
258 // XFcousListener
259 //____________________________________________________________________________________________________________
261 void OMRCListenerMultiplexerHelper::focusGained(const FocusEvent& aEvent ) throw( RuntimeException )
263 MULTIPLEX( XFocusListener, focusGained, FocusEvent, aEvent )
266 //____________________________________________________________________________________________________________
267 // XFcousListener
268 //____________________________________________________________________________________________________________
270 void OMRCListenerMultiplexerHelper::focusLost(const FocusEvent& aEvent ) throw( RuntimeException )
272 MULTIPLEX( XFocusListener, focusLost, FocusEvent, aEvent )
275 //____________________________________________________________________________________________________________
276 // XWindowListener
277 //____________________________________________________________________________________________________________
279 void OMRCListenerMultiplexerHelper::windowResized(const WindowEvent& aEvent ) throw( RuntimeException )
281 MULTIPLEX( XWindowListener, windowResized, WindowEvent, aEvent )
284 //____________________________________________________________________________________________________________
285 // XWindowListener
286 //____________________________________________________________________________________________________________
288 void OMRCListenerMultiplexerHelper::windowMoved(const WindowEvent& aEvent ) throw( RuntimeException )
290 MULTIPLEX( XWindowListener, windowMoved, WindowEvent, aEvent )
293 //____________________________________________________________________________________________________________
294 // XWindowListener
295 //____________________________________________________________________________________________________________
297 void OMRCListenerMultiplexerHelper::windowShown(const EventObject& aEvent ) throw( RuntimeException )
299 MULTIPLEX( XWindowListener, windowShown, EventObject, aEvent )
302 //____________________________________________________________________________________________________________
303 // XWindowListener
304 //____________________________________________________________________________________________________________
306 void OMRCListenerMultiplexerHelper::windowHidden(const EventObject& aEvent ) throw( RuntimeException )
308 MULTIPLEX( XWindowListener, windowHidden, EventObject, aEvent )
311 //____________________________________________________________________________________________________________
312 // XKeyListener
313 //____________________________________________________________________________________________________________
315 void OMRCListenerMultiplexerHelper::keyPressed(const KeyEvent& aEvent) throw( RuntimeException )
317 MULTIPLEX( XKeyListener, keyPressed, KeyEvent, aEvent )
320 //____________________________________________________________________________________________________________
321 // XKeyListener
322 //____________________________________________________________________________________________________________
324 void OMRCListenerMultiplexerHelper::keyReleased(const KeyEvent& aEvent) throw( RuntimeException )
326 MULTIPLEX( XKeyListener, keyReleased, KeyEvent, aEvent )
329 //____________________________________________________________________________________________________________
330 // XMouseListener
331 //____________________________________________________________________________________________________________
333 void OMRCListenerMultiplexerHelper::mousePressed(const MouseEvent& aEvent) throw( RuntimeException )
335 MULTIPLEX( XMouseListener, mousePressed, MouseEvent, aEvent )
338 //____________________________________________________________________________________________________________
339 // XMouseListener
340 //____________________________________________________________________________________________________________
342 void OMRCListenerMultiplexerHelper::mouseReleased(const MouseEvent& aEvent) throw( RuntimeException )
344 MULTIPLEX( XMouseListener, mouseReleased, MouseEvent, aEvent )
347 //____________________________________________________________________________________________________________
348 // XMouseListener
349 //____________________________________________________________________________________________________________
351 void OMRCListenerMultiplexerHelper::mouseEntered(const MouseEvent& aEvent) throw( RuntimeException )
353 MULTIPLEX( XMouseListener, mouseEntered, MouseEvent, aEvent )
356 //____________________________________________________________________________________________________________
357 // XMouseListener
358 //____________________________________________________________________________________________________________
360 void OMRCListenerMultiplexerHelper::mouseExited(const MouseEvent& aEvent) throw( RuntimeException )
362 MULTIPLEX( XMouseListener, mouseExited, MouseEvent, aEvent )
365 //____________________________________________________________________________________________________________
366 // XMouseMotionListener
367 //____________________________________________________________________________________________________________
369 void OMRCListenerMultiplexerHelper::mouseDragged(const MouseEvent& aEvent) throw( RuntimeException )
371 MULTIPLEX( XMouseMotionListener, mouseDragged, MouseEvent, aEvent )
374 //____________________________________________________________________________________________________________
375 // XMouseMotionListener
376 //____________________________________________________________________________________________________________
378 void OMRCListenerMultiplexerHelper::mouseMoved(const MouseEvent& aEvent) throw( RuntimeException )
380 MULTIPLEX( XMouseMotionListener, mouseMoved, MouseEvent, aEvent )
383 //____________________________________________________________________________________________________________
384 // XPaintListener
385 //____________________________________________________________________________________________________________
387 void OMRCListenerMultiplexerHelper::windowPaint(const PaintEvent& aEvent) throw( RuntimeException )
389 MULTIPLEX( XPaintListener, windowPaint, PaintEvent, aEvent )
392 //____________________________________________________________________________________________________________
393 // XTopWindowListener
394 //____________________________________________________________________________________________________________
396 void OMRCListenerMultiplexerHelper::windowOpened(const EventObject& aEvent) throw( RuntimeException )
398 MULTIPLEX( XTopWindowListener, windowOpened, EventObject, aEvent )
401 //____________________________________________________________________________________________________________
402 // XTopWindowListener
403 //____________________________________________________________________________________________________________
405 void OMRCListenerMultiplexerHelper::windowClosing( const EventObject& aEvent ) throw( RuntimeException )
407 MULTIPLEX( XTopWindowListener, windowClosing, EventObject, aEvent )
410 //____________________________________________________________________________________________________________
411 // XTopWindowListener
412 //____________________________________________________________________________________________________________
414 void OMRCListenerMultiplexerHelper::windowClosed( const EventObject& aEvent ) throw( RuntimeException )
416 MULTIPLEX( XTopWindowListener, windowClosed, EventObject, aEvent )
419 //____________________________________________________________________________________________________________
420 // XTopWindowListener
421 //____________________________________________________________________________________________________________
423 void OMRCListenerMultiplexerHelper::windowMinimized( const EventObject& aEvent ) throw( RuntimeException )
425 MULTIPLEX( XTopWindowListener, windowMinimized, EventObject, aEvent )
428 //____________________________________________________________________________________________________________
429 // XTopWindowListener
430 //____________________________________________________________________________________________________________
432 void OMRCListenerMultiplexerHelper::windowNormalized( const EventObject& aEvent ) throw( RuntimeException )
434 MULTIPLEX( XTopWindowListener, windowNormalized, EventObject, aEvent )
437 //____________________________________________________________________________________________________________
438 // XTopWindowListener
439 //____________________________________________________________________________________________________________
441 void OMRCListenerMultiplexerHelper::windowActivated( const EventObject& aEvent ) throw( RuntimeException )
443 MULTIPLEX( XTopWindowListener, windowActivated, EventObject, aEvent )
446 //____________________________________________________________________________________________________________
447 // XTopWindowListener
448 //____________________________________________________________________________________________________________
450 void OMRCListenerMultiplexerHelper::windowDeactivated( const EventObject& aEvent ) throw( RuntimeException )
452 MULTIPLEX( XTopWindowListener, windowDeactivated, EventObject, aEvent )
455 //____________________________________________________________________________________________________________
456 // protected method
457 //____________________________________________________________________________________________________________
459 void OMRCListenerMultiplexerHelper::impl_adviseToPeer( const Reference< XWindow >& xPeer ,
460 const Type& aType )
462 // add a listener to the source (peer)
463 if( aType == ::getCppuType((const Reference< XWindowListener >*)0) )
464 xPeer->addWindowListener( this );
465 else if( aType == ::getCppuType((const Reference< XKeyListener >*)0) )
466 xPeer->addKeyListener( this );
467 else if( aType == ::getCppuType((const Reference< XFocusListener >*)0) )
468 xPeer->addFocusListener( this );
469 else if( aType == ::getCppuType((const Reference< XMouseListener >*)0) )
470 xPeer->addMouseListener( this );
471 else if( aType == ::getCppuType((const Reference< XMouseMotionListener >*)0) )
472 xPeer->addMouseMotionListener( this );
473 else if( aType == ::getCppuType((const Reference< XPaintListener >*)0) )
474 xPeer->addPaintListener( this );
475 else if( aType == ::getCppuType((const Reference< XTopWindowListener >*)0) )
477 Reference< XTopWindow > xTop( xPeer, UNO_QUERY );
478 if( xTop.is() )
479 xTop->addTopWindowListener( this );
481 else
483 OSL_FAIL( "unknown listener" );
487 //____________________________________________________________________________________________________________
488 // protected method
489 //____________________________________________________________________________________________________________
491 void OMRCListenerMultiplexerHelper::impl_unadviseFromPeer( const Reference< XWindow >& xPeer ,
492 const Type& aType )
494 // the last listener is removed, remove the listener from the source (peer)
495 if( aType == ::getCppuType((const Reference< XWindowListener >*)0) )
496 xPeer->removeWindowListener( this );
497 else if( aType == ::getCppuType((const Reference< XKeyListener >*)0) )
498 xPeer->removeKeyListener( this );
499 else if( aType == ::getCppuType((const Reference< XFocusListener >*)0) )
500 xPeer->removeFocusListener( this );
501 else if( aType == ::getCppuType((const Reference< XMouseListener >*)0) )
502 xPeer->removeMouseListener( this );
503 else if( aType == ::getCppuType((const Reference< XMouseMotionListener >*)0) )
504 xPeer->removeMouseMotionListener( this );
505 else if( aType == ::getCppuType((const Reference< XPaintListener >*)0) )
506 xPeer->removePaintListener( this );
507 else if( aType == ::getCppuType((const Reference< XTopWindowListener >*)0) )
509 Reference< XTopWindow > xTop( xPeer, UNO_QUERY );
510 if( xTop.is() )
511 xTop->removeTopWindowListener( this );
513 else
515 OSL_FAIL( "unknown listener" );
519 } // namespace unocontrols
521 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */