xref: /aoo41x/main/sc/source/ui/vba/vbachartobjects.cxx (revision b3f79822)
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/table/XTableChartsSupplier.hpp>
27 #include <com/sun/star/table/XTableChart.hpp>
28 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
29 #include <ooo/vba/excel/XlChartType.hpp>
30 
31 
32 #include "vbachartobjects.hxx"
33 #include "vbachartobject.hxx"
34 #include "vbaglobals.hxx"
35 #include "cellsuno.hxx"
36 #include <vector>
37 #include <basic/sberrors.hxx>
38 
39 using namespace ::com::sun::star;
40 using namespace ::ooo::vba;
41 
42 
43 class ChartObjectEnumerationImpl : public EnumerationHelperImpl
44 {
45 	uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier;
46 
47 public:
48 
ChartObjectEnumerationImpl(const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<container::XEnumeration> & xEnumeration,const uno::Reference<drawing::XDrawPageSupplier> & _xDrawPageSupplier,const uno::Reference<XHelperInterface> & _xParent)49     ChartObjectEnumerationImpl( const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Reference< drawing::XDrawPageSupplier >& _xDrawPageSupplier, const uno::Reference< XHelperInterface >& _xParent ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( _xParent, xContext, xEnumeration ), xDrawPageSupplier( _xDrawPageSupplier ) {}
nextElement()50 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
51 	{
52 		uno::Reference< table::XTableChart > xTableChart( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
53 		// parent Object is sheet
54         return uno::makeAny(  uno::Reference< excel::XChartObject > ( new ScVbaChartObject(  m_xParent, m_xContext, xTableChart, xDrawPageSupplier ) ) );
55 	}
56 };
57 
58 
ScVbaChartObjects(const css::uno::Reference<ov::XHelperInterface> & _xParent,const css::uno::Reference<css::uno::XComponentContext> & _xContext,const css::uno::Reference<css::table::XTableCharts> & _xTableCharts,const uno::Reference<drawing::XDrawPageSupplier> & _xDrawPageSupplier)59 ScVbaChartObjects::ScVbaChartObjects( const css::uno::Reference< ov::XHelperInterface >& _xParent, const css::uno::Reference< css::uno::XComponentContext >& _xContext, const css::uno::Reference< css::table::XTableCharts >& _xTableCharts, const uno::Reference< drawing::XDrawPageSupplier >& _xDrawPageSupplier ) : ChartObjects_BASE(_xParent, _xContext, css::uno::Reference< css::container::XIndexAccess >( _xTableCharts, css::uno::UNO_QUERY ) ), xTableCharts( _xTableCharts ) , xDrawPageSupplier( _xDrawPageSupplier )
60 {
61 
62 }
63 
64 void
removeByName(const rtl::OUString & _sChartName)65 ScVbaChartObjects::removeByName(const rtl::OUString& _sChartName)
66 {
67 	xTableCharts->removeByName( _sChartName );
68 }
69 
70 uno::Sequence< rtl::OUString >
getChartObjectNames()71 ScVbaChartObjects::getChartObjectNames() throw( css::script::BasicErrorException )
72 {
73 	uno::Sequence< rtl::OUString > sChartNames;
74 	try
75 	{
76 		// c++ hackery
77 		uno::Reference< uno::XInterface > xIf( xDrawPageSupplier, uno::UNO_QUERY_THROW );
78 		ScCellRangesBase* pUno= dynamic_cast< ScCellRangesBase* >( xIf.get() );
79 		ScDocShell* pDocShell = NULL;
80 		if ( !pUno )
81 			throw uno::RuntimeException( rtl::OUString::createFromAscii("Failed to obtain the impl class from the drawpage"), uno::Reference< uno::XInterface >() );
82 		pDocShell = pUno->GetDocShell();
83 		if ( !pDocShell )
84 			throw uno::RuntimeException( rtl::OUString::createFromAscii("Failed to obtain the docshell implclass"), uno::Reference< uno::XInterface >() );
85 
86 		uno::Reference< sheet::XSpreadsheetDocument > xSpreadsheetDocument( pDocShell->GetModel(), uno::UNO_QUERY_THROW );
87 		uno::Reference< sheet::XSpreadsheets > xSpreadsheets = xSpreadsheetDocument->getSheets();
88 		std::vector< rtl::OUString > aChartNamesVector;
89 
90 		uno::Sequence< rtl::OUString > sSheetNames = xSpreadsheets->getElementNames();
91 		sal_Int32 nItems = sSheetNames.getLength();
92 		for (sal_Int32 i = 0; i < nItems; i++)
93 		{
94 			uno::Reference< table::XTableChartsSupplier > xLocTableChartsSupplier( xSpreadsheets->getByName(sSheetNames[i]), uno::UNO_QUERY_THROW );
95 			uno::Sequence< rtl::OUString > scurchartnames = xLocTableChartsSupplier->getCharts()->getElementNames();
96 			sal_Int32 nChartNames = scurchartnames.getLength();
97 			for (sal_Int32 n = 0; n < nChartNames; n++ )
98 				aChartNamesVector.push_back(scurchartnames[n]);
99 		}
100 		sChartNames.realloc( aChartNamesVector.size() );
101 		std::vector< rtl::OUString > ::const_iterator it = aChartNamesVector.begin();
102 		std::vector< rtl::OUString > ::const_iterator it_end = aChartNamesVector.end();
103 		for ( sal_Int32 index = 0 ; it != it_end; ++it, ++index )
104 			sChartNames[index] = *it;
105 	}
106 	catch (uno::Exception& )
107 	{
108 		throw script::BasicErrorException( rtl::OUString(), uno::Reference< uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString() );
109 	}
110 	return sChartNames;
111 }
112 
113 // XChartObjects
114 uno::Any SAL_CALL
Add(double _nX,double _nY,double _nWidth,double _nHeight)115 ScVbaChartObjects::Add( double _nX, double _nY, double _nWidth, double _nHeight ) throw (script::BasicErrorException)
116 {
117 	try
118 	{
119 		uno::Sequence< table::CellRangeAddress > aCellRangeAddress( 1 );
120 		awt::Rectangle aRectangle;
121 		aRectangle.X =  Millimeter::getInHundredthsOfOneMillimeter(_nX);
122 		aRectangle.Y = Millimeter::getInHundredthsOfOneMillimeter(_nY);
123 		aRectangle.Width = Millimeter::getInHundredthsOfOneMillimeter(_nWidth);
124 		aRectangle.Height = Millimeter::getInHundredthsOfOneMillimeter(_nHeight);
125 		// Note the space at the end of the stem ("Chart "). In ChartSheets only "Chart" is the stem
126 		rtl::OUString sPersistChartName = ContainerUtilities::getUniqueName( getChartObjectNames(), rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Chart " ) ) , rtl::OUString(), 1);
127 		xTableCharts->addNewByName(sPersistChartName, aRectangle, aCellRangeAddress, true, false );
128 		uno::Reference< excel::XChartObject > xChartObject( getItemByStringIndex( sPersistChartName ), uno::UNO_QUERY_THROW );
129 		xChartObject->getChart()->setChartType(excel::XlChartType::xlColumnClustered);
130 		return uno::makeAny( xChartObject );
131 	}
132 	catch ( uno::Exception& ex)
133 	{
134 		OSL_TRACE("AddItem caught exception ->%s", rtl::OUStringToOString( ex.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
135 	}
136 	return aNULL();
137 }
Delete()138 void SAL_CALL ScVbaChartObjects::Delete(  ) throw (script::BasicErrorException)
139 {
140 	uno::Sequence< rtl::OUString > sChartNames = xTableCharts->getElementNames();
141 	sal_Int32 ncount = sChartNames.getLength();
142 	for (sal_Int32 i = 0; i < ncount ; i++)
143 		removeByName(sChartNames[i]);
144 }
145 
146 // XEnumerationAccess
147 
148 uno::Reference< container::XEnumeration >
createEnumeration()149 ScVbaChartObjects::createEnumeration() throw (uno::RuntimeException)
150 {
151 	css::uno::Reference< container::XEnumerationAccess > xEnumAccess( xTableCharts, uno::UNO_QUERY_THROW );
152 	return new ChartObjectEnumerationImpl( mxContext, xEnumAccess->createEnumeration(), xDrawPageSupplier, getParent() /* sheet */);
153 }
154 
155 // XElementAccess
156 
157 uno::Type
getElementType()158 ScVbaChartObjects::getElementType() throw (uno::RuntimeException)
159 {
160 	return excel::XChartObject::static_type(0);
161 }
162 
163 // ScVbaCollectionBaseImpl
164 uno::Any
createCollectionObject(const css::uno::Any & aSource)165 ScVbaChartObjects::createCollectionObject( const css::uno::Any& aSource )
166 {
167 	uno::Reference< table::XTableChart > xTableChart( aSource, uno::UNO_QUERY_THROW );
168 	// correct parent object is sheet
169 	return uno::makeAny( uno::Reference< excel::XChartObject > ( new ScVbaChartObject( getParent(), mxContext, xTableChart, xDrawPageSupplier ) ) );
170 }
171 
172 rtl::OUString&
getServiceImplName()173 ScVbaChartObjects::getServiceImplName()
174 {
175 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaChartObjects") );
176 	return sImplName;
177 }
178 
179 css::uno::Sequence<rtl::OUString>
getServiceNames()180 ScVbaChartObjects::getServiceNames()
181 {
182 	static uno::Sequence< rtl::OUString > sNames;
183 	if ( sNames.getLength() == 0 )
184 	{
185 		sNames.realloc( 1 );
186 		sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.ChartObjects") );
187 	}
188 	return sNames;
189 }
190 
191