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.

353 lines
9.8KB

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