Audio plugin host https://kx.studio/carla
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.

359 lines
9.9KB

  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. MenuBarComponent::MenuBarComponent (MenuBarModel* m)
  21. : model (nullptr),
  22. itemUnderMouse (-1),
  23. currentPopupIndex (-1),
  24. topLevelIndexClicked (0)
  25. {
  26. setRepaintsOnMouseActivity (true);
  27. setWantsKeyboardFocus (false);
  28. setMouseClickGrabsKeyboardFocus (false);
  29. setModel (m);
  30. }
  31. MenuBarComponent::~MenuBarComponent()
  32. {
  33. setModel (nullptr);
  34. Desktop::getInstance().removeGlobalMouseListener (this);
  35. }
  36. MenuBarModel* MenuBarComponent::getModel() const noexcept
  37. {
  38. return model;
  39. }
  40. void MenuBarComponent::setModel (MenuBarModel* const newModel)
  41. {
  42. if (model != newModel)
  43. {
  44. if (model != nullptr)
  45. model->removeListener (this);
  46. model = newModel;
  47. if (model != nullptr)
  48. model->addListener (this);
  49. repaint();
  50. menuBarItemsChanged (nullptr);
  51. }
  52. }
  53. //==============================================================================
  54. void MenuBarComponent::paint (Graphics& g)
  55. {
  56. const bool isMouseOverBar = currentPopupIndex >= 0 || itemUnderMouse >= 0 || isMouseOver();
  57. getLookAndFeel().drawMenuBarBackground (g,
  58. getWidth(),
  59. getHeight(),
  60. isMouseOverBar,
  61. *this);
  62. if (model != nullptr)
  63. {
  64. for (int i = 0; i < menuNames.size(); ++i)
  65. {
  66. Graphics::ScopedSaveState ss (g);
  67. g.setOrigin (xPositions [i], 0);
  68. g.reduceClipRegion (0, 0, xPositions[i + 1] - xPositions[i], getHeight());
  69. getLookAndFeel().drawMenuBarItem (g,
  70. xPositions[i + 1] - xPositions[i],
  71. getHeight(),
  72. i,
  73. menuNames[i],
  74. i == itemUnderMouse,
  75. i == currentPopupIndex,
  76. isMouseOverBar,
  77. *this);
  78. }
  79. }
  80. }
  81. void MenuBarComponent::resized()
  82. {
  83. xPositions.clear();
  84. int x = 0;
  85. xPositions.add (x);
  86. for (int i = 0; i < menuNames.size(); ++i)
  87. {
  88. x += getLookAndFeel().getMenuBarItemWidth (*this, i, menuNames[i]);
  89. xPositions.add (x);
  90. }
  91. }
  92. int MenuBarComponent::getItemAt (Point<int> p)
  93. {
  94. for (int i = 0; i < xPositions.size(); ++i)
  95. if (p.x >= xPositions[i] && p.x < xPositions[i + 1])
  96. return reallyContains (p, true) ? i : -1;
  97. return -1;
  98. }
  99. void MenuBarComponent::repaintMenuItem (int index)
  100. {
  101. if (isPositiveAndBelow (index, xPositions.size()))
  102. {
  103. const int x1 = xPositions [index];
  104. const int x2 = xPositions [index + 1];
  105. repaint (x1 - 2, 0, x2 - x1 + 4, getHeight());
  106. }
  107. }
  108. void MenuBarComponent::setItemUnderMouse (const int index)
  109. {
  110. if (itemUnderMouse != index)
  111. {
  112. repaintMenuItem (itemUnderMouse);
  113. itemUnderMouse = index;
  114. repaintMenuItem (itemUnderMouse);
  115. }
  116. }
  117. void MenuBarComponent::setOpenItem (int index)
  118. {
  119. if (currentPopupIndex != index)
  120. {
  121. if (currentPopupIndex < 0 && index >= 0)
  122. model->handleMenuBarActivate (true);
  123. else if (currentPopupIndex >= 0 && index < 0)
  124. model->handleMenuBarActivate (false);
  125. repaintMenuItem (currentPopupIndex);
  126. currentPopupIndex = index;
  127. repaintMenuItem (currentPopupIndex);
  128. Desktop& desktop = Desktop::getInstance();
  129. if (index >= 0)
  130. desktop.addGlobalMouseListener (this);
  131. else
  132. desktop.removeGlobalMouseListener (this);
  133. }
  134. }
  135. void MenuBarComponent::updateItemUnderMouse (Point<int> p)
  136. {
  137. setItemUnderMouse (getItemAt (p));
  138. }
  139. void MenuBarComponent::showMenu (int index)
  140. {
  141. if (index != currentPopupIndex)
  142. {
  143. PopupMenu::dismissAllActiveMenus();
  144. menuBarItemsChanged (nullptr);
  145. setOpenItem (index);
  146. setItemUnderMouse (index);
  147. if (index >= 0)
  148. {
  149. PopupMenu m (model->getMenuForIndex (itemUnderMouse,
  150. menuNames [itemUnderMouse]));
  151. if (m.lookAndFeel == nullptr)
  152. m.setLookAndFeel (&getLookAndFeel());
  153. const Rectangle<int> itemPos (xPositions [index], 0, xPositions [index + 1] - xPositions [index], getHeight());
  154. m.showMenuAsync (PopupMenu::Options().withTargetComponent (this)
  155. .withTargetScreenArea (localAreaToGlobal (itemPos))
  156. .withMinimumWidth (itemPos.getWidth()),
  157. ModalCallbackFunction::forComponent (menuBarMenuDismissedCallback, this, index));
  158. }
  159. }
  160. }
  161. void MenuBarComponent::menuBarMenuDismissedCallback (int result, MenuBarComponent* bar, int topLevelIndex)
  162. {
  163. if (bar != nullptr)
  164. bar->menuDismissed (topLevelIndex, result);
  165. }
  166. void MenuBarComponent::menuDismissed (int topLevelIndex, int itemId)
  167. {
  168. topLevelIndexClicked = topLevelIndex;
  169. postCommandMessage (itemId);
  170. }
  171. void MenuBarComponent::handleCommandMessage (int commandId)
  172. {
  173. const Point<int> mousePos (getMouseXYRelative());
  174. updateItemUnderMouse (mousePos);
  175. if (currentPopupIndex == topLevelIndexClicked)
  176. setOpenItem (-1);
  177. if (commandId != 0 && model != nullptr)
  178. model->menuItemSelected (commandId, topLevelIndexClicked);
  179. }
  180. //==============================================================================
  181. void MenuBarComponent::mouseEnter (const MouseEvent& e)
  182. {
  183. if (e.eventComponent == this)
  184. updateItemUnderMouse (e.getPosition());
  185. }
  186. void MenuBarComponent::mouseExit (const MouseEvent& e)
  187. {
  188. if (e.eventComponent == this)
  189. updateItemUnderMouse (e.getPosition());
  190. }
  191. void MenuBarComponent::mouseDown (const MouseEvent& e)
  192. {
  193. if (currentPopupIndex < 0)
  194. {
  195. const MouseEvent e2 (e.getEventRelativeTo (this));
  196. updateItemUnderMouse (e2.getPosition());
  197. currentPopupIndex = -2;
  198. showMenu (itemUnderMouse);
  199. }
  200. }
  201. void MenuBarComponent::mouseDrag (const MouseEvent& e)
  202. {
  203. const MouseEvent e2 (e.getEventRelativeTo (this));
  204. const int item = getItemAt (e2.getPosition());
  205. if (item >= 0)
  206. showMenu (item);
  207. }
  208. void MenuBarComponent::mouseUp (const MouseEvent& e)
  209. {
  210. const MouseEvent e2 (e.getEventRelativeTo (this));
  211. updateItemUnderMouse (e2.getPosition());
  212. if (itemUnderMouse < 0 && getLocalBounds().contains (e2.x, e2.y))
  213. {
  214. setOpenItem (-1);
  215. PopupMenu::dismissAllActiveMenus();
  216. }
  217. }
  218. void MenuBarComponent::mouseMove (const MouseEvent& e)
  219. {
  220. const MouseEvent e2 (e.getEventRelativeTo (this));
  221. if (lastMousePos != e2.getPosition())
  222. {
  223. if (currentPopupIndex >= 0)
  224. {
  225. const int item = getItemAt (e2.getPosition());
  226. if (item >= 0)
  227. showMenu (item);
  228. }
  229. else
  230. {
  231. updateItemUnderMouse (e2.getPosition());
  232. }
  233. lastMousePos = e2.getPosition();
  234. }
  235. }
  236. bool MenuBarComponent::keyPressed (const KeyPress& key)
  237. {
  238. const int numMenus = menuNames.size();
  239. if (numMenus > 0)
  240. {
  241. const int currentIndex = jlimit (0, numMenus - 1, currentPopupIndex);
  242. if (key.isKeyCode (KeyPress::leftKey))
  243. {
  244. showMenu ((currentIndex + numMenus - 1) % numMenus);
  245. return true;
  246. }
  247. if (key.isKeyCode (KeyPress::rightKey))
  248. {
  249. showMenu ((currentIndex + 1) % numMenus);
  250. return true;
  251. }
  252. }
  253. return false;
  254. }
  255. void MenuBarComponent::menuBarItemsChanged (MenuBarModel* /*menuBarModel*/)
  256. {
  257. StringArray newNames;
  258. if (model != nullptr)
  259. newNames = model->getMenuBarNames();
  260. if (newNames != menuNames)
  261. {
  262. menuNames = newNames;
  263. repaint();
  264. resized();
  265. }
  266. }
  267. void MenuBarComponent::menuCommandInvoked (MenuBarModel* /*menuBarModel*/,
  268. const ApplicationCommandTarget::InvocationInfo& info)
  269. {
  270. if (model == nullptr || (info.commandFlags & ApplicationCommandInfo::dontTriggerVisualFeedback) != 0)
  271. return;
  272. for (int i = 0; i < menuNames.size(); ++i)
  273. {
  274. const PopupMenu menu (model->getMenuForIndex (i, menuNames [i]));
  275. if (menu.containsCommandItem (info.commandID))
  276. {
  277. setItemUnderMouse (i);
  278. startTimer (200);
  279. break;
  280. }
  281. }
  282. }
  283. void MenuBarComponent::timerCallback()
  284. {
  285. stopTimer();
  286. updateItemUnderMouse (getMouseXYRelative());
  287. }
  288. } // namespace juce