1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 #ifndef _XTDATAOBJECT_HXX_
30 #define _XTDATAOBJECT_HXX_
31 
32 
33 //------------------------------------------------------------------------
34 // includes
35 //------------------------------------------------------------------------
36 
37 #include <com/sun/star/datatransfer/XTransferable.hpp>
38 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
39 
40 #ifndef _DATAFORMATTRANSLATOR_HXX_
41 #include "DataFmtTransl.hxx"
42 #endif
43 
44 #ifndef _FETCLIST_HXX_
45 #include "FetcList.hxx"
46 #endif
47 
48 #if defined _MSC_VER
49 #pragma warning(push,1)
50 #endif
51 #include <windows.h>
52 #include <ole2.h>
53 #include <objidl.h>
54 #if defined _MSC_VER
55 #pragma warning(pop)
56 #endif
57 
58 /*--------------------------------------------------------------------------
59 	- the function principle of the windows clipboard:
60 	  a data provider offers all formats he can deliver on the clipboard
61 	  a clipboard client ask for the available formats on the clipboard
62 	  and decides if there is a format he can use
63 	  if there is one, he requests the data in this format
64 
65 	- This class inherits from IDataObject an so can be placed on the
66 	  OleClipboard. The class wrapps a transferable object which is the
67 	  original DataSource
68 	- DataFlavors offerd by this transferable will be translated into
69 	  appropriate clipboard formats
70 	- if the transferable contains text data always text and unicodetext
71 	  will be offered or vice versa
72 	- text data will be automaticaly converted between text und unicode text
73 	- although the transferable may support text in different charsets
74 	  (codepages) only text in one codepage can be offered by the clipboard
75 
76 ----------------------------------------------------------------------------*/
77 
78 class CStgTransferHelper;
79 
80 class CXTDataObject : public IDataObject
81 {
82 public:
83 	CXTDataObject( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& aServiceManager,
84 				   const ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable >& aXTransferable );
85     virtual ~CXTDataObject() {}
86 
87 	//-----------------------------------------------------------------
88 	// ole interface implementation
89 	//-----------------------------------------------------------------
90 
91     //IUnknown interface methods
92     STDMETHODIMP           QueryInterface(REFIID iid, LPVOID* ppvObject);
93     STDMETHODIMP_( ULONG ) AddRef( );
94     STDMETHODIMP_( ULONG ) Release( );
95 
96     // IDataObject interface methods
97     STDMETHODIMP GetData( LPFORMATETC pFormatetc, LPSTGMEDIUM pmedium );
98     STDMETHODIMP GetDataHere( LPFORMATETC pFormatetc, LPSTGMEDIUM pmedium );
99     STDMETHODIMP QueryGetData( LPFORMATETC pFormatetc );
100     STDMETHODIMP GetCanonicalFormatEtc( LPFORMATETC pFormatectIn, LPFORMATETC pFormatetcOut );
101     STDMETHODIMP SetData( LPFORMATETC pFormatetc, LPSTGMEDIUM pmedium, BOOL fRelease );
102     STDMETHODIMP EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC** ppenumFormatetc );
103     STDMETHODIMP DAdvise( LPFORMATETC pFormatetc, DWORD advf, LPADVISESINK pAdvSink, DWORD* pdwConnection );
104     STDMETHODIMP DUnadvise( DWORD dwConnection );
105     STDMETHODIMP EnumDAdvise( LPENUMSTATDATA* ppenumAdvise );
106 
107 	operator IDataObject*( );
108 
109 private:
110 	com::sun::star::datatransfer::DataFlavor SAL_CALL formatEtcToDataFlavor( const FORMATETC& aFormatEtc ) const;
111 
112 	void SAL_CALL renderDataAndSetupStgMedium( const sal_Int8* lpStorage,
113 											   const FORMATETC& fetc,
114 											   sal_uInt32 nInitStgSize,
115 											   sal_uInt32 nBytesToTransfer,
116 											   STGMEDIUM& stgmedium );
117 
118 	void SAL_CALL renderLocaleAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
119 	void SAL_CALL renderUnicodeAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
120 	void SAL_CALL renderAnyDataAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
121 
122 	HRESULT SAL_CALL renderSynthesizedFormatAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
123 	void    SAL_CALL renderSynthesizedUnicodeAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
124 	void    SAL_CALL renderSynthesizedTextAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
125 	void    SAL_CALL renderSynthesizedHtmlAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
126 
127 	void SAL_CALL setupStgMedium( const FORMATETC& fetc,
128 								  CStgTransferHelper& stgTransHlp,
129 								  STGMEDIUM& stgmedium );
130 
131 	void validateFormatEtc( LPFORMATETC lpFormatEtc ) const;
132 	void SAL_CALL invalidateStgMedium( STGMEDIUM& stgmedium ) const;
133 
134 	HRESULT SAL_CALL translateStgExceptionCode( HRESULT hr ) const;
135 
136     inline void SAL_CALL InitializeFormatEtcContainer( );
137 
138 private:
139 	LONG m_nRefCnt;
140 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_SrvMgr;
141 	::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable >      m_XTransferable;
142 	CFormatEtcContainer																       m_FormatEtcContainer;
143     sal_Bool                                                                               m_bFormatEtcContainerInitialized;
144 	CDataFormatTranslator															       m_DataFormatTranslator;
145 	CFormatRegistrar																       m_FormatRegistrar;
146 };
147 
148 //------------------------------------------------------------------------
149 //
150 //------------------------------------------------------------------------
151 
152 class CEnumFormatEtc : public IEnumFORMATETC
153 {
154 public:
155 	CEnumFormatEtc( LPUNKNOWN lpUnkOuter, const CFormatEtcContainer& aFormatEtcContainer );
156     virtual ~CEnumFormatEtc() {}
157 
158     // IUnknown
159     STDMETHODIMP           QueryInterface( REFIID iid, LPVOID* ppvObject );
160     STDMETHODIMP_( ULONG ) AddRef( );
161     STDMETHODIMP_( ULONG ) Release( );
162 
163     //IEnumFORMATETC
164     STDMETHODIMP Next( ULONG nRequested, LPFORMATETC lpDest, ULONG* lpFetched );
165     STDMETHODIMP Skip( ULONG celt );
166     STDMETHODIMP Reset( );
167     STDMETHODIMP Clone( IEnumFORMATETC** ppenum );
168 
169 private:
170 	LONG	            m_nRefCnt;
171 	LPUNKNOWN           m_lpUnkOuter;
172 	CFormatEtcContainer m_FormatEtcContainer;
173 };
174 
175 typedef CEnumFormatEtc *PCEnumFormatEtc;
176 
177 #endif
178