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.

510 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: MenusDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Showcases menu features.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics, juce_gui_extra
  26. exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
  27. type: Component
  28. mainClass: MenusDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #include "../Assets/DemoUtilities.h"
  34. //==============================================================================
  35. /**
  36. This struct contains a header component that will be used when the burger menu
  37. is enabled. It contains an icon that can be used to show the side panel containing
  38. the menu.
  39. */
  40. struct BurgerMenuHeader : public Component
  41. {
  42. BurgerMenuHeader (SidePanel& sp)
  43. : sidePanel (sp)
  44. {
  45. static const unsigned char burgerMenuPathData[]
  46. = { 110,109,0,0,128,64,0,0,32,65,108,0,0,224,65,0,0,32,65,98,254,212,232,65,0,0,32,65,0,0,240,65,252,
  47. 169,17,65,0,0,240,65,0,0,0,65,98,0,0,240,65,8,172,220,64,254,212,232,65,0,0,192,64,0,0,224,65,0,0,
  48. 192,64,108,0,0,128,64,0,0,192,64,98,16,88,57,64,0,0,192,64,0,0,0,64,8,172,220,64,0,0,0,64,0,0,0,65,
  49. 98,0,0,0,64,252,169,17,65,16,88,57,64,0,0,32,65,0,0,128,64,0,0,32,65,99,109,0,0,224,65,0,0,96,65,108,
  50. 0,0,128,64,0,0,96,65,98,16,88,57,64,0,0,96,65,0,0,0,64,4,86,110,65,0,0,0,64,0,0,128,65,98,0,0,0,64,
  51. 254,212,136,65,16,88,57,64,0,0,144,65,0,0,128,64,0,0,144,65,108,0,0,224,65,0,0,144,65,98,254,212,232,
  52. 65,0,0,144,65,0,0,240,65,254,212,136,65,0,0,240,65,0,0,128,65,98,0,0,240,65,4,86,110,65,254,212,232,
  53. 65,0,0,96,65,0,0,224,65,0,0,96,65,99,109,0,0,224,65,0,0,176,65,108,0,0,128,64,0,0,176,65,98,16,88,57,
  54. 64,0,0,176,65,0,0,0,64,2,43,183,65,0,0,0,64,0,0,192,65,98,0,0,0,64,254,212,200,65,16,88,57,64,0,0,208,
  55. 65,0,0,128,64,0,0,208,65,108,0,0,224,65,0,0,208,65,98,254,212,232,65,0,0,208,65,0,0,240,65,254,212,
  56. 200,65,0,0,240,65,0,0,192,65,98,0,0,240,65,2,43,183,65,254,212,232,65,0,0,176,65,0,0,224,65,0,0,176,
  57. 65,99,101,0,0 };
  58. Path p;
  59. p.loadPathFromData (burgerMenuPathData, sizeof (burgerMenuPathData));
  60. burgerButton.setShape (p, true, true, false);
  61. burgerButton.onClick = [this] { showOrHide(); };
  62. addAndMakeVisible (burgerButton);
  63. }
  64. ~BurgerMenuHeader()
  65. {
  66. sidePanel.showOrHide (false);
  67. }
  68. private:
  69. void paint (Graphics& g) override
  70. {
  71. auto titleBarBackgroundColour = getLookAndFeel().findColour (ResizableWindow::backgroundColourId)
  72. .darker();
  73. g.setColour (titleBarBackgroundColour);
  74. g.fillRect (getLocalBounds());
  75. }
  76. void resized() override
  77. {
  78. auto r = getLocalBounds();
  79. burgerButton.setBounds (r.removeFromRight (40).withSizeKeepingCentre (20, 20));
  80. titleLabel.setFont (Font (getHeight() * 0.5f, Font::plain));
  81. titleLabel.setBounds (r);
  82. }
  83. void showOrHide()
  84. {
  85. sidePanel.showOrHide (! sidePanel.isPanelShowing());
  86. }
  87. SidePanel& sidePanel;
  88. Label titleLabel { "titleLabel", "JUCE Demo" };
  89. ShapeButton burgerButton { "burgerButton", Colours::lightgrey, Colours::lightgrey, Colours::white };
  90. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BurgerMenuHeader)
  91. };
  92. //==============================================================================
  93. class MenusDemo : public Component,
  94. public ApplicationCommandTarget,
  95. public MenuBarModel
  96. {
  97. public:
  98. //==============================================================================
  99. /** A list of the commands that this demo responds to. */
  100. enum CommandIDs
  101. {
  102. menuPositionInsideWindow = 1,
  103. menuPositionGlobalMenuBar,
  104. menuPositionBurgerMenu,
  105. outerColourRed,
  106. outerColourGreen,
  107. outerColourBlue,
  108. innerColourRed,
  109. innerColourGreen,
  110. innerColourBlue
  111. };
  112. //==============================================================================
  113. /** Represents the possible menu positions. */
  114. enum class MenuBarPosition
  115. {
  116. window,
  117. global,
  118. burger
  119. };
  120. //==============================================================================
  121. MenusDemo()
  122. {
  123. menuBar.reset (new MenuBarComponent (this));
  124. addAndMakeVisible (menuBar.get());
  125. setApplicationCommandManagerToWatch (&commandManager);
  126. commandManager.registerAllCommandsForTarget (this);
  127. // this lets the command manager use keypresses that arrive in our window to send out commands
  128. addKeyListener (commandManager.getKeyMappings());
  129. addChildComponent (menuHeader);
  130. addAndMakeVisible (outerCommandTarget);
  131. addAndMakeVisible (sidePanel);
  132. setSize (500, 500);
  133. }
  134. ~MenusDemo()
  135. {
  136. #if JUCE_MAC
  137. MenuBarModel::setMacMainMenu (nullptr);
  138. #endif
  139. }
  140. void resized() override
  141. {
  142. auto b = getLocalBounds();
  143. if (menuBarPosition == MenuBarPosition::window)
  144. {
  145. menuBar->setBounds (b.removeFromTop (LookAndFeel::getDefaultLookAndFeel()
  146. .getDefaultMenuBarHeight()));
  147. }
  148. else if (menuBarPosition == MenuBarPosition::burger)
  149. {
  150. menuHeader.setBounds (b.removeFromTop (40));
  151. }
  152. outerCommandTarget.setBounds (b);
  153. }
  154. //==============================================================================
  155. StringArray getMenuBarNames() override
  156. {
  157. return { "Menu Position", "Outer Colour", "Inner Colour" };
  158. }
  159. PopupMenu getMenuForIndex (int menuIndex, const String& /*menuName*/) override
  160. {
  161. PopupMenu menu;
  162. if (menuIndex == 0)
  163. {
  164. menu.addCommandItem (&commandManager, CommandIDs::menuPositionInsideWindow);
  165. #if JUCE_MAC
  166. menu.addCommandItem (&commandManager, CommandIDs::menuPositionGlobalMenuBar);
  167. #endif
  168. menu.addCommandItem (&commandManager, CommandIDs::menuPositionBurgerMenu);
  169. }
  170. else if (menuIndex == 1)
  171. {
  172. menu.addCommandItem (&commandManager, CommandIDs::outerColourRed);
  173. menu.addCommandItem (&commandManager, CommandIDs::outerColourGreen);
  174. menu.addCommandItem (&commandManager, CommandIDs::outerColourBlue);
  175. }
  176. else if (menuIndex == 2)
  177. {
  178. menu.addCommandItem (&commandManager, CommandIDs::innerColourRed);
  179. menu.addCommandItem (&commandManager, CommandIDs::innerColourGreen);
  180. menu.addCommandItem (&commandManager, CommandIDs::innerColourBlue);
  181. }
  182. return menu;
  183. }
  184. void menuItemSelected (int /*menuItemID*/, int /*topLevelMenuIndex*/) override {}
  185. //==============================================================================
  186. // The following methods implement the ApplicationCommandTarget interface, allowing
  187. // this window to publish a set of actions it can perform, and which can be mapped
  188. // onto menus, keypresses, etc.
  189. ApplicationCommandTarget* getNextCommandTarget() override
  190. {
  191. return &outerCommandTarget;
  192. }
  193. void getAllCommands (Array<CommandID>& c) override
  194. {
  195. Array<CommandID> commands { CommandIDs::menuPositionInsideWindow,
  196. CommandIDs::menuPositionGlobalMenuBar,
  197. CommandIDs::menuPositionBurgerMenu };
  198. c.addArray (commands);
  199. }
  200. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result) override
  201. {
  202. switch (commandID)
  203. {
  204. case CommandIDs::menuPositionInsideWindow:
  205. result.setInfo ("Inside Window", "Places the menu bar inside the application window", "Menu", 0);
  206. result.setTicked (menuBarPosition == MenuBarPosition::window);
  207. result.addDefaultKeypress ('w', ModifierKeys::shiftModifier);
  208. break;
  209. case CommandIDs::menuPositionGlobalMenuBar:
  210. result.setInfo ("Global", "Uses a global menu bar", "Menu", 0);
  211. result.setTicked (menuBarPosition == MenuBarPosition::global);
  212. result.addDefaultKeypress ('g', ModifierKeys::shiftModifier);
  213. break;
  214. case CommandIDs::menuPositionBurgerMenu:
  215. result.setInfo ("Burger Menu", "Uses a burger menu", "Menu", 0);
  216. result.setTicked (menuBarPosition == MenuBarPosition::burger);
  217. result.addDefaultKeypress ('b', ModifierKeys::shiftModifier);
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. bool perform (const InvocationInfo& info) override
  224. {
  225. switch (info.commandID)
  226. {
  227. case CommandIDs::menuPositionInsideWindow:
  228. setMenuBarPosition (MenuBarPosition::window);
  229. break;
  230. case CommandIDs::menuPositionGlobalMenuBar:
  231. setMenuBarPosition (MenuBarPosition::global);
  232. break;
  233. case CommandIDs::menuPositionBurgerMenu:
  234. setMenuBarPosition (MenuBarPosition::burger);
  235. break;
  236. default:
  237. return false;
  238. }
  239. return true;
  240. }
  241. void setMenuBarPosition (MenuBarPosition newPosition)
  242. {
  243. if (menuBarPosition != newPosition)
  244. {
  245. menuBarPosition = newPosition;
  246. if (menuBarPosition != MenuBarPosition::burger)
  247. sidePanel.showOrHide (false);
  248. #if JUCE_MAC
  249. MenuBarModel::setMacMainMenu (menuBarPosition == MenuBarPosition::global ? this : nullptr);
  250. #endif
  251. menuBar->setVisible (menuBarPosition == MenuBarPosition::window);
  252. burgerMenu.setModel (menuBarPosition == MenuBarPosition::burger ? this : nullptr);
  253. menuHeader.setVisible (menuBarPosition == MenuBarPosition::burger);
  254. sidePanel.setContent (menuBarPosition == MenuBarPosition::burger ? &burgerMenu : nullptr, false);
  255. menuItemsChanged();
  256. resized();
  257. }
  258. }
  259. private:
  260. ApplicationCommandManager commandManager;
  261. std::unique_ptr<MenuBarComponent> menuBar;
  262. MenuBarPosition menuBarPosition = MenuBarPosition::window;
  263. SidePanel sidePanel { "Menu", 300, false };
  264. BurgerMenuComponent burgerMenu;
  265. BurgerMenuHeader menuHeader { sidePanel };
  266. //==============================================================================
  267. /**
  268. Command messages that aren't handled in the main component will be passed
  269. to this class to respond to.
  270. */
  271. class OuterCommandTarget : public Component,
  272. public ApplicationCommandTarget
  273. {
  274. public:
  275. OuterCommandTarget (ApplicationCommandManager& m)
  276. : commandManager (m),
  277. innerCommandTarget (commandManager)
  278. {
  279. commandManager.registerAllCommandsForTarget (this);
  280. addAndMakeVisible (innerCommandTarget);
  281. }
  282. void resized() override
  283. {
  284. innerCommandTarget.setBounds (getLocalBounds().reduced (50));
  285. }
  286. void paint (Graphics& g) override
  287. {
  288. g.fillAll (currentColour);
  289. }
  290. //==============================================================================
  291. ApplicationCommandTarget* getNextCommandTarget() override
  292. {
  293. return &innerCommandTarget;
  294. }
  295. void getAllCommands (Array<CommandID>& c) override
  296. {
  297. Array<CommandID> commands { CommandIDs::outerColourRed,
  298. CommandIDs::outerColourGreen,
  299. CommandIDs::outerColourBlue };
  300. c.addArray (commands);
  301. }
  302. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result) override
  303. {
  304. switch (commandID)
  305. {
  306. case CommandIDs::outerColourRed:
  307. result.setInfo ("Red", "Sets the outer colour to red", "Outer", 0);
  308. result.setTicked (currentColour == Colours::red);
  309. result.addDefaultKeypress ('r', ModifierKeys::commandModifier);
  310. break;
  311. case CommandIDs::outerColourGreen:
  312. result.setInfo ("Green", "Sets the outer colour to green", "Outer", 0);
  313. result.setTicked (currentColour == Colours::green);
  314. result.addDefaultKeypress ('g', ModifierKeys::commandModifier);
  315. break;
  316. case CommandIDs::outerColourBlue:
  317. result.setInfo ("Blue", "Sets the outer colour to blue", "Outer", 0);
  318. result.setTicked (currentColour == Colours::blue);
  319. result.addDefaultKeypress ('b', ModifierKeys::commandModifier);
  320. break;
  321. default:
  322. break;
  323. }
  324. }
  325. bool perform (const InvocationInfo& info) override
  326. {
  327. switch (info.commandID)
  328. {
  329. case CommandIDs::outerColourRed:
  330. currentColour = Colours::red;
  331. break;
  332. case CommandIDs::outerColourGreen:
  333. currentColour = Colours::green;
  334. break;
  335. case CommandIDs::outerColourBlue:
  336. currentColour = Colours::blue;
  337. break;
  338. default:
  339. return false;
  340. }
  341. repaint();
  342. return true;
  343. }
  344. private:
  345. //==============================================================================
  346. /**
  347. Command messages that aren't handled in the OuterCommandTarget will be passed
  348. to this class to respond to.
  349. */
  350. struct InnerCommandTarget : public Component,
  351. public ApplicationCommandTarget
  352. {
  353. InnerCommandTarget (ApplicationCommandManager& m)
  354. : commandManager (m)
  355. {
  356. commandManager.registerAllCommandsForTarget (this);
  357. }
  358. void paint (Graphics& g) override
  359. {
  360. g.fillAll (currentColour);
  361. }
  362. //==============================================================================
  363. ApplicationCommandTarget* getNextCommandTarget() override
  364. {
  365. // this will return the next parent component that is an ApplicationCommandTarget
  366. return findFirstTargetParentComponent();
  367. }
  368. void getAllCommands (Array<CommandID>& c) override
  369. {
  370. Array<CommandID> commands { CommandIDs::innerColourRed,
  371. CommandIDs::innerColourGreen,
  372. CommandIDs::innerColourBlue };
  373. c.addArray (commands);
  374. }
  375. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result) override
  376. {
  377. switch (commandID)
  378. {
  379. case CommandIDs::innerColourRed:
  380. result.setInfo ("Red", "Sets the inner colour to red", "Inner", 0);
  381. result.setTicked (currentColour == Colours::red);
  382. result.addDefaultKeypress ('r', ModifierKeys::commandModifier | ModifierKeys::shiftModifier);
  383. break;
  384. case CommandIDs::innerColourGreen:
  385. result.setInfo ("Green", "Sets the inner colour to green", "Inner", 0);
  386. result.setTicked (currentColour == Colours::green);
  387. result.addDefaultKeypress ('g', ModifierKeys::commandModifier | ModifierKeys::shiftModifier);
  388. break;
  389. case CommandIDs::innerColourBlue:
  390. result.setInfo ("Blue", "Sets the inner colour to blue", "Inner", 0);
  391. result.setTicked (currentColour == Colours::blue);
  392. result.addDefaultKeypress ('b', ModifierKeys::commandModifier | ModifierKeys::shiftModifier);
  393. break;
  394. default:
  395. break;
  396. }
  397. }
  398. bool perform (const InvocationInfo& info) override
  399. {
  400. switch (info.commandID)
  401. {
  402. case CommandIDs::innerColourRed:
  403. currentColour = Colours::red;
  404. break;
  405. case CommandIDs::innerColourGreen:
  406. currentColour = Colours::green;
  407. break;
  408. case CommandIDs::innerColourBlue:
  409. currentColour = Colours::blue;
  410. break;
  411. default:
  412. return false;
  413. }
  414. repaint();
  415. return true;
  416. }
  417. ApplicationCommandManager& commandManager;
  418. Colour currentColour { Colours::blue };
  419. };
  420. ApplicationCommandManager& commandManager;
  421. InnerCommandTarget innerCommandTarget;
  422. Colour currentColour { Colours::red };
  423. };
  424. OuterCommandTarget outerCommandTarget { commandManager };
  425. //==============================================================================
  426. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MenusDemo)
  427. };