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.

juce_DialogWindow.h 13KB

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