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.

279 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. @param desktopScale specifies the scale to use when drawing the window. In a plugin,
  50. the host controls the scale used to render the plugin editor.
  51. You should query the editor scale with
  52. Component::getApproximateScaleFactorForComponent() and pass the
  53. result here. You can ignore this parameter in a standalone app
  54. */
  55. DialogWindow (const String& name,
  56. Colour backgroundColour,
  57. bool escapeKeyTriggersCloseButton,
  58. bool addToDesktop = true,
  59. float desktopScale = 1.0f);
  60. /** Destructor.
  61. If a content component has been set with setContentOwned(), it will be deleted.
  62. */
  63. ~DialogWindow() override;
  64. //==============================================================================
  65. /** This class defines a collection of settings to be used to open a DialogWindow.
  66. The easiest way to open a DialogWindow is to create yourself a LaunchOptions structure,
  67. initialise its fields with the appropriate details, and then call its launchAsync()
  68. method to launch the dialog.
  69. */
  70. struct JUCE_API LaunchOptions
  71. {
  72. LaunchOptions() noexcept;
  73. /** The title to give the window. */
  74. String dialogTitle;
  75. /** The background colour for the window. */
  76. Colour dialogBackgroundColour = Colours::lightgrey;
  77. /** The content component to show in the window. This must not be null!
  78. Using an OptionalScopedPointer to hold this pointer lets you indicate whether
  79. you'd like the dialog to automatically delete the component when the dialog
  80. has terminated.
  81. */
  82. OptionalScopedPointer<Component> content;
  83. /** If this is not a nullptr, it indicates a component that you'd like to position this
  84. dialog box in front of. See the DocumentWindow::centreAroundComponent() method for
  85. more info about this parameter.
  86. */
  87. Component* componentToCentreAround = nullptr;
  88. /** If true, then the escape key will trigger the dialog's close button. */
  89. bool escapeKeyTriggersCloseButton = true;
  90. /** If true, the dialog will use a native title bar. See TopLevelWindow::setUsingNativeTitleBar() */
  91. bool useNativeTitleBar = true;
  92. /** If true, the window will be resizable. See ResizableWindow::setResizable() */
  93. bool resizable = true;
  94. /** Indicates whether to use a border or corner resizer component. See ResizableWindow::setResizable() */
  95. bool useBottomRightCornerResizer = false;
  96. /** Launches a new modal dialog window.
  97. This will create a dialog based on the settings in this structure,
  98. launch it modally, and return immediately. The window that is returned
  99. will be automatically deleted when the modal state is terminated.
  100. When the dialog's close button is clicked, it'll automatically terminate its
  101. modal state, but you can also do this programmatically by calling
  102. exitModalState (returnValue) on the DialogWindow.
  103. If your content component needs to find the dialog window that it is
  104. contained in, a quick trick is to do this:
  105. @code
  106. if (DialogWindow* dw = contentComponent->findParentComponentOfClass<DialogWindow>())
  107. dw->exitModalState (1234);
  108. @endcode
  109. */
  110. DialogWindow* launchAsync();
  111. /** Creates a new DialogWindow instance with these settings.
  112. This method simply creates the window, it doesn't run it modally. In most cases
  113. you'll want to use launchAsync() or runModal() instead.
  114. */
  115. DialogWindow* create();
  116. #if JUCE_MODAL_LOOPS_PERMITTED
  117. /** Launches and runs the dialog modally, returning the status code that was
  118. used to terminate the modal loop.
  119. Note that running modal loops inline is a BAD technique. If possible, always
  120. use launchAsync() instead of this method.
  121. */
  122. int runModal();
  123. #endif
  124. JUCE_DECLARE_NON_COPYABLE (LaunchOptions)
  125. };
  126. //==============================================================================
  127. /** Easy way of quickly showing a dialog box containing a given component.
  128. Note: This method has been superseded by the DialogWindow::LaunchOptions structure,
  129. which does the same job with some extra flexibility. The showDialog method is here
  130. for backwards compatibility, but please use DialogWindow::LaunchOptions in new code.
  131. This will open and display a DialogWindow containing a given component, making it
  132. modal, but returning immediately to allow the dialog to finish in its own time. If
  133. you want to block and run a modal loop until the dialog is dismissed, use showModalDialog()
  134. instead.
  135. To close the dialog programmatically, you should call exitModalState (returnValue) on
  136. the DialogWindow that is created. To find a pointer to this window from your
  137. contentComponent, you can do something like this:
  138. @code
  139. if (DialogWindow* dw = contentComponent->findParentComponentOfClass<DialogWindow>())
  140. dw->exitModalState (1234);
  141. @endcode
  142. @param dialogTitle the dialog box's title
  143. @param contentComponent the content component for the dialog box. Make sure
  144. that this has been set to the size you want it to
  145. be before calling this method. The component won't
  146. be deleted by this call, so you can re-use it or delete
  147. it afterwards
  148. @param componentToCentreAround if this is not a nullptr, it indicates a component that
  149. you'd like to show this dialog box in front of. See the
  150. DocumentWindow::centreAroundComponent() method for more
  151. info on this parameter
  152. @param backgroundColour a colour to use for the dialog box's background colour
  153. @param escapeKeyTriggersCloseButton if true, then pressing the escape key will cause the
  154. close button to be triggered
  155. @param shouldBeResizable if true, the dialog window has either a resizable border, or
  156. a corner resizer
  157. @param useBottomRightCornerResizer if shouldBeResizable is true, this indicates whether
  158. to use a border or corner resizer component. See ResizableWindow::setResizable()
  159. */
  160. static void showDialog (const String& dialogTitle,
  161. Component* contentComponent,
  162. Component* componentToCentreAround,
  163. Colour backgroundColour,
  164. bool escapeKeyTriggersCloseButton,
  165. bool shouldBeResizable = false,
  166. bool useBottomRightCornerResizer = false,
  167. bool useNativeTitleBar = true);
  168. #if JUCE_MODAL_LOOPS_PERMITTED
  169. /** Easy way of quickly showing a dialog box containing a given component.
  170. Note: This method has been superseded by the DialogWindow::LaunchOptions structure,
  171. which does the same job with some extra flexibility. The showDialog method is here
  172. for backwards compatibility, but please use DialogWindow::LaunchOptions in new code.
  173. This will open and display a DialogWindow containing a given component, returning
  174. when the user clicks its close button.
  175. It returns the value that was returned by the dialog box's runModalLoop() call.
  176. To close the dialog programmatically, you should call exitModalState (returnValue) on
  177. the DialogWindow that is created. To find a pointer to this window from your
  178. contentComponent, you can do something like this:
  179. @code
  180. if (DialogWindow* dw = contentComponent->findParentComponentOfClass<DialogWindow>())
  181. dw->exitModalState (1234);
  182. @endcode
  183. @param dialogTitle the dialog box's title
  184. @param contentComponent the content component for the dialog box. Make sure
  185. that this has been set to the size you want it to
  186. be before calling this method. The component won't
  187. be deleted by this call, so you can re-use it or delete
  188. it afterwards
  189. @param componentToCentreAround if this is not a nullptr, it indicates a component that
  190. you'd like to show this dialog box in front of. See the
  191. DocumentWindow::centreAroundComponent() method for more
  192. info on this parameter
  193. @param backgroundColour a colour to use for the dialog box's background colour
  194. @param escapeKeyTriggersCloseButton if true, then pressing the escape key will cause the
  195. close button to be triggered
  196. @param shouldBeResizable if true, the dialog window has either a resizable border, or
  197. a corner resizer
  198. @param useBottomRightCornerResizer if shouldBeResizable is true, this indicates whether
  199. to use a border or corner resizer component. See ResizableWindow::setResizable()
  200. */
  201. static int showModalDialog (const String& dialogTitle,
  202. Component* contentComponent,
  203. Component* componentToCentreAround,
  204. Colour backgroundColour,
  205. bool escapeKeyTriggersCloseButton,
  206. bool shouldBeResizable = false,
  207. bool useBottomRightCornerResizer = false,
  208. bool useNativeTitleBar = true);
  209. #endif
  210. /** Called when the escape key is pressed.
  211. This can be overridden to do things other than the default behaviour, which is to hide
  212. the window. Return true if the key has been used, or false if it was ignored.
  213. */
  214. virtual bool escapeKeyPressed();
  215. protected:
  216. //==============================================================================
  217. /** @internal */
  218. void resized() override;
  219. /** @internal */
  220. bool keyPressed (const KeyPress&) override;
  221. /** @internal */
  222. float getDesktopScaleFactor() const override { return desktopScale * Desktop::getInstance().getGlobalScaleFactor(); }
  223. private:
  224. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
  225. float desktopScale = 1.0f;
  226. bool escapeKeyTriggersCloseButton;
  227. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DialogWindow)
  228. };
  229. } // namespace juce