Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / accessibility / ShapeTypeHandler.cxx
blobdc3fb56ecda2cd5b2cd6338ba975ee410e3b36d3
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 ::rtl;
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::accessibility;
39 namespace accessibility {
41 // Pointer to the shape type handler singleton.
42 ShapeTypeHandler* ShapeTypeHandler::instance = NULL;
45 // Create an empty reference to an accessible object.
46 AccessibleShape*
47 CreateEmptyShapeReference (
48 const AccessibleShapeInfo& /*rShapeInfo*/,
49 const AccessibleShapeTreeInfo& /*rShapeTreeInfo*/,
50 ShapeTypeId /*nId*/)
52 return NULL;
58 ShapeTypeHandler& ShapeTypeHandler::Instance (void)
60 // Using double check pattern to make sure that exactly one instance of
61 // the shape type handler is instantiated.
62 if (instance == NULL)
64 SolarMutexGuard aGuard;
65 if (instance == NULL)
67 // Create the single instance of the shape type handler.
68 instance = new ShapeTypeHandler;
70 // Register the basic SVX shape types.
71 RegisterDrawShapeTypes ();
75 return *instance;
81 /** The given service name is first transformed into a slot id that
82 identifies the place of the type descriptor. From that descriptor the
83 shape type id is returned.
85 ShapeTypeId ShapeTypeHandler::GetTypeId (const OUString& aServiceName) const
87 tServiceNameToSlotId::iterator I (maServiceNameToSlotId.find (aServiceName));
88 if (I != maServiceNameToSlotId.end())
90 return maShapeTypeDescriptorList[I->second].mnShapeTypeId;
92 else
93 return -1;
98 /** Extract the specified shape's service name and forward the request to
99 the appropriate method.
101 ShapeTypeId ShapeTypeHandler::GetTypeId (const uno::Reference<drawing::XShape>& rxShape) const
103 uno::Reference<drawing::XShapeDescriptor> xDescriptor (rxShape, uno::UNO_QUERY);
104 if (xDescriptor.is())
105 return GetTypeId (xDescriptor->getShapeType());
106 else
107 return -1;
113 /** This factory method determines the type descriptor for the type of the
114 given shape, then calls the descriptor's create function, and finally
115 initializes the new object.
117 AccessibleShape*
118 ShapeTypeHandler::CreateAccessibleObject (
119 const AccessibleShapeInfo& rShapeInfo,
120 const AccessibleShapeTreeInfo& rShapeTreeInfo) const
122 ShapeTypeId nSlotId (GetSlotId (rShapeInfo.mxShape));
123 AccessibleShape* pShape =
124 maShapeTypeDescriptorList[nSlotId].maCreateFunction (
125 rShapeInfo,
126 rShapeTreeInfo,
127 maShapeTypeDescriptorList[nSlotId].mnShapeTypeId);
128 return pShape;
134 /** Create the single instance of this class and initialize its list of
135 type descriptors with an entry of an unknown type.
137 ShapeTypeHandler::ShapeTypeHandler (void)
138 : maShapeTypeDescriptorList (1)
140 // Make sure that at least the UNKNOWN entry is present.
141 // Resize the list, if necessary, so that the new type can be inserted.
142 maShapeTypeDescriptorList[0].mnShapeTypeId = UNKNOWN_SHAPE_TYPE;
143 maShapeTypeDescriptorList[0].msServiceName = "UNKNOWN_SHAPE_TYPE";
144 maShapeTypeDescriptorList[0].maCreateFunction = CreateEmptyShapeReference;
145 maServiceNameToSlotId[maShapeTypeDescriptorList[0].msServiceName] = 0;
151 ShapeTypeHandler::~ShapeTypeHandler (void)
153 // Because this class is a singleton and the only instance, whose
154 // destructor has just been called, is pointed to from instance,
155 // we reset the static variable instance, so that further calls to
156 // getInstance do not return an undefined object but create a new
157 // singleton.
158 instance = NULL;
164 bool ShapeTypeHandler::AddShapeTypeList (int nDescriptorCount,
165 ShapeTypeDescriptor aDescriptorList[])
167 SolarMutexGuard aGuard;
169 // Determine first id of new type descriptor(s).
170 int nFirstId = maShapeTypeDescriptorList.size();
172 // Resize the list, if necessary, so that the types can be inserted.
173 maShapeTypeDescriptorList.resize (nFirstId + nDescriptorCount);
175 for (int i=0; i<nDescriptorCount; i++)
177 #if OSL_DEBUG_LEVEL > 0
178 ShapeTypeId nId (aDescriptorList[i].mnShapeTypeId);
179 (void)nId;
180 #endif
182 // Fill Type descriptor.
183 maShapeTypeDescriptorList[nFirstId+i].mnShapeTypeId = aDescriptorList[i].mnShapeTypeId;
184 maShapeTypeDescriptorList[nFirstId+i].msServiceName = aDescriptorList[i].msServiceName;
185 maShapeTypeDescriptorList[nFirstId+i].maCreateFunction = aDescriptorList[i].maCreateFunction;
187 // Update inverse mapping from service name to the descriptor's position.
188 maServiceNameToSlotId[aDescriptorList[i].msServiceName] = nFirstId+i;
191 return true;
197 long ShapeTypeHandler::GetSlotId (const OUString& aServiceName) const
199 tServiceNameToSlotId::iterator I (maServiceNameToSlotId.find (aServiceName));
200 if (I != maServiceNameToSlotId.end())
201 return I->second;
202 else
203 return 0;
209 // Extract the given shape's service name and forward request to appropriate
210 // method.
211 long ShapeTypeHandler::GetSlotId (const uno::Reference<drawing::XShape>& rxShape) const
213 uno::Reference<drawing::XShapeDescriptor> xDescriptor (rxShape, uno::UNO_QUERY);
214 if (xDescriptor.is())
215 return GetSlotId (xDescriptor->getShapeType());
216 else
217 return 0;
220 /// get the accessible base name for an object
221 OUString
222 ShapeTypeHandler::CreateAccessibleBaseName (const uno::Reference<drawing::XShape>& rxShape)
223 throw (::com::sun::star::uno::RuntimeException)
225 sal_Int32 nResourceId;
226 OUString sName;
228 switch (ShapeTypeHandler::Instance().GetTypeId (rxShape))
230 // case DRAWING_3D_POLYGON: was removed in original code in
231 // AccessibleShape::CreateAccessibleBaseName. See issue 11190 for details.
232 // Id can be removed from SvxShapeTypes.hxx as well.
233 case DRAWING_3D_CUBE:
234 nResourceId = STR_ObjNameSingulCube3d;
235 break;
236 case DRAWING_3D_EXTRUDE:
237 nResourceId = STR_ObjNameSingulExtrude3d;
238 break;
239 case DRAWING_3D_LATHE:
240 nResourceId = STR_ObjNameSingulLathe3d;
241 break;
242 case DRAWING_3D_SCENE:
243 nResourceId = STR_ObjNameSingulScene3d;
244 break;
245 case DRAWING_3D_SPHERE:
246 nResourceId = STR_ObjNameSingulSphere3d;
247 break;
248 case DRAWING_CAPTION:
249 nResourceId = STR_ObjNameSingulCAPTION;
250 break;
251 case DRAWING_CLOSED_BEZIER:
252 nResourceId = STR_ObjNameSingulPATHFILL;
253 break;
254 case DRAWING_CLOSED_FREEHAND:
255 nResourceId = STR_ObjNameSingulFREEFILL;
256 break;
257 case DRAWING_CONNECTOR:
258 nResourceId = STR_ObjNameSingulEDGE;
259 break;
260 case DRAWING_CONTROL:
261 nResourceId = STR_ObjNameSingulUno;
262 break;
263 case DRAWING_ELLIPSE:
264 nResourceId = STR_ObjNameSingulCIRCE;
265 break;
266 case DRAWING_GROUP:
267 nResourceId = STR_ObjNameSingulGRUP;
268 break;
269 case DRAWING_LINE:
270 nResourceId = STR_ObjNameSingulLINE;
271 break;
272 case DRAWING_MEASURE:
273 nResourceId = STR_ObjNameSingulMEASURE;
274 break;
275 case DRAWING_OPEN_BEZIER:
276 nResourceId = STR_ObjNameSingulPATHLINE;
277 break;
278 case DRAWING_OPEN_FREEHAND:
279 nResourceId = STR_ObjNameSingulFREELINE;
280 break;
281 case DRAWING_PAGE:
282 nResourceId = STR_ObjNameSingulPAGE;
283 break;
284 case DRAWING_POLY_LINE:
285 nResourceId = STR_ObjNameSingulPLIN;
286 break;
287 case DRAWING_POLY_LINE_PATH:
288 nResourceId = STR_ObjNameSingulPLIN;
289 break;
290 case DRAWING_POLY_POLYGON:
291 nResourceId = STR_ObjNameSingulPOLY;
292 break;
293 case DRAWING_POLY_POLYGON_PATH:
294 nResourceId = STR_ObjNameSingulPOLY;
295 break;
296 case DRAWING_RECTANGLE:
297 nResourceId = STR_ObjNameSingulRECT;
298 break;
299 case DRAWING_CUSTOM:
301 nResourceId = STR_ObjNameSingulCUSTOMSHAPE;
303 SvxShape* pShape = SvxShape::getImplementation( rxShape );
304 if (pShape)
306 SdrObject *pSdrObj = pShape->GetSdrObject();
307 if (pSdrObj)
309 if(pSdrObj->ISA(SdrObjCustomShape))
311 SdrObjCustomShape* pCustomShape = (SdrObjCustomShape*)pSdrObj;
312 if(pCustomShape)
314 if (pCustomShape->IsTextPath())
315 nResourceId = STR_ObjNameSingulFONTWORK;
316 else
318 nResourceId = -1;
319 sName = pCustomShape->GetCustomShapeName();
325 break;
327 case DRAWING_TEXT:
328 nResourceId = STR_ObjNameSingulTEXT;
329 break;
330 default:
331 nResourceId = -1;
332 sName = "UnknownAccessibleShape";
333 uno::Reference<drawing::XShapeDescriptor> xDescriptor (rxShape, uno::UNO_QUERY);
334 if (xDescriptor.is())
335 sName += ": " + xDescriptor->getShapeType();
336 break;
339 if (nResourceId != -1)
341 SolarMutexGuard aGuard;
342 sName = OUString (SVX_RESSTR((unsigned short)nResourceId));
345 return sName;
348 } // end of namespace accessibility
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */