nss: upgrade to release 3.73
[LibreOffice.git] / sc / source / ui / vba / vbachartobjects.cxx
blob4f38a59a3e69d75fcf4cfc550b15002d9e47d545
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 <com/sun/star/table/XTableChartsSupplier.hpp>
21 #include <com/sun/star/table/XTableChart.hpp>
22 #include <com/sun/star/script/BasicErrorException.hpp>
23 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
24 #include <ooo/vba/excel/XlChartType.hpp>
26 #include "vbachartobjects.hxx"
27 #include "vbachartobject.hxx"
28 #include <docsh.hxx>
29 #include <cellsuno.hxx>
30 #include <vector>
31 #include <basic/sberrors.hxx>
32 #include <comphelper/sequence.hxx>
33 #include <tools/diagnose_ex.h>
35 using namespace ::com::sun::star;
36 using namespace ::ooo::vba;
38 namespace {
40 class ChartObjectEnumerationImpl : public EnumerationHelperImpl
42 uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier;
44 public:
45 /// @throws uno::RuntimeException
46 ChartObjectEnumerationImpl( const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Reference< drawing::XDrawPageSupplier >& _xDrawPageSupplier, const uno::Reference< XHelperInterface >& _xParent ) : EnumerationHelperImpl( _xParent, xContext, xEnumeration ), xDrawPageSupplier( _xDrawPageSupplier ) {}
47 virtual uno::Any SAL_CALL nextElement( ) override
49 uno::Any ret;
51 try
53 uno::Reference< table::XTableChart > xTableChart( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
54 // parent Object is sheet
55 ret <<= uno::Reference< excel::XChartObject > ( new ScVbaChartObject( m_xParent, m_xContext, xTableChart, xDrawPageSupplier ) );
57 catch (const lang::WrappedTargetException&)
59 throw;
61 catch (const container::NoSuchElementException&)
63 throw;
65 catch (const uno::RuntimeException&)
67 throw;
69 catch (const uno::Exception&)
71 css::uno::Any anyEx(cppu::getCaughtException());
72 throw lang::WrappedTargetException(
73 "Error creating ScVbaChartObject!",
74 static_cast < OWeakObject * > ( this ),
75 anyEx );
77 return ret;
83 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 )
88 void
89 ScVbaChartObjects::removeByName(const OUString& _sChartName)
91 xTableCharts->removeByName( _sChartName );
94 uno::Sequence< OUString >
95 ScVbaChartObjects::getChartObjectNames() const
97 uno::Sequence< OUString > sChartNames;
98 try
100 // c++ hackery
101 uno::Reference< uno::XInterface > xIf( xDrawPageSupplier, uno::UNO_QUERY_THROW );
102 ScCellRangesBase* pUno= dynamic_cast< ScCellRangesBase* >( xIf.get() );
103 ScDocShell* pDocShell = nullptr;
104 if ( !pUno )
105 throw uno::RuntimeException("Failed to obtain the impl class from the drawpage" );
106 pDocShell = pUno->GetDocShell();
107 if ( !pDocShell )
108 throw uno::RuntimeException("Failed to obtain the docshell implclass" );
110 uno::Reference< sheet::XSpreadsheetDocument > xSpreadsheetDocument( pDocShell->GetModel(), uno::UNO_QUERY_THROW );
111 uno::Reference< sheet::XSpreadsheets > xSpreadsheets = xSpreadsheetDocument->getSheets();
112 std::vector< OUString > aChartNamesVector;
114 const uno::Sequence< OUString > sSheetNames = xSpreadsheets->getElementNames();
115 for (const auto& rSheetName : sSheetNames)
117 uno::Reference< table::XTableChartsSupplier > xLocTableChartsSupplier( xSpreadsheets->getByName(rSheetName), uno::UNO_QUERY_THROW );
118 const uno::Sequence< OUString > scurchartnames = xLocTableChartsSupplier->getCharts()->getElementNames();
119 aChartNamesVector.insert( aChartNamesVector.end(), scurchartnames.begin(), scurchartnames.end() );
121 sChartNames = comphelper::containerToSequence( aChartNamesVector );
123 catch (uno::Exception& )
125 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
127 return sChartNames;
130 // XChartObjects
131 uno::Any SAL_CALL
132 ScVbaChartObjects::Add( double _nX, double _nY, double _nWidth, double _nHeight )
136 uno::Sequence< table::CellRangeAddress > aCellRangeAddress( 1 );
137 awt::Rectangle aRectangle;
138 aRectangle.X = Millimeter::getInHundredthsOfOneMillimeter(_nX);
139 aRectangle.Y = Millimeter::getInHundredthsOfOneMillimeter(_nY);
140 aRectangle.Width = Millimeter::getInHundredthsOfOneMillimeter(_nWidth);
141 aRectangle.Height = Millimeter::getInHundredthsOfOneMillimeter(_nHeight);
142 // Note the space at the end of the stem ("Chart "). In ChartSheets only "Chart" is the stem
143 OUString sPersistChartName = ContainerUtilities::getUniqueName( getChartObjectNames(), "Chart " , OUString(), 1);
144 xTableCharts->addNewByName(sPersistChartName, aRectangle, aCellRangeAddress, true, false );
145 uno::Reference< excel::XChartObject > xChartObject( getItemByStringIndex( sPersistChartName ), uno::UNO_QUERY_THROW );
146 xChartObject->getChart()->setChartType(excel::XlChartType::xlColumnClustered);
147 return uno::makeAny( xChartObject );
149 catch (const uno::Exception&)
151 DBG_UNHANDLED_EXCEPTION("sc");
153 return aNULL();
155 void SAL_CALL ScVbaChartObjects::Delete( )
157 const uno::Sequence< OUString > sChartNames = xTableCharts->getElementNames();
158 for (const auto& rChartName : sChartNames)
159 removeByName(rChartName);
162 // XEnumerationAccess
164 uno::Reference< container::XEnumeration >
165 ScVbaChartObjects::createEnumeration()
167 css::uno::Reference< container::XEnumerationAccess > xEnumAccess( xTableCharts, uno::UNO_QUERY_THROW );
168 return new ChartObjectEnumerationImpl( mxContext, xEnumAccess->createEnumeration(), xDrawPageSupplier, getParent() /* sheet */);
171 // XElementAccess
173 uno::Type
174 ScVbaChartObjects::getElementType()
176 return cppu::UnoType<excel::XChartObject>::get();
179 // ScVbaCollectionBaseImpl
180 uno::Any
181 ScVbaChartObjects::createCollectionObject( const css::uno::Any& aSource )
183 uno::Reference< table::XTableChart > xTableChart( aSource, uno::UNO_QUERY_THROW );
184 // correct parent object is sheet
185 return uno::makeAny( uno::Reference< excel::XChartObject > ( new ScVbaChartObject( getParent(), mxContext, xTableChart, xDrawPageSupplier ) ) );
188 OUString
189 ScVbaChartObjects::getServiceImplName()
191 return "ScVbaChartObjects";
194 css::uno::Sequence<OUString>
195 ScVbaChartObjects::getServiceNames()
197 static uno::Sequence< OUString > const sNames
199 "ooo.vba.excel.ChartObjects"
201 return sNames;
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */