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

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