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 VBAHELPER_VBAEVENTSHELPERBASE_HXX
21 : #define 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);
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);
55 :
56 : // document::XEventListener
57 : virtual void SAL_CALL notifyEvent( const css::document::EventObject& rEvent ) throw (css::uno::RuntimeException);
58 :
59 : // util::XChangesListener
60 : virtual void SAL_CALL changesOccurred( const css::util::ChangesEvent& rEvent ) throw (css::uno::RuntimeException);
61 :
62 : // lang::XEventListener
63 : virtual void SAL_CALL disposing( const css::lang::EventObject& rEvent ) throw (css::uno::RuntimeException);
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 0 : static inline void checkArgument( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) throw (css::lang::IllegalArgumentException)
72 0 : { 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 0 : struct EventHandlerInfo
83 : {
84 : sal_Int32 mnEventId;
85 : sal_Int32 mnModuleType;
86 : ::rtl::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 0 : struct EventQueueEntry
108 : {
109 : sal_Int32 mnEventId;
110 : css::uno::Sequence< css::uno::Any > maArgs;
111 0 : inline /*implicit*/ EventQueueEntry( sal_Int32 nEventId ) : mnEventId( nEventId ) {}
112 0 : 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) = 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 ::rtl::OUString implGetDocumentModuleName(
137 : const EventHandlerInfo& rInfo,
138 : const css::uno::Sequence< css::uno::Any >& rArgs ) const throw (css::lang::IllegalArgumentException) = 0;
139 :
140 : private:
141 : typedef ::std::map< sal_Int32, ::rtl::OUString > ModulePathMap;
142 :
143 : /** Starts listening at the document model. */
144 : void startListening();
145 : /** Stops listening at the document model. */
146 : void stopListening();
147 :
148 : /** Returns the event handler info struct for the specified event, or throws. */
149 : const EventHandlerInfo& getEventHandlerInfo( sal_Int32 nEventId ) const throw (css::lang::IllegalArgumentException);
150 :
151 : /** Searches the event handler in the document and returns its full script path. */
152 : ::rtl::OUString getEventHandlerPath(
153 : const EventHandlerInfo& rInfo,
154 : const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
155 :
156 : /** On first call, accesses the Basic library containing the VBA source code. */
157 : void ensureVBALibrary() throw (css::uno::RuntimeException);
158 :
159 : /** Returns the type of the Basic module with the specified name. */
160 : sal_Int32 getModuleType( const ::rtl::OUString& rModuleName ) throw (css::uno::RuntimeException);
161 :
162 : /** Updates the map containing paths to event handlers for a Basic module. */
163 : ModulePathMap& updateModulePathMap( const ::rtl::OUString& rModuleName ) throw (css::uno::RuntimeException);
164 :
165 : protected:
166 : css::uno::Reference< css::frame::XModel > mxModel;
167 : SfxObjectShell* mpShell;
168 :
169 : private:
170 : typedef ::std::map< sal_Int32, EventHandlerInfo > EventHandlerInfoMap;
171 : typedef boost::unordered_map< ::rtl::OUString, ModulePathMap, ::rtl::OUStringHash > EventHandlerPathMap;
172 :
173 : EventHandlerInfoMap maEventInfos;
174 : EventHandlerPathMap maEventPaths;
175 : css::uno::Reference< css::script::vba::XVBAModuleInfo > mxModuleInfos;
176 : ::rtl::OUString maLibraryName;
177 : bool mbDisposed;
178 : };
179 :
180 : // ============================================================================
181 :
182 : #endif
183 :
184 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|