1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
23 #include <cppuhelper/queryinterface.hxx>
25 #include <com/sun/star/awt/XWindow.hpp>
26 #include <com/sun/star/awt/XTopWindow.hpp>
28 using namespace ::cppu
;
29 using namespace ::osl
;
30 using namespace ::com::sun::star::uno
;
31 using namespace ::com::sun::star::awt
;
32 using namespace ::com::sun::star::lang
;
34 namespace unocontrols
{
38 #define MULTIPLEX( INTERFACE, METHOD, EVENTTYP, EVENT ) \
40 /* First get all interfaces from container with right type.*/ \
41 comphelper::OInterfaceContainerHelper2* pContainer = m_aListenerHolder.getContainer( cppu::UnoType<INTERFACE>::get() ); \
42 /* Do the follow only, if elements in container exist.*/ \
45 comphelper::OInterfaceIteratorHelper2 aIterator( *pContainer ); \
46 EVENTTYP aLocalEvent = EVENT; \
47 /* Remark: The control is the event source not the peer.*/ \
48 /* We must change the source of the event. */ \
49 aLocalEvent.Source = m_xControl; \
50 /* Is the control not destroyed? */ \
51 if( !aLocalEvent.Source ) \
53 if( !aIterator.hasMoreElements() ) \
55 INTERFACE * pListener = static_cast<INTERFACE *>(aIterator.next()); \
58 pListener->METHOD( aLocalEvent ); \
60 catch(const RuntimeException& ) \
62 /* Ignore all system exceptions from the listener! */ \
67 OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const Reference
< XWindow
>& xControl
,
68 const Reference
< XWindow
>& xPeer
)
70 , m_xControl ( xControl
)
71 , m_aListenerHolder ( m_aMutex
)
75 OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const OMRCListenerMultiplexerHelper
& aCopyInstance
)
80 , XMouseMotionListener()
82 , XTopWindowListener()
83 , OWeakObject(aCopyInstance
)
84 , m_aListenerHolder ( m_aMutex
)
88 OMRCListenerMultiplexerHelper::~OMRCListenerMultiplexerHelper()
94 Any SAL_CALL
OMRCListenerMultiplexerHelper::queryInterface( const Type
& rType
)
97 // Don't use mutex or guard in this method!!! Is a method of XInterface.
99 // Ask for my own supported interfaces ...
100 // Attention: XTypeProvider and XInterface are supported by WeakComponentImplHelper!
101 Any
aReturn ( ::cppu::queryInterface( rType
,
102 static_cast< XWindowListener
* > ( this ) ,
103 static_cast< XKeyListener
* > ( this ) ,
104 static_cast< XFocusListener
* > ( this ) ,
105 static_cast< XMouseListener
* > ( this ) ,
106 static_cast< XMouseMotionListener
* > ( this ) ,
107 static_cast< XPaintListener
* > ( this ) ,
108 static_cast< XTopWindowListener
* > ( this ) ,
109 static_cast< XTopWindowListener
* > ( this )
113 // If searched interface supported by this class ...
114 if ( aReturn
.hasValue() )
116 // ... return this information.
121 // Else; ... ask baseclass for interfaces!
122 return OWeakObject::queryInterface( rType
);
128 void SAL_CALL
OMRCListenerMultiplexerHelper::acquire() noexcept
131 // Don't use mutex or guard in this method!!! Is a method of XInterface.
133 // Forward to baseclass
134 OWeakObject::acquire();
139 void SAL_CALL
OMRCListenerMultiplexerHelper::release() noexcept
142 // Don't use mutex or guard in this method!!! Is a method of XInterface.
144 // Forward to baseclass
145 OWeakObject::release();
150 void OMRCListenerMultiplexerHelper::setPeer( const Reference
< XWindow
>& xPeer
)
152 MutexGuard
aGuard( m_aMutex
);
153 if( m_xPeer
== xPeer
)
158 // get all types from the listener added to the peer
159 const std::vector
< Type
> aContainedTypes
= m_aListenerHolder
.getContainedTypes();
160 // loop over all listener types and remove the listeners from the peer
161 for( const auto& rContainedType
: aContainedTypes
)
162 impl_unadviseFromPeer( m_xPeer
, rContainedType
);
167 // get all types from the listener added to the peer
168 const std::vector
< Type
> aContainedTypes
= m_aListenerHolder
.getContainedTypes();
169 // loop over all listener types and add the listeners to the peer
170 for( const auto& rContainedType
: aContainedTypes
)
171 impl_adviseToPeer( m_xPeer
, rContainedType
);
177 void OMRCListenerMultiplexerHelper::disposeAndClear()
180 aEvent
.Source
= m_xControl
;
181 m_aListenerHolder
.disposeAndClear( aEvent
);
186 void OMRCListenerMultiplexerHelper::advise( const Type
& aType
,
187 const Reference
< XInterface
>& xListener
)
189 MutexGuard
aGuard( m_aMutex
);
190 if( m_aListenerHolder
.addInterface( aType
, xListener
) == 1 )
192 // the first listener is added
195 impl_adviseToPeer( m_xPeer
, aType
);
202 void OMRCListenerMultiplexerHelper::unadvise( const Type
& aType
,
203 const Reference
< XInterface
>& xListener
)
205 MutexGuard
aGuard( m_aMutex
);
206 if( m_aListenerHolder
.removeInterface( aType
, xListener
) == 0 )
208 // the last listener is removed
211 impl_unadviseFromPeer( m_xPeer
, aType
);
218 void SAL_CALL
OMRCListenerMultiplexerHelper::disposing( const EventObject
& /*aSource*/ )
220 MutexGuard
aGuard( m_aMutex
);
221 // peer is disposed, clear the reference
227 void OMRCListenerMultiplexerHelper::focusGained(const FocusEvent
& aEvent
)
229 MULTIPLEX( XFocusListener
, focusGained
, FocusEvent
, aEvent
)
234 void OMRCListenerMultiplexerHelper::focusLost(const FocusEvent
& aEvent
)
236 MULTIPLEX( XFocusListener
, focusLost
, FocusEvent
, aEvent
)
241 void OMRCListenerMultiplexerHelper::windowResized(const WindowEvent
& aEvent
)
243 MULTIPLEX( XWindowListener
, windowResized
, WindowEvent
, aEvent
)
248 void OMRCListenerMultiplexerHelper::windowMoved(const WindowEvent
& aEvent
)
250 MULTIPLEX( XWindowListener
, windowMoved
, WindowEvent
, aEvent
)
255 void OMRCListenerMultiplexerHelper::windowShown(const EventObject
& aEvent
)
257 MULTIPLEX( XWindowListener
, windowShown
, EventObject
, aEvent
)
262 void OMRCListenerMultiplexerHelper::windowHidden(const EventObject
& aEvent
)
264 MULTIPLEX( XWindowListener
, windowHidden
, EventObject
, aEvent
)
269 void OMRCListenerMultiplexerHelper::keyPressed(const KeyEvent
& aEvent
)
271 MULTIPLEX( XKeyListener
, keyPressed
, KeyEvent
, aEvent
)
276 void OMRCListenerMultiplexerHelper::keyReleased(const KeyEvent
& aEvent
)
278 MULTIPLEX( XKeyListener
, keyReleased
, KeyEvent
, aEvent
)
283 void OMRCListenerMultiplexerHelper::mousePressed(const MouseEvent
& aEvent
)
285 MULTIPLEX( XMouseListener
, mousePressed
, MouseEvent
, aEvent
)
290 void OMRCListenerMultiplexerHelper::mouseReleased(const MouseEvent
& aEvent
)
292 MULTIPLEX( XMouseListener
, mouseReleased
, MouseEvent
, aEvent
)
297 void OMRCListenerMultiplexerHelper::mouseEntered(const MouseEvent
& aEvent
)
299 MULTIPLEX( XMouseListener
, mouseEntered
, MouseEvent
, aEvent
)
304 void OMRCListenerMultiplexerHelper::mouseExited(const MouseEvent
& aEvent
)
306 MULTIPLEX( XMouseListener
, mouseExited
, MouseEvent
, aEvent
)
309 // XMouseMotionListener
311 void OMRCListenerMultiplexerHelper::mouseDragged(const MouseEvent
& aEvent
)
313 MULTIPLEX( XMouseMotionListener
, mouseDragged
, MouseEvent
, aEvent
)
316 // XMouseMotionListener
318 void OMRCListenerMultiplexerHelper::mouseMoved(const MouseEvent
& aEvent
)
320 MULTIPLEX( XMouseMotionListener
, mouseMoved
, MouseEvent
, aEvent
)
325 void OMRCListenerMultiplexerHelper::windowPaint(const PaintEvent
& aEvent
)
327 MULTIPLEX( XPaintListener
, windowPaint
, PaintEvent
, aEvent
)
330 // XTopWindowListener
332 void OMRCListenerMultiplexerHelper::windowOpened(const EventObject
& aEvent
)
334 MULTIPLEX( XTopWindowListener
, windowOpened
, EventObject
, aEvent
)
337 // XTopWindowListener
339 void OMRCListenerMultiplexerHelper::windowClosing( const EventObject
& aEvent
)
341 MULTIPLEX( XTopWindowListener
, windowClosing
, EventObject
, aEvent
)
344 // XTopWindowListener
346 void OMRCListenerMultiplexerHelper::windowClosed( const EventObject
& aEvent
)
348 MULTIPLEX( XTopWindowListener
, windowClosed
, EventObject
, aEvent
)
351 // XTopWindowListener
353 void OMRCListenerMultiplexerHelper::windowMinimized( const EventObject
& aEvent
)
355 MULTIPLEX( XTopWindowListener
, windowMinimized
, EventObject
, aEvent
)
358 // XTopWindowListener
360 void OMRCListenerMultiplexerHelper::windowNormalized( const EventObject
& aEvent
)
362 MULTIPLEX( XTopWindowListener
, windowNormalized
, EventObject
, aEvent
)
365 // XTopWindowListener
367 void OMRCListenerMultiplexerHelper::windowActivated( const EventObject
& aEvent
)
369 MULTIPLEX( XTopWindowListener
, windowActivated
, EventObject
, aEvent
)
372 // XTopWindowListener
374 void OMRCListenerMultiplexerHelper::windowDeactivated( const EventObject
& aEvent
)
376 MULTIPLEX( XTopWindowListener
, windowDeactivated
, EventObject
, aEvent
)
381 void OMRCListenerMultiplexerHelper::impl_adviseToPeer( const Reference
< XWindow
>& xPeer
,
384 // add a listener to the source (peer)
385 if( aType
== cppu::UnoType
<XWindowListener
>::get())
386 xPeer
->addWindowListener( this );
387 else if( aType
== cppu::UnoType
<XKeyListener
>::get())
388 xPeer
->addKeyListener( this );
389 else if( aType
== cppu::UnoType
<XFocusListener
>::get())
390 xPeer
->addFocusListener( this );
391 else if( aType
== cppu::UnoType
<XMouseListener
>::get())
392 xPeer
->addMouseListener( this );
393 else if( aType
== cppu::UnoType
<XMouseMotionListener
>::get())
394 xPeer
->addMouseMotionListener( this );
395 else if( aType
== cppu::UnoType
<XPaintListener
>::get())
396 xPeer
->addPaintListener( this );
397 else if( aType
== cppu::UnoType
<XTopWindowListener
>::get())
399 Reference
< XTopWindow
> xTop( xPeer
, UNO_QUERY
);
401 xTop
->addTopWindowListener( this );
405 OSL_FAIL( "unknown listener" );
411 void OMRCListenerMultiplexerHelper::impl_unadviseFromPeer( const Reference
< XWindow
>& xPeer
,
414 // the last listener is removed, remove the listener from the source (peer)
415 if( aType
== cppu::UnoType
<XWindowListener
>::get())
416 xPeer
->removeWindowListener( this );
417 else if( aType
== cppu::UnoType
<XKeyListener
>::get())
418 xPeer
->removeKeyListener( this );
419 else if( aType
== cppu::UnoType
<XFocusListener
>::get())
420 xPeer
->removeFocusListener( this );
421 else if( aType
== cppu::UnoType
<XMouseListener
>::get())
422 xPeer
->removeMouseListener( this );
423 else if( aType
== cppu::UnoType
<XMouseMotionListener
>::get())
424 xPeer
->removeMouseMotionListener( this );
425 else if( aType
== cppu::UnoType
<XPaintListener
>::get())
426 xPeer
->removePaintListener( this );
427 else if( aType
== cppu::UnoType
<XTopWindowListener
>::get())
429 Reference
< XTopWindow
> xTop( xPeer
, UNO_QUERY
);
431 xTop
->removeTopWindowListener( this );
435 OSL_FAIL( "unknown listener" );
439 } // namespace unocontrols
441 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */