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>
25 #include <o3tl/any.hxx>
26 #include <comphelper/diagnose_ex.hxx>
31 #include <win/dnd_source.hxx>
32 #include "globals.hxx"
33 #include "sourcecontext.hxx"
34 #include "DtObjFactory.hxx"
36 #include <rtl/ustring.h>
37 #include <osl/thread.h>
43 using namespace com::sun::star::datatransfer
;
44 using namespace com::sun::star::datatransfer::dnd
;
45 using namespace com::sun::star::datatransfer::dnd::DNDConstants
;
46 using namespace com::sun::star::uno
;
47 using namespace com::sun::star::awt::MouseButton
;
48 using namespace com::sun::star::awt
;
49 using namespace com::sun::star::lang
;
51 static unsigned __stdcall
DndOleSTAFunc(void* pParams
);
53 DragSource::DragSource( const Reference
<XComponentContext
>& rxContext
):
54 WeakComponentImplHelper
< XDragSource
, XInitialization
, XServiceInfo
>(m_aMutex
),
55 m_xContext( rxContext
),
56 // m_pcurrentContext_impl(0),
57 m_hAppWindow(nullptr),
59 m_RunningDndOperationCount(0)
63 DragSource::~DragSource()
67 /** First start a new drag and drop thread if
68 the last one has finished
71 Do we really need a separate thread for
72 every Dnd operation or only if the source
73 thread is an MTA thread
76 void DragSource::StartDragImpl(
77 const DragGestureEvent
& trigger
,
78 sal_Int8 sourceActions
,
81 const Reference
<XTransferable
>& trans
,
82 const Reference
<XDragSourceListener
>& listener
)
84 // The actions supported by the drag source
85 m_sourceActions
= sourceActions
;
86 // We need to know which mouse button triggered the operation.
87 // If it was the left one, then the drop occurs when that button
88 // has been released and if it was the right one then the drop
89 // occurs when the right button has been released. If the event is not
90 // set then we assume that the left button is pressed.
92 trigger
.Event
>>= evtMouse
;
93 m_MouseButton
= evtMouse
.Buttons
;
95 // The SourceContext class administers the XDragSourceListener s and
96 // fires events to them. An instance only exists in the scope of this
97 // function. However, the drag and drop operation causes callbacks
98 // to the IDropSource interface implemented in this class (but only
99 // while this function executes). The source context is also used
100 // in DragSource::QueryContinueDrag.
101 m_currentContext
= new SourceContext(this, listener
);
103 // Convert the XTransferable data object into an IDataObject object;
106 g_XTransferable
= trans
;
109 m_spDataObject
= CDTransObjFactory::createDataObjFromTransferable(
112 // Obtain the id of the thread that created the window
114 m_threadIdWindow
= GetWindowThreadProcessId( m_hAppWindow
, &processId
);
116 // hold the instance for the DnD thread, it's too late
117 // to acquire at the start of the thread procedure
118 // the thread procedure is responsible for the release
121 // The thread accesses members of this instance but does not call acquire.
122 // Hopefully this instance is not destroyed before the thread has terminated.
124 = reinterpret_cast<HANDLE
>(_beginthreadex(nullptr, 0, DndOleSTAFunc
, this, 0, nullptr));
126 // detach from thread
127 CloseHandle(hThread
);
131 /** aArguments contains a machine id */
132 void SAL_CALL
DragSource::initialize( const Sequence
< Any
>& aArguments
)
134 if( aArguments
.getLength() >=2)
135 m_hAppWindow
= reinterpret_cast<HWND
>(static_cast<sal_uIntPtr
>(*o3tl::doAccess
<sal_uInt64
>(aArguments
[1])));
136 OSL_ASSERT( IsWindow( m_hAppWindow
) );
140 sal_Bool SAL_CALL
DragSource::isDragImageSupported( )
145 sal_Int32 SAL_CALL
DragSource::getDefaultCursor( sal_Int8
/*dragAction*/ )
150 /** Notifies the XDragSourceListener by
151 calling dragDropEnd */
152 void SAL_CALL
DragSource::startDrag(
153 const DragGestureEvent
& trigger
,
154 sal_Int8 sourceActions
,
157 const Reference
<XTransferable
>& trans
,
158 const Reference
<XDragSourceListener
>& listener
)
160 // Allow only one running dnd operation at a time,
161 // see XDragSource documentation
163 LONG cnt
= InterlockedIncrement(&m_RunningDndOperationCount
);
167 StartDragImpl(trigger
, sourceActions
, cursor
, image
, trans
, listener
);
171 cnt
= InterlockedDecrement(&m_RunningDndOperationCount
);
173 DragSourceDropEvent dsde
;
175 dsde
.DropAction
= ACTION_NONE
;
176 dsde
.DropSuccess
= false;
180 listener
->dragDropEnd(dsde
);
182 catch(RuntimeException
&)
184 TOOLS_WARN_EXCEPTION( "vcl", "Runtime exception during event dispatching");
190 HRESULT STDMETHODCALLTYPE
DragSource::QueryInterface( REFIID riid
, void **ppvObject
)
196 if( riid
== __uuidof( IUnknown
) )
197 *ppvObject
= static_cast<IUnknown
*>( this);
198 else if ( riid
== __uuidof( IDropSource
) )
199 *ppvObject
= static_cast<IDropSource
*>( this);
207 return E_NOINTERFACE
;
211 ULONG STDMETHODCALLTYPE
DragSource::AddRef()
214 return static_cast<ULONG
>(m_refCount
);
217 ULONG STDMETHODCALLTYPE
DragSource::Release()
219 ULONG ref
= m_refCount
;
225 HRESULT STDMETHODCALLTYPE
DragSource::QueryContinueDrag(
226 /* [in] */ BOOL fEscapePressed
,
227 /* [in] */ DWORD grfKeyState
)
229 #if defined DBG_CONSOLE_OUT
230 printf("\nDragSource::QueryContinueDrag");
233 HRESULT retVal
= S_OK
; // default continue DnD
237 retVal
= DRAGDROP_S_CANCEL
;
241 if( ( m_MouseButton
== MouseButton::RIGHT
&& !(grfKeyState
& MK_RBUTTON
) ) ||
242 ( m_MouseButton
== MouseButton::MIDDLE
&& !(grfKeyState
& MK_MBUTTON
) ) ||
243 ( m_MouseButton
== MouseButton::LEFT
&& !(grfKeyState
& MK_LBUTTON
) ) ||
244 ( m_MouseButton
== 0 && !(grfKeyState
& MK_LBUTTON
) ) )
246 retVal
= DRAGDROP_S_DROP
;
250 // fire dropActionChanged event.
251 // this is actually done by the context, which also detects whether the action
253 sal_Int8 dropAction
= fEscapePressed
? ACTION_NONE
:
254 dndOleKeysToAction( grfKeyState
, m_sourceActions
);
256 sal_Int8 userAction
= fEscapePressed
? ACTION_NONE
:
257 dndOleKeysToAction( grfKeyState
, -1 );
259 static_cast<SourceContext
*>(m_currentContext
.get())->fire_dropActionChanged(
260 dropAction
, userAction
);
265 HRESULT STDMETHODCALLTYPE
DragSource::GiveFeedback(
267 #if defined DBG_CONSOLE_OUT
272 #if defined DBG_CONSOLE_OUT
273 printf("\nDragSource::GiveFeedback %d", dwEffect
);
276 return DRAGDROP_S_USEDEFAULTCURSORS
;
280 OUString SAL_CALL
DragSource::getImplementationName( )
282 return "com.sun.star.comp.datatransfer.dnd.OleDragSource_V1";
285 sal_Bool SAL_CALL
DragSource::supportsService( const OUString
& ServiceName
)
287 return cppu::supportsService(this, ServiceName
);
290 Sequence
< OUString
> SAL_CALL
DragSource::getSupportedServiceNames( )
292 return { "com.sun.star.datatransfer.dnd.OleDragSource" };
295 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
296 dtrans_DragSource_get_implementation(
297 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
299 return cppu::acquire(new DragSource(context
));
302 /** This function is called as extra thread from
303 DragSource::executeDrag. The function
304 carries out a drag and drop operation by calling
305 DoDragDrop. The thread also notifies all
307 unsigned __stdcall
DndOleSTAFunc(void* pParams
)
309 osl_setThreadName("DragSource DndOleSTAFunc");
311 // The structure contains all arguments for DoDragDrop and other
312 DragSource
*pSource
= static_cast<DragSource
*>(pParams
);
314 // Drag and drop only works in a thread in which OleInitialize is called.
315 HRESULT hr
= OleInitialize( nullptr);
319 // We force the creation of a thread message queue. This is necessary
320 // for a later call to AttachThreadInput
322 PeekMessageW( &msgtemp
, nullptr, WM_USER
, WM_USER
, PM_NOREMOVE
);
324 DWORD threadId
= GetCurrentThreadId();
326 // This thread is attached to the thread that created the window. Hence
327 // this thread also receives all mouse and keyboard messages which are
328 // needed by DoDragDrop
329 AttachThreadInput( threadId
, pSource
->m_threadIdWindow
, TRUE
);
333 pSource
->m_spDataObject
.get(),
334 static_cast<IDropSource
*>(pSource
),
335 dndActionsToDropEffects( pSource
->m_sourceActions
),
338 // #105428 detach my message queue from the other threads
339 // message queue before calling fire_dragDropEnd else
340 // the office may appear to hang sometimes
341 AttachThreadInput( threadId
, pSource
->m_threadIdWindow
, FALSE
);
344 // clear the global transferable again
345 g_XTransferable
.clear();
348 OSL_ENSURE( hr
!= E_INVALIDARG
, "IDataObject impl does not contain valid data");
351 sal_Int8 action
= hr
== DRAGDROP_S_DROP
? dndOleDropEffectsToActions( dwEffect
) : ACTION_NONE
;
353 static_cast<SourceContext
*>(pSource
->m_currentContext
.get())->fire_dragDropEnd(
354 hr
== DRAGDROP_S_DROP
, action
);
356 // Destroy SourceContextslkfgj
357 pSource
->m_currentContext
= nullptr;
358 // Destroy the XTransferable wrapper
359 pSource
->m_spDataObject
=nullptr;
364 InterlockedDecrement(&pSource
->m_RunningDndOperationCount
);
366 // the DragSource was manually acquired by
367 // thread starting method DelayedStartDrag
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */