tdf#164627 docx export: consolidate getWordCompatibilityMode()
[LibreOffice.git] / vcl / win / dtrans / source.cxx
blob94124b23dd5e39cb1fc3c6ce9556c8811f29deed
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>
26 #include <comphelper/diagnose_ex.hxx>
28 #include <process.h>
29 #include <memory>
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>
38 #include <winuser.h>
39 #include <stdio.h>
41 using namespace cppu;
42 using namespace osl;
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),
58 m_MouseButton(0),
59 m_RunningDndOperationCount(0)
63 DragSource::~DragSource()
67 /** First start a new drag and drop thread if
68 the last one has finished
70 ????
71 Do we really need a separate thread for
72 every Dnd operation or only if the source
73 thread is an MTA thread
74 ????
76 void DragSource::StartDragImpl(
77 const DragGestureEvent& trigger,
78 sal_Int8 sourceActions,
79 sal_Int32 /*cursor*/,
80 sal_Int32 /*image*/,
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.
91 MouseEvent evtMouse;
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;
105 //--> TRA
106 g_XTransferable = trans;
107 //<-- TRA
109 m_spDataObject= CDTransObjFactory::createDataObjFromTransferable(
110 m_xContext, trans);
112 // Obtain the id of the thread that created the window
113 DWORD processId;
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
119 acquire();
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.
123 HANDLE hThread
124 = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, DndOleSTAFunc, this, 0, nullptr));
126 // detach from thread
127 CloseHandle(hThread);
130 // XInitialization
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) );
139 /** XDragSource */
140 sal_Bool SAL_CALL DragSource::isDragImageSupported( )
142 return false;
145 sal_Int32 SAL_CALL DragSource::getDefaultCursor( sal_Int8 /*dragAction*/ )
147 return 0;
150 /** Notifies the XDragSourceListener by
151 calling dragDropEnd */
152 void SAL_CALL DragSource::startDrag(
153 const DragGestureEvent& trigger,
154 sal_Int8 sourceActions,
155 sal_Int32 cursor,
156 sal_Int32 image,
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);
165 if (1 == cnt)
167 StartDragImpl(trigger, sourceActions, cursor, image, trans, listener);
169 else
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");
189 /** IDropTarget */
190 HRESULT STDMETHODCALLTYPE DragSource::QueryInterface( REFIID riid, void **ppvObject)
192 if( !ppvObject)
193 return E_POINTER;
194 *ppvObject= nullptr;
196 if( riid == __uuidof( IUnknown) )
197 *ppvObject= static_cast<IUnknown*>( this);
198 else if ( riid == __uuidof( IDropSource) )
199 *ppvObject= static_cast<IDropSource*>( this);
201 if(*ppvObject)
203 AddRef();
204 return S_OK;
206 else
207 return E_NOINTERFACE;
211 ULONG STDMETHODCALLTYPE DragSource::AddRef()
213 acquire();
214 return static_cast<ULONG>(m_refCount);
217 ULONG STDMETHODCALLTYPE DragSource::Release()
219 ULONG ref= m_refCount;
220 release();
221 return --ref;
224 /** IDropSource */
225 HRESULT STDMETHODCALLTYPE DragSource::QueryContinueDrag(
226 /* [in] */ BOOL fEscapePressed,
227 /* [in] */ DWORD grfKeyState)
229 #if defined DBG_CONSOLE_OUT
230 printf("\nDragSource::QueryContinueDrag");
231 #endif
233 HRESULT retVal= S_OK; // default continue DnD
235 if (fEscapePressed)
237 retVal= DRAGDROP_S_CANCEL;
239 else
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
252 // changed at all
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);
262 return retVal;
265 HRESULT STDMETHODCALLTYPE DragSource::GiveFeedback(
266 /* [in] */ DWORD
267 #if defined DBG_CONSOLE_OUT
268 dwEffect
269 #endif
272 #if defined DBG_CONSOLE_OUT
273 printf("\nDragSource::GiveFeedback %d", dwEffect);
274 #endif
276 return DRAGDROP_S_USEDEFAULTCURSORS;
279 // XServiceInfo
280 OUString SAL_CALL DragSource::getImplementationName( )
282 return "com.sun.star.comp.datatransfer.dnd.OleDragSource_V1";
284 // XServiceInfo
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
306 XSourceListener. */
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);
317 if(SUCCEEDED(hr))
319 // We force the creation of a thread message queue. This is necessary
320 // for a later call to AttachThreadInput
321 MSG msgtemp;
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 );
331 DWORD dwEffect= 0;
332 hr= DoDragDrop(
333 pSource->m_spDataObject.get(),
334 static_cast<IDropSource*>(pSource),
335 dndActionsToDropEffects( pSource->m_sourceActions),
336 &dwEffect);
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);
343 //--> TRA
344 // clear the global transferable again
345 g_XTransferable.clear();
346 //<-- TRA
348 OSL_ENSURE( hr != E_INVALIDARG, "IDataObject impl does not contain valid data");
350 //Fire event
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;
361 OleUninitialize();
364 InterlockedDecrement(&pSource->m_RunningDndOperationCount);
366 // the DragSource was manually acquired by
367 // thread starting method DelayedStartDrag
368 pSource->release();
370 return 0;
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */