1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <osl/diagnose.h>
30 #include <plugin/multiplx.hxx>
33 // class MRCListenerMultiplexerHelper
35 MRCListenerMultiplexerHelper::MRCListenerMultiplexerHelper
37 const Reference
< ::com::sun::star::awt::XWindow
> & rControl
38 , const Reference
< ::com::sun::star::awt::XWindow
> & rPeer
41 , xControl( Reference
< ::com::sun::star::awt::XControl
>( rControl
, UNO_QUERY
) )
42 , aListenerHolder( aMutex
)
47 void MRCListenerMultiplexerHelper::setPeer( const Reference
< ::com::sun::star::awt::XWindow
> & rPeer
)
49 ::osl::Guard
< ::osl::Mutex
> aGuard( aMutex
);
54 // get all uiks from the listener added to the peer
55 Sequence
<Type
> aContainedTypes
= aListenerHolder
.getContainedTypes();
56 const Type
* pArray
= aContainedTypes
.getConstArray();
57 sal_Int32 nCount
= aContainedTypes
.getLength();
58 // loop over all listener types and remove the listeners from the peer
59 for( sal_Int32 i
= 0; i
< nCount
; i
++ )
60 unadviseFromPeer( xPeer
, pArray
[i
] );
65 // get all uiks from the listener added to the peer
66 Sequence
<Type
> aContainedTypes
= aListenerHolder
.getContainedTypes();
67 const Type
* pArray
= aContainedTypes
.getConstArray();
68 sal_Int32 nCount
= aContainedTypes
.getLength();
69 // loop over all listener types and add the listeners to the peer
70 for( sal_Int32 i
= 0; i
< nCount
; i
++ )
71 adviseToPeer( xPeer
, pArray
[i
] );
76 // MRCListenerMultiplexerHelper
77 void MRCListenerMultiplexerHelper::disposeAndClear()
79 ::com::sun::star::lang::EventObject aEvt
;
80 aEvt
.Source
= xControl
;
81 aListenerHolder
.disposeAndClear( aEvt
);
84 // MRCListenerMultiplexerHelper
85 void MRCListenerMultiplexerHelper::adviseToPeer( const Reference
< ::com::sun::star::awt::XWindow
> & rPeer
, const Type
& type
)
87 // add a listener to the source (peer)
88 if( type
== cppu::UnoType
<com::sun::star::awt::XWindowListener
>::get())
89 rPeer
->addWindowListener( this );
90 else if( type
== cppu::UnoType
<com::sun::star::awt::XKeyListener
>::get())
91 rPeer
->addKeyListener( this );
92 else if( type
== cppu::UnoType
<com::sun::star::awt::XFocusListener
>::get())
93 rPeer
->addFocusListener( this );
94 else if( type
== cppu::UnoType
<com::sun::star::awt::XMouseListener
>::get())
95 rPeer
->addMouseListener( this );
96 else if( type
== cppu::UnoType
<com::sun::star::awt::XMouseMotionListener
>::get())
97 rPeer
->addMouseMotionListener( this );
98 else if( type
== cppu::UnoType
<com::sun::star::awt::XPaintListener
>::get())
99 rPeer
->addPaintListener( this );
100 else if( type
== cppu::UnoType
<com::sun::star::awt::XTopWindowListener
>::get())
102 Reference
< ::com::sun::star::awt::XTopWindow
> xTop( rPeer
, UNO_QUERY
);
104 xTop
->addTopWindowListener( this );
108 OSL_FAIL( "unknown listener" );
112 // MRCListenerMultiplexerHelper
113 void MRCListenerMultiplexerHelper::unadviseFromPeer( const Reference
< ::com::sun::star::awt::XWindow
> & rPeer
, const Type
& type
)
115 // the last listener is removed, remove the listener from the source (peer)
116 if( type
== cppu::UnoType
<com::sun::star::awt::XWindowListener
>::get())
117 rPeer
->removeWindowListener( this );
118 else if( type
== cppu::UnoType
<com::sun::star::awt::XKeyListener
>::get())
119 rPeer
->removeKeyListener( this );
120 else if( type
== cppu::UnoType
<com::sun::star::awt::XFocusListener
>::get())
121 rPeer
->removeFocusListener( this );
122 else if( type
== cppu::UnoType
<com::sun::star::awt::XMouseListener
>::get())
123 rPeer
->removeMouseListener( this );
124 else if( type
== cppu::UnoType
<com::sun::star::awt::XMouseMotionListener
>::get())
125 rPeer
->removeMouseMotionListener( this );
126 else if( type
== cppu::UnoType
<com::sun::star::awt::XPaintListener
>::get())
127 rPeer
->removePaintListener( this );
128 else if( type
== cppu::UnoType
<com::sun::star::awt::XTopWindowListener
>::get())
130 Reference
< ::com::sun::star::awt::XTopWindow
> xTop( rPeer
, UNO_QUERY
);
132 xTop
->removeTopWindowListener( this );
136 OSL_FAIL( "unknown listener" );
140 // MRCListenerMultiplexerHelper
141 void MRCListenerMultiplexerHelper::advise( const Type
& type
, const Reference
< XInterface
> & listener
)
143 ::osl::Guard
< ::osl::Mutex
> aGuard( aMutex
);
144 if( 1 == aListenerHolder
.addInterface( type
, listener
) )
146 // the first listener is added
148 adviseToPeer( xPeer
, type
);
152 // MRCListenerMultiplexerHelper
153 void MRCListenerMultiplexerHelper::unadvise(const Type
& type
, const Reference
< XInterface
> & listener
)
155 ::osl::Guard
< ::osl::Mutex
> aGuard( aMutex
);
156 ::cppu::OInterfaceContainerHelper
* pCont
= aListenerHolder
.getContainer( type
);
159 if( 0 == pCont
->removeInterface( listener
) && xPeer
.is() )
160 // the last listener is removed
161 unadviseFromPeer( xPeer
, type
);
165 // ::com::sun::star::lang::XEventListener
166 void MRCListenerMultiplexerHelper::disposing(const ::com::sun::star::lang::EventObject
& ) throw(std::exception
)
168 ::osl::Guard
< ::osl::Mutex
> aGuard( aMutex
);
169 // peer is disposed, clear the reference
170 xPeer
= Reference
< ::com::sun::star::awt::XWindow
> ();
173 #define MULTIPLEX( InterfaceName, MethodName, EventName ) \
174 ::cppu::OInterfaceContainerHelper * pCont; \
175 pCont = aListenerHolder.getContainer( cppu::UnoType< InterfaceName >::get()); \
178 ::cppu::OInterfaceIteratorHelper aIt( *pCont ); \
179 EventName aEvt = e; \
180 /* Remark: The control is the event source not the peer. We must change */ \
181 /* the source of the event */ \
182 aEvt.Source = xControl;\
183 /*.is the control not destroyed */ \
184 if( aEvt.Source.is() ) \
186 if( aIt.hasMoreElements() ) \
188 InterfaceName * pListener = static_cast<InterfaceName *>(aIt.next()); \
191 pListener->MethodName( aEvt ); \
193 catch(const RuntimeException&) \
195 /* ignore all usr system exceptions from the listener */ \
201 // ::com::sun::star::awt::XFocusListener
202 void MRCListenerMultiplexerHelper::focusGained(const ::com::sun::star::awt::FocusEvent
& e
) throw(std::exception
)
204 MULTIPLEX( ::com::sun::star::awt::XFocusListener
, focusGained
, ::com::sun::star::awt::FocusEvent
)
207 // ::com::sun::star::awt::XFocusListener
208 void MRCListenerMultiplexerHelper::focusLost(const ::com::sun::star::awt::FocusEvent
& e
) throw(std::exception
)
210 MULTIPLEX( ::com::sun::star::awt::XFocusListener
, focusLost
, ::com::sun::star::awt::FocusEvent
)
213 // ::com::sun::star::awt::XWindowListener
214 void MRCListenerMultiplexerHelper::windowResized(const ::com::sun::star::awt::WindowEvent
& e
) throw(std::exception
)
216 MULTIPLEX( ::com::sun::star::awt::XWindowListener
, windowResized
, ::com::sun::star::awt::WindowEvent
)
219 // ::com::sun::star::awt::XWindowListener
220 void MRCListenerMultiplexerHelper::windowMoved(const ::com::sun::star::awt::WindowEvent
& e
) throw(std::exception
)
222 MULTIPLEX( ::com::sun::star::awt::XWindowListener
, windowMoved
, ::com::sun::star::awt::WindowEvent
)
225 // ::com::sun::star::awt::XWindowListener
226 void MRCListenerMultiplexerHelper::windowShown(const ::com::sun::star::lang::EventObject
& e
) throw(std::exception
)
228 MULTIPLEX( ::com::sun::star::awt::XWindowListener
, windowShown
, ::com::sun::star::lang::EventObject
)
231 // ::com::sun::star::awt::XWindowListener
232 void MRCListenerMultiplexerHelper::windowHidden(const ::com::sun::star::lang::EventObject
& e
) throw(std::exception
)
234 MULTIPLEX( ::com::sun::star::awt::XWindowListener
, windowHidden
, ::com::sun::star::lang::EventObject
)
237 // ::com::sun::star::awt::XKeyListener
238 void MRCListenerMultiplexerHelper::keyPressed(const ::com::sun::star::awt::KeyEvent
& e
) throw(std::exception
)
240 MULTIPLEX( ::com::sun::star::awt::XKeyListener
, keyPressed
, ::com::sun::star::awt::KeyEvent
)
243 // ::com::sun::star::awt::XKeyListener
244 void MRCListenerMultiplexerHelper::keyReleased(const ::com::sun::star::awt::KeyEvent
& e
) throw(std::exception
)
246 MULTIPLEX( ::com::sun::star::awt::XKeyListener
, keyReleased
, ::com::sun::star::awt::KeyEvent
)
249 // ::com::sun::star::awt::XMouseListener
250 void MRCListenerMultiplexerHelper::mousePressed(const ::com::sun::star::awt::MouseEvent
& e
) throw(std::exception
)
252 MULTIPLEX( ::com::sun::star::awt::XMouseListener
, mousePressed
, ::com::sun::star::awt::MouseEvent
)
255 // ::com::sun::star::awt::XMouseListener
256 void MRCListenerMultiplexerHelper::mouseReleased(const ::com::sun::star::awt::MouseEvent
& e
) throw(std::exception
)
258 MULTIPLEX( ::com::sun::star::awt::XMouseListener
, mouseReleased
, ::com::sun::star::awt::MouseEvent
)
261 // ::com::sun::star::awt::XMouseListener
262 void MRCListenerMultiplexerHelper::mouseEntered(const ::com::sun::star::awt::MouseEvent
& e
) throw(std::exception
)
264 MULTIPLEX( ::com::sun::star::awt::XMouseListener
, mouseEntered
, ::com::sun::star::awt::MouseEvent
)
267 // ::com::sun::star::awt::XMouseListener
268 void MRCListenerMultiplexerHelper::mouseExited(const ::com::sun::star::awt::MouseEvent
& e
) throw(std::exception
)
270 MULTIPLEX( ::com::sun::star::awt::XMouseListener
, mouseExited
, ::com::sun::star::awt::MouseEvent
)
273 // ::com::sun::star::awt::XMouseMotionListener
274 void MRCListenerMultiplexerHelper::mouseDragged(const ::com::sun::star::awt::MouseEvent
& e
) throw(std::exception
)
276 MULTIPLEX( ::com::sun::star::awt::XMouseMotionListener
, mouseDragged
, ::com::sun::star::awt::MouseEvent
)
279 // ::com::sun::star::awt::XMouseMotionListener
280 void MRCListenerMultiplexerHelper::mouseMoved(const ::com::sun::star::awt::MouseEvent
& e
) throw(std::exception
)
282 MULTIPLEX( ::com::sun::star::awt::XMouseMotionListener
, mouseMoved
, ::com::sun::star::awt::MouseEvent
)
285 // ::com::sun::star::awt::XPaintListener
286 void MRCListenerMultiplexerHelper::windowPaint(const ::com::sun::star::awt::PaintEvent
& e
) throw(std::exception
)
288 MULTIPLEX( ::com::sun::star::awt::XPaintListener
, windowPaint
, ::com::sun::star::awt::PaintEvent
)
291 // ::com::sun::star::awt::XTopWindowListener
292 void MRCListenerMultiplexerHelper::windowOpened(const ::com::sun::star::lang::EventObject
& e
) throw(std::exception
)
294 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener
, windowOpened
, ::com::sun::star::lang::EventObject
)
297 // ::com::sun::star::awt::XTopWindowListener
298 void MRCListenerMultiplexerHelper::windowClosing( const ::com::sun::star::lang::EventObject
& e
) throw(std::exception
)
300 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener
, windowClosing
, ::com::sun::star::lang::EventObject
)
303 // ::com::sun::star::awt::XTopWindowListener
304 void MRCListenerMultiplexerHelper::windowClosed( const ::com::sun::star::lang::EventObject
& e
) throw(std::exception
)
306 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener
, windowClosed
, ::com::sun::star::lang::EventObject
)
309 // ::com::sun::star::awt::XTopWindowListener
310 void MRCListenerMultiplexerHelper::windowMinimized( const ::com::sun::star::lang::EventObject
& e
) throw(std::exception
)
312 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener
, windowMinimized
, ::com::sun::star::lang::EventObject
)
315 // ::com::sun::star::awt::XTopWindowListener
316 void MRCListenerMultiplexerHelper::windowNormalized( const ::com::sun::star::lang::EventObject
& e
) throw(std::exception
)
318 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener
, windowNormalized
, ::com::sun::star::lang::EventObject
)
321 // ::com::sun::star::awt::XTopWindowListener
322 void MRCListenerMultiplexerHelper::windowActivated( const ::com::sun::star::lang::EventObject
& e
) throw(std::exception
)
324 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener
, windowActivated
, ::com::sun::star::lang::EventObject
)
327 // ::com::sun::star::awt::XTopWindowListener
328 void MRCListenerMultiplexerHelper::windowDeactivated( const ::com::sun::star::lang::EventObject
& e
) throw(std::exception
)
330 MULTIPLEX( ::com::sun::star::awt::XTopWindowListener
, windowDeactivated
, ::com::sun::star::lang::EventObject
)
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */