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.

411 lines
18KB

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