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.

380 lines
10KB

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