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.

393 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef __JUCE_RESIZABLEWINDOW_JUCEHEADER__
  18. #define __JUCE_RESIZABLEWINDOW_JUCEHEADER__
  19. #include "juce_TopLevelWindow.h"
  20. #include "../mouse/juce_ComponentDragger.h"
  21. #include "../layout/juce_ResizableBorderComponent.h"
  22. #include "../layout/juce_ResizableCornerComponent.h"
  23. //==============================================================================
  24. /**
  25. A base class for top-level windows that can be dragged around and resized.
  26. To add content to the window, use its setContentOwned() or setContentNonOwned() methods
  27. to give it a component that will remain positioned inside it (leaving a gap around
  28. the edges for a border).
  29. It's not advisable to add child components directly to a ResizableWindow: put them
  30. inside your content component instead. And overriding methods like resized(), moved(), etc
  31. is also not recommended - instead override these methods for your content component.
  32. (If for some obscure reason you do need to override these methods, always remember to
  33. call the super-class's resized() method too, otherwise it'll fail to lay out the window
  34. decorations correctly).
  35. By default resizing isn't enabled - use the setResizable() method to enable it and
  36. to choose the style of resizing to use.
  37. @see TopLevelWindow
  38. */
  39. class JUCE_API ResizableWindow : public TopLevelWindow
  40. {
  41. public:
  42. //==============================================================================
  43. /** Creates a ResizableWindow.
  44. This constructor doesn't specify a background colour, so the LookAndFeel's default
  45. background colour will be used.
  46. @param name the name to give the component
  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. */
  50. ResizableWindow (const String& name,
  51. bool addToDesktop);
  52. /** Creates a ResizableWindow.
  53. @param name the name to give the component
  54. @param backgroundColour the colour to use for filling the window's background.
  55. @param addToDesktop if true, the window will be automatically added to the
  56. desktop; if false, you can use it as a child component
  57. */
  58. ResizableWindow (const String& name,
  59. const Colour& backgroundColour,
  60. bool addToDesktop);
  61. /** Destructor.
  62. If a content component has been set with setContentOwned(), it will be deleted.
  63. */
  64. ~ResizableWindow();
  65. //==============================================================================
  66. /** Returns 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. This method is the same as retrieving the colour for ResizableWindow::backgroundColourId.
  70. @see setBackgroundColour
  71. */
  72. Colour getBackgroundColour() const noexcept;
  73. /** Changes the colour currently being used for the window's background.
  74. As a convenience the window will fill itself with this colour, but you
  75. can override the paint() method if you need more customised behaviour.
  76. Note that the opaque state of this window is altered by this call to reflect
  77. the opacity of the colour passed-in. On window systems which can't support
  78. semi-transparent windows this might cause problems, (though it's unlikely you'll
  79. be using this class as a base for a semi-transparent component anyway).
  80. You can also use the ResizableWindow::backgroundColourId colour id to set
  81. this colour.
  82. @see getBackgroundColour
  83. */
  84. void setBackgroundColour (const Colour& newColour);
  85. //==============================================================================
  86. /** Make the window resizable or fixed.
  87. @param shouldBeResizable whether it's resizable at all
  88. @param useBottomRightCornerResizer if true, it'll add a ResizableCornerComponent at the
  89. bottom-right; if false, it'll use a ResizableBorderComponent
  90. around the edge
  91. @see setResizeLimits, isResizable
  92. */
  93. void setResizable (bool shouldBeResizable,
  94. bool useBottomRightCornerResizer);
  95. /** True if resizing is enabled.
  96. @see setResizable
  97. */
  98. bool isResizable() const noexcept;
  99. /** This sets the maximum and minimum sizes for the window.
  100. If the window's current size is outside these limits, it will be resized to
  101. make sure it's within them.
  102. Calling setBounds() on the component will bypass any size checking - it's only when
  103. the window is being resized by the user that these values are enforced.
  104. @see setResizable, setFixedAspectRatio
  105. */
  106. void setResizeLimits (int newMinimumWidth,
  107. int newMinimumHeight,
  108. int newMaximumWidth,
  109. int newMaximumHeight) noexcept;
  110. /** Returns the bounds constrainer object that this window is using.
  111. You can access this to change its properties.
  112. */
  113. ComponentBoundsConstrainer* getConstrainer() noexcept { return constrainer; }
  114. /** Sets the bounds-constrainer object to use for resizing and dragging this window.
  115. A pointer to the object you pass in will be kept, but it won't be deleted
  116. by this object, so it's the caller's responsiblity to manage it.
  117. If you pass 0, then no contraints will be placed on the positioning of the window.
  118. */
  119. void setConstrainer (ComponentBoundsConstrainer* newConstrainer);
  120. /** Calls the window's setBounds method, after first checking these bounds
  121. with the current constrainer.
  122. @see setConstrainer
  123. */
  124. void setBoundsConstrained (const Rectangle<int>& bounds);
  125. //==============================================================================
  126. /** Returns true if the window is currently in full-screen mode.
  127. @see setFullScreen
  128. */
  129. bool isFullScreen() const;
  130. /** Puts the window into full-screen mode, or restores it to its normal size.
  131. If true, the window will become full-screen; if false, it will return to the
  132. last size it was before being made full-screen.
  133. @see isFullScreen
  134. */
  135. void setFullScreen (bool shouldBeFullScreen);
  136. /** Returns true if the window is currently minimised.
  137. @see setMinimised
  138. */
  139. bool isMinimised() const;
  140. /** Minimises the window, or restores it to its previous position and size.
  141. When being un-minimised, it'll return to the last position and size it
  142. was in before being minimised.
  143. @see isMinimised
  144. */
  145. void setMinimised (bool shouldMinimise);
  146. /** Adds the window to the desktop using the default flags. */
  147. void addToDesktop();
  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, positon 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. protected:
  237. //==============================================================================
  238. /** @internal */
  239. void paint (Graphics&) override;
  240. /** (if overriding this, make sure you call ResizableWindow::moved() in your subclass) */
  241. void moved() override;
  242. /** (if overriding this, make sure you call ResizableWindow::resized() in your subclass) */
  243. void resized() override;
  244. /** @internal */
  245. void mouseDown (const MouseEvent&) override;
  246. /** @internal */
  247. void mouseDrag (const MouseEvent&) override;
  248. /** @internal */
  249. void lookAndFeelChanged() override;
  250. /** @internal */
  251. void childBoundsChanged (Component*) override;
  252. /** @internal */
  253. void parentSizeChanged() override;
  254. /** @internal */
  255. void visibilityChanged() override;
  256. /** @internal */
  257. void activeWindowStatusChanged() override;
  258. /** @internal */
  259. int getDesktopWindowStyleFlags() const override;
  260. #if JUCE_DEBUG
  261. /** Overridden to warn people about adding components directly to this component
  262. instead of using setContentOwned().
  263. If you know what you're doing and are sure you really want to add a component, specify
  264. a base-class method call to Component::addAndMakeVisible(), to side-step this warning.
  265. */
  266. void addChildComponent (Component* child, int zOrder = -1);
  267. /** Overridden to warn people about adding components directly to this component
  268. instead of using setContentOwned().
  269. If you know what you're doing and are sure you really want to add a component, specify
  270. a base-class method call to Component::addAndMakeVisible(), to side-step this warning.
  271. */
  272. void addAndMakeVisible (Component* child, int zOrder = -1);
  273. #endif
  274. ScopedPointer <ResizableCornerComponent> resizableCorner;
  275. ScopedPointer <ResizableBorderComponent> resizableBorder;
  276. private:
  277. //==============================================================================
  278. Component::SafePointer <Component> contentComponent;
  279. bool ownsContentComponent, resizeToFitContent, fullscreen;
  280. ComponentDragger dragger;
  281. Rectangle<int> lastNonFullScreenPos;
  282. ComponentBoundsConstrainer defaultConstrainer;
  283. ComponentBoundsConstrainer* constrainer;
  284. #if JUCE_DEBUG
  285. bool hasBeenResized;
  286. #endif
  287. void initialise (bool addToDesktop);
  288. void updateLastPos();
  289. void setContent (Component*, bool takeOwnership, bool resizeToFit);
  290. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  291. // The parameters for these methods have changed - please update your code!
  292. JUCE_DEPRECATED (void getBorderThickness (int& left, int& top, int& right, int& bottom));
  293. JUCE_DEPRECATED (void getContentComponentBorder (int& left, int& top, int& right, int& bottom));
  294. #endif
  295. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResizableWindow)
  296. };
  297. #endif // __JUCE_RESIZABLEWINDOW_JUCEHEADER__