The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

687 lines
21KB

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