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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. if (isUsingNativeTitleBar() || isKioskMode())
  138. return BorderSize<int>();
  139. return BorderSize<int> ((resizableBorder != nullptr && ! isFullScreen()) ? 4 : 1);
  140. }
  141. BorderSize<int> ResizableWindow::getContentComponentBorder()
  142. {
  143. return getBorderThickness();
  144. }
  145. void ResizableWindow::moved()
  146. {
  147. updateLastPosIfShowing();
  148. }
  149. void ResizableWindow::visibilityChanged()
  150. {
  151. TopLevelWindow::visibilityChanged();
  152. updateLastPosIfShowing();
  153. }
  154. void ResizableWindow::resized()
  155. {
  156. const bool resizerHidden = isFullScreen() || isKioskMode() || isUsingNativeTitleBar();
  157. if (resizableBorder != nullptr)
  158. {
  159. resizableBorder->setVisible (! resizerHidden);
  160. resizableBorder->setBorderThickness (getBorderThickness());
  161. resizableBorder->setSize (getWidth(), getHeight());
  162. resizableBorder->toBack();
  163. }
  164. if (resizableCorner != nullptr)
  165. {
  166. resizableCorner->setVisible (! resizerHidden);
  167. const int resizerSize = 18;
  168. resizableCorner->setBounds (getWidth() - resizerSize,
  169. getHeight() - resizerSize,
  170. resizerSize, resizerSize);
  171. }
  172. if (contentComponent != nullptr)
  173. {
  174. // The window expects to be able to be able to manage the size and position
  175. // of its content component, so you can't arbitrarily add a transform to it!
  176. jassert (! contentComponent->isTransformed());
  177. contentComponent->setBoundsInset (getContentComponentBorder());
  178. }
  179. updateLastPosIfShowing();
  180. #if JUCE_DEBUG
  181. hasBeenResized = true;
  182. #endif
  183. }
  184. void ResizableWindow::childBoundsChanged (Component* child)
  185. {
  186. if ((child == contentComponent) && (child != nullptr) && resizeToFitContent)
  187. {
  188. // not going to look very good if this component has a zero size..
  189. jassert (child->getWidth() > 0);
  190. jassert (child->getHeight() > 0);
  191. const BorderSize<int> borders (getContentComponentBorder());
  192. setSize (child->getWidth() + borders.getLeftAndRight(),
  193. child->getHeight() + borders.getTopAndBottom());
  194. }
  195. }
  196. //==============================================================================
  197. void ResizableWindow::activeWindowStatusChanged()
  198. {
  199. const BorderSize<int> border (getContentComponentBorder());
  200. Rectangle<int> area (getLocalBounds());
  201. repaint (area.removeFromTop (border.getTop()));
  202. repaint (area.removeFromLeft (border.getLeft()));
  203. repaint (area.removeFromRight (border.getRight()));
  204. repaint (area.removeFromBottom (border.getBottom()));
  205. }
  206. //==============================================================================
  207. void ResizableWindow::setResizable (const bool shouldBeResizable,
  208. const bool useBottomRightCornerResizer)
  209. {
  210. if (shouldBeResizable)
  211. {
  212. if (useBottomRightCornerResizer)
  213. {
  214. resizableBorder = nullptr;
  215. if (resizableCorner == nullptr)
  216. {
  217. Component::addChildComponent (resizableCorner = new ResizableCornerComponent (this, constrainer));
  218. resizableCorner->setAlwaysOnTop (true);
  219. }
  220. }
  221. else
  222. {
  223. resizableCorner = nullptr;
  224. if (resizableBorder == nullptr)
  225. Component::addChildComponent (resizableBorder = new ResizableBorderComponent (this, constrainer));
  226. }
  227. }
  228. else
  229. {
  230. resizableCorner = nullptr;
  231. resizableBorder = nullptr;
  232. }
  233. if (isUsingNativeTitleBar())
  234. recreateDesktopWindow();
  235. childBoundsChanged (contentComponent);
  236. resized();
  237. }
  238. bool ResizableWindow::isResizable() const noexcept
  239. {
  240. return resizableCorner != nullptr
  241. || resizableBorder != nullptr;
  242. }
  243. void ResizableWindow::setResizeLimits (const int newMinimumWidth,
  244. const int newMinimumHeight,
  245. const int newMaximumWidth,
  246. const int newMaximumHeight) noexcept
  247. {
  248. // if you've set up a custom constrainer then these settings won't have any effect..
  249. jassert (constrainer == &defaultConstrainer || constrainer == nullptr);
  250. if (constrainer == nullptr)
  251. setConstrainer (&defaultConstrainer);
  252. defaultConstrainer.setSizeLimits (newMinimumWidth, newMinimumHeight,
  253. newMaximumWidth, newMaximumHeight);
  254. setBoundsConstrained (getBounds());
  255. }
  256. void ResizableWindow::setConstrainer (ComponentBoundsConstrainer* newConstrainer)
  257. {
  258. if (constrainer != newConstrainer)
  259. {
  260. constrainer = newConstrainer;
  261. const bool useBottomRightCornerResizer = resizableCorner != nullptr;
  262. const bool shouldBeResizable = useBottomRightCornerResizer || resizableBorder != nullptr;
  263. resizableCorner = nullptr;
  264. resizableBorder = nullptr;
  265. setResizable (shouldBeResizable, useBottomRightCornerResizer);
  266. if (ComponentPeer* const peer = getPeer())
  267. peer->setConstrainer (newConstrainer);
  268. }
  269. }
  270. void ResizableWindow::setBoundsConstrained (const Rectangle<int>& newBounds)
  271. {
  272. if (constrainer != nullptr)
  273. constrainer->setBoundsForComponent (this, newBounds, false, false, false, false);
  274. else
  275. setBounds (newBounds);
  276. }
  277. //==============================================================================
  278. void ResizableWindow::paint (Graphics& g)
  279. {
  280. LookAndFeel& lf = getLookAndFeel();
  281. lf.fillResizableWindowBackground (g, getWidth(), getHeight(),
  282. getBorderThickness(), *this);
  283. if (! isFullScreen())
  284. lf.drawResizableWindowBorder (g, getWidth(), getHeight(),
  285. getBorderThickness(), *this);
  286. #if JUCE_DEBUG
  287. /* If this fails, then you've probably written a subclass with a resized()
  288. callback but forgotten to make it call its parent class's resized() method.
  289. It's important when you override methods like resized(), moved(),
  290. etc., that you make sure the base class methods also get called.
  291. Of course you shouldn't really be overriding ResizableWindow::resized() anyway,
  292. because your content should all be inside the content component - and it's the
  293. content component's resized() method that you should be using to do your
  294. layout.
  295. */
  296. jassert (hasBeenResized || (getWidth() == 0 && getHeight() == 0));
  297. #endif
  298. }
  299. void ResizableWindow::lookAndFeelChanged()
  300. {
  301. resized();
  302. if (isOnDesktop())
  303. {
  304. Component::addToDesktop (getDesktopWindowStyleFlags());
  305. if (ComponentPeer* const peer = getPeer())
  306. peer->setConstrainer (constrainer);
  307. }
  308. }
  309. Colour ResizableWindow::getBackgroundColour() const noexcept
  310. {
  311. return findColour (backgroundColourId, false);
  312. }
  313. void ResizableWindow::setBackgroundColour (Colour newColour)
  314. {
  315. Colour backgroundColour (newColour);
  316. if (! Desktop::canUseSemiTransparentWindows())
  317. backgroundColour = newColour.withAlpha (1.0f);
  318. setColour (backgroundColourId, backgroundColour);
  319. setOpaque (backgroundColour.isOpaque());
  320. repaint();
  321. }
  322. //==============================================================================
  323. bool ResizableWindow::isFullScreen() const
  324. {
  325. if (isOnDesktop())
  326. {
  327. ComponentPeer* const peer = getPeer();
  328. return peer != nullptr && peer->isFullScreen();
  329. }
  330. return fullscreen;
  331. }
  332. void ResizableWindow::setFullScreen (const bool shouldBeFullScreen)
  333. {
  334. if (shouldBeFullScreen != isFullScreen())
  335. {
  336. updateLastPosIfShowing();
  337. fullscreen = shouldBeFullScreen;
  338. if (isOnDesktop())
  339. {
  340. if (ComponentPeer* const peer = getPeer())
  341. {
  342. // keep a copy of this intact in case the real one gets messed-up while we're un-maximising
  343. const Rectangle<int> lastPos (lastNonFullScreenPos);
  344. peer->setFullScreen (shouldBeFullScreen);
  345. if ((! shouldBeFullScreen) && ! lastPos.isEmpty())
  346. setBounds (lastPos);
  347. }
  348. else
  349. {
  350. jassertfalse;
  351. }
  352. }
  353. else
  354. {
  355. if (shouldBeFullScreen)
  356. setBounds (0, 0, getParentWidth(), getParentHeight());
  357. else
  358. setBounds (lastNonFullScreenPos);
  359. }
  360. resized();
  361. }
  362. }
  363. bool ResizableWindow::isMinimised() const
  364. {
  365. if (ComponentPeer* const peer = getPeer())
  366. return peer->isMinimised();
  367. return false;
  368. }
  369. void ResizableWindow::setMinimised (const bool shouldMinimise)
  370. {
  371. if (shouldMinimise != isMinimised())
  372. {
  373. if (ComponentPeer* const peer = getPeer())
  374. {
  375. updateLastPosIfShowing();
  376. peer->setMinimised (shouldMinimise);
  377. }
  378. else
  379. {
  380. jassertfalse;
  381. }
  382. }
  383. }
  384. bool ResizableWindow::isKioskMode() const
  385. {
  386. if (isOnDesktop())
  387. if (ComponentPeer* peer = getPeer())
  388. return peer->isKioskMode();
  389. return Desktop::getInstance().getKioskModeComponent() == this;
  390. }
  391. void ResizableWindow::updateLastPosIfShowing()
  392. {
  393. if (isShowing())
  394. updateLastPosIfNotFullScreen();
  395. }
  396. void ResizableWindow::updateLastPosIfNotFullScreen()
  397. {
  398. if (! (isFullScreen() || isMinimised() || isKioskMode()))
  399. lastNonFullScreenPos = getBounds();
  400. }
  401. void ResizableWindow::parentSizeChanged()
  402. {
  403. if (isFullScreen() && getParentComponent() != nullptr)
  404. setBounds (getParentComponent()->getLocalBounds());
  405. }
  406. //==============================================================================
  407. String ResizableWindow::getWindowStateAsString()
  408. {
  409. updateLastPosIfShowing();
  410. return (isFullScreen() && ! isKioskMode() ? "fs " : "") + lastNonFullScreenPos.toString();
  411. }
  412. bool ResizableWindow::restoreWindowStateFromString (const String& s)
  413. {
  414. StringArray tokens;
  415. tokens.addTokens (s, false);
  416. tokens.removeEmptyStrings();
  417. tokens.trim();
  418. const bool fs = tokens[0].startsWithIgnoreCase ("fs");
  419. const int firstCoord = fs ? 1 : 0;
  420. if (tokens.size() != firstCoord + 4)
  421. return false;
  422. Rectangle<int> newPos (tokens[firstCoord].getIntValue(),
  423. tokens[firstCoord + 1].getIntValue(),
  424. tokens[firstCoord + 2].getIntValue(),
  425. tokens[firstCoord + 3].getIntValue());
  426. if (newPos.isEmpty())
  427. return false;
  428. ComponentPeer* const peer = isOnDesktop() ? getPeer() : nullptr;
  429. if (peer != nullptr)
  430. peer->getFrameSize().addTo (newPos);
  431. {
  432. Desktop& desktop = Desktop::getInstance();
  433. RectangleList<int> allMonitors (desktop.getDisplays().getRectangleList (true));
  434. allMonitors.clipTo (newPos);
  435. const Rectangle<int> onScreenArea (allMonitors.getBounds());
  436. if (onScreenArea.getWidth() * onScreenArea.getHeight() < 32 * 32)
  437. {
  438. const Rectangle<int> screen (desktop.getDisplays().getDisplayContaining (newPos.getCentre()).userArea);
  439. newPos.setSize (jmin (newPos.getWidth(), screen.getWidth()),
  440. jmin (newPos.getHeight(), screen.getHeight()));
  441. newPos.setPosition (jlimit (screen.getX(), screen.getRight() - newPos.getWidth(), newPos.getX()),
  442. jlimit (screen.getY(), screen.getBottom() - newPos.getHeight(), newPos.getY()));
  443. }
  444. }
  445. if (peer != nullptr)
  446. {
  447. peer->getFrameSize().subtractFrom (newPos);
  448. peer->setNonFullScreenBounds (newPos);
  449. }
  450. updateLastPosIfNotFullScreen();
  451. setFullScreen (fs);
  452. if (! fs)
  453. setBoundsConstrained (newPos);
  454. return true;
  455. }
  456. //==============================================================================
  457. void ResizableWindow::mouseDown (const MouseEvent& e)
  458. {
  459. if (! isFullScreen())
  460. dragger.startDraggingComponent (this, e);
  461. }
  462. void ResizableWindow::mouseDrag (const MouseEvent& e)
  463. {
  464. if (! isFullScreen())
  465. dragger.dragComponent (this, e, constrainer);
  466. }
  467. //==============================================================================
  468. #if JUCE_DEBUG
  469. void ResizableWindow::addChildComponent (Component* const child, int zOrder)
  470. {
  471. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  472. manages its child components automatically, and if you add your own it'll cause
  473. trouble. Instead, use setContentComponent() to give it a component which
  474. will be automatically resized and kept in the right place - then you can add
  475. subcomponents to the content comp. See the notes for the ResizableWindow class
  476. for more info.
  477. If you really know what you're doing and want to avoid this assertion, just call
  478. Component::addChildComponent directly.
  479. */
  480. jassertfalse;
  481. Component::addChildComponent (child, zOrder);
  482. }
  483. void ResizableWindow::addAndMakeVisible (Component* const child, int zOrder)
  484. {
  485. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  486. manages its child components automatically, and if you add your own it'll cause
  487. trouble. Instead, use setContentComponent() to give it a component which
  488. will be automatically resized and kept in the right place - then you can add
  489. subcomponents to the content comp. See the notes for the ResizableWindow class
  490. for more info.
  491. If you really know what you're doing and want to avoid this assertion, just call
  492. Component::addAndMakeVisible directly.
  493. */
  494. jassertfalse;
  495. Component::addAndMakeVisible (child, zOrder);
  496. }
  497. #endif