1/*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "AccessibilitySpinButton.h"
28
29#include "AXObjectCache.h"
30#include "RenderElement.h"
31
32namespace WebCore {
33
34PassRefPtr<AccessibilitySpinButton> AccessibilitySpinButton::create()
35{
36    return adoptRef(new AccessibilitySpinButton);
37}
38
39AccessibilitySpinButton::AccessibilitySpinButton()
40    : m_spinButtonElement(0)
41{
42}
43
44AccessibilitySpinButton::~AccessibilitySpinButton()
45{
46}
47
48AccessibilityObject* AccessibilitySpinButton::incrementButton()
49{
50    if (!m_haveChildren)
51        addChildren();
52
53    ASSERT(m_children.size() == 2);
54
55    return m_children[0].get();
56}
57
58AccessibilityObject* AccessibilitySpinButton::decrementButton()
59{
60    if (!m_haveChildren)
61        addChildren();
62
63    ASSERT(m_children.size() == 2);
64
65    return m_children[1].get();
66}
67
68LayoutRect AccessibilitySpinButton::elementRect() const
69{
70    ASSERT(m_spinButtonElement);
71
72    if (!m_spinButtonElement || !m_spinButtonElement->renderer())
73        return LayoutRect();
74
75    Vector<FloatQuad> quads;
76    m_spinButtonElement->renderer()->absoluteFocusRingQuads(quads);
77
78    return boundingBoxForQuads(m_spinButtonElement->renderer(), quads);
79}
80
81void AccessibilitySpinButton::addChildren()
82{
83    m_haveChildren = true;
84
85    AccessibilitySpinButtonPart* incrementor = toAccessibilitySpinButtonPart(axObjectCache()->getOrCreate(SpinButtonPartRole));
86    incrementor->setIsIncrementor(true);
87    incrementor->setParent(this);
88    m_children.append(incrementor);
89
90    AccessibilitySpinButtonPart* decrementor = toAccessibilitySpinButtonPart(axObjectCache()->getOrCreate(SpinButtonPartRole));
91    decrementor->setIsIncrementor(false);
92    decrementor->setParent(this);
93    m_children.append(decrementor);
94}
95
96void AccessibilitySpinButton::step(int amount)
97{
98    ASSERT(m_spinButtonElement);
99    if (!m_spinButtonElement)
100        return;
101
102    m_spinButtonElement->step(amount);
103}
104
105// AccessibilitySpinButtonPart
106
107AccessibilitySpinButtonPart::AccessibilitySpinButtonPart()
108    : m_isIncrementor(false)
109{
110}
111
112PassRefPtr<AccessibilitySpinButtonPart> AccessibilitySpinButtonPart::create()
113{
114    return adoptRef(new AccessibilitySpinButtonPart);
115}
116
117LayoutRect AccessibilitySpinButtonPart::elementRect() const
118{
119    // FIXME: This logic should exist in the render tree or elsewhere, but there is no
120    // relationship that exists that can be queried.
121
122    LayoutRect parentRect = parentObject()->elementRect();
123    if (m_isIncrementor)
124        parentRect.setHeight(parentRect.height() / 2);
125    else {
126        parentRect.setY(parentRect.y() + parentRect.height() / 2);
127        parentRect.setHeight(parentRect.height() / 2);
128    }
129
130    return parentRect;
131}
132
133bool AccessibilitySpinButtonPart::press()
134{
135    if (!m_parent || !m_parent->isSpinButton())
136        return false;
137
138    AccessibilitySpinButton* spinButton = toAccessibilitySpinButton(parentObject());
139    if (m_isIncrementor)
140        spinButton->step(1);
141    else
142        spinButton->step(-1);
143
144    return true;
145}
146
147} // namespace WebCore
148