XULTreeAccessible.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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 "XULTreeAccessible.h"
  6. #include "Accessible-inl.h"
  7. #include "DocAccessible-inl.h"
  8. #include "nsAccCache.h"
  9. #include "nsAccUtils.h"
  10. #include "nsCoreUtils.h"
  11. #include "nsEventShell.h"
  12. #include "DocAccessible.h"
  13. #include "Relation.h"
  14. #include "Role.h"
  15. #include "States.h"
  16. #include "XULTreeGridAccessible.h"
  17. #include "nsQueryObject.h"
  18. #include "nsComponentManagerUtils.h"
  19. #include "nsIAccessibleRelation.h"
  20. #include "nsIAutoCompleteInput.h"
  21. #include "nsIAutoCompletePopup.h"
  22. #include "nsIBoxObject.h"
  23. #include "nsIDOMXULElement.h"
  24. #include "nsIDOMXULMenuListElement.h"
  25. #include "nsIDOMXULMultSelectCntrlEl.h"
  26. #include "nsIDOMXULTreeElement.h"
  27. #include "nsITreeSelection.h"
  28. #include "nsIMutableArray.h"
  29. #include "nsTreeBodyFrame.h"
  30. #include "nsTreeColumns.h"
  31. #include "nsTreeUtils.h"
  32. using namespace mozilla::a11y;
  33. ////////////////////////////////////////////////////////////////////////////////
  34. // XULTreeAccessible
  35. ////////////////////////////////////////////////////////////////////////////////
  36. XULTreeAccessible::
  37. XULTreeAccessible(nsIContent* aContent, DocAccessible* aDoc,
  38. nsTreeBodyFrame* aTreeFrame) :
  39. AccessibleWrap(aContent, aDoc),
  40. mAccessibleCache(kDefaultTreeCacheLength)
  41. {
  42. mType = eXULTreeType;
  43. mGenericTypes |= eSelect;
  44. nsCOMPtr<nsITreeView> view = aTreeFrame->GetExistingView();
  45. mTreeView = view;
  46. mTree = nsCoreUtils::GetTreeBoxObject(aContent);
  47. NS_ASSERTION(mTree, "Can't get mTree!\n");
  48. nsIContent* parentContent = mContent->GetParent();
  49. if (parentContent) {
  50. nsCOMPtr<nsIAutoCompletePopup> autoCompletePopupElm =
  51. do_QueryInterface(parentContent);
  52. if (autoCompletePopupElm)
  53. mGenericTypes |= eAutoCompletePopup;
  54. }
  55. }
  56. XULTreeAccessible::~XULTreeAccessible()
  57. {
  58. }
  59. ////////////////////////////////////////////////////////////////////////////////
  60. // XULTreeAccessible: nsISupports and cycle collection implementation
  61. NS_IMPL_CYCLE_COLLECTION_INHERITED(XULTreeAccessible, Accessible,
  62. mTree, mAccessibleCache)
  63. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(XULTreeAccessible)
  64. NS_INTERFACE_MAP_END_INHERITING(Accessible)
  65. NS_IMPL_ADDREF_INHERITED(XULTreeAccessible, Accessible)
  66. NS_IMPL_RELEASE_INHERITED(XULTreeAccessible, Accessible)
  67. ////////////////////////////////////////////////////////////////////////////////
  68. // XULTreeAccessible: Accessible implementation
  69. uint64_t
  70. XULTreeAccessible::NativeState()
  71. {
  72. // Get focus status from base class.
  73. uint64_t state = Accessible::NativeState();
  74. // readonly state
  75. state |= states::READONLY;
  76. // multiselectable state.
  77. if (!mTreeView)
  78. return state;
  79. nsCOMPtr<nsITreeSelection> selection;
  80. mTreeView->GetSelection(getter_AddRefs(selection));
  81. NS_ENSURE_TRUE(selection, state);
  82. bool isSingle = false;
  83. nsresult rv = selection->GetSingle(&isSingle);
  84. NS_ENSURE_SUCCESS(rv, state);
  85. if (!isSingle)
  86. state |= states::MULTISELECTABLE;
  87. return state;
  88. }
  89. void
  90. XULTreeAccessible::Value(nsString& aValue)
  91. {
  92. aValue.Truncate();
  93. if (!mTreeView)
  94. return;
  95. // Return the value is the first selected child.
  96. nsCOMPtr<nsITreeSelection> selection;
  97. mTreeView->GetSelection(getter_AddRefs(selection));
  98. if (!selection)
  99. return;
  100. int32_t currentIndex;
  101. selection->GetCurrentIndex(&currentIndex);
  102. if (currentIndex >= 0) {
  103. nsCOMPtr<nsITreeColumn> keyCol;
  104. nsCOMPtr<nsITreeColumns> cols;
  105. mTree->GetColumns(getter_AddRefs(cols));
  106. if (cols)
  107. cols->GetKeyColumn(getter_AddRefs(keyCol));
  108. mTreeView->GetCellText(currentIndex, keyCol, aValue);
  109. }
  110. }
  111. ////////////////////////////////////////////////////////////////////////////////
  112. // XULTreeAccessible: Accessible implementation
  113. void
  114. XULTreeAccessible::Shutdown()
  115. {
  116. if (mDoc && !mDoc->IsDefunct()) {
  117. UnbindCacheEntriesFromDocument(mAccessibleCache);
  118. }
  119. mTree = nullptr;
  120. mTreeView = nullptr;
  121. AccessibleWrap::Shutdown();
  122. }
  123. role
  124. XULTreeAccessible::NativeRole()
  125. {
  126. // No primary column means we're in a list. In fact, history and mail turn off
  127. // the primary flag when switching to a flat view.
  128. nsIContent* child = nsTreeUtils::GetDescendantChild(mContent, nsGkAtoms::treechildren);
  129. NS_ASSERTION(child, "tree without treechildren!");
  130. nsTreeBodyFrame* treeFrame = do_QueryFrame(child->GetPrimaryFrame());
  131. NS_ASSERTION(treeFrame, "xul tree accessible for tree without a frame!");
  132. if (!treeFrame)
  133. return roles::LIST;
  134. RefPtr<nsTreeColumns> cols = treeFrame->Columns();
  135. nsCOMPtr<nsITreeColumn> primaryCol;
  136. cols->GetPrimaryColumn(getter_AddRefs(primaryCol));
  137. return primaryCol ? roles::OUTLINE : roles::LIST;
  138. }
  139. ////////////////////////////////////////////////////////////////////////////////
  140. // XULTreeAccessible: Accessible implementation (DON'T put methods here)
  141. Accessible*
  142. XULTreeAccessible::ChildAtPoint(int32_t aX, int32_t aY,
  143. EWhichChildAtPoint aWhichChild)
  144. {
  145. nsIFrame *frame = GetFrame();
  146. if (!frame)
  147. return nullptr;
  148. nsPresContext *presContext = frame->PresContext();
  149. nsIPresShell* presShell = presContext->PresShell();
  150. nsIFrame *rootFrame = presShell->GetRootFrame();
  151. NS_ENSURE_TRUE(rootFrame, nullptr);
  152. nsIntRect rootRect = rootFrame->GetScreenRect();
  153. int32_t clientX = presContext->DevPixelsToIntCSSPixels(aX) - rootRect.x;
  154. int32_t clientY = presContext->DevPixelsToIntCSSPixels(aY) - rootRect.y;
  155. int32_t row = -1;
  156. nsCOMPtr<nsITreeColumn> column;
  157. nsAutoString childEltUnused;
  158. mTree->GetCellAt(clientX, clientY, &row, getter_AddRefs(column),
  159. childEltUnused);
  160. // If we failed to find tree cell for the given point then it might be
  161. // tree columns.
  162. if (row == -1 || !column)
  163. return AccessibleWrap::ChildAtPoint(aX, aY, aWhichChild);
  164. Accessible* child = GetTreeItemAccessible(row);
  165. if (aWhichChild == eDeepestChild && child) {
  166. // Look for accessible cell for the found item accessible.
  167. RefPtr<XULTreeItemAccessibleBase> treeitem = do_QueryObject(child);
  168. Accessible* cell = treeitem->GetCellAccessible(column);
  169. if (cell)
  170. child = cell;
  171. }
  172. return child;
  173. }
  174. ////////////////////////////////////////////////////////////////////////////////
  175. // XULTreeAccessible: SelectAccessible
  176. Accessible*
  177. XULTreeAccessible::CurrentItem()
  178. {
  179. if (!mTreeView)
  180. return nullptr;
  181. nsCOMPtr<nsITreeSelection> selection;
  182. mTreeView->GetSelection(getter_AddRefs(selection));
  183. if (selection) {
  184. int32_t currentIndex = -1;
  185. selection->GetCurrentIndex(&currentIndex);
  186. if (currentIndex >= 0)
  187. return GetTreeItemAccessible(currentIndex);
  188. }
  189. return nullptr;
  190. }
  191. void
  192. XULTreeAccessible::SetCurrentItem(Accessible* aItem)
  193. {
  194. NS_ERROR("XULTreeAccessible::SetCurrentItem not implemented");
  195. }
  196. void
  197. XULTreeAccessible::SelectedItems(nsTArray<Accessible*>* aItems)
  198. {
  199. if (!mTreeView)
  200. return;
  201. nsCOMPtr<nsITreeSelection> selection;
  202. mTreeView->GetSelection(getter_AddRefs(selection));
  203. if (!selection)
  204. return;
  205. int32_t rangeCount = 0;
  206. selection->GetRangeCount(&rangeCount);
  207. for (int32_t rangeIdx = 0; rangeIdx < rangeCount; rangeIdx++) {
  208. int32_t firstIdx = 0, lastIdx = -1;
  209. selection->GetRangeAt(rangeIdx, &firstIdx, &lastIdx);
  210. for (int32_t rowIdx = firstIdx; rowIdx <= lastIdx; rowIdx++) {
  211. Accessible* item = GetTreeItemAccessible(rowIdx);
  212. if (item)
  213. aItems->AppendElement(item);
  214. }
  215. }
  216. }
  217. uint32_t
  218. XULTreeAccessible::SelectedItemCount()
  219. {
  220. if (!mTreeView)
  221. return 0;
  222. nsCOMPtr<nsITreeSelection> selection;
  223. mTreeView->GetSelection(getter_AddRefs(selection));
  224. if (selection) {
  225. int32_t count = 0;
  226. selection->GetCount(&count);
  227. return count;
  228. }
  229. return 0;
  230. }
  231. bool
  232. XULTreeAccessible::AddItemToSelection(uint32_t aIndex)
  233. {
  234. if (!mTreeView)
  235. return false;
  236. nsCOMPtr<nsITreeSelection> selection;
  237. mTreeView->GetSelection(getter_AddRefs(selection));
  238. if (selection) {
  239. bool isSelected = false;
  240. selection->IsSelected(aIndex, &isSelected);
  241. if (!isSelected)
  242. selection->ToggleSelect(aIndex);
  243. return true;
  244. }
  245. return false;
  246. }
  247. bool
  248. XULTreeAccessible::RemoveItemFromSelection(uint32_t aIndex)
  249. {
  250. if (!mTreeView)
  251. return false;
  252. nsCOMPtr<nsITreeSelection> selection;
  253. mTreeView->GetSelection(getter_AddRefs(selection));
  254. if (selection) {
  255. bool isSelected = false;
  256. selection->IsSelected(aIndex, &isSelected);
  257. if (isSelected)
  258. selection->ToggleSelect(aIndex);
  259. return true;
  260. }
  261. return false;
  262. }
  263. bool
  264. XULTreeAccessible::IsItemSelected(uint32_t aIndex)
  265. {
  266. if (!mTreeView)
  267. return false;
  268. nsCOMPtr<nsITreeSelection> selection;
  269. mTreeView->GetSelection(getter_AddRefs(selection));
  270. if (selection) {
  271. bool isSelected = false;
  272. selection->IsSelected(aIndex, &isSelected);
  273. return isSelected;
  274. }
  275. return false;
  276. }
  277. bool
  278. XULTreeAccessible::UnselectAll()
  279. {
  280. if (!mTreeView)
  281. return false;
  282. nsCOMPtr<nsITreeSelection> selection;
  283. mTreeView->GetSelection(getter_AddRefs(selection));
  284. if (!selection)
  285. return false;
  286. selection->ClearSelection();
  287. return true;
  288. }
  289. Accessible*
  290. XULTreeAccessible::GetSelectedItem(uint32_t aIndex)
  291. {
  292. if (!mTreeView)
  293. return nullptr;
  294. nsCOMPtr<nsITreeSelection> selection;
  295. mTreeView->GetSelection(getter_AddRefs(selection));
  296. if (!selection)
  297. return nullptr;
  298. uint32_t selCount = 0;
  299. int32_t rangeCount = 0;
  300. selection->GetRangeCount(&rangeCount);
  301. for (int32_t rangeIdx = 0; rangeIdx < rangeCount; rangeIdx++) {
  302. int32_t firstIdx = 0, lastIdx = -1;
  303. selection->GetRangeAt(rangeIdx, &firstIdx, &lastIdx);
  304. for (int32_t rowIdx = firstIdx; rowIdx <= lastIdx; rowIdx++) {
  305. if (selCount == aIndex)
  306. return GetTreeItemAccessible(rowIdx);
  307. selCount++;
  308. }
  309. }
  310. return nullptr;
  311. }
  312. bool
  313. XULTreeAccessible::SelectAll()
  314. {
  315. // see if we are multiple select if so set ourselves as such
  316. if (!mTreeView)
  317. return false;
  318. nsCOMPtr<nsITreeSelection> selection;
  319. mTreeView->GetSelection(getter_AddRefs(selection));
  320. if (selection) {
  321. bool single = false;
  322. selection->GetSingle(&single);
  323. if (!single) {
  324. selection->SelectAll();
  325. return true;
  326. }
  327. }
  328. return false;
  329. }
  330. ////////////////////////////////////////////////////////////////////////////////
  331. // XULTreeAccessible: Accessible implementation
  332. Accessible*
  333. XULTreeAccessible::GetChildAt(uint32_t aIndex) const
  334. {
  335. uint32_t childCount = Accessible::ChildCount();
  336. if (aIndex < childCount)
  337. return Accessible::GetChildAt(aIndex);
  338. return GetTreeItemAccessible(aIndex - childCount);
  339. }
  340. uint32_t
  341. XULTreeAccessible::ChildCount() const
  342. {
  343. // Tree's children count is row count + treecols count.
  344. uint32_t childCount = Accessible::ChildCount();
  345. if (!mTreeView)
  346. return childCount;
  347. int32_t rowCount = 0;
  348. mTreeView->GetRowCount(&rowCount);
  349. childCount += rowCount;
  350. return childCount;
  351. }
  352. Relation
  353. XULTreeAccessible::RelationByType(RelationType aType)
  354. {
  355. if (aType == RelationType::NODE_PARENT_OF) {
  356. if (mTreeView)
  357. return Relation(new XULTreeItemIterator(this, mTreeView, -1));
  358. return Relation();
  359. }
  360. return Accessible::RelationByType(aType);
  361. }
  362. ////////////////////////////////////////////////////////////////////////////////
  363. // XULTreeAccessible: Widgets
  364. bool
  365. XULTreeAccessible::IsWidget() const
  366. {
  367. return true;
  368. }
  369. bool
  370. XULTreeAccessible::IsActiveWidget() const
  371. {
  372. if (IsAutoCompletePopup()) {
  373. nsCOMPtr<nsIAutoCompletePopup> autoCompletePopupElm =
  374. do_QueryInterface(mContent->GetParent());
  375. if (autoCompletePopupElm) {
  376. bool isOpen = false;
  377. autoCompletePopupElm->GetPopupOpen(&isOpen);
  378. return isOpen;
  379. }
  380. }
  381. return FocusMgr()->HasDOMFocus(mContent);
  382. }
  383. bool
  384. XULTreeAccessible::AreItemsOperable() const
  385. {
  386. if (IsAutoCompletePopup()) {
  387. nsCOMPtr<nsIAutoCompletePopup> autoCompletePopupElm =
  388. do_QueryInterface(mContent->GetParent());
  389. if (autoCompletePopupElm) {
  390. bool isOpen = false;
  391. autoCompletePopupElm->GetPopupOpen(&isOpen);
  392. return isOpen;
  393. }
  394. }
  395. return true;
  396. }
  397. Accessible*
  398. XULTreeAccessible::ContainerWidget() const
  399. {
  400. if (IsAutoCompletePopup()) {
  401. // This works for XUL autocompletes. It doesn't work for HTML forms
  402. // autocomplete because of potential crossprocess calls (when autocomplete
  403. // lives in content process while popup lives in chrome process). If that's
  404. // a problem then rethink Widgets interface.
  405. nsCOMPtr<nsIDOMXULMenuListElement> menuListElm =
  406. do_QueryInterface(mContent->GetParent());
  407. if (menuListElm) {
  408. nsCOMPtr<nsIDOMNode> inputElm;
  409. menuListElm->GetInputField(getter_AddRefs(inputElm));
  410. if (inputElm) {
  411. nsCOMPtr<nsINode> inputNode = do_QueryInterface(inputElm);
  412. if (inputNode) {
  413. Accessible* input =
  414. mDoc->GetAccessible(inputNode);
  415. return input ? input->ContainerWidget() : nullptr;
  416. }
  417. }
  418. }
  419. }
  420. return nullptr;
  421. }
  422. ////////////////////////////////////////////////////////////////////////////////
  423. // XULTreeAccessible: public implementation
  424. Accessible*
  425. XULTreeAccessible::GetTreeItemAccessible(int32_t aRow) const
  426. {
  427. if (aRow < 0 || IsDefunct() || !mTreeView)
  428. return nullptr;
  429. int32_t rowCount = 0;
  430. nsresult rv = mTreeView->GetRowCount(&rowCount);
  431. if (NS_FAILED(rv) || aRow >= rowCount)
  432. return nullptr;
  433. void *key = reinterpret_cast<void*>(intptr_t(aRow));
  434. Accessible* cachedTreeItem = mAccessibleCache.GetWeak(key);
  435. if (cachedTreeItem)
  436. return cachedTreeItem;
  437. RefPtr<Accessible> treeItem = CreateTreeItemAccessible(aRow);
  438. if (treeItem) {
  439. mAccessibleCache.Put(key, treeItem);
  440. Document()->BindToDocument(treeItem, nullptr);
  441. return treeItem;
  442. }
  443. return nullptr;
  444. }
  445. void
  446. XULTreeAccessible::InvalidateCache(int32_t aRow, int32_t aCount)
  447. {
  448. if (IsDefunct())
  449. return;
  450. if (!mTreeView) {
  451. UnbindCacheEntriesFromDocument(mAccessibleCache);
  452. return;
  453. }
  454. // Do not invalidate the cache if rows have been inserted.
  455. if (aCount > 0)
  456. return;
  457. DocAccessible* document = Document();
  458. // Fire destroy event for removed tree items and delete them from caches.
  459. for (int32_t rowIdx = aRow; rowIdx < aRow - aCount; rowIdx++) {
  460. void* key = reinterpret_cast<void*>(intptr_t(rowIdx));
  461. Accessible* treeItem = mAccessibleCache.GetWeak(key);
  462. if (treeItem) {
  463. RefPtr<AccEvent> event =
  464. new AccEvent(nsIAccessibleEvent::EVENT_HIDE, treeItem);
  465. nsEventShell::FireEvent(event);
  466. // Unbind from document, shutdown and remove from tree cache.
  467. document->UnbindFromDocument(treeItem);
  468. mAccessibleCache.Remove(key);
  469. }
  470. }
  471. // We dealt with removed tree items already however we may keep tree items
  472. // having row indexes greater than row count. We should remove these dead tree
  473. // items silently from caches.
  474. int32_t newRowCount = 0;
  475. nsresult rv = mTreeView->GetRowCount(&newRowCount);
  476. if (NS_FAILED(rv))
  477. return;
  478. int32_t oldRowCount = newRowCount - aCount;
  479. for (int32_t rowIdx = newRowCount; rowIdx < oldRowCount; ++rowIdx) {
  480. void *key = reinterpret_cast<void*>(intptr_t(rowIdx));
  481. Accessible* treeItem = mAccessibleCache.GetWeak(key);
  482. if (treeItem) {
  483. // Unbind from document, shutdown and remove from tree cache.
  484. document->UnbindFromDocument(treeItem);
  485. mAccessibleCache.Remove(key);
  486. }
  487. }
  488. }
  489. void
  490. XULTreeAccessible::TreeViewInvalidated(int32_t aStartRow, int32_t aEndRow,
  491. int32_t aStartCol, int32_t aEndCol)
  492. {
  493. if (IsDefunct())
  494. return;
  495. if (!mTreeView) {
  496. UnbindCacheEntriesFromDocument(mAccessibleCache);
  497. return;
  498. }
  499. int32_t endRow = aEndRow;
  500. nsresult rv;
  501. if (endRow == -1) {
  502. int32_t rowCount = 0;
  503. rv = mTreeView->GetRowCount(&rowCount);
  504. if (NS_FAILED(rv))
  505. return;
  506. endRow = rowCount - 1;
  507. }
  508. nsCOMPtr<nsITreeColumns> treeColumns;
  509. mTree->GetColumns(getter_AddRefs(treeColumns));
  510. if (!treeColumns)
  511. return;
  512. int32_t endCol = aEndCol;
  513. if (endCol == -1) {
  514. int32_t colCount = 0;
  515. rv = treeColumns->GetCount(&colCount);
  516. if (NS_FAILED(rv))
  517. return;
  518. endCol = colCount - 1;
  519. }
  520. for (int32_t rowIdx = aStartRow; rowIdx <= endRow; ++rowIdx) {
  521. void *key = reinterpret_cast<void*>(intptr_t(rowIdx));
  522. Accessible* accessible = mAccessibleCache.GetWeak(key);
  523. if (accessible) {
  524. RefPtr<XULTreeItemAccessibleBase> treeitemAcc = do_QueryObject(accessible);
  525. NS_ASSERTION(treeitemAcc, "Wrong accessible at the given key!");
  526. treeitemAcc->RowInvalidated(aStartCol, endCol);
  527. }
  528. }
  529. }
  530. void
  531. XULTreeAccessible::TreeViewChanged(nsITreeView* aView)
  532. {
  533. if (IsDefunct())
  534. return;
  535. // Fire reorder event on tree accessible on accessible tree (do not fire
  536. // show/hide events on tree items because it can be expensive to fire them for
  537. // each tree item.
  538. RefPtr<AccReorderEvent> reorderEvent = new AccReorderEvent(this);
  539. Document()->FireDelayedEvent(reorderEvent);
  540. // Clear cache.
  541. UnbindCacheEntriesFromDocument(mAccessibleCache);
  542. mTreeView = aView;
  543. }
  544. ////////////////////////////////////////////////////////////////////////////////
  545. // XULTreeAccessible: protected implementation
  546. already_AddRefed<Accessible>
  547. XULTreeAccessible::CreateTreeItemAccessible(int32_t aRow) const
  548. {
  549. RefPtr<Accessible> accessible =
  550. new XULTreeItemAccessible(mContent, mDoc, const_cast<XULTreeAccessible*>(this),
  551. mTree, mTreeView, aRow);
  552. return accessible.forget();
  553. }
  554. ////////////////////////////////////////////////////////////////////////////////
  555. // XULTreeItemAccessibleBase
  556. ////////////////////////////////////////////////////////////////////////////////
  557. XULTreeItemAccessibleBase::
  558. XULTreeItemAccessibleBase(nsIContent* aContent, DocAccessible* aDoc,
  559. Accessible* aParent, nsITreeBoxObject* aTree,
  560. nsITreeView* aTreeView, int32_t aRow) :
  561. AccessibleWrap(aContent, aDoc),
  562. mTree(aTree), mTreeView(aTreeView), mRow(aRow)
  563. {
  564. mParent = aParent;
  565. mStateFlags |= eSharedNode;
  566. }
  567. XULTreeItemAccessibleBase::~XULTreeItemAccessibleBase()
  568. {
  569. }
  570. ////////////////////////////////////////////////////////////////////////////////
  571. // XULTreeItemAccessibleBase: nsISupports implementation
  572. NS_IMPL_CYCLE_COLLECTION_INHERITED(XULTreeItemAccessibleBase, Accessible,
  573. mTree)
  574. NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(XULTreeItemAccessibleBase)
  575. NS_INTERFACE_TABLE_INHERITED(XULTreeItemAccessibleBase,
  576. XULTreeItemAccessibleBase)
  577. NS_INTERFACE_TABLE_TAIL_INHERITING(Accessible)
  578. NS_IMPL_ADDREF_INHERITED(XULTreeItemAccessibleBase, Accessible)
  579. NS_IMPL_RELEASE_INHERITED(XULTreeItemAccessibleBase, Accessible)
  580. ////////////////////////////////////////////////////////////////////////////////
  581. // XULTreeItemAccessibleBase: Accessible
  582. Accessible*
  583. XULTreeItemAccessibleBase::FocusedChild()
  584. {
  585. return FocusMgr()->FocusedAccessible() == this ? this : nullptr;
  586. }
  587. nsIntRect
  588. XULTreeItemAccessibleBase::Bounds() const
  589. {
  590. // Get x coordinate and width from treechildren element, get y coordinate and
  591. // height from tree cell.
  592. nsCOMPtr<nsIBoxObject> boxObj = nsCoreUtils::GetTreeBodyBoxObject(mTree);
  593. if (!boxObj)
  594. return nsIntRect();
  595. nsCOMPtr<nsITreeColumn> column = nsCoreUtils::GetFirstSensibleColumn(mTree);
  596. int32_t x = 0, y = 0, width = 0, height = 0;
  597. nsresult rv = mTree->GetCoordsForCellItem(mRow, column, EmptyString(),
  598. &x, &y, &width, &height);
  599. if (NS_FAILED(rv))
  600. return nsIntRect();
  601. boxObj->GetWidth(&width);
  602. int32_t tcX = 0, tcY = 0;
  603. boxObj->GetScreenX(&tcX);
  604. boxObj->GetScreenY(&tcY);
  605. x = tcX;
  606. y += tcY;
  607. nsPresContext* presContext = mDoc->PresContext();
  608. return nsIntRect(presContext->CSSPixelsToDevPixels(x),
  609. presContext->CSSPixelsToDevPixels(y),
  610. presContext->CSSPixelsToDevPixels(width),
  611. presContext->CSSPixelsToDevPixels(height));
  612. }
  613. void
  614. XULTreeItemAccessibleBase::SetSelected(bool aSelect)
  615. {
  616. nsCOMPtr<nsITreeSelection> selection;
  617. mTreeView->GetSelection(getter_AddRefs(selection));
  618. if (selection) {
  619. bool isSelected = false;
  620. selection->IsSelected(mRow, &isSelected);
  621. if (isSelected != aSelect)
  622. selection->ToggleSelect(mRow);
  623. }
  624. }
  625. void
  626. XULTreeItemAccessibleBase::TakeFocus()
  627. {
  628. nsCOMPtr<nsITreeSelection> selection;
  629. mTreeView->GetSelection(getter_AddRefs(selection));
  630. if (selection)
  631. selection->SetCurrentIndex(mRow);
  632. // focus event will be fired here
  633. Accessible::TakeFocus();
  634. }
  635. Relation
  636. XULTreeItemAccessibleBase::RelationByType(RelationType aType)
  637. {
  638. switch (aType) {
  639. case RelationType::NODE_CHILD_OF: {
  640. int32_t parentIndex = -1;
  641. if (!NS_SUCCEEDED(mTreeView->GetParentIndex(mRow, &parentIndex)))
  642. return Relation();
  643. if (parentIndex == -1)
  644. return Relation(mParent);
  645. XULTreeAccessible* treeAcc = mParent->AsXULTree();
  646. return Relation(treeAcc->GetTreeItemAccessible(parentIndex));
  647. }
  648. case RelationType::NODE_PARENT_OF: {
  649. bool isTrue = false;
  650. if (NS_FAILED(mTreeView->IsContainerEmpty(mRow, &isTrue)) || isTrue)
  651. return Relation();
  652. if (NS_FAILED(mTreeView->IsContainerOpen(mRow, &isTrue)) || !isTrue)
  653. return Relation();
  654. XULTreeAccessible* tree = mParent->AsXULTree();
  655. return Relation(new XULTreeItemIterator(tree, mTreeView, mRow));
  656. }
  657. default:
  658. return Relation();
  659. }
  660. }
  661. uint8_t
  662. XULTreeItemAccessibleBase::ActionCount()
  663. {
  664. // "activate" action is available for all treeitems, "expand/collapse" action
  665. // is avaible for treeitem which is container.
  666. return IsExpandable() ? 2 : 1;
  667. }
  668. void
  669. XULTreeItemAccessibleBase::ActionNameAt(uint8_t aIndex, nsAString& aName)
  670. {
  671. if (aIndex == eAction_Click) {
  672. aName.AssignLiteral("activate");
  673. return;
  674. }
  675. if (aIndex == eAction_Expand && IsExpandable()) {
  676. bool isContainerOpen = false;
  677. mTreeView->IsContainerOpen(mRow, &isContainerOpen);
  678. if (isContainerOpen)
  679. aName.AssignLiteral("collapse");
  680. else
  681. aName.AssignLiteral("expand");
  682. }
  683. }
  684. bool
  685. XULTreeItemAccessibleBase::DoAction(uint8_t aIndex)
  686. {
  687. if (aIndex != eAction_Click &&
  688. (aIndex != eAction_Expand || !IsExpandable()))
  689. return false;
  690. DoCommand(nullptr, aIndex);
  691. return true;
  692. }
  693. ////////////////////////////////////////////////////////////////////////////////
  694. // XULTreeItemAccessibleBase: Accessible implementation
  695. void
  696. XULTreeItemAccessibleBase::Shutdown()
  697. {
  698. mTree = nullptr;
  699. mTreeView = nullptr;
  700. mRow = -1;
  701. AccessibleWrap::Shutdown();
  702. }
  703. GroupPos
  704. XULTreeItemAccessibleBase::GroupPosition()
  705. {
  706. GroupPos groupPos;
  707. int32_t level;
  708. nsresult rv = mTreeView->GetLevel(mRow, &level);
  709. NS_ENSURE_SUCCESS(rv, groupPos);
  710. int32_t topCount = 1;
  711. for (int32_t index = mRow - 1; index >= 0; index--) {
  712. int32_t lvl = -1;
  713. if (NS_SUCCEEDED(mTreeView->GetLevel(index, &lvl))) {
  714. if (lvl < level)
  715. break;
  716. if (lvl == level)
  717. topCount++;
  718. }
  719. }
  720. int32_t rowCount = 0;
  721. rv = mTreeView->GetRowCount(&rowCount);
  722. NS_ENSURE_SUCCESS(rv, groupPos);
  723. int32_t bottomCount = 0;
  724. for (int32_t index = mRow + 1; index < rowCount; index++) {
  725. int32_t lvl = -1;
  726. if (NS_SUCCEEDED(mTreeView->GetLevel(index, &lvl))) {
  727. if (lvl < level)
  728. break;
  729. if (lvl == level)
  730. bottomCount++;
  731. }
  732. }
  733. groupPos.level = level + 1;
  734. groupPos.setSize = topCount + bottomCount;
  735. groupPos.posInSet = topCount;
  736. return groupPos;
  737. }
  738. uint64_t
  739. XULTreeItemAccessibleBase::NativeState()
  740. {
  741. // focusable and selectable states
  742. uint64_t state = NativeInteractiveState();
  743. // expanded/collapsed state
  744. if (IsExpandable()) {
  745. bool isContainerOpen;
  746. mTreeView->IsContainerOpen(mRow, &isContainerOpen);
  747. state |= isContainerOpen ? states::EXPANDED : states::COLLAPSED;
  748. }
  749. // selected state
  750. nsCOMPtr<nsITreeSelection> selection;
  751. mTreeView->GetSelection(getter_AddRefs(selection));
  752. if (selection) {
  753. bool isSelected;
  754. selection->IsSelected(mRow, &isSelected);
  755. if (isSelected)
  756. state |= states::SELECTED;
  757. }
  758. // focused state
  759. if (FocusMgr()->IsFocused(this))
  760. state |= states::FOCUSED;
  761. // invisible state
  762. int32_t firstVisibleRow, lastVisibleRow;
  763. mTree->GetFirstVisibleRow(&firstVisibleRow);
  764. mTree->GetLastVisibleRow(&lastVisibleRow);
  765. if (mRow < firstVisibleRow || mRow > lastVisibleRow)
  766. state |= states::INVISIBLE;
  767. return state;
  768. }
  769. uint64_t
  770. XULTreeItemAccessibleBase::NativeInteractiveState() const
  771. {
  772. return states::FOCUSABLE | states::SELECTABLE;
  773. }
  774. int32_t
  775. XULTreeItemAccessibleBase::IndexInParent() const
  776. {
  777. return mParent ? mParent->ContentChildCount() + mRow : -1;
  778. }
  779. ////////////////////////////////////////////////////////////////////////////////
  780. // XULTreeItemAccessibleBase: Widgets
  781. Accessible*
  782. XULTreeItemAccessibleBase::ContainerWidget() const
  783. {
  784. return mParent;
  785. }
  786. ////////////////////////////////////////////////////////////////////////////////
  787. // XULTreeItemAccessibleBase: Accessible protected methods
  788. void
  789. XULTreeItemAccessibleBase::DispatchClickEvent(nsIContent* aContent,
  790. uint32_t aActionIndex)
  791. {
  792. if (IsDefunct())
  793. return;
  794. nsCOMPtr<nsITreeColumns> columns;
  795. mTree->GetColumns(getter_AddRefs(columns));
  796. if (!columns)
  797. return;
  798. // Get column and pseudo element.
  799. nsCOMPtr<nsITreeColumn> column;
  800. nsAutoString pseudoElm;
  801. if (aActionIndex == eAction_Click) {
  802. // Key column is visible and clickable.
  803. columns->GetKeyColumn(getter_AddRefs(column));
  804. } else {
  805. // Primary column contains a twisty we should click on.
  806. columns->GetPrimaryColumn(getter_AddRefs(column));
  807. pseudoElm = NS_LITERAL_STRING("twisty");
  808. }
  809. if (column)
  810. nsCoreUtils::DispatchClickEvent(mTree, mRow, column, pseudoElm);
  811. }
  812. Accessible*
  813. XULTreeItemAccessibleBase::GetSiblingAtOffset(int32_t aOffset,
  814. nsresult* aError) const
  815. {
  816. if (aError)
  817. *aError = NS_OK; // fail peacefully
  818. return mParent->GetChildAt(IndexInParent() + aOffset);
  819. }
  820. ////////////////////////////////////////////////////////////////////////////////
  821. // XULTreeItemAccessibleBase: protected implementation
  822. bool
  823. XULTreeItemAccessibleBase::IsExpandable()
  824. {
  825. bool isContainer = false;
  826. mTreeView->IsContainer(mRow, &isContainer);
  827. if (isContainer) {
  828. bool isEmpty = false;
  829. mTreeView->IsContainerEmpty(mRow, &isEmpty);
  830. if (!isEmpty) {
  831. nsCOMPtr<nsITreeColumns> columns;
  832. mTree->GetColumns(getter_AddRefs(columns));
  833. nsCOMPtr<nsITreeColumn> primaryColumn;
  834. if (columns) {
  835. columns->GetPrimaryColumn(getter_AddRefs(primaryColumn));
  836. if (primaryColumn &&
  837. !nsCoreUtils::IsColumnHidden(primaryColumn))
  838. return true;
  839. }
  840. }
  841. }
  842. return false;
  843. }
  844. void
  845. XULTreeItemAccessibleBase::GetCellName(nsITreeColumn* aColumn, nsAString& aName)
  846. {
  847. mTreeView->GetCellText(mRow, aColumn, aName);
  848. // If there is still no name try the cell value:
  849. // This is for graphical cells. We need tree/table view implementors to
  850. // implement FooView::GetCellValue to return a meaningful string for cases
  851. // where there is something shown in the cell (non-text) such as a star icon;
  852. // in which case GetCellValue for that cell would return "starred" or
  853. // "flagged" for example.
  854. if (aName.IsEmpty())
  855. mTreeView->GetCellValue(mRow, aColumn, aName);
  856. }
  857. ////////////////////////////////////////////////////////////////////////////////
  858. // XULTreeItemAccessible
  859. ////////////////////////////////////////////////////////////////////////////////
  860. XULTreeItemAccessible::
  861. XULTreeItemAccessible(nsIContent* aContent, DocAccessible* aDoc,
  862. Accessible* aParent, nsITreeBoxObject* aTree,
  863. nsITreeView* aTreeView, int32_t aRow) :
  864. XULTreeItemAccessibleBase(aContent, aDoc, aParent, aTree, aTreeView, aRow)
  865. {
  866. mStateFlags |= eNoKidsFromDOM;
  867. mColumn = nsCoreUtils::GetFirstSensibleColumn(mTree);
  868. GetCellName(mColumn, mCachedName);
  869. }
  870. XULTreeItemAccessible::~XULTreeItemAccessible()
  871. {
  872. }
  873. ////////////////////////////////////////////////////////////////////////////////
  874. // XULTreeItemAccessible: nsISupports implementation
  875. NS_IMPL_CYCLE_COLLECTION_INHERITED(XULTreeItemAccessible,
  876. XULTreeItemAccessibleBase,
  877. mColumn)
  878. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(XULTreeItemAccessible)
  879. NS_INTERFACE_MAP_END_INHERITING(XULTreeItemAccessibleBase)
  880. NS_IMPL_ADDREF_INHERITED(XULTreeItemAccessible, XULTreeItemAccessibleBase)
  881. NS_IMPL_RELEASE_INHERITED(XULTreeItemAccessible, XULTreeItemAccessibleBase)
  882. ////////////////////////////////////////////////////////////////////////////////
  883. // XULTreeItemAccessible: nsIAccessible implementation
  884. ENameValueFlag
  885. XULTreeItemAccessible::Name(nsString& aName)
  886. {
  887. aName.Truncate();
  888. GetCellName(mColumn, aName);
  889. return eNameOK;
  890. }
  891. ////////////////////////////////////////////////////////////////////////////////
  892. // XULTreeItemAccessible: Accessible implementation
  893. void
  894. XULTreeItemAccessible::Shutdown()
  895. {
  896. mColumn = nullptr;
  897. XULTreeItemAccessibleBase::Shutdown();
  898. }
  899. role
  900. XULTreeItemAccessible::NativeRole()
  901. {
  902. nsCOMPtr<nsITreeColumns> columns;
  903. mTree->GetColumns(getter_AddRefs(columns));
  904. if (!columns) {
  905. NS_ERROR("No tree columns object in the tree!");
  906. return roles::NOTHING;
  907. }
  908. nsCOMPtr<nsITreeColumn> primaryColumn;
  909. columns->GetPrimaryColumn(getter_AddRefs(primaryColumn));
  910. return primaryColumn ? roles::OUTLINEITEM : roles::LISTITEM;
  911. }
  912. ////////////////////////////////////////////////////////////////////////////////
  913. // XULTreeItemAccessible: XULTreeItemAccessibleBase implementation
  914. void
  915. XULTreeItemAccessible::RowInvalidated(int32_t aStartColIdx, int32_t aEndColIdx)
  916. {
  917. nsAutoString name;
  918. Name(name);
  919. if (name != mCachedName) {
  920. nsEventShell::FireEvent(nsIAccessibleEvent::EVENT_NAME_CHANGE, this);
  921. mCachedName = name;
  922. }
  923. }
  924. ////////////////////////////////////////////////////////////////////////////////
  925. // XULTreeColumAccessible
  926. ////////////////////////////////////////////////////////////////////////////////
  927. XULTreeColumAccessible::
  928. XULTreeColumAccessible(nsIContent* aContent, DocAccessible* aDoc) :
  929. XULColumAccessible(aContent, aDoc)
  930. {
  931. }
  932. Accessible*
  933. XULTreeColumAccessible::GetSiblingAtOffset(int32_t aOffset,
  934. nsresult* aError) const
  935. {
  936. if (aOffset < 0)
  937. return XULColumAccessible::GetSiblingAtOffset(aOffset, aError);
  938. if (aError)
  939. *aError = NS_OK; // fail peacefully
  940. nsCOMPtr<nsITreeBoxObject> tree = nsCoreUtils::GetTreeBoxObject(mContent);
  941. if (tree) {
  942. nsCOMPtr<nsITreeView> treeView;
  943. tree->GetView(getter_AddRefs(treeView));
  944. if (treeView) {
  945. int32_t rowCount = 0;
  946. treeView->GetRowCount(&rowCount);
  947. if (rowCount > 0 && aOffset <= rowCount) {
  948. XULTreeAccessible* treeAcc = Parent()->AsXULTree();
  949. if (treeAcc)
  950. return treeAcc->GetTreeItemAccessible(aOffset - 1);
  951. }
  952. }
  953. }
  954. return nullptr;
  955. }