Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / embeddedobj / source / commonembedding / visobj.cxx
blob6306ecfad0cff5dc0e05f81e4ff305669982d79f
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/embed/Aspects.hpp>
21 #include <com/sun/star/embed/EmbedStates.hpp>
22 #include <com/sun/star/embed/WrongStateException.hpp>
23 #include <com/sun/star/datatransfer/XTransferable.hpp>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <com/sun/star/lang/DisposedException.hpp>
28 #include <commonembobj.hxx>
29 #include <sal/log.hxx>
32 using namespace ::com::sun::star;
34 void SAL_CALL OCommonEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect, const awt::Size& aSize )
36 ::osl::MutexGuard aGuard( m_aMutex );
37 if ( m_bDisposed )
38 throw lang::DisposedException(); // TODO
40 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.common", "For iconified objects no graphical replacement is required!" );
41 if ( nAspect == embed::Aspects::MSOLE_ICON )
42 // no representation can be retrieved
43 throw embed::WrongStateException( "Illegal call!",
44 static_cast< ::cppu::OWeakObject* >(this) );
46 if ( m_nObjectState == -1 )
47 throw embed::WrongStateException( "The own object has no persistence!",
48 static_cast< ::cppu::OWeakObject* >(this) );
50 m_bHasClonedSize = false;
52 bool bBackToLoaded = false;
53 if ( m_nObjectState == embed::EmbedStates::LOADED )
55 changeState( embed::EmbedStates::RUNNING );
57 // the links should be switched back to loaded state for now to avoid locking problems
58 bBackToLoaded = m_bIsLink;
61 bool bSuccess = m_xDocHolder->SetExtent( nAspect, aSize );
63 if ( bBackToLoaded )
64 changeState( embed::EmbedStates::LOADED );
66 if ( !bSuccess )
67 throw uno::Exception("SetExtent failed", nullptr); // TODO:
70 awt::Size SAL_CALL OCommonEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect )
72 ::osl::MutexGuard aGuard( m_aMutex );
73 if ( m_bDisposed )
74 throw lang::DisposedException(); // TODO
76 if ( m_nObjectState == -1 )
77 throw embed::WrongStateException( "The own object has no persistence!",
78 static_cast< ::cppu::OWeakObject* >(this) );
80 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.common", "For iconified objects no graphical replacement is required!" );
82 if ( m_bHasClonedSize )
83 return m_aClonedSize;
85 bool bBackToLoaded = false;
86 if ( m_nObjectState == embed::EmbedStates::LOADED )
88 changeState( embed::EmbedStates::RUNNING );
90 // the links should be switched back to loaded state for now to avoid locking problems
91 bBackToLoaded = m_bIsLink;
94 awt::Size aResult;
95 bool bSuccess = m_xDocHolder->GetExtent( nAspect, &aResult );
97 if ( bBackToLoaded )
98 changeState( embed::EmbedStates::LOADED );
100 if ( !bSuccess )
101 throw uno::Exception("GetExtent failed", nullptr); // TODO:
103 return aResult;
106 sal_Int32 SAL_CALL OCommonEmbeddedObject::getMapUnit( sal_Int64 nAspect )
108 ::osl::MutexGuard aGuard( m_aMutex );
109 if ( m_bDisposed )
110 throw lang::DisposedException(); // TODO
112 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.common", "For iconified objects no graphical replacement is required!" );
113 if ( nAspect == embed::Aspects::MSOLE_ICON )
114 // no representation can be retrieved
115 throw embed::WrongStateException( "Illegal call!",
116 static_cast< ::cppu::OWeakObject* >(this) );
118 if ( m_nObjectState == -1 )
119 throw embed::WrongStateException( "The own object has no persistence!",
120 static_cast< ::cppu::OWeakObject* >(this) );
122 if ( m_bHasClonedSize )
123 return m_nClonedMapUnit;
125 bool bBackToLoaded = false;
126 if ( m_nObjectState == embed::EmbedStates::LOADED )
128 changeState( embed::EmbedStates::RUNNING );
130 // the links should be switched back to loaded state for now to avoid locking problems
131 bBackToLoaded = m_bIsLink;
134 sal_Int32 nResult = m_xDocHolder->GetMapUnit( nAspect );
136 if ( bBackToLoaded )
137 changeState( embed::EmbedStates::LOADED );
139 if ( nResult < 0 )
140 throw uno::Exception("result " + OUString::number(nResult), nullptr); // TODO:
142 return nResult;
145 embed::VisualRepresentation SAL_CALL OCommonEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect )
147 ::osl::MutexGuard aGuard( m_aMutex );
148 if ( m_bDisposed )
149 throw lang::DisposedException(); // TODO
151 if ( m_nObjectState == -1 )
152 throw embed::WrongStateException( "The own object has no persistence!",
153 static_cast< ::cppu::OWeakObject* >(this) );
156 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.common", "For iconified objects no graphical replacement is required!" );
157 if ( nAspect == embed::Aspects::MSOLE_ICON )
158 // no representation can be retrieved
159 throw embed::WrongStateException( "Illegal call!",
160 static_cast< ::cppu::OWeakObject* >(this) );
162 bool bBackToLoaded = false;
163 if ( m_nObjectState == embed::EmbedStates::LOADED )
165 // restore original VisualAreaSize, because writer objects set
166 // themselves to a default size OLESIZE
167 awt::Size aOrigSize = getVisualAreaSize(nAspect);
168 changeState(embed::EmbedStates::RUNNING);
169 const bool bIsChart = GetDocumentServiceName() == "com.sun.star.chart2.ChartDocument";
170 // tdf#108643 unless it's a chart, cause those are weird (#i103460#)
171 if (!bIsChart && aOrigSize != getVisualAreaSize(nAspect))
172 setVisualAreaSize(nAspect, aOrigSize);
174 // the links should be switched back to loaded state for now to avoid locking problems
175 bBackToLoaded = m_bIsLink;
178 SAL_WARN_IF( !m_xDocHolder->GetComponent().is(), "embeddedobj.common", "Running or Active object has no component!" );
180 // TODO: return for the aspect of the document
181 embed::VisualRepresentation aVisualRepresentation;
183 uno::Reference< embed::XVisualObject > xVisualObject( m_xDocHolder->GetComponent(), uno::UNO_QUERY );
184 if( xVisualObject.is())
186 aVisualRepresentation = xVisualObject->getPreferredVisualRepresentation( nAspect );
188 else
190 uno::Reference< datatransfer::XTransferable > xTransferable( m_xDocHolder->GetComponent(), uno::UNO_QUERY_THROW );
192 datatransfer::DataFlavor aDataFlavor(
193 "application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\"",
194 "GDIMetaFile",
195 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
197 if( !xTransferable->isDataFlavorSupported( aDataFlavor ))
198 throw uno::RuntimeException();
199 aVisualRepresentation.Data = xTransferable->getTransferData( aDataFlavor );
200 aVisualRepresentation.Flavor = aDataFlavor;
203 if ( bBackToLoaded )
204 changeState( embed::EmbedStates::LOADED );
206 return aVisualRepresentation;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */