XULSliderAccessible.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "XULSliderAccessible.h"
  6. #include "nsAccessibilityService.h"
  7. #include "Role.h"
  8. #include "States.h"
  9. #include "nsIFrame.h"
  10. #include "mozilla/dom/Element.h"
  11. #include "mozilla/FloatingPoint.h"
  12. using namespace mozilla::a11y;
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // XULSliderAccessible
  15. ////////////////////////////////////////////////////////////////////////////////
  16. XULSliderAccessible::
  17. XULSliderAccessible(nsIContent* aContent, DocAccessible* aDoc) :
  18. AccessibleWrap(aContent, aDoc)
  19. {
  20. mStateFlags |= eHasNumericValue | eNoXBLKids;
  21. }
  22. // Accessible
  23. role
  24. XULSliderAccessible::NativeRole()
  25. {
  26. return roles::SLIDER;
  27. }
  28. uint64_t
  29. XULSliderAccessible::NativeInteractiveState() const
  30. {
  31. if (NativelyUnavailable())
  32. return states::UNAVAILABLE;
  33. nsIContent* sliderElm = GetSliderElement();
  34. if (sliderElm) {
  35. nsIFrame* frame = sliderElm->GetPrimaryFrame();
  36. if (frame && frame->IsFocusable())
  37. return states::FOCUSABLE;
  38. }
  39. return 0;
  40. }
  41. bool
  42. XULSliderAccessible::NativelyUnavailable() const
  43. {
  44. return mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
  45. nsGkAtoms::_true, eCaseMatters);
  46. }
  47. void
  48. XULSliderAccessible::Value(nsString& aValue)
  49. {
  50. GetSliderAttr(nsGkAtoms::curpos, aValue);
  51. }
  52. uint8_t
  53. XULSliderAccessible::ActionCount()
  54. {
  55. return 1;
  56. }
  57. void
  58. XULSliderAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName)
  59. {
  60. aName.Truncate();
  61. if (aIndex == 0)
  62. aName.AssignLiteral("activate");
  63. }
  64. bool
  65. XULSliderAccessible::DoAction(uint8_t aIndex)
  66. {
  67. if (aIndex != 0)
  68. return false;
  69. nsIContent* sliderElm = GetSliderElement();
  70. if (sliderElm)
  71. DoCommand(sliderElm);
  72. return true;
  73. }
  74. double
  75. XULSliderAccessible::MaxValue() const
  76. {
  77. double value = AccessibleWrap::MaxValue();
  78. return IsNaN(value) ? GetSliderAttr(nsGkAtoms::maxpos) : value;
  79. }
  80. double
  81. XULSliderAccessible::MinValue() const
  82. {
  83. double value = AccessibleWrap::MinValue();
  84. return IsNaN(value) ? GetSliderAttr(nsGkAtoms::minpos) : value;
  85. }
  86. double
  87. XULSliderAccessible::Step() const
  88. {
  89. double value = AccessibleWrap::Step();
  90. return IsNaN(value) ? GetSliderAttr(nsGkAtoms::increment) : value;
  91. }
  92. double
  93. XULSliderAccessible::CurValue() const
  94. {
  95. double value = AccessibleWrap::CurValue();
  96. return IsNaN(value) ? GetSliderAttr(nsGkAtoms::curpos) : value;
  97. }
  98. bool
  99. XULSliderAccessible::SetCurValue(double aValue)
  100. {
  101. if (AccessibleWrap::SetCurValue(aValue))
  102. return true;
  103. return SetSliderAttr(nsGkAtoms::curpos, aValue);
  104. }
  105. // Utils
  106. nsIContent*
  107. XULSliderAccessible::GetSliderElement() const
  108. {
  109. if (!mSliderNode) {
  110. // XXX: we depend on anonymous content.
  111. mSliderNode = mContent->OwnerDoc()->
  112. GetAnonymousElementByAttribute(mContent, nsGkAtoms::anonid,
  113. NS_LITERAL_STRING("slider"));
  114. }
  115. return mSliderNode;
  116. }
  117. nsresult
  118. XULSliderAccessible::GetSliderAttr(nsIAtom* aName, nsAString& aValue) const
  119. {
  120. aValue.Truncate();
  121. if (IsDefunct())
  122. return NS_ERROR_FAILURE;
  123. nsIContent* sliderElm = GetSliderElement();
  124. if (sliderElm)
  125. sliderElm->GetAttr(kNameSpaceID_None, aName, aValue);
  126. return NS_OK;
  127. }
  128. nsresult
  129. XULSliderAccessible::SetSliderAttr(nsIAtom* aName, const nsAString& aValue)
  130. {
  131. if (IsDefunct())
  132. return NS_ERROR_FAILURE;
  133. nsIContent* sliderElm = GetSliderElement();
  134. if (sliderElm)
  135. sliderElm->SetAttr(kNameSpaceID_None, aName, aValue, true);
  136. return NS_OK;
  137. }
  138. double
  139. XULSliderAccessible::GetSliderAttr(nsIAtom* aName) const
  140. {
  141. nsAutoString attrValue;
  142. nsresult rv = GetSliderAttr(aName, attrValue);
  143. if (NS_FAILED(rv))
  144. return UnspecifiedNaN<double>();
  145. nsresult error = NS_OK;
  146. double value = attrValue.ToDouble(&error);
  147. return NS_FAILED(error) ? UnspecifiedNaN<double>() : value;
  148. }
  149. bool
  150. XULSliderAccessible::SetSliderAttr(nsIAtom* aName, double aValue)
  151. {
  152. nsAutoString value;
  153. value.AppendFloat(aValue);
  154. return NS_SUCCEEDED(SetSliderAttr(aName, value));
  155. }
  156. ////////////////////////////////////////////////////////////////////////////////
  157. // XULThumbAccessible
  158. ////////////////////////////////////////////////////////////////////////////////
  159. XULThumbAccessible::
  160. XULThumbAccessible(nsIContent* aContent, DocAccessible* aDoc) :
  161. AccessibleWrap(aContent, aDoc)
  162. {
  163. }
  164. ////////////////////////////////////////////////////////////////////////////////
  165. // XULThumbAccessible: Accessible
  166. role
  167. XULThumbAccessible::NativeRole()
  168. {
  169. return roles::INDICATOR;
  170. }