Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / accessibility / ShapeTypeHandler.cxx
blob1b169c761a3e12371c8c538c5f7995f6628d70f9
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/svdoashp.hxx>
30 #include <svx/strings.hrc>
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::accessibility;
35 namespace accessibility {
37 // Pointer to the shape type handler singleton.
38 ShapeTypeHandler* ShapeTypeHandler::instance = nullptr;
41 // Create an empty reference to an accessible object.
42 static rtl::Reference<AccessibleShape>
43 CreateEmptyShapeReference (
44 const AccessibleShapeInfo& /*rShapeInfo*/,
45 const AccessibleShapeTreeInfo& /*rShapeTreeInfo*/,
46 ShapeTypeId /*nId*/)
48 return nullptr;
52 ShapeTypeHandler& ShapeTypeHandler::Instance()
54 // Using double check pattern to make sure that exactly one instance of
55 // the shape type handler is instantiated.
56 if (instance == nullptr)
58 SolarMutexGuard aGuard;
59 if (instance == nullptr)
61 // Create the single instance of the shape type handler.
62 instance = new ShapeTypeHandler;
64 // Register the basic SVX shape types.
65 RegisterDrawShapeTypes ();
69 return *instance;
73 /** The given service name is first transformed into a slot id that
74 identifies the place of the type descriptor. From that descriptor the
75 shape type id is returned.
77 ShapeTypeId ShapeTypeHandler::GetTypeId (const OUString& aServiceName) const
79 tServiceNameToSlotId::const_iterator I (maServiceNameToSlotId.find (aServiceName));
80 if (I != maServiceNameToSlotId.end())
82 return maShapeTypeDescriptorList[I->second].mnShapeTypeId;
84 else
85 return -1;
89 /** Extract the specified shape's service name and forward the request to
90 the appropriate method.
92 ShapeTypeId ShapeTypeHandler::GetTypeId (const uno::Reference<drawing::XShape>& rxShape) const
94 if (rxShape.is())
95 return GetTypeId (rxShape->getShapeType());
96 else
97 return -1;
101 /** This factory method determines the type descriptor for the type of the
102 given shape, then calls the descriptor's create function, and finally
103 initializes the new object.
105 rtl::Reference<AccessibleShape>
106 ShapeTypeHandler::CreateAccessibleObject (
107 const AccessibleShapeInfo& rShapeInfo,
108 const AccessibleShapeTreeInfo& rShapeTreeInfo) const
110 ShapeTypeId nSlotId (GetSlotId (rShapeInfo.mxShape));
111 rtl::Reference<AccessibleShape> pShape(
112 maShapeTypeDescriptorList[nSlotId].maCreateFunction (
113 rShapeInfo,
114 rShapeTreeInfo,
115 maShapeTypeDescriptorList[nSlotId].mnShapeTypeId));
116 return pShape;
120 /** Create the single instance of this class and initialize its list of
121 type descriptors with an entry of an unknown type.
123 ShapeTypeHandler::ShapeTypeHandler()
124 : maShapeTypeDescriptorList (1)
126 // Make sure that at least the UNKNOWN entry is present.
127 // Resize the list, if necessary, so that the new type can be inserted.
128 maShapeTypeDescriptorList[0].mnShapeTypeId = UNKNOWN_SHAPE_TYPE;
129 maShapeTypeDescriptorList[0].msServiceName = "UNKNOWN_SHAPE_TYPE";
130 maShapeTypeDescriptorList[0].maCreateFunction = CreateEmptyShapeReference;
131 maServiceNameToSlotId[maShapeTypeDescriptorList[0].msServiceName] = 0;
135 ShapeTypeHandler::~ShapeTypeHandler()
137 // Because this class is a singleton and the only instance, whose
138 // destructor has just been called, is pointed to from instance,
139 // we reset the static variable instance, so that further calls to
140 // getInstance do not return an undefined object but create a new
141 // singleton.
142 instance = nullptr;
146 void ShapeTypeHandler::AddShapeTypeList (int nDescriptorCount,
147 ShapeTypeDescriptor const aDescriptorList[])
149 SolarMutexGuard aGuard;
151 // Determine first id of new type descriptor(s).
152 int nFirstId = maShapeTypeDescriptorList.size();
154 // Resize the list, if necessary, so that the types can be inserted.
155 maShapeTypeDescriptorList.resize (nFirstId + nDescriptorCount);
157 for (int i=0; i<nDescriptorCount; i++)
159 // Fill Type descriptor.
160 maShapeTypeDescriptorList[nFirstId+i].mnShapeTypeId = aDescriptorList[i].mnShapeTypeId;
161 maShapeTypeDescriptorList[nFirstId+i].msServiceName = aDescriptorList[i].msServiceName;
162 maShapeTypeDescriptorList[nFirstId+i].maCreateFunction = aDescriptorList[i].maCreateFunction;
164 // Update inverse mapping from service name to the descriptor's position.
165 maServiceNameToSlotId[aDescriptorList[i].msServiceName] = nFirstId+i;
170 tools::Long ShapeTypeHandler::GetSlotId (const OUString& aServiceName) const
172 tServiceNameToSlotId::const_iterator I (maServiceNameToSlotId.find (aServiceName));
173 if (I != maServiceNameToSlotId.end())
174 return I->second;
175 else
176 return 0;
180 // Extract the given shape's service name and forward request to appropriate
181 // method.
182 tools::Long ShapeTypeHandler::GetSlotId (const uno::Reference<drawing::XShape>& rxShape) const
184 if (rxShape.is())
185 return GetSlotId (rxShape->getShapeType());
186 else
187 return 0;
190 /// get the accessible base name for an object
191 OUString ShapeTypeHandler::CreateAccessibleBaseName (const uno::Reference<drawing::XShape>& rxShape)
193 TranslateId pResourceId;
194 OUString sName;
196 switch (ShapeTypeHandler::Instance().GetTypeId (rxShape))
198 // case DRAWING_3D_POLYGON: was removed in original code in
199 // AccessibleShape::CreateAccessibleBaseName. See issue 11190 for details.
200 // Id can be removed from SvxShapeTypes.hxx as well.
201 case DRAWING_3D_CUBE:
202 pResourceId = STR_ObjNameSingulCube3d;
203 break;
204 case DRAWING_3D_EXTRUDE:
205 pResourceId = STR_ObjNameSingulExtrude3d;
206 break;
207 case DRAWING_3D_LATHE:
208 pResourceId = STR_ObjNameSingulLathe3d;
209 break;
210 case DRAWING_3D_SCENE:
211 pResourceId = STR_ObjNameSingulScene3d;
212 break;
213 case DRAWING_3D_SPHERE:
214 pResourceId = STR_ObjNameSingulSphere3d;
215 break;
216 case DRAWING_CAPTION:
217 pResourceId = STR_ObjNameSingulCAPTION;
218 break;
219 case DRAWING_CLOSED_BEZIER:
220 pResourceId = STR_ObjNameSingulPATHFILL;
221 break;
222 case DRAWING_CLOSED_FREEHAND:
223 pResourceId = STR_ObjNameSingulFREEFILL;
224 break;
225 case DRAWING_CONNECTOR:
226 pResourceId = STR_ObjNameSingulEDGE;
227 break;
228 case DRAWING_CONTROL:
229 pResourceId = STR_ObjNameSingulUno;
230 break;
231 case DRAWING_ELLIPSE:
232 pResourceId = STR_ObjNameSingulCIRCE;
233 break;
234 case DRAWING_GROUP:
235 pResourceId = STR_ObjNameSingulGRUP;
236 break;
237 case DRAWING_LINE:
238 pResourceId = STR_ObjNameSingulLINE;
239 break;
240 case DRAWING_MEASURE:
241 pResourceId = STR_ObjNameSingulMEASURE;
242 break;
243 case DRAWING_OPEN_BEZIER:
244 pResourceId = STR_ObjNameSingulPATHLINE;
245 break;
246 case DRAWING_OPEN_FREEHAND:
247 pResourceId = STR_ObjNameSingulFREELINE;
248 break;
249 case DRAWING_PAGE:
250 pResourceId = STR_ObjNameSingulPAGE;
251 break;
252 case DRAWING_POLY_LINE:
253 pResourceId = STR_ObjNameSingulPLIN;
254 break;
255 case DRAWING_POLY_LINE_PATH:
256 pResourceId = STR_ObjNameSingulPLIN;
257 break;
258 case DRAWING_POLY_POLYGON:
259 pResourceId = STR_ObjNameSingulPOLY;
260 break;
261 case DRAWING_POLY_POLYGON_PATH:
262 pResourceId = STR_ObjNameSingulPOLY;
263 break;
264 case DRAWING_RECTANGLE:
265 pResourceId = STR_ObjNameSingulRECT;
266 break;
267 case DRAWING_CUSTOM:
268 pResourceId = STR_ObjNameSingulCUSTOMSHAPE;
270 if (SdrObject* pSdrObject = SdrObject::getSdrObjectFromXShape(rxShape))
272 if (auto pCustomShape = dynamic_cast<SdrObjCustomShape*>(pSdrObject))
274 if (pCustomShape->IsTextPath())
275 pResourceId = STR_ObjNameSingulFONTWORK;
276 else
278 pResourceId = {};
279 sName = pCustomShape->GetCustomShapeName();
283 break;
284 case DRAWING_TEXT:
285 pResourceId = STR_ObjNameSingulTEXT;
286 break;
287 default:
288 pResourceId = {};
289 sName = "UnknownAccessibleShape";
290 if (rxShape.is())
291 sName += ": " + rxShape->getShapeType();
292 break;
295 if (pResourceId)
297 SolarMutexGuard aGuard;
298 sName = SvxResId(pResourceId);
301 return sName;
304 } // end of namespace accessibility
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */