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.

342 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceDemoHeader.h"
  18. //==============================================================================
  19. /** Just a simple window that deletes itself when closed. */
  20. class BasicWindow : public DocumentWindow
  21. {
  22. public:
  23. BasicWindow (const String& name, Colour backgroundColour, int buttonsNeeded)
  24. : DocumentWindow (name, backgroundColour, buttonsNeeded)
  25. {
  26. }
  27. void closeButtonPressed()
  28. {
  29. delete this;
  30. }
  31. private:
  32. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BasicWindow)
  33. };
  34. //==============================================================================
  35. /** This window contains a ColourSelector which can be used to change the window's colour. */
  36. class ColourSelectorWindow : public DocumentWindow,
  37. private ChangeListener
  38. {
  39. public:
  40. ColourSelectorWindow (const String& name, Colour backgroundColour, int buttonsNeeded)
  41. : DocumentWindow (name, backgroundColour, buttonsNeeded),
  42. selector (ColourSelector::showColourAtTop
  43. | ColourSelector::showSliders
  44. | ColourSelector::showColourspace)
  45. {
  46. selector.setCurrentColour (backgroundColour);
  47. selector.setColour (ColourSelector::backgroundColourId, Colours::transparentWhite);
  48. selector.addChangeListener (this);
  49. setContentOwned (&selector, false);
  50. }
  51. ~ColourSelectorWindow()
  52. {
  53. selector.removeChangeListener (this);
  54. }
  55. void closeButtonPressed()
  56. {
  57. delete this;
  58. }
  59. private:
  60. ColourSelector selector;
  61. void changeListenerCallback (ChangeBroadcaster* source)
  62. {
  63. if (source == &selector)
  64. setBackgroundColour (selector.getCurrentColour());
  65. }
  66. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourSelectorWindow)
  67. };
  68. //==============================================================================
  69. class BouncingBallComponent : public Component,
  70. public Timer
  71. {
  72. public:
  73. BouncingBallComponent()
  74. {
  75. setInterceptsMouseClicks (false, false);
  76. Random random;
  77. const float size = 10.0f + random.nextInt (30);
  78. ballBounds.setBounds (random.nextFloat() * 100.0f,
  79. random.nextFloat() * 100.0f,
  80. size, size);
  81. direction.x = random.nextFloat() * 8.0f - 4.0f;
  82. direction.y = random.nextFloat() * 8.0f - 4.0f;
  83. colour = Colour ((juce::uint32) random.nextInt())
  84. .withAlpha (0.5f)
  85. .withBrightness (0.7f);
  86. startTimer (60);
  87. }
  88. void paint (Graphics& g) override
  89. {
  90. g.setColour (colour);
  91. g.fillEllipse (ballBounds - getPosition().toFloat());
  92. }
  93. void timerCallback() override
  94. {
  95. ballBounds += direction;
  96. if (ballBounds.getX() < 0) direction.x = std::abs (direction.x);
  97. if (ballBounds.getY() < 0) direction.y = std::abs (direction.y);
  98. if (ballBounds.getRight() > getParentWidth()) direction.x = -std::abs (direction.x);
  99. if (ballBounds.getBottom() > getParentHeight()) direction.y = -std::abs (direction.y);
  100. setBounds (ballBounds.getSmallestIntegerContainer());
  101. }
  102. private:
  103. Colour colour;
  104. Rectangle<float> ballBounds;
  105. Point<float> direction;
  106. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BouncingBallComponent)
  107. };
  108. //==============================================================================
  109. class BouncingBallsContainer : public Component
  110. {
  111. public:
  112. BouncingBallsContainer (int numBalls)
  113. {
  114. for (int i = 0; i < numBalls; ++i)
  115. {
  116. BouncingBallComponent* newBall = new BouncingBallComponent();
  117. balls.add (newBall);
  118. addAndMakeVisible (newBall);
  119. }
  120. }
  121. void mouseDown (const MouseEvent& e) override
  122. {
  123. dragger.startDraggingComponent (this, e);
  124. }
  125. void mouseDrag (const MouseEvent& e) override
  126. {
  127. // as there's no titlebar we have to manage the dragging ourselves
  128. dragger.dragComponent (this, e, 0);
  129. }
  130. void paint (Graphics& g) override
  131. {
  132. if (isOpaque())
  133. g.fillAll (Colours::white);
  134. else
  135. g.fillAll (Colours::blue.withAlpha (0.2f));
  136. g.setFont (16.0f);
  137. g.setColour (Colours::black);
  138. g.drawFittedText ("This window has no titlebar and a transparent background.",
  139. getLocalBounds().reduced (8, 0),
  140. Justification::centred, 5);
  141. g.drawRect (getLocalBounds());
  142. }
  143. private:
  144. ComponentDragger dragger;
  145. OwnedArray<BouncingBallComponent> balls;
  146. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BouncingBallsContainer)
  147. };
  148. //==============================================================================
  149. class WindowsDemo : public Component,
  150. private Button::Listener
  151. {
  152. public:
  153. enum Windows
  154. {
  155. dialog,
  156. document,
  157. alert,
  158. numWindows
  159. };
  160. WindowsDemo()
  161. {
  162. setOpaque (true);
  163. showWindowsButton.setButtonText ("Show Windows");
  164. addAndMakeVisible (showWindowsButton);
  165. showWindowsButton.addListener (this);
  166. closeWindowsButton.setButtonText ("Close Windows");
  167. addAndMakeVisible (closeWindowsButton);
  168. closeWindowsButton.addListener (this);
  169. }
  170. ~WindowsDemo()
  171. {
  172. closeAllWindows();
  173. closeWindowsButton.removeListener (this);
  174. showWindowsButton.removeListener (this);
  175. }
  176. void paint (Graphics& g) override
  177. {
  178. g.fillAll (Colours::grey);
  179. }
  180. void resized() override
  181. {
  182. const Rectangle<int> buttonSize (0, 0, 108, 28);
  183. Rectangle<int> area ((getWidth() / 2) - (buttonSize.getWidth() / 2),
  184. (getHeight() / 2) - buttonSize.getHeight(),
  185. buttonSize.getWidth(), buttonSize.getHeight());
  186. showWindowsButton.setBounds (area.reduced (2));
  187. closeWindowsButton.setBounds (area.translated (0, buttonSize.getHeight()).reduced (2));
  188. }
  189. private:
  190. // Because in this demo the windows delete themselves, we'll use the
  191. // Component::SafePointer class to point to them, which automatically becomes
  192. // null when the component that it points to is deleted.
  193. Array< Component::SafePointer<Component> > windows;
  194. TextButton showWindowsButton, closeWindowsButton;
  195. void showAllWindows()
  196. {
  197. closeAllWindows();
  198. showDocumentWindow (false);
  199. showDocumentWindow (true);
  200. showTransparentWindow();
  201. showDialogWindow();
  202. }
  203. void closeAllWindows()
  204. {
  205. for (int i = 0; i < windows.size(); ++i)
  206. windows.getReference(i).deleteAndZero();
  207. windows.clear();
  208. }
  209. void showDialogWindow()
  210. {
  211. String m;
  212. m << "Dialog Windows can be used to quickly show a component, usually blocking mouse input to other windows." << newLine
  213. << newLine
  214. << "They can also be quickly closed with the escape key, try it now.";
  215. DialogWindow::LaunchOptions options;
  216. Label* label = new Label();
  217. label->setText (m, dontSendNotification);
  218. label->setColour (Label::textColourId, Colours::whitesmoke);
  219. options.content.setOwned (label);
  220. Rectangle<int> area (0, 0, 300, 200);
  221. options.content->setSize (area.getWidth(), area.getHeight());
  222. options.dialogTitle = "Dialog Window";
  223. options.dialogBackgroundColour = Colour (0xff0e345a);
  224. options.escapeKeyTriggersCloseButton = true;
  225. options.useNativeTitleBar = false;
  226. options.resizable = true;
  227. DialogWindow* dw = options.launchAsync();
  228. dw->centreWithSize (300, 200);
  229. }
  230. void showDocumentWindow (bool native)
  231. {
  232. DocumentWindow* dw = new ColourSelectorWindow ("Document Window", getRandomBrightColour(), DocumentWindow::allButtons);
  233. windows.add (dw);
  234. Rectangle<int> area (0, 0, 300, 400);
  235. RectanglePlacement placement ((native ? RectanglePlacement::xLeft
  236. : RectanglePlacement::xRight)
  237. | RectanglePlacement::yTop
  238. | RectanglePlacement::doNotResize);
  239. Rectangle<int> result (placement.appliedTo (area, Desktop::getInstance().getDisplays()
  240. .getMainDisplay().userArea.reduced (20)));
  241. dw->setBounds (result);
  242. dw->setResizable (true, ! native);
  243. dw->setUsingNativeTitleBar (native);
  244. dw->setVisible (true);
  245. }
  246. void showTransparentWindow()
  247. {
  248. BouncingBallsContainer* balls = new BouncingBallsContainer (3);
  249. balls->addToDesktop (ComponentPeer::windowIsTemporary);
  250. windows.add (balls);
  251. Rectangle<int> area (0, 0, 200, 200);
  252. RectanglePlacement placement (RectanglePlacement::xLeft
  253. | RectanglePlacement::yBottom
  254. | RectanglePlacement::doNotResize);
  255. Rectangle<int> result (placement.appliedTo (area, Desktop::getInstance().getDisplays()
  256. .getMainDisplay().userArea.reduced (20)));
  257. balls->setBounds (result);
  258. balls->setVisible (true);
  259. }
  260. void buttonClicked (Button* button) override
  261. {
  262. if (button == &showWindowsButton)
  263. showAllWindows();
  264. else if (button == &closeWindowsButton)
  265. closeAllWindows();
  266. }
  267. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowsDemo)
  268. };
  269. // This static object will register this demo type in a global list of demos..
  270. static JuceDemoType<WindowsDemo> demo ("10 Components: Windows");