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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. if (ComponentPeer* const peer = getPeer())
  272. peer->setConstrainer (newConstrainer);
  273. }
  274. }
  275. void ResizableWindow::setBoundsConstrained (const Rectangle<int>& newBounds)
  276. {
  277. if (constrainer != nullptr)
  278. constrainer->setBoundsForComponent (this, newBounds, false, false, false, false);
  279. else
  280. setBounds (newBounds);
  281. }
  282. //==============================================================================
  283. void ResizableWindow::paint (Graphics& g)
  284. {
  285. LookAndFeel& lf = getLookAndFeel();
  286. lf.fillResizableWindowBackground (g, getWidth(), getHeight(),
  287. getBorderThickness(), *this);
  288. if (! isFullScreen())
  289. lf.drawResizableWindowBorder (g, getWidth(), getHeight(),
  290. getBorderThickness(), *this);
  291. #if JUCE_DEBUG
  292. /* If this fails, then you've probably written a subclass with a resized()
  293. callback but forgotten to make it call its parent class's resized() method.
  294. It's important when you override methods like resized(), moved(),
  295. etc., that you make sure the base class methods also get called.
  296. Of course you shouldn't really be overriding ResizableWindow::resized() anyway,
  297. because your content should all be inside the content component - and it's the
  298. content component's resized() method that you should be using to do your
  299. layout.
  300. */
  301. jassert (hasBeenResized || (getWidth() == 0 && getHeight() == 0));
  302. #endif
  303. }
  304. void ResizableWindow::lookAndFeelChanged()
  305. {
  306. resized();
  307. if (isOnDesktop())
  308. {
  309. Component::addToDesktop (getDesktopWindowStyleFlags());
  310. if (ComponentPeer* const peer = getPeer())
  311. peer->setConstrainer (constrainer);
  312. }
  313. }
  314. Colour ResizableWindow::getBackgroundColour() const noexcept
  315. {
  316. return findColour (backgroundColourId, false);
  317. }
  318. void ResizableWindow::setBackgroundColour (Colour newColour)
  319. {
  320. Colour backgroundColour (newColour);
  321. if (! Desktop::canUseSemiTransparentWindows())
  322. backgroundColour = newColour.withAlpha (1.0f);
  323. setColour (backgroundColourId, backgroundColour);
  324. setOpaque (backgroundColour.isOpaque());
  325. repaint();
  326. }
  327. //==============================================================================
  328. bool ResizableWindow::isFullScreen() const
  329. {
  330. if (isOnDesktop())
  331. {
  332. ComponentPeer* const peer = getPeer();
  333. return peer != nullptr && peer->isFullScreen();
  334. }
  335. return fullscreen;
  336. }
  337. void ResizableWindow::setFullScreen (const bool shouldBeFullScreen)
  338. {
  339. if (shouldBeFullScreen != isFullScreen())
  340. {
  341. updateLastPosIfShowing();
  342. fullscreen = shouldBeFullScreen;
  343. if (isOnDesktop())
  344. {
  345. if (ComponentPeer* const peer = getPeer())
  346. {
  347. // keep a copy of this intact in case the real one gets messed-up while we're un-maximising
  348. const Rectangle<int> lastPos (lastNonFullScreenPos);
  349. peer->setFullScreen (shouldBeFullScreen);
  350. if ((! shouldBeFullScreen) && ! lastPos.isEmpty())
  351. setBounds (lastPos);
  352. }
  353. else
  354. {
  355. jassertfalse;
  356. }
  357. }
  358. else
  359. {
  360. if (shouldBeFullScreen)
  361. setBounds (0, 0, getParentWidth(), getParentHeight());
  362. else
  363. setBounds (lastNonFullScreenPos);
  364. }
  365. resized();
  366. }
  367. }
  368. bool ResizableWindow::isMinimised() const
  369. {
  370. if (ComponentPeer* const peer = getPeer())
  371. return peer->isMinimised();
  372. return false;
  373. }
  374. void ResizableWindow::setMinimised (const bool shouldMinimise)
  375. {
  376. if (shouldMinimise != isMinimised())
  377. {
  378. if (ComponentPeer* const peer = getPeer())
  379. {
  380. updateLastPosIfShowing();
  381. peer->setMinimised (shouldMinimise);
  382. }
  383. else
  384. {
  385. jassertfalse;
  386. }
  387. }
  388. }
  389. bool ResizableWindow::isKioskMode() const
  390. {
  391. if (isOnDesktop())
  392. if (ComponentPeer* peer = getPeer())
  393. return peer->isKioskMode();
  394. return Desktop::getInstance().getKioskModeComponent() == this;
  395. }
  396. void ResizableWindow::updateLastPosIfShowing()
  397. {
  398. if (isShowing())
  399. updateLastPosIfNotFullScreen();
  400. }
  401. void ResizableWindow::updateLastPosIfNotFullScreen()
  402. {
  403. if (! (isFullScreen() || isMinimised() || isKioskMode()))
  404. lastNonFullScreenPos = getBounds();
  405. }
  406. void ResizableWindow::parentSizeChanged()
  407. {
  408. if (isFullScreen() && getParentComponent() != nullptr)
  409. setBounds (getParentComponent()->getLocalBounds());
  410. }
  411. //==============================================================================
  412. String ResizableWindow::getWindowStateAsString()
  413. {
  414. updateLastPosIfShowing();
  415. return (isFullScreen() && ! isKioskMode() ? "fs " : "") + lastNonFullScreenPos.toString();
  416. }
  417. bool ResizableWindow::restoreWindowStateFromString (const String& s)
  418. {
  419. StringArray tokens;
  420. tokens.addTokens (s, false);
  421. tokens.removeEmptyStrings();
  422. tokens.trim();
  423. const bool fs = tokens[0].startsWithIgnoreCase ("fs");
  424. const int firstCoord = fs ? 1 : 0;
  425. if (tokens.size() != firstCoord + 4)
  426. return false;
  427. Rectangle<int> newPos (tokens[firstCoord].getIntValue(),
  428. tokens[firstCoord + 1].getIntValue(),
  429. tokens[firstCoord + 2].getIntValue(),
  430. tokens[firstCoord + 3].getIntValue());
  431. if (newPos.isEmpty())
  432. return false;
  433. ComponentPeer* const peer = isOnDesktop() ? getPeer() : nullptr;
  434. if (peer != nullptr)
  435. peer->getFrameSize().addTo (newPos);
  436. {
  437. Desktop& desktop = Desktop::getInstance();
  438. RectangleList<int> allMonitors (desktop.getDisplays().getRectangleList (true));
  439. allMonitors.clipTo (newPos);
  440. const Rectangle<int> onScreenArea (allMonitors.getBounds());
  441. if (onScreenArea.getWidth() * onScreenArea.getHeight() < 32 * 32)
  442. {
  443. const Rectangle<int> screen (desktop.getDisplays().getDisplayContaining (newPos.getCentre()).userArea);
  444. newPos.setSize (jmin (newPos.getWidth(), screen.getWidth()),
  445. jmin (newPos.getHeight(), screen.getHeight()));
  446. newPos.setPosition (jlimit (screen.getX(), screen.getRight() - newPos.getWidth(), newPos.getX()),
  447. jlimit (screen.getY(), screen.getBottom() - newPos.getHeight(), newPos.getY()));
  448. }
  449. }
  450. if (peer != nullptr)
  451. {
  452. peer->getFrameSize().subtractFrom (newPos);
  453. peer->setNonFullScreenBounds (newPos);
  454. }
  455. updateLastPosIfNotFullScreen();
  456. if (fs)
  457. setBoundsConstrained (newPos);
  458. setFullScreen (fs);
  459. if (! fs)
  460. setBoundsConstrained (newPos);
  461. return true;
  462. }
  463. //==============================================================================
  464. void ResizableWindow::mouseDown (const MouseEvent& e)
  465. {
  466. if (canDrag && ! isFullScreen())
  467. {
  468. dragStarted = true;
  469. dragger.startDraggingComponent (this, e);
  470. }
  471. }
  472. void ResizableWindow::mouseDrag (const MouseEvent& e)
  473. {
  474. if (dragStarted)
  475. dragger.dragComponent (this, e, constrainer);
  476. }
  477. void ResizableWindow::mouseUp (const MouseEvent&)
  478. {
  479. dragStarted = false;
  480. }
  481. //==============================================================================
  482. #if JUCE_DEBUG
  483. void ResizableWindow::addChildComponent (Component* const child, int zOrder)
  484. {
  485. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  486. manages its child components automatically, and if you add your own it'll cause
  487. trouble. Instead, use setContentComponent() to give it a component which
  488. will be automatically resized and kept in the right place - then you can add
  489. subcomponents to the content comp. See the notes for the ResizableWindow class
  490. for more info.
  491. If you really know what you're doing and want to avoid this assertion, just call
  492. Component::addChildComponent directly.
  493. */
  494. jassertfalse;
  495. Component::addChildComponent (child, zOrder);
  496. }
  497. void ResizableWindow::addAndMakeVisible (Component* const child, int zOrder)
  498. {
  499. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  500. manages its child components automatically, and if you add your own it'll cause
  501. trouble. Instead, use setContentComponent() to give it a component which
  502. will be automatically resized and kept in the right place - then you can add
  503. subcomponents to the content comp. See the notes for the ResizableWindow class
  504. for more info.
  505. If you really know what you're doing and want to avoid this assertion, just call
  506. Component::addAndMakeVisible directly.
  507. */
  508. jassertfalse;
  509. Component::addAndMakeVisible (child, zOrder);
  510. }
  511. #endif