tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / embeddedobj / source / msole / olevisual.cxx
blobbcee997c189eef60e462a7a2a9524c881319c139
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #if defined (_WIN32)
31 #include <comphelper/mimeconfighelper.hxx>
32 #endif
33 #include <comphelper/seqstream.hxx>
34 #include <filter/msfilter/classids.hxx>
35 #include <sal/log.hxx>
37 #if defined(_WIN32)
38 #include "olecomponent.hxx"
39 #include <comphelper/diagnose_ex.hxx>
40 #endif
42 using namespace ::com::sun::star;
43 using namespace ::comphelper;
45 embed::VisualRepresentation OleEmbeddedObject::GetVisualRepresentationInNativeFormat_Impl(
46 const uno::Reference< io::XStream >& xCachedVisRepr )
48 embed::VisualRepresentation aVisualRepr;
50 // TODO: detect the format in the future for now use workaround
51 uno::Reference< io::XInputStream > xInStream = xCachedVisRepr->getInputStream();
52 if ( !xInStream.is() )
53 throw uno::RuntimeException();
54 uno::Reference< io::XSeekable > xSeekable( xCachedVisRepr, uno::UNO_QUERY_THROW );
56 uno::Sequence< sal_Int8 > aSeq( 2 );
57 xInStream->readBytes( aSeq, 2 );
58 xSeekable->seek( 0 );
59 if ( aSeq.getLength() == 2 && aSeq[0] == 'B' && aSeq[1] == 'M' )
61 // it's a bitmap
62 aVisualRepr.Flavor = datatransfer::DataFlavor(
63 u"application/x-openoffice-bitmap;windows_formatname=\"Bitmap\""_ustr,
64 u"Bitmap"_ustr,
65 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
67 else
69 // it's a metafile
70 aVisualRepr.Flavor = datatransfer::DataFlavor(
71 u"application/x-openoffice-wmf;windows_formatname=\"Image WMF\""_ustr,
72 u"Windows Metafile"_ustr,
73 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
76 sal_Int32 nStreamLength = static_cast<sal_Int32>(xSeekable->getLength());
77 uno::Sequence< sal_Int8 > aRepresent( nStreamLength );
78 xInStream->readBytes( aRepresent, nStreamLength );
79 aVisualRepr.Data <<= aRepresent;
81 return aVisualRepr;
84 void SAL_CALL OleEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect, const awt::Size& aSize )
86 // begin wrapping related part ====================
87 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
88 if ( xWrappedObject.is() )
90 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
91 xWrappedObject->setVisualAreaSize( nAspect, aSize );
92 return;
94 // end wrapping related part ====================
96 ::osl::ResettableMutexGuard aGuard( m_aMutex );
97 if ( m_bDisposed )
98 throw lang::DisposedException(); // TODO
100 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
101 if ( nAspect == embed::Aspects::MSOLE_ICON )
102 // no representation can be retrieved
103 throw embed::WrongStateException( u"Illegal call!"_ustr,
104 static_cast< ::cppu::OWeakObject* >(this) );
106 if ( m_nObjectState == -1 )
107 throw embed::WrongStateException( u"The object is not loaded!"_ustr,
108 static_cast< ::cppu::OWeakObject* >(this) );
110 #ifdef _WIN32
111 // RECOMPOSE_ON_RESIZE misc flag means that the object has to be switched to running state on resize.
112 // SetExtent() is called only for objects that require it,
113 // it should not be called for MSWord documents to workaround problem i49369
114 // If cached size is not set, that means that this is the size initialization, so there is no need to set the real size
115 bool bAllowToSetExtent =
116 ( ( getStatus( nAspect ) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE )
117 && !MimeConfigurationHelper::ClassIDsEqual(m_aClassID, MimeConfigurationHelper::GetSequenceClassID(MSO_WW8_CLASSID))
118 && m_bHasCachedSize );
120 if ( m_nObjectState == embed::EmbedStates::LOADED && bAllowToSetExtent )
122 aGuard.clear();
123 try {
124 changeState( embed::EmbedStates::RUNNING );
126 catch( const uno::Exception& )
128 SAL_WARN( "embeddedobj.ole", "The object should not be resized without activation!" );
130 aGuard.reset();
133 if ( m_pOleComponent && m_nObjectState != embed::EmbedStates::LOADED && bAllowToSetExtent )
135 awt::Size aSizeToSet = aSize;
136 aGuard.clear();
137 try {
138 m_pOleComponent->SetExtent( aSizeToSet, nAspect ); // will throw an exception in case of failure
139 m_bHasSizeToSet = false;
141 catch( const uno::Exception& )
143 // some objects do not allow to set the size even in running state
144 m_bHasSizeToSet = true;
145 m_aSizeToSet = aSizeToSet;
146 m_nAspectToSet = nAspect;
148 aGuard.reset();
150 #endif
152 // cache the values
153 m_bHasCachedSize = true;
154 m_aCachedSize = aSize;
155 m_nCachedAspect = nAspect;
158 awt::Size OleEmbeddedObject::getVisualAreaSize_impl(sal_Int64 nAspect,
159 osl::ResettableMutexGuard& guard)
161 // begin wrapping related part ====================
162 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
163 if ( xWrappedObject.is() )
165 osl::ResettableMutexGuardScopedReleaser area(guard);
166 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
167 return xWrappedObject->getVisualAreaSize( nAspect );
169 // end wrapping related part ====================
171 if ( m_bDisposed )
172 throw lang::DisposedException(); // TODO
174 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
175 if ( nAspect == embed::Aspects::MSOLE_ICON )
176 // no representation can be retrieved
177 throw embed::WrongStateException( u"Illegal call!"_ustr,
178 static_cast< ::cppu::OWeakObject* >(this) );
180 if ( m_nObjectState == -1 )
181 throw embed::WrongStateException( u"The object is not loaded!"_ustr,
182 static_cast< ::cppu::OWeakObject* >(this) );
184 awt::Size aResult;
186 #ifdef _WIN32
187 // TODO/LATER: Support different aspects
188 if ( m_pOleComponent && !m_bHasSizeToSet && nAspect == embed::Aspects::MSOLE_CONTENT )
192 // the cached size updated every time the object is stored
193 if ( m_bHasCachedSize )
195 aResult = m_aCachedSize;
197 else
199 // there is no internal cache
200 awt::Size aSize;
202 { // => unguarded
203 osl::ResettableMutexGuardScopedReleaser area(guard);
205 bool bBackToLoaded = false;
207 bool bSuccess = 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
213 try {
214 changeState( embed::EmbedStates::RUNNING );
215 // the links should be switched back to loaded state to avoid too
216 // many open MathType instances
217 bBackToLoaded = true;
219 catch( const uno::Exception& )
221 throw embed::NoVisualAreaSizeException(
222 "No size available!",
223 static_cast< ::cppu::OWeakObject* >(this) );
229 // first try to get size using replacement image
230 aSize = m_pOleComponent->GetExtent( nAspect ); // will throw an exception in case of failure
231 bSuccess = true;
233 catch( const uno::Exception& )
235 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "OleEmbeddedObject::getVisualAreaSize: GetExtent() failed:");
238 if (bBackToLoaded)
242 changeState(embed::EmbedStates::LOADED);
244 catch( const uno::Exception& )
246 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "ignoring ");
250 if ( !bSuccess )
254 // second try the cached replacement image
255 aSize = m_pOleComponent->GetCachedExtent( nAspect ); // will throw an exception in case of failure
256 bSuccess = true;
258 catch( const uno::Exception& )
260 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "OleEmbeddedObject::getVisualAreaSize: GetCachedExtent() failed:");
264 if ( !bSuccess )
268 // third try the size reported by the object
269 aSize = m_pOleComponent->GetRecommendedExtent( nAspect ); // will throw an exception in case of failure
270 bSuccess = true;
272 catch( const uno::Exception& )
274 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "OleEmbeddedObject::getVisualAreaSize: GetRecommendedExtent() failed:");
278 if ( !bSuccess )
279 throw embed::NoVisualAreaSizeException(
280 "No size available!",
281 static_cast< ::cppu::OWeakObject* >(this) );
283 } // <= unguarded
285 m_aCachedSize = aSize;
286 m_nCachedAspect = nAspect;
287 m_bHasCachedSize = true;
289 aResult = m_aCachedSize;
292 catch ( const embed::NoVisualAreaSizeException& )
294 throw;
296 catch ( const uno::Exception& )
298 throw embed::NoVisualAreaSizeException(
299 "No size available!",
300 static_cast< ::cppu::OWeakObject* >(this) );
303 else
304 #endif
306 // return cached value
307 if ( !m_bHasCachedSize )
309 throw embed::NoVisualAreaSizeException(
310 u"No size available!"_ustr,
311 static_cast< ::cppu::OWeakObject* >(this) );
313 SAL_WARN_IF( nAspect != m_nCachedAspect, "embeddedobj.ole", "Unexpected aspect is requested!" );
314 aResult = m_aCachedSize;
317 return aResult;
320 awt::Size SAL_CALL OleEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect )
322 osl::ResettableMutexGuard aGuard(m_aMutex);
323 return getVisualAreaSize_impl(nAspect, aGuard);
326 embed::VisualRepresentation SAL_CALL OleEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect )
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::ResettableMutexGuard aGuard(m_aMutex);
338 if ( m_bDisposed )
339 throw lang::DisposedException(); // TODO
341 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
342 if ( nAspect == embed::Aspects::MSOLE_ICON )
343 // no representation can be retrieved
344 throw embed::WrongStateException( u"Illegal call!"_ustr,
345 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( u"The object is not loaded!"_ustr,
351 static_cast< ::cppu::OWeakObject* >(this) );
353 // TODO: in case of different aspects they must be applied to the mediatype and XTransferable must be used
354 // the cache is used only as a fallback if object is not in loaded state
355 if ( !m_xCachedVisualRepresentation.is() && ( !m_bVisReplInitialized || m_bVisReplInStream )
356 && m_nObjectState == embed::EmbedStates::LOADED )
358 m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream, aGuard, true );
359 SetVisReplInStream( m_xCachedVisualRepresentation.is() );
362 #ifdef _WIN32
363 if ( !m_xCachedVisualRepresentation.is() && m_pOleComponent )
367 datatransfer::DataFlavor aDataFlavor(
368 "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"",
369 "Windows Metafile",
370 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
372 embed::VisualRepresentation aVisualRepr;
374 osl::ResettableMutexGuardScopedReleaser clearedMutex(aGuard);
375 if ( m_nObjectState == embed::EmbedStates::LOADED )
376 changeState( embed::EmbedStates::RUNNING );
377 aVisualRepr.Data = m_pOleComponent->getTransferData(aDataFlavor);
379 aVisualRepr.Flavor = aDataFlavor;
381 uno::Sequence< sal_Int8 > aVisReplSeq;
382 aVisualRepr.Data >>= aVisReplSeq;
383 if ( aVisReplSeq.getLength() )
385 m_xCachedVisualRepresentation = GetNewFilledTempStream_Impl(
386 uno::Reference< io::XInputStream >(
387 new ::comphelper::SequenceInputStream(aVisReplSeq)));
390 return aVisualRepr;
392 catch( const uno::Exception& )
395 #endif
397 // the cache is used only as a fallback if object is not in loaded state
398 if ( !m_xCachedVisualRepresentation.is() && ( !m_bVisReplInitialized || m_bVisReplInStream ) )
400 m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream, aGuard );
401 SetVisReplInStream( m_xCachedVisualRepresentation.is() );
404 if ( !m_xCachedVisualRepresentation.is() )
406 // no representation can be retrieved
407 throw embed::WrongStateException( u"Illegal call!"_ustr,
408 static_cast< ::cppu::OWeakObject* >(this) );
411 return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation );
414 sal_Int32 SAL_CALL OleEmbeddedObject::getMapUnit( sal_Int64 nAspect )
416 // begin wrapping related part ====================
417 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
418 if ( xWrappedObject.is() )
420 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
421 return xWrappedObject->getMapUnit( nAspect );
423 // end wrapping related part ====================
425 ::osl::MutexGuard aGuard( m_aMutex );
426 if ( m_bDisposed )
427 throw lang::DisposedException(); // TODO
429 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
430 if ( nAspect == embed::Aspects::MSOLE_ICON )
431 // no representation can be retrieved
432 throw embed::WrongStateException( u"Illegal call!"_ustr,
433 static_cast< ::cppu::OWeakObject* >(this) );
435 if ( m_nObjectState == -1 )
436 throw embed::WrongStateException( u"The object is not loaded!"_ustr,
437 static_cast< ::cppu::OWeakObject* >(this) );
439 return embed::EmbedMapUnits::ONE_100TH_MM;
443 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */