1/*
2 * Copyright (C) 2008 Nuanti Ltd.
3 * Copyright (C) 2009 Jan Alonzo
4 * Copyright (C) 2009, 2012 Igalia S.L.
5 *
6 * Portions from Mozilla a11y, copyright as follows:
7 *
8 * The Original Code is mozilla.org code.
9 *
10 * The Initial Developer of the Original Code is
11 * Sun Microsystems, Inc.
12 * Portions created by the Initial Developer are Copyright (C) 2002
13 * the Initial Developer. All Rights Reserved.
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Library General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23 * Library General Public License for more details.
24 *
25 * You should have received a copy of the GNU Library General Public License
26 * along with this library; see the file COPYING.LIB.  If not, write to
27 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 * Boston, MA 02110-1301, USA.
29 */
30
31#include "config.h"
32#include "WebKitAccessibleInterfaceComponent.h"
33
34#if HAVE(ACCESSIBILITY)
35
36#include "AccessibilityObject.h"
37#include "FrameView.h"
38#include "IntRect.h"
39#include "WebKitAccessibleUtil.h"
40#include "WebKitAccessibleWrapperAtk.h"
41
42using namespace WebCore;
43
44static AccessibilityObject* core(AtkComponent* component)
45{
46    if (!WEBKIT_IS_ACCESSIBLE(component))
47        return 0;
48
49    return webkitAccessibleGetAccessibilityObject(WEBKIT_ACCESSIBLE(component));
50}
51
52static IntPoint atkToContents(AccessibilityObject* coreObject, AtkCoordType coordType, gint x, gint y)
53{
54    IntPoint pos(x, y);
55
56    FrameView* frameView = coreObject->documentFrameView();
57    if (frameView) {
58        switch (coordType) {
59        case ATK_XY_SCREEN:
60            return frameView->screenToContents(pos);
61        case ATK_XY_WINDOW:
62            return frameView->windowToContents(pos);
63        }
64    }
65
66    return pos;
67}
68
69static AtkObject* webkitAccessibleComponentRefAccessibleAtPoint(AtkComponent* component, gint x, gint y, AtkCoordType coordType)
70{
71    g_return_val_if_fail(ATK_IS_COMPONENT(component), 0);
72    returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(component), 0);
73
74    IntPoint pos = atkToContents(core(component), coordType, x, y);
75
76    AccessibilityObject* target = core(component)->accessibilityHitTest(pos);
77    if (!target)
78        return 0;
79    g_object_ref(target->wrapper());
80    return target->wrapper();
81}
82
83static void webkitAccessibleComponentGetExtents(AtkComponent* component, gint* x, gint* y, gint* width, gint* height, AtkCoordType coordType)
84{
85    g_return_if_fail(ATK_IS_COMPONENT(component));
86    returnIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(component));
87
88    IntRect rect = pixelSnappedIntRect(core(component)->elementRect());
89    contentsRelativeToAtkCoordinateType(core(component), coordType, rect, x, y, width, height);
90}
91
92static gboolean webkitAccessibleComponentGrabFocus(AtkComponent* component)
93{
94    g_return_val_if_fail(ATK_IS_COMPONENT(component), FALSE);
95    returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(component), FALSE);
96
97    core(component)->setFocused(true);
98    return core(component)->isFocused();
99}
100
101void webkitAccessibleComponentInterfaceInit(AtkComponentIface* iface)
102{
103    iface->ref_accessible_at_point = webkitAccessibleComponentRefAccessibleAtPoint;
104    iface->get_extents = webkitAccessibleComponentGetExtents;
105    iface->grab_focus = webkitAccessibleComponentGrabFocus;
106}
107
108#endif
109