Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / dtrans / source / win32 / dnd / source.cxx
blobc797684d233ce734f5bca46f306d2c8251a36f7b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
27 #include <process.h>
28 #include <memory>
30 #include "source.hxx"
31 #include "globals.hxx"
32 #include "sourcecontext.hxx"
33 #include "../../inc/DtObjFactory.hxx"
34 #include <rtl/ustring.h>
35 #include <osl/thread.h>
36 #include <winuser.h>
37 #include <stdio.h>
39 using namespace cppu;
40 using namespace osl;
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),
56 m_MouseButton(0),
57 m_RunningDndOperationCount(0)
61 DragSource::~DragSource()
65 /** First start a new drag and drop thread if
66 the last one has finished
68 ????
69 Do we really need a separate thread for
70 every Dnd operation or only if the source
71 thread is an MTA thread
72 ????
74 void DragSource::StartDragImpl(
75 const DragGestureEvent& trigger,
76 sal_Int8 sourceActions,
77 sal_Int32 /*cursor*/,
78 sal_Int32 /*image*/,
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.
89 MouseEvent evtMouse;
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;
103 //--> TRA
104 g_XTransferable = trans;
105 //<-- TRA
107 m_spDataObject= CDTransObjFactory::createDataObjFromTransferable(
108 m_xContext, trans);
110 // Obtain the id of the thread that created the window
111 DWORD processId;
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
117 acquire();
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.
121 unsigned threadId;
122 HANDLE hThread= reinterpret_cast<HANDLE>(_beginthreadex(
123 nullptr, 0, DndOleSTAFunc, this, 0, &threadId));
125 // detach from thread
126 CloseHandle(hThread);
129 // XInitialization
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) );
138 /** XDragSource */
139 sal_Bool SAL_CALL DragSource::isDragImageSupported( )
141 return false;
144 sal_Int32 SAL_CALL DragSource::getDefaultCursor( sal_Int8 /*dragAction*/ )
146 return 0;
149 /** Notifies the XDragSourceListener by
150 calling dragDropEnd */
151 void SAL_CALL DragSource::startDrag(
152 const DragGestureEvent& trigger,
153 sal_Int8 sourceActions,
154 sal_Int32 cursor,
155 sal_Int32 image,
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);
164 if (1 == cnt)
166 StartDragImpl(trigger, sourceActions, cursor, image, trans, listener);
168 else
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");
188 /** IDropTarget */
189 HRESULT STDMETHODCALLTYPE DragSource::QueryInterface( REFIID riid, void **ppvObject)
191 if( !ppvObject)
192 return E_POINTER;
193 *ppvObject= nullptr;
195 if( riid == __uuidof( IUnknown) )
196 *ppvObject= static_cast<IUnknown*>( this);
197 else if ( riid == __uuidof( IDropSource) )
198 *ppvObject= static_cast<IDropSource*>( this);
200 if(*ppvObject)
202 AddRef();
203 return S_OK;
205 else
206 return E_NOINTERFACE;
210 ULONG STDMETHODCALLTYPE DragSource::AddRef()
212 acquire();
213 return static_cast<ULONG>(m_refCount);
216 ULONG STDMETHODCALLTYPE DragSource::Release()
218 ULONG ref= m_refCount;
219 release();
220 return --ref;
223 /** IDropSource */
224 HRESULT STDMETHODCALLTYPE DragSource::QueryContinueDrag(
225 /* [in] */ BOOL fEscapePressed,
226 /* [in] */ DWORD grfKeyState)
228 #if defined DBG_CONSOLE_OUT
229 printf("\nDragSource::QueryContinueDrag");
230 #endif
232 HRESULT retVal= S_OK; // default continue DnD
234 if (fEscapePressed)
236 retVal= DRAGDROP_S_CANCEL;
238 else
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
251 // changed at all
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);
261 return retVal;
264 HRESULT STDMETHODCALLTYPE DragSource::GiveFeedback(
265 /* [in] */ DWORD
266 #if defined DBG_CONSOLE_OUT
267 dwEffect
268 #endif
271 #if defined DBG_CONSOLE_OUT
272 printf("\nDragSource::GiveFeedback %d", dwEffect);
273 #endif
275 return DRAGDROP_S_USEDEFAULTCURSORS;
278 // XServiceInfo
279 OUString SAL_CALL DragSource::getImplementationName( )
281 return DNDSOURCE_IMPL_NAME;
283 // XServiceInfo
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
298 XSourceListener. */
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);
309 if(SUCCEEDED(hr))
311 // We force the creation of a thread message queue. This is necessary
312 // for a later call to AttachThreadInput
313 MSG msgtemp;
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 );
323 DWORD dwEffect= 0;
324 hr= DoDragDrop(
325 pSource->m_spDataObject.get(),
326 static_cast<IDropSource*>(pSource),
327 dndActionsToDropEffects( pSource->m_sourceActions),
328 &dwEffect);
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);
335 //--> TRA
336 // clear the global transferable again
337 g_XTransferable.clear();
338 //<-- TRA
340 OSL_ENSURE( hr != E_INVALIDARG, "IDataObject impl does not contain valid data");
342 //Fire event
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;
353 OleUninitialize();
356 InterlockedDecrement(&pSource->m_RunningDndOperationCount);
358 // the DragSource was manually acquired by
359 // thread starting method DelayedStartDrag
360 pSource->release();
362 return 0;
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */