The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

549 lines
21KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wlanguage-extension-token")
  16. int AccessibilityNativeHandle::idCounter = 0;
  17. //==============================================================================
  18. static String getAutomationId (const AccessibilityHandler& handler)
  19. {
  20. auto result = handler.getTitle();
  21. auto* parentComponent = handler.getComponent().getParentComponent();
  22. while (parentComponent != nullptr)
  23. {
  24. if (auto* parentHandler = parentComponent->getAccessibilityHandler())
  25. {
  26. auto parentTitle = parentHandler->getTitle();
  27. result << "." << (parentTitle.isNotEmpty() ? parentTitle : "<empty>");
  28. }
  29. parentComponent = parentComponent->getParentComponent();
  30. }
  31. return result;
  32. }
  33. static auto roleToControlTypeId (AccessibilityRole roleType)
  34. {
  35. switch (roleType)
  36. {
  37. case AccessibilityRole::popupMenu:
  38. case AccessibilityRole::dialogWindow:
  39. case AccessibilityRole::splashScreen:
  40. case AccessibilityRole::window: return ComTypes::UIA_WindowControlTypeId;
  41. case AccessibilityRole::label:
  42. case AccessibilityRole::staticText: return ComTypes::UIA_TextControlTypeId;
  43. case AccessibilityRole::column:
  44. case AccessibilityRole::row: return ComTypes::UIA_HeaderItemControlTypeId;
  45. case AccessibilityRole::button: return ComTypes::UIA_ButtonControlTypeId;
  46. case AccessibilityRole::toggleButton: return ComTypes::UIA_CheckBoxControlTypeId;
  47. case AccessibilityRole::radioButton: return ComTypes::UIA_RadioButtonControlTypeId;
  48. case AccessibilityRole::comboBox: return ComTypes::UIA_ComboBoxControlTypeId;
  49. case AccessibilityRole::image: return ComTypes::UIA_ImageControlTypeId;
  50. case AccessibilityRole::slider: return ComTypes::UIA_SliderControlTypeId;
  51. case AccessibilityRole::editableText: return ComTypes::UIA_EditControlTypeId;
  52. case AccessibilityRole::menuItem: return ComTypes::UIA_MenuItemControlTypeId;
  53. case AccessibilityRole::menuBar: return ComTypes::UIA_MenuBarControlTypeId;
  54. case AccessibilityRole::table: return ComTypes::UIA_TableControlTypeId;
  55. case AccessibilityRole::tableHeader: return ComTypes::UIA_HeaderControlTypeId;
  56. case AccessibilityRole::cell: return ComTypes::UIA_DataItemControlTypeId;
  57. case AccessibilityRole::hyperlink: return ComTypes::UIA_HyperlinkControlTypeId;
  58. case AccessibilityRole::list: return ComTypes::UIA_ListControlTypeId;
  59. case AccessibilityRole::listItem: return ComTypes::UIA_ListItemControlTypeId;
  60. case AccessibilityRole::tree: return ComTypes::UIA_TreeControlTypeId;
  61. case AccessibilityRole::treeItem: return ComTypes::UIA_TreeItemControlTypeId;
  62. case AccessibilityRole::progressBar: return ComTypes::UIA_ProgressBarControlTypeId;
  63. case AccessibilityRole::group: return ComTypes::UIA_GroupControlTypeId;
  64. case AccessibilityRole::scrollBar: return ComTypes::UIA_ScrollBarControlTypeId;
  65. case AccessibilityRole::tooltip: return ComTypes::UIA_ToolTipControlTypeId;
  66. case AccessibilityRole::ignored:
  67. case AccessibilityRole::unspecified: break;
  68. };
  69. return ComTypes::UIA_CustomControlTypeId;
  70. }
  71. //==============================================================================
  72. AccessibilityNativeHandle::AccessibilityNativeHandle (AccessibilityHandler& handler)
  73. : ComBaseClassHelper (0),
  74. accessibilityHandler (handler)
  75. {
  76. }
  77. //==============================================================================
  78. JUCE_COMRESULT AccessibilityNativeHandle::QueryInterface (REFIID refId, void** result)
  79. {
  80. *result = nullptr;
  81. if (! isElementValid())
  82. return (HRESULT) UIA_E_ELEMENTNOTAVAILABLE;
  83. if ((refId == __uuidof (ComTypes::IRawElementProviderFragmentRoot) && ! isFragmentRoot()))
  84. return E_NOINTERFACE;
  85. return ComBaseClassHelper::QueryInterface (refId, result);
  86. }
  87. //==============================================================================
  88. JUCE_COMRESULT AccessibilityNativeHandle::get_HostRawElementProvider (IRawElementProviderSimple** pRetVal)
  89. {
  90. return withCheckedComArgs (pRetVal, *this, [&]
  91. {
  92. if (isFragmentRoot())
  93. if (auto* wrapper = WindowsUIAWrapper::getInstanceWithoutCreating())
  94. return wrapper->hostProviderFromHwnd ((HWND) accessibilityHandler.getComponent().getWindowHandle(), pRetVal);
  95. return S_OK;
  96. });
  97. }
  98. JUCE_COMRESULT AccessibilityNativeHandle::get_ProviderOptions (ProviderOptions* options)
  99. {
  100. if (options == nullptr)
  101. return E_INVALIDARG;
  102. *options = (ProviderOptions) (ProviderOptions_ServerSideProvider | ProviderOptions_UseComThreading);
  103. return S_OK;
  104. }
  105. JUCE_COMRESULT AccessibilityNativeHandle::GetPatternProvider (PATTERNID pId, IUnknown** pRetVal)
  106. {
  107. return withCheckedComArgs (pRetVal, *this, [&]
  108. {
  109. *pRetVal = [&]() -> IUnknown*
  110. {
  111. const auto role = accessibilityHandler.getRole();
  112. const auto fragmentRoot = isFragmentRoot();
  113. switch (pId)
  114. {
  115. case ComTypes::UIA_WindowPatternId:
  116. {
  117. if (fragmentRoot)
  118. return new UIAWindowProvider (this);
  119. break;
  120. }
  121. case ComTypes::UIA_TransformPatternId:
  122. {
  123. if (fragmentRoot)
  124. return new UIATransformProvider (this);
  125. break;
  126. }
  127. case ComTypes::UIA_TextPatternId:
  128. case ComTypes::UIA_TextPattern2Id:
  129. {
  130. if (accessibilityHandler.getTextInterface() != nullptr)
  131. return new UIATextProvider (this);
  132. break;
  133. }
  134. case ComTypes::UIA_ValuePatternId:
  135. {
  136. if (accessibilityHandler.getValueInterface() != nullptr)
  137. return new UIAValueProvider (this);
  138. break;
  139. }
  140. case ComTypes::UIA_RangeValuePatternId:
  141. {
  142. if (accessibilityHandler.getValueInterface() != nullptr
  143. && accessibilityHandler.getValueInterface()->getRange().isValid())
  144. {
  145. return new UIARangeValueProvider (this);
  146. }
  147. break;
  148. }
  149. case ComTypes::UIA_TogglePatternId:
  150. {
  151. if (accessibilityHandler.getCurrentState().isCheckable()
  152. && (accessibilityHandler.getActions().contains (AccessibilityActionType::toggle)
  153. || accessibilityHandler.getActions().contains (AccessibilityActionType::press)))
  154. {
  155. return new UIAToggleProvider (this);
  156. }
  157. break;
  158. }
  159. case ComTypes::UIA_SelectionPatternId:
  160. {
  161. if (role == AccessibilityRole::list
  162. || role == AccessibilityRole::popupMenu
  163. || role == AccessibilityRole::tree)
  164. {
  165. return new UIASelectionProvider (this);
  166. }
  167. break;
  168. }
  169. case ComTypes::UIA_SelectionItemPatternId:
  170. {
  171. auto state = accessibilityHandler.getCurrentState();
  172. if (state.isSelectable() || state.isMultiSelectable()
  173. || role == AccessibilityRole::radioButton)
  174. {
  175. return new UIASelectionItemProvider (this);
  176. }
  177. break;
  178. }
  179. case ComTypes::UIA_GridPatternId:
  180. {
  181. if (accessibilityHandler.getTableInterface() != nullptr)
  182. return new UIAGridProvider (this);
  183. break;
  184. }
  185. case ComTypes::UIA_GridItemPatternId:
  186. {
  187. if (accessibilityHandler.getCellInterface() != nullptr)
  188. return new UIAGridItemProvider (this);
  189. break;
  190. }
  191. case ComTypes::UIA_InvokePatternId:
  192. {
  193. if (accessibilityHandler.getActions().contains (AccessibilityActionType::press))
  194. return new UIAInvokeProvider (this);
  195. break;
  196. }
  197. case ComTypes::UIA_ExpandCollapsePatternId:
  198. {
  199. if (accessibilityHandler.getActions().contains (AccessibilityActionType::showMenu)
  200. && accessibilityHandler.getCurrentState().isExpandable())
  201. return new UIAExpandCollapseProvider (this);
  202. break;
  203. }
  204. }
  205. return nullptr;
  206. }();
  207. return S_OK;
  208. });
  209. }
  210. JUCE_COMRESULT AccessibilityNativeHandle::GetPropertyValue (PROPERTYID propertyId, VARIANT* pRetVal)
  211. {
  212. return withCheckedComArgs (pRetVal, *this, [&]
  213. {
  214. VariantHelpers::clear (pRetVal);
  215. const auto role = accessibilityHandler.getRole();
  216. const auto state = accessibilityHandler.getCurrentState();
  217. const auto ignored = accessibilityHandler.isIgnored();
  218. switch (propertyId)
  219. {
  220. case UIA_AutomationIdPropertyId:
  221. VariantHelpers::setString (getAutomationId (accessibilityHandler), pRetVal);
  222. break;
  223. case UIA_ControlTypePropertyId:
  224. VariantHelpers::setInt (roleToControlTypeId (role), pRetVal);
  225. break;
  226. case UIA_FrameworkIdPropertyId:
  227. VariantHelpers::setString ("JUCE", pRetVal);
  228. break;
  229. case UIA_FullDescriptionPropertyId:
  230. VariantHelpers::setString (accessibilityHandler.getDescription(), pRetVal);
  231. break;
  232. case UIA_HelpTextPropertyId:
  233. VariantHelpers::setString (accessibilityHandler.getHelp(), pRetVal);
  234. break;
  235. case UIA_IsContentElementPropertyId:
  236. VariantHelpers::setBool (! ignored && accessibilityHandler.isVisibleWithinParent(),
  237. pRetVal);
  238. break;
  239. case UIA_IsControlElementPropertyId:
  240. VariantHelpers::setBool (true, pRetVal);
  241. break;
  242. case UIA_IsDialogPropertyId:
  243. VariantHelpers::setBool (role == AccessibilityRole::dialogWindow, pRetVal);
  244. break;
  245. case UIA_IsEnabledPropertyId:
  246. VariantHelpers::setBool (accessibilityHandler.getComponent().isEnabled(), pRetVal);
  247. break;
  248. case UIA_IsKeyboardFocusablePropertyId:
  249. VariantHelpers::setBool (state.isFocusable(), pRetVal);
  250. break;
  251. case UIA_HasKeyboardFocusPropertyId:
  252. VariantHelpers::setBool (accessibilityHandler.hasFocus (true), pRetVal);
  253. break;
  254. case UIA_IsOffscreenPropertyId:
  255. VariantHelpers::setBool (! accessibilityHandler.isVisibleWithinParent(), pRetVal);
  256. break;
  257. case UIA_IsPasswordPropertyId:
  258. if (auto* textInterface = accessibilityHandler.getTextInterface())
  259. VariantHelpers::setBool (textInterface->isDisplayingProtectedText(), pRetVal);
  260. break;
  261. case ComTypes::UIA_IsPeripheralPropertyId:
  262. VariantHelpers::setBool (role == AccessibilityRole::tooltip
  263. || role == AccessibilityRole::popupMenu
  264. || role == AccessibilityRole::splashScreen,
  265. pRetVal);
  266. break;
  267. case UIA_NamePropertyId:
  268. if (! ignored)
  269. VariantHelpers::setString (getElementName(), pRetVal);
  270. break;
  271. case UIA_ProcessIdPropertyId:
  272. VariantHelpers::setInt ((int) GetCurrentProcessId(), pRetVal);
  273. break;
  274. case UIA_NativeWindowHandlePropertyId:
  275. if (isFragmentRoot())
  276. VariantHelpers::setInt ((int) (pointer_sized_int) accessibilityHandler.getComponent().getWindowHandle(), pRetVal);
  277. break;
  278. }
  279. return S_OK;
  280. });
  281. }
  282. //==============================================================================
  283. JUCE_COMRESULT AccessibilityNativeHandle::Navigate (ComTypes::NavigateDirection direction, ComTypes::IRawElementProviderFragment** pRetVal)
  284. {
  285. return withCheckedComArgs (pRetVal, *this, [&]
  286. {
  287. auto* handler = [&]() -> AccessibilityHandler*
  288. {
  289. if (direction == ComTypes::NavigateDirection_Parent)
  290. return accessibilityHandler.getParent();
  291. if (direction == ComTypes::NavigateDirection_FirstChild
  292. || direction == ComTypes::NavigateDirection_LastChild)
  293. {
  294. auto children = accessibilityHandler.getChildren();
  295. return children.empty() ? nullptr
  296. : (direction == ComTypes::NavigateDirection_FirstChild ? children.front()
  297. : children.back());
  298. }
  299. if (direction == ComTypes::NavigateDirection_NextSibling
  300. || direction == ComTypes::NavigateDirection_PreviousSibling)
  301. {
  302. if (auto* parent = accessibilityHandler.getParent())
  303. {
  304. const auto siblings = parent->getChildren();
  305. const auto iter = std::find (siblings.cbegin(), siblings.cend(), &accessibilityHandler);
  306. if (iter == siblings.end())
  307. return nullptr;
  308. if (direction == ComTypes::NavigateDirection_NextSibling && iter != std::prev (siblings.cend()))
  309. return *std::next (iter);
  310. if (direction == ComTypes::NavigateDirection_PreviousSibling && iter != siblings.cbegin())
  311. return *std::prev (iter);
  312. }
  313. }
  314. return nullptr;
  315. }();
  316. if (handler != nullptr)
  317. if (auto* provider = handler->getNativeImplementation())
  318. if (provider->isElementValid())
  319. provider->QueryInterface (IID_PPV_ARGS (pRetVal));
  320. return S_OK;
  321. });
  322. }
  323. JUCE_COMRESULT AccessibilityNativeHandle::GetRuntimeId (SAFEARRAY** pRetVal)
  324. {
  325. return withCheckedComArgs (pRetVal, *this, [&]
  326. {
  327. if (! isFragmentRoot())
  328. {
  329. *pRetVal = SafeArrayCreateVector (VT_I4, 0, 2);
  330. if (*pRetVal == nullptr)
  331. return E_OUTOFMEMORY;
  332. for (LONG i = 0; i < 2; ++i)
  333. {
  334. auto hr = SafeArrayPutElement (*pRetVal, &i, &rtid[(size_t) i]);
  335. if (FAILED (hr))
  336. return E_FAIL;
  337. }
  338. }
  339. return S_OK;
  340. });
  341. }
  342. JUCE_COMRESULT AccessibilityNativeHandle::get_BoundingRectangle (ComTypes::UiaRect* pRetVal)
  343. {
  344. return withCheckedComArgs (pRetVal, *this, [&]
  345. {
  346. auto bounds = Desktop::getInstance().getDisplays()
  347. .logicalToPhysical (accessibilityHandler.getComponent().getScreenBounds());
  348. pRetVal->left = bounds.getX();
  349. pRetVal->top = bounds.getY();
  350. pRetVal->width = bounds.getWidth();
  351. pRetVal->height = bounds.getHeight();
  352. return S_OK;
  353. });
  354. }
  355. JUCE_COMRESULT AccessibilityNativeHandle::GetEmbeddedFragmentRoots (SAFEARRAY** pRetVal)
  356. {
  357. return withCheckedComArgs (pRetVal, *this, []
  358. {
  359. return S_OK;
  360. });
  361. }
  362. JUCE_COMRESULT AccessibilityNativeHandle::SetFocus()
  363. {
  364. if (! isElementValid())
  365. return (HRESULT) UIA_E_ELEMENTNOTAVAILABLE;
  366. const WeakReference<Component> safeComponent (&accessibilityHandler.getComponent());
  367. accessibilityHandler.getActions().invoke (AccessibilityActionType::focus);
  368. if (safeComponent != nullptr)
  369. accessibilityHandler.grabFocus();
  370. return S_OK;
  371. }
  372. JUCE_COMRESULT AccessibilityNativeHandle::get_FragmentRoot (ComTypes::IRawElementProviderFragmentRoot** pRetVal)
  373. {
  374. return withCheckedComArgs (pRetVal, *this, [&]() -> HRESULT
  375. {
  376. auto* handler = [&]() -> AccessibilityHandler*
  377. {
  378. if (isFragmentRoot())
  379. return &accessibilityHandler;
  380. if (auto* peer = accessibilityHandler.getComponent().getPeer())
  381. return peer->getComponent().getAccessibilityHandler();
  382. return nullptr;
  383. }();
  384. if (handler != nullptr)
  385. {
  386. handler->getNativeImplementation()->QueryInterface (IID_PPV_ARGS (pRetVal));
  387. return S_OK;
  388. }
  389. return (HRESULT) UIA_E_ELEMENTNOTAVAILABLE;
  390. });
  391. }
  392. //==============================================================================
  393. JUCE_COMRESULT AccessibilityNativeHandle::ElementProviderFromPoint (double x, double y, ComTypes::IRawElementProviderFragment** pRetVal)
  394. {
  395. return withCheckedComArgs (pRetVal, *this, [&]
  396. {
  397. auto* handler = [&]
  398. {
  399. auto logicalScreenPoint = Desktop::getInstance().getDisplays()
  400. .physicalToLogical (Point<int> (roundToInt (x),
  401. roundToInt (y)));
  402. if (auto* child = accessibilityHandler.getChildAt (logicalScreenPoint))
  403. return child;
  404. return &accessibilityHandler;
  405. }();
  406. handler->getNativeImplementation()->QueryInterface (IID_PPV_ARGS (pRetVal));
  407. return S_OK;
  408. });
  409. }
  410. JUCE_COMRESULT AccessibilityNativeHandle::GetFocus (ComTypes::IRawElementProviderFragment** pRetVal)
  411. {
  412. return withCheckedComArgs (pRetVal, *this, [&]
  413. {
  414. const auto getFocusHandler = [this]() -> AccessibilityHandler*
  415. {
  416. if (auto* modal = Component::getCurrentlyModalComponent())
  417. {
  418. const auto& component = accessibilityHandler.getComponent();
  419. if (! component.isParentOf (modal)
  420. && component.isCurrentlyBlockedByAnotherModalComponent())
  421. {
  422. if (auto* modalHandler = modal->getAccessibilityHandler())
  423. {
  424. if (auto* focusChild = modalHandler->getChildFocus())
  425. return focusChild;
  426. return modalHandler;
  427. }
  428. }
  429. }
  430. if (auto* focusChild = accessibilityHandler.getChildFocus())
  431. return focusChild;
  432. return nullptr;
  433. };
  434. if (auto* focusHandler = getFocusHandler())
  435. focusHandler->getNativeImplementation()->QueryInterface (IID_PPV_ARGS (pRetVal));
  436. return S_OK;
  437. });
  438. }
  439. //==============================================================================
  440. String AccessibilityNativeHandle::getElementName() const
  441. {
  442. if (accessibilityHandler.getRole() == AccessibilityRole::tooltip)
  443. return accessibilityHandler.getDescription();
  444. auto name = accessibilityHandler.getTitle();
  445. if (name.isEmpty() && isFragmentRoot())
  446. return getAccessibleApplicationOrPluginName();
  447. return name;
  448. }
  449. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  450. } // namespace juce