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.

524 lines
20KB

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