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