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.

462 lines
24KB

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