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