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