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.

483 lines
25KB

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