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.

648 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  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 5 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-5-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. 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. auto* peer = isOnDesktop() ? getPeer() : nullptr;
  440. if (peer != nullptr)
  441. peer->getFrameSize().addTo (newPos);
  442. {
  443. auto& desktop = Desktop::getInstance();
  444. auto allMonitors = desktop.getDisplays().getRectangleList (true);
  445. allMonitors.clipTo (newPos);
  446. auto onScreenArea = allMonitors.getBounds();
  447. if (onScreenArea.getWidth() * onScreenArea.getHeight() < 32 * 32)
  448. {
  449. auto screen = desktop.getDisplays().findDisplayForRect (newPos).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
  518. } // namespace juce