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.

617 lines
19KB

  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. contentComponent->setBoundsInset (getContentComponentBorder());
  189. updateLastPos();
  190. #if JUCE_DEBUG
  191. hasBeenResized = true;
  192. #endif
  193. }
  194. void ResizableWindow::childBoundsChanged (Component* child)
  195. {
  196. if ((child == contentComponent) && (child != nullptr) && resizeToFitContent)
  197. {
  198. // not going to look very good if this component has a zero size..
  199. jassert (child->getWidth() > 0);
  200. jassert (child->getHeight() > 0);
  201. const BorderSize<int> borders (getContentComponentBorder());
  202. setSize (child->getWidth() + borders.getLeftAndRight(),
  203. child->getHeight() + borders.getTopAndBottom());
  204. }
  205. }
  206. //==============================================================================
  207. void ResizableWindow::activeWindowStatusChanged()
  208. {
  209. const BorderSize<int> border (getContentComponentBorder());
  210. Rectangle<int> area (getLocalBounds());
  211. repaint (area.removeFromTop (border.getTop()));
  212. repaint (area.removeFromLeft (border.getLeft()));
  213. repaint (area.removeFromRight (border.getRight()));
  214. repaint (area.removeFromBottom (border.getBottom()));
  215. }
  216. //==============================================================================
  217. void ResizableWindow::setResizable (const bool shouldBeResizable,
  218. const bool useBottomRightCornerResizer)
  219. {
  220. if (shouldBeResizable)
  221. {
  222. if (useBottomRightCornerResizer)
  223. {
  224. resizableBorder = nullptr;
  225. if (resizableCorner == nullptr)
  226. {
  227. Component::addChildComponent (resizableCorner = new ResizableCornerComponent (this, constrainer));
  228. resizableCorner->setAlwaysOnTop (true);
  229. }
  230. }
  231. else
  232. {
  233. resizableCorner = nullptr;
  234. if (resizableBorder == nullptr)
  235. Component::addChildComponent (resizableBorder = new ResizableBorderComponent (this, constrainer));
  236. }
  237. }
  238. else
  239. {
  240. resizableCorner = nullptr;
  241. resizableBorder = nullptr;
  242. }
  243. if (isUsingNativeTitleBar())
  244. recreateDesktopWindow();
  245. childBoundsChanged (contentComponent);
  246. resized();
  247. }
  248. bool ResizableWindow::isResizable() const noexcept
  249. {
  250. return resizableCorner != nullptr
  251. || resizableBorder != nullptr;
  252. }
  253. void ResizableWindow::setResizeLimits (const int newMinimumWidth,
  254. const int newMinimumHeight,
  255. const int newMaximumWidth,
  256. const int newMaximumHeight) noexcept
  257. {
  258. // if you've set up a custom constrainer then these settings won't have any effect..
  259. jassert (constrainer == &defaultConstrainer || constrainer == nullptr);
  260. if (constrainer == nullptr)
  261. setConstrainer (&defaultConstrainer);
  262. defaultConstrainer.setSizeLimits (newMinimumWidth, newMinimumHeight,
  263. newMaximumWidth, newMaximumHeight);
  264. setBoundsConstrained (getBounds());
  265. }
  266. void ResizableWindow::setConstrainer (ComponentBoundsConstrainer* newConstrainer)
  267. {
  268. if (constrainer != newConstrainer)
  269. {
  270. constrainer = newConstrainer;
  271. const bool useBottomRightCornerResizer = resizableCorner != nullptr;
  272. const bool shouldBeResizable = useBottomRightCornerResizer || resizableBorder != nullptr;
  273. resizableCorner = nullptr;
  274. resizableBorder = nullptr;
  275. setResizable (shouldBeResizable, useBottomRightCornerResizer);
  276. ComponentPeer* const peer = getPeer();
  277. if (peer != nullptr)
  278. peer->setConstrainer (newConstrainer);
  279. }
  280. }
  281. void ResizableWindow::setBoundsConstrained (const Rectangle<int>& newBounds)
  282. {
  283. if (constrainer != nullptr)
  284. constrainer->setBoundsForComponent (this, newBounds, false, false, false, false);
  285. else
  286. setBounds (newBounds);
  287. }
  288. //==============================================================================
  289. void ResizableWindow::paint (Graphics& g)
  290. {
  291. getLookAndFeel().fillResizableWindowBackground (g, getWidth(), getHeight(),
  292. getBorderThickness(), *this);
  293. if (! isFullScreen())
  294. {
  295. getLookAndFeel().drawResizableWindowBorder (g, getWidth(), getHeight(),
  296. getBorderThickness(), *this);
  297. }
  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. ComponentPeer* const peer = getPeer();
  318. if (peer != nullptr)
  319. peer->setConstrainer (constrainer);
  320. }
  321. }
  322. const Colour ResizableWindow::getBackgroundColour() const noexcept
  323. {
  324. return findColour (backgroundColourId, false);
  325. }
  326. void ResizableWindow::setBackgroundColour (const Colour& newColour)
  327. {
  328. Colour backgroundColour (newColour);
  329. if (! Desktop::canUseSemiTransparentWindows())
  330. backgroundColour = newColour.withAlpha (1.0f);
  331. setColour (backgroundColourId, backgroundColour);
  332. setOpaque (backgroundColour.isOpaque());
  333. repaint();
  334. }
  335. //==============================================================================
  336. bool ResizableWindow::isFullScreen() const
  337. {
  338. if (isOnDesktop())
  339. {
  340. ComponentPeer* const peer = getPeer();
  341. return peer != nullptr && peer->isFullScreen();
  342. }
  343. return fullscreen;
  344. }
  345. void ResizableWindow::setFullScreen (const bool shouldBeFullScreen)
  346. {
  347. if (shouldBeFullScreen != isFullScreen())
  348. {
  349. updateLastPos();
  350. fullscreen = shouldBeFullScreen;
  351. if (isOnDesktop())
  352. {
  353. ComponentPeer* const peer = getPeer();
  354. if (peer != nullptr)
  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. ComponentPeer* const peer = getPeer();
  387. if (peer != nullptr)
  388. {
  389. updateLastPos();
  390. peer->setMinimised (shouldMinimise);
  391. }
  392. else
  393. {
  394. jassertfalse;
  395. }
  396. }
  397. }
  398. void ResizableWindow::updateLastPos()
  399. {
  400. if (isShowing() && ! (isFullScreen() || isMinimised()))
  401. {
  402. lastNonFullScreenPos = getBounds();
  403. }
  404. }
  405. void ResizableWindow::parentSizeChanged()
  406. {
  407. if (isFullScreen() && getParentComponent() != nullptr)
  408. {
  409. setBounds (0, 0, getParentWidth(), getParentHeight());
  410. }
  411. }
  412. //==============================================================================
  413. String ResizableWindow::getWindowStateAsString()
  414. {
  415. updateLastPos();
  416. return (isFullScreen() ? "fs " : "") + lastNonFullScreenPos.toString();
  417. }
  418. bool ResizableWindow::restoreWindowStateFromString (const String& s)
  419. {
  420. StringArray tokens;
  421. tokens.addTokens (s, false);
  422. tokens.removeEmptyStrings();
  423. tokens.trim();
  424. const bool fs = tokens[0].startsWithIgnoreCase ("fs");
  425. const int firstCoord = fs ? 1 : 0;
  426. if (tokens.size() != firstCoord + 4)
  427. return false;
  428. Rectangle<int> newPos (tokens[firstCoord].getIntValue(),
  429. tokens[firstCoord + 1].getIntValue(),
  430. tokens[firstCoord + 2].getIntValue(),
  431. tokens[firstCoord + 3].getIntValue());
  432. if (newPos.isEmpty())
  433. return false;
  434. const Rectangle<int> screen (Desktop::getInstance().getMonitorAreaContaining (newPos.getCentre()));
  435. ComponentPeer* const peer = isOnDesktop() ? getPeer() : nullptr;
  436. if (peer != nullptr)
  437. peer->getFrameSize().addTo (newPos);
  438. if (! screen.contains (newPos))
  439. {
  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. 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
  498. END_JUCE_NAMESPACE