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.

647 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. ResizableWindow::ResizableWindow (const String& name, bool shouldAddToDesktop)
  20. : TopLevelWindow (name, shouldAddToDesktop)
  21. {
  22. initialise (shouldAddToDesktop);
  23. }
  24. ResizableWindow::ResizableWindow (const String& name, Colour bkgnd, bool shouldAddToDesktop)
  25. : TopLevelWindow (name, shouldAddToDesktop)
  26. {
  27. setBackgroundColour (bkgnd);
  28. initialise (shouldAddToDesktop);
  29. }
  30. ResizableWindow::~ResizableWindow()
  31. {
  32. splashScreen.deleteAndZero();
  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) >= 0);
  37. jassert (resizableBorder == nullptr || getIndexOfChildComponent (resizableBorder) >= 0);
  38. resizableCorner = nullptr;
  39. resizableBorder = nullptr;
  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. /*
  48. ==========================================================================
  49. In accordance with the terms of the JUCE 5 End-Use License Agreement, the
  50. JUCE Code in SECTION A cannot be removed, changed or otherwise rendered
  51. ineffective unless you have a JUCE Indie or Pro license, or are using
  52. JUCE under the GPL v3 license.
  53. End User License Agreement: www.juce.com/juce-5-licence
  54. ==========================================================================
  55. */
  56. // BEGIN SECTION A
  57. #if ! JucePlugin_Build_Standalone
  58. splashScreen = new JUCESplashScreen (*this);
  59. #endif
  60. // END SECTION A
  61. defaultConstrainer.setMinimumOnscreenAmounts (0x10000, 16, 24, 16);
  62. lastNonFullScreenPos.setBounds (50, 50, 256, 256);
  63. if (shouldAddToDesktop)
  64. addToDesktop();
  65. }
  66. int ResizableWindow::getDesktopWindowStyleFlags() const
  67. {
  68. int styleFlags = TopLevelWindow::getDesktopWindowStyleFlags();
  69. if (isResizable() && (styleFlags & ComponentPeer::windowHasTitleBar) != 0)
  70. styleFlags |= ComponentPeer::windowIsResizable;
  71. return styleFlags;
  72. }
  73. //==============================================================================
  74. void ResizableWindow::clearContentComponent()
  75. {
  76. if (ownsContentComponent)
  77. {
  78. contentComponent.deleteAndZero();
  79. }
  80. else
  81. {
  82. removeChildComponent (contentComponent);
  83. contentComponent = nullptr;
  84. }
  85. }
  86. void ResizableWindow::setContent (Component* newContentComponent,
  87. const bool takeOwnership,
  88. const bool resizeToFitWhenContentChangesSize)
  89. {
  90. if (newContentComponent != contentComponent)
  91. {
  92. clearContentComponent();
  93. contentComponent = newContentComponent;
  94. Component::addAndMakeVisible (contentComponent);
  95. }
  96. ownsContentComponent = takeOwnership;
  97. resizeToFitContent = resizeToFitWhenContentChangesSize;
  98. if (resizeToFitWhenContentChangesSize)
  99. childBoundsChanged (contentComponent);
  100. resized(); // must always be called to position the new content comp
  101. }
  102. void ResizableWindow::setContentOwned (Component* newContentComponent, const bool resizeToFitWhenContentChangesSize)
  103. {
  104. setContent (newContentComponent, true, resizeToFitWhenContentChangesSize);
  105. }
  106. void ResizableWindow::setContentNonOwned (Component* newContentComponent, const bool resizeToFitWhenContentChangesSize)
  107. {
  108. setContent (newContentComponent, false, resizeToFitWhenContentChangesSize);
  109. }
  110. void ResizableWindow::setContentComponent (Component* const newContentComponent,
  111. const bool deleteOldOne,
  112. const bool resizeToFitWhenContentChangesSize)
  113. {
  114. if (newContentComponent != contentComponent)
  115. {
  116. if (deleteOldOne)
  117. {
  118. contentComponent.deleteAndZero();
  119. }
  120. else
  121. {
  122. removeChildComponent (contentComponent);
  123. contentComponent = nullptr;
  124. }
  125. }
  126. setContent (newContentComponent, true, resizeToFitWhenContentChangesSize);
  127. }
  128. void ResizableWindow::setContentComponentSize (int width, int height)
  129. {
  130. jassert (width > 0 && height > 0); // not a great idea to give it a zero size..
  131. const BorderSize<int> border (getContentComponentBorder());
  132. setSize (width + border.getLeftAndRight(),
  133. height + border.getTopAndBottom());
  134. }
  135. BorderSize<int> ResizableWindow::getBorderThickness()
  136. {
  137. if (isUsingNativeTitleBar() || isKioskMode())
  138. return BorderSize<int>();
  139. return BorderSize<int> ((resizableBorder != nullptr && ! isFullScreen()) ? 4 : 1);
  140. }
  141. BorderSize<int> ResizableWindow::getContentComponentBorder()
  142. {
  143. return getBorderThickness();
  144. }
  145. void ResizableWindow::moved()
  146. {
  147. updateLastPosIfShowing();
  148. }
  149. void ResizableWindow::visibilityChanged()
  150. {
  151. TopLevelWindow::visibilityChanged();
  152. updateLastPosIfShowing();
  153. }
  154. void ResizableWindow::resized()
  155. {
  156. const bool resizerHidden = isFullScreen() || isKioskMode() || isUsingNativeTitleBar();
  157. if (resizableBorder != nullptr)
  158. {
  159. resizableBorder->setVisible (! resizerHidden);
  160. resizableBorder->setBorderThickness (getBorderThickness());
  161. resizableBorder->setSize (getWidth(), getHeight());
  162. resizableBorder->toBack();
  163. }
  164. if (resizableCorner != nullptr)
  165. {
  166. resizableCorner->setVisible (! resizerHidden);
  167. const int resizerSize = 18;
  168. resizableCorner->setBounds (getWidth() - resizerSize,
  169. getHeight() - resizerSize,
  170. resizerSize, resizerSize);
  171. }
  172. if (contentComponent != nullptr)
  173. {
  174. // The window expects to be able to be able to manage the size and position
  175. // of its content component, so you can't arbitrarily add a transform to it!
  176. jassert (! contentComponent->isTransformed());
  177. contentComponent->setBoundsInset (getContentComponentBorder());
  178. }
  179. updateLastPosIfShowing();
  180. #if JUCE_DEBUG
  181. hasBeenResized = true;
  182. #endif
  183. }
  184. void ResizableWindow::childBoundsChanged (Component* child)
  185. {
  186. if ((child == contentComponent) && (child != nullptr) && resizeToFitContent)
  187. {
  188. // not going to look very good if this component has a zero size..
  189. jassert (child->getWidth() > 0);
  190. jassert (child->getHeight() > 0);
  191. auto borders = getContentComponentBorder();
  192. setSize (child->getWidth() + borders.getLeftAndRight(),
  193. child->getHeight() + borders.getTopAndBottom());
  194. }
  195. }
  196. //==============================================================================
  197. void ResizableWindow::activeWindowStatusChanged()
  198. {
  199. const BorderSize<int> border (getContentComponentBorder());
  200. Rectangle<int> area (getLocalBounds());
  201. repaint (area.removeFromTop (border.getTop()));
  202. repaint (area.removeFromLeft (border.getLeft()));
  203. repaint (area.removeFromRight (border.getRight()));
  204. repaint (area.removeFromBottom (border.getBottom()));
  205. }
  206. //==============================================================================
  207. void ResizableWindow::setResizable (const bool shouldBeResizable,
  208. const bool useBottomRightCornerResizer)
  209. {
  210. if (shouldBeResizable)
  211. {
  212. if (useBottomRightCornerResizer)
  213. {
  214. resizableBorder = nullptr;
  215. if (resizableCorner == nullptr)
  216. {
  217. Component::addChildComponent (resizableCorner = new ResizableCornerComponent (this, constrainer));
  218. resizableCorner->setAlwaysOnTop (true);
  219. }
  220. }
  221. else
  222. {
  223. resizableCorner = nullptr;
  224. if (resizableBorder == nullptr)
  225. Component::addChildComponent (resizableBorder = new ResizableBorderComponent (this, constrainer));
  226. }
  227. }
  228. else
  229. {
  230. resizableCorner = nullptr;
  231. resizableBorder = nullptr;
  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 (const int newMinimumWidth,
  244. const int newMinimumHeight,
  245. const int newMaximumWidth,
  246. const 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. const bool useBottomRightCornerResizer = resizableCorner != nullptr;
  266. const bool shouldBeResizable = useBottomRightCornerResizer || resizableBorder != nullptr;
  267. resizableCorner = nullptr;
  268. resizableBorder = nullptr;
  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. LookAndFeel& 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. Colour 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. ComponentPeer* const 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 (ComponentPeer* const peer = getPeer())
  343. {
  344. // keep a copy of this intact in case the real one gets messed-up while we're un-maximising
  345. const Rectangle<int> 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 (ComponentPeer* const 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 (ComponentPeer* const 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 (ComponentPeer* 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 (ComponentPeer* const 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. return (isFullScreen() && ! isKioskMode() ? "fs " : "") + lastNonFullScreenPos.toString();
  422. }
  423. bool ResizableWindow::restoreWindowStateFromString (const String& s)
  424. {
  425. StringArray tokens;
  426. tokens.addTokens (s, false);
  427. tokens.removeEmptyStrings();
  428. tokens.trim();
  429. const bool fs = tokens[0].startsWithIgnoreCase ("fs");
  430. const int firstCoord = fs ? 1 : 0;
  431. if (tokens.size() != firstCoord + 4)
  432. return false;
  433. Rectangle<int> newPos (tokens[firstCoord].getIntValue(),
  434. tokens[firstCoord + 1].getIntValue(),
  435. tokens[firstCoord + 2].getIntValue(),
  436. tokens[firstCoord + 3].getIntValue());
  437. if (newPos.isEmpty())
  438. return false;
  439. ComponentPeer* const peer = isOnDesktop() ? getPeer() : nullptr;
  440. if (peer != nullptr)
  441. peer->getFrameSize().addTo (newPos);
  442. {
  443. Desktop& desktop = Desktop::getInstance();
  444. RectangleList<int> allMonitors (desktop.getDisplays().getRectangleList (true));
  445. allMonitors.clipTo (newPos);
  446. const Rectangle<int> onScreenArea (allMonitors.getBounds());
  447. if (onScreenArea.getWidth() * onScreenArea.getHeight() < 32 * 32)
  448. {
  449. const Rectangle<int> screen (desktop.getDisplays().getDisplayContaining (newPos.getCentre()).userArea);
  450. newPos.setSize (jmin (newPos.getWidth(), screen.getWidth()),
  451. jmin (newPos.getHeight(), screen.getHeight()));
  452. newPos.setPosition (jlimit (screen.getX(), screen.getRight() - newPos.getWidth(), newPos.getX()),
  453. jlimit (screen.getY(), screen.getBottom() - newPos.getHeight(), newPos.getY()));
  454. }
  455. }
  456. if (peer != nullptr)
  457. {
  458. peer->getFrameSize().subtractFrom (newPos);
  459. peer->setNonFullScreenBounds (newPos);
  460. }
  461. updateLastPosIfNotFullScreen();
  462. if (fs)
  463. setBoundsConstrained (newPos);
  464. setFullScreen (fs);
  465. if (! fs)
  466. setBoundsConstrained (newPos);
  467. return true;
  468. }
  469. //==============================================================================
  470. void ResizableWindow::mouseDown (const MouseEvent& e)
  471. {
  472. if (canDrag && ! isFullScreen())
  473. {
  474. dragStarted = true;
  475. dragger.startDraggingComponent (this, e);
  476. }
  477. }
  478. void ResizableWindow::mouseDrag (const MouseEvent& e)
  479. {
  480. if (dragStarted)
  481. dragger.dragComponent (this, e, constrainer);
  482. }
  483. void ResizableWindow::mouseUp (const MouseEvent&)
  484. {
  485. dragStarted = false;
  486. }
  487. //==============================================================================
  488. #if JUCE_DEBUG
  489. void ResizableWindow::addChildComponent (Component* const child, int zOrder)
  490. {
  491. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  492. manages its child components automatically, and if you add your own it'll cause
  493. trouble. Instead, use setContentComponent() to give it a component which
  494. will be automatically resized and kept in the right place - then you can add
  495. subcomponents to the content comp. See the notes for the ResizableWindow class
  496. for more info.
  497. If you really know what you're doing and want to avoid this assertion, just call
  498. Component::addChildComponent directly.
  499. */
  500. jassertfalse;
  501. Component::addChildComponent (child, zOrder);
  502. }
  503. void ResizableWindow::addAndMakeVisible (Component* const child, int zOrder)
  504. {
  505. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  506. manages its child components automatically, and if you add your own it'll cause
  507. trouble. Instead, use setContentComponent() to give it a component which
  508. will be automatically resized and kept in the right place - then you can add
  509. subcomponents to the content comp. See the notes for the ResizableWindow class
  510. for more info.
  511. If you really know what you're doing and want to avoid this assertion, just call
  512. Component::addAndMakeVisible directly.
  513. */
  514. jassertfalse;
  515. Component::addAndMakeVisible (child, zOrder);
  516. }
  517. #endif