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