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 #ifndef INCLUDED_VCL_TRANSFER_HXX
21 #define INCLUDED_VCL_TRANSFER_HXX
23 #include <vcl/dllapi.h>
24 #include <tools/globname.hxx>
25 #include <tools/gen.hxx>
26 #include <tools/solar.h>
27 #include <sot/formats.hxx>
28 #include <sot/exchange.hxx>
29 #include <cppuhelper/implbase.hxx>
30 #include <com/sun/star/frame/XTerminateListener.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/datatransfer/XTransferable2.hpp>
33 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
34 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
35 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
36 #include <com/sun/star/datatransfer/dnd/XDragGestureListener.hpp>
37 #include <com/sun/star/datatransfer/dnd/DropTargetDragEvent.hpp>
38 #include <com/sun/star/datatransfer/dnd/DropTargetDropEvent.hpp>
39 #include <com/sun/star/datatransfer/dnd/XDragSourceListener.hpp>
40 #include <com/sun/star/datatransfer/dnd/XDropTargetListener.hpp>
41 #include <com/sun/star/embed/Aspects.hpp>
42 #include <rtl/ref.hxx>
47 namespace com::sun::star::datatransfer::dnd
{ class XDragGestureRecognizer
; }
48 namespace com::sun::star::io
{ class XInputStream
; }
49 namespace com::sun::star::datatransfer::dnd
{ class XDropTarget
; }
51 namespace tools
{ template <typename T
> class SvRef
; }
52 template <typename Arg
, typename Ret
> class Link
;
61 class SotStorageStream
;
62 namespace vcl
{ class Window
; }
66 #define DND_ACTION_NONE css::datatransfer::dnd::DNDConstants::ACTION_NONE
67 #define DND_ACTION_COPY css::datatransfer::dnd::DNDConstants::ACTION_COPY
68 #define DND_ACTION_MOVE css::datatransfer::dnd::DNDConstants::ACTION_MOVE
69 #define DND_ACTION_COPYMOVE css::datatransfer::dnd::DNDConstants::ACTION_COPY_OR_MOVE
70 #define DND_ACTION_LINK css::datatransfer::dnd::DNDConstants::ACTION_LINK
72 #define DND_POINTER_NONE 0
73 #define DND_IMAGE_NONE 0
75 struct TransferableObjectDescriptor
77 SvGlobalName maClassName
;
78 sal_uInt16 mnViewAspect
;
82 OUString maDisplayName
;
84 TransferableObjectDescriptor()
85 : mnViewAspect(css::embed::Aspects::MSOLE_CONTENT
)
88 VCL_DLLPUBLIC
friend SvStream
& WriteTransferableObjectDescriptor( SvStream
& rOStm
, const TransferableObjectDescriptor
& rObjDesc
);
91 struct AcceptDropEvent
94 const css::datatransfer::dnd::DropTargetDragEvent maDragEvent
;
100 mnAction( DND_ACTION_NONE
),
102 mbDefault( false ) {}
104 AcceptDropEvent( sal_Int8 nAction
,
105 const Point
& rPosPixel
,
106 css::datatransfer::dnd::DropTargetDragEvent aDragEvent
) :
107 maPosPixel( rPosPixel
),
108 maDragEvent(std::move( aDragEvent
)),
111 mbDefault( false ) {}
114 struct ExecuteDropEvent
117 const css::datatransfer::dnd::DropTargetDropEvent maDropEvent
;
121 ExecuteDropEvent( sal_Int8 nAction
,
122 const Point
& rPosPixel
,
123 css::datatransfer::dnd::DropTargetDropEvent aDropEvent
) :
124 maPosPixel( rPosPixel
),
125 maDropEvent(std::move( aDropEvent
)),
127 mbDefault( false ) {}
130 class VCL_DLLPUBLIC TransferableHelper
: public cppu::WeakImplHelper
< css::datatransfer::XTransferable2
,
131 css::datatransfer::clipboard::XClipboardOwner
,
132 css::datatransfer::dnd::XDragSourceListener
>
136 // nested class to implement the XTerminateListener interface
137 class SAL_DLLPRIVATE TerminateListener final
: public cppu::WeakImplHelper
< css::frame::XTerminateListener
, css::lang::XServiceInfo
>
141 TransferableHelper
& mrParent
;
146 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
148 // XTerminateListener
149 virtual void SAL_CALL
queryTermination( const css::lang::EventObject
& aEvent
) override
;
150 virtual void SAL_CALL
notifyTermination( const css::lang::EventObject
& aEvent
) override
;
153 virtual OUString SAL_CALL
getImplementationName() override
;
154 virtual sal_Bool SAL_CALL
supportsService( const OUString
& sServiceName
) override
;
155 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
159 TerminateListener( TransferableHelper
& rDropTargetHelper
);
160 virtual ~TerminateListener() override
;
163 friend class TransferableHelper::TerminateListener
;
168 OUString maLastFormat
;
169 mutable css::uno::Reference
< css::datatransfer::clipboard::XClipboard
> mxClipboard
;
170 rtl::Reference
< TerminateListener
> mxTerminateListener
;
171 DataFlavorExVector maFormats
;
172 std::unique_ptr
<TransferableObjectDescriptor
> mxObjDesc
;
175 ~TransferableHelper();
177 const css::uno::Reference
< css::datatransfer::clipboard::XClipboard
>&
178 getOwnClipboard() const { return mxClipboard
; }
183 virtual css::uno::Any SAL_CALL
getTransferData( const css::datatransfer::DataFlavor
& rFlavor
) override
;
184 virtual css::uno::Sequence
< css::datatransfer::DataFlavor
> SAL_CALL
getTransferDataFlavors() override
;
185 virtual sal_Bool SAL_CALL
isDataFlavorSupported( const css::datatransfer::DataFlavor
& rFlavor
) override
;
188 virtual css::uno::Any SAL_CALL
getTransferData2(
189 const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
) override
;
190 virtual sal_Bool SAL_CALL
isComplex() override
;
195 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
197 // XDragSourceListener
198 virtual void SAL_CALL
dragDropEnd( const css::datatransfer::dnd::DragSourceDropEvent
& dsde
) override
;
199 virtual void SAL_CALL
dragEnter( const css::datatransfer::dnd::DragSourceDragEvent
& dsde
) override
;
200 virtual void SAL_CALL
dragExit( const css::datatransfer::dnd::DragSourceEvent
& dse
) override
;
201 virtual void SAL_CALL
dragOver( const css::datatransfer::dnd::DragSourceDragEvent
& dsde
) override
;
202 virtual void SAL_CALL
dropActionChanged( const css::datatransfer::dnd::DragSourceDragEvent
& dsde
) override
;
205 virtual void SAL_CALL
lostOwnership( const css::uno::Reference
< css::datatransfer::clipboard::XClipboard
>& xClipboard
, const css::uno::Reference
< css::datatransfer::XTransferable
>& xTrans
) override
;
208 // derivees need to access lostOwnership in case hey override it
209 // on windows, changing the access rights to a method gives unresolved externals, so we introduce
210 // this impl-method here 'til the next incompatible update
211 void implCallOwnLostOwnership(
212 const css::uno::Reference
< css::datatransfer::clipboard::XClipboard
>& _rxClipboard
,
213 const css::uno::Reference
< css::datatransfer::XTransferable
>& _rxTrans
216 TransferableHelper::lostOwnership( _rxClipboard
, _rxTrans
);
222 SAL_DLLPRIVATE
void ImplFlush();
226 void AddFormat( SotClipboardFormatId nFormat
);
227 void AddFormat( const css::datatransfer::DataFlavor
& rFlavor
);
228 void RemoveFormat( SotClipboardFormatId nFormat
);
229 void RemoveFormat( const css::datatransfer::DataFlavor
& rFlavor
);
230 bool HasFormat( SotClipboardFormatId nFormat
);
233 bool SetAny( const css::uno::Any
& rAny
);
234 bool SetString( const OUString
& rString
);
235 bool SetBitmapEx( const BitmapEx
& rBitmap
, const css::datatransfer::DataFlavor
& rFlavor
);
236 bool SetGDIMetaFile( const GDIMetaFile
& rMtf
);
237 bool SetGraphic( const Graphic
& rGraphic
);
238 bool SetImageMap( const ImageMap
& rIMap
);
239 bool SetTransferableObjectDescriptor( const TransferableObjectDescriptor
& rDesc
);
240 bool SetINetBookmark( const INetBookmark
& rBmk
, const css::datatransfer::DataFlavor
& rFlavor
);
241 bool SetINetImage( const INetImage
& rINtImg
, const css::datatransfer::DataFlavor
& rFlavor
);
242 bool SetObject( void* pUserObject
, sal_uInt32 nUserObjectId
, const css::datatransfer::DataFlavor
& rFlavor
);
246 virtual void AddSupportedFormats() = 0;
247 virtual bool GetData( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
) = 0;
248 virtual bool WriteObject( SvStream
& rOStm
, void* pUserObject
, sal_uInt32 nUserObjectId
, const css::datatransfer::DataFlavor
& rFlavor
);
249 virtual void DragFinished( sal_Int8 nDropAction
);
250 virtual void ObjectReleased();
252 void CopyToSelection(const css::uno::Reference
<css::datatransfer::clipboard::XClipboard
> &rClipboard
) const;
255 void PrepareOLE( const TransferableObjectDescriptor
& rObjDesc
);
257 void CopyToClipboard(const css::uno::Reference
<css::datatransfer::clipboard::XClipboard
> &rClipboard
) const;
259 // convenience versions of the above which extract the XClipboard from the pWindow
260 void CopyToClipboard( vcl::Window
*pWindow
) const;
261 void CopyToPrimarySelection() const;
263 void StartDrag( vcl::Window
* pWindow
, sal_Int8 nDragSourceActions
);
265 static void ClearPrimarySelection();
268 struct TransferableDataHelper_Impl
;
270 class VCL_DLLPUBLIC TransferableDataHelper final
272 friend class DropTargetHelper
;
274 css::uno::Reference
< css::datatransfer::XTransferable
> mxTransfer
;
275 css::uno::Reference
< css::datatransfer::clipboard::XClipboard
> mxClipboard
;
276 DataFlavorExVector maFormats
;
277 std::unique_ptr
<TransferableObjectDescriptor
> mxObjDesc
;
278 std::unique_ptr
<TransferableDataHelper_Impl
> mxImpl
;
284 static void FillDataFlavorExVector( const css::uno::Sequence
< css::datatransfer::DataFlavor
>& rDataFlavorSeq
,
285 DataFlavorExVector
& rDataFlavorExVector
);
287 TransferableDataHelper();
288 TransferableDataHelper( const TransferableDataHelper
& rDataHelper
);
289 TransferableDataHelper( TransferableDataHelper
&& rDataHelper
) noexcept
;
290 TransferableDataHelper( const css::uno::Reference
< css::datatransfer::XTransferable
>& rxTransferable
);
291 ~TransferableDataHelper();
293 TransferableDataHelper
& operator=( const TransferableDataHelper
& rDataHelper
);
294 TransferableDataHelper
& operator=( TransferableDataHelper
&& rDataHelper
);
296 const css::uno::Reference
< css::datatransfer::XTransferable
>& GetTransferable() const { return mxTransfer
; }
297 css::uno::Reference
< css::datatransfer::XTransferable
> GetXTransferable() const;
299 bool HasFormat( SotClipboardFormatId nFormat
) const;
300 bool HasFormat( const css::datatransfer::DataFlavor
& rFlavor
) const;
302 sal_uInt32
GetFormatCount() const;
304 SotClipboardFormatId
GetFormat( sal_uInt32 nFormat
) const;
305 css::datatransfer::DataFlavor
GetFormatDataFlavor( sal_uInt32 nFormat
) const;
307 const DataFlavorExVector
& GetDataFlavorExVector() const {return maFormats
; }
309 bool StartClipboardListening( );
310 void StopClipboardListening( );
312 void Rebind( const css::uno::Reference
< css::datatransfer::XTransferable
>& _rxNewData
);
314 css::uno::Any
GetAny( SotClipboardFormatId nFormat
, const OUString
& rDestDoc
) const;
315 css::uno::Any
GetAny( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
) const;
317 bool GetString( SotClipboardFormatId nFormat
, OUString
& rStr
) const;
318 bool GetString( const css::datatransfer::DataFlavor
& rFlavor
, OUString
& rStr
) const;
320 bool GetBitmapEx( SotClipboardFormatId nFormat
, BitmapEx
& rBmp
) const;
321 bool GetBitmapEx( const css::datatransfer::DataFlavor
& rFlavor
, BitmapEx
& rBmp
) const;
323 /** Return as GDI metafile.
325 @param nMaxAction Allows you to limit the amount of actions; defaults to 0 which means no limit.
327 When you eg. Ctrl+a in Excel, you can get the entire sheet as
328 metafile, with over 3 million (!) actions; which is just too large for
329 any reasonable handling - and you need to set a limit.
331 bool GetGDIMetaFile( SotClipboardFormatId nFormat
, GDIMetaFile
& rMtf
, size_t nMaxActions
= 0 ) const;
332 bool GetGDIMetaFile( const css::datatransfer::DataFlavor
& rFlavor
, GDIMetaFile
& rMtf
) const;
334 bool GetGraphic( SotClipboardFormatId nFormat
, Graphic
& rGraphic
) const;
335 bool GetGraphic( const css::datatransfer::DataFlavor
& rFlavor
, Graphic
& rGraphic
) const;
337 bool GetImageMap( SotClipboardFormatId nFormat
, ImageMap
& rIMap
) const;
338 bool GetImageMap( const css::datatransfer::DataFlavor
& rFlavor
, ImageMap
& rImap
) const;
340 bool GetTransferableObjectDescriptor( SotClipboardFormatId nFormat
, TransferableObjectDescriptor
& rDesc
) const;
341 bool GetTransferableObjectDescriptor( TransferableObjectDescriptor
& rDesc
) const;
343 bool GetINetBookmark( SotClipboardFormatId nFormat
, INetBookmark
& rBmk
) const;
344 bool GetINetBookmark( const css::datatransfer::DataFlavor
& rFlavor
, INetBookmark
& rBmk
) const;
346 bool GetINetImage( SotClipboardFormatId nFormat
, INetImage
& rINtImg
) const;
347 bool GetINetImage( const css::datatransfer::DataFlavor
& rFlavor
, INetImage
& rINtImg
) const;
349 bool GetFileList( SotClipboardFormatId nFormat
, FileList
& rFileList
) const;
350 bool GetFileList( FileList
& rFileList
) const;
352 css::uno::Sequence
<sal_Int8
> GetSequence( SotClipboardFormatId nFormat
, const OUString
& rDestDoc
) const;
353 css::uno::Sequence
<sal_Int8
> GetSequence( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
) const;
355 std::unique_ptr
<SvStream
> GetSotStorageStream( SotClipboardFormatId nFormat
) const;
356 std::unique_ptr
<SvStream
> GetSotStorageStream( const css::datatransfer::DataFlavor
& rFlavor
) const;
358 css::uno::Reference
<css::io::XInputStream
> GetInputStream( SotClipboardFormatId nFormat
, const OUString
& rDestDoc
) const;
359 css::uno::Reference
<css::io::XInputStream
> GetInputStream( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
) const;
361 static TransferableDataHelper
CreateFromClipboard(const css::uno::Reference
<css::datatransfer::clipboard::XClipboard
>& rClipboard
);
362 static TransferableDataHelper
CreateFromSystemClipboard( vcl::Window
* pWindow
);
363 static TransferableDataHelper
CreateFromPrimarySelection();
364 static bool IsEqual( const css::datatransfer::DataFlavor
& rInternalFlavor
,
365 const css::datatransfer::DataFlavor
& rRequestFlavor
);
366 static bool WriteDDELink(SvStream
& stream
, std::u16string_view application
,
367 std::u16string_view topic
, std::u16string_view item
,
368 std::u16string_view extra
= {});
369 bool ReadDDELink(OUString
& application
, OUString
& topic
, OUString
& item
, OUString
& rest
) const;
372 class VCL_DLLPUBLIC
SAL_LOPLUGIN_ANNOTATE("crosscast") DragSourceHelper
376 // nested class to implement the XDragGestureListener interface
377 class SAL_DLLPRIVATE DragGestureListener final
: public cppu::WeakImplHelper
< css::datatransfer::dnd::XDragGestureListener
>
381 DragSourceHelper
& mrParent
;
386 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
388 // XDragGestureListener
389 virtual void SAL_CALL
dragGestureRecognized( const css::datatransfer::dnd::DragGestureEvent
& rDGE
) override
;
393 DragGestureListener( DragSourceHelper
& rDragSourceHelper
);
394 virtual ~DragGestureListener() override
;
397 friend class DragSourceHelper::DragGestureListener
;
401 css::uno::Reference
< css::datatransfer::dnd::XDragGestureRecognizer
> mxDragGestureRecognizer
;
403 rtl::Reference
< DragSourceHelper::DragGestureListener
> mxDragGestureListener
;
405 DragSourceHelper
& operator=( const DragSourceHelper
& rDragSourceHelper
) = delete;
406 bool operator==( const DragSourceHelper
& rDragSourceHelper
) const = delete;
410 // to be overridden by the application
411 virtual void StartDrag( sal_Int8 nAction
, const Point
& rPosPixel
);
413 DragSourceHelper( vcl::Window
* pWindow
);
415 virtual ~DragSourceHelper();
418 class VCL_DLLPUBLIC
SAL_LOPLUGIN_ANNOTATE("crosscast") DropTargetHelper
422 // nested class to implement the XDropTargetListener interface
423 class SAL_DLLPRIVATE DropTargetListener final
: public cppu::WeakImplHelper
< css::datatransfer::dnd::XDropTargetListener
>
427 DropTargetHelper
& mrParent
;
428 std::unique_ptr
<AcceptDropEvent
> mpLastDragOverEvent
;
431 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
433 // XDropTargetListener
434 virtual void SAL_CALL
drop( const css::datatransfer::dnd::DropTargetDropEvent
& dtde
) override
;
435 virtual void SAL_CALL
dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent
& dtdee
) override
;
436 virtual void SAL_CALL
dragExit( const css::datatransfer::dnd::DropTargetEvent
& dte
) override
;
437 virtual void SAL_CALL
dragOver( const css::datatransfer::dnd::DropTargetDragEvent
& dtde
) override
;
438 virtual void SAL_CALL
dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent
& dtde
) override
;
442 DropTargetListener( DropTargetHelper
& rDropTargetHelper
);
443 virtual ~DropTargetListener() override
;
446 friend class DropTargetHelper::DropTargetListener
;
450 css::uno::Reference
< css::datatransfer::dnd::XDropTarget
> mxDropTarget
;
452 rtl::Reference
< DropTargetHelper::DropTargetListener
> mxDropTargetListener
;
453 DataFlavorExVector maFormats
;
455 DropTargetHelper() = delete;
456 DropTargetHelper
& operator=( const DropTargetHelper
& rDropTargetHelper
) = delete;
457 bool operator==( const DropTargetHelper
& rDropTargetHelper
) const = delete;
459 void ImplConstruct();
461 // called by our own implementation of XDropTargetListener (DropTargetListener instance)
462 SAL_DLLPRIVATE
void ImplBeginDrag( const css::uno::Sequence
< css::datatransfer::DataFlavor
>& rSupportedDataFlavors
);
463 SAL_DLLPRIVATE
void ImplEndDrag();
467 // to be overridden by the application
468 SAL_DLLPRIVATE
virtual sal_Int8
AcceptDrop( const AcceptDropEvent
& rEvt
);
469 SAL_DLLPRIVATE
virtual sal_Int8
ExecuteDrop( const ExecuteDropEvent
& rEvt
);
471 DropTargetHelper( vcl::Window
* pWindow
);
472 DropTargetHelper( const css::uno::Reference
< css::datatransfer::dnd::XDropTarget
>& rxDropTarget
);
475 virtual ~DropTargetHelper();
477 // typically called by the application in ::AcceptDrop and ::ExecuteDrop and (see above)
478 bool IsDropFormatSupported(SotClipboardFormatId nFormat
) const;
480 const DataFlavorExVector
& GetDataFlavorExVector() const {return maFormats
; }
484 struct TransferDataContainer_Impl
;
486 class VCL_DLLPUBLIC TransferDataContainer
: public TransferableHelper
488 std::unique_ptr
<TransferDataContainer_Impl
> pImpl
;
490 virtual void AddSupportedFormats() override
;
491 virtual bool GetData( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
) override
;
495 TransferDataContainer();
496 virtual ~TransferDataContainer() override
;
498 void CopyINetBookmark( const INetBookmark
& rBkmk
);
499 void CopyString( const OUString
& rStr
);
500 void CopyString( SotClipboardFormatId nFmt
, const OUString
& rStr
);
502 void CopyByteString( SotClipboardFormatId nFormatId
, const OString
& rStr
);
503 void CopyAnyData( SotClipboardFormatId nFormatId
, const char* pData
, sal_uLong nLen
);
505 bool HasAnyData() const;
507 using TransferableHelper::StartDrag
;
508 void StartDrag( vcl::Window
* pWindow
, sal_Int8 nDragSourceActions
,
509 const Link
<sal_Int8
,void>& rCallback
);
510 virtual void DragFinished( sal_Int8 nDropAction
) override
;
513 css::uno::Reference
<css::datatransfer::clipboard::XClipboard
> VCL_DLLPUBLIC
GetSystemClipboard();
514 css::uno::Reference
<css::datatransfer::clipboard::XClipboard
> VCL_DLLPUBLIC
GetSystemPrimarySelection();
518 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */