nss: upgrade to release 3.73
[LibreOffice.git] / vcl / win / dtrans / source.cxx
blob93d94e94e4c751bd08232476aa849f0ad8df1366
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 "DtObjFactory.hxx"
35 #include <rtl/ustring.h>
36 #include <osl/thread.h>
37 #include <winuser.h>
38 #include <stdio.h>
40 using namespace cppu;
41 using namespace osl;
42 using namespace com::sun::star::datatransfer;
43 using namespace com::sun::star::datatransfer::dnd;
44 using namespace com::sun::star::datatransfer::dnd::DNDConstants;
45 using namespace com::sun::star::uno;
46 using namespace com::sun::star::awt::MouseButton;
47 using namespace com::sun::star::awt;
48 using namespace com::sun::star::lang;
50 static unsigned __stdcall DndOleSTAFunc(LPVOID pParams);
52 DragSource::DragSource( const Reference<XComponentContext>& rxContext):
53 WeakComponentImplHelper< XDragSource, XInitialization, XServiceInfo >(m_mutex),
54 m_xContext( rxContext ),
55 // m_pcurrentContext_impl(0),
56 m_hAppWindow(nullptr),
57 m_MouseButton(0),
58 m_RunningDndOperationCount(0)
62 DragSource::~DragSource()
66 /** First start a new drag and drop thread if
67 the last one has finished
69 ????
70 Do we really need a separate thread for
71 every Dnd operation or only if the source
72 thread is an MTA thread
73 ????
75 void DragSource::StartDragImpl(
76 const DragGestureEvent& trigger,
77 sal_Int8 sourceActions,
78 sal_Int32 /*cursor*/,
79 sal_Int32 /*image*/,
80 const Reference<XTransferable >& trans,
81 const Reference<XDragSourceListener >& listener )
83 // The actions supported by the drag source
84 m_sourceActions= sourceActions;
85 // We need to know which mouse button triggered the operation.
86 // If it was the left one, then the drop occurs when that button
87 // has been released and if it was the right one then the drop
88 // occurs when the right button has been released. If the event is not
89 // set then we assume that the left button is pressed.
90 MouseEvent evtMouse;
91 trigger.Event >>= evtMouse;
92 m_MouseButton= evtMouse.Buttons;
94 // The SourceContext class administers the XDragSourceListener s and
95 // fires events to them. An instance only exists in the scope of this
96 // function. However, the drag and drop operation causes callbacks
97 // to the IDropSource interface implemented in this class (but only
98 // while this function executes). The source context is also used
99 // in DragSource::QueryContinueDrag.
100 m_currentContext = new SourceContext(this, listener);
102 // Convert the XTransferable data object into an IDataObject object;
104 //--> TRA
105 g_XTransferable = trans;
106 //<-- TRA
108 m_spDataObject= CDTransObjFactory::createDataObjFromTransferable(
109 m_xContext, trans);
111 // Obtain the id of the thread that created the window
112 DWORD processId;
113 m_threadIdWindow= GetWindowThreadProcessId( m_hAppWindow, &processId);
115 // hold the instance for the DnD thread, it's too late
116 // to acquire at the start of the thread procedure
117 // the thread procedure is responsible for the release
118 acquire();
120 // The thread accesses members of this instance but does not call acquire.
121 // Hopefully this instance is not destroyed before the thread has terminated.
122 unsigned threadId;
123 HANDLE hThread= reinterpret_cast<HANDLE>(_beginthreadex(
124 nullptr, 0, DndOleSTAFunc, this, 0, &threadId));
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 OSL_FAIL("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(static_cast<cppu::OWeakObject*>(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(LPVOID 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: */