1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: olevisual.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_embeddedobj.hxx"
33 #include <com/sun/star/lang/DisposedException.hpp>
34 #include <com/sun/star/embed/EmbedStates.hpp>
35 #include <com/sun/star/embed/EmbedMapUnits.hpp>
36 #include <com/sun/star/embed/EmbedMisc.hpp>
37 #include <com/sun/star/embed/Aspects.hpp>
38 #include <com/sun/star/io/XSeekable.hpp>
39 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
41 #include <rtl/logfile.hxx>
43 #include <oleembobj.hxx>
44 #include <olecomponent.hxx>
45 #include <comphelper/mimeconfighelper.hxx>
46 #include <comphelper/seqstream.hxx>
48 using namespace ::com::sun::star
;
49 using namespace ::comphelper
;
51 embed::VisualRepresentation
OleEmbeddedObject::GetVisualRepresentationInNativeFormat_Impl(
52 const uno::Reference
< io::XStream
> xCachedVisRepr
)
53 throw ( uno::Exception
)
55 embed::VisualRepresentation aVisualRepr
;
57 // TODO: detect the format in the future for now use workaround
58 uno::Reference
< io::XInputStream
> xInStream
= xCachedVisRepr
->getInputStream();
59 uno::Reference
< io::XSeekable
> xSeekable( xCachedVisRepr
, uno::UNO_QUERY
);
60 if ( !xInStream
.is() || !xSeekable
.is() )
61 throw uno::RuntimeException();
63 uno::Sequence
< sal_Int8
> aSeq( 2 );
64 xInStream
->readBytes( aSeq
, 2 );
66 if ( aSeq
.getLength() == 2 && aSeq
[0] == 'B' && aSeq
[1] == 'M' )
69 aVisualRepr
.Flavor
= datatransfer::DataFlavor(
70 ::rtl::OUString::createFromAscii( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ),
71 ::rtl::OUString::createFromAscii( "Bitmap" ),
72 ::getCppuType( (const uno::Sequence
< sal_Int8
>*) NULL
) );
77 aVisualRepr
.Flavor
= datatransfer::DataFlavor(
78 ::rtl::OUString::createFromAscii( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
79 ::rtl::OUString::createFromAscii( "Windows Metafile" ),
80 ::getCppuType( (const uno::Sequence
< sal_Int8
>*) NULL
) );
83 sal_Int32 nStreamLength
= (sal_Int32
)xSeekable
->getLength();
84 uno::Sequence
< sal_Int8
> aRepresent( nStreamLength
);
85 xInStream
->readBytes( aRepresent
, nStreamLength
);
86 aVisualRepr
.Data
<<= aRepresent
;
91 void SAL_CALL
OleEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect
, const awt::Size
& aSize
)
92 throw ( lang::IllegalArgumentException
,
93 embed::WrongStateException
,
95 uno::RuntimeException
)
97 RTL_LOGFILE_CONTEXT( aLog
, "embeddedobj (mv76033) OleEmbeddedObject::setVisualAreaSize" );
99 // begin wrapping related part ====================
100 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
101 if ( xWrappedObject
.is() )
103 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
104 xWrappedObject
->setVisualAreaSize( nAspect
, aSize
);
107 // end wrapping related part ====================
109 ::osl::ResettableMutexGuard
aGuard( m_aMutex
);
111 throw lang::DisposedException(); // TODO
113 OSL_ENSURE( nAspect
!= embed::Aspects::MSOLE_ICON
, "For iconified objects no graphical replacement is required!\n" );
114 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
115 // no representation can be retrieved
116 throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Illegal call!\n" ),
117 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
119 if ( m_nObjectState
== -1 )
120 throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The object is not loaded!\n" ),
121 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
124 // RECOMPOSE_ON_RESIZE misc flag means that the object has to be switched to running state on resize.
125 // SetExtent() is called only for objects that require it,
126 // it should not be called for MSWord documents to workaround problem i49369
127 // If cached size is not set, that means that this is the size initialization, so there is no need to set the real size
128 sal_Bool bAllowToSetExtent
=
129 ( ( getStatus( nAspect
) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE
)
130 && !MimeConfigurationHelper::ClassIDsEqual( m_aClassID
, MimeConfigurationHelper::GetSequenceClassID( 0x00020906L
, 0x0000, 0x0000,
131 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 ) )
132 && m_bHasCachedSize
);
134 if ( m_nObjectState
== embed::EmbedStates::LOADED
&& bAllowToSetExtent
)
138 changeState( embed::EmbedStates::RUNNING
);
140 catch( uno::Exception
& )
142 OSL_ENSURE( sal_False
, "The object should not be resized without activation!\n" );
147 if ( m_pOleComponent
&& m_nObjectState
!= embed::EmbedStates::LOADED
&& bAllowToSetExtent
)
149 awt::Size aSizeToSet
= aSize
;
152 m_pOleComponent
->SetExtent( aSizeToSet
, nAspect
); // will throw an exception in case of failure
153 m_bHasSizeToSet
= sal_False
;
155 catch( uno::Exception
& )
157 // some objects do not allow to set the size even in running state
158 m_bHasSizeToSet
= sal_True
;
159 m_aSizeToSet
= aSizeToSet
;
160 m_nAspectToSet
= nAspect
;
167 m_bHasCachedSize
= sal_True
;
168 m_aCachedSize
= aSize
;
169 m_nCachedAspect
= nAspect
;
172 awt::Size SAL_CALL
OleEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect
)
173 throw ( lang::IllegalArgumentException
,
174 embed::WrongStateException
,
176 uno::RuntimeException
)
178 RTL_LOGFILE_CONTEXT( aLog
, "embeddedobj (mv76033) OleEmbeddedObject::getVisualAreaSize" );
180 // begin wrapping related part ====================
181 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
182 if ( xWrappedObject
.is() )
184 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
185 return xWrappedObject
->getVisualAreaSize( nAspect
);
187 // end wrapping related part ====================
189 ::osl::ResettableMutexGuard
aGuard( m_aMutex
);
191 throw lang::DisposedException(); // TODO
193 OSL_ENSURE( nAspect
!= embed::Aspects::MSOLE_ICON
, "For iconified objects no graphical replacement is required!\n" );
194 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
195 // no representation can be retrieved
196 throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Illegal call!\n" ),
197 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
199 if ( m_nObjectState
== -1 )
200 throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The object is not loaded!\n" ),
201 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
206 // TODO/LATER: Support different aspects
207 if ( m_pOleComponent
&& !m_bHasSizeToSet
&& nAspect
== embed::Aspects::MSOLE_CONTENT
)
211 // the cached size updated every time the object is stored
212 if ( m_bHasCachedSize
)
214 aResult
= m_aCachedSize
;
218 // there is no internal cache
222 sal_Bool bSuccess
= sal_False
;
223 if ( getCurrentState() == embed::EmbedStates::LOADED
)
225 OSL_ENSURE( sal_False
, "Loaded object has no cached size!\n" );
227 // try to switch the object to RUNNING state and request the value again
229 changeState( embed::EmbedStates::RUNNING
);
231 catch( uno::Exception
)
233 throw embed::NoVisualAreaSizeException(
234 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No size available!\n" ) ),
235 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
241 // first try to get size using replacement image
242 aSize
= m_pOleComponent
->GetExtent( nAspect
); // will throw an exception in case of failure
245 catch( uno::Exception
& )
253 // second try the cached replacement image
254 aSize
= m_pOleComponent
->GetCachedExtent( nAspect
); // will throw an exception in case of failure
257 catch( uno::Exception
& )
266 // third try the size reported by the object
267 aSize
= m_pOleComponent
->GetReccomendedExtent( nAspect
); // will throw an exception in case of failure
270 catch( uno::Exception
& )
276 throw embed::NoVisualAreaSizeException(
277 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No size available!\n" ) ),
278 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
282 m_aCachedSize
= aSize
;
283 m_nCachedAspect
= nAspect
;
284 m_bHasCachedSize
= sal_True
;
286 aResult
= m_aCachedSize
;
289 catch ( embed::NoVisualAreaSizeException
& )
293 catch ( uno::Exception
& )
295 throw embed::NoVisualAreaSizeException(
296 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No size available!\n" ) ),
297 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
303 // return cached value
304 if ( m_bHasCachedSize
)
306 OSL_ENSURE( nAspect
== m_nCachedAspect
, "Unexpected aspect is requested!\n" );
307 aResult
= m_aCachedSize
;
311 throw embed::NoVisualAreaSizeException(
312 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No size available!\n" ) ),
313 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
320 embed::VisualRepresentation SAL_CALL
OleEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect
)
321 throw ( lang::IllegalArgumentException
,
322 embed::WrongStateException
,
324 uno::RuntimeException
)
326 RTL_LOGFILE_CONTEXT( aLog
, "embeddedobj (mv76033) OleEmbeddedObject::getPreferredVisualRepresentation" );
328 // begin wrapping related part ====================
329 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
330 if ( xWrappedObject
.is() )
332 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
333 return xWrappedObject
->getPreferredVisualRepresentation( nAspect
);
335 // end wrapping related part ====================
337 ::osl::MutexGuard
aGuard( m_aMutex
);
339 throw lang::DisposedException(); // TODO
341 OSL_ENSURE( nAspect
!= embed::Aspects::MSOLE_ICON
, "For iconified objects no graphical replacement is required!\n" );
342 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
343 // no representation can be retrieved
344 throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Illegal call!\n" ),
345 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
347 // TODO: if the object has cached representation then it should be returned
348 // TODO: if the object has no cached representation and is in loaded state it should switch itself to the running state
349 if ( m_nObjectState
== -1 )
350 throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The object is not loaded!\n" ),
351 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
353 embed::VisualRepresentation aVisualRepr
;
355 // TODO: in case of different aspects they must be applied to the mediatype and XTransferable must be used
356 // the cache is used only as a fallback if object is not in loaded state
357 if ( !m_xCachedVisualRepresentation
.is() && ( !m_bVisReplInitialized
|| m_bVisReplInStream
)
358 && m_nObjectState
== embed::EmbedStates::LOADED
)
360 m_xCachedVisualRepresentation
= TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream
, sal_True
);
361 SetVisReplInStream( m_xCachedVisualRepresentation
.is() );
364 if ( m_xCachedVisualRepresentation
.is() )
366 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation
);
369 else if ( m_pOleComponent
)
373 if ( m_nObjectState
== embed::EmbedStates::LOADED
)
374 changeState( embed::EmbedStates::RUNNING
);
376 datatransfer::DataFlavor
aDataFlavor(
377 ::rtl::OUString::createFromAscii( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
378 ::rtl::OUString::createFromAscii( "Windows Metafile" ),
379 ::getCppuType( (const uno::Sequence
< sal_Int8
>*) NULL
) );
381 aVisualRepr
.Data
= m_pOleComponent
->getTransferData( aDataFlavor
);
382 aVisualRepr
.Flavor
= aDataFlavor
;
384 uno::Sequence
< sal_Int8
> aVisReplSeq
;
385 aVisualRepr
.Data
>>= aVisReplSeq
;
386 if ( aVisReplSeq
.getLength() )
388 m_xCachedVisualRepresentation
= GetNewFilledTempStream_Impl(
389 uno::Reference
< io::XInputStream
> ( static_cast< io::XInputStream
* > (
390 new ::comphelper::SequenceInputStream( aVisReplSeq
) ) ) );
395 catch( uno::Exception
& )
400 // the cache is used only as a fallback if object is not in loaded state
401 if ( !m_xCachedVisualRepresentation
.is() && ( !m_bVisReplInitialized
|| m_bVisReplInStream
) )
403 m_xCachedVisualRepresentation
= TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream
);
404 SetVisReplInStream( m_xCachedVisualRepresentation
.is() );
407 if ( !m_xCachedVisualRepresentation
.is() )
409 // no representation can be retrieved
410 throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Illegal call!\n" ),
411 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
414 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation
);
417 sal_Int32 SAL_CALL
OleEmbeddedObject::getMapUnit( sal_Int64 nAspect
)
418 throw ( uno::Exception
,
419 uno::RuntimeException
)
421 // begin wrapping related part ====================
422 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
423 if ( xWrappedObject
.is() )
425 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
426 return xWrappedObject
->getMapUnit( nAspect
);
428 // end wrapping related part ====================
430 ::osl::MutexGuard
aGuard( m_aMutex
);
432 throw lang::DisposedException(); // TODO
434 OSL_ENSURE( nAspect
!= embed::Aspects::MSOLE_ICON
, "For iconified objects no graphical replacement is required!\n" );
435 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
436 // no representation can be retrieved
437 throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Illegal call!\n" ),
438 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
440 if ( m_nObjectState
== -1 )
441 throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The object is not loaded!\n" ),
442 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
444 return embed::EmbedMapUnits::ONE_100TH_MM
;