nss: upgrade to release 3.73
[LibreOffice.git] / sc / source / ui / vba / vbaaxes.cxx
blob61d09208751e80514dee7d21becbb85c85fc007a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "vbaaxes.hxx"
21 #include "vbaaxis.hxx"
22 #include "vbachart.hxx"
23 #include <basic/sberrors.hxx>
24 #include <cppuhelper/exc_hlp.hxx>
25 #include <cppuhelper/implbase.hxx>
26 #include <com/sun/star/script/BasicErrorException.hpp>
27 #include <ooo/vba/excel/XlAxisType.hpp>
28 #include <ooo/vba/excel/XlAxisGroup.hpp>
29 #include <ooo/vba/excel/XAxis.hpp>
31 using namespace ::com::sun::star;
32 using namespace ::ooo::vba;
33 using namespace ::ooo::vba::excel::XlAxisType;
34 using namespace ::ooo::vba::excel::XlAxisGroup;
36 // each 'Item' in the Axes collection is indexed via 2 indexes, group and type.
37 // We need to 'flatten' this into a single index in order to be able to wrap
38 // iteration over the set of Axis(s) in a XIndexAccess implementation
40 typedef ::std::pair<sal_Int32, sal_Int32 > AxesCoordinate; // type and group combination
42 namespace {
44 class EnumWrapper : public EnumerationHelper_BASE
46 uno::Reference<container::XIndexAccess > m_xIndexAccess;
47 sal_Int32 nIndex;
48 public:
49 explicit EnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : m_xIndexAccess( xIndexAccess ), nIndex( 0 ) {}
50 virtual sal_Bool SAL_CALL hasMoreElements( ) override
52 return ( nIndex < m_xIndexAccess->getCount() );
55 virtual uno::Any SAL_CALL nextElement( ) override
57 if ( nIndex < m_xIndexAccess->getCount() )
58 return m_xIndexAccess->getByIndex( nIndex++ );
59 throw container::NoSuchElementException();
65 uno::Reference< excel::XAxis >
66 ScVbaAxes::createAxis( const uno::Reference< excel::XChart >& xChart, const uno::Reference< uno::XComponentContext >& xContext, sal_Int32 nType, sal_Int32 nAxisGroup )
68 ScVbaChart* pChart = static_cast< ScVbaChart* >( xChart.get() );
69 if ( !pChart )
70 throw uno::RuntimeException("Object failure, can't access chart implementation" );
72 uno::Reference< beans::XPropertySet > xAxisPropertySet;
73 if ((nType == xlCategory) || (nType == xlSeriesAxis) || (nType == xlValue))
75 if ((nAxisGroup != xlPrimary) && (nAxisGroup != xlSecondary))
76 DebugHelper::runtimeexception(ERRCODE_BASIC_METHOD_FAILED);
77 xAxisPropertySet.set( pChart->getAxisPropertySet(nType, nAxisGroup), uno::UNO_SET_THROW );
79 else
80 DebugHelper::runtimeexception(ERRCODE_BASIC_METHOD_FAILED);
81 uno::Reference< XHelperInterface > xParent( xChart, uno::UNO_QUERY_THROW );
82 return new ScVbaAxis( xParent, xContext, xAxisPropertySet, nType, nAxisGroup);
85 namespace {
87 class AxisIndexWrapper : public ::cppu::WeakImplHelper< container::XIndexAccess >
89 // if necessary for better performance we could change this into a map and cache the
90 // indices -> Axis, currently we create a new Axis object
91 // on each getByIndex
92 uno::Reference< uno::XComponentContext > mxContext;
93 std::vector< AxesCoordinate > mCoordinates;
94 uno::Reference< excel::XChart > mxChart;
95 public:
96 AxisIndexWrapper( const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< excel::XChart >& xChart ) : mxContext( xContext ), mxChart( xChart )
98 if ( !mxChart.is() )
99 return;
101 ScVbaChart* pChart = static_cast< ScVbaChart* >( mxChart.get() );
102 // primary
103 bool bBool = false;
104 uno::Reference< beans::XPropertySet > xDiagramPropertySet( pChart->xDiagramPropertySet() );
105 if ( ( xDiagramPropertySet->getPropertyValue("HasXAxis") >>= bBool ) && bBool )
106 mCoordinates.emplace_back( xlPrimary, xlCategory );
107 if ( ( xDiagramPropertySet->getPropertyValue("HasYAxis") >>= bBool ) && bBool )
108 mCoordinates.emplace_back( xlPrimary, xlSeriesAxis );
110 if ( pChart->is3D() )
111 mCoordinates.emplace_back( xlPrimary, xlValue );
113 // secondary
114 if ( ( xDiagramPropertySet->getPropertyValue("HasSecondaryXAxis") >>= bBool ) && bBool )
115 mCoordinates.emplace_back( xlSecondary, xlCategory );
116 if ( ( xDiagramPropertySet->getPropertyValue("HasSecondaryYAxis") >>= bBool ) && bBool )
117 mCoordinates.emplace_back( xlSecondary, xlSeriesAxis );
120 virtual ::sal_Int32 SAL_CALL getCount() override { return mCoordinates.size(); }
121 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
125 AxesCoordinate dIndexes = mCoordinates[ Index ];
126 return uno::makeAny( ScVbaAxes::createAxis( mxChart, mxContext, dIndexes.second, dIndexes.first ) );
128 catch (const css::script::BasicErrorException&)
130 css::uno::Any anyEx = cppu::getCaughtException();
131 throw css::lang::WrappedTargetException(
132 "Error Getting Index!",
133 static_cast < OWeakObject * > ( this ),
134 anyEx );
137 // XElementAccess
138 virtual uno::Type SAL_CALL getElementType() override
140 return cppu::UnoType<excel::XAxis>::get();
142 virtual sal_Bool SAL_CALL hasElements( ) override
144 return ( !mCoordinates.empty() );
148 uno::Reference< container::XIndexAccess > createIndexWrapper( const uno::Reference< excel::XChart >& xChart, const uno::Reference< uno::XComponentContext >& xContext )
150 return new AxisIndexWrapper( xContext, xChart );
155 // #FIXME The collection semantics will never work as this object is not yet initialised correctly
156 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 )
160 uno::Type SAL_CALL
161 ScVbaAxes::getElementType()
163 return cppu::UnoType<excel::XAxes>::get();
166 uno::Reference< container::XEnumeration > SAL_CALL
167 ScVbaAxes::createEnumeration()
169 return new EnumWrapper( m_xIndexAccess );
172 uno::Any SAL_CALL
173 ScVbaAxes::Item( const css::uno::Any& _nType, const css::uno::Any& _oAxisGroup)
175 // #TODO map the possible index combinations to a container::XIndexAccess wrapper impl
176 // using a vector of valid std::pair maybe?
177 // body helper api port bits
178 sal_Int32 nAxisGroup = xlPrimary;
179 sal_Int32 nType = -1;
180 if ( !_nType.hasValue() || !( _nType >>= nType ) )
181 throw uno::RuntimeException("Axes::Item Failed to extract type" );
183 if ( _oAxisGroup.hasValue() )
184 _oAxisGroup >>= nAxisGroup ;
186 return uno::makeAny( createAxis( moChartParent, mxContext, nType, nAxisGroup ) );
189 uno::Any
190 ScVbaAxes::createCollectionObject(const css::uno::Any& aSource)
192 return aSource; // pass through ( it's already an XAxis object
195 OUString
196 ScVbaAxes::getServiceImplName()
198 return "ScVbaAxes";
201 uno::Sequence< OUString >
202 ScVbaAxes::getServiceNames()
204 static uno::Sequence< OUString > const aServiceNames
206 "ooo.vba.excel.Axes"
208 return aServiceNames;
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */