1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #if defined(_MSC_VER) && (_MSC_VER > 1310)
28*cdf0e10cSrcweir #pragma warning(disable : 4917 4555)
29*cdf0e10cSrcweir #endif
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // actually this workaround should be in presys.h!
32*cdf0e10cSrcweir //#define UINT64 USE_WIN_UINT64
33*cdf0e10cSrcweir //#define INT64 USE_WIN_INT64
34*cdf0e10cSrcweir //#define UINT32 USE_WIN_UINT32
35*cdf0e10cSrcweir //#define INT32 USE_WIN_INT32
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir //#include <tools/presys.h>
38*cdf0e10cSrcweir #include "embeddoc.hxx"
39*cdf0e10cSrcweir //#include <tools/postsys.h>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir //#undef UINT64
42*cdf0e10cSrcweir //#undef INT64
43*cdf0e10cSrcweir //#undef UINT32
44*cdf0e10cSrcweir //#undef INT32
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <com/sun/star/uno/Any.h>
48*cdf0e10cSrcweir #include <com/sun/star/uno/Exception.hpp>
49*cdf0e10cSrcweir #include <com/sun/star/datatransfer/XTransferable.hpp>
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include <osl/thread.h>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir using namespace ::com::sun::star;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir //===============================================================================
57*cdf0e10cSrcweir // EmbedDocument_Impl
58*cdf0e10cSrcweir //===============================================================================
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir sal_uInt64 EmbedDocument_Impl::getMetaFileHandle_Impl( sal_Bool isEnhMeta )
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir 	sal_uInt64 pResult = NULL;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 	uno::Reference< datatransfer::XTransferable > xTransferable( m_pDocHolder->GetDocument(), uno::UNO_QUERY );
65*cdf0e10cSrcweir 	if ( xTransferable.is() )
66*cdf0e10cSrcweir 	{
67*cdf0e10cSrcweir 		uno::Sequence< sal_Int8 > aMetaBuffer;
68*cdf0e10cSrcweir 		datatransfer::DataFlavor aFlavor;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 		if ( isEnhMeta )
71*cdf0e10cSrcweir 		{
72*cdf0e10cSrcweir 			aFlavor.MimeType = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
73*cdf0e10cSrcweir 												"application/x-openoffice-emf;windows_formatname=\"Image EMF\"" ) );
74*cdf0e10cSrcweir 			aFlavor.HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Enhanced Windows MetaFile" ) );
75*cdf0e10cSrcweir 		}
76*cdf0e10cSrcweir 		else
77*cdf0e10cSrcweir 		{
78*cdf0e10cSrcweir 			aFlavor.MimeType = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
79*cdf0e10cSrcweir 												"application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ) );
80*cdf0e10cSrcweir 			aFlavor.HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Windows GDIMetaFile" ) );
81*cdf0e10cSrcweir 		}
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 		aFlavor.DataType = getCppuType( (const sal_uInt64*) 0 );
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 		uno::Any aAny = xTransferable->getTransferData( aFlavor );
86*cdf0e10cSrcweir 		aAny >>= pResult;
87*cdf0e10cSrcweir 	}
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 	return pResult;
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir //-------------------------------------------------------------------------------
93*cdf0e10cSrcweir // IDataObject
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::GetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	if ( !pFormatetc )
98*cdf0e10cSrcweir 		return DV_E_FORMATETC;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	if ( !pMedium )
101*cdf0e10cSrcweir 		return STG_E_MEDIUMFULL;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
104*cdf0e10cSrcweir 	  || pFormatetc->dwAspect == DVASPECT_ICON
105*cdf0e10cSrcweir 	  || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
106*cdf0e10cSrcweir 		return DV_E_DVASPECT;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 	if ( pFormatetc->cfFormat == CF_ENHMETAFILE )
109*cdf0e10cSrcweir 	{
110*cdf0e10cSrcweir 		if ( !( pFormatetc->tymed & TYMED_ENHMF ) )
111*cdf0e10cSrcweir 			return DV_E_TYMED;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 		HENHMETAFILE hMeta = reinterpret_cast<HENHMETAFILE>( getMetaFileHandle_Impl( sal_True ) );
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 		if ( hMeta )
116*cdf0e10cSrcweir 		{
117*cdf0e10cSrcweir 			pMedium->tymed = TYMED_ENHMF;
118*cdf0e10cSrcweir 			pMedium->hEnhMetaFile = hMeta;
119*cdf0e10cSrcweir 			pMedium->pUnkForRelease = NULL;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 			return S_OK;
122*cdf0e10cSrcweir 		}
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 		return STG_E_MEDIUMFULL;
125*cdf0e10cSrcweir 	}
126*cdf0e10cSrcweir 	else if ( pFormatetc->cfFormat == CF_METAFILEPICT )
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 	  	if ( !( pFormatetc->tymed & TYMED_MFPICT ) )
129*cdf0e10cSrcweir 			return DV_E_TYMED;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 		HGLOBAL hMeta = reinterpret_cast<HGLOBAL>( getMetaFileHandle_Impl( sal_False ) );
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 		if ( hMeta )
134*cdf0e10cSrcweir 		{
135*cdf0e10cSrcweir 			pMedium->tymed = TYMED_MFPICT;
136*cdf0e10cSrcweir 			pMedium->hMetaFilePict = hMeta;
137*cdf0e10cSrcweir 			pMedium->pUnkForRelease = NULL;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 			return S_OK;
140*cdf0e10cSrcweir 		}
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 		return STG_E_MEDIUMFULL;
143*cdf0e10cSrcweir 	}
144*cdf0e10cSrcweir 	else
145*cdf0e10cSrcweir 	{
146*cdf0e10cSrcweir 		CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
147*cdf0e10cSrcweir 		CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
148*cdf0e10cSrcweir 		if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
149*cdf0e10cSrcweir 		{
150*cdf0e10cSrcweir 			if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
151*cdf0e10cSrcweir 				return DV_E_TYMED;
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 			CComPtr< IStorage > pNewStg;
154*cdf0e10cSrcweir 			HRESULT hr = StgCreateDocfile( NULL, STGM_CREATE | STGM_READWRITE | STGM_DELETEONRELEASE, 0, &pNewStg );
155*cdf0e10cSrcweir 			if ( FAILED( hr ) || !pNewStg ) return STG_E_MEDIUMFULL;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 			hr = SaveTo_Impl( pNewStg );
158*cdf0e10cSrcweir 			if ( FAILED( hr ) ) return STG_E_MEDIUMFULL;
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 			pMedium->tymed = TYMED_ISTORAGE;
161*cdf0e10cSrcweir 			pMedium->pstg = pNewStg;
162*cdf0e10cSrcweir 			pMedium->pstg->AddRef();
163*cdf0e10cSrcweir 			pMedium->pUnkForRelease = ( IUnknown* )pNewStg;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 			return S_OK;
166*cdf0e10cSrcweir 		}
167*cdf0e10cSrcweir 	}
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 	return DV_E_FORMATETC;
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::GetDataHere( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
173*cdf0e10cSrcweir {
174*cdf0e10cSrcweir 	if ( !pFormatetc )
175*cdf0e10cSrcweir 		return DV_E_FORMATETC;
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 	if ( !pMedium )
178*cdf0e10cSrcweir 		return STG_E_MEDIUMFULL;
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 	if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
181*cdf0e10cSrcweir 	  || pFormatetc->dwAspect == DVASPECT_ICON
182*cdf0e10cSrcweir 	  || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
183*cdf0e10cSrcweir 		return DV_E_DVASPECT;
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
186*cdf0e10cSrcweir 	CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 	if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
189*cdf0e10cSrcweir 	{
190*cdf0e10cSrcweir 		if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
191*cdf0e10cSrcweir 			return DV_E_TYMED;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 		if ( !pMedium->pstg ) return STG_E_MEDIUMFULL;
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 		HRESULT hr = SaveTo_Impl( pMedium->pstg );
196*cdf0e10cSrcweir 		if ( FAILED( hr ) ) return STG_E_MEDIUMFULL;
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 		pMedium->tymed = TYMED_ISTORAGE;
199*cdf0e10cSrcweir 		pMedium->pUnkForRelease = NULL;
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 		return S_OK;
202*cdf0e10cSrcweir 	}
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 	return DV_E_FORMATETC;
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::QueryGetData( FORMATETC * pFormatetc )
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir 	if ( pFormatetc )
210*cdf0e10cSrcweir 	{
211*cdf0e10cSrcweir 		if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
212*cdf0e10cSrcweir 		  || pFormatetc->dwAspect == DVASPECT_ICON
213*cdf0e10cSrcweir 		  || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
214*cdf0e10cSrcweir 			return DV_E_DVASPECT;
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 		if ( pFormatetc->cfFormat == CF_ENHMETAFILE )
217*cdf0e10cSrcweir 		{
218*cdf0e10cSrcweir 			if ( !( pFormatetc->tymed & TYMED_ENHMF ) )
219*cdf0e10cSrcweir 				return DV_E_TYMED;
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 			return S_OK;
222*cdf0e10cSrcweir 		}
223*cdf0e10cSrcweir 		else if ( pFormatetc->cfFormat == CF_METAFILEPICT )
224*cdf0e10cSrcweir 		{
225*cdf0e10cSrcweir 	  		if ( !( pFormatetc->tymed & TYMED_MFPICT ) )
226*cdf0e10cSrcweir 				return DV_E_TYMED;
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 			return S_OK;
229*cdf0e10cSrcweir 		}
230*cdf0e10cSrcweir 		else
231*cdf0e10cSrcweir 		{
232*cdf0e10cSrcweir 			CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
233*cdf0e10cSrcweir 			CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
234*cdf0e10cSrcweir 			if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
235*cdf0e10cSrcweir 			{
236*cdf0e10cSrcweir 				if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
237*cdf0e10cSrcweir 					return DV_E_TYMED;
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 				return S_OK;
240*cdf0e10cSrcweir 			}
241*cdf0e10cSrcweir 		}
242*cdf0e10cSrcweir 	}
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 	return DV_E_FORMATETC;
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::GetCanonicalFormatEtc( FORMATETC * pFormatetcIn, FORMATETC * pFormatetcOut )
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir 	if ( !pFormatetcIn || !pFormatetcOut )
251*cdf0e10cSrcweir 		return DV_E_FORMATETC;
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir 	pFormatetcOut->ptd = NULL;
254*cdf0e10cSrcweir 	pFormatetcOut->cfFormat = pFormatetcIn->cfFormat;
255*cdf0e10cSrcweir 	pFormatetcOut->dwAspect = DVASPECT_CONTENT;
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir 	if ( pFormatetcIn->cfFormat == CF_ENHMETAFILE )
258*cdf0e10cSrcweir 	{
259*cdf0e10cSrcweir 		pFormatetcOut->tymed = TYMED_ENHMF;
260*cdf0e10cSrcweir 		return S_OK;
261*cdf0e10cSrcweir 	}
262*cdf0e10cSrcweir 	else if ( pFormatetcIn->cfFormat == CF_METAFILEPICT )
263*cdf0e10cSrcweir 	{
264*cdf0e10cSrcweir   		pFormatetcOut->tymed = TYMED_MFPICT;
265*cdf0e10cSrcweir 		return S_OK;
266*cdf0e10cSrcweir 	}
267*cdf0e10cSrcweir 	else
268*cdf0e10cSrcweir 	{
269*cdf0e10cSrcweir 		CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
270*cdf0e10cSrcweir 		CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
271*cdf0e10cSrcweir 		if ( pFormatetcIn->cfFormat == cf_embSource || pFormatetcIn->cfFormat == cf_embObj )
272*cdf0e10cSrcweir 		{
273*cdf0e10cSrcweir 			pFormatetcOut->tymed = TYMED_ISTORAGE;
274*cdf0e10cSrcweir 			return S_OK;
275*cdf0e10cSrcweir 		}
276*cdf0e10cSrcweir 	}
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 	return DV_E_FORMATETC;
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::SetData( FORMATETC * /*pFormatetc*/, STGMEDIUM * /*pMedium*/, BOOL /*fRelease*/ )
282*cdf0e10cSrcweir {
283*cdf0e10cSrcweir 	return E_NOTIMPL;
284*cdf0e10cSrcweir }
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC ** /*ppFormatetc*/ )
287*cdf0e10cSrcweir {
288*cdf0e10cSrcweir 	if ( dwDirection == DATADIR_GET )
289*cdf0e10cSrcweir 		return OLE_S_USEREG;
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir 	return E_NOTIMPL;
292*cdf0e10cSrcweir }
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::DAdvise( FORMATETC * pFormatetc, DWORD advf, IAdviseSink * pAdvSink, DWORD * pdwConnection )
295*cdf0e10cSrcweir {
296*cdf0e10cSrcweir 	if ( !m_pDAdviseHolder )
297*cdf0e10cSrcweir 		if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
298*cdf0e10cSrcweir 			return E_OUTOFMEMORY;
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir 	return m_pDAdviseHolder->Advise( (IDataObject*)this, pFormatetc, advf, pAdvSink, pdwConnection );
301*cdf0e10cSrcweir }
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::DUnadvise( DWORD dwConnection )
304*cdf0e10cSrcweir {
305*cdf0e10cSrcweir 	if ( !m_pDAdviseHolder )
306*cdf0e10cSrcweir 		if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
307*cdf0e10cSrcweir 			return E_OUTOFMEMORY;
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir 	return m_pDAdviseHolder->Unadvise( dwConnection );
310*cdf0e10cSrcweir }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir STDMETHODIMP EmbedDocument_Impl::EnumDAdvise( IEnumSTATDATA ** ppenumAdvise )
313*cdf0e10cSrcweir {
314*cdf0e10cSrcweir 	if ( !m_pDAdviseHolder )
315*cdf0e10cSrcweir 		if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
316*cdf0e10cSrcweir 			return E_OUTOFMEMORY;
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir 	return m_pDAdviseHolder->EnumAdvise( ppenumAdvise );
319*cdf0e10cSrcweir }
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir // Fix strange warnings about some
322*cdf0e10cSrcweir // ATL::CAxHostWindow::QueryInterface|AddRef|Releae functions.
323*cdf0e10cSrcweir // warning C4505: 'xxx' : unreferenced local function has been removed
324*cdf0e10cSrcweir #if defined(_MSC_VER)
325*cdf0e10cSrcweir #pragma warning(disable: 4505)
326*cdf0e10cSrcweir #endif
327