Update ooo320-m1
[ooovba.git] / embedserv / source / embed / ed_idataobj.cxx
blob30222e0e65fdd8b5fe128f773568513f8c726ed8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ed_idataobj.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #if defined(_MSC_VER) && (_MSC_VER > 1310)
31 #pragma warning(disable : 4917 4555)
32 #endif
34 // actually this workaround should be in presys.h!
35 //#define UINT64 USE_WIN_UINT64
36 //#define INT64 USE_WIN_INT64
37 //#define UINT32 USE_WIN_UINT32
38 //#define INT32 USE_WIN_INT32
40 //#include <tools/presys.h>
41 #include "embeddoc.hxx"
42 //#include <tools/postsys.h>
44 //#undef UINT64
45 //#undef INT64
46 //#undef UINT32
47 //#undef INT32
50 #include <com/sun/star/uno/Any.h>
51 #include <com/sun/star/uno/Exception.hpp>
52 #include <com/sun/star/datatransfer/XTransferable.hpp>
55 #include <osl/thread.h>
57 using namespace ::com::sun::star;
59 //===============================================================================
60 // EmbedDocument_Impl
61 //===============================================================================
63 sal_uInt64 EmbedDocument_Impl::getMetaFileHandle_Impl( sal_Bool isEnhMeta )
65 sal_uInt64 pResult = NULL;
67 uno::Reference< datatransfer::XTransferable > xTransferable( m_pDocHolder->GetDocument(), uno::UNO_QUERY );
68 if ( xTransferable.is() )
70 uno::Sequence< sal_Int8 > aMetaBuffer;
71 datatransfer::DataFlavor aFlavor;
73 if ( isEnhMeta )
75 aFlavor.MimeType = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
76 "application/x-openoffice-emf;windows_formatname=\"Image EMF\"" ) );
77 aFlavor.HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Enhanced Windows MetaFile" ) );
79 else
81 aFlavor.MimeType = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
82 "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ) );
83 aFlavor.HumanPresentableName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Windows GDIMetaFile" ) );
86 aFlavor.DataType = getCppuType( (const sal_uInt64*) 0 );
88 uno::Any aAny = xTransferable->getTransferData( aFlavor );
89 aAny >>= pResult;
92 return pResult;
95 //-------------------------------------------------------------------------------
96 // IDataObject
98 STDMETHODIMP EmbedDocument_Impl::GetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
100 if ( !pFormatetc )
101 return DV_E_FORMATETC;
103 if ( !pMedium )
104 return STG_E_MEDIUMFULL;
106 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
107 || pFormatetc->dwAspect == DVASPECT_ICON
108 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
109 return DV_E_DVASPECT;
111 if ( pFormatetc->cfFormat == CF_ENHMETAFILE )
113 if ( !( pFormatetc->tymed & TYMED_ENHMF ) )
114 return DV_E_TYMED;
116 HENHMETAFILE hMeta = reinterpret_cast<HENHMETAFILE>( getMetaFileHandle_Impl( sal_True ) );
118 if ( hMeta )
120 pMedium->tymed = TYMED_ENHMF;
121 pMedium->hEnhMetaFile = hMeta;
122 pMedium->pUnkForRelease = NULL;
124 return S_OK;
127 return STG_E_MEDIUMFULL;
129 else if ( pFormatetc->cfFormat == CF_METAFILEPICT )
131 if ( !( pFormatetc->tymed & TYMED_MFPICT ) )
132 return DV_E_TYMED;
134 HGLOBAL hMeta = reinterpret_cast<HGLOBAL>( getMetaFileHandle_Impl( sal_False ) );
136 if ( hMeta )
138 pMedium->tymed = TYMED_MFPICT;
139 pMedium->hMetaFilePict = hMeta;
140 pMedium->pUnkForRelease = NULL;
142 return S_OK;
145 return STG_E_MEDIUMFULL;
147 else
149 CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
150 CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
151 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
153 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
154 return DV_E_TYMED;
156 CComPtr< IStorage > pNewStg;
157 HRESULT hr = StgCreateDocfile( NULL, STGM_CREATE | STGM_READWRITE | STGM_DELETEONRELEASE, 0, &pNewStg );
158 if ( FAILED( hr ) || !pNewStg ) return STG_E_MEDIUMFULL;
160 hr = SaveTo_Impl( pNewStg );
161 if ( FAILED( hr ) ) return STG_E_MEDIUMFULL;
163 pMedium->tymed = TYMED_ISTORAGE;
164 pMedium->pstg = pNewStg;
165 pMedium->pstg->AddRef();
166 pMedium->pUnkForRelease = ( IUnknown* )pNewStg;
168 return S_OK;
172 return DV_E_FORMATETC;
175 STDMETHODIMP EmbedDocument_Impl::GetDataHere( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
177 if ( !pFormatetc )
178 return DV_E_FORMATETC;
180 if ( !pMedium )
181 return STG_E_MEDIUMFULL;
183 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
184 || pFormatetc->dwAspect == DVASPECT_ICON
185 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
186 return DV_E_DVASPECT;
188 CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
189 CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
191 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
193 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
194 return DV_E_TYMED;
196 if ( !pMedium->pstg ) return STG_E_MEDIUMFULL;
198 HRESULT hr = SaveTo_Impl( pMedium->pstg );
199 if ( FAILED( hr ) ) return STG_E_MEDIUMFULL;
201 pMedium->tymed = TYMED_ISTORAGE;
202 pMedium->pUnkForRelease = NULL;
204 return S_OK;
207 return DV_E_FORMATETC;
210 STDMETHODIMP EmbedDocument_Impl::QueryGetData( FORMATETC * pFormatetc )
212 if ( pFormatetc )
214 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
215 || pFormatetc->dwAspect == DVASPECT_ICON
216 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
217 return DV_E_DVASPECT;
219 if ( pFormatetc->cfFormat == CF_ENHMETAFILE )
221 if ( !( pFormatetc->tymed & TYMED_ENHMF ) )
222 return DV_E_TYMED;
224 return S_OK;
226 else if ( pFormatetc->cfFormat == CF_METAFILEPICT )
228 if ( !( pFormatetc->tymed & TYMED_MFPICT ) )
229 return DV_E_TYMED;
231 return S_OK;
233 else
235 CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
236 CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
237 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
239 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
240 return DV_E_TYMED;
242 return S_OK;
247 return DV_E_FORMATETC;
251 STDMETHODIMP EmbedDocument_Impl::GetCanonicalFormatEtc( FORMATETC * pFormatetcIn, FORMATETC * pFormatetcOut )
253 if ( !pFormatetcIn || !pFormatetcOut )
254 return DV_E_FORMATETC;
256 pFormatetcOut->ptd = NULL;
257 pFormatetcOut->cfFormat = pFormatetcIn->cfFormat;
258 pFormatetcOut->dwAspect = DVASPECT_CONTENT;
260 if ( pFormatetcIn->cfFormat == CF_ENHMETAFILE )
262 pFormatetcOut->tymed = TYMED_ENHMF;
263 return S_OK;
265 else if ( pFormatetcIn->cfFormat == CF_METAFILEPICT )
267 pFormatetcOut->tymed = TYMED_MFPICT;
268 return S_OK;
270 else
272 CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
273 CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
274 if ( pFormatetcIn->cfFormat == cf_embSource || pFormatetcIn->cfFormat == cf_embObj )
276 pFormatetcOut->tymed = TYMED_ISTORAGE;
277 return S_OK;
281 return DV_E_FORMATETC;
284 STDMETHODIMP EmbedDocument_Impl::SetData( FORMATETC * /*pFormatetc*/, STGMEDIUM * /*pMedium*/, BOOL /*fRelease*/ )
286 return E_NOTIMPL;
289 STDMETHODIMP EmbedDocument_Impl::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC ** /*ppFormatetc*/ )
291 if ( dwDirection == DATADIR_GET )
292 return OLE_S_USEREG;
294 return E_NOTIMPL;
297 STDMETHODIMP EmbedDocument_Impl::DAdvise( FORMATETC * pFormatetc, DWORD advf, IAdviseSink * pAdvSink, DWORD * pdwConnection )
299 if ( !m_pDAdviseHolder )
300 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
301 return E_OUTOFMEMORY;
303 return m_pDAdviseHolder->Advise( (IDataObject*)this, pFormatetc, advf, pAdvSink, pdwConnection );
306 STDMETHODIMP EmbedDocument_Impl::DUnadvise( DWORD dwConnection )
308 if ( !m_pDAdviseHolder )
309 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
310 return E_OUTOFMEMORY;
312 return m_pDAdviseHolder->Unadvise( dwConnection );
315 STDMETHODIMP EmbedDocument_Impl::EnumDAdvise( IEnumSTATDATA ** ppenumAdvise )
317 if ( !m_pDAdviseHolder )
318 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
319 return E_OUTOFMEMORY;
321 return m_pDAdviseHolder->EnumAdvise( ppenumAdvise );
324 // Fix strange warnings about some
325 // ATL::CAxHostWindow::QueryInterface|AddRef|Releae functions.
326 // warning C4505: 'xxx' : unreferenced local function has been removed
327 #if defined(_MSC_VER)
328 #pragma warning(disable: 4505)
329 #endif