Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / vbahelper / vbaeventshelperbase.hxx
blobe7038da7ab755c9aa6cce024da20e64a8d9b0120
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 <unordered_map>
27 #include <com/sun/star/document/XEventListener.hpp>
28 #include <com/sun/star/lang/EventObject.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
32 #include <com/sun/star/uno/Any.hxx>
33 #include <com/sun/star/uno/Reference.hxx>
34 #include <com/sun/star/uno/Sequence.hxx>
35 #include <com/sun/star/util/XChangesListener.hpp>
36 #include <cppuhelper/implbase.hxx>
37 #include <rtl/ustring.hxx>
38 #include <sal/types.h>
39 #include <vbahelper/vbadllapi.h>
41 namespace com::sun::star {
42 namespace document { struct EventObject; }
43 namespace frame { class XModel; }
44 namespace script::vba { class XVBAModuleInfo; }
45 namespace uno { class XComponentContext; }
46 namespace util { struct ChangesEvent; }
49 class SfxObjectShell;
51 typedef ::cppu::WeakImplHelper<
52 css::script::vba::XVBAEventProcessor,
53 css::document::XEventListener,
54 css::util::XChangesListener,
55 css::lang::XServiceInfo > VbaEventsHelperBase_BASE;
57 class VBAHELPER_DLLPUBLIC VbaEventsHelperBase : public VbaEventsHelperBase_BASE
59 public:
60 VbaEventsHelperBase(
61 const css::uno::Sequence< css::uno::Any >& rArgs );
62 virtual ~VbaEventsHelperBase() override;
64 // script::vba::XVBAEventProcessor
65 virtual sal_Bool SAL_CALL hasVbaEventHandler( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) override;
66 virtual sal_Bool SAL_CALL processVbaEvent( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) override;
68 // document::XEventListener
69 virtual void SAL_CALL notifyEvent( const css::document::EventObject& rEvent ) override;
71 // util::XChangesListener
72 virtual void SAL_CALL changesOccurred( const css::util::ChangesEvent& rEvent ) override;
74 // lang::XEventListener
75 virtual void SAL_CALL disposing( const css::lang::EventObject& rEvent ) override;
77 sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override;
79 // little helpers ---------------------------------------------------------
81 bool hasModule(const OUString& rModuleName);
83 /** Helper to execute event handlers without throwing any exceptions. */
84 void processVbaEventNoThrow( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs );
86 /** @throws css::lang::IllegalArgumentException if the passed sequence does not contain a value at the specified index. */
87 static void checkArgument( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex )
88 { if( (nIndex < 0) || (nIndex >= rArgs.getLength()) ) throw css::lang::IllegalArgumentException(); }
90 /** @throws css::lang::IllegalArgumentException if the passed sequence does not contain a value of a specific at the specified index. */
91 template< typename Type >
92 static void checkArgumentType( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex )
93 { checkArgument( rArgs, nIndex ); if( !rArgs[ nIndex ].has< Type >() ) throw css::lang::IllegalArgumentException(); }
95 protected:
98 struct EventHandlerInfo
100 sal_Int32 mnEventId;
101 sal_Int32 mnModuleType;
102 OUString maMacroName;
103 sal_Int32 mnCancelIndex;
104 css::uno::Any maUserData;
107 /** Registers a supported event handler.
109 @param nEventId Event identifier from com.sun.star.script.vba.VBAEventId.
110 @param nModuleType Type of the module containing the event handler.
111 @param pcMacroName Name of the associated VBA event handler macro.
112 @param nCancelIndex 0-based index of Cancel parameter, or -1.
113 @param rUserData User data for free usage in derived implementations. */
114 void registerEventHandler(
115 sal_Int32 nEventId,
116 sal_Int32 nModuleType,
117 const char* pcMacroName,
118 sal_Int32 nCancelIndex = -1,
119 const css::uno::Any& rUserData = css::uno::Any() );
122 struct EventQueueEntry
124 sal_Int32 mnEventId;
125 css::uno::Sequence< css::uno::Any > maArgs;
126 /*implicit*/ EventQueueEntry( sal_Int32 nEventId ) : mnEventId( nEventId ) {}
127 EventQueueEntry( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) : mnEventId( nEventId ), maArgs( rArgs ) {}
129 typedef ::std::deque< EventQueueEntry > EventQueue;
131 /** Derived classes do additional preparations and return whether the
132 event handler has to be called.
134 @throws css::uno::RuntimeException
136 virtual bool implPrepareEvent(
137 EventQueue& rEventQueue,
138 const EventHandlerInfo& rInfo,
139 const css::uno::Sequence< css::uno::Any >& rArgs ) = 0;
141 /** Derived classes have to return the argument list for the specified VBA event handler.
143 @throws css::lang::IllegalArgumentException
144 @throws css::uno::RuntimeException
146 virtual css::uno::Sequence< css::uno::Any > implBuildArgumentList(
147 const EventHandlerInfo& rInfo,
148 const css::uno::Sequence< css::uno::Any >& rArgs ) = 0;
150 /** Derived classes may do additional postprocessing. Called even if the
151 event handler does not exist, or if an error occurred during execution.
153 @throws css::uno::RuntimeException
155 virtual void implPostProcessEvent(
156 EventQueue& rEventQueue,
157 const EventHandlerInfo& rInfo,
158 bool bCancel ) = 0;
160 /** Derived classes have to return the name of the Basic document module.
162 @throws css::lang::IllegalArgumentException
163 @throws css::uno::RuntimeException
165 virtual OUString implGetDocumentModuleName(
166 const EventHandlerInfo& rInfo,
167 const css::uno::Sequence< css::uno::Any >& rArgs ) const = 0;
169 private:
170 typedef ::std::map< sal_Int32, OUString > ModulePathMap;
172 /** Starts listening at the document model. */
173 void startListening();
174 /** Stops listening at the document model. */
175 void stopListening();
177 /** Returns the event handler info struct for the specified event, or throws.
180 @throws css::lang::IllegalArgumentException
182 const EventHandlerInfo& getEventHandlerInfo( sal_Int32 nEventId ) const;
184 /** Searches the event handler in the document and returns its full script path.
187 @throws css::lang::IllegalArgumentException
188 @throws css::uno::RuntimeException
190 OUString getEventHandlerPath(
191 const EventHandlerInfo& rInfo,
192 const css::uno::Sequence< css::uno::Any >& rArgs );
194 /** On first call, accesses the Basic library containing the VBA source code.
196 @throws css::uno::RuntimeException
198 void ensureVBALibrary();
200 /** Returns the type of the Basic module with the specified name.
202 @throws css::uno::RuntimeException
204 sal_Int32 getModuleType( const OUString& rModuleName );
206 /** Updates the map containing paths to event handlers for a Basic module.
208 @throws css::uno::RuntimeException
210 ModulePathMap& updateModulePathMap( const OUString& rModuleName );
212 protected:
213 css::uno::Reference< css::frame::XModel > mxModel;
214 SfxObjectShell* mpShell;
216 private:
217 typedef std::map< sal_Int32, EventHandlerInfo > EventHandlerInfoMap;
218 typedef std::unordered_map< OUString, ModulePathMap > EventHandlerPathMap;
220 EventHandlerInfoMap maEventInfos;
221 EventHandlerPathMap maEventPaths;
222 css::uno::Reference< css::script::vba::XVBAModuleInfo > mxModuleInfos;
223 OUString maLibraryName;
224 bool mbDisposed;
228 #endif
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */