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