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.

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