summaryrefslogtreecommitdiff
path: root/src/gui/accessible/qaccessible_mac_p.h
blob: 4ee3a2d9968c9ab8e0787995b7bded498babbadf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QACCESSIBLE_MAC_P_H
#define QACCESSIBLE_MAC_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
//

#include <qglobal.h>
#include <private/qt_mac_p.h>
#include <qaccessible.h>
#include <qwidget.h>
#include <qdebug.h>

//#define Q_ACCESSIBLE_MAC_DEBUG

QT_BEGIN_NAMESPACE

/*
    QAccessibleInterfaceWrapper wraps QAccessibleInterface and adds
    a ref count. QAccessibleInterfaceWrapper is a "by-value" class.
*/
class Q_AUTOTEST_EXPORT QAccessibleInterfaceWrapper
{
public:
    QAccessibleInterfaceWrapper()
    : interface(0), childrenIsRegistered(new bool(false)), refCount(new int(1)) { }

    QAccessibleInterfaceWrapper(QAccessibleInterface *interface)
    :interface(interface), childrenIsRegistered(new bool(false)), refCount(new int(1))  { }

    ~QAccessibleInterfaceWrapper()
    {
        if (--(*refCount) == 0) {
            delete interface;
            delete refCount;
            delete childrenIsRegistered;
        }
    }

    QAccessibleInterfaceWrapper(const QAccessibleInterfaceWrapper &other)
    :interface(other.interface), childrenIsRegistered(other.childrenIsRegistered), refCount(other.refCount)
    {
        ++(*refCount);
    }

    void operator=(const QAccessibleInterfaceWrapper &other)
    {
        if (other.interface == interface)
            return;

        if (--(*refCount) == 0) {
            delete interface;
            delete refCount;
            delete childrenIsRegistered;
        }

        interface = other.interface;
        childrenIsRegistered = other.childrenIsRegistered;
        refCount = other.refCount;
        ++(*refCount);
    }

    QAccessibleInterface *interface;
    bool *childrenIsRegistered;
private:
    int *refCount;
};

/*
    QAInterface represents one accessiblity item. It hides the fact that
    one QAccessibleInterface may represent more than one item, and it also
    automates the memory management for QAccessibleInterfaces using the
    QAccessibleInterfaceWrapper wrapper class.

    It has the same API as QAccessibleInterface, minus the child parameter
    in the funcitons.
*/
class Q_AUTOTEST_EXPORT QAInterface : public QAccessible
{
public:
    QAInterface()
    : base(QAccessibleInterfaceWrapper())
    { }

    QAInterface(QAccessibleInterface *interface, int child = 0)
    {
        if (interface == 0 || child > interface->childCount()) {
           base = QAccessibleInterfaceWrapper(); 
        } else {
            base = QAccessibleInterfaceWrapper(interface);
            m_cachedObject = interface->object();
            this->child = child;
        }
    }

    QAInterface(QAccessibleInterfaceWrapper wrapper, int child = 0)
    :base(wrapper), m_cachedObject(wrapper.interface->object()), child(child)
    { }

    QAInterface(const QAInterface &other, int child)
    {
        if (other.isValid() == false || child > other.childCount()) {
           base = QAccessibleInterfaceWrapper();
        } else {
            base = other.base;
            m_cachedObject = other.m_cachedObject;
            this->child = child;
        }
    }

    bool operator==(const QAInterface &other) const;
    bool operator!=(const QAInterface &other) const;

    inline QString actionText (int action, Text text) const
    { return base.interface->actionText(action, text, child); }

    QAInterface childAt(int x, int y) const
    {
        if (!checkValid())
            return QAInterface();

        const int foundChild = base.interface->childAt(x, y);

        if (foundChild == -1)
            return QAInterface();

        if (child == 0)
            return navigate(QAccessible::Child, foundChild);

        if (foundChild == child)
            return *this;
        return QAInterface();
    }

    int indexOfChild(const QAInterface &child) const
    {
        if (!checkValid())
            return -1;

        if (*this != child.parent())
            return -1;

        if (object() == child.object())
            return child.id();

        return base.interface->indexOfChild(child.base.interface);
    }

    inline int childCount() const
    {
        if (!checkValid())
            return 0;

        if (child != 0)
            return 0;
        return base.interface->childCount();
    }

    QList<QAInterface> children() const
    {
        if (!checkValid())
            return QList<QAInterface>();

        QList<QAInterface> children;
        for (int i = 1; i <= childCount(); ++i) {
            children.append(navigate(QAccessible::Child, i));
        }
        return children;
    }

    QAInterface childAt(int index) const
    {
        return navigate(QAccessible::Child, index);
    }

    inline void doAction(int action, const QVariantList &params = QVariantList()) const
    {
        if (!checkValid())
            return;

        base.interface->doAction(action, child, params);
    }

    QAInterface navigate(RelationFlag relation, int entry) const;

    inline QObject * object() const
    {
        if (!checkValid())
            return 0;

        return base.interface->object();
    }

    QAInterface objectInterface() const
    {
        if (!checkValid())
            return QAInterface();

        QObject *obj = object();
        QAInterface current = *this;
        while (obj == 0)
        {
            QAInterface parent = current.parent();
            if (parent.isValid() == false)
                break;
            obj = parent.object();
            current = parent;
        }
        return current;
    }

