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.

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