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.

394 lines
17KB

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