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/lang/XUnoTunnel.hpp>
33 #include <com/sun/star/datatransfer/XTransferable2.hpp>
34 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
35 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
36 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
37 #include <com/sun/star/datatransfer/dnd/XDragGestureListener.hpp>
38 #include <com/sun/star/datatransfer/dnd/DropTargetDragEvent.hpp>
39 #include <com/sun/star/datatransfer/dnd/DropTargetDropEvent.hpp>
40 #include <com/sun/star/datatransfer/dnd/XDragSourceListener.hpp>
41 #include <com/sun/star/datatransfer/dnd/XDropTargetListener.hpp>
42 #include <com/sun/star/embed/Aspects.hpp>
45 namespace com::sun::star::datatransfer::dnd
{ class XDragGestureRecognizer
; }
46 namespace com::sun::star::io
{ class XInputStream
; }
47 namespace com::sun::star::datatransfer::dnd
{ class XDropTarget
; }
49 namespace tools
{ template <typename T
> class SvRef
; }
50 template <typename Arg
, typename Ret
> class Link
;
59 class SotStorageStream
;
60 namespace vcl
{ class Window
; }
64 #define DND_ACTION_NONE css::datatransfer::dnd::DNDConstants::ACTION_NONE
65 #define DND_ACTION_COPY css::datatransfer::dnd::DNDConstants::ACTION_COPY
66 #define DND_ACTION_MOVE css::datatransfer::dnd::DNDConstants::ACTION_MOVE
67 #define DND_ACTION_COPYMOVE css::datatransfer::dnd::DNDConstants::ACTION_COPY_OR_MOVE
68 #define DND_ACTION_LINK css::datatransfer::dnd::DNDConstants::ACTION_LINK
70 #define DND_POINTER_NONE 0
71 #define DND_IMAGE_NONE 0
73 struct TransferableObjectDescriptor
75 SvGlobalName maClassName
;
76 sal_uInt16 mnViewAspect
;
80 OUString maDisplayName
;
82 TransferableObjectDescriptor()
83 : mnViewAspect(css::embed::Aspects::MSOLE_CONTENT
)
86 VCL_DLLPUBLIC
friend SvStream
& WriteTransferableObjectDescriptor( SvStream
& rOStm
, const TransferableObjectDescriptor
& rObjDesc
);
89 struct AcceptDropEvent
93 const css::datatransfer::dnd::DropTargetDragEvent maDragEvent
;
98 mnAction( DND_ACTION_NONE
),
100 mbDefault( false ) {}
102 AcceptDropEvent( sal_Int8 nAction
,
103 const Point
& rPosPixel
,
104 const css::datatransfer::dnd::DropTargetDragEvent
& rDragEvent
) :
106 maPosPixel( rPosPixel
),
107 maDragEvent( rDragEvent
),
109 mbDefault( false ) {}
112 struct ExecuteDropEvent
116 const css::datatransfer::dnd::DropTargetDropEvent maDropEvent
;
119 ExecuteDropEvent( sal_Int8 nAction
,
120 const Point
& rPosPixel
,
121 const css::datatransfer::dnd::DropTargetDropEvent
& rDropEvent
) :
123 maPosPixel( rPosPixel
),
124 maDropEvent( rDropEvent
),
125 mbDefault( false ) {}
128 class VCL_DLLPUBLIC TransferableHelper
: public cppu::WeakImplHelper
< css::datatransfer::XTransferable2
,
129 css::datatransfer::clipboard::XClipboardOwner
,
130 css::datatransfer::dnd::XDragSourceListener
,
131 css::lang::XUnoTunnel
>
135 // nested class to implement the XTerminateListener interface
136 class VCL_DLLPRIVATE TerminateListener
: public cppu::WeakImplHelper
< css::frame::XTerminateListener
, css::lang::XServiceInfo
>
140 TransferableHelper
& mrParent
;
145 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
147 // XTerminateListener
148 virtual void SAL_CALL
queryTermination( const css::lang::EventObject
& aEvent
) override
;
149 virtual void SAL_CALL
notifyTermination( const css::lang::EventObject
& aEvent
) override
;
152 virtual OUString SAL_CALL
getImplementationName() override
;
153 virtual sal_Bool SAL_CALL
supportsService( const OUString
& sServiceName
) override
;
154 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
158 TerminateListener( TransferableHelper
& rDropTargetHelper
);
159 virtual ~TerminateListener() override
;
162 friend class TransferableHelper::TerminateListener
;
167 OUString maLastFormat
;
168 mutable css::uno::Reference
< css::datatransfer::clipboard::XClipboard
> mxClipboard
;
169 css::uno::Reference
< css::frame::XTerminateListener
> mxTerminateListener
;
170 DataFlavorExVector maFormats
;
171 std::unique_ptr
<TransferableObjectDescriptor
> mxObjDesc
;
174 ~TransferableHelper();
176 const css::uno::Reference
< css::datatransfer::clipboard::XClipboard
>&
177 getOwnClipboard() const { return mxClipboard
; }
182 virtual css::uno::Any SAL_CALL
getTransferData( const css::datatransfer::DataFlavor
& rFlavor
) override
;
183 virtual css::uno::Sequence
< css::datatransfer::DataFlavor
> SAL_CALL
getTransferDataFlavors() override
;
184 virtual sal_Bool SAL_CALL
isDataFlavorSupported( const css::datatransfer::DataFlavor
& rFlavor
) override
;
187 virtual css::uno::Any SAL_CALL
getTransferData2(
188 const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
) override
;
189 virtual sal_Bool SAL_CALL
isComplex() override
;
192 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
194 // XDragSourceListener
195 virtual void SAL_CALL
dragDropEnd( const css::datatransfer::dnd::DragSourceDropEvent
& dsde
) override
;
196 virtual void SAL_CALL
dragEnter( const css::datatransfer::dnd::DragSourceDragEvent
& dsde
) override
;
197 virtual void SAL_CALL
dragExit( const css::datatransfer::dnd::DragSourceEvent
& dse
) override
;
198 virtual void SAL_CALL
dragOver( const css::datatransfer::dnd::DragSourceDragEvent
& dsde
) override
;
199 virtual void SAL_CALL
dropActionChanged( const css::datatransfer::dnd::DragSourceDragEvent
& dsde
) override
;
204 virtual void SAL_CALL
lostOwnership( const css::uno::Reference
< css::datatransfer::clipboard::XClipboard
>& xClipboard
, const css::uno::Reference
< css::datatransfer::XTransferable
>& xTrans
) override
;
207 // derivees need to access lostOwnership in case hey override it
208 // on windows, changing the access rights to a method gives unresolved externals, so we introduce
209 // this impl-method here 'til the next incompatible update
210 void implCallOwnLostOwnership(
211 const css::uno::Reference
< css::datatransfer::clipboard::XClipboard
>& _rxClipboard
,
212 const css::uno::Reference
< css::datatransfer::XTransferable
>& _rxTrans
215 TransferableHelper::lostOwnership( _rxClipboard
, _rxTrans
);
221 VCL_DLLPRIVATE
void ImplFlush();
225 void AddFormat( SotClipboardFormatId nFormat
);
226 void AddFormat( const css::datatransfer::DataFlavor
& rFlavor
);
227 void RemoveFormat( SotClipboardFormatId nFormat
);
228 void RemoveFormat( const css::datatransfer::DataFlavor
& rFlavor
);
229 bool HasFormat( SotClipboardFormatId nFormat
);
232 bool SetAny( const css::uno::Any
& rAny
);
233 bool SetString( const OUString
& rString
, const css::datatransfer::DataFlavor
& rFlavor
);
234 bool SetBitmapEx( const BitmapEx
& rBitmap
, const css::datatransfer::DataFlavor
& rFlavor
);
235 bool SetGDIMetaFile( const GDIMetaFile
& rMtf
);
236 bool SetGraphic( const Graphic
& rGraphic
);
237 bool SetImageMap( const ImageMap
& rIMap
);
238 bool SetTransferableObjectDescriptor( const TransferableObjectDescriptor
& rDesc
);
239 bool SetINetBookmark( const INetBookmark
& rBmk
, const css::datatransfer::DataFlavor
& rFlavor
);
240 bool SetINetImage( const INetImage
& rINtImg
, const css::datatransfer::DataFlavor
& rFlavor
);
241 bool SetObject( void* pUserObject
, sal_uInt32 nUserObjectId
, const css::datatransfer::DataFlavor
& rFlavor
);
245 virtual void AddSupportedFormats() = 0;
246 virtual bool GetData( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
) = 0;
247 virtual bool WriteObject( tools::SvRef
<SotStorageStream
>& rxOStm
, void* pUserObject
, sal_uInt32 nUserObjectId
, const css::datatransfer::DataFlavor
& rFlavor
);
248 virtual void DragFinished( sal_Int8 nDropAction
);
249 virtual void ObjectReleased();
253 void PrepareOLE( const TransferableObjectDescriptor
& rObjDesc
);
255 void CopyToClipboard(const css::uno::Reference
<css::datatransfer::clipboard::XClipboard
> &rClipboard
) const;
256 void CopyToSelection(const css::uno::Reference
<css::datatransfer::clipboard::XClipboard
> &rClipboard
) const;
258 // convenience versions of the above which extract the XClipboard from the pWindow
259 void CopyToClipboard( vcl::Window
*pWindow
) const;
260 void CopyToSelection( vcl::Window
*pWindow
) const;
262 void StartDrag( vcl::Window
* pWindow
, sal_Int8 nDragSourceActions
);
264 static void ClearSelection( vcl::Window
*pWindow
);
266 static const css::uno::Sequence
< sal_Int8
>& getUnoTunnelId();
270 virtual sal_Int64 SAL_CALL
getSomething( const css::uno::Sequence
< sal_Int8
>& rId
) override
;
273 struct TransferableDataHelper_Impl
;
275 class VCL_DLLPUBLIC TransferableDataHelper final
277 friend class DropTargetHelper
;
279 css::uno::Reference
< css::datatransfer::XTransferable
> mxTransfer
;
280 css::uno::Reference
< css::datatransfer::clipboard::XClipboard
> mxClipboard
;
281 DataFlavorExVector maFormats
;
282 std::unique_ptr
<TransferableObjectDescriptor
> mxObjDesc
;
283 std::unique_ptr
<TransferableDataHelper_Impl
> mxImpl
;
289 static void FillDataFlavorExVector( const css::uno::Sequence
< css::datatransfer::DataFlavor
>& rDataFlavorSeq
,
290 DataFlavorExVector
& rDataFlavorExVector
);
292 TransferableDataHelper();
293 TransferableDataHelper( const TransferableDataHelper
& rDataHelper
);
294 TransferableDataHelper( TransferableDataHelper
&& rDataHelper
) noexcept
;
295 TransferableDataHelper( const css::uno::Reference
< css::datatransfer::XTransferable
>& rxTransferable
);
296 ~TransferableDataHelper();
298 TransferableDataHelper
& operator=( const TransferableDataHelper
& rDataHelper
);
299 TransferableDataHelper
& operator=( TransferableDataHelper
&& rDataHelper
);
301 const css::uno::Reference
< css::datatransfer::XTransferable
>& GetTransferable() const { return mxTransfer
; }
302 css::uno::Reference
< css::datatransfer::XTransferable
> GetXTransferable() const;
304 bool HasFormat( SotClipboardFormatId nFormat
) const;
305 bool HasFormat( const css::datatransfer::DataFlavor
& rFlavor
) const;
307 sal_uInt32
GetFormatCount() const;
309 SotClipboardFormatId
GetFormat( sal_uInt32 nFormat
) const;
310 css::datatransfer::DataFlavor
GetFormatDataFlavor( sal_uInt32 nFormat
) const;
312 const DataFlavorExVector
& GetDataFlavorExVector() const {return maFormats
; }
314 bool StartClipboardListening( );
315 void StopClipboardListening( );
317 void Rebind( const css::uno::Reference
< css::datatransfer::XTransferable
>& _rxNewData
);
319 css::uno::Any
GetAny( SotClipboardFormatId nFormat
, const OUString
& rDestDoc
) const;
320 css::uno::Any
GetAny( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
) const;
322 bool GetString( SotClipboardFormatId nFormat
, OUString
& rStr
);
323 bool GetString( const css::datatransfer::DataFlavor
& rFlavor
, OUString
& rStr
);
325 bool GetBitmapEx( SotClipboardFormatId nFormat
, BitmapEx
& rBmp
);
326 bool GetBitmapEx( const css::datatransfer::DataFlavor
& rFlavor
, BitmapEx
& rBmp
);
328 /** Return as GDI metafile.
330 @param nMaxAction Allows you to limit the amount of actions; defaults to 0 which means no limit.
332 When you eg. Ctrl+a in Excel, you can get the entire sheet as
333 metafile, with over 3 million (!) actions; which is just too large for
334 any reasonable handling - and you need to set a limit.
336 bool GetGDIMetaFile( SotClipboardFormatId nFormat
, GDIMetaFile
& rMtf
, size_t nMaxActions
= 0 );
337 bool GetGDIMetaFile( const css::datatransfer::DataFlavor
& rFlavor
, GDIMetaFile
& rMtf
);
339 bool GetGraphic( SotClipboardFormatId nFormat
, Graphic
& rGraphic
);
340 bool GetGraphic( const css::datatransfer::DataFlavor
& rFlavor
, Graphic
& rGraphic
);
342 bool GetImageMap( SotClipboardFormatId nFormat
, ImageMap
& rIMap
);
343 bool GetImageMap( const css::datatransfer::DataFlavor
& rFlavor
, ImageMap
& rImap
);
345 bool GetTransferableObjectDescriptor( SotClipboardFormatId nFormat
, TransferableObjectDescriptor
& rDesc
);
346 bool GetTransferableObjectDescriptor( TransferableObjectDescriptor
& rDesc
);
348 bool GetINetBookmark( SotClipboardFormatId nFormat
, INetBookmark
& rBmk
);
349 bool GetINetBookmark( const css::datatransfer::DataFlavor
& rFlavor
, INetBookmark
& rBmk
);
351 bool GetINetImage( SotClipboardFormatId nFormat
, INetImage
& rINtImg
);
352 bool GetINetImage( const css::datatransfer::DataFlavor
& rFlavor
, INetImage
& rINtImg
);
354 bool GetFileList( SotClipboardFormatId nFormat
, FileList
& rFileList
);
355 bool GetFileList( FileList
& rFileList
);
357 css::uno::Sequence
<sal_Int8
> GetSequence( SotClipboardFormatId nFormat
, const OUString
& rDestDoc
);
358 css::uno::Sequence
<sal_Int8
> GetSequence( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
);
360 bool GetSotStorageStream( SotClipboardFormatId nFormat
, tools::SvRef
<SotStorageStream
>& rStreamRef
);
361 bool GetSotStorageStream( const css::datatransfer::DataFlavor
& rFlavor
, tools::SvRef
<SotStorageStream
>& rStreamRef
);
363 css::uno::Reference
<css::io::XInputStream
> GetInputStream( SotClipboardFormatId nFormat
, const OUString
& rDestDoc
);
364 css::uno::Reference
<css::io::XInputStream
> GetInputStream( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
);
366 static TransferableDataHelper
CreateFromClipboard(const css::uno::Reference
<css::datatransfer::clipboard::XClipboard
>& rClipboard
);
367 static TransferableDataHelper
CreateFromSystemClipboard( vcl::Window
* pWindow
);
368 static TransferableDataHelper
CreateFromSelection( vcl::Window
* pWindow
);
369 static bool IsEqual( const css::datatransfer::DataFlavor
& rInternalFlavor
,
370 const css::datatransfer::DataFlavor
& rRequestFlavor
);
373 class VCL_DLLPUBLIC DragSourceHelper
377 // nested class to implement the XDragGestureListener interface
378 class SAL_DLLPRIVATE DragGestureListener
: public cppu::WeakImplHelper
< css::datatransfer::dnd::XDragGestureListener
>
382 DragSourceHelper
& mrParent
;
387 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
389 // XDragGestureListener
390 virtual void SAL_CALL
dragGestureRecognized( const css::datatransfer::dnd::DragGestureEvent
& rDGE
) override
;
394 DragGestureListener( DragSourceHelper
& rDragSourceHelper
);
395 virtual ~DragGestureListener() override
;
398 friend class DragSourceHelper::DragGestureListener
;
402 css::uno::Reference
< css::datatransfer::dnd::XDragGestureRecognizer
> mxDragGestureRecognizer
;
404 css::uno::Reference
< css::datatransfer::dnd::XDragGestureListener
> mxDragGestureListener
;
406 DragSourceHelper
& operator=( const DragSourceHelper
& rDragSourceHelper
) = delete;
407 bool operator==( const DragSourceHelper
& rDragSourceHelper
) const = delete;
411 // to be overridden by the application
412 virtual void StartDrag( sal_Int8 nAction
, const Point
& rPosPixel
);
414 DragSourceHelper( vcl::Window
* pWindow
);
416 virtual ~DragSourceHelper();
419 class VCL_DLLPUBLIC DropTargetHelper
423 // nested class to implement the XDropTargetListener interface
424 class SAL_DLLPRIVATE DropTargetListener
: public cppu::WeakImplHelper
< css::datatransfer::dnd::XDropTargetListener
>
428 DropTargetHelper
& mrParent
;
429 std::unique_ptr
<AcceptDropEvent
> mpLastDragOverEvent
;
432 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
434 // XDropTargetListener
435 virtual void SAL_CALL
drop( const css::datatransfer::dnd::DropTargetDropEvent
& dtde
) override
;
436 virtual void SAL_CALL
dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent
& dtdee
) override
;
437 virtual void SAL_CALL
dragExit( const css::datatransfer::dnd::DropTargetEvent
& dte
) override
;
438 virtual void SAL_CALL
dragOver( const css::datatransfer::dnd::DropTargetDragEvent
& dtde
) override
;
439 virtual void SAL_CALL
dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent
& dtde
) override
;
443 DropTargetListener( DropTargetHelper
& rDropTargetHelper
);
444 virtual ~DropTargetListener() override
;
447 friend class DropTargetHelper::DropTargetListener
;
451 css::uno::Reference
< css::datatransfer::dnd::XDropTarget
> mxDropTarget
;
453 css::uno::Reference
< css::datatransfer::dnd::XDropTargetListener
> mxDropTargetListener
;
454 DataFlavorExVector maFormats
;
456 DropTargetHelper() = delete;
457 DropTargetHelper
& operator=( const DropTargetHelper
& rDropTargetHelper
) = delete;
458 bool operator==( const DropTargetHelper
& rDropTargetHelper
) const = delete;
460 void ImplConstruct();
462 // called by our own implementation of XDropTargetListener (DropTargetListener instance)
463 void ImplBeginDrag( const css::uno::Sequence
< css::datatransfer::DataFlavor
>& rSupportedDataFlavors
);
468 // to be overridden by the application
469 virtual sal_Int8
AcceptDrop( const AcceptDropEvent
& rEvt
);
470 virtual sal_Int8
ExecuteDrop( const ExecuteDropEvent
& rEvt
);
472 DropTargetHelper( vcl::Window
* pWindow
);
473 DropTargetHelper( const css::uno::Reference
< css::datatransfer::dnd::XDropTarget
>& rxDropTarget
);
476 virtual ~DropTargetHelper();
478 // typically called by the application in ::AcceptDrop and ::ExecuteDrop and (see above)
479 bool IsDropFormatSupported(SotClipboardFormatId nFormat
) const;
481 const DataFlavorExVector
& GetDataFlavorExVector() const {return maFormats
; }
485 struct TransferDataContainer_Impl
;
487 class VCL_DLLPUBLIC TransferDataContainer
: public TransferableHelper
489 std::unique_ptr
<TransferDataContainer_Impl
> pImpl
;
491 virtual void AddSupportedFormats() override
;
492 virtual bool GetData( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
) override
;
496 TransferDataContainer();
497 virtual ~TransferDataContainer() override
;
499 void CopyINetBookmark( const INetBookmark
& rBkmk
);
500 void CopyString( const OUString
& rStr
);
501 void CopyString( SotClipboardFormatId nFmt
, const OUString
& rStr
);
503 void CopyByteString( SotClipboardFormatId nFormatId
, const OString
& rStr
);
504 void CopyAnyData( SotClipboardFormatId nFormatId
, const char* pData
, sal_uLong nLen
);
506 bool HasAnyData() const;
508 using TransferableHelper::StartDrag
;
509 void StartDrag( vcl::Window
* pWindow
, sal_Int8 nDragSourceActions
,
510 const Link
<sal_Int8
,void>& rCallbck
);
511 virtual void DragFinished( sal_Int8 nDropAction
) override
;
514 css::uno::Reference
<css::datatransfer::clipboard::XClipboard
> VCL_DLLPUBLIC
GetSystemClipboard();
515 css::uno::Reference
<css::datatransfer::clipboard::XClipboard
> VCL_DLLPUBLIC
GetSystemPrimarySelection();
519 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */