1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <embeddoc.hxx>
22 #include <com/sun/star/uno/Any.h>
23 #include <com/sun/star/uno/Exception.hpp>
24 #include <com/sun/star/datatransfer/XTransferable.hpp>
27 #include <osl/thread.h>
29 using namespace ::com::sun::star
;
35 sal_uInt64
EmbedDocument_Impl::getMetaFileHandle_Impl( bool isEnhMeta
)
37 sal_uInt64 pResult
= 0;
39 uno::Reference
< datatransfer::XTransferable
> xTransferable( m_pDocHolder
->GetDocument(), uno::UNO_QUERY
);
40 if ( xTransferable
.is() )
42 datatransfer::DataFlavor aFlavor
;
46 aFlavor
.MimeType
= "application/x-openoffice-emf;windows_formatname=\"Image EMF\"";
47 aFlavor
.HumanPresentableName
= "Enhanced Windows MetaFile";
51 aFlavor
.MimeType
= "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"";
52 aFlavor
.HumanPresentableName
= "Windows GDIMetaFile";
55 aFlavor
.DataType
= cppu::UnoType
<sal_uInt64
>::get();
57 uno::Any aAny
= xTransferable
->getTransferData( aFlavor
);
67 COM_DECLSPEC_NOTHROW STDMETHODIMP
EmbedDocument_Impl::GetData( FORMATETC
* pFormatetc
, STGMEDIUM
* pMedium
)
70 return DV_E_FORMATETC
;
73 return STG_E_MEDIUMFULL
;
75 if ( pFormatetc
->dwAspect
== DVASPECT_THUMBNAIL
76 || pFormatetc
->dwAspect
== DVASPECT_ICON
77 || pFormatetc
->dwAspect
== DVASPECT_DOCPRINT
)
80 if ( pFormatetc
->cfFormat
== CF_ENHMETAFILE
)
82 if ( !( pFormatetc
->tymed
& TYMED_ENHMF
) )
85 HENHMETAFILE hMeta
= reinterpret_cast<HENHMETAFILE
>( getMetaFileHandle_Impl( true ) );
89 pMedium
->tymed
= TYMED_ENHMF
;
90 pMedium
->hEnhMetaFile
= hMeta
;
91 pMedium
->pUnkForRelease
= nullptr;
96 return STG_E_MEDIUMFULL
;
98 else if ( pFormatetc
->cfFormat
== CF_METAFILEPICT
)
100 if ( !( pFormatetc
->tymed
& TYMED_MFPICT
) )
103 HGLOBAL hMeta
= reinterpret_cast<HGLOBAL
>( getMetaFileHandle_Impl( false ) );
107 pMedium
->tymed
= TYMED_MFPICT
;
108 pMedium
->hMetaFilePict
= hMeta
;
109 pMedium
->pUnkForRelease
= nullptr;
114 return STG_E_MEDIUMFULL
;
118 CLIPFORMAT cf_embSource
= static_cast<CLIPFORMAT
>(RegisterClipboardFormatW( L
"Embed Source" ));
119 CLIPFORMAT cf_embObj
= static_cast<CLIPFORMAT
>(RegisterClipboardFormatW( L
"Embedded Object" ));
120 if ( pFormatetc
->cfFormat
== cf_embSource
|| pFormatetc
->cfFormat
== cf_embObj
)
122 if ( !( pFormatetc
->tymed
& TYMED_ISTORAGE
) )
125 CComPtr
< IStorage
> pNewStg
;
126 HRESULT hr
= StgCreateDocfile( nullptr, STGM_CREATE
| STGM_READWRITE
| STGM_DELETEONRELEASE
, 0, &pNewStg
);
127 if ( FAILED( hr
) || !pNewStg
) return STG_E_MEDIUMFULL
;
129 hr
= SaveTo_Impl( pNewStg
);
130 if ( FAILED( hr
) ) return STG_E_MEDIUMFULL
;
132 pMedium
->tymed
= TYMED_ISTORAGE
;
133 pMedium
->pstg
= pNewStg
;
134 pMedium
->pstg
->AddRef();
135 pMedium
->pUnkForRelease
= pNewStg
;
141 return DV_E_FORMATETC
;
144 COM_DECLSPEC_NOTHROW STDMETHODIMP
EmbedDocument_Impl::GetDataHere( FORMATETC
* pFormatetc
, STGMEDIUM
* pMedium
)
147 return DV_E_FORMATETC
;
150 return STG_E_MEDIUMFULL
;
152 if ( pFormatetc
->dwAspect
== DVASPECT_THUMBNAIL
153 || pFormatetc
->dwAspect
== DVASPECT_ICON
154 || pFormatetc
->dwAspect
== DVASPECT_DOCPRINT
)
155 return DV_E_DVASPECT
;
157 CLIPFORMAT cf_embSource
= static_cast<CLIPFORMAT
>(RegisterClipboardFormatW( L
"Embed Source" ));
158 CLIPFORMAT cf_embObj
= static_cast<CLIPFORMAT
>(RegisterClipboardFormatW( L
"Embedded Object" ));
160 if ( pFormatetc
->cfFormat
== cf_embSource
|| pFormatetc
->cfFormat
== cf_embObj
)
162 if ( !( pFormatetc
->tymed
& TYMED_ISTORAGE
) )
165 if ( !pMedium
->pstg
) return STG_E_MEDIUMFULL
;
167 HRESULT hr
= SaveTo_Impl( pMedium
->pstg
);
168 if ( FAILED( hr
) ) return STG_E_MEDIUMFULL
;
170 pMedium
->tymed
= TYMED_ISTORAGE
;
171 pMedium
->pUnkForRelease
= nullptr;
176 return DV_E_FORMATETC
;
179 COM_DECLSPEC_NOTHROW STDMETHODIMP
EmbedDocument_Impl::QueryGetData( FORMATETC
* pFormatetc
)
183 if ( pFormatetc
->dwAspect
== DVASPECT_THUMBNAIL
184 || pFormatetc
->dwAspect
== DVASPECT_ICON
185 || pFormatetc
->dwAspect
== DVASPECT_DOCPRINT
)
186 return DV_E_DVASPECT
;
188 if ( pFormatetc
->cfFormat
== CF_ENHMETAFILE
)
190 if ( !( pFormatetc
->tymed
& TYMED_ENHMF
) )
195 else if ( pFormatetc
->cfFormat
== CF_METAFILEPICT
)
197 if ( !( pFormatetc
->tymed
& TYMED_MFPICT
) )
204 CLIPFORMAT cf_embSource
= static_cast<CLIPFORMAT
>(RegisterClipboardFormatW( L
"Embed Source" ));
205 CLIPFORMAT cf_embObj
= static_cast<CLIPFORMAT
>(RegisterClipboardFormatW( L
"Embedded Object" ));
206 if ( pFormatetc
->cfFormat
== cf_embSource
|| pFormatetc
->cfFormat
== cf_embObj
)
208 if ( !( pFormatetc
->tymed
& TYMED_ISTORAGE
) )
216 return DV_E_FORMATETC
;
220 COM_DECLSPEC_NOTHROW STDMETHODIMP
EmbedDocument_Impl::GetCanonicalFormatEtc( FORMATETC
* pFormatetcIn
, FORMATETC
* pFormatetcOut
)
222 if ( !pFormatetcIn
|| !pFormatetcOut
)
223 return DV_E_FORMATETC
;
225 pFormatetcOut
->ptd
= nullptr;
226 pFormatetcOut
->cfFormat
= pFormatetcIn
->cfFormat
;
227 pFormatetcOut
->dwAspect
= DVASPECT_CONTENT
;
229 if ( pFormatetcIn
->cfFormat
== CF_ENHMETAFILE
)
231 pFormatetcOut
->tymed
= TYMED_ENHMF
;
234 else if ( pFormatetcIn
->cfFormat
== CF_METAFILEPICT
)
236 pFormatetcOut
->tymed
= TYMED_MFPICT
;
241 CLIPFORMAT cf_embSource
= static_cast<CLIPFORMAT
>(RegisterClipboardFormatW( L
"Embed Source" ));
242 CLIPFORMAT cf_embObj
= static_cast<CLIPFORMAT
>(RegisterClipboardFormatW( L
"Embedded Object" ));
243 if ( pFormatetcIn
->cfFormat
== cf_embSource
|| pFormatetcIn
->cfFormat
== cf_embObj
)
245 pFormatetcOut
->tymed
= TYMED_ISTORAGE
;
250 return DV_E_FORMATETC
;
253 COM_DECLSPEC_NOTHROW STDMETHODIMP
EmbedDocument_Impl::SetData( FORMATETC
* /*pFormatetc*/, STGMEDIUM
* /*pMedium*/, BOOL
/*fRelease*/ )
258 COM_DECLSPEC_NOTHROW STDMETHODIMP
EmbedDocument_Impl::EnumFormatEtc( DWORD dwDirection
, IEnumFORMATETC
** /*ppFormatetc*/ )
260 if ( dwDirection
== DATADIR_GET
)
266 COM_DECLSPEC_NOTHROW STDMETHODIMP
EmbedDocument_Impl::DAdvise( FORMATETC
* pFormatetc
, DWORD advf
, IAdviseSink
* pAdvSink
, DWORD
* pdwConnection
)
268 if ( !m_pDAdviseHolder
)
269 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder
) ) || !m_pDAdviseHolder
)
270 return E_OUTOFMEMORY
;
272 return m_pDAdviseHolder
->Advise( static_cast<IDataObject
*>(this), pFormatetc
, advf
, pAdvSink
, pdwConnection
);
275 COM_DECLSPEC_NOTHROW STDMETHODIMP
EmbedDocument_Impl::DUnadvise( DWORD dwConnection
)
277 if ( !m_pDAdviseHolder
)
278 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder
) ) || !m_pDAdviseHolder
)
279 return E_OUTOFMEMORY
;
281 return m_pDAdviseHolder
->Unadvise( dwConnection
);
284 COM_DECLSPEC_NOTHROW STDMETHODIMP
EmbedDocument_Impl::EnumDAdvise( IEnumSTATDATA
** ppenumAdvise
)
286 if ( !m_pDAdviseHolder
)
287 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder
) ) || !m_pDAdviseHolder
)
288 return E_OUTOFMEMORY
;
290 return m_pDAdviseHolder
->EnumAdvise( ppenumAdvise
);
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */