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.

405 lines
18KB

  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 base class for top-level windows that can be dragged around and resized.
  18. To add content to the window, use its setContentOwned() or setContentNonOwned() methods
  19. to give it a component that will remain positioned inside it (leaving a gap around
  20. the edges for a border).
  21. It's not advisable to add child components directly to a ResizableWindow: put them
  22. inside your content component instead. And overriding methods like resized(), moved(), etc
  23. is also not recommended - instead override these methods for your content component.
  24. (If for some obscure reason you do need to override these methods, always remember to
  25. call the super-class's resized() method too, otherwise it'll fail to lay out the window
  26. decorations correctly).
  27. By default resizing isn't enabled - use the setResizable() method to enable it and
  28. to choose the style of resizing to use.
  29. @see TopLevelWindow
  30. @tags{GUI}
  31. */
  32. class JUCE_API ResizableWindow : public TopLevelWindow
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates a ResizableWindow.
  37. This constructor doesn't specify a background colour, so the LookAndFeel's default
  38. background colour will be used.
  39. @param name the name to give the component
  40. @param addToDesktop if true, the window will be automatically added to the
  41. desktop; if false, you can use it as a child component
  42. */
  43. ResizableWindow (const String& name,
  44. bool addToDesktop);
  45. /** Creates a ResizableWindow.
  46. @param name the name to give the component
  47. @param backgroundColour the colour to use for filling the window's background.
  48. @param addToDesktop if true, the window will be automatically added to the
  49. desktop; if false, you can use it as a child component
  50. */
  51. ResizableWindow (const String& name,
  52. Colour backgroundColour,
  53. bool addToDesktop);
  54. /** Destructor.
  55. If a content component has been set with setContentOwned(), it will be deleted.
  56. */
  57. ~ResizableWindow() override;
  58. //==============================================================================
  59. /** Returns the colour currently being used for the window's background.
  60. As a convenience the window will fill itself with this colour, but you
  61. can override the paint() method if you need more customised behaviour.
  62. This method is the same as retrieving the colour for ResizableWindow::backgroundColourId.
  63. @see setBackgroundColour
  64. */
  65. Colour getBackgroundColour() const noexcept;
  66. /** Changes the colour currently being used for the window's background.
  67. As a convenience the window will fill itself with this colour, but you
  68. can override the paint() method if you need more customised behaviour.
  69. Note that the opaque state of this window is altered by this call to reflect
  70. the opacity of the colour passed-in. On window systems which can't support
  71. semi-transparent windows this might cause problems, (though it's unlikely you'll
  72. be using this class as a base for a semi-transparent component anyway).
  73. You can also use the ResizableWindow::backgroundColourId colour id to set
  74. this colour.
  75. @see getBackgroundColour
  76. */
  77. void setBackgroundColour (Colour newColour);
  78. //==============================================================================
  79. /** Make the window resizable or fixed.
  80. @param shouldBeResizable whether it's resizable at all
  81. @param useBottomRightCornerResizer if true, it'll add a ResizableCornerComponent at the
  82. bottom-right; if false, it'll use a ResizableBorderComponent
  83. around the edge
  84. @see setResizeLimits, isResizable
  85. */
  86. void setResizable (bool shouldBeResizable,
  87. bool useBottomRightCornerResizer);
  88. /** Returns true if resizing is enabled.
  89. @see setResizable
  90. */
  91. bool isResizable() const noexcept;
  92. /** This sets the maximum and minimum sizes for the window.
  93. If the window's current size is outside these limits, it will be resized to
  94. make sure it's within them.
  95. A direct call to setBounds() will bypass any constraint checks, but when the
  96. window is dragged by the user or resized by other indirect means, the constrainer
  97. will limit the numbers involved.
  98. @see setResizable, setFixedAspectRatio
  99. */
  100. void setResizeLimits (int newMinimumWidth,
  101. int newMinimumHeight,
  102. int newMaximumWidth,
  103. int newMaximumHeight) noexcept;
  104. /** Can be used to enable or disable user-dragging of the window. */
  105. void setDraggable (bool shouldBeDraggable) noexcept;
  106. /** Returns true if the window can be dragged around by the user. */
  107. bool isDraggable() const noexcept { return canDrag; }
  108. /** Returns the bounds constrainer object that this window is using.
  109. You can access this to change its properties.
  110. */
  111. ComponentBoundsConstrainer* getConstrainer() noexcept { return constrainer; }
  112. /** Sets the bounds-constrainer object to use for resizing and dragging this window.
  113. A pointer to the object you pass in will be kept, but it won't be deleted
  114. by this object, so it's the caller's responsibility to manage it.
  115. If you pass a nullptr, then no constraints will be placed on the positioning of the window.
  116. */
  117. void setConstrainer (ComponentBoundsConstrainer* newConstrainer);
  118. /** Calls the window's setBounds method, after first checking these bounds
  119. with the current constrainer.
  120. @see setConstrainer
  121. */
  122. void setBoundsConstrained (const Rectangle<int>& newBounds);
  123. //==============================================================================
  124. /** Returns true if the window is currently in full-screen mode.
  125. @see setFullScreen
  126. */
  127. bool isFullScreen() const;
  128. /** Puts the window into full-screen mode, or restores it to its normal size.
  129. If true, the window will become full-screen; if false, it will return to the
  130. last size it was before being made full-screen.
  131. @see isFullScreen
  132. */
  133. void setFullScreen (bool shouldBeFullScreen);
  134. /** Returns true if the window is currently minimised.
  135. @see setMinimised
  136. */
  137. bool isMinimised() const;
  138. /** Minimises the window, or restores it to its previous position and size.
  139. When being un-minimised, it'll return to the last position and size it
  140. was in before being minimised.
  141. @see isMinimised
  142. */
  143. void setMinimised (bool shouldMinimise);
  144. /** Returns true if the window has been placed in kiosk-mode.
  145. @see Desktop::setKioskComponent
  146. */
  147. bool isKioskMode() const;
  148. //==============================================================================
  149. /** Returns a string which encodes the window's current size and position.
  150. This string will encapsulate the window's size, position, and whether it's
  151. in full-screen mode. It's intended for letting your application save and
  152. restore a window's position.
  153. Use the restoreWindowStateFromString() to restore from a saved state.
  154. @see restoreWindowStateFromString
  155. */
  156. String getWindowStateAsString();
  157. /** Restores the window to a previously-saved size and position.
  158. This restores the window's size, position and full-screen status from an
  159. string that was previously created with the getWindowStateAsString()
  160. method.
  161. @returns false if the string wasn't a valid window state
  162. @see getWindowStateAsString
  163. */
  164. bool restoreWindowStateFromString (const String& previousState);
  165. //==============================================================================
  166. /** Returns the current content component.
  167. This will be the component set by setContentOwned() or setContentNonOwned, or
  168. nullptr if none has yet been specified.
  169. @see setContentOwned, setContentNonOwned
  170. */
  171. Component* getContentComponent() const noexcept { return contentComponent; }
  172. /** Changes the current content component.
  173. This sets a component that will be placed in the centre of the ResizableWindow,
  174. (leaving a space around the edge for the border).
  175. You should never add components directly to a ResizableWindow (or any of its subclasses)
  176. with addChildComponent(). Instead, add them to the content component.
  177. @param newContentComponent the new component to use - this component will be deleted when it's
  178. no longer needed (i.e. when the window is deleted or a new content
  179. component is set for it). To set a component that this window will not
  180. delete, call setContentNonOwned() instead.
  181. @param resizeToFitWhenContentChangesSize if true, then the ResizableWindow will maintain its size
  182. such that it always fits around the size of the content component. If false,
  183. the new content will be resized to fit the current space available.
  184. */
  185. void setContentOwned (Component* newContentComponent,
  186. bool resizeToFitWhenContentChangesSize);
  187. /** Changes the current content component.
  188. This sets a component that will be placed in the centre of the ResizableWindow,
  189. (leaving a space around the edge for the border).
  190. You should never add components directly to a ResizableWindow (or any of its subclasses)
  191. with addChildComponent(). Instead, add them to the content component.
  192. @param newContentComponent the new component to use - this component will NOT be deleted by this
  193. component, so it's the caller's responsibility to manage its lifetime (it's
  194. ok to delete it while this window is still using it). To set a content
  195. component that the window will delete, call setContentOwned() instead.
  196. @param resizeToFitWhenContentChangesSize if true, then the ResizableWindow will maintain its size
  197. such that it always fits around the size of the content component. If false,
  198. the new content will be resized to fit the current space available.
  199. */
  200. void setContentNonOwned (Component* newContentComponent,
  201. bool resizeToFitWhenContentChangesSize);
  202. /** Removes the current content component.
  203. If the previous content component was added with setContentOwned(), it will also be deleted. If
  204. it was added with setContentNonOwned(), it will simply be removed from this component.
  205. */
  206. void clearContentComponent();
  207. /** Changes the window so that the content component ends up with the specified size.
  208. This is basically a setSize call on the window, but which adds on the borders,
  209. so you can specify the content component's target size.
  210. */
  211. void setContentComponentSize (int width, int height);
  212. /** Returns the width of the frame to use around the window.
  213. @see getContentComponentBorder
  214. */
  215. virtual BorderSize<int> getBorderThickness();
  216. /** Returns the insets to use when positioning the content component.
  217. @see getBorderThickness
  218. */
  219. virtual BorderSize<int> getContentComponentBorder();
  220. //==============================================================================
  221. /** A set of colour IDs to use to change the colour of various aspects of the window.
  222. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  223. methods.
  224. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  225. */
  226. enum ColourIds
  227. {
  228. backgroundColourId = 0x1005700, /**< A colour to use to fill the window's background. */
  229. };
  230. //==============================================================================
  231. // Deprecated: use setContentOwned() and setContentNonOwned() instead.
  232. JUCE_DEPRECATED (void setContentComponent (Component* newContentComponent,
  233. bool deleteOldOne = true,
  234. bool resizeToFit = false));
  235. using TopLevelWindow::addToDesktop;
  236. //==============================================================================
  237. /** This abstract base class is implemented by LookAndFeel classes to provide
  238. window drawing functionality.
  239. */
  240. struct JUCE_API LookAndFeelMethods
  241. {
  242. virtual ~LookAndFeelMethods() = default;
  243. //==============================================================================
  244. virtual void drawCornerResizer (Graphics&, int w, int h, bool isMouseOver, bool isMouseDragging) = 0;
  245. virtual void drawResizableFrame (Graphics&, int w, int h, const BorderSize<int>&) = 0;
  246. virtual void fillResizableWindowBackground (Graphics&, int w, int h, const BorderSize<int>&, ResizableWindow&) = 0;
  247. virtual void drawResizableWindowBorder (Graphics&, int w, int h, const BorderSize<int>& border, ResizableWindow&) = 0;
  248. };
  249. protected:
  250. /** @internal */
  251. void paint (Graphics&) override;
  252. /** (if overriding this, make sure you call ResizableWindow::moved() in your subclass) */
  253. void moved() override;
  254. /** (if overriding this, make sure you call ResizableWindow::resized() in your subclass) */
  255. void resized() override;
  256. /** @internal */
  257. void mouseDown (const MouseEvent&) override;
  258. /** @internal */
  259. void mouseDrag (const MouseEvent&) override;
  260. /** @internal */
  261. void mouseUp (const MouseEvent&) override;
  262. /** @internal */
  263. void lookAndFeelChanged() override;
  264. /** @internal */
  265. void childBoundsChanged (Component*) override;
  266. /** @internal */
  267. void parentSizeChanged() override;
  268. /** @internal */
  269. void visibilityChanged() override;
  270. /** @internal */
  271. void activeWindowStatusChanged() override;
  272. /** @internal */
  273. int getDesktopWindowStyleFlags() const override;
  274. #if JUCE_DEBUG
  275. /** Overridden to warn people about adding components directly to this component
  276. instead of using setContentOwned().
  277. If you know what you're doing and are sure you really want to add a component, specify
  278. a base-class method call to Component::addAndMakeVisible(), to side-step this warning.
  279. */
  280. void addChildComponent (Component*, int zOrder = -1);
  281. /** Overridden to warn people about adding components directly to this component
  282. instead of using setContentOwned().
  283. If you know what you're doing and are sure you really want to add a component, specify
  284. a base-class method call to Component::addAndMakeVisible(), to side-step this warning.
  285. */
  286. void addAndMakeVisible (Component*, int zOrder = -1);
  287. #endif
  288. std::unique_ptr<ResizableCornerComponent> resizableCorner;
  289. std::unique_ptr<ResizableBorderComponent> resizableBorder;
  290. private:
  291. //==============================================================================
  292. Component::SafePointer<Component> contentComponent;
  293. bool ownsContentComponent = false, resizeToFitContent = false, fullscreen = false, canDrag = true, dragStarted = false;
  294. ComponentDragger dragger;
  295. Rectangle<int> lastNonFullScreenPos;
  296. ComponentBoundsConstrainer defaultConstrainer;
  297. ComponentBoundsConstrainer* constrainer = nullptr;
  298. #if JUCE_DEBUG
  299. bool hasBeenResized = false;
  300. #endif
  301. void initialise (bool addToDesktop);
  302. void updateLastPosIfNotFullScreen();
  303. void updateLastPosIfShowing();
  304. void setContent (Component*, bool takeOwnership, bool resizeToFit);
  305. void updatePeerConstrainer();
  306. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  307. // The parameters for these methods have changed - please update your code!
  308. JUCE_DEPRECATED (void getBorderThickness (int& left, int& top, int& right, int& bottom));
  309. JUCE_DEPRECATED (void getContentComponentBorder (int& left, int& top, int& right, int& bottom));
  310. #endif
  311. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResizableWindow)
  312. };
  313. } // namespace juce