Update ooo320-m1
[ooovba.git] / sc / source / ui / vba / vbaworkbook.cxx
bloba50d7f9b1dc2e5e16ad3efeeade1eb096b790e50
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: vbaworkbook.cxx,v $
10 * $Revision: 1.5 $
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 <vbahelper/helperdecl.hxx>
31 #include <tools/urlobj.hxx>
32 #include <comphelper/unwrapargs.hxx>
34 #include <com/sun/star/util/XModifiable.hpp>
35 #include <com/sun/star/util/XProtectable.hpp>
36 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
37 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
38 #include <com/sun/star/frame/XStorable.hpp>
39 #include <com/sun/star/frame/XFrame.hpp>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <ooo/vba/excel/XlFileFormat.hpp>
43 #include "scextopt.hxx"
44 #include "vbaworksheet.hxx"
45 #include "vbaworksheets.hxx"
46 #include "vbaworkbook.hxx"
47 #include "vbawindows.hxx"
48 #include "vbastyles.hxx"
49 #include "excelvbahelper.hxx"
50 #include "vbapalette.hxx"
51 #include <osl/file.hxx>
52 #include <stdio.h>
53 #include "vbanames.hxx" // Amelia Wang
54 #include "nameuno.hxx"
55 #include "docoptio.hxx"
57 // Much of the impl. for the equivalend UNO module is
58 // sc/source/ui/unoobj/docuno.cxx, viewuno.cxx
60 using namespace ::ooo::vba;
61 using namespace ::com::sun::star;
63 class ActiveSheet : public ScVbaWorksheet
65 protected:
66 virtual uno::Reference< frame::XModel > getModel()
68 return getCurrentExcelDoc( mxContext );
70 virtual uno::Reference< sheet::XSpreadsheet > getSheet()
72 uno::Reference< frame::XModel > xModel = getModel();
73 uno::Reference< sheet::XSpreadsheet > xSheet;
74 if ( xModel.is() )
76 uno::Reference< sheet::XSpreadsheetView > xSpreadsheet(
77 xModel->getCurrentController(), uno::UNO_QUERY );
78 if ( xSpreadsheet.is() )
79 xSheet = xSpreadsheet->getActiveSheet();
81 return xSheet;
83 public:
84 ActiveSheet( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext ) : ScVbaWorksheet( xParent, xContext ) {}
88 uno::Sequence< sal_Int32 > ScVbaWorkbook::ColorData;
90 void ScVbaWorkbook::initColorData( const uno::Sequence< sal_Int32 >& sColors )
92 const sal_Int32* pSource = sColors.getConstArray();
93 sal_Int32* pDest = ColorData.getArray();
94 const sal_Int32* pEnd = pSource + sColors.getLength();
95 for ( ; pSource != pEnd; ++pSource, ++pDest )
96 *pDest = *pSource;
100 void SAL_CALL
101 ScVbaWorkbook::ResetColors( ) throw (::script::BasicErrorException, ::uno::RuntimeException)
103 uno::Reference< container::XIndexAccess > xIndexAccess( ScVbaPalette::getDefaultPalette(), uno::UNO_QUERY_THROW );
104 sal_Int32 nLen = xIndexAccess->getCount();
105 ColorData.realloc( nLen );
107 uno::Sequence< sal_Int32 > dDefaultColors( nLen );
108 sal_Int32* pDest = dDefaultColors.getArray();
109 for ( sal_Int32 index=0; index < nLen; ++pDest, ++index )
110 xIndexAccess->getByIndex( index ) >>= (*pDest);
111 initColorData( dDefaultColors );
114 ::uno::Any SAL_CALL
115 ScVbaWorkbook::Colors( const ::uno::Any& Index ) throw (::script::BasicErrorException, ::uno::RuntimeException)
117 uno::Any aRet;
118 if ( Index.getValue() )
120 sal_Int32 nIndex = 0;
121 Index >>= nIndex;
122 aRet = uno::makeAny( XLRGBToOORGB( ColorData[ --nIndex ] ) );
124 else
125 aRet = uno::makeAny( ColorData );
126 return aRet;
129 ::sal_Int32 SAL_CALL
130 ScVbaWorkbook::FileFormat( ) throw (::script::BasicErrorException, ::uno::RuntimeException)
132 sal_Int32 aFileFormat = 0;
133 rtl::OUString aFilterName;
134 uno::Sequence< beans::PropertyValue > aArgs = getModel()->getArgs();
136 // #FIXME - seems suspect should we not walk through the properties
137 // to find the FilterName
138 if (aArgs[0].Name.equalsAscii( "FilterName")) {
139 aArgs[0].Value >>= aFilterName;
140 } else {
141 aArgs[1].Value >>= aFilterName;
144 if (aFilterName.equalsAscii("Text - txt - csv (StarCalc)")) {
145 aFileFormat = excel::XlFileFormat::xlCSV; //xlFileFormat.
148 if (aFilterName.equalsAscii("DBF")) {
149 aFileFormat = excel::XlFileFormat::xlDBF4;
152 if (aFilterName.equalsAscii("DIF")) {
153 aFileFormat = excel::XlFileFormat::xlDIF;
156 if (aFilterName.equalsAscii("Lotus")) {
157 aFileFormat = excel::XlFileFormat::xlWK3;
160 if (aFilterName.equalsAscii("MS Excel 4.0")) {
161 aFileFormat = excel::XlFileFormat::xlExcel4Workbook;
164 if (aFilterName.equalsAscii("MS Excel 5.0/95")) {
165 aFileFormat = excel::XlFileFormat::xlExcel5;
168 if (aFilterName.equalsAscii("MS Excel 97")) {
169 aFileFormat = excel::XlFileFormat::xlExcel9795;
172 if (aFilterName.equalsAscii("HTML (StarCalc)")) {
173 aFileFormat = excel::XlFileFormat::xlHtml;
176 if (aFilterName.equalsAscii("calc_StarOffice_XML_Calc_Template")) {
177 aFileFormat = excel::XlFileFormat::xlTemplate;
180 if (aFilterName.equalsAscii("StarOffice XML (Calc)")) {
181 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
183 if (aFilterName.equalsAscii("calc8")) {
184 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
187 return aFileFormat;
190 void
191 ScVbaWorkbook::init()
193 if ( !ColorData.getLength() )
194 ResetColors();
196 ScVbaWorkbook::ScVbaWorkbook( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext) :ScVbaWorkbook_BASE( xParent, xContext )
198 //#FIXME this persists the color data per office instance and
199 // not per workbook instance, need to hook the data into XModel
200 // ( e.g. we already store the imported palette in there )
201 // so we should,
202 // a) make the class that does that a service
203 // b) make that service implement XIndexContainer
204 init();
207 ScVbaWorkbook::ScVbaWorkbook( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, css::uno::Reference< css::frame::XModel > xModel ) : ScVbaWorkbook_BASE( xParent, xContext, xModel )
209 init();
212 ScVbaWorkbook::ScVbaWorkbook( uno::Sequence< uno::Any> const & args,
213 uno::Reference< uno::XComponentContext> const & xContext ) : ScVbaWorkbook_BASE( args, xContext )
216 init();
219 uno::Reference< excel::XWorksheet >
220 ScVbaWorkbook::getActiveSheet() throw (uno::RuntimeException)
222 uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ) );
223 uno::Reference< sheet::XSpreadsheet > xSheet;
224 uno::Reference< sheet::XSpreadsheetView > xView( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
225 if ( xView.is() )
226 xSheet = xView->getActiveSheet();
227 return new ScVbaWorksheet( this, mxContext, xSheet, xModel );
229 uno::Any SAL_CALL
230 ScVbaWorkbook::Sheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
232 return Worksheets( aIndex );
237 uno::Any SAL_CALL
238 ScVbaWorkbook::Worksheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
240 uno::Reference< frame::XModel > xModel( getModel() );
241 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xModel, uno::UNO_QUERY_THROW );
242 uno::Reference<container::XIndexAccess > xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
243 uno::Reference< XCollection > xWorkSheets( new ScVbaWorksheets( this, mxContext, xSheets, xModel ) );
244 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
246 return uno::Any( xWorkSheets );
248 // pass on to collection
249 return uno::Any( xWorkSheets->Item( aIndex, uno::Any() ) );
251 uno::Any SAL_CALL
252 ScVbaWorkbook::Windows( const uno::Any& aIndex ) throw (uno::RuntimeException)
255 uno::Reference< excel::XWindows > xWindows( new ScVbaWindows( getParent(), mxContext ) );
256 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
257 return uno::Any( xWindows );
258 return uno::Any( xWindows->Item( aIndex, uno::Any() ) );
261 void SAL_CALL
262 ScVbaWorkbook::Activate() throw (uno::RuntimeException)
264 VbaDocumentBase::Activate();
267 ::sal_Bool
268 ScVbaWorkbook::getProtectStructure() throw (uno::RuntimeException)
270 uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
271 return xProt->isProtected();
274 ::sal_Bool SAL_CALL ScVbaWorkbook::getPrecisionAsDisplayed() throw (uno::RuntimeException)
276 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
277 ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
278 return pDoc->GetDocOptions().IsCalcAsShown();
281 void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed ) throw (uno::RuntimeException)
283 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
284 ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
285 ScDocOptions aOpt = pDoc->GetDocOptions();
286 aOpt.SetCalcAsShown( _precisionAsDisplayed );
287 pDoc->SetDocOptions( aOpt );
290 void
291 ScVbaWorkbook::SaveCopyAs( const rtl::OUString& sFileName ) throw ( uno::RuntimeException)
293 rtl::OUString aURL;
294 osl::FileBase::getFileURLFromSystemPath( sFileName, aURL );
295 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
296 uno::Sequence< beans::PropertyValue > storeProps(1);
297 storeProps[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterName" ) );
298 storeProps[0].Value <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MS Excel 97" ) );
299 xStor->storeToURL( aURL, storeProps );
302 css::uno::Any SAL_CALL
303 ScVbaWorkbook::Styles( const::uno::Any& Item ) throw (uno::RuntimeException)
305 // quick look and Styles object doesn't seem to have a valid parent
306 // or a least the object browser just shows an object that has no
307 // variables ( therefore... leave as NULL for now )
308 uno::Reference< XCollection > dStyles = new ScVbaStyles( uno::Reference< XHelperInterface >(), mxContext, getModel() );
309 if ( Item.hasValue() )
310 return dStyles->Item( Item, uno::Any() );
311 return uno::makeAny( dStyles );
314 // Amelia Wang
315 uno::Any SAL_CALL
316 ScVbaWorkbook::Names( const css::uno::Any& aIndex ) throw (uno::RuntimeException)
318 uno::Reference< frame::XModel > xModel( getModel() );
319 uno::Reference< beans::XPropertySet > xProps( xModel, uno::UNO_QUERY_THROW );
320 uno::Reference< sheet::XNamedRanges > xNamedRanges( xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("NamedRanges") ) ), uno::UNO_QUERY_THROW );
321 uno::Reference< XCollection > xNames( new ScVbaNames( this , mxContext , xNamedRanges , xModel ));
322 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
324 return uno::Any( xNames );
326 return uno::Any( xNames->Item( aIndex, uno::Any() ) );
329 rtl::OUString&
330 ScVbaWorkbook::getServiceImplName()
332 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWorkbook") );
333 return sImplName;
336 uno::Sequence< rtl::OUString >
337 ScVbaWorkbook::getServiceNames()
339 static uno::Sequence< rtl::OUString > aServiceNames;
340 if ( aServiceNames.getLength() == 0 )
342 aServiceNames.realloc( 1 );
343 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Workbook" ) );
345 return aServiceNames;
348 ::rtl::OUString SAL_CALL
349 ScVbaWorkbook::getCodeName() throw (css::uno::RuntimeException)
351 #ifdef VBA_OOBUILD_HACK
352 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
353 ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
354 ScExtDocOptions* pExtOptions = pDoc->GetExtDocOptions();
355 ScExtDocSettings pExtSettings = pExtOptions->GetDocSettings();
356 ::rtl::OUString sGlobCodeName = pExtSettings.maGlobCodeName;
357 return sGlobCodeName;
358 #else
359 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
360 #endif
362 #ifdef VBA_OOBUILD_HACK
363 void SAL_CALL
364 ScVbaWorkbook::setCodeName( const ::rtl::OUString& sGlobCodeName ) throw (css::uno::RuntimeException)
366 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
367 ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
368 ScExtDocOptions* pExtOptions = pDoc->GetExtDocOptions();
369 ScExtDocSettings pExtSettings = pExtOptions->GetDocSettings();
370 pExtSettings.maGlobCodeName = sGlobCodeName;
371 #else
372 void SAL_CALL
373 ScVbaWorkbook::setCodeName( const ::rtl::OUString& ) throw (css::uno::RuntimeException)
375 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
376 #endif
379 namespace workbook
381 namespace sdecl = comphelper::service_decl;
382 sdecl::vba_service_class_<ScVbaWorkbook, sdecl::with_args<true> > serviceImpl;
383 extern sdecl::ServiceDecl const serviceDecl(
384 serviceImpl,
385 "ScVbaWorkbook",
386 "ooo.vba.excel.Workbook" );