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.

253 lines
8.2KB

  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_COMCLASS (ISelectionProvider2, "14f68475-ee1c-44f6-a869-d239381f0fe7") : public ISelectionProvider
  21. {
  22. JUCE_COMCALL get_FirstSelectedItem (IRawElementProviderSimple** retVal) = 0;
  23. JUCE_COMCALL get_LastSelectedItem (IRawElementProviderSimple** retVal) = 0;
  24. JUCE_COMCALL get_CurrentSelectedItem (IRawElementProviderSimple** retVal) = 0;
  25. JUCE_COMCALL get_ItemCount (int* retVal) = 0;
  26. };
  27. //==============================================================================
  28. class UIASelectionItemProvider : public UIAProviderBase,
  29. public ComBaseClassHelper<ISelectionItemProvider>
  30. {
  31. public:
  32. explicit UIASelectionItemProvider (AccessibilityNativeHandle* nativeHandle)
  33. : UIAProviderBase (nativeHandle),
  34. isRadioButton (getHandler().getRole() == AccessibilityRole::radioButton)
  35. {
  36. }
  37. //==============================================================================
  38. JUCE_COMRESULT AddToSelection() override
  39. {
  40. if (! isElementValid())
  41. return UIA_E_ELEMENTNOTAVAILABLE;
  42. const auto& handler = getHandler();
  43. if (isRadioButton)
  44. {
  45. handler.getActions().invoke (AccessibilityActionType::press);
  46. sendAccessibilityAutomationEvent (handler, UIA_SelectionItem_ElementSelectedEventId);
  47. return S_OK;
  48. }
  49. handler.getActions().invoke (AccessibilityActionType::toggle);
  50. handler.getActions().invoke (AccessibilityActionType::press);
  51. return S_OK;
  52. }
  53. JUCE_COMRESULT get_IsSelected (BOOL* pRetVal) override
  54. {
  55. return withCheckedComArgs (pRetVal, *this, [&]
  56. {
  57. const auto state = getHandler().getCurrentState();
  58. *pRetVal = isRadioButton ? state.isChecked() : state.isSelected();
  59. return S_OK;
  60. });
  61. }
  62. JUCE_COMRESULT get_SelectionContainer (IRawElementProviderSimple** pRetVal) override
  63. {
  64. return withCheckedComArgs (pRetVal, *this, [&]
  65. {
  66. if (! isRadioButton)
  67. if (auto* parent = getHandler().getParent())
  68. parent->getNativeImplementation()->QueryInterface (IID_PPV_ARGS (pRetVal));
  69. return S_OK;
  70. });
  71. }
  72. JUCE_COMRESULT RemoveFromSelection() override
  73. {
  74. if (! isElementValid())
  75. return UIA_E_ELEMENTNOTAVAILABLE;
  76. if (! isRadioButton)
  77. {
  78. const auto& handler = getHandler();
  79. if (handler.getCurrentState().isSelected())
  80. getHandler().getActions().invoke (AccessibilityActionType::toggle);
  81. }
  82. return S_OK;
  83. }
  84. JUCE_COMRESULT Select() override
  85. {
  86. if (! isElementValid())
  87. return UIA_E_ELEMENTNOTAVAILABLE;
  88. AddToSelection();
  89. if (! isRadioButton)
  90. {
  91. const auto& handler = getHandler();
  92. if (auto* parent = handler.getParent())
  93. for (auto* child : parent->getChildren())
  94. if (child != &handler && child->getCurrentState().isSelected())
  95. child->getActions().invoke (AccessibilityActionType::toggle);
  96. }
  97. return S_OK;
  98. }
  99. private:
  100. const bool isRadioButton;
  101. //==============================================================================
  102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UIASelectionItemProvider)
  103. };
  104. //==============================================================================
  105. class UIASelectionProvider : public UIAProviderBase,
  106. public ComBaseClassHelper<ISelectionProvider2>
  107. {
  108. public:
  109. explicit UIASelectionProvider (AccessibilityNativeHandle* nativeHandle)
  110. : UIAProviderBase (nativeHandle)
  111. {
  112. }
  113. //==============================================================================
  114. JUCE_COMRESULT QueryInterface (REFIID iid, void** result) override
  115. {
  116. if (iid == _uuidof (IUnknown) || iid == _uuidof (ISelectionProvider))
  117. return castToType<ISelectionProvider> (result);
  118. if (iid == _uuidof (ISelectionProvider2))
  119. return castToType<ISelectionProvider2> (result);
  120. *result = nullptr;
  121. return E_NOINTERFACE;
  122. }
  123. //==============================================================================
  124. JUCE_COMRESULT get_CanSelectMultiple (BOOL* pRetVal) override
  125. {
  126. return withCheckedComArgs (pRetVal, *this, [&]
  127. {
  128. *pRetVal = isMultiSelectable();
  129. return S_OK;
  130. });
  131. }
  132. JUCE_COMRESULT get_IsSelectionRequired (BOOL* pRetVal) override
  133. {
  134. return withCheckedComArgs (pRetVal, *this, [&]
  135. {
  136. *pRetVal = getSelectedChildren().size() > 0 && ! isMultiSelectable();
  137. return S_OK;
  138. });
  139. }
  140. JUCE_COMRESULT GetSelection (SAFEARRAY** pRetVal) override
  141. {
  142. return withCheckedComArgs (pRetVal, *this, [&]
  143. {
  144. return addHandlersToArray (getSelectedChildren(), pRetVal);
  145. });
  146. }
  147. //==============================================================================
  148. JUCE_COMRESULT get_FirstSelectedItem (IRawElementProviderSimple** pRetVal) override
  149. {
  150. return withCheckedComArgs (pRetVal, *this, [&]
  151. {
  152. auto selectedChildren = getSelectedChildren();
  153. if (! selectedChildren.empty())
  154. selectedChildren.front()->getNativeImplementation()->QueryInterface (IID_PPV_ARGS (pRetVal));
  155. return S_OK;
  156. });
  157. }
  158. JUCE_COMRESULT get_LastSelectedItem (IRawElementProviderSimple** pRetVal) override
  159. {
  160. return withCheckedComArgs (pRetVal, *this, [&]
  161. {
  162. auto selectedChildren = getSelectedChildren();
  163. if (! selectedChildren.empty())
  164. selectedChildren.back()->getNativeImplementation()->QueryInterface (IID_PPV_ARGS (pRetVal));
  165. return S_OK;
  166. });
  167. }
  168. JUCE_COMRESULT get_CurrentSelectedItem (IRawElementProviderSimple** pRetVal) override
  169. {
  170. return withCheckedComArgs (pRetVal, *this, [&]
  171. {
  172. get_FirstSelectedItem (pRetVal);
  173. return S_OK;
  174. });
  175. }
  176. JUCE_COMRESULT get_ItemCount (int* pRetVal) override
  177. {
  178. return withCheckedComArgs (pRetVal, *this, [&]
  179. {
  180. *pRetVal = (int) getSelectedChildren().size();
  181. return S_OK;
  182. });
  183. }
  184. private:
  185. bool isMultiSelectable() const noexcept
  186. {
  187. return getHandler().getCurrentState().isMultiSelectable();
  188. }
  189. std::vector<const AccessibilityHandler*> getSelectedChildren() const
  190. {
  191. std::vector<const AccessibilityHandler*> selectedHandlers;
  192. for (auto* child : getHandler().getComponent().getChildren())
  193. if (auto* handler = child->getAccessibilityHandler())
  194. if (handler->getCurrentState().isSelected())
  195. selectedHandlers.push_back (handler);
  196. return selectedHandlers;
  197. }
  198. //==============================================================================
  199. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UIASelectionProvider)
  200. };
  201. } // namespace juce