xref: /trunk/main/svx/inc/svx/ShapeTypeHandler.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 
24 #ifndef _SVX_ACCESSIBILITY_SHAPE_TYPE_HANDLER_HXX
25 #define _SVX_ACCESSIBILITY_SHAPE_TYPE_HANDLER_HXX
26 
27 #include <svx/AccessibleShapeTreeInfo.hxx>
28 #include <svx/AccessibleShapeInfo.hxx>
29 #include <svx/AccessibleShape.hxx>
30 #include <com/sun/star/accessibility/XAccessible.hpp>
31 #include <com/sun/star/uno/XInterface.hpp>
32 #include <com/sun/star/drawing/XShape.hpp>
33 #include <comphelper/stl_types.hxx>
34 #include <com/sun/star/document/XEventBroadcaster.hpp>
35 #include "svx/svxdllapi.h"
36 
37 #include <rtl/ustring.hxx>
38 #include <vector>
39 #include <hash_map>
40 
41 namespace accessibility {
42 
43 /** Use an integer to represent shape type ids.  A ShapeTypeId is unique
44     inside one project but is not over the project boundaries.
45 */
46 typedef int ShapeTypeId;
47 
48 /** Define the function type for creating accessible objects for given
49     service names.
50 */
51 typedef AccessibleShape* (*tCreateFunction)
52     (const AccessibleShapeInfo& rShapeInfo,
53     const AccessibleShapeTreeInfo& rShapeTreeInfo,
54     ShapeTypeId nId);
55 
56 /** Each shape type is described by listing its id, its service name and a
57     function which creates a new accessible object that can represent that
58     service.  The id has to be unique with respect to the create function.
59 */
60 struct ShapeTypeDescriptor
61 {
62     ShapeTypeId         mnShapeTypeId;
63     rtl::OUString       msServiceName;
64     tCreateFunction     maCreateFunction;
ShapeTypeDescriptoraccessibility::ShapeTypeDescriptor65     ShapeTypeDescriptor (
66         ShapeTypeId nId, const rtl::OUString& sName, tCreateFunction aFunction)
67     :   mnShapeTypeId (nId),
68         msServiceName (sName),
69         maCreateFunction (aFunction)
70     {}
ShapeTypeDescriptoraccessibility::ShapeTypeDescriptor71     ShapeTypeDescriptor (void)
72     :   mnShapeTypeId (-1),
73         msServiceName (),
74         maCreateFunction (NULL)
75     {}
76 };
77 
78 /** @descr
79         This class is a singleton that has the purpose to transform between
80         service names of shapes and associated enum values and to create new
81         accessible objects for given shapes.
82 */
83 class SVX_DLLPUBLIC ShapeTypeHandler
84 {
85 public:
86     enum { UNKNOWN_SHAPE_TYPE = 0 };
87 
88     /** This function returns a reference to the only instance of this class.
89         Use this instance to retrieve a shape's type and service name.
90         @return
91             Returns a reference to a <type>ShapeTypeHandler</type> object.
92      */
93     static ShapeTypeHandler& Instance (void);
94 
95     /**  Determines the type id of a shape with the given service name.
96          @param aServiceName
97              Service name of the shape for which to return the type id.
98          @return
99              Returns the type id of the shape with the given service name or
100              -1 when the service name is not known.
101      */
102     ShapeTypeId GetTypeId (const ::rtl::OUString& aServiceName) const;
103 
104     /**  Determines the type id of the specified shape.
105          @param xShape
106              Reference to the shape for which to return the type id.
107          @return
108              Returns the type id of the specified shape or
109              -1 when the given reference is either not
110              set or the referenced object does not support the
111              <type>XShapeDescriptor</type> interface.
112      */
113     ShapeTypeId GetTypeId (const ::com::sun::star::uno::Reference<
114         ::com::sun::star::drawing::XShape>& rxShape) const;
115 
116     /**  Return the service name of a shape with the specified type.
117          @param aTypeId
118              Id of the shape type for which to retrieve the service name.
119              An unknown shape id results in an empty string.
120          @return
121              The returned string contains the service name, with which a
122              shape of the given type can be created.
123      */
124     const ::rtl::OUString& GetServiceName (ShapeTypeId aTypeId) const;
125 
126     /**  Create a new accessible object for the given shape.
127          @param rShapeInfo
128              Bundle of information passed to the new accessible shape.
129          @param rShapeTreeInfo
130              Bundle of information passed down the shape tree.
131          @return
132              Pointer to the implementation object that implements the
133              <code>XAccessible</code> interface.  This pointer may be NULL
134              if the specified shape is of unknown type.
135      */
136     AccessibleShape*
137         CreateAccessibleObject (
138             const AccessibleShapeInfo& rShapeInfo,
139             const AccessibleShapeTreeInfo& rShapeTreeInfo) const;
140 
141     /** Compatibility function.
142     */
143     AccessibleShape*
CreateAccessibleObject(const::com::sun::star::uno::Reference<::com::sun::star::drawing::XShape> & rxShape,const::com::sun::star::uno::Reference<::com::sun::star::accessibility::XAccessible> & rxParent,const AccessibleShapeTreeInfo & rShapeTreeInfo) const144         CreateAccessibleObject (
145             const ::com::sun::star::uno::Reference<
146             ::com::sun::star::drawing::XShape>& rxShape,
147             const ::com::sun::star::uno::Reference<
148             ::com::sun::star::accessibility::XAccessible>& rxParent,
149             const AccessibleShapeTreeInfo& rShapeTreeInfo) const
150     {
151         AccessibleShapeInfo aShapeInfo(rxShape, rxParent);
152         return CreateAccessibleObject (aShapeInfo, rShapeTreeInfo);
153     }
154 
155     /**  Add new shape types to the internal tables.  Each new shape type is
156          described by one shape type descriptor.  See
157          <type>ShapeTypeDescriptor</type> for more details.
158 
159          @param nDescriptorCount
160              Number of new shape types.
161          @param aDescriptorList
162              Array of new shape type descriptors.
163          @return
164              The returned flag indicates whether the specified shape
165              descriptors have been successfully added.
166      */
167     bool AddShapeTypeList (int nDescriptorCount,
168         ShapeTypeDescriptor aDescriptorList[]);
169 
170     /// get the accessible base name for an object
171     static ::rtl::OUString CreateAccessibleBaseName (
172         const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape >& rxShape);
173 
174 protected:
175     // Declare default constructor, copy constructor, destructor, and
176     // assignment operation protected so that no one accidentally creates a
177     // second instance of this singleton class or deletes it.
178     ShapeTypeHandler (void);
179     ShapeTypeHandler (const ShapeTypeHandler& aHandler);             // never implemented, this is a singleton class
180     ShapeTypeHandler& operator= (const ShapeTypeHandler& aHandler); // never implemented, this is a singleton class
181 
182     /** This destructor is never called at the moment.  But because this
183         class is a singleton this is not a problem.
184     */
185     virtual ~ShapeTypeHandler (void);
186 
187 private:
188     /// Pointer to the only instance of this class.
189     static ShapeTypeHandler* instance;
190 
191     /** List of shape type descriptors.  This list is normally build up in
192         several steps when libraries that implement shapes are loaded and
193         call the addShapeTypeList method.  After that no modifications of
194         the list take place.
195     */
196     ::std::vector<ShapeTypeDescriptor> maShapeTypeDescriptorList;
197 
198     /** This hash map allows the fast look up of a type descriptor for a
199         given service name.
200     */
201     typedef ::std::hash_map<
202         ::rtl::OUString,ShapeTypeId,
203         ::rtl::OUStringHash,
204         //        ::comphelper::UStringHash,
205         ::comphelper::UStringEqual> tServiceNameToSlotId;
206     mutable tServiceNameToSlotId maServiceNameToSlotId;
207 
208     /**  Determine the slot id of the specified shape type.  With this id
209          internal methods can access the associated type descriptor.
210          @param aServiceName
211              Service name of the shape for which to return the slot id.
212          @return
213              Returns the slot id of the shape with the given service name or
214              0 when the service name is not known.
215      */
216     SVX_DLLPRIVATE long GetSlotId (const ::rtl::OUString& aServiceName) const;
217 
218     /**  Determine the slot id of the specified shape type.  With this id
219          internal methods can access the associated type descriptor.
220          @param rxShape
221              Shape for which to return the slot id.
222          @return
223              Returns the slot id of the shape with the given service name or
224              0 when the service name is not known.
225      */
226     SVX_DLLPRIVATE long GetSlotId (const ::com::sun::star::uno::Reference<
227         ::com::sun::star::drawing::XShape>& rxShape) const;
228 };
229 
230 } // end of namespace accessible
231 
232 #endif
233