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