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.

459 lines
13KB

  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. class MenuBarComponent::AccessibleItemComponent : public Component
  21. {
  22. public:
  23. AccessibleItemComponent (MenuBarComponent& comp, const String& menuItemName)
  24. : owner (comp),
  25. name (menuItemName)
  26. {
  27. setInterceptsMouseClicks (false, false);
  28. }
  29. const String& getName() const noexcept { return name; }
  30. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
  31. {
  32. class ComponentHandler : public AccessibilityHandler
  33. {
  34. public:
  35. explicit ComponentHandler (AccessibleItemComponent& item)
  36. : AccessibilityHandler (item,
  37. AccessibilityRole::menuItem,
  38. getAccessibilityActions (item)),
  39. itemComponent (item)
  40. {
  41. }
  42. AccessibleState getCurrentState() const override
  43. {
  44. auto state = AccessibilityHandler::getCurrentState().withSelectable();
  45. return state.isFocused() ? state.withSelected() : state;
  46. }
  47. String getTitle() const override { return itemComponent.name; }
  48. private:
  49. static AccessibilityActions getAccessibilityActions (AccessibleItemComponent& item)
  50. {
  51. auto showMenu = [&item] { item.owner.showMenu (item.owner.indexOfItemComponent (&item)); };
  52. return AccessibilityActions().addAction (AccessibilityActionType::focus,
  53. [&item] { item.owner.setItemUnderMouse (item.owner.indexOfItemComponent (&item)); })
  54. .addAction (AccessibilityActionType::press, showMenu)
  55. .addAction (AccessibilityActionType::showMenu, showMenu);
  56. }
  57. AccessibleItemComponent& itemComponent;
  58. };
  59. return std::make_unique<ComponentHandler> (*this);
  60. }
  61. private:
  62. MenuBarComponent& owner;
  63. const String name;
  64. };
  65. MenuBarComponent::MenuBarComponent (MenuBarModel* m)
  66. {
  67. setRepaintsOnMouseActivity (true);
  68. setWantsKeyboardFocus (false);
  69. setMouseClickGrabsKeyboardFocus (false);
  70. setModel (m);
  71. }
  72. MenuBarComponent::~MenuBarComponent()
  73. {
  74. setModel (nullptr);
  75. Desktop::getInstance().removeGlobalMouseListener (this);
  76. }
  77. MenuBarModel* MenuBarComponent::getModel() const noexcept
  78. {
  79. return model;
  80. }
  81. void MenuBarComponent::setModel (MenuBarModel* const newModel)
  82. {
  83. if (model != newModel)
  84. {
  85. if (model != nullptr)
  86. model->removeListener (this);
  87. model = newModel;
  88. if (model != nullptr)
  89. model->addListener (this);
  90. repaint();
  91. menuBarItemsChanged (nullptr);
  92. }
  93. }
  94. //==============================================================================
  95. void MenuBarComponent::paint (Graphics& g)
  96. {
  97. const auto isMouseOverBar = (currentPopupIndex >= 0 || itemUnderMouse >= 0 || isMouseOver());
  98. getLookAndFeel().drawMenuBarBackground (g, getWidth(), getHeight(), isMouseOverBar, *this);
  99. if (model == nullptr)
  100. return;
  101. for (size_t i = 0; i < itemComponents.size(); ++i)
  102. {
  103. const auto& itemComponent = itemComponents[i];
  104. const auto itemBounds = itemComponent->getBounds();
  105. Graphics::ScopedSaveState ss (g);
  106. g.setOrigin (itemBounds.getX(), 0);
  107. g.reduceClipRegion (0, 0, itemBounds.getWidth(), itemBounds.getHeight());
  108. getLookAndFeel().drawMenuBarItem (g,
  109. itemBounds.getWidth(),
  110. itemBounds.getHeight(),
  111. (int) i,
  112. itemComponent->getName(),
  113. (int) i == itemUnderMouse,
  114. (int) i == currentPopupIndex,
  115. isMouseOverBar,
  116. *this);
  117. }
  118. }
  119. void MenuBarComponent::resized()
  120. {
  121. int x = 0;
  122. for (size_t i = 0; i < itemComponents.size(); ++i)
  123. {
  124. auto& itemComponent = itemComponents[i];
  125. auto w = getLookAndFeel().getMenuBarItemWidth (*this, (int) i, itemComponent->getName());
  126. itemComponent->setBounds (x, 0, w, getHeight());
  127. x += w;
  128. }
  129. }
  130. int MenuBarComponent::getItemAt (Point<int> p)
  131. {
  132. for (size_t i = 0; i < itemComponents.size(); ++i)
  133. if (itemComponents[i]->getBounds().contains (p) && reallyContains (p, true))
  134. return (int) i;
  135. return -1;
  136. }
  137. void MenuBarComponent::repaintMenuItem (int index)
  138. {
  139. if (isPositiveAndBelow (index, (int) itemComponents.size()))
  140. {
  141. auto itemBounds = itemComponents[(size_t) index]->getBounds();
  142. repaint (itemBounds.getX() - 2,
  143. 0,
  144. itemBounds.getWidth() + 4,
  145. itemBounds.getHeight());
  146. }
  147. }
  148. void MenuBarComponent::setItemUnderMouse (int index)
  149. {
  150. if (itemUnderMouse == index)
  151. return;
  152. repaintMenuItem (itemUnderMouse);
  153. itemUnderMouse = index;
  154. repaintMenuItem (itemUnderMouse);
  155. if (isPositiveAndBelow (itemUnderMouse, (int) itemComponents.size()))
  156. if (auto* handler = itemComponents[(size_t) itemUnderMouse]->getAccessibilityHandler())
  157. handler->grabFocus();
  158. }
  159. void MenuBarComponent::setOpenItem (int index)
  160. {
  161. if (currentPopupIndex != index)
  162. {
  163. if (currentPopupIndex < 0 && index >= 0)
  164. model->handleMenuBarActivate (true);
  165. else if (currentPopupIndex >= 0 && index < 0)
  166. model->handleMenuBarActivate (false);
  167. repaintMenuItem (currentPopupIndex);
  168. currentPopupIndex = index;
  169. repaintMenuItem (currentPopupIndex);
  170. auto& desktop = Desktop::getInstance();
  171. if (index >= 0)
  172. desktop.addGlobalMouseListener (this);
  173. else
  174. desktop.removeGlobalMouseListener (this);
  175. }
  176. }
  177. void MenuBarComponent::updateItemUnderMouse (Point<int> p)
  178. {
  179. setItemUnderMouse (getItemAt (p));
  180. }
  181. void MenuBarComponent::showMenu (int index)
  182. {
  183. if (index != currentPopupIndex)
  184. {
  185. PopupMenu::dismissAllActiveMenus();
  186. menuBarItemsChanged (nullptr);
  187. setOpenItem (index);
  188. setItemUnderMouse (index);
  189. if (isPositiveAndBelow (index, (int) itemComponents.size()))
  190. {
  191. const auto& itemComponent = itemComponents[(size_t) index];
  192. auto m = model->getMenuForIndex (itemUnderMouse, itemComponent->getName());
  193. if (m.lookAndFeel == nullptr)
  194. m.setLookAndFeel (&getLookAndFeel());
  195. auto itemBounds = itemComponent->getBounds();
  196. m.showMenuAsync (PopupMenu::Options().withTargetComponent (this)
  197. .withTargetScreenArea (localAreaToGlobal (itemBounds))
  198. .withMinimumWidth (itemBounds.getWidth()),
  199. [this, index] (int result) { menuDismissed (index, result); });
  200. }
  201. }
  202. }
  203. void MenuBarComponent::menuDismissed (int topLevelIndex, int itemId)
  204. {
  205. topLevelIndexClicked = topLevelIndex;
  206. postCommandMessage (itemId);
  207. }
  208. void MenuBarComponent::handleCommandMessage (int commandId)
  209. {
  210. updateItemUnderMouse (getMouseXYRelative());
  211. if (currentPopupIndex == topLevelIndexClicked)
  212. setOpenItem (-1);
  213. if (commandId != 0 && model != nullptr)
  214. model->menuItemSelected (commandId, topLevelIndexClicked);
  215. }
  216. //==============================================================================
  217. void MenuBarComponent::mouseEnter (const MouseEvent& e)
  218. {
  219. if (e.eventComponent == this)
  220. updateItemUnderMouse (e.getPosition());
  221. }
  222. void MenuBarComponent::mouseExit (const MouseEvent& e)
  223. {
  224. if (e.eventComponent == this)
  225. updateItemUnderMouse (e.getPosition());
  226. }
  227. void MenuBarComponent::mouseDown (const MouseEvent& e)
  228. {
  229. if (currentPopupIndex < 0)
  230. {
  231. updateItemUnderMouse (e.getEventRelativeTo (this).getPosition());
  232. currentPopupIndex = -2;
  233. showMenu (itemUnderMouse);
  234. }
  235. }
  236. void MenuBarComponent::mouseDrag (const MouseEvent& e)
  237. {
  238. const auto item = getItemAt (e.getEventRelativeTo (this).getPosition());
  239. if (item >= 0)
  240. showMenu (item);
  241. }
  242. void MenuBarComponent::mouseUp (const MouseEvent& e)
  243. {
  244. const auto e2 = e.getEventRelativeTo (this);
  245. updateItemUnderMouse (e2.getPosition());
  246. if (itemUnderMouse < 0 && getLocalBounds().contains (e2.x, e2.y))
  247. {
  248. setOpenItem (-1);
  249. PopupMenu::dismissAllActiveMenus();
  250. }
  251. }
  252. void MenuBarComponent::mouseMove (const MouseEvent& e)
  253. {
  254. const auto e2 = e.getEventRelativeTo (this);
  255. if (lastMousePos != e2.getPosition())
  256. {
  257. if (currentPopupIndex >= 0)
  258. {
  259. const auto item = getItemAt (e2.getPosition());
  260. if (item >= 0)
  261. showMenu (item);
  262. }
  263. else
  264. {
  265. updateItemUnderMouse (e2.getPosition());
  266. }
  267. lastMousePos = e2.getPosition();
  268. }
  269. }
  270. bool MenuBarComponent::keyPressed (const KeyPress& key)
  271. {
  272. const auto numMenus = (int) itemComponents.size();
  273. if (numMenus > 0)
  274. {
  275. const auto currentIndex = jlimit (0, numMenus - 1, currentPopupIndex);
  276. if (key.isKeyCode (KeyPress::leftKey))
  277. {
  278. showMenu ((currentIndex + numMenus - 1) % numMenus);
  279. return true;
  280. }
  281. if (key.isKeyCode (KeyPress::rightKey))
  282. {
  283. showMenu ((currentIndex + 1) % numMenus);
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. void MenuBarComponent::menuBarItemsChanged (MenuBarModel*)
  290. {
  291. StringArray newNames;
  292. if (model != nullptr)
  293. newNames = model->getMenuBarNames();
  294. auto itemsHaveChanged = [this, &newNames]
  295. {
  296. if ((int) itemComponents.size() != newNames.size())
  297. return true;
  298. for (size_t i = 0; i < itemComponents.size(); ++i)
  299. if (itemComponents[i]->getName() != newNames[(int) i])
  300. return true;
  301. return false;
  302. }();
  303. if (itemsHaveChanged)
  304. {
  305. updateItemComponents (newNames);
  306. repaint();
  307. resized();
  308. }
  309. }
  310. void MenuBarComponent::updateItemComponents (const StringArray& menuNames)
  311. {
  312. itemComponents.clear();
  313. for (const auto& name : menuNames)
  314. {
  315. itemComponents.push_back (std::make_unique<AccessibleItemComponent> (*this, name));
  316. addAndMakeVisible (*itemComponents.back());
  317. }
  318. }
  319. int MenuBarComponent::indexOfItemComponent (AccessibleItemComponent* itemComponent) const
  320. {
  321. const auto iter = std::find_if (itemComponents.cbegin(), itemComponents.cend(),
  322. [itemComponent] (const std::unique_ptr<AccessibleItemComponent>& c) { return c.get() == itemComponent; });
  323. if (iter != itemComponents.cend())
  324. return (int) std::distance (itemComponents.cbegin(), iter);
  325. jassertfalse;
  326. return -1;
  327. }
  328. void MenuBarComponent::menuCommandInvoked (MenuBarModel*, const ApplicationCommandTarget::InvocationInfo& info)
  329. {
  330. if (model == nullptr || (info.commandFlags & ApplicationCommandInfo::dontTriggerVisualFeedback) != 0)
  331. return;
  332. for (size_t i = 0; i < itemComponents.size(); ++i)
  333. {
  334. const auto menu = model->getMenuForIndex ((int) i, itemComponents[i]->getName());
  335. if (menu.containsCommandItem (info.commandID))
  336. {
  337. setItemUnderMouse ((int) i);
  338. startTimer (200);
  339. break;
  340. }
  341. }
  342. }
  343. void MenuBarComponent::timerCallback()
  344. {
  345. stopTimer();
  346. updateItemUnderMouse (getMouseXYRelative());
  347. }
  348. //==============================================================================
  349. std::unique_ptr<AccessibilityHandler> MenuBarComponent::createAccessibilityHandler()
  350. {
  351. struct MenuBarComponentAccessibilityHandler : public AccessibilityHandler
  352. {
  353. explicit MenuBarComponentAccessibilityHandler (MenuBarComponent& menuBarComponent)
  354. : AccessibilityHandler (menuBarComponent, AccessibilityRole::menuBar)
  355. {
  356. }
  357. AccessibleState getCurrentState() const override { return AccessibleState().withIgnored(); }
  358. };
  359. return std::make_unique<MenuBarComponentAccessibilityHandler> (*this);
  360. }
  361. } // namespace juce