Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / embeddedobj / source / msole / olevisual.cxx
blob617d8e4d7ea45d4d670d080b0ba9e38fd719c7c8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <com/sun/star/lang/DisposedException.hpp>
30 #include <com/sun/star/embed/EmbedStates.hpp>
31 #include <com/sun/star/embed/EmbedMapUnits.hpp>
32 #include <com/sun/star/embed/EmbedMisc.hpp>
33 #include <com/sun/star/embed/Aspects.hpp>
34 #include <com/sun/star/io/XSeekable.hpp>
35 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
37 #include <rtl/logfile.hxx>
39 #include <oleembobj.hxx>
40 #include <olecomponent.hxx>
41 #include <comphelper/mimeconfighelper.hxx>
42 #include <comphelper/seqstream.hxx>
44 using namespace ::com::sun::star;
45 using namespace ::comphelper;
47 embed::VisualRepresentation OleEmbeddedObject::GetVisualRepresentationInNativeFormat_Impl(
48 const uno::Reference< io::XStream > xCachedVisRepr )
49 throw ( uno::Exception )
51 embed::VisualRepresentation aVisualRepr;
53 // TODO: detect the format in the future for now use workaround
54 uno::Reference< io::XInputStream > xInStream = xCachedVisRepr->getInputStream();
55 uno::Reference< io::XSeekable > xSeekable( xCachedVisRepr, uno::UNO_QUERY );
56 if ( !xInStream.is() || !xSeekable.is() )
57 throw uno::RuntimeException();
59 uno::Sequence< sal_Int8 > aSeq( 2 );
60 xInStream->readBytes( aSeq, 2 );
61 xSeekable->seek( 0 );
62 if ( aSeq.getLength() == 2 && aSeq[0] == 'B' && aSeq[1] == 'M' )
64 // it's a bitmap
65 aVisualRepr.Flavor = datatransfer::DataFlavor(
66 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" )),
67 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Bitmap" )),
68 ::getCppuType( (const uno::Sequence< sal_Int8 >*) NULL ) );
70 else
72 // it's a metafile
73 aVisualRepr.Flavor = datatransfer::DataFlavor(
74 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" )),
75 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Windows Metafile" )),
76 ::getCppuType( (const uno::Sequence< sal_Int8 >*) NULL ) );
79 sal_Int32 nStreamLength = (sal_Int32)xSeekable->getLength();
80 uno::Sequence< sal_Int8 > aRepresent( nStreamLength );
81 xInStream->readBytes( aRepresent, nStreamLength );
82 aVisualRepr.Data <<= aRepresent;
84 return aVisualRepr;
87 void SAL_CALL OleEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect, const awt::Size& aSize )
88 throw ( lang::IllegalArgumentException,
89 embed::WrongStateException,
90 uno::Exception,
91 uno::RuntimeException )
93 RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OleEmbeddedObject::setVisualAreaSize" );
95 // begin wrapping related part ====================
96 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
97 if ( xWrappedObject.is() )
99 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
100 xWrappedObject->setVisualAreaSize( nAspect, aSize );
101 return;
103 // end wrapping related part ====================
105 ::osl::ResettableMutexGuard aGuard( m_aMutex );
106 if ( m_bDisposed )
107 throw lang::DisposedException(); // TODO
109 OSL_ENSURE( nAspect != embed::Aspects::MSOLE_ICON, "For iconified objects no graphical replacement is required!\n" );
110 if ( nAspect == embed::Aspects::MSOLE_ICON )
111 // no representation can be retrieved
112 throw embed::WrongStateException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Illegal call!\n" )),
113 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
115 if ( m_nObjectState == -1 )
116 throw embed::WrongStateException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "The object is not loaded!\n" )),
117 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
119 #ifdef WNT
120 // RECOMPOSE_ON_RESIZE misc flag means that the object has to be switched to running state on resize.
121 // SetExtent() is called only for objects that require it,
122 // it should not be called for MSWord documents to workaround problem i49369
123 // If cached size is not set, that means that this is the size initialization, so there is no need to set the real size
124 sal_Bool bAllowToSetExtent =
125 ( ( getStatus( nAspect ) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE )
126 && !MimeConfigurationHelper::ClassIDsEqual( m_aClassID, MimeConfigurationHelper::GetSequenceClassID( 0x00020906L, 0x0000, 0x0000,
127 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 ) )
128 && m_bHasCachedSize );
130 if ( m_nObjectState == embed::EmbedStates::LOADED && bAllowToSetExtent )
132 aGuard.clear();
133 try {
134 changeState( embed::EmbedStates::RUNNING );
136 catch( const uno::Exception& )
138 OSL_FAIL( "The object should not be resized without activation!\n" );
140 aGuard.reset();
143 if ( m_pOleComponent && m_nObjectState != embed::EmbedStates::LOADED && bAllowToSetExtent )
145 awt::Size aSizeToSet = aSize;
146 aGuard.clear();
147 try {
148 m_pOleComponent->SetExtent( aSizeToSet, nAspect ); // will throw an exception in case of failure
149 m_bHasSizeToSet = sal_False;
151 catch( const uno::Exception& )
153 // some objects do not allow to set the size even in running state
154 m_bHasSizeToSet = sal_True;
155 m_aSizeToSet = aSizeToSet;
156 m_nAspectToSet = nAspect;
158 aGuard.reset();
160 #endif
162 // cache the values
163 m_bHasCachedSize = sal_True;
164 m_aCachedSize = aSize;
165 m_nCachedAspect = nAspect;
168 awt::Size SAL_CALL OleEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect )
169 throw ( lang::IllegalArgumentException,
170 embed::WrongStateException,
171 uno::Exception,
172 uno::RuntimeException )
174 RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OleEmbeddedObject::getVisualAreaSize" );
176 // begin wrapping related part ====================
177 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
178 if ( xWrappedObject.is() )
180 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
181 return xWrappedObject->getVisualAreaSize( nAspect );
183 // end wrapping related part ====================
185 ::osl::ResettableMutexGuard aGuard( m_aMutex );
186 if ( m_bDisposed )
187 throw lang::DisposedException(); // TODO
189 OSL_ENSURE( nAspect != embed::Aspects::MSOLE_ICON, "For iconified objects no graphical replacement is required!\n" );
190 if ( nAspect == embed::Aspects::MSOLE_ICON )
191 // no representation can be retrieved
192 throw embed::WrongStateException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Illegal call!\n" )),
193 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
195 if ( m_nObjectState == -1 )
196 throw embed::WrongStateException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "The object is not loaded!\n" )),
197 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
199 awt::Size aResult;
201 #ifdef WNT
202 // TODO/LATER: Support different aspects
203 if ( m_pOleComponent && !m_bHasSizeToSet && nAspect == embed::Aspects::MSOLE_CONTENT )
207 // the cached size updated every time the object is stored
208 if ( m_bHasCachedSize )
210 aResult = m_aCachedSize;
212 else
214 // there is no internal cache
215 awt::Size aSize;
216 aGuard.clear();
218 sal_Bool bSuccess = sal_False;
219 if ( getCurrentState() == embed::EmbedStates::LOADED )
221 OSL_FAIL( "Loaded object has no cached size!\n" );
223 // try to switch the object to RUNNING state and request the value again
224 try {
225 changeState( embed::EmbedStates::RUNNING );
227 catch( const uno::Exception& )
229 throw embed::NoVisualAreaSizeException(
230 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No size available!\n" ) ),
231 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
237 // first try to get size using replacement image
238 aSize = m_pOleComponent->GetExtent( nAspect ); // will throw an exception in case of failure
239 bSuccess = sal_True;
241 catch( const uno::Exception& )
245 if ( !bSuccess )
249 // second try the cached replacement image
250 aSize = m_pOleComponent->GetCachedExtent( nAspect ); // will throw an exception in case of failure
251 bSuccess = sal_True;
253 catch( const uno::Exception& )
258 if ( !bSuccess )
262 // third try the size reported by the object
263 aSize = m_pOleComponent->GetReccomendedExtent( nAspect ); // will throw an exception in case of failure
264 bSuccess = sal_True;
266 catch( const uno::Exception& )
271 if ( !bSuccess )
272 throw embed::NoVisualAreaSizeException(
273 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No size available!\n" ) ),
274 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
276 aGuard.reset();
278 m_aCachedSize = aSize;
279 m_nCachedAspect = nAspect;
280 m_bHasCachedSize = sal_True;
282 aResult = m_aCachedSize;
285 catch ( const embed::NoVisualAreaSizeException& )
287 throw;
289 catch ( const uno::Exception& )
291 throw embed::NoVisualAreaSizeException(
292 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No size available!\n" ) ),
293 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
296 else
297 #endif
299 // return cached value
300 if ( m_bHasCachedSize )
302 OSL_ENSURE( nAspect == m_nCachedAspect, "Unexpected aspect is requested!\n" );
303 aResult = m_aCachedSize;
305 else
307 throw embed::NoVisualAreaSizeException(
308 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No size available!\n" ) ),
309 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
313 return aResult;
316 embed::VisualRepresentation SAL_CALL OleEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect )
317 throw ( lang::IllegalArgumentException,
318 embed::WrongStateException,
319 uno::Exception,
320 uno::RuntimeException )
322 RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OleEmbeddedObject::getPreferredVisualRepresentation" );
324 // begin wrapping related part ====================
325 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
326 if ( xWrappedObject.is() )
328 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
329 return xWrappedObject->getPreferredVisualRepresentation( nAspect );
331 // end wrapping related part ====================
333 ::osl::MutexGuard aGuard( m_aMutex );
334 if ( m_bDisposed )
335 throw lang::DisposedException(); // TODO
337 OSL_ENSURE( nAspect != embed::Aspects::MSOLE_ICON, "For iconified objects no graphical replacement is required!\n" );
338 if ( nAspect == embed::Aspects::MSOLE_ICON )
339 // no representation can be retrieved
340 throw embed::WrongStateException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Illegal call!\n" )),
341 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
343 // TODO: if the object has cached representation then it should be returned
344 // TODO: if the object has no cached representation and is in loaded state it should switch itself to the running state
345 if ( m_nObjectState == -1 )
346 throw embed::WrongStateException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "The object is not loaded!\n" )),
347 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
349 embed::VisualRepresentation aVisualRepr;
351 // TODO: in case of different aspects they must be applied to the mediatype and XTransferable must be used
352 // the cache is used only as a fallback if object is not in loaded state
353 if ( !m_xCachedVisualRepresentation.is() && ( !m_bVisReplInitialized || m_bVisReplInStream )
354 && m_nObjectState == embed::EmbedStates::LOADED )
356 m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream, sal_True );
357 SetVisReplInStream( m_xCachedVisualRepresentation.is() );
360 if ( m_xCachedVisualRepresentation.is() )
362 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation );
364 #ifdef WNT
365 else if ( m_pOleComponent )
369 if ( m_nObjectState == embed::EmbedStates::LOADED )
370 changeState( embed::EmbedStates::RUNNING );
372 datatransfer::DataFlavor aDataFlavor(
373 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" )),
374 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Windows Metafile" )),
375 ::getCppuType( (const uno::Sequence< sal_Int8 >*) NULL ) );
377 aVisualRepr.Data = m_pOleComponent->getTransferData( aDataFlavor );
378 aVisualRepr.Flavor = aDataFlavor;
380 uno::Sequence< sal_Int8 > aVisReplSeq;
381 aVisualRepr.Data >>= aVisReplSeq;
382 if ( aVisReplSeq.getLength() )
384 m_xCachedVisualRepresentation = GetNewFilledTempStream_Impl(
385 uno::Reference< io::XInputStream > ( static_cast< io::XInputStream* > (
386 new ::comphelper::SequenceInputStream( aVisReplSeq ) ) ) );
389 return aVisualRepr;
391 catch( const uno::Exception& )
394 #endif
396 // the cache is used only as a fallback if object is not in loaded state
397 if ( !m_xCachedVisualRepresentation.is() && ( !m_bVisReplInitialized || m_bVisReplInStream ) )
399 m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream );
400 SetVisReplInStream( m_xCachedVisualRepresentation.is() );
403 if ( !m_xCachedVisualRepresentation.is() )
405 // no representation can be retrieved
406 throw embed::WrongStateException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Illegal call!\n" )),
407 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
410 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation );
413 sal_Int32 SAL_CALL OleEmbeddedObject::getMapUnit( sal_Int64 nAspect )
414 throw ( uno::Exception,
415 uno::RuntimeException)
417 // begin wrapping related part ====================
418 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
419 if ( xWrappedObject.is() )
421 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
422 return xWrappedObject->getMapUnit( nAspect );
424 // end wrapping related part ====================
426 ::osl::MutexGuard aGuard( m_aMutex );
427 if ( m_bDisposed )
428 throw lang::DisposedException(); // TODO
430 OSL_ENSURE( nAspect != embed::Aspects::MSOLE_ICON, "For iconified objects no graphical replacement is required!\n" );
431 if ( nAspect == embed::Aspects::MSOLE_ICON )
432 // no representation can be retrieved
433 throw embed::WrongStateException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Illegal call!\n" )),
434 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
436 if ( m_nObjectState == -1 )
437 throw embed::WrongStateException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "The object is not loaded!\n" )),
438 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
440 return embed::EmbedMapUnits::ONE_100TH_MM;
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */