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 <comphelper/mimeconfighelper.hxx>
30 #include <comphelper/seqstream.hxx>
33 #include <olecomponent.hxx>
36 using namespace ::com::sun::star
;
37 using namespace ::comphelper
;
39 embed::VisualRepresentation
OleEmbeddedObject::GetVisualRepresentationInNativeFormat_Impl(
40 const uno::Reference
< io::XStream
> xCachedVisRepr
)
41 throw ( uno::Exception
)
43 embed::VisualRepresentation aVisualRepr
;
45 // TODO: detect the format in the future for now use workaround
46 uno::Reference
< io::XInputStream
> xInStream
= xCachedVisRepr
->getInputStream();
47 uno::Reference
< io::XSeekable
> xSeekable( xCachedVisRepr
, uno::UNO_QUERY
);
48 if ( !xInStream
.is() || !xSeekable
.is() )
49 throw uno::RuntimeException();
51 uno::Sequence
< sal_Int8
> aSeq( 2 );
52 xInStream
->readBytes( aSeq
, 2 );
54 if ( aSeq
.getLength() == 2 && aSeq
[0] == 'B' && aSeq
[1] == 'M' )
57 aVisualRepr
.Flavor
= datatransfer::DataFlavor(
58 OUString( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ),
60 cppu::UnoType
<uno::Sequence
< sal_Int8
>>::get() );
65 aVisualRepr
.Flavor
= datatransfer::DataFlavor(
66 OUString( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
67 OUString( "Windows Metafile" ),
68 cppu::UnoType
<uno::Sequence
< sal_Int8
>>::get() );
71 sal_Int32 nStreamLength
= (sal_Int32
)xSeekable
->getLength();
72 uno::Sequence
< sal_Int8
> aRepresent( nStreamLength
);
73 xInStream
->readBytes( aRepresent
, nStreamLength
);
74 aVisualRepr
.Data
<<= aRepresent
;
79 void SAL_CALL
OleEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect
, const awt::Size
& aSize
)
80 throw ( lang::IllegalArgumentException
,
81 embed::WrongStateException
,
83 uno::RuntimeException
, std::exception
)
85 // begin wrapping related part ====================
86 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
87 if ( xWrappedObject
.is() )
89 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
90 xWrappedObject
->setVisualAreaSize( nAspect
, aSize
);
93 // end wrapping related part ====================
95 ::osl::ResettableMutexGuard
aGuard( m_aMutex
);
97 throw lang::DisposedException(); // TODO
99 SAL_WARN_IF( nAspect
== embed::Aspects::MSOLE_ICON
, "embeddedobj.ole", "For iconified objects no graphical replacement is required!\n" );
100 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
101 // no representation can be retrieved
102 throw embed::WrongStateException( "Illegal call!",
103 static_cast< ::cppu::OWeakObject
* >(this) );
105 if ( m_nObjectState
== -1 )
106 throw embed::WrongStateException( "The object is not loaded!",
107 static_cast< ::cppu::OWeakObject
* >(this) );
110 // RECOMPOSE_ON_RESIZE misc flag means that the object has to be switched to running state on resize.
111 // SetExtent() is called only for objects that require it,
112 // it should not be called for MSWord documents to workaround problem i49369
113 // If cached size is not set, that means that this is the size initialization, so there is no need to set the real size
114 sal_Bool bAllowToSetExtent
=
115 ( ( getStatus( nAspect
) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE
)
116 && !MimeConfigurationHelper::ClassIDsEqual( m_aClassID
, MimeConfigurationHelper::GetSequenceClassID( 0x00020906L
, 0x0000, 0x0000,
117 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 ) )
118 && m_bHasCachedSize
);
120 if ( m_nObjectState
== embed::EmbedStates::LOADED
&& bAllowToSetExtent
)
124 changeState( embed::EmbedStates::RUNNING
);
126 catch( const uno::Exception
& )
128 SAL_WARN( "embeddedobj.ole", "The object should not be resized without activation!\n" );
133 if ( m_pOleComponent
&& m_nObjectState
!= embed::EmbedStates::LOADED
&& bAllowToSetExtent
)
135 awt::Size aSizeToSet
= aSize
;
138 m_pOleComponent
->SetExtent( aSizeToSet
, nAspect
); // will throw an exception in case of failure
139 m_bHasSizeToSet
= sal_False
;
141 catch( const uno::Exception
& )
143 // some objects do not allow to set the size even in running state
144 m_bHasSizeToSet
= sal_True
;
145 m_aSizeToSet
= aSizeToSet
;
146 m_nAspectToSet
= nAspect
;
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
,
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
);
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) );
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
;
202 // there is no internal cache
206 sal_Bool bSuccess
= sal_False
;
207 if ( getCurrentState() == embed::EmbedStates::LOADED
)
209 SAL_WARN( "embeddedobj.ole", "Loaded object has no cached size!" );
211 // try to switch the object to RUNNING state and request the value again
213 changeState( embed::EmbedStates::RUNNING
);
215 catch( const uno::Exception
& )
217 throw embed::NoVisualAreaSizeException(
218 "No size available!",
219 static_cast< ::cppu::OWeakObject
* >(this) );
225 // first try to get size using replacement image
226 aSize
= m_pOleComponent
->GetExtent( nAspect
); // will throw an exception in case of failure
229 catch( const uno::Exception
& )
237 // second try the cached replacement image
238 aSize
= m_pOleComponent
->GetCachedExtent( nAspect
); // will throw an exception in case of failure
241 catch( const uno::Exception
& )
250 // third try the size reported by the object
251 aSize
= m_pOleComponent
->GetReccomendedExtent( nAspect
); // will throw an exception in case of failure
254 catch( const uno::Exception
& )
260 throw embed::NoVisualAreaSizeException(
261 "No size available!",
262 static_cast< ::cppu::OWeakObject
* >(this) );
266 m_aCachedSize
= aSize
;
267 m_nCachedAspect
= nAspect
;
268 m_bHasCachedSize
= sal_True
;
270 aResult
= m_aCachedSize
;
273 catch ( const embed::NoVisualAreaSizeException
& )
277 catch ( const uno::Exception
& )
279 throw embed::NoVisualAreaSizeException(
280 "No size available!",
281 static_cast< ::cppu::OWeakObject
* >(this) );
287 // return cached value
288 if ( m_bHasCachedSize
)
290 SAL_WARN_IF( nAspect
!= m_nCachedAspect
, "embeddedobj.ole", "Unexpected aspect is requested!" );
291 aResult
= m_aCachedSize
;
295 throw embed::NoVisualAreaSizeException(
296 "No size available!",
297 static_cast< ::cppu::OWeakObject
* >(this) );
304 embed::VisualRepresentation SAL_CALL
OleEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect
)
305 throw ( lang::IllegalArgumentException
,
306 embed::WrongStateException
,
308 uno::RuntimeException
, std::exception
)
310 // begin wrapping related part ====================
311 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
312 if ( xWrappedObject
.is() )
314 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
315 return xWrappedObject
->getPreferredVisualRepresentation( nAspect
);
317 // end wrapping related part ====================
319 ::osl::MutexGuard
aGuard( m_aMutex
);
321 throw lang::DisposedException(); // TODO
323 SAL_WARN_IF( nAspect
== embed::Aspects::MSOLE_ICON
, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
324 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
325 // no representation can be retrieved
326 throw embed::WrongStateException( "Illegal call!",
327 static_cast< ::cppu::OWeakObject
* >(this) );
329 // TODO: if the object has cached representation then it should be returned
330 // TODO: if the object has no cached representation and is in loaded state it should switch itself to the running state
331 if ( m_nObjectState
== -1 )
332 throw embed::WrongStateException( "The object is not loaded!",
333 static_cast< ::cppu::OWeakObject
* >(this) );
335 embed::VisualRepresentation aVisualRepr
;
337 // TODO: in case of different aspects they must be applied to the mediatype and XTransferable must be used
338 // the cache is used only as a fallback if object is not in loaded state
339 if ( !m_xCachedVisualRepresentation
.is() && ( !m_bVisReplInitialized
|| m_bVisReplInStream
)
340 && m_nObjectState
== embed::EmbedStates::LOADED
)
342 m_xCachedVisualRepresentation
= TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream
, true );
343 SetVisReplInStream( m_xCachedVisualRepresentation
.is() );
346 if ( m_xCachedVisualRepresentation
.is() )
348 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation
);
351 else if ( m_pOleComponent
)
355 if ( m_nObjectState
== embed::EmbedStates::LOADED
)
356 changeState( embed::EmbedStates::RUNNING
);
358 datatransfer::DataFlavor
aDataFlavor(
359 OUString( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
360 OUString( "Windows Metafile" ),
361 cppu::UnoType
<uno::Sequence
< sal_Int8
>>::get() );
363 aVisualRepr
.Data
= m_pOleComponent
->getTransferData( aDataFlavor
);
364 aVisualRepr
.Flavor
= aDataFlavor
;
366 uno::Sequence
< sal_Int8
> aVisReplSeq
;
367 aVisualRepr
.Data
>>= aVisReplSeq
;
368 if ( aVisReplSeq
.getLength() )
370 m_xCachedVisualRepresentation
= GetNewFilledTempStream_Impl(
371 uno::Reference
< io::XInputStream
> ( static_cast< io::XInputStream
* > (
372 new ::comphelper::SequenceInputStream( aVisReplSeq
) ) ) );
377 catch( const uno::Exception
& )
382 // the cache is used only as a fallback if object is not in loaded state
383 if ( !m_xCachedVisualRepresentation
.is() && ( !m_bVisReplInitialized
|| m_bVisReplInStream
) )
385 m_xCachedVisualRepresentation
= TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream
);
386 SetVisReplInStream( m_xCachedVisualRepresentation
.is() );
389 if ( !m_xCachedVisualRepresentation
.is() )
391 // no representation can be retrieved
392 throw embed::WrongStateException( "Illegal call!",
393 static_cast< ::cppu::OWeakObject
* >(this) );
396 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation
);
399 sal_Int32 SAL_CALL
OleEmbeddedObject::getMapUnit( sal_Int64 nAspect
)
400 throw ( uno::Exception
,
401 uno::RuntimeException
, std::exception
)
403 // begin wrapping related part ====================
404 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
405 if ( xWrappedObject
.is() )
407 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
408 return xWrappedObject
->getMapUnit( nAspect
);
410 // end wrapping related part ====================
412 ::osl::MutexGuard
aGuard( m_aMutex
);
414 throw lang::DisposedException(); // TODO
416 SAL_WARN_IF( nAspect
== embed::Aspects::MSOLE_ICON
, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
417 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
418 // no representation can be retrieved
419 throw embed::WrongStateException( "Illegal call!",
420 static_cast< ::cppu::OWeakObject
* >(this) );
422 if ( m_nObjectState
== -1 )
423 throw embed::WrongStateException( "The object is not loaded!",
424 static_cast< ::cppu::OWeakObject
* >(this) );
426 return embed::EmbedMapUnits::ONE_100TH_MM
;
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */