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.

364 lines
12KB

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