Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "XULSliderAccessible.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsAccessibilityService.h" |
michael@0 | 9 | #include "Role.h" |
michael@0 | 10 | #include "States.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "nsIFrame.h" |
michael@0 | 13 | #include "mozilla/dom/Element.h" |
michael@0 | 14 | #include "mozilla/FloatingPoint.h" |
michael@0 | 15 | |
michael@0 | 16 | using namespace mozilla::a11y; |
michael@0 | 17 | |
michael@0 | 18 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 19 | // XULSliderAccessible |
michael@0 | 20 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 21 | |
michael@0 | 22 | XULSliderAccessible:: |
michael@0 | 23 | XULSliderAccessible(nsIContent* aContent, DocAccessible* aDoc) : |
michael@0 | 24 | AccessibleWrap(aContent, aDoc) |
michael@0 | 25 | { |
michael@0 | 26 | mStateFlags |= eHasNumericValue; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | // Accessible |
michael@0 | 30 | |
michael@0 | 31 | role |
michael@0 | 32 | XULSliderAccessible::NativeRole() |
michael@0 | 33 | { |
michael@0 | 34 | return roles::SLIDER; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | uint64_t |
michael@0 | 38 | XULSliderAccessible::NativeInteractiveState() const |
michael@0 | 39 | { |
michael@0 | 40 | if (NativelyUnavailable()) |
michael@0 | 41 | return states::UNAVAILABLE; |
michael@0 | 42 | |
michael@0 | 43 | nsIContent* sliderElm = GetSliderElement(); |
michael@0 | 44 | if (sliderElm) { |
michael@0 | 45 | nsIFrame* frame = sliderElm->GetPrimaryFrame(); |
michael@0 | 46 | if (frame && frame->IsFocusable()) |
michael@0 | 47 | return states::FOCUSABLE; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | return 0; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | bool |
michael@0 | 54 | XULSliderAccessible::NativelyUnavailable() const |
michael@0 | 55 | { |
michael@0 | 56 | return mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled, |
michael@0 | 57 | nsGkAtoms::_true, eCaseMatters); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | // nsIAccessible |
michael@0 | 61 | |
michael@0 | 62 | void |
michael@0 | 63 | XULSliderAccessible::Value(nsString& aValue) |
michael@0 | 64 | { |
michael@0 | 65 | GetSliderAttr(nsGkAtoms::curpos, aValue); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | uint8_t |
michael@0 | 69 | XULSliderAccessible::ActionCount() |
michael@0 | 70 | { |
michael@0 | 71 | return 1; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | NS_IMETHODIMP |
michael@0 | 75 | XULSliderAccessible::GetActionName(uint8_t aIndex, nsAString& aName) |
michael@0 | 76 | { |
michael@0 | 77 | aName.Truncate(); |
michael@0 | 78 | |
michael@0 | 79 | NS_ENSURE_ARG(aIndex == 0); |
michael@0 | 80 | |
michael@0 | 81 | aName.AssignLiteral("activate"); |
michael@0 | 82 | return NS_OK; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | NS_IMETHODIMP |
michael@0 | 86 | XULSliderAccessible::DoAction(uint8_t aIndex) |
michael@0 | 87 | { |
michael@0 | 88 | NS_ENSURE_ARG(aIndex == 0); |
michael@0 | 89 | |
michael@0 | 90 | nsIContent* sliderElm = GetSliderElement(); |
michael@0 | 91 | if (sliderElm) |
michael@0 | 92 | DoCommand(sliderElm); |
michael@0 | 93 | |
michael@0 | 94 | return NS_OK; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | double |
michael@0 | 98 | XULSliderAccessible::MaxValue() const |
michael@0 | 99 | { |
michael@0 | 100 | double value = AccessibleWrap::MaxValue(); |
michael@0 | 101 | return IsNaN(value) ? GetSliderAttr(nsGkAtoms::maxpos) : value; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | double |
michael@0 | 105 | XULSliderAccessible::MinValue() const |
michael@0 | 106 | { |
michael@0 | 107 | double value = AccessibleWrap::MinValue(); |
michael@0 | 108 | return IsNaN(value) ? GetSliderAttr(nsGkAtoms::minpos) : value; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | double |
michael@0 | 112 | XULSliderAccessible::Step() const |
michael@0 | 113 | { |
michael@0 | 114 | double value = AccessibleWrap::Step(); |
michael@0 | 115 | return IsNaN(value) ? GetSliderAttr(nsGkAtoms::increment) : value; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | double |
michael@0 | 119 | XULSliderAccessible::CurValue() const |
michael@0 | 120 | { |
michael@0 | 121 | double value = AccessibleWrap::CurValue(); |
michael@0 | 122 | return IsNaN(value) ? GetSliderAttr(nsGkAtoms::curpos) : value; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | bool |
michael@0 | 126 | XULSliderAccessible::SetCurValue(double aValue) |
michael@0 | 127 | { |
michael@0 | 128 | if (AccessibleWrap::SetCurValue(aValue)) |
michael@0 | 129 | return true; |
michael@0 | 130 | |
michael@0 | 131 | return SetSliderAttr(nsGkAtoms::curpos, aValue); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | bool |
michael@0 | 135 | XULSliderAccessible::CanHaveAnonChildren() |
michael@0 | 136 | { |
michael@0 | 137 | // Do not allow anonymous xul:slider be accessible. |
michael@0 | 138 | return false; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | // Utils |
michael@0 | 142 | |
michael@0 | 143 | nsIContent* |
michael@0 | 144 | XULSliderAccessible::GetSliderElement() const |
michael@0 | 145 | { |
michael@0 | 146 | if (!mSliderNode) { |
michael@0 | 147 | // XXX: we depend on anonymous content. |
michael@0 | 148 | mSliderNode = mContent->OwnerDoc()-> |
michael@0 | 149 | GetAnonymousElementByAttribute(mContent, nsGkAtoms::anonid, |
michael@0 | 150 | NS_LITERAL_STRING("slider")); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | return mSliderNode; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | nsresult |
michael@0 | 157 | XULSliderAccessible::GetSliderAttr(nsIAtom* aName, nsAString& aValue) const |
michael@0 | 158 | { |
michael@0 | 159 | aValue.Truncate(); |
michael@0 | 160 | |
michael@0 | 161 | if (IsDefunct()) |
michael@0 | 162 | return NS_ERROR_FAILURE; |
michael@0 | 163 | |
michael@0 | 164 | nsIContent* sliderElm = GetSliderElement(); |
michael@0 | 165 | if (sliderElm) |
michael@0 | 166 | sliderElm->GetAttr(kNameSpaceID_None, aName, aValue); |
michael@0 | 167 | |
michael@0 | 168 | return NS_OK; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | nsresult |
michael@0 | 172 | XULSliderAccessible::SetSliderAttr(nsIAtom* aName, const nsAString& aValue) |
michael@0 | 173 | { |
michael@0 | 174 | if (IsDefunct()) |
michael@0 | 175 | return NS_ERROR_FAILURE; |
michael@0 | 176 | |
michael@0 | 177 | nsIContent* sliderElm = GetSliderElement(); |
michael@0 | 178 | if (sliderElm) |
michael@0 | 179 | sliderElm->SetAttr(kNameSpaceID_None, aName, aValue, true); |
michael@0 | 180 | |
michael@0 | 181 | return NS_OK; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | double |
michael@0 | 185 | XULSliderAccessible::GetSliderAttr(nsIAtom* aName) const |
michael@0 | 186 | { |
michael@0 | 187 | nsAutoString attrValue; |
michael@0 | 188 | nsresult rv = GetSliderAttr(aName, attrValue); |
michael@0 | 189 | if (NS_FAILED(rv)) |
michael@0 | 190 | return UnspecifiedNaN<double>(); |
michael@0 | 191 | |
michael@0 | 192 | nsresult error = NS_OK; |
michael@0 | 193 | double value = attrValue.ToDouble(&error); |
michael@0 | 194 | return NS_FAILED(error) ? UnspecifiedNaN<double>() : value; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | bool |
michael@0 | 198 | XULSliderAccessible::SetSliderAttr(nsIAtom* aName, double aValue) |
michael@0 | 199 | { |
michael@0 | 200 | nsAutoString value; |
michael@0 | 201 | value.AppendFloat(aValue); |
michael@0 | 202 | |
michael@0 | 203 | return NS_SUCCEEDED(SetSliderAttr(aName, value)); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | |
michael@0 | 207 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 208 | // XULThumbAccessible |
michael@0 | 209 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 210 | |
michael@0 | 211 | XULThumbAccessible:: |
michael@0 | 212 | XULThumbAccessible(nsIContent* aContent, DocAccessible* aDoc) : |
michael@0 | 213 | AccessibleWrap(aContent, aDoc) |
michael@0 | 214 | { |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 218 | // XULThumbAccessible: Accessible |
michael@0 | 219 | |
michael@0 | 220 | role |
michael@0 | 221 | XULThumbAccessible::NativeRole() |
michael@0 | 222 | { |
michael@0 | 223 | return roles::INDICATOR; |
michael@0 | 224 | } |
michael@0 | 225 |