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.

juce_ResizableWindow.h 17KB

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