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.

560 lines
17KB

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