|
1 /* -*- Mode: C++; tab-width: 4; 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 |
|
6 #include "XULComboboxAccessible.h" |
|
7 |
|
8 #include "Accessible-inl.h" |
|
9 #include "nsAccessibilityService.h" |
|
10 #include "DocAccessible.h" |
|
11 #include "nsCoreUtils.h" |
|
12 #include "Role.h" |
|
13 #include "States.h" |
|
14 |
|
15 #include "nsIAutoCompleteInput.h" |
|
16 #include "nsIDOMXULMenuListElement.h" |
|
17 #include "nsIDOMXULSelectCntrlItemEl.h" |
|
18 |
|
19 using namespace mozilla::a11y; |
|
20 |
|
21 //////////////////////////////////////////////////////////////////////////////// |
|
22 // XULComboboxAccessible |
|
23 //////////////////////////////////////////////////////////////////////////////// |
|
24 |
|
25 XULComboboxAccessible:: |
|
26 XULComboboxAccessible(nsIContent* aContent, DocAccessible* aDoc) : |
|
27 AccessibleWrap(aContent, aDoc) |
|
28 { |
|
29 if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, |
|
30 nsGkAtoms::autocomplete, eIgnoreCase)) |
|
31 mGenericTypes |= eAutoComplete; |
|
32 else |
|
33 mGenericTypes |= eCombobox; |
|
34 } |
|
35 |
|
36 role |
|
37 XULComboboxAccessible::NativeRole() |
|
38 { |
|
39 return IsAutoComplete() ? roles::AUTOCOMPLETE : roles::COMBOBOX; |
|
40 } |
|
41 |
|
42 uint64_t |
|
43 XULComboboxAccessible::NativeState() |
|
44 { |
|
45 // As a nsComboboxAccessible we can have the following states: |
|
46 // STATE_FOCUSED |
|
47 // STATE_FOCUSABLE |
|
48 // STATE_HASPOPUP |
|
49 // STATE_EXPANDED |
|
50 // STATE_COLLAPSED |
|
51 |
|
52 // Get focus status from base class |
|
53 uint64_t state = Accessible::NativeState(); |
|
54 |
|
55 nsCOMPtr<nsIDOMXULMenuListElement> menuList(do_QueryInterface(mContent)); |
|
56 if (menuList) { |
|
57 bool isOpen = false; |
|
58 menuList->GetOpen(&isOpen); |
|
59 if (isOpen) |
|
60 state |= states::EXPANDED; |
|
61 else |
|
62 state |= states::COLLAPSED; |
|
63 } |
|
64 |
|
65 return state | states::HASPOPUP; |
|
66 } |
|
67 |
|
68 void |
|
69 XULComboboxAccessible::Description(nsString& aDescription) |
|
70 { |
|
71 aDescription.Truncate(); |
|
72 // Use description of currently focused option |
|
73 nsCOMPtr<nsIDOMXULMenuListElement> menuListElm(do_QueryInterface(mContent)); |
|
74 if (!menuListElm) |
|
75 return; |
|
76 |
|
77 nsCOMPtr<nsIDOMXULSelectControlItemElement> focusedOptionItem; |
|
78 menuListElm->GetSelectedItem(getter_AddRefs(focusedOptionItem)); |
|
79 nsCOMPtr<nsIContent> focusedOptionContent = |
|
80 do_QueryInterface(focusedOptionItem); |
|
81 if (focusedOptionContent && mDoc) { |
|
82 Accessible* focusedOptionAcc = mDoc->GetAccessible(focusedOptionContent); |
|
83 if (focusedOptionAcc) |
|
84 focusedOptionAcc->Description(aDescription); |
|
85 } |
|
86 } |
|
87 |
|
88 void |
|
89 XULComboboxAccessible::Value(nsString& aValue) |
|
90 { |
|
91 aValue.Truncate(); |
|
92 |
|
93 // The value is the option or text shown entered in the combobox. |
|
94 nsCOMPtr<nsIDOMXULMenuListElement> menuList(do_QueryInterface(mContent)); |
|
95 if (menuList) |
|
96 menuList->GetLabel(aValue); |
|
97 } |
|
98 |
|
99 bool |
|
100 XULComboboxAccessible::CanHaveAnonChildren() |
|
101 { |
|
102 if (mContent->NodeInfo()->Equals(nsGkAtoms::textbox, kNameSpaceID_XUL) || |
|
103 mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::editable, |
|
104 nsGkAtoms::_true, eIgnoreCase)) { |
|
105 // Both the XUL <textbox type="autocomplete"> and <menulist editable="true"> widgets |
|
106 // use XULComboboxAccessible. We need to walk the anonymous children for these |
|
107 // so that the entry field is a child |
|
108 return true; |
|
109 } |
|
110 |
|
111 // Argument of false indicates we don't walk anonymous children for |
|
112 // menuitems |
|
113 return false; |
|
114 } |
|
115 |
|
116 uint8_t |
|
117 XULComboboxAccessible::ActionCount() |
|
118 { |
|
119 // Just one action (click). |
|
120 return 1; |
|
121 } |
|
122 |
|
123 NS_IMETHODIMP |
|
124 XULComboboxAccessible::DoAction(uint8_t aIndex) |
|
125 { |
|
126 if (aIndex != XULComboboxAccessible::eAction_Click) { |
|
127 return NS_ERROR_INVALID_ARG; |
|
128 } |
|
129 |
|
130 if (IsDefunct()) |
|
131 return NS_ERROR_FAILURE; |
|
132 |
|
133 // Programmaticaly toggle the combo box. |
|
134 nsCOMPtr<nsIDOMXULMenuListElement> menuList(do_QueryInterface(mContent)); |
|
135 if (!menuList) { |
|
136 return NS_ERROR_FAILURE; |
|
137 } |
|
138 bool isDroppedDown; |
|
139 menuList->GetOpen(&isDroppedDown); |
|
140 return menuList->SetOpen(!isDroppedDown); |
|
141 } |
|
142 |
|
143 NS_IMETHODIMP |
|
144 XULComboboxAccessible::GetActionName(uint8_t aIndex, nsAString& aName) |
|
145 { |
|
146 if (aIndex != XULComboboxAccessible::eAction_Click) { |
|
147 return NS_ERROR_INVALID_ARG; |
|
148 } |
|
149 |
|
150 if (IsDefunct()) |
|
151 return NS_ERROR_FAILURE; |
|
152 |
|
153 // Our action name is the reverse of our state: |
|
154 // if we are close -> open is our name. |
|
155 // if we are open -> close is our name. |
|
156 // Uses the frame to get the state, updated on every click. |
|
157 |
|
158 nsCOMPtr<nsIDOMXULMenuListElement> menuList(do_QueryInterface(mContent)); |
|
159 if (!menuList) { |
|
160 return NS_ERROR_FAILURE; |
|
161 } |
|
162 bool isDroppedDown; |
|
163 menuList->GetOpen(&isDroppedDown); |
|
164 if (isDroppedDown) |
|
165 aName.AssignLiteral("close"); |
|
166 else |
|
167 aName.AssignLiteral("open"); |
|
168 |
|
169 return NS_OK; |
|
170 } |
|
171 |
|
172 //////////////////////////////////////////////////////////////////////////////// |
|
173 // Widgets |
|
174 |
|
175 bool |
|
176 XULComboboxAccessible::IsActiveWidget() const |
|
177 { |
|
178 if (IsAutoComplete() || |
|
179 mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::editable, |
|
180 nsGkAtoms::_true, eIgnoreCase)) { |
|
181 int32_t childCount = mChildren.Length(); |
|
182 for (int32_t idx = 0; idx < childCount; idx++) { |
|
183 Accessible* child = mChildren[idx]; |
|
184 if (child->Role() == roles::ENTRY) |
|
185 return FocusMgr()->HasDOMFocus(child->GetContent()); |
|
186 } |
|
187 return false; |
|
188 } |
|
189 |
|
190 return FocusMgr()->HasDOMFocus(mContent); |
|
191 } |
|
192 |
|
193 bool |
|
194 XULComboboxAccessible::AreItemsOperable() const |
|
195 { |
|
196 if (IsAutoComplete()) { |
|
197 nsCOMPtr<nsIAutoCompleteInput> autoCompleteInputElm = |
|
198 do_QueryInterface(mContent); |
|
199 if (autoCompleteInputElm) { |
|
200 bool isOpen = false; |
|
201 autoCompleteInputElm->GetPopupOpen(&isOpen); |
|
202 return isOpen; |
|
203 } |
|
204 return false; |
|
205 } |
|
206 |
|
207 nsCOMPtr<nsIDOMXULMenuListElement> menuListElm = do_QueryInterface(mContent); |
|
208 if (menuListElm) { |
|
209 bool isOpen = false; |
|
210 menuListElm->GetOpen(&isOpen); |
|
211 return isOpen; |
|
212 } |
|
213 |
|
214 return false; |
|
215 } |