Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / ui / vba / vbaworkbook.cxx
blob7665a226ef9b3f770abc649e909c6c5cd12e17c1
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>
23 #include <com/sun/star/util/XProtectable.hpp>
24 #include <com/sun/star/sheet/XNamedRanges.hpp>
25 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
26 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
27 #include <com/sun/star/frame/XStorable.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <ooo/vba/excel/XlFileFormat.hpp>
30 #include <ooo/vba/excel/XApplication.hpp>
32 #include "service.hxx"
33 #include "vbaworksheet.hxx"
34 #include "vbaworksheets.hxx"
35 #include "vbaworkbook.hxx"
36 #include "vbawindows.hxx"
37 #include "vbastyles.hxx"
38 #include "excelvbahelper.hxx"
39 #include "vbapalette.hxx"
40 #include <osl/file.hxx>
41 #include "vbanames.hxx"
42 #include <docoptio.hxx>
43 #include <docsh.hxx>
45 // Much of the impl. for the equivalent UNO module is
46 // sc/source/ui/unoobj/docuno.cxx, viewuno.cxx
48 using namespace ::ooo::vba;
49 using namespace ::com::sun::star;
51 uno::Sequence< sal_Int32 > ScVbaWorkbook::ColorData;
53 void ScVbaWorkbook::initColorData( const uno::Sequence< sal_Int32 >& sColors )
55 std::copy(sColors.begin(), sColors.end(), ColorData.begin());
58 void SAL_CALL
59 ScVbaWorkbook::ResetColors( )
61 uno::Reference< container::XIndexAccess > xIndexAccess( ScVbaPalette::getDefaultPalette(), uno::UNO_SET_THROW );
62 sal_Int32 nLen = xIndexAccess->getCount();
63 ColorData.realloc( nLen );
65 uno::Sequence< sal_Int32 > dDefaultColors( nLen );
66 sal_Int32* pDest = dDefaultColors.getArray();
67 for ( sal_Int32 index=0; index < nLen; ++pDest, ++index )
68 xIndexAccess->getByIndex( index ) >>= *pDest;
69 initColorData( dDefaultColors );
72 ::uno::Any SAL_CALL
73 ScVbaWorkbook::Colors( const ::uno::Any& Index )
75 uno::Any aRet;
76 if ( Index.hasValue() )
78 sal_Int32 nIndex = 0;
79 Index >>= nIndex;
80 aRet <<= XLRGBToOORGB( ColorData[ --nIndex ] );
82 else
83 aRet <<= ColorData;
84 return aRet;
87 bool ScVbaWorkbook::setFilterPropsFromFormat( sal_Int32 nFormat, uno::Sequence< beans::PropertyValue >& rProps )
89 auto pProp = std::find_if(rProps.begin(), rProps.end(),
90 [](const beans::PropertyValue& rProp) { return rProp.Name == "FilterName"; });
91 bool bRes = pProp != rProps.end();
92 if (bRes)
94 switch( nFormat )
96 case excel::XlFileFormat::xlCSV:
97 pProp->Value <<= OUString(SC_TEXT_CSV_FILTER_NAME);
98 break;
99 case excel::XlFileFormat::xlDBF4:
100 pProp->Value <<= OUString("DBF");
101 break;
102 case excel::XlFileFormat::xlDIF:
103 pProp->Value <<= OUString("DIF");
104 break;
105 case excel::XlFileFormat::xlWK3:
106 pProp->Value <<= OUString("Lotus");
107 break;
108 case excel::XlFileFormat::xlExcel4Workbook:
109 pProp->Value <<= OUString("MS Excel 4.0");
110 break;
111 case excel::XlFileFormat::xlExcel5:
112 pProp->Value <<= OUString("MS Excel 5.0/95");
113 break;
114 case excel::XlFileFormat::xlHtml:
115 pProp->Value <<= OUString("HTML (StarCalc)");
116 break;
117 case excel::XlFileFormat::xlExcel9795:
118 default:
119 pProp->Value <<= OUString("MS Excel 97");
120 break;
123 return bRes;
126 ::sal_Int32 SAL_CALL
127 ScVbaWorkbook::getFileFormat( )
129 sal_Int32 aFileFormat = 0;
130 OUString aFilterName;
131 uno::Sequence< beans::PropertyValue > aArgs = getModel()->getArgs();
133 // #FIXME - seems suspect should we not walk through the properties
134 // to find the FilterName
135 if ( aArgs[0].Name == "FilterName" ) {
136 aArgs[0].Value >>= aFilterName;
137 } else {
138 aArgs[1].Value >>= aFilterName;
141 if (aFilterName == SC_TEXT_CSV_FILTER_NAME) {
142 aFileFormat = excel::XlFileFormat::xlCSV; //xlFileFormat.
145 if ( aFilterName == "DBF" ) {
146 aFileFormat = excel::XlFileFormat::xlDBF4;
149 if ( aFilterName == "DIF" ) {
150 aFileFormat = excel::XlFileFormat::xlDIF;
153 if ( aFilterName == "Lotus" ) {
154 aFileFormat = excel::XlFileFormat::xlWK3;
157 if ( aFilterName == "MS Excel 4.0" ) {
158 aFileFormat = excel::XlFileFormat::xlExcel4Workbook;
161 if ( aFilterName == "MS Excel 5.0/95" ) {
162 aFileFormat = excel::XlFileFormat::xlExcel5;
165 if ( aFilterName == "MS Excel 97" ) {
166 aFileFormat = excel::XlFileFormat::xlExcel9795;
169 if (aFilterName == "HTML (StarCalc)") {
170 aFileFormat = excel::XlFileFormat::xlHtml;
173 if ( aFilterName == "calc_StarOffice_XML_Calc_Template" ) {
174 aFileFormat = excel::XlFileFormat::xlTemplate;
177 if (aFilterName == "StarOffice XML (Calc)") {
178 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
180 if ( aFilterName == "calc8" ) {
181 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
184 return aFileFormat;
187 void
188 ScVbaWorkbook::init()
190 if ( !ColorData.hasElements() )
191 ResetColors();
192 uno::Reference< frame::XModel > xModel = getModel();
193 if ( xModel.is() )
194 excel::getDocShell( xModel )->RegisterAutomationWorkbookObject( this );
197 ScVbaWorkbook::ScVbaWorkbook( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, css::uno::Reference< css::frame::XModel > const & xModel ) : ScVbaWorkbook_BASE( xParent, xContext, xModel )
199 init();
202 ScVbaWorkbook::ScVbaWorkbook( uno::Sequence< uno::Any> const & args,
203 uno::Reference< uno::XComponentContext> const & xContext ) : ScVbaWorkbook_BASE( args, xContext )
205 init();
208 uno::Reference< excel::XWorksheet >
209 ScVbaWorkbook::getActiveSheet()
211 uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW );
212 uno::Reference< sheet::XSpreadsheetView > xView( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
213 uno::Reference< sheet::XSpreadsheet > xSheet( xView->getActiveSheet(), uno::UNO_SET_THROW );
214 // #162503# return the original sheet module wrapper object, instead of a new instance
215 uno::Reference< excel::XWorksheet > xWorksheet( excel::getUnoSheetModuleObj( xSheet ), uno::UNO_QUERY );
216 if( xWorksheet.is() ) return xWorksheet;
217 // #i116936# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
218 return new ScVbaWorksheet( this, mxContext, xSheet, xModel );
221 uno::Any SAL_CALL
222 ScVbaWorkbook::Sheets( const uno::Any& aIndex )
224 return Worksheets( aIndex );
227 uno::Any SAL_CALL
228 ScVbaWorkbook::Worksheets( const uno::Any& aIndex )
230 uno::Reference< frame::XModel > xModel( getModel() );
231 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xModel, uno::UNO_QUERY_THROW );
232 uno::Reference<container::XIndexAccess > xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
233 uno::Reference< XCollection > xWorkSheets( new ScVbaWorksheets( this, mxContext, xSheets, xModel ) );
234 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
236 return uno::Any( xWorkSheets );
238 // pass on to collection
239 return xWorkSheets->Item( aIndex, uno::Any() );
241 uno::Any SAL_CALL
242 ScVbaWorkbook::Windows( const uno::Any& aIndex )
245 uno::Reference< excel::XWindows > xWindows( new ScVbaWindows( getParent(), mxContext ) );
246 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
247 return uno::Any( xWindows );
248 return xWindows->Item( aIndex, uno::Any() );
251 void SAL_CALL
252 ScVbaWorkbook::Activate()
254 VbaDocumentBase::Activate();
257 void
258 ScVbaWorkbook::Protect( const uno::Any &aPassword )
260 VbaDocumentBase::Protect( aPassword );
263 sal_Bool
264 ScVbaWorkbook::getProtectStructure()
266 uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
267 return xProt->isProtected();
270 sal_Bool SAL_CALL ScVbaWorkbook::getPrecisionAsDisplayed()
272 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
273 ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
274 return rDoc.GetDocOptions().IsCalcAsShown();
277 void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed )
279 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
280 ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
281 ScDocOptions aOpt = rDoc.GetDocOptions();
282 aOpt.SetCalcAsShown( _precisionAsDisplayed );
283 rDoc.SetDocOptions( aOpt );
286 OUString SAL_CALL ScVbaWorkbook::getAuthor()
288 uno::Reference<document::XDocumentPropertiesSupplier> xDPS( getModel(), uno::UNO_QUERY );
289 if (!xDPS.is())
290 return "?";
291 uno::Reference<document::XDocumentProperties> xDocProps = xDPS->getDocumentProperties();
292 return xDocProps->getAuthor();
295 void SAL_CALL ScVbaWorkbook::setAuthor( const OUString& _author )
297 uno::Reference<document::XDocumentPropertiesSupplier> xDPS( getModel(), uno::UNO_QUERY );
298 if (!xDPS.is())
299 return;
300 uno::Reference<document::XDocumentProperties> xDocProps = xDPS->getDocumentProperties();
301 xDocProps->setAuthor( _author );
304 void
305 ScVbaWorkbook::SaveCopyAs( const OUString& sFileName )
307 OUString aURL;
308 osl::FileBase::getFileURLFromSystemPath( sFileName, aURL );
309 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
310 uno::Sequence< beans::PropertyValue > storeProps(1);
311 storeProps[0].Name = "FilterName";
312 storeProps[0].Value <<= OUString( "MS Excel 97" );
313 xStor->storeToURL( aURL, storeProps );
316 void SAL_CALL
317 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*/ )
319 OUString sFileName;
320 FileName >>= sFileName;
321 OUString sURL;
322 osl::FileBase::getFileURLFromSystemPath( sFileName, sURL );
323 // detect if there is no path then we need
324 // to use the current folder
325 INetURLObject aURL( sURL );
326 sURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
327 if( sURL.isEmpty() )
329 // need to add cur dir ( of this workbook ) or else the 'Work' dir
330 sURL = getModel()->getURL();
332 if ( sURL.isEmpty() )
334 // not path available from 'this' document
335 // need to add the 'document'/work directory then
336 uno::Reference< excel::XApplication > xApplication ( Application(),uno::UNO_QUERY_THROW );
337 OUString sWorkPath = xApplication->getDefaultFilePath();
338 OUString sWorkURL;
339 osl::FileBase::getFileURLFromSystemPath( sWorkPath, sWorkURL );
340 aURL.SetURL( sWorkURL );
342 else
344 aURL.SetURL( sURL );
345 aURL.Append( sFileName );
347 sURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
351 sal_Int32 nFileFormat = excel::XlFileFormat::xlExcel9795;
352 FileFormat >>= nFileFormat;
354 uno::Sequence< beans::PropertyValue > storeProps(1);
355 storeProps[0].Name = "FilterName" ;
357 setFilterPropsFromFormat( nFileFormat, storeProps );
359 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
360 xStor->storeAsURL( sURL, storeProps );
363 css::uno::Any SAL_CALL
364 ScVbaWorkbook::Styles( const uno::Any& Item )
366 // quick look and Styles object doesn't seem to have a valid parent
367 // or a least the object browser just shows an object that has no
368 // variables ( therefore... leave as NULL for now )
369 uno::Reference< XCollection > dStyles = new ScVbaStyles( uno::Reference< XHelperInterface >(), mxContext, getModel() );
370 if ( Item.hasValue() )
371 return dStyles->Item( Item, uno::Any() );
372 return uno::makeAny( dStyles );
375 uno::Any SAL_CALL
376 ScVbaWorkbook::Names( const uno::Any& aIndex )
378 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
379 uno::Reference< beans::XPropertySet > xProps( xModel, uno::UNO_QUERY_THROW );
380 uno::Reference< sheet::XNamedRanges > xNamedRanges( xProps->getPropertyValue("NamedRanges"), uno::UNO_QUERY_THROW );
381 uno::Reference< XCollection > xNames( new ScVbaNames( this, mxContext, xNamedRanges, xModel ) );
382 if ( aIndex.hasValue() )
383 return xNames->Item( aIndex, uno::Any() );
384 return uno::Any( xNames );
387 OUString
388 ScVbaWorkbook::getServiceImplName()
390 return "ScVbaWorkbook";
393 uno::Sequence< OUString >
394 ScVbaWorkbook::getServiceNames()
396 static uno::Sequence< OUString > const aServiceNames
398 "ooo.vba.excel.Workbook"
400 return aServiceNames;
403 OUString SAL_CALL
404 ScVbaWorkbook::getCodeName()
406 uno::Reference< beans::XPropertySet > xModelProp( getModel(), uno::UNO_QUERY_THROW );
407 return xModelProp->getPropertyValue("CodeName").get< OUString >();
410 sal_Int64
411 ScVbaWorkbook::getSomething(const uno::Sequence<sal_Int8 >& rId )
413 if (isUnoTunnelId<ScVbaWorksheet>(rId))
415 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
417 return 0;
420 namespace workbook
422 namespace sdecl = comphelper::service_decl;
423 sdecl::vba_service_class_<ScVbaWorkbook, sdecl::with_args<true> > const serviceImpl;
424 sdecl::ServiceDecl const serviceDecl(
425 serviceImpl,
426 "ScVbaWorkbook",
427 "ooo.vba.excel.Workbook" );
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */