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.

345 lines
11KB

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