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.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. ResizableWindow::ResizableWindow (const String& name,
  18. const bool addToDesktop_)
  19. : TopLevelWindow (name, addToDesktop_),
  20. ownsContentComponent (false),
  21. resizeToFitContent (false),
  22. fullscreen (false),
  23. constrainer (nullptr)
  24. #if JUCE_DEBUG
  25. , hasBeenResized (false)
  26. #endif
  27. {
  28. initialise (addToDesktop_);
  29. }
  30. ResizableWindow::ResizableWindow (const String& name,
  31. Colour backgroundColour_,
  32. const bool addToDesktop_)
  33. : TopLevelWindow (name, addToDesktop_),
  34. ownsContentComponent (false),
  35. resizeToFitContent (false),
  36. fullscreen (false),
  37. constrainer (nullptr)
  38. #if JUCE_DEBUG
  39. , hasBeenResized (false)
  40. #endif
  41. {
  42. setBackgroundColour (backgroundColour_);
  43. initialise (addToDesktop_);
  44. }
  45. ResizableWindow::~ResizableWindow()
  46. {
  47. // Don't delete or remove the resizer components yourself! They're managed by the
  48. // ResizableWindow, and you should leave them alone! You may have deleted them
  49. // accidentally by careless use of deleteAllChildren()..?
  50. jassert (resizableCorner == nullptr || getIndexOfChildComponent (resizableCorner) >= 0);
  51. jassert (resizableBorder == nullptr || getIndexOfChildComponent (resizableBorder) >= 0);
  52. resizableCorner = nullptr;
  53. resizableBorder = nullptr;
  54. clearContentComponent();
  55. // have you been adding your own components directly to this window..? tut tut tut.
  56. // Read the instructions for using a ResizableWindow!
  57. jassert (getNumChildComponents() == 0);
  58. }
  59. void ResizableWindow::initialise (const bool shouldAddToDesktop)
  60. {
  61. defaultConstrainer.setMinimumOnscreenAmounts (0x10000, 16, 24, 16);
  62. lastNonFullScreenPos.setBounds (50, 50, 256, 256);
  63. if (shouldAddToDesktop)
  64. addToDesktop();
  65. }
  66. int ResizableWindow::getDesktopWindowStyleFlags() const
  67. {
  68. int styleFlags = TopLevelWindow::getDesktopWindowStyleFlags();
  69. if (isResizable() && (styleFlags & ComponentPeer::windowHasTitleBar) != 0)
  70. styleFlags |= ComponentPeer::windowIsResizable;
  71. return styleFlags;
  72. }
  73. //==============================================================================
  74. void ResizableWindow::clearContentComponent()
  75. {
  76. if (ownsContentComponent)
  77. {
  78. contentComponent.deleteAndZero();
  79. }
  80. else
  81. {
  82. removeChildComponent (contentComponent);
  83. contentComponent = nullptr;
  84. }
  85. }
  86. void ResizableWindow::setContent (Component* newContentComponent,
  87. const bool takeOwnership,
  88. const bool resizeToFitWhenContentChangesSize)
  89. {
  90. if (newContentComponent != contentComponent)
  91. {
  92. clearContentComponent();
  93. contentComponent = newContentComponent;
  94. Component::addAndMakeVisible (contentComponent);
  95. }
  96. ownsContentComponent = takeOwnership;
  97. resizeToFitContent = resizeToFitWhenContentChangesSize;
  98. if (resizeToFitWhenContentChangesSize)
  99. childBoundsChanged (contentComponent);
  100. resized(); // must always be called to position the new content comp
  101. }
  102. void ResizableWindow::setContentOwned (Component* newContentComponent, const bool resizeToFitWhenContentChangesSize)
  103. {
  104. setContent (newContentComponent, true, resizeToFitWhenContentChangesSize);
  105. }
  106. void ResizableWindow::setContentNonOwned (Component* newContentComponent, const bool resizeToFitWhenContentChangesSize)
  107. {
  108. setContent (newContentComponent, false, resizeToFitWhenContentChangesSize);
  109. }
  110. void ResizableWindow::setContentComponent (Component* const newContentComponent,
  111. const bool deleteOldOne,
  112. const bool resizeToFitWhenContentChangesSize)
  113. {
  114. if (newContentComponent != contentComponent)
  115. {
  116. if (deleteOldOne)
  117. {
  118. contentComponent.deleteAndZero();
  119. }
  120. else
  121. {
  122. removeChildComponent (contentComponent);
  123. contentComponent = nullptr;
  124. }
  125. }
  126. setContent (newContentComponent, true, resizeToFitWhenContentChangesSize);
  127. }
  128. void ResizableWindow::setContentComponentSize (int width, int height)
  129. {
  130. jassert (width > 0 && height > 0); // not a great idea to give it a zero size..
  131. const BorderSize<int> border (getContentComponentBorder());
  132. setSize (width + border.getLeftAndRight(),
  133. height + border.getTopAndBottom());
  134. }
  135. BorderSize<int> ResizableWindow::getBorderThickness()
  136. {
  137. return BorderSize<int> (isUsingNativeTitleBar() ? 0 : ((resizableBorder != nullptr && ! isFullScreen()) ? 5 : 3));
  138. }
  139. BorderSize<int> ResizableWindow::getContentComponentBorder()
  140. {
  141. return getBorderThickness();
  142. }
  143. void ResizableWindow::moved()
  144. {
  145. updateLastPos();
  146. }
  147. void ResizableWindow::visibilityChanged()
  148. {
  149. TopLevelWindow::visibilityChanged();
  150. updateLastPos();
  151. }
  152. void ResizableWindow::resized()
  153. {
  154. if (resizableBorder != nullptr)
  155. {
  156. #if JUCE_WINDOWS || JUCE_LINUX
  157. // hide the resizable border if the OS already provides one..
  158. resizableBorder->setVisible (! (isFullScreen() || isUsingNativeTitleBar()));
  159. #else
  160. resizableBorder->setVisible (! isFullScreen());
  161. #endif
  162. resizableBorder->setBorderThickness (getBorderThickness());
  163. resizableBorder->setSize (getWidth(), getHeight());
  164. resizableBorder->toBack();
  165. }
  166. if (resizableCorner != nullptr)
  167. {
  168. #if JUCE_MAC
  169. // hide the resizable border if the OS already provides one..
  170. resizableCorner->setVisible (! (isFullScreen() || isUsingNativeTitleBar()));
  171. #else
  172. resizableCorner->setVisible (! isFullScreen());
  173. #endif
  174. const int resizerSize = 18;
  175. resizableCorner->setBounds (getWidth() - resizerSize,
  176. getHeight() - resizerSize,
  177. resizerSize, resizerSize);
  178. }
  179. if (contentComponent != nullptr)
  180. {
  181. // The window expects to be able to be able to manage the size and position
  182. // of its content component, so you can't arbitrarily add a transform to it!
  183. jassert (! contentComponent->isTransformed());
  184. contentComponent->setBoundsInset (getContentComponentBorder());
  185. }
  186. updateLastPos();
  187. #if JUCE_DEBUG
  188. hasBeenResized = true;
  189. #endif
  190. }
  191. void ResizableWindow::childBoundsChanged (Component* child)
  192. {
  193. if ((child == contentComponent) && (child != nullptr) && resizeToFitContent)
  194. {
  195. // not going to look very good if this component has a zero size..
  196. jassert (child->getWidth() > 0);
  197. jassert (child->getHeight() > 0);
  198. const BorderSize<int> borders (getContentComponentBorder());
  199. setSize (child->getWidth() + borders.getLeftAndRight(),
  200. child->getHeight() + borders.getTopAndBottom());
  201. }
  202. }
  203. //==============================================================================
  204. void ResizableWindow::activeWindowStatusChanged()
  205. {
  206. const BorderSize<int> border (getContentComponentBorder());
  207. Rectangle<int> area (getLocalBounds());
  208. repaint (area.removeFromTop (border.getTop()));
  209. repaint (area.removeFromLeft (border.getLeft()));
  210. repaint (area.removeFromRight (border.getRight()));
  211. repaint (area.removeFromBottom (border.getBottom()));
  212. }
  213. //==============================================================================
  214. void ResizableWindow::setResizable (const bool shouldBeResizable,
  215. const bool useBottomRightCornerResizer)
  216. {
  217. if (shouldBeResizable)
  218. {
  219. if (useBottomRightCornerResizer)
  220. {
  221. resizableBorder = nullptr;
  222. if (resizableCorner == nullptr)
  223. {
  224. Component::addChildComponent (resizableCorner = new ResizableCornerComponent (this, constrainer));
  225. resizableCorner->setAlwaysOnTop (true);
  226. }
  227. }
  228. else
  229. {
  230. resizableCorner = nullptr;
  231. if (resizableBorder == nullptr)
  232. Component::addChildComponent (resizableBorder = new ResizableBorderComponent (this, constrainer));
  233. }
  234. }
  235. else
  236. {
  237. resizableCorner = nullptr;
  238. resizableBorder = nullptr;
  239. }
  240. if (isUsingNativeTitleBar())
  241. recreateDesktopWindow();
  242. childBoundsChanged (contentComponent);
  243. resized();
  244. }
  245. bool ResizableWindow::isResizable() const noexcept
  246. {
  247. return resizableCorner != nullptr
  248. || resizableBorder != nullptr;
  249. }
  250. void ResizableWindow::setResizeLimits (const int newMinimumWidth,
  251. const int newMinimumHeight,
  252. const int newMaximumWidth,
  253. const int newMaximumHeight) noexcept
  254. {
  255. // if you've set up a custom constrainer then these settings won't have any effect..
  256. jassert (constrainer == &defaultConstrainer || constrainer == nullptr);
  257. if (constrainer == nullptr)
  258. setConstrainer (&defaultConstrainer);
  259. defaultConstrainer.setSizeLimits (newMinimumWidth, newMinimumHeight,
  260. newMaximumWidth, newMaximumHeight);
  261. setBoundsConstrained (getBounds());
  262. }
  263. void ResizableWindow::setConstrainer (ComponentBoundsConstrainer* newConstrainer)
  264. {
  265. if (constrainer != newConstrainer)
  266. {
  267. constrainer = newConstrainer;
  268. const bool useBottomRightCornerResizer = resizableCorner != nullptr;
  269. const bool shouldBeResizable = useBottomRightCornerResizer || resizableBorder != nullptr;
  270. resizableCorner = nullptr;
  271. resizableBorder = nullptr;
  272. setResizable (shouldBeResizable, useBottomRightCornerResizer);
  273. if (ComponentPeer* const peer = getPeer())
  274. peer->setConstrainer (newConstrainer);
  275. }
  276. }
  277. void ResizableWindow::setBoundsConstrained (const Rectangle<int>& newBounds)
  278. {
  279. if (constrainer != nullptr)
  280. constrainer->setBoundsForComponent (this, newBounds, false, false, false, false);
  281. else
  282. setBounds (newBounds);
  283. }
  284. //==============================================================================
  285. void ResizableWindow::paint (Graphics& g)
  286. {
  287. LookAndFeel& lf = getLookAndFeel();
  288. lf.fillResizableWindowBackground (g, getWidth(), getHeight(),
  289. getBorderThickness(), *this);
  290. if (! isFullScreen())
  291. lf.drawResizableWindowBorder (g, getWidth(), getHeight(),
  292. getBorderThickness(), *this);
  293. #if JUCE_DEBUG
  294. /* If this fails, then you've probably written a subclass with a resized()
  295. callback but forgotten to make it call its parent class's resized() method.
  296. It's important when you override methods like resized(), moved(),
  297. etc., that you make sure the base class methods also get called.
  298. Of course you shouldn't really be overriding ResizableWindow::resized() anyway,
  299. because your content should all be inside the content component - and it's the
  300. content component's resized() method that you should be using to do your
  301. layout.
  302. */
  303. jassert (hasBeenResized || (getWidth() == 0 && getHeight() == 0));
  304. #endif
  305. }
  306. void ResizableWindow::lookAndFeelChanged()
  307. {
  308. resized();
  309. if (isOnDesktop())
  310. {
  311. Component::addToDesktop (getDesktopWindowStyleFlags());
  312. if (ComponentPeer* const peer = getPeer())
  313. peer->setConstrainer (constrainer);
  314. }
  315. }
  316. Colour ResizableWindow::getBackgroundColour() const noexcept
  317. {
  318. return findColour (backgroundColourId, false);
  319. }
  320. void ResizableWindow::setBackgroundColour (Colour newColour)
  321. {
  322. Colour backgroundColour (newColour);
  323. if (! Desktop::canUseSemiTransparentWindows())
  324. backgroundColour = newColour.withAlpha (1.0f);
  325. setColour (backgroundColourId, backgroundColour);
  326. setOpaque (backgroundColour.isOpaque());
  327. repaint();
  328. }
  329. //==============================================================================
  330. bool ResizableWindow::isFullScreen() const
  331. {
  332. if (isOnDesktop())
  333. {
  334. ComponentPeer* const peer = getPeer();
  335. return peer != nullptr && peer->isFullScreen();
  336. }
  337. return fullscreen;
  338. }
  339. void ResizableWindow::setFullScreen (const bool shouldBeFullScreen)
  340. {
  341. if (shouldBeFullScreen != isFullScreen())
  342. {
  343. updateLastPos();
  344. fullscreen = shouldBeFullScreen;
  345. if (isOnDesktop())
  346. {
  347. if (ComponentPeer* const peer = getPeer())
  348. {
  349. // keep a copy of this intact in case the real one gets messed-up while we're un-maximising
  350. const Rectangle<int> lastPos (lastNonFullScreenPos);
  351. peer->setFullScreen (shouldBeFullScreen);
  352. if ((! shouldBeFullScreen) && ! lastPos.isEmpty())
  353. setBounds (lastPos);
  354. }
  355. else
  356. {
  357. jassertfalse;
  358. }
  359. }
  360. else
  361. {
  362. if (shouldBeFullScreen)
  363. setBounds (0, 0, getParentWidth(), getParentHeight());
  364. else
  365. setBounds (lastNonFullScreenPos);
  366. }
  367. resized();
  368. }
  369. }
  370. bool ResizableWindow::isMinimised() const
  371. {
  372. if (ComponentPeer* const peer = getPeer())
  373. return peer->isMinimised();
  374. return false;
  375. }
  376. void ResizableWindow::setMinimised (const bool shouldMinimise)
  377. {
  378. if (shouldMinimise != isMinimised())
  379. {
  380. if (ComponentPeer* const peer = getPeer())
  381. {
  382. updateLastPos();
  383. peer->setMinimised (shouldMinimise);
  384. }
  385. else
  386. {
  387. jassertfalse;
  388. }
  389. }
  390. }
  391. void ResizableWindow::updateLastPos()
  392. {
  393. if (isShowing() && ! (isFullScreen() || isMinimised()))
  394. lastNonFullScreenPos = getBounds();
  395. }
  396. void ResizableWindow::parentSizeChanged()
  397. {
  398. if (isFullScreen() && getParentComponent() != nullptr)
  399. setBounds (getParentComponent()->getLocalBounds());
  400. }
  401. //==============================================================================
  402. String ResizableWindow::getWindowStateAsString()
  403. {
  404. updateLastPos();
  405. return (isFullScreen() ? "fs " : "") + lastNonFullScreenPos.toString();
  406. }
  407. bool ResizableWindow::restoreWindowStateFromString (const String& s)
  408. {
  409. StringArray tokens;
  410. tokens.addTokens (s, false);
  411. tokens.removeEmptyStrings();
  412. tokens.trim();
  413. const bool fs = tokens[0].startsWithIgnoreCase ("fs");
  414. const int firstCoord = fs ? 1 : 0;
  415. if (tokens.size() != firstCoord + 4)
  416. return false;
  417. Rectangle<int> newPos (tokens[firstCoord].getIntValue(),
  418. tokens[firstCoord + 1].getIntValue(),
  419. tokens[firstCoord + 2].getIntValue(),
  420. tokens[firstCoord + 3].getIntValue());
  421. if (newPos.isEmpty())
  422. return false;
  423. ComponentPeer* const peer = isOnDesktop() ? getPeer() : nullptr;
  424. if (peer != nullptr)
  425. peer->getFrameSize().addTo (newPos);
  426. {
  427. Desktop& desktop = Desktop::getInstance();
  428. RectangleList<int> allMonitors (desktop.getDisplays().getRectangleList (true));
  429. allMonitors.clipTo (newPos);
  430. const Rectangle<int> onScreenArea (allMonitors.getBounds());
  431. if (onScreenArea.getWidth() * onScreenArea.getHeight() < 32 * 32)
  432. {
  433. const Rectangle<int> screen (desktop.getDisplays().getDisplayContaining (newPos.getCentre()).userArea);
  434. newPos.setSize (jmin (newPos.getWidth(), screen.getWidth()),
  435. jmin (newPos.getHeight(), screen.getHeight()));
  436. newPos.setPosition (jlimit (screen.getX(), screen.getRight() - newPos.getWidth(), newPos.getX()),
  437. jlimit (screen.getY(), screen.getBottom() - newPos.getHeight(), newPos.getY()));
  438. }
  439. }
  440. if (peer != nullptr)
  441. {
  442. peer->getFrameSize().subtractFrom (newPos);
  443. peer->setNonFullScreenBounds (newPos);
  444. }
  445. lastNonFullScreenPos = newPos;
  446. setFullScreen (fs);
  447. if (! fs)
  448. setBoundsConstrained (newPos);
  449. return true;
  450. }
  451. //==============================================================================
  452. void ResizableWindow::mouseDown (const MouseEvent& e)
  453. {
  454. if (! isFullScreen())
  455. dragger.startDraggingComponent (this, e);
  456. }
  457. void ResizableWindow::mouseDrag (const MouseEvent& e)
  458. {
  459. if (! isFullScreen())
  460. dragger.dragComponent (this, e, constrainer);
  461. }
  462. //==============================================================================
  463. #if JUCE_DEBUG
  464. void ResizableWindow::addChildComponent (Component* const child, int zOrder)
  465. {
  466. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  467. manages its child components automatically, and if you add your own it'll cause
  468. trouble. Instead, use setContentComponent() to give it a component which
  469. will be automatically resized and kept in the right place - then you can add
  470. subcomponents to the content comp. See the notes for the ResizableWindow class
  471. for more info.
  472. If you really know what you're doing and want to avoid this assertion, just call
  473. Component::addChildComponent directly.
  474. */
  475. jassertfalse;
  476. Component::addChildComponent (child, zOrder);
  477. }
  478. void ResizableWindow::addAndMakeVisible (Component* const child, int zOrder)
  479. {
  480. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  481. manages its child components automatically, and if you add your own it'll cause
  482. trouble. Instead, use setContentComponent() to give it a component which
  483. will be automatically resized and kept in the right place - then you can add
  484. subcomponents to the content comp. See the notes for the ResizableWindow class
  485. for more info.
  486. If you really know what you're doing and want to avoid this assertion, just call
  487. Component::addAndMakeVisible directly.
  488. */
  489. jassertfalse;
  490. Component::addAndMakeVisible (child, zOrder);
  491. }
  492. #endif