summaryrefslogtreecommitdiff
path: root/accessible/xul/XULSliderAccessible.cpp
blob: 476cb17ebf52044de5ef0c2ae1cb5a5b4b1218db (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "XULSliderAccessible.h"

#include "nsAccessibilityService.h"
#include "Role.h"
#include "States.h"

#include "nsIFrame.h"
#include "mozilla/dom/Element.h"
#include "mozilla/FloatingPoint.h"

using namespace mozilla::a11y;

////////////////////////////////////////////////////////////////////////////////
// XULSliderAccessible
////////////////////////////////////////////////////////////////////////////////

XULSliderAccessible::
  XULSliderAccessible(nsIContent* aContent, DocAccessible* aDoc) :
  AccessibleWrap(aContent, aDoc)
{
  mStateFlags |= eHasNumericValue | eNoXBLKids;
}

// Accessible

role
XULSliderAccessible::NativeRole()
{
  return roles::SLIDER;
}

uint64_t
XULSliderAccessible::NativeInteractiveState() const
 {
  if (NativelyUnavailable())
    return states::UNAVAILABLE;

  nsIContent* sliderElm = GetSliderElement();
  if (sliderElm) {
    nsIFrame* frame = sliderElm->GetPrimaryFrame();
    if (frame && frame->IsFocusable())
      return states::FOCUSABLE;
  }

  return 0;
}

bool
XULSliderAccessible::NativelyUnavailable() const
{
  return mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
                               nsGkAtoms::_true, eCaseMatters);
}

void
XULSliderAccessible::Value(nsString& aValue)
{
  GetSliderAttr(nsGkAtoms::curpos, aValue);
}

uint8_t
XULSliderAccessible::ActionCount()
{
  return 1;
}

void
XULSliderAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName)
{
  aName.Truncate();
  if (aIndex == 0)
    aName.AssignLiteral("activate");
}

bool
XULSliderAccessible::DoAction(uint8_t aIndex)
{
  if (aIndex != 0)
    return false;

  nsIContent* sliderElm = GetSliderElement();
  if (sliderElm)
    DoCommand(sliderElm);

  return true;
}

double
XULSliderAccessible::MaxValue() const
{
  double value = AccessibleWrap::MaxValue();
  return IsNaN(value) ? GetSliderAttr(nsGkAtoms::maxpos) : value;
}

double
XULSliderAccessible::MinValue() const
{
  double value = AccessibleWrap::MinValue();
  return IsNaN(value) ? GetSliderAttr(nsGkAtoms::minpos) : value;
}

double
XULSliderAccessible::Step() const
{
  double value = AccessibleWrap::Step();
  return IsNaN(value) ? GetSliderAttr(nsGkAtoms::increment) : value;
}

double
XULSliderAccessible::CurValue() const
{
  double value = AccessibleWrap::CurValue();
  return IsNaN(value) ? GetSliderAttr(nsGkAtoms::curpos) : value;
}

bool
XULSliderAccessible::SetCurValue(double aValue)
{
  if (AccessibleWrap::SetCurValue(aValue))
    return true;

  return SetSliderAttr(nsGkAtoms::curpos, aValue);
}

// Utils

nsIContent*
XULSliderAccessible::GetSliderElement() const
{
  if (!mSliderNode) {
    // XXX: we depend on anonymous content.
    mSliderNode = mContent->OwnerDoc()->
      GetAnonymousElementByAttribute(mContent, nsGkAtoms::anonid,
                                     NS_LITERAL_STRING("slider"));
  }

  return mSliderNode;
}

nsresult
XULSliderAccessible::GetSliderAttr(nsIAtom* aName, nsAString& aValue) const
{
  aValue.Truncate();

  if (IsDefunct())
    return NS_ERROR_FAILURE;

  nsIContent* sliderElm = GetSliderElement();
  if (sliderElm)
    sliderElm->GetAttr(kNameSpaceID_None, aName, aValue);

  return NS_OK;
}

nsresult
XULSliderAccessible::SetSliderAttr(nsIAtom* aName, const nsAString& aValue)
{
  if (IsDefunct())
    return NS_ERROR_FAILURE;

  nsIContent* sliderElm = GetSliderElement();
  if (sliderElm)
    sliderElm->SetAttr(kNameSpaceID_None, aName, aValue, true);

  return NS_OK;
}

double
XULSliderAccessible::GetSliderAttr(nsIAtom* aName) const
{
  nsAutoString attrValue;
  nsresult rv = GetSliderAttr(aName, attrValue);
  if (NS_FAILED(rv))
    return UnspecifiedNaN<double>();

  nsresult error = NS_OK;
  double value = attrValue.ToDouble(&error);
  return NS_FAILED(error) ? UnspecifiedNaN<double>() : value;
}

bool
XULSliderAccessible::SetSliderAttr(nsIAtom* aName, double aValue)
{
  nsAutoString value;
  value.AppendFloat(aValue);

  return NS_SUCCEEDED(SetSliderAttr(aName, value));
}


////////////////////////////////////////////////////////////////////////////////
// XULThumbAccessible
////////////////////////////////////////////////////////////////////////////////

XULThumbAccessible::
  XULThumbAccessible(nsIContent* aContent, DocAccessible* aDoc) :
  AccessibleWrap(aContent, aDoc)
{
}

////////////////////////////////////////////////////////////////////////////////
// XULThumbAccessible: Accessible

role
XULThumbAccessible::NativeRole()
{
  return roles::INDICATOR;
}