The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

628 lines
19KB

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