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.

366 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2020 - 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: WindowsDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Displays various types of windows.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics, juce_gui_extra
  26. exporters: xcode_mac, vs2019, linux_make, androidstudio, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: WindowsDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. //==============================================================================
  36. /** Just a simple window that deletes itself when closed. */
  37. class BasicWindow : public DocumentWindow
  38. {
  39. public:
  40. BasicWindow (const String& name, Colour backgroundColour, int buttonsNeeded)
  41. : DocumentWindow (name, backgroundColour, buttonsNeeded)
  42. {}
  43. void closeButtonPressed()
  44. {
  45. delete this;
  46. }
  47. private:
  48. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BasicWindow)
  49. };
  50. //==============================================================================
  51. /** This window contains a ColourSelector which can be used to change the window's colour. */
  52. class ColourSelectorWindow : public DocumentWindow,
  53. private ChangeListener
  54. {
  55. public:
  56. ColourSelectorWindow (const String& name, Colour backgroundColour, int buttonsNeeded)
  57. : DocumentWindow (name, backgroundColour, buttonsNeeded)
  58. {
  59. selector.setCurrentColour (backgroundColour);
  60. selector.setColour (ColourSelector::backgroundColourId, Colours::transparentWhite);
  61. selector.addChangeListener (this);
  62. setContentOwned (&selector, false);
  63. }
  64. ~ColourSelectorWindow()
  65. {
  66. selector.removeChangeListener (this);
  67. }
  68. void closeButtonPressed()
  69. {
  70. delete this;
  71. }
  72. private:
  73. ColourSelector selector { ColourSelector::showColourAtTop
  74. | ColourSelector::showSliders
  75. | ColourSelector::showColourspace };
  76. void changeListenerCallback (ChangeBroadcaster* source)
  77. {
  78. if (source == &selector)
  79. setBackgroundColour (selector.getCurrentColour());
  80. }
  81. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourSelectorWindow)
  82. };
  83. //==============================================================================
  84. class BouncingBallComponent : public Component,
  85. public Timer
  86. {
  87. public:
  88. BouncingBallComponent()
  89. {
  90. setInterceptsMouseClicks (false, false);
  91. Random random;
  92. auto size = 10.0f + (float) random.nextInt (30);
  93. ballBounds.setBounds (random.nextFloat() * 100.0f,
  94. random.nextFloat() * 100.0f,
  95. size, size);
  96. direction.x = random.nextFloat() * 8.0f - 4.0f;
  97. direction.y = random.nextFloat() * 8.0f - 4.0f;
  98. colour = Colour ((juce::uint32) random.nextInt())
  99. .withAlpha (0.5f)
  100. .withBrightness (0.7f);
  101. startTimer (60);
  102. }
  103. void paint (Graphics& g) override
  104. {
  105. g.setColour (colour);
  106. g.fillEllipse (ballBounds - getPosition().toFloat());
  107. }
  108. void timerCallback() override
  109. {
  110. ballBounds += direction;
  111. if (ballBounds.getX() < 0) direction.x = std::abs (direction.x);
  112. if (ballBounds.getY() < 0) direction.y = std::abs (direction.y);
  113. if (ballBounds.getRight() > (float) getParentWidth()) direction.x = -std::abs (direction.x);
  114. if (ballBounds.getBottom() > (float) getParentHeight()) direction.y = -std::abs (direction.y);
  115. setBounds (ballBounds.getSmallestIntegerContainer());
  116. }
  117. private:
  118. Colour colour;
  119. Rectangle<float> ballBounds;
  120. Point<float> direction;
  121. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BouncingBallComponent)
  122. };
  123. //==============================================================================
  124. class BouncingBallsContainer : public Component
  125. {
  126. public:
  127. BouncingBallsContainer (int numBalls)
  128. {
  129. for (int i = 0; i < numBalls; ++i)
  130. {
  131. auto* newBall = new BouncingBallComponent();
  132. balls.add (newBall);
  133. addAndMakeVisible (newBall);
  134. }
  135. }
  136. void mouseDown (const MouseEvent& e) override
  137. {
  138. dragger.startDraggingComponent (this, e);
  139. }
  140. void mouseDrag (const MouseEvent& e) override
  141. {
  142. // as there's no titlebar we have to manage the dragging ourselves
  143. dragger.dragComponent (this, e, nullptr);
  144. }
  145. void paint (Graphics& g) override
  146. {
  147. if (isOpaque())
  148. g.fillAll (Colours::white);
  149. else
  150. g.fillAll (Colours::blue.withAlpha (0.2f));
  151. g.setFont (16.0f);
  152. g.setColour (Colours::black);
  153. g.drawFittedText ("This window has no titlebar and a transparent background.",
  154. getLocalBounds().reduced (8, 0),
  155. Justification::centred, 5);
  156. g.drawRect (getLocalBounds());
  157. }
  158. private:
  159. ComponentDragger dragger;
  160. OwnedArray<BouncingBallComponent> balls;
  161. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BouncingBallsContainer)
  162. };
  163. //==============================================================================
  164. class WindowsDemo : public Component
  165. {
  166. public:
  167. enum Windows
  168. {
  169. dialog,
  170. document,
  171. alert,
  172. numWindows
  173. };
  174. WindowsDemo()
  175. {
  176. setOpaque (true);
  177. addAndMakeVisible (showWindowsButton);
  178. showWindowsButton.onClick = [this] { showAllWindows(); };
  179. addAndMakeVisible (closeWindowsButton);
  180. closeWindowsButton.onClick = [this] { closeAllWindows(); };
  181. setSize (250, 250);
  182. }
  183. ~WindowsDemo() override
  184. {
  185. if (dialogWindow != nullptr)
  186. {
  187. dialogWindow->exitModalState (0);
  188. // we are shutting down: can't wait for the message manager
  189. // to eventually delete this
  190. delete dialogWindow;
  191. }
  192. closeAllWindows();
  193. }
  194. void paint (Graphics& g) override
  195. {
  196. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  197. Colours::grey));
  198. }
  199. void resized() override
  200. {
  201. Rectangle<int> buttonSize (0, 0, 108, 28);
  202. Rectangle<int> area ((getWidth() / 2) - (buttonSize.getWidth() / 2),
  203. (getHeight() / 2) - buttonSize.getHeight(),
  204. buttonSize.getWidth(), buttonSize.getHeight());
  205. showWindowsButton .setBounds (area.reduced (2));
  206. closeWindowsButton.setBounds (area.translated (0, buttonSize.getHeight()).reduced (2));
  207. }
  208. private:
  209. // Because in this demo the windows delete themselves, we'll use the
  210. // Component::SafePointer class to point to them, which automatically becomes
  211. // null when the component that it points to is deleted.
  212. Array<Component::SafePointer<Component>> windows;
  213. SafePointer<DialogWindow> dialogWindow;
  214. TextButton showWindowsButton { "Show Windows" },
  215. closeWindowsButton { "Close Windows" };
  216. void showAllWindows()
  217. {
  218. closeAllWindows();
  219. showDocumentWindow (false);
  220. showDocumentWindow (true);
  221. showTransparentWindow();
  222. showDialogWindow();
  223. }
  224. void closeAllWindows()
  225. {
  226. for (auto& window : windows)
  227. window.deleteAndZero();
  228. windows.clear();
  229. }
  230. void showDialogWindow()
  231. {
  232. String m;
  233. m << "Dialog Windows can be used to quickly show a component, usually blocking mouse input to other windows." << newLine
  234. << newLine
  235. << "They can also be quickly closed with the escape key, try it now.";
  236. DialogWindow::LaunchOptions options;
  237. auto* label = new Label();
  238. label->setText (m, dontSendNotification);
  239. label->setColour (Label::textColourId, Colours::whitesmoke);
  240. options.content.setOwned (label);
  241. Rectangle<int> area (0, 0, 300, 200);
  242. options.content->setSize (area.getWidth(), area.getHeight());
  243. options.dialogTitle = "Dialog Window";
  244. options.dialogBackgroundColour = Colour (0xff0e345a);
  245. options.escapeKeyTriggersCloseButton = true;
  246. options.useNativeTitleBar = false;
  247. options.resizable = true;
  248. dialogWindow = options.launchAsync();
  249. if (dialogWindow != nullptr)
  250. dialogWindow->centreWithSize (300, 200);
  251. }
  252. void showDocumentWindow (bool native)
  253. {
  254. auto* dw = new ColourSelectorWindow ("Document Window", getRandomBrightColour(), DocumentWindow::allButtons);
  255. windows.add (dw);
  256. Rectangle<int> area (0, 0, 300, 400);
  257. RectanglePlacement placement ((native ? RectanglePlacement::xLeft
  258. : RectanglePlacement::xRight)
  259. | RectanglePlacement::yTop
  260. | RectanglePlacement::doNotResize);
  261. auto result = placement.appliedTo (area, Desktop::getInstance().getDisplays()
  262. .getPrimaryDisplay()->userArea.reduced (20));
  263. dw->setBounds (result);
  264. dw->setResizable (true, ! native);
  265. dw->setUsingNativeTitleBar (native);
  266. dw->setVisible (true);
  267. }
  268. void showTransparentWindow()
  269. {
  270. auto* balls = new BouncingBallsContainer (3);
  271. balls->addToDesktop (ComponentPeer::windowIsTemporary);
  272. windows.add (balls);
  273. Rectangle<int> area (0, 0, 200, 200);
  274. RectanglePlacement placement (RectanglePlacement::xLeft
  275. | RectanglePlacement::yBottom
  276. | RectanglePlacement::doNotResize);
  277. auto result = placement.appliedTo (area, Desktop::getInstance().getDisplays()
  278. .getPrimaryDisplay()->userArea.reduced (20));
  279. balls->setBounds (result);
  280. balls->setVisible (true);
  281. }
  282. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowsDemo)
  283. };