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