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.

579 lines
18KB

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