xref: /AOO41X/main/vbahelper/inc/vbahelper/vbaeventshelperbase.hxx (revision 67e470dafe1997e73f56ff7ff4878983707e3e07)
1*d4a7ee09SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d4a7ee09SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d4a7ee09SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d4a7ee09SAndrew Rist  * distributed with this work for additional information
6*d4a7ee09SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d4a7ee09SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d4a7ee09SAndrew Rist  * "License"); you may not use this file except in compliance
9*d4a7ee09SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*d4a7ee09SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*d4a7ee09SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d4a7ee09SAndrew Rist  * software distributed under the License is distributed on an
15*d4a7ee09SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d4a7ee09SAndrew Rist  * KIND, either express or implied.  See the License for the
17*d4a7ee09SAndrew Rist  * specific language governing permissions and limitations
18*d4a7ee09SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*d4a7ee09SAndrew Rist  *************************************************************/
21*d4a7ee09SAndrew Rist 
22*d4a7ee09SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef VBAHELPER_VBAEVENTSHELPERBASE_HXX
25cdf0e10cSrcweir #define VBAHELPER_VBAEVENTSHELPERBASE_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <deque>
28cdf0e10cSrcweir #include <hash_map>
29cdf0e10cSrcweir #include <map>
30cdf0e10cSrcweir #include <com/sun/star/document/XEventListener.hpp>
31cdf0e10cSrcweir #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
32cdf0e10cSrcweir #include <com/sun/star/util/XChangesListener.hpp>
33cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
34cdf0e10cSrcweir #include "vbahelper/vbahelper.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir namespace com { namespace sun { namespace star {
37cdf0e10cSrcweir     namespace script { namespace vba { class XVBAModuleInfo; } }
38cdf0e10cSrcweir     namespace uno { class XComponentContext; }
39cdf0e10cSrcweir } } }
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // ============================================================================
42cdf0e10cSrcweir 
43cdf0e10cSrcweir typedef ::cppu::WeakImplHelper3<
44cdf0e10cSrcweir     css::script::vba::XVBAEventProcessor,
45cdf0e10cSrcweir     css::document::XEventListener,
46cdf0e10cSrcweir     css::util::XChangesListener > VbaEventsHelperBase_BASE;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir class VBAHELPER_DLLPUBLIC VbaEventsHelperBase : public VbaEventsHelperBase_BASE
49cdf0e10cSrcweir {
50cdf0e10cSrcweir public:
51cdf0e10cSrcweir     VbaEventsHelperBase(
52cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Any >& rArgs,
53cdf0e10cSrcweir         const css::uno::Reference< css::uno::XComponentContext >& xContext );
54cdf0e10cSrcweir     virtual ~VbaEventsHelperBase();
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     // script::vba::XVBAEventProcessor
57cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasVbaEventHandler( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
58cdf0e10cSrcweir     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);
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     // document::XEventListener
61cdf0e10cSrcweir     virtual void SAL_CALL notifyEvent( const css::document::EventObject& rEvent ) throw (css::uno::RuntimeException);
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     // util::XChangesListener
64cdf0e10cSrcweir     virtual void SAL_CALL changesOccurred( const css::util::ChangesEvent& rEvent ) throw (css::uno::RuntimeException);
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     // lang::XEventListener
67cdf0e10cSrcweir     virtual void SAL_CALL disposing( const css::lang::EventObject& rEvent ) throw (css::uno::RuntimeException);
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     // little helpers ---------------------------------------------------------
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     /** Helper to execute event handlers without throwing any exceptions. */
72cdf0e10cSrcweir     void processVbaEventNoThrow( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs );
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     /** Throws, if the passed sequence does not contain a value at the specified index. */
checkArgument(const css::uno::Sequence<css::uno::Any> & rArgs,sal_Int32 nIndex)75cdf0e10cSrcweir     static inline void checkArgument( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
76cdf0e10cSrcweir         { if( (nIndex < 0) || (nIndex >= rArgs.getLength()) ) throw css::lang::IllegalArgumentException(); }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     /** Throws, if the passed sequence does not contain a value of a specific at the specified index. */
79cdf0e10cSrcweir     template< typename Type >
checkArgumentType(const css::uno::Sequence<css::uno::Any> & rArgs,sal_Int32 nIndex)80cdf0e10cSrcweir     static inline void checkArgumentType( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
81cdf0e10cSrcweir         { checkArgument( rArgs, nIndex ); if( !rArgs[ nIndex ].has< Type >() ) throw css::lang::IllegalArgumentException(); }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir protected:
84cdf0e10cSrcweir     // ------------------------------------------------------------------------
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     struct EventHandlerInfo
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         sal_Int32 mnEventId;
89cdf0e10cSrcweir         sal_Int32 mnModuleType;
90cdf0e10cSrcweir         ::rtl::OUString maMacroName;
91cdf0e10cSrcweir         sal_Int32 mnCancelIndex;
92cdf0e10cSrcweir         css::uno::Any maUserData;
93cdf0e10cSrcweir     };
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     /** Registers a supported event handler.
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         @param nEventId  Event identifier from com.sun.star.script.vba.VBAEventId.
98cdf0e10cSrcweir         @param nModuleType  Type of the module containing the event handler.
99cdf0e10cSrcweir         @param pcMacroName  Name of the associated VBA event handler macro.
100cdf0e10cSrcweir         @param nCancelIndex  0-based index of Cancel parameter, or -1.
101cdf0e10cSrcweir         @param rUserData  User data for free usage in derived implementations. */
102cdf0e10cSrcweir     void registerEventHandler(
103cdf0e10cSrcweir             sal_Int32 nEventId,
104cdf0e10cSrcweir             sal_Int32 nModuleType,
105cdf0e10cSrcweir             const sal_Char* pcMacroName,
106cdf0e10cSrcweir             sal_Int32 nCancelIndex = -1,
107cdf0e10cSrcweir             const css::uno::Any& rUserData = css::uno::Any() );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     // ------------------------------------------------------------------------
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     struct EventQueueEntry
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         sal_Int32 mnEventId;
114cdf0e10cSrcweir         css::uno::Sequence< css::uno::Any > maArgs;
EventQueueEntryVbaEventsHelperBase::EventQueueEntry115cdf0e10cSrcweir         inline /*implicit*/ EventQueueEntry( sal_Int32 nEventId ) : mnEventId( nEventId ) {}
EventQueueEntryVbaEventsHelperBase::EventQueueEntry116cdf0e10cSrcweir         inline EventQueueEntry( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) : mnEventId( nEventId ), maArgs( rArgs ) {}
117cdf0e10cSrcweir     };
118cdf0e10cSrcweir     typedef ::std::deque< EventQueueEntry > EventQueue;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     /** Derived classes do additional prpeparations and return whether the
121cdf0e10cSrcweir         event handler has to be called. */
122cdf0e10cSrcweir     virtual bool implPrepareEvent(
123cdf0e10cSrcweir         EventQueue& rEventQueue,
124cdf0e10cSrcweir         const EventHandlerInfo& rInfo,
125cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::uno::RuntimeException) = 0;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     /** Derived classes have to return the argument list for the specified VBA event handler. */
128cdf0e10cSrcweir     virtual css::uno::Sequence< css::uno::Any > implBuildArgumentList(
129cdf0e10cSrcweir         const EventHandlerInfo& rInfo,
130cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException) = 0;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     /** Derived classes may do additional postprocessing. Called even if the
133cdf0e10cSrcweir         event handler does not exist, or if an error occured during execution. */
134cdf0e10cSrcweir     virtual void implPostProcessEvent(
135cdf0e10cSrcweir         EventQueue& rEventQueue,
136cdf0e10cSrcweir         const EventHandlerInfo& rInfo,
137cdf0e10cSrcweir         bool bCancel ) throw (css::uno::RuntimeException) = 0;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     /** Derived classes have to return the name of the Basic document module. */
140cdf0e10cSrcweir     virtual ::rtl::OUString implGetDocumentModuleName(
141cdf0e10cSrcweir         const EventHandlerInfo& rInfo,
142cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Any >& rArgs ) const throw (css::lang::IllegalArgumentException) = 0;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir private:
145cdf0e10cSrcweir     typedef ::std::map< sal_Int32, ::rtl::OUString > ModulePathMap;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     /** Starts listening at the document model. */
148cdf0e10cSrcweir     void startListening();
149cdf0e10cSrcweir     /** Stops listening at the document model. */
150cdf0e10cSrcweir     void stopListening();
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     /** Returns the event handler info struct for the specified event, or throws. */
153cdf0e10cSrcweir     const EventHandlerInfo& getEventHandlerInfo( sal_Int32 nEventId ) const throw (css::lang::IllegalArgumentException);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     /** Searches the event handler in the document and returns its full script path. */
156cdf0e10cSrcweir     ::rtl::OUString getEventHandlerPath(
157cdf0e10cSrcweir         const EventHandlerInfo& rInfo,
158cdf0e10cSrcweir         const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     /** On first call, accesses the Basic library containing the VBA source code. */
161cdf0e10cSrcweir     void ensureVBALibrary() throw (css::uno::RuntimeException);
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     /** Returns the type of the Basic module with the specified name. */
164cdf0e10cSrcweir     sal_Int32 getModuleType( const ::rtl::OUString& rModuleName ) throw (css::uno::RuntimeException);
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     /** Updates the map containing paths to event handlers for a Basic module. */
167cdf0e10cSrcweir     ModulePathMap& updateModulePathMap( const ::rtl::OUString& rModuleName ) throw (css::uno::RuntimeException);
168cdf0e10cSrcweir 
169cdf0e10cSrcweir protected:
170cdf0e10cSrcweir     css::uno::Reference< css::frame::XModel > mxModel;
171cdf0e10cSrcweir     SfxObjectShell* mpShell;
172cdf0e10cSrcweir 
173cdf0e10cSrcweir private:
174cdf0e10cSrcweir     typedef ::std::map< sal_Int32, EventHandlerInfo > EventHandlerInfoMap;
175cdf0e10cSrcweir     typedef ::std::hash_map< ::rtl::OUString, ModulePathMap, ::rtl::OUStringHash > EventHandlerPathMap;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     EventHandlerInfoMap maEventInfos;
178cdf0e10cSrcweir     EventHandlerPathMap maEventPaths;
179cdf0e10cSrcweir     css::uno::Reference< css::script::vba::XVBAModuleInfo > mxModuleInfos;
180cdf0e10cSrcweir     ::rtl::OUString maLibraryName;
181cdf0e10cSrcweir     bool mbDisposed;
182cdf0e10cSrcweir };
183cdf0e10cSrcweir 
184cdf0e10cSrcweir // ============================================================================
185cdf0e10cSrcweir 
186cdf0e10cSrcweir #endif
187