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.

490 lines
16KB

  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: 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, vs2022, 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 final : 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 final : 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 final : 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 final : 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 final : 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. addAndMakeVisible (alertWindowResult);
  182. alertWindowResult.setJustificationType (Justification::centred);
  183. setSize (250, 250);
  184. }
  185. ~WindowsDemo() override
  186. {
  187. if (dialogWindow != nullptr)
  188. {
  189. dialogWindow->exitModalState (0);
  190. // we are shutting down: can't wait for the message manager
  191. // to eventually delete this
  192. delete dialogWindow;
  193. }
  194. closeAllWindows();
  195. }
  196. void paint (Graphics& g) override
  197. {
  198. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  199. Colours::grey));
  200. }
  201. void resized() override
  202. {
  203. FlexBox flexBox;
  204. flexBox.flexDirection = FlexBox::Direction::column;
  205. flexBox.justifyContent = FlexBox::JustifyContent::center;
  206. constexpr auto buttonWidth = 108.0f;
  207. constexpr auto componentHeight = 24.0f;
  208. constexpr auto gap = 4.0f;
  209. flexBox.items.add (FlexItem { showWindowsButton }.withHeight (componentHeight)
  210. .withMinWidth (buttonWidth)
  211. .withAlignSelf (FlexItem::AlignSelf::center));
  212. flexBox.items.add (FlexItem{}.withHeight (gap));
  213. flexBox.items.add (FlexItem { closeWindowsButton }.withHeight (componentHeight)
  214. .withMinWidth (buttonWidth)
  215. .withAlignSelf (FlexItem::AlignSelf::center));
  216. flexBox.items.add (FlexItem{}.withHeight (gap));
  217. flexBox.items.add (FlexItem { alertWindowResult }.withHeight (componentHeight));
  218. flexBox.performLayout (getLocalBounds());
  219. }
  220. private:
  221. // Because in this demo the windows delete themselves, we'll use the
  222. // Component::SafePointer class to point to them, which automatically becomes
  223. // null when the component that it points to is deleted.
  224. Array<Component::SafePointer<Component>> windows;
  225. SafePointer<DialogWindow> dialogWindow;
  226. TextButton showWindowsButton { "Show Windows" },
  227. closeWindowsButton { "Close Windows" };
  228. Label alertWindowResult { "Alert Window result" };
  229. void showAllWindows()
  230. {
  231. closeAllWindows();
  232. showDocumentWindow (false);
  233. showDocumentWindow (true);
  234. showTransparentWindow();
  235. showAlertWindow();
  236. showDialogWindow();
  237. }
  238. void closeAllWindows()
  239. {
  240. for (auto& window : windows)
  241. window.deleteAndZero();
  242. windows.clear();
  243. alertWindowResult.setText ("", dontSendNotification);
  244. }
  245. static auto getDisplayArea()
  246. {
  247. return Desktop::getInstance().getDisplays().getPrimaryDisplay()->userArea.reduced (20);
  248. }
  249. void showDialogWindow()
  250. {
  251. String m;
  252. m << "Dialog Windows can be used to quickly show a component, usually blocking mouse input to other windows." << newLine
  253. << newLine
  254. << "They can also be quickly closed with the escape key, try it now.";
  255. DialogWindow::LaunchOptions options;
  256. auto* label = new Label();
  257. label->setText (m, dontSendNotification);
  258. label->setColour (Label::textColourId, Colours::whitesmoke);
  259. options.content.setOwned (label);
  260. Rectangle<int> area (0, 0, 300, 200);
  261. options.content->setSize (area.getWidth(), area.getHeight());
  262. options.dialogTitle = "Dialog Window";
  263. options.dialogBackgroundColour = Colour (0xff0e345a);
  264. options.escapeKeyTriggersCloseButton = true;
  265. options.useNativeTitleBar = false;
  266. options.resizable = true;
  267. dialogWindow = options.launchAsync();
  268. if (dialogWindow != nullptr)
  269. dialogWindow->centreWithSize (300, 200);
  270. }
  271. void showDocumentWindow (bool native)
  272. {
  273. auto* dw = new ColourSelectorWindow ("Document Window", getRandomBrightColour(), DocumentWindow::allButtons);
  274. windows.add (dw);
  275. Rectangle<int> area (0, 0, 300, 400);
  276. RectanglePlacement placement ((native ? RectanglePlacement::xLeft
  277. : RectanglePlacement::xRight)
  278. | RectanglePlacement::yTop
  279. | RectanglePlacement::doNotResize);
  280. auto result = placement.appliedTo (area, getDisplayArea());
  281. dw->setBounds (result);
  282. dw->setResizable (true, ! native);
  283. dw->setUsingNativeTitleBar (native);
  284. dw->setVisible (true);
  285. }
  286. void showTransparentWindow()
  287. {
  288. auto* balls = new BouncingBallsContainer (3);
  289. balls->addToDesktop (ComponentPeer::windowIsTemporary);
  290. windows.add (balls);
  291. Rectangle<int> area (0, 0, 200, 200);
  292. RectanglePlacement placement (RectanglePlacement::xLeft
  293. | RectanglePlacement::yBottom
  294. | RectanglePlacement::doNotResize);
  295. auto result = placement.appliedTo (area, getDisplayArea());
  296. balls->setBounds (result);
  297. balls->setVisible (true);
  298. }
  299. void showAlertWindow()
  300. {
  301. auto* alertWindow = new AlertWindow ("Alert Window",
  302. "For more complex dialogs, you can easily add components to an AlertWindow, such as...",
  303. MessageBoxIconType::InfoIcon);
  304. windows.add (alertWindow);
  305. alertWindow->addTextBlock ("Text block");
  306. alertWindow->addComboBox ("Combo box", {"Combo box", "Item 2", "Item 3"});
  307. alertWindow->addTextEditor ("Text editor", "Text editor");
  308. alertWindow->addTextEditor ("Password", "password", "including for passwords", true);
  309. alertWindowCustomComponent.emplace();
  310. alertWindow->addCustomComponent (&(*alertWindowCustomComponent));
  311. alertWindow->addTextBlock ("Progress bar");
  312. alertWindow->addProgressBarComponent (alertWindowCustomComponent->value, ProgressBar::Style::linear);
  313. alertWindow->addProgressBarComponent (alertWindowCustomComponent->value, ProgressBar::Style::circular);
  314. alertWindow->addTextBlock ("Press any button, or the escape key, to close the window");
  315. enum AlertWindowResult
  316. {
  317. noButtonPressed,
  318. button1Pressed,
  319. button2Pressed
  320. };
  321. alertWindow->addButton ("Button 1", AlertWindowResult::button1Pressed);
  322. alertWindow->addButton ("Button 2", AlertWindowResult::button2Pressed);
  323. RectanglePlacement placement { RectanglePlacement::yMid
  324. | RectanglePlacement::xLeft
  325. | RectanglePlacement::doNotResize };
  326. alertWindow->setBounds (placement.appliedTo (alertWindow->getBounds(), getDisplayArea()));
  327. alertWindowResult.setText ("", dontSendNotification);
  328. alertWindow->enterModalState (false, ModalCallbackFunction::create ([ref = SafePointer { this }] (int result)
  329. {
  330. if (ref == nullptr)
  331. return;
  332. const auto text = [&]
  333. {
  334. switch (result)
  335. {
  336. case noButtonPressed:
  337. return "Dismissed the Alert Window without pressing a button";
  338. case button1Pressed:
  339. return "Dismissed the Alert Window using Button 1";
  340. case button2Pressed:
  341. return "Dismissed the Alert Window using Button 2";
  342. }
  343. return "Unhandled event when dismissing the Alert Window";
  344. }();
  345. ref->alertWindowResult.setText (text, dontSendNotification);
  346. }), true);
  347. }
  348. class AlertWindowCustomComponent final : public Component,
  349. private Slider::Listener
  350. {
  351. public:
  352. AlertWindowCustomComponent()
  353. {
  354. slider.setRange (0.0, 1.0);
  355. slider.setValue (0.5, NotificationType::dontSendNotification);
  356. slider.addListener (this);
  357. addAndMakeVisible (label);
  358. addAndMakeVisible (slider);
  359. setSize (200, 50);
  360. }
  361. ~AlertWindowCustomComponent() override
  362. {
  363. slider.removeListener (this);
  364. }
  365. void resized() override
  366. {
  367. auto bounds = getLocalBounds();
  368. label.setBounds (bounds.removeFromTop (getHeight() / 2));
  369. slider.setBounds (bounds);
  370. }
  371. void sliderValueChanged (Slider*) override
  372. {
  373. value = slider.getValue();
  374. }
  375. double value { -1.0 };
  376. private:
  377. Label label { "Label", "Custom component" };
  378. Slider slider { Slider::SliderStyle::LinearHorizontal,
  379. Slider::TextEntryBoxPosition::NoTextBox };
  380. };
  381. std::optional<AlertWindowCustomComponent> alertWindowCustomComponent;
  382. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowsDemo)
  383. };