xref: /AOO41X/main/sc/source/ui/vba/vbachartobject.cxx (revision 8809db7a87f97847b57a57f4cd2b0104b2b83182)
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 
23 #include "vbachart.hxx"
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/document/XEmbeddedObjectSupplier.hpp>
26 #include <com/sun/star/container/XNamed.hpp>
27 #include <com/sun/star/script/BasicErrorException.hpp>
28 #include <basic/sberrors.hxx>
29 #include "vbachartobject.hxx"
30 #include "vbachartobjects.hxx"
31 
32 using namespace ::com::sun::star;
33 using namespace ::ooo::vba;
34 
35 const rtl::OUString CHART_NAME( RTL_CONSTASCII_USTRINGPARAM("Name") );
36 const rtl::OUString PERSIST_NAME( RTL_CONSTASCII_USTRINGPARAM("PersistName") );
37 
38 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 )
39 {
40         xDrawPage = xDrawPageSupplier->getDrawPage();
41         xEmbeddedObjectSupplier.set( xTableChart, uno::UNO_QUERY_THROW );
42         xNamed.set( xTableChart, uno::UNO_QUERY_THROW );
43         sPersistName = getPersistName();
44         xShape = setShape();
45         setName(sPersistName);
46         oShapeHelper.reset(new ShapeHelper(xShape));
47 }
48 
49 rtl::OUString ScVbaChartObject::getPersistName()
50 {
51     if ( !sPersistName.getLength() )
52         sPersistName = xNamed->getName();
53     return sPersistName;
54 }
55 
56 uno::Reference< drawing::XShape >
57 ScVbaChartObject::setShape() throw ( script::BasicErrorException )
58 {
59     try
60     {
61         sal_Int32 nItems = xDrawPage->getCount();
62         for (int i = 0; i < nItems; i++)
63         {
64             xShape.set( xDrawPage->getByIndex(i), uno::UNO_QUERY_THROW );
65             if (xShape->getShapeType().compareToAscii("com.sun.star.drawing.OLE2Shape") == 0 )
66             {
67                 uno::Reference< beans::XPropertySet > xShapePropertySet(xShape, uno::UNO_QUERY_THROW );
68                 rtl::OUString sName;
69                 xShapePropertySet->getPropertyValue(PERSIST_NAME ) >>=sName;
70                 if ( sName.equals(sPersistName))
71                 {
72                     xNamedShape.set( xShape, uno::UNO_QUERY_THROW );
73                     return xShape;
74                 }
75             }
76         }
77     }
78     catch (uno::Exception& )
79     {
80         throw script::BasicErrorException( rtl::OUString(), uno::Reference< uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString() );
81     }
82     return NULL;
83 }
84 
85 void SAL_CALL
86 ScVbaChartObject::setName( const rtl::OUString& sName ) throw (css::uno::RuntimeException)
87 {
88     xNamedShape->setName(sName);
89 }
90 
91 
92 ::rtl::OUString SAL_CALL
93 ScVbaChartObject::getName() throw (css::uno::RuntimeException)
94 {
95     return xNamedShape->getName();
96 }
97 
98 void SAL_CALL
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
112 ScVbaChartObject::Activate() throw ( script::BasicErrorException )
113 {
114     try
115     {
116         // #TODO #FIXME should be ThisWorkbook or equivelant, 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
130 ScVbaChartObject::getChart() throw (css::uno::RuntimeException)
131 {
132     return new ScVbaChart( this, mxContext, xEmbeddedObjectSupplier->getEmbeddedObject(), xTableChart );
133 }
134 
135 rtl::OUString&
136 ScVbaChartObject::getServiceImplName()
137 {
138     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaChartObject") );
139     return sImplName;
140 }
141 
142 uno::Sequence< rtl::OUString >
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
155 ScVbaChartObject::getHeight()
156 {
157     return oShapeHelper->getHeight();
158 }
159 
160 void
161 ScVbaChartObject::setHeight(double _fheight) throw ( script::BasicErrorException )
162 {
163     oShapeHelper->setHeight(_fheight);
164 }
165 
166 double
167 ScVbaChartObject::getWidth()
168 {
169         return oShapeHelper->getWidth();
170 }
171 
172 void
173 ScVbaChartObject::setWidth(double _fWidth) throw ( script::BasicErrorException )
174 {
175     oShapeHelper->setWidth(_fWidth);
176 }
177 
178 double
179 ScVbaChartObject::getLeft()
180 {
181         return oShapeHelper->getLeft();
182 }
183 
184 void
185 ScVbaChartObject::setLeft(double _fLeft)
186 {
187     oShapeHelper->setLeft(_fLeft);
188 }
189 
190 double
191 ScVbaChartObject::getTop()
192 {
193         return oShapeHelper->getTop();
194 }
195 
196 void
197 ScVbaChartObject::setTop(double _fTop)
198 {
199     oShapeHelper->setTop(_fTop);
200 }
201 
202 uno::Reference< uno::XInterface >
203 ScVbaChartObject::getUnoObject() throw (script::BasicErrorException)
204 {
205     return uno::Reference< uno::XInterface >( xShape, uno::UNO_QUERY );
206 }
207