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