xref: /trunk/main/sc/source/ui/vba/vbachartobjects.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #include "vbachart.hxx"
28*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
29*cdf0e10cSrcweir #include <com/sun/star/document/XEmbeddedObjectSupplier.hpp>
30*cdf0e10cSrcweir #include <com/sun/star/table/XTableChartsSupplier.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/table/XTableChart.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
33*cdf0e10cSrcweir #include <ooo/vba/excel/XlChartType.hpp>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include "vbachartobjects.hxx"
37*cdf0e10cSrcweir #include "vbachartobject.hxx"
38*cdf0e10cSrcweir #include "vbaglobals.hxx"
39*cdf0e10cSrcweir #include "cellsuno.hxx"
40*cdf0e10cSrcweir #include <vector>
41*cdf0e10cSrcweir #include <basic/sberrors.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir using namespace ::com::sun::star;
44*cdf0e10cSrcweir using namespace ::ooo::vba;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir class ChartObjectEnumerationImpl : public EnumerationHelperImpl
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir     uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir public:
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir     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 ) {}
54*cdf0e10cSrcweir     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
55*cdf0e10cSrcweir     {
56*cdf0e10cSrcweir         uno::Reference< table::XTableChart > xTableChart( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
57*cdf0e10cSrcweir         // parent Object is sheet
58*cdf0e10cSrcweir         return uno::makeAny(  uno::Reference< excel::XChartObject > ( new ScVbaChartObject(  m_xParent, m_xContext, xTableChart, xDrawPageSupplier ) ) );
59*cdf0e10cSrcweir     }
60*cdf0e10cSrcweir };
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 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 )
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir void
69*cdf0e10cSrcweir ScVbaChartObjects::removeByName(const rtl::OUString& _sChartName)
70*cdf0e10cSrcweir {
71*cdf0e10cSrcweir     xTableCharts->removeByName( _sChartName );
72*cdf0e10cSrcweir }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir uno::Sequence< rtl::OUString >
75*cdf0e10cSrcweir ScVbaChartObjects::getChartObjectNames() throw( css::script::BasicErrorException )
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir     uno::Sequence< rtl::OUString > sChartNames;
78*cdf0e10cSrcweir     try
79*cdf0e10cSrcweir     {
80*cdf0e10cSrcweir         // c++ hackery
81*cdf0e10cSrcweir         uno::Reference< uno::XInterface > xIf( xDrawPageSupplier, uno::UNO_QUERY_THROW );
82*cdf0e10cSrcweir         ScCellRangesBase* pUno= dynamic_cast< ScCellRangesBase* >( xIf.get() );
83*cdf0e10cSrcweir         ScDocShell* pDocShell = NULL;
84*cdf0e10cSrcweir         if ( !pUno )
85*cdf0e10cSrcweir             throw uno::RuntimeException( rtl::OUString::createFromAscii("Failed to obtain the impl class from the drawpage"), uno::Reference< uno::XInterface >() );
86*cdf0e10cSrcweir         pDocShell = pUno->GetDocShell();
87*cdf0e10cSrcweir         if ( !pDocShell )
88*cdf0e10cSrcweir             throw uno::RuntimeException( rtl::OUString::createFromAscii("Failed to obtain the docshell implclass"), uno::Reference< uno::XInterface >() );
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir         uno::Reference< sheet::XSpreadsheetDocument > xSpreadsheetDocument( pDocShell->GetModel(), uno::UNO_QUERY_THROW );
91*cdf0e10cSrcweir         uno::Reference< sheet::XSpreadsheets > xSpreadsheets = xSpreadsheetDocument->getSheets();
92*cdf0e10cSrcweir         std::vector< rtl::OUString > aChartNamesVector;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         uno::Sequence< rtl::OUString > sSheetNames = xSpreadsheets->getElementNames();
95*cdf0e10cSrcweir         sal_Int32 nItems = sSheetNames.getLength();
96*cdf0e10cSrcweir         for (sal_Int32 i = 0; i < nItems; i++)
97*cdf0e10cSrcweir         {
98*cdf0e10cSrcweir             uno::Reference< table::XTableChartsSupplier > xLocTableChartsSupplier( xSpreadsheets->getByName(sSheetNames[i]), uno::UNO_QUERY_THROW );
99*cdf0e10cSrcweir             uno::Sequence< rtl::OUString > scurchartnames = xLocTableChartsSupplier->getCharts()->getElementNames();
100*cdf0e10cSrcweir             sal_Int32 nChartNames = scurchartnames.getLength();
101*cdf0e10cSrcweir             for (sal_Int32 n = 0; n < nChartNames; n++ )
102*cdf0e10cSrcweir                 aChartNamesVector.push_back(scurchartnames[n]);
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir         sChartNames.realloc( aChartNamesVector.size() );
105*cdf0e10cSrcweir         std::vector< rtl::OUString > ::const_iterator it = aChartNamesVector.begin();
106*cdf0e10cSrcweir         std::vector< rtl::OUString > ::const_iterator it_end = aChartNamesVector.end();
107*cdf0e10cSrcweir         for ( sal_Int32 index = 0 ; it != it_end; ++it, ++index )
108*cdf0e10cSrcweir             sChartNames[index] = *it;
109*cdf0e10cSrcweir     }
110*cdf0e10cSrcweir     catch (uno::Exception& )
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         throw script::BasicErrorException( rtl::OUString(), uno::Reference< uno::XInterface >(), SbERR_METHOD_FAILED, rtl::OUString() );
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir     return sChartNames;
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir // XChartObjects
118*cdf0e10cSrcweir uno::Any SAL_CALL
119*cdf0e10cSrcweir ScVbaChartObjects::Add( double _nX, double _nY, double _nWidth, double _nHeight ) throw (script::BasicErrorException)
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir     try
122*cdf0e10cSrcweir     {
123*cdf0e10cSrcweir         uno::Sequence< table::CellRangeAddress > aCellRangeAddress( 1 );
124*cdf0e10cSrcweir         awt::Rectangle aRectangle;
125*cdf0e10cSrcweir         aRectangle.X =  Millimeter::getInHundredthsOfOneMillimeter(_nX);
126*cdf0e10cSrcweir         aRectangle.Y = Millimeter::getInHundredthsOfOneMillimeter(_nY);
127*cdf0e10cSrcweir         aRectangle.Width = Millimeter::getInHundredthsOfOneMillimeter(_nWidth);
128*cdf0e10cSrcweir         aRectangle.Height = Millimeter::getInHundredthsOfOneMillimeter(_nHeight);
129*cdf0e10cSrcweir         // Note the space at the end of the stem ("Chart "). In ChartSheets only "Chart" is the stem
130*cdf0e10cSrcweir         rtl::OUString sPersistChartName = ContainerUtilities::getUniqueName( getChartObjectNames(), rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Chart " ) ) , rtl::OUString(), 1);
131*cdf0e10cSrcweir         xTableCharts->addNewByName(sPersistChartName, aRectangle, aCellRangeAddress, true, false );
132*cdf0e10cSrcweir         uno::Reference< excel::XChartObject > xChartObject( getItemByStringIndex( sPersistChartName ), uno::UNO_QUERY_THROW );
133*cdf0e10cSrcweir         xChartObject->getChart()->setChartType(excel::XlChartType::xlColumnClustered);
134*cdf0e10cSrcweir         return uno::makeAny( xChartObject );
135*cdf0e10cSrcweir     }
136*cdf0e10cSrcweir     catch ( uno::Exception& ex)
137*cdf0e10cSrcweir     {
138*cdf0e10cSrcweir         OSL_TRACE("AddItem caught exception ->%s", rtl::OUStringToOString( ex.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir     return aNULL();
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir void SAL_CALL ScVbaChartObjects::Delete(  ) throw (script::BasicErrorException)
143*cdf0e10cSrcweir {
144*cdf0e10cSrcweir     uno::Sequence< rtl::OUString > sChartNames = xTableCharts->getElementNames();
145*cdf0e10cSrcweir     sal_Int32 ncount = sChartNames.getLength();
146*cdf0e10cSrcweir     for (sal_Int32 i = 0; i < ncount ; i++)
147*cdf0e10cSrcweir         removeByName(sChartNames[i]);
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir // XEnumerationAccess
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir uno::Reference< container::XEnumeration >
153*cdf0e10cSrcweir ScVbaChartObjects::createEnumeration() throw (uno::RuntimeException)
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir     css::uno::Reference< container::XEnumerationAccess > xEnumAccess( xTableCharts, uno::UNO_QUERY_THROW );
156*cdf0e10cSrcweir     return new ChartObjectEnumerationImpl( mxContext, xEnumAccess->createEnumeration(), xDrawPageSupplier, getParent() /* sheet */);
157*cdf0e10cSrcweir }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir // XElementAccess
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir uno::Type
162*cdf0e10cSrcweir ScVbaChartObjects::getElementType() throw (uno::RuntimeException)
163*cdf0e10cSrcweir {
164*cdf0e10cSrcweir     return excel::XChartObject::static_type(0);
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir // ScVbaCollectionBaseImpl
168*cdf0e10cSrcweir uno::Any
169*cdf0e10cSrcweir ScVbaChartObjects::createCollectionObject( const css::uno::Any& aSource )
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir     uno::Reference< table::XTableChart > xTableChart( aSource, uno::UNO_QUERY_THROW );
172*cdf0e10cSrcweir     // correct parent object is sheet
173*cdf0e10cSrcweir     return uno::makeAny( uno::Reference< excel::XChartObject > ( new ScVbaChartObject( getParent(), mxContext, xTableChart, xDrawPageSupplier ) ) );
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir rtl::OUString&
177*cdf0e10cSrcweir ScVbaChartObjects::getServiceImplName()
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaChartObjects") );
180*cdf0e10cSrcweir     return sImplName;
181*cdf0e10cSrcweir }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir css::uno::Sequence<rtl::OUString>
184*cdf0e10cSrcweir ScVbaChartObjects::getServiceNames()
185*cdf0e10cSrcweir {
186*cdf0e10cSrcweir     static uno::Sequence< rtl::OUString > sNames;
187*cdf0e10cSrcweir     if ( sNames.getLength() == 0 )
188*cdf0e10cSrcweir     {
189*cdf0e10cSrcweir         sNames.realloc( 1 );
190*cdf0e10cSrcweir         sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.ChartObjects") );
191*cdf0e10cSrcweir     }
192*cdf0e10cSrcweir     return sNames;
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir 
195