1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <dndlistenercontainer.hxx>
22 using namespace ::cppu
;
23 using namespace ::com::sun::star::uno
;
24 using namespace ::com::sun::star::datatransfer
;
25 using namespace ::com::sun::star::datatransfer::dnd
;
27 DNDListenerContainer::DNDListenerContainer( sal_Int8 nDefaultActions
)
28 : WeakComponentImplHelper
< XDragGestureRecognizer
, XDropTargetDragContext
, XDropTargetDropContext
, XDropTarget
>(GetMutex())
31 m_nDefaultActions
= nDefaultActions
;
34 DNDListenerContainer::~DNDListenerContainer()
38 void SAL_CALL
DNDListenerContainer::addDragGestureListener( const Reference
< XDragGestureListener
>& dgl
)
40 rBHelper
.addListener( cppu::UnoType
<XDragGestureListener
>::get(), dgl
);
43 void SAL_CALL
DNDListenerContainer::removeDragGestureListener( const Reference
< XDragGestureListener
>& dgl
)
45 rBHelper
.removeListener( cppu::UnoType
<XDragGestureListener
>::get(), dgl
);
48 void SAL_CALL
DNDListenerContainer::resetRecognizer( )
52 void SAL_CALL
DNDListenerContainer::addDropTargetListener( const Reference
< XDropTargetListener
>& dtl
)
54 rBHelper
.addListener( cppu::UnoType
<XDropTargetListener
>::get(), dtl
);
57 void SAL_CALL
DNDListenerContainer::removeDropTargetListener( const Reference
< XDropTargetListener
>& dtl
)
59 rBHelper
.removeListener( cppu::UnoType
<XDropTargetListener
>::get(), dtl
);
62 sal_Bool SAL_CALL
DNDListenerContainer::isActive( )
67 void SAL_CALL
DNDListenerContainer::setActive( sal_Bool active
)
72 sal_Int8 SAL_CALL
DNDListenerContainer::getDefaultActions( )
74 return m_nDefaultActions
;
77 void SAL_CALL
DNDListenerContainer::setDefaultActions( sal_Int8 actions
)
79 m_nDefaultActions
= actions
;
82 sal_uInt32
DNDListenerContainer::fireDropEvent( const Reference
< XDropTargetDropContext
>& context
,
83 sal_Int8 dropAction
, sal_Int32 locationX
, sal_Int32 locationY
, sal_Int8 sourceActions
,
84 const Reference
< XTransferable
>& transferable
)
88 // fire DropTargetDropEvent on all XDropTargetListeners
89 OInterfaceContainerHelper
*pContainer
= rBHelper
.getContainer( cppu::UnoType
<XDropTargetListener
>::get());
91 if( pContainer
&& m_bActive
)
93 OInterfaceIteratorHelper
aIterator( *pContainer
);
95 // remember context to use in own context methods
96 m_xDropTargetDropContext
= context
;
98 // do not construct the event before you are sure at least one listener is registered
99 DropTargetDropEvent
aEvent( static_cast < XDropTarget
* > (this), 0,
100 static_cast < XDropTargetDropContext
* > (this), dropAction
,
101 locationX
, locationY
, sourceActions
, transferable
);
103 while (aIterator
.hasMoreElements())
105 // FIXME: this can be simplified as soon as the Iterator has a remove method
106 Reference
< XInterface
> xElement( aIterator
.next() );
110 // this may result in a runtime exception
111 Reference
< XDropTargetListener
> xListener( xElement
, UNO_QUERY
);
115 // fire drop until the first one has accepted
116 if( m_xDropTargetDropContext
.is() )
117 xListener
->drop( aEvent
);
120 DropTargetEvent
aDTEvent( static_cast < XDropTarget
* > (this), 0 );
121 xListener
->dragExit( aDTEvent
);
127 catch (const RuntimeException
&)
129 pContainer
->removeInterface( xElement
);
133 // if context still valid, then reject drop
134 if( m_xDropTargetDropContext
.is() )
136 m_xDropTargetDropContext
.clear();
140 context
->rejectDrop();
142 catch (const RuntimeException
&)
151 sal_uInt32
DNDListenerContainer::fireDragExitEvent()
155 // fire DropTargetDropEvent on all XDropTargetListeners
156 OInterfaceContainerHelper
*pContainer
= rBHelper
.getContainer( cppu::UnoType
<XDropTargetListener
>::get());
158 if( pContainer
&& m_bActive
)
160 OInterfaceIteratorHelper
aIterator( *pContainer
);
162 // do not construct the event before you are sure at least one listener is registered
163 DropTargetEvent
aEvent( static_cast < XDropTarget
* > (this), 0 );
165 while (aIterator
.hasMoreElements())
167 // FIXME: this can be simplified as soon as the Iterator has a remove method
168 Reference
< XInterface
> xElement( aIterator
.next() );
172 // this may result in a runtime exception
173 Reference
< XDropTargetListener
> xListener( xElement
, UNO_QUERY
);
177 xListener
->dragExit( aEvent
);
181 catch (const RuntimeException
&)
183 pContainer
->removeInterface( xElement
);
191 sal_uInt32
DNDListenerContainer::fireDragOverEvent( const Reference
< XDropTargetDragContext
>& context
,
192 sal_Int8 dropAction
, sal_Int32 locationX
, sal_Int32 locationY
, sal_Int8 sourceActions
)
196 // fire DropTargetDropEvent on all XDropTargetListeners
197 OInterfaceContainerHelper
*pContainer
= rBHelper
.getContainer( cppu::UnoType
<XDropTargetListener
>::get());
199 if( pContainer
&& m_bActive
)
201 OInterfaceIteratorHelper
aIterator( *pContainer
);
203 // remember context to use in own context methods
204 m_xDropTargetDragContext
= context
;
206 // do not construct the event before you are sure at least one listener is registered
207 DropTargetDragEvent
aEvent( static_cast < XDropTarget
* > (this), 0,
208 static_cast < XDropTargetDragContext
* > (this),
209 dropAction
, locationX
, locationY
, sourceActions
);
211 while (aIterator
.hasMoreElements())
213 // FIXME: this can be simplified as soon as the Iterator has a remove method
214 Reference
< XInterface
> xElement( aIterator
.next() );
218 // this may result in a runtime exception
219 Reference
< XDropTargetListener
> xListener( xElement
, UNO_QUERY
);
223 if( m_xDropTargetDragContext
.is() )
224 xListener
->dragOver( aEvent
);
228 catch (const RuntimeException
&)
230 pContainer
->removeInterface( xElement
);
234 // if context still valid, then reject drag
235 if( m_xDropTargetDragContext
.is() )
237 m_xDropTargetDragContext
.clear();
241 context
->rejectDrag();
243 catch (const RuntimeException
&)
252 sal_uInt32
DNDListenerContainer::fireDragEnterEvent( const Reference
< XDropTargetDragContext
>& context
,
253 sal_Int8 dropAction
, sal_Int32 locationX
, sal_Int32 locationY
, sal_Int8 sourceActions
,
254 const Sequence
< DataFlavor
>& dataFlavors
)
258 // fire DropTargetDropEvent on all XDropTargetListeners
259 OInterfaceContainerHelper
*pContainer
= rBHelper
.getContainer( cppu::UnoType
<XDropTargetListener
>::get());
261 if( pContainer
&& m_bActive
)
263 OInterfaceIteratorHelper
aIterator( *pContainer
);
265 // remember context to use in own context methods
266 m_xDropTargetDragContext
= context
;
268 // do not construct the event before you are sure at least one listener is registered
269 DropTargetDragEnterEvent
aEvent( static_cast < XDropTarget
* > (this), 0,
270 static_cast < XDropTargetDragContext
* > (this),
271 dropAction
, locationX
, locationY
, sourceActions
, dataFlavors
);
273 while (aIterator
.hasMoreElements())
275 // FIXME: this can be simplified as soon as the Iterator has a remove method
276 Reference
< XInterface
> xElement( aIterator
.next() );
280 // this may result in a runtime exception
281 Reference
< XDropTargetListener
> xListener( xElement
, UNO_QUERY
);
285 if( m_xDropTargetDragContext
.is() )
286 xListener
->dragEnter( aEvent
);
290 catch (const RuntimeException
&)
292 pContainer
->removeInterface( xElement
);
296 // if context still valid, then reject drag
297 if( m_xDropTargetDragContext
.is() )
299 m_xDropTargetDragContext
.clear();
303 context
->rejectDrag();
305 catch (const RuntimeException
&)
314 sal_uInt32
DNDListenerContainer::fireDropActionChangedEvent( const Reference
< XDropTargetDragContext
>& context
,
315 sal_Int8 dropAction
, sal_Int32 locationX
, sal_Int32 locationY
, sal_Int8 sourceActions
)
319 // fire DropTargetDropEvent on all XDropTargetListeners
320 OInterfaceContainerHelper
*pContainer
= rBHelper
.getContainer( cppu::UnoType
<XDropTargetListener
>::get());
322 if( pContainer
&& m_bActive
)
324 OInterfaceIteratorHelper
aIterator( *pContainer
);
326 // remember context to use in own context methods
327 m_xDropTargetDragContext
= context
;
329 // do not construct the event before you are sure at least one listener is registered
330 DropTargetDragEvent
aEvent( static_cast < XDropTarget
* > (this), 0,
331 static_cast < XDropTargetDragContext
* > (this),
332 dropAction
, locationX
, locationY
, sourceActions
);
334 while (aIterator
.hasMoreElements())
336 // FIXME: this can be simplified as soon as the Iterator has a remove method
337 Reference
< XInterface
> xElement( aIterator
.next() );
341 // this may result in a runtime exception
342 Reference
< XDropTargetListener
> xListener( xElement
, UNO_QUERY
);
346 if( m_xDropTargetDragContext
.is() )
347 xListener
->dropActionChanged( aEvent
);
351 catch (const RuntimeException
&)
353 pContainer
->removeInterface( xElement
);
357 // if context still valid, then reject drag
358 if( m_xDropTargetDragContext
.is() )
360 m_xDropTargetDragContext
.clear();
364 context
->rejectDrag();
366 catch (const RuntimeException
&)
375 sal_uInt32
DNDListenerContainer::fireDragGestureEvent( sal_Int8 dragAction
, sal_Int32 dragOriginX
,
376 sal_Int32 dragOriginY
, const Reference
< XDragSource
>& dragSource
, const Any
& triggerEvent
)
380 // fire DropTargetDropEvent on all XDropTargetListeners
381 OInterfaceContainerHelper
*pContainer
= rBHelper
.getContainer( cppu::UnoType
<XDragGestureListener
>::get());
385 OInterfaceIteratorHelper
aIterator( *pContainer
);
387 // do not construct the event before you are sure at least one listener is registered
388 DragGestureEvent
aEvent( static_cast < XDragGestureRecognizer
* > (this), dragAction
,
389 dragOriginX
, dragOriginY
, dragSource
, triggerEvent
);
391 while( aIterator
.hasMoreElements() )
393 // FIXME: this can be simplified as soon as the Iterator has a remove method
394 Reference
< XInterface
> xElement( aIterator
.next() );
398 // this may result in a runtime exception
399 Reference
< XDragGestureListener
> xListener( xElement
, UNO_QUERY
);
403 xListener
->dragGestureRecognized( aEvent
);
407 catch (const RuntimeException
&)
409 pContainer
->removeInterface( xElement
);
417 void SAL_CALL
DNDListenerContainer::acceptDrag( sal_Int8 dragOperation
)
419 if( m_xDropTargetDragContext
.is() )
421 m_xDropTargetDragContext
->acceptDrag( dragOperation
);
422 m_xDropTargetDragContext
.clear();
426 void SAL_CALL
DNDListenerContainer::rejectDrag( )
428 // nothing to do here
431 void SAL_CALL
DNDListenerContainer::acceptDrop( sal_Int8 dropOperation
)
433 if( m_xDropTargetDropContext
.is() )
434 m_xDropTargetDropContext
->acceptDrop( dropOperation
);
437 void SAL_CALL
DNDListenerContainer::rejectDrop( )
439 // nothing to do here
442 void SAL_CALL
DNDListenerContainer::dropComplete( sal_Bool success
)
444 if( m_xDropTargetDropContext
.is() )
446 m_xDropTargetDropContext
->dropComplete( success
);
447 m_xDropTargetDropContext
.clear();
451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */