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 <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
21 #include <com/sun/star/datatransfer/XTransferable.hpp>
22 #include <com/sun/star/awt/MouseButton.hpp>
23 #include <com/sun/star/awt/MouseEvent.hpp>
24 #include <cppuhelper/supportsservice.hxx>
29 #include "globals.hxx"
30 #include "sourcecontext.hxx"
31 #include "../../inc/DtObjFactory.hxx"
32 #include <rtl/ustring.h>
33 #include <osl/thread.h>
41 #define __uuidof(I) IID_##I
46 using namespace com::sun::star::datatransfer
;
47 using namespace com::sun::star::datatransfer::dnd
;
48 using namespace com::sun::star::datatransfer::dnd::DNDConstants
;
49 using namespace com::sun::star::uno
;
50 using namespace com::sun::star::awt::MouseButton
;
51 using namespace com::sun::star::awt
;
52 using namespace com::sun::star::lang
;
54 extern Reference
< XTransferable
> g_XTransferable
;
56 unsigned __stdcall
DndOleSTAFunc(LPVOID pParams
);
58 DragSource::DragSource( const Reference
<XComponentContext
>& rxContext
):
59 WeakComponentImplHelper3
< XDragSource
, XInitialization
, XServiceInfo
>(m_mutex
),
60 m_xContext( rxContext
),
61 // m_pcurrentContext_impl(0),
64 m_RunningDndOperationCount(0)
68 DragSource::~DragSource()
72 /** First start a new drag and drop thread if
73 the last one has finished
76 Do we really need a separate thread for
77 every Dnd opeartion or only if the source
78 thread is an MTA thread
81 void DragSource::StartDragImpl(
82 const DragGestureEvent
& trigger
,
83 sal_Int8 sourceActions
,
86 const Reference
<XTransferable
>& trans
,
87 const Reference
<XDragSourceListener
>& listener
)
89 // The actions supported by the drag source
90 m_sourceActions
= sourceActions
;
91 // We need to know which mouse button triggered the operation.
92 // If it was the left one, then the drop occurs when that button
93 // has been released and if it was the right one then the drop
94 // occurs when the right button has been released. If the event is not
95 // set then we assume that the left button is pressed.
97 trigger
.Event
>>= evtMouse
;
98 m_MouseButton
= evtMouse
.Buttons
;
100 // The SourceContext class administers the XDragSourceListener s and
101 // fires events to them. An instance only exists in the scope of this
102 // function. However, the drag and drop operation causes callbacks
103 // to the IDropSource interface implemented in this class (but only
104 // while this function executes). The source context is also used
105 // in DragSource::QueryContinueDrag.
106 m_currentContext
= static_cast<XDragSourceContext
*>( new SourceContext(
107 static_cast<DragSource
*>(this), listener
) );
109 // Convert the XTransferable data object into an IDataObject object;
112 g_XTransferable
= trans
;
115 m_spDataObject
= m_aDataConverter
.createDataObjFromTransferable(
118 // Obtain the id of the thread that created the window
120 m_threadIdWindow
= GetWindowThreadProcessId( m_hAppWindow
, &processId
);
122 // hold the instance for the DnD thread, it's to late
123 // to acquire at the start of the thread procedure
124 // the thread procedure is responsible for the release
127 // The thread acccesses members of this instance but does not call acquire.
128 // Hopefully this instance is not destroyed before the thread has terminated.
130 HANDLE hThread
= reinterpret_cast<HANDLE
>(_beginthreadex(
131 0, 0, DndOleSTAFunc
, reinterpret_cast<void*>(this), 0, &threadId
));
133 // detach from thread
134 CloseHandle(hThread
);
138 /** aArguments contains a machine id */
139 void SAL_CALL
DragSource::initialize( const Sequence
< Any
>& aArguments
)
140 throw(Exception
, RuntimeException
)
142 if( aArguments
.getLength() >=2)
143 m_hAppWindow
= *(HWND
*)aArguments
[1].getValue();
144 OSL_ASSERT( IsWindow( m_hAppWindow
) );
148 sal_Bool SAL_CALL
DragSource::isDragImageSupported( )
149 throw(RuntimeException
)
154 sal_Int32 SAL_CALL
DragSource::getDefaultCursor( sal_Int8
/*dragAction*/ )
155 throw( IllegalArgumentException
, RuntimeException
)
160 /** Notifies the XDragSourceListener by
161 calling dragDropEnd */
162 void SAL_CALL
DragSource::startDrag(
163 const DragGestureEvent
& trigger
,
164 sal_Int8 sourceActions
,
167 const Reference
<XTransferable
>& trans
,
168 const Reference
<XDragSourceListener
>& listener
) throw( RuntimeException
)
170 // Allow only one running dnd operation at a time,
171 // see XDragSource documentation
173 long cnt
= InterlockedIncrement(&m_RunningDndOperationCount
);
177 StartDragImpl(trigger
, sourceActions
, cursor
, image
, trans
, listener
);
181 cnt
= InterlockedDecrement(&m_RunningDndOperationCount
);
183 DragSourceDropEvent dsde
;
185 dsde
.DropAction
= ACTION_NONE
;
186 dsde
.DropSuccess
= false;
190 listener
->dragDropEnd(dsde
);
192 catch(RuntimeException
&)
194 OSL_FAIL("Runtime exception during event dispatching");
200 HRESULT STDMETHODCALLTYPE
DragSource::QueryInterface( REFIID riid
, void **ppvObject
)
206 if( riid
== __uuidof( IUnknown
) )
207 *ppvObject
= static_cast<IUnknown
*>( this);
208 else if ( riid
== __uuidof( IDropSource
) )
209 *ppvObject
= static_cast<IDropSource
*>( this);
217 return E_NOINTERFACE
;
221 ULONG STDMETHODCALLTYPE
DragSource::AddRef()
224 return (ULONG
) m_refCount
;
227 ULONG STDMETHODCALLTYPE
DragSource::Release()
229 ULONG ref
= m_refCount
;
235 HRESULT STDMETHODCALLTYPE
DragSource::QueryContinueDrag(
236 /* [in] */ BOOL fEscapePressed
,
237 /* [in] */ DWORD grfKeyState
)
239 #if defined DBG_CONSOLE_OUT
240 printf("\nDragSource::QueryContinueDrag");
243 HRESULT retVal
= S_OK
; // default continue DnD
247 retVal
= DRAGDROP_S_CANCEL
;
251 if( ( m_MouseButton
== MouseButton::RIGHT
&& !(grfKeyState
& MK_RBUTTON
) ) ||
252 ( m_MouseButton
== MouseButton::MIDDLE
&& !(grfKeyState
& MK_MBUTTON
) ) ||
253 ( m_MouseButton
== MouseButton::LEFT
&& !(grfKeyState
& MK_LBUTTON
) ) ||
254 ( m_MouseButton
== 0 && !(grfKeyState
& MK_LBUTTON
) ) )
256 retVal
= DRAGDROP_S_DROP
;
260 // fire dropActionChanged event.
261 // this is actually done by the context, which also detects whether the action
263 sal_Int8 dropAction
= fEscapePressed
? ACTION_NONE
:
264 dndOleKeysToAction( grfKeyState
, m_sourceActions
);
266 sal_Int8 userAction
= fEscapePressed
? ACTION_NONE
:
267 dndOleKeysToAction( grfKeyState
, -1 );
269 static_cast<SourceContext
*>(m_currentContext
.get())->fire_dropActionChanged(
270 dropAction
, userAction
);
275 HRESULT STDMETHODCALLTYPE
DragSource::GiveFeedback(
277 #if defined DBG_CONSOLE_OUT
282 #if defined DBG_CONSOLE_OUT
283 printf("\nDragSource::GiveFeedback %d", dwEffect
);
286 return DRAGDROP_S_USEDEFAULTCURSORS
;
290 OUString SAL_CALL
DragSource::getImplementationName( ) throw (RuntimeException
)
292 return OUString(DNDSOURCE_IMPL_NAME
);
295 sal_Bool SAL_CALL
DragSource::supportsService( const OUString
& ServiceName
) throw (RuntimeException
)
297 return cppu::supportsService(this, ServiceName
);
300 Sequence
< OUString
> SAL_CALL
DragSource::getSupportedServiceNames( ) throw (RuntimeException
)
302 OUString names
[1]= {OUString(DNDSOURCE_SERVICE_NAME
)};
304 return Sequence
<OUString
>(names
, 1);
307 /** This function is called as extra thread from
308 DragSource::executeDrag. The function
309 carries out a drag and drop operation by calling
310 DoDragDrop. The thread also notifies all
312 unsigned __stdcall
DndOleSTAFunc(LPVOID pParams
)
314 osl_setThreadName("DragSource DndOleSTAFunc");
316 // The structure contains all arguments for DoDragDrop and other
317 DragSource
*pSource
= (DragSource
*)pParams
;
319 // Drag and drop only works in a thread in which OleInitialize is called.
320 HRESULT hr
= OleInitialize( NULL
);
324 // We force the creation of a thread message queue. This is necessary
325 // for a later call to AttachThreadInput
327 PeekMessage( &msgtemp
, NULL
, WM_USER
, WM_USER
, PM_NOREMOVE
);
329 DWORD threadId
= GetCurrentThreadId();
331 // This thread is attached to the thread that created the window. Hence
332 // this thread also receives all mouse and keyboard messages which are
333 // needed by DoDragDrop
334 AttachThreadInput( threadId
, pSource
->m_threadIdWindow
, TRUE
);
338 pSource
->m_spDataObject
.get(),
339 static_cast<IDropSource
*>(pSource
),
340 dndActionsToDropEffects( pSource
->m_sourceActions
),
343 // #105428 detach my message queue from the other threads
344 // message queue before calling fire_dragDropEnd else
345 // the office may appear to hang sometimes
346 AttachThreadInput( threadId
, pSource
->m_threadIdWindow
, FALSE
);
349 // clear the global transferable again
350 g_XTransferable
.clear();
353 OSL_ENSURE( hr
!= E_INVALIDARG
, "IDataObject impl does not contain valid data");
356 sal_Int8 action
= hr
== DRAGDROP_S_DROP
? dndOleDropEffectsToActions( dwEffect
) : ACTION_NONE
;
358 static_cast<SourceContext
*>(pSource
->m_currentContext
.get())->fire_dragDropEnd(
359 hr
== DRAGDROP_S_DROP
? sal_True
: sal_False
, action
);
361 // Destroy SourceContextslkfgj
362 pSource
->m_currentContext
= 0;
363 // Destroy the XTransferable wrapper
364 pSource
->m_spDataObject
=0;
369 InterlockedDecrement(&pSource
->m_RunningDndOperationCount
);
371 // the DragSource was manually acquired by
372 // thread starting method DelayedStartDrag
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */