xref: /aoo41x/main/sc/source/ui/vba/vbachartobject.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #include "vbachart.hxx"
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/document/XEmbeddedObjectSupplier.hpp>
30 #include <com/sun/star/container/XNamed.hpp>
31 #include <com/sun/star/script/BasicErrorException.hpp>
32 #include <basic/sberrors.hxx>
33 #include "vbachartobject.hxx"
34 #include "vbachartobjects.hxx"
35 
36 using namespace ::com::sun::star;
37 using namespace ::ooo::vba;
38 
39 const rtl::OUString CHART_NAME( RTL_CONSTASCII_USTRINGPARAM("Name") );
40 const rtl::OUString PERSIST_NAME( RTL_CONSTASCII_USTRINGPARAM("PersistName") );
41 
42 ScVbaChartObject::ScVbaChartObject( const css::uno::Reference< ov::XHelperInterface >& _xParent, const css::uno::Reference< css::uno::XComponentContext >& _xContext, const css::uno::Reference< css::table::XTableChart >& _xTableChart, const css::uno::Reference< css::drawing::XDrawPageSupplier >& _xDrawPageSupplier ) : ChartObjectImpl_BASE( _xParent, _xContext ), xTableChart( _xTableChart ), xDrawPageSupplier( _xDrawPageSupplier )
43 {
44         xDrawPage = xDrawPageSupplier->getDrawPage();
45         xEmbeddedObjectSupplier.set( xTableChart, uno::UNO_QUERY_THROW );
46         xNamed.set( xTableChart, uno::UNO_QUERY_THROW );
47         sPersistName = getPersistName();
48         xShape = setShape();
49         setName(sPersistName);
50         oShapeHelper.reset(new ShapeHelper(xShape));
51 }
52 
53 rtl::OUString ScVbaChartObject::getPersistName()
54 {
55 	if ( !sPersistName.getLength() )
56 		sPersistName = xNamed->getName();
57 	return sPersistName;
58 }
59 
60 uno::Reference< drawing::XShape >
61 ScVbaChartObject::setShape() throw ( script::BasicErrorException )
62 {
63 	try
64 	{
65 		sal_Int32 nItems = xDrawPage->getCount();
66 		for (int i = 0; i < nItems; i++)
67 		{
68 			xShape.set( xDrawPage->getByIndex(i), uno::UNO_QUERY_THROW );
69 			if (xShape->getShapeType().compareToAscii("com.sun.star.drawing.OLE2Shape") == 0 )
70 			{
71 				uno::Reference< beans::XPropertySet > xShapePropertySet(xShape, uno::UNO_QUERY_THROW );
72 				rtl::OUString sName;
73 				xShapePropertySet->getPropertyValue(PERSIST_NAME ) >>=sName;
74 				if ( sName.equals(sPersistName))
75 				{
76 					xNamedShape.set( xShape, uno::UNO_QUERY_THROW );
77 					return xShape;
78 				}
79 			}
80 		}
81 	}
82 	catch (uno::Exception& )
83 	{
84 		throw script::BasicErrorException( rtl::OUString(), uno::Reference< uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString() );
85 	}
86 	return NULL;
87 }
88 
89 void SAL_CALL
90 ScVbaChartObject::setName( const rtl::OUString& sName ) throw (css::uno::RuntimeException)
91 {
92 	xNamedShape->setName(sName);
93 }
94 
95 
96 ::rtl::OUString SAL_CALL
97 ScVbaChartObject::getName() throw (css::uno::RuntimeException)
98 {
99 	return xNamedShape->getName();
100 }
101 
102 void SAL_CALL
103 ScVbaChartObject::Delete() throw ( css::script::BasicErrorException )
104 {
105 	// parent of this object is sheet
106 	uno::Reference< excel::XWorksheet > xParent( getParent(), uno::UNO_QUERY_THROW );
107 	uno::Reference< excel::XChartObjects > xColl( xParent->ChartObjects( uno::Any() ), uno::UNO_QUERY_THROW );
108 	ScVbaChartObjects* pChartObjectsImpl = static_cast< ScVbaChartObjects* >( xColl.get() );
109 	if (pChartObjectsImpl)
110 		pChartObjectsImpl->removeByName( getPersistName() );
111 	else
112 		throw script::BasicErrorException( rtl::OUString(), uno::Reference< uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent is not ChartObjects" ) ) );
113 }
114 
115 void
116 ScVbaChartObject::Activate() throw ( script::BasicErrorException )
117 {
118 	try
119 	{
120 		// #TODO #FIXME should be ThisWorkbook or equivelant, or in
121 		// fact probably the chart object should be created with
122 		// the XModel owner
123 		//uno::Reference< view::XSelectionSupplier > xSelectionSupplier( getXModel().getCurrentController());
124 		uno::Reference< view::XSelectionSupplier > xSelectionSupplier( getCurrentExcelDoc(mxContext)->getCurrentController(), uno::UNO_QUERY_THROW );
125 		xSelectionSupplier->select(uno::makeAny(xShape));
126 	}
127 	catch (uno::Exception& )
128 	{
129 		throw script::BasicErrorException( rtl::OUString(), uno::Reference< uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ChartObject Activate internal error" ) ) );
130 	}
131 }
132 
133 uno::Reference< excel::XChart > SAL_CALL
134 ScVbaChartObject::getChart() throw (css::uno::RuntimeException)
135 {
136 	return new ScVbaChart( this, mxContext, xEmbeddedObjectSupplier->getEmbeddedObject(), xTableChart );
137 }
138 
139 rtl::OUString&
140 ScVbaChartObject::getServiceImplName()
141 {
142 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaChartObject") );
143 	return sImplName;
144 }
145 
146 uno::Sequence< rtl::OUString >
147 ScVbaChartObject::getServiceNames()
148 {
149 	static uno::Sequence< rtl::OUString > aServiceNames;
150 	if ( aServiceNames.getLength() == 0 )
151 	{
152 		aServiceNames.realloc( 1 );
153 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.ChartObject" ) );
154 	}
155 	return aServiceNames;
156 }
157 
158 double
159 ScVbaChartObject::getHeight()
160 {
161 	return oShapeHelper->getHeight();
162 }
163 
164 void
165 ScVbaChartObject::setHeight(double _fheight) throw ( script::BasicErrorException )
166 {
167 	oShapeHelper->setHeight(_fheight);
168 }
169 
170 double
171 ScVbaChartObject::getWidth()
172 {
173         return oShapeHelper->getWidth();
174 }
175 
176 void
177 ScVbaChartObject::setWidth(double _fWidth) throw ( script::BasicErrorException )
178 {
179 	oShapeHelper->setWidth(_fWidth);
180 }
181 
182 double
183 ScVbaChartObject::getLeft()
184 {
185         return oShapeHelper->getLeft();
186 }
187 
188 void
189 ScVbaChartObject::setLeft(double _fLeft)
190 {
191 	oShapeHelper->setLeft(_fLeft);
192 }
193 
194 double
195 ScVbaChartObject::getTop()
196 {
197         return oShapeHelper->getTop();
198 }
199 
200 void
201 ScVbaChartObject::setTop(double _fTop)
202 {
203 	oShapeHelper->setTop(_fTop);
204 }
205 
206 uno::Reference< uno::XInterface >
207 ScVbaChartObject::getUnoObject() throw (script::BasicErrorException)
208 {
209 	return uno::Reference< uno::XInterface >( xShape, uno::UNO_QUERY );
210 }
211