1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
29 #include "globals.hxx"
30 #include "sourcecontext.hxx"
31 #include "../../inc/DtObjFactory.hxx"
32 #include <rtl/ustring.h>
40 #define __uuidof(I) IID_##I
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),
63 m_RunningDndOperationCount(0)
67 DragSource::~DragSource()
71 //----------------------------------------------------
72 /** First start a new drag and drop thread if
73 the last one has finished
76 Do we really need a separate thread for
77 every Dnd opeartion or only if the source
78 thread is an MTA thread
81 void DragSource::StartDragImpl(
82 const DragGestureEvent
& trigger
,
83 sal_Int8 sourceActions
,
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.
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;
112 g_XTransferable
= trans
;
115 m_spDataObject
= m_aDataConverter
.createDataObjFromTransferable(
118 // Obtain the id of the thread that created the window
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
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.
130 HANDLE hThread
= reinterpret_cast<HANDLE
>(_beginthreadex(
131 0, 0, DndOleSTAFunc
, reinterpret_cast<void*>(this), 0, &threadId
));
133 // detach from thread
134 CloseHandle(hThread
);
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 //----------------------------------------------------
153 sal_Bool SAL_CALL
DragSource::isDragImageSupported( )
154 throw(RuntimeException
)
159 //----------------------------------------------------
162 sal_Int32 SAL_CALL
DragSource::getDefaultCursor( sal_Int8
/*dragAction*/ )
163 throw( IllegalArgumentException
, RuntimeException
)
168 //----------------------------------------------------
169 /** Notifies the XDragSourceListener by
172 void SAL_CALL
DragSource::startDrag(
173 const DragGestureEvent
& trigger
,
174 sal_Int8 sourceActions
,
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
);
187 StartDragImpl(trigger
, sourceActions
, cursor
, image
, trans
, listener
);
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 //----------------------------------------------------
212 HRESULT STDMETHODCALLTYPE
DragSource::QueryInterface( REFIID riid
, void **ppvObject
)
218 if( riid
== __uuidof( IUnknown
) )
219 *ppvObject
= static_cast<IUnknown
*>( this);
220 else if ( riid
== __uuidof( IDropSource
) )
221 *ppvObject
= static_cast<IDropSource
*>( this);
229 return E_NOINTERFACE
;
233 //----------------------------------------------------
236 ULONG STDMETHODCALLTYPE
DragSource::AddRef( void)
239 return (ULONG
) m_refCount
;
242 //----------------------------------------------------
245 ULONG STDMETHODCALLTYPE
DragSource::Release( void)
247 ULONG ref
= m_refCount
;
252 //----------------------------------------------------
255 HRESULT STDMETHODCALLTYPE
DragSource::QueryContinueDrag(
256 /* [in] */ BOOL fEscapePressed
,
257 /* [in] */ DWORD grfKeyState
)
259 #if defined DBG_CONSOLE_OUT
260 printf("\nDragSource::QueryContinueDrag");
263 HRESULT retVal
= S_OK
; // default continue DnD
267 retVal
= DRAGDROP_S_CANCEL
;
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
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
);
295 //----------------------------------------------------
298 HRESULT STDMETHODCALLTYPE
DragSource::GiveFeedback(
300 #if defined DBG_CONSOLE_OUT
305 #if defined DBG_CONSOLE_OUT
306 printf("\nDragSource::GiveFeedback %d", dwEffect
);
309 return DRAGDROP_S_USEDEFAULTCURSORS
;
313 OUString SAL_CALL
DragSource::getImplementationName( ) throw (RuntimeException
)
315 return OUString(RTL_CONSTASCII_USTRINGPARAM(DNDSOURCE_IMPL_NAME
));
318 sal_Bool SAL_CALL
DragSource::supportsService( const OUString
& ServiceName
) throw (RuntimeException
)
320 if( ServiceName
== DNDSOURCE_SERVICE_NAME
)
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
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
);
349 // We force the creation of a thread message queue. This is necessary
350 // for a later call to AttachThreadInput
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
);
363 pSource
->m_spDataObject
.get(),
364 static_cast<IDropSource
*>(pSource
),
365 dndActionsToDropEffects( pSource
->m_sourceActions
),
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
);
374 // clear the global transferable again
375 g_XTransferable
= Reference
< XTransferable
>( );
378 OSL_ENSURE( hr
!= E_INVALIDARG
, "IDataObject impl does not contain valid data");
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;
394 InterlockedDecrement(&pSource
->m_RunningDndOperationCount
);
396 // the DragSource was manually acquired by
397 // thread starting method DelayedStartDrag
406 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */