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