source: trunk/src/gui/accessible/qaccessible_mac.mm@ 463

Last change on this file since 463 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 93.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qaccessible.h"
43
44#ifndef QT_NO_ACCESSIBILITY
45#include "qaccessible_mac_p.h"
46#include "qhash.h"
47#include "qset.h"
48#include "qpointer.h"
49#include "qapplication.h"
50#include "qmainwindow.h"
51#include "qtextdocument.h"
52#include "qdebug.h"
53#include "qabstractslider.h"
54#include "qsplitter.h"
55#include "qtabwidget.h"
56#include "qlistview.h"
57#include "qtableview.h"
58#include "qdockwidget.h"
59
60#include <private/qt_mac_p.h>
61#include <private/qwidget_p.h>
62#include <CoreFoundation/CoreFoundation.h>
63
64QT_BEGIN_NAMESPACE
65
66/*
67 Set up platform defines. There is a one-to-one correspondence between the
68 Carbon and Cocoa roles and attributes, but the prefix and type changes.
69*/
70#ifdef QT_MAC_USE_COCOA
71typedef NSString * const QAXRoleType;
72#define QAXApplicationRole NSAccessibilityApplicationRole
73#define QAXButtonRole NSAccessibilityButtonRole
74#define QAXCancelAction NSAccessibilityCancelAction
75#define QAXCheckBoxRole NSAccessibilityCheckBoxRole
76#define QAXChildrenAttribute NSAccessibilityChildrenAttribute
77#define QAXCloseButtonAttribute NSAccessibilityCloseButtonAttribute
78#define QAXCloseButtonAttribute NSAccessibilityCloseButtonAttribute
79#define QAXColumnRole NSAccessibilityColumnRole
80#define QAXConfirmAction NSAccessibilityConfirmAction
81#define QAXContentsAttribute NSAccessibilityContentsAttribute
82#define QAXDecrementAction NSAccessibilityDecrementAction
83#define QAXDecrementArrowSubrole NSAccessibilityDecrementArrowSubrole
84#define QAXDecrementPageSubrole NSAccessibilityDecrementPageSubrole
85#define QAXDescriptionAttribute NSAccessibilityDescriptionAttribute
86#define QAXEnabledAttribute NSAccessibilityEnabledAttribute
87#define QAXExpandedAttribute NSAccessibilityExpandedAttribute
88#define QAXFocusedAttribute NSAccessibilityFocusedAttribute
89#define QAXFocusedUIElementChangedNotification NSAccessibilityFocusedUIElementChangedNotification
90#define QAXFocusedWindowChangedNotification NSAccessibilityFocusedWindowChangedNotification
91#define QAXGroupRole NSAccessibilityGroupRole
92#define QAXGrowAreaAttribute NSAccessibilityGrowAreaAttribute
93#define QAXGrowAreaRole NSAccessibilityGrowAreaRole
94#define QAXHelpAttribute NSAccessibilityHelpAttribute
95#define QAXHorizontalOrientationValue NSAccessibilityHorizontalOrientationValue
96#define QAXHorizontalScrollBarAttribute NSAccessibilityHorizontalScrollBarAttribute
97#define QAXIncrementAction NSAccessibilityIncrementAction
98#define QAXIncrementArrowSubrole NSAccessibilityIncrementArrowSubrole
99#define QAXIncrementPageSubrole NSAccessibilityIncrementPageSubrole
100#define QAXIncrementorRole NSAccessibilityIncrementorRole
101#define QAXLinkedUIElementsAttribute NSAccessibilityLinkedUIElementsAttribute
102#define QAXListRole NSAccessibilityListRole
103#define QAXMainAttribute NSAccessibilityMainAttribute
104#define QAXMaxValueAttribute NSAccessibilityMaxValueAttribute
105#define QAXMenuBarRole NSAccessibilityMenuBarRole
106#define QAXMenuButtonRole NSAccessibilityMenuButtonRole
107#define QAXMenuClosedNotification NSAccessibilityMenuClosedNotification
108#define QAXMenuItemRole NSAccessibilityMenuItemRole
109#define QAXMenuOpenedNotification NSAccessibilityMenuOpenedNotification
110#define QAXMenuRole NSAccessibilityMenuRole
111#define QAXMinValueAttribute NSAccessibilityMinValueAttribute
112#define QAXMinimizeButtonAttribute NSAccessibilityMinimizeButtonAttribute
113#define QAXMinimizedAttribute NSAccessibilityMinimizedAttribute
114#define QAXNextContentsAttribute NSAccessibilityNextContentsAttribute
115#define QAXOrientationAttribute NSAccessibilityOrientationAttribute
116#define QAXParentAttribute NSAccessibilityParentAttribute
117#define QAXPickAction NSAccessibilityPickAction
118#define QAXPopUpButtonRole NSAccessibilityPopUpButtonRole
119#define QAXPositionAttribute NSAccessibilityPositionAttribute
120#define QAXPressAction NSAccessibilityPressAction
121#define QAXPreviousContentsAttribute NSAccessibilityPreviousContentsAttribute
122#define QAXProgressIndicatorRole NSAccessibilityProgressIndicatorRole
123#define QAXRadioButtonRole NSAccessibilityRadioButtonRole
124#define QAXRoleAttribute NSAccessibilityRoleAttribute
125#define QAXRoleDescriptionAttribute NSAccessibilityRoleDescriptionAttribute
126#define QAXRowRole NSAccessibilityRowRole
127#define QAXRowsAttribute NSAccessibilityRowsAttribute
128#define QAXScrollAreaRole NSAccessibilityScrollAreaRole
129#define QAXScrollBarRole NSAccessibilityScrollBarRole
130#define QAXSelectedAttribute NSAccessibilitySelectedAttribute
131#define QAXSelectedChildrenAttribute NSAccessibilitySelectedChildrenAttribute
132#define QAXSelectedRowsAttribute NSAccessibilitySelectedRowsAttribute
133#define QAXSizeAttribute NSAccessibilitySizeAttribute
134#define QAXSliderRole NSAccessibilitySliderRole
135#define QAXSplitGroupRole NSAccessibilitySplitGroupRole
136#define QAXSplitterRole NSAccessibilitySplitterRole
137#define QAXSplittersAttribute NSAccessibilitySplittersAttribute
138#define QAXStaticTextRole NSAccessibilityStaticTextRole
139#define QAXSubroleAttribute NSAccessibilitySubroleAttribute
140#define QAXSubroleAttribute NSAccessibilitySubroleAttribute
141#define QAXTabGroupRole NSAccessibilityTabGroupRole
142#define QAXTableRole NSAccessibilityTableRole
143#define QAXTabsAttribute NSAccessibilityTabsAttribute
144#define QAXTextFieldRole NSAccessibilityTextFieldRole
145#define QAXTitleAttribute NSAccessibilityTitleAttribute
146#define QAXTitleUIElementAttribute NSAccessibilityTitleUIElementAttribute
147#define QAXToolbarButtonAttribute NSAccessibilityToolbarButtonAttribute
148#define QAXToolbarRole NSAccessibilityToolbarRole
149#define QAXTopLevelUIElementAttribute NSAccessibilityTopLevelUIElementAttribute
150#define QAXUnknownRole NSAccessibilityUnknownRole
151#define QAXValueAttribute NSAccessibilityValueAttribute
152#define QAXValueChangedNotification NSAccessibilityValueChangedNotification
153#define QAXValueIndicatorRole NSAccessibilityValueIndicatorRole
154#define QAXVerticalOrientationValue NSAccessibilityVerticalOrientationValue
155#define QAXVerticalScrollBarAttribute NSAccessibilityVerticalScrollBarAttribute
156#define QAXVisibleRowsAttribute NSAccessibilityVisibleRowsAttribute
157#define QAXWindowAttribute NSAccessibilityWindowAttribute
158#define QAXWindowCreatedNotification NSAccessibilityWindowCreatedNotification
159#define QAXWindowMovedNotification NSAccessibilityWindowMovedNotification
160#define QAXWindowRole NSAccessibilityWindowRole
161#define QAXZoomButtonAttribute NSAccessibilityZoomButtonAttribute
162#else
163typedef CFStringRef const QAXRoleType;
164#define QAXApplicationRole kAXApplicationRole
165#define QAXButtonRole kAXButtonRole
166#define QAXCancelAction kAXCancelAction
167#define QAXCheckBoxRole kAXCheckBoxRole
168#define QAXChildrenAttribute kAXChildrenAttribute
169#define QAXCloseButtonAttribute kAXCloseButtonAttribute
170#define QAXColumnRole kAXColumnRole
171#define QAXConfirmAction kAXConfirmAction
172#define QAXContentsAttribute kAXContentsAttribute
173#define QAXDecrementAction kAXDecrementAction
174#define QAXDecrementArrowSubrole kAXDecrementArrowSubrole
175#define QAXDecrementPageSubrole kAXDecrementPageSubrole
176#define QAXDescriptionAttribute kAXDescriptionAttribute
177#define QAXEnabledAttribute kAXEnabledAttribute
178#define QAXExpandedAttribute kAXExpandedAttribute
179#define QAXFocusedAttribute kAXFocusedAttribute
180#define QAXFocusedUIElementChangedNotification kAXFocusedUIElementChangedNotification
181#define QAXFocusedWindowChangedNotification kAXFocusedWindowChangedNotification
182#define QAXGroupRole kAXGroupRole
183#define QAXGrowAreaAttribute kAXGrowAreaAttribute
184#define QAXGrowAreaRole kAXGrowAreaRole
185#define QAXHelpAttribute kAXHelpAttribute
186#define QAXHorizontalOrientationValue kAXHorizontalOrientationValue
187#define QAXHorizontalScrollBarAttribute kAXHorizontalScrollBarAttribute
188#define QAXIncrementAction kAXIncrementAction
189#define QAXIncrementArrowSubrole kAXIncrementArrowSubrole
190#define QAXIncrementPageSubrole kAXIncrementPageSubrole
191#define QAXIncrementorRole kAXIncrementorRole
192#define QAXLinkedUIElementsAttribute kAXLinkedUIElementsAttribute
193#define QAXListRole kAXListRole
194#define QAXMainAttribute kAXMainAttribute
195#define QAXMaxValueAttribute kAXMaxValueAttribute
196#define QAXMenuBarRole kAXMenuBarRole
197#define QAXMenuButtonRole kAXMenuButtonRole
198#define QAXMenuClosedNotification kAXMenuClosedNotification
199#define QAXMenuItemRole kAXMenuItemRole
200#define QAXMenuOpenedNotification kAXMenuOpenedNotification
201#define QAXMenuRole kAXMenuRole
202#define QAXMinValueAttribute kAXMinValueAttribute
203#define QAXMinimizeButtonAttribute kAXMinimizeButtonAttribute
204#define QAXMinimizedAttribute kAXMinimizedAttribute
205#define QAXNextContentsAttribute kAXNextContentsAttribute
206#define QAXOrientationAttribute kAXOrientationAttribute
207#define QAXParentAttribute kAXParentAttribute
208#define QAXPickAction kAXPickAction
209#define QAXPopUpButtonRole kAXPopUpButtonRole
210#define QAXPositionAttribute kAXPositionAttribute
211#define QAXPressAction kAXPressAction
212#define QAXPreviousContentsAttribute kAXPreviousContentsAttribute
213#define QAXProgressIndicatorRole kAXProgressIndicatorRole
214#define QAXRadioButtonRole kAXRadioButtonRole
215#define QAXRoleAttribute kAXRoleAttribute
216#define QAXRoleDescriptionAttribute kAXRoleDescriptionAttribute
217#define QAXRowRole kAXRowRole
218#define QAXRowsAttribute kAXRowsAttribute
219#define QAXScrollAreaRole kAXScrollAreaRole
220#define QAXScrollBarRole kAXScrollBarRole
221#define QAXSelectedAttribute kAXSelectedAttribute
222#define QAXSelectedChildrenAttribute kAXSelectedChildrenAttribute
223#define QAXSelectedRowsAttribute kAXSelectedRowsAttribute
224#define QAXSizeAttribute kAXSizeAttribute
225#define QAXSliderRole kAXSliderRole
226#define QAXSplitGroupRole kAXSplitGroupRole
227#define QAXSplitterRole kAXSplitterRole
228#define QAXSplittersAttribute kAXSplittersAttribute
229#define QAXStaticTextRole kAXStaticTextRole
230#define QAXSubroleAttribute kAXSubroleAttribute
231#define QAXTabGroupRole kAXTabGroupRole
232#define QAXTableRole kAXTableRole
233#define QAXTabsAttribute kAXTabsAttribute
234#define QAXTextFieldRole kAXTextFieldRole
235#define QAXTitleAttribute kAXTitleAttribute
236#define QAXTitleUIElementAttribute kAXTitleUIElementAttribute
237#define QAXToolbarButtonAttribute kAXToolbarButtonAttribute
238#define QAXToolbarRole kAXToolbarRole
239#define QAXTopLevelUIElementAttribute kAXTopLevelUIElementAttribute
240#define QAXUnknownRole kAXUnknownRole
241#define QAXValueAttribute kAXValueAttribute
242#define QAXValueChangedNotification kAXValueChangedNotification
243#define QAXValueIndicatorRole kAXValueIndicatorRole
244#define QAXVerticalOrientationValue kAXVerticalOrientationValue
245#define QAXVerticalScrollBarAttribute kAXVerticalScrollBarAttribute
246#define QAXVisibleRowsAttribute kAXVisibleRowsAttribute
247#define QAXWindowAttribute kAXWindowAttribute
248#define QAXWindowCreatedNotification kAXWindowCreatedNotification
249#define QAXWindowMovedNotification kAXWindowMovedNotification
250#define QAXWindowRole kAXWindowRole
251#define QAXZoomButtonAttribute kAXZoomButtonAttribute
252#endif
253
254
255/*****************************************************************************
256 Externals
257 *****************************************************************************/
258extern bool qt_mac_is_macsheet(const QWidget *w); //qwidget_mac.cpp
259extern bool qt_mac_is_macdrawer(const QWidget *w); //qwidget_mac.cpp
260
261/*****************************************************************************
262 QAccessible Bindings
263 *****************************************************************************/
264//hardcoded bindings between control info and (known) QWidgets
265struct QAccessibleTextBinding {
266 int qt;
267 QAXRoleType mac;
268 bool settable;
269} text_bindings[][10] = {
270 { { QAccessible::MenuItem, QAXMenuItemRole, false },
271 { -1, 0, false }
272 },
273 { { QAccessible::MenuBar, QAXMenuBarRole, false },
274 { -1, 0, false }
275 },
276 { { QAccessible::ScrollBar, QAXScrollBarRole, false },
277 { -1, 0, false }
278 },
279 { { QAccessible::Grip, QAXGrowAreaRole, false },
280 { -1, 0, false }
281 },
282 { { QAccessible::Window, QAXWindowRole, false },
283 { -1, 0, false }
284 },
285 { { QAccessible::Dialog, QAXWindowRole, false },
286 { -1, 0, false }
287 },
288 { { QAccessible::AlertMessage, QAXWindowRole, false },
289 { -1, 0, false }
290 },
291 { { QAccessible::ToolTip, QAXWindowRole, false },
292 { -1, 0, false }
293 },
294 { { QAccessible::HelpBalloon, QAXWindowRole, false },
295 { -1, 0, false }
296 },
297 { { QAccessible::PopupMenu, QAXMenuRole, false },
298 { -1, 0, false }
299 },
300 { { QAccessible::Application, QAXApplicationRole, false },
301 { -1, 0, false }
302 },
303 { { QAccessible::Pane, QAXGroupRole, false },
304 { -1, 0, false }
305 },
306 { { QAccessible::Grouping, QAXGroupRole, false },
307 { -1, 0, false }
308 },
309 { { QAccessible::Separator, QAXSplitterRole, false },
310 { -1, 0, false }
311 },
312 { { QAccessible::ToolBar, QAXToolbarRole, false },
313 { -1, 0, false }
314 },
315 { { QAccessible::PageTab, QAXRadioButtonRole, false },
316 { -1, 0, false }
317 },
318 { { QAccessible::ButtonMenu, QAXMenuButtonRole, false },
319 { -1, 0, false }
320 },
321 { { QAccessible::ButtonDropDown, QAXPopUpButtonRole, false },
322 { -1, 0, false }
323 },
324 { { QAccessible::SpinBox, QAXIncrementorRole, false },
325 { -1, 0, false }
326 },
327 { { QAccessible::Slider, QAXSliderRole, false },
328 { -1, 0, false }
329 },
330 { { QAccessible::ProgressBar, QAXProgressIndicatorRole, false },
331 { -1, 0, false }
332 },
333 { { QAccessible::ComboBox, QAXPopUpButtonRole, false },
334 { -1, 0, false }
335 },
336 { { QAccessible::RadioButton, QAXRadioButtonRole, false },
337 { -1, 0, false }
338 },
339 { { QAccessible::CheckBox, QAXCheckBoxRole, false },
340 { -1, 0, false }
341 },
342 { { QAccessible::StaticText, QAXStaticTextRole, false },
343 { QAccessible::Name, QAXValueAttribute, false },
344 { -1, 0, false }
345 },
346 { { QAccessible::Table, QAXTableRole, false },
347 { -1, 0, false }
348 },
349 { { QAccessible::StatusBar, QAXStaticTextRole, false },
350 { -1, 0, false }
351 },
352 { { QAccessible::Column, QAXColumnRole, false },
353 { -1, 0, false }
354 },
355 { { QAccessible::ColumnHeader, QAXColumnRole, false },
356 { -1, 0, false }
357 },
358 { { QAccessible::Row, QAXRowRole, false },
359 { -1, 0, false }
360 },
361 { { QAccessible::RowHeader, QAXRowRole, false },
362 { -1, 0, false }
363 },
364 { { QAccessible::Cell, QAXTextFieldRole, false },
365 { -1, 0, false }
366 },
367 { { QAccessible::PushButton, QAXButtonRole, false },
368 { -1, 0, false }
369 },
370 { { QAccessible::EditableText, QAXTextFieldRole, true },
371 { -1, 0, false }
372 },
373 { { QAccessible::Link, QAXTextFieldRole, false },
374 { -1, 0, false }
375 },
376 { { QAccessible::Indicator, QAXValueIndicatorRole, false },
377 { -1, 0, false }
378 },
379 { { QAccessible::Splitter, QAXSplitGroupRole, false },
380 { -1, 0, false }
381 },
382 { { QAccessible::List, QAXListRole, false },
383 { -1, 0, false }
384 },
385 { { QAccessible::ListItem, QAXStaticTextRole, false },
386 { -1, 0, false }
387 },
388 { { QAccessible::Cell, QAXStaticTextRole, false },
389 { -1, 0, false }
390 },
391 { { -1, 0, false } }
392};
393
394class QAInterface;
395static CFStringRef macRole(const QAInterface &interface);
396
397QDebug operator<<(QDebug debug, const QAInterface &interface)
398{
399 if (interface.isValid() == false)
400 debug << "invalid interface";
401 else
402 debug << interface.object() << "id" << interface.id() << "role" << hex << interface.role();
403 return debug;
404}
405
406// The root of the Qt accessible hiearchy.
407static QObject *rootObject = 0;
408
409
410bool QAInterface::operator==(const QAInterface &other) const
411{
412 if (isValid() == false || other.isValid() == false)
413 return (isValid() && other.isValid());
414
415 // walk up the parent chain, comparing child indexes, until we reach
416 // an interface that has a QObject.
417 QAInterface currentThis = *this;
418 QAInterface currentOther = other;
419
420 while (currentThis.object() == 0) {
421 if (currentOther.object() != 0)
422 return false;
423
424 // fail if the child indexes in the two hirearchies don't match.
425 if (currentThis.parent().indexOfChild(currentThis) !=
426 currentOther.parent().indexOfChild(currentOther))
427 return false;
428
429 currentThis = currentThis.parent();
430 currentOther = currentOther.parent();
431 }
432
433 return (currentThis.object() == currentOther.object() && currentThis.id() == currentOther.id());
434}
435
436bool QAInterface::operator!=(const QAInterface &other) const
437{
438 return !operator==(other);
439}
440
441uint qHash(const QAInterface &item)
442{
443 if (item.isValid())
444 return qHash(item.object()) + qHash(item.id());
445 else
446 return qHash(item.cachedObject()) + qHash(item.id());
447}
448
449QAInterface QAInterface::navigate(RelationFlag relation, int entry) const
450{
451 if (!checkValid())
452 return QAInterface();
453
454 // On a QAccessibleInterface that handles its own children we can short-circut
455 // the navigation if this QAInterface refers to one of the children:
456 if (child != 0) {
457 // The Ancestor interface will always be the same QAccessibleInterface with
458 // a child value of 0.
459 if (relation == QAccessible::Ancestor)
460 return QAInterface(*this, 0);
461
462 // The child hiearchy is only one level deep, so navigating to a child
463 // of a child is not possible.
464 if (relation == QAccessible::Child) {
465 return QAInterface();
466 }
467 }
468 QAccessibleInterface *child_iface = 0;
469
470 const int status = base.interface->navigate(relation, entry, &child_iface);
471
472 if (status == -1)
473 return QAInterface(); // not found;
474
475 // Check if target is a child of this interface.
476 if (!child_iface) {
477 return QAInterface(*this, status);
478 } else {
479 // Target is child_iface or a child of that (status decides).
480 return QAInterface(child_iface, status);
481 }
482}
483
484QAElement::QAElement()
485:elementRef(0)
486{}
487
488QAElement::QAElement(AXUIElementRef elementRef)
489:elementRef(elementRef)
490{
491 if (elementRef != 0) {
492 CFRetain(elementRef);
493 CFRetain(object());
494 }
495}
496
497QAElement::QAElement(const QAElement &element)
498:elementRef(element.elementRef)
499{
500 if (elementRef != 0) {
501 CFRetain(elementRef);
502 CFRetain(object());
503 }
504}
505
506QAElement::QAElement(HIObjectRef object, int child)
507 :elementRef(
508#ifndef QT_MAC_USE_COCOA
509 AXUIElementCreateWithHIObjectAndIdentifier(object, child)
510#endif
511)
512{
513#ifndef QT_MAC_USE_COCOA
514 if (object == 0) {
515 elementRef = 0; // Create invalid QAElement.
516 } else {
517 elementRef = AXUIElementCreateWithHIObjectAndIdentifier(object, child);
518 CFRetain(object);
519 }
520#else
521 Q_UNUSED(object);
522 Q_UNUSED(child);
523#endif
524}
525
526QAElement::~QAElement()
527{
528 if (elementRef != 0) {
529 CFRelease(object());
530 CFRelease(elementRef);
531 }
532}
533
534void QAElement::operator=(const QAElement &other)
535{
536 if (*this == other)
537 return;
538
539 if (elementRef != 0) {
540 CFRelease(object());
541 CFRelease(elementRef);
542 }
543
544 elementRef = other.elementRef;
545
546 if (elementRef != 0) {
547 CFRetain(elementRef);
548 CFRetain(object());
549 }
550}
551
552bool QAElement::operator==(const QAElement &other) const
553{
554 if (elementRef == 0 || other.elementRef == 0)
555 return (elementRef == other.elementRef);
556
557 return CFEqual(elementRef, other.elementRef);
558}
559
560uint qHash(QAElement element)
561{
562 return qHash(element.object()) + qHash(element.id());
563}
564
565#ifndef QT_MAC_USE_COCOA
566static QInterfaceFactory *createFactory(const QAInterface &interface);
567#endif
568Q_GLOBAL_STATIC(QAccessibleHierarchyManager, accessibleHierarchyManager);
569
570/*
571 Reomves all accessibility info accosiated with the sender object.
572*/
573void QAccessibleHierarchyManager::objectDestroyed(QObject *object)
574{
575 HIObjectRef hiObject = qobjectHiobjectHash.value(object);
576 delete qobjectElementHash.value(object);
577 qobjectElementHash.remove(object);
578 hiobjectInterfaceHash.remove(hiObject);
579}
580
581/*
582 Removes all stored items.
583*/
584void QAccessibleHierarchyManager::reset()
585{
586 qDeleteAll(qobjectElementHash);
587 qobjectElementHash.clear();
588 hiobjectInterfaceHash.clear();
589 qobjectHiobjectHash.clear();
590}
591
592QAccessibleHierarchyManager *QAccessibleHierarchyManager::instance()
593{
594 return accessibleHierarchyManager();
595}
596
597#ifndef QT_MAC_USE_COCOA
598static bool isItemView(const QAInterface &interface)
599{
600 QObject *object = interface.object();
601 return (interface.role() == QAccessible::List || interface.role() == QAccessible::Table
602 || (object && qobject_cast<QAbstractItemView *>(interface.object()))
603 || (object && object->objectName() == QLatin1String("qt_scrollarea_viewport")
604 && qobject_cast<QAbstractItemView *>(object->parent())));
605}
606#endif
607
608static bool isTabWidget(const QAInterface &interface)
609{
610 if (QObject *object = interface.object())
611 return (object->inherits("QTabWidget") && interface.id() == 0);
612 return false;
613}
614
615static bool isStandaloneTabBar(const QAInterface &interface)
616{
617 QObject *object = interface.object();
618 if (interface.role() == QAccessible::PageTabList && object)
619 return (qobject_cast<QTabWidget *>(object->parent()) == 0);
620
621 return false;
622}
623
624static bool isEmbeddedTabBar(const QAInterface &interface)
625{
626 QObject *object = interface.object();
627 if (interface.role() == QAccessible::PageTabList && object)
628 return (qobject_cast<QTabWidget *>(object->parent()));
629
630 return false;
631}
632
633/*
634 Decides if a QAInterface is interesting from an accessibility users point of view.
635*/
636bool isItInteresting(const QAInterface &interface)
637{
638 // Mac accessibility does not have an attribute that corresponds to the Invisible/Offscreen
639 // state, so we disable the interface here.
640 const QAccessible::State state = interface.state();
641 if (state & QAccessible::Invisible ||
642 state & QAccessible::Offscreen )
643 return false;
644
645 const QAccessible::Role role = interface.role();
646
647 if (QObject * const object = interface.object()) {
648 const QString className = QLatin1String(object->metaObject()->className());
649
650 // VoiceOver focusing on tool tips can be confusing. The contents of the
651 // tool tip is avalible through the description attribute anyway, so
652 // we disable accessibility for tool tips.
653 if (className == QLatin1String("QTipLabel"))
654 return false;
655
656 // Hide TabBars that has a QTabWidget parent (the tab widget handles the accessibility)
657 if (isEmbeddedTabBar(interface))
658 return false;
659
660 // Hide docked dockwidgets. ### causes infinitie loop in the apple accessibility code.
661 /* if (QDockWidget *dockWidget = qobject_cast<QDockWidget *>(object)) {
662 if (dockWidget->isFloating() == false)
663 return false;
664 }
665 */
666 }
667
668 // Client is a generic role returned by plain QWidgets or other
669 // widgets that does not have separate QAccessible interface, such
670 // as the TabWidget. Return false unless macRole gives the interface
671 // a special role.
672 if (role == QAccessible::Client && macRole(interface) == CFStringRef(QAXUnknownRole))
673 return false;
674
675 // Some roles are not interesting:
676 if (role == QAccessible::Border || // QFrame
677 role == QAccessible::Application || // We use the system-provided application element.
678 role == QAccessible::MenuItem) // The system also provides the menu items.
679 return false;
680
681 // It is probably better to access the toolbar buttons directly than having
682 // to navigate through the toolbar.
683 if (role == QAccessible::ToolBar)
684 return false;
685
686 return true;
687}
688
689QAElement QAccessibleHierarchyManager::registerInterface(QObject *object, int child)
690{
691#ifndef QT_MAC_USE_COCOA
692 return registerInterface(QAInterface(QAccessible::queryAccessibleInterface(object), child));
693#else
694 Q_UNUSED(object);
695 Q_UNUSED(child);
696 return QAElement();
697#endif
698}
699
700/*
701 Creates a QAXUIelement that corresponds to the given QAInterface.
702*/
703QAElement QAccessibleHierarchyManager::registerInterface(const QAInterface &interface)
704{
705#ifndef QT_MAC_USE_COCOA
706 if (interface.isValid() == false)
707 return QAElement();
708 QAInterface objectInterface = interface.objectInterface();
709
710 QObject * qobject = objectInterface.object();
711 HIObjectRef hiobject = objectInterface.hiObject();
712 if (qobject == 0 || hiobject == 0)
713 return QAElement();
714
715 if (qobjectElementHash.contains(qobject) == false) {
716 registerInterface(qobject, hiobject, createFactory(interface));
717 HIObjectSetAccessibilityIgnored(hiobject, !isItInteresting(interface));
718 }
719
720 return QAElement(hiobject, interface.id());
721#else
722 Q_UNUSED(interface);
723 return QAElement();
724#endif
725}
726
727#ifndef QT_MAC_USE_COCOA
728#include "qaccessible_mac_carbon.cpp"
729#endif
730
731void QAccessibleHierarchyManager::registerInterface(QObject * qobject, HIObjectRef hiobject, QInterfaceFactory *interfaceFactory)
732{
733#ifndef QT_MAC_USE_COCOA
734 if (qobjectElementHash.contains(qobject) == false) {
735 qobjectElementHash.insert(qobject, interfaceFactory);
736 qobjectHiobjectHash.insert(qobject, hiobject);
737 connect(qobject, SIGNAL(destroyed(QObject *)), SLOT(objectDestroyed(QObject *)));
738 }
739
740 if (hiobjectInterfaceHash.contains(hiobject) == false) {
741 hiobjectInterfaceHash.insert(hiobject, interfaceFactory);
742 installAcessibilityEventHandler(hiobject);
743 }
744#else
745 Q_UNUSED(qobject);
746 Q_UNUSED(hiobject);
747 Q_UNUSED(interfaceFactory);
748#endif
749}
750
751void QAccessibleHierarchyManager::registerChildren(const QAInterface &interface)
752{
753 QObject * const object = interface.object();
754 if (object == 0)
755 return;
756
757 QInterfaceFactory *interfaceFactory = qobjectElementHash.value(object);
758
759 if (interfaceFactory == 0)
760 return;
761
762 interfaceFactory->registerChildren();
763}
764
765QAInterface QAccessibleHierarchyManager::lookup(const AXUIElementRef &element)
766{
767 if (element == 0)
768 return QAInterface();
769#ifndef QT_MAC_USE_COCOA
770 HIObjectRef hiObject = AXUIElementGetHIObject(element);
771
772 QInterfaceFactory *factory = hiobjectInterfaceHash.value(hiObject);
773 if (factory == 0) {
774 return QAInterface();
775 }
776
777 UInt64 id;
778 AXUIElementGetIdentifier(element, &id);
779 return factory->interface(id);
780#else
781 return QAInterface();
782#endif;
783}
784
785QAInterface QAccessibleHierarchyManager::lookup(const QAElement &element)
786{
787 return lookup(element.element());
788}
789
790QAElement QAccessibleHierarchyManager::lookup(const QAInterface &interface)
791{
792 if (interface.isValid() == false)
793 return QAElement();
794
795 QInterfaceFactory *factory = qobjectElementHash.value(interface.objectInterface().object());
796 if (factory == 0)
797 return QAElement();
798
799 return factory->element(interface);
800}
801
802QAElement QAccessibleHierarchyManager::lookup(QObject * const object, int id)
803{
804 QInterfaceFactory *factory = qobjectElementHash.value(object);
805 if (factory == 0)
806 return QAElement();
807
808 return factory->element(id);
809}
810
811/*
812 Standard interface mapping, return the stored interface
813 or HIObjectRef, and there is an one-to-one mapping between
814 the identifier and child.
815*/
816class QStandardInterfaceFactory : public QInterfaceFactory
817{
818public:
819 QStandardInterfaceFactory(const QAInterface &interface)
820 : m_interface(interface), object(interface.hiObject())
821 {
822 CFRetain(object);
823 }
824
825 ~QStandardInterfaceFactory()
826 {
827 CFRelease(object);
828 }
829
830
831 QAInterface interface(UInt64 identifier)
832 {
833 const int child = identifier;
834 return QAInterface(m_interface, child);
835 }
836
837 QAElement element(int id)
838 {
839 return QAElement(object, id);
840 }
841
842 QAElement element(const QAInterface &interface)
843 {
844 if (interface.object() == 0)
845 return QAElement();
846 return QAElement(object, interface.id());
847 }
848
849 void registerChildren()
850 {
851 const int childCount = m_interface.childCount();
852 for (int i = 1; i <= childCount; ++i) {
853 accessibleHierarchyManager()->registerInterface(m_interface.navigate(QAccessible::Child, i));
854 }
855 }
856
857private:
858 QAInterface m_interface;
859 HIObjectRef object;
860};
861
862/*
863 Interface mapping where that creates one HIObject for each interface child.
864*/
865class QMultipleHIObjectFactory : public QInterfaceFactory
866{
867public:
868 QMultipleHIObjectFactory(const QAInterface &interface)
869 : m_interface(interface)
870 { }
871
872 ~QMultipleHIObjectFactory()
873 {
874 foreach (HIObjectRef object, objects) {
875 CFRelease(object);
876 }
877 }
878
879 QAInterface interface(UInt64 identifier)
880 {
881 const int child = identifier;
882 return QAInterface(m_interface, child);
883 }
884
885 QAElement element(int child)
886 {
887 if (child == 0)
888 return QAElement(m_interface.hiObject(), 0);
889
890 if (child > objects.count())
891 return QAElement();
892
893 return QAElement(objects.at(child - 1), child);
894 }
895
896 void registerChildren()
897 {
898#ifndef QT_MAC_USE_COCOA
899 const int childCount = m_interface.childCount();
900 for (int i = 1; i <= childCount; ++i) {
901 HIObjectRef hiobject;
902 HIObjectCreate(kObjectQtAccessibility, 0, &hiobject);
903 objects.append(hiobject);
904 accessibleHierarchyManager()->registerInterface(m_interface.object(), hiobject, this);
905 HIObjectSetAccessibilityIgnored(hiobject, !isItInteresting(m_interface.navigate(QAccessible::Child, i)));
906 }
907#endif
908 }
909
910private:
911 QAInterface m_interface;
912 QList<HIObjectRef> objects;
913};
914
915class QItemViewInterfaceFactory : public QInterfaceFactory
916{
917public:
918 QItemViewInterfaceFactory(const QAInterface &interface)
919 : m_interface(interface), object(interface.hiObject())
920 {
921 CFRetain(object);
922 columnCount = 0;
923 if (QTableView * tableView = qobject_cast<QTableView *>(interface.parent().object())) {
924 if (tableView->model())
925 columnCount = tableView->model()->columnCount();
926 if (tableView->verticalHeader())
927 ++columnCount;
928 }
929 }
930
931 ~QItemViewInterfaceFactory()
932 {
933 CFRelease(object);
934 }
935
936 QAInterface interface(UInt64 identifier)
937 {
938 if (identifier == 0)
939 return m_interface;
940
941 if (m_interface.role() == QAccessible::List)
942 return m_interface.childAt(identifier);
943
944 if (m_interface.role() == QAccessible::Table) {
945 const int index = identifier;
946 if (index == 0)
947 return m_interface; // return the item view interface.
948
949 const int rowIndex = (index - 1) / (columnCount + 1);
950 const int cellIndex = (index - 1) % (columnCount + 1);
951/*
952 qDebug() << "index" << index;
953 qDebug() << "rowIndex" << rowIndex;
954 qDebug() << "cellIndex" << cellIndex;
955*/
956 const QAInterface rowInterface = m_interface.childAt(rowIndex + 1);
957
958 if ((cellIndex) == 0) // Is it a row?
959 return rowInterface;
960 else {
961 return rowInterface.childAt(cellIndex);
962 }
963 }
964
965 return QAInterface();
966 }
967
968 QAElement element(int id)
969 {
970 if (id != 0) {
971 return QAElement();
972 }
973 return QAElement(object, 0);
974 }
975
976 QAElement element(const QAInterface &interface)
977 {
978 if (interface.object() && interface.object() == m_interface.object()) {
979 return QAElement(object, 0);
980 } else if (m_interface.role() == QAccessible::List) {
981 if (interface.parent().object() && interface.parent().object() == m_interface.object())
982 return QAElement(object, m_interface.indexOfChild(interface));
983 } else if (m_interface.role() == QAccessible::Table) {
984 QAInterface currentInterface = interface;
985 int index = 0;
986
987 while (currentInterface.isValid() && currentInterface.object() == 0) {
988 const QAInterface parentInterface = currentInterface.parent();
989/*
990 qDebug() << "current index" << index;
991 qDebug() << "current interface" << interface;
992
993 qDebug() << "parent interface" << parentInterface;
994 qDebug() << "grandparent interface" << parentInterface.parent();
995 qDebug() << "childCount" << interface.childCount();
996 qDebug() << "index of child" << parentInterface.indexOfChild(currentInterface);
997*/
998 index += ((parentInterface.indexOfChild(currentInterface) - 1) * (currentInterface.childCount() + 1)) + 1;
999 currentInterface = parentInterface;
1000// qDebug() << "new current interface" << currentInterface;
1001 }
1002 if (currentInterface.object() == m_interface.object())
1003 return QAElement(object, index);
1004
1005
1006 }
1007 return QAElement();
1008 }
1009
1010 void registerChildren()
1011 {
1012 // Item view child interfraces don't have their own qobjects, so there is nothing to register here.
1013 }
1014
1015private:
1016 QAInterface m_interface;
1017 HIObjectRef object;
1018 int columnCount; // for table views;
1019};
1020
1021#ifndef QT_MAC_USE_COCOA
1022static bool managesChildren(const QAInterface &interface)
1023{
1024 return (interface.childCount() > 0 && interface.childAt(1).id() > 0);
1025}
1026
1027static QInterfaceFactory *createFactory(const QAInterface &interface)
1028{
1029 if (isItemView(interface)) {
1030 return new QItemViewInterfaceFactory(interface);
1031 } if (managesChildren(interface)) {
1032 return new QMultipleHIObjectFactory(interface);
1033 }
1034
1035 return new QStandardInterfaceFactory(interface);
1036}
1037#endif
1038
1039QList<QAElement> lookup(const QList<QAInterface> &interfaces)
1040{
1041 QList<QAElement> elements;
1042 foreach (const QAInterface &interface, interfaces)
1043 if (interface.isValid()) {
1044 const QAElement element = accessibleHierarchyManager()->lookup(interface);
1045 if (element.isValid())
1046 elements.append(element);
1047 }
1048 return elements;
1049}
1050
1051// Debug output helpers:
1052/*
1053static QString nameForEventKind(UInt32 kind)
1054{
1055 switch(kind) {
1056 case kEventAccessibleGetChildAtPoint: return QString("GetChildAtPoint"); break;
1057 case kEventAccessibleGetAllAttributeNames: return QString("GetAllAttributeNames"); break;
1058 case kEventAccessibleGetNamedAttribute: return QString("GetNamedAttribute"); break;
1059 case kEventAccessibleSetNamedAttribute: return QString("SetNamedAttribute"); break;
1060 case kEventAccessibleGetAllActionNames: return QString("GetAllActionNames"); break;
1061 case kEventAccessibleGetFocusedChild: return QString("GetFocusedChild"); break;
1062 default:
1063 return QString("Unknown accessibility event type: %1").arg(kind);
1064 break;
1065 };
1066}
1067*/
1068#ifndef QT_MAC_USE_COCOA
1069static bool qt_mac_append_cf_uniq(CFMutableArrayRef array, CFTypeRef value)
1070{
1071 if (value == 0)
1072 return false;
1073
1074 CFRange range;
1075 range.location = 0;
1076 range.length = CFArrayGetCount(array);
1077 if(!CFArrayContainsValue(array, range, value)) {
1078 CFArrayAppendValue(array, value);
1079 return true;
1080 }
1081 return false;
1082}
1083
1084static OSStatus setAttributeValue(EventRef event, const QList<QAElement> &elements)
1085{
1086 CFMutableArrayRef array = CFArrayCreateMutable(0, 0, &kCFTypeArrayCallBacks);
1087 foreach (const QAElement &element, elements) {
1088 if (element.isValid())
1089 CFArrayAppendValue(array, element.element());
1090 }
1091
1092 const OSStatus err = SetEventParameter(event, kEventParamAccessibleAttributeValue,
1093 typeCFTypeRef, sizeof(array), &array);
1094 CFRelease(array);
1095 return err;
1096}
1097#endif //QT_MAC_USE_COCOA
1098
1099/*
1100 Gets the AccessibleObject parameter from an event.
1101*/
1102static inline AXUIElementRef getAccessibleObjectParameter(EventRef event)
1103{
1104 AXUIElementRef element;
1105 GetEventParameter(event, kEventParamAccessibleObject, typeCFTypeRef, 0,
1106 sizeof(element), 0, &element);
1107 return element;
1108}
1109
1110/*
1111 The application event handler makes sure that all top-level qt windows are registered
1112 before any accessibility events are handeled.
1113*/
1114#ifndef QT_MAC_USE_COCOA
1115static OSStatus applicationEventHandler(EventHandlerCallRef next_ref, EventRef event, void *)
1116{
1117 QAInterface rootInterface(QAccessible::queryAccessibleInterface(rootObject ? rootObject : qApp), 0);
1118 accessibleHierarchyManager()->registerChildren(rootInterface);
1119
1120 return CallNextEventHandler(next_ref, event);
1121}
1122
1123/*
1124 Returns the value for element by combining the QAccessibility::Checked and
1125 QAccessibility::Mixed flags into an int value that the Mac accessibilty
1126 system understands. This works for check boxes, radio buttons, and the like.
1127 The return values are:
1128 0: unchecked
1129 1: checked
1130 2: undecided
1131*/
1132static int buttonValue(QAInterface element)
1133{
1134 const QAccessible::State state = element.state();
1135 if (state & QAccessible::Mixed)
1136 return 2;
1137 else if(state & QAccessible::Checked)
1138 return 1;
1139 else
1140 return 0;
1141}
1142
1143static QString getValue(const QAInterface &interface)
1144{
1145 const QAccessible::Role role = interface.role();
1146 if (role == QAccessible::RadioButton || role == QAccessible::CheckBox)
1147 return QString::number(buttonValue(interface));
1148 else
1149 return interface.text(QAccessible::Value);
1150}
1151#endif //QT_MAC_USE_COCOA
1152
1153/*
1154 Translates a QAccessible::Role into a mac accessibility role.
1155*/
1156static CFStringRef macRole(const QAInterface &interface)
1157{
1158 const QAccessible::Role qtRole = interface.role();
1159
1160// qDebug() << "role for" << interface.object() << "interface role" << hex << qtRole;
1161
1162 // Qt accessibility: QAccessible::Splitter contains QAccessible::Grip.
1163 // Mac accessibility: AXSplitGroup contains AXSplitter.
1164 if (qtRole == QAccessible::Grip) {
1165 const QAInterface parent = interface.parent();
1166 if (parent.isValid() && parent.role() == QAccessible::Splitter)
1167 return CFStringRef(QAXSplitterRole);
1168 }
1169
1170 // Tab widgets and standalone tab bars get the kAXTabGroupRole. Accessibility
1171 // for tab bars emebedded in a tab widget is handled by the tab widget.
1172 if (isTabWidget(interface) || isStandaloneTabBar(interface))
1173 return kAXTabGroupRole;
1174
1175 if (QObject *object = interface.object()) {
1176 // ### The interface for an abstract scroll area returns the generic "Client"
1177 // role, so we have to to an extra detect on the QObject here.
1178 if (object->inherits("QAbstractScrollArea") && interface.id() == 0)
1179 return CFStringRef(QAXScrollAreaRole);
1180
1181 if (object->inherits("QDockWidget"))
1182 return CFStringRef(QAXUnknownRole);
1183 }
1184
1185 int i = 0;
1186 int testRole = text_bindings[i][0].qt;
1187 while (testRole != -1) {
1188 if (testRole == qtRole)
1189 return CFStringRef(text_bindings[i][0].mac);
1190 ++i;
1191 testRole = text_bindings[i][0].qt;
1192 }