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.

621 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. ComponentPeer* const peer = getPeer();
  280. if (peer != nullptr)
  281. peer->setConstrainer (newConstrainer);
  282. }
  283. }
  284. void ResizableWindow::setBoundsConstrained (const Rectangle<int>& newBounds)
  285. {
  286. if (constrainer != nullptr)
  287. constrainer->setBoundsForComponent (this, newBounds, false, false, false, false);
  288. else
  289. setBounds (newBounds);
  290. }
  291. //==============================================================================
  292. void ResizableWindow::paint (Graphics& g)
  293. {
  294. getLookAndFeel().fillResizableWindowBackground (g, getWidth(), getHeight(),
  295. getBorderThickness(), *this);
  296. if (! isFullScreen())
  297. {
  298. getLookAndFeel().drawResizableWindowBorder (g, getWidth(), getHeight(),
  299. getBorderThickness(), *this);
  300. }
  301. #if JUCE_DEBUG
  302. /* If this fails, then you've probably written a subclass with a resized()
  303. callback but forgotten to make it call its parent class's resized() method.
  304. It's important when you override methods like resized(), moved(),
  305. etc., that you make sure the base class methods also get called.
  306. Of course you shouldn't really be overriding ResizableWindow::resized() anyway,
  307. because your content should all be inside the content component - and it's the
  308. content component's resized() method that you should be using to do your
  309. layout.
  310. */
  311. jassert (hasBeenResized || (getWidth() == 0 && getHeight() == 0));
  312. #endif
  313. }
  314. void ResizableWindow::lookAndFeelChanged()
  315. {
  316. resized();
  317. if (isOnDesktop())
  318. {
  319. Component::addToDesktop (getDesktopWindowStyleFlags());
  320. ComponentPeer* const peer = getPeer();
  321. if (peer != nullptr)
  322. peer->setConstrainer (constrainer);
  323. }
  324. }
  325. Colour ResizableWindow::getBackgroundColour() const noexcept
  326. {
  327. return findColour (backgroundColourId, false);
  328. }
  329. void ResizableWindow::setBackgroundColour (const Colour& newColour)
  330. {
  331. Colour backgroundColour (newColour);
  332. if (! Desktop::canUseSemiTransparentWindows())
  333. backgroundColour = newColour.withAlpha (1.0f);
  334. setColour (backgroundColourId, backgroundColour);
  335. setOpaque (backgroundColour.isOpaque());
  336. repaint();
  337. }
  338. //==============================================================================
  339. bool ResizableWindow::isFullScreen() const
  340. {
  341. if (isOnDesktop())
  342. {
  343. ComponentPeer* const peer = getPeer();
  344. return peer != nullptr && peer->isFullScreen();
  345. }
  346. return fullscreen;
  347. }
  348. void ResizableWindow::setFullScreen (const bool shouldBeFullScreen)
  349. {
  350. if (shouldBeFullScreen != isFullScreen())
  351. {
  352. updateLastPos();
  353. fullscreen = shouldBeFullScreen;
  354. if (isOnDesktop())
  355. {
  356. ComponentPeer* const peer = getPeer();
  357. if (peer != nullptr)
  358. {
  359. // keep a copy of this intact in case the real one gets messed-up while we're un-maximising
  360. const Rectangle<int> lastPos (lastNonFullScreenPos);
  361. peer->setFullScreen (shouldBeFullScreen);
  362. if ((! shouldBeFullScreen) && ! lastPos.isEmpty())
  363. setBounds (lastPos);
  364. }
  365. else
  366. {
  367. jassertfalse;
  368. }
  369. }
  370. else
  371. {
  372. if (shouldBeFullScreen)
  373. setBounds (0, 0, getParentWidth(), getParentHeight());
  374. else
  375. setBounds (lastNonFullScreenPos);
  376. }
  377. resized();
  378. }
  379. }
  380. bool ResizableWindow::isMinimised() const
  381. {
  382. ComponentPeer* const peer = getPeer();
  383. return (peer != nullptr) && peer->isMinimised();
  384. }
  385. void ResizableWindow::setMinimised (const bool shouldMinimise)
  386. {
  387. if (shouldMinimise != isMinimised())
  388. {
  389. ComponentPeer* const peer = getPeer();
  390. if (peer != nullptr)
  391. {
  392. updateLastPos();
  393. peer->setMinimised (shouldMinimise);
  394. }
  395. else
  396. {
  397. jassertfalse;
  398. }
  399. }
  400. }
  401. void ResizableWindow::updateLastPos()
  402. {
  403. if (isShowing() && ! (isFullScreen() || isMinimised()))
  404. lastNonFullScreenPos = getBounds();
  405. }
  406. void ResizableWindow::parentSizeChanged()
  407. {
  408. if (isFullScreen() && getParentComponent() != nullptr)
  409. setBounds (0, 0, getParentWidth(), getParentHeight());
  410. }
  411. //==============================================================================
  412. String ResizableWindow::getWindowStateAsString()
  413. {
  414. updateLastPos();
  415. return (isFullScreen() ? "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 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. lastNonFullScreenPos = newPos;
  456. setFullScreen (fs);
  457. if (! fs)
  458. setBoundsConstrained (newPos);
  459. return true;
  460. }
  461. //==============================================================================
  462. void ResizableWindow::mouseDown (const MouseEvent& e)
  463. {
  464. if (! isFullScreen())
  465. dragger.startDraggingComponent (this, e);
  466. }
  467. void ResizableWindow::mouseDrag (const MouseEvent& e)
  468. {
  469. if (! isFullScreen())
  470. dragger.dragComponent (this, e, constrainer);
  471. }
  472. //==============================================================================
  473. #if JUCE_DEBUG
  474. void ResizableWindow::addChildComponent (Component* const child, int zOrder)
  475. {
  476. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  477. manages its child components automatically, and if you add your own it'll cause
  478. trouble. Instead, use setContentComponent() to give it a component which
  479. will be automatically resized and kept in the right place - then you can add
  480. subcomponents to the content comp. See the notes for the ResizableWindow class
  481. for more info.
  482. If you really know what you're doing and want to avoid this assertion, just call
  483. Component::addChildComponent directly.
  484. */
  485. jassertfalse;
  486. Component::addChildComponent (child, zOrder);
  487. }
  488. void ResizableWindow::addAndMakeVisible (Component* const child, int zOrder)
  489. {
  490. /* Agh! You shouldn't add components directly to a ResizableWindow - this class
  491. manages its child components automatically, and if you add your own it'll cause
  492. trouble. Instead, use setContentComponent() to give it a component which
  493. will be automatically resized and kept in the right place - then you can add
  494. subcomponents to the content comp. See the notes for the ResizableWindow class
  495. for more info.
  496. If you really know what you're doing and want to avoid this assertion, just call
  497. Component::addAndMakeVisible directly.
  498. */
  499. jassertfalse;
  500. Component::addAndMakeVisible (child, zOrder);
  501. }
  502. #endif