fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / dtrans / source / win32 / dnd / source.cxx
blobb7a1aa0551ed7850a232b884972a34fdc914c7e8
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 <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 <osl/thread.h>
34 #include <winuser.h>
35 #include <stdio.h>
37 #ifdef __MINGW32__
38 #if defined __uuidof
39 #undef __uuidof
40 #endif
41 #define __uuidof(I) IID_##I
42 #endif
44 using namespace cppu;
45 using namespace osl;
46 using namespace com::sun::star::datatransfer;
47 using namespace com::sun::star::datatransfer::dnd;
48 using namespace com::sun::star::datatransfer::dnd::DNDConstants;
49 using namespace com::sun::star::uno;
50 using namespace com::sun::star::awt::MouseButton;
51 using namespace com::sun::star::awt;
52 using namespace com::sun::star::lang;
54 extern Reference< XTransferable > g_XTransferable;
56 unsigned __stdcall DndOleSTAFunc(LPVOID pParams);
58 DragSource::DragSource( const Reference<XComponentContext>& rxContext):
59 WeakComponentImplHelper3< XDragSource, XInitialization, XServiceInfo >(m_mutex),
60 m_xContext( rxContext ),
61 // m_pcurrentContext_impl(0),
62 m_hAppWindow(0),
63 m_MouseButton(0),
64 m_RunningDndOperationCount(0)
68 DragSource::~DragSource()
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 // function. 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
138 /** aArguments contains a machine id */
139 void SAL_CALL DragSource::initialize( const Sequence< Any >& aArguments )
140 throw(Exception, RuntimeException)
142 if( aArguments.getLength() >=2)
143 m_hAppWindow= *(HWND*)aArguments[1].getValue();
144 OSL_ASSERT( IsWindow( m_hAppWindow) );
147 /** XDragSource */
148 sal_Bool SAL_CALL DragSource::isDragImageSupported( )
149 throw(RuntimeException)
151 return 0;
154 sal_Int32 SAL_CALL DragSource::getDefaultCursor( sal_Int8 /*dragAction*/ )
155 throw( IllegalArgumentException, RuntimeException)
157 return 0;
160 /** Notifies the XDragSourceListener by
161 calling dragDropEnd */
162 void SAL_CALL DragSource::startDrag(
163 const DragGestureEvent& trigger,
164 sal_Int8 sourceActions,
165 sal_Int32 cursor,
166 sal_Int32 image,
167 const Reference<XTransferable >& trans,
168 const Reference<XDragSourceListener >& listener ) throw( RuntimeException)
170 // Allow only one running dnd operation at a time,
171 // see XDragSource documentation
173 long cnt = InterlockedIncrement(&m_RunningDndOperationCount);
175 if (1 == cnt)
177 StartDragImpl(trigger, sourceActions, cursor, image, trans, listener);
179 else
181 cnt = InterlockedDecrement(&m_RunningDndOperationCount);
183 DragSourceDropEvent dsde;
185 dsde.DropAction = ACTION_NONE;
186 dsde.DropSuccess = false;
190 listener->dragDropEnd(dsde);
192 catch(RuntimeException&)
194 OSL_FAIL("Runtime exception during event dispatching");
199 /** IDropTarget */
200 HRESULT STDMETHODCALLTYPE DragSource::QueryInterface( REFIID riid, void **ppvObject)
202 if( !ppvObject)
203 return E_POINTER;
204 *ppvObject= NULL;
206 if( riid == __uuidof( IUnknown) )
207 *ppvObject= static_cast<IUnknown*>( this);
208 else if ( riid == __uuidof( IDropSource) )
209 *ppvObject= static_cast<IDropSource*>( this);
211 if(*ppvObject)
213 AddRef();
214 return S_OK;
216 else
217 return E_NOINTERFACE;
221 ULONG STDMETHODCALLTYPE DragSource::AddRef()
223 acquire();
224 return (ULONG) m_refCount;
227 ULONG STDMETHODCALLTYPE DragSource::Release()
229 ULONG ref= m_refCount;
230 release();
231 return --ref;
234 /** IDropSource */
235 HRESULT STDMETHODCALLTYPE DragSource::QueryContinueDrag(
236 /* [in] */ BOOL fEscapePressed,
237 /* [in] */ DWORD grfKeyState)
239 #if defined DBG_CONSOLE_OUT
240 printf("\nDragSource::QueryContinueDrag");
241 #endif
243 HRESULT retVal= S_OK; // default continue DnD
245 if (fEscapePressed)
247 retVal= DRAGDROP_S_CANCEL;
249 else
251 if( ( m_MouseButton == MouseButton::RIGHT && !(grfKeyState & MK_RBUTTON) ) ||
252 ( m_MouseButton == MouseButton::MIDDLE && !(grfKeyState & MK_MBUTTON) ) ||
253 ( m_MouseButton == MouseButton::LEFT && !(grfKeyState & MK_LBUTTON) ) ||
254 ( m_MouseButton == 0 && !(grfKeyState & MK_LBUTTON) ) )
256 retVal= DRAGDROP_S_DROP;
260 // fire dropActionChanged event.
261 // this is actually done by the context, which also detects whether the action
262 // changed at all
263 sal_Int8 dropAction= fEscapePressed ? ACTION_NONE :
264 dndOleKeysToAction( grfKeyState, m_sourceActions);
266 sal_Int8 userAction= fEscapePressed ? ACTION_NONE :
267 dndOleKeysToAction( grfKeyState, -1 );
269 static_cast<SourceContext*>(m_currentContext.get())->fire_dropActionChanged(
270 dropAction, userAction);
272 return retVal;
275 HRESULT STDMETHODCALLTYPE DragSource::GiveFeedback(
276 /* [in] */ DWORD
277 #if defined DBG_CONSOLE_OUT
278 dwEffect
279 #endif
282 #if defined DBG_CONSOLE_OUT
283 printf("\nDragSource::GiveFeedback %d", dwEffect);
284 #endif
286 return DRAGDROP_S_USEDEFAULTCURSORS;
289 // XServiceInfo
290 OUString SAL_CALL DragSource::getImplementationName( ) throw (RuntimeException)
292 return OUString(DNDSOURCE_IMPL_NAME);
294 // XServiceInfo
295 sal_Bool SAL_CALL DragSource::supportsService( const OUString& ServiceName ) throw (RuntimeException)
297 return cppu::supportsService(this, ServiceName);
300 Sequence< OUString > SAL_CALL DragSource::getSupportedServiceNames( ) throw (RuntimeException)
302 OUString names[1]= {OUString(DNDSOURCE_SERVICE_NAME)};
304 return Sequence<OUString>(names, 1);
307 /** This function is called as extra thread from
308 DragSource::executeDrag. The function
309 carries out a drag and drop operation by calling
310 DoDragDrop. The thread also notifies all
311 XSourceListener. */
312 unsigned __stdcall DndOleSTAFunc(LPVOID pParams)
314 osl_setThreadName("DragSource DndOleSTAFunc");
316 // The structure contains all arguments for DoDragDrop and other
317 DragSource *pSource= (DragSource*)pParams;
319 // Drag and drop only works in a thread in which OleInitialize is called.
320 HRESULT hr= OleInitialize( NULL);
322 if(SUCCEEDED(hr))
324 // We force the creation of a thread message queue. This is necessary
325 // for a later call to AttachThreadInput
326 MSG msgtemp;
327 PeekMessage( &msgtemp, NULL, WM_USER, WM_USER, PM_NOREMOVE);
329 DWORD threadId= GetCurrentThreadId();
331 // This thread is attached to the thread that created the window. Hence
332 // this thread also receives all mouse and keyboard messages which are
333 // needed by DoDragDrop
334 AttachThreadInput( threadId , pSource->m_threadIdWindow, TRUE );
336 DWORD dwEffect= 0;
337 hr= DoDragDrop(
338 pSource->m_spDataObject.get(),
339 static_cast<IDropSource*>(pSource),
340 dndActionsToDropEffects( pSource->m_sourceActions),
341 &dwEffect);
343 // #105428 detach my message queue from the other threads
344 // message queue before calling fire_dragDropEnd else
345 // the office may appear to hang sometimes
346 AttachThreadInput( threadId, pSource->m_threadIdWindow, FALSE);
348 //--> TRA
349 // clear the global transferable again
350 g_XTransferable.clear();
351 //<-- TRA
353 OSL_ENSURE( hr != E_INVALIDARG, "IDataObject impl does not contain valid data");
355 //Fire event
356 sal_Int8 action= hr == DRAGDROP_S_DROP ? dndOleDropEffectsToActions( dwEffect) : ACTION_NONE;
358 static_cast<SourceContext*>(pSource->m_currentContext.get())->fire_dragDropEnd(
359 hr == DRAGDROP_S_DROP ? sal_True : sal_False, action);
361 // Destroy SourceContextslkfgj
362 pSource->m_currentContext= 0;
363 // Destroy the XTransferable wrapper
364 pSource->m_spDataObject=0;
366 OleUninitialize();
369 InterlockedDecrement(&pSource->m_RunningDndOperationCount);
371 // the DragSource was manually acquired by
372 // thread starting method DelayedStartDrag
373 pSource->release();
375 return 0;
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */