vcl: 'horizontically'
[LibreOffice.git] / include / vcl / transfer.hxx
blobf7c41fafa00ba78cb039007f8bda19f574d1ba2d
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/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>
43 #include <memory>
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;
52 class BitmapEx;
53 class GDIMetaFile;
54 class Graphic;
55 class ImageMap;
56 class INetBookmark;
57 class INetImage;
58 class FileList;
59 class SotStorageStream;
60 class SotTempStream;
61 namespace vcl { class Window; }
63 // Drag&Drop defines
65 #define DND_ACTION_NONE css::datatransfer::dnd::DNDConstants::ACTION_NONE
66 #define DND_ACTION_COPY css::datatransfer::dnd::DNDConstants::ACTION_COPY
67 #define DND_ACTION_MOVE css::datatransfer::dnd::DNDConstants::ACTION_MOVE
68 #define DND_ACTION_COPYMOVE css::datatransfer::dnd::DNDConstants::ACTION_COPY_OR_MOVE
69 #define DND_ACTION_LINK css::datatransfer::dnd::DNDConstants::ACTION_LINK
71 #define DND_POINTER_NONE 0
72 #define DND_IMAGE_NONE 0
74 struct TransferableObjectDescriptor
76 SvGlobalName maClassName;
77 sal_uInt16 mnViewAspect;
78 Point maDragStartPos;
79 Size maSize;
80 OUString maTypeName;
81 OUString maDisplayName;
83 TransferableObjectDescriptor()
84 : mnViewAspect(css::embed::Aspects::MSOLE_CONTENT)
87 VCL_DLLPUBLIC friend SvStream& WriteTransferableObjectDescriptor( SvStream& rOStm, const TransferableObjectDescriptor& rObjDesc );
90 struct AcceptDropEvent
92 sal_Int8 mnAction;
93 Point maPosPixel;
94 const css::datatransfer::dnd::DropTargetDragEvent maDragEvent;
95 bool mbLeaving;
96 bool mbDefault;
98 AcceptDropEvent() :
99 mnAction( DND_ACTION_NONE ),
100 mbLeaving( false ),
101 mbDefault( false ) {}
103 AcceptDropEvent( sal_Int8 nAction,
104 const Point& rPosPixel,
105 const css::datatransfer::dnd::DropTargetDragEvent& rDragEvent ) :
106 mnAction( nAction ),
107 maPosPixel( rPosPixel ),
108 maDragEvent( rDragEvent ),
109 mbLeaving( false ),
110 mbDefault( false ) {}
113 struct ExecuteDropEvent
115 sal_Int8 mnAction;
116 Point maPosPixel;
117 const css::datatransfer::dnd::DropTargetDropEvent maDropEvent;
118 bool mbDefault;
120 ExecuteDropEvent( sal_Int8 nAction,
121 const Point& rPosPixel,
122 const css::datatransfer::dnd::DropTargetDropEvent& rDropEvent ) :
123 mnAction( nAction ),
124 maPosPixel( rPosPixel ),
125 maDropEvent( rDropEvent ),
126 mbDefault( false ) {}
129 class VCL_DLLPUBLIC TransferableHelper : public cppu::WeakImplHelper< css::datatransfer::XTransferable2,
130 css::datatransfer::clipboard::XClipboardOwner,
131 css::datatransfer::dnd::XDragSourceListener,
132 css::lang::XUnoTunnel >
134 private:
136 // nested class to implement the XTerminateListener interface
137 class VCL_DLLPRIVATE TerminateListener : 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 private:
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 // XEventListener
193 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
195 // XDragSourceListener
196 virtual void SAL_CALL dragDropEnd( const css::datatransfer::dnd::DragSourceDropEvent& dsde ) override;
197 virtual void SAL_CALL dragEnter( const css::datatransfer::dnd::DragSourceDragEvent& dsde ) override;
198 virtual void SAL_CALL dragExit( const css::datatransfer::dnd::DragSourceEvent& dse ) override;
199 virtual void SAL_CALL dragOver( const css::datatransfer::dnd::DragSourceDragEvent& dsde ) override;
200 virtual void SAL_CALL dropActionChanged( const css::datatransfer::dnd::DragSourceDragEvent& dsde ) override;
202 private:
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, const css::datatransfer::DataFlavor& rFlavor );
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 public:
254 void PrepareOLE( const TransferableObjectDescriptor& rObjDesc );
256 void CopyToClipboard(const css::uno::Reference<css::datatransfer::clipboard::XClipboard> &rClipboard) const;
257 void CopyToSelection(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 CopyToSelection( vcl::Window *pWindow ) const;
263 void StartDrag( vcl::Window* pWindow, sal_Int8 nDragSourceActions );
265 static void ClearSelection( vcl::Window *pWindow );
267 static const css::uno::Sequence< sal_Int8 >& getUnoTunnelId();
269 public:
271 virtual sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< sal_Int8 >& rId ) override;
274 struct TransferableDataHelper_Impl;
276 class VCL_DLLPUBLIC TransferableDataHelper final
278 friend class DropTargetHelper;
280 css::uno::Reference< css::datatransfer::XTransferable > mxTransfer;
281 css::uno::Reference< css::datatransfer::clipboard::XClipboard > mxClipboard;
282 DataFlavorExVector maFormats;
283 std::unique_ptr<TransferableObjectDescriptor> mxObjDesc;
284 std::unique_ptr<TransferableDataHelper_Impl> mxImpl;
286 void InitFormats();
288 public:
290 static void FillDataFlavorExVector( const css::uno::Sequence< css::datatransfer::DataFlavor >& rDataFlavorSeq,
291 DataFlavorExVector& rDataFlavorExVector );
293 TransferableDataHelper();
294 TransferableDataHelper( const TransferableDataHelper& rDataHelper );
295 TransferableDataHelper( TransferableDataHelper&& rDataHelper ) noexcept;
296 TransferableDataHelper( const css::uno::Reference< css::datatransfer::XTransferable >& rxTransferable );
297 ~TransferableDataHelper();
299 TransferableDataHelper& operator=( const TransferableDataHelper& rDataHelper );
300 TransferableDataHelper& operator=( TransferableDataHelper&& rDataHelper );
302 const css::uno::Reference< css::datatransfer::XTransferable >& GetTransferable() const { return mxTransfer; }
303 css::uno::Reference< css::datatransfer::XTransferable > GetXTransferable() const;
305 bool HasFormat( SotClipboardFormatId nFormat ) const;
306 bool HasFormat( const css::datatransfer::DataFlavor& rFlavor ) const;
308 sal_uInt32 GetFormatCount() const;
310 SotClipboardFormatId GetFormat( sal_uInt32 nFormat ) const;
311 css::datatransfer::DataFlavor GetFormatDataFlavor( sal_uInt32 nFormat ) const;
313 const DataFlavorExVector& GetDataFlavorExVector() const {return maFormats; }
315 bool StartClipboardListening( );
316 void StopClipboardListening( );
318 void Rebind( const css::uno::Reference< css::datatransfer::XTransferable >& _rxNewData );
320 css::uno::Any GetAny( SotClipboardFormatId nFormat, const OUString& rDestDoc ) const;
321 css::uno::Any GetAny( const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc ) const;
323 bool GetString( SotClipboardFormatId nFormat, OUString& rStr );
324 bool GetString( const css::datatransfer::DataFlavor& rFlavor, OUString& rStr );
326 bool GetBitmapEx( SotClipboardFormatId nFormat, BitmapEx& rBmp );
327 bool GetBitmapEx( const css::datatransfer::DataFlavor& rFlavor, BitmapEx& rBmp );
329 /** Return as GDI metafile.
331 @param nMaxAction Allows you to limit the amount of actions; defaults to 0 which means no limit.
333 When you eg. Ctrl+a in Excel, you can get the entire sheet as
334 metafile, with over 3 million (!) actions; which is just too large for
335 any reasonable handling - and you need to set a limit.
337 bool GetGDIMetaFile( SotClipboardFormatId nFormat, GDIMetaFile& rMtf, size_t nMaxActions = 0 );
338 bool GetGDIMetaFile( const css::datatransfer::DataFlavor& rFlavor, GDIMetaFile& rMtf );
340 bool GetGraphic( SotClipboardFormatId nFormat, Graphic& rGraphic );
341 bool GetGraphic( const css::datatransfer::DataFlavor& rFlavor, Graphic& rGraphic );
343 bool GetImageMap( SotClipboardFormatId nFormat, ImageMap& rIMap );
344 bool GetImageMap( const css::datatransfer::DataFlavor& rFlavor, ImageMap& rImap );
346 bool GetTransferableObjectDescriptor( SotClipboardFormatId nFormat, TransferableObjectDescriptor& rDesc );
347 bool GetTransferableObjectDescriptor( TransferableObjectDescriptor& rDesc );
349 bool GetINetBookmark( SotClipboardFormatId nFormat, INetBookmark& rBmk );
350 bool GetINetBookmark( const css::datatransfer::DataFlavor& rFlavor, INetBookmark& rBmk );
352 bool GetINetImage( SotClipboardFormatId nFormat, INetImage& rINtImg );
353 bool GetINetImage( const css::datatransfer::DataFlavor& rFlavor, INetImage& rINtImg );
355 bool GetFileList( SotClipboardFormatId nFormat, FileList& rFileList );
356 bool GetFileList( FileList& rFileList );
358 css::uno::Sequence<sal_Int8> GetSequence( SotClipboardFormatId nFormat, const OUString& rDestDoc );
359 css::uno::Sequence<sal_Int8> GetSequence( const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc );
361 bool GetSotStorageStream( SotClipboardFormatId nFormat, tools::SvRef<SotTempStream>& rStreamRef );
362 bool GetSotStorageStream( const css::datatransfer::DataFlavor& rFlavor, tools::SvRef<SotTempStream>& rStreamRef );
364 css::uno::Reference<css::io::XInputStream> GetInputStream( SotClipboardFormatId nFormat, const OUString& rDestDoc );
365 css::uno::Reference<css::io::XInputStream> GetInputStream( const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc );
367 static TransferableDataHelper CreateFromClipboard(const css::uno::Reference<css::datatransfer::clipboard::XClipboard>& rClipboard);
368 static TransferableDataHelper CreateFromSystemClipboard( vcl::Window * pWindow );
369 static TransferableDataHelper CreateFromSelection( vcl::Window * pWindow );
370 static bool IsEqual( const css::datatransfer::DataFlavor& rInternalFlavor,
371 const css::datatransfer::DataFlavor& rRequestFlavor );
374 class VCL_DLLPUBLIC DragSourceHelper
376 private:
378 // nested class to implement the XDragGestureListener interface
379 class SAL_DLLPRIVATE DragGestureListener : public cppu::WeakImplHelper< css::datatransfer::dnd::XDragGestureListener >
381 private:
383 DragSourceHelper& mrParent;
385 private:
387 // XEventListener
388 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
390 // XDragGestureListener
391 virtual void SAL_CALL dragGestureRecognized( const css::datatransfer::dnd::DragGestureEvent& rDGE ) override;
393 public:
395 DragGestureListener( DragSourceHelper& rDragSourceHelper );
396 virtual ~DragGestureListener() override;
399 friend class DragSourceHelper::DragGestureListener;
401 private:
402 osl::Mutex maMutex;
403 css::uno::Reference< css::datatransfer::dnd::XDragGestureRecognizer > mxDragGestureRecognizer;
405 css::uno::Reference< css::datatransfer::dnd::XDragGestureListener > mxDragGestureListener;
407 DragSourceHelper& operator=( const DragSourceHelper& rDragSourceHelper ) = delete;
408 bool operator==( const DragSourceHelper& rDragSourceHelper ) const = delete;
410 public:
412 // to be overridden by the application
413 virtual void StartDrag( sal_Int8 nAction, const Point& rPosPixel );
415 DragSourceHelper( vcl::Window* pWindow );
416 void dispose();
417 virtual ~DragSourceHelper();
420 class VCL_DLLPUBLIC DropTargetHelper
422 private:
424 // nested class to implement the XDropTargetListener interface
425 class SAL_DLLPRIVATE DropTargetListener : public cppu::WeakImplHelper< css::datatransfer::dnd::XDropTargetListener >
427 private:
429 DropTargetHelper& mrParent;
430 std::unique_ptr<AcceptDropEvent> mpLastDragOverEvent;
432 // XEventListener
433 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
435 // XDropTargetListener
436 virtual void SAL_CALL drop( const css::datatransfer::dnd::DropTargetDropEvent& dtde ) override;
437 virtual void SAL_CALL dragEnter( const css::datatransfer::dnd::DropTargetDragEnterEvent& dtdee ) override;
438 virtual void SAL_CALL dragExit( const css::datatransfer::dnd::DropTargetEvent& dte ) override;
439 virtual void SAL_CALL dragOver( const css::datatransfer::dnd::DropTargetDragEvent& dtde ) override;
440 virtual void SAL_CALL dropActionChanged( const css::datatransfer::dnd::DropTargetDragEvent& dtde ) override;
442 public:
444 DropTargetListener( DropTargetHelper& rDropTargetHelper );
445 virtual ~DropTargetListener() override;
448 friend class DropTargetHelper::DropTargetListener;
450 private:
451 osl::Mutex maMutex;
452 css::uno::Reference< css::datatransfer::dnd::XDropTarget > mxDropTarget;
454 css::uno::Reference< css::datatransfer::dnd::XDropTargetListener > mxDropTargetListener;
455 DataFlavorExVector maFormats;
457 DropTargetHelper() = delete;
458 DropTargetHelper& operator=( const DropTargetHelper& rDropTargetHelper ) = delete;
459 bool operator==( const DropTargetHelper& rDropTargetHelper ) const = delete;
461 void ImplConstruct();
463 // called by our own implementation of XDropTargetListener (DropTargetListener instance)
464 void ImplBeginDrag( const css::uno::Sequence< css::datatransfer::DataFlavor >& rSupportedDataFlavors );
465 void ImplEndDrag();
467 public:
469 // to be overridden by the application
470 virtual sal_Int8 AcceptDrop( const AcceptDropEvent& rEvt );
471 virtual sal_Int8 ExecuteDrop( const ExecuteDropEvent& rEvt );
473 DropTargetHelper( vcl::Window* pWindow );
474 DropTargetHelper( const css::uno::Reference< css::datatransfer::dnd::XDropTarget >& rxDropTarget );
476 void dispose();
477 virtual ~DropTargetHelper();
479 // typically called by the application in ::AcceptDrop and ::ExecuteDrop and (see above)
480 bool IsDropFormatSupported(SotClipboardFormatId nFormat) const;
482 const DataFlavorExVector& GetDataFlavorExVector() const {return maFormats; }
486 struct TransferDataContainer_Impl;
488 class VCL_DLLPUBLIC TransferDataContainer : public TransferableHelper
490 std::unique_ptr<TransferDataContainer_Impl> pImpl;
492 virtual void AddSupportedFormats() override;
493 virtual bool GetData( const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc ) override;
495 public:
497 TransferDataContainer();
498 virtual ~TransferDataContainer() override;
500 void CopyINetBookmark( const INetBookmark& rBkmk );
501 void CopyString( const OUString& rStr );
502 void CopyString( SotClipboardFormatId nFmt, const OUString& rStr );
504 void CopyByteString( SotClipboardFormatId nFormatId, const OString& rStr );
505 void CopyAnyData( SotClipboardFormatId nFormatId, const char* pData, sal_uLong nLen );
507 bool HasAnyData() const;
509 using TransferableHelper::StartDrag;
510 void StartDrag( vcl::Window* pWindow, sal_Int8 nDragSourceActions,
511 const Link<sal_Int8,void>& rCallbck );
512 virtual void DragFinished( sal_Int8 nDropAction ) override;
515 css::uno::Reference<css::datatransfer::clipboard::XClipboard> VCL_DLLPUBLIC GetSystemClipboard();
516 css::uno::Reference<css::datatransfer::clipboard::XClipboard> VCL_DLLPUBLIC GetSystemPrimarySelection();
518 #endif
520 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */