1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
)
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
);
83 ScVbaWorkbook::Colors( const ::uno::Any
& Index
) throw (::script::BasicErrorException
, ::uno::RuntimeException
)
86 if ( Index
.getValue() )
90 aRet
= uno::makeAny( XLRGBToOORGB( ColorData
[ --nIndex
] ) );
93 aRet
= uno::makeAny( ColorData
);
97 bool ScVbaWorkbook::setFilterPropsFromFormat( sal_Int32 nFormat
, uno::Sequence
< beans::PropertyValue
>& rProps
)
100 for ( sal_Int32 index
= 0; index
< rProps
.getLength(); ++index
)
102 if ( rProps
[ index
].Name
== "FilterName" )
106 case excel::XlFileFormat::xlCSV
:
107 rProps
[ index
].Value
= uno::Any( OUString("Text - txt - csv (StarCalc)") );
109 case excel::XlFileFormat::xlDBF4
:
110 rProps
[ index
].Value
= uno::Any( OUString("DBF") );
112 case excel::XlFileFormat::xlDIF
:
113 rProps
[ index
].Value
= uno::Any( OUString("DIF") );
115 case excel::XlFileFormat::xlWK3
:
116 rProps
[ index
].Value
= uno::Any( OUString("Lotus") );
118 case excel::XlFileFormat::xlExcel4Workbook
:
119 rProps
[ index
].Value
= uno::Any( OUString("MS Excel 4.0") );
121 case excel::XlFileFormat::xlExcel5
:
122 rProps
[ index
].Value
= uno::Any( OUString("MS Excel 5.0/95") );
124 case excel::XlFileFormat::xlHtml
:
125 rProps
[ index
].Value
= uno::Any( OUString("HTML (StarCalc)") );
127 case excel::XlFileFormat::xlExcel9795
:
129 rProps
[ index
].Value
= uno::Any( OUString("MS Excel 97") );
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
;
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
;
201 ScVbaWorkbook::init()
203 if ( !ColorData
.getLength() )
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
)
212 ScVbaWorkbook::ScVbaWorkbook( uno::Sequence
< uno::Any
> const & args
,
213 uno::Reference
< uno::XComponentContext
> const & xContext
) : ScVbaWorkbook_BASE( args
, xContext
)
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
);
232 ScVbaWorkbook::Sheets( const uno::Any
& aIndex
) throw (uno::RuntimeException
)
234 return Worksheets( aIndex
);
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() ) );
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() ) );
262 ScVbaWorkbook::Activate() throw (uno::RuntimeException
)
264 VbaDocumentBase::Activate();
268 ScVbaWorkbook::Protect( const uno::Any
&aPassword
) throw (uno::RuntimeException
)
270 VbaDocumentBase::Protect( aPassword
);
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
);
297 ScVbaWorkbook::SaveCopyAs( const OUString
& sFileName
) throw ( uno::RuntimeException
)
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
);
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
)
312 FileName
>>= sFileName
;
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
);
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();
331 osl::FileBase::getFileURLFromSystemPath( sWorkPath
, sWorkURL
);
332 aURL
.SetURL( sWorkURL
);
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
);
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
);
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
;
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
>();
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));
419 namespace sdecl
= comphelper::service_decl
;
420 sdecl::vba_service_class_
<ScVbaWorkbook
, sdecl::with_args
<true> > serviceImpl
;
421 extern sdecl::ServiceDecl
const serviceDecl(
424 "ooo.vba.excel.Workbook" );
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */