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