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.

261 lines
13KB

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