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/embed/WrongStateException.hpp>
26 #include <com/sun/star/io/XSeekable.hpp>
27 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
29 #include <oleembobj.hxx>
30 #include <comphelper/mimeconfighelper.hxx>
31 #include <comphelper/seqstream.hxx>
32 #include <filter/msfilter/classids.hxx>
33 #include <sal/log.hxx>
36 #include "olecomponent.hxx"
37 #include <tools/diagnose_ex.h>
40 using namespace ::com::sun::star
;
41 using namespace ::comphelper
;
43 embed::VisualRepresentation
OleEmbeddedObject::GetVisualRepresentationInNativeFormat_Impl(
44 const uno::Reference
< io::XStream
>& xCachedVisRepr
)
46 embed::VisualRepresentation aVisualRepr
;
48 // TODO: detect the format in the future for now use workaround
49 uno::Reference
< io::XInputStream
> xInStream
= xCachedVisRepr
->getInputStream();
50 if ( !xInStream
.is() )
51 throw uno::RuntimeException();
52 uno::Reference
< io::XSeekable
> xSeekable( xCachedVisRepr
, uno::UNO_QUERY_THROW
);
54 uno::Sequence
< sal_Int8
> aSeq( 2 );
55 xInStream
->readBytes( aSeq
, 2 );
57 if ( aSeq
.getLength() == 2 && aSeq
[0] == 'B' && aSeq
[1] == 'M' )
60 aVisualRepr
.Flavor
= datatransfer::DataFlavor(
61 "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"",
63 cppu::UnoType
<uno::Sequence
< sal_Int8
>>::get() );
68 aVisualRepr
.Flavor
= datatransfer::DataFlavor(
69 "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"",
71 cppu::UnoType
<uno::Sequence
< sal_Int8
>>::get() );
74 sal_Int32 nStreamLength
= static_cast<sal_Int32
>(xSeekable
->getLength());
75 uno::Sequence
< sal_Int8
> aRepresent( nStreamLength
);
76 xInStream
->readBytes( aRepresent
, nStreamLength
);
77 aVisualRepr
.Data
<<= aRepresent
;
82 void SAL_CALL
OleEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect
, const awt::Size
& aSize
)
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!" );
99 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
100 // no representation can be retrieved
101 throw embed::WrongStateException( "Illegal call!",
102 static_cast< ::cppu::OWeakObject
* >(this) );
104 if ( m_nObjectState
== -1 )
105 throw embed::WrongStateException( "The object is not loaded!",
106 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 bool bAllowToSetExtent
=
114 ( ( getStatus( nAspect
) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE
)
115 && !MimeConfigurationHelper::ClassIDsEqual(m_aClassID
, MimeConfigurationHelper::GetSequenceClassID(MSO_WW8_CLASSID
))
116 && m_bHasCachedSize
);
118 if ( m_nObjectState
== embed::EmbedStates::LOADED
&& bAllowToSetExtent
)
122 changeState( embed::EmbedStates::RUNNING
);
124 catch( const uno::Exception
& )
126 SAL_WARN( "embeddedobj.ole", "The object should not be resized without activation!" );
131 if ( m_pOleComponent
&& m_nObjectState
!= embed::EmbedStates::LOADED
&& bAllowToSetExtent
)
133 awt::Size aSizeToSet
= aSize
;
136 m_pOleComponent
->SetExtent( aSizeToSet
, nAspect
); // will throw an exception in case of failure
137 m_bHasSizeToSet
= false;
139 catch( const uno::Exception
& )
141 // some objects do not allow to set the size even in running state
142 m_bHasSizeToSet
= true;
143 m_aSizeToSet
= aSizeToSet
;
144 m_nAspectToSet
= nAspect
;
151 m_bHasCachedSize
= true;
152 m_aCachedSize
= aSize
;
153 m_nCachedAspect
= nAspect
;
156 awt::Size SAL_CALL
OleEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect
)
158 // begin wrapping related part ====================
159 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
160 if ( xWrappedObject
.is() )
162 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
163 return xWrappedObject
->getVisualAreaSize( nAspect
);
165 // end wrapping related part ====================
167 ::osl::ResettableMutexGuard
aGuard( m_aMutex
);
169 throw lang::DisposedException(); // TODO
171 SAL_WARN_IF( nAspect
== embed::Aspects::MSOLE_ICON
, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
172 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
173 // no representation can be retrieved
174 throw embed::WrongStateException( "Illegal call!",
175 static_cast< ::cppu::OWeakObject
* >(this) );
177 if ( m_nObjectState
== -1 )
178 throw embed::WrongStateException( "The object is not loaded!",
179 static_cast< ::cppu::OWeakObject
* >(this) );
184 // TODO/LATER: Support different aspects
185 if ( m_pOleComponent
&& !m_bHasSizeToSet
&& nAspect
== embed::Aspects::MSOLE_CONTENT
)
189 // the cached size updated every time the object is stored
190 if ( m_bHasCachedSize
)
192 aResult
= m_aCachedSize
;
196 // there is no internal cache
200 bool bBackToLoaded
= false;
202 bool bSuccess
= false;
203 if ( getCurrentState() == embed::EmbedStates::LOADED
)
205 SAL_WARN( "embeddedobj.ole", "Loaded object has no cached size!" );
207 // try to switch the object to RUNNING state and request the value again
209 changeState( embed::EmbedStates::RUNNING
);
210 // the links should be switched back to loaded state to avoid too
211 // many open MathType instances
212 bBackToLoaded
= true;
214 catch( const uno::Exception
& )
216 throw embed::NoVisualAreaSizeException(
217 "No size available!",
218 static_cast< ::cppu::OWeakObject
* >(this) );
224 // first try to get size using replacement image
225 aSize
= m_pOleComponent
->GetExtent( nAspect
); // will throw an exception in case of failure
228 catch( const uno::Exception
& )
236 changeState(embed::EmbedStates::LOADED
);
238 catch( const uno::Exception
& )
240 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "ignoring ");
248 // second try the cached replacement image
249 aSize
= m_pOleComponent
->GetCachedExtent( nAspect
); // will throw an exception in case of failure
252 catch( const uno::Exception
& )
261 // third try the size reported by the object
262 aSize
= m_pOleComponent
->GetRecommendedExtent( nAspect
); // will throw an exception in case of failure
265 catch( const uno::Exception
& )
271 throw embed::NoVisualAreaSizeException(
272 "No size available!",
273 static_cast< ::cppu::OWeakObject
* >(this) );
277 m_aCachedSize
= aSize
;
278 m_nCachedAspect
= nAspect
;
279 m_bHasCachedSize
= true;
281 aResult
= m_aCachedSize
;
284 catch ( const embed::NoVisualAreaSizeException
& )
288 catch ( const uno::Exception
& )
290 throw embed::NoVisualAreaSizeException(
291 "No size available!",
292 static_cast< ::cppu::OWeakObject
* >(this) );
298 // return cached value
299 if ( !m_bHasCachedSize
)
301 throw embed::NoVisualAreaSizeException(
302 "No size available!",
303 static_cast< ::cppu::OWeakObject
* >(this) );
305 SAL_WARN_IF( nAspect
!= m_nCachedAspect
, "embeddedobj.ole", "Unexpected aspect is requested!" );
306 aResult
= m_aCachedSize
;
312 embed::VisualRepresentation SAL_CALL
OleEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect
)
314 // begin wrapping related part ====================
315 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
316 if ( xWrappedObject
.is() )
318 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
319 return xWrappedObject
->getPreferredVisualRepresentation( nAspect
);
321 // end wrapping related part ====================
323 ::osl::MutexGuard
aGuard( m_aMutex
);
325 throw lang::DisposedException(); // TODO
327 SAL_WARN_IF( nAspect
== embed::Aspects::MSOLE_ICON
, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
328 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
329 // no representation can be retrieved
330 throw embed::WrongStateException( "Illegal call!",
331 static_cast< ::cppu::OWeakObject
* >(this) );
333 // TODO: if the object has cached representation then it should be returned
334 // TODO: if the object has no cached representation and is in loaded state it should switch itself to the running state
335 if ( m_nObjectState
== -1 )
336 throw embed::WrongStateException( "The object is not loaded!",
337 static_cast< ::cppu::OWeakObject
* >(this) );
339 // TODO: in case of different aspects they must be applied to the mediatype and XTransferable must be used
340 // the cache is used only as a fallback if object is not in loaded state
341 if ( !m_xCachedVisualRepresentation
.is() && ( !m_bVisReplInitialized
|| m_bVisReplInStream
)
342 && m_nObjectState
== embed::EmbedStates::LOADED
)
344 m_xCachedVisualRepresentation
= TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream
, true );
345 SetVisReplInStream( m_xCachedVisualRepresentation
.is() );
349 if ( !m_xCachedVisualRepresentation
.is() && m_pOleComponent
)
353 if ( m_nObjectState
== embed::EmbedStates::LOADED
)
354 changeState( embed::EmbedStates::RUNNING
);
356 datatransfer::DataFlavor
aDataFlavor(
357 "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"",
359 cppu::UnoType
<uno::Sequence
< sal_Int8
>>::get() );
361 embed::VisualRepresentation aVisualRepr
;
362 aVisualRepr
.Data
= m_pOleComponent
->getTransferData( aDataFlavor
);
363 aVisualRepr
.Flavor
= aDataFlavor
;
365 uno::Sequence
< sal_Int8
> aVisReplSeq
;
366 aVisualRepr
.Data
>>= aVisReplSeq
;
367 if ( aVisReplSeq
.getLength() )
369 m_xCachedVisualRepresentation
= GetNewFilledTempStream_Impl(
370 uno::Reference
< io::XInputStream
>(
371 new ::comphelper::SequenceInputStream(aVisReplSeq
)));
376 catch( const uno::Exception
& )
381 // the cache is used only as a fallback if object is not in loaded state
382 if ( !m_xCachedVisualRepresentation
.is() && ( !m_bVisReplInitialized
|| m_bVisReplInStream
) )
384 m_xCachedVisualRepresentation
= TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream
);
385 SetVisReplInStream( m_xCachedVisualRepresentation
.is() );
388 if ( !m_xCachedVisualRepresentation
.is() )
390 // no representation can be retrieved
391 throw embed::WrongStateException( "Illegal call!",
392 static_cast< ::cppu::OWeakObject
* >(this) );
395 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation
);
398 sal_Int32 SAL_CALL
OleEmbeddedObject::getMapUnit( sal_Int64 nAspect
)
400 // begin wrapping related part ====================
401 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
402 if ( xWrappedObject
.is() )
404 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
405 return xWrappedObject
->getMapUnit( nAspect
);
407 // end wrapping related part ====================
409 ::osl::MutexGuard
aGuard( m_aMutex
);
411 throw lang::DisposedException(); // TODO
413 SAL_WARN_IF( nAspect
== embed::Aspects::MSOLE_ICON
, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
414 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
415 // no representation can be retrieved
416 throw embed::WrongStateException( "Illegal call!",
417 static_cast< ::cppu::OWeakObject
* >(this) );
419 if ( m_nObjectState
== -1 )
420 throw embed::WrongStateException( "The object is not loaded!",
421 static_cast< ::cppu::OWeakObject
* >(this) );
423 return embed::EmbedMapUnits::ONE_100TH_MM
;
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */