xref: /AOO41X/main/sc/source/ui/vba/vbaaxes.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "vbaaxes.hxx"
29*cdf0e10cSrcweir #include "vbaaxis.hxx"
30*cdf0e10cSrcweir #include "vbachart.hxx"
31*cdf0e10cSrcweir #include <ooo/vba/excel/XlAxisType.hpp>
32*cdf0e10cSrcweir #include <ooo/vba/excel/XlAxisGroup.hpp>
33*cdf0e10cSrcweir #include <ooo/vba/excel/XAxis.hpp>
34*cdf0e10cSrcweir #include <map>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir using namespace ::com::sun::star;
37*cdf0e10cSrcweir using namespace ::ooo::vba;
38*cdf0e10cSrcweir using namespace ::ooo::vba::excel::XlAxisType;
39*cdf0e10cSrcweir using namespace ::ooo::vba::excel::XlAxisGroup;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir // each 'Item' in the Axes collection is  indexed via 2 indexes,  group and type.
42*cdf0e10cSrcweir // We need to 'flatten' this into a single index in order to be able to wrap
43*cdf0e10cSrcweir // iteration over the set of Axis(s) in a XIndexAccess implementation
44*cdf0e10cSrcweir //
45*cdf0e10cSrcweir typedef ::std::pair<sal_Int32, sal_Int32 > AxesCoordinate; // type and group combination
46*cdf0e10cSrcweir typedef ::std::vector< AxesCoordinate > vecAxesIndices;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1< container::XIndexAccess > AxisIndexWrapper_BASE;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir class EnumWrapper : public EnumerationHelper_BASE
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir         uno::Reference<container::XIndexAccess > m_xIndexAccess;
53*cdf0e10cSrcweir 		sal_Int32 nIndex;
54*cdf0e10cSrcweir public:
55*cdf0e10cSrcweir         EnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : m_xIndexAccess( xIndexAccess ), nIndex( 0 ) {}
56*cdf0e10cSrcweir         virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
57*cdf0e10cSrcweir         {
58*cdf0e10cSrcweir                 return ( nIndex < m_xIndexAccess->getCount() );
59*cdf0e10cSrcweir         }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir         virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
62*cdf0e10cSrcweir         {
63*cdf0e10cSrcweir                 if ( nIndex < m_xIndexAccess->getCount() )
64*cdf0e10cSrcweir                         return m_xIndexAccess->getByIndex( nIndex++ );
65*cdf0e10cSrcweir                 throw container::NoSuchElementException();
66*cdf0e10cSrcweir         }
67*cdf0e10cSrcweir };
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir uno::Reference< excel::XAxis >
71*cdf0e10cSrcweir ScVbaAxes::createAxis( const uno::Reference< excel::XChart >& xChart, const uno::Reference< uno::XComponentContext >& xContext,  sal_Int32 nType, sal_Int32 nAxisGroup ) throw ( uno::RuntimeException )
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 	ScVbaChart* pChart = static_cast< ScVbaChart* >( xChart.get() );
74*cdf0e10cSrcweir 	if ( !pChart )
75*cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString::createFromAscii( "Object failure, can't access chart implementation" ), uno::Reference< uno::XInterface >()  );
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xAxisPropertySet;
78*cdf0e10cSrcweir 	if (((nType == xlCategory) || (nType == xlSeriesAxis) || (nType == xlValue)))
79*cdf0e10cSrcweir 	{
80*cdf0e10cSrcweir 		if ((nAxisGroup != xlPrimary) && (nAxisGroup != xlSecondary))
81*cdf0e10cSrcweir 			throw script::BasicErrorException( rtl::OUString(), NULL, SbERR_METHOD_FAILED, rtl::OUString());
82*cdf0e10cSrcweir 		xAxisPropertySet.set( pChart->getAxisPropertySet(nType, nAxisGroup), uno::UNO_QUERY_THROW );
83*cdf0e10cSrcweir 	}
84*cdf0e10cSrcweir 	else
85*cdf0e10cSrcweir 		throw script::BasicErrorException( rtl::OUString(), NULL, SbERR_METHOD_FAILED, rtl::OUString());
86*cdf0e10cSrcweir 	uno::Reference< XHelperInterface > xParent( xChart, uno::UNO_QUERY_THROW );
87*cdf0e10cSrcweir 	return new ScVbaAxis( xParent, xContext, xAxisPropertySet, nType, nAxisGroup);
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir class AxisIndexWrapper : public AxisIndexWrapper_BASE
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir 	// if necessary for better performance we could change this into a map and cache the
93*cdf0e10cSrcweir 	// indices -> Axis, currently we create a new Axis object
94*cdf0e10cSrcweir 	// on each getByIndex
95*cdf0e10cSrcweir 	uno::Reference< uno::XComponentContext > mxContext;
96*cdf0e10cSrcweir 	vecAxesIndices mCoordinates;
97*cdf0e10cSrcweir 	uno::Reference< excel::XChart > mxChart;
98*cdf0e10cSrcweir public:
99*cdf0e10cSrcweir 	AxisIndexWrapper( const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< excel::XChart >& xChart ) : mxContext( xContext ), mxChart( xChart )
100*cdf0e10cSrcweir 	{
101*cdf0e10cSrcweir 		if ( mxChart.is() )
102*cdf0e10cSrcweir 		{
103*cdf0e10cSrcweir 			ScVbaChart* pChart = static_cast< ScVbaChart* >( mxChart.get() );
104*cdf0e10cSrcweir 			// primary
105*cdf0e10cSrcweir 			sal_Bool bBool = false;
106*cdf0e10cSrcweir 			uno::Reference< beans::XPropertySet > xDiagramPropertySet( pChart->xDiagramPropertySet() );
107*cdf0e10cSrcweir 			if ( ( xDiagramPropertySet->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "HasXAxis" ) ) ) >>= bBool )  && bBool )
108*cdf0e10cSrcweir 				mCoordinates.push_back( AxesCoordinate( xlPrimary, xlCategory ) );
109*cdf0e10cSrcweir 			if ( ( xDiagramPropertySet->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "HasYAxis" ) ) ) >>= bBool )  && bBool )
110*cdf0e10cSrcweir 				mCoordinates.push_back( AxesCoordinate( xlPrimary, xlSeriesAxis ) );
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 			if (  pChart->is3D() )
113*cdf0e10cSrcweir 				mCoordinates.push_back( AxesCoordinate( xlPrimary, xlValue ) );
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 			// secondary
116*cdf0e10cSrcweir 			if ( ( xDiagramPropertySet->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "HasSecondaryXAxis" ) ) ) >>= bBool )  && bBool )
117*cdf0e10cSrcweir 				mCoordinates.push_back( AxesCoordinate( xlSecondary, xlCategory ) );
118*cdf0e10cSrcweir 			if ( ( xDiagramPropertySet->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "HasSecondaryYAxis" ) ) ) >>= bBool )  && bBool )
119*cdf0e10cSrcweir 				mCoordinates.push_back( AxesCoordinate( xlSecondary, xlSeriesAxis ) );
120*cdf0e10cSrcweir 		}
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 	}
123*cdf0e10cSrcweir 	virtual ::sal_Int32 SAL_CALL getCount() throw (uno::RuntimeException) { return mCoordinates.size(); }
124*cdf0e10cSrcweir 	virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, ::uno::RuntimeException)
125*cdf0e10cSrcweir 	{
126*cdf0e10cSrcweir 			AxesCoordinate dIndexes = mCoordinates[ Index ];
127*cdf0e10cSrcweir 			return uno::makeAny( ScVbaAxes::createAxis( mxChart, mxContext, dIndexes.second, dIndexes.first ) );
128*cdf0e10cSrcweir 	}
129*cdf0e10cSrcweir 	// XElementAccess
130*cdf0e10cSrcweir 	virtual uno::Type SAL_CALL getElementType() throw (uno::RuntimeException)
131*cdf0e10cSrcweir 	{
132*cdf0e10cSrcweir 		return excel::XAxis::static_type(0);
133*cdf0e10cSrcweir 	}
134*cdf0e10cSrcweir 	virtual ::sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException)
135*cdf0e10cSrcweir 	{
136*cdf0e10cSrcweir 		return ( mCoordinates.size() > 0 );
137*cdf0e10cSrcweir 	}
138*cdf0e10cSrcweir };
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir uno::Reference< container::XIndexAccess > createIndexWrapper( const uno::Reference< excel::XChart >& xChart, const uno::Reference< uno::XComponentContext >& xContext )
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir 	return new AxisIndexWrapper( xContext, xChart );
143*cdf0e10cSrcweir }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir // #FIXME The collection semantics will never work as this object is not yet initialised correctly
146*cdf0e10cSrcweir ScVbaAxes::ScVbaAxes( const uno::Reference< XHelperInterface >& xParent,const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< excel::XChart >& xChart ) : ScVbaAxes_BASE( xParent, xContext, createIndexWrapper( xChart, xContext )), moChartParent( xChart )
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir uno::Type SAL_CALL
151*cdf0e10cSrcweir ScVbaAxes::getElementType() throw (css::uno::RuntimeException)
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir 	return  excel::XAxes::static_type(0);
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir uno::Reference< container::XEnumeration > SAL_CALL
157*cdf0e10cSrcweir ScVbaAxes::createEnumeration() throw (css::uno::RuntimeException)
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir 	return new EnumWrapper( m_xIndexAccess );
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir uno::Any SAL_CALL
163*cdf0e10cSrcweir ScVbaAxes::Item( const css::uno::Any& _nType, const css::uno::Any& _oAxisGroup) throw (css::uno::RuntimeException)
164*cdf0e10cSrcweir {
165*cdf0e10cSrcweir 	// #TODO map the possible index combinations to a container::XIndexAccess wrapper impl
166*cdf0e10cSrcweir 	// using a vector of valid std::pair maybe?
167*cdf0e10cSrcweir 	// bodgy helperapi port bits
168*cdf0e10cSrcweir 	sal_Int32 nAxisGroup = xlPrimary;
169*cdf0e10cSrcweir 	sal_Int32 nType = -1;
170*cdf0e10cSrcweir 	if ( !_nType.hasValue() || ( ( _nType >>= nType ) == sal_False )  )
171*cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString::createFromAscii( "Axes::Item Failed to extract type" ), uno::Reference< uno::XInterface >()  );
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 	if ( _oAxisGroup.hasValue() )
174*cdf0e10cSrcweir 		_oAxisGroup >>= nAxisGroup ;
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	return uno::makeAny( createAxis( moChartParent, mxContext, nType, nAxisGroup ) );
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir uno::Any
180*cdf0e10cSrcweir ScVbaAxes::createCollectionObject(const css::uno::Any& aSource)
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir 	return aSource; // pass through ( it's already an XAxis object
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir rtl::OUString&
186*cdf0e10cSrcweir ScVbaAxes::getServiceImplName()
187*cdf0e10cSrcweir {
188*cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaAxes") );
189*cdf0e10cSrcweir 	return sImplName;
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir uno::Sequence< rtl::OUString >
193*cdf0e10cSrcweir ScVbaAxes::getServiceNames()
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
196*cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
197*cdf0e10cSrcweir 	{
198*cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
199*cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Axes" ) );
200*cdf0e10cSrcweir 	}
201*cdf0e10cSrcweir 	return aServiceNames;
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204