Add implementations of QAIM::sibling in public APIs.
[profile/ivi/qtbase.git] / src / gui / accessible / qaccessible.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qaccessible.h"
43
44 #include "qaccessibleplugin.h"
45 #include "qaccessibleobject.h"
46 #include "qaccessiblebridge.h"
47 #include <QtGui/QGuiApplication>
48 #include <private/qguiapplication_p.h>
49 #include <qpa/qplatformaccessibility.h>
50 #include <qpa/qplatformintegration.h>
51
52 #include <QtCore/qdebug.h>
53 #include <QtCore/qmetaobject.h>
54 #include <private/qfactoryloader_p.h>
55
56 QT_BEGIN_NAMESPACE
57
58 /*!
59     \class QAccessible
60     \brief The QAccessible class provides enums and static functions
61     related to accessibility.
62     \internal
63
64     \ingroup accessibility
65     \inmodule QtWidgets
66
67     This class is part of \l {Accessibility for QWidget Applications}.
68
69     Accessible applications can be used by people who are not able to
70     use applications by conventional means.
71
72     The functions in this class are used for communication between
73     accessible applications (also called AT Servers) and
74     accessibility tools (AT Clients), such as screen readers and
75     braille displays. Clients and servers communicate in the following way:
76
77     \list
78     \li  \e{AT Servers} notify the clients about events through calls to the
79         updateAccessibility() function.
80
81     \li  \e{AT Clients} request information about the objects in the server.
82         The QAccessibleInterface class is the core interface, and encapsulates
83         this information in a pure virtual API. Implementations of the interface
84         are provided by Qt through the queryAccessibleInterface() API.
85     \endlist
86
87     The communication between servers and clients is initialized by
88     the setRootObject() function. Function pointers can be installed
89     to replace or extend the default behavior of the static functions
90     in QAccessible.
91
92     Qt supports Microsoft Active Accessibility (MSAA), Mac OS X
93     Accessibility, and the Unix/X11 AT-SPI standard. Other backends
94     can be supported using QAccessibleBridge.
95
96     In addition to QAccessible's static functions, Qt offers one
97     generic interface, QAccessibleInterface, that can be used to wrap
98     all widgets and objects (e.g., QPushButton). This single
99     interface provides all the metadata necessary for the assistive
100     technologies. Qt provides implementations of this interface for
101     its built-in widgets as plugins.
102
103     When you develop custom widgets, you can create custom subclasses
104     of QAccessibleInterface and distribute them as plugins (using
105     QAccessiblePlugin) or compile them into the application.
106     Likewise, Qt's predefined accessibility support can be built as
107     plugin (the default) or directly into the Qt library. The main
108     advantage of using plugins is that the accessibility classes are
109     only loaded into memory if they are actually used; they don't
110     slow down the common case where no assistive technology is being
111     used.
112
113     Qt also includes two convenience classes, QAccessibleObject and
114     QAccessibleWidget, that inherit from QAccessibleInterface and
115     provide the lowest common denominator of metadata (e.g., widget
116     geometry, window title, basic help text). You can use them as
117     base classes when wrapping your custom QObject or QWidget
118     subclasses.
119
120     \sa QAccessibleInterface
121 */
122
123
124 /*!
125     \class QAccessible::State
126
127     \inmodule QtGui
128
129     This structure defines bit flags that indicate
130     the state of an accessible object. The values are:
131
132     \value active                  The object is the active window or the active sub-element in a container (that would get focus when focusing the container).
133     \value adjustable              The object represents an adjustable value, e.g. sliders.
134     \value animated                The object's appearance changes frequently.
135     \value busy                    The object cannot accept input at the moment.
136     \value checkable               The object is checkable.
137     \value checked                 The object's check box is checked.
138     \value checkStateMixed         The third state of checkboxes (half checked in tri-state check boxes).
139     \value collapsed               The object is collapsed, e.g. a closed listview item, or an iconified window.
140     \value defaultButton           The object represents the default button in a dialog.
141     \value defunct                 The object no longer exists.
142     \value editable                The object has a text carret (and often implements the text interface).
143     \value expandable              The object is expandable, mostly used for cells in a tree view.
144     \value expanded                The object is expanded, currently its children are visible.
145     \value extSelectable           The object supports extended selection.
146     \value focusable               The object can receive focus. Only objects in the active window can receive focus.
147     \value focused                 The object has keyboard focus.
148     \value hasPopup                The object opens a popup.
149     \value hotTracked              The object's appearance is sensitive to the mouse cursor position.
150     \value invalid                 The object is no longer valid (because it has been deleted).
151     \value invalidEntry            Input validation current input invalid.
152     \value invisible               The object is not visible to the user.
153     \value linked                  The object is linked to another object, e.g. a hyperlink.
154     \value marqueed                The object displays scrolling contents, e.g. a log view.
155     \value modal                   The object blocks input from other objects.
156     \value movable                 The object can be moved.
157     \value multiLine               The object has multiple lines of text (word wrap), as opposed to a single line.
158     \value multiSelectable         The object supports multiple selected items.
159     \value offscreen               The object is clipped by the visible area. Objects that are off screen are also invisible.
160     \value passwordEdit            The object is a password field, e.g. a line edit for entering a Password.
161     \value playsSound              The object produces sound when interacted with.
162     \value pressed                 The object is pressed.
163     \value readOnly                The object can usually be edited, but is explicitly set to read-only.
164     \value selectable              The object is selectable.
165     \value selectableText          The object has text which can be selected. This is different from selectable which refers to the object's children.
166     \value selected                The object is selected.
167     \value selfVoicing             The object describes itself through speech or sound.
168     \value sizeable                The object can be resized, e.g. top-level windows.
169     \value summaryElement          The object summarizes the state of the window and should be treated with priority.
170     \value supportsAutoCompletion  The object has auto-completion, for example in line edits or combo boxes.
171     \value traversed               The object is linked and has been visited.
172     \value updatesFrequently       The object changes frequently and needs to be refreshed when accessing it.
173     \value disabled                The object is unavailable to the user, e.g. a disabled widget.
174
175     Implementations of QAccessibleInterface::state() return a combination
176     of these flags.
177 */
178
179 /*!
180     \fn QAccessible::State::State()
181
182     Creates a new QAccessible::State with all states set to false.
183 */
184
185 /*!
186     \enum QAccessible::Event
187
188     This enum type defines accessible event types.
189
190     \value AcceleratorChanged               The keyboard accelerator for an action has been changed.
191     \value ActionChanged                    An action has been changed.
192     \value ActiveDescendantChanged
193     \value Alert                            A system alert (e.g., a message from a QMessageBox)
194     \value AttributeChanged
195     \value ContextHelpEnd                   Context help (QWhatsThis) for an object is finished.
196     \value ContextHelpStart                 Context help (QWhatsThis) for an object is initiated.
197     \value DefaultActionChanged             The default QAccessible::Action for the accessible
198                                             object has changed.
199     \value DescriptionChanged               The object's QAccessible::Description changed.
200     \value DialogEnd                        A dialog (QDialog) has been hidden
201     \value DialogStart                      A dialog (QDialog) has been set visible.
202     \value DocumentContentChanged           The contents of a text document have changed.
203     \value DocumentLoadComplete             A document has been loaded.
204     \value DocumentLoadStopped              A document load has been stopped.
205     \value DocumentReload                   A document reload has been initiated.
206     \value DragDropEnd                      A drag and drop operation is about to finished.
207     \value DragDropStart                    A drag and drop operation is about to be initiated.
208     \value Focus                            An object has gained keyboard focus.
209     \value ForegroundChanged                A window has been activated (i.e., a new window has
210                                             gained focus on the desktop).
211     \value HelpChanged                      The QAccessible::Help text property of an object has
212                                             changed.
213     \value HyperlinkEndIndexChanged         The end position of the display text for a hypertext
214                                             link has changed.
215     \value HyperlinkNumberOfAnchorsChanged  The number of anchors in a hypertext link has changed,
216                                             perhaps because the display text has been split to
217                                             provide more than one link.
218     \value HyperlinkSelectedLinkChanged     The link for the selected hypertext link has changed.
219     \value HyperlinkStartIndexChanged       The start position of the display text for a hypertext
220                                             link has changed.
221     \value HypertextChanged                 The display text for a hypertext link has changed.
222     \value HypertextLinkActivated           A hypertext link has been activated, perhaps by being
223                                             clicked or via a key press.
224     \value HypertextLinkSelected            A hypertext link has been selected.
225     \value HypertextNLinksChanged
226     \value LocationChanged                  An object's location on the screen has changed.
227     \value MenuCommand                      A menu item is triggered.
228     \value MenuEnd                          A menu has been closed (Qt uses PopupMenuEnd for all
229                                             menus).
230     \value MenuStart                        A menu has been opened on the menubar (Qt uses
231                                             PopupMenuStart for all menus).
232     \value NameChanged                      The QAccessible::Name property of an object has changed.
233     \value ObjectAttributeChanged
234     \value ObjectCreated                    A new object is created.
235     \value ObjectDestroyed                  An object is deleted.
236     \value ObjectHide                       An object is hidden; for example, with QWidget::hide().
237                                             Any children the object that is hidden has do not send
238                                             this event. It is not sent when an object is hidden as
239                                             it is being obcured by others.
240     \value ObjectReorder                    A layout or item view  has added, removed, or moved an
241                                             object (Qt does not use this event).
242     \value ObjectShow                       An object is displayed; for example, with
243                                             QWidget::show().
244     \value PageChanged
245     \value ParentChanged                    An object's parent object changed.
246     \value PopupMenuEnd                     A pop-up menu has closed.
247     \value PopupMenuStart                   A pop-up menu has opened.
248     \value ScrollingEnd                     A scrollbar scroll operation has ended (the mouse has
249                                             released the slider handle).
250     \value ScrollingStart                   A scrollbar scroll operation is about to start; this may
251                                             be caused by a mouse press on the slider handle, for
252                                             example.
253     \value SectionChanged
254     \value SelectionAdd                     An item has been added to the selection in an item view.
255     \value SelectionRemove                  An item has been removed from an item view selection.
256     \value Selection                        The selection has changed in a menu or item view.
257     \value SelectionWithin                  Several changes to a selection has occurred in an item
258                                             view.
259     \value SoundPlayed                      A sound has been played by an object
260     \omitvalue StateChanged                 The QAccessible::State of an object has changed.
261                                             This value is used internally for the QAccessibleStateChangeEvent.
262     \value TableCaptionChanged              A table caption has been changed.
263     \value TableColumnDescriptionChanged    The description of a table column, typically found in
264                                             the column's header, has been changed.
265     \value TableColumnHeaderChanged         A table column header has been changed.
266     \value TableModelChanged                The model providing data for a table has been changed.
267     \value TableRowDescriptionChanged       The description of a table row, typically found in the
268                                             row's header, has been changed.
269     \value TableRowHeaderChanged            A table row header has been changed.
270     \value TableSummaryChanged              The summary of a table has been changed.
271     \omitvalue TextAttributeChanged
272     \omitvalue TextCaretMoved                   The caret has moved in an editable widget.
273                                             The caret represents the cursor position in an editable
274                                             widget with the input focus.
275     \value TextColumnChanged                A text column has been changed.
276     \omitvalue TextInserted                     Text has been inserted into an editable widget.
277     \omitvalue TextRemoved                      Text has been removed from an editable widget.
278     \omitvalue TextSelectionChanged             The selected text has changed in an editable widget.
279     \omitvalue TextUpdated                      The text has been update in an editable widget.
280     \omitvalue ValueChanged                     The QAccessible::Value of an object has changed.
281     \value VisibleDataChanged
282
283     The values for this enum are defined to be the same as those defined in the
284     \l{AccessibleEventID.idl File Reference}{IAccessible2} and
285     \l{Microsoft Active Accessibility Event Constants}{MSAA} specifications.
286 */
287
288 /*!
289     \enum QAccessible::Role
290
291     This enum defines the role of an accessible object. The roles are:
292
293     \value AlertMessage     An object that is used to alert the user.
294     \value Animation        An object that displays an animation.
295     \value Application      The application's main window.
296     \value Assistant        An object that provids interactive help.
297     \value Border           An object that represents a border.
298     \value ButtonDropDown   A button that drops down a list of items.
299     \value ButtonDropGrid   A button that drops down a grid.
300     \value ButtonMenu       A button that drops down a menu.
301     \value Canvas           An object that displays graphics that the user can interact with.
302     \value Caret            An object that represents the system caret (text cursor).
303     \value Cell             A cell in a table.
304     \value Chart            An object that displays a graphical representation of data.
305     \value CheckBox         An object that represents an option that can be checked or unchecked. Some options provide a "mixed" state, e.g. neither checked nor unchecked.
306     \value Client           The client area in a window.
307     \value Clock            A clock displaying time.
308     \value Column           A column of cells, usually within a table.
309     \value ColumnHeader     A header for a column of data.
310     \value ComboBox         A list of choices that the user can select from.
311     \value Cursor           An object that represents the mouse cursor.
312     \value Desktop          The object represents the desktop or workspace.
313     \value Dial             An object that represents a dial or knob.
314     \value Dialog           A dialog box.
315     \value Document         A document window, usually in an MDI environment.
316     \value EditableText     Editable text
317     \value Equation         An object that represents a mathematical equation.
318     \value Graphic          A graphic or picture, e.g. an icon.
319     \value Grip             A grip that the user can drag to change the size of widgets.
320     \value Grouping         An object that represents a logical grouping of other objects.
321     \value HelpBalloon      An object that displays help in a separate, short lived window.
322     \value HotkeyField      A hotkey field that allows the user to enter a key sequence.
323     \value Indicator        An indicator that represents a current value or item.
324     \value LayeredPane      An object that can contain layered children, e.g. in a stack.
325     \value Link             A link to something else.
326     \value List             A list of items, from which the user can select one or more items.
327     \value ListItem         An item in a list of items.
328     \value MenuBar          A menu bar from which menus are opened by the user.
329     \value MenuItem         An item in a menu or menu bar.
330     \value NoRole           The object has no role. This usually indicates an invalid object.
331     \value PageTab          A page tab that the user can select to switch to a different page in a dialog.
332     \value PageTabList      A list of page tabs.
333     \value Pane             A generic container.
334     \value PopupMenu        A menu which lists options that the user can select to perform an action.
335     \value ProgressBar      The object displays the progress of an operation in progress.
336     \value PropertyPage     A property page where the user can change options and settings.
337     \value Button           A button.
338     \value RadioButton      An object that represents an option that is mutually exclusive with other options.
339     \value Row              A row of cells, usually within a table.
340     \value RowHeader        A header for a row of data.
341     \value ScrollBar        A scroll bar, which allows the user to scroll the visible area.
342     \value Separator        A separator that divides space into logical areas.
343     \value Slider           A slider that allows the user to select a value within a given range.
344     \value Sound            An object that represents a sound.
345     \value SpinBox          A spin box widget that allows the user to enter a value within a given range.
346     \value Splitter         A splitter distributing available space between its child widgets.
347     \value StaticText       Static text, such as labels for other widgets.
348     \value StatusBar        A status bar.
349     \value Table            A table representing data in a grid of rows and columns.
350     \value Terminal         A terminal or command line interface.
351     \value TitleBar         The title bar caption of a window.
352     \value ToolBar          A tool bar, which groups widgets that the user accesses frequently.
353     \value ToolTip          A tool tip which provides information about other objects.
354     \value Tree             A list of items in a tree structure.
355     \value TreeItem         An item in a tree structure.
356     \value UserRole         The first value to be used for user defined roles.
357     \value Whitespace       Blank space between other objects.
358     \value Window           A top level window.
359 */
360
361 /*!
362     \enum QAccessible::RelationFlag
363
364     This enum type defines bit flags that can be combined to indicate
365     the relationship between two accessible objects.
366
367     \value Label            The first object is the label of the second object.
368     \value Labelled         The first object is labelled by the second object.
369     \value Controller       The first object controls the second object.
370     \value Controlled       The first object is controlled by the second object.
371     \value AllRelations     Used as a mask to specify that we are interesting in information
372                             about all relations
373
374     Implementations of relations() return a combination of these flags.
375     Some values are mutually exclusive.
376 */
377
378 /*!
379     \enum QAccessible::Text
380
381     This enum specifies string information that an accessible object
382     returns.
383
384     \value Name         The name of the object. This can be used both
385                         as an identifier or a short description by
386                         accessible clients.
387     \value Description  A short text describing the object.
388     \value Value        The value of the object.
389     \value Help         A longer text giving information about how to use the object.
390     \value Accelerator  The keyboard shortcut that executes the object's default action.
391     \value UserText     The first value to be used for user defined text.
392     \omitvalue DebugDescription
393 */
394
395 /*!
396     \enum QAccessible::InterfaceType
397
398     \l QAccessibleInterface supports several sub interfaces.
399     In order to provide more information about some objects, their accessible
400     representation should implement one or more of these interfaces.
401     When subclassing one of these interfaces, \l QAccessibleInterface::interface_cast also needs to be implemented.
402
403     \value TextInterface            For text that supports selections or is more than one line. Simple labels do not need to implement this interface.
404     \value EditableTextInterface    For text that can be edited by the user.
405     \value ValueInterface           For objects that are used to manipulate a value, for example slider or scroll bar.
406     \value ActionInterface          For interactive objects that allow the user to trigger an action. Basically everything that allows for example mouse interaction.
407     \omitvalue ImageInterface       For objects that represent an image. This interface is generally less important.
408     \value TableInterface           For lists, tables and trees.
409     \value TableCellInterface       For cells in a TableInterface object.
410
411     \sa QAccessibleInterface::interface_cast, QAccessibleTextInterface, QAccessibleEditableTextInterface, QAccessibleValueInterface, QAccessibleActionInterface, QAccessibleTableInterface, QAccessibleTableCellInterface
412 */
413
414 /*!
415     \fn QAccessibleInterface::~QAccessibleInterface()
416
417     Destroys the object.
418 */
419
420
421
422
423 /* accessible widgets plugin discovery stuff */
424 #ifndef QT_NO_LIBRARY
425 Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
426     (QAccessibleFactoryInterface_iid, QLatin1String("/accessible")))
427 #endif
428
429 Q_GLOBAL_STATIC(QList<QAccessible::InterfaceFactory>, qAccessibleFactories)
430
431 QAccessible::UpdateHandler QAccessible::updateHandler = 0;
432 QAccessible::RootObjectHandler QAccessible::rootObjectHandler = 0;
433
434 static bool accessibility_active = false;
435 static bool cleanupAdded = false;
436
437 static QPlatformAccessibility *platformAccessibility()
438 {
439     QPlatformIntegration *pfIntegration = QGuiApplicationPrivate::platformIntegration();
440     return pfIntegration ? pfIntegration->accessibility() : 0;
441 }
442
443 /*!
444     \internal
445 */
446 void QAccessible::cleanup()
447 {
448     if (QPlatformAccessibility *pfAccessibility = platformAccessibility())
449         pfAccessibility->cleanup();
450 }
451
452 static void qAccessibleCleanup()
453 {
454     qAccessibleFactories()->clear();
455 }
456
457 /*!
458     \typedef QAccessible::InterfaceFactory
459
460     This is a typedef for a pointer to a function with the following
461     signature:
462
463     \snippet code/src_gui_accessible_qaccessible.cpp 1
464
465     The function receives a QString and a QObject pointer, where the
466     QString is the key identifying the interface. The QObject is used
467     to pass on to the QAccessibleInterface so that it can hold a reference
468     to it.
469
470     If the key and the QObject does not have a corresponding
471     QAccessibleInterface, a null-pointer will be returned.
472
473     Installed factories are called by queryAccessibilityInterface() until
474     one provides an interface.
475 */
476
477 /*!
478     \typedef QAccessible::UpdateHandler
479
480     \internal
481
482     A function pointer type. Use a function with this prototype to install
483     your own update function.
484
485     The function is called by updateAccessibility().
486 */
487
488 /*!
489     \typedef QAccessible::RootObjectHandler
490
491     \internal
492
493     A function pointer type. Use a function with this prototype to install
494     your own root object handler.
495
496     The function is called by setRootObject().
497 */
498
499
500 /*!
501     Installs the InterfaceFactory \a factory. The last factory added
502     is the first one used by queryAccessibleInterface().
503 */
504 void QAccessible::installFactory(InterfaceFactory factory)
505 {
506     if (!factory)
507         return;
508
509     if (!cleanupAdded) {
510         qAddPostRoutine(qAccessibleCleanup);
511         cleanupAdded = true;
512     }
513     if (qAccessibleFactories()->contains(factory))
514         return;
515     qAccessibleFactories()->append(factory);
516 }
517
518 /*!
519     Removes \a factory from the list of installed InterfaceFactories.
520 */
521 void QAccessible::removeFactory(InterfaceFactory factory)
522 {
523     qAccessibleFactories()->removeAll(factory);
524 }
525
526 /*!
527     \internal
528
529     Installs the given \a handler as the function to be used by
530     updateAccessibility(), and returns the previously installed
531     handler.
532 */
533 QAccessible::UpdateHandler QAccessible::installUpdateHandler(UpdateHandler handler)
534 {
535     UpdateHandler old = updateHandler;
536     updateHandler = handler;
537     return old;
538 }
539
540 /*!
541     Installs the given \a handler as the function to be used by setRootObject(),
542     and returns the previously installed handler.
543 */
544 QAccessible::RootObjectHandler QAccessible::installRootObjectHandler(RootObjectHandler handler)
545 {
546     RootObjectHandler old = rootObjectHandler;
547     rootObjectHandler = handler;
548     return old;
549 }
550
551 /*!
552     If a QAccessibleInterface implementation exists for the given \a object,
553     this function returns a pointer to the implementation; otherwise it
554     returns 0.
555
556     The function calls all installed factory functions (from most
557     recently installed to least recently installed) until one is found
558     that provides an interface for the class of \a object. If no
559     factory can provide an accessibility implementation for the class
560     the function loads installed accessibility plugins, and tests if
561     any of the plugins can provide the implementation.
562
563     If no implementation for the object's class is available, the
564     function tries to find an implementation for the object's parent
565     class, using the above strategy.
566
567     \warning The caller is responsible for deleting the returned
568     interface after use.
569 */
570 QAccessibleInterface *QAccessible::queryAccessibleInterface(QObject *object)
571 {
572     accessibility_active = true;
573     if (!object)
574         return 0;
575
576     const QMetaObject *mo = object->metaObject();
577     while (mo) {
578         const QString cn = QLatin1String(mo->className());
579         for (int i = qAccessibleFactories()->count(); i > 0; --i) {
580             InterfaceFactory factory = qAccessibleFactories()->at(i - 1);
581             if (QAccessibleInterface *iface = factory(cn, object))
582                 return iface;
583         }
584 #ifndef QT_NO_LIBRARY
585         if (QAccessibleInterface * iface = qLoadPlugin1<QAccessibleInterface, QAccessiblePlugin>(loader(), cn, object))
586             return iface;
587 #endif
588         mo = mo->superClass();
589     }
590
591     if (object == qApp)
592         return new QAccessibleApplication;
593
594     return 0;
595 }
596
597 /*!
598     Returns true if an accessibility implementation has been requested
599     during the runtime of the application; otherwise returns false.
600
601     Use this function to prevent potentially expensive notifications via
602     updateAccessibility().
603 */
604 bool QAccessible::isActive()
605 {
606     return accessibility_active;
607 }
608
609
610
611 /*!
612   Sets the root object of the accessible objects of this application
613   to \a object.  All other accessible objects are reachable using object
614   navigation from the root object.
615
616   Normally, it isn't necessary to call this function, because Qt sets
617   the QApplication object as the root object immediately before the
618   event loop is entered in QApplication::exec().
619
620   Use QAccessible::installRootObjectHandler() to redirect the function
621   call to a customized handler function.
622
623   \sa queryAccessibleInterface()
624 */
625 void QAccessible::setRootObject(QObject *object)
626 {
627     if (rootObjectHandler) {
628         rootObjectHandler(object);
629         return;
630     }
631
632     if (QPlatformAccessibility *pfAccessibility = platformAccessibility())
633         pfAccessibility->setRootObject(object);
634 }
635
636 /*!
637   Notifies about a change that might be relevant for accessibility clients.
638
639   \a event provides details about the change. These include the source
640   of the change and the nature of the change.  The \a event should
641   contain enough information give meaningful notifications.
642
643   For example, the type \c ValueChange indicates that the position of
644   a slider has been changed.
645
646   Call this function whenever the state of your accessible object or
647   one of its sub-elements has been changed either programmatically
648   (e.g. by calling QLabel::setText()) or by user interaction.
649
650   If there are no accessibility tools listening to this event, the
651   performance penalty for calling this function is small, but if
652   determining the parameters of the call is expensive you can test
653   isActive() to avoid unnecessary computation.
654 */
655 void QAccessible::updateAccessibility(QAccessibleEvent *event)
656 {
657     if (updateHandler) {
658         updateHandler(event);
659         return;
660     }
661
662     if (!isActive())
663         return;
664
665     if (QPlatformAccessibility *pfAccessibility = platformAccessibility())
666         pfAccessibility->notifyAccessibilityUpdate(event);
667 }
668
669 /*!
670     \class QAccessibleInterface
671     \brief The QAccessibleInterface class defines an interface that exposes information
672     about accessible objects.
673     \internal
674
675     \ingroup accessibility
676     \inmodule QtGui
677
678     This class is part of \l {Accessibility for QWidget Applications}.
679
680     Accessibility tools (also called AT Clients), such as screen readers
681     or braille displays, require high-level information about
682     accessible objects in an application. Accessible objects provide
683     specialized input and output methods, making it possible for users
684     to use accessibility tools with enabled applications (AT Servers).
685
686     Every element that the user needs to interact with or react to is
687     an accessible object, and should provide this information. These
688     are mainly visual objects, such as widgets and widget elements, but
689     can also be content, such as sounds.
690
691     The AT client uses three basic concepts to acquire information
692     about any accessible object in an application:
693     \list
694     \li \e Properties The client can read information about
695     accessible objects. In some cases the client can also modify these
696     properties; such as text in a line edit.
697     \li \e Actions The client can invoke actions like pressing a button
698     or .
699     \li \e{Relationships and Navigation} The client can traverse from one
700     accessible object to another, using the relationships between objects.
701     \endlist
702
703     The QAccessibleInterface defines the API for these three concepts.
704
705     \section1 Relationships and Navigation
706
707     The functions childCount() and indexOfChild() return the number of
708     children of an accessible object and the index a child object has
709     in its parent. The childAt() function returns a child QAccessibleInterface
710     that is found at a position. The child does not have to be a direct
711     child. This allows bypassing intermediate layers when the parent already knows the
712     top-most child. childAt() is used for hit testing (finding the object
713     under the mouse).
714
715     The relations() function provides information about the relations an
716     object has to other objects, and parent() and child() allows
717     traversing from one object to another object.
718
719     \section1 Properties
720
721     The central property of an accessible objects is what role() it
722     has. Different objects can have the same role, e.g. both the "Add
723     line" element in a scroll bar and the \c OK button in a dialog have
724     the same role, "button". The role implies what kind of
725     interaction the user can perform with the user interface element.
726
727     An object's state() property is a combination of different state
728     flags and can describe both how the object's state differs from a
729     "normal" state, e.g. it might be unavailable, and also how it
730     behaves, e.g. it might be selectable.
731
732     The text() property provides textual information about the object.
733     An object usually has a name, but can provide extended information
734     such as a description, help text, or information about any
735     keyboard accelerators it provides. Some objects allow changing the
736     text() property through the setText() function, but this
737     information is in most cases read-only.
738
739     The rect() property provides information about the geometry of an
740     accessible object. This information is usually only available for
741     visual objects.
742
743     \section1 Interfaces
744
745     To enable the user to interact with an accessible object the
746     object must implement QAccessibleActionInterface in addition to
747     QAccessibleInterface.
748     Objects that support selections can define actions to change the selection.
749
750     There are several other interfaces that should be implemented as required.
751     QAccessibleTextInterface should be used for bigger texts edits such as document views.
752     This interface should not be implemented for labels/single line edits.
753     The complementary QAccessibleEditableTextInterface should be added when the
754     Text is editable.
755
756     For sliders, scrollbars and other numerical value selectors QAccessibleValueInterface
757     should be implemented.
758
759     Lists, tables and trees should implement QAccessibleTableInterface.
760
761     \sa QAccessible, QAccessibleActionInterface, QAccessibleTextInterface, QAccessibleEditableTextInterface, QAccessibleValueInterface, QAccessibleTableInterface
762 */
763
764 /*!
765     \fn bool QAccessibleInterface::isValid() const
766
767     Returns true if all the data necessary to use this interface
768     implementation is valid (e.g. all pointers are non-null);
769     otherwise returns false.
770
771     \sa object()
772 */
773
774 /*!
775     \fn QObject *QAccessibleInterface::object() const
776
777     Returns a pointer to the QObject this interface implementation provides
778     information for.
779
780     \sa isValid()
781 */
782
783 /*!
784     \fn int QAccessibleInterface::childCount() const
785
786     Returns the number of children that belong to this object. A child
787     can provide accessibility information on its own (e.g. a child
788     widget), or be a sub-element of this accessible object.
789
790     All objects provide this information.
791
792     \sa indexOfChild()
793 */
794
795 /*!
796     \fn int QAccessibleInterface::indexOfChild(const QAccessibleInterface *child) const
797
798     Returns the 0-based index of the object \a child in this object's
799     children list, or -1 if \a child is not a child of this object.
800
801     All objects provide this information about their children.
802
803     \sa childCount()
804 */
805
806 /*!
807     Returns the meaningful relations to other widgets. Usually this will not return parent/child
808     relations, unless they are handled in a specific way such as in tree views.
809     It will typically return the labelled-by and label relations.
810     It should never return itself.
811
812     \sa parent(), child()
813 */
814 QVector<QPair<QAccessibleInterface*, QAccessible::Relation> >
815 QAccessibleInterface::relations(QAccessible::Relation /*match = QAccessible::AllRelations*/) const
816 {
817     return QVector<QPair<QAccessibleInterface*, QAccessible::Relation> >();
818 }
819
820 /*!
821     Returns the object that has the keyboard focus.
822
823     The object returned can be any descendant, including itself.
824 */
825 QAccessibleInterface *QAccessibleInterface::focusChild() const
826 {
827     return 0;
828 }
829
830 /*!
831     \fn QAccessibleInterface *QAccessibleInterface::childAt(int x, int y) const
832
833     Returns the QAccessibleInterface of a child that contains the screen coordinates (\a x, \a y).
834     If there are no children at this position this function returns 0.
835     The returned accessible must be a child, but not necessarily a direct child.
836
837     This function is only relyable for visible objects (invisible
838     object might not be laid out correctly).
839
840     All visual objects provide this information.
841
842     A default implementation is provided for objects inheriting QAccessibleObject. This will iterate
843     over all children. If the widget manages its children (e.g. a table) it will be more efficient
844     to write a specialized implementation.
845
846     \sa rect()
847 */
848
849 /*!
850     \fn QAccessibleInterface* QAccessibleInterface::parent() const
851
852     Returns the QAccessibleInterface of the parent in the accessible object hierarchy.
853
854     Returns 0 if no parent exists (e.g. for the top level application object).
855
856     \sa child()
857 */
858
859 /*!
860     \fn QAccessibleInterface* QAccessibleInterface::child(int index) const
861
862     Returns the accessible child with index \a index.
863     0-based index. The number of children of an object can be checked with childCount.
864
865     Returns 0 when asking for an invalid child (e.g. when the child became invalid in the meantime).
866
867     \sa childCount(), parent()
868 */
869
870 /*!
871     \fn QString QAccessibleInterface::text(QAccessible::Text t) const
872
873     Returns the value of the text property \a t of the object.
874
875     The \l QAccessible::Name is a string used by clients to identify, find, or
876     announce an accessible object for the user. All objects must have
877     a name that is unique within their container. The name can be
878     used differently by clients, so the name should both give a
879     short description of the object and be unique.
880
881     An accessible object's \l QAccessible::Description provides textual information
882     about an object's visual appearance. The description is primarily
883     used to provide greater context for vision-impaired users, but is
884     also used for context searching or other applications. Not all
885     objects have a description. An "OK" button would not need a
886     description, but a tool button that shows a picture of a smiley
887     would.
888
889     The \l QAccessible::Value of an accessible object represents visual information
890     contained by the object, e.g. the text in a line edit. Usually,
891     the value can be modified by the user. Not all objects have a
892     value, e.g. static text labels don't, and some objects have a
893     state that already is the value, e.g. toggle buttons.
894
895     The \l QAccessible::Help text provides information about the function and
896     usage of an accessible object. Not all objects provide this
897     information.
898
899     The \l QAccessible::Accelerator is a keyboard shortcut that activates the
900     object's default action. A keyboard shortcut is the underlined
901     character in the text of a menu, menu item or widget, and is
902     either the character itself, or a combination of this character
903     and a modifier key like Alt, Ctrl or Shift. Command controls like
904     tool buttons also have shortcut keys and usually display them in
905     their tooltip.
906
907     All objects provide a string for \l QAccessible::Name.
908
909     \sa role(), state()
910 */
911
912 /*!
913     \fn void QAccessibleInterface::setText(QAccessible::Text t, const QString &text)
914
915     Sets the text property \a t of the object to \a text.
916
917     Note that the text properties of most objects are read-only
918     so calling this function might have no effect.
919
920     \sa text()
921 */
922
923 /*!
924     \fn QRect QAccessibleInterface::rect() const
925
926     Returns the geometry of the object. The geometry is in screen coordinates.
927
928     This function is only reliable for visible objects (invisible
929     objects might not be laid out correctly).
930
931     All visual objects provide this information.
932
933     \sa childAt()
934 */
935
936 /*!
937     \fn QAccessible::Role QAccessibleInterface::role() const
938
939     Returns the role of the object.
940     The role of an object is usually static.
941
942     All accessible objects have a role.
943
944     \sa text(), state()
945 */
946
947 /*!
948     \fn QAccessible::State QAccessibleInterface::state() const
949
950     Returns the current state of the object.
951     The returned value is a combination of the flags in
952     the QAccessible::StateFlag enumeration.
953
954     All accessible objects have a state.
955
956     \sa text(), role()
957 */
958
959
960 /*!
961     \fn QAccessibleEditableTextInterface *QAccessibleInterface::editableTextInterface()
962     \internal
963 */
964
965 /*!
966     Returns the accessible's foreground color if applicable or an invalid QColor.
967
968     \sa backgroundColor()
969 */
970 QColor QAccessibleInterface::foregroundColor() const
971 {
972     return QColor();
973 }
974
975 /*!
976     Returns the accessible's background color if applicable or an invalid QColor.
977
978     \sa foregroundColor()
979 */
980 QColor QAccessibleInterface::backgroundColor() const
981 {
982     return QColor();
983 }
984
985 /*!
986     \fn QAccessibleTextInterface *QAccessibleInterface::textInterface()
987     \internal
988 */
989
990 /*!
991     \fn QAccessibleValueInterface *QAccessibleInterface::valueInterface()
992     \internal
993 */
994
995 /*!
996     \fn QAccessibleTableInterface *QAccessibleInterface::tableInterface()
997     \internal
998 */
999
1000 /*!
1001     \fn QAccessibleTableCellInterface *QAccessibleInterface::tableCellInterface()
1002     \internal
1003 */
1004
1005 /*!
1006     \fn QAccessibleActionInterface *QAccessibleInterface::actionInterface()
1007     \internal
1008 */
1009
1010 /*!
1011     \fn QAccessibleImageInterface *QAccessibleInterface::imageInterface()
1012     \internal
1013 */
1014
1015 /*!
1016     \class QAccessibleEvent
1017     \internal
1018
1019     \brief The QAccessibleEvent class provides detailed updates to the
1020     accessibility framework.
1021
1022     This class is used with \l QAccessible::updateAccessibility().
1023
1024     The event type is one of the values of \l QAccessible::Event, which
1025     determines the subclass of QAccessibleEvent that applies.
1026
1027     \ingroup accessibility
1028     \inmodule QtGui
1029 */
1030
1031 /*! \fn QAccessibleEvent::QAccessibleEvent(QObject *obj, QAccessible::Event type)
1032   Constructs a QAccessibleEvent of the specified \a type. It also
1033   expects an \a object, which is the source of the event.
1034  */
1035
1036 /*! \fn QAccessibleEvent::~QAccessibleEvent()
1037   Destroys the event.
1038 */
1039
1040 /*! \fn QAccessible::Event QAccessibleEvent::type() const
1041   Returns the event type.
1042 */
1043
1044 /*! \fn QObject* QAccessibleEvent::object() const
1045   Returns the event object.
1046 */
1047
1048 /*! \fn void QAccessibleEvent::setChild(int child)
1049   Sets the child index to \a child.
1050 */
1051
1052 /*! \fn int QAccessibleEvent::child() const
1053   Returns the child index.
1054 */
1055
1056 /*!
1057     Returns the QAccessibleInterface associated with the event.
1058     The caller of this function takes ownership of the returned interface.
1059 */
1060 QAccessibleInterface *QAccessibleEvent::accessibleInterface() const
1061 {
1062     QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(m_object);
1063     if (!iface)
1064         return 0;
1065
1066     if (m_child >= 0) {
1067         QAccessibleInterface *child = iface->child(m_child);
1068         if (child) {
1069             delete iface;
1070             iface = child;
1071         } else {
1072             qWarning() << "Cannot creat accessible child interface for object: " << m_object << " index: " << m_child;
1073         }
1074     }
1075     return iface;
1076 }
1077
1078 /*!
1079     Returns the window associated with the underlying object.
1080     For instance, QAccessibleWidget reimplements this and returns
1081     the windowHandle() of the QWidget.
1082
1083     It is used on some platforms to be able to notify the AT client about
1084     state changes.
1085     The backend will traverse up all ancestors until it finds a window.
1086     (This means that at least one interface among the ancestors should
1087     return a valid QWindow pointer).
1088
1089     The default implementation of this returns 0.
1090     \preliminary
1091   */
1092 QWindow *QAccessibleInterface::window() const
1093 {
1094     return 0;
1095 }
1096
1097 /*!
1098     \internal
1099     Method to allow extending this class without breaking binary compatibility.
1100     The actual behavior and format of \a data depends on \a id argument
1101     which must be defined if the class is to be extended with another virtual
1102     function.
1103     Currently, this is unused.
1104 */
1105 void QAccessibleInterface::virtual_hook(int /*id*/, void * /*data*/)
1106 {
1107 }
1108
1109 /*!
1110     \fn void *QAccessibleInterface::interface_cast(QAccessible::InterfaceType type)
1111
1112     Returns a specialized accessibility interface \a type from the
1113     generic QAccessibleInterface.
1114
1115     This function must be reimplemented when providing more
1116     information about a widget or object through the specialized
1117     interfaces. For example a line edit should implement the
1118     QAccessibleTextInterface and QAccessibleEditableTextInterface.
1119
1120     Qt's QLineEdit for example has its accessibility support
1121     implemented in QAccessibleLineEdit.
1122
1123     \code
1124 void *QAccessibleLineEdit::interface_cast(QAccessible::InterfaceType t)
1125 {
1126     if (t == QAccessible::TextInterface)
1127         return static_cast<QAccessibleTextInterface*>(this);
1128     else if (t == QAccessible::EditableTextInterface)
1129         return static_cast<QAccessibleEditableTextInterface*>(this);
1130     return QAccessibleWidget::interface_cast(t);
1131 }
1132     \endcode
1133
1134     \sa QAccessible::InterfaceType, QAccessibleTextInterface,
1135     QAccessibleEditableTextInterface, QAccessibleValueInterface,
1136     QAccessibleActionInterface, QAccessibleTableInterface,
1137     QAccessibleTableCellInterface
1138 */
1139
1140 /*! \internal */
1141 const char *qAccessibleRoleString(QAccessible::Role role)
1142 {
1143     if (role >=0x40)
1144          role = QAccessible::UserRole;
1145     static int roleEnum = QAccessible::staticMetaObject.indexOfEnumerator("Role");
1146     return QAccessible::staticMetaObject.enumerator(roleEnum).valueToKey(role);
1147 }
1148
1149 /*! \internal */
1150 const char *qAccessibleEventString(QAccessible::Event event)
1151 {
1152     static int eventEnum = QAccessible::staticMetaObject.indexOfEnumerator("Event");
1153     return QAccessible::staticMetaObject.enumerator(eventEnum).valueToKey(event);
1154 }
1155
1156 /*! \internal */
1157 bool operator==(const QAccessible::State &first, const QAccessible::State &second)
1158 {
1159     return memcmp(&first, &second, sizeof(QAccessible::State)) == 0;
1160 }
1161
1162 #ifndef QT_NO_DEBUG_STREAM
1163 Q_GUI_EXPORT QDebug operator<<(QDebug d, const QAccessibleInterface *iface)
1164 {
1165     if (!iface) {
1166         d << "QAccessibleInterface(null)";
1167         return d;
1168     }
1169     d.nospace();
1170     d << "QAccessibleInterface(" << hex << (void *) iface << dec;
1171     if (iface->isValid()) {
1172         d << " name=" << iface->text(QAccessible::Name) << " ";
1173         d << "role=" << qAccessibleRoleString(iface->role()) << " ";
1174         if (iface->childCount())
1175             d << "childc=" << iface->childCount() << " ";
1176         if (iface->object()) {
1177             d << "obj=" << iface->object();
1178         }
1179         QStringList stateStrings;
1180         QAccessible::State st = iface->state();
1181         if (st.focusable)
1182             stateStrings << QLatin1String("focusable");
1183         if (st.focused)
1184             stateStrings << QLatin1String("focused");
1185         if (st.selected)
1186             stateStrings << QLatin1String("selected");
1187         if (st.invisible)
1188             stateStrings << QLatin1String("invisible");
1189
1190         if (!stateStrings.isEmpty())
1191             d << stateStrings.join(QLatin1Char('|'));
1192
1193         if (!st.invisible)
1194             d << "rect=" << iface->rect();
1195
1196     } else {
1197         d << " invalid";
1198     }
1199     d << ")";
1200     return d.space();
1201 }
1202
1203 /*! \internal */
1204 QDebug operator<<(QDebug d, const QAccessibleEvent &ev)
1205 {
1206     if (!&ev) {
1207         d << "QAccessibleEvent(null)";
1208         return d;
1209     }
1210     d.nospace() << "QAccessibleEvent(object=" << hex << ev.object();
1211     d.nospace() << dec;
1212     d.nospace() << "child=" << ev.child();
1213     d << " event=" << qAccessibleEventString(ev.type());
1214     if (ev.type() == QAccessible::StateChanged) {
1215         QAccessible::State changed = static_cast<const QAccessibleStateChangeEvent*>(&ev)->changedStates();
1216         d << "State changed:";
1217         if (changed.disabled) d << "disabled";
1218         if (changed.selected) d << "selected";
1219         if (changed.focusable) d << "focusable";
1220         if (changed.focused) d << "focused";
1221         if (changed.pressed) d << "pressed";
1222         if (changed.checkable) d << "checkable";
1223         if (changed.checked) d << "checked";
1224         if (changed.checkStateMixed) d << "checkStateMixed";
1225         if (changed.readOnly) d << "readOnly";
1226         if (changed.hotTracked) d << "hotTracked";
1227         if (changed.defaultButton) d << "defaultButton";
1228         if (changed.expanded) d << "expanded";
1229         if (changed.collapsed) d << "collapsed";
1230         if (changed.busy) d << "busy";
1231         if (changed.expandable) d << "expandable";
1232         if (changed.marqueed) d << "marqueed";
1233         if (changed.animated) d << "animated";
1234         if (changed.invisible) d << "invisible";
1235         if (changed.offscreen) d << "offscreen";
1236         if (changed.sizeable) d << "sizeable";
1237         if (changed.movable) d << "movable";
1238         if (changed.selfVoicing) d << "selfVoicing";
1239         if (changed.selectable) d << "selectable";
1240         if (changed.linked) d << "linked";
1241         if (changed.traversed) d << "traversed";
1242         if (changed.multiSelectable) d << "multiSelectable";
1243         if (changed.extSelectable) d << "extSelectable";
1244         if (changed.passwordEdit) d << "passwordEdit"; // used to be Protected
1245         if (changed.hasPopup) d << "hasPopup";
1246         if (changed.modal) d << "modal";
1247
1248         // IA2 - we chose to not add some IA2 states for now
1249         // Below the ones that seem helpful
1250         if (changed.active) d << "active";
1251         if (changed.invalid) d << "invalid"; // = defunct
1252         if (changed.editable) d << "editable";
1253         if (changed.multiLine) d << "multiLine";
1254         if (changed.selectableText) d << "selectableText";
1255         if (changed.supportsAutoCompletion) d << "supportsAutoCompletion";
1256
1257     }
1258     d.nospace() << ")";
1259     return d.space();
1260 }
1261
1262 #endif
1263
1264 QT_END_NAMESPACE
1265