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.

626 lines
19KB

  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. // Don't delete or remove the resizer components yourself! They're managed by the
  33. // ResizableWindow, and you should leave them alone! You may have deleted them
  34. // accidentally by careless use of deleteAllChildren()..?
  35. jassert (resizableCorner == nullptr || getIndexOfChildComponent (resizableCorner) >= 0);
  36. jassert (resizableBorder == nullptr || getIndexOfChildComponent (resizableBorder) >= 0);
  37. resizableCorner = nullptr;
  38. resizableBorder = nullptr;
  39. clearContentComponent();
  40. // have you been adding your own components directly to this window..? tut tut tut.
  41. // Read the instructions for using a ResizableWindow!
  42. jassert (getNumChildComponents() == 0);
  43. }
  44. void ResizableWindow::initialise (const bool shouldAddToDesktop)
  45. {
  46. defaultConstrainer.setMinimumOnscreenAmounts (0x10000, 16, 24, 16);
  47. lastNonFullScreenPos.setBounds (50, 50, 256, 256);
  48. if (shouldAddToDesktop)
  49. addToDesktop();
  50. }
  51. int ResizableWindow::getDesktopWindowStyleFlags() const
  52. {
  53. int styleFlags = TopLevelWindow::getDesktopWindowStyleFlags();
  54. if (isResizable() && (styleFlags & ComponentPeer::windowHasTitleBar) != 0)
  55. styleFlags |= ComponentPeer::windowIsResizable;
  56. return styleFlags;
  57. }
  58. //==============================================================================
  59. void ResizableWindow::clearContentComponent()
  60. {
  61. if (ownsContentComponent)
  62. {
  63. contentComponent.deleteAndZero();
  64. }
  65. else
  66. {
  67. removeChildComponent (contentComponent);
  68. contentComponent = nullptr;
  69. }
  70. }
  71. void ResizableWindow::setContent (Component* newContentComponent,
  72. const bool takeOwnership,
  73. const bool resizeToFitWhenContentChangesSize)
  74. {
  75. if (newContentComponent != contentComponent)
  76. {
  77. clearContentComponent();
  78. contentComponent = newContentComponent;
  79. Component::addAndMakeVisible (contentComponent);
  80. }
  81. ownsContentComponent = takeOwnership;
  82. resizeToFitContent = resizeToFitWhenContentChangesSize;
  83. if (resizeToFitWhenContentChangesSize)
  84. childBoundsChanged (contentComponent);
  85. resized(); // must always be called to position the new content comp
  86. }
  87. void ResizableWindow::setContentOwned (Component* newContentComponent, const bool resizeToFitWhenContentChangesSize)
  88. {
  89. setContent (newContentComponent, true, resizeToFitWhenContentChangesSize);
  90. }
  91. void ResizableWindow::setContentNonOwned (Component* newContentComponent, const bool resizeToFitWhenContentChangesSize)
  92. {
  93. setContent (newContentComponent, false, resizeToFitWhenContentChangesSize);
  94. }
  95. void ResizableWindow::setContentComponent (Component* const newContentComponent,
  96. const bool deleteOldOne,
  97. const bool resizeToFitWhenContentChangesSize)
  98. {
  99. if (newContentComponent != contentComponent)
  100. {
  101. if (deleteOldOne)
  102. {
  103. contentComponent.deleteAndZero();
  104. }
  105. else
  106. {
  107. removeChildComponent (contentComponent);
  108. contentComponent = nullptr;
  109. }
  110. }
  111. setContent (newContentComponent, true, resizeToFitWhenContentChangesSize);
  112. }
  113. void ResizableWindow::setContentComponentSize (int width, int height)
  114. {
  115. jassert (width > 0 && height > 0); // not a great idea to give it a zero size..
  116. const BorderSize<int> border (getContentComponentBorder());
  117. setSize (width + border.getLeftAndRight(),
  118. height + border.getTopAndBottom());
  119. }
  120. BorderSize<int> ResizableWindow::getBorderThickness()
  121. {
  122. if (isUsingNativeTitleBar() || isKioskMode())
  123. return BorderSize<int>();
  124. return BorderSize<int> ((resizableBorder != nullptr && ! isFullScreen()) ? 4 : 1);
  125. }
  126. BorderSize<int> ResizableWindow::getContentComponentBorder()
  127. {
  128. return getBorderThickness();
  129. }
  130. void ResizableWindow::moved()
  131. {
  132. updateLastPosIfShowing();
  133. }
  134. void ResizableWindow::visibilityChanged()
  135. {
  136. TopLevelWindow::visibilityChanged();
  137. updateLastPosIfShowing();
  138. }
  139. void ResizableWindow::resized()
  140. {
  141. const bool resizerHidden = isFullScreen() || isKioskMode() || isUsingNativeTitleBar();
  142. if (resizableBorder != nullptr)
  143. {
  144. resizableBorder->setVisible (! resizerHidden);
  145. resizableBorder->setBorderThickness (getBorderThickness());
  146. resizableBorder->setSize (getWidth(), getHeight());
  147. resizableBorder->toBack();
  148. }
  149. if (resizableCorner != nullptr)
  150. {
  151. resizableCorner->setVisible (! resizerHidden);
  152. const int resizerSize = 18;
  153. resizableCorner->setBounds (getWidth() - resizerSize,
  154. getHeight() - resizerSize,
  155. resizerSize, resizerSize);
  156. }
  157. if (contentComponent != nullptr)
  158. {
  159. // The window expects to be able to be able to manage the size and position
  160. // of its content component, so you can't arbitrarily add a transform to it!
  161. jassert (! contentComponent->isTransformed());
  162. contentComponent->setBoundsInset (getContentComponentBorder());
  163. }
  164. updateLastPosIfShowing();
  165. #if JUCE_DEBUG
  166. hasBeenResized = true;
  167. #endif
  168. }
  169. void ResizableWindow::childBoundsChanged (Component* child)
  170. {
  171. if ((child == contentComponent) && (child != nullptr) && resizeToFitContent)
  172. {
  173. // not going to look very good if this component has a zero size..
  174. jassert (child->getWidth() > 0);
  175. jassert (child->getHeight() > 0);
  176. auto borders = getContentComponentBorder();
  177. setSize (child->getWidth() + borders.getLeftAndRight(),
  178. child->getHeight() + borders.getTopAndBottom());
  179. }
  180. }
  181. //==============================================================================
  182. void ResizableWindow::activeWindowStatusChanged()
  183. {
  184. const BorderSize<int> border (getContentComponentBorder());
  185. Rectangle<int> area (getLocalBounds());
  186. repaint (area.removeFromTop (border.getTop()));
  187. repaint (area.removeFromLeft (border.getLeft()));
  188. repaint (area.removeFromRight (border.getRight()));
  189. repaint (area.removeFromBottom (border.getBottom()));
  190. }
  191. //==============================================================================
  192. void ResizableWindow::setResizable (const bool shouldBeResizable,
  193. const bool useBottomRightCornerResizer)
  194. {
  195. if (shouldBeResizable)
  196. {
  197. if (useBottomRightCornerResizer)
  198. {
  199. resizableBorder = nullptr;
  200. if (resizableCorner == nullptr)
  201. {
  202. Component::addChildComponent (resizableCorner = new ResizableCornerComponent (this, constrainer));
  203. resizableCorner->setAlwaysOnTop (true);
  204. }
  205. }
  206. else
  207. {
  208. resizableCorner = nullptr;
  209. if (resizableBorder == nullptr)
  210. Component::addChildComponent (resizableBorder = new ResizableBorderComponent (this, constrainer));
  211. }
  212. }
  213. else
  214. {
  215. resizableCorner = nullptr;
  216. resizableBorder = nullptr;
  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 (const int newMinimumWidth,
  229. const int newMinimumHeight,
  230. const int newMaximumWidth,
  231. const 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. const bool useBottomRightCornerResizer = resizableCorner != nullptr;
  251. const bool shouldBeResizable = useBottomRightCornerResizer || resizableBorder != nullptr;
  252. resizableCorner = nullptr;
  253. resizableBorder = nullptr;
  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. LookAndFeel& 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. Colour 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. ComponentPeer* const 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 (ComponentPeer* const peer = getPeer())
  328. {
  329. // keep a copy of this intact in case the real one gets messed-up while we're un-maximising
  330. const Rectangle<int> 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 (ComponentPeer* const 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 (ComponentPeer* const 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 (ComponentPeer* 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 (ComponentPeer* const 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. ComponentPeer* const peer = isOnDesktop() ? getPeer() : nullptr;
  425. if (peer != nullptr)
  426. peer->getFrameSize().addTo (newPos);
  427. {
  428. Desktop& desktop = Desktop::getInstance();
  429. RectangleList<int> allMonitors (desktop.getDisplays().getRectangleList (true));
  430. allMonitors.clipTo (newPos);
  431. const Rectangle<int> onScreenArea (allMonitors.getBounds());
  432. if (onScreenArea.getWidth() * onScreenArea.getHeight() < 32 * 32)
  433. {
  434. const Rectangle<int> screen (desktop.getDisplays().getDisplayContaining (newPos.getCentre()).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