1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
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
; }
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
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
) override
;
70 virtual sal_Bool SAL_CALL
processVbaEvent( sal_Int32 nEventId
, const css::uno::Sequence
< css::uno::Any
>& rArgs
) override
;
72 // document::XEventListener
73 virtual void SAL_CALL
notifyEvent( const css::document::EventObject
& rEvent
) override
;
75 // util::XChangesListener
76 virtual void SAL_CALL
changesOccurred( const css::util::ChangesEvent
& rEvent
) override
;
78 // lang::XEventListener
79 virtual void SAL_CALL
disposing( const css::lang::EventObject
& rEvent
) override
;
81 sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
;
83 // little helpers ---------------------------------------------------------
85 /** Helper to execute event handlers without throwing any exceptions. */
86 void processVbaEventNoThrow( sal_Int32 nEventId
, const css::uno::Sequence
< css::uno::Any
>& rArgs
);
88 /** @throws css::lang::IllegalArgumentException if the passed sequence does not contain a value at the specified index. */
89 static void checkArgument( const css::uno::Sequence
< css::uno::Any
>& rArgs
, sal_Int32 nIndex
)
90 { if( (nIndex
< 0) || (nIndex
>= rArgs
.getLength()) ) throw css::lang::IllegalArgumentException(); }
92 /** @throws css::lang::IllegalArgumentException if the passed sequence does not contain a value of a specific at the specified index. */
93 template< typename Type
>
94 static void checkArgumentType( const css::uno::Sequence
< css::uno::Any
>& rArgs
, sal_Int32 nIndex
)
95 { checkArgument( rArgs
, nIndex
); if( !rArgs
[ nIndex
].has
< Type
>() ) throw css::lang::IllegalArgumentException(); }
100 struct EventHandlerInfo
103 sal_Int32 mnModuleType
;
104 OUString maMacroName
;
105 sal_Int32 mnCancelIndex
;
106 css::uno::Any maUserData
;
109 /** Registers a supported event handler.
111 @param nEventId Event identifier from com.sun.star.script.vba.VBAEventId.
112 @param nModuleType Type of the module containing the event handler.
113 @param pcMacroName Name of the associated VBA event handler macro.
114 @param nCancelIndex 0-based index of Cancel parameter, or -1.
115 @param rUserData User data for free usage in derived implementations. */
116 void registerEventHandler(
118 sal_Int32 nModuleType
,
119 const sal_Char
* pcMacroName
,
120 sal_Int32 nCancelIndex
= -1,
121 const css::uno::Any
& rUserData
= css::uno::Any() );
124 struct EventQueueEntry
127 css::uno::Sequence
< css::uno::Any
> maArgs
;
128 /*implicit*/ EventQueueEntry( sal_Int32 nEventId
) : mnEventId( nEventId
) {}
129 EventQueueEntry( sal_Int32 nEventId
, const css::uno::Sequence
< css::uno::Any
>& rArgs
) : mnEventId( nEventId
), maArgs( rArgs
) {}
131 typedef ::std::deque
< EventQueueEntry
> EventQueue
;
133 /** Derived classes do additional preparations and return whether the
134 event handler has to be called.
136 @throws css::uno::RuntimeException
138 virtual bool implPrepareEvent(
139 EventQueue
& rEventQueue
,
140 const EventHandlerInfo
& rInfo
,
141 const css::uno::Sequence
< css::uno::Any
>& rArgs
) = 0;
143 /** Derived classes have to return the argument list for the specified VBA event handler.
145 @throws css::lang::IllegalArgumentException
146 @throws css::uno::RuntimeException
148 virtual css::uno::Sequence
< css::uno::Any
> implBuildArgumentList(
149 const EventHandlerInfo
& rInfo
,
150 const css::uno::Sequence
< css::uno::Any
>& rArgs
) = 0;
152 /** Derived classes may do additional postprocessing. Called even if the
153 event handler does not exist, or if an error occurred during execution.
155 @throws css::uno::RuntimeException
157 virtual void implPostProcessEvent(
158 EventQueue
& rEventQueue
,
159 const EventHandlerInfo
& rInfo
,
162 /** Derived classes have to return the name of the Basic document module.
164 @throws css::lang::IllegalArgumentException
165 @throws css::uno::RuntimeException
167 virtual OUString
implGetDocumentModuleName(
168 const EventHandlerInfo
& rInfo
,
169 const css::uno::Sequence
< css::uno::Any
>& rArgs
) const = 0;
172 typedef ::std::map
< sal_Int32
, OUString
> ModulePathMap
;
174 /** Starts listening at the document model. */
175 void startListening();
176 /** Stops listening at the document model. */
177 void stopListening();
179 /** Returns the event handler info struct for the specified event, or throws.
182 @throws css::lang::IllegalArgumentException
184 const EventHandlerInfo
& getEventHandlerInfo( sal_Int32 nEventId
) const;
186 /** Searches the event handler in the document and returns its full script path.
189 @throws css::lang::IllegalArgumentException
190 @throws css::uno::RuntimeException
192 OUString
getEventHandlerPath(
193 const EventHandlerInfo
& rInfo
,
194 const css::uno::Sequence
< css::uno::Any
>& rArgs
);
196 /** On first call, accesses the Basic library containing the VBA source code.
198 @throws css::uno::RuntimeException
200 void ensureVBALibrary();
202 /** Returns the type of the Basic module with the specified name.
204 @throws css::uno::RuntimeException
206 sal_Int32
getModuleType( const OUString
& rModuleName
);
208 /** Updates the map containing paths to event handlers for a Basic module.
210 @throws css::uno::RuntimeException
212 ModulePathMap
& updateModulePathMap( const OUString
& rModuleName
);
215 css::uno::Reference
< css::frame::XModel
> mxModel
;
216 SfxObjectShell
* mpShell
;
219 typedef std::map
< sal_Int32
, EventHandlerInfo
> EventHandlerInfoMap
;
220 typedef std::unordered_map
< OUString
, ModulePathMap
, OUStringHash
> EventHandlerPathMap
;
222 EventHandlerInfoMap maEventInfos
;
223 EventHandlerPathMap maEventPaths
;
224 css::uno::Reference
< css::script::vba::XVBAModuleInfo
> mxModuleInfos
;
225 OUString maLibraryName
;
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */