tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / vba / vbaworkbook.cxx
blob0313c86c12a854b58d90783edecdae44d60a8dab
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 <sal/config.h>
22 #include <comphelper/propertyvalue.hxx>
23 #include <tools/urlobj.hxx>
25 #include <com/sun/star/util/XProtectable.hpp>
26 #include <com/sun/star/sheet/XNamedRanges.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/beans/XPropertySet.hpp>
31 #include <ooo/vba/excel/XlFileFormat.hpp>
32 #include <ooo/vba/excel/XApplication.hpp>
34 #include "vbaworksheet.hxx"
35 #include "vbaworksheets.hxx"
36 #include "vbaworkbook.hxx"
37 #include "vbawindows.hxx"
38 #include "vbastyles.hxx"
39 #include "excelvbahelper.hxx"
40 #include "vbapalette.hxx"
41 #include <osl/file.hxx>
42 #include "vbanames.hxx"
43 #include <docoptio.hxx>
44 #include <docsh.hxx>
46 // Much of the impl. for the equivalent UNO module is
47 // sc/source/ui/unoobj/docuno.cxx, viewuno.cxx
49 using namespace ::ooo::vba;
50 using namespace ::com::sun::star;
52 uno::Sequence< sal_Int32 > ScVbaWorkbook::ColorData;
54 void SAL_CALL
55 ScVbaWorkbook::ResetColors( )
57 uno::Reference< container::XIndexAccess > xIndexAccess( ScVbaPalette::getDefaultPalette(), uno::UNO_SET_THROW );
58 sal_Int32 nLen = xIndexAccess->getCount();
59 ColorData.realloc( nLen );
61 sal_Int32* pDest = ColorData.getArray();
62 for ( sal_Int32 index=0; index < nLen; ++pDest, ++index )
63 xIndexAccess->getByIndex( index ) >>= *pDest;
66 ::uno::Any SAL_CALL
67 ScVbaWorkbook::Colors( const ::uno::Any& Index )
69 uno::Any aRet;
70 if ( Index.hasValue() )
72 sal_Int32 nIndex = 0;
73 Index >>= nIndex;
74 aRet <<= XLRGBToOORGB( ColorData[ --nIndex ] );
76 else
77 aRet <<= ColorData;
78 return aRet;
81 bool ScVbaWorkbook::setFilterPropsFromFormat( sal_Int32 nFormat, uno::Sequence< beans::PropertyValue >& rProps )
83 auto [begin, end] = asNonConstRange(rProps);
84 auto pProp = std::find_if(begin, end,
85 [](const beans::PropertyValue& rProp) { return rProp.Name == "FilterName"; });
86 bool bRes = pProp != end;
87 if (bRes)
89 switch( nFormat )
91 case excel::XlFileFormat::xlCSV:
92 pProp->Value <<= SC_TEXT_CSV_FILTER_NAME;
93 break;
94 case excel::XlFileFormat::xlDBF4:
95 pProp->Value <<= u"DBF"_ustr;
96 break;
97 case excel::XlFileFormat::xlDIF:
98 pProp->Value <<= u"DIF"_ustr;
99 break;
100 case excel::XlFileFormat::xlWK3:
101 pProp->Value <<= u"Lotus"_ustr;
102 break;
103 case excel::XlFileFormat::xlExcel4Workbook:
104 pProp->Value <<= u"MS Excel 4.0"_ustr;
105 break;
106 case excel::XlFileFormat::xlExcel5:
107 pProp->Value <<= u"MS Excel 5.0/95"_ustr;
108 break;
109 case excel::XlFileFormat::xlHtml:
110 pProp->Value <<= u"HTML (StarCalc)"_ustr;
111 break;
112 case excel::XlFileFormat::xlExcel9795:
113 default:
114 pProp->Value <<= u"MS Excel 97"_ustr;
115 break;
118 return bRes;
121 ::sal_Int32 SAL_CALL
122 ScVbaWorkbook::getFileFormat( )
124 sal_Int32 aFileFormat = 0;
125 OUString aFilterName;
126 uno::Sequence< beans::PropertyValue > aArgs = getModel()->getArgs();
128 // #FIXME - seems suspect should we not walk through the properties
129 // to find the FilterName
130 if ( aArgs[0].Name == "FilterName" ) {
131 aArgs[0].Value >>= aFilterName;
132 } else {
133 aArgs[1].Value >>= aFilterName;
136 if (aFilterName == SC_TEXT_CSV_FILTER_NAME) {
137 aFileFormat = excel::XlFileFormat::xlCSV; //xlFileFormat.
140 if ( aFilterName == "DBF" ) {
141 aFileFormat = excel::XlFileFormat::xlDBF4;
144 if ( aFilterName == "DIF" ) {
145 aFileFormat = excel::XlFileFormat::xlDIF;
148 if ( aFilterName == "Lotus" ) {
149 aFileFormat = excel::XlFileFormat::xlWK3;
152 if ( aFilterName == "MS Excel 4.0" ) {
153 aFileFormat = excel::XlFileFormat::xlExcel4Workbook;
156 if ( aFilterName == "MS Excel 5.0/95" ) {
157 aFileFormat = excel::XlFileFormat::xlExcel5;
160 if ( aFilterName == "MS Excel 97" ) {
161 aFileFormat = excel::XlFileFormat::xlExcel9795;
164 if (aFilterName == "HTML (StarCalc)") {
165 aFileFormat = excel::XlFileFormat::xlHtml;
168 if ( aFilterName == "calc_StarOffice_XML_Calc_Template" ) {
169 aFileFormat = excel::XlFileFormat::xlTemplate;
172 if (aFilterName == "StarOffice XML (Calc)") {
173 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
175 if ( aFilterName == "calc8" ) {
176 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
179 return aFileFormat;
182 void
183 ScVbaWorkbook::init()
185 if ( !ColorData.hasElements() )
186 ResetColors();
187 if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
188 pShell->RegisterAutomationWorkbookObject( this );
191 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 )
193 init();
196 ScVbaWorkbook::ScVbaWorkbook( uno::Sequence< uno::Any> const & args,
197 uno::Reference< uno::XComponentContext> const & xContext ) : ScVbaWorkbook_BASE( args, xContext )
199 init();
202 uno::Reference< excel::XWorksheet >
203 ScVbaWorkbook::getActiveSheet()
205 uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW );
206 uno::Reference< sheet::XSpreadsheetView > xView( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
207 uno::Reference< sheet::XSpreadsheet > xSheet( xView->getActiveSheet(), uno::UNO_SET_THROW );
208 // #162503# return the original sheet module wrapper object, instead of a new instance
209 uno::Reference< excel::XWorksheet > xWorksheet( excel::getUnoSheetModuleObj( xSheet ), uno::UNO_QUERY );
210 if( xWorksheet.is() ) return xWorksheet;
211 // #i116936# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
212 return new ScVbaWorksheet( this, mxContext, xSheet, xModel );
215 uno::Any SAL_CALL
216 ScVbaWorkbook::Sheets( const uno::Any& aIndex )
218 return Worksheets( aIndex );
221 uno::Any SAL_CALL
222 ScVbaWorkbook::Worksheets( const uno::Any& aIndex )
224 uno::Reference< frame::XModel > xModel( getModel() );
225 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xModel, uno::UNO_QUERY_THROW );
226 uno::Reference<container::XIndexAccess > xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
227 uno::Reference< XCollection > xWorkSheets( new ScVbaWorksheets( this, mxContext, xSheets, xModel ) );
228 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
230 return uno::Any( xWorkSheets );
232 // pass on to collection
233 return xWorkSheets->Item( aIndex, uno::Any() );
235 uno::Any SAL_CALL
236 ScVbaWorkbook::Windows( const uno::Any& aIndex )
239 uno::Reference< excel::XWindows > xWindows( new ScVbaWindows( getParent(), mxContext ) );
240 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
241 return uno::Any( xWindows );
242 return xWindows->Item( aIndex, uno::Any() );
245 void SAL_CALL
246 ScVbaWorkbook::Activate()
248 VbaDocumentBase::Activate();
251 void
252 ScVbaWorkbook::Protect( const uno::Any &aPassword )
254 VbaDocumentBase::Protect( aPassword );
257 sal_Bool
258 ScVbaWorkbook::getProtectStructure()
260 uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
261 return xProt->isProtected();
264 sal_Bool SAL_CALL ScVbaWorkbook::getPrecisionAsDisplayed()
266 if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
268 ScDocument& rDoc = pShell->GetDocument();
269 return rDoc.GetDocOptions().IsCalcAsShown();
271 return false;
274 void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed )
276 if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
278 ScDocument& rDoc = pShell->GetDocument();
279 ScDocOptions aOpt = rDoc.GetDocOptions();
280 aOpt.SetCalcAsShown( _precisionAsDisplayed );
281 rDoc.SetDocOptions( aOpt );
285 OUString SAL_CALL ScVbaWorkbook::getAuthor()
287 uno::Reference<document::XDocumentPropertiesSupplier> xDPS( getModel(), uno::UNO_QUERY );
288 if (!xDPS.is())
289 return u"?"_ustr;
290 uno::Reference<document::XDocumentProperties> xDocProps = xDPS->getDocumentProperties();
291 return xDocProps->getAuthor();
294 void SAL_CALL ScVbaWorkbook::setAuthor( const OUString& _author )
296 uno::Reference<document::XDocumentPropertiesSupplier> xDPS( getModel(), uno::UNO_QUERY );
297 if (!xDPS.is())
298 return;
299 uno::Reference<document::XDocumentProperties> xDocProps = xDPS->getDocumentProperties();
300 xDocProps->setAuthor( _author );
303 void
304 ScVbaWorkbook::SaveCopyAs( const OUString& sFileName )
306 OUString aURL;
307 osl::FileBase::getFileURLFromSystemPath( sFileName, aURL );
308 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
309 uno::Sequence< beans::PropertyValue > storeProps{ comphelper::makePropertyValue(
310 u"FilterName"_ustr, u"MS Excel 97"_ustr) };
311 xStor->storeToURL( aURL, storeProps );
314 void SAL_CALL
315 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*/ )
317 OUString sFileName;
318 FileName >>= sFileName;
319 OUString sURL;
320 osl::FileBase::getFileURLFromSystemPath( sFileName, sURL );
321 // detect if there is no path then we need
322 // to use the current folder
323 INetURLObject aURL( sURL );
324 sURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
325 if( sURL.isEmpty() )
327 // need to add cur dir ( of this workbook ) or else the 'Work' dir
328 sURL = getModel()->getURL();
330 if ( sURL.isEmpty() )
332 // not path available from 'this' document
333 // need to add the 'document'/work directory then
334 uno::Reference< excel::XApplication > xApplication ( Application(),uno::UNO_QUERY_THROW );
335 OUString sWorkPath = xApplication->getDefaultFilePath();
336 OUString sWorkURL;
337 osl::FileBase::getFileURLFromSystemPath( sWorkPath, sWorkURL );
338 aURL.SetURL( sWorkURL );
340 else
342 aURL.SetURL( sURL );
343 aURL.Append( sFileName );
345 sURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
349 sal_Int32 nFileFormat = excel::XlFileFormat::xlExcel9795;
350 FileFormat >>= nFileFormat;
352 uno::Sequence storeProps{ comphelper::makePropertyValue(u"FilterName"_ustr, uno::Any()) };
353 setFilterPropsFromFormat( nFileFormat, storeProps );
355 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
356 xStor->storeAsURL( sURL, storeProps );
359 void SAL_CALL
360 ScVbaWorkbook::ExportAsFixedFormat(const css::uno::Any& Type, const css::uno::Any& FileName, const css::uno::Any& Quality,
361 const css::uno::Any& IncludeDocProperties, const css::uno::Any& /*IgnorePrintAreas*/, const css::uno::Any& From,
362 const css::uno::Any& To, const css::uno::Any& OpenAfterPublish, const css::uno::Any& /*FixedFormatExtClassPtr*/)
364 uno::Reference< frame::XModel > xModel(getModel(), uno::UNO_SET_THROW);
365 uno::Reference< excel::XApplication > xApplication(Application(), uno::UNO_QUERY_THROW);
367 excel::ExportAsFixedFormatHelper(xModel, xApplication, Type, FileName, Quality,
368 IncludeDocProperties, From, To, OpenAfterPublish);
371 css::uno::Any SAL_CALL
372 ScVbaWorkbook::Styles( const uno::Any& Item )
374 // quick look and Styles object doesn't seem to have a valid parent
375 // or a least the object browser just shows an object that has no
376 // variables ( therefore... leave as NULL for now )
377 uno::Reference< XCollection > dStyles = new ScVbaStyles( uno::Reference< XHelperInterface >(), mxContext, getModel() );
378 if ( Item.hasValue() )
379 return dStyles->Item( Item, uno::Any() );
380 return uno::Any( dStyles );
383 uno::Any SAL_CALL
384 ScVbaWorkbook::Names( const uno::Any& aIndex )
386 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
387 uno::Reference< beans::XPropertySet > xProps( xModel, uno::UNO_QUERY_THROW );
388 uno::Reference< sheet::XNamedRanges > xNamedRanges( xProps->getPropertyValue(u"NamedRanges"_ustr), uno::UNO_QUERY_THROW );
389 uno::Reference< XCollection > xNames( new ScVbaNames( this, mxContext, xNamedRanges, xModel ) );
390 if ( aIndex.hasValue() )
391 return xNames->Item( aIndex, uno::Any() );
392 return uno::Any( xNames );
395 OUString
396 ScVbaWorkbook::getServiceImplName()
398 return u"ScVbaWorkbook"_ustr;
401 uno::Sequence< OUString >
402 ScVbaWorkbook::getServiceNames()
404 static uno::Sequence< OUString > const aServiceNames
406 u"ooo.vba.excel.Workbook"_ustr
408 return aServiceNames;
411 OUString SAL_CALL
412 ScVbaWorkbook::getCodeName()
414 uno::Reference< beans::XPropertySet > xModelProp( getModel(), uno::UNO_QUERY_THROW );
415 return xModelProp->getPropertyValue(u"CodeName"_ustr).get< OUString >();
418 sal_Int64
419 ScVbaWorkbook::getSomething(const uno::Sequence<sal_Int8 >& rId )
421 if (comphelper::isUnoTunnelId<ScVbaWorksheet>(rId)) // ???
423 return comphelper::getSomething_cast(this);
425 return 0;
428 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
429 Calc_ScVbaWorkbook_get_implementation(
430 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
432 return cppu::acquire(new ScVbaWorkbook(args, context));
436 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */