Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "vbahelper/vbaapplicationbase.hxx"
21 : #include <sal/macros.h>
22 :
23 : #include <com/sun/star/container/XIndexAccess.hpp>
24 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
26 : #include <com/sun/star/lang/XComponent.hpp>
27 : #include <com/sun/star/container/XEnumeration.hpp>
28 : #include <com/sun/star/frame/XLayoutManager.hpp>
29 : #include <com/sun/star/frame/XDesktop.hpp>
30 : #include <com/sun/star/container/XEnumerationAccess.hpp>
31 : #include <com/sun/star/document/XDocumentProperties.hpp>
32 : #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
33 : #include <com/sun/star/document/XEmbeddedScripts.hpp>
34 : #include <com/sun/star/awt/XWindow2.hpp>
35 :
36 : #include <boost/unordered_map.hpp>
37 : #include <filter/msfilter/msvbahelper.hxx>
38 : #include <tools/datetime.hxx>
39 :
40 : #include <basic/sbx.hxx>
41 : #include <basic/sbstar.hxx>
42 : #include <basic/sbuno.hxx>
43 : #include <basic/sbmeth.hxx>
44 : #include <basic/sbmod.hxx>
45 : #include <basic/vbahelper.hxx>
46 :
47 : #include "vbacommandbars.hxx"
48 :
49 : using namespace ::com::sun::star;
50 : using namespace ::ooo::vba;
51 :
52 : #define OFFICEVERSION "11.0"
53 :
54 : // ====VbaTimerInfo==================================
55 : typedef ::std::pair< OUString, ::std::pair< double, double > > VbaTimerInfo;
56 :
57 : // ====VbaTimer==================================
58 : class VbaTimer
59 : {
60 : Timer m_aTimer;
61 : VbaTimerInfo m_aTimerInfo;
62 : ::rtl::Reference< VbaApplicationBase > m_xBase;
63 :
64 : // the following declarations are here to prevent the usage of them
65 : VbaTimer( const VbaTimer& );
66 : VbaTimer& operator=( const VbaTimer& );
67 :
68 : public:
69 0 : VbaTimer()
70 0 : {}
71 :
72 0 : virtual ~VbaTimer()
73 0 : {
74 0 : m_aTimer.Stop();
75 0 : }
76 :
77 0 : static double GetNow()
78 : {
79 0 : Date aDateNow( Date::SYSTEM );
80 0 : Time aTimeNow( Time::SYSTEM );
81 0 : Date aRefDate( 1,1,1900 );
82 0 : long nDiffDays = (long)(aDateNow - aRefDate);
83 0 : nDiffDays += 2; // Change VisualBasic: 1.Jan.1900 == 2
84 :
85 0 : long nDiffSeconds = aTimeNow.GetHour() * 3600 + aTimeNow.GetMin() * 60 + aTimeNow.GetSec();
86 0 : return (double)nDiffDays + ((double)nDiffSeconds)/(double)(24*3600);
87 : }
88 :
89 0 : static sal_Int32 GetTimerMiliseconds( double nFrom, double nTo )
90 : {
91 0 : double nResult = nTo - nFrom;
92 0 : if ( nResult > 0 )
93 0 : nResult *= 24*3600*1000;
94 : else
95 0 : nResult = 50;
96 :
97 0 : return (sal_Int32) nResult;
98 : }
99 :
100 0 : void Start( const ::rtl::Reference< VbaApplicationBase > xBase, const OUString& aFunction, double nFrom, double nTo )
101 : {
102 0 : if ( !xBase.is() || aFunction.isEmpty() )
103 0 : throw uno::RuntimeException( "Unexpected arguments!" , uno::Reference< uno::XInterface >() );
104 :
105 0 : m_xBase = xBase;
106 0 : m_aTimerInfo = VbaTimerInfo( aFunction, ::std::pair< double, double >( nFrom, nTo ) );
107 0 : m_aTimer.SetTimeoutHdl( LINK( this, VbaTimer, MacroCallHdl ) );
108 0 : m_aTimer.SetTimeout( GetTimerMiliseconds( GetNow(), nFrom ) );
109 0 : m_aTimer.Start();
110 0 : }
111 :
112 : DECL_LINK( MacroCallHdl, void* );
113 : };
114 :
115 0 : IMPL_LINK_NOARG(VbaTimer, MacroCallHdl)
116 : {
117 0 : if ( m_aTimerInfo.second.second == 0 || GetNow() < m_aTimerInfo.second.second )
118 : {
119 0 : uno::Any aDummyArg;
120 : try
121 : {
122 0 : m_xBase->Run( m_aTimerInfo.first, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg, aDummyArg );
123 : }
124 0 : catch( uno::Exception& )
125 0 : {}
126 : }
127 :
128 : // mast be the last call in the method since it deletes the timer
129 : try
130 : {
131 0 : m_xBase->OnTime( uno::makeAny( m_aTimerInfo.second.first ), m_aTimerInfo.first, uno::makeAny( m_aTimerInfo.second.second ), uno::makeAny( sal_False ) );
132 0 : } catch( uno::Exception& )
133 : {}
134 :
135 0 : return 0;
136 : }
137 :
138 : // ====VbaTimerInfoHash==================================
139 : struct VbaTimerInfoHash
140 : {
141 0 : size_t operator()( const VbaTimerInfo& rTimerInfo ) const
142 : {
143 0 : return (size_t)rTimerInfo.first.hashCode()
144 0 : + (size_t)rtl_str_hashCode_WithLength( (char*)&rTimerInfo.second.first, sizeof( double ) )
145 0 : + (size_t)rtl_str_hashCode_WithLength( (char*)&rTimerInfo.second.second, sizeof( double ) );
146 : }
147 : };
148 :
149 : // ====VbaTimerHashMap==================================
150 : typedef ::boost::unordered_map< VbaTimerInfo, VbaTimer*, VbaTimerInfoHash, ::std::equal_to< VbaTimerInfo > > VbaTimerHashMap;
151 :
152 : // ====VbaApplicationBase_Impl==================================
153 : struct VbaApplicationBase_Impl
154 : {
155 : VbaTimerHashMap m_aTimerHash;
156 : sal_Bool mbVisible;
157 :
158 1 : inline VbaApplicationBase_Impl() : mbVisible( sal_True ) {}
159 :
160 2 : virtual ~VbaApplicationBase_Impl()
161 2 : {
162 : // remove the remaining timers
163 3 : for ( VbaTimerHashMap::iterator aIter = m_aTimerHash.begin();
164 2 : aIter != m_aTimerHash.end();
165 : ++aIter )
166 : {
167 0 : delete aIter->second;
168 0 : aIter->second = NULL;
169 : }
170 2 : }
171 : };
172 :
173 : // ====VbaApplicationBase==================================
174 1 : VbaApplicationBase::VbaApplicationBase( const uno::Reference< uno::XComponentContext >& xContext )
175 : : ApplicationBase_BASE( uno::Reference< XHelperInterface >(), xContext )
176 1 : , m_pImpl( new VbaApplicationBase_Impl )
177 : {
178 1 : }
179 :
180 2 : VbaApplicationBase::~VbaApplicationBase()
181 : {
182 1 : delete m_pImpl;
183 1 : }
184 :
185 : sal_Bool SAL_CALL
186 0 : VbaApplicationBase::getScreenUpdating() throw (uno::RuntimeException)
187 : {
188 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
189 0 : return !xModel->hasControllersLocked();
190 : }
191 :
192 : void SAL_CALL
193 0 : VbaApplicationBase::setScreenUpdating(sal_Bool bUpdate) throw (uno::RuntimeException)
194 : {
195 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
196 : // #163808# use helper from module "basic" to lock all documents of this application
197 0 : ::basic::vba::lockControllersOfAllDocuments( xModel, !bUpdate );
198 0 : }
199 :
200 : sal_Bool SAL_CALL
201 0 : VbaApplicationBase::getDisplayStatusBar() throw (uno::RuntimeException)
202 : {
203 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
204 0 : uno::Reference< frame::XFrame > xFrame( xModel->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
205 0 : uno::Reference< beans::XPropertySet > xProps( xFrame, uno::UNO_QUERY_THROW );
206 :
207 0 : if( xProps.is() ){
208 0 : uno::Reference< frame::XLayoutManager > xLayoutManager( xProps->getPropertyValue( "LayoutManager"), uno::UNO_QUERY_THROW );
209 0 : OUString url( "private:resource/statusbar/statusbar" );
210 0 : if( xLayoutManager.is() && xLayoutManager->isElementVisible( url ) ){
211 0 : return sal_True;
212 0 : }
213 : }
214 0 : return sal_False;
215 : }
216 :
217 : void SAL_CALL
218 0 : VbaApplicationBase::setDisplayStatusBar(sal_Bool bDisplayStatusBar) throw (uno::RuntimeException)
219 : {
220 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
221 0 : uno::Reference< frame::XFrame > xFrame( xModel->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
222 0 : uno::Reference< beans::XPropertySet > xProps( xFrame, uno::UNO_QUERY_THROW );
223 :
224 0 : if( xProps.is() ){
225 0 : uno::Reference< frame::XLayoutManager > xLayoutManager( xProps->getPropertyValue( "LayoutManager" ), uno::UNO_QUERY_THROW );
226 0 : OUString url( "private:resource/statusbar/statusbar" );
227 0 : if( xLayoutManager.is() ){
228 0 : if( bDisplayStatusBar && !xLayoutManager->isElementVisible( url ) ){
229 0 : if( !xLayoutManager->showElement( url ) )
230 0 : xLayoutManager->createElement( url );
231 : return;
232 : }
233 0 : else if( !bDisplayStatusBar && xLayoutManager->isElementVisible( url ) ){
234 0 : xLayoutManager->hideElement( url );
235 : return;
236 : }
237 0 : }
238 : }
239 0 : return;
240 : }
241 :
242 0 : ::sal_Bool SAL_CALL VbaApplicationBase::getInteractive()
243 : throw (uno::RuntimeException)
244 : {
245 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
246 0 : uno::Reference< frame::XFrame > xFrame( xModel->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
247 0 : uno::Reference< awt::XWindow2 > xWindow( xFrame->getContainerWindow(), uno::UNO_QUERY_THROW );
248 :
249 0 : return xWindow->isEnabled();
250 : }
251 :
252 0 : void SAL_CALL VbaApplicationBase::setInteractive( ::sal_Bool bInteractive )
253 : throw (uno::RuntimeException)
254 : {
255 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
256 : // #163808# use helper from module "basic" to enable/disable all container windows of all documents of this application
257 0 : ::basic::vba::enableContainerWindowsOfAllDocuments( xModel, bInteractive );
258 0 : }
259 :
260 0 : sal_Bool SAL_CALL VbaApplicationBase::getVisible() throw (uno::RuntimeException)
261 : {
262 0 : return m_pImpl->mbVisible; // dummy implementation
263 : }
264 :
265 0 : void SAL_CALL VbaApplicationBase::setVisible( sal_Bool bVisible ) throw (uno::RuntimeException)
266 : {
267 0 : m_pImpl->mbVisible = bVisible; // dummy implementation
268 0 : }
269 :
270 :
271 : void SAL_CALL
272 0 : VbaApplicationBase::OnKey( const OUString& Key, const uno::Any& Procedure ) throw (uno::RuntimeException)
273 : {
274 : // parse the Key & modifiers
275 0 : awt::KeyEvent aKeyEvent = parseKeyEvent( Key );
276 0 : OUString MacroName;
277 0 : Procedure >>= MacroName;
278 0 : uno::Reference< frame::XModel > xModel;
279 0 : SbMethod* pMeth = StarBASIC::GetActiveMethod();
280 0 : if ( pMeth )
281 : {
282 0 : SbModule* pMod = dynamic_cast< SbModule* >( pMeth->GetParent() );
283 0 : if ( pMod )
284 0 : xModel = StarBASIC::GetModelFromBasic( pMod );
285 : }
286 :
287 0 : if ( !xModel.is() )
288 0 : xModel = getCurrentDocument();
289 :
290 0 : applyShortCutKeyBinding( xModel, aKeyEvent, MacroName );
291 0 : }
292 :
293 : uno::Any SAL_CALL
294 0 : VbaApplicationBase::CommandBars( const uno::Any& aIndex ) throw (uno::RuntimeException)
295 : {
296 0 : uno::Reference< XCommandBars > xCommandBars( new ScVbaCommandBars( this, mxContext, uno::Reference< container::XIndexAccess >(), getCurrentDocument() ) );
297 0 : if( aIndex.hasValue() )
298 0 : return uno::makeAny( xCommandBars->Item( aIndex, uno::Any() ) );
299 0 : return uno::makeAny( xCommandBars );
300 : }
301 :
302 : OUString SAL_CALL
303 0 : VbaApplicationBase::getVersion() throw (uno::RuntimeException)
304 : {
305 0 : return OUString(OFFICEVERSION);
306 : }
307 :
308 0 : uno::Any SAL_CALL VbaApplicationBase::Run( const OUString& MacroName, const uno::Any& varg1, const uno::Any& varg2, const uno::Any& varg3, const uno::Any& varg4, const uno::Any& varg5, const uno::Any& varg6, const uno::Any& varg7, const uno::Any& varg8, const uno::Any& varg9, const uno::Any& varg10, const uno::Any& varg11, const uno::Any& varg12, const uno::Any& varg13, const uno::Any& varg14, const uno::Any& varg15, const uno::Any& varg16, const uno::Any& varg17, const uno::Any& varg18, const uno::Any& varg19, const uno::Any& varg20, const uno::Any& varg21, const uno::Any& varg22, const uno::Any& varg23, const uno::Any& varg24, const uno::Any& varg25, const uno::Any& varg26, const uno::Any& varg27, const uno::Any& varg28, const uno::Any& varg29, const uno::Any& varg30 ) throw (uno::RuntimeException)
309 : {
310 0 : OUString aMacroName = MacroName.trim();
311 0 : if (0 == aMacroName.indexOf('!'))
312 0 : aMacroName = aMacroName.copy(1).trim();
313 :
314 0 : uno::Reference< frame::XModel > xModel;
315 0 : SbMethod* pMeth = StarBASIC::GetActiveMethod();
316 0 : if ( pMeth )
317 : {
318 0 : SbModule* pMod = dynamic_cast< SbModule* >( pMeth->GetParent() );
319 0 : if ( pMod )
320 0 : xModel = StarBASIC::GetModelFromBasic( pMod );
321 : }
322 :
323 0 : if ( !xModel.is() )
324 0 : xModel = getCurrentDocument();
325 :
326 0 : MacroResolvedInfo aMacroInfo = resolveVBAMacro( getSfxObjShell( xModel ), aMacroName );
327 0 : if( aMacroInfo.mbFound )
328 : {
329 : // handle the arguments
330 0 : const uno::Any* aArgsPtrArray[] = { &varg1, &varg2, &varg3, &varg4, &varg5, &varg6, &varg7, &varg8, &varg9, &varg10, &varg11, &varg12, &varg13, &varg14, &varg15, &varg16, &varg17, &varg18, &varg19, &varg20, &varg21, &varg22, &varg23, &varg24, &varg25, &varg26, &varg27, &varg28, &varg29, &varg30 };
331 :
332 0 : int nArg = sizeof( aArgsPtrArray ) / sizeof( aArgsPtrArray[0] );
333 0 : uno::Sequence< uno::Any > aArgs( nArg );
334 :
335 0 : const uno::Any** pArg = aArgsPtrArray;
336 0 : const uno::Any** pArgEnd = ( aArgsPtrArray + nArg );
337 :
338 0 : sal_Int32 nArgProcessed = 0;
339 :
340 0 : for ( ; pArg != pArgEnd; ++pArg, ++nArgProcessed )
341 0 : aArgs[ nArgProcessed ] = **pArg;
342 :
343 : // resize array to position of last param with value
344 0 : aArgs.realloc( nArgProcessed + 1 );
345 :
346 0 : uno::Any aRet;
347 0 : uno::Any aDummyCaller;
348 0 : executeMacro( aMacroInfo.mpDocContext, aMacroInfo.msResolvedMacro, aArgs, aRet, aDummyCaller );
349 :
350 0 : return aRet;
351 : }
352 : else
353 : {
354 0 : throw uno::RuntimeException( "The macro doesn't exist" , uno::Reference< uno::XInterface >() );
355 0 : }
356 : }
357 :
358 0 : void SAL_CALL VbaApplicationBase::OnTime( const uno::Any& aEarliestTime, const OUString& aFunction, const uno::Any& aLatestTime, const uno::Any& aSchedule )
359 : throw ( uno::RuntimeException )
360 : {
361 0 : if ( aFunction.isEmpty() )
362 0 : throw uno::RuntimeException( "Unexpected function name!" , uno::Reference< uno::XInterface >() );
363 :
364 0 : double nEarliestTime = 0;
365 0 : double nLatestTime = 0;
366 0 : if ( !( aEarliestTime >>= nEarliestTime )
367 0 : || ( aLatestTime.hasValue() && !( aLatestTime >>= nLatestTime ) ) )
368 0 : throw uno::RuntimeException( "Only double is supported as time for now!" , uno::Reference< uno::XInterface >() );
369 :
370 0 : sal_Bool bSetTimer = sal_True;
371 0 : aSchedule >>= bSetTimer;
372 :
373 0 : VbaTimerInfo aTimerIndex( aFunction, ::std::pair< double, double >( nEarliestTime, nLatestTime ) );
374 :
375 0 : VbaTimerHashMap::iterator aIter = m_pImpl->m_aTimerHash.find( aTimerIndex );
376 0 : if ( aIter != m_pImpl->m_aTimerHash.end() )
377 : {
378 0 : delete aIter->second;
379 0 : aIter->second = NULL;
380 0 : m_pImpl->m_aTimerHash.erase( aIter );
381 : }
382 :
383 0 : if ( bSetTimer )
384 : {
385 0 : VbaTimer* pTimer = new VbaTimer;
386 0 : m_pImpl->m_aTimerHash[ aTimerIndex ] = pTimer;
387 0 : pTimer->Start( this, aFunction, nEarliestTime, nLatestTime );
388 0 : }
389 0 : }
390 :
391 0 : float SAL_CALL VbaApplicationBase::CentimetersToPoints( float _Centimeters ) throw (uno::RuntimeException)
392 : {
393 : // i cm = 28.35 points
394 : static const float rate = 28.35f;
395 0 : return ( _Centimeters * rate );
396 : }
397 :
398 0 : uno::Any SAL_CALL VbaApplicationBase::getVBE() throw (uno::RuntimeException)
399 : {
400 : try // return empty object on error
401 : {
402 : // "VBE" object does not have a parent, but pass document model to be able to determine application type
403 0 : uno::Sequence< uno::Any > aArgs( 1 );
404 0 : aArgs[ 0 ] <<= getCurrentDocument();
405 0 : uno::Reference< lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
406 0 : uno::Reference< uno::XInterface > xVBE = xServiceManager->createInstanceWithArgumentsAndContext(
407 0 : "ooo.vba.vbide.VBE" , aArgs, mxContext );
408 0 : return uno::Any( xVBE );
409 : }
410 0 : catch( const uno::Exception& )
411 : {
412 : }
413 0 : return uno::Any();
414 : }
415 :
416 : OUString
417 0 : VbaApplicationBase::getServiceImplName()
418 : {
419 0 : return OUString("VbaApplicationBase");
420 : }
421 :
422 : uno::Sequence<OUString>
423 0 : VbaApplicationBase::getServiceNames()
424 : {
425 0 : static uno::Sequence< OUString > aServiceNames;
426 0 : if ( aServiceNames.getLength() == 0 )
427 : {
428 0 : aServiceNames.realloc( 1 );
429 0 : aServiceNames[ 0 ] = "ooo.vba.VbaApplicationBase";
430 : }
431 0 : return aServiceNames;
432 : }
433 :
434 0 : void SAL_CALL VbaApplicationBase::Undo()
435 : throw (uno::RuntimeException)
436 : {
437 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
438 0 : dispatchRequests( xModel, ".uno:Undo" );
439 0 : }
440 :
441 0 : void VbaApplicationBase::Quit() throw (uno::RuntimeException)
442 : {
443 : // need to stop basic
444 0 : SbMethod* pMeth = StarBASIC::GetActiveMethod();
445 0 : if ( pMeth )
446 : {
447 0 : SbModule* pMod = dynamic_cast< SbModule* >( pMeth->GetParent() );
448 0 : if ( pMod )
449 : {
450 0 : StarBASIC* pBasic = dynamic_cast< StarBASIC* >( pMod->GetParent() );
451 0 : if ( pBasic )
452 0 : pBasic->QuitAndExitApplication();
453 : }
454 : }
455 0 : }
456 :
457 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|