bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / ui / vba / vbaworkbook.cxx
blob3a36112d4a2a392f4b0b79b4f2bc6bc5dadce0e7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vbahelper/helperdecl.hxx>
21 #include <tools/urlobj.hxx>
22 #include <comphelper/unwrapargs.hxx>
23 #include <comphelper/servicehelper.hxx>
25 #include <com/sun/star/util/XModifiable.hpp>
26 #include <com/sun/star/util/XProtectable.hpp>
27 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
28 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
29 #include <com/sun/star/frame/XStorable.hpp>
30 #include <com/sun/star/frame/XFrame.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <ooo/vba/excel/XlFileFormat.hpp>
33 #include <ooo/vba/excel/XApplication.hpp>
35 #include "scextopt.hxx"
36 #include "vbaworksheet.hxx"
37 #include "vbaworksheets.hxx"
38 #include "vbaworkbook.hxx"
39 #include "vbawindows.hxx"
40 #include "vbastyles.hxx"
41 #include "excelvbahelper.hxx"
42 #include "vbapalette.hxx"
43 #include <osl/file.hxx>
44 #include <stdio.h>
45 #include "vbanames.hxx"
46 #include "nameuno.hxx"
47 #include "docoptio.hxx"
48 #include "unonames.hxx"
50 // Much of the impl. for the equivalend UNO module is
51 // sc/source/ui/unoobj/docuno.cxx, viewuno.cxx
53 using namespace ::ooo::vba;
54 using namespace ::com::sun::star;
56 uno::Sequence< sal_Int32 > ScVbaWorkbook::ColorData;
58 void ScVbaWorkbook::initColorData( const uno::Sequence< sal_Int32 >& sColors )
60 const sal_Int32* pSource = sColors.getConstArray();
61 sal_Int32* pDest = ColorData.getArray();
62 const sal_Int32* pEnd = pSource + sColors.getLength();
63 for ( ; pSource != pEnd; ++pSource, ++pDest )
64 *pDest = *pSource;
68 void SAL_CALL
69 ScVbaWorkbook::ResetColors( ) throw (::script::BasicErrorException, ::uno::RuntimeException)
71 uno::Reference< container::XIndexAccess > xIndexAccess( ScVbaPalette::getDefaultPalette(), uno::UNO_QUERY_THROW );
72 sal_Int32 nLen = xIndexAccess->getCount();
73 ColorData.realloc( nLen );
75 uno::Sequence< sal_Int32 > dDefaultColors( nLen );
76 sal_Int32* pDest = dDefaultColors.getArray();
77 for ( sal_Int32 index=0; index < nLen; ++pDest, ++index )
78 xIndexAccess->getByIndex( index ) >>= (*pDest);
79 initColorData( dDefaultColors );
82 ::uno::Any SAL_CALL
83 ScVbaWorkbook::Colors( const ::uno::Any& Index ) throw (::script::BasicErrorException, ::uno::RuntimeException)
85 uno::Any aRet;
86 if ( Index.getValue() )
88 sal_Int32 nIndex = 0;
89 Index >>= nIndex;
90 aRet = uno::makeAny( XLRGBToOORGB( ColorData[ --nIndex ] ) );
92 else
93 aRet = uno::makeAny( ColorData );
94 return aRet;
97 bool ScVbaWorkbook::setFilterPropsFromFormat( sal_Int32 nFormat, uno::Sequence< beans::PropertyValue >& rProps )
99 bool bRes = false;
100 for ( sal_Int32 index = 0; index < rProps.getLength(); ++index )
102 if ( rProps[ index ].Name == "FilterName" )
104 switch( nFormat )
106 case excel::XlFileFormat::xlCSV:
107 rProps[ index ].Value = uno::Any( OUString("Text - txt - csv (StarCalc)") );
108 break;
109 case excel::XlFileFormat::xlDBF4:
110 rProps[ index ].Value = uno::Any( OUString("DBF") );
111 break;
112 case excel::XlFileFormat::xlDIF:
113 rProps[ index ].Value = uno::Any( OUString("DIF") );
114 break;
115 case excel::XlFileFormat::xlWK3:
116 rProps[ index ].Value = uno::Any( OUString("Lotus") );
117 break;
118 case excel::XlFileFormat::xlExcel4Workbook:
119 rProps[ index ].Value = uno::Any( OUString("MS Excel 4.0") );
120 break;
121 case excel::XlFileFormat::xlExcel5:
122 rProps[ index ].Value = uno::Any( OUString("MS Excel 5.0/95") );
123 break;
124 case excel::XlFileFormat::xlHtml:
125 rProps[ index ].Value = uno::Any( OUString("HTML (StarCalc)") );
126 break;
127 case excel::XlFileFormat::xlExcel9795:
128 default:
129 rProps[ index ].Value = uno::Any( OUString("MS Excel 97") );
130 break;
132 bRes = true;
133 break;
136 return bRes;
139 ::sal_Int32 SAL_CALL
140 ScVbaWorkbook::getFileFormat( ) throw (::uno::RuntimeException)
142 sal_Int32 aFileFormat = 0;
143 OUString aFilterName;
144 uno::Sequence< beans::PropertyValue > aArgs = getModel()->getArgs();
146 // #FIXME - seems suspect should we not walk through the properties
147 // to find the FilterName
148 if ( aArgs[0].Name == "FilterName" ) {
149 aArgs[0].Value >>= aFilterName;
150 } else {
151 aArgs[1].Value >>= aFilterName;
154 if (aFilterName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Text - txt - csv (StarCalc)"))) {
155 aFileFormat = excel::XlFileFormat::xlCSV; //xlFileFormat.
158 if ( aFilterName == "DBF" ) {
159 aFileFormat = excel::XlFileFormat::xlDBF4;
162 if ( aFilterName == "DIF" ) {
163 aFileFormat = excel::XlFileFormat::xlDIF;
166 if ( aFilterName == "Lotus" ) {
167 aFileFormat = excel::XlFileFormat::xlWK3;
170 if ( aFilterName == "MS Excel 4.0" ) {
171 aFileFormat = excel::XlFileFormat::xlExcel4Workbook;
174 if ( aFilterName == "MS Excel 5.0/95" ) {
175 aFileFormat = excel::XlFileFormat::xlExcel5;
178 if ( aFilterName == "MS Excel 97" ) {
179 aFileFormat = excel::XlFileFormat::xlExcel9795;
182 if (aFilterName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("HTML (StarCalc)"))) {
183 aFileFormat = excel::XlFileFormat::xlHtml;
186 if ( aFilterName == "calc_StarOffice_XML_Calc_Template" ) {
187 aFileFormat = excel::XlFileFormat::xlTemplate;
190 if (aFilterName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("StarOffice XML (Calc)"))) {
191 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
193 if ( aFilterName == "calc8" ) {
194 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
197 return aFileFormat;
200 void
201 ScVbaWorkbook::init()
203 if ( !ColorData.getLength() )
204 ResetColors();
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 )
215 init();
218 uno::Reference< excel::XWorksheet >
219 ScVbaWorkbook::getActiveSheet() throw (uno::RuntimeException)
221 uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW );
222 uno::Reference< sheet::XSpreadsheetView > xView( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
223 uno::Reference< sheet::XSpreadsheet > xSheet( xView->getActiveSheet(), uno::UNO_SET_THROW );
224 // #162503# return the original sheet module wrapper object, instead of a new instance
225 uno::Reference< excel::XWorksheet > xWorksheet( excel::getUnoSheetModuleObj( xSheet ), uno::UNO_QUERY );
226 if( xWorksheet.is() ) return xWorksheet;
227 // #i116936# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
228 return new ScVbaWorksheet( this, mxContext, xSheet, xModel );
231 uno::Any SAL_CALL
232 ScVbaWorkbook::Sheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
234 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 void
268 ScVbaWorkbook::Protect( const uno::Any &aPassword ) throw (uno::RuntimeException)
270 VbaDocumentBase::Protect( aPassword );
273 ::sal_Bool
274 ScVbaWorkbook::getProtectStructure() throw (uno::RuntimeException)
276 uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
277 return xProt->isProtected();
280 ::sal_Bool SAL_CALL ScVbaWorkbook::getPrecisionAsDisplayed() throw (uno::RuntimeException)
282 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
283 ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
284 return pDoc->GetDocOptions().IsCalcAsShown();
287 void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed ) throw (uno::RuntimeException)
289 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
290 ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
291 ScDocOptions aOpt = pDoc->GetDocOptions();
292 aOpt.SetCalcAsShown( _precisionAsDisplayed );
293 pDoc->SetDocOptions( aOpt );
296 void
297 ScVbaWorkbook::SaveCopyAs( const OUString& sFileName ) throw ( uno::RuntimeException)
299 OUString aURL;
300 osl::FileBase::getFileURLFromSystemPath( sFileName, aURL );
301 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
302 uno::Sequence< beans::PropertyValue > storeProps(1);
303 storeProps[0].Name = OUString( "FilterName" );
304 storeProps[0].Value <<= OUString( "MS Excel 97" );
305 xStor->storeToURL( aURL, storeProps );
308 void SAL_CALL
309 ScVbaWorkbook::SaveAs( const uno::Any& FileName, const uno::Any& FileFormat, const uno::Any& /*Password*/, const uno::Any& /*WriteResPassword*/, const uno::Any& /*ReadOnlyRecommended*/, const uno::Any& /*CreateBackup*/, const uno::Any& /*AccessMode*/, const uno::Any& /*ConflictResolution*/, const uno::Any& /*AddToMru*/, const uno::Any& /*TextCodepage*/, const uno::Any& /*TextVisualLayout*/, const uno::Any& /*Local*/ ) throw (::com::sun::star::uno::RuntimeException)
311 OUString sFileName;
312 FileName >>= sFileName;
313 OUString sURL;
314 osl::FileBase::getFileURLFromSystemPath( sFileName, sURL );
315 // detect if there is no path if there is no path then we need
316 // to use the current current folder
317 INetURLObject aURL( sURL );
318 sURL = aURL.GetMainURL( INetURLObject::DECODE_TO_IURI );
319 if( sURL.isEmpty() )
321 // need to add cur dir ( of this workbook ) or else the 'Work' dir
322 sURL = getModel()->getURL();
324 if ( sURL.isEmpty() )
326 // not path available from 'this' document
327 // need to add the 'document'/work directory then
328 uno::Reference< excel::XApplication > xApplication ( Application(),uno::UNO_QUERY_THROW );
329 OUString sWorkPath = xApplication->getDefaultFilePath();
330 OUString sWorkURL;
331 osl::FileBase::getFileURLFromSystemPath( sWorkPath, sWorkURL );
332 aURL.SetURL( sWorkURL );
334 else
336 aURL.SetURL( sURL );
337 aURL.Append( sFileName );
339 sURL = aURL.GetMainURL( INetURLObject::DECODE_TO_IURI );
343 sal_Int32 nFileFormat = excel::XlFileFormat::xlExcel9795;
344 FileFormat >>= nFileFormat;
346 uno::Sequence< beans::PropertyValue > storeProps(1);
347 storeProps[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterName" ) );
349 setFilterPropsFromFormat( nFileFormat, storeProps );
351 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
352 OUString sFilterName;
353 storeProps[0].Value >>= sFilterName;
354 xStor->storeAsURL( sURL, storeProps );
357 css::uno::Any SAL_CALL
358 ScVbaWorkbook::Styles( const uno::Any& Item ) throw (uno::RuntimeException)
360 // quick look and Styles object doesn't seem to have a valid parent
361 // or a least the object browser just shows an object that has no
362 // variables ( therefore... leave as NULL for now )
363 uno::Reference< XCollection > dStyles = new ScVbaStyles( uno::Reference< XHelperInterface >(), mxContext, getModel() );
364 if ( Item.hasValue() )
365 return dStyles->Item( Item, uno::Any() );
366 return uno::makeAny( dStyles );
369 uno::Any SAL_CALL
370 ScVbaWorkbook::Names( const uno::Any& aIndex ) throw (uno::RuntimeException)
372 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
373 uno::Reference< beans::XPropertySet > xProps( xModel, uno::UNO_QUERY_THROW );
374 uno::Reference< sheet::XNamedRanges > xNamedRanges( xProps->getPropertyValue( OUString("NamedRanges") ), uno::UNO_QUERY_THROW );
375 uno::Reference< XCollection > xNames( new ScVbaNames( this, mxContext, xNamedRanges, xModel ) );
376 if ( aIndex.hasValue() )
377 return uno::Any( xNames->Item( aIndex, uno::Any() ) );
378 return uno::Any( xNames );
381 OUString
382 ScVbaWorkbook::getServiceImplName()
384 return OUString("ScVbaWorkbook");
387 uno::Sequence< OUString >
388 ScVbaWorkbook::getServiceNames()
390 static uno::Sequence< OUString > aServiceNames;
391 if ( aServiceNames.getLength() == 0 )
393 aServiceNames.realloc( 1 );
394 aServiceNames[ 0 ] = OUString("ooo.vba.excel.Workbook" );
396 return aServiceNames;
399 OUString SAL_CALL
400 ScVbaWorkbook::getCodeName() throw (css::uno::RuntimeException)
402 uno::Reference< beans::XPropertySet > xModelProp( getModel(), uno::UNO_QUERY_THROW );
403 return xModelProp->getPropertyValue( OUString( "CodeName" ) ).get< OUString >();
406 sal_Int64
407 ScVbaWorkbook::getSomething(const uno::Sequence<sal_Int8 >& rId ) throw(css::uno::RuntimeException)
409 if (rId.getLength() == 16 &&
410 0 == memcmp( ScVbaWorksheet::getUnoTunnelId().getConstArray(), rId.getConstArray(), 16 ))
412 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
414 return 0;
417 namespace workbook
419 namespace sdecl = comphelper::service_decl;
420 sdecl::vba_service_class_<ScVbaWorkbook, sdecl::with_args<true> > serviceImpl;
421 extern sdecl::ServiceDecl const serviceDecl(
422 serviceImpl,
423 "ScVbaWorkbook",
424 "ooo.vba.excel.Workbook" );
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */