Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_VBAHELPER_VBAEVENTSHELPERBASE_HXX
21 : #define INCLUDED_VBAHELPER_VBAEVENTSHELPERBASE_HXX
22 :
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>
31 :
32 : namespace com { namespace sun { namespace star {
33 : namespace script { namespace vba { class XVBAModuleInfo; } }
34 : namespace uno { class XComponentContext; }
35 : } } }
36 :
37 :
38 :
39 : typedef ::cppu::WeakImplHelper3<
40 : css::script::vba::XVBAEventProcessor,
41 : css::document::XEventListener,
42 : css::util::XChangesListener > VbaEventsHelperBase_BASE;
43 :
44 : class VBAHELPER_DLLPUBLIC VbaEventsHelperBase : public VbaEventsHelperBase_BASE
45 : {
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();
51 :
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, std::exception) SAL_OVERRIDE;
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, std::exception) SAL_OVERRIDE;
55 :
56 : // document::XEventListener
57 : virtual void SAL_CALL notifyEvent( const css::document::EventObject& rEvent ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
58 :
59 : // util::XChangesListener
60 : virtual void SAL_CALL changesOccurred( const css::util::ChangesEvent& rEvent ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
61 :
62 : // lang::XEventListener
63 : virtual void SAL_CALL disposing( const css::lang::EventObject& rEvent ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
64 :
65 : // little helpers ---------------------------------------------------------
66 :
67 : /** Helper to execute event handlers without throwing any exceptions. */
68 : void processVbaEventNoThrow( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs );
69 :
70 : /** Throws, if the passed sequence does not contain a value at the specified index. */
71 2008 : static inline void checkArgument( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
72 2008 : { if( (nIndex < 0) || (nIndex >= rArgs.getLength()) ) throw css::lang::IllegalArgumentException(); }
73 :
74 : /** Throws, if the passed sequence does not contain a value of a specific at the specified index. */
75 : template< typename Type >
76 0 : static inline void checkArgumentType( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
77 0 : { checkArgument( rArgs, nIndex ); if( !rArgs[ nIndex ].has< Type >() ) throw css::lang::IllegalArgumentException(); }
78 :
79 : protected:
80 :
81 :
82 3016 : struct EventHandlerInfo
83 : {
84 : sal_Int32 mnEventId;
85 : sal_Int32 mnModuleType;
86 : OUString maMacroName;
87 : sal_Int32 mnCancelIndex;
88 : css::uno::Any maUserData;
89 : };
90 :
91 : /** Registers a supported event handler.
92 :
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() );
104 :
105 :
106 :
107 11640 : struct EventQueueEntry
108 : {
109 : sal_Int32 mnEventId;
110 : css::uno::Sequence< css::uno::Any > maArgs;
111 102 : inline /*implicit*/ EventQueueEntry( sal_Int32 nEventId ) : mnEventId( nEventId ) {}
112 3778 : inline EventQueueEntry( sal_Int32 nEventId, const css::uno::Sequence< css::uno::Any >& rArgs ) : mnEventId( nEventId ), maArgs( rArgs ) {}
113 : };
114 : typedef ::std::deque< EventQueueEntry > EventQueue;
115 :
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;
122 :
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, css::uno::RuntimeException) = 0;
127 :
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;
134 :
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
139 : throw (css::lang::IllegalArgumentException,
140 : css::uno::RuntimeException) = 0;
141 :
142 : private:
143 : typedef ::std::map< sal_Int32, OUString > ModulePathMap;
144 :
145 : /** Starts listening at the document model. */
146 : void startListening();
147 : /** Stops listening at the document model. */
148 : void stopListening();
149 :
150 : /** Returns the event handler info struct for the specified event, or throws. */
151 : const EventHandlerInfo& getEventHandlerInfo( sal_Int32 nEventId ) const throw (css::lang::IllegalArgumentException);
152 :
153 : /** Searches the event handler in the document and returns its full script path. */
154 : OUString getEventHandlerPath(
155 : const EventHandlerInfo& rInfo,
156 : const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
157 :
158 : /** On first call, accesses the Basic library containing the VBA source code. */
159 : void ensureVBALibrary() throw (css::uno::RuntimeException);
160 :
161 : /** Returns the type of the Basic module with the specified name. */
162 : sal_Int32 getModuleType( const OUString& rModuleName ) throw (css::uno::RuntimeException);
163 :
164 : /** Updates the map containing paths to event handlers for a Basic module. */
165 : ModulePathMap& updateModulePathMap( const OUString& rModuleName ) throw (css::uno::RuntimeException);
166 :
167 : protected:
168 : css::uno::Reference< css::frame::XModel > mxModel;
169 : SfxObjectShell* mpShell;
170 :
171 : private:
172 : typedef ::std::map< sal_Int32, EventHandlerInfo > EventHandlerInfoMap;
173 : typedef boost::unordered_map< OUString, ModulePathMap, OUStringHash > EventHandlerPathMap;
174 :
175 : EventHandlerInfoMap maEventInfos;
176 : EventHandlerPathMap maEventPaths;
177 : css::uno::Reference< css::script::vba::XVBAModuleInfo > mxModuleInfos;
178 : OUString maLibraryName;
179 : bool mbDisposed;
180 : };
181 :
182 :
183 :
184 : #endif
185 :
186 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|