fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / vba / vbaworkbook.cxx
blob1883e29d639b47ed89d2e7acac8f6055945654e3
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;
67 void SAL_CALL
68 ScVbaWorkbook::ResetColors( ) throw (::script::BasicErrorException, ::uno::RuntimeException, std::exception)
70 uno::Reference< container::XIndexAccess > xIndexAccess( ScVbaPalette::getDefaultPalette(), uno::UNO_QUERY_THROW );
71 sal_Int32 nLen = xIndexAccess->getCount();
72 ColorData.realloc( nLen );
74 uno::Sequence< sal_Int32 > dDefaultColors( nLen );
75 sal_Int32* pDest = dDefaultColors.getArray();
76 for ( sal_Int32 index=0; index < nLen; ++pDest, ++index )
77 xIndexAccess->getByIndex( index ) >>= (*pDest);
78 initColorData( dDefaultColors );
81 ::uno::Any SAL_CALL
82 ScVbaWorkbook::Colors( const ::uno::Any& Index ) throw (::script::BasicErrorException, ::uno::RuntimeException, std::exception)
84 uno::Any aRet;
85 if ( Index.getValue() )
87 sal_Int32 nIndex = 0;
88 Index >>= nIndex;
89 aRet = uno::makeAny( XLRGBToOORGB( ColorData[ --nIndex ] ) );
91 else
92 aRet = uno::makeAny( ColorData );
93 return aRet;
96 bool ScVbaWorkbook::setFilterPropsFromFormat( sal_Int32 nFormat, uno::Sequence< beans::PropertyValue >& rProps )
98 bool bRes = false;
99 for ( sal_Int32 index = 0; index < rProps.getLength(); ++index )
101 if ( rProps[ index ].Name == "FilterName" )
103 switch( nFormat )
105 case excel::XlFileFormat::xlCSV:
106 rProps[ index ].Value = uno::Any( OUString("Text - txt - csv (StarCalc)") );
107 break;
108 case excel::XlFileFormat::xlDBF4:
109 rProps[ index ].Value = uno::Any( OUString("DBF") );
110 break;
111 case excel::XlFileFormat::xlDIF:
112 rProps[ index ].Value = uno::Any( OUString("DIF") );
113 break;
114 case excel::XlFileFormat::xlWK3:
115 rProps[ index ].Value = uno::Any( OUString("Lotus") );
116 break;
117 case excel::XlFileFormat::xlExcel4Workbook:
118 rProps[ index ].Value = uno::Any( OUString("MS Excel 4.0") );
119 break;
120 case excel::XlFileFormat::xlExcel5:
121 rProps[ index ].Value = uno::Any( OUString("MS Excel 5.0/95") );
122 break;
123 case excel::XlFileFormat::xlHtml:
124 rProps[ index ].Value = uno::Any( OUString("HTML (StarCalc)") );
125 break;
126 case excel::XlFileFormat::xlExcel9795:
127 default:
128 rProps[ index ].Value = uno::Any( OUString("MS Excel 97") );
129 break;
131 bRes = true;
132 break;
135 return bRes;
138 ::sal_Int32 SAL_CALL
139 ScVbaWorkbook::getFileFormat( ) throw (::uno::RuntimeException, std::exception)
141 sal_Int32 aFileFormat = 0;
142 OUString aFilterName;
143 uno::Sequence< beans::PropertyValue > aArgs = getModel()->getArgs();
145 // #FIXME - seems suspect should we not walk through the properties
146 // to find the FilterName
147 if ( aArgs[0].Name == "FilterName" ) {
148 aArgs[0].Value >>= aFilterName;
149 } else {
150 aArgs[1].Value >>= aFilterName;
153 if (aFilterName == "Text - txt - csv (StarCalc)") {
154 aFileFormat = excel::XlFileFormat::xlCSV; //xlFileFormat.
157 if ( aFilterName == "DBF" ) {
158 aFileFormat = excel::XlFileFormat::xlDBF4;
161 if ( aFilterName == "DIF" ) {
162 aFileFormat = excel::XlFileFormat::xlDIF;
165 if ( aFilterName == "Lotus" ) {
166 aFileFormat = excel::XlFileFormat::xlWK3;
169 if ( aFilterName == "MS Excel 4.0" ) {
170 aFileFormat = excel::XlFileFormat::xlExcel4Workbook;
173 if ( aFilterName == "MS Excel 5.0/95" ) {
174 aFileFormat = excel::XlFileFormat::xlExcel5;
177 if ( aFilterName == "MS Excel 97" ) {
178 aFileFormat = excel::XlFileFormat::xlExcel9795;
181 if (aFilterName == "HTML (StarCalc)") {
182 aFileFormat = excel::XlFileFormat::xlHtml;
185 if ( aFilterName == "calc_StarOffice_XML_Calc_Template" ) {
186 aFileFormat = excel::XlFileFormat::xlTemplate;
189 if (aFilterName == "StarOffice XML (Calc)") {
190 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
192 if ( aFilterName == "calc8" ) {
193 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
196 return aFileFormat;
199 void
200 ScVbaWorkbook::init()
202 if ( !ColorData.getLength() )
203 ResetColors();
206 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 )
208 init();
211 ScVbaWorkbook::ScVbaWorkbook( uno::Sequence< uno::Any> const & args,
212 uno::Reference< uno::XComponentContext> const & xContext ) : ScVbaWorkbook_BASE( args, xContext )
214 init();
217 uno::Reference< excel::XWorksheet >
218 ScVbaWorkbook::getActiveSheet() throw (uno::RuntimeException, std::exception)
220 uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW );
221 uno::Reference< sheet::XSpreadsheetView > xView( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
222 uno::Reference< sheet::XSpreadsheet > xSheet( xView->getActiveSheet(), uno::UNO_SET_THROW );
223 // #162503# return the original sheet module wrapper object, instead of a new instance
224 uno::Reference< excel::XWorksheet > xWorksheet( excel::getUnoSheetModuleObj( xSheet ), uno::UNO_QUERY );
225 if( xWorksheet.is() ) return xWorksheet;
226 // #i116936# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
227 return new ScVbaWorksheet( this, mxContext, xSheet, xModel );
230 uno::Any SAL_CALL
231 ScVbaWorkbook::Sheets( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
233 return Worksheets( aIndex );
236 uno::Any SAL_CALL
237 ScVbaWorkbook::Worksheets( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
239 uno::Reference< frame::XModel > xModel( getModel() );
240 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xModel, uno::UNO_QUERY_THROW );
241 uno::Reference<container::XIndexAccess > xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
242 uno::Reference< XCollection > xWorkSheets( new ScVbaWorksheets( this, mxContext, xSheets, xModel ) );
243 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
245 return uno::Any( xWorkSheets );
247 // pass on to collection
248 return uno::Any( xWorkSheets->Item( aIndex, uno::Any() ) );
250 uno::Any SAL_CALL
251 ScVbaWorkbook::Windows( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
254 uno::Reference< excel::XWindows > xWindows( new ScVbaWindows( getParent(), mxContext ) );
255 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
256 return uno::Any( xWindows );
257 return uno::Any( xWindows->Item( aIndex, uno::Any() ) );
260 void SAL_CALL
261 ScVbaWorkbook::Activate() throw (uno::RuntimeException, std::exception)
263 VbaDocumentBase::Activate();
266 void
267 ScVbaWorkbook::Protect( const uno::Any &aPassword ) throw (uno::RuntimeException)
269 VbaDocumentBase::Protect( aPassword );
272 sal_Bool
273 ScVbaWorkbook::getProtectStructure() throw (uno::RuntimeException, std::exception)
275 uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
276 return xProt->isProtected();
279 sal_Bool SAL_CALL ScVbaWorkbook::getPrecisionAsDisplayed() throw (uno::RuntimeException, std::exception)
281 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
282 ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
283 return rDoc.GetDocOptions().IsCalcAsShown();
286 void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed ) throw (uno::RuntimeException, std::exception)
288 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
289 ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
290 ScDocOptions aOpt = rDoc.GetDocOptions();
291 aOpt.SetCalcAsShown( _precisionAsDisplayed );
292 rDoc.SetDocOptions( aOpt );
295 void
296 ScVbaWorkbook::SaveCopyAs( const OUString& sFileName ) throw ( uno::RuntimeException, std::exception)
298 OUString aURL;
299 osl::FileBase::getFileURLFromSystemPath( sFileName, aURL );
300 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
301 uno::Sequence< beans::PropertyValue > storeProps(1);
302 storeProps[0].Name = "FilterName";
303 storeProps[0].Value <<= OUString( "MS Excel 97" );
304 xStor->storeToURL( aURL, storeProps );
307 void SAL_CALL
308 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, std::exception)
310 OUString sFileName;
311 FileName >>= sFileName;
312 OUString sURL;
313 osl::FileBase::getFileURLFromSystemPath( sFileName, sURL );
314 // detect if there is no path if there is no path then we need
315 // to use the current current folder
316 INetURLObject aURL( sURL );
317 sURL = aURL.GetMainURL( INetURLObject::DECODE_TO_IURI );
318 if( sURL.isEmpty() )
320 // need to add cur dir ( of this workbook ) or else the 'Work' dir
321 sURL = getModel()->getURL();
323 if ( sURL.isEmpty() )
325 // not path available from 'this' document
326 // need to add the 'document'/work directory then
327 uno::Reference< excel::XApplication > xApplication ( Application(),uno::UNO_QUERY_THROW );
328 OUString sWorkPath = xApplication->getDefaultFilePath();
329 OUString sWorkURL;
330 osl::FileBase::getFileURLFromSystemPath( sWorkPath, sWorkURL );
331 aURL.SetURL( sWorkURL );
333 else
335 aURL.SetURL( sURL );
336 aURL.Append( sFileName );
338 sURL = aURL.GetMainURL( INetURLObject::DECODE_TO_IURI );
342 sal_Int32 nFileFormat = excel::XlFileFormat::xlExcel9795;
343 FileFormat >>= nFileFormat;
345 uno::Sequence< beans::PropertyValue > storeProps(1);
346 storeProps[0].Name = "FilterName" ;
348 setFilterPropsFromFormat( nFileFormat, storeProps );
350 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
351 OUString sFilterName;
352 storeProps[0].Value >>= sFilterName;
353 xStor->storeAsURL( sURL, storeProps );
356 css::uno::Any SAL_CALL
357 ScVbaWorkbook::Styles( const uno::Any& Item ) throw (::script::BasicErrorException, uno::RuntimeException, std::exception)
359 // quick look and Styles object doesn't seem to have a valid parent
360 // or a least the object browser just shows an object that has no
361 // variables ( therefore... leave as NULL for now )
362 uno::Reference< XCollection > dStyles = new ScVbaStyles( uno::Reference< XHelperInterface >(), mxContext, getModel() );
363 if ( Item.hasValue() )
364 return dStyles->Item( Item, uno::Any() );
365 return uno::makeAny( dStyles );
368 uno::Any SAL_CALL
369 ScVbaWorkbook::Names( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
371 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
372 uno::Reference< beans::XPropertySet > xProps( xModel, uno::UNO_QUERY_THROW );
373 uno::Reference< sheet::XNamedRanges > xNamedRanges( xProps->getPropertyValue("NamedRanges"), uno::UNO_QUERY_THROW );
374 uno::Reference< XCollection > xNames( new ScVbaNames( this, mxContext, xNamedRanges, xModel ) );
375 if ( aIndex.hasValue() )
376 return uno::Any( xNames->Item( aIndex, uno::Any() ) );
377 return uno::Any( xNames );
380 OUString
381 ScVbaWorkbook::getServiceImplName()
383 return OUString("ScVbaWorkbook");
386 uno::Sequence< OUString >
387 ScVbaWorkbook::getServiceNames()
389 static uno::Sequence< OUString > aServiceNames;
390 if ( aServiceNames.getLength() == 0 )
392 aServiceNames.realloc( 1 );
393 aServiceNames[ 0 ] = "ooo.vba.excel.Workbook";
395 return aServiceNames;
398 OUString SAL_CALL
399 ScVbaWorkbook::getCodeName() throw (css::uno::RuntimeException, std::exception)
401 uno::Reference< beans::XPropertySet > xModelProp( getModel(), uno::UNO_QUERY_THROW );
402 return xModelProp->getPropertyValue("CodeName").get< OUString >();
405 sal_Int64
406 ScVbaWorkbook::getSomething(const uno::Sequence<sal_Int8 >& rId ) throw(css::uno::RuntimeException, std::exception)
408 if (rId.getLength() == 16 &&
409 0 == memcmp( ScVbaWorksheet::getUnoTunnelId().getConstArray(), rId.getConstArray(), 16 ))
411 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
413 return 0;
416 namespace workbook
418 namespace sdecl = comphelper::service_decl;
419 sdecl::vba_service_class_<ScVbaWorkbook, sdecl::with_args<true> > serviceImpl;
420 extern sdecl::ServiceDecl const serviceDecl(
421 serviceImpl,
422 "ScVbaWorkbook",
423 "ooo.vba.excel.Workbook" );
426 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */