1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _XTDATAOBJECT_HXX_
25 #define _XTDATAOBJECT_HXX_
26 
27 
28 //------------------------------------------------------------------------
29 // includes
30 //------------------------------------------------------------------------
31 
32 #include <com/sun/star/datatransfer/XTransferable.hpp>
33 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
34 
35 #ifndef _DATAFORMATTRANSLATOR_HXX_
36 #include "DataFmtTransl.hxx"
37 #endif
38 
39 #ifndef _FETCLIST_HXX_
40 #include "FetcList.hxx"
41 #endif
42 
43 #if defined _MSC_VER
44 #pragma warning(push,1)
45 #endif
46 #include <windows.h>
47 #include <ole2.h>
48 #include <objidl.h>
49 #if defined _MSC_VER
50 #pragma warning(pop)
51 #endif
52 
53 /*--------------------------------------------------------------------------
54 	- the function principle of the windows clipboard:
55 	  a data provider offers all formats he can deliver on the clipboard
56 	  a clipboard client ask for the available formats on the clipboard
57 	  and decides if there is a format he can use
58 	  if there is one, he requests the data in this format
59 
60 	- This class inherits from IDataObject an so can be placed on the
61 	  OleClipboard. The class wraps a transferable object which is the
62 	  original DataSource
63 	- DataFlavors offered by this transferable will be translated into
64 	  appropriate clipboard formats
65 	- if the transferable contains text data always text and unicodetext
66 	  will be offered or vice versa
67 	- text data will be automatically converted between text and unicode text
68 	- although the transferable may support text in different charsets
69 	  (codepages) only text in one codepage can be offered by the clipboard
70 
71 ----------------------------------------------------------------------------*/
72 
73 class CStgTransferHelper;
74 
75 class CXTDataObject : public IDataObject
76 {
77 public:
78 	CXTDataObject( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& aServiceManager,
79 				   const ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable >& aXTransferable );
~CXTDataObject()80 	virtual ~CXTDataObject() {}
81 
82 	//-----------------------------------------------------------------
83 	// ole interface implementation
84 	//-----------------------------------------------------------------
85 
86 	//IUnknown interface methods
87 	STDMETHODIMP           QueryInterface(REFIID iid, LPVOID* ppvObject);
88 	STDMETHODIMP_( ULONG ) AddRef( );
89 	STDMETHODIMP_( ULONG ) Release( );
90 
91 	// IDataObject interface methods
92 	STDMETHODIMP GetData( LPFORMATETC pFormatetc, LPSTGMEDIUM pmedium );
93 	STDMETHODIMP GetDataHere( LPFORMATETC pFormatetc, LPSTGMEDIUM pmedium );
94 	STDMETHODIMP QueryGetData( LPFORMATETC pFormatetc );
95 	STDMETHODIMP GetCanonicalFormatEtc( LPFORMATETC pFormatectIn, LPFORMATETC pFormatetcOut );
96 	STDMETHODIMP SetData( LPFORMATETC pFormatetc, LPSTGMEDIUM pmedium, BOOL fRelease );
97 	STDMETHODIMP EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC** ppenumFormatetc );
98 	STDMETHODIMP DAdvise( LPFORMATETC pFormatetc, DWORD advf, LPADVISESINK pAdvSink, DWORD* pdwConnection );
99 	STDMETHODIMP DUnadvise( DWORD dwConnection );
100 	STDMETHODIMP EnumDAdvise( LPENUMSTATDATA* ppenumAdvise );
101 
102 	operator IDataObject*( );
103 
104 private:
105 	com::sun::star::datatransfer::DataFlavor SAL_CALL formatEtcToDataFlavor( const FORMATETC& aFormatEtc ) const;
106 
107 	void SAL_CALL renderDataAndSetupStgMedium( const sal_Int8* lpStorage,
108 											   const FORMATETC& fetc,
109 											   sal_uInt32 nInitStgSize,
110 											   sal_uInt32 nBytesToTransfer,
111 											   STGMEDIUM& stgmedium );
112 
113 	void SAL_CALL renderLocaleAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
114 	void SAL_CALL renderUnicodeAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
115 	void SAL_CALL renderAnyDataAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
116 
117 	HRESULT SAL_CALL renderSynthesizedFormatAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
118 	void    SAL_CALL renderSynthesizedUnicodeAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
119 	void    SAL_CALL renderSynthesizedTextAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
120 	void    SAL_CALL renderSynthesizedHtmlAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
121 
122 	void SAL_CALL setupStgMedium( const FORMATETC& fetc,
123 								  CStgTransferHelper& stgTransHlp,
124 								  STGMEDIUM& stgmedium );
125 
126 	void validateFormatEtc( LPFORMATETC lpFormatEtc ) const;
127 	void SAL_CALL invalidateStgMedium( STGMEDIUM& stgmedium ) const;
128 
129 	HRESULT SAL_CALL translateStgExceptionCode( HRESULT hr ) const;
130 
131 	inline void SAL_CALL InitializeFormatEtcContainer( );
132 
133 private:
134 	LONG m_nRefCnt;
135 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_SrvMgr;
136 	::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable >      m_XTransferable;
137 	CFormatEtcContainer																       m_FormatEtcContainer;
138 	sal_Bool                                                                               m_bFormatEtcContainerInitialized;
139 	CDataFormatTranslator															       m_DataFormatTranslator;
140 	CFormatRegistrar																       m_FormatRegistrar;
141 };
142 
143 //------------------------------------------------------------------------
144 //
145 //------------------------------------------------------------------------
146 
147 class CEnumFormatEtc : public IEnumFORMATETC
148 {
149 public:
150 	CEnumFormatEtc( LPUNKNOWN lpUnkOuter, const CFormatEtcContainer& aFormatEtcContainer );
~CEnumFormatEtc()151 	virtual ~CEnumFormatEtc() {}
152 
153 	// IUnknown
154 	STDMETHODIMP           QueryInterface( REFIID iid, LPVOID* ppvObject );
155 	STDMETHODIMP_( ULONG ) AddRef( );
156 	STDMETHODIMP_( ULONG ) Release( );
157 
158 	// IEnumFORMATETC
159 	STDMETHODIMP Next( ULONG nRequested, LPFORMATETC lpDest, ULONG* lpFetched );
160 	STDMETHODIMP Skip( ULONG celt );
161 	STDMETHODIMP Reset( );
162 	STDMETHODIMP Clone( IEnumFORMATETC** ppenum );
163 
164 private:
165 	LONG	            m_nRefCnt;
166 	LPUNKNOWN           m_lpUnkOuter;
167 	CFormatEtcContainer m_FormatEtcContainer;
168 };
169 
170 typedef CEnumFormatEtc *PCEnumFormatEtc;
171 
172 #endif
173