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 
25 #ifndef _XTDATAOBJECT_HXX_
26 #define _XTDATAOBJECT_HXX_
27 
28 
29 //------------------------------------------------------------------------
30 // includes
31 //------------------------------------------------------------------------
32 
33 #include <com/sun/star/datatransfer/XTransferable.hpp>
34 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
35 
36 #ifndef _DATAFORMATTRANSLATOR_HXX_
37 #include "DataFmtTransl.hxx"
38 #endif
39 
40 #ifndef _FETCLIST_HXX_
41 #include "FetcList.hxx"
42 #endif
43 
44 #if defined _MSC_VER
45 #pragma warning(push,1)
46 #endif
47 #include <windows.h>
48 #include <ole2.h>
49 #include <objidl.h>
50 #if defined _MSC_VER
51 #pragma warning(pop)
52 #endif
53 
54 /*--------------------------------------------------------------------------
55 	- the function principle of the windows clipboard:
56 	  a data provider offers all formats he can deliver on the clipboard
57 	  a clipboard client ask for the available formats on the clipboard
58 	  and decides if there is a format he can use
59 	  if there is one, he requests the data in this format
60 
61 	- This class inherits from IDataObject an so can be placed on the
62 	  OleClipboard. The class wrapps a transferable object which is the
63 	  original DataSource
64 	- DataFlavors offerd by this transferable will be translated into
65 	  appropriate clipboard formats
66 	- if the transferable contains text data always text and unicodetext
67 	  will be offered or vice versa
68 	- text data will be automaticaly converted between text und unicode text
69 	- although the transferable may support text in different charsets
70 	  (codepages) only text in one codepage can be offered by the clipboard
71 
72 ----------------------------------------------------------------------------*/
73 
74 class CStgTransferHelper;
75 
76 class CXTDataObject : public IDataObject
77 {
78 public:
79 	CXTDataObject( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& aServiceManager,
80 				   const ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable >& aXTransferable );
81     virtual ~CXTDataObject() {}
82 
83 	//-----------------------------------------------------------------
84 	// ole interface implementation
85 	//-----------------------------------------------------------------
86 
87     //IUnknown interface methods
88     STDMETHODIMP           QueryInterface(REFIID iid, LPVOID* ppvObject);
89     STDMETHODIMP_( ULONG ) AddRef( );
90     STDMETHODIMP_( ULONG ) Release( );
91 
92     // IDataObject interface methods
93     STDMETHODIMP GetData( LPFORMATETC pFormatetc, LPSTGMEDIUM pmedium );
94     STDMETHODIMP GetDataHere( LPFORMATETC pFormatetc, LPSTGMEDIUM pmedium );
95     STDMETHODIMP QueryGetData( LPFORMATETC pFormatetc );
96     STDMETHODIMP GetCanonicalFormatEtc( LPFORMATETC pFormatectIn, LPFORMATETC pFormatetcOut );
97     STDMETHODIMP SetData( LPFORMATETC pFormatetc, LPSTGMEDIUM pmedium, BOOL fRelease );
98     STDMETHODIMP EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC** ppenumFormatetc );
99     STDMETHODIMP DAdvise( LPFORMATETC pFormatetc, DWORD advf, LPADVISESINK pAdvSink, DWORD* pdwConnection );
100     STDMETHODIMP DUnadvise( DWORD dwConnection );
101     STDMETHODIMP EnumDAdvise( LPENUMSTATDATA* ppenumAdvise );
102 
103 	operator IDataObject*( );
104 
105 private:
106 	com::sun::star::datatransfer::DataFlavor SAL_CALL formatEtcToDataFlavor( const FORMATETC& aFormatEtc ) const;
107 
108 	void SAL_CALL renderDataAndSetupStgMedium( const sal_Int8* lpStorage,
109 											   const FORMATETC& fetc,
110 											   sal_uInt32 nInitStgSize,
111 											   sal_uInt32 nBytesToTransfer,
112 											   STGMEDIUM& stgmedium );
113 
114 	void SAL_CALL renderLocaleAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
115 	void SAL_CALL renderUnicodeAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
116 	void SAL_CALL renderAnyDataAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
117 
118 	HRESULT SAL_CALL renderSynthesizedFormatAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
119 	void    SAL_CALL renderSynthesizedUnicodeAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
120 	void    SAL_CALL renderSynthesizedTextAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
121 	void    SAL_CALL renderSynthesizedHtmlAndSetupStgMedium( FORMATETC& fetc, STGMEDIUM& stgmedium );
122 
123 	void SAL_CALL setupStgMedium( const FORMATETC& fetc,
124 								  CStgTransferHelper& stgTransHlp,
125 								  STGMEDIUM& stgmedium );
126 
127 	void validateFormatEtc( LPFORMATETC lpFormatEtc ) const;
128 	void SAL_CALL invalidateStgMedium( STGMEDIUM& stgmedium ) const;
129 
130 	HRESULT SAL_CALL translateStgExceptionCode( HRESULT hr ) const;
131 
132     inline void SAL_CALL InitializeFormatEtcContainer( );
133 
134 private:
135 	LONG m_nRefCnt;
136 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_SrvMgr;
137 	::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable >      m_XTransferable;
138 	CFormatEtcContainer																       m_FormatEtcContainer;
139     sal_Bool                                                                               m_bFormatEtcContainerInitialized;
140 	CDataFormatTranslator															       m_DataFormatTranslator;
141 	CFormatRegistrar																       m_FormatRegistrar;
142 };
143 
144 //------------------------------------------------------------------------
145 //
146 //------------------------------------------------------------------------
147 
148 class CEnumFormatEtc : public IEnumFORMATETC
149 {
150 public:
151 	CEnumFormatEtc( LPUNKNOWN lpUnkOuter, const CFormatEtcContainer& aFormatEtcContainer );
152     virtual ~CEnumFormatEtc() {}
153 
154     // IUnknown
155     STDMETHODIMP           QueryInterface( REFIID iid, LPVOID* ppvObject );
156     STDMETHODIMP_( ULONG ) AddRef( );
157     STDMETHODIMP_( ULONG ) Release( );
158 
159     //IEnumFORMATETC
160     STDMETHODIMP Next( ULONG nRequested, LPFORMATETC lpDest, ULONG* lpFetched );
161     STDMETHODIMP Skip( ULONG celt );
162     STDMETHODIMP Reset( );
163     STDMETHODIMP Clone( IEnumFORMATETC** ppenum );
164 
165 private:
166 	LONG	            m_nRefCnt;
167 	LPUNKNOWN           m_lpUnkOuter;
168 	CFormatEtcContainer m_FormatEtcContainer;
169 };
170 
171 typedef CEnumFormatEtc *PCEnumFormatEtc;
172 
173 #endif
174