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

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