bump product version to 4.2.0.1
[LibreOffice.git] / include / vbahelper / vbaeventshelperbase.hxx
blob2edbf57afe9facb94175cebddc0a4e6d113bd601
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_VBAHELPER_VBAEVENTSHELPERBASE_HXX
21 #define INCLUDED_VBAHELPER_VBAEVENTSHELPERBASE_HXX
23 #include <deque>
24 #include <map>
25 #include <boost/unordered_map.hpp>
26 #include <com/sun/star/document/XEventListener.hpp>
27 #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
28 #include <com/sun/star/util/XChangesListener.hpp>
29 #include <cppuhelper/implbase3.hxx>
30 #include <vbahelper/vbahelper.hxx>
32 namespace com { namespace sun { namespace star {
33 namespace script { namespace vba { class XVBAModuleInfo; } }
34 namespace uno { class XComponentContext; }
35 } } }
37 // ============================================================================
39 typedef ::cppu::WeakImplHelper3<
40 css::script::vba::XVBAEventProcessor,
41 css::document::XEventListener,
42 css::util::XChangesListener > VbaEventsHelperBase_BASE;
44 class VBAHELPER_DLLPUBLIC VbaEventsHelperBase : public VbaEventsHelperBase_BASE
46 public:
47 VbaEventsHelperBase(
48 const css::uno::Sequence< css::uno::Any >& rArgs,
49 const css::uno::Reference< css::uno::XComponentContext >& xContext );
50 virtual ~VbaEventsHelperBase();
52 // script::vba::XVBAEventProcessor
53 virtual sal_Bool SAL_CALL hasVbaEventHandler( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
54 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);
56 // document::XEventListener
57 virtual void SAL_CALL notifyEvent( const css::document::EventObject& rEvent ) throw (css::uno::RuntimeException);
59 // util::XChangesListener
60 virtual void SAL_CALL changesOccurred( const css::util::ChangesEvent& rEvent ) throw (css::uno::RuntimeException);
62 // lang::XEventListener
63 virtual void SAL_CALL disposing( const css::lang::EventObject& rEvent ) throw (css::uno::RuntimeException);
65 // little helpers ---------------------------------------------------------
67 /** Helper to execute event handlers without throwing any exceptions. */
68 void processVbaEventNoThrow( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs );
70 /** Throws, if the passed sequence does not contain a value at the specified index. */
71 static inline void checkArgument( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
72 { if( (nIndex < 0) || (nIndex >= rArgs.getLength()) ) throw css::lang::IllegalArgumentException(); }
74 /** Throws, if the passed sequence does not contain a value of a specific at the specified index. */
75 template< typename Type >
76 static inline void checkArgumentType( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
77 { checkArgument( rArgs, nIndex ); if( !rArgs[ nIndex ].has< Type >() ) throw css::lang::IllegalArgumentException(); }
79 protected:
80 // ------------------------------------------------------------------------
82 struct EventHandlerInfo
84 sal_Int32 mnEventId;
85 sal_Int32 mnModuleType;
86 OUString maMacroName;
87 sal_Int32 mnCancelIndex;
88 css::uno::Any maUserData;
91 /** Registers a supported event handler.
93 @param nEventId Event identifier from com.sun.star.script.vba.VBAEventId.
94 @param nModuleType Type of the module containing the event handler.
95 @param pcMacroName Name of the associated VBA event handler macro.
96 @param nCancelIndex 0-based index of Cancel parameter, or -1.
97 @param rUserData User data for free usage in derived implementations. */
98 void registerEventHandler(
99 sal_Int32 nEventId,
100 sal_Int32 nModuleType,
101 const sal_Char* pcMacroName,
102 sal_Int32 nCancelIndex = -1,
103 const css::uno::Any& rUserData = css::uno::Any() );
105 // ------------------------------------------------------------------------
107 struct EventQueueEntry
109 sal_Int32 mnEventId;
110 css::uno::Sequence< css::uno::Any > maArgs;
111 inline /*implicit*/ EventQueueEntry( sal_Int32 nEventId ) : mnEventId( nEventId ) {}
112 inline EventQueueEntry( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) : mnEventId( nEventId ), maArgs( rArgs ) {}
114 typedef ::std::deque< EventQueueEntry > EventQueue;
116 /** Derived classes do additional prpeparations and return whether the
117 event handler has to be called. */
118 virtual bool implPrepareEvent(
119 EventQueue& rEventQueue,
120 const EventHandlerInfo& rInfo,
121 const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::uno::RuntimeException) = 0;
123 /** Derived classes have to return the argument list for the specified VBA event handler. */
124 virtual css::uno::Sequence< css::uno::Any > implBuildArgumentList(
125 const EventHandlerInfo& rInfo,
126 const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException) = 0;
128 /** Derived classes may do additional postprocessing. Called even if the
129 event handler does not exist, or if an error occurred during execution. */
130 virtual void implPostProcessEvent(
131 EventQueue& rEventQueue,
132 const EventHandlerInfo& rInfo,
133 bool bCancel ) throw (css::uno::RuntimeException) = 0;
135 /** Derived classes have to return the name of the Basic document module. */
136 virtual OUString implGetDocumentModuleName(
137 const EventHandlerInfo& rInfo,
138 const css::uno::Sequence< css::uno::Any >& rArgs ) const throw (css::lang::IllegalArgumentException) = 0;
140 private:
141 typedef ::std::map< sal_Int32, OUString > ModulePathMap;
143 /** Starts listening at the document model. */
144 void startListening();
145 /** Stops listening at the document model. */
146 void stopListening();
148 /** Returns the event handler info struct for the specified event, or throws. */
149 const EventHandlerInfo& getEventHandlerInfo( sal_Int32 nEventId ) const throw (css::lang::IllegalArgumentException);
151 /** Searches the event handler in the document and returns its full script path. */
152 OUString getEventHandlerPath(
153 const EventHandlerInfo& rInfo,
154 const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
156 /** On first call, accesses the Basic library containing the VBA source code. */
157 void ensureVBALibrary() throw (css::uno::RuntimeException);
159 /** Returns the type of the Basic module with the specified name. */
160 sal_Int32 getModuleType( const OUString& rModuleName ) throw (css::uno::RuntimeException);
162 /** Updates the map containing paths to event handlers for a Basic module. */
163 ModulePathMap& updateModulePathMap( const OUString& rModuleName ) throw (css::uno::RuntimeException);
165 protected:
166 css::uno::Reference< css::frame::XModel > mxModel;
167 SfxObjectShell* mpShell;
169 private:
170 typedef ::std::map< sal_Int32, EventHandlerInfo > EventHandlerInfoMap;
171 typedef boost::unordered_map< OUString, ModulePathMap, OUStringHash > EventHandlerPathMap;
173 EventHandlerInfoMap maEventInfos;
174 EventHandlerPathMap maEventPaths;
175 css::uno::Reference< css::script::vba::XVBAModuleInfo > mxModuleInfos;
176 OUString maLibraryName;
177 bool mbDisposed;
180 // ============================================================================
182 #endif
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */