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.

261 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. /**
  22. A dialog-box style window.
  23. This class is a convenient way of creating a DocumentWindow with a close button
  24. that can be triggered by pressing the escape key.
  25. Any of the methods available to a DocumentWindow or ResizableWindow are also
  26. available to this, so it can be made resizable, have a menu bar, etc.
  27. You can either override or use an instance of the DialogWindow class directly,
  28. or you can use a DialogWindow::LaunchOptions structure to quickly set up and
  29. launch a box containing a content component.
  30. If you use the class directly, you'll need to override the
  31. DocumentWindow::closeButtonPressed() method to handle the user clicking the close
  32. button - for more info, see the DocumentWindow help.
  33. @see DocumentWindow, ResizableWindow
  34. */
  35. class JUCE_API DialogWindow : public DocumentWindow
  36. {
  37. public:
  38. //==============================================================================
  39. /** Creates a DialogWindow.
  40. @param name the name to give the component - this is also
  41. the title shown at the top of the window. To change
  42. this later, use setName()
  43. @param backgroundColour the colour to use for filling the window's background.
  44. @param escapeKeyTriggersCloseButton if true, then pressing the escape key will cause the
  45. close button to be triggered
  46. @param addToDesktop if true, the window will be automatically added to the
  47. desktop; if false, you can use it as a child component
  48. */
  49. DialogWindow (const String& name,
  50. Colour backgroundColour,
  51. bool escapeKeyTriggersCloseButton,
  52. bool addToDesktop = true);
  53. /** Destructor.
  54. If a content component has been set with setContentOwned(), it will be deleted.
  55. */
  56. ~DialogWindow();
  57. //==============================================================================
  58. /** This class defines a collection of settings to be used to open a DialogWindow.
  59. The easiest way to open a DialogWindow is to create yourself a LaunchOptions structure,
  60. initialise its fields with the appropriate details, and then call its launchAsync()
  61. method to launch the dialog.
  62. */
  63. struct JUCE_API LaunchOptions
  64. {
  65. LaunchOptions() noexcept;
  66. /** The title to give the window. */
  67. String dialogTitle;
  68. /** The background colour for the window. */
  69. Colour dialogBackgroundColour;
  70. /** The content component to show in the window. This must not be null!
  71. Using an OptionalScopedPointer to hold this pointer lets you indicate whether
  72. you'd like the dialog to automatically delete the component when the dialog
  73. has terminated.
  74. */
  75. OptionalScopedPointer<Component> content;
  76. /** If this is not a nullptr, it indicates a component that you'd like to position this
  77. dialog box in front of. See the DocumentWindow::centreAroundComponent() method for
  78. more info about this parameter.
  79. */
  80. Component* componentToCentreAround;
  81. /** If true, then the escape key will trigger the dialog's close button. */
  82. bool escapeKeyTriggersCloseButton;
  83. /** If true, the dialog will use a native title bar. See TopLevelWindow::setUsingNativeTitleBar() */
  84. bool useNativeTitleBar;
  85. /** If true, the window will be resizable. See ResizableWindow::setResizable() */
  86. bool resizable;
  87. /** Indicates whether to use a border or corner resizer component. See ResizableWindow::setResizable() */
  88. bool useBottomRightCornerResizer;
  89. /** Launches a new modal dialog window.
  90. This will create a dialog based on the settings in this structure,
  91. launch it modally, and return immediately. The window that is returned
  92. will be automatically deleted when the modal state is terminated.
  93. When the dialog's close button is clicked, it'll automatically terminate its
  94. modal state, but you can also do this programmatically by calling
  95. exitModalState (returnValue) on the DialogWindow.
  96. If your content component needs to find the dialog window that it is
  97. contained in, a quick trick is to do this:
  98. @code
  99. if (DialogWindow* dw = contentComponent->findParentComponentOfClass<DialogWindow>())
  100. dw->exitModalState (1234);
  101. @endcode
  102. */
  103. DialogWindow* launchAsync();
  104. /** Creates a new DialogWindow instance with these settings.
  105. This method simply creates the window, it doesn't run it modally. In most cases
  106. you'll want to use launchAsync() or runModal() instead.
  107. */
  108. DialogWindow* create();
  109. #if JUCE_MODAL_LOOPS_PERMITTED || DOXYGEN
  110. /** Launches and runs the dialog modally, returning the status code that was
  111. used to terminate the modal loop.
  112. Note that running modal loops inline is a BAD technique. If possible, always
  113. use launchAsync() instead of this method.
  114. */
  115. int runModal();
  116. #endif
  117. };
  118. //==============================================================================
  119. /** Easy way of quickly showing a dialog box containing a given component.
  120. Note: this method has been superceded by the DialogWindow::LaunchOptions structure,
  121. which does the same job with some extra flexibility. The showDialog method is here
  122. for backwards compatibility, but please use DialogWindow::LaunchOptions in new code.
  123. This will open and display a DialogWindow containing a given component, making it
  124. modal, but returning immediately to allow the dialog to finish in its own time. If
  125. you want to block and run a modal loop until the dialog is dismissed, use showModalDialog()
  126. instead.
  127. To close the dialog programmatically, you should call exitModalState (returnValue) on
  128. the DialogWindow that is created. To find a pointer to this window from your
  129. contentComponent, you can do something like this:
  130. @code
  131. if (DialogWindow* dw = contentComponent->findParentComponentOfClass<DialogWindow>())
  132. dw->exitModalState (1234);
  133. @endcode
  134. @param dialogTitle the dialog box's title
  135. @param contentComponent the content component for the dialog box. Make sure
  136. that this has been set to the size you want it to
  137. be before calling this method. The component won't
  138. be deleted by this call, so you can re-use it or delete
  139. it afterwards
  140. @param componentToCentreAround if this is not a nullptr, it indicates a component that
  141. you'd like to show this dialog box in front of. See the
  142. DocumentWindow::centreAroundComponent() method for more
  143. info on this parameter
  144. @param backgroundColour a colour to use for the dialog box's background colour
  145. @param escapeKeyTriggersCloseButton if true, then pressing the escape key will cause the
  146. close button to be triggered
  147. @param shouldBeResizable if true, the dialog window has either a resizable border, or
  148. a corner resizer
  149. @param useBottomRightCornerResizer if shouldBeResizable is true, this indicates whether
  150. to use a border or corner resizer component. See ResizableWindow::setResizable()
  151. */
  152. static void showDialog (const String& dialogTitle,
  153. Component* contentComponent,
  154. Component* componentToCentreAround,
  155. Colour backgroundColour,
  156. bool escapeKeyTriggersCloseButton,
  157. bool shouldBeResizable = false,
  158. bool useBottomRightCornerResizer = false);
  159. #if JUCE_MODAL_LOOPS_PERMITTED || DOXYGEN
  160. /** Easy way of quickly showing a dialog box containing a given component.
  161. Note: this method has been superceded by the DialogWindow::LaunchOptions structure,
  162. which does the same job with some extra flexibility. The showDialog method is here
  163. for backwards compatibility, but please use DialogWindow::LaunchOptions in new code.
  164. This will open and display a DialogWindow containing a given component, returning
  165. when the user clicks its close button.
  166. It returns the value that was returned by the dialog box's runModalLoop() call.
  167. To close the dialog programmatically, you should call exitModalState (returnValue) on
  168. the DialogWindow that is created. To find a pointer to this window from your
  169. contentComponent, you can do something like this:
  170. @code
  171. if (DialogWindow* dw = contentComponent->findParentComponentOfClass<DialogWindow>())
  172. dw->exitModalState (1234);
  173. @endcode
  174. @param dialogTitle the dialog box's title
  175. @param contentComponent the content component for the dialog box. Make sure
  176. that this has been set to the size you want it to
  177. be before calling this method. The component won't
  178. be deleted by this call, so you can re-use it or delete
  179. it afterwards
  180. @param componentToCentreAround if this is not a nullptr, it indicates a component that
  181. you'd like to show this dialog box in front of. See the
  182. DocumentWindow::centreAroundComponent() method for more
  183. info on this parameter
  184. @param backgroundColour a colour to use for the dialog box's background colour
  185. @param escapeKeyTriggersCloseButton if true, then pressing the escape key will cause the
  186. close button to be triggered
  187. @param shouldBeResizable if true, the dialog window has either a resizable border, or
  188. a corner resizer
  189. @param useBottomRightCornerResizer if shouldBeResizable is true, this indicates whether
  190. to use a border or corner resizer component. See ResizableWindow::setResizable()
  191. */
  192. static int showModalDialog (const String& dialogTitle,
  193. Component* contentComponent,
  194. Component* componentToCentreAround,
  195. Colour backgroundColour,
  196. bool escapeKeyTriggersCloseButton,
  197. bool shouldBeResizable = false,
  198. bool useBottomRightCornerResizer = false);
  199. #endif
  200. /** Called when the escape key is pressed.
  201. This can be overridden to do things other than the default behaviour, which is to hide
  202. the window. Return true if the key has been used, or false if it was ignored.
  203. */
  204. virtual bool escapeKeyPressed();
  205. protected:
  206. //==============================================================================
  207. /** @internal */
  208. void resized() override;
  209. /** @internal */
  210. bool keyPressed (const KeyPress&) override;
  211. private:
  212. bool escapeKeyTriggersCloseButton;
  213. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DialogWindow)
  214. };