1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the plugins 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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qiosplatformaccessibility.h"
41#include "quiaccessibilityelement.h"
42
43#include <QtGui/private/qguiapplication_p.h>
44
45@implementation QUIView (Accessibility)
46
47- (void)createAccessibleElement:(QAccessibleInterface *)iface
48{
49    if (!iface || iface->state().invisible || (iface->text(QAccessible::Name).isEmpty() && iface->text(QAccessible::Value).isEmpty() && iface->text(QAccessible::Description).isEmpty()))
50        return;
51    QAccessible::Id accessibleId = QAccessible::uniqueId(iface);
52    UIAccessibilityElement *elem = [[QT_MANGLE_NAMESPACE(QMacAccessibilityElement) alloc] initWithId:accessibleId withAccessibilityContainer:self];
53    [m_accessibleElements addObject:elem];
54    [elem release];
55}
56
57- (void)createAccessibleContainer:(QAccessibleInterface *)iface
58{
59    if (!iface)
60        return;
61
62    [self createAccessibleElement: iface];
63    for (int i = 0; i < iface->childCount(); ++i)
64        [self createAccessibleContainer: iface->child(i)];
65}
66
67- (void)initAccessibility
68{
69    static bool init = false;
70    if (!init)
71        QGuiApplicationPrivate::platformIntegration()->accessibility()->setActive(true);
72    init = true;
73
74    if ([m_accessibleElements count])
75        return;
76
77    QWindow *win = self.platformWindow->window();
78    QAccessibleInterface *iface = win->accessibleRoot();
79    if (iface)
80        [self createAccessibleContainer: iface];
81}
82
83- (void)clearAccessibleCache
84{
85    [m_accessibleElements removeAllObjects];
86    UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, @"");
87}
88
89// this is a container, returning yes here means the functions below will never be called
90- (BOOL)isAccessibilityElement
91{
92    return NO;
93}
94
95- (NSInteger)accessibilityElementCount
96{
97    [self initAccessibility];
98    return [m_accessibleElements count];
99}
100
101- (id)accessibilityElementAtIndex:(NSInteger)index
102{
103    [self initAccessibility];
104    if (NSUInteger(index) >= [m_accessibleElements count])
105        return nil;
106    return m_accessibleElements[index];
107}
108
109- (NSInteger)indexOfAccessibilityElement:(id)element
110{
111    [self initAccessibility];
112    return [m_accessibleElements indexOfObject:element];
113}
114
115- (NSArray *)accessibilityElements
116{
117    [self initAccessibility];
118    return m_accessibleElements;
119}
120
121@end
122