xref: /trunk/main/svtools/inc/svtools/unoevent.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _SVTOOLS_UNOEVENT_HXX_
24 #define _SVTOOLS_UNOEVENT_HXX_
25 
26 #include "svtools/svtdllapi.h"
27 #include <com/sun/star/container/XNameReplace.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/uno/XInterface.hpp>
30 #include <cppuhelper/implbase2.hxx>
31 
32 class SvxMacroTableDtor;
33 class SvxMacroItem;
34 class SvxMacro;
35 
36 /** SvEventDescription: Description of a single event.
37     mnEvent is the id used by SvxMacroItem
38     mpEventName is the api name for this event
39 
40     the last event in an array is indicated by mnEvent && mpEventName == 0
41 */
42 struct SvEventDescription
43 {
44     sal_uInt16 mnEvent;
45     const sal_Char* mpEventName;
46 };
47 
48 /**
49  * SvBaseEventDescriptor: Abstract class that implements the basics
50  * of an XNameReplace that is delivered by the
51  * XEventsSupplier::getEvents() method.
52  *
53  * The functionality this class provides is:
54  * 1) Which elements are in the XNameReplace?
55  * 2) Mapping from Api names to item IDs.
56  * 3) conversion from SvxMacroItem to Any and vice versa.
57  *
58  * All details of how to actually get and set SvxMacroItem(s) have to
59  * be supplied by the base class.
60  */
61 class SVT_DLLPUBLIC SvBaseEventDescriptor : public cppu::WeakImplHelper2
62 <
63     ::com::sun::star::container::XNameReplace,
64     ::com::sun::star::lang::XServiceInfo
65 >
66 {
67     const ::rtl::OUString sEventType;
68     const ::rtl::OUString sMacroName;
69     const ::rtl::OUString sLibrary;
70     const ::rtl::OUString sStarBasic;
71     const ::rtl::OUString sJavaScript;
72     const ::rtl::OUString sScript;
73     const ::rtl::OUString sNone;
74 
75 
76     /// name of own service
77     const ::rtl::OUString sServiceName;
78 
79 protected:
80     const ::rtl::OUString sEmpty;
81 
82     /// last element is 0, 0
83     const SvEventDescription* mpSupportedMacroItems;
84     sal_Int16 mnMacroItems;
85 
86 public:
87 
88     SvBaseEventDescriptor(const SvEventDescription* pSupportedMacroItems);
89 
90     virtual ~SvBaseEventDescriptor();
91 
92 
93     // XNameReplace
94     /// calls replaceByName(const sal_uInt16, const SvxMacro&)
95     virtual void SAL_CALL replaceByName(
96         const ::rtl::OUString& rName,                /// API name of event
97         const ::com::sun::star::uno::Any& rElement ) /// event (PropertyValues)
98             ;
99 
100     // XNameAccess (via XNameReplace)
101     /// calls getByName(sal_uInt16)
102     virtual ::com::sun::star::uno::Any SAL_CALL getByName(
103         const ::rtl::OUString& rName )  /// API name of event
104             ;
105 
106     // XNameAxcess (via XNameReplace)
107     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
108                                                         getElementNames();
109 
110     // XNameAccess (via XNameReplace)
111     virtual sal_Bool SAL_CALL hasByName(
112         const ::rtl::OUString& rName );
113 
114     // XElementAccess (via XNameReplace)
115     virtual ::com::sun::star::uno::Type SAL_CALL getElementType();
116 
117     // XElementAccess (via XNameReplace)
118     virtual sal_Bool SAL_CALL hasElements();
119 
120     // XServiceInfo
121     /// must be implemented in subclass
122     virtual rtl::OUString SAL_CALL getImplementationName(void) = 0;
123 
124     // XServiceInfo
125     virtual sal_Bool SAL_CALL supportsService(const rtl::OUString& ServiceName);
126 
127     // XServiceInfo
128     virtual ::com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL
129         getSupportedServiceNames(void);
130 
131 protected:
132 
133     /// Must be implemented in subclass.
134     virtual void replaceByName(
135         const sal_uInt16 nEvent,        /// item ID of event
136         const SvxMacro& rMacro)     /// event (will be copied)
137              = 0;
138 
139     /// Must be implemented in subclass.
140     virtual void getByName(
141         SvxMacro& rMacro,
142         const sal_uInt16 nEvent ) = 0;
143 
144     /// convert an API event name to the event ID as used by SvxMacroItem
145     sal_uInt16 mapNameToEventID(const ::rtl::OUString& rName) const;
146 
147     /// convert an event ID to an API event name
148     ::rtl::OUString mapEventIDToName(sal_uInt16 nPoolID) const;
149 
150     /// get the event ID for the name; return 0 if not supported
151     sal_uInt16 getMacroID(const ::rtl::OUString& rName) const;
152 
153     /// create PropertyValues and Any from macro
154     void getAnyFromMacro(
155         ::com::sun::star::uno::Any& aAny,   // Any to be filled by Macro values
156         const SvxMacro& rMacro);
157 
158     /// create macro from PropertyValues (in an Any)
159     void getMacroFromAny(
160         SvxMacro& aMacro,       // reference to be filled by Any
161         const ::com::sun::star::uno::Any& rAny);
162 
163 };
164 
165 
166 
167 
168 /**
169  * SvEventDescriptor: Implement the XNameReplace that is delivered by
170  * the XEventsSupplier::getEvents() method. The SvEventDescriptor has
171  * to be subclassed to implement the events for a specific
172  * objects. The subclass has to
173  * 1) supply the super class constructor with a list of known events (item IDs)
174  * 2) supply the super class constructor with a reference of it's parent object
175  *    (to prevent destruction)
176  * 3) implement getItem() and setItem(...) methods.
177  *
178  * If no object is available to which the SvEventDescriptor can attach itself,
179  * the class SvDetachedEventDescriptor should be used.
180  */
181 class SVT_DLLPUBLIC SvEventDescriptor : public SvBaseEventDescriptor
182 {
183     /// keep reference to parent to prevent it from being destroyed
184     ::com::sun::star::uno::Reference<
185         ::com::sun::star::uno::XInterface > xParentRef;
186 
187 public:
188 
189     SvEventDescriptor(::com::sun::star::uno::XInterface& rParent,
190                       const SvEventDescription* pSupportedMacroItems);
191 
192     virtual ~SvEventDescriptor();
193 
194 
195 protected:
196 
197 
198     using SvBaseEventDescriptor::replaceByName;
199     virtual void replaceByName(
200         const sal_uInt16 nEvent,        /// item ID of event
201         const SvxMacro& rMacro)     /// event (will be copied)
202             ;
203 
204     using SvBaseEventDescriptor::getByName;
205     virtual void getByName(
206         SvxMacro& rMacros,          /// macro to be filled with values
207         const sal_uInt16 nEvent )       /// item ID of event
208             ;
209 
210 
211     /// Get the SvxMacroItem from the parent.
212     /// must be implemented by subclass
213     virtual const SvxMacroItem& getMacroItem() = 0;
214 
215     /// Set the SvxMacroItem at the parent.
216     /// must be implemented by subclass
217     virtual void setMacroItem(const SvxMacroItem& rItem) = 0;
218 
219     /// Get the SvxMacroItem Which Id needed for the current application
220     /// must be implemented by subclass
221     virtual sal_uInt16 getMacroItemWhich() const = 0;
222 };
223 
224 
225 /**
226  * SvDetachedEventDescriptor:
227  */
228 class SVT_DLLPUBLIC SvDetachedEventDescriptor : public SvBaseEventDescriptor
229 {
230     // the macros; aMacros[i] is the value for aSupportedMacroItemIDs[i]
231     SvxMacro** aMacros;
232 
233     const ::rtl::OUString sImplName;
234 
235 public:
236 
237     SvDetachedEventDescriptor(const SvEventDescription* pSupportedMacroItems);
238 
239     virtual ~SvDetachedEventDescriptor();
240 
241     //XServiceInfo
242     virtual rtl::OUString SAL_CALL getImplementationName(void);
243 
244 protected:
245 
246     sal_Int16 getIndex(const sal_uInt16 nID) const;
247 
248     using SvBaseEventDescriptor::replaceByName;
249     virtual void replaceByName(
250         const sal_uInt16 nEvent,        /// item ID of event
251         const SvxMacro& rMacro)     /// event (will be copied)
252             ;
253 
254     using SvBaseEventDescriptor::getByName;
255     virtual void getByName(
256         SvxMacro& rMacro,           /// macro to be filled
257         const sal_uInt16 nEvent )       /// item ID of event
258             ;
259 
260     /// do we have an event?
261     /// return sal_True: we have a macro for the event
262     /// return sal_False: no macro; getByName() will return an empty macro
263     /// IllegalArgumentException: the event is not supported
264     using SvBaseEventDescriptor::hasByName;
265     virtual sal_Bool hasByName(
266         const sal_uInt16 nEvent ) const     /// item ID of event
267             ;
268 
269 };
270 
271 class SVT_DLLPUBLIC SvMacroTableEventDescriptor : public SvDetachedEventDescriptor
272 {
273 public:
274 
275     SvMacroTableEventDescriptor(const SvEventDescription* pSupportedMacroItems);
276     SvMacroTableEventDescriptor(const SvxMacroTableDtor& aFmt,
277                                 const SvEventDescription* pSupportedMacroItems);
278 
279     virtual ~SvMacroTableEventDescriptor();
280 
281     void copyMacrosFromTable(const SvxMacroTableDtor& aFmt);
282     void copyMacrosIntoTable(SvxMacroTableDtor& aFmt);
283 };
284 
285 #endif
286