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 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. auto stateString = (isFullScreen() && ! isKioskMode() ? "fs " : "") + lastNonFullScreenPos.toString();
  412. #if JUCE_LINUX
  413. if (auto* peer = isOnDesktop() ? getPeer() : nullptr)
  414. {
  415. if (const auto optionalFrameSize = peer->getFrameSizeIfPresent())
  416. {
  417. const auto& frameSize = *optionalFrameSize;
  418. stateString << " frame " << frameSize.getTop() << ' ' << frameSize.getLeft()
  419. << ' ' << frameSize.getBottom() << ' ' << frameSize.getRight();
  420. }
  421. }
  422. #endif
  423. return stateString;
  424. }
  425. bool ResizableWindow::restoreWindowStateFromString (const String& s)
  426. {
  427. StringArray tokens;
  428. tokens.addTokens (s, false);
  429. tokens.removeEmptyStrings();
  430. tokens.trim();
  431. const bool fs = tokens[0].startsWithIgnoreCase ("fs");
  432. const int firstCoord = fs ? 1 : 0;
  433. if (tokens.size() < firstCoord + 4)
  434. return false;
  435. Rectangle<int> newPos (tokens[firstCoord].getIntValue(),
  436. tokens[firstCoord + 1].getIntValue(),
  437. tokens[firstCoord + 2].getIntValue(),
  438. tokens[firstCoord + 3].getIntValue());
  439. if (newPos.isEmpty())
  440. return false;
  441. auto* peer = isOnDesktop() ? getPeer() : nullptr;
  442. if (peer != nullptr)
  443. {
  444. if (const auto frameSize = peer->getFrameSizeIfPresent())
  445. frameSize->addTo (newPos);
  446. }
  447. #if JUCE_LINUX
  448. if (peer == nullptr || ! peer->getFrameSizeIfPresent())
  449. {
  450. // We need to adjust for the frame size before we create a peer, as X11
  451. // doesn't provide this information at construction time.
  452. if (tokens[firstCoord + 4] == "frame" && tokens.size() == firstCoord + 9)
  453. {
  454. BorderSize<int> frame { tokens[firstCoord + 5].getIntValue(),
  455. tokens[firstCoord + 6].getIntValue(),
  456. tokens[firstCoord + 7].getIntValue(),
  457. tokens[firstCoord + 8].getIntValue() };
  458. newPos.setX (newPos.getX() - frame.getLeft());
  459. newPos.setY (newPos.getY() - frame.getTop());
  460. setBounds (newPos);
  461. }
  462. }
  463. #endif
  464. {
  465. auto& desktop = Desktop::getInstance();
  466. auto allMonitors = desktop.getDisplays().getRectangleList (true);
  467. allMonitors.clipTo (newPos);
  468. auto onScreenArea = allMonitors.getBounds();
  469. if (onScreenArea.getWidth() * onScreenArea.getHeight() < 32 * 32)
  470. {
  471. auto screen = desktop.getDisplays().getDisplayForRect (newPos)->userArea;
  472. newPos.setSize (jmin (newPos.getWidth(), screen.getWidth()),
  473. jmin (newPos.getHeight(), screen.getHeight()));
  474. newPos.setPosition (jlimit (screen.getX(), screen.getRight() - newPos.getWidth(), newPos.getX()),
  475. jlimit (screen.getY(), screen.getBottom() - newPos.getHeight(), newPos.getY()));
  476. }
  477. }
  478. if (peer != nullptr)
  479. {
  480. if (const auto frameSize = peer->getFrameSizeIfPresent())
  481. frameSize->subtractFrom (newPos);
  482. peer->setNonFullScreenBounds (newPos);
  483. }
  484. updateLastPosIfNotFullScreen();
  485. if (fs)
  486. setBoundsConstrained (newPos);
  487. setFullScreen (fs);
  488. if (! fs)
  489. setBoundsConstrained (newPos);
  490. return true;
  491. }
  492. //==============================================================================
  493. void ResizableWindow::mouseDown (const MouseEvent& e)
  494. {
  495. if (canDrag && ! isFullScreen())
  496. {
  497. dragStarted = true;
  498. dragger.startDraggingComponent (this, e);
  499. }
  500. }
  501. void ResizableWindow::mouseDrag (const MouseEvent& e)
  502. {
  503. if (dragStarted)
  504. dragger.dragComponent (this, e, constrainer);
  505. }
  506. void ResizableWindow::mouseUp (const MouseEvent&)
  507. {
  508. dragStarted = false;
  509. }
  510. //==============================================================================
  511. #if JUCE_DEBUG
  512. void ResizableWindow::addChildComponent (Component* const child, int zOrder)
  513. {
  514. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  515. manages its child components automatically, and if you add your own it'll cause
  516. trouble. Instead, use setContentComponent() to give it a component which
  517. will be automatically resized and kept in the right place - then you can add
  518. subcomponents to the content comp. See the notes for the ResizableWindow class
  519. for more info.
  520. If you really know what you're doing and want to avoid this assertion, just call
  521. Component::addChildComponent directly.
  522. */
  523. jassertfalse;
  524. Component::addChildComponent (child, zOrder);
  525. }
  526. void ResizableWindow::addAndMakeVisible (Component* const child, int zOrder)
  527. {
  528. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  529. manages its child components automatically, and if you add your own it'll cause
  530. trouble. Instead, use setContentComponent() to give it a component which
  531. will be automatically resized and kept in the right place - then you can add
  532. subcomponents to the content comp. See the notes for the ResizableWindow class
  533. for more info.
  534. If you really know what you're doing and want to avoid this assertion, just call
  535. Component::addAndMakeVisible directly.
  536. */
  537. jassertfalse;
  538. Component::addAndMakeVisible (child, zOrder);
  539. }
  540. #endif
  541. } // namespace juce