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

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