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.

625 lines
19KB

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