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>
31 #include "globals.hxx"
32 #include "sourcecontext.hxx"
33 #include "../../inc/DtObjFactory.hxx"
34 #include <rtl/ustring.h>
35 #include <osl/thread.h>
41 using namespace com::sun::star::datatransfer
;
42 using namespace com::sun::star::datatransfer::dnd
;
43 using namespace com::sun::star::datatransfer::dnd::DNDConstants
;
44 using namespace com::sun::star::uno
;
45 using namespace com::sun::star::awt::MouseButton
;
46 using namespace com::sun::star::awt
;
47 using namespace com::sun::star::lang
;
49 static unsigned __stdcall
DndOleSTAFunc(LPVOID pParams
);
51 DragSource::DragSource( const Reference
<XComponentContext
>& rxContext
):
52 WeakComponentImplHelper
< XDragSource
, XInitialization
, XServiceInfo
>(m_mutex
),
53 m_xContext( rxContext
),
54 // m_pcurrentContext_impl(0),
55 m_hAppWindow(nullptr),
57 m_RunningDndOperationCount(0)
61 DragSource::~DragSource()
65 /** First start a new drag and drop thread if
66 the last one has finished
69 Do we really need a separate thread for
70 every Dnd operation or only if the source
71 thread is an MTA thread
74 void DragSource::StartDragImpl(
75 const DragGestureEvent
& trigger
,
76 sal_Int8 sourceActions
,
79 const Reference
<XTransferable
>& trans
,
80 const Reference
<XDragSourceListener
>& listener
)
82 // The actions supported by the drag source
83 m_sourceActions
= sourceActions
;
84 // We need to know which mouse button triggered the operation.
85 // If it was the left one, then the drop occurs when that button
86 // has been released and if it was the right one then the drop
87 // occurs when the right button has been released. If the event is not
88 // set then we assume that the left button is pressed.
90 trigger
.Event
>>= evtMouse
;
91 m_MouseButton
= evtMouse
.Buttons
;
93 // The SourceContext class administers the XDragSourceListener s and
94 // fires events to them. An instance only exists in the scope of this
95 // function. However, the drag and drop operation causes callbacks
96 // to the IDropSource interface implemented in this class (but only
97 // while this function executes). The source context is also used
98 // in DragSource::QueryContinueDrag.
99 m_currentContext
= new SourceContext(this, listener
);
101 // Convert the XTransferable data object into an IDataObject object;
104 g_XTransferable
= trans
;
107 m_spDataObject
= CDTransObjFactory::createDataObjFromTransferable(
110 // Obtain the id of the thread that created the window
112 m_threadIdWindow
= GetWindowThreadProcessId( m_hAppWindow
, &processId
);
114 // hold the instance for the DnD thread, it's too late
115 // to acquire at the start of the thread procedure
116 // the thread procedure is responsible for the release
119 // The thread accesses members of this instance but does not call acquire.
120 // Hopefully this instance is not destroyed before the thread has terminated.
122 HANDLE hThread
= reinterpret_cast<HANDLE
>(_beginthreadex(
123 nullptr, 0, DndOleSTAFunc
, this, 0, &threadId
));
125 // detach from thread
126 CloseHandle(hThread
);
130 /** aArguments contains a machine id */
131 void SAL_CALL
DragSource::initialize( const Sequence
< Any
>& aArguments
)
133 if( aArguments
.getLength() >=2)
134 m_hAppWindow
= reinterpret_cast<HWND
>(static_cast<sal_uIntPtr
>(*o3tl::doAccess
<sal_uInt64
>(aArguments
[1])));
135 OSL_ASSERT( IsWindow( m_hAppWindow
) );
139 sal_Bool SAL_CALL
DragSource::isDragImageSupported( )
144 sal_Int32 SAL_CALL
DragSource::getDefaultCursor( sal_Int8
/*dragAction*/ )
149 /** Notifies the XDragSourceListener by
150 calling dragDropEnd */
151 void SAL_CALL
DragSource::startDrag(
152 const DragGestureEvent
& trigger
,
153 sal_Int8 sourceActions
,
156 const Reference
<XTransferable
>& trans
,
157 const Reference
<XDragSourceListener
>& listener
)
159 // Allow only one running dnd operation at a time,
160 // see XDragSource documentation
162 long cnt
= InterlockedIncrement(&m_RunningDndOperationCount
);
166 StartDragImpl(trigger
, sourceActions
, cursor
, image
, trans
, listener
);
170 cnt
= InterlockedDecrement(&m_RunningDndOperationCount
);
172 DragSourceDropEvent dsde
;
174 dsde
.DropAction
= ACTION_NONE
;
175 dsde
.DropSuccess
= false;
179 listener
->dragDropEnd(dsde
);
181 catch(RuntimeException
&)
183 OSL_FAIL("Runtime exception during event dispatching");
189 HRESULT STDMETHODCALLTYPE
DragSource::QueryInterface( REFIID riid
, void **ppvObject
)
195 if( riid
== __uuidof( IUnknown
) )
196 *ppvObject
= static_cast<IUnknown
*>( this);
197 else if ( riid
== __uuidof( IDropSource
) )
198 *ppvObject
= static_cast<IDropSource
*>( this);
206 return E_NOINTERFACE
;
210 ULONG STDMETHODCALLTYPE
DragSource::AddRef()
213 return static_cast<ULONG
>(m_refCount
);
216 ULONG STDMETHODCALLTYPE
DragSource::Release()
218 ULONG ref
= m_refCount
;
224 HRESULT STDMETHODCALLTYPE
DragSource::QueryContinueDrag(
225 /* [in] */ BOOL fEscapePressed
,
226 /* [in] */ DWORD grfKeyState
)
228 #if defined DBG_CONSOLE_OUT
229 printf("\nDragSource::QueryContinueDrag");
232 HRESULT retVal
= S_OK
; // default continue DnD
236 retVal
= DRAGDROP_S_CANCEL
;
240 if( ( m_MouseButton
== MouseButton::RIGHT
&& !(grfKeyState
& MK_RBUTTON
) ) ||
241 ( m_MouseButton
== MouseButton::MIDDLE
&& !(grfKeyState
& MK_MBUTTON
) ) ||
242 ( m_MouseButton
== MouseButton::LEFT
&& !(grfKeyState
& MK_LBUTTON
) ) ||
243 ( m_MouseButton
== 0 && !(grfKeyState
& MK_LBUTTON
) ) )
245 retVal
= DRAGDROP_S_DROP
;
249 // fire dropActionChanged event.
250 // this is actually done by the context, which also detects whether the action
252 sal_Int8 dropAction
= fEscapePressed
? ACTION_NONE
:
253 dndOleKeysToAction( grfKeyState
, m_sourceActions
);
255 sal_Int8 userAction
= fEscapePressed
? ACTION_NONE
:
256 dndOleKeysToAction( grfKeyState
, -1 );
258 static_cast<SourceContext
*>(m_currentContext
.get())->fire_dropActionChanged(
259 dropAction
, userAction
);
264 HRESULT STDMETHODCALLTYPE
DragSource::GiveFeedback(
266 #if defined DBG_CONSOLE_OUT
271 #if defined DBG_CONSOLE_OUT
272 printf("\nDragSource::GiveFeedback %d", dwEffect
);
275 return DRAGDROP_S_USEDEFAULTCURSORS
;
279 OUString SAL_CALL
DragSource::getImplementationName( )
281 return DNDSOURCE_IMPL_NAME
;
284 sal_Bool SAL_CALL
DragSource::supportsService( const OUString
& ServiceName
)
286 return cppu::supportsService(this, ServiceName
);
289 Sequence
< OUString
> SAL_CALL
DragSource::getSupportedServiceNames( )
291 return { DNDSOURCE_SERVICE_NAME
};
294 /** This function is called as extra thread from
295 DragSource::executeDrag. The function
296 carries out a drag and drop operation by calling
297 DoDragDrop. The thread also notifies all
299 unsigned __stdcall
DndOleSTAFunc(LPVOID pParams
)
301 osl_setThreadName("DragSource DndOleSTAFunc");
303 // The structure contains all arguments for DoDragDrop and other
304 DragSource
*pSource
= static_cast<DragSource
*>(pParams
);
306 // Drag and drop only works in a thread in which OleInitialize is called.
307 HRESULT hr
= OleInitialize( nullptr);
311 // We force the creation of a thread message queue. This is necessary
312 // for a later call to AttachThreadInput
314 PeekMessageW( &msgtemp
, nullptr, WM_USER
, WM_USER
, PM_NOREMOVE
);
316 DWORD threadId
= GetCurrentThreadId();
318 // This thread is attached to the thread that created the window. Hence
319 // this thread also receives all mouse and keyboard messages which are
320 // needed by DoDragDrop
321 AttachThreadInput( threadId
, pSource
->m_threadIdWindow
, TRUE
);
325 pSource
->m_spDataObject
.get(),
326 static_cast<IDropSource
*>(pSource
),
327 dndActionsToDropEffects( pSource
->m_sourceActions
),
330 // #105428 detach my message queue from the other threads
331 // message queue before calling fire_dragDropEnd else
332 // the office may appear to hang sometimes
333 AttachThreadInput( threadId
, pSource
->m_threadIdWindow
, FALSE
);
336 // clear the global transferable again
337 g_XTransferable
.clear();
340 OSL_ENSURE( hr
!= E_INVALIDARG
, "IDataObject impl does not contain valid data");
343 sal_Int8 action
= hr
== DRAGDROP_S_DROP
? dndOleDropEffectsToActions( dwEffect
) : ACTION_NONE
;
345 static_cast<SourceContext
*>(pSource
->m_currentContext
.get())->fire_dragDropEnd(
346 hr
== DRAGDROP_S_DROP
, action
);
348 // Destroy SourceContextslkfgj
349 pSource
->m_currentContext
= nullptr;
350 // Destroy the XTransferable wrapper
351 pSource
->m_spDataObject
=nullptr;
356 InterlockedDecrement(&pSource
->m_RunningDndOperationCount
);
358 // the DragSource was manually acquired by
359 // thread starting method DelayedStartDrag
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */