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.

556 lines
20KB

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