1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbaworkbook.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <vbahelper/helperdecl.hxx>
31 #include <tools/urlobj.hxx>
32 #include <comphelper/unwrapargs.hxx>
34 #include <com/sun/star/util/XModifiable.hpp>
35 #include <com/sun/star/util/XProtectable.hpp>
36 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
37 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
38 #include <com/sun/star/frame/XStorable.hpp>
39 #include <com/sun/star/frame/XFrame.hpp>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <ooo/vba/excel/XlFileFormat.hpp>
43 #include "scextopt.hxx"
44 #include "vbaworksheet.hxx"
45 #include "vbaworksheets.hxx"
46 #include "vbaworkbook.hxx"
47 #include "vbawindows.hxx"
48 #include "vbastyles.hxx"
49 #include "excelvbahelper.hxx"
50 #include "vbapalette.hxx"
51 #include <osl/file.hxx>
53 #include "vbanames.hxx" // Amelia Wang
54 #include "nameuno.hxx"
56 // Much of the impl. for the equivalend UNO module is
57 // sc/source/ui/unoobj/docuno.cxx, viewuno.cxx
59 using namespace ::ooo::vba
;
60 using namespace ::com::sun::star
;
62 class ActiveSheet
: public ScVbaWorksheet
65 virtual uno::Reference
< frame::XModel
> getModel()
67 return getCurrentExcelDoc();
69 virtual uno::Reference
< sheet::XSpreadsheet
> getSheet()
71 uno::Reference
< frame::XModel
> xModel
= getModel();
72 uno::Reference
< sheet::XSpreadsheet
> xSheet
;
75 uno::Reference
< sheet::XSpreadsheetView
> xSpreadsheet(
76 xModel
->getCurrentController(), uno::UNO_QUERY
);
77 if ( xSpreadsheet
.is() )
78 xSheet
= xSpreadsheet
->getActiveSheet();
83 ActiveSheet( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
) : ScVbaWorksheet( xParent
, xContext
) {}
87 uno::Sequence
< sal_Int32
> ScVbaWorkbook::ColorData
;
89 void ScVbaWorkbook::initColorData( const uno::Sequence
< sal_Int32
>& sColors
)
91 const sal_Int32
* pSource
= sColors
.getConstArray();
92 sal_Int32
* pDest
= ColorData
.getArray();
93 const sal_Int32
* pEnd
= pSource
+ sColors
.getLength();
94 for ( ; pSource
!= pEnd
; ++pSource
, ++pDest
)
100 ScVbaWorkbook::ResetColors( ) throw (::script::BasicErrorException
, ::uno::RuntimeException
)
102 uno::Reference
< container::XIndexAccess
> xIndexAccess( ScVbaPalette::getDefaultPalette(), uno::UNO_QUERY_THROW
);
103 sal_Int32 nLen
= xIndexAccess
->getCount();
104 ColorData
.realloc( nLen
);
106 uno::Sequence
< sal_Int32
> dDefaultColors( nLen
);
107 sal_Int32
* pDest
= dDefaultColors
.getArray();
108 for ( sal_Int32 index
=0; index
< nLen
; ++pDest
, ++index
)
109 xIndexAccess
->getByIndex( index
) >>= (*pDest
);
110 initColorData( dDefaultColors
);
114 ScVbaWorkbook::Colors( const ::uno::Any
& Index
) throw (::script::BasicErrorException
, ::uno::RuntimeException
)
117 if ( Index
.getValue() )
119 sal_Int32 nIndex
= 0;
121 aRet
= uno::makeAny( XLRGBToOORGB( ColorData
[ --nIndex
] ) );
124 aRet
= uno::makeAny( ColorData
);
129 ScVbaWorkbook::FileFormat( ) throw (::script::BasicErrorException
, ::uno::RuntimeException
)
131 sal_Int32 aFileFormat
= 0;
132 rtl::OUString aFilterName
;
133 uno::Sequence
< beans::PropertyValue
> aArgs
= getModel()->getArgs();
135 // #FIXME - seems suspect should we not walk through the properties
136 // to find the FilterName
137 if (aArgs
[0].Name
.equalsAscii( "FilterName")) {
138 aArgs
[0].Value
>>= aFilterName
;
140 aArgs
[1].Value
>>= aFilterName
;
143 if (aFilterName
.equalsAscii("Text - txt - csv (StarCalc)")) {
144 aFileFormat
= excel::XlFileFormat::xlCSV
; //xlFileFormat.
147 if (aFilterName
.equalsAscii("DBF")) {
148 aFileFormat
= excel::XlFileFormat::xlDBF4
;
151 if (aFilterName
.equalsAscii("DIF")) {
152 aFileFormat
= excel::XlFileFormat::xlDIF
;
155 if (aFilterName
.equalsAscii("Lotus")) {
156 aFileFormat
= excel::XlFileFormat::xlWK3
;
159 if (aFilterName
.equalsAscii("MS Excel 4.0")) {
160 aFileFormat
= excel::XlFileFormat::xlExcel4Workbook
;
163 if (aFilterName
.equalsAscii("MS Excel 5.0/95")) {
164 aFileFormat
= excel::XlFileFormat::xlExcel5
;
167 if (aFilterName
.equalsAscii("MS Excel 97")) {
168 aFileFormat
= excel::XlFileFormat::xlExcel9795
;
171 if (aFilterName
.equalsAscii("HTML (StarCalc)")) {
172 aFileFormat
= excel::XlFileFormat::xlHtml
;
175 if (aFilterName
.equalsAscii("calc_StarOffice_XML_Calc_Template")) {
176 aFileFormat
= excel::XlFileFormat::xlTemplate
;
179 if (aFilterName
.equalsAscii("StarOffice XML (Calc)")) {
180 aFileFormat
= excel::XlFileFormat::xlWorkbookNormal
;
182 if (aFilterName
.equalsAscii("calc8")) {
183 aFileFormat
= excel::XlFileFormat::xlWorkbookNormal
;
190 ScVbaWorkbook::init()
192 if ( !ColorData
.getLength() )
195 ScVbaWorkbook::ScVbaWorkbook( const css::uno::Reference
< ov::XHelperInterface
>& xParent
, const css::uno::Reference
< css::uno::XComponentContext
>& xContext
) :ScVbaWorkbook_BASE( xParent
, xContext
)
197 //#FIXME this persists the color data per office instance and
198 // not per workbook instance, need to hook the data into XModel
199 // ( e.g. we already store the imported palette in there )
201 // a) make the class that does that a service
202 // b) make that service implement XIndexContainer
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
)
211 ScVbaWorkbook::ScVbaWorkbook( uno::Sequence
< uno::Any
> const & args
,
212 uno::Reference
< uno::XComponentContext
> const & xContext
) : ScVbaWorkbook_BASE( args
, xContext
)
218 uno::Reference
< excel::XWorksheet
>
219 ScVbaWorkbook::getActiveSheet() throw (uno::RuntimeException
)
221 return new ActiveSheet( this, mxContext
);
224 ScVbaWorkbook::Sheets( const uno::Any
& aIndex
) throw (uno::RuntimeException
)
226 return Worksheets( aIndex
);
232 ScVbaWorkbook::Worksheets( const uno::Any
& aIndex
) throw (uno::RuntimeException
)
234 uno::Reference
< frame::XModel
> xModel( getModel() );
235 uno::Reference
<sheet::XSpreadsheetDocument
> xSpreadDoc( xModel
, uno::UNO_QUERY_THROW
);
236 uno::Reference
<container::XIndexAccess
> xSheets( xSpreadDoc
->getSheets(), uno::UNO_QUERY_THROW
);
237 uno::Reference
< XCollection
> xWorkSheets( new ScVbaWorksheets( this, mxContext
, xSheets
, xModel
) );
238 if ( aIndex
.getValueTypeClass() == uno::TypeClass_VOID
)
240 return uno::Any( xWorkSheets
);
242 // pass on to collection
243 return uno::Any( xWorkSheets
->Item( aIndex
, uno::Any() ) );
246 ScVbaWorkbook::Windows( const uno::Any
& aIndex
) throw (uno::RuntimeException
)
249 uno::Reference
< excel::XWindows
> xWindows( new ScVbaWindows( getParent(), mxContext
) );
250 if ( aIndex
.getValueTypeClass() == uno::TypeClass_VOID
)
251 return uno::Any( xWindows
);
252 return uno::Any( xWindows
->Item( aIndex
, uno::Any() ) );
256 ScVbaWorkbook::Activate() throw (uno::RuntimeException
)
258 VbaDocumentBase::Activate();
262 ScVbaWorkbook::getProtectStructure() throw (uno::RuntimeException
)
264 uno::Reference
< util::XProtectable
> xProt( getModel(), uno::UNO_QUERY_THROW
);
265 return xProt
->isProtected();
269 ScVbaWorkbook::SaveCopyAs( const rtl::OUString
& sFileName
) throw ( uno::RuntimeException
)
272 osl::FileBase::getFileURLFromSystemPath( sFileName
, aURL
);
273 uno::Reference
< frame::XStorable
> xStor( getModel(), uno::UNO_QUERY_THROW
);
274 uno::Sequence
< beans::PropertyValue
> storeProps(1);
275 storeProps
[0].Name
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterName" ) );
276 storeProps
[0].Value
<<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MS Excel 97" ) );
277 xStor
->storeToURL( aURL
, storeProps
);
280 css::uno::Any SAL_CALL
281 ScVbaWorkbook::Styles( const::uno::Any
& Item
) throw (uno::RuntimeException
)
283 // quick look and Styles object doesn't seem to have a valid parent
284 // or a least the object browser just shows an object that has no
285 // variables ( therefore... leave as NULL for now )
286 uno::Reference
< XCollection
> dStyles
= new ScVbaStyles( uno::Reference
< XHelperInterface
>(), mxContext
, getModel() );
287 if ( Item
.hasValue() )
288 return dStyles
->Item( Item
, uno::Any() );
289 return uno::makeAny( dStyles
);
294 ScVbaWorkbook::Names( const css::uno::Any
& aIndex
) throw (uno::RuntimeException
)
296 uno::Reference
< frame::XModel
> xModel( getModel() );
297 uno::Reference
< beans::XPropertySet
> xProps( xModel
, uno::UNO_QUERY_THROW
);
298 uno::Reference
< sheet::XNamedRanges
> xNamedRanges( xProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("NamedRanges") ) ), uno::UNO_QUERY_THROW
);
299 uno::Reference
< XCollection
> xNames( new ScVbaNames( this , mxContext
, xNamedRanges
, xModel
));
300 if ( aIndex
.getValueTypeClass() == uno::TypeClass_VOID
)
302 return uno::Any( xNames
);
304 return uno::Any( xNames
->Item( aIndex
, uno::Any() ) );
308 ScVbaWorkbook::getServiceImplName()
310 static rtl::OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWorkbook") );
314 uno::Sequence
< rtl::OUString
>
315 ScVbaWorkbook::getServiceNames()
317 static uno::Sequence
< rtl::OUString
> aServiceNames
;
318 if ( aServiceNames
.getLength() == 0 )
320 aServiceNames
.realloc( 1 );
321 aServiceNames
[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Workbook" ) );
323 return aServiceNames
;
326 ::rtl::OUString SAL_CALL
327 ScVbaWorkbook::getCodeName() throw (css::uno::RuntimeException
)
329 #ifdef VBA_OOBUILD_HACK
330 uno::Reference
< frame::XModel
> xModel( getModel(), uno::UNO_QUERY_THROW
);
331 ScDocument
* pDoc
= excel::getDocShell( xModel
)->GetDocument();
332 ScExtDocOptions
* pExtOptions
= pDoc
->GetExtDocOptions();
333 ScExtDocSettings pExtSettings
= pExtOptions
->GetDocSettings();
334 ::rtl::OUString sGlobCodeName
= pExtSettings
.maGlobCodeName
;
335 return sGlobCodeName
;
337 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference
< uno::XInterface
>() );
340 #ifdef VBA_OOBUILD_HACK
342 ScVbaWorkbook::setCodeName( const ::rtl::OUString
& sGlobCodeName
) throw (css::uno::RuntimeException
)
344 uno::Reference
< frame::XModel
> xModel( getModel(), uno::UNO_QUERY_THROW
);
345 ScDocument
* pDoc
= excel::getDocShell( xModel
)->GetDocument();
346 ScExtDocOptions
* pExtOptions
= pDoc
->GetExtDocOptions();
347 ScExtDocSettings pExtSettings
= pExtOptions
->GetDocSettings();
348 pExtSettings
.maGlobCodeName
= sGlobCodeName
;
351 ScVbaWorkbook::setCodeName( const ::rtl::OUString
& ) throw (css::uno::RuntimeException
)
353 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference
< uno::XInterface
>() );
359 namespace sdecl
= comphelper::service_decl
;
360 sdecl::vba_service_class_
<ScVbaWorkbook
, sdecl::with_args
<true> > serviceImpl
;
361 extern sdecl::ServiceDecl
const serviceDecl(
364 "ooo.vba.excel.Workbook" );