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 #pragma warning(disable : 4917 4555)
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
;
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
;
49 aFlavor
.MimeType
= "application/x-openoffice-emf;windows_formatname=\"Image EMF\"";
50 aFlavor
.HumanPresentableName
= "Enhanced Windows MetaFile";
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
);
70 STDMETHODIMP
EmbedDocument_Impl::GetData( FORMATETC
* pFormatetc
, STGMEDIUM
* pMedium
)
73 return DV_E_FORMATETC
;
76 return STG_E_MEDIUMFULL
;
78 if ( pFormatetc
->dwAspect
== DVASPECT_THUMBNAIL
79 || pFormatetc
->dwAspect
== DVASPECT_ICON
80 || pFormatetc
->dwAspect
== DVASPECT_DOCPRINT
)
83 if ( pFormatetc
->cfFormat
== CF_ENHMETAFILE
)
85 if ( !( pFormatetc
->tymed
& TYMED_ENHMF
) )
88 HENHMETAFILE hMeta
= reinterpret_cast<HENHMETAFILE
>( getMetaFileHandle_Impl( sal_True
) );
92 pMedium
->tymed
= TYMED_ENHMF
;
93 pMedium
->hEnhMetaFile
= hMeta
;
94 pMedium
->pUnkForRelease
= NULL
;
99 return STG_E_MEDIUMFULL
;
101 else if ( pFormatetc
->cfFormat
== CF_METAFILEPICT
)
103 if ( !( pFormatetc
->tymed
& TYMED_MFPICT
) )
106 HGLOBAL hMeta
= reinterpret_cast<HGLOBAL
>( getMetaFileHandle_Impl( sal_False
) );
110 pMedium
->tymed
= TYMED_MFPICT
;
111 pMedium
->hMetaFilePict
= hMeta
;
112 pMedium
->pUnkForRelease
= NULL
;
117 return STG_E_MEDIUMFULL
;
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
) )
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
;
144 return DV_E_FORMATETC
;
147 STDMETHODIMP
EmbedDocument_Impl::GetDataHere( FORMATETC
* pFormatetc
, STGMEDIUM
* pMedium
)
150 return DV_E_FORMATETC
;
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
) )
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
;
179 return DV_E_FORMATETC
;
182 STDMETHODIMP
EmbedDocument_Impl::QueryGetData( FORMATETC
* 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
) )
198 else if ( pFormatetc
->cfFormat
== CF_METAFILEPICT
)
200 if ( !( pFormatetc
->tymed
& TYMED_MFPICT
) )
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
) )
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
;
237 else if ( pFormatetcIn
->cfFormat
== CF_METAFILEPICT
)
239 pFormatetcOut
->tymed
= TYMED_MFPICT
;
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
;
253 return DV_E_FORMATETC
;
256 STDMETHODIMP
EmbedDocument_Impl::SetData( FORMATETC
* /*pFormatetc*/, STGMEDIUM
* /*pMedium*/, BOOL
/*fRelease*/ )
261 STDMETHODIMP
EmbedDocument_Impl::EnumFormatEtc( DWORD dwDirection
, IEnumFORMATETC
** /*ppFormatetc*/ )
263 if ( dwDirection
== DATADIR_GET
)
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: */