The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

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