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 #include "vbachart.hxx" 24cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 25cdf0e10cSrcweir #include <com/sun/star/document/XEmbeddedObjectSupplier.hpp> 26cdf0e10cSrcweir #include <com/sun/star/table/XTableChartsSupplier.hpp> 27cdf0e10cSrcweir #include <com/sun/star/table/XTableChart.hpp> 28cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetDocument.hpp> 29cdf0e10cSrcweir #include <ooo/vba/excel/XlChartType.hpp> 30cdf0e10cSrcweir 31cdf0e10cSrcweir 32cdf0e10cSrcweir #include "vbachartobjects.hxx" 33cdf0e10cSrcweir #include "vbachartobject.hxx" 34cdf0e10cSrcweir #include "vbaglobals.hxx" 35cdf0e10cSrcweir #include "cellsuno.hxx" 36cdf0e10cSrcweir #include <vector> 37cdf0e10cSrcweir #include <basic/sberrors.hxx> 38cdf0e10cSrcweir 39cdf0e10cSrcweir using namespace ::com::sun::star; 40cdf0e10cSrcweir using namespace ::ooo::vba; 41cdf0e10cSrcweir 42cdf0e10cSrcweir 43cdf0e10cSrcweir class ChartObjectEnumerationImpl : public EnumerationHelperImpl 44cdf0e10cSrcweir { 45cdf0e10cSrcweir uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier; 46cdf0e10cSrcweir 47cdf0e10cSrcweir public: 48cdf0e10cSrcweir 49cdf0e10cSrcweir ChartObjectEnumerationImpl( const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Reference< drawing::XDrawPageSupplier >& _xDrawPageSupplier, const uno::Reference< XHelperInterface >& _xParent ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( _xParent, xContext, xEnumeration ), xDrawPageSupplier( _xDrawPageSupplier ) {} 50cdf0e10cSrcweir virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 51cdf0e10cSrcweir { 52cdf0e10cSrcweir uno::Reference< table::XTableChart > xTableChart( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW ); 53cdf0e10cSrcweir // parent Object is sheet 54cdf0e10cSrcweir return uno::makeAny( uno::Reference< excel::XChartObject > ( new ScVbaChartObject( m_xParent, m_xContext, xTableChart, xDrawPageSupplier ) ) ); 55cdf0e10cSrcweir } 56cdf0e10cSrcweir }; 57cdf0e10cSrcweir 58cdf0e10cSrcweir 59cdf0e10cSrcweir 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 ) 60cdf0e10cSrcweir { 61cdf0e10cSrcweir 62cdf0e10cSrcweir } 63cdf0e10cSrcweir 64cdf0e10cSrcweir void 65cdf0e10cSrcweir ScVbaChartObjects::removeByName(const rtl::OUString& _sChartName) 66cdf0e10cSrcweir { 67cdf0e10cSrcweir xTableCharts->removeByName( _sChartName ); 68cdf0e10cSrcweir } 69cdf0e10cSrcweir 70cdf0e10cSrcweir uno::Sequence< rtl::OUString > 71cdf0e10cSrcweir ScVbaChartObjects::getChartObjectNames() throw( css::script::BasicErrorException ) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir uno::Sequence< rtl::OUString > sChartNames; 74cdf0e10cSrcweir try 75cdf0e10cSrcweir { 76cdf0e10cSrcweir // c++ hackery 77cdf0e10cSrcweir uno::Reference< uno::XInterface > xIf( xDrawPageSupplier, uno::UNO_QUERY_THROW ); 78cdf0e10cSrcweir ScCellRangesBase* pUno= dynamic_cast< ScCellRangesBase* >( xIf.get() ); 79cdf0e10cSrcweir ScDocShell* pDocShell = NULL; 80cdf0e10cSrcweir if ( !pUno ) 81cdf0e10cSrcweir throw uno::RuntimeException( rtl::OUString::createFromAscii("Failed to obtain the impl class from the drawpage"), uno::Reference< uno::XInterface >() ); 82cdf0e10cSrcweir pDocShell = pUno->GetDocShell(); 83cdf0e10cSrcweir if ( !pDocShell ) 84cdf0e10cSrcweir throw uno::RuntimeException( rtl::OUString::createFromAscii("Failed to obtain the docshell implclass"), uno::Reference< uno::XInterface >() ); 85cdf0e10cSrcweir 86cdf0e10cSrcweir uno::Reference< sheet::XSpreadsheetDocument > xSpreadsheetDocument( pDocShell->GetModel(), uno::UNO_QUERY_THROW ); 87cdf0e10cSrcweir uno::Reference< sheet::XSpreadsheets > xSpreadsheets = xSpreadsheetDocument->getSheets(); 88cdf0e10cSrcweir std::vector< rtl::OUString > aChartNamesVector; 89cdf0e10cSrcweir 90cdf0e10cSrcweir uno::Sequence< rtl::OUString > sSheetNames = xSpreadsheets->getElementNames(); 91cdf0e10cSrcweir sal_Int32 nItems = sSheetNames.getLength(); 92cdf0e10cSrcweir for (sal_Int32 i = 0; i < nItems; i++) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir uno::Reference< table::XTableChartsSupplier > xLocTableChartsSupplier( xSpreadsheets->getByName(sSheetNames[i]), uno::UNO_QUERY_THROW ); 95cdf0e10cSrcweir uno::Sequence< rtl::OUString > scurchartnames = xLocTableChartsSupplier->getCharts()->getElementNames(); 96cdf0e10cSrcweir sal_Int32 nChartNames = scurchartnames.getLength(); 97cdf0e10cSrcweir for (sal_Int32 n = 0; n < nChartNames; n++ ) 98cdf0e10cSrcweir aChartNamesVector.push_back(scurchartnames[n]); 99cdf0e10cSrcweir } 100cdf0e10cSrcweir sChartNames.realloc( aChartNamesVector.size() ); 101cdf0e10cSrcweir std::vector< rtl::OUString > ::const_iterator it = aChartNamesVector.begin(); 102cdf0e10cSrcweir std::vector< rtl::OUString > ::const_iterator it_end = aChartNamesVector.end(); 103cdf0e10cSrcweir for ( sal_Int32 index = 0 ; it != it_end; ++it, ++index ) 104cdf0e10cSrcweir sChartNames[index] = *it; 105cdf0e10cSrcweir } 106cdf0e10cSrcweir catch (uno::Exception& ) 107cdf0e10cSrcweir { 108cdf0e10cSrcweir throw script::BasicErrorException( rtl::OUString(), uno::Reference< uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString() ); 109cdf0e10cSrcweir } 110cdf0e10cSrcweir return sChartNames; 111cdf0e10cSrcweir } 112cdf0e10cSrcweir 113cdf0e10cSrcweir // XChartObjects 114cdf0e10cSrcweir uno::Any SAL_CALL 115cdf0e10cSrcweir ScVbaChartObjects::Add( double _nX, double _nY, double _nWidth, double _nHeight ) throw (script::BasicErrorException) 116cdf0e10cSrcweir { 117cdf0e10cSrcweir try 118cdf0e10cSrcweir { 119cdf0e10cSrcweir uno::Sequence< table::CellRangeAddress > aCellRangeAddress( 1 ); 120cdf0e10cSrcweir awt::Rectangle aRectangle; 121cdf0e10cSrcweir aRectangle.X = Millimeter::getInHundredthsOfOneMillimeter(_nX); 122cdf0e10cSrcweir aRectangle.Y = Millimeter::getInHundredthsOfOneMillimeter(_nY); 123cdf0e10cSrcweir aRectangle.Width = Millimeter::getInHundredthsOfOneMillimeter(_nWidth); 124cdf0e10cSrcweir aRectangle.Height = Millimeter::getInHundredthsOfOneMillimeter(_nHeight); 125cdf0e10cSrcweir // Note the space at the end of the stem ("Chart "). In ChartSheets only "Chart" is the stem 126cdf0e10cSrcweir rtl::OUString sPersistChartName = ContainerUtilities::getUniqueName( getChartObjectNames(), rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Chart " ) ) , rtl::OUString(), 1); 127cdf0e10cSrcweir xTableCharts->addNewByName(sPersistChartName, aRectangle, aCellRangeAddress, true, false ); 128cdf0e10cSrcweir uno::Reference< excel::XChartObject > xChartObject( getItemByStringIndex( sPersistChartName ), uno::UNO_QUERY_THROW ); 129cdf0e10cSrcweir xChartObject->getChart()->setChartType(excel::XlChartType::xlColumnClustered); 130cdf0e10cSrcweir return uno::makeAny( xChartObject ); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir catch ( uno::Exception& ex) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir OSL_TRACE("AddItem caught exception ->%s", rtl::OUStringToOString( ex.Message, RTL_TEXTENCODING_UTF8 ).getStr() ); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir return aNULL(); 137cdf0e10cSrcweir } 138cdf0e10cSrcweir void SAL_CALL ScVbaChartObjects::Delete( ) throw (script::BasicErrorException) 139cdf0e10cSrcweir { 140cdf0e10cSrcweir uno::Sequence< rtl::OUString > sChartNames = xTableCharts->getElementNames(); 141cdf0e10cSrcweir sal_Int32 ncount = sChartNames.getLength(); 142cdf0e10cSrcweir for (sal_Int32 i = 0; i < ncount ; i++) 143cdf0e10cSrcweir removeByName(sChartNames[i]); 144cdf0e10cSrcweir } 145cdf0e10cSrcweir 146cdf0e10cSrcweir // XEnumerationAccess 147cdf0e10cSrcweir 148cdf0e10cSrcweir uno::Reference< container::XEnumeration > 149cdf0e10cSrcweir ScVbaChartObjects::createEnumeration() throw (uno::RuntimeException) 150cdf0e10cSrcweir { 151cdf0e10cSrcweir css::uno::Reference< container::XEnumerationAccess > xEnumAccess( xTableCharts, uno::UNO_QUERY_THROW ); 152cdf0e10cSrcweir return new ChartObjectEnumerationImpl( mxContext, xEnumAccess->createEnumeration(), xDrawPageSupplier, getParent() /* sheet */); 153cdf0e10cSrcweir } 154cdf0e10cSrcweir 155cdf0e10cSrcweir // XElementAccess 156cdf0e10cSrcweir 157cdf0e10cSrcweir uno::Type 158cdf0e10cSrcweir ScVbaChartObjects::getElementType() throw (uno::RuntimeException) 159cdf0e10cSrcweir { 160cdf0e10cSrcweir return excel::XChartObject::static_type(0); 161cdf0e10cSrcweir } 162cdf0e10cSrcweir 163cdf0e10cSrcweir // ScVbaCollectionBaseImpl 164cdf0e10cSrcweir uno::Any 165cdf0e10cSrcweir ScVbaChartObjects::createCollectionObject( const css::uno::Any& aSource ) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir uno::Reference< table::XTableChart > xTableChart( aSource, uno::UNO_QUERY_THROW ); 168cdf0e10cSrcweir // correct parent object is sheet 169cdf0e10cSrcweir return uno::makeAny( uno::Reference< excel::XChartObject > ( new ScVbaChartObject( getParent(), mxContext, xTableChart, xDrawPageSupplier ) ) ); 170cdf0e10cSrcweir } 171cdf0e10cSrcweir 172cdf0e10cSrcweir rtl::OUString& 173cdf0e10cSrcweir ScVbaChartObjects::getServiceImplName() 174cdf0e10cSrcweir { 175cdf0e10cSrcweir static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaChartObjects") ); 176cdf0e10cSrcweir return sImplName; 177cdf0e10cSrcweir } 178cdf0e10cSrcweir 179cdf0e10cSrcweir css::uno::Sequence<rtl::OUString> 180cdf0e10cSrcweir ScVbaChartObjects::getServiceNames() 181cdf0e10cSrcweir { 182cdf0e10cSrcweir static uno::Sequence< rtl::OUString > sNames; 183cdf0e10cSrcweir if ( sNames.getLength() == 0 ) 184cdf0e10cSrcweir { 185cdf0e10cSrcweir sNames.realloc( 1 ); 186cdf0e10cSrcweir sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.ChartObjects") ); 187cdf0e10cSrcweir } 188cdf0e10cSrcweir return sNames; 189cdf0e10cSrcweir } 190cdf0e10cSrcweir 191