Audio plugin host https://kx.studio/carla
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.

juce_AlertWindow.h 25KB

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