build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / include / vbahelper / vbaeventshelperbase.hxx
blob2ffea8e39f8cd99fc0a4784407f1da751e0bb3c7
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 <exception>
25 #include <map>
26 #include <unordered_map>
28 #include <com/sun/star/document/XEventListener.hpp>
29 #include <com/sun/star/lang/EventObject.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
33 #include <com/sun/star/uno/Any.hxx>
34 #include <com/sun/star/uno/Reference.hxx>
35 #include <com/sun/star/uno/RuntimeException.hpp>
36 #include <com/sun/star/uno/Sequence.hxx>
37 #include <com/sun/star/util/VetoException.hpp>
38 #include <com/sun/star/util/XChangesListener.hpp>
39 #include <cppuhelper/implbase.hxx>
40 #include <rtl/ustring.hxx>
41 #include <sal/types.h>
42 #include <vbahelper/vbadllapi.h>
44 namespace com { namespace sun { namespace star {
45 namespace document { struct EventObject; }
46 namespace frame { class XModel; }
47 namespace script { namespace vba { class XVBAModuleInfo; } }
48 namespace uno { class XComponentContext; }
49 namespace util { struct ChangesEvent; }
50 } } }
52 class SfxObjectShell;
54 typedef ::cppu::WeakImplHelper<
55 css::script::vba::XVBAEventProcessor,
56 css::document::XEventListener,
57 css::util::XChangesListener,
58 css::lang::XServiceInfo > VbaEventsHelperBase_BASE;
60 class VBAHELPER_DLLPUBLIC VbaEventsHelperBase : public VbaEventsHelperBase_BASE
62 public:
63 VbaEventsHelperBase(
64 const css::uno::Sequence< css::uno::Any >& rArgs,
65 const css::uno::Reference< css::uno::XComponentContext >& xContext );
66 virtual ~VbaEventsHelperBase() override;
68 // script::vba::XVBAEventProcessor
69 virtual sal_Bool SAL_CALL hasVbaEventHandler( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException, std::exception) override;
70 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, std::exception) override;
72 // document::XEventListener
73 virtual void SAL_CALL notifyEvent( const css::document::EventObject& rEvent ) throw (css::uno::RuntimeException, std::exception) override;
75 // util::XChangesListener
76 virtual void SAL_CALL changesOccurred( const css::util::ChangesEvent& rEvent ) throw (css::uno::RuntimeException, std::exception) override;
78 // lang::XEventListener
79 virtual void SAL_CALL disposing( const css::lang::EventObject& rEvent ) throw (css::uno::RuntimeException, std::exception) override;
81 sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
82 throw (css::uno::RuntimeException, std::exception) override;
84 // little helpers ---------------------------------------------------------
86 /** Helper to execute event handlers without throwing any exceptions. */
87 void processVbaEventNoThrow( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs );
89 /** Throws, if the passed sequence does not contain a value at the specified index. */
90 static inline void checkArgument( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
91 { if( (nIndex < 0) || (nIndex >= rArgs.getLength()) ) throw css::lang::IllegalArgumentException(); }
93 /** Throws, if the passed sequence does not contain a value of a specific at the specified index. */
94 template< typename Type >
95 static inline void checkArgumentType( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
96 { checkArgument( rArgs, nIndex ); if( !rArgs[ nIndex ].has< Type >() ) throw css::lang::IllegalArgumentException(); }
98 protected:
101 struct EventHandlerInfo
103 sal_Int32 mnEventId;
104 sal_Int32 mnModuleType;
105 OUString maMacroName;
106 sal_Int32 mnCancelIndex;
107 css::uno::Any maUserData;
110 /** Registers a supported event handler.
112 @param nEventId Event identifier from com.sun.star.script.vba.VBAEventId.
113 @param nModuleType Type of the module containing the event handler.
114 @param pcMacroName Name of the associated VBA event handler macro.
115 @param nCancelIndex 0-based index of Cancel parameter, or -1.
116 @param rUserData User data for free usage in derived implementations. */
117 void registerEventHandler(
118 sal_Int32 nEventId,
119 sal_Int32 nModuleType,
120 const sal_Char* pcMacroName,
121 sal_Int32 nCancelIndex = -1,
122 const css::uno::Any& rUserData = css::uno::Any() );
125 struct EventQueueEntry
127 sal_Int32 mnEventId;
128 css::uno::Sequence< css::uno::Any > maArgs;
129 inline /*implicit*/ EventQueueEntry( sal_Int32 nEventId ) : mnEventId( nEventId ) {}
130 inline EventQueueEntry( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) : mnEventId( nEventId ), maArgs( rArgs ) {}
132 typedef ::std::deque< EventQueueEntry > EventQueue;
134 /** Derived classes do additional prpeparations and return whether the
135 event handler has to be called. */
136 virtual bool implPrepareEvent(
137 EventQueue& rEventQueue,
138 const EventHandlerInfo& rInfo,
139 const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::uno::RuntimeException) = 0;
141 /** Derived classes have to return the argument list for the specified VBA event handler. */
142 virtual css::uno::Sequence< css::uno::Any > implBuildArgumentList(
143 const EventHandlerInfo& rInfo,
144 const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException, std::exception) = 0;
146 /** Derived classes may do additional postprocessing. Called even if the
147 event handler does not exist, or if an error occurred during execution. */
148 virtual void implPostProcessEvent(
149 EventQueue& rEventQueue,
150 const EventHandlerInfo& rInfo,
151 bool bCancel ) throw (css::uno::RuntimeException) = 0;
153 /** Derived classes have to return the name of the Basic document module. */
154 virtual OUString implGetDocumentModuleName(
155 const EventHandlerInfo& rInfo,
156 const css::uno::Sequence< css::uno::Any >& rArgs ) const
157 throw (css::lang::IllegalArgumentException,
158 css::uno::RuntimeException) = 0;
160 private:
161 typedef ::std::map< sal_Int32, OUString > ModulePathMap;
163 /** Starts listening at the document model. */
164 void startListening();
165 /** Stops listening at the document model. */
166 void stopListening();
168 /** Returns the event handler info struct for the specified event, or throws. */
169 const EventHandlerInfo& getEventHandlerInfo( sal_Int32 nEventId ) const throw (css::lang::IllegalArgumentException);
171 /** Searches the event handler in the document and returns its full script path. */
172 OUString getEventHandlerPath(
173 const EventHandlerInfo& rInfo,
174 const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException, std::exception);
176 /** On first call, accesses the Basic library containing the VBA source code. */
177 void ensureVBALibrary() throw (css::uno::RuntimeException);
179 /** Returns the type of the Basic module with the specified name. */
180 sal_Int32 getModuleType( const OUString& rModuleName ) throw (css::uno::RuntimeException);
182 /** Updates the map containing paths to event handlers for a Basic module. */
183 ModulePathMap& updateModulePathMap( const OUString& rModuleName ) throw (css::uno::RuntimeException, std::exception);
185 protected:
186 css::uno::Reference< css::frame::XModel > mxModel;
187 SfxObjectShell* mpShell;
189 private:
190 typedef std::map< sal_Int32, EventHandlerInfo > EventHandlerInfoMap;
191 typedef std::unordered_map< OUString, ModulePathMap, OUStringHash > EventHandlerPathMap;
193 EventHandlerInfoMap maEventInfos;
194 EventHandlerPathMap maEventPaths;
195 css::uno::Reference< css::script::vba::XVBAModuleInfo > mxModuleInfos;
196 OUString maLibraryName;
197 bool mbDisposed;
201 #endif
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */