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.

juce_ResizableWindow.cpp 20KB

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