1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_embeddedobj.hxx"
30*cdf0e10cSrcweir #include <com/sun/star/embed/Aspects.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/embed/EmbedStates.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/datatransfer/XTransferable.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
34*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <rtl/logfile.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <commonembobj.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using namespace ::com::sun::star;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir void SAL_CALL OCommonEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect, const awt::Size& aSize )
45*cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
46*cdf0e10cSrcweir 				embed::WrongStateException,
47*cdf0e10cSrcweir 				uno::Exception,
48*cdf0e10cSrcweir 				uno::RuntimeException )
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OCommonEmbeddedObject::setVisualAreaSize" );
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
53*cdf0e10cSrcweir 	if ( m_bDisposed )
54*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 	OSL_ENSURE( nAspect != embed::Aspects::MSOLE_ICON, "For iconified objects no graphical replacement is required!\n" );
57*cdf0e10cSrcweir 	if ( nAspect == embed::Aspects::MSOLE_ICON )
58*cdf0e10cSrcweir 		// no representation can be retrieved
59*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Illegal call!\n" ),
60*cdf0e10cSrcweir 									uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
63*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The own object has no persistence!\n" ),
64*cdf0e10cSrcweir 									uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     m_bHasClonedSize = sal_False;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     sal_Bool bBackToLoaded = sal_False;
69*cdf0e10cSrcweir 	if ( m_nObjectState == embed::EmbedStates::LOADED )
70*cdf0e10cSrcweir     {
71*cdf0e10cSrcweir 		changeState( embed::EmbedStates::RUNNING );
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir         // the links should be switched back to loaded state for now to avoid locking problems
74*cdf0e10cSrcweir         bBackToLoaded = m_bIsLink;
75*cdf0e10cSrcweir     }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	sal_Bool bSuccess = m_pDocHolder->SetExtent( nAspect, aSize );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     if ( bBackToLoaded )
80*cdf0e10cSrcweir 		changeState( embed::EmbedStates::LOADED );
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     if ( !bSuccess )
83*cdf0e10cSrcweir 		throw uno::Exception(); // TODO:
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir awt::Size SAL_CALL OCommonEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect )
87*cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
88*cdf0e10cSrcweir 				embed::WrongStateException,
89*cdf0e10cSrcweir 				uno::Exception,
90*cdf0e10cSrcweir 				uno::RuntimeException )
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OCommonEmbeddedObject::getVisualAreaSize" );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
95*cdf0e10cSrcweir 	if ( m_bDisposed )
96*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
99*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The own object has no persistence!\n" ),
100*cdf0e10cSrcweir 									uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 	OSL_ENSURE( nAspect != embed::Aspects::MSOLE_ICON, "For iconified objects no graphical replacement is required!\n" );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     if ( m_bHasClonedSize )
105*cdf0e10cSrcweir         return m_aClonedSize;
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     sal_Bool bBackToLoaded = sal_False;
108*cdf0e10cSrcweir 	if ( m_nObjectState == embed::EmbedStates::LOADED )
109*cdf0e10cSrcweir     {
110*cdf0e10cSrcweir 		changeState( embed::EmbedStates::RUNNING );
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         // the links should be switched back to loaded state for now to avoid locking problems
113*cdf0e10cSrcweir         bBackToLoaded = m_bIsLink;
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 	awt::Size aResult;
117*cdf0e10cSrcweir 	sal_Bool bSuccess = m_pDocHolder->GetExtent( nAspect, &aResult );
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     if ( bBackToLoaded )
120*cdf0e10cSrcweir 		changeState( embed::EmbedStates::LOADED );
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     if ( !bSuccess )
123*cdf0e10cSrcweir 		throw uno::Exception(); // TODO:
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 	return aResult;
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir sal_Int32 SAL_CALL OCommonEmbeddedObject::getMapUnit( sal_Int64 nAspect )
129*cdf0e10cSrcweir 		throw ( uno::Exception,
130*cdf0e10cSrcweir 				uno::RuntimeException)
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
133*cdf0e10cSrcweir 	if ( m_bDisposed )
134*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	OSL_ENSURE( nAspect != embed::Aspects::MSOLE_ICON, "For iconified objects no graphical replacement is required!\n" );
137*cdf0e10cSrcweir 	if ( nAspect == embed::Aspects::MSOLE_ICON )
138*cdf0e10cSrcweir 		// no representation can be retrieved
139*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Illegal call!\n" ),
140*cdf0e10cSrcweir 									uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
143*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The own object has no persistence!\n" ),
144*cdf0e10cSrcweir 									uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     if ( m_bHasClonedSize )
147*cdf0e10cSrcweir         return m_nClonedMapUnit;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     sal_Bool bBackToLoaded = sal_False;
150*cdf0e10cSrcweir 	if ( m_nObjectState == embed::EmbedStates::LOADED )
151*cdf0e10cSrcweir     {
152*cdf0e10cSrcweir 		changeState( embed::EmbedStates::RUNNING );
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir         // the links should be switched back to loaded state for now to avoid locking problems
155*cdf0e10cSrcweir         bBackToLoaded = m_bIsLink;
156*cdf0e10cSrcweir     }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir     sal_Int32 nResult = m_pDocHolder->GetMapUnit( nAspect );
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     if ( bBackToLoaded )
161*cdf0e10cSrcweir 		changeState( embed::EmbedStates::LOADED );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     if ( nResult < 0  )
164*cdf0e10cSrcweir 		throw uno::Exception(); // TODO:
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	return nResult;
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir embed::VisualRepresentation SAL_CALL OCommonEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect )
170*cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
171*cdf0e10cSrcweir 				embed::WrongStateException,
172*cdf0e10cSrcweir 				uno::Exception,
173*cdf0e10cSrcweir 				uno::RuntimeException )
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OCommonEmbeddedObject::getPrefferedVisualRepresentation" );
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
178*cdf0e10cSrcweir 	if ( m_bDisposed )
179*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
182*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The own object has no persistence!\n" ),
183*cdf0e10cSrcweir 									uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 	OSL_ENSURE( nAspect != embed::Aspects::MSOLE_ICON, "For iconified objects no graphical replacement is required!\n" );
187*cdf0e10cSrcweir 	if ( nAspect == embed::Aspects::MSOLE_ICON )
188*cdf0e10cSrcweir 		// no representation can be retrieved
189*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Illegal call!\n" ),
190*cdf0e10cSrcweir 									uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     sal_Bool bBackToLoaded = sal_False;
193*cdf0e10cSrcweir 	if ( m_nObjectState == embed::EmbedStates::LOADED )
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir 		changeState( embed::EmbedStates::RUNNING );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir         // the links should be switched back to loaded state for now to avoid locking problems
198*cdf0e10cSrcweir         bBackToLoaded = m_bIsLink;
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     OSL_ENSURE( m_pDocHolder->GetComponent().is(), "Running or Active object has no component!\n" );
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 	// TODO: return for the aspect of the document
204*cdf0e10cSrcweir 	embed::VisualRepresentation aVisualRepresentation;
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     uno::Reference< embed::XVisualObject > xVisualObject( m_pDocHolder->GetComponent(), uno::UNO_QUERY );
207*cdf0e10cSrcweir     if( xVisualObject.is())
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir         aVisualRepresentation = xVisualObject->getPreferredVisualRepresentation( nAspect );
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir     else
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir         uno::Reference< datatransfer::XTransferable > xTransferable( m_pDocHolder->GetComponent(), uno::UNO_QUERY );
214*cdf0e10cSrcweir         if (!xTransferable.is() )
215*cdf0e10cSrcweir             throw uno::RuntimeException();
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 	    datatransfer::DataFlavor aDataFlavor(
218*cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii( "application/x-openoffice-gdimetafile;windows_formatname=\"GDIMetaFile\"" ),
219*cdf0e10cSrcweir 			    ::rtl::OUString::createFromAscii( "GDIMetaFile" ),
220*cdf0e10cSrcweir 			    ::getCppuType( (const uno::Sequence< sal_Int8 >*) NULL ) );
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir         if( xTransferable->isDataFlavorSupported( aDataFlavor ))
223*cdf0e10cSrcweir         {
224*cdf0e10cSrcweir             aVisualRepresentation.Data = xTransferable->getTransferData( aDataFlavor );
225*cdf0e10cSrcweir             aVisualRepresentation.Flavor = aDataFlavor;
226*cdf0e10cSrcweir         }
227*cdf0e10cSrcweir         else
228*cdf0e10cSrcweir             throw uno::RuntimeException();
229*cdf0e10cSrcweir     }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir     if ( bBackToLoaded )
232*cdf0e10cSrcweir 		changeState( embed::EmbedStates::LOADED );
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 	return aVisualRepresentation;
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237