bump product version to 4.1.6.2
[LibreOffice.git] / dtrans / source / win32 / dnd / source.cxx
blob882c72b844162b933dc9bcc3dd757d7d1a835ac1
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>
25 #include <process.h>
26 #include <memory>
28 #include "source.hxx"
29 #include "globals.hxx"
30 #include "sourcecontext.hxx"
31 #include "../../inc/DtObjFactory.hxx"
32 #include <rtl/ustring.h>
33 #include <winuser.h>
34 #include <stdio.h>
36 #ifdef __MINGW32__
37 #if defined __uuidof
38 #undef __uuidof
39 #endif
40 #define __uuidof(I) IID_##I
41 #endif
43 using namespace cppu;
44 using namespace osl;
45 using namespace com::sun::star::datatransfer;
46 using namespace com::sun::star::datatransfer::dnd;
47 using namespace com::sun::star::datatransfer::dnd::DNDConstants;
48 using namespace com::sun::star::uno;
49 using namespace com::sun::star::awt::MouseButton;
50 using namespace com::sun::star::awt;
51 using namespace com::sun::star::lang;
53 extern Reference< XTransferable > g_XTransferable;
55 unsigned __stdcall DndOleSTAFunc(LPVOID pParams);
57 DragSource::DragSource( const Reference<XComponentContext>& rxContext):
58 WeakComponentImplHelper3< XDragSource, XInitialization, XServiceInfo >(m_mutex),
59 m_xContext( rxContext ),
60 // m_pcurrentContext_impl(0),
61 m_hAppWindow(0),
62 m_MouseButton(0),
63 m_RunningDndOperationCount(0)
67 DragSource::~DragSource()
71 //----------------------------------------------------
72 /** First start a new drag and drop thread if
73 the last one has finished
75 ????
76 Do we really need a separate thread for
77 every Dnd opeartion or only if the source
78 thread is an MTA thread
79 ????
81 void DragSource::StartDragImpl(
82 const DragGestureEvent& trigger,
83 sal_Int8 sourceActions,
84 sal_Int32 /*cursor*/,
85 sal_Int32 /*image*/,
86 const Reference<XTransferable >& trans,
87 const Reference<XDragSourceListener >& listener )
89 // The actions supported by the drag source
90 m_sourceActions= sourceActions;
91 // We need to know which mouse button triggered the operation.
92 // If it was the left one, then the drop occurs when that button
93 // has been released and if it was the right one then the drop
94 // occurs when the right button has been released. If the event is not
95 // set then we assume that the left button is pressed.
96 MouseEvent evtMouse;
97 trigger.Event >>= evtMouse;
98 m_MouseButton= evtMouse.Buttons;
100 // The SourceContext class administers the XDragSourceListener s and
101 // fires events to them. An instance only exists in the scope of this
102 // functions. However, the drag and drop operation causes callbacks
103 // to the IDropSource interface implemented in this class (but only
104 // while this function executes). The source context is also used
105 // in DragSource::QueryContinueDrag.
106 m_currentContext= static_cast<XDragSourceContext*>( new SourceContext(
107 static_cast<DragSource*>(this), listener ) );
109 // Convert the XTransferable data object into an IDataObject object;
111 //--> TRA
112 g_XTransferable = trans;
113 //<-- TRA
115 m_spDataObject= m_aDataConverter.createDataObjFromTransferable(
116 m_xContext, trans);
118 // Obtain the id of the thread that created the window
119 DWORD processId;
120 m_threadIdWindow= GetWindowThreadProcessId( m_hAppWindow, &processId);
122 // hold the instance for the DnD thread, it's to late
123 // to acquire at the start of the thread procedure
124 // the thread procedure is responsible for the release
125 acquire();
127 // The thread acccesses members of this instance but does not call acquire.
128 // Hopefully this instance is not destroyed before the thread has terminated.
129 unsigned threadId;
130 HANDLE hThread= reinterpret_cast<HANDLE>(_beginthreadex(
131 0, 0, DndOleSTAFunc, reinterpret_cast<void*>(this), 0, &threadId));
133 // detach from thread
134 CloseHandle(hThread);
137 // XInitialization
139 //----------------------------------------------------
140 /** aArguments contains a machine id
142 void SAL_CALL DragSource::initialize( const Sequence< Any >& aArguments )
143 throw(Exception, RuntimeException)
145 if( aArguments.getLength() >=2)
146 m_hAppWindow= *(HWND*)aArguments[1].getValue();
147 OSL_ASSERT( IsWindow( m_hAppWindow) );
150 //----------------------------------------------------
151 /** XDragSource
153 sal_Bool SAL_CALL DragSource::isDragImageSupported( )
154 throw(RuntimeException)
156 return 0;
159 //----------------------------------------------------
162 sal_Int32 SAL_CALL DragSource::getDefaultCursor( sal_Int8 /*dragAction*/ )
163 throw( IllegalArgumentException, RuntimeException)
165 return 0;
168 //----------------------------------------------------
169 /** Notifies the XDragSourceListener by
170 calling dragDropEnd
172 void SAL_CALL DragSource::startDrag(
173 const DragGestureEvent& trigger,
174 sal_Int8 sourceActions,
175 sal_Int32 cursor,
176 sal_Int32 image,
177 const Reference<XTransferable >& trans,
178 const Reference<XDragSourceListener >& listener ) throw( RuntimeException)
180 // Allow only one running dnd operation at a time,
181 // see XDragSource documentation
183 long cnt = InterlockedIncrement(&m_RunningDndOperationCount);
185 if (1 == cnt)
187 StartDragImpl(trigger, sourceActions, cursor, image, trans, listener);
189 else
191 cnt = InterlockedDecrement(&m_RunningDndOperationCount);
193 DragSourceDropEvent dsde;
195 dsde.DropAction = ACTION_NONE;
196 dsde.DropSuccess = false;
200 listener->dragDropEnd(dsde);
202 catch(RuntimeException&)
204 OSL_FAIL("Runtime exception during event dispatching");
209 //----------------------------------------------------
210 /**IDropTarget
212 HRESULT STDMETHODCALLTYPE DragSource::QueryInterface( REFIID riid, void **ppvObject)
214 if( !ppvObject)
215 return E_POINTER;
216 *ppvObject= NULL;
218 if( riid == __uuidof( IUnknown) )
219 *ppvObject= static_cast<IUnknown*>( this);
220 else if ( riid == __uuidof( IDropSource) )
221 *ppvObject= static_cast<IDropSource*>( this);
223 if(*ppvObject)
225 AddRef();
226 return S_OK;
228 else
229 return E_NOINTERFACE;
233 //----------------------------------------------------
236 ULONG STDMETHODCALLTYPE DragSource::AddRef( void)
238 acquire();
239 return (ULONG) m_refCount;
242 //----------------------------------------------------
245 ULONG STDMETHODCALLTYPE DragSource::Release( void)
247 ULONG ref= m_refCount;
248 release();
249 return --ref;
252 //----------------------------------------------------
253 /** IDropSource
255 HRESULT STDMETHODCALLTYPE DragSource::QueryContinueDrag(
256 /* [in] */ BOOL fEscapePressed,
257 /* [in] */ DWORD grfKeyState)
259 #if defined DBG_CONSOLE_OUT
260 printf("\nDragSource::QueryContinueDrag");
261 #endif
263 HRESULT retVal= S_OK; // default continue DnD
265 if (fEscapePressed)
267 retVal= DRAGDROP_S_CANCEL;
269 else
271 if( ( m_MouseButton == MouseButton::RIGHT && !(grfKeyState & MK_RBUTTON) ) ||
272 ( m_MouseButton == MouseButton::MIDDLE && !(grfKeyState & MK_MBUTTON) ) ||
273 ( m_MouseButton == MouseButton::LEFT && !(grfKeyState & MK_LBUTTON) ) ||
274 ( m_MouseButton == 0 && !(grfKeyState & MK_LBUTTON) ) )
276 retVal= DRAGDROP_S_DROP;
280 // fire dropActionChanged event.
281 // this is actually done by the context, which also detects whether the action
282 // changed at all
283 sal_Int8 dropAction= fEscapePressed ? ACTION_NONE :
284 dndOleKeysToAction( grfKeyState, m_sourceActions);
286 sal_Int8 userAction= fEscapePressed ? ACTION_NONE :
287 dndOleKeysToAction( grfKeyState, -1 );
289 static_cast<SourceContext*>(m_currentContext.get())->fire_dropActionChanged(
290 dropAction, userAction);
292 return retVal;
295 //----------------------------------------------------
298 HRESULT STDMETHODCALLTYPE DragSource::GiveFeedback(
299 /* [in] */ DWORD
300 #if defined DBG_CONSOLE_OUT
301 dwEffect
302 #endif
305 #if defined DBG_CONSOLE_OUT
306 printf("\nDragSource::GiveFeedback %d", dwEffect);
307 #endif
309 return DRAGDROP_S_USEDEFAULTCURSORS;
312 // XServiceInfo
313 OUString SAL_CALL DragSource::getImplementationName( ) throw (RuntimeException)
315 return OUString(RTL_CONSTASCII_USTRINGPARAM(DNDSOURCE_IMPL_NAME));
317 // XServiceInfo
318 sal_Bool SAL_CALL DragSource::supportsService( const OUString& ServiceName ) throw (RuntimeException)
320 if( ServiceName == DNDSOURCE_SERVICE_NAME )
321 return sal_True;
322 return sal_False;
325 Sequence< OUString > SAL_CALL DragSource::getSupportedServiceNames( ) throw (RuntimeException)
327 OUString names[1]= {OUString(RTL_CONSTASCII_USTRINGPARAM(DNDSOURCE_SERVICE_NAME))};
329 return Sequence<OUString>(names, 1);
332 //----------------------------------------------------
333 /**This function is called as extra thread from
334 DragSource::executeDrag. The function
335 carries out a drag and drop operation by calling
336 DoDragDrop. The thread also notifies all
337 XSourceListener.
339 unsigned __stdcall DndOleSTAFunc(LPVOID pParams)
341 // The structure contains all arguments for DoDragDrop and other
342 DragSource *pSource= (DragSource*)pParams;
344 // Drag and drop only works in a thread in which OleInitialize is called.
345 HRESULT hr= OleInitialize( NULL);
347 if(SUCCEEDED(hr))
349 // We force the creation of a thread message queue. This is necessary
350 // for a later call to AttachThreadInput
351 MSG msgtemp;
352 PeekMessage( &msgtemp, NULL, WM_USER, WM_USER, PM_NOREMOVE);
354 DWORD threadId= GetCurrentThreadId();
356 // This thread is attached to the thread that created the window. Hence
357 // this thread also receives all mouse and keyboard messages which are
358 // needed by DoDragDrop
359 AttachThreadInput( threadId , pSource->m_threadIdWindow, TRUE );
361 DWORD dwEffect= 0;
362 hr= DoDragDrop(
363 pSource->m_spDataObject.get(),
364 static_cast<IDropSource*>(pSource),
365 dndActionsToDropEffects( pSource->m_sourceActions),
366 &dwEffect);
368 // #105428 detach my message queue from the other threads
369 // message queue before calling fire_dragDropEnd else
370 // the office may appear to hang sometimes
371 AttachThreadInput( threadId, pSource->m_threadIdWindow, FALSE);
373 //--> TRA
374 // clear the global transferable again
375 g_XTransferable = Reference< XTransferable >( );
376 //<-- TRA
378 OSL_ENSURE( hr != E_INVALIDARG, "IDataObject impl does not contain valid data");
380 //Fire event
381 sal_Int8 action= hr == DRAGDROP_S_DROP ? dndOleDropEffectsToActions( dwEffect) : ACTION_NONE;
383 static_cast<SourceContext*>(pSource->m_currentContext.get())->fire_dragDropEnd(
384 hr == DRAGDROP_S_DROP ? sal_True : sal_False, action);
386 // Destroy SourceContextslkfgj
387 pSource->m_currentContext= 0;
388 // Destroy the XTransferable wrapper
389 pSource->m_spDataObject=0;
391 OleUninitialize();
394 InterlockedDecrement(&pSource->m_RunningDndOperationCount);
396 // the DragSource was manually acquired by
397 // thread starting method DelayedStartDrag
398 pSource->release();
400 return 0;
406 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */