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 :
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 ;
29 :
30 : namespace unocontrols{
31 :
32 : //____________________________________________________________________________________________________________
33 : // macros
34 : //____________________________________________________________________________________________________________
35 :
36 : #define MULTIPLEX( INTERFACE, METHOD, EVENTTYP, EVENT ) \
37 : \
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 : } \
64 : }
65 :
66 : //____________________________________________________________________________________________________________
67 : // construct/destruct
68 : //____________________________________________________________________________________________________________
69 :
70 0 : OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const Reference< XWindow >& xControl ,
71 : const Reference< XWindow >& xPeer )
72 : : m_xPeer ( xPeer )
73 : , m_xControl ( xControl )
74 0 : , m_aListenerHolder ( m_aMutex )
75 : {
76 0 : }
77 :
78 0 : OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const OMRCListenerMultiplexerHelper& /*aCopyInstance*/ )
79 : : XFocusListener()
80 : , XWindowListener()
81 : , XKeyListener()
82 : , XMouseListener()
83 : , XMouseMotionListener()
84 : , XPaintListener()
85 : , XTopWindowListener()
86 : , OWeakObject()
87 0 : , m_aListenerHolder ( m_aMutex )
88 : {
89 0 : }
90 :
91 0 : OMRCListenerMultiplexerHelper::~OMRCListenerMultiplexerHelper()
92 : {
93 0 : }
94 :
95 : //____________________________________________________________________________________________________________
96 : // XInterface
97 : //____________________________________________________________________________________________________________
98 :
99 0 : Any SAL_CALL OMRCListenerMultiplexerHelper::queryInterface( const Type& rType ) throw( RuntimeException )
100 : {
101 : // Attention:
102 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
103 :
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 )
115 : )
116 0 : );
117 :
118 : // If searched interface supported by this class ...
119 0 : if ( aReturn.hasValue() == sal_True )
120 : {
121 : // ... return this information.
122 0 : return aReturn ;
123 : }
124 : else
125 : {
126 : // Else; ... ask baseclass for interfaces!
127 0 : return OWeakObject::queryInterface( rType );
128 0 : }
129 : }
130 :
131 : //____________________________________________________________________________________________________________
132 : // XInterface
133 : //____________________________________________________________________________________________________________
134 :
135 0 : void SAL_CALL OMRCListenerMultiplexerHelper::acquire() throw()
136 : {
137 : // Attention:
138 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
139 :
140 : // Forward to baseclass
141 0 : OWeakObject::acquire();
142 0 : }
143 :
144 : //____________________________________________________________________________________________________________
145 : // XInterface
146 : //____________________________________________________________________________________________________________
147 :
148 0 : void SAL_CALL OMRCListenerMultiplexerHelper::release() throw()
149 : {
150 : // Attention:
151 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
152 :
153 : // Forward to baseclass
154 0 : OWeakObject::release();
155 0 : }
156 :
157 : //____________________________________________________________________________________________________________
158 : // operator
159 : //____________________________________________________________________________________________________________
160 :
161 0 : OMRCListenerMultiplexerHelper::operator Reference< XInterface >() const
162 : {
163 0 : return ((OWeakObject*)this) ;
164 : }
165 :
166 : //____________________________________________________________________________________________________________
167 : // container method
168 : //____________________________________________________________________________________________________________
169 :
170 0 : void OMRCListenerMultiplexerHelper::setPeer( const Reference< XWindow >& xPeer )
171 : {
172 0 : MutexGuard aGuard( m_aMutex );
173 0 : if( m_xPeer != xPeer )
174 : {
175 0 : if( m_xPeer.is() )
176 : {
177 : // get all types from the listener added to the peer
178 0 : Sequence< Type > aContainedTypes = m_aListenerHolder.getContainedTypes();
179 0 : const Type* pArray = aContainedTypes.getConstArray();
180 0 : sal_Int32 nCount = aContainedTypes.getLength();
181 : // loop over all listener types and remove the listeners from the peer
182 0 : for( sal_Int32 i=0; i<nCount; i++ )
183 0 : impl_unadviseFromPeer( m_xPeer, pArray[i] );
184 : }
185 0 : m_xPeer = xPeer;
186 0 : if( m_xPeer.is() )
187 : {
188 : // get all types from the listener added to the peer
189 0 : Sequence< Type > aContainedTypes = m_aListenerHolder.getContainedTypes();
190 0 : const Type* pArray = aContainedTypes.getConstArray();
191 0 : sal_Int32 nCount = aContainedTypes.getLength();
192 : // loop over all listener types and add the listeners to the peer
193 0 : for( sal_Int32 i = 0; i < nCount; i++ )
194 0 : impl_adviseToPeer( m_xPeer, pArray[i] );
195 : }
196 0 : }
197 0 : }
198 :
199 : //____________________________________________________________________________________________________________
200 : // container method
201 : //____________________________________________________________________________________________________________
202 :
203 0 : void OMRCListenerMultiplexerHelper::disposeAndClear()
204 : {
205 0 : EventObject aEvent ;
206 0 : aEvent.Source = m_xControl ;
207 0 : m_aListenerHolder.disposeAndClear( aEvent );
208 0 : }
209 :
210 : //____________________________________________________________________________________________________________
211 : // container method
212 : //____________________________________________________________________________________________________________
213 :
214 0 : void OMRCListenerMultiplexerHelper::advise( const Type& aType ,
215 : const Reference< XInterface >& xListener )
216 : {
217 0 : MutexGuard aGuard( m_aMutex );
218 0 : if( m_aListenerHolder.addInterface( aType, xListener ) == 1 )
219 : {
220 : // the first listener is added
221 0 : if( m_xPeer.is() )
222 : {
223 0 : impl_adviseToPeer( m_xPeer, aType );
224 : }
225 0 : }
226 0 : }
227 :
228 : //____________________________________________________________________________________________________________
229 : // container method
230 : //____________________________________________________________________________________________________________
231 :
232 0 : void OMRCListenerMultiplexerHelper::unadvise( const Type& aType ,
233 : const Reference< XInterface >& xListener )
234 : {
235 0 : MutexGuard aGuard( m_aMutex );
236 0 : if( m_aListenerHolder.removeInterface( aType, xListener ) == 0 )
237 : {
238 : // the last listener is removed
239 0 : if ( m_xPeer.is() )
240 : {
241 0 : impl_unadviseFromPeer( m_xPeer, aType );
242 : }
243 0 : }
244 0 : }
245 :
246 : //____________________________________________________________________________________________________________
247 : // XEventListener
248 : //____________________________________________________________________________________________________________
249 :
250 0 : void SAL_CALL OMRCListenerMultiplexerHelper::disposing( const EventObject& /*aSource*/ ) throw( RuntimeException )
251 : {
252 0 : MutexGuard aGuard( m_aMutex );
253 : // peer is disposed, clear the reference
254 0 : m_xPeer = Reference< XWindow >();
255 0 : }
256 :
257 : //____________________________________________________________________________________________________________
258 : // XFcousListener
259 : //____________________________________________________________________________________________________________
260 :
261 0 : void OMRCListenerMultiplexerHelper::focusGained(const FocusEvent& aEvent ) throw( RuntimeException )
262 : {
263 0 : MULTIPLEX( XFocusListener, focusGained, FocusEvent, aEvent )
264 0 : }
265 :
266 : //____________________________________________________________________________________________________________
267 : // XFcousListener
268 : //____________________________________________________________________________________________________________
269 :
270 0 : void OMRCListenerMultiplexerHelper::focusLost(const FocusEvent& aEvent ) throw( RuntimeException )
271 : {
272 0 : MULTIPLEX( XFocusListener, focusLost, FocusEvent, aEvent )
273 0 : }
274 :
275 : //____________________________________________________________________________________________________________
276 : // XWindowListener
277 : //____________________________________________________________________________________________________________
278 :
279 0 : void OMRCListenerMultiplexerHelper::windowResized(const WindowEvent& aEvent ) throw( RuntimeException )
280 : {
281 0 : MULTIPLEX( XWindowListener, windowResized, WindowEvent, aEvent )
282 0 : }
283 :
284 : //____________________________________________________________________________________________________________
285 : // XWindowListener
286 : //____________________________________________________________________________________________________________
287 :
288 0 : void OMRCListenerMultiplexerHelper::windowMoved(const WindowEvent& aEvent ) throw( RuntimeException )
289 : {
290 0 : MULTIPLEX( XWindowListener, windowMoved, WindowEvent, aEvent )
291 0 : }
292 :
293 : //____________________________________________________________________________________________________________
294 : // XWindowListener
295 : //____________________________________________________________________________________________________________
296 :
297 0 : void OMRCListenerMultiplexerHelper::windowShown(const EventObject& aEvent ) throw( RuntimeException )
298 : {
299 0 : MULTIPLEX( XWindowListener, windowShown, EventObject, aEvent )
300 0 : }
301 :
302 : //____________________________________________________________________________________________________________
303 : // XWindowListener
304 : //____________________________________________________________________________________________________________
305 :
306 0 : void OMRCListenerMultiplexerHelper::windowHidden(const EventObject& aEvent ) throw( RuntimeException )
307 : {
308 0 : MULTIPLEX( XWindowListener, windowHidden, EventObject, aEvent )
309 0 : }
310 :
311 : //____________________________________________________________________________________________________________
312 : // XKeyListener
313 : //____________________________________________________________________________________________________________
314 :
315 0 : void OMRCListenerMultiplexerHelper::keyPressed(const KeyEvent& aEvent) throw( RuntimeException )
316 : {
317 0 : MULTIPLEX( XKeyListener, keyPressed, KeyEvent, aEvent )
318 0 : }
319 :
320 : //____________________________________________________________________________________________________________
321 : // XKeyListener
322 : //____________________________________________________________________________________________________________
323 :
324 0 : void OMRCListenerMultiplexerHelper::keyReleased(const KeyEvent& aEvent) throw( RuntimeException )
325 : {
326 0 : MULTIPLEX( XKeyListener, keyReleased, KeyEvent, aEvent )
327 0 : }
328 :
329 : //____________________________________________________________________________________________________________
330 : // XMouseListener
331 : //____________________________________________________________________________________________________________
332 :
333 0 : void OMRCListenerMultiplexerHelper::mousePressed(const MouseEvent& aEvent) throw( RuntimeException )
334 : {
335 0 : MULTIPLEX( XMouseListener, mousePressed, MouseEvent, aEvent )
336 0 : }
337 :
338 : //____________________________________________________________________________________________________________
339 : // XMouseListener
340 : //____________________________________________________________________________________________________________
341 :
342 0 : void OMRCListenerMultiplexerHelper::mouseReleased(const MouseEvent& aEvent) throw( RuntimeException )
343 : {
344 0 : MULTIPLEX( XMouseListener, mouseReleased, MouseEvent, aEvent )
345 0 : }
346 :
347 : //____________________________________________________________________________________________________________
348 : // XMouseListener
349 : //____________________________________________________________________________________________________________
350 :
351 0 : void OMRCListenerMultiplexerHelper::mouseEntered(const MouseEvent& aEvent) throw( RuntimeException )
352 : {
353 0 : MULTIPLEX( XMouseListener, mouseEntered, MouseEvent, aEvent )
354 0 : }
355 :
356 : //____________________________________________________________________________________________________________
357 : // XMouseListener
358 : //____________________________________________________________________________________________________________
359 :
360 0 : void OMRCListenerMultiplexerHelper::mouseExited(const MouseEvent& aEvent) throw( RuntimeException )
361 : {
362 0 : MULTIPLEX( XMouseListener, mouseExited, MouseEvent, aEvent )
363 0 : }
364 :
365 : //____________________________________________________________________________________________________________
366 : // XMouseMotionListener
367 : //____________________________________________________________________________________________________________
368 :
369 0 : void OMRCListenerMultiplexerHelper::mouseDragged(const MouseEvent& aEvent) throw( RuntimeException )
370 : {
371 0 : MULTIPLEX( XMouseMotionListener, mouseDragged, MouseEvent, aEvent )
372 0 : }
373 :
374 : //____________________________________________________________________________________________________________
375 : // XMouseMotionListener
376 : //____________________________________________________________________________________________________________
377 :
378 0 : void OMRCListenerMultiplexerHelper::mouseMoved(const MouseEvent& aEvent) throw( RuntimeException )
379 : {
380 0 : MULTIPLEX( XMouseMotionListener, mouseMoved, MouseEvent, aEvent )
381 0 : }
382 :
383 : //____________________________________________________________________________________________________________
384 : // XPaintListener
385 : //____________________________________________________________________________________________________________
386 :
387 0 : void OMRCListenerMultiplexerHelper::windowPaint(const PaintEvent& aEvent) throw( RuntimeException )
388 : {
389 0 : MULTIPLEX( XPaintListener, windowPaint, PaintEvent, aEvent )
390 0 : }
391 :
392 : //____________________________________________________________________________________________________________
393 : // XTopWindowListener
394 : //____________________________________________________________________________________________________________
395 :
396 0 : void OMRCListenerMultiplexerHelper::windowOpened(const EventObject& aEvent) throw( RuntimeException )
397 : {
398 0 : MULTIPLEX( XTopWindowListener, windowOpened, EventObject, aEvent )
399 0 : }
400 :
401 : //____________________________________________________________________________________________________________
402 : // XTopWindowListener
403 : //____________________________________________________________________________________________________________
404 :
405 0 : void OMRCListenerMultiplexerHelper::windowClosing( const EventObject& aEvent ) throw( RuntimeException )
406 : {
407 0 : MULTIPLEX( XTopWindowListener, windowClosing, EventObject, aEvent )
408 0 : }
409 :
410 : //____________________________________________________________________________________________________________
411 : // XTopWindowListener
412 : //____________________________________________________________________________________________________________
413 :
414 0 : void OMRCListenerMultiplexerHelper::windowClosed( const EventObject& aEvent ) throw( RuntimeException )
415 : {
416 0 : MULTIPLEX( XTopWindowListener, windowClosed, EventObject, aEvent )
417 0 : }
418 :
419 : //____________________________________________________________________________________________________________
420 : // XTopWindowListener
421 : //____________________________________________________________________________________________________________
422 :
423 0 : void OMRCListenerMultiplexerHelper::windowMinimized( const EventObject& aEvent ) throw( RuntimeException )
424 : {
425 0 : MULTIPLEX( XTopWindowListener, windowMinimized, EventObject, aEvent )
426 0 : }
427 :
428 : //____________________________________________________________________________________________________________
429 : // XTopWindowListener
430 : //____________________________________________________________________________________________________________
431 :
432 0 : void OMRCListenerMultiplexerHelper::windowNormalized( const EventObject& aEvent ) throw( RuntimeException )
433 : {
434 0 : MULTIPLEX( XTopWindowListener, windowNormalized, EventObject, aEvent )
435 0 : }
436 :
437 : //____________________________________________________________________________________________________________
438 : // XTopWindowListener
439 : //____________________________________________________________________________________________________________
440 :
441 0 : void OMRCListenerMultiplexerHelper::windowActivated( const EventObject& aEvent ) throw( RuntimeException )
442 : {
443 0 : MULTIPLEX( XTopWindowListener, windowActivated, EventObject, aEvent )
444 0 : }
445 :
446 : //____________________________________________________________________________________________________________
447 : // XTopWindowListener
448 : //____________________________________________________________________________________________________________
449 :
450 0 : void OMRCListenerMultiplexerHelper::windowDeactivated( const EventObject& aEvent ) throw( RuntimeException )
451 : {
452 0 : MULTIPLEX( XTopWindowListener, windowDeactivated, EventObject, aEvent )
453 0 : }
454 :
455 : //____________________________________________________________________________________________________________
456 : // protected method
457 : //____________________________________________________________________________________________________________
458 :
459 0 : void OMRCListenerMultiplexerHelper::impl_adviseToPeer( const Reference< XWindow >& xPeer ,
460 : const Type& aType )
461 : {
462 : // add a listener to the source (peer)
463 0 : if( aType == ::getCppuType((const Reference< XWindowListener >*)0) )
464 0 : xPeer->addWindowListener( this );
465 0 : else if( aType == ::getCppuType((const Reference< XKeyListener >*)0) )
466 0 : xPeer->addKeyListener( this );
467 0 : else if( aType == ::getCppuType((const Reference< XFocusListener >*)0) )
468 0 : xPeer->addFocusListener( this );
469 0 : else if( aType == ::getCppuType((const Reference< XMouseListener >*)0) )
470 0 : xPeer->addMouseListener( this );
471 0 : else if( aType == ::getCppuType((const Reference< XMouseMotionListener >*)0) )
472 0 : xPeer->addMouseMotionListener( this );
473 0 : else if( aType == ::getCppuType((const Reference< XPaintListener >*)0) )
474 0 : xPeer->addPaintListener( this );
475 0 : else if( aType == ::getCppuType((const Reference< XTopWindowListener >*)0) )
476 : {
477 0 : Reference< XTopWindow > xTop( xPeer, UNO_QUERY );
478 0 : if( xTop.is() )
479 0 : xTop->addTopWindowListener( this );
480 : }
481 : else
482 : {
483 : OSL_FAIL( "unknown listener" );
484 : }
485 0 : }
486 :
487 : //____________________________________________________________________________________________________________
488 : // protected method
489 : //____________________________________________________________________________________________________________
490 :
491 0 : void OMRCListenerMultiplexerHelper::impl_unadviseFromPeer( const Reference< XWindow >& xPeer ,
492 : const Type& aType )
493 : {
494 : // the last listener is removed, remove the listener from the source (peer)
495 0 : if( aType == ::getCppuType((const Reference< XWindowListener >*)0) )
496 0 : xPeer->removeWindowListener( this );
497 0 : else if( aType == ::getCppuType((const Reference< XKeyListener >*)0) )
498 0 : xPeer->removeKeyListener( this );
499 0 : else if( aType == ::getCppuType((const Reference< XFocusListener >*)0) )
500 0 : xPeer->removeFocusListener( this );
501 0 : else if( aType == ::getCppuType((const Reference< XMouseListener >*)0) )
502 0 : xPeer->removeMouseListener( this );
503 0 : else if( aType == ::getCppuType((const Reference< XMouseMotionListener >*)0) )
504 0 : xPeer->removeMouseMotionListener( this );
505 0 : else if( aType == ::getCppuType((const Reference< XPaintListener >*)0) )
506 0 : xPeer->removePaintListener( this );
507 0 : else if( aType == ::getCppuType((const Reference< XTopWindowListener >*)0) )
508 : {
509 0 : Reference< XTopWindow > xTop( xPeer, UNO_QUERY );
510 0 : if( xTop.is() )
511 0 : xTop->removeTopWindowListener( this );
512 : }
513 : else
514 : {
515 : OSL_FAIL( "unknown listener" );
516 : }
517 0 : }
518 :
519 : } // namespace unocontrols
520 :
521 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|