build fix
[LibreOffice.git] / embeddedobj / source / msole / olevisual.cxx
blob1acb87706386aea6f3e97a071c0e1273d564e163
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/io/XSeekable.hpp>
26 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
28 #include <oleembobj.hxx>
29 #include <comphelper/mimeconfighelper.hxx>
30 #include <comphelper/seqstream.hxx>
31 #include <filter/msfilter/classids.hxx>
33 #if defined(_WIN32)
34 #include <olecomponent.hxx>
35 #endif
37 using namespace ::com::sun::star;
38 using namespace ::comphelper;
40 embed::VisualRepresentation OleEmbeddedObject::GetVisualRepresentationInNativeFormat_Impl(
41 const uno::Reference< io::XStream >& xCachedVisRepr )
42 throw ( uno::Exception )
44 embed::VisualRepresentation aVisualRepr;
46 // TODO: detect the format in the future for now use workaround
47 uno::Reference< io::XInputStream > xInStream = xCachedVisRepr->getInputStream();
48 uno::Reference< io::XSeekable > xSeekable( xCachedVisRepr, uno::UNO_QUERY );
49 if ( !xInStream.is() || !xSeekable.is() )
50 throw uno::RuntimeException();
52 uno::Sequence< sal_Int8 > aSeq( 2 );
53 xInStream->readBytes( aSeq, 2 );
54 xSeekable->seek( 0 );
55 if ( aSeq.getLength() == 2 && aSeq[0] == 'B' && aSeq[1] == 'M' )
57 // it's a bitmap
58 aVisualRepr.Flavor = datatransfer::DataFlavor(
59 OUString( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ),
60 OUString( "Bitmap" ),
61 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
63 else
65 // it's a metafile
66 aVisualRepr.Flavor = datatransfer::DataFlavor(
67 OUString( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
68 OUString( "Windows Metafile" ),
69 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
72 sal_Int32 nStreamLength = (sal_Int32)xSeekable->getLength();
73 uno::Sequence< sal_Int8 > aRepresent( nStreamLength );
74 xInStream->readBytes( aRepresent, nStreamLength );
75 aVisualRepr.Data <<= aRepresent;
77 return aVisualRepr;
80 void SAL_CALL OleEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect, const awt::Size& aSize )
81 throw ( lang::IllegalArgumentException,
82 embed::WrongStateException,
83 uno::Exception,
84 uno::RuntimeException, std::exception )
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!\n" );
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!\n" );
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 )
159 throw ( lang::IllegalArgumentException,
160 embed::WrongStateException,
161 uno::Exception,
162 uno::RuntimeException, std::exception )
164 // begin wrapping related part ====================
165 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
166 if ( xWrappedObject.is() )
168 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
169 return xWrappedObject->getVisualAreaSize( nAspect );
171 // end wrapping related part ====================
173 ::osl::ResettableMutexGuard aGuard( m_aMutex );
174 if ( m_bDisposed )
175 throw lang::DisposedException(); // TODO
177 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
178 if ( nAspect == embed::Aspects::MSOLE_ICON )
179 // no representation can be retrieved
180 throw embed::WrongStateException( "Illegal call!",
181 static_cast< ::cppu::OWeakObject* >(this) );
183 if ( m_nObjectState == -1 )
184 throw embed::WrongStateException( "The object is not loaded!",
185 static_cast< ::cppu::OWeakObject* >(this) );
187 awt::Size aResult;
189 #ifdef _WIN32
190 // TODO/LATER: Support different aspects
191 if ( m_pOleComponent && !m_bHasSizeToSet && nAspect == embed::Aspects::MSOLE_CONTENT )
195 // the cached size updated every time the object is stored
196 if ( m_bHasCachedSize )
198 aResult = m_aCachedSize;
200 else
202 // there is no internal cache
203 awt::Size aSize;
204 aGuard.clear();
206 bool bBackToLoaded = false;
208 bool bSuccess = false;
209 if ( getCurrentState() == embed::EmbedStates::LOADED )
211 SAL_WARN( "embeddedobj.ole", "Loaded object has no cached size!" );
213 // try to switch the object to RUNNING state and request the value again
214 try {
215 changeState( embed::EmbedStates::RUNNING );
216 // the links should be switched back to loaded state to avoid too
217 // many open MathType instances
218 bBackToLoaded = true;
220 catch( const uno::Exception& )
222 throw embed::NoVisualAreaSizeException(
223 "No size available!",
224 static_cast< ::cppu::OWeakObject* >(this) );
230 // first try to get size using replacement image
231 aSize = m_pOleComponent->GetExtent( nAspect ); // will throw an exception in case of failure
232 bSuccess = true;
234 catch( const uno::Exception& )
238 if (bBackToLoaded)
242 changeState(embed::EmbedStates::LOADED);
244 catch( const uno::Exception& e )
246 SAL_WARN(
247 "embeddedobj.ole",
248 "ignoring Exception " << e.Message);
252 if ( !bSuccess )
256 // second try the cached replacement image
257 aSize = m_pOleComponent->GetCachedExtent( nAspect ); // will throw an exception in case of failure
258 bSuccess = true;
260 catch( const uno::Exception& )
265 if ( !bSuccess )
269 // third try the size reported by the object
270 aSize = m_pOleComponent->GetReccomendedExtent( nAspect ); // will throw an exception in case of failure
271 bSuccess = true;
273 catch( const uno::Exception& )
278 if ( !bSuccess )
279 throw embed::NoVisualAreaSizeException(
280 "No size available!",
281 static_cast< ::cppu::OWeakObject* >(this) );
283 aGuard.reset();
285 m_aCachedSize = aSize;
286 m_nCachedAspect = nAspect;
287 m_bHasCachedSize = true;
289 aResult = m_aCachedSize;
292 catch ( const embed::NoVisualAreaSizeException& )
294 throw;
296 catch ( const uno::Exception& )
298 throw embed::NoVisualAreaSizeException(
299 "No size available!",
300 static_cast< ::cppu::OWeakObject* >(this) );
303 else
304 #endif
306 // return cached value
307 if ( m_bHasCachedSize )
309 SAL_WARN_IF( nAspect != m_nCachedAspect, "embeddedobj.ole", "Unexpected aspect is requested!" );
310 aResult = m_aCachedSize;
312 else
314 throw embed::NoVisualAreaSizeException(
315 "No size available!",
316 static_cast< ::cppu::OWeakObject* >(this) );
320 return aResult;
323 embed::VisualRepresentation SAL_CALL OleEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect )
324 throw ( lang::IllegalArgumentException,
325 embed::WrongStateException,
326 uno::Exception,
327 uno::RuntimeException, std::exception )
329 // begin wrapping related part ====================
330 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
331 if ( xWrappedObject.is() )
333 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
334 return xWrappedObject->getPreferredVisualRepresentation( nAspect );
336 // end wrapping related part ====================
338 ::osl::MutexGuard aGuard( m_aMutex );
339 if ( m_bDisposed )
340 throw lang::DisposedException(); // TODO
342 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
343 if ( nAspect == embed::Aspects::MSOLE_ICON )
344 // no representation can be retrieved
345 throw embed::WrongStateException( "Illegal call!",
346 static_cast< ::cppu::OWeakObject* >(this) );
348 // TODO: if the object has cached representation then it should be returned
349 // TODO: if the object has no cached representation and is in loaded state it should switch itself to the running state
350 if ( m_nObjectState == -1 )
351 throw embed::WrongStateException( "The object is not loaded!",
352 static_cast< ::cppu::OWeakObject* >(this) );
354 embed::VisualRepresentation aVisualRepr;
356 // TODO: in case of different aspects they must be applied to the mediatype and XTransferable must be used
357 // the cache is used only as a fallback if object is not in loaded state
358 if ( !m_xCachedVisualRepresentation.is() && ( !m_bVisReplInitialized || m_bVisReplInStream )
359 && m_nObjectState == embed::EmbedStates::LOADED )
361 m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream, true );
362 SetVisReplInStream( m_xCachedVisualRepresentation.is() );
365 if ( m_xCachedVisualRepresentation.is() )
367 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation );
369 #ifdef _WIN32
370 else if ( m_pOleComponent )
374 if ( m_nObjectState == embed::EmbedStates::LOADED )
375 changeState( embed::EmbedStates::RUNNING );
377 datatransfer::DataFlavor aDataFlavor(
378 OUString( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
379 OUString( "Windows Metafile" ),
380 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
382 aVisualRepr.Data = m_pOleComponent->getTransferData( aDataFlavor );
383 aVisualRepr.Flavor = aDataFlavor;
385 uno::Sequence< sal_Int8 > aVisReplSeq;
386 aVisualRepr.Data >>= aVisReplSeq;
387 if ( aVisReplSeq.getLength() )
389 m_xCachedVisualRepresentation = GetNewFilledTempStream_Impl(
390 uno::Reference< io::XInputStream > ( static_cast< io::XInputStream* > (
391 new ::comphelper::SequenceInputStream( aVisReplSeq ) ) ) );
394 return aVisualRepr;
396 catch( const uno::Exception& )
399 #endif
401 // the cache is used only as a fallback if object is not in loaded state
402 if ( !m_xCachedVisualRepresentation.is() && ( !m_bVisReplInitialized || m_bVisReplInStream ) )
404 m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream );
405 SetVisReplInStream( m_xCachedVisualRepresentation.is() );
408 if ( !m_xCachedVisualRepresentation.is() )
410 // no representation can be retrieved
411 throw embed::WrongStateException( "Illegal call!",
412 static_cast< ::cppu::OWeakObject* >(this) );
415 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation );
418 sal_Int32 SAL_CALL OleEmbeddedObject::getMapUnit( sal_Int64 nAspect )
419 throw ( uno::Exception,
420 uno::RuntimeException, std::exception)
422 // begin wrapping related part ====================
423 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
424 if ( xWrappedObject.is() )
426 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
427 return xWrappedObject->getMapUnit( nAspect );
429 // end wrapping related part ====================
431 ::osl::MutexGuard aGuard( m_aMutex );
432 if ( m_bDisposed )
433 throw lang::DisposedException(); // TODO
435 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
436 if ( nAspect == embed::Aspects::MSOLE_ICON )
437 // no representation can be retrieved
438 throw embed::WrongStateException( "Illegal call!",
439 static_cast< ::cppu::OWeakObject* >(this) );
441 if ( m_nObjectState == -1 )
442 throw embed::WrongStateException( "The object is not loaded!",
443 static_cast< ::cppu::OWeakObject* >(this) );
445 return embed::EmbedMapUnits::ONE_100TH_MM;
449 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */