lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / svx / source / accessibility / ShapeTypeHandler.cxx
blob2581573e499398413d6ed969a280ca4063f0a8c3
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 <com/sun/star/drawing/XShapeDescriptor.hpp>
25 #include <osl/mutex.hxx>
26 #include <vcl/svapp.hxx>
27 #include <svx/dialmgr.hxx>
29 #include <svx/unoshape.hxx>
30 #include <svx/svdoashp.hxx>
31 #include "svx/unoapi.hxx"
33 #include "svx/svdstr.hrc"
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::accessibility;
38 namespace accessibility {
40 // Pointer to the shape type handler singleton.
41 ShapeTypeHandler* ShapeTypeHandler::instance = NULL;
44 // Create an empty reference to an accessible object.
45 AccessibleShape*
46 CreateEmptyShapeReference (
47 const AccessibleShapeInfo& /*rShapeInfo*/,
48 const AccessibleShapeTreeInfo& /*rShapeTreeInfo*/,
49 ShapeTypeId /*nId*/)
51 return NULL;
57 ShapeTypeHandler& ShapeTypeHandler::Instance()
59 // Using double check pattern to make sure that exactly one instance of
60 // the shape type handler is instantiated.
61 if (instance == NULL)
63 SolarMutexGuard aGuard;
64 if (instance == NULL)
66 // Create the single instance of the shape type handler.
67 instance = new ShapeTypeHandler;
69 // Register the basic SVX shape types.
70 RegisterDrawShapeTypes ();
74 return *instance;
80 /** The given service name is first transformed into a slot id that
81 identifies the place of the type descriptor. From that descriptor the
82 shape type id is returned.
84 ShapeTypeId ShapeTypeHandler::GetTypeId (const OUString& aServiceName) const
86 tServiceNameToSlotId::iterator I (maServiceNameToSlotId.find (aServiceName));
87 if (I != maServiceNameToSlotId.end())
89 return maShapeTypeDescriptorList[I->second].mnShapeTypeId;
91 else
92 return -1;
97 /** Extract the specified shape's service name and forward the request to
98 the appropriate method.
100 ShapeTypeId ShapeTypeHandler::GetTypeId (const uno::Reference<drawing::XShape>& rxShape) const
102 uno::Reference<drawing::XShapeDescriptor> xDescriptor (rxShape, uno::UNO_QUERY);
103 if (xDescriptor.is())
104 return GetTypeId (xDescriptor->getShapeType());
105 else
106 return -1;
112 /** This factory method determines the type descriptor for the type of the
113 given shape, then calls the descriptor's create function, and finally
114 initializes the new object.
116 AccessibleShape*
117 ShapeTypeHandler::CreateAccessibleObject (
118 const AccessibleShapeInfo& rShapeInfo,
119 const AccessibleShapeTreeInfo& rShapeTreeInfo) const
121 ShapeTypeId nSlotId (GetSlotId (rShapeInfo.mxShape));
122 AccessibleShape* pShape =
123 maShapeTypeDescriptorList[nSlotId].maCreateFunction (
124 rShapeInfo,
125 rShapeTreeInfo,
126 maShapeTypeDescriptorList[nSlotId].mnShapeTypeId);
127 return pShape;
133 /** Create the single instance of this class and initialize its list of
134 type descriptors with an entry of an unknown type.
136 ShapeTypeHandler::ShapeTypeHandler()
137 : maShapeTypeDescriptorList (1)
139 // Make sure that at least the UNKNOWN entry is present.
140 // Resize the list, if necessary, so that the new type can be inserted.
141 maShapeTypeDescriptorList[0].mnShapeTypeId = UNKNOWN_SHAPE_TYPE;
142 maShapeTypeDescriptorList[0].msServiceName = "UNKNOWN_SHAPE_TYPE";
143 maShapeTypeDescriptorList[0].maCreateFunction = CreateEmptyShapeReference;
144 maServiceNameToSlotId[maShapeTypeDescriptorList[0].msServiceName] = 0;
150 ShapeTypeHandler::~ShapeTypeHandler()
152 // Because this class is a singleton and the only instance, whose
153 // destructor has just been called, is pointed to from instance,
154 // we reset the static variable instance, so that further calls to
155 // getInstance do not return an undefined object but create a new
156 // singleton.
157 instance = NULL;
163 bool ShapeTypeHandler::AddShapeTypeList (int nDescriptorCount,
164 ShapeTypeDescriptor aDescriptorList[])
166 SolarMutexGuard aGuard;
168 // Determine first id of new type descriptor(s).
169 int nFirstId = maShapeTypeDescriptorList.size();
171 // Resize the list, if necessary, so that the types can be inserted.
172 maShapeTypeDescriptorList.resize (nFirstId + nDescriptorCount);
174 for (int i=0; i<nDescriptorCount; i++)
176 #if OSL_DEBUG_LEVEL > 0
177 ShapeTypeId nId (aDescriptorList[i].mnShapeTypeId);
178 (void)nId;
179 #endif
181 // Fill Type descriptor.
182 maShapeTypeDescriptorList[nFirstId+i].mnShapeTypeId = aDescriptorList[i].mnShapeTypeId;
183 maShapeTypeDescriptorList[nFirstId+i].msServiceName = aDescriptorList[i].msServiceName;
184 maShapeTypeDescriptorList[nFirstId+i].maCreateFunction = aDescriptorList[i].maCreateFunction;
186 // Update inverse mapping from service name to the descriptor's position.
187 maServiceNameToSlotId[aDescriptorList[i].msServiceName] = nFirstId+i;
190 return true;
196 long ShapeTypeHandler::GetSlotId (const OUString& aServiceName) const
198 tServiceNameToSlotId::iterator I (maServiceNameToSlotId.find (aServiceName));
199 if (I != maServiceNameToSlotId.end())
200 return I->second;
201 else
202 return 0;
208 // Extract the given shape's service name and forward request to appropriate
209 // method.
210 long ShapeTypeHandler::GetSlotId (const uno::Reference<drawing::XShape>& rxShape) const
212 uno::Reference<drawing::XShapeDescriptor> xDescriptor (rxShape, uno::UNO_QUERY);
213 if (xDescriptor.is())
214 return GetSlotId (xDescriptor->getShapeType());
215 else
216 return 0;
219 /// get the accessible base name for an object
220 OUString
221 ShapeTypeHandler::CreateAccessibleBaseName (const uno::Reference<drawing::XShape>& rxShape)
222 throw (::com::sun::star::uno::RuntimeException)
224 sal_Int32 nResourceId;
225 OUString sName;
227 switch (ShapeTypeHandler::Instance().GetTypeId (rxShape))
229 // case DRAWING_3D_POLYGON: was removed in original code in
230 // AccessibleShape::CreateAccessibleBaseName. See issue 11190 for details.
231 // Id can be removed from SvxShapeTypes.hxx as well.
232 case DRAWING_3D_CUBE:
233 nResourceId = STR_ObjNameSingulCube3d;
234 break;
235 case DRAWING_3D_EXTRUDE:
236 nResourceId = STR_ObjNameSingulExtrude3d;
237 break;
238 case DRAWING_3D_LATHE:
239 nResourceId = STR_ObjNameSingulLathe3d;
240 break;
241 case DRAWING_3D_SCENE:
242 nResourceId = STR_ObjNameSingulScene3d;
243 break;
244 case DRAWING_3D_SPHERE:
245 nResourceId = STR_ObjNameSingulSphere3d;
246 break;
247 case DRAWING_CAPTION:
248 nResourceId = STR_ObjNameSingulCAPTION;
249 break;
250 case DRAWING_CLOSED_BEZIER:
251 nResourceId = STR_ObjNameSingulPATHFILL;
252 break;
253 case DRAWING_CLOSED_FREEHAND:
254 nResourceId = STR_ObjNameSingulFREEFILL;
255 break;
256 case DRAWING_CONNECTOR:
257 nResourceId = STR_ObjNameSingulEDGE;
258 break;
259 case DRAWING_CONTROL:
260 nResourceId = STR_ObjNameSingulUno;
261 break;
262 case DRAWING_ELLIPSE:
263 nResourceId = STR_ObjNameSingulCIRCE;
264 break;
265 case DRAWING_GROUP:
266 nResourceId = STR_ObjNameSingulGRUP;
267 break;
268 case DRAWING_LINE:
269 nResourceId = STR_ObjNameSingulLINE;
270 break;
271 case DRAWING_MEASURE:
272 nResourceId = STR_ObjNameSingulMEASURE;
273 break;
274 case DRAWING_OPEN_BEZIER:
275 nResourceId = STR_ObjNameSingulPATHLINE;
276 break;
277 case DRAWING_OPEN_FREEHAND:
278 nResourceId = STR_ObjNameSingulFREELINE;
279 break;
280 case DRAWING_PAGE:
281 nResourceId = STR_ObjNameSingulPAGE;
282 break;
283 case DRAWING_POLY_LINE:
284 nResourceId = STR_ObjNameSingulPLIN;
285 break;
286 case DRAWING_POLY_LINE_PATH:
287 nResourceId = STR_ObjNameSingulPLIN;
288 break;
289 case DRAWING_POLY_POLYGON:
290 nResourceId = STR_ObjNameSingulPOLY;
291 break;
292 case DRAWING_POLY_POLYGON_PATH:
293 nResourceId = STR_ObjNameSingulPOLY;
294 break;
295 case DRAWING_RECTANGLE:
296 nResourceId = STR_ObjNameSingulRECT;
297 break;
298 case DRAWING_CUSTOM:
300 nResourceId = STR_ObjNameSingulCUSTOMSHAPE;
302 SvxShape* pShape = SvxShape::getImplementation( rxShape );
303 if (pShape)
305 SdrObject *pSdrObj = pShape->GetSdrObject();
306 if (pSdrObj)
308 if(pSdrObj->ISA(SdrObjCustomShape))
310 SdrObjCustomShape* pCustomShape = static_cast<SdrObjCustomShape*>(pSdrObj);
311 if(pCustomShape)
313 if (pCustomShape->IsTextPath())
314 nResourceId = STR_ObjNameSingulFONTWORK;
315 else
317 nResourceId = -1;
318 sName = pCustomShape->GetCustomShapeName();
324 break;
326 case DRAWING_TEXT:
327 nResourceId = STR_ObjNameSingulTEXT;
328 break;
329 default:
330 nResourceId = -1;
331 sName = "UnknownAccessibleShape";
332 uno::Reference<drawing::XShapeDescriptor> xDescriptor (rxShape, uno::UNO_QUERY);
333 if (xDescriptor.is())
334 sName += ": " + xDescriptor->getShapeType();
335 break;
338 if (nResourceId != -1)
340 SolarMutexGuard aGuard;
341 sName = OUString (SVX_RESSTR((unsigned short)nResourceId));
344 return sName;
347 } // end of namespace accessibility
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */