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.

365 lines
14KB

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