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