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.

471 lines
24KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. #ifndef __JUCE_ALERTWINDOW_JUCEHEADER__
  18. #define __JUCE_ALERTWINDOW_JUCEHEADER__
  19. #include "juce_TopLevelWindow.h"
  20. #include "../buttons/juce_TextButton.h"
  21. #include "../widgets/juce_ComboBox.h"
  22. #include "../widgets/juce_TextEditor.h"
  23. #include "../widgets/juce_ProgressBar.h"
  24. #include "../mouse/juce_ComponentDragger.h"
  25. //==============================================================================
  26. /** A window that displays a message and has buttons for the user to react to it.
  27. For simple dialog boxes with just a couple of buttons on them, there are
  28. some static methods for running these.
  29. For more complex dialogs, an AlertWindow can be created, then it can have some
  30. buttons and components added to it, and its runModalLoop() method is then used to
  31. show it. The value returned by runModalLoop() shows which button the
  32. user pressed to dismiss the box.
  33. @see ThreadWithProgressWindow
  34. */
  35. class JUCE_API AlertWindow : public TopLevelWindow,
  36. private ButtonListener // (can't use Button::Listener due to idiotic VC2005 bug)
  37. {
  38. public:
  39. //==============================================================================
  40. /** The type of icon to show in the dialog box. */
  41. enum AlertIconType
  42. {
  43. NoIcon, /**< No icon will be shown on the dialog box. */
  44. QuestionIcon, /**< A question-mark icon, for dialog boxes that need the
  45. user to answer a question. */
  46. WarningIcon, /**< An exclamation mark to indicate that the dialog is a
  47. warning about something and shouldn't be ignored. */
  48. InfoIcon /**< An icon that indicates that the dialog box is just
  49. giving the user some information, which doesn't require
  50. a response from them. */
  51. };
  52. //==============================================================================
  53. /** Creates an AlertWindow.
  54. @param title the headline to show at the top of the dialog box
  55. @param message a longer, more descriptive message to show underneath the
  56. headline
  57. @param iconType the type of icon to display
  58. @param associatedComponent if this is non-null, it specifies the component that the
  59. alert window should be associated with. Depending on the look
  60. and feel, this might be used for positioning of the alert window.
  61. */
  62. AlertWindow (const String& title,
  63. const String& message,
  64. AlertIconType iconType,
  65. Component* associatedComponent = nullptr);
  66. /** Destroys the AlertWindow */
  67. ~AlertWindow();
  68. //==============================================================================
  69. /** Returns the type of alert icon that was specified when the window
  70. was created. */
  71. AlertIconType getAlertType() const noexcept { return alertIconType; }
  72. //==============================================================================
  73. /** Changes the dialog box's message.
  74. This will also resize the window to fit the new message if required.
  75. */
  76. void setMessage (const String& message);
  77. //==============================================================================
  78. /** Adds a button to the window.
  79. @param name the text to show on the button
  80. @param returnValue the value that should be returned from runModalLoop()
  81. if this is the button that the user presses.
  82. @param shortcutKey1 an optional key that can be pressed to trigger this button
  83. @param shortcutKey2 a second optional key that can be pressed to trigger this button
  84. */
  85. void addButton (const String& name,
  86. int returnValue,
  87. const KeyPress& shortcutKey1 = KeyPress(),
  88. const KeyPress& shortcutKey2 = KeyPress());
  89. /** Returns the number of buttons that the window currently has. */
  90. int getNumButtons() const;
  91. /** Invokes a click of one of the buttons. */
  92. void triggerButtonClick (const String& buttonName);
  93. /** If set to true and the window contains no buttons, then pressing the escape key will make
  94. the alert cancel its modal state.
  95. By default this setting is true - turn it off if you don't want the box to respond to
  96. the escape key. Note that it is ignored if you have any buttons, and in that case you
  97. should give the buttons appropriate keypresses to trigger cancelling if you want to.
  98. */
  99. void setEscapeKeyCancels (bool shouldEscapeKeyCancel);
  100. //==============================================================================
  101. /** Adds a textbox to the window for entering strings.
  102. @param name an internal name for the text-box. This is the name to pass to
  103. the getTextEditorContents() method to find out what the
  104. user typed-in.
  105. @param initialContents a string to show in the text box when it's first shown
  106. @param onScreenLabel if this is non-empty, it will be displayed next to the
  107. text-box to label it.
  108. @param isPasswordBox if true, the text editor will display asterisks instead of
  109. the actual text
  110. @see getTextEditorContents
  111. */
  112. void addTextEditor (const String& name,
  113. const String& initialContents,
  114. const String& onScreenLabel = String::empty,
  115. bool isPasswordBox = false);
  116. /** Returns the contents of a named textbox.
  117. After showing an AlertWindow that contains a text editor, this can be
  118. used to find out what the user has typed into it.
  119. @param nameOfTextEditor the name of the text box that you're interested in
  120. @see addTextEditor
  121. */
  122. String getTextEditorContents (const String& nameOfTextEditor) const;
  123. /** Returns a pointer to a textbox that was added with addTextEditor(). */
  124. TextEditor* getTextEditor (const String& nameOfTextEditor) const;
  125. //==============================================================================
  126. /** Adds a drop-down list of choices to the box.
  127. After the box has been shown, the getComboBoxComponent() method can
  128. be used to find out which item the user picked.
  129. @param name the label to use for the drop-down list
  130. @param items the list of items to show in it
  131. @param onScreenLabel if this is non-empty, it will be displayed next to the
  132. combo-box to label it.
  133. @see getComboBoxComponent
  134. */
  135. void addComboBox (const String& name,
  136. const StringArray& items,
  137. const String& onScreenLabel = String::empty);
  138. /** Returns a drop-down list that was added to the AlertWindow.
  139. @param nameOfList the name that was passed into the addComboBox() method
  140. when creating the drop-down
  141. @returns the ComboBox component, or nullptr if none was found for the given name.
  142. */
  143. ComboBox* getComboBoxComponent (const String& nameOfList) const;
  144. //==============================================================================
  145. /** Adds a block of text.
  146. This is handy for adding a multi-line note next to a textbox or combo-box,
  147. to provide more details about what's going on.
  148. */
  149. void addTextBlock (const String& text);
  150. //==============================================================================
  151. /** Adds a progress-bar to the window.
  152. @param progressValue a variable that will be repeatedly checked while the
  153. dialog box is visible, to see how far the process has
  154. got. The value should be in the range 0 to 1.0
  155. */
  156. void addProgressBarComponent (double& progressValue);
  157. //==============================================================================
  158. /** Adds a user-defined component to the dialog box.
  159. @param component the component to add - its size should be set up correctly
  160. before it is passed in. The caller is responsible for deleting
  161. the component later on - the AlertWindow won't delete it.
  162. */
  163. void addCustomComponent (Component* component);
  164. /** Returns the number of custom components in the dialog box.
  165. @see getCustomComponent, addCustomComponent
  166. */
  167. int getNumCustomComponents() const;
  168. /** Returns one of the custom components in the dialog box.
  169. @param index a value 0 to (getNumCustomComponents() - 1). Out-of-range indexes
  170. will return 0
  171. @see getNumCustomComponents, addCustomComponent
  172. */
  173. Component* getCustomComponent (int index) const;
  174. /** Removes one of the custom components in the dialog box.
  175. Note that this won't delete it, it just removes the component from the window
  176. @param index a value 0 to (getNumCustomComponents() - 1). Out-of-range indexes
  177. will return 0
  178. @returns the component that was removed (or null)
  179. @see getNumCustomComponents, addCustomComponent
  180. */
  181. Component* removeCustomComponent (int index);
  182. //==============================================================================
  183. /** Returns true if the window contains any components other than just buttons.*/
  184. bool containsAnyExtraComponents() const;
  185. //==============================================================================
  186. // easy-to-use message box functions:
  187. #if JUCE_MODAL_LOOPS_PERMITTED
  188. /** Shows a dialog box that just has a message and a single button to get rid of it.
  189. The box is shown modally, and the method will block until the user has clicked the
  190. button (or pressed the escape or return keys).
  191. @param iconType the type of icon to show
  192. @param title the headline to show at the top of the box
  193. @param message a longer, more descriptive message to show underneath the
  194. headline
  195. @param buttonText the text to show in the button - if this string is empty, the
  196. default string "OK" (or a localised version) will be used.
  197. @param associatedComponent if this is non-null, it specifies the component that the
  198. alert window should be associated with. Depending on the look
  199. and feel, this might be used for positioning of the alert window.
  200. */
  201. static void JUCE_CALLTYPE showMessageBox (AlertIconType iconType,
  202. const String& title,
  203. const String& message,
  204. const String& buttonText = String::empty,
  205. Component* associatedComponent = nullptr);
  206. #endif
  207. /** Shows a dialog box that just has a message and a single button to get rid of it.
  208. The box will be displayed and placed into a modal state, but this method will
  209. return immediately, and if a callback was supplied, it will be invoked later
  210. when the user dismisses the box.
  211. @param iconType the type of icon to show
  212. @param title the headline to show at the top of the box
  213. @param message a longer, more descriptive message to show underneath the
  214. headline
  215. @param buttonText the text to show in the button - if this string is empty, the
  216. default string "OK" (or a localised version) will be used.
  217. @param associatedComponent if this is non-null, it specifies the component that the
  218. alert window should be associated with. Depending on the look
  219. and feel, this might be used for positioning of the alert window.
  220. @param callback if this is non-null, the callback will receive a call to its
  221. modalStateFinished() when the box is dismissed. The callback object
  222. will be owned and deleted by the system, so make sure that it works
  223. safely and doesn't keep any references to objects that might be deleted
  224. before it gets called.
  225. */
  226. static void JUCE_CALLTYPE showMessageBoxAsync (AlertIconType iconType,
  227. const String& title,
  228. const String& message,
  229. const String& buttonText = String::empty,
  230. Component* associatedComponent = nullptr,
  231. ModalComponentManager::Callback* callback = nullptr);
  232. /** Shows a dialog box with two buttons.
  233. Ideal for ok/cancel or yes/no choices. The return key can also be used
  234. to trigger the first button, and the escape key for the second button.
  235. If the callback parameter is null, the box is shown modally, and the method will
  236. block until the user has clicked the button (or pressed the escape or return keys).
  237. If the callback parameter is non-null, the box will be displayed and placed into a
  238. modal state, but this method will return immediately, and the callback will be invoked
  239. later when the user dismisses the box.
  240. @param iconType the type of icon to show
  241. @param title the headline to show at the top of the box
  242. @param message a longer, more descriptive message to show underneath the
  243. headline
  244. @param button1Text the text to show in the first button - if this string is
  245. empty, the default string "OK" (or a localised version of it)
  246. will be used.
  247. @param button2Text the text to show in the second button - if this string is
  248. empty, the default string "cancel" (or a localised version of it)
  249. will be used.
  250. @param associatedComponent if this is non-null, it specifies the component that the
  251. alert window should be associated with. Depending on the look
  252. and feel, this might be used for positioning of the alert window.
  253. @param callback if this is non-null, the menu will be launched asynchronously,
  254. returning immediately, and the callback will receive a call to its
  255. modalStateFinished() when the box is dismissed, with its parameter
  256. being 1 if the ok button was pressed, or 0 for cancel. The callback object
  257. will be owned and deleted by the system, so make sure that it works
  258. safely and doesn't keep any references to objects that might be deleted
  259. before it gets called.
  260. @returns true if button 1 was clicked, false if it was button 2. If the callback parameter
  261. is not null, the method always returns false, and the user's choice is delivered
  262. later by the callback.
  263. */
  264. static bool JUCE_CALLTYPE showOkCancelBox (AlertIconType iconType,
  265. const String& title,
  266. const String& message,
  267. #if JUCE_MODAL_LOOPS_PERMITTED
  268. const String& button1Text = String::empty,
  269. const String& button2Text = String::empty,
  270. Component* associatedComponent = nullptr,
  271. ModalComponentManager::Callback* callback = nullptr);
  272. #else
  273. const String& button1Text,
  274. const String& button2Text,
  275. Component* associatedComponent,
  276. ModalComponentManager::Callback* callback);
  277. #endif
  278. /** Shows a dialog box with three buttons.
  279. Ideal for yes/no/cancel boxes.
  280. The escape key can be used to trigger the third button.
  281. If the callback parameter is null, the box is shown modally, and the method will
  282. block until the user has clicked the button (or pressed the escape or return keys).
  283. If the callback parameter is non-null, the box will be displayed and placed into a
  284. modal state, but this method will return immediately, and the callback will be invoked
  285. later when the user dismisses the box.
  286. @param iconType the type of icon to show
  287. @param title the headline to show at the top of the box
  288. @param message a longer, more descriptive message to show underneath the
  289. headline
  290. @param button1Text the text to show in the first button - if an empty string, then
  291. "yes" will be used (or a localised version of it)
  292. @param button2Text the text to show in the first button - if an empty string, then
  293. "no" will be used (or a localised version of it)
  294. @param button3Text the text to show in the first button - if an empty string, then
  295. "cancel" will be used (or a localised version of it)
  296. @param associatedComponent if this is non-null, it specifies the component that the
  297. alert window should be associated with. Depending on the look
  298. and feel, this might be used for positioning of the alert window.
  299. @param callback if this is non-null, the menu will be launched asynchronously,
  300. returning immediately, and the callback will receive a call to its
  301. modalStateFinished() when the box is dismissed, with its parameter
  302. being 1 if the "yes" button was pressed, 2 for the "no" button, or 0
  303. if it was cancelled. The callback object will be owned and deleted by the
  304. system, so make sure that it works safely and doesn't keep any references
  305. to objects that might be deleted before it gets called.
  306. @returns If the callback parameter has been set, this returns 0. Otherwise, it
  307. returns one of the following values:
  308. - 0 if the third button was pressed (normally used for 'cancel')
  309. - 1 if the first button was pressed (normally used for 'yes')
  310. - 2 if the middle button was pressed (normally used for 'no')
  311. */
  312. static int JUCE_CALLTYPE showYesNoCancelBox (AlertIconType iconType,
  313. const String& title,
  314. const String& message,
  315. #if JUCE_MODAL_LOOPS_PERMITTED
  316. const String& button1Text = String::empty,
  317. const String& button2Text = String::empty,
  318. const String& button3Text = String::empty,
  319. Component* associatedComponent = nullptr,
  320. ModalComponentManager::Callback* callback = nullptr);
  321. #else
  322. const String& button1Text,
  323. const String& button2Text,
  324. const String& button3Text,
  325. Component* associatedComponent,
  326. ModalComponentManager::Callback* callback);
  327. #endif
  328. //==============================================================================
  329. /** Shows an operating-system native dialog box.
  330. @param title the title to use at the top
  331. @param bodyText the longer message to show
  332. @param isOkCancel if true, this will show an ok/cancel box, if false,
  333. it'll show a box with just an ok button
  334. @returns true if the ok button was pressed, false if they pressed cancel.
  335. */
  336. #if JUCE_MODAL_LOOPS_PERMITTED
  337. static bool JUCE_CALLTYPE showNativeDialogBox (const String& title,
  338. const String& bodyText,
  339. bool isOkCancel);
  340. #endif
  341. //==============================================================================
  342. /** A set of colour IDs to use to change the colour of various aspects of the alert box.
  343. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  344. methods.
  345. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  346. */
  347. enum ColourIds
  348. {
  349. backgroundColourId = 0x1001800, /**< The background colour for the window. */
  350. textColourId = 0x1001810, /**< The colour for the text. */
  351. outlineColourId = 0x1001820 /**< An optional colour to use to draw a border around the window. */
  352. };
  353. protected:
  354. //==============================================================================
  355. /** @internal */
  356. void paint (Graphics&) override;
  357. /** @internal */
  358. void mouseDown (const MouseEvent&) override;
  359. /** @internal */
  360. void mouseDrag (const MouseEvent&) override;
  361. /** @internal */
  362. bool keyPressed (const KeyPress&) override;
  363. /** @internal */
  364. void buttonClicked (Button*) override;
  365. /** @internal */
  366. void lookAndFeelChanged() override;
  367. /** @internal */
  368. void userTriedToCloseWindow() override;
  369. /** @internal */
  370. int getDesktopWindowStyleFlags() const override;
  371. private:
  372. //==============================================================================
  373. String text;
  374. TextLayout textLayout;
  375. AlertIconType alertIconType;
  376. ComponentBoundsConstrainer constrainer;
  377. ComponentDragger dragger;
  378. Rectangle<int> textArea;
  379. OwnedArray<TextButton> buttons;
  380. OwnedArray<TextEditor> textBoxes;
  381. OwnedArray<ComboBox> comboBoxes;
  382. OwnedArray<ProgressBar> progressBars;
  383. Array<Component*> customComps;
  384. OwnedArray<Component> textBlocks;
  385. Array<Component*> allComps;
  386. StringArray textboxNames, comboBoxNames;
  387. Component* associatedComponent;
  388. bool escapeKeyCancels;
  389. void updateLayout (bool onlyIncreaseSize);
  390. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AlertWindow)
  391. };
  392. #endif // __JUCE_ALERTWINDOW_JUCEHEADER__