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.

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