Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / embeddedobj / source / msole / olevisual.cxx
blob7d8de650db3e521d0b77a36ddd195850ae5e6272
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 .
20 #include <com/sun/star/lang/DisposedException.hpp>
21 #include <com/sun/star/embed/EmbedStates.hpp>
22 #include <com/sun/star/embed/EmbedMapUnits.hpp>
23 #include <com/sun/star/embed/EmbedMisc.hpp>
24 #include <com/sun/star/embed/Aspects.hpp>
25 #include <com/sun/star/embed/WrongStateException.hpp>
26 #include <com/sun/star/io/XSeekable.hpp>
27 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
29 #include <oleembobj.hxx>
30 #if defined (_WIN32)
31 #include <comphelper/mimeconfighelper.hxx>
32 #endif
33 #include <comphelper/seqstream.hxx>
34 #include <filter/msfilter/classids.hxx>
35 #include <sal/log.hxx>
37 #if defined(_WIN32)
38 #include "olecomponent.hxx"
39 #include <comphelper/diagnose_ex.hxx>
40 #endif
42 using namespace ::com::sun::star;
43 using namespace ::comphelper;
45 embed::VisualRepresentation OleEmbeddedObject::GetVisualRepresentationInNativeFormat_Impl(
46 const uno::Reference< io::XStream >& xCachedVisRepr )
48 embed::VisualRepresentation aVisualRepr;
50 // TODO: detect the format in the future for now use workaround
51 uno::Reference< io::XInputStream > xInStream = xCachedVisRepr->getInputStream();
52 if ( !xInStream.is() )
53 throw uno::RuntimeException();
54 uno::Reference< io::XSeekable > xSeekable( xCachedVisRepr, uno::UNO_QUERY_THROW );
56 uno::Sequence< sal_Int8 > aSeq( 2 );
57 xInStream->readBytes( aSeq, 2 );
58 xSeekable->seek( 0 );
59 if ( aSeq.getLength() == 2 && aSeq[0] == 'B' && aSeq[1] == 'M' )
61 // it's a bitmap
62 aVisualRepr.Flavor = datatransfer::DataFlavor(
63 "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"",
64 "Bitmap",
65 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
67 else
69 // it's a metafile
70 aVisualRepr.Flavor = datatransfer::DataFlavor(
71 "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"",
72 "Windows Metafile",
73 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
76 sal_Int32 nStreamLength = static_cast<sal_Int32>(xSeekable->getLength());
77 uno::Sequence< sal_Int8 > aRepresent( nStreamLength );
78 xInStream->readBytes( aRepresent, nStreamLength );
79 aVisualRepr.Data <<= aRepresent;
81 return aVisualRepr;
84 void SAL_CALL OleEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect, const awt::Size& aSize )
86 // begin wrapping related part ====================
87 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
88 if ( xWrappedObject.is() )
90 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
91 xWrappedObject->setVisualAreaSize( nAspect, aSize );
92 return;
94 // end wrapping related part ====================
96 ::osl::ResettableMutexGuard aGuard( m_aMutex );
97 if ( m_bDisposed )
98 throw lang::DisposedException(); // TODO
100 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
101 if ( nAspect == embed::Aspects::MSOLE_ICON )
102 // no representation can be retrieved
103 throw embed::WrongStateException( "Illegal call!",
104 static_cast< ::cppu::OWeakObject* >(this) );
106 if ( m_nObjectState == -1 )
107 throw embed::WrongStateException( "The object is not loaded!",
108 static_cast< ::cppu::OWeakObject* >(this) );
110 #ifdef _WIN32
111 // RECOMPOSE_ON_RESIZE misc flag means that the object has to be switched to running state on resize.
112 // SetExtent() is called only for objects that require it,
113 // it should not be called for MSWord documents to workaround problem i49369
114 // If cached size is not set, that means that this is the size initialization, so there is no need to set the real size
115 bool bAllowToSetExtent =
116 ( ( getStatus( nAspect ) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE )
117 && !MimeConfigurationHelper::ClassIDsEqual(m_aClassID, MimeConfigurationHelper::GetSequenceClassID(MSO_WW8_CLASSID))
118 && m_bHasCachedSize );
120 if ( m_nObjectState == embed::EmbedStates::LOADED && bAllowToSetExtent )
122 aGuard.clear();
123 try {
124 changeState( embed::EmbedStates::RUNNING );
126 catch( const uno::Exception& )
128 SAL_WARN( "embeddedobj.ole", "The object should not be resized without activation!" );
130 aGuard.reset();
133 if ( m_pOleComponent && m_nObjectState != embed::EmbedStates::LOADED && bAllowToSetExtent )
135 awt::Size aSizeToSet = aSize;
136 aGuard.clear();
137 try {
138 m_pOleComponent->SetExtent( aSizeToSet, nAspect ); // will throw an exception in case of failure
139 m_bHasSizeToSet = false;
141 catch( const uno::Exception& )
143 // some objects do not allow to set the size even in running state
144 m_bHasSizeToSet = true;
145 m_aSizeToSet = aSizeToSet;
146 m_nAspectToSet = nAspect;
148 aGuard.reset();
150 #endif
152 // cache the values
153 m_bHasCachedSize = true;
154 m_aCachedSize = aSize;
155 m_nCachedAspect = nAspect;
158 awt::Size SAL_CALL OleEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect )
160 // begin wrapping related part ====================
161 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
162 if ( xWrappedObject.is() )
164 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
165 return xWrappedObject->getVisualAreaSize( nAspect );
167 // end wrapping related part ====================
169 ::osl::ResettableMutexGuard aGuard( m_aMutex );
170 if ( m_bDisposed )
171 throw lang::DisposedException(); // TODO
173 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
174 if ( nAspect == embed::Aspects::MSOLE_ICON )
175 // no representation can be retrieved
176 throw embed::WrongStateException( "Illegal call!",
177 static_cast< ::cppu::OWeakObject* >(this) );
179 if ( m_nObjectState == -1 )
180 throw embed::WrongStateException( "The object is not loaded!",
181 static_cast< ::cppu::OWeakObject* >(this) );
183 awt::Size aResult;
185 #ifdef _WIN32
186 // TODO/LATER: Support different aspects
187 if ( m_pOleComponent && !m_bHasSizeToSet && nAspect == embed::Aspects::MSOLE_CONTENT )
191 // the cached size updated every time the object is stored
192 if ( m_bHasCachedSize )
194 aResult = m_aCachedSize;
196 else
198 // there is no internal cache
199 awt::Size aSize;
200 aGuard.clear();
202 bool bBackToLoaded = false;
204 bool bSuccess = false;
205 if ( getCurrentState() == embed::EmbedStates::LOADED )
207 SAL_WARN( "embeddedobj.ole", "Loaded object has no cached size!" );
209 // try to switch the object to RUNNING state and request the value again
210 try {
211 changeState( embed::EmbedStates::RUNNING );
212 // the links should be switched back to loaded state to avoid too
213 // many open MathType instances
214 bBackToLoaded = true;
216 catch( const uno::Exception& )
218 throw embed::NoVisualAreaSizeException(
219 "No size available!",
220 static_cast< ::cppu::OWeakObject* >(this) );
226 // first try to get size using replacement image
227 aSize = m_pOleComponent->GetExtent( nAspect ); // will throw an exception in case of failure
228 bSuccess = true;
230 catch( const uno::Exception& )
232 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "OleEmbeddedObject::getVisualAreaSize: GetExtent() failed:");
235 if (bBackToLoaded)
239 changeState(embed::EmbedStates::LOADED);
241 catch( const uno::Exception& )
243 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "ignoring ");
247 if ( !bSuccess )
251 // second try the cached replacement image
252 aSize = m_pOleComponent->GetCachedExtent( nAspect ); // will throw an exception in case of failure
253 bSuccess = true;
255 catch( const uno::Exception& )
257 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "OleEmbeddedObject::getVisualAreaSize: GetCachedExtent() failed:");
261 if ( !bSuccess )
265 // third try the size reported by the object
266 aSize = m_pOleComponent->GetRecommendedExtent( nAspect ); // will throw an exception in case of failure
267 bSuccess = true;
269 catch( const uno::Exception& )
271 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "OleEmbeddedObject::getVisualAreaSize: GetRecommendedExtent() failed:");
275 if ( !bSuccess )
276 throw embed::NoVisualAreaSizeException(
277 "No size available!",
278 static_cast< ::cppu::OWeakObject* >(this) );
280 aGuard.reset();
282 m_aCachedSize = aSize;
283 m_nCachedAspect = nAspect;
284 m_bHasCachedSize = true;
286 aResult = m_aCachedSize;
289 catch ( const embed::NoVisualAreaSizeException& )
291 throw;
293 catch ( const uno::Exception& )
295 throw embed::NoVisualAreaSizeException(
296 "No size available!",
297 static_cast< ::cppu::OWeakObject* >(this) );
300 else
301 #endif
303 // return cached value
304 if ( !m_bHasCachedSize )
306 throw embed::NoVisualAreaSizeException(
307 "No size available!",
308 static_cast< ::cppu::OWeakObject* >(this) );
310 SAL_WARN_IF( nAspect != m_nCachedAspect, "embeddedobj.ole", "Unexpected aspect is requested!" );
311 aResult = m_aCachedSize;
314 return aResult;
317 embed::VisualRepresentation SAL_CALL OleEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect )
319 // begin wrapping related part ====================
320 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
321 if ( xWrappedObject.is() )
323 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
324 return xWrappedObject->getPreferredVisualRepresentation( nAspect );
326 // end wrapping related part ====================
328 ::osl::MutexGuard aGuard( m_aMutex );
329 if ( m_bDisposed )
330 throw lang::DisposedException(); // TODO
332 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
333 if ( nAspect == embed::Aspects::MSOLE_ICON )
334 // no representation can be retrieved
335 throw embed::WrongStateException( "Illegal call!",
336 static_cast< ::cppu::OWeakObject* >(this) );
338 // TODO: if the object has cached representation then it should be returned
339 // TODO: if the object has no cached representation and is in loaded state it should switch itself to the running state
340 if ( m_nObjectState == -1 )
341 throw embed::WrongStateException( "The object is not loaded!",
342 static_cast< ::cppu::OWeakObject* >(this) );
344 // TODO: in case of different aspects they must be applied to the mediatype and XTransferable must be used
345 // the cache is used only as a fallback if object is not in loaded state
346 if ( !m_xCachedVisualRepresentation.is() && ( !m_bVisReplInitialized || m_bVisReplInStream )
347 && m_nObjectState == embed::EmbedStates::LOADED )
349 m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream, true );
350 SetVisReplInStream( m_xCachedVisualRepresentation.is() );
353 #ifdef _WIN32
354 if ( !m_xCachedVisualRepresentation.is() && m_pOleComponent )
358 if ( m_nObjectState == embed::EmbedStates::LOADED )
359 changeState( embed::EmbedStates::RUNNING );
361 datatransfer::DataFlavor aDataFlavor(
362 "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"",
363 "Windows Metafile",
364 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
366 embed::VisualRepresentation aVisualRepr;
367 aVisualRepr.Data = m_pOleComponent->getTransferData( aDataFlavor );
368 aVisualRepr.Flavor = aDataFlavor;
370 uno::Sequence< sal_Int8 > aVisReplSeq;
371 aVisualRepr.Data >>= aVisReplSeq;
372 if ( aVisReplSeq.getLength() )
374 m_xCachedVisualRepresentation = GetNewFilledTempStream_Impl(
375 uno::Reference< io::XInputStream >(
376 new ::comphelper::SequenceInputStream(aVisReplSeq)));
379 return aVisualRepr;
381 catch( const uno::Exception& )
384 #endif
386 // the cache is used only as a fallback if object is not in loaded state
387 if ( !m_xCachedVisualRepresentation.is() && ( !m_bVisReplInitialized || m_bVisReplInStream ) )
389 m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream );
390 SetVisReplInStream( m_xCachedVisualRepresentation.is() );
393 if ( !m_xCachedVisualRepresentation.is() )
395 // no representation can be retrieved
396 throw embed::WrongStateException( "Illegal call!",
397 static_cast< ::cppu::OWeakObject* >(this) );
400 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation );
403 sal_Int32 SAL_CALL OleEmbeddedObject::getMapUnit( sal_Int64 nAspect )
405 // begin wrapping related part ====================
406 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
407 if ( xWrappedObject.is() )
409 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
410 return xWrappedObject->getMapUnit( nAspect );
412 // end wrapping related part ====================
414 ::osl::MutexGuard aGuard( m_aMutex );
415 if ( m_bDisposed )
416 throw lang::DisposedException(); // TODO
418 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
419 if ( nAspect == embed::Aspects::MSOLE_ICON )
420 // no representation can be retrieved
421 throw embed::WrongStateException( "Illegal call!",
422 static_cast< ::cppu::OWeakObject* >(this) );
424 if ( m_nObjectState == -1 )
425 throw embed::WrongStateException( "The object is not loaded!",
426 static_cast< ::cppu::OWeakObject* >(this) );
428 return embed::EmbedMapUnits::ONE_100TH_MM;
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */