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.

367 lines
14KB

  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. class DemoBackgroundThread : public ThreadWithProgressWindow
  22. {
  23. public:
  24. DemoBackgroundThread()
  25. : ThreadWithProgressWindow ("busy doing some important things...", true, true)
  26. {
  27. setStatusMessage ("Getting ready...");
  28. }
  29. void run() override
  30. {
  31. setProgress (-1.0); // setting a value beyond the range 0 -> 1 will show a spinning bar..
  32. setStatusMessage ("Preparing to do some stuff...");
  33. wait (2000);
  34. const int thingsToDo = 10;
  35. for (int i = 0; i < thingsToDo; ++i)
  36. {
  37. // must check this as often as possible, because this is
  38. // how we know if the user's pressed 'cancel'
  39. if (threadShouldExit())
  40. return;
  41. // this will update the progress bar on the dialog box
  42. setProgress (i / (double) thingsToDo);
  43. setStatusMessage (String (thingsToDo - i) + " things left to do...");
  44. wait (500);
  45. }
  46. setProgress (-1.0); // setting a value beyond the range 0 -> 1 will show a spinning bar..
  47. setStatusMessage ("Finishing off the last few bits and pieces!");
  48. wait (2000);
  49. }
  50. // This method gets called on the message thread once our thread has finished..
  51. void threadComplete (bool userPressedCancel) override
  52. {
  53. if (userPressedCancel)
  54. {
  55. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  56. "Progress window",
  57. "You pressed cancel!");
  58. }
  59. else
  60. {
  61. // thread finished normally..
  62. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  63. "Progress window",
  64. "Thread finished ok!");
  65. }
  66. // ..and clean up by deleting our thread object..
  67. delete this;
  68. }
  69. };
  70. //==============================================================================
  71. class DialogsDemo : public Component,
  72. private Button::Listener
  73. {
  74. public:
  75. enum DialogType
  76. {
  77. plainAlertWindow,
  78. warningAlertWindow,
  79. infoAlertWindow,
  80. questionAlertWindow,
  81. okCancelAlertWindow,
  82. extraComponentsAlertWindow,
  83. calloutBoxWindow,
  84. progressWindow,
  85. loadChooser,
  86. loadWithPreviewChooser,
  87. directoryChooser,
  88. saveChooser,
  89. numDialogs
  90. };
  91. DialogsDemo()
  92. {
  93. setOpaque (true);
  94. addAndMakeVisible (nativeButton);
  95. nativeButton.setButtonText ("Use Native Windows");
  96. nativeButton.addListener (this);
  97. static const char* windowNames[] =
  98. {
  99. "Plain Alert Window",
  100. "Alert Window With Warning Icon",
  101. "Alert Window With Info Icon",
  102. "Alert Window With Question Icon",
  103. "OK Cancel Alert Window",
  104. "Alert Window With Extra Components",
  105. "CalloutBox",
  106. "Thread With Progress Window",
  107. "'Load' File Browser",
  108. "'Load' File Browser With Image Preview",
  109. "'Choose Directory' File Browser",
  110. "'Save' File Browser"
  111. };
  112. // warn in case we add any windows
  113. jassert (numElementsInArray (windowNames) == numDialogs);
  114. for (int i = 0; i < numDialogs; ++i)
  115. {
  116. TextButton* newButton = new TextButton();
  117. windowButtons.add (newButton);
  118. addAndMakeVisible (newButton);
  119. newButton->setButtonText (windowNames[i]);
  120. newButton->addListener (this);
  121. }
  122. }
  123. ~DialogsDemo()
  124. {
  125. nativeButton.removeListener (this);
  126. for (int i = windowButtons.size(); --i >= 0;)
  127. if (TextButton* button = windowButtons.getUnchecked (i))
  128. button->removeListener (this);
  129. }
  130. //==============================================================================
  131. void paint (Graphics& g) override
  132. {
  133. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  134. }
  135. void resized() override
  136. {
  137. Rectangle<int> area (getLocalBounds().reduced (5, 15));
  138. Rectangle<int> topRow;
  139. for (int i = 0; i < windowButtons.size(); ++i)
  140. {
  141. if (topRow.getWidth() < 10 || i == loadChooser)
  142. topRow = area.removeFromTop (26);
  143. if (i == progressWindow)
  144. area.removeFromTop (20);
  145. windowButtons.getUnchecked (i)
  146. ->setBounds (topRow.removeFromLeft (area.getWidth() / 2).reduced (4, 2));
  147. }
  148. area.removeFromTop (15);
  149. nativeButton.setBounds (area.removeFromTop (24));
  150. }
  151. private:
  152. OwnedArray<TextButton> windowButtons;
  153. ToggleButton nativeButton;
  154. static void alertBoxResultChosen (int result, DialogsDemo*)
  155. {
  156. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  157. "Alert Box",
  158. "Result code: " + String (result));
  159. }
  160. void showWindow (Component& button, DialogType type)
  161. {
  162. if (type >= plainAlertWindow && type <= questionAlertWindow)
  163. {
  164. AlertWindow::AlertIconType icon = AlertWindow::NoIcon;
  165. switch (type)
  166. {
  167. case warningAlertWindow: icon = AlertWindow::WarningIcon; break;
  168. case infoAlertWindow: icon = AlertWindow::InfoIcon; break;
  169. case questionAlertWindow: icon = AlertWindow::QuestionIcon; break;
  170. default: break;
  171. }
  172. AlertWindow::showMessageBoxAsync (icon,
  173. "This is an AlertWindow",
  174. "And this is the AlertWindow's message. Blah blah blah blah blah blah blah blah blah blah blah blah blah.",
  175. "OK");
  176. }
  177. else if (type == okCancelAlertWindow)
  178. {
  179. AlertWindow::showOkCancelBox (AlertWindow::QuestionIcon,
  180. "This is an ok/cancel AlertWindow",
  181. "And this is the AlertWindow's message. Blah blah blah blah blah blah blah blah blah blah blah blah blah.",
  182. String(),
  183. String(),
  184. 0,
  185. ModalCallbackFunction::forComponent (alertBoxResultChosen, this));
  186. }
  187. else if (type == calloutBoxWindow)
  188. {
  189. ColourSelector* colourSelector = new ColourSelector();
  190. colourSelector->setName ("background");
  191. colourSelector->setCurrentColour (findColour (TextButton::buttonColourId));
  192. colourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  193. colourSelector->setSize (300, 400);
  194. CallOutBox::launchAsynchronously (colourSelector, button.getScreenBounds(), nullptr);
  195. }
  196. else if (type == extraComponentsAlertWindow)
  197. {
  198. #if JUCE_MODAL_LOOPS_PERMITTED
  199. AlertWindow w ("AlertWindow demo..",
  200. "This AlertWindow has a couple of extra components added to show how to add drop-down lists and text entry boxes.",
  201. AlertWindow::QuestionIcon);
  202. w.addTextEditor ("text", "enter some text here", "text field:");
  203. const char* options[] = { "option 1", "option 2", "option 3", "option 4", nullptr };
  204. w.addComboBox ("option", StringArray (options), "some options");
  205. w.addButton ("OK", 1, KeyPress (KeyPress::returnKey, 0, 0));
  206. w.addButton ("Cancel", 0, KeyPress (KeyPress::escapeKey, 0, 0));
  207. if (w.runModalLoop() != 0) // is they picked 'ok'
  208. {
  209. // this is the item they chose in the drop-down list..
  210. const int optionIndexChosen = w.getComboBoxComponent ("option")->getSelectedItemIndex();
  211. ignoreUnused (optionIndexChosen);
  212. // this is the text they entered..
  213. String text = w.getTextEditorContents ("text");
  214. }
  215. #endif
  216. }
  217. else if (type == progressWindow)
  218. {
  219. // This will launch our ThreadWithProgressWindow in a modal state. (Our subclass
  220. // will take care of deleting the object when the task has finished)
  221. (new DemoBackgroundThread())->launchThread();
  222. }
  223. else if (type >= loadChooser && type <= saveChooser)
  224. {
  225. #if JUCE_MODAL_LOOPS_PERMITTED
  226. const bool useNativeVersion = nativeButton.getToggleState();
  227. if (type == loadChooser)
  228. {
  229. FileChooser fc ("Choose a file to open...",
  230. File::getCurrentWorkingDirectory(),
  231. "*",
  232. useNativeVersion);
  233. if (fc.browseForMultipleFilesToOpen())
  234. {
  235. String chosen;
  236. for (int i = 0; i < fc.getResults().size(); ++i)
  237. chosen << fc.getResults().getReference(i).getFullPathName() << "\n";
  238. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  239. "File Chooser...",
  240. "You picked: " + chosen);
  241. }
  242. }
  243. else if (type == loadWithPreviewChooser)
  244. {
  245. ImagePreviewComponent imagePreview;
  246. imagePreview.setSize (200, 200);
  247. FileChooser fc ("Choose an image to open...",
  248. File::getSpecialLocation (File::userPicturesDirectory),
  249. "*.jpg;*.jpeg;*.png;*.gif",
  250. useNativeVersion);
  251. if (fc.browseForMultipleFilesToOpen (&imagePreview))
  252. {
  253. String chosen;
  254. for (int i = 0; i < fc.getResults().size(); ++i)
  255. chosen << fc.getResults().getReference (i).getFullPathName() << "\n";
  256. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  257. "File Chooser...",
  258. "You picked: " + chosen);
  259. }
  260. }
  261. else if (type == saveChooser)
  262. {
  263. FileChooser fc ("Choose a file to save...",
  264. File::getCurrentWorkingDirectory(),
  265. "*",
  266. useNativeVersion);
  267. if (fc.browseForFileToSave (true))
  268. {
  269. File chosenFile = fc.getResult();
  270. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  271. "File Chooser...",
  272. "You picked: " + chosenFile.getFullPathName());
  273. }
  274. }
  275. else if (type == directoryChooser)
  276. {
  277. FileChooser fc ("Choose a directory...",
  278. File::getCurrentWorkingDirectory(),
  279. "*",
  280. useNativeVersion);
  281. if (fc.browseForDirectory())
  282. {
  283. File chosenDirectory = fc.getResult();
  284. AlertWindow::showMessageBoxAsync (AlertWindow::InfoIcon,
  285. "File Chooser...",
  286. "You picked: " + chosenDirectory.getFullPathName());
  287. }
  288. }
  289. #endif
  290. }
  291. }
  292. void buttonClicked (Button* button) override
  293. {
  294. if (button == &nativeButton)
  295. {
  296. getLookAndFeel().setUsingNativeAlertWindows (nativeButton.getToggleState());
  297. return;
  298. }
  299. for (int i = windowButtons.size(); --i >= 0;)
  300. if (button == windowButtons.getUnchecked (i))
  301. return showWindow (*button, static_cast<DialogType> (i));
  302. }
  303. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DialogsDemo)
  304. };
  305. // This static object will register this demo type in a global list of demos..
  306. static JuceDemoType<DialogsDemo> demo ("10 Components: Dialog Boxes");