xref: /aoo41x/main/sc/source/ui/vba/vbachartobject.cxx (revision 7d1ce60b)
1b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b3f79822SAndrew Rist  * distributed with this work for additional information
6b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10b3f79822SAndrew Rist  *
11b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12b3f79822SAndrew Rist  *
13b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17b3f79822SAndrew Rist  * specific language governing permissions and limitations
18b3f79822SAndrew Rist  * under the License.
19b3f79822SAndrew Rist  *
20b3f79822SAndrew Rist  *************************************************************/
21b3f79822SAndrew Rist 
22cdf0e10cSrcweir #include "vbachart.hxx"
23cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
24cdf0e10cSrcweir #include <com/sun/star/document/XEmbeddedObjectSupplier.hpp>
25cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp>
26cdf0e10cSrcweir #include <com/sun/star/script/BasicErrorException.hpp>
27cdf0e10cSrcweir #include <basic/sberrors.hxx>
28cdf0e10cSrcweir #include "vbachartobject.hxx"
29cdf0e10cSrcweir #include "vbachartobjects.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace ::com::sun::star;
32cdf0e10cSrcweir using namespace ::ooo::vba;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir const rtl::OUString CHART_NAME( RTL_CONSTASCII_USTRINGPARAM("Name") );
35cdf0e10cSrcweir const rtl::OUString PERSIST_NAME( RTL_CONSTASCII_USTRINGPARAM("PersistName") );
36cdf0e10cSrcweir 
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)37cdf0e10cSrcweir 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 )
38cdf0e10cSrcweir {
39*7d1ce60bSWang Lei     xDrawPage = xDrawPageSupplier->getDrawPage();
40*7d1ce60bSWang Lei     xEmbeddedObjectSupplier.set( xTableChart, uno::UNO_QUERY_THROW );
41*7d1ce60bSWang Lei     xNamed.set( xTableChart, uno::UNO_QUERY_THROW );
42*7d1ce60bSWang Lei     sPersistName = getPersistName();
43*7d1ce60bSWang Lei     xShape = setShape();
44*7d1ce60bSWang Lei // #i121178#: don't set the persist name to the object but the OLE object's name(displaying name)
45*7d1ce60bSWang Lei //    setName(sPersistName);
46*7d1ce60bSWang Lei     setName(xNamed->getDisplayName());
47*7d1ce60bSWang Lei     oShapeHelper.reset(new ShapeHelper(xShape));
48cdf0e10cSrcweir }
49cdf0e10cSrcweir 
getPersistName()50cdf0e10cSrcweir rtl::OUString ScVbaChartObject::getPersistName()
51cdf0e10cSrcweir {
52cdf0e10cSrcweir 	if ( !sPersistName.getLength() )
53cdf0e10cSrcweir 		sPersistName = xNamed->getName();
54cdf0e10cSrcweir 	return sPersistName;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
57cdf0e10cSrcweir uno::Reference< drawing::XShape >
setShape()58cdf0e10cSrcweir ScVbaChartObject::setShape() throw ( script::BasicErrorException )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir 	try
61cdf0e10cSrcweir 	{
62cdf0e10cSrcweir 		sal_Int32 nItems = xDrawPage->getCount();
63cdf0e10cSrcweir 		for (int i = 0; i < nItems; i++)
64cdf0e10cSrcweir 		{
65cdf0e10cSrcweir 			xShape.set( xDrawPage->getByIndex(i), uno::UNO_QUERY_THROW );
66cdf0e10cSrcweir 			if (xShape->getShapeType().compareToAscii("com.sun.star.drawing.OLE2Shape") == 0 )
67cdf0e10cSrcweir 			{
68cdf0e10cSrcweir 				uno::Reference< beans::XPropertySet > xShapePropertySet(xShape, uno::UNO_QUERY_THROW );
69cdf0e10cSrcweir 				rtl::OUString sName;
70cdf0e10cSrcweir 				xShapePropertySet->getPropertyValue(PERSIST_NAME ) >>=sName;
71cdf0e10cSrcweir 				if ( sName.equals(sPersistName))
72cdf0e10cSrcweir 				{
73cdf0e10cSrcweir 					xNamedShape.set( xShape, uno::UNO_QUERY_THROW );
74cdf0e10cSrcweir 					return xShape;
75cdf0e10cSrcweir 				}
76cdf0e10cSrcweir 			}
77cdf0e10cSrcweir 		}
78cdf0e10cSrcweir 	}
79cdf0e10cSrcweir 	catch (uno::Exception& )
80cdf0e10cSrcweir 	{
81cdf0e10cSrcweir 		throw script::BasicErrorException( rtl::OUString(), uno::Reference< uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString() );
82cdf0e10cSrcweir 	}
83cdf0e10cSrcweir 	return NULL;
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir void SAL_CALL
setName(const rtl::OUString & sName)87cdf0e10cSrcweir ScVbaChartObject::setName( const rtl::OUString& sName ) throw (css::uno::RuntimeException)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir 	xNamedShape->setName(sName);
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir ::rtl::OUString SAL_CALL
getName()93cdf0e10cSrcweir ScVbaChartObject::getName() throw (css::uno::RuntimeException)
94cdf0e10cSrcweir {
95cdf0e10cSrcweir 	return xNamedShape->getName();
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir void SAL_CALL
Delete()99cdf0e10cSrcweir ScVbaChartObject::Delete() throw ( css::script::BasicErrorException )
100cdf0e10cSrcweir {
101cdf0e10cSrcweir 	// parent of this object is sheet
102cdf0e10cSrcweir 	uno::Reference< excel::XWorksheet > xParent( getParent(), uno::UNO_QUERY_THROW );
103cdf0e10cSrcweir 	uno::Reference< excel::XChartObjects > xColl( xParent->ChartObjects( uno::Any() ), uno::UNO_QUERY_THROW );
104cdf0e10cSrcweir 	ScVbaChartObjects* pChartObjectsImpl = static_cast< ScVbaChartObjects* >( xColl.get() );
105cdf0e10cSrcweir 	if (pChartObjectsImpl)
106cdf0e10cSrcweir 		pChartObjectsImpl->removeByName( getPersistName() );
107cdf0e10cSrcweir 	else
108cdf0e10cSrcweir 		throw script::BasicErrorException( rtl::OUString(), uno::Reference< uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent is not ChartObjects" ) ) );
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir void
Activate()112cdf0e10cSrcweir ScVbaChartObject::Activate() throw ( script::BasicErrorException )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir 	try
115cdf0e10cSrcweir 	{
116cdf0e10cSrcweir 		// #TODO #FIXME should be ThisWorkbook or equivelant, or in
117cdf0e10cSrcweir 		// fact probably the chart object should be created with
118cdf0e10cSrcweir 		// the XModel owner
119cdf0e10cSrcweir 		//uno::Reference< view::XSelectionSupplier > xSelectionSupplier( getXModel().getCurrentController());
120cdf0e10cSrcweir 		uno::Reference< view::XSelectionSupplier > xSelectionSupplier( getCurrentExcelDoc(mxContext)->getCurrentController(), uno::UNO_QUERY_THROW );
121cdf0e10cSrcweir 		xSelectionSupplier->select(uno::makeAny(xShape));
122cdf0e10cSrcweir 	}
123cdf0e10cSrcweir 	catch (uno::Exception& )
124cdf0e10cSrcweir 	{
125cdf0e10cSrcweir 		throw script::BasicErrorException( rtl::OUString(), uno::Reference< uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ChartObject Activate internal error" ) ) );
126cdf0e10cSrcweir 	}
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir uno::Reference< excel::XChart > SAL_CALL
getChart()130cdf0e10cSrcweir ScVbaChartObject::getChart() throw (css::uno::RuntimeException)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir 	return new ScVbaChart( this, mxContext, xEmbeddedObjectSupplier->getEmbeddedObject(), xTableChart );
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir rtl::OUString&
getServiceImplName()136cdf0e10cSrcweir ScVbaChartObject::getServiceImplName()
137cdf0e10cSrcweir {
138cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaChartObject") );
139cdf0e10cSrcweir 	return sImplName;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir uno::Sequence< rtl::OUString >
getServiceNames()143cdf0e10cSrcweir ScVbaChartObject::getServiceNames()
144cdf0e10cSrcweir {
145cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
146cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
147cdf0e10cSrcweir 	{
148cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
149cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.ChartObject" ) );
150cdf0e10cSrcweir 	}
151cdf0e10cSrcweir 	return aServiceNames;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir double
getHeight()155cdf0e10cSrcweir ScVbaChartObject::getHeight()
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	return oShapeHelper->getHeight();
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir void
setHeight(double _fheight)161cdf0e10cSrcweir ScVbaChartObject::setHeight(double _fheight) throw ( script::BasicErrorException )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir 	oShapeHelper->setHeight(_fheight);
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir double
getWidth()167cdf0e10cSrcweir ScVbaChartObject::getWidth()
168cdf0e10cSrcweir {
169cdf0e10cSrcweir         return oShapeHelper->getWidth();
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir void
setWidth(double _fWidth)173cdf0e10cSrcweir ScVbaChartObject::setWidth(double _fWidth) throw ( script::BasicErrorException )
174cdf0e10cSrcweir {
175cdf0e10cSrcweir 	oShapeHelper->setWidth(_fWidth);
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir double
getLeft()179cdf0e10cSrcweir ScVbaChartObject::getLeft()
180cdf0e10cSrcweir {
181cdf0e10cSrcweir         return oShapeHelper->getLeft();
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir void
setLeft(double _fLeft)185cdf0e10cSrcweir ScVbaChartObject::setLeft(double _fLeft)
186cdf0e10cSrcweir {
187cdf0e10cSrcweir 	oShapeHelper->setLeft(_fLeft);
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir double
getTop()191cdf0e10cSrcweir ScVbaChartObject::getTop()
192cdf0e10cSrcweir {
193cdf0e10cSrcweir         return oShapeHelper->getTop();
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir void
setTop(double _fTop)197cdf0e10cSrcweir ScVbaChartObject::setTop(double _fTop)
198cdf0e10cSrcweir {
199cdf0e10cSrcweir 	oShapeHelper->setTop(_fTop);
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir uno::Reference< uno::XInterface >
getUnoObject()203cdf0e10cSrcweir ScVbaChartObject::getUnoObject() throw (script::BasicErrorException)
204cdf0e10cSrcweir {
205cdf0e10cSrcweir 	return uno::Reference< uno::XInterface >( xShape, uno::UNO_QUERY );
206cdf0e10cSrcweir }
207