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/noncopyable.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 : #include <unordered_map>
50 :
51 : using namespace ::com::sun::star;
52 : using namespace ::ooo::vba;
53 :
54 : #define OFFICEVERSION "11.0"
55 :
56 : // ====VbaTimerInfo==================================
57 : typedef ::std::pair< OUString, ::std::pair< double, double > > VbaTimerInfo;
58 :
59 : // ====VbaTimer==================================
60 : class VbaTimer: private boost::noncopyable
61 : {
62 : Timer m_aTimer;
63 : VbaTimerInfo m_aTimerInfo;
64 : ::rtl::Reference< VbaApplicationBase > m_xBase;
65 :
66 : public:
67 0 : VbaTimer()
68 0 : {}
69 :
70 0 : virtual ~VbaTimer()
71 0 : {
72 0 : m_aTimer.Stop();
73 0 : }
74 :
75 0 : static double GetNow()
76 : {
77 0 : Date aDateNow( Date::SYSTEM );
78 0 : tools::Time aTimeNow( tools::Time::SYSTEM );
79 0 : Date aRefDate( 1,1,1900 );
80 0 : long nDiffDays = (long)(aDateNow - aRefDate);
81 0 : nDiffDays += 2; // Change VisualBasic: 1.Jan.1900 == 2
82 :
83 0 : long nDiffSeconds = aTimeNow.GetHour() * 3600 + aTimeNow.GetMin() * 60 + aTimeNow.GetSec();
84 0 : return (double)nDiffDays + ((double)nDiffSeconds)/(double)(24*3600);
85 : }
86 :
87 0 : static sal_Int32 GetTimerMiliseconds( double nFrom, double nTo )
88 : {
89 0 : double nResult = nTo - nFrom;
90 0 : if ( nResult > 0 )
91 0 : nResult *= 24*3600*1000;
92 : else
93 0 : nResult = 50;
94 :
95 0 : return (sal_Int32) nResult;
96 : }
97 :
98 0 : void Start( const ::rtl::Reference< VbaApplicationBase >& xBase, const OUString& aFunction, double nFrom, double nTo )
99 : {
100 0 : if ( !xBase.is() || aFunction.isEmpty() )
101 0 : throw uno::RuntimeException( "Unexpected arguments!" );
102 :
103 0 : m_xBase = xBase;
104 0 : m_aTimerInfo = VbaTimerInfo( aFunction, ::std::pair< double, double >( nFrom, nTo ) );
105 0 : m_aTimer.SetTimeoutHdl( LINK( this, VbaTimer, MacroCallHdl ) );
106 0 : m_aTimer.SetTimeout( GetTimerMiliseconds( GetNow(), nFrom ) );
107 0 : m_aTimer.Start();
108 0 : }
109 :
110 : DECL_LINK_TYPED( MacroCallHdl, Timer*, void );
111 : };
112 :
113 0 : IMPL_LINK_NOARG_TYPED(VbaTimer, MacroCallHdl, Timer *, void)
114 : {
115 0 : if ( m_aTimerInfo.second.second == 0 || GetNow() < m_aTimerInfo.second.second )
116 : {
117 0 : uno::Any aDummyArg;
118 : try
119 : {
120 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 );
121 : }
122 0 : catch( uno::Exception& )
123 0 : {}
124 : }
125 :
126 : // mast be the last call in the method since it deletes the timer
127 : try
128 : {
129 0 : m_xBase->OnTime( uno::makeAny( m_aTimerInfo.second.first ), m_aTimerInfo.first, uno::makeAny( m_aTimerInfo.second.second ), uno::makeAny( sal_False ) );
130 0 : } catch( uno::Exception& )
131 : {}
132 0 : }
133 :
134 : // ====VbaTimerInfoHash==================================
135 : struct VbaTimerInfoHash
136 : {
137 0 : size_t operator()( const VbaTimerInfo& rTimerInfo ) const
138 : {
139 0 : return (size_t)rTimerInfo.first.hashCode()
140 0 : + (size_t)rtl_str_hashCode_WithLength( reinterpret_cast<char const *>(&rTimerInfo.second.first), sizeof( double ) )
141 0 : + (size_t)rtl_str_hashCode_WithLength( reinterpret_cast<char const *>(&rTimerInfo.second.second), sizeof( double ) );
142 : }
143 : };
144 :
145 : // ====VbaTimerHashMap==================================
146 : typedef std::unordered_map< VbaTimerInfo, VbaTimer*, VbaTimerInfoHash, std::equal_to< VbaTimerInfo > > VbaTimerHashMap;
147 :
148 : // ====VbaApplicationBase_Impl==================================
149 : struct VbaApplicationBase_Impl
150 : {
151 : VbaTimerHashMap m_aTimerHash;
152 : bool mbVisible;
153 :
154 29 : inline VbaApplicationBase_Impl() : mbVisible( true ) {}
155 :
156 58 : virtual ~VbaApplicationBase_Impl()
157 58 : {
158 : // remove the remaining timers
159 87 : for ( VbaTimerHashMap::iterator aIter = m_aTimerHash.begin();
160 58 : aIter != m_aTimerHash.end();
161 : ++aIter )
162 : {
163 0 : delete aIter->second;
164 0 : aIter->second = NULL;
165 : }
166 58 : }
167 : };
168 :
169 : // ====VbaApplicationBase==================================
170 29 : VbaApplicationBase::VbaApplicationBase( const uno::Reference< uno::XComponentContext >& xContext )
171 : : ApplicationBase_BASE( uno::Reference< XHelperInterface >(), xContext )
172 29 : , m_pImpl( new VbaApplicationBase_Impl )
173 : {
174 29 : }
175 :
176 58 : VbaApplicationBase::~VbaApplicationBase()
177 : {
178 29 : delete m_pImpl;
179 29 : }
180 :
181 : sal_Bool SAL_CALL
182 1 : VbaApplicationBase::getScreenUpdating() throw (uno::RuntimeException, std::exception)
183 : {
184 1 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
185 1 : return !xModel->hasControllersLocked();
186 : }
187 :
188 : void SAL_CALL
189 0 : VbaApplicationBase::setScreenUpdating(sal_Bool bUpdate) throw (uno::RuntimeException, std::exception)
190 : {
191 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
192 : // #163808# use helper from module "basic" to lock all documents of this application
193 0 : ::basic::vba::lockControllersOfAllDocuments( xModel, !bUpdate );
194 0 : }
195 :
196 : sal_Bool SAL_CALL
197 0 : VbaApplicationBase::getDisplayStatusBar() throw (uno::RuntimeException, std::exception)
198 : {
199 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
200 0 : uno::Reference< frame::XFrame > xFrame( xModel->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
201 0 : uno::Reference< beans::XPropertySet > xProps( xFrame, uno::UNO_QUERY_THROW );
202 :
203 0 : if( xProps.is() ){
204 0 : uno::Reference< frame::XLayoutManager > xLayoutManager( xProps->getPropertyValue( "LayoutManager"), uno::UNO_QUERY_THROW );
205 0 : OUString url( "private:resource/statusbar/statusbar" );
206 0 : if( xLayoutManager.is() && xLayoutManager->isElementVisible( url ) ){
207 0 : return sal_True;
208 0 : }
209 : }
210 0 : return sal_False;
211 : }
212 :
213 : void SAL_CALL
214 0 : VbaApplicationBase::setDisplayStatusBar(sal_Bool bDisplayStatusBar) throw (uno::RuntimeException, std::exception)
215 : {
216 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
217 0 : uno::Reference< frame::XFrame > xFrame( xModel->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
218 0 : uno::Reference< beans::XPropertySet > xProps( xFrame, uno::UNO_QUERY_THROW );
219 :
220 0 : if( xProps.is() ){
221 0 : uno::Reference< frame::XLayoutManager > xLayoutManager( xProps->getPropertyValue( "LayoutManager" ), uno::UNO_QUERY_THROW );
222 0 : OUString url( "private:resource/statusbar/statusbar" );
223 0 : if( xLayoutManager.is() ){
224 0 : if( bDisplayStatusBar && !xLayoutManager->isElementVisible( url ) ){
225 0 : if( !xLayoutManager->showElement( url ) )
226 0 : xLayoutManager->createElement( url );
227 0 : return;
228 : }
229 0 : else if( !bDisplayStatusBar && xLayoutManager->isElementVisible( url ) ){
230 0 : xLayoutManager->hideElement( url );
231 0 : return;
232 : }
233 0 : }
234 : }
235 0 : return;
236 : }
237 :
238 1 : sal_Bool SAL_CALL VbaApplicationBase::getInteractive()
239 : throw (uno::RuntimeException, std::exception)
240 : {
241 1 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
242 2 : uno::Reference< frame::XFrame > xFrame( xModel->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
243 2 : uno::Reference< awt::XWindow2 > xWindow( xFrame->getContainerWindow(), uno::UNO_QUERY_THROW );
244 :
245 2 : return xWindow->isEnabled();
246 : }
247 :
248 0 : void SAL_CALL VbaApplicationBase::setInteractive( sal_Bool bInteractive )
249 : throw (uno::RuntimeException, std::exception)
250 : {
251 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
252 : // #163808# use helper from module "basic" to enable/disable all container windows of all documents of this application
253 0 : ::basic::vba::enableContainerWindowsOfAllDocuments( xModel, bInteractive );
254 0 : }
255 :
256 0 : sal_Bool SAL_CALL VbaApplicationBase::getVisible() throw (uno::RuntimeException, std::exception)
257 : {
258 0 : return m_pImpl->mbVisible; // dummy implementation
259 : }
260 :
261 0 : void SAL_CALL VbaApplicationBase::setVisible( sal_Bool bVisible ) throw (uno::RuntimeException, std::exception)
262 : {
263 0 : m_pImpl->mbVisible = bVisible; // dummy implementation
264 0 : }
265 :
266 :
267 : void SAL_CALL
268 0 : VbaApplicationBase::OnKey( const OUString& Key, const uno::Any& Procedure ) throw (uno::RuntimeException, std::exception)
269 : {
270 : // parse the Key & modifiers
271 0 : awt::KeyEvent aKeyEvent = parseKeyEvent( Key );
272 0 : OUString MacroName;
273 0 : Procedure >>= MacroName;
274 0 : uno::Reference< frame::XModel > xModel;
275 0 : SbMethod* pMeth = StarBASIC::GetActiveMethod();
276 0 : if ( pMeth )
277 : {
278 0 : SbModule* pMod = dynamic_cast< SbModule* >( pMeth->GetParent() );
279 0 : if ( pMod )
280 0 : xModel = StarBASIC::GetModelFromBasic( pMod );
281 : }
282 :
283 0 : if ( !xModel.is() )
284 0 : xModel = getCurrentDocument();
285 :
286 0 : applyShortCutKeyBinding( xModel, aKeyEvent, MacroName );
287 0 : }
288 :
289 : uno::Any SAL_CALL
290 0 : VbaApplicationBase::CommandBars( const uno::Any& aIndex ) throw (uno::RuntimeException, std::exception)
291 : {
292 0 : uno::Reference< XCommandBars > xCommandBars( new ScVbaCommandBars( this, mxContext, uno::Reference< container::XIndexAccess >(), getCurrentDocument() ) );
293 0 : if( aIndex.hasValue() )
294 0 : return xCommandBars->Item( aIndex, uno::Any() );
295 0 : return uno::makeAny( xCommandBars );
296 : }
297 :
298 : OUString SAL_CALL
299 0 : VbaApplicationBase::getVersion() throw (uno::RuntimeException, std::exception)
300 : {
301 0 : return OUString(OFFICEVERSION);
302 : }
303 :
304 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, std::exception)
305 : {
306 0 : OUString aMacroName = MacroName.trim();
307 0 : if( aMacroName.startsWith("!") )
308 0 : aMacroName = aMacroName.copy(1).trim();
309 :
310 0 : uno::Reference< frame::XModel > xModel;
311 0 : SbMethod* pMeth = StarBASIC::GetActiveMethod();
312 0 : if ( pMeth )
313 : {
314 0 : SbModule* pMod = dynamic_cast< SbModule* >( pMeth->GetParent() );
315 0 : if ( pMod )
316 0 : xModel = StarBASIC::GetModelFromBasic( pMod );
317 : }
318 :
319 0 : if ( !xModel.is() )
320 0 : xModel = getCurrentDocument();
321 :
322 0 : MacroResolvedInfo aMacroInfo = resolveVBAMacro( getSfxObjShell( xModel ), aMacroName );
323 0 : if( aMacroInfo.mbFound )
324 : {
325 : // handle the arguments
326 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 };
327 :
328 0 : int nArg = sizeof( aArgsPtrArray ) / sizeof( aArgsPtrArray[0] );
329 0 : uno::Sequence< uno::Any > aArgs( nArg );
330 :
331 0 : const uno::Any** pArg = aArgsPtrArray;
332 0 : const uno::Any** pArgEnd = ( aArgsPtrArray + nArg );
333 :
334 0 : sal_Int32 nArgProcessed = 0;
335 :
336 0 : for ( ; pArg != pArgEnd; ++pArg, ++nArgProcessed )
337 0 : aArgs[ nArgProcessed ] = **pArg;
338 :
339 : // resize array to position of last param with value
340 0 : aArgs.realloc( nArgProcessed + 1 );
341 :
342 0 : uno::Any aRet;
343 0 : uno::Any aDummyCaller;
344 0 : executeMacro( aMacroInfo.mpDocContext, aMacroInfo.msResolvedMacro, aArgs, aRet, aDummyCaller );
345 :
346 0 : return aRet;
347 : }
348 : else
349 : {
350 0 : throw uno::RuntimeException( "The macro doesn't exist" );
351 0 : }
352 : }
353 :
354 0 : void SAL_CALL VbaApplicationBase::OnTime( const uno::Any& aEarliestTime, const OUString& aFunction, const uno::Any& aLatestTime, const uno::Any& aSchedule )
355 : throw ( uno::RuntimeException, std::exception )
356 : {
357 0 : if ( aFunction.isEmpty() )
358 0 : throw uno::RuntimeException( "Unexpected function name!" );
359 :
360 0 : double nEarliestTime = 0;
361 0 : double nLatestTime = 0;
362 0 : if ( !( aEarliestTime >>= nEarliestTime )
363 0 : || ( aLatestTime.hasValue() && !( aLatestTime >>= nLatestTime ) ) )
364 0 : throw uno::RuntimeException( "Only double is supported as time for now!" );
365 :
366 0 : bool bSetTimer = true;
367 0 : aSchedule >>= bSetTimer;
368 :
369 0 : VbaTimerInfo aTimerIndex( aFunction, ::std::pair< double, double >( nEarliestTime, nLatestTime ) );
370 :
371 0 : VbaTimerHashMap::iterator aIter = m_pImpl->m_aTimerHash.find( aTimerIndex );
372 0 : if ( aIter != m_pImpl->m_aTimerHash.end() )
373 : {
374 0 : delete aIter->second;
375 0 : aIter->second = NULL;
376 0 : m_pImpl->m_aTimerHash.erase( aIter );
377 : }
378 :
379 0 : if ( bSetTimer )
380 : {
381 0 : VbaTimer* pTimer = new VbaTimer;
382 0 : m_pImpl->m_aTimerHash[ aTimerIndex ] = pTimer;
383 0 : pTimer->Start( this, aFunction, nEarliestTime, nLatestTime );
384 0 : }
385 0 : }
386 :
387 0 : float SAL_CALL VbaApplicationBase::CentimetersToPoints( float _Centimeters ) throw (uno::RuntimeException, std::exception)
388 : {
389 : // i cm = 28.35 points
390 : static const float rate = 28.35f;
391 0 : return ( _Centimeters * rate );
392 : }
393 :
394 0 : uno::Any SAL_CALL VbaApplicationBase::getVBE() throw (uno::RuntimeException, std::exception)
395 : {
396 : try // return empty object on error
397 : {
398 : // "VBE" object does not have a parent, but pass document model to be able to determine application type
399 0 : uno::Sequence< uno::Any > aArgs( 1 );
400 0 : aArgs[ 0 ] <<= getCurrentDocument();
401 0 : uno::Reference< lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
402 0 : uno::Reference< uno::XInterface > xVBE = xServiceManager->createInstanceWithArgumentsAndContext(
403 0 : "ooo.vba.vbide.VBE" , aArgs, mxContext );
404 0 : return uno::Any( xVBE );
405 : }
406 0 : catch( const uno::Exception& )
407 : {
408 : }
409 0 : return uno::Any();
410 : }
411 :
412 : OUString
413 0 : VbaApplicationBase::getServiceImplName()
414 : {
415 0 : return OUString("VbaApplicationBase");
416 : }
417 :
418 : uno::Sequence<OUString>
419 0 : VbaApplicationBase::getServiceNames()
420 : {
421 0 : static uno::Sequence< OUString > aServiceNames;
422 0 : if ( aServiceNames.getLength() == 0 )
423 : {
424 0 : aServiceNames.realloc( 1 );
425 0 : aServiceNames[ 0 ] = "ooo.vba.VbaApplicationBase";
426 : }
427 0 : return aServiceNames;
428 : }
429 :
430 0 : void SAL_CALL VbaApplicationBase::Undo()
431 : throw (uno::RuntimeException, std::exception)
432 : {
433 0 : uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
434 0 : dispatchRequests( xModel, ".uno:Undo" );
435 0 : }
436 :
437 0 : void VbaApplicationBase::Quit() throw (uno::RuntimeException, std::exception)
438 : {
439 : // need to stop basic
440 0 : SbMethod* pMeth = StarBASIC::GetActiveMethod();
441 0 : if ( pMeth )
442 : {
443 0 : SbModule* pMod = dynamic_cast< SbModule* >( pMeth->GetParent() );
444 0 : if ( pMod )
445 : {
446 0 : StarBASIC* pBasic = dynamic_cast< StarBASIC* >( pMod->GetParent() );
447 0 : if ( pBasic )
448 0 : pBasic->QuitAndExitApplication();
449 : }
450 : }
451 0 : }
452 :
453 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|