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 #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 <olecomponent.hxx>
30 #include <comphelper/mimeconfighelper.hxx>
31 #include <comphelper/seqstream.hxx>
33 using namespace ::com::sun::star
;
34 using namespace ::comphelper
;
36 embed::VisualRepresentation
OleEmbeddedObject::GetVisualRepresentationInNativeFormat_Impl(
37 const uno::Reference
< io::XStream
> xCachedVisRepr
)
38 throw ( uno::Exception
)
40 embed::VisualRepresentation aVisualRepr
;
42 // TODO: detect the format in the future for now use workaround
43 uno::Reference
< io::XInputStream
> xInStream
= xCachedVisRepr
->getInputStream();
44 uno::Reference
< io::XSeekable
> xSeekable( xCachedVisRepr
, uno::UNO_QUERY
);
45 if ( !xInStream
.is() || !xSeekable
.is() )
46 throw uno::RuntimeException();
48 uno::Sequence
< sal_Int8
> aSeq( 2 );
49 xInStream
->readBytes( aSeq
, 2 );
51 if ( aSeq
.getLength() == 2 && aSeq
[0] == 'B' && aSeq
[1] == 'M' )
54 aVisualRepr
.Flavor
= datatransfer::DataFlavor(
55 OUString( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ),
57 ::getCppuType( (const uno::Sequence
< sal_Int8
>*) NULL
) );
62 aVisualRepr
.Flavor
= datatransfer::DataFlavor(
63 OUString( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
64 OUString( "Windows Metafile" ),
65 ::getCppuType( (const uno::Sequence
< sal_Int8
>*) NULL
) );
68 sal_Int32 nStreamLength
= (sal_Int32
)xSeekable
->getLength();
69 uno::Sequence
< sal_Int8
> aRepresent( nStreamLength
);
70 xInStream
->readBytes( aRepresent
, nStreamLength
);
71 aVisualRepr
.Data
<<= aRepresent
;
76 void SAL_CALL
OleEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect
, const awt::Size
& aSize
)
77 throw ( lang::IllegalArgumentException
,
78 embed::WrongStateException
,
80 uno::RuntimeException
)
82 SAL_INFO( "embeddedobj.ole", "embeddedobj (mv76033) OleEmbeddedObject::setVisualAreaSize" );
84 // begin wrapping related part ====================
85 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
86 if ( xWrappedObject
.is() )
88 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
89 xWrappedObject
->setVisualAreaSize( nAspect
, aSize
);
92 // end wrapping related part ====================
94 ::osl::ResettableMutexGuard
aGuard( m_aMutex
);
96 throw lang::DisposedException(); // TODO
98 SAL_WARN_IF( nAspect
== embed::Aspects::MSOLE_ICON
, "embeddedobj.ole", "For iconified objects no graphical replacement is required!\n" );
99 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
100 // no representation can be retrieved
101 throw embed::WrongStateException( OUString( "Illegal call!\n" ),
102 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
104 if ( m_nObjectState
== -1 )
105 throw embed::WrongStateException( OUString( "The object is not loaded!\n" ),
106 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
109 // RECOMPOSE_ON_RESIZE misc flag means that the object has to be switched to running state on resize.
110 // SetExtent() is called only for objects that require it,
111 // it should not be called for MSWord documents to workaround problem i49369
112 // If cached size is not set, that means that this is the size initialization, so there is no need to set the real size
113 sal_Bool bAllowToSetExtent
=
114 ( ( getStatus( nAspect
) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE
)
115 && !MimeConfigurationHelper::ClassIDsEqual( m_aClassID
, MimeConfigurationHelper::GetSequenceClassID( 0x00020906L
, 0x0000, 0x0000,
116 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 ) )
117 && m_bHasCachedSize
);
119 if ( m_nObjectState
== embed::EmbedStates::LOADED
&& bAllowToSetExtent
)
123 changeState( embed::EmbedStates::RUNNING
);
125 catch( const uno::Exception
& )
127 SAL_WARN( "embeddedobj.ole", "The object should not be resized without activation!\n" );
132 if ( m_pOleComponent
&& m_nObjectState
!= embed::EmbedStates::LOADED
&& bAllowToSetExtent
)
134 awt::Size aSizeToSet
= aSize
;
137 m_pOleComponent
->SetExtent( aSizeToSet
, nAspect
); // will throw an exception in case of failure
138 m_bHasSizeToSet
= sal_False
;
140 catch( const uno::Exception
& )
142 // some objects do not allow to set the size even in running state
143 m_bHasSizeToSet
= sal_True
;
144 m_aSizeToSet
= aSizeToSet
;
145 m_nAspectToSet
= nAspect
;
152 m_bHasCachedSize
= sal_True
;
153 m_aCachedSize
= aSize
;
154 m_nCachedAspect
= nAspect
;
157 awt::Size SAL_CALL
OleEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect
)
158 throw ( lang::IllegalArgumentException
,
159 embed::WrongStateException
,
161 uno::RuntimeException
)
163 SAL_INFO( "embeddedobj.ole", "embeddedobj (mv76033) OleEmbeddedObject::getVisualAreaSize" );
165 // begin wrapping related part ====================
166 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
167 if ( xWrappedObject
.is() )
169 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
170 return xWrappedObject
->getVisualAreaSize( nAspect
);
172 // end wrapping related part ====================
174 ::osl::ResettableMutexGuard
aGuard( m_aMutex
);
176 throw lang::DisposedException(); // TODO
178 SAL_WARN_IF( nAspect
== embed::Aspects::MSOLE_ICON
, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
179 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
180 // no representation can be retrieved
181 throw embed::WrongStateException( OUString( "Illegal call!\n" ),
182 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
184 if ( m_nObjectState
== -1 )
185 throw embed::WrongStateException( OUString( "The object is not loaded!\n" ),
186 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
191 // TODO/LATER: Support different aspects
192 if ( m_pOleComponent
&& !m_bHasSizeToSet
&& nAspect
== embed::Aspects::MSOLE_CONTENT
)
196 // the cached size updated every time the object is stored
197 if ( m_bHasCachedSize
)
199 aResult
= m_aCachedSize
;
203 // there is no internal cache
207 sal_Bool bSuccess
= sal_False
;
208 if ( getCurrentState() == embed::EmbedStates::LOADED
)
210 SAL_WARN( "embeddedobj.ole", "Loaded object has no cached size!" );
212 // try to switch the object to RUNNING state and request the value again
214 changeState( embed::EmbedStates::RUNNING
);
216 catch( const uno::Exception
& )
218 throw embed::NoVisualAreaSizeException(
219 OUString( "No size available!\n" ),
220 uno::Reference
< uno::XInterface
>( 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
230 catch( const uno::Exception
& )
238 // second try the cached replacement image
239 aSize
= m_pOleComponent
->GetCachedExtent( nAspect
); // will throw an exception in case of failure
242 catch( const uno::Exception
& )
251 // third try the size reported by the object
252 aSize
= m_pOleComponent
->GetReccomendedExtent( nAspect
); // will throw an exception in case of failure
255 catch( const uno::Exception
& )
261 throw embed::NoVisualAreaSizeException(
262 OUString( "No size available!\n" ),
263 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
267 m_aCachedSize
= aSize
;
268 m_nCachedAspect
= nAspect
;
269 m_bHasCachedSize
= sal_True
;
271 aResult
= m_aCachedSize
;
274 catch ( const embed::NoVisualAreaSizeException
& )
278 catch ( const uno::Exception
& )
280 throw embed::NoVisualAreaSizeException(
281 OUString( "No size available!\n" ),
282 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
288 // return cached value
289 if ( m_bHasCachedSize
)
291 SAL_WARN_IF( nAspect
!= m_nCachedAspect
, "embeddedobj.ole", "Unexpected aspect is requested!" );
292 aResult
= m_aCachedSize
;
296 throw embed::NoVisualAreaSizeException(
297 OUString( "No size available!\n" ),
298 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
305 embed::VisualRepresentation SAL_CALL
OleEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect
)
306 throw ( lang::IllegalArgumentException
,
307 embed::WrongStateException
,
309 uno::RuntimeException
)
311 SAL_INFO( "embeddedobj.ole", "embeddedobj (mv76033) OleEmbeddedObject::getPreferredVisualRepresentation" );
313 // begin wrapping related part ====================
314 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
315 if ( xWrappedObject
.is() )
317 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
318 return xWrappedObject
->getPreferredVisualRepresentation( nAspect
);
320 // end wrapping related part ====================
322 ::osl::MutexGuard
aGuard( m_aMutex
);
324 throw lang::DisposedException(); // TODO
326 SAL_WARN_IF( nAspect
== embed::Aspects::MSOLE_ICON
, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
327 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
328 // no representation can be retrieved
329 throw embed::WrongStateException( OUString( "Illegal call!\n" ),
330 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
332 // TODO: if the object has cached representation then it should be returned
333 // TODO: if the object has no cached representation and is in loaded state it should switch itself to the running state
334 if ( m_nObjectState
== -1 )
335 throw embed::WrongStateException( OUString( "The object is not loaded!\n" ),
336 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
338 embed::VisualRepresentation aVisualRepr
;
340 // TODO: in case of different aspects they must be applied to the mediatype and XTransferable must be used
341 // the cache is used only as a fallback if object is not in loaded state
342 if ( !m_xCachedVisualRepresentation
.is() && ( !m_bVisReplInitialized
|| m_bVisReplInStream
)
343 && m_nObjectState
== embed::EmbedStates::LOADED
)
345 m_xCachedVisualRepresentation
= TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream
, sal_True
);
346 SetVisReplInStream( m_xCachedVisualRepresentation
.is() );
349 if ( m_xCachedVisualRepresentation
.is() )
351 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation
);
354 else if ( m_pOleComponent
)
358 if ( m_nObjectState
== embed::EmbedStates::LOADED
)
359 changeState( embed::EmbedStates::RUNNING
);
361 datatransfer::DataFlavor
aDataFlavor(
362 OUString( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
363 OUString( "Windows Metafile" ),
364 ::getCppuType( (const uno::Sequence
< sal_Int8
>*) NULL
) );
366 aVisualRepr
.Data
= m_pOleComponent
->getTransferData( aDataFlavor
);
367 aVisualRepr
.Flavor
= aDataFlavor
;
369 uno::Sequence
< sal_Int8
> aVisReplSeq
;
370 aVisualRepr
.Data
>>= aVisReplSeq
;
371 if ( aVisReplSeq
.getLength() )
373 m_xCachedVisualRepresentation
= GetNewFilledTempStream_Impl(
374 uno::Reference
< io::XInputStream
> ( static_cast< io::XInputStream
* > (
375 new ::comphelper::SequenceInputStream( aVisReplSeq
) ) ) );
380 catch( const uno::Exception
& )
385 // the cache is used only as a fallback if object is not in loaded state
386 if ( !m_xCachedVisualRepresentation
.is() && ( !m_bVisReplInitialized
|| m_bVisReplInStream
) )
388 m_xCachedVisualRepresentation
= TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream
);
389 SetVisReplInStream( m_xCachedVisualRepresentation
.is() );
392 if ( !m_xCachedVisualRepresentation
.is() )
394 // no representation can be retrieved
395 throw embed::WrongStateException( OUString( "Illegal call!\n" ),
396 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
399 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation
);
402 sal_Int32 SAL_CALL
OleEmbeddedObject::getMapUnit( sal_Int64 nAspect
)
403 throw ( uno::Exception
,
404 uno::RuntimeException
)
406 // begin wrapping related part ====================
407 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
408 if ( xWrappedObject
.is() )
410 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
411 return xWrappedObject
->getMapUnit( nAspect
);
413 // end wrapping related part ====================
415 ::osl::MutexGuard
aGuard( m_aMutex
);
417 throw lang::DisposedException(); // TODO
419 SAL_WARN_IF( nAspect
== embed::Aspects::MSOLE_ICON
, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
420 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
421 // no representation can be retrieved
422 throw embed::WrongStateException( OUString( "Illegal call!\n" ),
423 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
425 if ( m_nObjectState
== -1 )
426 throw embed::WrongStateException( OUString( "The object is not loaded!\n" ),
427 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
429 return embed::EmbedMapUnits::ONE_100TH_MM
;
433 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */