xref: /AOO41X/main/vbahelper/inc/vbahelper/vbaeventshelperbase.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef VBAHELPER_VBAEVENTSHELPERBASE_HXX
29*cdf0e10cSrcweir #define VBAHELPER_VBAEVENTSHELPERBASE_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <deque>
32*cdf0e10cSrcweir #include <hash_map>
33*cdf0e10cSrcweir #include <map>
34*cdf0e10cSrcweir #include <com/sun/star/document/XEventListener.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/util/XChangesListener.hpp>
37*cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
38*cdf0e10cSrcweir #include "vbahelper/vbahelper.hxx"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir namespace com { namespace sun { namespace star {
41*cdf0e10cSrcweir     namespace script { namespace vba { class XVBAModuleInfo; } }
42*cdf0e10cSrcweir     namespace uno { class XComponentContext; }
43*cdf0e10cSrcweir } } }
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir // ============================================================================
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper3<
48*cdf0e10cSrcweir     css::script::vba::XVBAEventProcessor,
49*cdf0e10cSrcweir     css::document::XEventListener,
50*cdf0e10cSrcweir     css::util::XChangesListener > VbaEventsHelperBase_BASE;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir class VBAHELPER_DLLPUBLIC VbaEventsHelperBase : public VbaEventsHelperBase_BASE
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir public:
55*cdf0e10cSrcweir     VbaEventsHelperBase(
56*cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Any >& rArgs,
57*cdf0e10cSrcweir         const css::uno::Reference< css::uno::XComponentContext >& xContext );
58*cdf0e10cSrcweir     virtual ~VbaEventsHelperBase();
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     // script::vba::XVBAEventProcessor
61*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasVbaEventHandler( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
62*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL processVbaEvent( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::util::VetoException, css::uno::RuntimeException);
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     // document::XEventListener
65*cdf0e10cSrcweir     virtual void SAL_CALL notifyEvent( const css::document::EventObject& rEvent ) throw (css::uno::RuntimeException);
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     // util::XChangesListener
68*cdf0e10cSrcweir     virtual void SAL_CALL changesOccurred( const css::util::ChangesEvent& rEvent ) throw (css::uno::RuntimeException);
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     // lang::XEventListener
71*cdf0e10cSrcweir     virtual void SAL_CALL disposing( const css::lang::EventObject& rEvent ) throw (css::uno::RuntimeException);
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     // little helpers ---------------------------------------------------------
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     /** Helper to execute event handlers without throwing any exceptions. */
76*cdf0e10cSrcweir     void processVbaEventNoThrow( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs );
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     /** Throws, if the passed sequence does not contain a value at the specified index. */
79*cdf0e10cSrcweir     static inline void checkArgument( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
80*cdf0e10cSrcweir         { if( (nIndex < 0) || (nIndex >= rArgs.getLength()) ) throw css::lang::IllegalArgumentException(); }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     /** Throws, if the passed sequence does not contain a value of a specific at the specified index. */
83*cdf0e10cSrcweir     template< typename Type >
84*cdf0e10cSrcweir     static inline void checkArgumentType( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
85*cdf0e10cSrcweir         { checkArgument( rArgs, nIndex ); if( !rArgs[ nIndex ].has< Type >() ) throw css::lang::IllegalArgumentException(); }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir protected:
88*cdf0e10cSrcweir     // ------------------------------------------------------------------------
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     struct EventHandlerInfo
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir         sal_Int32 mnEventId;
93*cdf0e10cSrcweir         sal_Int32 mnModuleType;
94*cdf0e10cSrcweir         ::rtl::OUString maMacroName;
95*cdf0e10cSrcweir         sal_Int32 mnCancelIndex;
96*cdf0e10cSrcweir         css::uno::Any maUserData;
97*cdf0e10cSrcweir     };
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     /** Registers a supported event handler.
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir         @param nEventId  Event identifier from com.sun.star.script.vba.VBAEventId.
102*cdf0e10cSrcweir         @param nModuleType  Type of the module containing the event handler.
103*cdf0e10cSrcweir         @param pcMacroName  Name of the associated VBA event handler macro.
104*cdf0e10cSrcweir         @param nCancelIndex  0-based index of Cancel parameter, or -1.
105*cdf0e10cSrcweir         @param rUserData  User data for free usage in derived implementations. */
106*cdf0e10cSrcweir     void registerEventHandler(
107*cdf0e10cSrcweir             sal_Int32 nEventId,
108*cdf0e10cSrcweir             sal_Int32 nModuleType,
109*cdf0e10cSrcweir             const sal_Char* pcMacroName,
110*cdf0e10cSrcweir             sal_Int32 nCancelIndex = -1,
111*cdf0e10cSrcweir             const css::uno::Any& rUserData = css::uno::Any() );
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir     // ------------------------------------------------------------------------
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     struct EventQueueEntry
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir         sal_Int32 mnEventId;
118*cdf0e10cSrcweir         css::uno::Sequence< css::uno::Any > maArgs;
119*cdf0e10cSrcweir         inline /*implicit*/ EventQueueEntry( sal_Int32 nEventId ) : mnEventId( nEventId ) {}
120*cdf0e10cSrcweir         inline EventQueueEntry( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) : mnEventId( nEventId ), maArgs( rArgs ) {}
121*cdf0e10cSrcweir     };
122*cdf0e10cSrcweir     typedef ::std::deque< EventQueueEntry > EventQueue;
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     /** Derived classes do additional prpeparations and return whether the
125*cdf0e10cSrcweir         event handler has to be called. */
126*cdf0e10cSrcweir     virtual bool implPrepareEvent(
127*cdf0e10cSrcweir         EventQueue& rEventQueue,
128*cdf0e10cSrcweir         const EventHandlerInfo& rInfo,
129*cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::uno::RuntimeException) = 0;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     /** Derived classes have to return the argument list for the specified VBA event handler. */
132*cdf0e10cSrcweir     virtual css::uno::Sequence< css::uno::Any > implBuildArgumentList(
133*cdf0e10cSrcweir         const EventHandlerInfo& rInfo,
134*cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException) = 0;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     /** Derived classes may do additional postprocessing. Called even if the
137*cdf0e10cSrcweir         event handler does not exist, or if an error occured during execution. */
138*cdf0e10cSrcweir     virtual void implPostProcessEvent(
139*cdf0e10cSrcweir         EventQueue& rEventQueue,
140*cdf0e10cSrcweir         const EventHandlerInfo& rInfo,
141*cdf0e10cSrcweir         bool bCancel ) throw (css::uno::RuntimeException) = 0;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     /** Derived classes have to return the name of the Basic document module. */
144*cdf0e10cSrcweir     virtual ::rtl::OUString implGetDocumentModuleName(
145*cdf0e10cSrcweir         const EventHandlerInfo& rInfo,
146*cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Any >& rArgs ) const throw (css::lang::IllegalArgumentException) = 0;
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir private:
149*cdf0e10cSrcweir     typedef ::std::map< sal_Int32, ::rtl::OUString > ModulePathMap;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     /** Starts listening at the document model. */
152*cdf0e10cSrcweir     void startListening();
153*cdf0e10cSrcweir     /** Stops listening at the document model. */
154*cdf0e10cSrcweir     void stopListening();
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     /** Returns the event handler info struct for the specified event, or throws. */
157*cdf0e10cSrcweir     const EventHandlerInfo& getEventHandlerInfo( sal_Int32 nEventId ) const throw (css::lang::IllegalArgumentException);
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     /** Searches the event handler in the document and returns its full script path. */
160*cdf0e10cSrcweir     ::rtl::OUString getEventHandlerPath(
161*cdf0e10cSrcweir         const EventHandlerInfo& rInfo,
162*cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     /** On first call, accesses the Basic library containing the VBA source code. */
165*cdf0e10cSrcweir     void ensureVBALibrary() throw (css::uno::RuntimeException);
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     /** Returns the type of the Basic module with the specified name. */
168*cdf0e10cSrcweir     sal_Int32 getModuleType( const ::rtl::OUString& rModuleName ) throw (css::uno::RuntimeException);
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     /** Updates the map containing paths to event handlers for a Basic module. */
171*cdf0e10cSrcweir     ModulePathMap& updateModulePathMap( const ::rtl::OUString& rModuleName ) throw (css::uno::RuntimeException);
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir protected:
174*cdf0e10cSrcweir     css::uno::Reference< css::frame::XModel > mxModel;
175*cdf0e10cSrcweir     SfxObjectShell* mpShell;
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir private:
178*cdf0e10cSrcweir     typedef ::std::map< sal_Int32, EventHandlerInfo > EventHandlerInfoMap;
179*cdf0e10cSrcweir     typedef ::std::hash_map< ::rtl::OUString, ModulePathMap, ::rtl::OUStringHash > EventHandlerPathMap;
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     EventHandlerInfoMap maEventInfos;
182*cdf0e10cSrcweir     EventHandlerPathMap maEventPaths;
183*cdf0e10cSrcweir     css::uno::Reference< css::script::vba::XVBAModuleInfo > mxModuleInfos;
184*cdf0e10cSrcweir     ::rtl::OUString maLibraryName;
185*cdf0e10cSrcweir     bool mbDisposed;
186*cdf0e10cSrcweir };
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir // ============================================================================
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir #endif
191