FormControlAccessible.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // NOTE: alphabetically ordered
  6. #include "FormControlAccessible.h"
  7. #include "Role.h"
  8. #include "mozilla/FloatingPoint.h"
  9. #include "nsIDOMHTMLFormElement.h"
  10. #include "nsIDOMXULElement.h"
  11. #include "nsIDOMXULControlElement.h"
  12. using namespace mozilla::a11y;
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // ProgressMeterAccessible
  15. ////////////////////////////////////////////////////////////////////////////////
  16. template class mozilla::a11y::ProgressMeterAccessible<1>;
  17. template class mozilla::a11y::ProgressMeterAccessible<100>;
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // Accessible
  20. template<int Max>
  21. role
  22. ProgressMeterAccessible<Max>::NativeRole()
  23. {
  24. return roles::PROGRESSBAR;
  25. }
  26. template<int Max>
  27. uint64_t
  28. ProgressMeterAccessible<Max>::NativeState()
  29. {
  30. uint64_t state = LeafAccessible::NativeState();
  31. // An undetermined progressbar (i.e. without a value) has a mixed state.
  32. nsAutoString attrValue;
  33. mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::value, attrValue);
  34. if (attrValue.IsEmpty())
  35. state |= states::MIXED;
  36. return state;
  37. }
  38. ////////////////////////////////////////////////////////////////////////////////
  39. // ProgressMeterAccessible<Max>: Widgets
  40. template<int Max>
  41. bool
  42. ProgressMeterAccessible<Max>::IsWidget() const
  43. {
  44. return true;
  45. }
  46. ////////////////////////////////////////////////////////////////////////////////
  47. // ProgressMeterAccessible<Max>: Value
  48. template<int Max>
  49. void
  50. ProgressMeterAccessible<Max>::Value(nsString& aValue)
  51. {
  52. LeafAccessible::Value(aValue);
  53. if (!aValue.IsEmpty())
  54. return;
  55. double maxValue = MaxValue();
  56. if (IsNaN(maxValue) || maxValue == 0)
  57. return;
  58. double curValue = CurValue();
  59. if (IsNaN(curValue))
  60. return;
  61. // Treat the current value bigger than maximum as 100%.
  62. double percentValue = (curValue < maxValue) ?
  63. (curValue / maxValue) * 100 : 100;
  64. aValue.AppendFloat(percentValue);
  65. aValue.Append('%');
  66. }
  67. template<int Max>
  68. double
  69. ProgressMeterAccessible<Max>::MaxValue() const
  70. {
  71. double value = LeafAccessible::MaxValue();
  72. if (!IsNaN(value))
  73. return value;
  74. nsAutoString strValue;
  75. if (mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::max, strValue)) {
  76. nsresult result = NS_OK;
  77. value = strValue.ToDouble(&result);
  78. if (NS_SUCCEEDED(result))
  79. return value;
  80. }
  81. return Max;
  82. }
  83. template<int Max>
  84. double
  85. ProgressMeterAccessible<Max>::MinValue() const
  86. {
  87. double value = LeafAccessible::MinValue();
  88. return IsNaN(value) ? 0 : value;
  89. }
  90. template<int Max>
  91. double
  92. ProgressMeterAccessible<Max>::Step() const
  93. {
  94. double value = LeafAccessible::Step();
  95. return IsNaN(value) ? 0 : value;
  96. }
  97. template<int Max>
  98. double
  99. ProgressMeterAccessible<Max>::CurValue() const
  100. {
  101. double value = LeafAccessible::CurValue();
  102. if (!IsNaN(value))
  103. return value;
  104. nsAutoString attrValue;
  105. if (!mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::value, attrValue))
  106. return UnspecifiedNaN<double>();
  107. nsresult error = NS_OK;
  108. value = attrValue.ToDouble(&error);
  109. return NS_FAILED(error) ? UnspecifiedNaN<double>() : value;
  110. }
  111. template<int Max>
  112. bool
  113. ProgressMeterAccessible<Max>::SetCurValue(double aValue)
  114. {
  115. return false; // progress meters are readonly.
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////
  118. // RadioButtonAccessible
  119. ////////////////////////////////////////////////////////////////////////////////
  120. RadioButtonAccessible::
  121. RadioButtonAccessible(nsIContent* aContent, DocAccessible* aDoc) :
  122. LeafAccessible(aContent, aDoc)
  123. {
  124. }
  125. uint8_t
  126. RadioButtonAccessible::ActionCount()
  127. {
  128. return 1;
  129. }
  130. void
  131. RadioButtonAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName)
  132. {
  133. if (aIndex == eAction_Click)
  134. aName.AssignLiteral("select");
  135. }
  136. bool
  137. RadioButtonAccessible::DoAction(uint8_t aIndex)
  138. {
  139. if (aIndex != eAction_Click)
  140. return false;
  141. DoCommand();
  142. return true;
  143. }
  144. role
  145. RadioButtonAccessible::NativeRole()
  146. {
  147. return roles::RADIOBUTTON;
  148. }
  149. ////////////////////////////////////////////////////////////////////////////////
  150. // RadioButtonAccessible: Widgets
  151. bool
  152. RadioButtonAccessible::IsWidget() const
  153. {
  154. return true;
  155. }