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 virtual ~VbaEventsHelperBase() override
;
67 // script::vba::XVBAEventProcessor
68 virtual sal_Bool SAL_CALL
hasVbaEventHandler( sal_Int32 nEventId
, const css::uno::Sequence
< css::uno::Any
>& rArgs
) override
;
69 virtual sal_Bool SAL_CALL
processVbaEvent( sal_Int32 nEventId
, const css::uno::Sequence
< css::uno::Any
>& rArgs
) override
;
71 // document::XEventListener
72 virtual void SAL_CALL
notifyEvent( const css::document::EventObject
& rEvent
) override
;
74 // util::XChangesListener
75 virtual void SAL_CALL
changesOccurred( const css::util::ChangesEvent
& rEvent
) override
;
77 // lang::XEventListener
78 virtual void SAL_CALL
disposing( const css::lang::EventObject
& rEvent
) override
;
80 sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
;
82 // little helpers ---------------------------------------------------------
84 /** Helper to execute event handlers without throwing any exceptions. */
85 void processVbaEventNoThrow( sal_Int32 nEventId
, const css::uno::Sequence
< css::uno::Any
>& rArgs
);
87 /** @throws css::lang::IllegalArgumentException if the passed sequence does not contain a value at the specified index. */
88 static void checkArgument( const css::uno::Sequence
< css::uno::Any
>& rArgs
, sal_Int32 nIndex
)
89 { if( (nIndex
< 0) || (nIndex
>= rArgs
.getLength()) ) throw css::lang::IllegalArgumentException(); }
91 /** @throws css::lang::IllegalArgumentException if the passed sequence does not contain a value of a specific at the specified index. */
92 template< typename Type
>
93 static void checkArgumentType( const css::uno::Sequence
< css::uno::Any
>& rArgs
, sal_Int32 nIndex
)
94 { checkArgument( rArgs
, nIndex
); if( !rArgs
[ nIndex
].has
< Type
>() ) throw css::lang::IllegalArgumentException(); }
99 struct EventHandlerInfo
102 sal_Int32 mnModuleType
;
103 OUString maMacroName
;
104 sal_Int32 mnCancelIndex
;
105 css::uno::Any maUserData
;
108 /** Registers a supported event handler.
110 @param nEventId Event identifier from com.sun.star.script.vba.VBAEventId.
111 @param nModuleType Type of the module containing the event handler.
112 @param pcMacroName Name of the associated VBA event handler macro.
113 @param nCancelIndex 0-based index of Cancel parameter, or -1.
114 @param rUserData User data for free usage in derived implementations. */
115 void registerEventHandler(
117 sal_Int32 nModuleType
,
118 const sal_Char
* pcMacroName
,
119 sal_Int32 nCancelIndex
= -1,
120 const css::uno::Any
& rUserData
= css::uno::Any() );
123 struct EventQueueEntry
125 sal_Int32
const mnEventId
;
126 css::uno::Sequence
< css::uno::Any
> const maArgs
;
127 /*implicit*/ EventQueueEntry( sal_Int32 nEventId
) : mnEventId( nEventId
) {}
128 EventQueueEntry( sal_Int32 nEventId
, const css::uno::Sequence
< css::uno::Any
>& rArgs
) : mnEventId( nEventId
), maArgs( rArgs
) {}
130 typedef ::std::deque
< EventQueueEntry
> EventQueue
;
132 /** Derived classes do additional preparations and return whether the
133 event handler has to be called.
135 @throws css::uno::RuntimeException
137 virtual bool implPrepareEvent(
138 EventQueue
& rEventQueue
,
139 const EventHandlerInfo
& rInfo
,
140 const css::uno::Sequence
< css::uno::Any
>& rArgs
) = 0;
142 /** Derived classes have to return the argument list for the specified VBA event handler.
144 @throws css::lang::IllegalArgumentException
145 @throws css::uno::RuntimeException
147 virtual css::uno::Sequence
< css::uno::Any
> implBuildArgumentList(
148 const EventHandlerInfo
& rInfo
,
149 const css::uno::Sequence
< css::uno::Any
>& rArgs
) = 0;
151 /** Derived classes may do additional postprocessing. Called even if the
152 event handler does not exist, or if an error occurred during execution.
154 @throws css::uno::RuntimeException
156 virtual void implPostProcessEvent(
157 EventQueue
& rEventQueue
,
158 const EventHandlerInfo
& rInfo
,
161 /** Derived classes have to return the name of the Basic document module.
163 @throws css::lang::IllegalArgumentException
164 @throws css::uno::RuntimeException
166 virtual OUString
implGetDocumentModuleName(
167 const EventHandlerInfo
& rInfo
,
168 const css::uno::Sequence
< css::uno::Any
>& rArgs
) const = 0;
171 typedef ::std::map
< sal_Int32
, OUString
> ModulePathMap
;
173 /** Starts listening at the document model. */
174 void startListening();
175 /** Stops listening at the document model. */
176 void stopListening();
178 /** Returns the event handler info struct for the specified event, or throws.
181 @throws css::lang::IllegalArgumentException
183 const EventHandlerInfo
& getEventHandlerInfo( sal_Int32 nEventId
) const;
185 /** Searches the event handler in the document and returns its full script path.
188 @throws css::lang::IllegalArgumentException
189 @throws css::uno::RuntimeException
191 OUString
getEventHandlerPath(
192 const EventHandlerInfo
& rInfo
,
193 const css::uno::Sequence
< css::uno::Any
>& rArgs
);
195 /** On first call, accesses the Basic library containing the VBA source code.
197 @throws css::uno::RuntimeException
199 void ensureVBALibrary();
201 /** Returns the type of the Basic module with the specified name.
203 @throws css::uno::RuntimeException
205 sal_Int32
getModuleType( const OUString
& rModuleName
);
207 /** Updates the map containing paths to event handlers for a Basic module.
209 @throws css::uno::RuntimeException
211 ModulePathMap
& updateModulePathMap( const OUString
& rModuleName
);
214 css::uno::Reference
< css::frame::XModel
> mxModel
;
215 SfxObjectShell
* mpShell
;
218 typedef std::map
< sal_Int32
, EventHandlerInfo
> EventHandlerInfoMap
;
219 typedef std::unordered_map
< OUString
, ModulePathMap
> EventHandlerPathMap
;
221 EventHandlerInfoMap maEventInfos
;
222 EventHandlerPathMap maEventPaths
;
223 css::uno::Reference
< css::script::vba::XVBAModuleInfo
> mxModuleInfos
;
224 OUString maLibraryName
;
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */