20#include <config_vclplug.h>
22#include <osl/mutex.hxx>
28#include <com/sun/star/datatransfer/clipboard/SystemClipboard.hpp>
29#include <com/sun/star/datatransfer/dnd/XDropTargetDragContext.hpp>
30#include <com/sun/star/datatransfer/dnd/XDragGestureRecognizer.hpp>
31#include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
32#include <com/sun/star/uno/DeploymentException.hpp>
46DragSourceHelper::DragGestureListener::DragGestureListener( DragSourceHelper& rDragSourceHelper ) :
52DragSourceHelper::DragGestureListener::~DragGestureListener()
57void SAL_CALL DragSourceHelper::DragGestureListener::disposing(
const EventObject& )
62void SAL_CALL DragSourceHelper::DragGestureListener::dragGestureRecognized(
const DragGestureEvent& rDGE )
66 const Point aPtPixel( rDGE.DragOriginX, rDGE.DragOriginY );
67 mrParent.StartDrag( rDGE.DragAction, aPtPixel );
71DragSourceHelper::DragSourceHelper(
vcl::Window* pWindow ) :
72 mxDragGestureRecognizer( pWindow->GetDragGestureRecognizer() )
74 if( mxDragGestureRecognizer.is() )
76 mxDragGestureListener =
new DragSourceHelper::DragGestureListener( *
this );
77 mxDragGestureRecognizer->addDragGestureListener( mxDragGestureListener );
82void DragSourceHelper::dispose()
84 Reference<XDragGestureRecognizer> xTmp;
86 std::scoped_lock aGuard( maMutex );
87 xTmp = std::move(mxDragGestureRecognizer);
90 xTmp->removeDragGestureListener( mxDragGestureListener );
93DragSourceHelper::~DragSourceHelper()
99void DragSourceHelper::StartDrag(
sal_Int8,
const Point& )
104DropTargetHelper::DropTargetListener::DropTargetListener( DropTargetHelper& rDropTargetHelper ) :
110DropTargetHelper::DropTargetListener::~DropTargetListener()
115void SAL_CALL DropTargetHelper::DropTargetListener::disposing(
const EventObject& )
120void SAL_CALL DropTargetHelper::DropTargetListener::drop(
const DropTargetDropEvent& rDTDE )
127 ExecuteDropEvent aExecuteEvt( rDTDE.DropAction & ~DNDConstants::ACTION_DEFAULT,
Point( rDTDE.LocationX, rDTDE.LocationY ), rDTDE );
129 aExecuteEvt.mbDefault = ( ( rDTDE.DropAction & DNDConstants::ACTION_DEFAULT ) != 0 );
133 aAcceptEvent.
mnAction = aExecuteEvt.mnAction;
134 aAcceptEvent.
maPosPixel = aExecuteEvt.maPosPixel;
135 static_cast<DropTargetEvent&
>(
const_cast<DropTargetDragEvent&
>( aAcceptEvent.
maDragEvent )) = rDTDE;
136 const_cast<DropTargetDragEvent&
>( aAcceptEvent.
maDragEvent ).DropAction = rDTDE.DropAction;
137 const_cast<DropTargetDragEvent&
>( aAcceptEvent.
maDragEvent ).LocationX = rDTDE.LocationX;
138 const_cast<DropTargetDragEvent&
>( aAcceptEvent.
maDragEvent ).LocationY = rDTDE.LocationY;
139 const_cast<DropTargetDragEvent&
>( aAcceptEvent.
maDragEvent ).SourceActions = rDTDE.SourceActions;
141 aAcceptEvent.
mbDefault = aExecuteEvt.mbDefault;
145 if( DNDConstants::ACTION_NONE != nRet )
147 rDTDE.Context->acceptDrop( nRet );
149 if( aExecuteEvt.mbDefault )
150 aExecuteEvt.mnAction = nRet;
152 nRet =
mrParent.ExecuteDrop( aExecuteEvt );
155 rDTDE.Context->dropComplete( DNDConstants::ACTION_NONE != nRet );
157 mpLastDragOverEvent.reset();
159 catch(
const css::uno::Exception& )
165void SAL_CALL DropTargetHelper::DropTargetListener::dragEnter(
const DropTargetDragEnterEvent& rDTDEE )
171 mrParent.ImplBeginDrag( rDTDEE.SupportedDataFlavors );
173 catch(
const css::uno::Exception& )
181void SAL_CALL DropTargetHelper::DropTargetListener::dragOver(
const DropTargetDragEvent& rDTDE )
187 mpLastDragOverEvent.reset(
new AcceptDropEvent( rDTDE.DropAction & ~DNDConstants::ACTION_DEFAULT,
Point( rDTDE.LocationX, rDTDE.LocationY ), rDTDE ) );
188 mpLastDragOverEvent->mbDefault = ( ( rDTDE.DropAction & DNDConstants::ACTION_DEFAULT ) != 0 );
192 if( DNDConstants::ACTION_NONE == nRet )
193 rDTDE.Context->rejectDrag();
195 rDTDE.Context->acceptDrag( nRet );
197 catch(
const css::uno::Exception& )
203void SAL_CALL DropTargetHelper::DropTargetListener::dragExit(
const DropTargetEvent& )
209 if( mpLastDragOverEvent )
211 mpLastDragOverEvent->mbLeaving =
true;
212 mrParent.AcceptDrop( *mpLastDragOverEvent );
213 mpLastDragOverEvent.reset();
218 catch(
const css::uno::Exception& )
224void SAL_CALL DropTargetHelper::DropTargetListener::dropActionChanged(
const DropTargetDragEvent& )
229DropTargetHelper::DropTargetHelper(
vcl::Window* pWindow ) :
230 mxDropTarget( pWindow->GetDropTarget() )
236DropTargetHelper::DropTargetHelper(
const Reference< XDropTarget >& rxDropTarget ) :
237 mxDropTarget( rxDropTarget )
243void DropTargetHelper::dispose()
245 Reference< XDropTarget > xTmp;
247 std::scoped_lock aGuard( maMutex );
248 xTmp = std::move(mxDropTarget);
251 xTmp->removeDropTargetListener( mxDropTargetListener );
254DropTargetHelper::~DropTargetHelper()
260void DropTargetHelper::ImplConstruct()
262 if( mxDropTarget.is() )
264 mxDropTargetListener =
new DropTargetHelper::DropTargetListener( *
this );
265 mxDropTarget->addDropTargetListener( mxDropTargetListener );
266 mxDropTarget->setActive(
true );
271void DropTargetHelper::ImplBeginDrag(
const Sequence< DataFlavor >& rSupportedDataFlavors )
278void DropTargetHelper::ImplEndDrag()
286 return DNDConstants::ACTION_NONE;
292 return DNDConstants::ACTION_NONE;
298 return std::any_of(maFormats.begin(), maFormats.end(),
299 [&](
const DataFlavorEx& data) { return data.mnSotId == nFormat; });
307struct TDataCntnrEntry_Impl
344 const css::datatransfer::DataFlavor& rFlavor,
const OUString& )
350 for (
auto const& format :
pImpl->aFmtList)
352 if( nFmtId == format.nId )
354 bFnd =
SetAny( format.aAny );
363 case SotClipboardFormatId::STRING:
364 case SotClipboardFormatId::SOLK:
365 case SotClipboardFormatId::NETSCAPE_BOOKMARK:
366 case SotClipboardFormatId::FILECONTENT:
367 case SotClipboardFormatId::FILEGRPDESCRIPTOR:
368 case SotClipboardFormatId::UNIFORMRESOURCELOCATOR:
382 if( !
pImpl->pBookmk )
385 *
pImpl->pBookmk = rBkmk;
387 AddFormat( SotClipboardFormatId::STRING );
389 AddFormat( SotClipboardFormatId::NETSCAPE_BOOKMARK );
390 AddFormat( SotClipboardFormatId::FILECONTENT );
391 AddFormat( SotClipboardFormatId::FILEGRPDESCRIPTOR );
392 AddFormat( SotClipboardFormatId::UNIFORMRESOURCELOCATOR );
401 TDataCntnrEntry_Impl aEntry;
402 aEntry.nId = nFormatId;
406 aEntry.aAny <<=
aSeq;
407 pImpl->aFmtList.push_back( aEntry );
414 const OString& rStr )
416 CopyAnyData( nFormatId, rStr.getStr(), rStr.getLength() );
422 if( !rStr.isEmpty() )
424 TDataCntnrEntry_Impl aEntry;
426 aEntry.aAny <<= rStr;
427 pImpl->aFmtList.push_back( aEntry );
435 CopyString( SotClipboardFormatId::STRING, rStr );
441 return !
pImpl->aFmtList.empty() ||
442 nullptr !=
pImpl->pBookmk;
450 pImpl->aFinishedLnk = rLnk;
457 pImpl->aFinishedLnk.Call( nDropAction );
471 if (!data->m_xSystemClipboard.is())
475 data->m_xSystemClipboard = css::datatransfer::clipboard::SystemClipboard::create(
478 catch (DeploymentException
const &) {}
480 return data->m_xSystemClipboard;
482 Reference<XClipboard> xClipboard;
485 xClipboard = css::datatransfer::clipboard::SystemClipboard::create(
488 catch (DeploymentException
const &) {}
495 Reference<XClipboard> xSelection;
502 Sequence< Any >
args{
Any(OUString(
"PRIMARY")) };
503 xSelection.set(xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
504 "com.sun.star.datatransfer.clipboard.SystemClipboard",
args, xContext), UNO_QUERY_THROW);
506 static Reference< XClipboard > s_xSelection(
507 xContext->getServiceManager()->createInstanceWithContext(
508 "com.sun.star.datatransfer.clipboard.GenericClipboard", xContext), UNO_QUERY);
509 xSelection = s_xSelection;
BaseContainerNodeSharedPtr & mrParent
static SotClipboardFormatId GetFormat(const css::datatransfer::DataFlavor &rFlavor)
void StartDrag(vcl::Window *pWindow, sal_Int8 nDragSourceActions, const Link< sal_Int8, void > &rCallback)
virtual ~TransferDataContainer() override
void CopyAnyData(SotClipboardFormatId nFormatId, const char *pData, sal_uLong nLen)
void CopyINetBookmark(const INetBookmark &rBkmk)
virtual void DragFinished(sal_Int8 nDropAction) override
virtual bool GetData(const css::datatransfer::DataFlavor &rFlavor, const OUString &rDestDoc) override
std::unique_ptr< TransferDataContainer_Impl > pImpl
virtual void AddSupportedFormats() override
void CopyString(const OUString &rStr)
void CopyByteString(SotClipboardFormatId nFormatId, const OString &rStr)
static void FillDataFlavorExVector(const css::uno::Sequence< css::datatransfer::DataFlavor > &rDataFlavorSeq, DataFlavorExVector &rDataFlavorExVector)
void StartDrag(vcl::Window *pWindow, sal_Int8 nDragSourceActions)
bool SetAny(const css::uno::Any &rAny)
bool SetINetBookmark(const INetBookmark &rBmk, const css::datatransfer::DataFlavor &rFlavor)
void AddFormat(SotClipboardFormatId nFormat)
#define DBG_TESTSOLARMUTEX()
Sequence< sal_Int8 > aSeq
std::unique_ptr< sal_Int32[]> pData
Reference< XComponentContext > getProcessComponentContext()
const css::datatransfer::dnd::DropTargetDragEvent maDragEvent
std::vector< TDataCntnrEntry_Impl > aFmtList
std::unique_ptr< INetBookmark > pBookmk
TransferDataContainer_Impl()
Link< sal_Int8, void > aFinishedLnk
ImplSVData * ImplGetSVData()
Reference< XClipboard > GetSystemPrimarySelection()
Reference< XClipboard > GetSystemClipboard()