Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "multiplexer.hxx"
21 :
22 : #include <osl/diagnose.h>
23 : #include <cppuhelper/queryinterface.hxx>
24 :
25 : using namespace ::cppu;
26 : using namespace ::osl;
27 : using namespace ::com::sun::star::uno;
28 : using namespace ::com::sun::star::awt;
29 : using namespace ::com::sun::star::lang;
30 :
31 : namespace unocontrols{
32 :
33 : // macros
34 :
35 : #define MULTIPLEX( INTERFACE, METHOD, EVENTTYP, EVENT ) \
36 : \
37 : /* First get all interfaces from container with right type.*/ \
38 : OInterfaceContainerHelper* pContainer = m_aListenerHolder.getContainer( cppu::UnoType<INTERFACE>::get() ); \
39 : /* Do the follow only, if elements in container exist.*/ \
40 : if( pContainer != NULL ) \
41 : { \
42 : OInterfaceIteratorHelper aIterator( *pContainer ); \
43 : EVENTTYP aLocalEvent = EVENT; \
44 : /* Remark: The control is the event source not the peer.*/ \
45 : /* We must change the source of the event. */ \
46 : aLocalEvent.Source = m_xControl; \
47 : /* Is the control not destroyed? */ \
48 : if( aLocalEvent.Source.is() ) \
49 : { \
50 : if( aIterator.hasMoreElements() ) \
51 : { \
52 : INTERFACE * pListener = static_cast<INTERFACE *>(aIterator.next()); \
53 : try \
54 : { \
55 : pListener->METHOD( aLocalEvent ); \
56 : } \
57 : catch(const RuntimeException& ) \
58 : { \
59 : /* Ignore all system exceptions from the listener! */ \
60 : } \
61 : } \
62 : } \
63 : }
64 :
65 : // construct/destruct
66 :
67 0 : OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const Reference< XWindow >& xControl ,
68 : const Reference< XWindow >& xPeer )
69 : : m_xPeer ( xPeer )
70 : , m_xControl ( xControl )
71 0 : , m_aListenerHolder ( m_aMutex )
72 : {
73 0 : }
74 :
75 0 : OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const OMRCListenerMultiplexerHelper& /*aCopyInstance*/ )
76 : : XFocusListener()
77 : , XWindowListener()
78 : , XKeyListener()
79 : , XMouseListener()
80 : , XMouseMotionListener()
81 : , XPaintListener()
82 : , XTopWindowListener()
83 : , OWeakObject()
84 0 : , m_aListenerHolder ( m_aMutex )
85 : {
86 0 : }
87 :
88 0 : OMRCListenerMultiplexerHelper::~OMRCListenerMultiplexerHelper()
89 : {
90 0 : }
91 :
92 : // XInterface
93 :
94 0 : Any SAL_CALL OMRCListenerMultiplexerHelper::queryInterface( const Type& rType ) throw( RuntimeException, std::exception )
95 : {
96 : // Attention:
97 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
98 :
99 : // Ask for my own supported interfaces ...
100 : // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
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 )
110 : )
111 0 : );
112 :
113 : // If searched interface supported by this class ...
114 0 : if ( aReturn.hasValue() )
115 : {
116 : // ... return this information.
117 0 : return aReturn;
118 : }
119 : else
120 : {
121 : // Else; ... ask baseclass for interfaces!
122 0 : return OWeakObject::queryInterface( rType );
123 0 : }
124 : }
125 :
126 : // XInterface
127 :
128 0 : void SAL_CALL OMRCListenerMultiplexerHelper::acquire() throw()
129 : {
130 : // Attention:
131 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
132 :
133 : // Forward to baseclass
134 0 : OWeakObject::acquire();
135 0 : }
136 :
137 : // XInterface
138 :
139 0 : void SAL_CALL OMRCListenerMultiplexerHelper::release() throw()
140 : {
141 : // Attention:
142 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
143 :
144 : // Forward to baseclass
145 0 : OWeakObject::release();
146 0 : }
147 :
148 : // container method
149 :
150 0 : void OMRCListenerMultiplexerHelper::setPeer( const Reference< XWindow >& xPeer )
151 : {
152 0 : MutexGuard aGuard( m_aMutex );
153 0 : if( m_xPeer != xPeer )
154 : {
155 0 : if( m_xPeer.is() )
156 : {
157 : // get all types from the listener added to the peer
158 0 : Sequence< Type > aContainedTypes = m_aListenerHolder.getContainedTypes();
159 0 : const Type* pArray = aContainedTypes.getConstArray();
160 0 : sal_Int32 nCount = aContainedTypes.getLength();
161 : // loop over all listener types and remove the listeners from the peer
162 0 : for( sal_Int32 i=0; i<nCount; i++ )
163 0 : impl_unadviseFromPeer( m_xPeer, pArray[i] );
164 : }
165 0 : m_xPeer = xPeer;
166 0 : if( m_xPeer.is() )
167 : {
168 : // get all types from the listener added to the peer
169 0 : Sequence< Type > aContainedTypes = m_aListenerHolder.getContainedTypes();
170 0 : const Type* pArray = aContainedTypes.getConstArray();
171 0 : sal_Int32 nCount = aContainedTypes.getLength();
172 : // loop over all listener types and add the listeners to the peer
173 0 : for( sal_Int32 i = 0; i < nCount; i++ )
174 0 : impl_adviseToPeer( m_xPeer, pArray[i] );
175 : }
176 0 : }
177 0 : }
178 :
179 : // container method
180 :
181 0 : void OMRCListenerMultiplexerHelper::disposeAndClear()
182 : {
183 0 : EventObject aEvent;
184 0 : aEvent.Source = m_xControl;
185 0 : m_aListenerHolder.disposeAndClear( aEvent );
186 0 : }
187 :
188 : // container method
189 :
190 0 : void OMRCListenerMultiplexerHelper::advise( const Type& aType ,
191 : const Reference< XInterface >& xListener )
192 : {
193 0 : MutexGuard aGuard( m_aMutex );
194 0 : if( m_aListenerHolder.addInterface( aType, xListener ) == 1 )
195 : {
196 : // the first listener is added
197 0 : if( m_xPeer.is() )
198 : {
199 0 : impl_adviseToPeer( m_xPeer, aType );
200 : }
201 0 : }
202 0 : }
203 :
204 : // container method
205 :
206 0 : void OMRCListenerMultiplexerHelper::unadvise( const Type& aType ,
207 : const Reference< XInterface >& xListener )
208 : {
209 0 : MutexGuard aGuard( m_aMutex );
210 0 : if( m_aListenerHolder.removeInterface( aType, xListener ) == 0 )
211 : {
212 : // the last listener is removed
213 0 : if ( m_xPeer.is() )
214 : {
215 0 : impl_unadviseFromPeer( m_xPeer, aType );
216 : }
217 0 : }
218 0 : }
219 :
220 : // XEventListener
221 :
222 0 : void SAL_CALL OMRCListenerMultiplexerHelper::disposing( const EventObject& /*aSource*/ ) throw( RuntimeException, std::exception )
223 : {
224 0 : MutexGuard aGuard( m_aMutex );
225 : // peer is disposed, clear the reference
226 0 : m_xPeer.clear();
227 0 : }
228 :
229 : // XFcousListener
230 :
231 0 : void OMRCListenerMultiplexerHelper::focusGained(const FocusEvent& aEvent ) throw( RuntimeException, std::exception )
232 : {
233 0 : MULTIPLEX( XFocusListener, focusGained, FocusEvent, aEvent )
234 0 : }
235 :
236 : // XFcousListener
237 :
238 0 : void OMRCListenerMultiplexerHelper::focusLost(const FocusEvent& aEvent ) throw( RuntimeException, std::exception )
239 : {
240 0 : MULTIPLEX( XFocusListener, focusLost, FocusEvent, aEvent )
241 0 : }
242 :
243 : // XWindowListener
244 :
245 0 : void OMRCListenerMultiplexerHelper::windowResized(const WindowEvent& aEvent ) throw( RuntimeException, std::exception )
246 : {
247 0 : MULTIPLEX( XWindowListener, windowResized, WindowEvent, aEvent )
248 0 : }
249 :
250 : // XWindowListener
251 :
252 0 : void OMRCListenerMultiplexerHelper::windowMoved(const WindowEvent& aEvent ) throw( RuntimeException, std::exception )
253 : {
254 0 : MULTIPLEX( XWindowListener, windowMoved, WindowEvent, aEvent )
255 0 : }
256 :
257 : // XWindowListener
258 :
259 0 : void OMRCListenerMultiplexerHelper::windowShown(const EventObject& aEvent ) throw( RuntimeException, std::exception )
260 : {
261 0 : MULTIPLEX( XWindowListener, windowShown, EventObject, aEvent )
262 0 : }
263 :
264 : // XWindowListener
265 :
266 0 : void OMRCListenerMultiplexerHelper::windowHidden(const EventObject& aEvent ) throw( RuntimeException, std::exception )
267 : {
268 0 : MULTIPLEX( XWindowListener, windowHidden, EventObject, aEvent )
269 0 : }
270 :
271 : // XKeyListener
272 :
273 0 : void OMRCListenerMultiplexerHelper::keyPressed(const KeyEvent& aEvent) throw( RuntimeException, std::exception )
274 : {
275 0 : MULTIPLEX( XKeyListener, keyPressed, KeyEvent, aEvent )
276 0 : }
277 :
278 : // XKeyListener
279 :
280 0 : void OMRCListenerMultiplexerHelper::keyReleased(const KeyEvent& aEvent) throw( RuntimeException, std::exception )
281 : {
282 0 : MULTIPLEX( XKeyListener, keyReleased, KeyEvent, aEvent )
283 0 : }
284 :
285 : // XMouseListener
286 :
287 0 : void OMRCListenerMultiplexerHelper::mousePressed(const MouseEvent& aEvent) throw( RuntimeException, std::exception )
288 : {
289 0 : MULTIPLEX( XMouseListener, mousePressed, MouseEvent, aEvent )
290 0 : }
291 :
292 : // XMouseListener
293 :
294 0 : void OMRCListenerMultiplexerHelper::mouseReleased(const MouseEvent& aEvent) throw( RuntimeException, std::exception )
295 : {
296 0 : MULTIPLEX( XMouseListener, mouseReleased, MouseEvent, aEvent )
297 0 : }
298 :
299 : // XMouseListener
300 :
301 0 : void OMRCListenerMultiplexerHelper::mouseEntered(const MouseEvent& aEvent) throw( RuntimeException, std::exception )
302 : {
303 0 : MULTIPLEX( XMouseListener, mouseEntered, MouseEvent, aEvent )
304 0 : }
305 :
306 : // XMouseListener
307 :
308 0 : void OMRCListenerMultiplexerHelper::mouseExited(const MouseEvent& aEvent) throw( RuntimeException, std::exception )
309 : {
310 0 : MULTIPLEX( XMouseListener, mouseExited, MouseEvent, aEvent )
311 0 : }
312 :
313 : // XMouseMotionListener
314 :
315 0 : void OMRCListenerMultiplexerHelper::mouseDragged(const MouseEvent& aEvent) throw( RuntimeException, std::exception )
316 : {
317 0 : MULTIPLEX( XMouseMotionListener, mouseDragged, MouseEvent, aEvent )
318 0 : }
319 :
320 : // XMouseMotionListener
321 :
322 0 : void OMRCListenerMultiplexerHelper::mouseMoved(const MouseEvent& aEvent) throw( RuntimeException, std::exception )
323 : {
324 0 : MULTIPLEX( XMouseMotionListener, mouseMoved, MouseEvent, aEvent )
325 0 : }
326 :
327 : // XPaintListener
328 :
329 0 : void OMRCListenerMultiplexerHelper::windowPaint(const PaintEvent& aEvent) throw( RuntimeException, std::exception )
330 : {
331 0 : MULTIPLEX( XPaintListener, windowPaint, PaintEvent, aEvent )
332 0 : }
333 :
334 : // XTopWindowListener
335 :
336 0 : void OMRCListenerMultiplexerHelper::windowOpened(const EventObject& aEvent) throw( RuntimeException, std::exception )
337 : {
338 0 : MULTIPLEX( XTopWindowListener, windowOpened, EventObject, aEvent )
339 0 : }
340 :
341 : // XTopWindowListener
342 :
343 0 : void OMRCListenerMultiplexerHelper::windowClosing( const EventObject& aEvent ) throw( RuntimeException, std::exception )
344 : {
345 0 : MULTIPLEX( XTopWindowListener, windowClosing, EventObject, aEvent )
346 0 : }
347 :
348 : // XTopWindowListener
349 :
350 0 : void OMRCListenerMultiplexerHelper::windowClosed( const EventObject& aEvent ) throw( RuntimeException, std::exception )
351 : {
352 0 : MULTIPLEX( XTopWindowListener, windowClosed, EventObject, aEvent )
353 0 : }
354 :
355 : // XTopWindowListener
356 :
357 0 : void OMRCListenerMultiplexerHelper::windowMinimized( const EventObject& aEvent ) throw( RuntimeException, std::exception )
358 : {
359 0 : MULTIPLEX( XTopWindowListener, windowMinimized, EventObject, aEvent )
360 0 : }
361 :
362 : // XTopWindowListener
363 :
364 0 : void OMRCListenerMultiplexerHelper::windowNormalized( const EventObject& aEvent ) throw( RuntimeException, std::exception )
365 : {
366 0 : MULTIPLEX( XTopWindowListener, windowNormalized, EventObject, aEvent )
367 0 : }
368 :
369 : // XTopWindowListener
370 :
371 0 : void OMRCListenerMultiplexerHelper::windowActivated( const EventObject& aEvent ) throw( RuntimeException, std::exception )
372 : {
373 0 : MULTIPLEX( XTopWindowListener, windowActivated, EventObject, aEvent )
374 0 : }
375 :
376 : // XTopWindowListener
377 :
378 0 : void OMRCListenerMultiplexerHelper::windowDeactivated( const EventObject& aEvent ) throw( RuntimeException, std::exception )
379 : {
380 0 : MULTIPLEX( XTopWindowListener, windowDeactivated, EventObject, aEvent )
381 0 : }
382 :
383 : // protected method
384 :
385 0 : void OMRCListenerMultiplexerHelper::impl_adviseToPeer( const Reference< XWindow >& xPeer ,
386 : const Type& aType )
387 : {
388 : // add a listener to the source (peer)
389 0 : if( aType == cppu::UnoType<XWindowListener>::get())
390 0 : xPeer->addWindowListener( this );
391 0 : else if( aType == cppu::UnoType<XKeyListener>::get())
392 0 : xPeer->addKeyListener( this );
393 0 : else if( aType == cppu::UnoType<XFocusListener>::get())
394 0 : xPeer->addFocusListener( this );
395 0 : else if( aType == cppu::UnoType<XMouseListener>::get())
396 0 : xPeer->addMouseListener( this );
397 0 : else if( aType == cppu::UnoType<XMouseMotionListener>::get())
398 0 : xPeer->addMouseMotionListener( this );
399 0 : else if( aType == cppu::UnoType<XPaintListener>::get())
400 0 : xPeer->addPaintListener( this );
401 0 : else if( aType == cppu::UnoType<XTopWindowListener>::get())
402 : {
403 0 : Reference< XTopWindow > xTop( xPeer, UNO_QUERY );
404 0 : if( xTop.is() )
405 0 : xTop->addTopWindowListener( this );
406 : }
407 : else
408 : {
409 : OSL_FAIL( "unknown listener" );
410 : }
411 0 : }
412 :
413 : // protected method
414 :
415 0 : void OMRCListenerMultiplexerHelper::impl_unadviseFromPeer( const Reference< XWindow >& xPeer ,
416 : const Type& aType )
417 : {
418 : // the last listener is removed, remove the listener from the source (peer)
419 0 : if( aType == cppu::UnoType<XWindowListener>::get())
420 0 : xPeer->removeWindowListener( this );
421 0 : else if( aType == cppu::UnoType<XKeyListener>::get())
422 0 : xPeer->removeKeyListener( this );
423 0 : else if( aType == cppu::UnoType<XFocusListener>::get())
424 0 : xPeer->removeFocusListener( this );
425 0 : else if( aType == cppu::UnoType<XMouseListener>::get())
426 0 : xPeer->removeMouseListener( this );
427 0 : else if( aType == cppu::UnoType<XMouseMotionListener>::get())
428 0 : xPeer->removeMouseMotionListener( this );
429 0 : else if( aType == cppu::UnoType<XPaintListener>::get())
430 0 : xPeer->removePaintListener( this );
431 0 : else if( aType == cppu::UnoType<XTopWindowListener>::get())
432 : {
433 0 : Reference< XTopWindow > xTop( xPeer, UNO_QUERY );
434 0 : if( xTop.is() )
435 0 : xTop->removeTopWindowListener( this );
436 : }
437 : else
438 : {
439 : OSL_FAIL( "unknown listener" );
440 : }
441 0 : }
442 :
443 : } // namespace unocontrols
444 :
445 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|