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
20KB

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