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.

656 lines
20KB

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