LibreOffice Module dtrans (master)  1
source.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
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>
25 #include <o3tl/any.hxx>
26 
27 #include <process.h>
28 #include <memory>
29 
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>
38 
39 using namespace cppu;
40 using namespace osl;
41 using namespace com::sun::star::datatransfer;
42 using namespace com::sun::star::datatransfer::dnd;
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;
48 
49 static unsigned __stdcall DndOleSTAFunc(LPVOID pParams);
50 
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)
58 {
59 }
60 
62 {
63 }
64 
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 )
81 {
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;
92 
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);
100 
101  // Convert the XTransferable data object into an IDataObject object;
102 
103  //--> TRA
104  g_XTransferable = trans;
105  //<-- TRA
106 
108  m_xContext, trans);
109 
110  // Obtain the id of the thread that created the window
111  DWORD processId;
112  m_threadIdWindow= GetWindowThreadProcessId( m_hAppWindow, &processId);
113 
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();
118 
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));
124 
125  // detach from thread
126  CloseHandle(hThread);
127 }
128 
129 // XInitialization
131 void SAL_CALL DragSource::initialize( const Sequence< Any >& aArguments )
132 {
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) );
136 }
137 
140 {
141  return false;
142 }
143 
144 sal_Int32 SAL_CALL DragSource::getDefaultCursor( sal_Int8 /*dragAction*/ )
145 {
146  return 0;
147 }
148 
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 )
158 {
159  // Allow only one running dnd operation at a time,
160  // see XDragSource documentation
161 
162  long cnt = InterlockedIncrement(&m_RunningDndOperationCount);
163 
164  if (1 == cnt)
165  {
166  StartDragImpl(trigger, sourceActions, cursor, image, trans, listener);
167  }
168  else
169  {
170  cnt = InterlockedDecrement(&m_RunningDndOperationCount);
171 
172  DragSourceDropEvent dsde;
173 
174  dsde.DropAction = ACTION_NONE;
175  dsde.DropSuccess = false;
176 
177  try
178  {
179  listener->dragDropEnd(dsde);
180  }
181  catch(RuntimeException&)
182  {
183  OSL_FAIL("Runtime exception during event dispatching");
184  }
185  }
186 }
187 
189 HRESULT STDMETHODCALLTYPE DragSource::QueryInterface( REFIID riid, void **ppvObject)
190 {
191  if( !ppvObject)
192  return E_POINTER;
193  *ppvObject= nullptr;
194 
195  if( riid == __uuidof( IUnknown) )
196  *ppvObject= static_cast<IUnknown*>( this);
197  else if ( riid == __uuidof( IDropSource) )
198  *ppvObject= static_cast<IDropSource*>( this);
199 
200  if(*ppvObject)
201  {
202  AddRef();
203  return S_OK;
204  }
205  else
206  return E_NOINTERFACE;
207 
208 }
209 
210 ULONG STDMETHODCALLTYPE DragSource::AddRef()
211 {
212  acquire();
213  return static_cast<ULONG>(m_refCount);
214 }
215 
216 ULONG STDMETHODCALLTYPE DragSource::Release()
217 {
218  ULONG ref= m_refCount;
219  release();
220  return --ref;
221 }
222 
224 HRESULT STDMETHODCALLTYPE DragSource::QueryContinueDrag(
225 /* [in] */ BOOL fEscapePressed,
226 /* [in] */ DWORD grfKeyState)
227 {
228 #if defined DBG_CONSOLE_OUT
229  printf("\nDragSource::QueryContinueDrag");
230 #endif
231 
232  HRESULT retVal= S_OK; // default continue DnD
233 
234  if (fEscapePressed)
235  {
236  retVal= DRAGDROP_S_CANCEL;
237  }
238  else
239  {
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) ) )
244  {
245  retVal= DRAGDROP_S_DROP;
246  }
247  }
248 
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);
254 
255  sal_Int8 userAction= fEscapePressed ? ACTION_NONE :
256  dndOleKeysToAction( grfKeyState, -1 );
257 
258  static_cast<SourceContext*>(m_currentContext.get())->fire_dropActionChanged(
259  dropAction, userAction);
260 
261  return retVal;
262 }
263 
264 HRESULT STDMETHODCALLTYPE DragSource::GiveFeedback(
265 /* [in] */ DWORD
266 #if defined DBG_CONSOLE_OUT
267 dwEffect
268 #endif
269 )
270 {
271 #if defined DBG_CONSOLE_OUT
272  printf("\nDragSource::GiveFeedback %d", dwEffect);
273 #endif
274 
275  return DRAGDROP_S_USEDEFAULTCURSORS;
276 }
277 
278 // XServiceInfo
280 {
281  return DNDSOURCE_IMPL_NAME;
282 }
283 // XServiceInfo
284 sal_Bool SAL_CALL DragSource::supportsService( const OUString& ServiceName )
285 {
286  return cppu::supportsService(this, ServiceName);
287 }
288 
290 {
291  return { DNDSOURCE_SERVICE_NAME };
292 }
293 
299 unsigned __stdcall DndOleSTAFunc(LPVOID pParams)
300 {
301  osl_setThreadName("DragSource DndOleSTAFunc");
302 
303  // The structure contains all arguments for DoDragDrop and other
304  DragSource *pSource= static_cast<DragSource*>(pParams);
305 
306  // Drag and drop only works in a thread in which OleInitialize is called.
307  HRESULT hr= OleInitialize( nullptr);
308 
309  if(SUCCEEDED(hr))
310  {
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);
315 
316  DWORD threadId= GetCurrentThreadId();
317 
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 );
322 
323  DWORD dwEffect= 0;
324  hr= DoDragDrop(
325  pSource->m_spDataObject.get(),
326  static_cast<IDropSource*>(pSource),
328  &dwEffect);
329 
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);
334 
335  //--> TRA
336  // clear the global transferable again
337  g_XTransferable.clear();
338  //<-- TRA
339 
340  OSL_ENSURE( hr != E_INVALIDARG, "IDataObject impl does not contain valid data");
341 
342  //Fire event
343  sal_Int8 action= hr == DRAGDROP_S_DROP ? dndOleDropEffectsToActions( dwEffect) : ACTION_NONE;
344 
345  static_cast<SourceContext*>(pSource->m_currentContext.get())->fire_dragDropEnd(
346  hr == DRAGDROP_S_DROP, action);
347 
348  // Destroy SourceContextslkfgj
349  pSource->m_currentContext= nullptr;
350  // Destroy the XTransferable wrapper
351  pSource->m_spDataObject=nullptr;
352 
353  OleUninitialize();
354  }
355 
356  InterlockedDecrement(&pSource->m_RunningDndOperationCount);
357 
358  // the DragSource was manually acquired by
359  // thread starting method DelayedStartDrag
360  pSource->release();
361 
362  return 0;
363 }
364 
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_Bool SAL_CALL isDragImageSupported() override
XDragSource.
Definition: source.cxx:139
virtual sal_Int32 SAL_CALL getDefaultCursor(sal_Int8 dragAction) override
Definition: source.cxx:144
DWORD m_threadIdWindow
Definition: source.hxx:76
signed char sal_Int8
EXTERN_C BOOL BOOL const wchar_t *pProgramPath HRESULT hr
IDataObjectPtr m_spDataObject
Definition: source.hxx:81
DragSource(const Reference< XComponentContext > &rxContext)
Definition: source.cxx:51
sal_Int8 dndOleDropEffectsToActions(DWORD dwEffect)
Definition: globals.cxx:85
long m_RunningDndOperationCount
Definition: source.hxx:71
virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: source.cxx:289
const wchar_t *typedef BOOL
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
css::uno::Reference< css::datatransfer::XTransferable > g_XTransferable
Definition: globals.cxx:28
sal_Int8 m_sourceActions
Definition: source.hxx:83
Reference< XDragSourceContext > m_currentContext
Definition: source.hxx:78
virtual ULONG STDMETHODCALLTYPE Release() override
Definition: source.cxx:216
#define TRUE
void StartDragImpl(const DragGestureEvent &trigger, sal_Int8 sourceActions, sal_Int32 cursor, sal_Int32 image, const Reference< XTransferable > &trans, const Reference< XDragSourceListener > &listener)
First start a new drag and drop thread if the last one has finished.
Definition: source.cxx:74
DWORD dndActionsToDropEffects(sal_Int8 actions)
Definition: globals.cxx:98
unsigned char sal_Bool
static unsigned __stdcall DndOleSTAFunc(LPVOID pParams)
This function is called as extra thread from DragSource::executeDrag.
Definition: source.cxx:299
virtual ~DragSource() override
Definition: source.cxx:61
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject) override
IDropTarget.
Definition: source.cxx:189
sal_Int8 dndOleKeysToAction(DWORD grfKeyState, sal_Int8 nSourceActions)
Definition: globals.cxx:34
virtual HRESULT STDMETHODCALLTYPE QueryContinueDrag(BOOL fEscapePressed, DWORD grfKeyState) override
IDropSource.
Definition: source.cxx:224
IDataObjectPtr createDataObjFromTransferable(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::datatransfer::XTransferable > &refXTransferable)
virtual void SAL_CALL startDrag(const DragGestureEvent &trigger, sal_Int8 sourceActions, sal_Int32 cursor, sal_Int32 image, const Reference< XTransferable > &trans, const Reference< XDragSourceListener > &listener) override
Notifies the XDragSourceListener by calling dragDropEnd.
Definition: source.cxx:151
#define DNDSOURCE_IMPL_NAME
Definition: globals.hxx:35
virtual OUString SAL_CALL getImplementationName() override
Definition: source.cxx:279
#define FALSE
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: source.cxx:284
virtual void SAL_CALL initialize(const Sequence< Any > &aArguments) override
aArguments contains a machine id
Definition: source.cxx:131
virtual ULONG STDMETHODCALLTYPE AddRef() override
Definition: source.cxx:210
#define DNDSOURCE_SERVICE_NAME
Definition: globals.hxx:34
virtual HRESULT STDMETHODCALLTYPE GiveFeedback(DWORD dwEffect) override
Definition: source.cxx:264
Reference< XComponentContext > m_xContext
Definition: source.hxx:54
const uno::Reference< uno::XComponentContext > m_xContext
HWND m_hAppWindow
Definition: source.hxx:55
short m_MouseButton
Definition: source.hxx:58