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.

583 lines
30KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. /** Creates an AlertWindow.
  36. @param title the headline to show at the top of the dialog box
  37. @param message a longer, more descriptive message to show underneath the
  38. headline
  39. @param iconType the type of icon to display
  40. @param associatedComponent if this is non-null, it specifies the component that the
  41. alert window should be associated with. Depending on the look
  42. and feel, this might be used for positioning of the alert window.
  43. */
  44. AlertWindow (const String& title,
  45. const String& message,
  46. MessageBoxIconType iconType,
  47. Component* associatedComponent = nullptr);
  48. /** Destroys the AlertWindow */
  49. ~AlertWindow() override;
  50. //==============================================================================
  51. /** Returns the type of alert icon that was specified when the window
  52. was created. */
  53. MessageBoxIconType getAlertType() const noexcept { return alertIconType; }
  54. //==============================================================================
  55. /** Changes the dialog box's message.
  56. This will also resize the window to fit the new message if required.
  57. */
  58. void setMessage (const String& message);
  59. //==============================================================================
  60. /** Adds a button to the window.
  61. @param name the text to show on the button
  62. @param returnValue the value that should be returned from runModalLoop()
  63. if this is the button that the user presses.
  64. @param shortcutKey1 an optional key that can be pressed to trigger this button
  65. @param shortcutKey2 a second optional key that can be pressed to trigger this button
  66. */
  67. void addButton (const String& name,
  68. int returnValue,
  69. const KeyPress& shortcutKey1 = KeyPress(),
  70. const KeyPress& shortcutKey2 = KeyPress());
  71. /** Returns the number of buttons that the window currently has. */
  72. int getNumButtons() const;
  73. /** Returns a Button that was added to the AlertWindow.
  74. @param index the index of the button in order that it was added with the addButton() method.
  75. @returns the Button component, or nullptr if the index is out of bounds.
  76. @see getNumButtons
  77. */
  78. Button* getButton (int index) const;
  79. /** Returns a Button that was added to the AlertWindow.
  80. @param buttonName the name that was passed into the addButton() method
  81. @returns the Button component, or nullptr if none was found for the given name.
  82. */
  83. Button* getButton (const String& buttonName) const;
  84. /** Invokes a click of one of the buttons. */
  85. void triggerButtonClick (const String& buttonName);
  86. /** If set to true and the window contains no buttons, then pressing the escape key will make
  87. the alert cancel its modal state.
  88. By default this setting is true - turn it off if you don't want the box to respond to
  89. the escape key. Note that it is ignored if you have any buttons, and in that case you
  90. should give the buttons appropriate keypresses to trigger cancelling if you want to.
  91. */
  92. void setEscapeKeyCancels (bool shouldEscapeKeyCancel);
  93. //==============================================================================
  94. /** Adds a textbox to the window for entering strings.
  95. @param name an internal name for the text-box. This is the name to pass to
  96. the getTextEditorContents() method to find out what the
  97. user typed-in.
  98. @param initialContents a string to show in the text box when it's first shown
  99. @param onScreenLabel if this is non-empty, it will be displayed next to the
  100. text-box to label it.
  101. @param isPasswordBox if true, the text editor will display asterisks instead of
  102. the actual text
  103. @see getTextEditorContents
  104. */
  105. void addTextEditor (const String& name,
  106. const String& initialContents,
  107. const String& onScreenLabel = String(),
  108. bool isPasswordBox = false);
  109. /** Returns the contents of a named textbox.
  110. After showing an AlertWindow that contains a text editor, this can be
  111. used to find out what the user has typed into it.
  112. @param nameOfTextEditor the name of the text box that you're interested in
  113. @see addTextEditor
  114. */
  115. String getTextEditorContents (const String& nameOfTextEditor) const;
  116. /** Returns a pointer to a textbox that was added with addTextEditor(). */
  117. TextEditor* getTextEditor (const String& nameOfTextEditor) const;
  118. //==============================================================================
  119. /** Adds a drop-down list of choices to the box.
  120. After the box has been shown, the getComboBoxComponent() method can
  121. be used to find out which item the user picked.
  122. @param name the label to use for the drop-down list
  123. @param items the list of items to show in it
  124. @param onScreenLabel if this is non-empty, it will be displayed next to the
  125. combo-box to label it.
  126. @see getComboBoxComponent
  127. */
  128. void addComboBox (const String& name,
  129. const StringArray& items,
  130. const String& onScreenLabel = String());
  131. /** Returns a drop-down list that was added to the AlertWindow.
  132. @param nameOfList the name that was passed into the addComboBox() method
  133. when creating the drop-down
  134. @returns the ComboBox component, or nullptr if none was found for the given name.
  135. */
  136. ComboBox* getComboBoxComponent (const String& nameOfList) const;
  137. //==============================================================================
  138. /** Adds a block of text.
  139. This is handy for adding a multi-line note next to a textbox or combo-box,
  140. to provide more details about what's going on.
  141. */
  142. void addTextBlock (const String& text);
  143. //==============================================================================
  144. /** Adds a progress-bar to the window.
  145. @param progressValue a variable that will be repeatedly checked while the
  146. dialog box is visible, to see how far the process has
  147. got. The value should be in the range 0 to 1.0
  148. */
  149. void addProgressBarComponent (double& progressValue);
  150. //==============================================================================
  151. /** Adds a user-defined component to the dialog box.
  152. @param component the component to add - its size should be set up correctly
  153. before it is passed in. The caller is responsible for deleting
  154. the component later on - the AlertWindow won't delete it.
  155. */
  156. void addCustomComponent (Component* component);
  157. /** Returns the number of custom components in the dialog box.
  158. @see getCustomComponent, addCustomComponent
  159. */
  160. int getNumCustomComponents() const;
  161. /** Returns one of the custom components in the dialog box.
  162. @param index a value 0 to (getNumCustomComponents() - 1).
  163. Out-of-range indexes will return nullptr
  164. @see getNumCustomComponents, addCustomComponent
  165. */
  166. Component* getCustomComponent (int index) const;
  167. /** Removes one of the custom components in the dialog box.
  168. Note that this won't delete it, it just removes the component from the window
  169. @param index a value 0 to (getNumCustomComponents() - 1).
  170. Out-of-range indexes will return nullptr
  171. @returns the component that was removed (or null)
  172. @see getNumCustomComponents, addCustomComponent
  173. */
  174. Component* removeCustomComponent (int index);
  175. //==============================================================================
  176. /** Returns true if the window contains any components other than just buttons.*/
  177. bool containsAnyExtraComponents() const;
  178. //==============================================================================
  179. #if JUCE_MODAL_LOOPS_PERMITTED
  180. /** Shows a dialog box that just has a message and a single button to get rid of it.
  181. The box is shown modally, and the method will block until the user has clicked the
  182. button (or pressed the escape or return keys).
  183. @param iconType the type of icon to show
  184. @param title the headline to show at the top of the box
  185. @param message a longer, more descriptive message to show underneath the
  186. headline
  187. @param buttonText the text to show in the button - if this string is empty, the
  188. default string "OK" (or a localised version) will be used.
  189. @param associatedComponent if this is non-null, it specifies the component that the
  190. alert window should be associated with. Depending on the look
  191. and feel, this might be used for positioning of the alert window.
  192. */
  193. static void JUCE_CALLTYPE showMessageBox (MessageBoxIconType iconType,
  194. const String& title,
  195. const String& message,
  196. const String& buttonText = String(),
  197. Component* associatedComponent = nullptr);
  198. /** Shows a dialog box using the specified options.
  199. The box is shown modally, and the method will block until the user dismisses it.
  200. @param options the options to use when creating the dialog.
  201. @returns the index of the button that was clicked.
  202. @see MessageBoxOptions
  203. */
  204. static int JUCE_CALLTYPE show (const MessageBoxOptions& options);
  205. #endif
  206. /** Shows a dialog box using the specified options.
  207. The box will be displayed and placed into a modal state, but this method will return
  208. immediately, and the callback will be invoked later when the user dismisses the box.
  209. @param options the options to use when creating the dialog.
  210. @param callback if this is non-null, the callback will receive a call to its
  211. modalStateFinished() when the box is dismissed with the index of the
  212. button that was clicked as its argument.
  213. The callback object will be owned and deleted by the system, so make sure
  214. that it works safely and doesn't keep any references to objects that might
  215. be deleted before it gets called.
  216. @see MessageBoxOptions
  217. */
  218. static void JUCE_CALLTYPE showAsync (const MessageBoxOptions& options,
  219. ModalComponentManager::Callback* callback);
  220. /** Shows a dialog box using the specified options.
  221. The box will be displayed and placed into a modal state, but this method will return
  222. immediately, and the callback will be invoked later when the user dismisses the box.
  223. @param options the options to use when creating the dialog.
  224. @param callback if this is non-null, the callback will be called when the box is
  225. dismissed with the index of the button that was clicked as its argument.
  226. @see MessageBoxOptions
  227. */
  228. static void JUCE_CALLTYPE showAsync (const MessageBoxOptions& options,
  229. std::function<void (int)> callback);
  230. /** Shows a dialog box that just has a message and a single button to get rid of it.
  231. The box will be displayed and placed into a modal state, but this method will
  232. return immediately, and if a callback was supplied, it will be invoked later
  233. 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 buttonText the text to show in the button - if this string is empty, the
  239. default string "OK" (or a localised version) 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 callback will receive a call to its
  244. modalStateFinished() when the box is dismissed. The callback object
  245. will be owned and deleted by the system, so make sure that it works
  246. safely and doesn't keep any references to objects that might be deleted
  247. before it gets called.
  248. */
  249. static void JUCE_CALLTYPE showMessageBoxAsync (MessageBoxIconType iconType,
  250. const String& title,
  251. const String& message,
  252. const String& buttonText = String(),
  253. Component* associatedComponent = nullptr,
  254. ModalComponentManager::Callback* callback = nullptr);
  255. /** Shows a dialog box with two buttons.
  256. Ideal for ok/cancel or yes/no choices. The return key can also be used
  257. to trigger the first button, and the escape key for the second button.
  258. If the callback parameter is null, the box is shown modally, and the method will
  259. block until the user has clicked the button (or pressed the escape or return keys).
  260. If the callback parameter is non-null, the box will be displayed and placed into a
  261. modal state, but this method will return immediately, and the callback will be invoked
  262. later when the user dismisses the box.
  263. @param iconType the type of icon to show
  264. @param title the headline to show at the top of the box
  265. @param message a longer, more descriptive message to show underneath the
  266. headline
  267. @param button1Text the text to show in the first button - if this string is
  268. empty, the default string "OK" (or a localised version of it)
  269. will be used.
  270. @param button2Text the text to show in the second button - if this string is
  271. empty, the default string "cancel" (or a localised version of it)
  272. will be used.
  273. @param associatedComponent if this is non-null, it specifies the component that the
  274. alert window should be associated with. Depending on the look
  275. and feel, this might be used for positioning of the alert window.
  276. @param callback if this is non-null, the menu will be launched asynchronously,
  277. returning immediately, and the callback will receive a call to its
  278. modalStateFinished() when the box is dismissed, with its parameter
  279. being 1 if the ok button was pressed, or 0 for cancel. The callback object
  280. will be owned and deleted by the system, so make sure that it works
  281. safely and doesn't keep any references to objects that might be deleted
  282. before it gets called.
  283. @returns true if button 1 was clicked, false if it was button 2. If the callback parameter
  284. is not null, the method always returns false, and the user's choice is delivered
  285. later by the callback.
  286. */
  287. static bool JUCE_CALLTYPE showOkCancelBox (MessageBoxIconType iconType,
  288. const String& title,
  289. const String& message,
  290. #if JUCE_MODAL_LOOPS_PERMITTED
  291. const String& button1Text = String(),
  292. const String& button2Text = String(),
  293. Component* associatedComponent = nullptr,
  294. ModalComponentManager::Callback* callback = nullptr);
  295. #else
  296. const String& button1Text,
  297. const String& button2Text,
  298. Component* associatedComponent,
  299. ModalComponentManager::Callback* callback);
  300. #endif
  301. /** Shows a dialog box with three buttons.
  302. Ideal for yes/no/cancel boxes.
  303. The escape key can be used to trigger the third button.
  304. If the callback parameter is null, the box is shown modally, and the method will
  305. block until the user has clicked the button (or pressed the escape or return keys).
  306. If the callback parameter is non-null, the box will be displayed and placed into a
  307. modal state, but this method will return immediately, and the callback will be invoked
  308. later when the user dismisses the box.
  309. @param iconType the type of icon to show
  310. @param title the headline to show at the top of the box
  311. @param message a longer, more descriptive message to show underneath the
  312. headline
  313. @param button1Text the text to show in the first button - if an empty string, then
  314. "yes" will be used (or a localised version of it)
  315. @param button2Text the text to show in the first button - if an empty string, then
  316. "no" will be used (or a localised version of it)
  317. @param button3Text the text to show in the first button - if an empty string, then
  318. "cancel" will be used (or a localised version of it)
  319. @param associatedComponent if this is non-null, it specifies the component that the
  320. alert window should be associated with. Depending on the look
  321. and feel, this might be used for positioning of the alert window.
  322. @param callback if this is non-null, the menu will be launched asynchronously,
  323. returning immediately, and the callback will receive a call to its
  324. modalStateFinished() when the box is dismissed, with its parameter
  325. being 1 if the "yes" button was pressed, 2 for the "no" button, or 0
  326. if it was cancelled. The callback object will be owned and deleted by the
  327. system, so make sure that it works safely and doesn't keep any references
  328. to objects that might be deleted before it gets called.
  329. @returns If the callback parameter has been set, this returns 0. Otherwise, it
  330. returns one of the following values:
  331. - 0 if the third button was pressed (normally used for 'cancel')
  332. - 1 if the first button was pressed (normally used for 'yes')
  333. - 2 if the middle button was pressed (normally used for 'no')
  334. */
  335. static int JUCE_CALLTYPE showYesNoCancelBox (MessageBoxIconType iconType,
  336. const String& title,
  337. const String& message,
  338. #if JUCE_MODAL_LOOPS_PERMITTED
  339. const String& button1Text = String(),
  340. const String& button2Text = String(),
  341. const String& button3Text = String(),
  342. Component* associatedComponent = nullptr,
  343. ModalComponentManager::Callback* callback = nullptr);
  344. #else
  345. const String& button1Text,
  346. const String& button2Text,
  347. const String& button3Text,
  348. Component* associatedComponent,
  349. ModalComponentManager::Callback* callback);
  350. #endif
  351. /** Shows an alert window using the specified options.
  352. The box will be displayed and placed into a modal state, but this method will return
  353. immediately, and the callback will be invoked later when the user dismisses the box.
  354. This function is always asynchronous, even if the callback is null.
  355. The result codes returned by the alert window are as follows.
  356. - One button:
  357. - button[0] returns 0
  358. - Two buttons:
  359. - button[0] returns 1
  360. - button[1] returns 0
  361. - Three buttons:
  362. - button[0] returns 1
  363. - button[1] returns 2
  364. - button[2] returns 0
  365. @param options the options to use when creating the dialog.
  366. @param callback if this is non-null, the callback will receive a call to its
  367. modalStateFinished() when the box is dismissed with the index of the
  368. button that was clicked as its argument.
  369. The callback object will be owned and deleted by the system, so make sure
  370. that it works safely and doesn't keep any references to objects that might
  371. be deleted before it gets called.
  372. @returns a ScopedMessageBox instance. The message box will remain visible for no
  373. longer than the ScopedMessageBox remains alive.
  374. @see MessageBoxOptions
  375. */
  376. [[nodiscard]] static ScopedMessageBox showScopedAsync (const MessageBoxOptions& options,
  377. std::function<void (int)> callback);
  378. //==============================================================================
  379. #if JUCE_MODAL_LOOPS_PERMITTED && ! defined (DOXYGEN)
  380. /** Shows an operating-system native dialog box.
  381. @param title the title to use at the top
  382. @param bodyText the longer message to show
  383. @param isOkCancel if true, this will show an ok/cancel box, if false,
  384. it'll show a box with just an ok button
  385. @returns true if the ok button was pressed, false if they pressed cancel.
  386. */
  387. [[deprecated ("Use the NativeMessageBox methods instead for more options")]]
  388. static bool JUCE_CALLTYPE showNativeDialogBox (const String& title,
  389. const String& bodyText,
  390. bool isOkCancel);
  391. #endif
  392. //==============================================================================
  393. /** A set of colour IDs to use to change the colour of various aspects of the alert box.
  394. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  395. methods.
  396. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  397. */
  398. enum ColourIds
  399. {
  400. backgroundColourId = 0x1001800, /**< The background colour for the window. */
  401. textColourId = 0x1001810, /**< The colour for the text. */
  402. outlineColourId = 0x1001820 /**< An optional colour to use to draw a border around the window. */
  403. };
  404. //==============================================================================
  405. /** This abstract base class is implemented by LookAndFeel classes to provide
  406. alert-window drawing functionality.
  407. */
  408. struct JUCE_API LookAndFeelMethods
  409. {
  410. virtual ~LookAndFeelMethods() = default;
  411. virtual AlertWindow* createAlertWindow (const String& title, const String& message,
  412. const String& button1,
  413. const String& button2,
  414. const String& button3,
  415. MessageBoxIconType iconType,
  416. int numButtons,
  417. Component* associatedComponent) = 0;
  418. virtual void drawAlertBox (Graphics&, AlertWindow&, const Rectangle<int>& textArea, TextLayout&) = 0;
  419. virtual int getAlertBoxWindowFlags() = 0;
  420. virtual Array<int> getWidthsForTextButtons (AlertWindow&, const Array<TextButton*>&) = 0;
  421. virtual int getAlertWindowButtonHeight() = 0;
  422. virtual Font getAlertWindowTitleFont() = 0;
  423. virtual Font getAlertWindowMessageFont() = 0;
  424. virtual Font getAlertWindowFont() = 0;
  425. };
  426. //==============================================================================
  427. using AlertIconType = MessageBoxIconType;
  428. static constexpr auto NoIcon = MessageBoxIconType::NoIcon;
  429. static constexpr auto QuestionIcon = MessageBoxIconType::QuestionIcon;
  430. static constexpr auto WarningIcon = MessageBoxIconType::WarningIcon;
  431. static constexpr auto InfoIcon = MessageBoxIconType::InfoIcon;
  432. /** @internal */
  433. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
  434. protected:
  435. //==============================================================================
  436. /** @internal */
  437. void paint (Graphics&) override;
  438. /** @internal */
  439. void mouseDown (const MouseEvent&) override;
  440. /** @internal */
  441. void mouseDrag (const MouseEvent&) override;
  442. /** @internal */
  443. bool keyPressed (const KeyPress&) override;
  444. /** @internal */
  445. void lookAndFeelChanged() override;
  446. /** @internal */
  447. void userTriedToCloseWindow() override;
  448. /** @internal */
  449. int getDesktopWindowStyleFlags() const override;
  450. /** @internal */
  451. float getDesktopScaleFactor() const override { return desktopScale * Desktop::getInstance().getGlobalScaleFactor(); }
  452. private:
  453. //==============================================================================
  454. String text;
  455. TextLayout textLayout;
  456. Label accessibleMessageLabel;
  457. MessageBoxIconType alertIconType;
  458. ComponentBoundsConstrainer constrainer;
  459. ComponentDragger dragger;
  460. Rectangle<int> textArea;
  461. OwnedArray<TextButton> buttons;
  462. OwnedArray<TextEditor> textBoxes;
  463. OwnedArray<ComboBox> comboBoxes;
  464. OwnedArray<ProgressBar> progressBars;
  465. Array<Component*> customComps;
  466. OwnedArray<Component> textBlocks;
  467. Array<Component*> allComps;
  468. StringArray textboxNames, comboBoxNames;
  469. Component* const associatedComponent;
  470. bool escapeKeyCancels = true;
  471. float desktopScale = 1.0f;
  472. void exitAlert (Button* button);
  473. void updateLayout (bool onlyIncreaseSize);
  474. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AlertWindow)
  475. };
  476. } // namespace juce