Accessible.h 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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. #ifndef _Accessible_H_
  6. #define _Accessible_H_
  7. #include "mozilla/a11y/AccTypes.h"
  8. #include "mozilla/a11y/RelationType.h"
  9. #include "mozilla/a11y/Role.h"
  10. #include "mozilla/a11y/States.h"
  11. #include "mozilla/UniquePtr.h"
  12. #include "nsIContent.h"
  13. #include "nsIContentInlines.h"
  14. #include "nsString.h"
  15. #include "nsTArray.h"
  16. #include "nsRefPtrHashtable.h"
  17. #include "nsRect.h"
  18. struct nsRoleMapEntry;
  19. struct nsRect;
  20. class nsIFrame;
  21. class nsIAtom;
  22. class nsIPersistentProperties;
  23. namespace mozilla {
  24. namespace a11y {
  25. class Accessible;
  26. class AccEvent;
  27. class AccGroupInfo;
  28. class ApplicationAccessible;
  29. class DocAccessible;
  30. class EmbeddedObjCollector;
  31. class EventTree;
  32. class HTMLImageMapAccessible;
  33. class HTMLLIAccessible;
  34. class HyperTextAccessible;
  35. class ImageAccessible;
  36. class KeyBinding;
  37. class OuterDocAccessible;
  38. class ProxyAccessible;
  39. class Relation;
  40. class RootAccessible;
  41. class TableAccessible;
  42. class TableCellAccessible;
  43. class TextLeafAccessible;
  44. class XULLabelAccessible;
  45. class XULTreeAccessible;
  46. #ifdef A11Y_LOG
  47. namespace logging {
  48. typedef const char* (*GetTreePrefix)(void* aData, Accessible*);
  49. void Tree(const char* aTitle, const char* aMsgText, Accessible* aRoot,
  50. GetTreePrefix aPrefixFunc, void* GetTreePrefixData);
  51. };
  52. #endif
  53. /**
  54. * Name type flags.
  55. */
  56. enum ENameValueFlag {
  57. /**
  58. * Name either
  59. * a) present (not empty): !name.IsEmpty()
  60. * b) no name (was missed): name.IsVoid()
  61. */
  62. eNameOK,
  63. /**
  64. * Name was left empty by the author on purpose:
  65. * name.IsEmpty() && !name.IsVoid().
  66. */
  67. eNoNameOnPurpose,
  68. /**
  69. * Name was computed from the subtree.
  70. */
  71. eNameFromSubtree,
  72. /**
  73. * Tooltip was used as a name.
  74. */
  75. eNameFromTooltip
  76. };
  77. /**
  78. * Group position (level, position in set and set size).
  79. */
  80. struct GroupPos
  81. {
  82. GroupPos() : level(0), posInSet(0), setSize(0) { }
  83. GroupPos(int32_t aLevel, int32_t aPosInSet, int32_t aSetSize) :
  84. level(aLevel), posInSet(aPosInSet), setSize(aSetSize) { }
  85. int32_t level;
  86. int32_t posInSet;
  87. int32_t setSize;
  88. };
  89. /**
  90. * An index type. Assert if out of range value was attempted to be used.
  91. */
  92. class index_t
  93. {
  94. public:
  95. MOZ_IMPLICIT index_t(int32_t aVal) : mVal(aVal) {}
  96. operator uint32_t() const
  97. {
  98. MOZ_ASSERT(mVal >= 0, "Attempt to use wrong index!");
  99. return mVal;
  100. }
  101. bool IsValid() const { return mVal >= 0; }
  102. private:
  103. int32_t mVal;
  104. };
  105. typedef nsRefPtrHashtable<nsPtrHashKey<const void>, Accessible>
  106. AccessibleHashtable;
  107. #define NS_ACCESSIBLE_IMPL_IID \
  108. { /* 133c8bf4-4913-4355-bd50-426bd1d6e1ad */ \
  109. 0x133c8bf4, \
  110. 0x4913, \
  111. 0x4355, \
  112. { 0xbd, 0x50, 0x42, 0x6b, 0xd1, 0xd6, 0xe1, 0xad } \
  113. }
  114. class Accessible : public nsISupports
  115. {
  116. public:
  117. Accessible(nsIContent* aContent, DocAccessible* aDoc);
  118. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  119. NS_DECL_CYCLE_COLLECTION_CLASS(Accessible)
  120. NS_DECLARE_STATIC_IID_ACCESSOR(NS_ACCESSIBLE_IMPL_IID)
  121. //////////////////////////////////////////////////////////////////////////////
  122. // Public methods
  123. /**
  124. * Return the document accessible for this accessible.
  125. */
  126. DocAccessible* Document() const { return mDoc; }
  127. /**
  128. * Return the root document accessible for this accessible.
  129. */
  130. a11y::RootAccessible* RootAccessible() const;
  131. /**
  132. * Return frame for this accessible.
  133. */
  134. virtual nsIFrame* GetFrame() const;
  135. /**
  136. * Return DOM node associated with the accessible.
  137. */
  138. virtual nsINode* GetNode() const;
  139. inline already_AddRefed<nsIDOMNode> DOMNode() const
  140. {
  141. nsCOMPtr<nsIDOMNode> DOMNode = do_QueryInterface(GetNode());
  142. return DOMNode.forget();
  143. }
  144. nsIContent* GetContent() const { return mContent; }
  145. mozilla::dom::Element* Elm() const
  146. { return mContent && mContent->IsElement() ? mContent->AsElement() : nullptr; }
  147. /**
  148. * Return node type information of DOM node associated with the accessible.
  149. */
  150. bool IsContent() const
  151. { return GetNode() && GetNode()->IsNodeOfType(nsINode::eCONTENT); }
  152. /**
  153. * Return the unique identifier of the accessible.
  154. */
  155. void* UniqueID() { return static_cast<void*>(this); }
  156. /**
  157. * Return language associated with the accessible.
  158. */
  159. void Language(nsAString& aLocale);
  160. /**
  161. * Get the description of this accessible.
  162. */
  163. virtual void Description(nsString& aDescription);
  164. /**
  165. * Get the value of this accessible.
  166. */
  167. virtual void Value(nsString& aValue);
  168. /**
  169. * Get help string for the accessible.
  170. */
  171. void Help(nsString& aHelp) const { aHelp.Truncate(); }
  172. /**
  173. * Get the name of this accessible.
  174. *
  175. * Note: aName.IsVoid() when name was left empty by the author on purpose.
  176. * aName.IsEmpty() when the author missed name, AT can try to repair a name.
  177. */
  178. virtual ENameValueFlag Name(nsString& aName);
  179. /**
  180. * Maps ARIA state attributes to state of accessible. Note the given state
  181. * argument should hold states for accessible before you pass it into this
  182. * method.
  183. *
  184. * @param [in/out] where to fill the states into.
  185. */
  186. virtual void ApplyARIAState(uint64_t* aState) const;
  187. /**
  188. * Return enumerated accessible role (see constants in Role.h).
  189. */
  190. mozilla::a11y::role Role();
  191. /**
  192. * Return true if ARIA role is specified on the element.
  193. */
  194. bool HasARIARole() const;
  195. bool IsARIARole(nsIAtom* aARIARole) const;
  196. bool HasStrongARIARole() const;
  197. /**
  198. * Retrun ARIA role map if any.
  199. */
  200. const nsRoleMapEntry* ARIARoleMap() const;
  201. /**
  202. * Return accessible role specified by ARIA (see constants in
  203. * roles).
  204. */
  205. mozilla::a11y::role ARIARole();
  206. /**
  207. * Return a landmark role if applied.
  208. */
  209. virtual nsIAtom* LandmarkRole() const;
  210. /**
  211. * Returns enumerated accessible role from native markup (see constants in
  212. * Role.h). Doesn't take into account ARIA roles.
  213. */
  214. virtual mozilla::a11y::role NativeRole();
  215. /**
  216. * Return all states of accessible (including ARIA states).
  217. */
  218. virtual uint64_t State();
  219. /**
  220. * Return interactive states present on the accessible
  221. * (@see NativeInteractiveState).
  222. */
  223. uint64_t InteractiveState() const
  224. {
  225. uint64_t state = NativeInteractiveState();
  226. ApplyARIAState(&state);
  227. return state;
  228. }
  229. /**
  230. * Return link states present on the accessible.
  231. */
  232. uint64_t LinkState() const
  233. {
  234. uint64_t state = NativeLinkState();
  235. ApplyARIAState(&state);
  236. return state;
  237. }
  238. /**
  239. * Return if accessible is unavailable.
  240. */
  241. bool Unavailable() const
  242. {
  243. uint64_t state = NativelyUnavailable() ? states::UNAVAILABLE : 0;
  244. ApplyARIAState(&state);
  245. return state & states::UNAVAILABLE;
  246. }
  247. /**
  248. * Return the states of accessible, not taking into account ARIA states.
  249. * Use State() to get complete set of states.
  250. */
  251. virtual uint64_t NativeState();
  252. /**
  253. * Return native interactice state (unavailable, focusable or selectable).
  254. */
  255. virtual uint64_t NativeInteractiveState() const;
  256. /**
  257. * Return native link states present on the accessible.
  258. */
  259. virtual uint64_t NativeLinkState() const;
  260. /**
  261. * Return bit set of invisible and offscreen states.
  262. */
  263. uint64_t VisibilityState();
  264. /**
  265. * Return true if native unavailable state present.
  266. */
  267. virtual bool NativelyUnavailable() const;
  268. /**
  269. * Return object attributes for the accessible.
  270. */
  271. virtual already_AddRefed<nsIPersistentProperties> Attributes();
  272. /**
  273. * Return group position (level, position in set and set size).
  274. */
  275. virtual mozilla::a11y::GroupPos GroupPosition();
  276. /**
  277. * Used by ChildAtPoint() method to get direct or deepest child at point.
  278. */
  279. enum EWhichChildAtPoint {
  280. eDirectChild,
  281. eDeepestChild
  282. };
  283. /**
  284. * Return direct or deepest child at the given point.
  285. *
  286. * @param aX [in] x coordinate relative screen
  287. * @param aY [in] y coordinate relative screen
  288. * @param aWhichChild [in] flag points if deepest or direct child
  289. * should be returned
  290. */
  291. virtual Accessible* ChildAtPoint(int32_t aX, int32_t aY,
  292. EWhichChildAtPoint aWhichChild);
  293. /**
  294. * Return the focused child if any.
  295. */
  296. virtual Accessible* FocusedChild();
  297. /**
  298. * Return calculated group level based on accessible hierarchy.
  299. */
  300. virtual int32_t GetLevelInternal();
  301. /**
  302. * Calculate position in group and group size ('posinset' and 'setsize') based
  303. * on accessible hierarchy.
  304. *
  305. * @param aPosInSet [out] accessible position in the group
  306. * @param aSetSize [out] the group size
  307. */
  308. virtual void GetPositionAndSizeInternal(int32_t *aPosInSet,
  309. int32_t *aSetSize);
  310. /**
  311. * Get the relation of the given type.
  312. */
  313. virtual Relation RelationByType(RelationType aType);
  314. //////////////////////////////////////////////////////////////////////////////
  315. // Initializing methods
  316. /**
  317. * Shutdown this accessible object.
  318. */
  319. virtual void Shutdown();
  320. /**
  321. * Set the ARIA role map entry for a new accessible.
  322. */
  323. void SetRoleMapEntry(const nsRoleMapEntry* aRoleMapEntry);
  324. /**
  325. * Append/insert/remove a child. Return true if operation was successful.
  326. */
  327. bool AppendChild(Accessible* aChild)
  328. { return InsertChildAt(mChildren.Length(), aChild); }
  329. virtual bool InsertChildAt(uint32_t aIndex, Accessible* aChild);
  330. /**
  331. * Inserts a child after given sibling. If the child cannot be inserted,
  332. * then the child is unbound from the document, and false is returned. Make
  333. * sure to null out any references on the child object as it may be destroyed.
  334. */
  335. bool InsertAfter(Accessible* aNewChild, Accessible* aRefChild);
  336. virtual bool RemoveChild(Accessible* aChild);
  337. /**
  338. * Reallocates the child withing its parent.
  339. */
  340. void MoveChild(uint32_t aNewIndex, Accessible* aChild);
  341. //////////////////////////////////////////////////////////////////////////////
  342. // Accessible tree traverse methods
  343. /**
  344. * Return parent accessible.
  345. */
  346. Accessible* Parent() const { return mParent; }
  347. /**
  348. * Return child accessible at the given index.
  349. */
  350. virtual Accessible* GetChildAt(uint32_t aIndex) const;
  351. /**
  352. * Return child accessible count.
  353. */
  354. virtual uint32_t ChildCount() const;
  355. /**
  356. * Return index of the given child accessible.
  357. */
  358. int32_t GetIndexOf(const Accessible* aChild) const
  359. { return (aChild->mParent != this) ? -1 : aChild->IndexInParent(); }
  360. /**
  361. * Return index in parent accessible.
  362. */
  363. virtual int32_t IndexInParent() const;
  364. /**
  365. * Return true if accessible has children;
  366. */
  367. bool HasChildren() { return !!GetChildAt(0); }
  368. /**
  369. * Return first/last/next/previous sibling of the accessible.
  370. */
  371. inline Accessible* NextSibling() const
  372. { return GetSiblingAtOffset(1); }
  373. inline Accessible* PrevSibling() const
  374. { return GetSiblingAtOffset(-1); }
  375. inline Accessible* FirstChild()
  376. { return GetChildAt(0); }
  377. inline Accessible* LastChild()
  378. {
  379. uint32_t childCount = ChildCount();
  380. return childCount != 0 ? GetChildAt(childCount - 1) : nullptr;
  381. }
  382. /**
  383. * Return embedded accessible children count.
  384. */
  385. uint32_t EmbeddedChildCount();
  386. /**
  387. * Return embedded accessible child at the given index.
  388. */
  389. Accessible* GetEmbeddedChildAt(uint32_t aIndex);
  390. /**
  391. * Return index of the given embedded accessible child.
  392. */
  393. int32_t GetIndexOfEmbeddedChild(Accessible* aChild);
  394. /**
  395. * Return number of content children/content child at index. The content
  396. * child is created from markup in contrast to it's never constructed by its
  397. * parent accessible (like treeitem accessibles for XUL trees).
  398. */
  399. uint32_t ContentChildCount() const { return mChildren.Length(); }
  400. Accessible* ContentChildAt(uint32_t aIndex) const
  401. { return mChildren.ElementAt(aIndex); }
  402. /**
  403. * Return true if the accessible is attached to tree.
  404. */
  405. bool IsBoundToParent() const { return !!mParent; }
  406. //////////////////////////////////////////////////////////////////////////////
  407. // Miscellaneous methods
  408. /**
  409. * Handle accessible event, i.e. process it, notifies observers and fires
  410. * platform specific event.
  411. */
  412. virtual nsresult HandleAccEvent(AccEvent* aAccEvent);
  413. /**
  414. * Return true if the accessible is an acceptable child.
  415. */
  416. virtual bool IsAcceptableChild(nsIContent* aEl) const
  417. { return true; }
  418. /**
  419. * Returns text of accessible if accessible has text role otherwise empty
  420. * string.
  421. *
  422. * @param aText [in] returned text of the accessible
  423. * @param aStartOffset [in, optional] start offset inside of the accessible,
  424. * if missed entire text is appended
  425. * @param aLength [in, optional] required length of text, if missed
  426. * then text form start offset till the end is appended
  427. */
  428. virtual void AppendTextTo(nsAString& aText, uint32_t aStartOffset = 0,
  429. uint32_t aLength = UINT32_MAX);
  430. /**
  431. * Return boundaries in screen coordinates.
  432. */
  433. virtual nsIntRect Bounds() const;
  434. /**
  435. * Return boundaries rect relative the bounding frame.
  436. */
  437. virtual nsRect RelativeBounds(nsIFrame** aRelativeFrame) const;
  438. /**
  439. * Selects the accessible within its container if applicable.
  440. */
  441. virtual void SetSelected(bool aSelect);
  442. /**
  443. * Select the accessible within its container.
  444. */
  445. void TakeSelection();
  446. /**
  447. * Focus the accessible.
  448. */
  449. virtual void TakeFocus();
  450. /**
  451. * Scroll the accessible into view.
  452. */
  453. void ScrollTo(uint32_t aHow) const;
  454. /**
  455. * Scroll the accessible to the given point.
  456. */
  457. void ScrollToPoint(uint32_t aCoordinateType, int32_t aX, int32_t aY);
  458. /**
  459. * Get a pointer to accessibility interface for this node, which is specific
  460. * to the OS/accessibility toolkit we're running on.
  461. */
  462. virtual void GetNativeInterface(void** aNativeAccessible);
  463. //////////////////////////////////////////////////////////////////////////////
  464. // Downcasting and types
  465. inline bool IsAbbreviation() const
  466. {
  467. return mContent->IsAnyOfHTMLElements(nsGkAtoms::abbr, nsGkAtoms::acronym);
  468. }
  469. bool IsAlert() const { return HasGenericType(eAlert); }
  470. bool IsApplication() const { return mType == eApplicationType; }
  471. ApplicationAccessible* AsApplication();
  472. bool IsAutoComplete() const { return HasGenericType(eAutoComplete); }
  473. bool IsAutoCompletePopup() const
  474. { return HasGenericType(eAutoCompletePopup); }
  475. bool IsButton() const { return HasGenericType(eButton); }
  476. bool IsCombobox() const { return HasGenericType(eCombobox); }
  477. bool IsDoc() const { return HasGenericType(eDocument); }
  478. DocAccessible* AsDoc();
  479. bool IsGenericHyperText() const { return mType == eHyperTextType; }
  480. bool IsHyperText() const { return HasGenericType(eHyperText); }
  481. HyperTextAccessible* AsHyperText();
  482. bool IsHTMLBr() const { return mType == eHTMLBRType; }
  483. bool IsHTMLCaption() const { return mType == eHTMLCaptionType; }
  484. bool IsHTMLCombobox() const { return mType == eHTMLComboboxType; }
  485. bool IsHTMLFileInput() const { return mType == eHTMLFileInputType; }
  486. bool IsHTMLListItem() const { return mType == eHTMLLiType; }
  487. HTMLLIAccessible* AsHTMLListItem();
  488. bool IsHTMLOptGroup() const { return mType == eHTMLOptGroupType; }
  489. bool IsHTMLTable() const { return mType == eHTMLTableType; }
  490. bool IsHTMLTableRow() const { return mType == eHTMLTableRowType; }
  491. bool IsImage() const { return mType == eImageType; }
  492. ImageAccessible* AsImage();
  493. bool IsImageMap() const { return mType == eImageMapType; }
  494. HTMLImageMapAccessible* AsImageMap();
  495. bool IsList() const { return HasGenericType(eList); }
  496. bool IsListControl() const { return HasGenericType(eListControl); }
  497. bool IsMenuButton() const { return HasGenericType(eMenuButton); }
  498. bool IsMenuPopup() const { return mType == eMenuPopupType; }
  499. bool IsProxy() const { return mType == eProxyType; }
  500. ProxyAccessible* Proxy() const
  501. {
  502. MOZ_ASSERT(IsProxy());
  503. return mBits.proxy;
  504. }
  505. uint32_t ProxyInterfaces() const
  506. {
  507. MOZ_ASSERT(IsProxy());
  508. return mInt.mProxyInterfaces;
  509. }
  510. void SetProxyInterfaces(uint32_t aInterfaces)
  511. {
  512. MOZ_ASSERT(IsProxy());
  513. mInt.mProxyInterfaces = aInterfaces;
  514. }
  515. bool IsOuterDoc() const { return mType == eOuterDocType; }
  516. OuterDocAccessible* AsOuterDoc();
  517. bool IsProgress() const { return mType == eProgressType; }
  518. bool IsRoot() const { return mType == eRootType; }
  519. a11y::RootAccessible* AsRoot();
  520. bool IsSearchbox() const;
  521. bool IsSelect() const { return HasGenericType(eSelect); }
  522. bool IsTable() const { return HasGenericType(eTable); }
  523. virtual TableAccessible* AsTable() { return nullptr; }
  524. bool IsTableCell() const { return HasGenericType(eTableCell); }
  525. virtual TableCellAccessible* AsTableCell() { return nullptr; }
  526. const TableCellAccessible* AsTableCell() const
  527. { return const_cast<Accessible*>(this)->AsTableCell(); }
  528. bool IsTableRow() const { return HasGenericType(eTableRow); }
  529. bool IsTextField() const { return mType == eHTMLTextFieldType; }
  530. bool IsText() const { return mGenericTypes & eText; }
  531. bool IsTextLeaf() const { return mType == eTextLeafType; }
  532. TextLeafAccessible* AsTextLeaf();
  533. bool IsXULLabel() const { return mType == eXULLabelType; }
  534. XULLabelAccessible* AsXULLabel();
  535. bool IsXULListItem() const { return mType == eXULListItemType; }
  536. bool IsXULTabpanels() const { return mType == eXULTabpanelsType; }
  537. bool IsXULTree() const { return mType == eXULTreeType; }
  538. XULTreeAccessible* AsXULTree();
  539. /**
  540. * Return true if the accessible belongs to the given accessible type.
  541. */
  542. bool HasGenericType(AccGenericType aType) const;
  543. //////////////////////////////////////////////////////////////////////////////
  544. // ActionAccessible
  545. /**
  546. * Return the number of actions that can be performed on this accessible.
  547. */
  548. virtual uint8_t ActionCount();
  549. /**
  550. * Return action name at given index.
  551. */
  552. virtual void ActionNameAt(uint8_t aIndex, nsAString& aName);
  553. /**
  554. * Default to localized action name.
  555. */
  556. void ActionDescriptionAt(uint8_t aIndex, nsAString& aDescription)
  557. {
  558. nsAutoString name;
  559. ActionNameAt(aIndex, name);
  560. TranslateString(name, aDescription);
  561. }
  562. /**
  563. * Invoke the accessible action.
  564. */
  565. virtual bool DoAction(uint8_t aIndex);
  566. /**
  567. * Return access key, such as Alt+D.
  568. */
  569. virtual KeyBinding AccessKey() const;
  570. /**
  571. * Return global keyboard shortcut for default action, such as Ctrl+O for
  572. * Open file menuitem.
  573. */
  574. virtual KeyBinding KeyboardShortcut() const;
  575. //////////////////////////////////////////////////////////////////////////////
  576. // HyperLinkAccessible (any embedded object in text can implement HyperLink,
  577. // which helps determine where it is located within containing text).
  578. /**
  579. * Return true if the accessible is hyper link accessible.
  580. */
  581. virtual bool IsLink();
  582. /**
  583. * Return the start offset of the link within the parent accessible.
  584. */
  585. virtual uint32_t StartOffset();
  586. /**
  587. * Return the end offset of the link within the parent accessible.
  588. */
  589. virtual uint32_t EndOffset();
  590. /**
  591. * Return true if the link is valid (e. g. points to a valid URL).
  592. */
  593. inline bool IsLinkValid()
  594. {
  595. NS_PRECONDITION(IsLink(), "IsLinkValid is called on not hyper link!");
  596. // XXX In order to implement this we would need to follow every link
  597. // Perhaps we can get information about invalid links from the cache
  598. // In the mean time authors can use role="link" aria-invalid="true"
  599. // to force it for links they internally know to be invalid
  600. return (0 == (State() & mozilla::a11y::states::INVALID));
  601. }
  602. /**
  603. * Return the number of anchors within the link.
  604. */
  605. virtual uint32_t AnchorCount();
  606. /**
  607. * Returns an anchor accessible at the given index.
  608. */
  609. virtual Accessible* AnchorAt(uint32_t aAnchorIndex);
  610. /**
  611. * Returns an anchor URI at the given index.
  612. */
  613. virtual already_AddRefed<nsIURI> AnchorURIAt(uint32_t aAnchorIndex);
  614. /**
  615. * Returns a text point for the accessible element.
  616. */
  617. void ToTextPoint(HyperTextAccessible** aContainer, int32_t* aOffset,
  618. bool aIsBefore = true) const;
  619. //////////////////////////////////////////////////////////////////////////////
  620. // SelectAccessible
  621. /**
  622. * Return an array of selected items.
  623. */
  624. virtual void SelectedItems(nsTArray<Accessible*>* aItems);
  625. /**
  626. * Return the number of selected items.
  627. */
  628. virtual uint32_t SelectedItemCount();
  629. /**
  630. * Return selected item at the given index.
  631. */
  632. virtual Accessible* GetSelectedItem(uint32_t aIndex);
  633. /**
  634. * Determine if item at the given index is selected.
  635. */
  636. virtual bool IsItemSelected(uint32_t aIndex);
  637. /**
  638. * Add item at the given index the selection. Return true if success.
  639. */
  640. virtual bool AddItemToSelection(uint32_t aIndex);
  641. /**
  642. * Remove item at the given index from the selection. Return if success.
  643. */
  644. virtual bool RemoveItemFromSelection(uint32_t aIndex);
  645. /**
  646. * Select all items. Return true if success.
  647. */
  648. virtual bool SelectAll();
  649. /**
  650. * Unselect all items. Return true if success.
  651. */
  652. virtual bool UnselectAll();
  653. //////////////////////////////////////////////////////////////////////////////
  654. // Value (numeric value interface)
  655. virtual double MaxValue() const;
  656. virtual double MinValue() const;
  657. virtual double CurValue() const;
  658. virtual double Step() const;
  659. virtual bool SetCurValue(double aValue);
  660. //////////////////////////////////////////////////////////////////////////////
  661. // Widgets
  662. /**
  663. * Return true if accessible is a widget, i.e. control or accessible that
  664. * manages its items. Note, being a widget the accessible may be a part of
  665. * composite widget.
  666. */
  667. virtual bool IsWidget() const;
  668. /**
  669. * Return true if the widget is active, i.e. has a focus within it.
  670. */
  671. virtual bool IsActiveWidget() const;
  672. /**
  673. * Return true if the widget has items and items are operable by user and
  674. * can be activated.
  675. */
  676. virtual bool AreItemsOperable() const;
  677. /**
  678. * Return the current item of the widget, i.e. an item that has or will have
  679. * keyboard focus when widget gets active.
  680. */
  681. virtual Accessible* CurrentItem();
  682. /**
  683. * Set the current item of the widget.
  684. */
  685. virtual void SetCurrentItem(Accessible* aItem);
  686. /**
  687. * Return container widget this accessible belongs to.
  688. */
  689. virtual Accessible* ContainerWidget() const;
  690. /**
  691. * Return the localized string for the given key.
  692. */
  693. static void TranslateString(const nsString& aKey, nsAString& aStringOut);
  694. /**
  695. * Return true if the accessible is defunct.
  696. */
  697. bool IsDefunct() const { return mStateFlags & eIsDefunct; }
  698. /**
  699. * Return false if the accessible is no longer in the document.
  700. */
  701. bool IsInDocument() const { return !(mStateFlags & eIsNotInDocument); }
  702. /**
  703. * Return true if the accessible should be contained by document node map.
  704. */
  705. bool IsNodeMapEntry() const
  706. { return HasOwnContent() && !(mStateFlags & eNotNodeMapEntry); }
  707. /**
  708. * Return true if the accessible's group info needs to be updated.
  709. */
  710. inline bool HasDirtyGroupInfo() const { return mStateFlags & eGroupInfoDirty; }
  711. /**
  712. * Return true if the accessible has associated DOM content.
  713. */
  714. bool HasOwnContent() const
  715. { return mContent && !(mStateFlags & eSharedNode); }
  716. /**
  717. * Return true if the accessible has a numeric value.
  718. */
  719. bool HasNumericValue() const;
  720. /**
  721. * Return true if the accessible state change is processed by handling proper
  722. * DOM UI event, if otherwise then false. For example, HTMLCheckboxAccessible
  723. * process nsIDocumentObserver::ContentStateChanged instead
  724. * 'CheckboxStateChange' event.
  725. */
  726. bool NeedsDOMUIEvent() const
  727. { return !(mStateFlags & eIgnoreDOMUIEvent); }
  728. /**
  729. * Get/set survivingInUpdate bit on child indicating that parent recollects
  730. * its children.
  731. */
  732. bool IsSurvivingInUpdate() const { return mStateFlags & eSurvivingInUpdate; }
  733. void SetSurvivingInUpdate(bool aIsSurviving)
  734. {
  735. if (aIsSurviving)
  736. mStateFlags |= eSurvivingInUpdate;
  737. else
  738. mStateFlags &= ~eSurvivingInUpdate;
  739. }
  740. /**
  741. * Get/set repositioned bit indicating that the accessible was moved in
  742. * the accessible tree, i.e. the accessible tree structure differs from DOM.
  743. */
  744. bool IsRelocated() const { return mStateFlags & eRelocated; }
  745. void SetRelocated(bool aRelocated)
  746. {
  747. if (aRelocated)
  748. mStateFlags |= eRelocated;
  749. else
  750. mStateFlags &= ~eRelocated;
  751. }
  752. /**
  753. * Return true if the accessible doesn't allow accessible children from XBL
  754. * anonymous subtree.
  755. */
  756. bool NoXBLKids() const { return mStateFlags & eNoXBLKids; }
  757. /**
  758. * Return true if the accessible allows accessible children from subtree of
  759. * a DOM element of this accessible.
  760. */
  761. bool KidsFromDOM() const { return !(mStateFlags & eNoKidsFromDOM); }
  762. /**
  763. * Return true if this accessible has a parent whose name depends on this
  764. * accessible.
  765. */
  766. bool HasNameDependentParent() const
  767. { return mContextFlags & eHasNameDependentParent; }
  768. /**
  769. * Return true if aria-hidden="true" is applied to the accessible or inherited
  770. * from the parent.
  771. */
  772. bool IsARIAHidden() const { return mContextFlags & eARIAHidden; }
  773. void SetARIAHidden(bool aIsDefined);
  774. /**
  775. * Return true if the element is inside an alert.
  776. */
  777. bool IsInsideAlert() const { return mContextFlags & eInsideAlert; }
  778. /**
  779. * Return true if there is a pending reorder event for this accessible.
  780. */
  781. bool ReorderEventTarget() const { return mReorderEventTarget; }
  782. /**
  783. * Return true if there is a pending show event for this accessible.
  784. */
  785. bool ShowEventTarget() const { return mShowEventTarget; }
  786. /**
  787. * Return true if there is a pending hide event for this accessible.
  788. */
  789. bool HideEventTarget() const { return mHideEventTarget; }
  790. /**
  791. * Set if there is a pending reorder event for this accessible.
  792. */
  793. void SetReorderEventTarget(bool aTarget) { mReorderEventTarget = aTarget; }
  794. /**
  795. * Set if this accessible is a show event target.
  796. */
  797. void SetShowEventTarget(bool aTarget) { mShowEventTarget = aTarget; }
  798. /**
  799. * Set if this accessible is a hide event target.
  800. */
  801. void SetHideEventTarget(bool aTarget) { mHideEventTarget = aTarget; }
  802. protected:
  803. virtual ~Accessible();
  804. /**
  805. * Return the accessible name provided by native markup. It doesn't take
  806. * into account ARIA markup used to specify the name.
  807. */
  808. virtual mozilla::a11y::ENameValueFlag NativeName(nsString& aName);
  809. /**
  810. * Return the accessible description provided by native markup. It doesn't take
  811. * into account ARIA markup used to specify the description.
  812. */
  813. virtual void NativeDescription(nsString& aDescription);
  814. /**
  815. * Return object attributes provided by native markup. It doesn't take into
  816. * account ARIA.
  817. */
  818. virtual already_AddRefed<nsIPersistentProperties> NativeAttributes();
  819. //////////////////////////////////////////////////////////////////////////////
  820. // Initializing, cache and tree traverse methods
  821. /**
  822. * Destroy the object.
  823. */
  824. void LastRelease();
  825. /**
  826. * Set accessible parent and index in parent.
  827. */
  828. void BindToParent(Accessible* aParent, uint32_t aIndexInParent);
  829. void UnbindFromParent();
  830. /**
  831. * Return sibling accessible at the given offset.
  832. */
  833. virtual Accessible* GetSiblingAtOffset(int32_t aOffset,
  834. nsresult *aError = nullptr) const;
  835. /**
  836. * Flags used to describe the state of this accessible.
  837. */
  838. enum StateFlags {
  839. eIsDefunct = 1 << 0, // accessible is defunct
  840. eIsNotInDocument = 1 << 1, // accessible is not in document
  841. eSharedNode = 1 << 2, // accessible shares DOM node from another accessible
  842. eNotNodeMapEntry = 1 << 3, // accessible shouldn't be in document node map
  843. eHasNumericValue = 1 << 4, // accessible has a numeric value
  844. eGroupInfoDirty = 1 << 5, // accessible needs to update group info
  845. eKidsMutating = 1 << 6, // subtree is being mutated
  846. eIgnoreDOMUIEvent = 1 << 7, // don't process DOM UI events for a11y events
  847. eSurvivingInUpdate = 1 << 8, // parent drops children to recollect them
  848. eRelocated = 1 << 9, // accessible was moved in tree
  849. eNoXBLKids = 1 << 10, // accessible don't allows XBL children
  850. eNoKidsFromDOM = 1 << 11, // accessible doesn't allow children from DOM
  851. eHasTextKids = 1 << 12, // accessible have a text leaf in children
  852. eLastStateFlag = eNoKidsFromDOM
  853. };
  854. /**
  855. * Flags used for contextual information about the accessible.
  856. */
  857. enum ContextFlags {
  858. eHasNameDependentParent = 1 << 0, // Parent's name depends on this accessible.
  859. eARIAHidden = 1 << 1,
  860. eInsideAlert = 1 << 2,
  861. eLastContextFlag = eInsideAlert
  862. };
  863. protected:
  864. //////////////////////////////////////////////////////////////////////////////
  865. // Miscellaneous helpers
  866. /**
  867. * Return ARIA role (helper method).
  868. */
  869. mozilla::a11y::role ARIATransformRole(mozilla::a11y::role aRole);
  870. //////////////////////////////////////////////////////////////////////////////
  871. // Name helpers
  872. /**
  873. * Returns the accessible name specified by ARIA.
  874. */
  875. void ARIAName(nsString& aName);
  876. /**
  877. * Return the name for XUL element.
  878. */
  879. static void XULElmName(DocAccessible* aDocument,
  880. nsIContent* aElm, nsString& aName);
  881. // helper method to verify frames
  882. static nsresult GetFullKeyName(const nsAString& aModifierName, const nsAString& aKeyName, nsAString& aStringOut);
  883. //////////////////////////////////////////////////////////////////////////////
  884. // Action helpers
  885. /**
  886. * Prepares click action that will be invoked in timeout.
  887. *
  888. * @note DoCommand() prepares an action in timeout because when action
  889. * command opens a modal dialog/window, it won't return until the
  890. * dialog/window is closed. If executing action command directly in
  891. * nsIAccessible::DoAction() method, it will block AT tools (e.g. GOK) that
  892. * invoke action of mozilla accessibles direclty (see bug 277888 for details).
  893. *
  894. * @param aContent [in, optional] element to click
  895. * @param aActionIndex [in, optional] index of accessible action
  896. */
  897. void DoCommand(nsIContent *aContent = nullptr, uint32_t aActionIndex = 0);
  898. /**
  899. * Dispatch click event.
  900. */
  901. virtual void DispatchClickEvent(nsIContent *aContent, uint32_t aActionIndex);
  902. //////////////////////////////////////////////////////////////////////////////
  903. // Helpers
  904. /**
  905. * Get the container node for an atomic region, defined by aria-atomic="true"
  906. * @return the container node
  907. */
  908. nsIContent* GetAtomicRegion() const;
  909. /**
  910. * Return numeric value of the given ARIA attribute, NaN if not applicable.
  911. *
  912. * @param aARIAProperty [in] the ARIA property we're using
  913. * @return a numeric value
  914. */
  915. double AttrNumericValue(nsIAtom* aARIAAttr) const;
  916. /**
  917. * Return the action rule based on ARIA enum constants EActionRule
  918. * (see ARIAMap.h). Used by ActionCount() and ActionNameAt().
  919. */
  920. uint32_t GetActionRule() const;
  921. /**
  922. * Return group info.
  923. */
  924. AccGroupInfo* GetGroupInfo();
  925. // Data Members
  926. nsCOMPtr<nsIContent> mContent;
  927. RefPtr<DocAccessible> mDoc;
  928. Accessible* mParent;
  929. nsTArray<Accessible*> mChildren;
  930. int32_t mIndexInParent;
  931. static const uint8_t kStateFlagsBits = 13;
  932. static const uint8_t kContextFlagsBits = 3;
  933. static const uint8_t kTypeBits = 6;
  934. static const uint8_t kGenericTypesBits = 16;
  935. /**
  936. * Non-NO_ROLE_MAP_ENTRY_INDEX indicates author-supplied role;
  937. * possibly state & value as well
  938. */
  939. uint8_t mRoleMapEntryIndex;
  940. /**
  941. * Keep in sync with StateFlags, ContextFlags, and AccTypes.
  942. */
  943. uint32_t mStateFlags : kStateFlagsBits;
  944. uint32_t mContextFlags : kContextFlagsBits;
  945. uint32_t mType : kTypeBits;
  946. uint32_t mGenericTypes : kGenericTypesBits;
  947. uint32_t mReorderEventTarget : 1;
  948. uint32_t mShowEventTarget : 1;
  949. uint32_t mHideEventTarget : 1;
  950. void StaticAsserts() const;
  951. #ifdef A11Y_LOG
  952. friend void logging::Tree(const char* aTitle, const char* aMsgText,
  953. Accessible* aRoot,
  954. logging::GetTreePrefix aPrefixFunc,
  955. void* aGetTreePrefixData);
  956. #endif
  957. friend class DocAccessible;
  958. friend class xpcAccessible;
  959. friend class TreeMutation;
  960. UniquePtr<mozilla::a11y::EmbeddedObjCollector> mEmbeddedObjCollector;
  961. union {
  962. int32_t mIndexOfEmbeddedChild;
  963. uint32_t mProxyInterfaces;
  964. } mInt;
  965. friend class EmbeddedObjCollector;
  966. union
  967. {
  968. AccGroupInfo* groupInfo;
  969. ProxyAccessible* proxy;
  970. } mBits;
  971. friend class AccGroupInfo;
  972. private:
  973. Accessible() = delete;
  974. Accessible(const Accessible&) = delete;
  975. Accessible& operator =(const Accessible&) = delete;
  976. };
  977. NS_DEFINE_STATIC_IID_ACCESSOR(Accessible,
  978. NS_ACCESSIBLE_IMPL_IID)
  979. /**
  980. * Represent key binding associated with accessible (such as access key and
  981. * global keyboard shortcuts).
  982. */
  983. class KeyBinding
  984. {
  985. public:
  986. /**
  987. * Modifier mask values.
  988. */
  989. static const uint32_t kShift = 1;
  990. static const uint32_t kControl = 2;
  991. static const uint32_t kAlt = 4;
  992. static const uint32_t kMeta = 8;
  993. static const uint32_t kOS = 16;
  994. static uint32_t AccelModifier();
  995. KeyBinding() : mKey(0), mModifierMask(0) {}
  996. KeyBinding(uint32_t aKey, uint32_t aModifierMask) :
  997. mKey(aKey), mModifierMask(aModifierMask) {}
  998. inline bool IsEmpty() const { return !mKey; }
  999. inline uint32_t Key() const { return mKey; }
  1000. inline uint32_t ModifierMask() const { return mModifierMask; }
  1001. enum Format {
  1002. ePlatformFormat,
  1003. eAtkFormat
  1004. };
  1005. /**
  1006. * Return formatted string for this key binding depending on the given format.
  1007. */
  1008. inline void ToString(nsAString& aValue,
  1009. Format aFormat = ePlatformFormat) const
  1010. {
  1011. aValue.Truncate();
  1012. AppendToString(aValue, aFormat);
  1013. }
  1014. inline void AppendToString(nsAString& aValue,
  1015. Format aFormat = ePlatformFormat) const
  1016. {
  1017. if (mKey) {
  1018. if (aFormat == ePlatformFormat)
  1019. ToPlatformFormat(aValue);
  1020. else
  1021. ToAtkFormat(aValue);
  1022. }
  1023. }
  1024. private:
  1025. void ToPlatformFormat(nsAString& aValue) const;
  1026. void ToAtkFormat(nsAString& aValue) const;
  1027. uint32_t mKey;
  1028. uint32_t mModifierMask;
  1029. };
  1030. } // namespace a11y
  1031. } // namespace mozilla
  1032. #endif