Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / vcl / transfer.hxx
blobe460e9f9c155a7d1bb6be3e7be998833bfa6abbd
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 #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 <memory>
43 #include <mutex>
44 #include <utility>
46 namespace com::sun::star::datatransfer::dnd { class XDragGestureRecognizer; }
47 namespace com::sun::star::io { class XInputStream; }
48 namespace com::sun::star::datatransfer::dnd { class XDropTarget; }
50 namespace tools { template <typename T> class SvRef; }
51 template <typename Arg, typename Ret> class Link;
53 class BitmapEx;
54 class GDIMetaFile;
55 class Graphic;
56 class ImageMap;
57 class INetBookmark;
58 class INetImage;
59 class FileList;
60 class SotStorageStream;
61 class SotTempStream;
62 namespace vcl { class Window; }
64 // Drag&Drop defines
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;
79 Point maDragStartPos;
80 Size maSize;
81 OUString maTypeName;
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
93 Point maPosPixel;
94 const css::datatransfer::dnd::DropTargetDragEvent maDragEvent;
95 sal_Int8 mnAction;
96 bool mbLeaving;
97 bool mbDefault;
99 AcceptDropEvent() :
100 mnAction( DND_ACTION_NONE ),
101 mbLeaving( false ),
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 )),
109 mnAction( nAction ),
110 mbLeaving( false ),
111 mbDefault( false ) {}
114 struct ExecuteDropEvent
116 Point maPosPixel;
117 const css::datatransfer::dnd::DropTargetDropEvent maDropEvent;
118 sal_Int8 mnAction;
119 bool mbDefault;
121 ExecuteDropEvent( sal_Int8 nAction,
122 const Point& rPosPixel,
123 css::datatransfer::dnd::DropTargetDropEvent aDropEvent ) :
124 maPosPixel( rPosPixel ),
125 maDropEvent(std::move( aDropEvent )),
126 mnAction( nAction ),
127 mbDefault( false ) {}
130 class VCL_DLLPUBLIC TransferableHelper : public cppu::WeakImplHelper< css::datatransfer::XTransferable2,
131 css::datatransfer::clipboard::XClipboardOwner,
132 css::datatransfer::dnd::XDragSourceListener>
134 private:
136 // nested class to implement the XTerminateListener interface
137 class VCL_DLLPRIVATE TerminateListener final : public cppu::WeakImplHelper< css::frame::XTerminateListener, css::lang::XServiceInfo >
139 private:
141 TransferableHelper& mrParent;
143 private:
145 // XEventListener
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;
152 // XServiceInfo
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;
157 public:
159 TerminateListener( TransferableHelper& rDropTargetHelper );
160 virtual ~TerminateListener() override;
163 friend class TransferableHelper::TerminateListener;
165 private:
167 css::uno::Any maAny;
168 OUString maLastFormat;
169 mutable css::uno::Reference< css::datatransfer::clipboard::XClipboard > mxClipboard;
170 css::uno::Reference< css::frame::XTerminateListener > mxTerminateListener;
171 DataFlavorExVector maFormats;
172 std::unique_ptr<TransferableObjectDescriptor> mxObjDesc;
174 protected:
175 ~TransferableHelper();
177 const css::uno::Reference< css::datatransfer::clipboard::XClipboard >&
178 getOwnClipboard() const { return mxClipboard; }
180 public:
182 // XTransferable
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;
187 // Transferable2
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;
192 private:
194 // XEventListener
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;
204 // XClipboardOwner
205 virtual void SAL_CALL lostOwnership( const css::uno::Reference< css::datatransfer::clipboard::XClipboard >& xClipboard, const css::uno::Reference< css::datatransfer::XTransferable >& xTrans ) override;
207 protected:
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 );
220 private:
222 VCL_DLLPRIVATE void ImplFlush();
224 protected:
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 );
231 void ClearFormats();
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 );
244 protected:
246 virtual void AddSupportedFormats() = 0;
247 virtual bool GetData( const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc ) = 0;
248 virtual bool WriteObject( tools::SvRef<SotTempStream>& rxOStm, 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;
253 public:
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;
280 void InitFormats();
282 public:
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 );
341 bool GetTransferableObjectDescriptor( TransferableObjectDescriptor& rDesc );
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 bool GetSotStorageStream( SotClipboardFormatId nFormat, tools::SvRef<SotTempStream>& rStreamRef ) const;
356 bool GetSotStorageStream( const css::datatransfer::DataFlavor& rFlavor, tools::SvRef<SotTempStream>& rStreamRef ) 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 );
368 class VCL_DLLPUBLIC SAL_LOPLUGIN_ANNOTATE("crosscast") DragSourceHelper
370 private:
372 // nested class to implement the XDragGestureListener interface
373 class SAL_DLLPRIVATE DragGestureListener final : public cppu::WeakImplHelper< css::datatransfer::dnd::XDragGestureListener >
375 private:
377 DragSourceHelper& mrParent;
379 private:
381 // XEventListener
382 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
384 // XDragGestureListener
385 virtual void SAL_CALL dragGestureRecognized( const css::datatransfer::dnd::DragGestureEvent& rDGE ) override;
387 public:
389 DragGestureListener( DragSourceHelper& rDragSourceHelper );
390 virtual ~DragGestureListener() override;
393 friend class DragSourceHelper::DragGestureListener;
395 private:
396 std::mutex maMutex;
397 css::uno::Reference< css::datatransfer::dnd::XDragGestureRecognizer > mxDragGestureRecognizer;
399 css::uno::Reference< css::datatransfer::dnd::XDragGestureListener > mxDragGestureListener;
401 DragSourceHelper& operator=( const DragSourceHelper& rDragSourceHelper ) = delete;
402 bool operator==( const DragSourceHelper& rDragSourceHelper ) const = delete;
404 public:
406 // to be overridden by the application
407 virtual void StartDrag( sal_Int8 nAction, const Point& rPosPixel );
409 DragSourceHelper( vcl::Window* pWindow );
410 void dispose();
411 virtual ~DragSourceHelper();
414 class VCL_DLLPUBLIC SAL_LOPLUGIN_ANNOTATE("crosscast") DropTargetHelper
416 private:
418 // nested class to implement the XDropTargetListener interface
419 class SAL_DLLPRIVATE DropTargetListener final : public cppu::WeakImplHelper< css::datatransfer::dnd::XDropTargetListener >
421 private:
423 DropTargetHelper& mrParent;
424 std::unique_ptr<AcceptDropEvent> mpLastDragOverEvent;
426 // XEventListener
427 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
429 // XDropTargetListener
430 virtual void SAL_CALL drop( const css::datatransfer::dnd::DropTargetDropEvent& dtde ) override;
431 virtual void SAL_CALL dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent& dtdee ) override;
432 virtual void SAL_CALL dragExit( const css::datatransfer::dnd::DropTargetEvent& dte ) override;
433 virtual void SAL_CALL dragOver( const css::datatransfer::dnd::DropTargetDragEvent& dtde ) override;
434 virtual void SAL_CALL dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent& dtde ) override;
436 public:
438 DropTargetListener( DropTargetHelper& rDropTargetHelper );
439 virtual ~DropTargetListener() override;
442 friend class DropTargetHelper::DropTargetListener;
444 private:
445 std::mutex maMutex;
446 css::uno::Reference< css::datatransfer::dnd::XDropTarget > mxDropTarget;
448 css::uno::Reference< css::datatransfer::dnd::XDropTargetListener > mxDropTargetListener;
449 DataFlavorExVector maFormats;
451 DropTargetHelper() = delete;
452 DropTargetHelper& operator=( const DropTargetHelper& rDropTargetHelper ) = delete;
453 bool operator==( const DropTargetHelper& rDropTargetHelper ) const = delete;
455 void ImplConstruct();
457 // called by our own implementation of XDropTargetListener (DropTargetListener instance)
458 void ImplBeginDrag( const css::uno::Sequence< css::datatransfer::DataFlavor >& rSupportedDataFlavors );
459 void ImplEndDrag();
461 public:
463 // to be overridden by the application
464 virtual sal_Int8 AcceptDrop( const AcceptDropEvent& rEvt );
465 virtual sal_Int8 ExecuteDrop( const ExecuteDropEvent& rEvt );
467 DropTargetHelper( vcl::Window* pWindow );
468 DropTargetHelper( const css::uno::Reference< css::datatransfer::dnd::XDropTarget >& rxDropTarget );
470 void dispose();
471 virtual ~DropTargetHelper();
473 // typically called by the application in ::AcceptDrop and ::ExecuteDrop and (see above)
474 bool IsDropFormatSupported(SotClipboardFormatId nFormat) const;
476 const DataFlavorExVector& GetDataFlavorExVector() const {return maFormats; }
480 struct TransferDataContainer_Impl;
482 class VCL_DLLPUBLIC TransferDataContainer : public TransferableHelper
484 std::unique_ptr<TransferDataContainer_Impl> pImpl;
486 virtual void AddSupportedFormats() override;
487 virtual bool GetData( const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc ) override;
489 public:
491 TransferDataContainer();
492 virtual ~TransferDataContainer() override;
494 void CopyINetBookmark( const INetBookmark& rBkmk );
495 void CopyString( const OUString& rStr );
496 void CopyString( SotClipboardFormatId nFmt, const OUString& rStr );
498 void CopyByteString( SotClipboardFormatId nFormatId, const OString& rStr );
499 void CopyAnyData( SotClipboardFormatId nFormatId, const char* pData, sal_uLong nLen );
501 bool HasAnyData() const;
503 using TransferableHelper::StartDrag;
504 void StartDrag( vcl::Window* pWindow, sal_Int8 nDragSourceActions,
505 const Link<sal_Int8,void>& rCallback );
506 virtual void DragFinished( sal_Int8 nDropAction ) override;
509 css::uno::Reference<css::datatransfer::clipboard::XClipboard> VCL_DLLPUBLIC GetSystemClipboard();
510 css::uno::Reference<css::datatransfer::clipboard::XClipboard> VCL_DLLPUBLIC GetSystemPrimarySelection();
512 #endif
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */