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

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