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.

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