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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. /** Can be used to enable or disable user-dragging of the window. */
  108. void setDraggable (bool shouldBeDraggable) noexcept;
  109. /** Returns true if the window can be dragged around by the user. */
  110. bool isDraggable() const noexcept { return canDrag; }
  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 a nullptr, 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. /** Returns true if the window has been placed in kiosk-mode.
  148. @see Desktop::setKioskComponent
  149. */
  150. bool isKioskMode() const;
  151. //==============================================================================
  152. /** Returns a string which encodes the window's current size and position.
  153. This string will encapsulate the window's size, position, and whether it's
  154. in full-screen mode. It's intended for letting your application save and
  155. restore a window's position.
  156. Use the restoreWindowStateFromString() to restore from a saved state.
  157. @see restoreWindowStateFromString
  158. */
  159. String getWindowStateAsString();
  160. /** Restores the window to a previously-saved size and position.
  161. This restores the window's size, positon and full-screen status from an
  162. string that was previously created with the getWindowStateAsString()
  163. method.
  164. @returns false if the string wasn't a valid window state
  165. @see getWindowStateAsString
  166. */
  167. bool restoreWindowStateFromString (const String& previousState);
  168. //==============================================================================
  169. /** Returns the current content component.
  170. This will be the component set by setContentOwned() or setContentNonOwned, or
  171. nullptr if none has yet been specified.
  172. @see setContentOwned, setContentNonOwned
  173. */
  174. Component* getContentComponent() const noexcept { return contentComponent; }
  175. /** Changes the current content component.
  176. This sets a component that will be placed in the centre of the ResizableWindow,
  177. (leaving a space around the edge for the border).
  178. You should never add components directly to a ResizableWindow (or any of its subclasses)
  179. with addChildComponent(). Instead, add them to the content component.
  180. @param newContentComponent the new component to use - this component will be deleted when it's
  181. no longer needed (i.e. when the window is deleted or a new content
  182. component is set for it). To set a component that this window will not
  183. delete, call setContentNonOwned() instead.
  184. @param resizeToFitWhenContentChangesSize if true, then the ResizableWindow will maintain its size
  185. such that it always fits around the size of the content component. If false,
  186. the new content will be resized to fit the current space available.
  187. */
  188. void setContentOwned (Component* newContentComponent,
  189. bool resizeToFitWhenContentChangesSize);
  190. /** Changes the current content component.
  191. This sets a component that will be placed in the centre of the ResizableWindow,
  192. (leaving a space around the edge for the border).
  193. You should never add components directly to a ResizableWindow (or any of its subclasses)
  194. with addChildComponent(). Instead, add them to the content component.
  195. @param newContentComponent the new component to use - this component will NOT be deleted by this
  196. component, so it's the caller's responsibility to manage its lifetime (it's
  197. ok to delete it while this window is still using it). To set a content
  198. component that the window will delete, call setContentOwned() instead.
  199. @param resizeToFitWhenContentChangesSize if true, then the ResizableWindow will maintain its size
  200. such that it always fits around the size of the content component. If false,
  201. the new content will be resized to fit the current space available.
  202. */
  203. void setContentNonOwned (Component* newContentComponent,
  204. bool resizeToFitWhenContentChangesSize);
  205. /** Removes the current content component.
  206. If the previous content component was added with setContentOwned(), it will also be deleted. If
  207. it was added with setContentNonOwned(), it will simply be removed from this component.
  208. */
  209. void clearContentComponent();
  210. /** Changes the window so that the content component ends up with the specified size.
  211. This is basically a setSize call on the window, but which adds on the borders,
  212. so you can specify the content component's target size.
  213. */
  214. void setContentComponentSize (int width, int height);
  215. /** Returns the width of the frame to use around the window.
  216. @see getContentComponentBorder
  217. */
  218. virtual BorderSize<int> getBorderThickness();
  219. /** Returns the insets to use when positioning the content component.
  220. @see getBorderThickness
  221. */
  222. virtual BorderSize<int> getContentComponentBorder();
  223. //==============================================================================
  224. /** A set of colour IDs to use to change the colour of various aspects of the window.
  225. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  226. methods.
  227. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  228. */
  229. enum ColourIds
  230. {
  231. backgroundColourId = 0x1005700, /**< A colour to use to fill the window's background. */
  232. };
  233. //==============================================================================
  234. // Deprecated: use setContentOwned() and setContentNonOwned() instead.
  235. JUCE_DEPRECATED (void setContentComponent (Component* newContentComponent,
  236. bool deleteOldOne = true,
  237. bool resizeToFit = false));
  238. using TopLevelWindow::addToDesktop;
  239. //==============================================================================
  240. /** This abstract base class is implemented by LookAndFeel classes to provide
  241. window drawing functionality.
  242. */
  243. struct JUCE_API LookAndFeelMethods
  244. {
  245. virtual ~LookAndFeelMethods() {}
  246. //==============================================================================
  247. virtual void drawCornerResizer (Graphics&, int w, int h, bool isMouseOver, bool isMouseDragging) = 0;
  248. virtual void drawResizableFrame (Graphics&, int w, int h, const BorderSize<int>&) = 0;
  249. virtual void fillResizableWindowBackground (Graphics&, int w, int h, const BorderSize<int>&, ResizableWindow&) = 0;
  250. virtual void drawResizableWindowBorder (Graphics&, int w, int h, const BorderSize<int>& border, ResizableWindow&) = 0;
  251. };
  252. protected:
  253. /** @internal */
  254. void paint (Graphics&) override;
  255. /** (if overriding this, make sure you call ResizableWindow::moved() in your subclass) */
  256. void moved() override;
  257. /** (if overriding this, make sure you call ResizableWindow::resized() in your subclass) */
  258. void resized() override;
  259. /** @internal */
  260. void mouseDown (const MouseEvent&) override;
  261. /** @internal */
  262. void mouseDrag (const MouseEvent&) override;
  263. /** @internal */
  264. void mouseUp (const MouseEvent&) override;
  265. /** @internal */
  266. void lookAndFeelChanged() override;
  267. /** @internal */
  268. void childBoundsChanged (Component*) override;
  269. /** @internal */
  270. void parentSizeChanged() override;
  271. /** @internal */
  272. void visibilityChanged() override;
  273. /** @internal */
  274. void activeWindowStatusChanged() override;
  275. /** @internal */
  276. int getDesktopWindowStyleFlags() const override;
  277. #if JUCE_DEBUG
  278. /** Overridden to warn people about adding components directly to this component
  279. instead of using setContentOwned().
  280. If you know what you're doing and are sure you really want to add a component, specify
  281. a base-class method call to Component::addAndMakeVisible(), to side-step this warning.
  282. */
  283. void addChildComponent (Component*, int zOrder = -1);
  284. /** Overridden to warn people about adding components directly to this component
  285. instead of using setContentOwned().
  286. If you know what you're doing and are sure you really want to add a component, specify
  287. a base-class method call to Component::addAndMakeVisible(), to side-step this warning.
  288. */
  289. void addAndMakeVisible (Component*, int zOrder = -1);
  290. #endif
  291. ScopedPointer<ResizableCornerComponent> resizableCorner;
  292. ScopedPointer<ResizableBorderComponent> resizableBorder;
  293. private:
  294. //==============================================================================
  295. Component::SafePointer<Component> contentComponent;
  296. bool ownsContentComponent, resizeToFitContent, fullscreen, canDrag, dragStarted;
  297. ComponentDragger dragger;
  298. Rectangle<int> lastNonFullScreenPos;
  299. ComponentBoundsConstrainer defaultConstrainer;
  300. ComponentBoundsConstrainer* constrainer;
  301. #if JUCE_DEBUG
  302. bool hasBeenResized;
  303. #endif
  304. void initialise (bool addToDesktop);
  305. void updateLastPosIfNotFullScreen();
  306. void updateLastPosIfShowing();
  307. void setContent (Component*, bool takeOwnership, bool resizeToFit);
  308. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  309. // The parameters for these methods have changed - please update your code!
  310. JUCE_DEPRECATED (void getBorderThickness (int& left, int& top, int& right, int& bottom));
  311. JUCE_DEPRECATED (void getContentComponentBorder (int& left, int& top, int& right, int& bottom));
  312. #endif
  313. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResizableWindow)
  314. };
  315. #endif // JUCE_RESIZABLEWINDOW_H_INCLUDED