    inline HIObjectRef hiObject() const
    {
        if (!checkValid())
            return 0;
        QWidget * const widget = qobject_cast<QWidget * const>(object());
        if (widget)
            return (HIObjectRef)widget->winId();
        else
            return 0;
    }

    inline QObject * cachedObject() const
    {
        if (!checkValid())
            return 0;
        return m_cachedObject;
    }

    inline QRect rect() const
    {
        if (!checkValid())
            return QRect();
        return base.interface->rect(child);
    }

    inline Role role() const
    {
        if (!checkValid())
            return QAccessible::NoRole;
        return base.interface->role(child);
    }

    inline void setText(Text t, const QString &text) const
    {
        if (!checkValid())
            return;
        base.interface->setText(t, child, text);
    }

    inline State state() const
    {
        if (!checkValid())
            return 0;
        return base.interface->state(child);
    }

    inline QString text (Text text) const
    {
        if (!checkValid())
            return QString();
        return base.interface->text(text, child);
    }

    inline QString value() const
    { return text(QAccessible::Value); }

    inline QString name() const
    { return text(QAccessible::Name); }

    inline int userActionCount() const
    {
        if (!checkValid())
            return 0;
        return base.interface->userActionCount(child);
    }

    inline QString className() const
    {
        if (!checkValid())
            return QString();
        return QLatin1String(base.interface->object()->metaObject()->className());
    }

    inline bool isHIView() const
    { return (child == 0 && object() != 0); }

    inline int id() const
    { return child; }

    inline bool isValid() const
    {
        return (base.interface != 0 && base.interface->isValid());
    }

    QAInterface parent() const
    { return navigate(QAccessible::Ancestor, 1); }

    QAccessibleInterfaceWrapper interfaceWrapper() const
    { return base; }

protected:
    bool checkValid() const
    {
        const bool valid = isValid();
#ifdef Q_ACCESSIBLE_MAC_DEBUG
        if (!valid)
            qFatal("QAccessible_mac: tried to use invalid interface.");
#endif
        return valid;
    }

    QAccessibleInterfaceWrapper base;
    QObject *m_cachedObject;
    int child;
};

Q_AUTOTEST_EXPORT QDebug operator<<(QDebug debug, const QAInterface &interface);

/*
    QAElement is a thin wrapper around an AXUIElementRef that automates
    the ref-counting.
*/
class Q_AUTOTEST_EXPORT QAElement
{
public:
    QAElement();
    explicit QAElement(AXUIElementRef elementRef);
    QAElement(const QAElement &element);
    QAElement(HIObjectRef, int child);
    ~QAElement();

    inline HIObjectRef object() const
    {
#ifndef Q_WS_MAC64
        return AXUIElementGetHIObject(elementRef);
#else
        return 0;
#endif
    }

    inline int id() const
    {
        UInt64 theId;
#ifndef QT_MAC_USE_COCOA
        AXUIElementGetIdentifier(elementRef, &theId);
#else
        theId = 0;
#endif
        return theId;
    }

    inline AXUIElementRef element() const
    {
        return elementRef;
    }

    inline bool isValid() const
    {
        return (elementRef != 0);
    }

    void operator=(const QAElement &other);
    bool operator==(const QAElement &other) const;
private:
    AXUIElementRef elementRef;
};


class QInterfaceFactory
{
public:
    virtual QAInterface interface(UInt64 identifier) = 0;
    virtual QAElement element(int id) = 0;
    virtual QAElement element(const QAInterface &interface)
    {
        return element(interface.id());
    }
    virtual void registerChildren() = 0;
    virtual ~QInterfaceFactory() {}
};

/*
    QAccessibleHierarchyManager bridges the Mac and Qt accessibility hierarchies.
    There is a one-to-one relationship between QAElements on the Mac side
    and QAInterfaces on the Qt side, and this class provies lookup funcitons
    that translates between these to items.

    The identity of a QAInterface is determined by its QAccessibleInterface and
    child identifier, and the identity of a QAElement is determined by its
    HIObjectRef and identifier.

    QAccessibleHierarchyManager receives QObject::destroyed() signals and deletes
    the accessibility objects for destroyed objects.
*/
class Q_AUTOTEST_EXPORT QAccessibleHierarchyManager : public QObject
{
Q_OBJECT
public:
    ~QAccessibleHierarchyManager() { reset(); }
    static QAccessibleHierarchyManager *instance();
    void reset();

    QAElement registerInterface(QObject *object, int child);
    QAElement registerInterface(const QAInterface &interface);
    void registerInterface(QObject *object, HIObjectRef hiobject, QInterfaceFactory *interfaceFactory);

    void registerChildren(const QAInterface &interface);

    QAInterface lookup(const AXUIElementRef &element);
    QAInterface lookup(const QAElement &element);
    QAElement lookup(const QAInterface &interface);
    QAElement lookup(QObject * const object, int id);
private slots:
    void objectDestroyed(QObject *);
private:
    typedef QHash<QObject *, QInterfaceFactory *> QObjectElementHash;
    typedef QHash<HIObjectRef, QInterfaceFactory *> HIObjectInterfaceHash;
    typedef QHash<QObject *, HIObjectRef> QObjectHIObjectHash;

    QObjectElementHash qobjectElementHash;
    HIObjectInterfaceHash hiobjectInterfaceHash;
    QObjectHIObjectHash qobjectHiobjectHash;
};

Q_AUTOTEST_EXPORT bool isItInteresting(const QAInterface &interface);

QT_END_NAMESPACE

#endif