xref: /AOO41X/main/sc/source/ui/vba/vbaworksheets.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 "vbaworksheets.hxx"
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include <sfx2/dispatch.hxx>
30*cdf0e10cSrcweir #include <sfx2/app.hxx>
31*cdf0e10cSrcweir #include <sfx2/bindings.hxx>
32*cdf0e10cSrcweir #include <sfx2/request.hxx>
33*cdf0e10cSrcweir #include <sfx2/viewfrm.hxx>
34*cdf0e10cSrcweir #include <sfx2/itemwrapper.hxx>
35*cdf0e10cSrcweir #include <svl/itemset.hxx>
36*cdf0e10cSrcweir #include <svl/eitem.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
39*cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetView.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #include <ooo/vba/excel/XApplication.hpp>
49*cdf0e10cSrcweir #include <tools/string.hxx>
50*cdf0e10cSrcweir #include "tabvwsh.hxx"
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include "vbaglobals.hxx"
53*cdf0e10cSrcweir #include "vbaworksheet.hxx"
54*cdf0e10cSrcweir #include "vbaworkbook.hxx"
55*cdf0e10cSrcweir #include "unonames.hxx"
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir using namespace ::ooo::vba;
58*cdf0e10cSrcweir using namespace ::com::sun::star;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1< container::XEnumeration > SheetEnumeration_BASE;
62*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper3< container::XNameAccess, container::XIndexAccess, container::XEnumerationAccess > SheetCollectionHelper_BASE;
63*cdf0e10cSrcweir // a map ( or hashmap ) wont do as we need also to preserve the order
64*cdf0e10cSrcweir // (as added ) of the items
65*cdf0e10cSrcweir typedef std::vector< uno::Reference< sheet::XSpreadsheet > >  SheetMap;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir // #FIXME #TODO the implementation of the Sheets collections sucks,
69*cdf0e10cSrcweir // e.g. there is no support for tracking sheets added/removed from the collection
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir class WorkSheetsEnumeration : public SheetEnumeration_BASE
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 	SheetMap mSheetMap;
74*cdf0e10cSrcweir 	SheetMap::iterator mIt;
75*cdf0e10cSrcweir public:
76*cdf0e10cSrcweir 	WorkSheetsEnumeration( const SheetMap& sMap ) : mSheetMap( sMap ), mIt( mSheetMap.begin() ) {}
77*cdf0e10cSrcweir 	virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
78*cdf0e10cSrcweir 	{
79*cdf0e10cSrcweir 		return ( mIt != mSheetMap.end() );
80*cdf0e10cSrcweir 	}
81*cdf0e10cSrcweir 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
82*cdf0e10cSrcweir 	{
83*cdf0e10cSrcweir 		if ( !hasMoreElements() )
84*cdf0e10cSrcweir 			throw container::NoSuchElementException();
85*cdf0e10cSrcweir 		uno::Reference< sheet::XSpreadsheet > xSheet( *mIt++ );
86*cdf0e10cSrcweir 		return uno::makeAny( xSheet ) ;
87*cdf0e10cSrcweir 	}
88*cdf0e10cSrcweir };
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir class SheetCollectionHelper : public SheetCollectionHelper_BASE
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir 	SheetMap mSheetMap;
93*cdf0e10cSrcweir 	SheetMap::iterator cachePos;
94*cdf0e10cSrcweir public:
95*cdf0e10cSrcweir 	SheetCollectionHelper( const SheetMap& sMap ) : mSheetMap( sMap ), cachePos(mSheetMap.begin()) {}
96*cdf0e10cSrcweir 	// XElementAccess
97*cdf0e10cSrcweir 	virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException) { return  sheet::XSpreadsheet::static_type(0); }
98*cdf0e10cSrcweir 	virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException) { return ( mSheetMap.size() > 0 ); }
99*cdf0e10cSrcweir 	// XNameAcess
100*cdf0e10cSrcweir 	virtual uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
101*cdf0e10cSrcweir 	{
102*cdf0e10cSrcweir 		if ( !hasByName(aName) )
103*cdf0e10cSrcweir 			throw container::NoSuchElementException();
104*cdf0e10cSrcweir 		return uno::makeAny( *cachePos );
105*cdf0e10cSrcweir 	}
106*cdf0e10cSrcweir 	virtual uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw (uno::RuntimeException)
107*cdf0e10cSrcweir 	{
108*cdf0e10cSrcweir 		uno::Sequence< rtl::OUString > sNames( mSheetMap.size() );
109*cdf0e10cSrcweir 		rtl::OUString* pString = sNames.getArray();
110*cdf0e10cSrcweir 		SheetMap::iterator it = mSheetMap.begin();
111*cdf0e10cSrcweir 		SheetMap::iterator it_end = mSheetMap.end();
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 		for ( ; it != it_end; ++it, ++pString )
114*cdf0e10cSrcweir 		{
115*cdf0e10cSrcweir 			uno::Reference< container::XNamed > xName( *it, uno::UNO_QUERY_THROW );
116*cdf0e10cSrcweir 			*pString = xName->getName();
117*cdf0e10cSrcweir 		}
118*cdf0e10cSrcweir 		return sNames;
119*cdf0e10cSrcweir 	}
120*cdf0e10cSrcweir 	virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
121*cdf0e10cSrcweir 	{
122*cdf0e10cSrcweir 		cachePos = mSheetMap.begin();
123*cdf0e10cSrcweir 		SheetMap::iterator it_end = mSheetMap.end();
124*cdf0e10cSrcweir 		for ( ; cachePos != it_end; ++cachePos )
125*cdf0e10cSrcweir 		{
126*cdf0e10cSrcweir 			uno::Reference< container::XNamed > xName( *cachePos, uno::UNO_QUERY_THROW );
127*cdf0e10cSrcweir 			if ( aName.equals( xName->getName() ) )
128*cdf0e10cSrcweir 				break;
129*cdf0e10cSrcweir 		}
130*cdf0e10cSrcweir 		return ( cachePos != it_end );
131*cdf0e10cSrcweir 	}
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	// XElementAccess
134*cdf0e10cSrcweir 	virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException) { return mSheetMap.size(); }
135*cdf0e10cSrcweir 	virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
136*cdf0e10cSrcweir 	{
137*cdf0e10cSrcweir 		if ( Index < 0 || Index >= getCount() )
138*cdf0e10cSrcweir 			throw lang::IndexOutOfBoundsException();
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 		return uno::makeAny( mSheetMap[ Index ] );
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	}
143*cdf0e10cSrcweir 	// XEnumerationAccess
144*cdf0e10cSrcweir 	virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration(  ) throw (uno::RuntimeException)
145*cdf0e10cSrcweir 	{
146*cdf0e10cSrcweir 		return new WorkSheetsEnumeration( mSheetMap );
147*cdf0e10cSrcweir 	}
148*cdf0e10cSrcweir };
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir class SheetsEnumeration : public EnumerationHelperImpl
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir 	uno::Reference< frame::XModel > m_xModel;
153*cdf0e10cSrcweir public:
154*cdf0e10cSrcweir     SheetsEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration,  const uno::Reference< frame::XModel >& xModel  ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_xModel( xModel ) {}
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
157*cdf0e10cSrcweir 	{
158*cdf0e10cSrcweir 		uno::Reference< sheet::XSpreadsheet > xSheet( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
159*cdf0e10cSrcweir 		uno::Reference< XHelperInterface > xIf = excel::getUnoSheetModuleObj( xSheet );
160*cdf0e10cSrcweir 		uno::Any aRet;
161*cdf0e10cSrcweir 		if ( !xIf.is() )
162*cdf0e10cSrcweir         {
163*cdf0e10cSrcweir 			// if the Sheet is in a document created by the api unfortunately ( at the
164*cdf0e10cSrcweir 			// moment, it actually wont have the special Document modules
165*cdf0e10cSrcweir 			uno::Reference< excel::XWorksheet > xNewSheet( new ScVbaWorksheet( m_xParent, m_xContext, xSheet, m_xModel ) );
166*cdf0e10cSrcweir 			aRet <<= xNewSheet;
167*cdf0e10cSrcweir         }
168*cdf0e10cSrcweir         else
169*cdf0e10cSrcweir 			aRet <<= xIf;
170*cdf0e10cSrcweir 		return aRet;
171*cdf0e10cSrcweir 	}
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir };
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir ScVbaWorksheets::ScVbaWorksheets( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< ::com::sun::star::uno::XComponentContext > & xContext, const uno::Reference< container::XIndexAccess >& xSheets, const uno::Reference< frame::XModel >& xModel ): ScVbaWorksheets_BASE( xParent, xContext,  xSheets ), mxModel( xModel ), m_xSheets( uno::Reference< sheet::XSpreadsheets >( xSheets, uno::UNO_QUERY ) )
176*cdf0e10cSrcweir {
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir ScVbaWorksheets::ScVbaWorksheets( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< ::com::sun::star::uno::XComponentContext > & xContext, const uno::Reference< container::XEnumerationAccess >& xEnumAccess, const uno::Reference< frame::XModel >& xModel  ):  ScVbaWorksheets_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( xEnumAccess, uno::UNO_QUERY ) ), mxModel(xModel)
180*cdf0e10cSrcweir {
181*cdf0e10cSrcweir }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir // XEnumerationAccess
184*cdf0e10cSrcweir uno::Type
185*cdf0e10cSrcweir ScVbaWorksheets::getElementType() throw (uno::RuntimeException)
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir 	return excel::XWorksheet::static_type(0);
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir uno::Reference< container::XEnumeration >
191*cdf0e10cSrcweir ScVbaWorksheets::createEnumeration() throw (uno::RuntimeException)
192*cdf0e10cSrcweir {
193*cdf0e10cSrcweir 	if ( !m_xSheets.is() )
194*cdf0e10cSrcweir 	{
195*cdf0e10cSrcweir 		uno::Reference< container::XEnumerationAccess > xAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
196*cdf0e10cSrcweir 		return xAccess->createEnumeration();
197*cdf0e10cSrcweir 	}
198*cdf0e10cSrcweir 	uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xSheets, uno::UNO_QUERY_THROW );
199*cdf0e10cSrcweir 	return new SheetsEnumeration( this, mxContext, xEnumAccess->createEnumeration(), mxModel );
200*cdf0e10cSrcweir }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir uno::Any
203*cdf0e10cSrcweir ScVbaWorksheets::createCollectionObject( const uno::Any& aSource )
204*cdf0e10cSrcweir {
205*cdf0e10cSrcweir 	uno::Reference< sheet::XSpreadsheet > xSheet( aSource, uno::UNO_QUERY );
206*cdf0e10cSrcweir 	uno::Reference< XHelperInterface > xIf = excel::getUnoSheetModuleObj( xSheet );
207*cdf0e10cSrcweir 	uno::Any aRet;
208*cdf0e10cSrcweir 	if ( !xIf.is() )
209*cdf0e10cSrcweir 	{
210*cdf0e10cSrcweir 		// if the Sheet is in a document created by the api unfortunately ( at the
211*cdf0e10cSrcweir 		// moment, it actually wont have the special Document modules
212*cdf0e10cSrcweir 		uno::Reference< excel::XWorksheet > xNewSheet( new ScVbaWorksheet( getParent(), mxContext, xSheet, mxModel ) );
213*cdf0e10cSrcweir 		aRet <<= xNewSheet;
214*cdf0e10cSrcweir 	}
215*cdf0e10cSrcweir 	else
216*cdf0e10cSrcweir 		aRet <<= xIf;
217*cdf0e10cSrcweir 	return aRet;
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir // XWorksheets
221*cdf0e10cSrcweir uno::Any
222*cdf0e10cSrcweir ScVbaWorksheets::Add( const uno::Any& Before, const uno::Any& After,
223*cdf0e10cSrcweir 					 const uno::Any& Count, const uno::Any& Type ) throw (uno::RuntimeException)
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir 	if ( isSelectedSheets() )
226*cdf0e10cSrcweir 		return uno::Any(); // or should we throw?
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 	rtl::OUString aStringSheet;
229*cdf0e10cSrcweir 	sal_Bool bBefore(sal_True);
230*cdf0e10cSrcweir 	SCTAB nSheetIndex = 0;
231*cdf0e10cSrcweir 	SCTAB nNewSheets = 1, nType = 0;
232*cdf0e10cSrcweir 	Count >>= nNewSheets;
233*cdf0e10cSrcweir 	Type >>= nType;
234*cdf0e10cSrcweir 	SCTAB nCount = 0;
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir 	uno::Reference< excel::XWorksheet > xBeforeAfterSheet;
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 	if ( Before.hasValue() )
239*cdf0e10cSrcweir 	{
240*cdf0e10cSrcweir         	if ( Before >>= xBeforeAfterSheet )
241*cdf0e10cSrcweir 			aStringSheet = xBeforeAfterSheet->getName();
242*cdf0e10cSrcweir 		else
243*cdf0e10cSrcweir 			Before >>= aStringSheet;
244*cdf0e10cSrcweir 	}
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir 	if (!aStringSheet.getLength() && After.hasValue() )
247*cdf0e10cSrcweir 	{
248*cdf0e10cSrcweir         	if ( After >>= xBeforeAfterSheet )
249*cdf0e10cSrcweir 			aStringSheet = xBeforeAfterSheet->getName();
250*cdf0e10cSrcweir 		else
251*cdf0e10cSrcweir 			After >>= aStringSheet;
252*cdf0e10cSrcweir 		bBefore = sal_False;
253*cdf0e10cSrcweir 	}
254*cdf0e10cSrcweir 	if (!aStringSheet.getLength())
255*cdf0e10cSrcweir 	{
256*cdf0e10cSrcweir 		uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
257*cdf0e10cSrcweir 		aStringSheet = xApplication->getActiveWorkbook()->getActiveSheet()->getName();
258*cdf0e10cSrcweir 		bBefore = sal_True;
259*cdf0e10cSrcweir 	}
260*cdf0e10cSrcweir 		nCount = static_cast< SCTAB >( m_xIndexAccess->getCount() );
261*cdf0e10cSrcweir 		for (SCTAB i=0; i < nCount; i++)
262*cdf0e10cSrcweir 		{
263*cdf0e10cSrcweir 			uno::Reference< sheet::XSpreadsheet > xSheet(m_xIndexAccess->getByIndex(i), uno::UNO_QUERY);
264*cdf0e10cSrcweir 			uno::Reference< container::XNamed > xNamed( xSheet, uno::UNO_QUERY_THROW );
265*cdf0e10cSrcweir 			if (xNamed->getName() == aStringSheet)
266*cdf0e10cSrcweir 			{
267*cdf0e10cSrcweir 				nSheetIndex = i;
268*cdf0e10cSrcweir 				break;
269*cdf0e10cSrcweir 			}
270*cdf0e10cSrcweir 		}
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 	if(!bBefore)
273*cdf0e10cSrcweir 		nSheetIndex++;
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 	SCTAB nSheetName = nCount + 1L;
276*cdf0e10cSrcweir 	String aStringBase( RTL_CONSTASCII_USTRINGPARAM("Sheet") );
277*cdf0e10cSrcweir 	uno::Any result;
278*cdf0e10cSrcweir 	for (SCTAB i=0; i < nNewSheets; i++, nSheetName++)
279*cdf0e10cSrcweir 	{
280*cdf0e10cSrcweir 		String aStringName = aStringBase;
281*cdf0e10cSrcweir 		aStringName += String::CreateFromInt32(nSheetName);
282*cdf0e10cSrcweir 		while (m_xNameAccess->hasByName(aStringName))
283*cdf0e10cSrcweir 		{
284*cdf0e10cSrcweir 			nSheetName++;
285*cdf0e10cSrcweir 			aStringName = aStringBase;
286*cdf0e10cSrcweir 			aStringName += String::CreateFromInt32(nSheetName);
287*cdf0e10cSrcweir 		}
288*cdf0e10cSrcweir 		m_xSheets->insertNewByName(aStringName, nSheetIndex + i);
289*cdf0e10cSrcweir 		result = getItemByStringIndex( aStringName );
290*cdf0e10cSrcweir 	}
291*cdf0e10cSrcweir 	uno::Reference< excel::XWorksheet > xNewSheet( result, uno::UNO_QUERY );
292*cdf0e10cSrcweir 	if ( xNewSheet.is() )
293*cdf0e10cSrcweir 		xNewSheet->Activate();
294*cdf0e10cSrcweir 	return  result;
295*cdf0e10cSrcweir }
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir void
298*cdf0e10cSrcweir ScVbaWorksheets::Delete() throw (uno::RuntimeException)
299*cdf0e10cSrcweir {
300*cdf0e10cSrcweir 	// #TODO #INVESTIGATE
301*cdf0e10cSrcweir 	// mmm this method could be trouble if the underlying
302*cdf0e10cSrcweir 	// uno objects ( the m_xIndexAccess etc ) aren't aware of the
303*cdf0e10cSrcweir 	// contents that are deleted
304*cdf0e10cSrcweir 	sal_Int32 nElems = getCount();
305*cdf0e10cSrcweir 	for ( sal_Int32 nItem = 1; nItem <= nElems; ++nItem )
306*cdf0e10cSrcweir 	{
307*cdf0e10cSrcweir 		uno::Reference< excel::XWorksheet > xSheet( Item( uno::makeAny( nItem ), uno::Any() ), uno::UNO_QUERY_THROW );
308*cdf0e10cSrcweir 		xSheet->Delete();
309*cdf0e10cSrcweir 	}
310*cdf0e10cSrcweir }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir bool
313*cdf0e10cSrcweir ScVbaWorksheets::isSelectedSheets()
314*cdf0e10cSrcweir {
315*cdf0e10cSrcweir 	return !m_xSheets.is();
316*cdf0e10cSrcweir }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir void SAL_CALL
319*cdf0e10cSrcweir ScVbaWorksheets::PrintOut( const uno::Any& From, const uno::Any& To, const uno::Any& Copies, const uno::Any& Preview, const uno::Any& ActivePrinter, const uno::Any& PrintToFile, const uno::Any& Collate, const uno::Any& PrToFileName ) throw (uno::RuntimeException)
320*cdf0e10cSrcweir {
321*cdf0e10cSrcweir 	sal_Int32 nTo = 0;
322*cdf0e10cSrcweir 	sal_Int32 nFrom = 0;
323*cdf0e10cSrcweir 	sal_Int16 nCopies = 1;
324*cdf0e10cSrcweir 	sal_Bool bCollate = sal_False;
325*cdf0e10cSrcweir 	sal_Bool bSelection = sal_False;
326*cdf0e10cSrcweir 	From >>= nFrom;
327*cdf0e10cSrcweir 	To >>= nTo;
328*cdf0e10cSrcweir 	Copies >>= nCopies;
329*cdf0e10cSrcweir 	if ( nCopies > 1 ) // Collate only useful when more that 1 copy
330*cdf0e10cSrcweir 		Collate >>= bCollate;
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 	if ( !( nFrom || nTo ) )
333*cdf0e10cSrcweir 		if ( isSelectedSheets() )
334*cdf0e10cSrcweir 			bSelection = sal_True;
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir 	PrintOutHelper( excel::getBestViewShell( mxModel ), From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, bSelection );
337*cdf0e10cSrcweir }
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir uno::Any SAL_CALL
340*cdf0e10cSrcweir ScVbaWorksheets::getVisible() throw (uno::RuntimeException)
341*cdf0e10cSrcweir {
342*cdf0e10cSrcweir 	sal_Bool bVisible = sal_True;
343*cdf0e10cSrcweir 	uno::Reference< container::XEnumeration > xEnum( createEnumeration(), uno::UNO_QUERY_THROW );
344*cdf0e10cSrcweir 	while ( xEnum->hasMoreElements() )
345*cdf0e10cSrcweir 	{
346*cdf0e10cSrcweir 		uno::Reference< excel::XWorksheet > xSheet( xEnum->nextElement(), uno::UNO_QUERY_THROW );
347*cdf0e10cSrcweir 		if ( xSheet->getVisible() == sal_False )
348*cdf0e10cSrcweir 		{
349*cdf0e10cSrcweir 				bVisible = sal_False;
350*cdf0e10cSrcweir 				break;
351*cdf0e10cSrcweir 		}
352*cdf0e10cSrcweir 	}
353*cdf0e10cSrcweir 	return uno::makeAny( bVisible );
354*cdf0e10cSrcweir }
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir void SAL_CALL
357*cdf0e10cSrcweir ScVbaWorksheets::setVisible( const uno::Any& _visible ) throw (uno::RuntimeException)
358*cdf0e10cSrcweir {
359*cdf0e10cSrcweir 	sal_Bool bState = sal_False;
360*cdf0e10cSrcweir 	if ( _visible >>= bState )
361*cdf0e10cSrcweir 	{
362*cdf0e10cSrcweir 		uno::Reference< container::XEnumeration > xEnum( createEnumeration(), uno::UNO_QUERY_THROW );
363*cdf0e10cSrcweir 		while ( xEnum->hasMoreElements() )
364*cdf0e10cSrcweir 		{
365*cdf0e10cSrcweir 			uno::Reference< excel::XWorksheet > xSheet( xEnum->nextElement(), uno::UNO_QUERY_THROW );
366*cdf0e10cSrcweir 			xSheet->setVisible( bState );
367*cdf0e10cSrcweir 		}
368*cdf0e10cSrcweir 	}
369*cdf0e10cSrcweir 	else
370*cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString(
371*cdf0e10cSrcweir 			RTL_CONSTASCII_USTRINGPARAM( "Visible property doesn't support non boolean #FIXME" ) ), uno::Reference< uno::XInterface >() );
372*cdf0e10cSrcweir }
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir void SAL_CALL
375*cdf0e10cSrcweir ScVbaWorksheets::Select( const uno::Any& Replace ) throw (uno::RuntimeException)
376*cdf0e10cSrcweir {
377*cdf0e10cSrcweir 	ScTabViewShell* pViewShell = excel::getBestViewShell( mxModel );
378*cdf0e10cSrcweir 	if ( !pViewShell )
379*cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Cannot obtain view shell" ) ), uno::Reference< uno::XInterface >() );
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir 	ScMarkData& rMarkData = pViewShell->GetViewData()->GetMarkData();
382*cdf0e10cSrcweir 	sal_Bool bReplace = sal_True;
383*cdf0e10cSrcweir 	Replace >>= bReplace;
384*cdf0e10cSrcweir 	// Replace is defaulted to True, meanining this current collection
385*cdf0e10cSrcweir 	// becomes the Selection, if it were false then the current selection would
386*cdf0e10cSrcweir 	// be extended
387*cdf0e10cSrcweir 	bool bSelectSingle = bReplace;
388*cdf0e10cSrcweir 	sal_Int32 nElems = getCount();
389*cdf0e10cSrcweir 	for ( sal_Int32 nItem = 1; nItem <= nElems; ++nItem )
390*cdf0e10cSrcweir 	{
391*cdf0e10cSrcweir 		uno::Reference< excel::XWorksheet > xSheet( Item( uno::makeAny( nItem ), uno::Any() ), uno::UNO_QUERY_THROW );
392*cdf0e10cSrcweir 		ScVbaWorksheet* pSheet = dynamic_cast< ScVbaWorksheet* >( xSheet.get() );
393*cdf0e10cSrcweir 		if ( pSheet )
394*cdf0e10cSrcweir 		{
395*cdf0e10cSrcweir 			if ( bSelectSingle )
396*cdf0e10cSrcweir 			{
397*cdf0e10cSrcweir 				rMarkData.SelectOneTable( static_cast< SCTAB >( pSheet->getSheetID() ) );
398*cdf0e10cSrcweir 				bSelectSingle = false;
399*cdf0e10cSrcweir 			}
400*cdf0e10cSrcweir 			else
401*cdf0e10cSrcweir 				rMarkData.SelectTable( static_cast< SCTAB >( pSheet->getSheetID() ), sal_True );
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir 		}
404*cdf0e10cSrcweir 	}
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir }
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir //ScVbaCollectionBaseImpl
410*cdf0e10cSrcweir uno::Any SAL_CALL
411*cdf0e10cSrcweir ScVbaWorksheets::Item( const uno::Any& Index, const uno::Any& Index2  ) throw (uno::RuntimeException)
412*cdf0e10cSrcweir {
413*cdf0e10cSrcweir 	if ( Index.getValueTypeClass() == uno::TypeClass_SEQUENCE )
414*cdf0e10cSrcweir 	{
415*cdf0e10cSrcweir 		uno::Reference< script::XTypeConverter > xConverter = getTypeConverter(mxContext);
416*cdf0e10cSrcweir 		uno::Any aConverted;
417*cdf0e10cSrcweir 		aConverted = xConverter->convertTo( Index, getCppuType((uno::Sequence< uno::Any >*)0) );
418*cdf0e10cSrcweir 		SheetMap mSheets;
419*cdf0e10cSrcweir 		uno::Sequence< uno::Any > sIndices;
420*cdf0e10cSrcweir 		aConverted >>= sIndices;
421*cdf0e10cSrcweir 		sal_Int32 nElems = sIndices.getLength();
422*cdf0e10cSrcweir 		for( sal_Int32 index = 0; index < nElems; ++index )
423*cdf0e10cSrcweir 		{
424*cdf0e10cSrcweir 			uno::Reference< excel::XWorksheet > xWorkSheet( ScVbaWorksheets_BASE::Item( sIndices[ index ], Index2 ), uno::UNO_QUERY_THROW );
425*cdf0e10cSrcweir 			ScVbaWorksheet* pWorkSheet = dynamic_cast< ScVbaWorksheet* >( xWorkSheet.get() );
426*cdf0e10cSrcweir 			if ( pWorkSheet )
427*cdf0e10cSrcweir 			{
428*cdf0e10cSrcweir 				uno::Reference< sheet::XSpreadsheet > xSheet( pWorkSheet->getSheet() , uno::UNO_QUERY_THROW );
429*cdf0e10cSrcweir 				uno::Reference< container::XNamed > xName( xSheet, uno::UNO_QUERY_THROW );
430*cdf0e10cSrcweir 				mSheets.push_back( xSheet );
431*cdf0e10cSrcweir 			}
432*cdf0e10cSrcweir 		}
433*cdf0e10cSrcweir 		uno::Reference< container::XIndexAccess > xIndexAccess = new SheetCollectionHelper( mSheets );
434*cdf0e10cSrcweir 		uno::Reference< XCollection > xSelectedSheets(  new ScVbaWorksheets( this->getParent(), mxContext, xIndexAccess, mxModel ) );
435*cdf0e10cSrcweir 		return uno::makeAny( xSelectedSheets );
436*cdf0e10cSrcweir 	}
437*cdf0e10cSrcweir 	return 	ScVbaWorksheets_BASE::Item( Index, Index2 );
438*cdf0e10cSrcweir }
439*cdf0e10cSrcweir 
440*cdf0e10cSrcweir uno::Any
441*cdf0e10cSrcweir ScVbaWorksheets::getItemByStringIndex( const rtl::OUString& sIndex ) throw (uno::RuntimeException)
442*cdf0e10cSrcweir {
443*cdf0e10cSrcweir 	return ScVbaWorksheets_BASE::getItemByStringIndex( sIndex );
444*cdf0e10cSrcweir }
445*cdf0e10cSrcweir 
446*cdf0e10cSrcweir rtl::OUString&
447*cdf0e10cSrcweir ScVbaWorksheets::getServiceImplName()
448*cdf0e10cSrcweir {
449*cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWorksheets") );
450*cdf0e10cSrcweir 	return sImplName;
451*cdf0e10cSrcweir }
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir css::uno::Sequence<rtl::OUString>
454*cdf0e10cSrcweir ScVbaWorksheets::getServiceNames()
455*cdf0e10cSrcweir {
456*cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > sNames;
457*cdf0e10cSrcweir 	if ( sNames.getLength() == 0 )
458*cdf0e10cSrcweir 	{
459*cdf0e10cSrcweir 		sNames.realloc( 1 );
460*cdf0e10cSrcweir 		sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Worksheets") );
461*cdf0e10cSrcweir 	}
462*cdf0e10cSrcweir 	return sNames;
463*cdf0e10cSrcweir }
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir /*static*/ bool ScVbaWorksheets::nameExists( uno::Reference <sheet::XSpreadsheetDocument>& xSpreadDoc, const ::rtl::OUString & name, SCTAB& nTab ) throw ( lang::IllegalArgumentException )
466*cdf0e10cSrcweir {
467*cdf0e10cSrcweir 	if (!xSpreadDoc.is())
468*cdf0e10cSrcweir 		throw lang::IllegalArgumentException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nameExists() xSpreadDoc is null" ) ), uno::Reference< uno::XInterface  >(), 1 );
469*cdf0e10cSrcweir 	uno::Reference <container::XIndexAccess> xIndex( xSpreadDoc->getSheets(), uno::UNO_QUERY );
470*cdf0e10cSrcweir 	if ( xIndex.is() )
471*cdf0e10cSrcweir 	{
472*cdf0e10cSrcweir 		SCTAB  nCount = static_cast< SCTAB >( xIndex->getCount() );
473*cdf0e10cSrcweir 		for (SCTAB i=0; i < nCount; i++)
474*cdf0e10cSrcweir 		{
475*cdf0e10cSrcweir 			uno::Reference< container::XNamed > xNamed( xIndex->getByIndex(i), uno::UNO_QUERY_THROW );
476*cdf0e10cSrcweir 			if (xNamed->getName() == name)
477*cdf0e10cSrcweir 			{
478*cdf0e10cSrcweir 				nTab = i;
479*cdf0e10cSrcweir 				return true;
480*cdf0e10cSrcweir 			}
481*cdf0e10cSrcweir 		}
482*cdf0e10cSrcweir 	}
483*cdf0e10cSrcweir 	return false;
484*cdf0e10cSrcweir }
485