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.

272 lines
14KB

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