Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / embedserv / source / embed / ed_idataobj.cxx
blobc51327c35c7d14e31a0f05b80e914a68b8af150b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifdef _MSC_VER
20 #pragma warning(disable : 4917 4555)
21 #endif
23 #include "embeddoc.hxx"
25 #include <com/sun/star/uno/Any.h>
26 #include <com/sun/star/uno/Exception.hpp>
27 #include <com/sun/star/datatransfer/XTransferable.hpp>
30 #include <osl/thread.h>
32 using namespace ::com::sun::star;
35 // EmbedDocument_Impl
38 sal_uInt64 EmbedDocument_Impl::getMetaFileHandle_Impl( sal_Bool isEnhMeta )
40 sal_uInt64 pResult = NULL;
42 uno::Reference< datatransfer::XTransferable > xTransferable( m_pDocHolder->GetDocument(), uno::UNO_QUERY );
43 if ( xTransferable.is() )
45 datatransfer::DataFlavor aFlavor;
47 if ( isEnhMeta )
49 aFlavor.MimeType = "application/x-openoffice-emf;windows_formatname=\"Image EMF\"";
50 aFlavor.HumanPresentableName = "Enhanced Windows MetaFile";
52 else
54 aFlavor.MimeType = "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"";
55 aFlavor.HumanPresentableName = "Windows GDIMetaFile";
58 aFlavor.DataType = cppu::UnoType<sal_uInt64>::get();
60 uno::Any aAny = xTransferable->getTransferData( aFlavor );
61 aAny >>= pResult;
64 return pResult;
68 // IDataObject
70 STDMETHODIMP EmbedDocument_Impl::GetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
72 if ( !pFormatetc )
73 return DV_E_FORMATETC;
75 if ( !pMedium )
76 return STG_E_MEDIUMFULL;
78 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
79 || pFormatetc->dwAspect == DVASPECT_ICON
80 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
81 return DV_E_DVASPECT;
83 if ( pFormatetc->cfFormat == CF_ENHMETAFILE )
85 if ( !( pFormatetc->tymed & TYMED_ENHMF ) )
86 return DV_E_TYMED;
88 HENHMETAFILE hMeta = reinterpret_cast<HENHMETAFILE>( getMetaFileHandle_Impl( sal_True ) );
90 if ( hMeta )
92 pMedium->tymed = TYMED_ENHMF;
93 pMedium->hEnhMetaFile = hMeta;
94 pMedium->pUnkForRelease = NULL;
96 return S_OK;
99 return STG_E_MEDIUMFULL;
101 else if ( pFormatetc->cfFormat == CF_METAFILEPICT )
103 if ( !( pFormatetc->tymed & TYMED_MFPICT ) )
104 return DV_E_TYMED;
106 HGLOBAL hMeta = reinterpret_cast<HGLOBAL>( getMetaFileHandle_Impl( sal_False ) );
108 if ( hMeta )
110 pMedium->tymed = TYMED_MFPICT;
111 pMedium->hMetaFilePict = hMeta;
112 pMedium->pUnkForRelease = NULL;
114 return S_OK;
117 return STG_E_MEDIUMFULL;
119 else
121 CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
122 CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
123 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
125 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
126 return DV_E_TYMED;
128 CComPtr< IStorage > pNewStg;
129 HRESULT hr = StgCreateDocfile( NULL, STGM_CREATE | STGM_READWRITE | STGM_DELETEONRELEASE, 0, &pNewStg );
130 if ( FAILED( hr ) || !pNewStg ) return STG_E_MEDIUMFULL;
132 hr = SaveTo_Impl( pNewStg );
133 if ( FAILED( hr ) ) return STG_E_MEDIUMFULL;
135 pMedium->tymed = TYMED_ISTORAGE;
136 pMedium->pstg = pNewStg;
137 pMedium->pstg->AddRef();
138 pMedium->pUnkForRelease = ( IUnknown* )pNewStg;
140 return S_OK;
144 return DV_E_FORMATETC;
147 STDMETHODIMP EmbedDocument_Impl::GetDataHere( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
149 if ( !pFormatetc )
150 return DV_E_FORMATETC;
152 if ( !pMedium )
153 return STG_E_MEDIUMFULL;
155 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
156 || pFormatetc->dwAspect == DVASPECT_ICON
157 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
158 return DV_E_DVASPECT;
160 CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
161 CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
163 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
165 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
166 return DV_E_TYMED;
168 if ( !pMedium->pstg ) return STG_E_MEDIUMFULL;
170 HRESULT hr = SaveTo_Impl( pMedium->pstg );
171 if ( FAILED( hr ) ) return STG_E_MEDIUMFULL;
173 pMedium->tymed = TYMED_ISTORAGE;
174 pMedium->pUnkForRelease = NULL;
176 return S_OK;
179 return DV_E_FORMATETC;
182 STDMETHODIMP EmbedDocument_Impl::QueryGetData( FORMATETC * pFormatetc )
184 if ( pFormatetc )
186 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
187 || pFormatetc->dwAspect == DVASPECT_ICON
188 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
189 return DV_E_DVASPECT;
191 if ( pFormatetc->cfFormat == CF_ENHMETAFILE )
193 if ( !( pFormatetc->tymed & TYMED_ENHMF ) )
194 return DV_E_TYMED;
196 return S_OK;
198 else if ( pFormatetc->cfFormat == CF_METAFILEPICT )
200 if ( !( pFormatetc->tymed & TYMED_MFPICT ) )
201 return DV_E_TYMED;
203 return S_OK;
205 else
207 CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
208 CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
209 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
211 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
212 return DV_E_TYMED;
214 return S_OK;
219 return DV_E_FORMATETC;
223 STDMETHODIMP EmbedDocument_Impl::GetCanonicalFormatEtc( FORMATETC * pFormatetcIn, FORMATETC * pFormatetcOut )
225 if ( !pFormatetcIn || !pFormatetcOut )
226 return DV_E_FORMATETC;
228 pFormatetcOut->ptd = NULL;
229 pFormatetcOut->cfFormat = pFormatetcIn->cfFormat;
230 pFormatetcOut->dwAspect = DVASPECT_CONTENT;
232 if ( pFormatetcIn->cfFormat == CF_ENHMETAFILE )
234 pFormatetcOut->tymed = TYMED_ENHMF;
235 return S_OK;
237 else if ( pFormatetcIn->cfFormat == CF_METAFILEPICT )
239 pFormatetcOut->tymed = TYMED_MFPICT;
240 return S_OK;
242 else
244 CLIPFORMAT cf_embSource = (CLIPFORMAT)RegisterClipboardFormatA( "Embed Source" );
245 CLIPFORMAT cf_embObj = (CLIPFORMAT)RegisterClipboardFormatA( "Embedded Object" );
246 if ( pFormatetcIn->cfFormat == cf_embSource || pFormatetcIn->cfFormat == cf_embObj )
248 pFormatetcOut->tymed = TYMED_ISTORAGE;
249 return S_OK;
253 return DV_E_FORMATETC;
256 STDMETHODIMP EmbedDocument_Impl::SetData( FORMATETC * /*pFormatetc*/, STGMEDIUM * /*pMedium*/, BOOL /*fRelease*/ )
258 return E_NOTIMPL;
261 STDMETHODIMP EmbedDocument_Impl::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC ** /*ppFormatetc*/ )
263 if ( dwDirection == DATADIR_GET )
264 return OLE_S_USEREG;
266 return E_NOTIMPL;
269 STDMETHODIMP EmbedDocument_Impl::DAdvise( FORMATETC * pFormatetc, DWORD advf, IAdviseSink * pAdvSink, DWORD * pdwConnection )
271 if ( !m_pDAdviseHolder )
272 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
273 return E_OUTOFMEMORY;
275 return m_pDAdviseHolder->Advise( (IDataObject*)this, pFormatetc, advf, pAdvSink, pdwConnection );
278 STDMETHODIMP EmbedDocument_Impl::DUnadvise( DWORD dwConnection )
280 if ( !m_pDAdviseHolder )
281 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
282 return E_OUTOFMEMORY;
284 return m_pDAdviseHolder->Unadvise( dwConnection );
287 STDMETHODIMP EmbedDocument_Impl::EnumDAdvise( IEnumSTATDATA ** ppenumAdvise )
289 if ( !m_pDAdviseHolder )
290 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
291 return E_OUTOFMEMORY;
293 return m_pDAdviseHolder->EnumAdvise( ppenumAdvise );
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */