xref: /aoo41x/main/sc/source/ui/vba/vbaapplication.cxx (revision b3f79822)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b3f79822SAndrew Rist  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19*b3f79822SAndrew Rist  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <stdio.h>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetView.hpp>
27cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheets.hpp>
28cdf0e10cSrcweir #include <com/sun/star/view/XSelectionSupplier.hpp>
29cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
30cdf0e10cSrcweir #include <ooo/vba/excel/XlCalculation.hpp>
31cdf0e10cSrcweir #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
32cdf0e10cSrcweir #include <com/sun/star/sheet/XCalculatable.hpp>
33cdf0e10cSrcweir #include <com/sun/star/frame/XLayoutManager.hpp>
34cdf0e10cSrcweir #include <com/sun/star/task/XStatusIndicatorSupplier.hpp>
35cdf0e10cSrcweir #include <com/sun/star/task/XStatusIndicator.hpp>
36cdf0e10cSrcweir #include <ooo/vba/excel/XlMousePointer.hpp>
37cdf0e10cSrcweir #include <com/sun/star/sheet/XNamedRanges.hpp>
38cdf0e10cSrcweir #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
39cdf0e10cSrcweir #include <ooo/vba/XExecutableDialog.hpp>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include "vbaapplication.hxx"
42cdf0e10cSrcweir #include "vbaworkbooks.hxx"
43cdf0e10cSrcweir #include "vbaworkbook.hxx"
44cdf0e10cSrcweir #include "vbaworksheets.hxx"
45cdf0e10cSrcweir #include "vbarange.hxx"
46cdf0e10cSrcweir #include "vbawsfunction.hxx"
47cdf0e10cSrcweir #include "vbadialogs.hxx"
48cdf0e10cSrcweir #include "vbawindow.hxx"
49cdf0e10cSrcweir #include "vbawindows.hxx"
50cdf0e10cSrcweir #include "vbaglobals.hxx"
51cdf0e10cSrcweir #include "tabvwsh.hxx"
52cdf0e10cSrcweir #include "gridwin.hxx"
53cdf0e10cSrcweir #include "vbanames.hxx"
54cdf0e10cSrcweir #include <vbahelper/vbashape.hxx>
55cdf0e10cSrcweir #include "vbatextboxshape.hxx"
56cdf0e10cSrcweir #include "vbaassistant.hxx"
57cdf0e10cSrcweir #include "sc.hrc"
58cdf0e10cSrcweir 
59cdf0e10cSrcweir #include <osl/file.hxx>
60cdf0e10cSrcweir #include <rtl/instance.hxx>
61cdf0e10cSrcweir 
62cdf0e10cSrcweir #include <sfx2/request.hxx>
63cdf0e10cSrcweir #include <sfx2/objsh.hxx>
64cdf0e10cSrcweir #include <sfx2/viewfrm.hxx>
65cdf0e10cSrcweir #include <sfx2/app.hxx>
66cdf0e10cSrcweir 
67cdf0e10cSrcweir #include <toolkit/awt/vclxwindow.hxx>
68cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
69cdf0e10cSrcweir 
70cdf0e10cSrcweir #include <tools/diagnose_ex.h>
71cdf0e10cSrcweir 
72cdf0e10cSrcweir #include <docuno.hxx>
73cdf0e10cSrcweir 
74cdf0e10cSrcweir #include <basic/sbx.hxx>
75cdf0e10cSrcweir #include <basic/sbstar.hxx>
76cdf0e10cSrcweir #include <basic/sbuno.hxx>
77cdf0e10cSrcweir #include <basic/sbmeth.hxx>
78cdf0e10cSrcweir 
79cdf0e10cSrcweir #include "convuno.hxx"
80cdf0e10cSrcweir #include "cellsuno.hxx"
81cdf0e10cSrcweir #include "docsh.hxx"
82cdf0e10cSrcweir #include <vbahelper/helperdecl.hxx>
83cdf0e10cSrcweir #include "excelvbahelper.hxx"
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
86cdf0e10cSrcweir using namespace ::ooo::vba;
87cdf0e10cSrcweir using namespace ::com::sun::star;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir // #TODO is this defined somewhere else?
90cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 ) //unix
91cdf0e10cSrcweir #define FILE_PATH_SEPERATOR "/"
92cdf0e10cSrcweir #else // windows
93cdf0e10cSrcweir #define FILE_PATH_SEPERATOR "\\"
94cdf0e10cSrcweir #endif
95cdf0e10cSrcweir 
96cdf0e10cSrcweir uno::Any sbxToUnoValue( SbxVariable* pVar );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir // ============================================================================
99cdf0e10cSrcweir 
100cdf0e10cSrcweir /** Global application settings shared by all open workbooks. */
101cdf0e10cSrcweir struct ScVbaAppSettings
102cdf0e10cSrcweir {
103cdf0e10cSrcweir     sal_Int32 mnCalculation;
104cdf0e10cSrcweir     sal_Bool mbDisplayAlerts;
105cdf0e10cSrcweir     sal_Bool mbEnableEvents;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     explicit ScVbaAppSettings();
108cdf0e10cSrcweir };
109cdf0e10cSrcweir 
ScVbaAppSettings()110cdf0e10cSrcweir ScVbaAppSettings::ScVbaAppSettings() :
111cdf0e10cSrcweir     mnCalculation( excel::XlCalculation::xlCalculationAutomatic ),
112cdf0e10cSrcweir     mbDisplayAlerts( sal_True ),
113cdf0e10cSrcweir     mbEnableEvents( sal_True )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir struct ScVbaStaticAppSettings : public ::rtl::Static< ScVbaAppSettings, ScVbaStaticAppSettings > {};
118cdf0e10cSrcweir 
119cdf0e10cSrcweir // ============================================================================
120cdf0e10cSrcweir 
ScVbaApplication(const uno::Reference<uno::XComponentContext> & xContext)121cdf0e10cSrcweir ScVbaApplication::ScVbaApplication( const uno::Reference<uno::XComponentContext >& xContext ) :
122cdf0e10cSrcweir     ScVbaApplication_BASE( xContext ),
123cdf0e10cSrcweir     mrAppSettings( ScVbaStaticAppSettings::get() )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
~ScVbaApplication()127cdf0e10cSrcweir ScVbaApplication::~ScVbaApplication()
128cdf0e10cSrcweir {
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
getDocumentEventsEnabled()131cdf0e10cSrcweir /*static*/ bool ScVbaApplication::getDocumentEventsEnabled()
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     return ScVbaStaticAppSettings::get().mbEnableEvents;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
GetDocShell(const uno::Reference<frame::XModel> & xModel)136cdf0e10cSrcweir SfxObjectShell* ScVbaApplication::GetDocShell( const uno::Reference< frame::XModel >& xModel ) throw (uno::RuntimeException)
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     return static_cast< SfxObjectShell* >( excel::getDocShell( xModel ) );
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir ::rtl::OUString SAL_CALL
getExactName(const::rtl::OUString & aApproximateName)142cdf0e10cSrcweir ScVbaApplication::getExactName( const ::rtl::OUString& aApproximateName ) throw (uno::RuntimeException)
143cdf0e10cSrcweir {
144cdf0e10cSrcweir     uno::Reference< beans::XExactName > xWSF( new ScVbaWSFunction( this, mxContext ) );
145cdf0e10cSrcweir     return xWSF->getExactName( aApproximateName );
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir uno::Reference< beans::XIntrospectionAccess > SAL_CALL
getIntrospection()149cdf0e10cSrcweir ScVbaApplication::getIntrospection() throw(css::uno::RuntimeException)
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
152cdf0e10cSrcweir     return xWSF->getIntrospection();
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir uno::Any SAL_CALL
invoke(const::rtl::OUString & FunctionName,const uno::Sequence<uno::Any> & Params,uno::Sequence<sal_Int16> & OutParamIndex,uno::Sequence<uno::Any> & OutParam)156cdf0e10cSrcweir ScVbaApplication::invoke( const ::rtl::OUString& FunctionName, const uno::Sequence< uno::Any >& Params, uno::Sequence< sal_Int16 >& OutParamIndex, uno::Sequence< uno::Any >& OutParam) throw(lang::IllegalArgumentException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     /*  When calling the functions directly at the Application object, no runtime
159cdf0e10cSrcweir         errors are thrown, but the error is inserted into the return value. */
160cdf0e10cSrcweir     uno::Any aAny;
161cdf0e10cSrcweir     try
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
164cdf0e10cSrcweir         aAny = xWSF->invoke( FunctionName, Params, OutParamIndex, OutParam );
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir     catch( uno::Exception& )
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir         aAny <<= script::BasicErrorException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), 1000, ::rtl::OUString() );
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir     return aAny;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir void SAL_CALL
setValue(const::rtl::OUString & PropertyName,const uno::Any & Value)174cdf0e10cSrcweir ScVbaApplication::setValue( const ::rtl::OUString& PropertyName, const uno::Any& Value ) throw(beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
177cdf0e10cSrcweir     xWSF->setValue( PropertyName, Value );
178cdf0e10cSrcweir }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir uno::Any SAL_CALL
getValue(const::rtl::OUString & PropertyName)181cdf0e10cSrcweir ScVbaApplication::getValue( const ::rtl::OUString& PropertyName ) throw(beans::UnknownPropertyException, uno::RuntimeException)
182cdf0e10cSrcweir {
183cdf0e10cSrcweir     uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
184cdf0e10cSrcweir     return xWSF->getValue( PropertyName );
185cdf0e10cSrcweir }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir sal_Bool SAL_CALL
hasMethod(const::rtl::OUString & Name)188cdf0e10cSrcweir ScVbaApplication::hasMethod( const ::rtl::OUString& Name ) throw(uno::RuntimeException)
189cdf0e10cSrcweir {
190cdf0e10cSrcweir     uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
191cdf0e10cSrcweir     return xWSF->hasMethod( Name );
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir sal_Bool SAL_CALL
hasProperty(const::rtl::OUString & Name)195cdf0e10cSrcweir ScVbaApplication::hasProperty( const ::rtl::OUString& Name ) throw(uno::RuntimeException)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir     uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
198cdf0e10cSrcweir     return xWSF->hasProperty( Name );
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir uno::Reference< excel::XWorkbook >
getActiveWorkbook()202cdf0e10cSrcweir ScVbaApplication::getActiveWorkbook() throw (uno::RuntimeException)
203cdf0e10cSrcweir {
204c83e58a0SMichael Stahl 	uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW );
205c83e58a0SMichael Stahl     uno::Reference< excel::XWorkbook > xWorkbook( getVBADocument( xModel ), uno::UNO_QUERY );
206c83e58a0SMichael Stahl     if( xWorkbook.is() ) return xWorkbook;
207c83e58a0SMichael Stahl     // #i116936# getVBADocument() may return null in documents without global VBA mode enabled
208c83e58a0SMichael Stahl     return new ScVbaWorkbook( this, mxContext, xModel );
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir uno::Reference< excel::XWorkbook > SAL_CALL
getThisWorkbook()212cdf0e10cSrcweir ScVbaApplication::getThisWorkbook() throw (uno::RuntimeException)
213cdf0e10cSrcweir {
214c83e58a0SMichael Stahl 	uno::Reference< frame::XModel > xModel( getThisExcelDoc( mxContext ), uno::UNO_SET_THROW );
215c83e58a0SMichael Stahl     uno::Reference< excel::XWorkbook > xWorkbook( getVBADocument( xModel ), uno::UNO_QUERY );
216c83e58a0SMichael Stahl     if( xWorkbook.is() ) return xWorkbook;
217c83e58a0SMichael Stahl     // #i116936# getVBADocument() may return null in documents without global VBA mode enabled
218c83e58a0SMichael Stahl     return new ScVbaWorkbook( this, mxContext, xModel );
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir uno::Reference< XAssistant > SAL_CALL
getAssistant()222cdf0e10cSrcweir ScVbaApplication::getAssistant() throw (uno::RuntimeException)
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     return uno::Reference< XAssistant >( new ScVbaAssistant( this, mxContext ) );
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir uno::Any SAL_CALL
getSelection()228cdf0e10cSrcweir ScVbaApplication::getSelection() throw (uno::RuntimeException)
229cdf0e10cSrcweir {
230cdf0e10cSrcweir     OSL_TRACE("** ScVbaApplication::getSelection() ** ");
231cdf0e10cSrcweir     uno::Reference< frame::XModel > xModel( getCurrentDocument() );
232cdf0e10cSrcweir     uno::Reference< lang::XServiceInfo > xServiceInfo( xModel->getCurrentSelection(), uno::UNO_QUERY_THROW );
233cdf0e10cSrcweir     rtl::OUString sImpementaionName = xServiceInfo->getImplementationName();
234cdf0e10cSrcweir     if( sImpementaionName.equalsIgnoreAsciiCaseAscii("com.sun.star.drawing.SvxShapeCollection") )
235cdf0e10cSrcweir     {
236cdf0e10cSrcweir         uno::Reference< drawing::XShapes > xShapes( xModel->getCurrentSelection(), uno::UNO_QUERY_THROW );
237cdf0e10cSrcweir         uno::Reference< container::XIndexAccess > xIndexAccess( xShapes, uno::UNO_QUERY_THROW );
238cdf0e10cSrcweir         uno::Reference< drawing::XShape > xShape( xIndexAccess->getByIndex(0), uno::UNO_QUERY_THROW );
239cdf0e10cSrcweir 	// if ScVbaShape::getType( xShape ) == office::MsoShapeType::msoAutoShape
240cdf0e10cSrcweir 	// and the uno object implements the com.sun.star.drawing.Text service
241cdf0e10cSrcweir 	// return a textboxshape object
242cdf0e10cSrcweir 	if ( ScVbaShape::getType( xShape ) == office::MsoShapeType::msoAutoShape )
243cdf0e10cSrcweir 	{
244cdf0e10cSrcweir 		uno::Reference< lang::XServiceInfo > xShapeServiceInfo( xShape, uno::UNO_QUERY_THROW );
245cdf0e10cSrcweir 		if ( xShapeServiceInfo->supportsService( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.drawing.Text" ) ) )  )
246cdf0e10cSrcweir 		{
247cdf0e10cSrcweir                 return uno::makeAny( uno::Reference< msforms::XTextBoxShape >(new ScVbaTextBoxShape( mxContext, xShape, xShapes, xModel ) ) );
248cdf0e10cSrcweir 		}
249cdf0e10cSrcweir 	}
250cdf0e10cSrcweir         return uno::makeAny( uno::Reference< msforms::XShape >(new ScVbaShape( this, mxContext, xShape, xShapes, xModel, ScVbaShape::getType( xShape ) ) ) );
251cdf0e10cSrcweir     }
252cdf0e10cSrcweir     else if( xServiceInfo->supportsService( rtl::OUString::createFromAscii("com.sun.star.sheet.SheetCellRange")) ||
253cdf0e10cSrcweir              xServiceInfo->supportsService( rtl::OUString::createFromAscii("com.sun.star.sheet.SheetCellRanges")))
254cdf0e10cSrcweir     {
255cdf0e10cSrcweir 	    uno::Reference< table::XCellRange > xRange( getCurrentDocument()->getCurrentSelection(), ::uno::UNO_QUERY);
256cdf0e10cSrcweir 	    if ( !xRange.is() )
257cdf0e10cSrcweir 	    {
258cdf0e10cSrcweir 		    uno::Reference< sheet::XSheetCellRangeContainer > xRanges( getCurrentDocument()->getCurrentSelection(), ::uno::UNO_QUERY);
259cdf0e10cSrcweir 		    if ( xRanges.is() )
260cdf0e10cSrcweir                 return uno::makeAny( uno::Reference< excel::XRange >( new ScVbaRange( excel::getUnoSheetModuleObj( xRanges ), mxContext, xRanges ) ) );
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 	    }
263cdf0e10cSrcweir         return uno::makeAny( uno::Reference< excel::XRange >(new ScVbaRange( excel::getUnoSheetModuleObj( xRange ), mxContext, xRange ) ) );
264cdf0e10cSrcweir     }
265cdf0e10cSrcweir     else
266cdf0e10cSrcweir     {
267cdf0e10cSrcweir         throw uno::RuntimeException( sImpementaionName + rtl::OUString::createFromAscii(" not suported"), uno::Reference< uno::XInterface >() );
268cdf0e10cSrcweir     }
269cdf0e10cSrcweir }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir uno::Reference< excel::XRange >
getActiveCell()272cdf0e10cSrcweir ScVbaApplication::getActiveCell() throw (uno::RuntimeException )
273cdf0e10cSrcweir {
274cdf0e10cSrcweir 	uno::Reference< sheet::XSpreadsheetView > xView( getCurrentDocument()->getCurrentController(), uno::UNO_QUERY_THROW );
275cdf0e10cSrcweir 	uno::Reference< table::XCellRange > xRange( xView->getActiveSheet(), ::uno::UNO_QUERY_THROW);
276cdf0e10cSrcweir 	ScTabViewShell* pViewShell = excel::getCurrentBestViewShell(mxContext);
277cdf0e10cSrcweir 	if ( !pViewShell )
278cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString::createFromAscii("No ViewShell available"), uno::Reference< uno::XInterface >() );
279cdf0e10cSrcweir 	ScViewData* pTabView = pViewShell->GetViewData();
280cdf0e10cSrcweir 	if ( !pTabView )
281cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString::createFromAscii("No ViewData available"), uno::Reference< uno::XInterface >() );
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 	sal_Int32 nCursorX = pTabView->GetCurX();
284cdf0e10cSrcweir 	sal_Int32 nCursorY = pTabView->GetCurY();
285cdf0e10cSrcweir 
286cdf0e10cSrcweir     // #i117392# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
287cdf0e10cSrcweir 	return new ScVbaRange( excel::getUnoSheetModuleObj( xRange ), mxContext, xRange->getCellRangeByPosition( nCursorX, nCursorY, nCursorX, nCursorY ) );
288cdf0e10cSrcweir }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir uno::Any SAL_CALL
Workbooks(const uno::Any & aIndex)291cdf0e10cSrcweir ScVbaApplication::Workbooks( const uno::Any& aIndex ) throw (uno::RuntimeException)
292cdf0e10cSrcweir {
293cdf0e10cSrcweir 	uno::Reference< XCollection > xWorkBooks( new ScVbaWorkbooks( this, mxContext ) );
294cdf0e10cSrcweir 	if (  aIndex.getValueTypeClass() == uno::TypeClass_VOID )
295cdf0e10cSrcweir 	{
296cdf0e10cSrcweir 		// void then somebody did Workbooks.something in vba
297cdf0e10cSrcweir 	    return uno::Any( xWorkBooks );
298cdf0e10cSrcweir 	}
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 	return uno::Any ( xWorkBooks->Item( aIndex, uno::Any() ) );
301cdf0e10cSrcweir }
302cdf0e10cSrcweir 
303cdf0e10cSrcweir uno::Any SAL_CALL
Worksheets(const uno::Any & aIndex)304cdf0e10cSrcweir ScVbaApplication::Worksheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
305cdf0e10cSrcweir {
306cdf0e10cSrcweir     uno::Reference< excel::XWorkbook > xWorkbook( getActiveWorkbook(), uno::UNO_SET_THROW );
307cdf0e10cSrcweir     return xWorkbook->Worksheets( aIndex );
308cdf0e10cSrcweir }
309cdf0e10cSrcweir 
310cdf0e10cSrcweir uno::Any SAL_CALL
WorksheetFunction()311cdf0e10cSrcweir ScVbaApplication::WorksheetFunction( ) throw (::com::sun::star::uno::RuntimeException)
312cdf0e10cSrcweir {
313cdf0e10cSrcweir     return uno::makeAny( uno::Reference< script::XInvocation >( new ScVbaWSFunction( this, mxContext ) ) );
314cdf0e10cSrcweir }
315cdf0e10cSrcweir 
316cdf0e10cSrcweir uno::Any SAL_CALL
Evaluate(const::rtl::OUString & Name)317cdf0e10cSrcweir ScVbaApplication::Evaluate( const ::rtl::OUString& Name ) throw (uno::RuntimeException)
318cdf0e10cSrcweir {
319cdf0e10cSrcweir 	// #TODO Evaluate allows other things to be evaluated, e.g. functions
320cdf0e10cSrcweir 	// I think ( like SIN(3) etc. ) need to investigate that
321cdf0e10cSrcweir 	// named Ranges also? e.g. [MyRange] if so need a list of named ranges
322cdf0e10cSrcweir 	uno::Any aVoid;
323cdf0e10cSrcweir 	return uno::Any( getActiveWorkbook()->getActiveSheet()->Range( uno::Any( Name ), aVoid ) );
324cdf0e10cSrcweir }
325cdf0e10cSrcweir 
326cdf0e10cSrcweir uno::Any
Dialogs(const uno::Any & aIndex)327cdf0e10cSrcweir ScVbaApplication::Dialogs( const uno::Any &aIndex ) throw (uno::RuntimeException)
328cdf0e10cSrcweir {
329cdf0e10cSrcweir 	uno::Reference< excel::XDialogs > xDialogs( new ScVbaDialogs( uno::Reference< XHelperInterface >( this ), mxContext, getCurrentDocument() ) );
330cdf0e10cSrcweir 	if( !aIndex.hasValue() )
331cdf0e10cSrcweir 		return uno::Any( xDialogs );
332cdf0e10cSrcweir 	return uno::Any( xDialogs->Item( aIndex ) );
333cdf0e10cSrcweir }
334cdf0e10cSrcweir 
335cdf0e10cSrcweir uno::Reference< excel::XWindow > SAL_CALL
getActiveWindow()336cdf0e10cSrcweir ScVbaApplication::getActiveWindow() throw (uno::RuntimeException)
337cdf0e10cSrcweir {
338cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel = getCurrentDocument();
339cdf0e10cSrcweir 	uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
340cdf0e10cSrcweir 	uno::Reference< XHelperInterface > xParent( getActiveWorkbook(), uno::UNO_QUERY_THROW );
341cdf0e10cSrcweir 	uno::Reference< excel::XWindow > xWin( new ScVbaWindow( xParent, mxContext, xModel, xController ) );
342cdf0e10cSrcweir 	return xWin;
343cdf0e10cSrcweir }
344cdf0e10cSrcweir 
345cdf0e10cSrcweir uno::Any SAL_CALL
getCutCopyMode()346cdf0e10cSrcweir ScVbaApplication::getCutCopyMode() throw (uno::RuntimeException)
347cdf0e10cSrcweir {
348cdf0e10cSrcweir 	//# FIXME TODO, implementation
349cdf0e10cSrcweir 	uno::Any result;
350cdf0e10cSrcweir 	result <<= sal_False;
351cdf0e10cSrcweir 	return result;
352cdf0e10cSrcweir }
353cdf0e10cSrcweir 
354cdf0e10cSrcweir void SAL_CALL
setCutCopyMode(const uno::Any &)355cdf0e10cSrcweir ScVbaApplication::setCutCopyMode( const uno::Any& /*_cutcopymode*/ ) throw (uno::RuntimeException)
356cdf0e10cSrcweir {
357cdf0e10cSrcweir 	//# FIXME TODO, implementation
358cdf0e10cSrcweir }
359cdf0e10cSrcweir 
360cdf0e10cSrcweir uno::Any SAL_CALL
getStatusBar()361cdf0e10cSrcweir ScVbaApplication::getStatusBar() throw (uno::RuntimeException)
362cdf0e10cSrcweir {
363cdf0e10cSrcweir 	return uno::makeAny( !getDisplayStatusBar() );
364cdf0e10cSrcweir }
365cdf0e10cSrcweir 
366cdf0e10cSrcweir void SAL_CALL
setStatusBar(const uno::Any & _statusbar)367cdf0e10cSrcweir ScVbaApplication::setStatusBar( const uno::Any& _statusbar ) throw (uno::RuntimeException)
368cdf0e10cSrcweir {
369cdf0e10cSrcweir     rtl::OUString sText;
370cdf0e10cSrcweir     sal_Bool bDefault = sal_False;
371cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
372cdf0e10cSrcweir     uno::Reference< task::XStatusIndicatorSupplier > xStatusIndicatorSupplier( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
373cdf0e10cSrcweir     uno::Reference< task::XStatusIndicator > xStatusIndicator( xStatusIndicatorSupplier->getStatusIndicator(), uno::UNO_QUERY_THROW );
374cdf0e10cSrcweir     if( _statusbar >>= sText )
375cdf0e10cSrcweir     {
376cdf0e10cSrcweir         setDisplayStatusBar( sal_True );
377cdf0e10cSrcweir         if ( sText.getLength() )
378cdf0e10cSrcweir             xStatusIndicator->start( sText, 100 );
379cdf0e10cSrcweir         else
380cdf0e10cSrcweir             xStatusIndicator->end();        // restore normal state for empty text
381cdf0e10cSrcweir     }
382cdf0e10cSrcweir     else if( _statusbar >>= bDefault )
383cdf0e10cSrcweir     {
384cdf0e10cSrcweir         if( bDefault == sal_False )
385cdf0e10cSrcweir         {
386cdf0e10cSrcweir             xStatusIndicator->end();
387cdf0e10cSrcweir             setDisplayStatusBar( sal_True );
388cdf0e10cSrcweir         }
389cdf0e10cSrcweir     }
390cdf0e10cSrcweir     else
391cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString::createFromAscii( "Invalid prarameter. It should be a string or False" ),
392cdf0e10cSrcweir             uno::Reference< uno::XInterface >() );
393cdf0e10cSrcweir }
394cdf0e10cSrcweir 
395cdf0e10cSrcweir ::sal_Int32 SAL_CALL
getCalculation()396cdf0e10cSrcweir ScVbaApplication::getCalculation() throw (uno::RuntimeException)
397cdf0e10cSrcweir {
398cdf0e10cSrcweir     // TODO: in Excel, this is an application-wide setting
399cdf0e10cSrcweir 	uno::Reference<sheet::XCalculatable> xCalc(getCurrentDocument(), uno::UNO_QUERY_THROW);
400cdf0e10cSrcweir 	if(xCalc->isAutomaticCalculationEnabled())
401cdf0e10cSrcweir 		return excel::XlCalculation::xlCalculationAutomatic;
402cdf0e10cSrcweir 	else
403cdf0e10cSrcweir 		return excel::XlCalculation::xlCalculationManual;
404cdf0e10cSrcweir }
405cdf0e10cSrcweir 
406cdf0e10cSrcweir void SAL_CALL
setCalculation(::sal_Int32 _calculation)407cdf0e10cSrcweir ScVbaApplication::setCalculation( ::sal_Int32 _calculation ) throw (uno::RuntimeException)
408cdf0e10cSrcweir {
409cdf0e10cSrcweir     // TODO: in Excel, this is an application-wide setting
410cdf0e10cSrcweir 	uno::Reference< sheet::XCalculatable > xCalc(getCurrentDocument(), uno::UNO_QUERY_THROW);
411cdf0e10cSrcweir 	switch(_calculation)
412cdf0e10cSrcweir 	{
413cdf0e10cSrcweir 		case excel::XlCalculation::xlCalculationManual:
414cdf0e10cSrcweir 			xCalc->enableAutomaticCalculation(sal_False);
415cdf0e10cSrcweir 			break;
416cdf0e10cSrcweir 		case excel::XlCalculation::xlCalculationAutomatic:
417cdf0e10cSrcweir 		case excel::XlCalculation::xlCalculationSemiautomatic:
418cdf0e10cSrcweir 			xCalc->enableAutomaticCalculation(sal_True);
419cdf0e10cSrcweir 			break;
420cdf0e10cSrcweir 	}
421cdf0e10cSrcweir }
422cdf0e10cSrcweir 
423cdf0e10cSrcweir uno::Any SAL_CALL
Windows(const uno::Any & aIndex)424cdf0e10cSrcweir ScVbaApplication::Windows( const uno::Any& aIndex  ) throw (uno::RuntimeException)
425cdf0e10cSrcweir {
426cdf0e10cSrcweir 	uno::Reference< excel::XWindows >  xWindows( new ScVbaWindows( this, mxContext ) );
427cdf0e10cSrcweir 	if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
428cdf0e10cSrcweir 		return uno::Any( xWindows );
429cdf0e10cSrcweir 	return uno::Any( xWindows->Item( aIndex, uno::Any() ) );
430cdf0e10cSrcweir }
431cdf0e10cSrcweir void SAL_CALL
wait(double time)432cdf0e10cSrcweir ScVbaApplication::wait( double time ) throw (uno::RuntimeException)
433cdf0e10cSrcweir {
434cdf0e10cSrcweir 	StarBASIC* pBasic = SFX_APP()->GetBasic();
435cdf0e10cSrcweir 	SbxArrayRef aArgs = new SbxArray;
436cdf0e10cSrcweir 	SbxVariableRef aRef = new SbxVariable;
437cdf0e10cSrcweir 	aRef->PutDouble( time );
438cdf0e10cSrcweir 	aArgs->Put(  aRef, 1 );
439cdf0e10cSrcweir 	SbMethod* pMeth = (SbMethod*)pBasic->GetRtl()->Find( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("WaitUntil") ), SbxCLASS_METHOD );
440cdf0e10cSrcweir 
441cdf0e10cSrcweir 	if ( pMeth )
442cdf0e10cSrcweir 	{
443cdf0e10cSrcweir 		pMeth->SetParameters( aArgs );
444cdf0e10cSrcweir 		SbxVariableRef refTemp = pMeth;
445cdf0e10cSrcweir 		// forces a broadcast
446cdf0e10cSrcweir 		SbxVariableRef pNew = new  SbxMethod( *((SbxMethod*)pMeth));
447cdf0e10cSrcweir 	}
448cdf0e10cSrcweir }
449cdf0e10cSrcweir 
450cdf0e10cSrcweir uno::Any SAL_CALL
Range(const uno::Any & Cell1,const uno::Any & Cell2)451cdf0e10cSrcweir ScVbaApplication::Range( const uno::Any& Cell1, const uno::Any& Cell2 ) throw (uno::RuntimeException)
452cdf0e10cSrcweir {
453cdf0e10cSrcweir 	uno::Reference< excel::XRange > xVbRange = ScVbaRange::ApplicationRange( mxContext, Cell1, Cell2 );
454cdf0e10cSrcweir 	return uno::makeAny( xVbRange );
455cdf0e10cSrcweir }
456cdf0e10cSrcweir 
457cdf0e10cSrcweir uno::Any SAL_CALL
Names(const css::uno::Any & aIndex)458cdf0e10cSrcweir ScVbaApplication::Names( const css::uno::Any& aIndex ) throw ( uno::RuntimeException )
459cdf0e10cSrcweir {
460cdf0e10cSrcweir     uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
461cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xPropertySet( xModel, uno::UNO_QUERY_THROW );
462cdf0e10cSrcweir     uno::Reference< sheet::XNamedRanges > xNamedRanges( xPropertySet->getPropertyValue( rtl::OUString::createFromAscii("NamedRanges")) , uno::UNO_QUERY_THROW );
463cdf0e10cSrcweir     css::uno::Reference< excel::XNames > xNames ( new ScVbaNames( this , mxContext , xNamedRanges , xModel ) );
464cdf0e10cSrcweir     if (  aIndex.getValueTypeClass() == uno::TypeClass_VOID )
465cdf0e10cSrcweir     {
466cdf0e10cSrcweir         return uno::Any( xNames );
467cdf0e10cSrcweir }
468cdf0e10cSrcweir     return uno::Any( xNames->Item( aIndex, uno::Any() ) );
469cdf0e10cSrcweir }
470cdf0e10cSrcweir 
471cdf0e10cSrcweir 
472cdf0e10cSrcweir uno::Reference< excel::XWorksheet > SAL_CALL
getActiveSheet()473cdf0e10cSrcweir ScVbaApplication::getActiveSheet() throw (uno::RuntimeException)
474cdf0e10cSrcweir {
475cdf0e10cSrcweir     uno::Reference< excel::XWorksheet > result;
476cdf0e10cSrcweir     uno::Reference< excel::XWorkbook > xWorkbook( getActiveWorkbook(), uno::UNO_QUERY );
477cdf0e10cSrcweir     if ( xWorkbook.is() )
478cdf0e10cSrcweir     {
479cdf0e10cSrcweir         uno::Reference< excel::XWorksheet > xWorksheet(
480cdf0e10cSrcweir             xWorkbook->getActiveSheet(), uno::UNO_QUERY );
481cdf0e10cSrcweir         if ( xWorksheet.is() )
482cdf0e10cSrcweir         {
483cdf0e10cSrcweir             result = xWorksheet;
484cdf0e10cSrcweir         }
485cdf0e10cSrcweir     }
486cdf0e10cSrcweir 
487cdf0e10cSrcweir     if ( !result.is() )
488cdf0e10cSrcweir     {
489cdf0e10cSrcweir         // Fixme - check if this is reasonable/desired behavior
490cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString::createFromAscii(
491cdf0e10cSrcweir             "No activeSheet available" ), uno::Reference< uno::XInterface >() );
492cdf0e10cSrcweir     }
493cdf0e10cSrcweir     return result;
494cdf0e10cSrcweir 
495cdf0e10cSrcweir }
496cdf0e10cSrcweir 
497cdf0e10cSrcweir /*******************************************************************************
498cdf0e10cSrcweir  *  In msdn:
499cdf0e10cSrcweir  *  Reference   Optional Variant. The destination. Can be a Range
500cdf0e10cSrcweir  *  object, a string that contains a cell reference in R1C1-style notation,
501cdf0e10cSrcweir  *  or a string that contains a Visual Basic procedure name.
502cdf0e10cSrcweir  *  Scroll   Optional Variant. True to scrol, False to not scroll through
503cdf0e10cSrcweir  *  the window. The default is False.
504cdf0e10cSrcweir  *  Parser is split to three parts, Range, R1C1 string and procedure name.
505cdf0e10cSrcweir  *  by test excel, it seems Scroll no effect. ???
506cdf0e10cSrcweir *******************************************************************************/
507cdf0e10cSrcweir void SAL_CALL
GoTo(const uno::Any & Reference,const uno::Any & Scroll)508cdf0e10cSrcweir ScVbaApplication::GoTo( const uno::Any& Reference, const uno::Any& Scroll ) throw (uno::RuntimeException)
509cdf0e10cSrcweir {
510cdf0e10cSrcweir     //test Scroll is a boolean
511cdf0e10cSrcweir     sal_Bool bScroll = sal_False;
512cdf0e10cSrcweir     //R1C1-style string or a string of procedure name.
513cdf0e10cSrcweir 
514cdf0e10cSrcweir     if( Scroll.hasValue() )
515cdf0e10cSrcweir     {
516cdf0e10cSrcweir         sal_Bool aScroll = sal_False;
517cdf0e10cSrcweir         if( Scroll >>= aScroll )
518cdf0e10cSrcweir         {
519cdf0e10cSrcweir             bScroll = aScroll;
520cdf0e10cSrcweir         }
521cdf0e10cSrcweir         else
522cdf0e10cSrcweir             throw uno::RuntimeException( rtl::OUString::createFromAscii( "sencond parameter should be boolean" ),
523cdf0e10cSrcweir                     uno::Reference< uno::XInterface >() );
524cdf0e10cSrcweir     }
525cdf0e10cSrcweir 
526cdf0e10cSrcweir     rtl::OUString sRangeName;
527cdf0e10cSrcweir     if( Reference >>= sRangeName )
528cdf0e10cSrcweir     {
529cdf0e10cSrcweir         uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
530cdf0e10cSrcweir         uno::Reference< sheet::XSpreadsheetView > xSpreadsheet(
531cdf0e10cSrcweir                 xModel->getCurrentController(), uno::UNO_QUERY_THROW );
532cdf0e10cSrcweir         uno::Reference< sheet::XSpreadsheet > xDoc = xSpreadsheet->getActiveSheet();
533cdf0e10cSrcweir 
534cdf0e10cSrcweir         ScTabViewShell* pShell = excel::getCurrentBestViewShell( mxContext );
535cdf0e10cSrcweir         ScGridWindow* gridWindow = (ScGridWindow*)pShell->GetWindow();
536cdf0e10cSrcweir         try
537cdf0e10cSrcweir         {
538cdf0e10cSrcweir             uno::Reference< excel::XRange > xVbaSheetRange = ScVbaRange::getRangeObjectForName(
539cdf0e10cSrcweir                 mxContext, sRangeName, excel::getDocShell( xModel ), formula::FormulaGrammar::CONV_XL_R1C1 );
540cdf0e10cSrcweir 
541cdf0e10cSrcweir             if( bScroll )
542cdf0e10cSrcweir             {
543cdf0e10cSrcweir                 xVbaSheetRange->Select();
544cdf0e10cSrcweir                 uno::Reference< excel::XWindow >  xWindow = getActiveWindow();
545cdf0e10cSrcweir                 ScSplitPos eWhich = pShell->GetViewData()->GetActivePart();
546cdf0e10cSrcweir                 sal_Int32 nValueX = pShell->GetViewData()->GetPosX(WhichH(eWhich));
547cdf0e10cSrcweir                 sal_Int32 nValueY = pShell->GetViewData()->GetPosY(WhichV(eWhich));
548cdf0e10cSrcweir                 xWindow->SmallScroll( uno::makeAny( (sal_Int16)(xVbaSheetRange->getRow() - 1) ),
549cdf0e10cSrcweir                          uno::makeAny( (sal_Int16)nValueY ),
550cdf0e10cSrcweir                          uno::makeAny( (sal_Int16)(xVbaSheetRange->getColumn() - 1)  ),
551cdf0e10cSrcweir                          uno::makeAny( (sal_Int16)nValueX ) );
552cdf0e10cSrcweir                 gridWindow->GrabFocus();
553cdf0e10cSrcweir             }
554cdf0e10cSrcweir             else
555cdf0e10cSrcweir             {
556cdf0e10cSrcweir                 xVbaSheetRange->Select();
557cdf0e10cSrcweir                 gridWindow->GrabFocus();
558cdf0e10cSrcweir             }
559cdf0e10cSrcweir         }
560cdf0e10cSrcweir         catch( uno::RuntimeException )
561cdf0e10cSrcweir         {
562cdf0e10cSrcweir             //maybe this should be a procedure name
563cdf0e10cSrcweir             //TODO for procedure name
564cdf0e10cSrcweir             //browse::XBrowseNodeFactory is a singlton. OUString::createFromAscii( "/singletons/com.sun.star.script.browse.theBrowseNodeFactory")
565cdf0e10cSrcweir             //and the createView( browse::BrowseNodeFactoryViewTypes::MACROSELECTOR ) to get a root browse::XBrowseNode.
566cdf0e10cSrcweir             //for query XInvocation interface.
567cdf0e10cSrcweir             //but how to directly get the XInvocation?
568cdf0e10cSrcweir             throw uno::RuntimeException( rtl::OUString::createFromAscii( "invalid reference for range name, it should be procedure name" ),
569cdf0e10cSrcweir                     uno::Reference< uno::XInterface >() );
570cdf0e10cSrcweir         }
571cdf0e10cSrcweir         return;
572cdf0e10cSrcweir     }
573cdf0e10cSrcweir     uno::Reference< excel::XRange > xRange;
574cdf0e10cSrcweir     if( Reference >>= xRange )
575cdf0e10cSrcweir     {
576cdf0e10cSrcweir         uno::Reference< excel::XRange > xVbaRange( Reference, uno::UNO_QUERY );
577cdf0e10cSrcweir         ScTabViewShell* pShell = excel::getCurrentBestViewShell( mxContext );
578cdf0e10cSrcweir         ScGridWindow* gridWindow = (ScGridWindow*)pShell->GetWindow();
579cdf0e10cSrcweir         if ( xVbaRange.is() )
580cdf0e10cSrcweir         {
581cdf0e10cSrcweir             //TODO bScroll should be using, In this time, it doesenot have effection
582cdf0e10cSrcweir             if( bScroll )
583cdf0e10cSrcweir             {
584cdf0e10cSrcweir                 xVbaRange->Select();
585cdf0e10cSrcweir                 uno::Reference< excel::XWindow >  xWindow = getActiveWindow();
586cdf0e10cSrcweir                 ScSplitPos eWhich = pShell->GetViewData()->GetActivePart();
587cdf0e10cSrcweir                 sal_Int32 nValueX = pShell->GetViewData()->GetPosX(WhichH(eWhich));
588cdf0e10cSrcweir                 sal_Int32 nValueY = pShell->GetViewData()->GetPosY(WhichV(eWhich));
589cdf0e10cSrcweir                 xWindow->SmallScroll( uno::makeAny( (sal_Int16)(xVbaRange->getRow() - 1) ),
590cdf0e10cSrcweir                          uno::makeAny( (sal_Int16)nValueY ),
591cdf0e10cSrcweir                          uno::makeAny( (sal_Int16)(xVbaRange->getColumn() - 1)  ),
592cdf0e10cSrcweir                          uno::makeAny( (sal_Int16)nValueX ) );
593cdf0e10cSrcweir                 gridWindow->GrabFocus();
594cdf0e10cSrcweir             }
595cdf0e10cSrcweir             else
596cdf0e10cSrcweir             {
597cdf0e10cSrcweir                 xVbaRange->Select();
598cdf0e10cSrcweir                 gridWindow->GrabFocus();
599cdf0e10cSrcweir             }
600cdf0e10cSrcweir         }
601cdf0e10cSrcweir         return;
602cdf0e10cSrcweir     }
603cdf0e10cSrcweir     throw uno::RuntimeException( rtl::OUString::createFromAscii( "invalid reference or name" ),
604cdf0e10cSrcweir             uno::Reference< uno::XInterface >() );
605cdf0e10cSrcweir }
606cdf0e10cSrcweir 
607cdf0e10cSrcweir sal_Int32 SAL_CALL
getCursor()608cdf0e10cSrcweir ScVbaApplication::getCursor() throw (uno::RuntimeException)
609cdf0e10cSrcweir {
610cdf0e10cSrcweir     sal_Int32 nPointerStyle =  getPointerStyle(getCurrentDocument());
611cdf0e10cSrcweir 
612cdf0e10cSrcweir     switch( nPointerStyle )
613cdf0e10cSrcweir     {
614cdf0e10cSrcweir         case POINTER_ARROW:
615cdf0e10cSrcweir             return excel::XlMousePointer::xlNorthwestArrow;
616cdf0e10cSrcweir         case POINTER_NULL:
617cdf0e10cSrcweir             return excel::XlMousePointer::xlDefault;
618cdf0e10cSrcweir         case POINTER_WAIT:
619cdf0e10cSrcweir             return excel::XlMousePointer::xlWait;
620cdf0e10cSrcweir         case POINTER_TEXT:
621cdf0e10cSrcweir             return excel::XlMousePointer::xlIBeam;
622cdf0e10cSrcweir         default:
623cdf0e10cSrcweir             return excel::XlMousePointer::xlDefault;
624cdf0e10cSrcweir     }
625cdf0e10cSrcweir }
626cdf0e10cSrcweir 
627cdf0e10cSrcweir void SAL_CALL
setCursor(sal_Int32 _cursor)628cdf0e10cSrcweir ScVbaApplication::setCursor( sal_Int32 _cursor ) throw (uno::RuntimeException)
629cdf0e10cSrcweir {
630cdf0e10cSrcweir     try
631cdf0e10cSrcweir     {
632cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
633cdf0e10cSrcweir         switch( _cursor )
634cdf0e10cSrcweir         {
635cdf0e10cSrcweir             case excel::XlMousePointer::xlNorthwestArrow:
636cdf0e10cSrcweir             {
637cdf0e10cSrcweir                 const Pointer& rPointer( POINTER_ARROW );
638cdf0e10cSrcweir                 setCursorHelper( xModel, rPointer, sal_False );
639cdf0e10cSrcweir                 break;
640cdf0e10cSrcweir             }
641cdf0e10cSrcweir             case excel::XlMousePointer::xlWait:
642cdf0e10cSrcweir             case excel::XlMousePointer::xlIBeam:
643cdf0e10cSrcweir             {
644cdf0e10cSrcweir                 const Pointer& rPointer( static_cast< PointerStyle >( _cursor ) );
645cdf0e10cSrcweir                 //It will set the edit window, toobar and statusbar's mouse pointer.
646cdf0e10cSrcweir                 setCursorHelper( xModel, rPointer, sal_True );
647cdf0e10cSrcweir                 break;
648cdf0e10cSrcweir             }
649cdf0e10cSrcweir             case excel::XlMousePointer::xlDefault:
650cdf0e10cSrcweir             {
651cdf0e10cSrcweir                 const Pointer& rPointer( POINTER_NULL );
652cdf0e10cSrcweir                 setCursorHelper( xModel, rPointer, sal_False );
653cdf0e10cSrcweir                 break;
654cdf0e10cSrcweir             }
655cdf0e10cSrcweir             default:
656cdf0e10cSrcweir                 throw uno::RuntimeException( rtl::OUString(
657cdf0e10cSrcweir                         RTL_CONSTASCII_USTRINGPARAM("Unknown value for Cursor pointer")), uno::Reference< uno::XInterface >() );
658cdf0e10cSrcweir                 // TODO: isn't this a flaw in the API? It should be allowed to throw an
659cdf0e10cSrcweir                 // IllegalArgumentException, or so
660cdf0e10cSrcweir         }
661cdf0e10cSrcweir     }
662cdf0e10cSrcweir     catch( const uno::Exception& )
663cdf0e10cSrcweir     {
664cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
665cdf0e10cSrcweir     }
666cdf0e10cSrcweir }
667cdf0e10cSrcweir 
668cdf0e10cSrcweir // #TODO perhaps we should switch the return type depending of the filter
669cdf0e10cSrcweir // type, e.g. return Calc for Calc and Excel if its an imported doc
670cdf0e10cSrcweir rtl::OUString SAL_CALL
getName()671cdf0e10cSrcweir ScVbaApplication::getName() throw (uno::RuntimeException)
672cdf0e10cSrcweir {
673cdf0e10cSrcweir 	static rtl::OUString appName( RTL_CONSTASCII_USTRINGPARAM("Microsoft Excel" ) );
674cdf0e10cSrcweir 	return appName;
675cdf0e10cSrcweir }
676cdf0e10cSrcweir 
677cdf0e10cSrcweir // #TODO #FIXME get/setDisplayAlerts are just stub impl
678cdf0e10cSrcweir // here just the status of the switch is set
679cdf0e10cSrcweir // the function that throws an error message needs to
680cdf0e10cSrcweir // evaluate this switch in order to know whether it has to disable the
681cdf0e10cSrcweir // error message thrown by OpenOffice
682cdf0e10cSrcweir 
683cdf0e10cSrcweir void SAL_CALL
setDisplayAlerts(sal_Bool displayAlerts)684cdf0e10cSrcweir ScVbaApplication::setDisplayAlerts(sal_Bool displayAlerts) throw (uno::RuntimeException)
685cdf0e10cSrcweir {
686cdf0e10cSrcweir     mrAppSettings.mbDisplayAlerts = displayAlerts;
687cdf0e10cSrcweir }
688cdf0e10cSrcweir 
689cdf0e10cSrcweir sal_Bool SAL_CALL
getDisplayAlerts()690cdf0e10cSrcweir ScVbaApplication::getDisplayAlerts() throw (uno::RuntimeException)
691cdf0e10cSrcweir {
692cdf0e10cSrcweir 	return mrAppSettings.mbDisplayAlerts;
693cdf0e10cSrcweir }
694cdf0e10cSrcweir 
695cdf0e10cSrcweir void SAL_CALL
setEnableEvents(sal_Bool bEnable)696cdf0e10cSrcweir ScVbaApplication::setEnableEvents(sal_Bool bEnable) throw (uno::RuntimeException)
697cdf0e10cSrcweir {
698cdf0e10cSrcweir 	mrAppSettings.mbEnableEvents = bEnable;
699cdf0e10cSrcweir }
700cdf0e10cSrcweir 
701cdf0e10cSrcweir sal_Bool SAL_CALL
getEnableEvents()702cdf0e10cSrcweir ScVbaApplication::getEnableEvents() throw (uno::RuntimeException)
703cdf0e10cSrcweir {
704cdf0e10cSrcweir 	return mrAppSettings.mbEnableEvents;
705cdf0e10cSrcweir }
706cdf0e10cSrcweir 
707cdf0e10cSrcweir void SAL_CALL
Calculate()708cdf0e10cSrcweir ScVbaApplication::Calculate() throw(  script::BasicErrorException , uno::RuntimeException )
709cdf0e10cSrcweir {
710cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
711cdf0e10cSrcweir 	uno::Reference< sheet::XCalculatable > xCalculatable( getCurrentDocument(), uno::UNO_QUERY_THROW );
712cdf0e10cSrcweir 	xCalculatable->calculateAll();
713cdf0e10cSrcweir }
714cdf0e10cSrcweir 
lcl_getPathSettingsService(const uno::Reference<uno::XComponentContext> & xContext)715cdf0e10cSrcweir uno::Reference< beans::XPropertySet > lcl_getPathSettingsService( const uno::Reference< uno::XComponentContext >& xContext ) throw ( uno::RuntimeException )
716cdf0e10cSrcweir {
717cdf0e10cSrcweir 	static uno::Reference< beans::XPropertySet >  xPathSettings;
718cdf0e10cSrcweir 	if ( !xPathSettings.is() )
719cdf0e10cSrcweir 	{
720cdf0e10cSrcweir 		uno::Reference< lang::XMultiComponentFactory > xSMgr( xContext->getServiceManager(), uno::UNO_QUERY_THROW );
721cdf0e10cSrcweir 		xPathSettings.set( xSMgr->createInstanceWithContext(::rtl::OUString::createFromAscii("com.sun.star.util.PathSettings"), xContext), uno::UNO_QUERY_THROW );
722cdf0e10cSrcweir 	}
723cdf0e10cSrcweir 	return xPathSettings;
724cdf0e10cSrcweir }
getOfficePath(const rtl::OUString & _sPathType)725cdf0e10cSrcweir rtl::OUString ScVbaApplication::getOfficePath( const rtl::OUString& _sPathType ) throw ( uno::RuntimeException )
726cdf0e10cSrcweir {
727cdf0e10cSrcweir 	rtl::OUString sRetPath;
728cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xProps = lcl_getPathSettingsService( mxContext );
729cdf0e10cSrcweir 	try
730cdf0e10cSrcweir 	{
731cdf0e10cSrcweir 		rtl::OUString sUrl;
732cdf0e10cSrcweir 	 	xProps->getPropertyValue( _sPathType ) >>= sUrl;
733cdf0e10cSrcweir 
734cdf0e10cSrcweir 		// if its a list of paths then use the last one
735cdf0e10cSrcweir 		sal_Int32 nIndex =  sUrl.lastIndexOf( ';' ) ;
736cdf0e10cSrcweir 		if ( nIndex > 0 )
737cdf0e10cSrcweir 			sUrl = sUrl.copy( nIndex + 1 );
738cdf0e10cSrcweir 		::osl::File::getSystemPathFromFileURL( sUrl, sRetPath );
739cdf0e10cSrcweir 	}
740cdf0e10cSrcweir 	catch (uno::Exception&)
741cdf0e10cSrcweir 	{
742cdf0e10cSrcweir 		DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
743cdf0e10cSrcweir 	}
744cdf0e10cSrcweir 	return sRetPath;
745cdf0e10cSrcweir }
746cdf0e10cSrcweir 
747cdf0e10cSrcweir void SAL_CALL
setDefaultFilePath(const::rtl::OUString & DefaultFilePath)748cdf0e10cSrcweir ScVbaApplication::setDefaultFilePath( const ::rtl::OUString& DefaultFilePath ) throw (uno::RuntimeException)
749cdf0e10cSrcweir {
750cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xProps = lcl_getPathSettingsService( mxContext );
751cdf0e10cSrcweir 	rtl::OUString aURL;
752cdf0e10cSrcweir 	osl::FileBase::getFileURLFromSystemPath( DefaultFilePath, aURL );
753cdf0e10cSrcweir 	xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Work")), uno::Any( aURL ) );
754cdf0e10cSrcweir }
755cdf0e10cSrcweir 
756cdf0e10cSrcweir ::rtl::OUString SAL_CALL
getDefaultFilePath()757cdf0e10cSrcweir ScVbaApplication::getDefaultFilePath() throw (uno::RuntimeException)
758cdf0e10cSrcweir {
759cdf0e10cSrcweir 	return getOfficePath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Work")));
760cdf0e10cSrcweir }
761cdf0e10cSrcweir 
762cdf0e10cSrcweir ::rtl::OUString SAL_CALL
getLibraryPath()763cdf0e10cSrcweir ScVbaApplication::getLibraryPath() throw (uno::RuntimeException)
764cdf0e10cSrcweir {
765cdf0e10cSrcweir 	return getOfficePath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Basic")));
766cdf0e10cSrcweir }
767cdf0e10cSrcweir 
768cdf0e10cSrcweir ::rtl::OUString SAL_CALL
getTemplatesPath()769cdf0e10cSrcweir ScVbaApplication::getTemplatesPath() throw (uno::RuntimeException)
770cdf0e10cSrcweir {
771cdf0e10cSrcweir 	return getOfficePath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Template")));
772cdf0e10cSrcweir }
773cdf0e10cSrcweir 
774cdf0e10cSrcweir ::rtl::OUString SAL_CALL
getPathSeparator()775cdf0e10cSrcweir ScVbaApplication::getPathSeparator() throw (uno::RuntimeException)
776cdf0e10cSrcweir {
777cdf0e10cSrcweir 	static rtl::OUString sPathSep( RTL_CONSTASCII_USTRINGPARAM( FILE_PATH_SEPERATOR ) );
778cdf0e10cSrcweir 	return sPathSep;
779cdf0e10cSrcweir }
780cdf0e10cSrcweir 
781cdf0e10cSrcweir // ----------------------------------------------------------------------------
782cdf0e10cSrcweir // Helpers for Intersect and Union
783cdf0e10cSrcweir 
784cdf0e10cSrcweir namespace {
785cdf0e10cSrcweir 
786cdf0e10cSrcweir typedef ::std::list< ScRange > ListOfScRange;
787cdf0e10cSrcweir 
788cdf0e10cSrcweir /** Appends all ranges of a VBA Range object in the passed Any to the list of ranges. */
lclAddToListOfScRange(ListOfScRange & rList,const uno::Any & rArg)789cdf0e10cSrcweir void lclAddToListOfScRange( ListOfScRange& rList, const uno::Any& rArg )
790cdf0e10cSrcweir         throw (script::BasicErrorException, uno::RuntimeException)
791cdf0e10cSrcweir {
792cdf0e10cSrcweir     if( rArg.hasValue() )
793cdf0e10cSrcweir     {
794cdf0e10cSrcweir         uno::Reference< excel::XRange > xRange( rArg, uno::UNO_QUERY_THROW );
795cdf0e10cSrcweir         uno::Reference< XCollection > xCol( xRange->Areas( uno::Any() ), uno::UNO_QUERY_THROW );
796cdf0e10cSrcweir         for( sal_Int32 nIdx = 1, nCount = xCol->getCount(); nIdx <= nCount; ++nIdx )
797cdf0e10cSrcweir         {
798cdf0e10cSrcweir             uno::Reference< excel::XRange > xAreaRange( xCol->Item( uno::Any( nIdx ), uno::Any() ), uno::UNO_QUERY_THROW );
799cdf0e10cSrcweir             uno::Reference< sheet::XCellRangeAddressable > xAddressable( xAreaRange->getCellRange(), uno::UNO_QUERY_THROW );
800cdf0e10cSrcweir             ScRange aScRange;
801cdf0e10cSrcweir             ScUnoConversion::FillScRange( aScRange, xAddressable->getRangeAddress() );
802cdf0e10cSrcweir             rList.push_back( aScRange );
803cdf0e10cSrcweir         }
804cdf0e10cSrcweir     }
805cdf0e10cSrcweir }
806cdf0e10cSrcweir 
807cdf0e10cSrcweir /** Returns true, if the passed ranges can be expressed by a single range. The
808cdf0e10cSrcweir     new range will be contained in r1 then, the range r2 can be removed. */
lclTryJoin(ScRange & r1,const ScRange & r2)809cdf0e10cSrcweir bool lclTryJoin( ScRange& r1, const ScRange& r2 )
810cdf0e10cSrcweir {
811cdf0e10cSrcweir     // 1) r2 is completely inside r1
812cdf0e10cSrcweir     if( r1.In( r2 ) )
813cdf0e10cSrcweir         return true;
814cdf0e10cSrcweir 
815cdf0e10cSrcweir     // 2) r1 is completely inside r2
816cdf0e10cSrcweir     if( r2.In( r1 ) )
817cdf0e10cSrcweir     {
818cdf0e10cSrcweir         r1 = r2;
819cdf0e10cSrcweir         return true;
820cdf0e10cSrcweir     }
821cdf0e10cSrcweir 
822cdf0e10cSrcweir     SCCOL n1L = r1.aStart.Col();
823cdf0e10cSrcweir     SCCOL n1R = r1.aEnd.Col();
824cdf0e10cSrcweir     SCROW n1T = r1.aStart.Row();
825cdf0e10cSrcweir     SCROW n1B = r1.aEnd.Row();
826cdf0e10cSrcweir     SCCOL n2L = r2.aStart.Col();
827cdf0e10cSrcweir     SCCOL n2R = r2.aEnd.Col();
828cdf0e10cSrcweir     SCROW n2T = r2.aStart.Row();
829cdf0e10cSrcweir     SCROW n2B = r2.aEnd.Row();
830cdf0e10cSrcweir 
831cdf0e10cSrcweir     // 3) r1 and r2 have equal upper and lower border
832cdf0e10cSrcweir     if( (n1T == n2T) && (n1B == n2B) )
833cdf0e10cSrcweir     {
834cdf0e10cSrcweir         // check that r1 overlaps or touches r2
835cdf0e10cSrcweir         if( ((n1L < n2L) && (n2L - 1 <= n1R)) || ((n2L < n1L) && (n1L - 1 <= n2R)) )
836cdf0e10cSrcweir         {
837cdf0e10cSrcweir             r1.aStart.SetCol( ::std::min( n1L, n2L ) );
838cdf0e10cSrcweir             r1.aEnd.SetCol( ::std::max( n1R, n2R ) );
839cdf0e10cSrcweir             return true;
840cdf0e10cSrcweir         }
841cdf0e10cSrcweir         return false;
842cdf0e10cSrcweir     }
843cdf0e10cSrcweir 
844cdf0e10cSrcweir     // 4) r1 and r2 have equal left and right border
845cdf0e10cSrcweir     if( (n1L == n2L) && (n1R == n2R) )
846cdf0e10cSrcweir     {
847cdf0e10cSrcweir         // check that r1 overlaps or touches r2
848cdf0e10cSrcweir         if( ((n1T < n2T) && (n2T + 1 <= n1B)) || ((n2T < n1T) && (n1T + 1 <= n2B)) )
849cdf0e10cSrcweir         {
850cdf0e10cSrcweir             r1.aStart.SetRow( ::std::min( n1T, n2T ) );
851cdf0e10cSrcweir             r1.aEnd.SetRow( ::std::max( n1B, n2B ) );
852cdf0e10cSrcweir             return true;
853cdf0e10cSrcweir         }
854cdf0e10cSrcweir         return false;
855cdf0e10cSrcweir     }
856cdf0e10cSrcweir 
857cdf0e10cSrcweir     // 5) cannot join these ranges
858cdf0e10cSrcweir     return false;
859cdf0e10cSrcweir }
860cdf0e10cSrcweir 
861cdf0e10cSrcweir /** Strips out ranges that are contained by other ranges, joins ranges that can be joined
862cdf0e10cSrcweir     together (aligned borders, e.g. A4:D10 and B4:E10 would be combined to A4:E10. */
lclJoinRanges(ListOfScRange & rList)863cdf0e10cSrcweir void lclJoinRanges( ListOfScRange& rList )
864cdf0e10cSrcweir {
865cdf0e10cSrcweir     ListOfScRange::iterator aOuterIt = rList.begin();
866cdf0e10cSrcweir     while( aOuterIt != rList.end() )
867cdf0e10cSrcweir     {
868cdf0e10cSrcweir         bool bAnyErased = false;    // true = any range erased from rList
869cdf0e10cSrcweir         ListOfScRange::iterator aInnerIt = rList.begin();
870cdf0e10cSrcweir         while( aInnerIt != rList.end() )
871cdf0e10cSrcweir         {
872cdf0e10cSrcweir             bool bInnerErased = false;   // true = aInnerIt erased from rList
873cdf0e10cSrcweir             // do not compare a range with itself
874cdf0e10cSrcweir             if( (aOuterIt != aInnerIt) && lclTryJoin( *aOuterIt, *aInnerIt ) )
875cdf0e10cSrcweir             {
876cdf0e10cSrcweir                 // aOuterIt points to joined range, aInnerIt will be removed
877cdf0e10cSrcweir 				aInnerIt = rList.erase( aInnerIt );
878cdf0e10cSrcweir                 bInnerErased = bAnyErased = true;
879cdf0e10cSrcweir             }
880cdf0e10cSrcweir             /*  If aInnerIt has been erased from rList, it already points to
881cdf0e10cSrcweir                 the next element (return value of list::erase()). */
882cdf0e10cSrcweir             if( !bInnerErased )
883cdf0e10cSrcweir                 ++aInnerIt;
884cdf0e10cSrcweir 		}
885cdf0e10cSrcweir         // if any range has been erased, repeat outer loop with the same range
886cdf0e10cSrcweir         if( !bAnyErased )
887cdf0e10cSrcweir             ++aOuterIt;
888cdf0e10cSrcweir 	}
889cdf0e10cSrcweir }
890cdf0e10cSrcweir 
891cdf0e10cSrcweir /** Intersects the passed list with all ranges of a VBA Range object in the passed Any. */
lclIntersectRanges(ListOfScRange & rList,const uno::Any & rArg)892cdf0e10cSrcweir void lclIntersectRanges( ListOfScRange& rList, const uno::Any& rArg )
893cdf0e10cSrcweir         throw (script::BasicErrorException, uno::RuntimeException)
894cdf0e10cSrcweir {
895cdf0e10cSrcweir     // extract the ranges from the passed argument, will throw on invalid data
896cdf0e10cSrcweir     ListOfScRange aList2;
897cdf0e10cSrcweir     lclAddToListOfScRange( aList2, rArg );
898cdf0e10cSrcweir     // do nothing, if the passed list is already empty
899cdf0e10cSrcweir     if( !rList.empty() && !aList2.empty() )
900cdf0e10cSrcweir     {
901cdf0e10cSrcweir         // save original list in a local
902cdf0e10cSrcweir         ListOfScRange aList1;
903cdf0e10cSrcweir         aList1.swap( rList );
904cdf0e10cSrcweir         // join ranges from passed argument
905cdf0e10cSrcweir         lclJoinRanges( aList2 );
906cdf0e10cSrcweir         // calculate intersection of the ranges in both lists
907cdf0e10cSrcweir         for( ListOfScRange::const_iterator aOuterIt = aList1.begin(), aOuterEnd = aList1.end(); aOuterIt != aOuterEnd; ++aOuterIt )
908cdf0e10cSrcweir         {
909cdf0e10cSrcweir             for( ListOfScRange::const_iterator aInnerIt = aList2.begin(), aInnerEnd = aList2.end(); aInnerIt != aInnerEnd; ++aInnerIt )
910cdf0e10cSrcweir             {
911cdf0e10cSrcweir                 if( aOuterIt->Intersects( *aInnerIt ) )
912cdf0e10cSrcweir                 {
913cdf0e10cSrcweir                     ScRange aIsectRange(
914cdf0e10cSrcweir                         Max( aOuterIt->aStart.Col(), aInnerIt->aStart.Col() ),
915cdf0e10cSrcweir                         Max( aOuterIt->aStart.Row(), aInnerIt->aStart.Row() ),
916cdf0e10cSrcweir                         Max( aOuterIt->aStart.Tab(), aInnerIt->aStart.Tab() ),
917cdf0e10cSrcweir                         Min( aOuterIt->aEnd.Col(),   aInnerIt->aEnd.Col() ),
918cdf0e10cSrcweir                         Min( aOuterIt->aEnd.Row(),   aInnerIt->aEnd.Row() ),
919cdf0e10cSrcweir                         Min( aOuterIt->aEnd.Tab(),   aInnerIt->aEnd.Tab() ) );
920cdf0e10cSrcweir                     rList.push_back( aIsectRange );
921cdf0e10cSrcweir                 }
922cdf0e10cSrcweir             }
923cdf0e10cSrcweir         }
924cdf0e10cSrcweir         // again, join the result ranges
925cdf0e10cSrcweir         lclJoinRanges( rList );
926cdf0e10cSrcweir     }
927cdf0e10cSrcweir }
928cdf0e10cSrcweir 
929cdf0e10cSrcweir /** Creates a VBA Range object from the passed list of ranges. */
lclCreateVbaRange(const uno::Reference<uno::XComponentContext> & rxContext,const uno::Reference<frame::XModel> & rxModel,const ListOfScRange & rList)930cdf0e10cSrcweir uno::Reference< excel::XRange > lclCreateVbaRange(
931cdf0e10cSrcweir         const uno::Reference< uno::XComponentContext >& rxContext,
932cdf0e10cSrcweir         const uno::Reference< frame::XModel >& rxModel,
933cdf0e10cSrcweir         const ListOfScRange& rList ) throw (uno::RuntimeException)
934cdf0e10cSrcweir {
935cdf0e10cSrcweir     ScDocShell* pDocShell = excel::getDocShell( rxModel );
936cdf0e10cSrcweir     if( !pDocShell ) throw uno::RuntimeException();
937cdf0e10cSrcweir 
938cdf0e10cSrcweir 	ScRangeList aCellRanges;
939cdf0e10cSrcweir 	for( ListOfScRange::const_iterator aIt = rList.begin(), aEnd = rList.end(); aIt != aEnd; ++aIt )
940cdf0e10cSrcweir 		aCellRanges.Append( *aIt );
941cdf0e10cSrcweir 
942cdf0e10cSrcweir 	if( aCellRanges.Count() == 1 )
943cdf0e10cSrcweir 	{
944cdf0e10cSrcweir         uno::Reference< table::XCellRange > xRange( new ScCellRangeObj( pDocShell, *aCellRanges.First() ) );
945cdf0e10cSrcweir 		return new ScVbaRange( excel::getUnoSheetModuleObj( xRange ), rxContext, xRange );
946cdf0e10cSrcweir 	}
947cdf0e10cSrcweir 	if( aCellRanges.Count() > 1 )
948cdf0e10cSrcweir 	{
949cdf0e10cSrcweir 		uno::Reference< sheet::XSheetCellRangeContainer > xRanges( new ScCellRangesObj( pDocShell, aCellRanges ) );
950cdf0e10cSrcweir     	return new ScVbaRange( excel::getUnoSheetModuleObj( xRanges ), rxContext, xRanges );
951cdf0e10cSrcweir 	}
952cdf0e10cSrcweir 	return 0;
953cdf0e10cSrcweir }
954cdf0e10cSrcweir 
955cdf0e10cSrcweir } // namespace
956cdf0e10cSrcweir 
957cdf0e10cSrcweir // ----------------------------------------------------------------------------
958cdf0e10cSrcweir 
Intersect(const uno::Reference<excel::XRange> & rArg1,const uno::Reference<excel::XRange> & rArg2,const uno::Any & rArg3,const uno::Any & rArg4,const uno::Any & rArg5,const uno::Any & rArg6,const uno::Any & rArg7,const uno::Any & rArg8,const uno::Any & rArg9,const uno::Any & rArg10,const uno::Any & rArg11,const uno::Any & rArg12,const uno::Any & rArg13,const uno::Any & rArg14,const uno::Any & rArg15,const uno::Any & rArg16,const uno::Any & rArg17,const uno::Any & rArg18,const uno::Any & rArg19,const uno::Any & rArg20,const uno::Any & rArg21,const uno::Any & rArg22,const uno::Any & rArg23,const uno::Any & rArg24,const uno::Any & rArg25,const uno::Any & rArg26,const uno::Any & rArg27,const uno::Any & rArg28,const uno::Any & rArg29,const uno::Any & rArg30)959cdf0e10cSrcweir uno::Reference< excel::XRange > SAL_CALL ScVbaApplication::Intersect(
960cdf0e10cSrcweir         const uno::Reference< excel::XRange >& rArg1, const uno::Reference< excel::XRange >& rArg2,
961cdf0e10cSrcweir         const uno::Any& rArg3, const uno::Any& rArg4, const uno::Any& rArg5, const uno::Any& rArg6,
962cdf0e10cSrcweir         const uno::Any& rArg7, const uno::Any& rArg8, const uno::Any& rArg9, const uno::Any& rArg10,
963cdf0e10cSrcweir         const uno::Any& rArg11, const uno::Any& rArg12, const uno::Any& rArg13, const uno::Any& rArg14,
964cdf0e10cSrcweir         const uno::Any& rArg15, const uno::Any& rArg16, const uno::Any& rArg17, const uno::Any& rArg18,
965cdf0e10cSrcweir         const uno::Any& rArg19, const uno::Any& rArg20, const uno::Any& rArg21, const uno::Any& rArg22,
966cdf0e10cSrcweir         const uno::Any& rArg23, const uno::Any& rArg24, const uno::Any& rArg25, const uno::Any& rArg26,
967cdf0e10cSrcweir         const uno::Any& rArg27, const uno::Any& rArg28, const uno::Any& rArg29, const uno::Any& rArg30 )
968cdf0e10cSrcweir         throw (script::BasicErrorException, uno::RuntimeException)
969cdf0e10cSrcweir {
970cdf0e10cSrcweir     if( !rArg1.is() || !rArg2.is() )
971cdf0e10cSrcweir         DebugHelper::exception( SbERR_BAD_PARAMETER, rtl::OUString() );
972cdf0e10cSrcweir 
973cdf0e10cSrcweir     // initialize the result list with 1st parameter, join its ranges together
974cdf0e10cSrcweir     ListOfScRange aList;
975cdf0e10cSrcweir     lclAddToListOfScRange( aList, uno::Any( rArg1 ) );
976cdf0e10cSrcweir 	lclJoinRanges( aList );
977cdf0e10cSrcweir 
978cdf0e10cSrcweir     // process all other parameters, this updates the list with intersection
979cdf0e10cSrcweir     lclIntersectRanges( aList, uno::Any( rArg2 ) );
980cdf0e10cSrcweir     lclIntersectRanges( aList, rArg3 );
981cdf0e10cSrcweir     lclIntersectRanges( aList, rArg4 );
982cdf0e10cSrcweir     lclIntersectRanges( aList, rArg5 );
983cdf0e10cSrcweir     lclIntersectRanges( aList, rArg6 );
984cdf0e10cSrcweir     lclIntersectRanges( aList, rArg7 );
985cdf0e10cSrcweir     lclIntersectRanges( aList, rArg8 );
986cdf0e10cSrcweir     lclIntersectRanges( aList, rArg9 );
987cdf0e10cSrcweir     lclIntersectRanges( aList, rArg10 );
988cdf0e10cSrcweir     lclIntersectRanges( aList, rArg11 );
989cdf0e10cSrcweir     lclIntersectRanges( aList, rArg12 );
990cdf0e10cSrcweir     lclIntersectRanges( aList, rArg13 );
991cdf0e10cSrcweir     lclIntersectRanges( aList, rArg14 );
992cdf0e10cSrcweir     lclIntersectRanges( aList, rArg15 );
993cdf0e10cSrcweir     lclIntersectRanges( aList, rArg16 );
994cdf0e10cSrcweir     lclIntersectRanges( aList, rArg17 );
995cdf0e10cSrcweir     lclIntersectRanges( aList, rArg18 );
996cdf0e10cSrcweir     lclIntersectRanges( aList, rArg19 );
997cdf0e10cSrcweir     lclIntersectRanges( aList, rArg20 );
998cdf0e10cSrcweir     lclIntersectRanges( aList, rArg21 );
999cdf0e10cSrcweir     lclIntersectRanges( aList, rArg22 );
1000cdf0e10cSrcweir     lclIntersectRanges( aList, rArg23 );
1001cdf0e10cSrcweir     lclIntersectRanges( aList, rArg24 );
1002cdf0e10cSrcweir     lclIntersectRanges( aList, rArg25 );
1003cdf0e10cSrcweir     lclIntersectRanges( aList, rArg26 );
1004cdf0e10cSrcweir     lclIntersectRanges( aList, rArg27 );
1005cdf0e10cSrcweir     lclIntersectRanges( aList, rArg28 );
1006cdf0e10cSrcweir     lclIntersectRanges( aList, rArg29 );
1007cdf0e10cSrcweir     lclIntersectRanges( aList, rArg30 );
1008cdf0e10cSrcweir 
1009cdf0e10cSrcweir     // create the VBA Range object
1010cdf0e10cSrcweir     return lclCreateVbaRange( mxContext, getCurrentDocument(), aList );
1011cdf0e10cSrcweir }
1012cdf0e10cSrcweir 
Union(const uno::Reference<excel::XRange> & rArg1,const uno::Reference<excel::XRange> & rArg2,const uno::Any & rArg3,const uno::Any & rArg4,const uno::Any & rArg5,const uno::Any & rArg6,const uno::Any & rArg7,const uno::Any & rArg8,const uno::Any & rArg9,const uno::Any & rArg10,const uno::Any & rArg11,const uno::Any & rArg12,const uno::Any & rArg13,const uno::Any & rArg14,const uno::Any & rArg15,const uno::Any & rArg16,const uno::Any & rArg17,const uno::Any & rArg18,const uno::Any & rArg19,const uno::Any & rArg20,const uno::Any & rArg21,const uno::Any & rArg22,const uno::Any & rArg23,const uno::Any & rArg24,const uno::Any & rArg25,const uno::Any & rArg26,const uno::Any & rArg27,const uno::Any & rArg28,const uno::Any & rArg29,const uno::Any & rArg30)1013cdf0e10cSrcweir uno::Reference< excel::XRange > SAL_CALL ScVbaApplication::Union(
1014cdf0e10cSrcweir         const uno::Reference< excel::XRange >& rArg1, const uno::Reference< excel::XRange >& rArg2,
1015cdf0e10cSrcweir         const uno::Any& rArg3, const uno::Any& rArg4, const uno::Any& rArg5, const uno::Any& rArg6,
1016cdf0e10cSrcweir         const uno::Any& rArg7, const uno::Any& rArg8, const uno::Any& rArg9, const uno::Any& rArg10,
1017cdf0e10cSrcweir         const uno::Any& rArg11, const uno::Any& rArg12, const uno::Any& rArg13, const uno::Any& rArg14,
1018cdf0e10cSrcweir         const uno::Any& rArg15, const uno::Any& rArg16, const uno::Any& rArg17, const uno::Any& rArg18,
1019cdf0e10cSrcweir         const uno::Any& rArg19, const uno::Any& rArg20, const uno::Any& rArg21, const uno::Any& rArg22,
1020cdf0e10cSrcweir         const uno::Any& rArg23, const uno::Any& rArg24, const uno::Any& rArg25, const uno::Any& rArg26,
1021cdf0e10cSrcweir         const uno::Any& rArg27, const uno::Any& rArg28, const uno::Any& rArg29, const uno::Any& rArg30 )
1022cdf0e10cSrcweir         throw (script::BasicErrorException, uno::RuntimeException)
1023cdf0e10cSrcweir {
1024cdf0e10cSrcweir     if( !rArg1.is() || !rArg2.is() )
1025cdf0e10cSrcweir         DebugHelper::exception( SbERR_BAD_PARAMETER, rtl::OUString() );
1026cdf0e10cSrcweir 
1027cdf0e10cSrcweir     ListOfScRange aList;
1028cdf0e10cSrcweir     lclAddToListOfScRange( aList, uno::Any( rArg1 ) );
1029cdf0e10cSrcweir     lclAddToListOfScRange( aList, uno::Any( rArg2 ) );
1030cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg3 );
1031cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg4 );
1032cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg5 );
1033cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg6 );
1034cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg7 );
1035cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg8 );
1036cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg9 );
1037cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg10 );
1038cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg11 );
1039cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg12 );
1040cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg13 );
1041cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg14 );
1042cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg15 );
1043cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg16 );
1044cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg17 );
1045cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg18 );
1046cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg19 );
1047cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg20 );
1048cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg21 );
1049cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg22 );
1050cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg23 );
1051cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg24 );
1052cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg25 );
1053cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg26 );
1054cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg27 );
1055cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg28 );
1056cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg29 );
1057cdf0e10cSrcweir     lclAddToListOfScRange( aList, rArg30 );
1058cdf0e10cSrcweir 
1059cdf0e10cSrcweir     // simply join together all ranges as much as possible, strip out covered ranges etc.
1060cdf0e10cSrcweir 	lclJoinRanges( aList );
1061cdf0e10cSrcweir 
1062cdf0e10cSrcweir     // create the VBA Range object
1063cdf0e10cSrcweir     return lclCreateVbaRange( mxContext, getCurrentDocument(), aList );
1064cdf0e10cSrcweir }
1065cdf0e10cSrcweir 
1066cdf0e10cSrcweir void
Volatile(const uno::Any & aVolatile)1067cdf0e10cSrcweir ScVbaApplication::Volatile( const uno::Any& aVolatile )  throw ( uno::RuntimeException )
1068cdf0e10cSrcweir {
1069cdf0e10cSrcweir 	sal_Bool bVolatile = sal_True;
1070cdf0e10cSrcweir 	aVolatile >>= bVolatile;
1071cdf0e10cSrcweir 	return;
1072cdf0e10cSrcweir }
1073cdf0e10cSrcweir 
1074cdf0e10cSrcweir void SAL_CALL
DoEvents()1075cdf0e10cSrcweir ScVbaApplication::DoEvents() throw ( uno::RuntimeException )
1076cdf0e10cSrcweir {
1077cdf0e10cSrcweir }
1078cdf0e10cSrcweir ::sal_Bool SAL_CALL
getDisplayFormulaBar()1079cdf0e10cSrcweir ScVbaApplication::getDisplayFormulaBar() throw ( css::uno::RuntimeException )
1080cdf0e10cSrcweir {
1081cdf0e10cSrcweir 	sal_Bool bRes = sal_False;
1082cdf0e10cSrcweir 	ScTabViewShell* pViewShell = excel::getCurrentBestViewShell( mxContext );
1083cdf0e10cSrcweir 	if ( pViewShell )
1084cdf0e10cSrcweir 	{
1085cdf0e10cSrcweir 		SfxBoolItem sfxFormBar( FID_TOGGLEINPUTLINE);
1086cdf0e10cSrcweir 		SfxAllItemSet reqList(  SFX_APP()->GetPool() );
1087cdf0e10cSrcweir 		reqList.Put( sfxFormBar );
1088cdf0e10cSrcweir 
1089cdf0e10cSrcweir 		pViewShell->GetState( reqList );
1090cdf0e10cSrcweir 		const SfxPoolItem *pItem=0;
1091cdf0e10cSrcweir 		if ( reqList.GetItemState( FID_TOGGLEINPUTLINE, sal_False, &pItem ) == SFX_ITEM_SET )
1092cdf0e10cSrcweir 			bRes =   ((SfxBoolItem*)pItem)->GetValue();
1093cdf0e10cSrcweir 	}
1094cdf0e10cSrcweir 	return bRes;
1095cdf0e10cSrcweir }
1096cdf0e10cSrcweir 
1097cdf0e10cSrcweir void SAL_CALL
setDisplayFormulaBar(::sal_Bool _displayformulabar)1098cdf0e10cSrcweir ScVbaApplication::setDisplayFormulaBar( ::sal_Bool _displayformulabar ) throw ( css::uno::RuntimeException )
1099cdf0e10cSrcweir {
1100cdf0e10cSrcweir 	ScTabViewShell* pViewShell = excel::getCurrentBestViewShell( mxContext );
1101cdf0e10cSrcweir 	if ( pViewShell && ( _displayformulabar !=  getDisplayFormulaBar() ) )
1102cdf0e10cSrcweir 	{
1103cdf0e10cSrcweir 		SfxBoolItem sfxFormBar( FID_TOGGLEINPUTLINE, _displayformulabar);
1104cdf0e10cSrcweir 		SfxAllItemSet reqList(  SFX_APP()->GetPool() );
1105cdf0e10cSrcweir 		SfxRequest aReq( FID_TOGGLEINPUTLINE, 0, reqList );
1106cdf0e10cSrcweir 		pViewShell->Execute( aReq );
1107cdf0e10cSrcweir 	}
1108cdf0e10cSrcweir }
1109cdf0e10cSrcweir 
1110cdf0e10cSrcweir uno::Any SAL_CALL
Caller(const uno::Any &)1111cdf0e10cSrcweir ScVbaApplication::Caller( const uno::Any& /*aIndex*/ ) throw ( uno::RuntimeException )
1112cdf0e10cSrcweir {
1113cdf0e10cSrcweir 	StarBASIC* pBasic = SFX_APP()->GetBasic();
1114cdf0e10cSrcweir 	SbMethod* pMeth = (SbMethod*)pBasic->GetRtl()->Find( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FuncCaller") ), SbxCLASS_METHOD );
1115cdf0e10cSrcweir 	uno::Any aRet;
1116cdf0e10cSrcweir 	if ( pMeth )
1117cdf0e10cSrcweir 	{
1118cdf0e10cSrcweir 		SbxVariableRef refTemp = pMeth;
1119cdf0e10cSrcweir 		// forces a broadcast
1120cdf0e10cSrcweir 		SbxVariableRef pNew = new  SbxMethod( *((SbxMethod*)pMeth));
1121cdf0e10cSrcweir                 OSL_TRACE("pNew has type %d and string value %s", pNew->GetType(), rtl::OUStringToOString( pNew->GetString(), RTL_TEXTENCODING_UTF8 ).getStr() );
1122cdf0e10cSrcweir 		aRet = sbxToUnoValue( pNew );
1123cdf0e10cSrcweir 	}
1124cdf0e10cSrcweir 	return aRet;
1125cdf0e10cSrcweir }
1126cdf0e10cSrcweir 
GetOpenFilename(const uno::Any & rFileFilter,const uno::Any & rFilterIndex,const uno::Any & rTitle,const uno::Any & rButtonText,const uno::Any & rMultiSelect)1127cdf0e10cSrcweir uno::Any SAL_CALL ScVbaApplication::GetOpenFilename(
1128cdf0e10cSrcweir         const uno::Any& rFileFilter, const uno::Any& rFilterIndex, const uno::Any& rTitle,
1129cdf0e10cSrcweir         const uno::Any& rButtonText, const uno::Any& rMultiSelect ) throw (uno::RuntimeException)
1130cdf0e10cSrcweir {
1131cdf0e10cSrcweir     uno::Sequence< uno::Any > aArgs( 6 );
1132cdf0e10cSrcweir     aArgs[ 0 ] <<= getThisExcelDoc( mxContext );
1133cdf0e10cSrcweir     aArgs[ 1 ] = rFileFilter;
1134cdf0e10cSrcweir     aArgs[ 2 ] = rFilterIndex;
1135cdf0e10cSrcweir     aArgs[ 3 ] = rTitle;
1136cdf0e10cSrcweir     aArgs[ 4 ] = rButtonText;
1137cdf0e10cSrcweir     aArgs[ 5 ] = rMultiSelect;
1138cdf0e10cSrcweir 	uno::Reference< lang::XMultiComponentFactory > xFactory( mxContext->getServiceManager(), uno::UNO_SET_THROW );
1139cdf0e10cSrcweir 	uno::Reference< XExecutableDialog > xFilePicker( xFactory->createInstanceWithArgumentsAndContext(
1140cdf0e10cSrcweir         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.OpenFilePicker" ) ), aArgs, mxContext ), uno::UNO_QUERY_THROW );
1141cdf0e10cSrcweir     return xFilePicker->execute();
1142cdf0e10cSrcweir }
1143cdf0e10cSrcweir 
GetSaveAsFilename(const uno::Any & rInitialFileName,const uno::Any & rFileFilter,const uno::Any & rFilterIndex,const uno::Any & rTitle,const uno::Any & rButtonText)1144cdf0e10cSrcweir uno::Any SAL_CALL ScVbaApplication::GetSaveAsFilename(
1145cdf0e10cSrcweir         const uno::Any& rInitialFileName, const uno::Any& rFileFilter, const uno::Any& rFilterIndex,
1146cdf0e10cSrcweir         const uno::Any& rTitle, const uno::Any& rButtonText ) throw (uno::RuntimeException)
1147cdf0e10cSrcweir {
1148cdf0e10cSrcweir     uno::Sequence< uno::Any > aArgs( 6 );
1149cdf0e10cSrcweir     aArgs[ 0 ] <<= getThisExcelDoc( mxContext );
1150cdf0e10cSrcweir     aArgs[ 1 ] = rInitialFileName;
1151cdf0e10cSrcweir     aArgs[ 2 ] = rFileFilter;
1152cdf0e10cSrcweir     aArgs[ 3 ] = rFilterIndex;
1153cdf0e10cSrcweir     aArgs[ 4 ] = rTitle;
1154cdf0e10cSrcweir     aArgs[ 5 ] = rButtonText;
1155cdf0e10cSrcweir 	uno::Reference< lang::XMultiComponentFactory > xFactory( mxContext->getServiceManager(), uno::UNO_SET_THROW );
1156cdf0e10cSrcweir 	uno::Reference< XExecutableDialog > xFilePicker( xFactory->createInstanceWithArgumentsAndContext(
1157cdf0e10cSrcweir         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.SaveAsFilePicker" ) ), aArgs, mxContext ), uno::UNO_QUERY_THROW );
1158cdf0e10cSrcweir     return xFilePicker->execute();
1159cdf0e10cSrcweir }
1160cdf0e10cSrcweir 
1161cdf0e10cSrcweir uno::Reference< frame::XModel >
getCurrentDocument()1162cdf0e10cSrcweir ScVbaApplication::getCurrentDocument() throw (css::uno::RuntimeException)
1163cdf0e10cSrcweir {
1164cdf0e10cSrcweir     return getCurrentExcelDoc(mxContext);
1165cdf0e10cSrcweir }
1166cdf0e10cSrcweir 
1167cdf0e10cSrcweir rtl::OUString&
getServiceImplName()1168cdf0e10cSrcweir ScVbaApplication::getServiceImplName()
1169cdf0e10cSrcweir {
1170cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaApplication") );
1171cdf0e10cSrcweir 	return sImplName;
1172cdf0e10cSrcweir }
1173cdf0e10cSrcweir 
1174cdf0e10cSrcweir uno::Sequence< rtl::OUString >
getServiceNames()1175cdf0e10cSrcweir ScVbaApplication::getServiceNames()
1176cdf0e10cSrcweir {
1177cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
1178cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
1179cdf0e10cSrcweir 	{
1180cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
1181cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Application" ) );
1182cdf0e10cSrcweir 	}
1183cdf0e10cSrcweir 	return aServiceNames;
1184cdf0e10cSrcweir }
1185cdf0e10cSrcweir 
1186cdf0e10cSrcweir namespace application
1187cdf0e10cSrcweir {
1188cdf0e10cSrcweir namespace sdecl = comphelper::service_decl;
1189cdf0e10cSrcweir sdecl::vba_service_class_<ScVbaApplication, sdecl::with_args<false> > serviceImpl;
1190cdf0e10cSrcweir extern sdecl::ServiceDecl const serviceDecl(
1191cdf0e10cSrcweir     serviceImpl,
1192cdf0e10cSrcweir     "ScVbaApplication",
1193cdf0e10cSrcweir     "ooo.vba.excel.Application" );
1194cdf0e10cSrcweir }
1195