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 - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 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. #ifndef __JUCE_ALERTWINDOW_JUCEHEADER__
  19. #define __JUCE_ALERTWINDOW_JUCEHEADER__
  20. #include "juce_TopLevelWindow.h"
  21. #include "../buttons/juce_TextButton.h"
  22. #include "../widgets/juce_ComboBox.h"
  23. #include "../widgets/juce_TextEditor.h"
  24. #include "../widgets/juce_ProgressBar.h"
  25. #include "../mouse/juce_ComponentDragger.h"
  26. //==============================================================================
  27. /** A window that displays a message and has buttons for the user to react to it.
  28. For simple dialog boxes with just a couple of buttons on them, there are
  29. some static methods for running these.
  30. For more complex dialogs, an AlertWindow can be created, then it can have some
  31. buttons and components added to it, and its runModalLoop() method is then used to
  32. show it. The value returned by runModalLoop() shows which button the
  33. user pressed to dismiss the box.
  34. @see ThreadWithProgressWindow
  35. */
  36. class JUCE_API AlertWindow : public TopLevelWindow,
  37. private ButtonListener // (can't use Button::Listener due to idiotic VC2005 bug)
  38. {
  39. public:
  40. //==============================================================================
  41. /** The type of icon to show in the dialog box. */
  42. enum AlertIconType
  43. {
  44. NoIcon, /**< No icon will be shown on the dialog box. */
  45. QuestionIcon, /**< A question-mark icon, for dialog boxes that need the
  46. user to answer a question. */
  47. WarningIcon, /**< An exclamation mark to indicate that the dialog is a
  48. warning about something and shouldn't be ignored. */
  49. InfoIcon /**< An icon that indicates that the dialog box is just
  50. giving the user some information, which doesn't require
  51. a response from them. */
  52. };
  53. //==============================================================================
  54. /** Creates an AlertWindow.
  55. @param title the headline to show at the top of the dialog box
  56. @param message a longer, more descriptive message to show underneath the
  57. headline
  58. @param iconType the type of icon to display
  59. @param associatedComponent if this is non-null, it specifies the component that the
  60. alert window should be associated with. Depending on the look
  61. and feel, this might be used for positioning of the alert window.
  62. */
  63. AlertWindow (const String& title,
  64. const String& message,
  65. AlertIconType iconType,
  66. Component* associatedComponent = nullptr);
  67. /** Destroys the AlertWindow */
  68. ~AlertWindow();
  69. //==============================================================================
  70. /** Returns the type of alert icon that was specified when the window
  71. was created. */
  72. AlertIconType getAlertType() const noexcept { return alertIconType; }
  73. //==============================================================================
  74. /** Changes the dialog box's message.
  75. This will also resize the window to fit the new message if required.
  76. */
  77. void setMessage (const String& message);
  78. //==============================================================================
  79. /** Adds a button to the window.
  80. @param name the text to show on the button
  81. @param returnValue the value that should be returned from runModalLoop()
  82. if this is the button that the user presses.
  83. @param shortcutKey1 an optional key that can be pressed to trigger this button
  84. @param shortcutKey2 a second optional key that can be pressed to trigger this button
  85. */
  86. void addButton (const String& name,
  87. int returnValue,
  88. const KeyPress& shortcutKey1 = KeyPress(),
  89. const KeyPress& shortcutKey2 = KeyPress());
  90. /** Returns the number of buttons that the window currently has. */
  91. int getNumButtons() const;
  92. /** Invokes a click of one of the buttons. */
  93. void triggerButtonClick (const String& buttonName);
  94. /** If set to true and the window contains no buttons, then pressing the escape key will make
  95. the alert cancel its modal state.
  96. By default this setting is true - turn it off if you don't want the box to respond to
  97. the escape key. Note that it is ignored if you have any buttons, and in that case you
  98. should give the buttons appropriate keypresses to trigger cancelling if you want to.
  99. */
  100. void setEscapeKeyCancels (bool shouldEscapeKeyCancel);
  101. //==============================================================================
  102. /** Adds a textbox to the window for entering strings.
  103. @param name an internal name for the text-box. This is the name to pass to
  104. the getTextEditorContents() method to find out what the
  105. user typed-in.
  106. @param initialContents a string to show in the text box when it's first shown
  107. @param onScreenLabel if this is non-empty, it will be displayed next to the
  108. text-box to label it.
  109. @param isPasswordBox if true, the text editor will display asterisks instead of
  110. the actual text
  111. @see getTextEditorContents
  112. */
  113. void addTextEditor (const String& name,
  114. const String& initialContents,
  115. const String& onScreenLabel = String::empty,
  116. bool isPasswordBox = false);
  117. /** Returns the contents of a named textbox.
  118. After showing an AlertWindow that contains a text editor, this can be
  119. used to find out what the user has typed into it.
  120. @param nameOfTextEditor the name of the text box that you're interested in
  121. @see addTextEditor
  122. */
  123. String getTextEditorContents (const String& nameOfTextEditor) const;
  124. /** Returns a pointer to a textbox that was added with addTextEditor(). */
  125. TextEditor* getTextEditor (const String& nameOfTextEditor) const;
  126. //==============================================================================
  127. /** Adds a drop-down list of choices to the box.
  128. After the box has been shown, the getComboBoxComponent() method can
  129. be used to find out which item the user picked.
  130. @param name the label to use for the drop-down list
  131. @param items the list of items to show in it
  132. @param onScreenLabel if this is non-empty, it will be displayed next to the
  133. combo-box to label it.
  134. @see getComboBoxComponent
  135. */
  136. void addComboBox (const String& name,
  137. const StringArray& items,
  138. const String& onScreenLabel = String::empty);
  139. /** Returns a drop-down list that was added to the AlertWindow.
  140. @param nameOfList the name that was passed into the addComboBox() method
  141. when creating the drop-down
  142. @returns the ComboBox component, or 0 if none was found for the given name.
  143. */
  144. ComboBox* getComboBoxComponent (const String& nameOfList) const;
  145. //==============================================================================
  146. /** Adds a block of text.
  147. This is handy for adding a multi-line note next to a textbox or combo-box,
  148. to provide more details about what's going on.
  149. */
  150. void addTextBlock (const String& text);
  151. //==============================================================================
  152. /** Adds a progress-bar to the window.
  153. @param progressValue a variable that will be repeatedly checked while the
  154. dialog box is visible, to see how far the process has
  155. got. The value should be in the range 0 to 1.0
  156. */
  157. void addProgressBarComponent (double& progressValue);
  158. //==============================================================================
  159. /** Adds a user-defined component to the dialog box.
  160. @param component the component to add - its size should be set up correctly
  161. before it is passed in. The caller is responsible for deleting
  162. the component later on - the AlertWindow won't delete it.
  163. */
  164. void addCustomComponent (Component* component);
  165. /** Returns the number of custom components in the dialog box.
  166. @see getCustomComponent, addCustomComponent
  167. */
  168. int getNumCustomComponents() const;
  169. /** Returns one of the custom components in the dialog box.
  170. @param index a value 0 to (getNumCustomComponents() - 1). Out-of-range indexes
  171. will return 0
  172. @see getNumCustomComponents, addCustomComponent
  173. */
  174. Component* getCustomComponent (int index) const;
  175. /** Removes one of the custom components in the dialog box.
  176. Note that this won't delete it, it just removes the component from the window
  177. @param index a value 0 to (getNumCustomComponents() - 1). Out-of-range indexes
  178. will return 0
  179. @returns the component that was removed (or null)
  180. @see getNumCustomComponents, addCustomComponent
  181. */
  182. Component* removeCustomComponent (int index);
  183. //==============================================================================
  184. /** Returns true if the window contains any components other than just buttons.*/
  185. bool containsAnyExtraComponents() const;
  186. //==============================================================================
  187. // easy-to-use message box functions:
  188. /** Shows a dialog box that just has a message and a single button to get rid of it.
  189. If the callback parameter is null, the box is shown modally, and the method will
  190. block until the user has clicked the button (or pressed the escape or return keys).
  191. If the callback parameter is non-null, the box will be displayed and placed into a
  192. modal state, but this method will return immediately, and the callback will be invoked
  193. later when the user dismisses the box.
  194. @param iconType the type of icon to show
  195. @param title the headline to show at the top of the box
  196. @param message a longer, more descriptive message to show underneath the
  197. headline
  198. @param buttonText the text to show in the button - if this string is empty, the
  199. default string "ok" (or a localised version) will be used.
  200. @param associatedComponent if this is non-null, it specifies the component that the
  201. alert window should be associated with. Depending on the look
  202. and feel, this might be used for positioning of the alert window.
  203. */
  204. #if JUCE_MODAL_LOOPS_PERMITTED
  205. static void JUCE_CALLTYPE showMessageBox (AlertIconType iconType,
  206. const String& title,
  207. const String& message,
  208. const String& buttonText = String::empty,
  209. Component* associatedComponent = nullptr);
  210. #endif
  211. /** Shows a dialog box that just has a message and a single button to get rid of it.
  212. If the callback parameter is null, the box is shown modally, and the method will
  213. block until the user has clicked the button (or pressed the escape or return keys).
  214. If the callback parameter is non-null, the box will be displayed and placed into a
  215. modal state, but this method will return immediately, and the callback will be invoked
  216. later when the user dismisses the box.
  217. @param iconType the type of icon to show
  218. @param title the headline to show at the top of the box
  219. @param message a longer, more descriptive message to show underneath the
  220. headline
  221. @param buttonText the text to show in the button - if this string is empty, the
  222. default string "ok" (or a localised version) will be used.
  223. @param associatedComponent if this is non-null, it specifies the component that the
  224. alert window should be associated with. Depending on the look
  225. and feel, this might be used for positioning of the alert window.
  226. */
  227. static void JUCE_CALLTYPE showMessageBoxAsync (AlertIconType iconType,
  228. const String& title,
  229. const String& message,
  230. const String& buttonText = String::empty,
  231. Component* associatedComponent = 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& g);
  357. /** @internal */
  358. void mouseDown (const MouseEvent& e);
  359. /** @internal */
  360. void mouseDrag (const MouseEvent& e);
  361. /** @internal */
  362. bool keyPressed (const KeyPress& key);
  363. /** @internal */
  364. void buttonClicked (Button* button);
  365. /** @internal */
  366. void lookAndFeelChanged();
  367. /** @internal */
  368. void userTriedToCloseWindow();
  369. /** @internal */
  370. int getDesktopWindowStyleFlags() const;
  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__