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.

487 lines
25KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /** A window that displays a message and has buttons for the user to react to it.
  20. For simple dialog boxes with just a couple of buttons on them, there are
  21. some static methods for running these.
  22. For more complex dialogs, an AlertWindow can be created, then it can have some
  23. buttons and components added to it, and its runModalLoop() method is then used to
  24. show it. The value returned by runModalLoop() shows which button the
  25. user pressed to dismiss the box.
  26. @see ThreadWithProgressWindow
  27. */
  28. class JUCE_API AlertWindow : public TopLevelWindow,
  29. private ButtonListener // (can't use Button::Listener due to idiotic VC2005 bug)
  30. {
  31. public:
  32. //==============================================================================
  33. /** The type of icon to show in the dialog box. */
  34. enum AlertIconType
  35. {
  36. NoIcon, /**< No icon will be shown on the dialog box. */
  37. QuestionIcon, /**< A question-mark icon, for dialog boxes that need the
  38. user to answer a question. */
  39. WarningIcon, /**< An exclamation mark to indicate that the dialog is a
  40. warning about something and shouldn't be ignored. */
  41. InfoIcon /**< An icon that indicates that the dialog box is just
  42. giving the user some information, which doesn't require
  43. a response from them. */
  44. };
  45. //==============================================================================
  46. /** Creates an AlertWindow.
  47. @param title the headline to show at the top of the dialog box
  48. @param message a longer, more descriptive message to show underneath the
  49. headline
  50. @param iconType the type of icon to display
  51. @param associatedComponent if this is non-null, it specifies the component that the
  52. alert window should be associated with. Depending on the look
  53. and feel, this might be used for positioning of the alert window.
  54. */
  55. AlertWindow (const String& title,
  56. const String& message,
  57. AlertIconType iconType,
  58. Component* associatedComponent = nullptr);
  59. /** Destroys the AlertWindow */
  60. ~AlertWindow();
  61. //==============================================================================
  62. /** Returns the type of alert icon that was specified when the window
  63. was created. */
  64. AlertIconType getAlertType() const noexcept { return alertIconType; }
  65. //==============================================================================
  66. /** Changes the dialog box's message.
  67. This will also resize the window to fit the new message if required.
  68. */
  69. void setMessage (const String& message);
  70. //==============================================================================
  71. /** Adds a button to the window.
  72. @param name the text to show on the button
  73. @param returnValue the value that should be returned from runModalLoop()
  74. if this is the button that the user presses.
  75. @param shortcutKey1 an optional key that can be pressed to trigger this button
  76. @param shortcutKey2 a second optional key that can be pressed to trigger this button
  77. */
  78. void addButton (const String& name,
  79. int returnValue,
  80. const KeyPress& shortcutKey1 = KeyPress(),
  81. const KeyPress& shortcutKey2 = KeyPress());
  82. /** Returns the number of buttons that the window currently has. */
  83. int getNumButtons() 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. // easy-to-use message box functions:
  180. #if JUCE_MODAL_LOOPS_PERMITTED
  181. /** Shows a dialog box that just has a message and a single button to get rid of it.
  182. The box is shown modally, and the method will block until the user has clicked the
  183. button (or pressed the escape or return keys).
  184. @param iconType the type of icon to show
  185. @param title the headline to show at the top of the box
  186. @param message a longer, more descriptive message to show underneath the
  187. headline
  188. @param buttonText the text to show in the button - if this string is empty, the
  189. default string "OK" (or a localised version) will be used.
  190. @param associatedComponent if this is non-null, it specifies the component that the
  191. alert window should be associated with. Depending on the look
  192. and feel, this might be used for positioning of the alert window.
  193. */
  194. static void JUCE_CALLTYPE showMessageBox (AlertIconType iconType,
  195. const String& title,
  196. const String& message,
  197. const String& buttonText = String(),
  198. Component* associatedComponent = nullptr);
  199. #endif
  200. /** Shows a dialog box that just has a message and a single button to get rid of it.
  201. The box will be displayed and placed into a modal state, but this method will
  202. return immediately, and if a callback was supplied, it will be invoked later
  203. when the user dismisses the box.
  204. @param iconType the type of icon to show
  205. @param title the headline to show at the top of the box
  206. @param message a longer, more descriptive message to show underneath the
  207. headline
  208. @param buttonText the text to show in the button - if this string is empty, the
  209. default string "OK" (or a localised version) will be used.
  210. @param associatedComponent if this is non-null, it specifies the component that the
  211. alert window should be associated with. Depending on the look
  212. and feel, this might be used for positioning of the alert window.
  213. @param callback if this is non-null, the callback will receive a call to its
  214. modalStateFinished() when the box is dismissed. The callback object
  215. will be owned and deleted by the system, so make sure that it works
  216. safely and doesn't keep any references to objects that might be deleted
  217. before it gets called.
  218. */
  219. static void JUCE_CALLTYPE showMessageBoxAsync (AlertIconType iconType,
  220. const String& title,
  221. const String& message,
  222. const String& buttonText = String(),
  223. Component* associatedComponent = nullptr,
  224. ModalComponentManager::Callback* callback = nullptr);
  225. /** Shows a dialog box with two buttons.
  226. Ideal for ok/cancel or yes/no choices. The return key can also be used
  227. to trigger the first button, and the escape key for the second button.
  228. If the callback parameter is null, the box is shown modally, and the method will
  229. block until the user has clicked the button (or pressed the escape or return keys).
  230. If the callback parameter is non-null, the box will be displayed and placed into a
  231. modal state, but this method will return immediately, and the callback will be invoked
  232. later when the user dismisses the box.
  233. @param iconType the type of icon to show
  234. @param title the headline to show at the top of the box
  235. @param message a longer, more descriptive message to show underneath the
  236. headline
  237. @param button1Text the text to show in the first button - if this string is
  238. empty, the default string "OK" (or a localised version of it)
  239. will be used.
  240. @param button2Text the text to show in the second button - if this string is
  241. empty, the default string "cancel" (or a localised version of it)
  242. will be used.
  243. @param associatedComponent if this is non-null, it specifies the component that the
  244. alert window should be associated with. Depending on the look
  245. and feel, this might be used for positioning of the alert window.
  246. @param callback if this is non-null, the menu will be launched asynchronously,
  247. returning immediately, and the callback will receive a call to its
  248. modalStateFinished() when the box is dismissed, with its parameter
  249. being 1 if the ok button was pressed, or 0 for cancel. The callback object
  250. will be owned and deleted by the system, so make sure that it works
  251. safely and doesn't keep any references to objects that might be deleted
  252. before it gets called.
  253. @returns true if button 1 was clicked, false if it was button 2. If the callback parameter
  254. is not null, the method always returns false, and the user's choice is delivered
  255. later by the callback.
  256. */
  257. static bool JUCE_CALLTYPE showOkCancelBox (AlertIconType iconType,
  258. const String& title,
  259. const String& message,
  260. #if JUCE_MODAL_LOOPS_PERMITTED
  261. const String& button1Text = String(),
  262. const String& button2Text = String(),
  263. Component* associatedComponent = nullptr,
  264. ModalComponentManager::Callback* callback = nullptr);
  265. #else
  266. const String& button1Text,
  267. const String& button2Text,
  268. Component* associatedComponent,
  269. ModalComponentManager::Callback* callback);
  270. #endif
  271. /** Shows a dialog box with three buttons.
  272. Ideal for yes/no/cancel boxes.
  273. The escape key can be used to trigger the third button.
  274. If the callback parameter is null, the box is shown modally, and the method will
  275. block until the user has clicked the button (or pressed the escape or return keys).
  276. If the callback parameter is non-null, the box will be displayed and placed into a
  277. modal state, but this method will return immediately, and the callback will be invoked
  278. later when the user dismisses the box.
  279. @param iconType the type of icon to show
  280. @param title the headline to show at the top of the box
  281. @param message a longer, more descriptive message to show underneath the
  282. headline
  283. @param button1Text the text to show in the first button - if an empty string, then
  284. "yes" will be used (or a localised version of it)
  285. @param button2Text the text to show in the first button - if an empty string, then
  286. "no" will be used (or a localised version of it)
  287. @param button3Text the text to show in the first button - if an empty string, then
  288. "cancel" will be used (or a localised version of it)
  289. @param associatedComponent if this is non-null, it specifies the component that the
  290. alert window should be associated with. Depending on the look
  291. and feel, this might be used for positioning of the alert window.
  292. @param callback if this is non-null, the menu will be launched asynchronously,
  293. returning immediately, and the callback will receive a call to its
  294. modalStateFinished() when the box is dismissed, with its parameter
  295. being 1 if the "yes" button was pressed, 2 for the "no" button, or 0
  296. if it was cancelled. The callback object will be owned and deleted by the
  297. system, so make sure that it works safely and doesn't keep any references
  298. to objects that might be deleted before it gets called.
  299. @returns If the callback parameter has been set, this returns 0. Otherwise, it
  300. returns one of the following values:
  301. - 0 if the third button was pressed (normally used for 'cancel')
  302. - 1 if the first button was pressed (normally used for 'yes')
  303. - 2 if the middle button was pressed (normally used for 'no')
  304. */
  305. static int JUCE_CALLTYPE showYesNoCancelBox (AlertIconType iconType,
  306. const String& title,
  307. const String& message,
  308. #if JUCE_MODAL_LOOPS_PERMITTED
  309. const String& button1Text = String(),
  310. const String& button2Text = String(),
  311. const String& button3Text = String(),
  312. Component* associatedComponent = nullptr,
  313. ModalComponentManager::Callback* callback = nullptr);
  314. #else
  315. const String& button1Text,
  316. const String& button2Text,
  317. const String& button3Text,
  318. Component* associatedComponent,
  319. ModalComponentManager::Callback* callback);
  320. #endif
  321. //==============================================================================
  322. /** Shows an operating-system native dialog box.
  323. @param title the title to use at the top
  324. @param bodyText the longer message to show
  325. @param isOkCancel if true, this will show an ok/cancel box, if false,
  326. it'll show a box with just an ok button
  327. @returns true if the ok button was pressed, false if they pressed cancel.
  328. */
  329. #if JUCE_MODAL_LOOPS_PERMITTED
  330. static bool JUCE_CALLTYPE showNativeDialogBox (const String& title,
  331. const String& bodyText,
  332. bool isOkCancel);
  333. #endif
  334. //==============================================================================
  335. /** A set of colour IDs to use to change the colour of various aspects of the alert box.
  336. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  337. methods.
  338. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  339. */
  340. enum ColourIds
  341. {
  342. backgroundColourId = 0x1001800, /**< The background colour for the window. */
  343. textColourId = 0x1001810, /**< The colour for the text. */
  344. outlineColourId = 0x1001820 /**< An optional colour to use to draw a border around the window. */
  345. };
  346. //==============================================================================
  347. /** This abstract base class is implemented by LookAndFeel classes to provide
  348. alert-window drawing functionality.
  349. */
  350. struct JUCE_API LookAndFeelMethods
  351. {
  352. virtual ~LookAndFeelMethods() {}
  353. virtual AlertWindow* createAlertWindow (const String& title, const String& message,
  354. const String& button1,
  355. const String& button2,
  356. const String& button3,
  357. AlertWindow::AlertIconType iconType,
  358. int numButtons,
  359. Component* associatedComponent) = 0;
  360. virtual void drawAlertBox (Graphics&, AlertWindow&, const Rectangle<int>& textArea, TextLayout&) = 0;
  361. virtual int getAlertBoxWindowFlags() = 0;
  362. virtual Array<int> getWidthsForTextButtons (AlertWindow&, const Array<TextButton*>&) = 0;
  363. virtual int getAlertWindowButtonHeight() = 0;
  364. virtual Font getAlertWindowTitleFont() = 0;
  365. virtual Font getAlertWindowMessageFont() = 0;
  366. virtual Font getAlertWindowFont() = 0;
  367. };
  368. protected:
  369. //==============================================================================
  370. /** @internal */
  371. void paint (Graphics&) override;
  372. /** @internal */
  373. void mouseDown (const MouseEvent&) override;
  374. /** @internal */
  375. void mouseDrag (const MouseEvent&) override;
  376. /** @internal */
  377. bool keyPressed (const KeyPress&) override;
  378. /** @internal */
  379. void buttonClicked (Button*) override;
  380. /** @internal */
  381. void lookAndFeelChanged() override;
  382. /** @internal */
  383. void userTriedToCloseWindow() override;
  384. /** @internal */
  385. int getDesktopWindowStyleFlags() const override;
  386. private:
  387. //==============================================================================
  388. String text;
  389. TextLayout textLayout;
  390. AlertIconType alertIconType;
  391. ComponentBoundsConstrainer constrainer;
  392. ComponentDragger dragger;
  393. Rectangle<int> textArea;
  394. OwnedArray<TextButton> buttons;
  395. OwnedArray<TextEditor> textBoxes;
  396. OwnedArray<ComboBox> comboBoxes;
  397. OwnedArray<ProgressBar> progressBars;
  398. Array<Component*> customComps;
  399. OwnedArray<Component> textBlocks;
  400. Array<Component*> allComps;
  401. StringArray textboxNames, comboBoxNames;
  402. Component* associatedComponent;
  403. bool escapeKeyCancels;
  404. void updateLayout (bool onlyIncreaseSize);
  405. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AlertWindow)
  406. };