bump product version to 7.2.5.1
[LibreOffice.git] / svx / source / accessibility / ShapeTypeHandler.cxx
blob368c2f698ee9410e1935dcac6982015728ec10a1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <svx/ShapeTypeHandler.hxx>
22 #include <svx/SvxShapeTypes.hxx>
23 #include <svx/AccessibleShapeInfo.hxx>
24 #include <vcl/svapp.hxx>
25 #include <svx/AccessibleShape.hxx>
26 #include <svx/dialmgr.hxx>
28 #include <svx/unoshape.hxx>
29 #include <svx/svdoashp.hxx>
31 #include <svx/strings.hrc>
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::accessibility;
36 namespace accessibility {
38 // Pointer to the shape type handler singleton.
39 ShapeTypeHandler* ShapeTypeHandler::instance = nullptr;
42 // Create an empty reference to an accessible object.
43 static rtl::Reference<AccessibleShape>
44 CreateEmptyShapeReference (
45 const AccessibleShapeInfo& /*rShapeInfo*/,
46 const AccessibleShapeTreeInfo& /*rShapeTreeInfo*/,
47 ShapeTypeId /*nId*/)
49 return nullptr;
53 ShapeTypeHandler& ShapeTypeHandler::Instance()
55 // Using double check pattern to make sure that exactly one instance of
56 // the shape type handler is instantiated.
57 if (instance == nullptr)
59 SolarMutexGuard aGuard;
60 if (instance == nullptr)
62 // Create the single instance of the shape type handler.
63 instance = new ShapeTypeHandler;
65 // Register the basic SVX shape types.
66 RegisterDrawShapeTypes ();
70 return *instance;
74 /** The given service name is first transformed into a slot id that
75 identifies the place of the type descriptor. From that descriptor the
76 shape type id is returned.
78 ShapeTypeId ShapeTypeHandler::GetTypeId (const OUString& aServiceName) const
80 tServiceNameToSlotId::const_iterator I (maServiceNameToSlotId.find (aServiceName));
81 if (I != maServiceNameToSlotId.end())
83 return maShapeTypeDescriptorList[I->second].mnShapeTypeId;
85 else
86 return -1;
90 /** Extract the specified shape's service name and forward the request to
91 the appropriate method.
93 ShapeTypeId ShapeTypeHandler::GetTypeId (const uno::Reference<drawing::XShape>& rxShape) const
95 if (rxShape.is())
96 return GetTypeId (rxShape->getShapeType());
97 else
98 return -1;
102 /** This factory method determines the type descriptor for the type of the
103 given shape, then calls the descriptor's create function, and finally
104 initializes the new object.
106 rtl::Reference<AccessibleShape>
107 ShapeTypeHandler::CreateAccessibleObject (
108 const AccessibleShapeInfo& rShapeInfo,
109 const AccessibleShapeTreeInfo& rShapeTreeInfo) const
111 ShapeTypeId nSlotId (GetSlotId (rShapeInfo.mxShape));
112 rtl::Reference<AccessibleShape> pShape(
113 maShapeTypeDescriptorList[nSlotId].maCreateFunction (
114 rShapeInfo,
115 rShapeTreeInfo,
116 maShapeTypeDescriptorList[nSlotId].mnShapeTypeId));
117 return pShape;
121 /** Create the single instance of this class and initialize its list of
122 type descriptors with an entry of an unknown type.
124 ShapeTypeHandler::ShapeTypeHandler()
125 : maShapeTypeDescriptorList (1)
127 // Make sure that at least the UNKNOWN entry is present.
128 // Resize the list, if necessary, so that the new type can be inserted.
129 maShapeTypeDescriptorList[0].mnShapeTypeId = UNKNOWN_SHAPE_TYPE;
130 maShapeTypeDescriptorList[0].msServiceName = "UNKNOWN_SHAPE_TYPE";
131 maShapeTypeDescriptorList[0].maCreateFunction = CreateEmptyShapeReference;
132 maServiceNameToSlotId[maShapeTypeDescriptorList[0].msServiceName] = 0;
136 ShapeTypeHandler::~ShapeTypeHandler()
138 // Because this class is a singleton and the only instance, whose
139 // destructor has just been called, is pointed to from instance,
140 // we reset the static variable instance, so that further calls to
141 // getInstance do not return an undefined object but create a new
142 // singleton.
143 instance = nullptr;
147 void ShapeTypeHandler::AddShapeTypeList (int nDescriptorCount,
148 ShapeTypeDescriptor const aDescriptorList[])
150 SolarMutexGuard aGuard;
152 // Determine first id of new type descriptor(s).
153 int nFirstId = maShapeTypeDescriptorList.size();
155 // Resize the list, if necessary, so that the types can be inserted.
156 maShapeTypeDescriptorList.resize (nFirstId + nDescriptorCount);
158 for (int i=0; i<nDescriptorCount; i++)
160 // Fill Type descriptor.
161 maShapeTypeDescriptorList[nFirstId+i].mnShapeTypeId = aDescriptorList[i].mnShapeTypeId;
162 maShapeTypeDescriptorList[nFirstId+i].msServiceName = aDescriptorList[i].msServiceName;
163 maShapeTypeDescriptorList[nFirstId+i].maCreateFunction = aDescriptorList[i].maCreateFunction;
165 // Update inverse mapping from service name to the descriptor's position.
166 maServiceNameToSlotId[aDescriptorList[i].msServiceName] = nFirstId+i;
171 tools::Long ShapeTypeHandler::GetSlotId (const OUString& aServiceName) const
173 tServiceNameToSlotId::const_iterator I (maServiceNameToSlotId.find (aServiceName));
174 if (I != maServiceNameToSlotId.end())
175 return I->second;
176 else
177 return 0;
181 // Extract the given shape's service name and forward request to appropriate
182 // method.
183 tools::Long ShapeTypeHandler::GetSlotId (const uno::Reference<drawing::XShape>& rxShape) const
185 if (rxShape.is())
186 return GetSlotId (rxShape->getShapeType());
187 else
188 return 0;
191 /// get the accessible base name for an object
192 OUString ShapeTypeHandler::CreateAccessibleBaseName (const uno::Reference<drawing::XShape>& rxShape)
194 const char* pResourceId;
195 OUString sName;
197 switch (ShapeTypeHandler::Instance().GetTypeId (rxShape))
199 // case DRAWING_3D_POLYGON: was removed in original code in
200 // AccessibleShape::CreateAccessibleBaseName. See issue 11190 for details.
201 // Id can be removed from SvxShapeTypes.hxx as well.
202 case DRAWING_3D_CUBE:
203 pResourceId = STR_ObjNameSingulCube3d;
204 break;
205 case DRAWING_3D_EXTRUDE:
206 pResourceId = STR_ObjNameSingulExtrude3d;
207 break;
208 case DRAWING_3D_LATHE:
209 pResourceId = STR_ObjNameSingulLathe3d;
210 break;
211 case DRAWING_3D_SCENE:
212 pResourceId = STR_ObjNameSingulScene3d;
213 break;
214 case DRAWING_3D_SPHERE:
215 pResourceId = STR_ObjNameSingulSphere3d;
216 break;
217 case DRAWING_CAPTION:
218 pResourceId = STR_ObjNameSingulCAPTION;
219 break;
220 case DRAWING_CLOSED_BEZIER:
221 pResourceId = STR_ObjNameSingulPATHFILL;
222 break;
223 case DRAWING_CLOSED_FREEHAND:
224 pResourceId = STR_ObjNameSingulFREEFILL;
225 break;
226 case DRAWING_CONNECTOR:
227 pResourceId = STR_ObjNameSingulEDGE;
228 break;
229 case DRAWING_CONTROL:
230 pResourceId = STR_ObjNameSingulUno;
231 break;
232 case DRAWING_ELLIPSE:
233 pResourceId = STR_ObjNameSingulCIRCE;
234 break;
235 case DRAWING_GROUP:
236 pResourceId = STR_ObjNameSingulGRUP;
237 break;
238 case DRAWING_LINE:
239 pResourceId = STR_ObjNameSingulLINE;
240 break;
241 case DRAWING_MEASURE:
242 pResourceId = STR_ObjNameSingulMEASURE;
243 break;
244 case DRAWING_OPEN_BEZIER:
245 pResourceId = STR_ObjNameSingulPATHLINE;
246 break;
247 case DRAWING_OPEN_FREEHAND:
248 pResourceId = STR_ObjNameSingulFREELINE;
249 break;
250 case DRAWING_PAGE:
251 pResourceId = STR_ObjNameSingulPAGE;
252 break;
253 case DRAWING_POLY_LINE:
254 pResourceId = STR_ObjNameSingulPLIN;
255 break;
256 case DRAWING_POLY_LINE_PATH:
257 pResourceId = STR_ObjNameSingulPLIN;
258 break;
259 case DRAWING_POLY_POLYGON:
260 pResourceId = STR_ObjNameSingulPOLY;
261 break;
262 case DRAWING_POLY_POLYGON_PATH:
263 pResourceId = STR_ObjNameSingulPOLY;
264 break;
265 case DRAWING_RECTANGLE:
266 pResourceId = STR_ObjNameSingulRECT;
267 break;
268 case DRAWING_CUSTOM:
269 pResourceId = STR_ObjNameSingulCUSTOMSHAPE;
271 if (SdrObject* pSdrObject = SdrObject::getSdrObjectFromXShape(rxShape))
273 if (auto pCustomShape = dynamic_cast<SdrObjCustomShape*>(pSdrObject))
275 if (pCustomShape->IsTextPath())
276 pResourceId = STR_ObjNameSingulFONTWORK;
277 else
279 pResourceId = nullptr;
280 sName = pCustomShape->GetCustomShapeName();
284 break;
285 case DRAWING_TEXT:
286 pResourceId = STR_ObjNameSingulTEXT;
287 break;
288 default:
289 pResourceId = nullptr;
290 sName = "UnknownAccessibleShape";
291 if (rxShape.is())
292 sName += ": " + rxShape->getShapeType();
293 break;
296 if (pResourceId)
298 SolarMutexGuard aGuard;
299 sName = SvxResId(pResourceId);
302 return sName;
305 } // end of namespace accessibility
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */