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