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.

525 lines
27KB

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