1 11 package org.eclipse.swt.accessibility; 12 13 14 import java.util.*; 15 import org.eclipse.swt.internal.*; 16 import org.eclipse.swt.internal.accessibility.gtk.*; 17 import org.eclipse.swt.internal.gtk.*; 18 import org.eclipse.swt.*; 19 20 class AccessibleFactory { 21 int handle; 22 int objectParentType; 23 int widgetTypeName; 24 Callback atkObjectFactoryCB_create_accessible; 25 Callback gTypeInfo_base_init_factory; 26 Hashtable accessibles = new Hashtable (9); 27 28 static final Hashtable Types = new Hashtable (9); 29 static final Hashtable Factories = new Hashtable (9); 30 static final int DefaultParentType = OS.g_type_from_name (Converter.wcsToMbcs (null, "GtkAccessible", true)); static final byte[] FACTORY_PARENTTYPENAME = Converter.wcsToMbcs (null, "AtkObjectFactory", true); static final byte[] SWT_TYPE_PREFIX = Converter.wcsToMbcs (null, "SWT", false); static final byte[] CHILD_TYPENAME = Converter.wcsToMbcs (null, "Child", false); static final byte[] FACTORY_TYPENAME = Converter.wcsToMbcs (null, "SWTFactory", true); static final int[] actionRoles = { 36 ACC.ROLE_CHECKBUTTON, ACC.ROLE_COMBOBOX, ACC.ROLE_LINK, 37 ACC.ROLE_MENUITEM, ACC.ROLE_PUSHBUTTON, ACC.ROLE_RADIOBUTTON, 38 }; 39 static final int[] hypertextRoles = {ACC.ROLE_LINK}; 40 static final int[] selectionRoles = { 41 ACC.ROLE_LIST, ACC.ROLE_TABFOLDER, ACC.ROLE_TABLE, ACC.ROLE_TREE, 42 }; 43 static final int[] textRoles = { 44 ACC.ROLE_COMBOBOX, ACC.ROLE_LINK, ACC.ROLE_LABEL, ACC.ROLE_TEXT, 45 }; 46 47 48 static final Callback AtkActionCB_get_keybinding; 49 static final Callback AtkActionCB_get_name; 50 static final Callback AtkComponentCB_get_extents; 51 static final Callback AtkComponentCB_get_position; 52 static final Callback AtkComponentCB_get_size; 53 static final Callback AtkComponentCB_ref_accessible_at_point; 54 static final Callback AtkHypertextCB_get_link; 55 static final Callback AtkHypertextCB_get_n_links; 56 static final Callback AtkHypertextCB_get_link_index; 57 static final Callback AtkObjectCB_get_description; 58 static final Callback AtkObjectCB_get_index_in_parent; 59 static final Callback AtkObjectCB_get_n_children; 60 static final Callback AtkObjectCB_get_name; 61 static final Callback AtkObjectCB_get_parent; 62 static final Callback AtkObjectCB_get_role; 63 static final Callback AtkObjectCB_ref_child; 64 static final Callback AtkObjectCB_ref_state_set; 65 static final Callback AtkSelectionCB_is_child_selected; 66 static final Callback AtkSelectionCB_ref_selection; 67 static final Callback AtkTextCB_get_caret_offset; 68 static final Callback AtkTextCB_get_n_selections; 69 static final Callback AtkTextCB_get_selection; 70 static final Callback AtkTextCB_get_text; 71 static final Callback AtkTextCB_get_text_after_offset; 72 static final Callback AtkTextCB_get_text_at_offset; 73 static final Callback AtkTextCB_get_text_before_offset; 74 static final Callback AtkTextCB_get_character_at_offset; 75 static final Callback AtkTextCB_get_character_count; 76 static final Callback GObjectClass_finalize; 77 78 static final Callback InitActionIfaceCB; 79 static final Callback InitComponentIfaceCB; 80 static final Callback InitHypertextIfaceCB; 81 static final Callback GTypeInfo_base_init_type; 82 static final Callback InitSelectionIfaceCB; 83 static final Callback InitTextIfaceCB; 84 85 static int ObjectIfaceDefinition; 86 static final int ActionIfaceDefinition; 87 static final int ComponentIfaceDefinition; 88 static final int HypertextIfaceDefinition; 89 static final int SelectionIfaceDefinition; 90 static final int TextIfaceDefinition; 91 static { 92 AtkActionCB_get_keybinding = newCallback (AccessibleObject.class, "atkAction_get_keybinding", 2); AtkActionCB_get_name = newCallback (AccessibleObject.class, "atkAction_get_name", 2); AtkComponentCB_get_extents = newCallback (AccessibleObject.class, "atkComponent_get_extents", 6); AtkComponentCB_get_position = newCallback (AccessibleObject.class, "atkComponent_get_position", 4); AtkComponentCB_get_size = newCallback (AccessibleObject.class, "atkComponent_get_size", 4); AtkComponentCB_ref_accessible_at_point = newCallback (AccessibleObject.class, "atkComponent_ref_accessible_at_point", 4); AtkHypertextCB_get_link = newCallback (AccessibleObject.class, "atkHypertext_get_link", 2); AtkHypertextCB_get_n_links = newCallback (AccessibleObject.class, "atkHypertext_get_n_links", 1); AtkHypertextCB_get_link_index = newCallback (AccessibleObject.class, "atkHypertext_get_link_index", 2); AtkObjectCB_get_name = newCallback (AccessibleObject.class, "atkObject_get_name", 1); AtkObjectCB_get_description = newCallback (AccessibleObject.class, "atkObject_get_description", 1); AtkObjectCB_get_n_children = newCallback (AccessibleObject.class, "atkObject_get_n_children", 1); AtkObjectCB_get_role = newCallback (AccessibleObject.class, "atkObject_get_role", 1); AtkObjectCB_get_parent = newCallback (AccessibleObject.class, "atkObject_get_parent", 1); AtkObjectCB_ref_state_set = newCallback (AccessibleObject.class, "atkObject_ref_state_set", 1); AtkObjectCB_get_index_in_parent = newCallback (AccessibleObject.class, "atkObject_get_index_in_parent", 1); AtkObjectCB_ref_child = newCallback (AccessibleObject.class, "atkObject_ref_child", 2); AtkSelectionCB_is_child_selected = newCallback (AccessibleObject.class, "atkSelection_is_child_selected", 2); AtkSelectionCB_ref_selection = newCallback (AccessibleObject.class, "atkSelection_ref_selection", 2); AtkTextCB_get_caret_offset = newCallback (AccessibleObject.class, "atkText_get_caret_offset", 1); AtkTextCB_get_n_selections = newCallback (AccessibleObject.class, "atkText_get_n_selections", 1); AtkTextCB_get_selection = newCallback (AccessibleObject.class, "atkText_get_selection", 4); AtkTextCB_get_text = newCallback (AccessibleObject.class, "atkText_get_text", 3); AtkTextCB_get_text_after_offset = newCallback (AccessibleObject.class, "atkText_get_text_after_offset", 5); AtkTextCB_get_text_at_offset = newCallback ( AccessibleObject.class, "atkText_get_text_at_offset", 5); AtkTextCB_get_text_before_offset = newCallback (AccessibleObject.class, "atkText_get_text_before_offset", 5); AtkTextCB_get_character_at_offset = newCallback (AccessibleObject.class, "atkText_get_character_at_offset", 2); AtkTextCB_get_character_count = newCallback (AccessibleObject.class, "atkText_get_character_count", 1); GObjectClass_finalize = newCallback (AccessibleObject.class, "gObjectClass_finalize", 1); GTypeInfo_base_init_type = newCallback (AccessibleFactory.class, "gTypeInfo_base_init_type", 1); 123 InitActionIfaceCB = newCallback (AccessibleFactory.class, "initActionIfaceCB", 1); GInterfaceInfo interfaceInfo = new GInterfaceInfo (); 125 interfaceInfo.interface_init = InitActionIfaceCB.getAddress (); 126 ActionIfaceDefinition = OS.g_malloc (GInterfaceInfo.sizeof); 127 OS.memmove (ActionIfaceDefinition, interfaceInfo, GInterfaceInfo.sizeof); 128 129 InitComponentIfaceCB = newCallback (AccessibleFactory.class, "initComponentIfaceCB", 1); interfaceInfo = new GInterfaceInfo (); 131 interfaceInfo.interface_init = InitComponentIfaceCB.getAddress (); 132 ComponentIfaceDefinition = OS.g_malloc (GInterfaceInfo.sizeof); 133 OS.memmove (ComponentIfaceDefinition, interfaceInfo, GInterfaceInfo.sizeof); 134 135 InitHypertextIfaceCB = newCallback (AccessibleFactory.class, "initHypertextIfaceCB", 1); interfaceInfo = new GInterfaceInfo (); 137 interfaceInfo.interface_init = InitHypertextIfaceCB.getAddress (); 138 HypertextIfaceDefinition = OS.g_malloc (GInterfaceInfo.sizeof); 139 OS.memmove (HypertextIfaceDefinition, interfaceInfo, GInterfaceInfo.sizeof); 140 141 InitSelectionIfaceCB = newCallback (AccessibleFactory.class, "initSelectionIfaceCB", 1); interfaceInfo = new GInterfaceInfo (); 143 interfaceInfo.interface_init = InitSelectionIfaceCB.getAddress (); 144 SelectionIfaceDefinition = OS.g_malloc (GInterfaceInfo.sizeof); 145 OS.memmove (SelectionIfaceDefinition, interfaceInfo, GInterfaceInfo.sizeof); 146 147 InitTextIfaceCB = newCallback (AccessibleFactory.class, "initTextIfaceCB", 1); interfaceInfo = new GInterfaceInfo (); 149 interfaceInfo.interface_init = InitTextIfaceCB.getAddress (); 150 TextIfaceDefinition = OS.g_malloc (GInterfaceInfo.sizeof); 151 OS.memmove (TextIfaceDefinition, interfaceInfo, GInterfaceInfo.sizeof); 152 } 153 154 static private Callback newCallback (Object object, String method, int argCount) { 155 Callback callback = new Callback (object, method, argCount); 156 if (callback.getAddress () == 0) SWT.error (SWT.ERROR_NO_MORE_CALLBACKS); 157 return callback; 158 } 159 160 private AccessibleFactory (int widgetType) { 161 super (); 162 163 if (DefaultParentType == 0) return; 164 widgetTypeName = OS.g_type_name (widgetType); 165 int widgetTypeNameLength = OS.strlen (widgetTypeName) + 1; 166 byte[] buffer = new byte [widgetTypeNameLength]; 167 OS.memmove (buffer, widgetTypeName, widgetTypeNameLength); 168 byte[] factoryName = new byte [FACTORY_TYPENAME.length + widgetTypeNameLength - 1]; 169 System.arraycopy (FACTORY_TYPENAME, 0, factoryName, 0, FACTORY_TYPENAME.length); 170 System.arraycopy (buffer, 0, factoryName, FACTORY_TYPENAME.length - 1, widgetTypeNameLength); 171 if (OS.g_type_from_name (factoryName) == 0) { 172 173 int registry = ATK.atk_get_default_registry (); 174 int previousFactory = ATK.atk_registry_get_factory (registry, widgetType); 175 objectParentType = ATK.atk_object_factory_get_accessible_type (previousFactory); 176 if (objectParentType == 0) objectParentType = DefaultParentType; 177 int factoryParentType = OS.g_type_from_name (FACTORY_PARENTTYPENAME); 178 gTypeInfo_base_init_factory = new Callback (this, "gTypeInfo_base_init_factory", 1); int address = gTypeInfo_base_init_factory.getAddress (); 180 if (address == 0) SWT.error (SWT.ERROR_NO_MORE_CALLBACKS); 181 GTypeInfo typeInfo = new GTypeInfo (); 182 typeInfo.base_init = address; 183 typeInfo.class_size = (short)ATK.AtkObjectFactoryClass_sizeof (); 184 typeInfo.instance_size = (short)ATK.AtkObjectFactory_sizeof (); 185 handle = OS.g_malloc (GTypeInfo.sizeof); 186 OS.memmove (handle, typeInfo, GTypeInfo.sizeof); 187 int swtFactory = OS.g_type_register_static (factoryParentType, factoryName, handle, 0); 188 ATK.atk_registry_set_factory_type (registry, widgetType, swtFactory); 189 } 190 } 191 192 void addAccessible (Accessible accessible) { 193 int controlHandle = accessible.getControlHandle (); 194 accessibles.put (new LONG (controlHandle), accessible); 195 } 196 197 int atkObjectFactory_create_accessible (int widget) { 198 Accessible accessible = (Accessible) accessibles.get (new LONG (widget)); 199 if (accessible == null) { 200 204 int result = OS.g_object_new (objectParentType, 0); 205 ATK.atk_object_initialize (result, widget); 206 return result; 207 } 208 int typeNameLength = OS.strlen (widgetTypeName); 209 byte[] buffer = new byte [typeNameLength]; 210 OS.memmove (buffer, widgetTypeName, typeNameLength); 211 int type = getType (buffer, accessible, objectParentType, ACC.CHILDID_SELF); 212 AccessibleObject object = new AccessibleObject (type, widget, accessible, objectParentType, false); 213 accessible.accessibleObject = object; 214 return object.handle; 215 } 216 217 static int getChildType (Accessible accessible, int childIndex) { 218 return getType (CHILD_TYPENAME, accessible, DefaultParentType, childIndex); 219 } 220 221 static int getDefaultParentType () { 222 return DefaultParentType; 223 } 224 225 static int getType (byte[] widgetTypeName, Accessible accessible, int parentType, int childId) { 226 AccessibleControlEvent event = new AccessibleControlEvent (accessible); 227 event.childID = childId; 228 AccessibleControlListener[] listeners = accessible.getControlListeners (); 229 for (int i = 0; i < listeners.length; i++) { 230 listeners [i].getRole (event); 231 } 232 boolean action = false, hypertext = false, selection = false, text = false; 233 if (event.detail != 0) { 234 for (int i = 0; i < actionRoles.length; i++) { 235 if (event.detail == actionRoles [i]) { 236 action = true; 237 break; 238 } 239 } 240 for (int i = 0; i < hypertextRoles.length; i++) { 241 if (event.detail == hypertextRoles [i]) { 242 hypertext = true; 243 break; 244 } 245 } 246 for (int i = 0; i < selectionRoles.length; i++) { 247 if (event.detail == selectionRoles [i]) { 248 selection = true; 249 break; 250 } 251 } 252 for (int i = 0; i < textRoles.length; i++) { 253 if (event.detail == textRoles [i]) { 254 text = true; 255 break; 256 } 257 } 258 } else { 259 action = hypertext = selection = text = true; 260 } 261 String swtTypeName = new String (SWT_TYPE_PREFIX); 262 swtTypeName += new String (widgetTypeName); 263 if (action) swtTypeName += "Action"; if (hypertext) swtTypeName += "Hypertext"; if (selection) swtTypeName += "Selection"; if (text) swtTypeName += "Text"; 268 int type = 0; 269 LONG typeInt = (LONG)Types.get (swtTypeName); 270 if (typeInt != null) { 271 type = typeInt.value; 272 } else { 273 274 int queryPtr = OS.g_malloc (GTypeQuery.sizeof); 275 OS.g_type_query (parentType, queryPtr); 276 GTypeQuery query = new GTypeQuery (); 277 OS.memmove (query, queryPtr, GTypeQuery.sizeof); 278 OS.g_free (queryPtr); 279 GTypeInfo typeInfo = new GTypeInfo (); 280 typeInfo.base_init = GTypeInfo_base_init_type.getAddress (); 281 typeInfo.class_size = (short) query.class_size; 282 typeInfo.instance_size = (short) query.instance_size; 283 ObjectIfaceDefinition = OS.g_malloc (GTypeInfo.sizeof); 284 OS.memmove (ObjectIfaceDefinition, typeInfo, GTypeInfo.sizeof); 285 byte[] nameBytes = new byte [swtTypeName.length () + 1]; 286 System.arraycopy(swtTypeName.getBytes (), 0, nameBytes, 0, swtTypeName.length ()); 287 type = OS.g_type_register_static (parentType, nameBytes, ObjectIfaceDefinition, 0); 288 OS.g_type_add_interface_static (type, AccessibleObject.ATK_COMPONENT_TYPE, ComponentIfaceDefinition); 289 if (action) OS.g_type_add_interface_static (type, AccessibleObject.ATK_ACTION_TYPE, ActionIfaceDefinition); 290 if (hypertext) OS.g_type_add_interface_static (type, AccessibleObject.ATK_HYPERTEXT_TYPE, HypertextIfaceDefinition); 291 if (selection) OS.g_type_add_interface_static (type, AccessibleObject.ATK_SELECTION_TYPE, SelectionIfaceDefinition); 292 if (text) OS.g_type_add_interface_static (type, AccessibleObject.ATK_TEXT_TYPE, TextIfaceDefinition); 293 Types.put (swtTypeName, new LONG (type)); 294 } 295 return type; 296 } 297 298 int gTypeInfo_base_init_factory (int klass) { 299 int atkObjectFactoryClass = ATK.ATK_OBJECT_FACTORY_CLASS (klass); 300 AtkObjectFactoryClass objectFactoryClassStruct = new AtkObjectFactoryClass (); 301 ATK.memmove (objectFactoryClassStruct, atkObjectFactoryClass); 302 atkObjectFactoryCB_create_accessible = new Callback (this, "atkObjectFactory_create_accessible", 1); int address = atkObjectFactoryCB_create_accessible.getAddress (); 304 if (address == 0) SWT.error (SWT.ERROR_NO_MORE_CALLBACKS); 305 objectFactoryClassStruct.create_accessible = address; 306 ATK.memmove (atkObjectFactoryClass, objectFactoryClassStruct); 307 return 0; 308 } 309 310 static int gTypeInfo_base_init_type (int klass) { 311 AtkObjectClass objectClass = new AtkObjectClass (); 312 ATK.memmove (objectClass, klass); 313 objectClass.get_name = AtkObjectCB_get_name.getAddress (); 314 objectClass.get_description = AtkObjectCB_get_description.getAddress (); 315 objectClass.get_n_children = AtkObjectCB_get_n_children.getAddress (); 316 objectClass.get_role = AtkObjectCB_get_role.getAddress (); 317 objectClass.get_parent = AtkObjectCB_get_parent.getAddress (); 318 objectClass.ref_state_set = AtkObjectCB_ref_state_set.getAddress (); 319 objectClass.get_index_in_parent = AtkObjectCB_get_index_in_parent.getAddress (); 320 objectClass.ref_child = AtkObjectCB_ref_child.getAddress (); 321 int gObjectClass = OS.G_OBJECT_CLASS (klass); 322 GObjectClass objectClassStruct = new GObjectClass (); 323 OS.memmove (objectClassStruct, gObjectClass); 324 objectClassStruct.finalize = GObjectClass_finalize.getAddress (); 325 OS.memmove (gObjectClass, objectClassStruct); 326 ATK.memmove (klass, objectClass); 327 return 0; 328 } 329 330 static int initActionIfaceCB (int iface) { 331 AtkActionIface actionIface = new AtkActionIface (); 332 ATK.memmove (actionIface, iface); 333 actionIface.get_keybinding = AtkActionCB_get_keybinding.getAddress (); 334 actionIface.get_name = AtkActionCB_get_name.getAddress (); 335 ATK.memmove (iface, actionIface); 336 return 0; 337 } 338 339 static int initComponentIfaceCB (int iface) { 340 AtkComponentIface componentIface = new AtkComponentIface (); 341 ATK.memmove (componentIface, iface); 342 componentIface.get_extents = AtkComponentCB_get_extents.getAddress (); 343 componentIface.get_position = AtkComponentCB_get_position.getAddress (); 344 componentIface.get_size = AtkComponentCB_get_size.getAddress (); 345 componentIface.ref_accessible_at_point = AtkComponentCB_ref_accessible_at_point.getAddress (); 346 ATK.memmove (iface, componentIface); 347 return 0; 348 } 349 350 static int initHypertextIfaceCB (int iface) { 351 AtkHypertextIface hypertextIface = new AtkHypertextIface (); 352 ATK.memmove (hypertextIface, iface); 353 hypertextIface.get_link = AtkHypertextCB_get_link.getAddress (); 354 hypertextIface.get_link_index = AtkHypertextCB_get_link_index.getAddress (); 355 hypertextIface.get_n_links = AtkHypertextCB_get_n_links.getAddress (); 356 ATK.memmove (iface, hypertextIface); 357 return 0; 358 } 359 360 static int initSelectionIfaceCB (int iface) { 361 AtkSelectionIface selectionIface = new AtkSelectionIface (); 362 ATK.memmove (selectionIface, iface); 363 selectionIface.is_child_selected = AtkSelectionCB_is_child_selected.getAddress (); 364 selectionIface.ref_selection = AtkSelectionCB_ref_selection.getAddress (); 365 ATK.memmove (iface, selectionIface); 366 return 0; 367 } 368 369 static int initTextIfaceCB (int iface) { 370 AtkTextIface textInterface = new AtkTextIface (); 371 ATK.memmove (textInterface, iface); 372 textInterface.get_caret_offset = AtkTextCB_get_caret_offset.getAddress (); 373 textInterface.get_character_at_offset = AtkTextCB_get_character_at_offset.getAddress (); 374 textInterface.get_character_count = AtkTextCB_get_character_count.getAddress (); 375 textInterface.get_n_selections = AtkTextCB_get_n_selections.getAddress (); 376 textInterface.get_selection = AtkTextCB_get_selection.getAddress (); 377 textInterface.get_text = AtkTextCB_get_text.getAddress (); 378 textInterface.get_text_after_offset = AtkTextCB_get_text_after_offset.getAddress (); 379 textInterface.get_text_at_offset = AtkTextCB_get_text_at_offset.getAddress (); 380 textInterface.get_text_before_offset = AtkTextCB_get_text_before_offset.getAddress (); 381 ATK.memmove (iface, textInterface); 382 return 0; 383 } 384 385 static void registerAccessible (Accessible accessible) { 386 int controlHandle = accessible.getControlHandle (); 387 int widgetType = OS.G_OBJECT_TYPE (controlHandle); 388 AccessibleFactory factory = (AccessibleFactory) Factories.get (new LONG (widgetType)); 389 if (factory == null) { 390 factory = new AccessibleFactory (widgetType); 391 Factories.put (new LONG (widgetType), factory); 392 } 393 factory.addAccessible (accessible); 394 } 395 396 void removeAccessible (Accessible accessible) { 397 accessibles.remove (new LONG (accessible.getControlHandle ())); 398 } 399 400 static void unregisterAccessible (Accessible accessible) { 401 int controlHandle = accessible.getControlHandle (); 402 int widgetType = OS.G_OBJECT_TYPE (controlHandle); 403 AccessibleFactory factory = (AccessibleFactory) Factories.get (new LONG (widgetType)); 404 if (factory != null) { 405 factory.removeAccessible (accessible); 406 } 407 } 408 } 409 | Popular Tags |