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