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

Last change on this file since 494 was 2, checked in by Dmitry A. Kuminov, 16 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 }
1193
1194// qDebug() << "got unknown role!" << interface << interface.parent();
1195
1196 return CFStringRef(QAXUnknownRole);
1197}
1198
1199/*
1200 Translates a QAccessible::Role and an attribute name into a QAccessible::Text, taking into
1201 account execptions listed in text_bindings.
1202*/
1203#ifndef QT_MAC_USE_COCOA
1204static int textForRoleAndAttribute(QAccessible::Role role, CFStringRef attribute)
1205{
1206 // Search for exception, return it if found.
1207 int testRole = text_bindings[0][0].qt;
1208 int i = 0;
1209 while (testRole != -1) {
1210 if (testRole == role) {
1211 int j = 1;
1212 int qtRole = text_bindings[i][j].qt;
1213 CFStringRef testAttribute = CFStringRef(text_bindings[i][j].mac);
1214 while (qtRole != -1) {
1215 if (CFStringCompare(attribute, testAttribute, 0) == kCFCompareEqualTo) {
1216 return (QAccessible::Text)qtRole;
1217 }
1218 ++j;
1219 testAttribute = CFStringRef(text_bindings[i][j].mac); /// ### custom compare
1220 qtRole = text_bindings[i][j].qt; /// ### custom compare
1221 }
1222 break;
1223 }
1224 ++i;
1225 testRole = text_bindings[i][0].qt;
1226 }
1227
1228 // Return default mappping
1229 if (CFStringCompare(attribute, CFStringRef(QAXTitleAttribute), 0) == kCFCompareEqualTo)
1230 return QAccessible::Name;
1231 else if (CFStringCompare(attribute, CFStringRef(QAXValueAttribute), 0) == kCFCompareEqualTo)
1232 return QAccessible::Value;
1233 else if (CFStringCompare(attribute, CFStringRef(QAXHelpAttribute), 0) == kCFCompareEqualTo)
1234 return QAccessible::Help;
1235#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
1236 else if (CFStringCompare(attribute, CFStringRef(QAXDescriptionAttribute), 0) == kCFCompareEqualTo)
1237 return QAccessible::Description;
1238#endif
1239 else
1240 return -1;
1241}
1242
1243/*
1244 Returns the subrole string constant for the interface if it has one,
1245 else returns an empty string.
1246*/
1247static QCFString subrole(const QAInterface &interface)
1248{
1249 const QAInterface parent = interface.parent();
1250 if (parent.isValid() == false)
1251 return QCFString();
1252
1253 if (parent.role() == QAccessible::ScrollBar) {
1254 QCFString subrole;
1255 switch(interface.id()) {
1256 case 1: subrole = CFStringRef(QAXDecrementArrowSubrole); break;
1257 case 2: subrole = CFStringRef(QAXDecrementPageSubrole); break;
1258 case 4: subrole = CFStringRef(QAXIncrementPageSubrole); break;
1259 case 5: subrole = CFStringRef(QAXIncrementArrowSubrole); break;
1260 default:
1261 break;
1262 }
1263 return subrole;
1264 }
1265 return QCFString();
1266}
1267
1268// Gets the scroll bar orientation by asking the QAbstractSlider object directly.
1269static Qt::Orientation scrollBarOrientation(const QAInterface &scrollBar)
1270{
1271 QObject *const object = scrollBar.object();
1272 if (QAbstractSlider * const sliderObject = qobject_cast<QAbstractSlider * const>(object))
1273 return sliderObject->orientation();
1274
1275 return Qt::Vertical; // D'oh! The interface wasn't a scroll bar.
1276}
1277
1278static QAInterface scrollAreaGetScrollBarInterface(const QAInterface &scrollArea, Qt::Orientation orientation)
1279{
1280 if (macRole(scrollArea) != CFStringRef(CFStringRef(QAXScrollAreaRole)))
1281 return QAInterface();
1282
1283 // Child 1 is the contents widget, 2 and 3 are the scroll bar containers wich contains possible scroll bars.
1284 for (int i = 2; i <= 3; ++i) {
1285 QAInterface scrollBarContainer = scrollArea.childAt(i);
1286 for (int i = 1; i <= scrollBarContainer.childCount(); ++i) {
1287 QAInterface scrollBar = scrollBarContainer.childAt(i);
1288 if (scrollBar.isValid() &&
1289 scrollBar.role() == QAccessible::ScrollBar &&
1290 scrollBarOrientation(scrollBar) == orientation)
1291 return scrollBar;
1292 }
1293 }
1294
1295 return QAInterface();
1296}
1297
1298static bool scrollAreaHasScrollBar(const QAInterface &scrollArea, Qt::Orientation orientation)
1299{
1300 return scrollAreaGetScrollBarInterface(scrollArea, orientation).isValid();
1301}
1302
1303static QAElement scrollAreaGetScrollBar(const QAInterface &scrollArea, Qt::Orientation orientation)
1304{
1305 return accessibleHierarchyManager()->lookup(scrollAreaGetScrollBarInterface(scrollArea, orientation));
1306}
1307
1308static QAElement scrollAreaGetContents(const QAInterface &scrollArea)
1309{
1310 // Child 1 is the contents widget,
1311 return accessibleHierarchyManager()->lookup(scrollArea.navigate(QAccessible::Child, 1));
1312}
1313
1314static QAElement tabWidgetGetContents(const QAInterface &interface)
1315{
1316 // A kAXTabGroup has a kAXContents attribute, which consists of the
1317 // ui elements for the current tab page. Get the current tab page
1318 // from the QStackedWidget, where the current visible page can
1319 // be found at index 1.
1320 QAInterface stackedWidget = interface.childAt(1);
1321 accessibleHierarchyManager()->registerChildren(stackedWidget);
1322 QAInterface tabPageInterface = stackedWidget.childAt(1);
1323 return accessibleHierarchyManager()->lookup(tabPageInterface);
1324}
1325
1326static QList<QAElement> tabBarGetTabs(const QAInterface &interface)
1327{
1328 // Get the tabs by searching for children with the "PageTab" role.
1329 // This filters out the left/right navigation buttons.
1330 accessibleHierarchyManager()->registerChildren(interface);
1331 QList<QAElement> tabs;
1332 const int numChildren = interface.childCount();
1333 for (int i = 1; i < numChildren + 1; ++i) {
1334 QAInterface child = interface.navigate(QAccessible::Child, i);
1335 if (child.isValid() && child.role() == QAccessible::PageTab) {
1336 tabs.append(accessibleHierarchyManager()->lookup(child));
1337 }
1338 }
1339 return tabs;
1340}
1341
1342static QList<QAElement> tabWidgetGetTabs(const QAInterface &interface)
1343{
1344 // Each QTabWidget has two children, a QStackedWidget and a QTabBar.
1345 // Get the tabs from the QTabBar.
1346 return tabBarGetTabs(interface.childAt(2));
1347}
1348
1349static QList<QAElement> tabWidgetGetChildren(const QAInterface &interface)
1350{
1351 // The children for a kAXTabGroup should consist of the tabs and the
1352 // contents of the current open tab page.
1353 QList<QAElement> children = tabWidgetGetTabs(interface);
1354 children += tabWidgetGetContents(interface);
1355 return children;
1356}
1357#endif //QT_MAC_USE_COCOA
1358
1359/*
1360 Returns the label (buddy) interface for interface, or 0 if it has none.
1361*/
1362/*
1363static QAInterface findLabel(const QAInterface &interface)
1364{
1365 return interface.navigate(QAccessible::Label, 1);
1366}
1367*/
1368/*
1369 Returns a list of interfaces this interface labels, or an empty list if it doesn't label any.
1370*/
1371/*
1372static QList<QAInterface> findLabelled(const QAInterface &interface)
1373{
1374 QList<QAInterface> interfaceList;
1375
1376 int count = 1;
1377 const QAInterface labelled = interface.navigate(QAccessible::Labelled, count);
1378 while (labelled.isValid()) {
1379 interfaceList.append(labelled);
1380 ++count;
1381 }
1382 return interfaceList;
1383}
1384*/
1385/*
1386 Tests if the given QAInterface has data for a mac attribute.
1387*/
1388#ifndef QT_MAC_USE_COCOA
1389static bool supportsAttribute(CFStringRef attribute, const QAInterface &interface)
1390{
1391 const int text = textForRoleAndAttribute(interface.role(), attribute);
1392
1393 // Special case: Static texts don't have a title.
1394 if (interface.role() == QAccessible::StaticText && attribute == CFStringRef(QAXTitleAttribute))
1395 return false;
1396
1397 // Return true if we the attribute matched a QAccessible::Role and we get text for that role from the interface.
1398 if (text != -1) {
1399 if (text == QAccessible::Value) // Special case for Value, see getValue()
1400 return !getValue(interface).isEmpty();
1401 else
1402 return !interface.text((QAccessible::Text)text).isEmpty();
1403 }
1404
1405 if (CFStringCompare(attribute, CFStringRef(QAXChildrenAttribute), 0) == kCFCompareEqualTo) {
1406 if (interface.childCount() > 0)
1407 return true;
1408 }
1409
1410 if (CFStringCompare(attribute, CFStringRef(QAXSubroleAttribute), 0) == kCFCompareEqualTo) {
1411 return (subrole(interface) != QCFString());
1412 }
1413
1414 return false;
1415}
1416
1417static void appendIfSupported(CFMutableArrayRef array, CFStringRef attribute, const QAInterface &interface)
1418{
1419 if (supportsAttribute(attribute, interface))
1420 qt_mac_append_cf_uniq(array, attribute);
1421}
1422
1423/*
1424 Returns the names of the attributes the give QAInterface supports.
1425*/
1426static OSStatus getAllAttributeNames(EventRef event, const QAInterface &interface, EventHandlerCallRef next_ref)
1427{
1428 // Call system event handler.
1429 OSStatus err = CallNextEventHandler(next_ref, event);
1430 if(err != noErr && err != eventNotHandledErr)
1431 return err;
1432 CFMutableArrayRef attrs = 0;
1433 GetEventParameter(event, kEventParamAccessibleAttributeNames, typeCFMutableArrayRef, 0,
1434 sizeof(attrs), 0, &attrs);
1435
1436 if (!attrs)
1437 return eventNotHandledErr;
1438
1439 // Append attribute names that are always supported.
1440 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXPositionAttribute));
1441 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXSizeAttribute));
1442 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXRoleAttribute));
1443 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXEnabledAttribute));
1444 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXWindowAttribute));
1445#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
1446 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXTopLevelUIElementAttribute));
1447#endif
1448
1449 // Append these names if the QInterafceItem returns any data for them.
1450 appendIfSupported(attrs, CFStringRef(QAXTitleAttribute), interface);
1451 appendIfSupported(attrs, CFStringRef(QAXValueAttribute), interface);
1452#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
1453 appendIfSupported(attrs, CFStringRef(QAXDescriptionAttribute), interface);
1454 appendIfSupported(attrs, CFStringRef(QAXLinkedUIElementsAttribute), interface);
1455#endif
1456 appendIfSupported(attrs, CFStringRef(QAXHelpAttribute), interface);
1457 appendIfSupported(attrs, CFStringRef(QAXTitleUIElementAttribute), interface);
1458 appendIfSupported(attrs, CFStringRef(QAXChildrenAttribute), interface);
1459 appendIfSupported(attrs, CFStringRef(QAXSubroleAttribute), interface);
1460
1461 // Append attribute names based on the interaface role.
1462 switch (interface.role()) {
1463 case QAccessible::Window:
1464 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXMainAttribute));
1465 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXMinimizedAttribute));
1466 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXCloseButtonAttribute));
1467 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXZoomButtonAttribute));
1468 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXMinimizeButtonAttribute));
1469 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXToolbarButtonAttribute));
1470 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXGrowAreaAttribute));
1471 break;
1472 case QAccessible::RadioButton:
1473 case QAccessible::CheckBox:
1474 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXMinValueAttribute));
1475 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXMaxValueAttribute));
1476 break;
1477 case QAccessible::ScrollBar:
1478 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXOrientationAttribute));
1479 break;
1480 case QAccessible::Splitter:
1481 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXSplittersAttribute));
1482 break;
1483 case QAccessible::Table:
1484 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXRowsAttribute));
1485 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXVisibleRowsAttribute));
1486 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXSelectedRowsAttribute));
1487 break;
1488 default:
1489 break;
1490 }
1491
1492 // Append attribute names based on the mac accessibility role.
1493 const QCFString mac_role = macRole(interface);
1494 if (mac_role == CFStringRef(QAXSplitterRole)) {
1495 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXPreviousContentsAttribute));
1496 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXNextContentsAttribute));
1497 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXOrientationAttribute));
1498 } else if (mac_role == CFStringRef(QAXScrollAreaRole)) {
1499 if (scrollAreaHasScrollBar(interface, Qt::Horizontal))
1500 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXHorizontalScrollBarAttribute));
1501 if (scrollAreaHasScrollBar(interface, Qt::Vertical))
1502 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXVerticalScrollBarAttribute));
1503 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXContentsAttribute));
1504 } else if (mac_role == CFStringRef(QAXTabGroupRole)) {
1505 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXTabsAttribute));
1506 // Only tab widgets can have the contents attribute, there is no way of getting
1507 // the contents from a QTabBar.
1508 if (isTabWidget(interface))
1509 qt_mac_append_cf_uniq(attrs, CFStringRef(QAXContentsAttribute));
1510 }
1511
1512 return noErr;
1513}
1514
1515static void handleStringAttribute(EventRef event, QAccessible::Text text, const QAInterface &interface)
1516{
1517 QString str = interface.text(text);
1518 if (str.isEmpty())
1519 return;
1520
1521 // Remove any html markup from the text string, or VoiceOver will read the html tags.
1522 static QTextDocument document;
1523 document.setHtml(str);
1524 str = document.toPlainText();
1525
1526 CFStringRef cfstr = QCFString::toCFStringRef(str);
1527 SetEventParameter(event, kEventParamAccessibleAttributeValue, typeCFStringRef, sizeof(cfstr), &cfstr);
1528}
1529
1530/*
1531 Handles the parent attribute for a interface.
1532 There are basically three cases here:
1533 1. interface is a HIView and has only HIView children.
1534 2. interface is a HIView but has children that is not a HIView
1535 3. interface is not a HIView.
1536*/
1537static OSStatus handleChildrenAttribute(EventHandlerCallRef next_ref, EventRef event, QAInterface &interface)
1538{
1539 // Add the children for this interface to the global QAccessibelHierachyManager.
1540 accessibleHierarchyManager()->registerChildren(interface);
1541
1542 if (isTabWidget(interface)) {
1543 QList<QAElement> children = tabWidgetGetChildren(interface);
1544 const int childCount = children.count();
1545
1546 CFMutableArrayRef array = 0;
1547 array = CFArrayCreateMutable(0, 0, &kCFTypeArrayCallBacks);
1548 for (int i = 0; i < childCount; ++i) {
1549 qt_mac_append_cf_uniq(array, children.at(i).element());
1550 }
1551
1552 OSStatus err;
1553 err = SetEventParameter(event, kEventParamAccessibleAttributeValue, typeCFArrayRef, sizeof(array), &array);
1554 if (err != noErr)
1555 qWarning("Qt:Internal error (%s:%d)", __FILE__, __LINE__);
1556
1557 return noErr;
1558 }
1559
1560 const QList<QAElement> children = lookup(interface.children());
1561 const int childCount = children.count();
1562
1563 OSStatus err = eventNotHandledErr;
1564 if (interface.isHIView())
1565 err = CallNextEventHandler(next_ref, event);
1566
1567 CFMutableArrayRef array = 0;
1568 int arraySize = 0;
1569 if (err == noErr) {
1570 CFTypeRef obj = 0;
1571 err = GetEventParameter(event, kEventParamAccessibleAttributeValue, typeCFTypeRef, NULL , sizeof(obj), NULL, &obj);
1572 if (err == noErr && obj != 0) {
1573 array = (CFMutableArrayRef)obj;
1574 arraySize = CFArrayGetCount(array);
1575 }
1576 }
1577
1578 if (array) {
1579 CFArrayRemoveAllValues(array);
1580 for (int i = 0; i < childCount; ++i) {
1581 qt_mac_append_cf_uniq(array, children.at(i).element());
1582 }
1583 } else {
1584 array = CFArrayCreateMutable(0, 0, &kCFTypeArrayCallBacks);
1585 for (int i = 0; i < childCount; ++i) {
1586 qt_mac_append_cf_uniq(array, children.at(i).element());
1587 }
1588