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.

juce_MenuBarComponent.cpp 9.6KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. repaintMenuItem (currentPopupIndex);
  119. currentPopupIndex = index;
  120. repaintMenuItem (currentPopupIndex);
  121. Desktop& desktop = Desktop::getInstance();
  122. if (index >= 0)
  123. desktop.addGlobalMouseListener (this);
  124. else
  125. desktop.removeGlobalMouseListener (this);
  126. }
  127. }
  128. void MenuBarComponent::updateItemUnderMouse (Point<int> p)
  129. {
  130. setItemUnderMouse (getItemAt (p));
  131. }
  132. void MenuBarComponent::showMenu (int index)
  133. {
  134. if (index != currentPopupIndex)
  135. {
  136. PopupMenu::dismissAllActiveMenus();
  137. menuBarItemsChanged (nullptr);
  138. setOpenItem (index);
  139. setItemUnderMouse (index);
  140. if (index >= 0)
  141. {
  142. PopupMenu m (model->getMenuForIndex (itemUnderMouse,
  143. menuNames [itemUnderMouse]));
  144. if (m.lookAndFeel == nullptr)
  145. m.setLookAndFeel (&getLookAndFeel());
  146. const Rectangle<int> itemPos (xPositions [index], 0, xPositions [index + 1] - xPositions [index], getHeight());
  147. m.showMenuAsync (PopupMenu::Options().withTargetComponent (this)
  148. .withTargetScreenArea (localAreaToGlobal (itemPos))
  149. .withMinimumWidth (itemPos.getWidth()),
  150. ModalCallbackFunction::forComponent (menuBarMenuDismissedCallback, this, index));
  151. }
  152. }
  153. }
  154. void MenuBarComponent::menuBarMenuDismissedCallback (int result, MenuBarComponent* bar, int topLevelIndex)
  155. {
  156. if (bar != nullptr)
  157. bar->menuDismissed (topLevelIndex, result);
  158. }
  159. void MenuBarComponent::menuDismissed (int topLevelIndex, int itemId)
  160. {
  161. topLevelIndexClicked = topLevelIndex;
  162. postCommandMessage (itemId);
  163. }
  164. void MenuBarComponent::handleCommandMessage (int commandId)
  165. {
  166. const Point<int> mousePos (getMouseXYRelative());
  167. updateItemUnderMouse (mousePos);
  168. if (currentPopupIndex == topLevelIndexClicked)
  169. setOpenItem (-1);
  170. if (commandId != 0 && model != nullptr)
  171. model->menuItemSelected (commandId, topLevelIndexClicked);
  172. }
  173. //==============================================================================
  174. void MenuBarComponent::mouseEnter (const MouseEvent& e)
  175. {
  176. if (e.eventComponent == this)
  177. updateItemUnderMouse (e.getPosition());
  178. }
  179. void MenuBarComponent::mouseExit (const MouseEvent& e)
  180. {
  181. if (e.eventComponent == this)
  182. updateItemUnderMouse (e.getPosition());
  183. }
  184. void MenuBarComponent::mouseDown (const MouseEvent& e)
  185. {
  186. if (currentPopupIndex < 0)
  187. {
  188. const MouseEvent e2 (e.getEventRelativeTo (this));
  189. updateItemUnderMouse (e2.getPosition());
  190. currentPopupIndex = -2;
  191. showMenu (itemUnderMouse);
  192. }
  193. }
  194. void MenuBarComponent::mouseDrag (const MouseEvent& e)
  195. {
  196. const MouseEvent e2 (e.getEventRelativeTo (this));
  197. const int item = getItemAt (e2.getPosition());
  198. if (item >= 0)
  199. showMenu (item);
  200. }
  201. void MenuBarComponent::mouseUp (const MouseEvent& e)
  202. {
  203. const MouseEvent e2 (e.getEventRelativeTo (this));
  204. updateItemUnderMouse (e2.getPosition());
  205. if (itemUnderMouse < 0 && getLocalBounds().contains (e2.x, e2.y))
  206. {
  207. setOpenItem (-1);
  208. PopupMenu::dismissAllActiveMenus();
  209. }
  210. }
  211. void MenuBarComponent::mouseMove (const MouseEvent& e)
  212. {
  213. const MouseEvent e2 (e.getEventRelativeTo (this));
  214. if (lastMousePos != e2.getPosition())
  215. {
  216. if (currentPopupIndex >= 0)
  217. {
  218. const int item = getItemAt (e2.getPosition());
  219. if (item >= 0)
  220. showMenu (item);
  221. }
  222. else
  223. {
  224. updateItemUnderMouse (e2.getPosition());
  225. }
  226. lastMousePos = e2.getPosition();
  227. }
  228. }
  229. bool MenuBarComponent::keyPressed (const KeyPress& key)
  230. {
  231. const int numMenus = menuNames.size();
  232. if (numMenus > 0)
  233. {
  234. const int currentIndex = jlimit (0, numMenus - 1, currentPopupIndex);
  235. if (key.isKeyCode (KeyPress::leftKey))
  236. {
  237. showMenu ((currentIndex + numMenus - 1) % numMenus);
  238. return true;
  239. }
  240. if (key.isKeyCode (KeyPress::rightKey))
  241. {
  242. showMenu ((currentIndex + 1) % numMenus);
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248. void MenuBarComponent::menuBarItemsChanged (MenuBarModel* /*menuBarModel*/)
  249. {
  250. StringArray newNames;
  251. if (model != nullptr)
  252. newNames = model->getMenuBarNames();
  253. if (newNames != menuNames)
  254. {
  255. menuNames = newNames;
  256. repaint();
  257. resized();
  258. }
  259. }
  260. void MenuBarComponent::menuCommandInvoked (MenuBarModel* /*menuBarModel*/,
  261. const ApplicationCommandTarget::InvocationInfo& info)
  262. {
  263. if (model == nullptr || (info.commandFlags & ApplicationCommandInfo::dontTriggerVisualFeedback) != 0)
  264. return;
  265. for (int i = 0; i < menuNames.size(); ++i)
  266. {
  267. const PopupMenu menu (model->getMenuForIndex (i, menuNames [i]));
  268. if (menu.containsCommandItem (info.commandID))
  269. {
  270. setItemUnderMouse (i);
  271. startTimer (200);
  272. break;
  273. }
  274. }
  275. }
  276. void MenuBarComponent::timerCallback()
  277. {
  278. stopTimer();
  279. updateItemUnderMouse (getMouseXYRelative());
  280. }