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.

685 lines
21KB

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