Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / UnoControls / source / base / multiplexer.cxx
blob0f0f34b68660ea1c56b0750e766716f9eece5fe7
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>
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 {
36 // macros
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.*/ \
43 if( !pContainer ) \
44 return; \
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 ) \
52 return; \
53 if( !aIterator.hasMoreElements() ) \
54 return; \
55 INTERFACE * pListener = static_cast<INTERFACE *>(aIterator.next()); \
56 try \
57 { \
58 pListener->METHOD( aLocalEvent ); \
59 } \
60 catch(const RuntimeException& ) \
61 { \
62 /* Ignore all system exceptions from the listener! */ \
65 // construct/destruct
67 OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const Reference< XWindow >& xControl ,
68 const Reference< XWindow >& xPeer )
69 : m_xPeer ( xPeer )
70 , m_xControl ( xControl )
71 , m_aListenerHolder ( m_aMutex )
75 OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const OMRCListenerMultiplexerHelper& aCopyInstance )
76 : XFocusListener()
77 , XWindowListener()
78 , XKeyListener()
79 , XMouseListener()
80 , XMouseMotionListener()
81 , XPaintListener()
82 , XTopWindowListener()
83 , OWeakObject(aCopyInstance)
84 , m_aListenerHolder ( m_aMutex )
88 OMRCListenerMultiplexerHelper::~OMRCListenerMultiplexerHelper()
92 // XInterface
94 Any SAL_CALL OMRCListenerMultiplexerHelper::queryInterface( const Type& rType )
96 // Attention:
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.
117 return aReturn;
119 else
121 // Else; ... ask baseclass for interfaces!
122 return OWeakObject::queryInterface( rType );
126 // XInterface
128 void SAL_CALL OMRCListenerMultiplexerHelper::acquire() noexcept
130 // Attention:
131 // Don't use mutex or guard in this method!!! Is a method of XInterface.
133 // Forward to baseclass
134 OWeakObject::acquire();
137 // XInterface
139 void SAL_CALL OMRCListenerMultiplexerHelper::release() noexcept
141 // Attention:
142 // Don't use mutex or guard in this method!!! Is a method of XInterface.
144 // Forward to baseclass
145 OWeakObject::release();
148 // container method
150 void OMRCListenerMultiplexerHelper::setPeer( const Reference< XWindow >& xPeer )
152 MutexGuard aGuard( m_aMutex );
153 if( m_xPeer == xPeer )
154 return;
156 if( m_xPeer.is() )
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 );
164 m_xPeer = xPeer;
165 if( m_xPeer.is() )
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 );
175 // container method
177 void OMRCListenerMultiplexerHelper::disposeAndClear()
179 EventObject aEvent;
180 aEvent.Source = m_xControl;
181 m_aListenerHolder.disposeAndClear( aEvent );
184 // container method
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
193 if( m_xPeer.is() )
195 impl_adviseToPeer( m_xPeer, aType );
200 // container method
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
209 if ( m_xPeer.is() )
211 impl_unadviseFromPeer( m_xPeer, aType );
216 // XEventListener
218 void SAL_CALL OMRCListenerMultiplexerHelper::disposing( const EventObject& /*aSource*/ )
220 MutexGuard aGuard( m_aMutex );
221 // peer is disposed, clear the reference
222 m_xPeer.clear();
225 // XFcousListener
227 void OMRCListenerMultiplexerHelper::focusGained(const FocusEvent& aEvent )
229 MULTIPLEX( XFocusListener, focusGained, FocusEvent, aEvent )
232 // XFcousListener
234 void OMRCListenerMultiplexerHelper::focusLost(const FocusEvent& aEvent )
236 MULTIPLEX( XFocusListener, focusLost, FocusEvent, aEvent )
239 // XWindowListener
241 void OMRCListenerMultiplexerHelper::windowResized(const WindowEvent& aEvent )
243 MULTIPLEX( XWindowListener, windowResized, WindowEvent, aEvent )
246 // XWindowListener
248 void OMRCListenerMultiplexerHelper::windowMoved(const WindowEvent& aEvent )
250 MULTIPLEX( XWindowListener, windowMoved, WindowEvent, aEvent )
253 // XWindowListener
255 void OMRCListenerMultiplexerHelper::windowShown(const EventObject& aEvent )
257 MULTIPLEX( XWindowListener, windowShown, EventObject, aEvent )
260 // XWindowListener
262 void OMRCListenerMultiplexerHelper::windowHidden(const EventObject& aEvent )
264 MULTIPLEX( XWindowListener, windowHidden, EventObject, aEvent )
267 // XKeyListener
269 void OMRCListenerMultiplexerHelper::keyPressed(const KeyEvent& aEvent)
271 MULTIPLEX( XKeyListener, keyPressed, KeyEvent, aEvent )
274 // XKeyListener
276 void OMRCListenerMultiplexerHelper::keyReleased(const KeyEvent& aEvent)
278 MULTIPLEX( XKeyListener, keyReleased, KeyEvent, aEvent )
281 // XMouseListener
283 void OMRCListenerMultiplexerHelper::mousePressed(const MouseEvent& aEvent)
285 MULTIPLEX( XMouseListener, mousePressed, MouseEvent, aEvent )
288 // XMouseListener
290 void OMRCListenerMultiplexerHelper::mouseReleased(const MouseEvent& aEvent)
292 MULTIPLEX( XMouseListener, mouseReleased, MouseEvent, aEvent )
295 // XMouseListener
297 void OMRCListenerMultiplexerHelper::mouseEntered(const MouseEvent& aEvent)
299 MULTIPLEX( XMouseListener, mouseEntered, MouseEvent, aEvent )
302 // XMouseListener
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 )
323 // XPaintListener
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 )
379 // protected method
381 void OMRCListenerMultiplexerHelper::impl_adviseToPeer( const Reference< XWindow >& xPeer ,
382 const Type& aType )
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 );
400 if( xTop.is() )
401 xTop->addTopWindowListener( this );
403 else
405 OSL_FAIL( "unknown listener" );
409 // protected method
411 void OMRCListenerMultiplexerHelper::impl_unadviseFromPeer( const Reference< XWindow >& xPeer ,
412 const Type& aType )
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 );
430 if( xTop.is() )
431 xTop->removeTopWindowListener( this );
433 else
435 OSL_FAIL( "unknown listener" );
439 } // namespace unocontrols
441 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */