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.

351 lines
9.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. MenuBarComponent::MenuBarComponent (MenuBarModel* model_)
  21. : model (nullptr),
  22. itemUnderMouse (-1),
  23. currentPopupIndex (-1),
  24. topLevelIndexClicked (0)
  25. {
  26. setRepaintsOnMouseActivity (true);
  27. setWantsKeyboardFocus (false);
  28. setMouseClickGrabsKeyboardFocus (false);
  29. setModel (model_);
  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 (const Point<int>& p)
  93. {
  94. for (int i = 0; i < xPositions.size(); ++i)
  95. if (p.getX() >= xPositions[i] && p.getX() < 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. repaintMenuItem (currentPopupIndex);
  122. currentPopupIndex = index;
  123. repaintMenuItem (currentPopupIndex);
  124. Desktop& desktop = Desktop::getInstance();
  125. if (index >= 0)
  126. desktop.addGlobalMouseListener (this);
  127. else
  128. desktop.removeGlobalMouseListener (this);
  129. }
  130. }
  131. void MenuBarComponent::updateItemUnderMouse (const Point<int>& p)
  132. {
  133. setItemUnderMouse (getItemAt (p));
  134. }
  135. void MenuBarComponent::showMenu (int index)
  136. {
  137. if (index != currentPopupIndex)
  138. {
  139. PopupMenu::dismissAllActiveMenus();
  140. menuBarItemsChanged (nullptr);
  141. setOpenItem (index);
  142. setItemUnderMouse (index);
  143. if (index >= 0)
  144. {
  145. PopupMenu m (model->getMenuForIndex (itemUnderMouse,
  146. menuNames [itemUnderMouse]));
  147. if (m.lookAndFeel == nullptr)
  148. m.setLookAndFeel (&getLookAndFeel());
  149. const Rectangle<int> itemPos (xPositions [index], 0, xPositions [index + 1] - xPositions [index], getHeight());
  150. m.showMenuAsync (PopupMenu::Options().withTargetComponent (this)
  151. .withTargetScreenArea (localAreaToGlobal (itemPos))
  152. .withMinimumWidth (itemPos.getWidth()),
  153. ModalCallbackFunction::forComponent (menuBarMenuDismissedCallback, this, index));
  154. }
  155. }
  156. }
  157. void MenuBarComponent::menuBarMenuDismissedCallback (int result, MenuBarComponent* bar, int topLevelIndex)
  158. {
  159. if (bar != nullptr)
  160. bar->menuDismissed (topLevelIndex, result);
  161. }
  162. void MenuBarComponent::menuDismissed (int topLevelIndex, int itemId)
  163. {
  164. topLevelIndexClicked = topLevelIndex;
  165. postCommandMessage (itemId);
  166. }
  167. void MenuBarComponent::handleCommandMessage (int commandId)
  168. {
  169. const Point<int> mousePos (getMouseXYRelative());
  170. updateItemUnderMouse (mousePos);
  171. if (currentPopupIndex == topLevelIndexClicked)
  172. setOpenItem (-1);
  173. if (commandId != 0 && model != nullptr)
  174. model->menuItemSelected (commandId, topLevelIndexClicked);
  175. }
  176. //==============================================================================
  177. void MenuBarComponent::mouseEnter (const MouseEvent& e)
  178. {
  179. if (e.eventComponent == this)
  180. updateItemUnderMouse (e.getPosition());
  181. }
  182. void MenuBarComponent::mouseExit (const MouseEvent& e)
  183. {
  184. if (e.eventComponent == this)
  185. updateItemUnderMouse (e.getPosition());
  186. }
  187. void MenuBarComponent::mouseDown (const MouseEvent& e)
  188. {
  189. if (currentPopupIndex < 0)
  190. {
  191. const MouseEvent e2 (e.getEventRelativeTo (this));
  192. updateItemUnderMouse (e2.getPosition());
  193. currentPopupIndex = -2;
  194. showMenu (itemUnderMouse);
  195. }
  196. }
  197. void MenuBarComponent::mouseDrag (const MouseEvent& e)
  198. {
  199. const MouseEvent e2 (e.getEventRelativeTo (this));
  200. const int item = getItemAt (e2.getPosition());
  201. if (item >= 0)
  202. showMenu (item);
  203. }
  204. void MenuBarComponent::mouseUp (const MouseEvent& e)
  205. {
  206. const MouseEvent e2 (e.getEventRelativeTo (this));
  207. updateItemUnderMouse (e2.getPosition());
  208. if (itemUnderMouse < 0 && getLocalBounds().contains (e2.x, e2.y))
  209. {
  210. setOpenItem (-1);
  211. PopupMenu::dismissAllActiveMenus();
  212. }
  213. }
  214. void MenuBarComponent::mouseMove (const MouseEvent& e)
  215. {
  216. const MouseEvent e2 (e.getEventRelativeTo (this));
  217. if (lastMousePos != e2.getPosition())
  218. {
  219. if (currentPopupIndex >= 0)
  220. {
  221. const int item = getItemAt (e2.getPosition());
  222. if (item >= 0)
  223. showMenu (item);
  224. }
  225. else
  226. {
  227. updateItemUnderMouse (e2.getPosition());
  228. }
  229. lastMousePos = e2.getPosition();
  230. }
  231. }
  232. bool MenuBarComponent::keyPressed (const KeyPress& key)
  233. {
  234. bool used = false;
  235. const int numMenus = menuNames.size();
  236. const int currentIndex = jlimit (0, menuNames.size() - 1, currentPopupIndex);
  237. if (key.isKeyCode (KeyPress::leftKey))
  238. {
  239. showMenu ((currentIndex + numMenus - 1) % numMenus);
  240. used = true;
  241. }
  242. else if (key.isKeyCode (KeyPress::rightKey))
  243. {
  244. showMenu ((currentIndex + 1) % numMenus);
  245. used = true;
  246. }
  247. return used;
  248. }
  249. void MenuBarComponent::menuBarItemsChanged (MenuBarModel* /*menuBarModel*/)
  250. {
  251. StringArray newNames;
  252. if (model != nullptr)
  253. newNames = model->getMenuBarNames();
  254. if (newNames != menuNames)
  255. {
  256. menuNames = newNames;
  257. repaint();
  258. resized();
  259. }
  260. }
  261. void MenuBarComponent::menuCommandInvoked (MenuBarModel* /*menuBarModel*/,
  262. const ApplicationCommandTarget::InvocationInfo& info)
  263. {
  264. if (model == nullptr || (info.commandFlags & ApplicationCommandInfo::dontTriggerVisualFeedback) != 0)
  265. return;
  266. for (int i = 0; i < menuNames.size(); ++i)
  267. {
  268. const PopupMenu menu (model->getMenuForIndex (i, menuNames [i]));
  269. if (menu.containsCommandItem (info.commandID))
  270. {
  271. setItemUnderMouse (i);
  272. startTimer (200);
  273. break;
  274. }
  275. }
  276. }
  277. void MenuBarComponent::timerCallback()
  278. {
  279. stopTimer();
  280. updateItemUnderMouse (getMouseXYRelative());
  281. }
  282. END_JUCE_NAMESPACE