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.

389 lines
13KB

  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. class DocumentWindow::ButtonListenerProxy : public ButtonListener // (can't use Button::Listener due to idiotic VC2005 bug)
  21. {
  22. public:
  23. ButtonListenerProxy (DocumentWindow& owner_)
  24. : owner (owner_)
  25. {
  26. }
  27. void buttonClicked (Button* button)
  28. {
  29. if (button == owner.getMinimiseButton()) owner.minimiseButtonPressed();
  30. else if (button == owner.getMaximiseButton()) owner.maximiseButtonPressed();
  31. else if (button == owner.getCloseButton()) owner.closeButtonPressed();
  32. }
  33. private:
  34. DocumentWindow& owner;
  35. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonListenerProxy);
  36. };
  37. //==============================================================================
  38. DocumentWindow::DocumentWindow (const String& title,
  39. const Colour& backgroundColour,
  40. const int requiredButtons_,
  41. const bool addToDesktop_)
  42. : ResizableWindow (title, backgroundColour, addToDesktop_),
  43. titleBarHeight (26),
  44. menuBarHeight (24),
  45. requiredButtons (requiredButtons_),
  46. #if JUCE_MAC
  47. positionTitleBarButtonsOnLeft (true),
  48. #else
  49. positionTitleBarButtonsOnLeft (false),
  50. #endif
  51. drawTitleTextCentred (true),
  52. menuBarModel (nullptr)
  53. {
  54. setResizeLimits (128, 128, 32768, 32768);
  55. DocumentWindow::lookAndFeelChanged();
  56. }
  57. DocumentWindow::~DocumentWindow()
  58. {
  59. // Don't delete or remove the resizer components yourself! They're managed by the
  60. // DocumentWindow, and you should leave them alone! You may have deleted them
  61. // accidentally by careless use of deleteAllChildren()..?
  62. jassert (menuBar == nullptr || getIndexOfChildComponent (menuBar) >= 0);
  63. jassert (titleBarButtons[0] == nullptr || getIndexOfChildComponent (titleBarButtons[0]) >= 0);
  64. jassert (titleBarButtons[1] == nullptr || getIndexOfChildComponent (titleBarButtons[1]) >= 0);
  65. jassert (titleBarButtons[2] == nullptr || getIndexOfChildComponent (titleBarButtons[2]) >= 0);
  66. for (int i = numElementsInArray (titleBarButtons); --i >= 0;)
  67. titleBarButtons[i] = nullptr;
  68. menuBar = nullptr;
  69. }
  70. //==============================================================================
  71. void DocumentWindow::repaintTitleBar()
  72. {
  73. repaint (getTitleBarArea());
  74. }
  75. void DocumentWindow::setName (const String& newName)
  76. {
  77. if (newName != getName())
  78. {
  79. Component::setName (newName);
  80. repaintTitleBar();
  81. }
  82. }
  83. void DocumentWindow::setIcon (const Image& imageToUse)
  84. {
  85. titleBarIcon = imageToUse;
  86. repaintTitleBar();
  87. }
  88. void DocumentWindow::setTitleBarHeight (const int newHeight)
  89. {
  90. titleBarHeight = newHeight;
  91. resized();
  92. repaintTitleBar();
  93. }
  94. void DocumentWindow::setTitleBarButtonsRequired (const int requiredButtons_,
  95. const bool positionTitleBarButtonsOnLeft_)
  96. {
  97. requiredButtons = requiredButtons_;
  98. positionTitleBarButtonsOnLeft = positionTitleBarButtonsOnLeft_;
  99. lookAndFeelChanged();
  100. }
  101. void DocumentWindow::setTitleBarTextCentred (const bool textShouldBeCentred)
  102. {
  103. drawTitleTextCentred = textShouldBeCentred;
  104. repaintTitleBar();
  105. }
  106. //==============================================================================
  107. void DocumentWindow::setMenuBar (MenuBarModel* newMenuBarModel, const int newMenuBarHeight)
  108. {
  109. if (menuBarModel != newMenuBarModel)
  110. {
  111. menuBar = nullptr;
  112. menuBarModel = newMenuBarModel;
  113. menuBarHeight = newMenuBarHeight > 0 ? newMenuBarHeight
  114. : getLookAndFeel().getDefaultMenuBarHeight();
  115. if (menuBarModel != nullptr)
  116. setMenuBarComponent (new MenuBarComponent (menuBarModel));
  117. resized();
  118. }
  119. }
  120. Component* DocumentWindow::getMenuBarComponent() const noexcept
  121. {
  122. return menuBar;
  123. }
  124. void DocumentWindow::setMenuBarComponent (Component* newMenuBarComponent)
  125. {
  126. // (call the Component method directly to avoid the assertion in ResizableWindow)
  127. Component::addAndMakeVisible (menuBar = newMenuBarComponent);
  128. if (menuBar != nullptr)
  129. menuBar->setEnabled (isActiveWindow());
  130. resized();
  131. }
  132. //==============================================================================
  133. void DocumentWindow::closeButtonPressed()
  134. {
  135. /* If you've got a close button, you have to override this method to get
  136. rid of your window!
  137. If the window is just a pop-up, you should override this method and make
  138. it delete the window in whatever way is appropriate for your app. E.g. you
  139. might just want to call "delete this".
  140. If your app is centred around this window such that the whole app should quit when
  141. the window is closed, then you will probably want to use this method as an opportunity
  142. to call JUCEApplication::quit(), and leave the window to be deleted later by your
  143. JUCEApplication::shutdown() method. (Doing it this way means that your window will
  144. still get cleaned-up if the app is quit by some other means (e.g. a cmd-Q on the mac
  145. or closing it via the taskbar icon on Windows).
  146. */
  147. jassertfalse;
  148. }
  149. void DocumentWindow::minimiseButtonPressed()
  150. {
  151. setMinimised (true);
  152. }
  153. void DocumentWindow::maximiseButtonPressed()
  154. {
  155. setFullScreen (! isFullScreen());
  156. }
  157. //==============================================================================
  158. void DocumentWindow::paint (Graphics& g)
  159. {
  160. ResizableWindow::paint (g);
  161. if (resizableBorder == nullptr)
  162. {
  163. g.setColour (getBackgroundColour().overlaidWith (Colour (0x80000000)));
  164. const BorderSize<int> border (getBorderThickness());
  165. g.fillRect (0, 0, getWidth(), border.getTop());
  166. g.fillRect (0, border.getTop(), border.getLeft(), getHeight() - border.getTopAndBottom());
  167. g.fillRect (getWidth() - border.getRight(), border.getTop(), border.getRight(), getHeight() - border.getTopAndBottom());
  168. g.fillRect (0, getHeight() - border.getBottom(), getWidth(), border.getBottom());
  169. }
  170. const Rectangle<int> titleBarArea (getTitleBarArea());
  171. g.reduceClipRegion (titleBarArea);
  172. g.setOrigin (titleBarArea.getX(), titleBarArea.getY());
  173. int titleSpaceX1 = 6;
  174. int titleSpaceX2 = titleBarArea.getWidth() - 6;
  175. for (int i = 0; i < 3; ++i)
  176. {
  177. if (titleBarButtons[i] != nullptr)
  178. {
  179. if (positionTitleBarButtonsOnLeft)
  180. titleSpaceX1 = jmax (titleSpaceX1, titleBarButtons[i]->getRight() + (getWidth() - titleBarButtons[i]->getRight()) / 8);
  181. else
  182. titleSpaceX2 = jmin (titleSpaceX2, titleBarButtons[i]->getX() - (titleBarButtons[i]->getX() / 8));
  183. }
  184. }
  185. getLookAndFeel().drawDocumentWindowTitleBar (*this, g,
  186. titleBarArea.getWidth(),
  187. titleBarArea.getHeight(),
  188. titleSpaceX1,
  189. jmax (1, titleSpaceX2 - titleSpaceX1),
  190. titleBarIcon.isValid() ? &titleBarIcon : 0,
  191. ! drawTitleTextCentred);
  192. }
  193. void DocumentWindow::resized()
  194. {
  195. ResizableWindow::resized();
  196. if (titleBarButtons[1] != nullptr)
  197. titleBarButtons[1]->setToggleState (isFullScreen(), false);
  198. const Rectangle<int> titleBarArea (getTitleBarArea());
  199. getLookAndFeel()
  200. .positionDocumentWindowButtons (*this,
  201. titleBarArea.getX(), titleBarArea.getY(),
  202. titleBarArea.getWidth(), titleBarArea.getHeight(),
  203. titleBarButtons[0],
  204. titleBarButtons[1],
  205. titleBarButtons[2],
  206. positionTitleBarButtonsOnLeft);
  207. if (menuBar != nullptr)
  208. menuBar->setBounds (titleBarArea.getX(), titleBarArea.getBottom(),
  209. titleBarArea.getWidth(), menuBarHeight);
  210. }
  211. const BorderSize<int> DocumentWindow::getBorderThickness()
  212. {
  213. return BorderSize<int> ((isFullScreen() || isUsingNativeTitleBar())
  214. ? 0 : (resizableBorder != nullptr ? 4 : 1));
  215. }
  216. const BorderSize<int> DocumentWindow::getContentComponentBorder()
  217. {
  218. BorderSize<int> border (getBorderThickness());
  219. border.setTop (border.getTop()
  220. + (isUsingNativeTitleBar() ? 0 : titleBarHeight)
  221. + (menuBar != nullptr ? menuBarHeight : 0));
  222. return border;
  223. }
  224. int DocumentWindow::getTitleBarHeight() const
  225. {
  226. return isUsingNativeTitleBar() ? 0 : jmin (titleBarHeight, getHeight() - 4);
  227. }
  228. const Rectangle<int> DocumentWindow::getTitleBarArea()
  229. {
  230. const BorderSize<int> border (getBorderThickness());
  231. return Rectangle<int> (border.getLeft(), border.getTop(),
  232. getWidth() - border.getLeftAndRight(),
  233. getTitleBarHeight());
  234. }
  235. Button* DocumentWindow::getCloseButton() const noexcept { return titleBarButtons[2]; }
  236. Button* DocumentWindow::getMinimiseButton() const noexcept { return titleBarButtons[0]; }
  237. Button* DocumentWindow::getMaximiseButton() const noexcept { return titleBarButtons[1]; }
  238. int DocumentWindow::getDesktopWindowStyleFlags() const
  239. {
  240. int styleFlags = ResizableWindow::getDesktopWindowStyleFlags();
  241. if ((requiredButtons & minimiseButton) != 0) styleFlags |= ComponentPeer::windowHasMinimiseButton;
  242. if ((requiredButtons & maximiseButton) != 0) styleFlags |= ComponentPeer::windowHasMaximiseButton;
  243. if ((requiredButtons & closeButton) != 0) styleFlags |= ComponentPeer::windowHasCloseButton;
  244. return styleFlags;
  245. }
  246. void DocumentWindow::lookAndFeelChanged()
  247. {
  248. int i;
  249. for (i = numElementsInArray (titleBarButtons); --i >= 0;)
  250. titleBarButtons[i] = nullptr;
  251. if (! isUsingNativeTitleBar())
  252. {
  253. LookAndFeel& lf = getLookAndFeel();
  254. if ((requiredButtons & minimiseButton) != 0)
  255. titleBarButtons[0] = lf.createDocumentWindowButton (minimiseButton);
  256. if ((requiredButtons & maximiseButton) != 0)
  257. titleBarButtons[1] = lf.createDocumentWindowButton (maximiseButton);
  258. if ((requiredButtons & closeButton) != 0)
  259. titleBarButtons[2] = lf.createDocumentWindowButton (closeButton);
  260. for (i = 0; i < 3; ++i)
  261. {
  262. if (titleBarButtons[i] != nullptr)
  263. {
  264. if (buttonListener == nullptr)
  265. buttonListener = new ButtonListenerProxy (*this);
  266. titleBarButtons[i]->addListener (buttonListener);
  267. titleBarButtons[i]->setWantsKeyboardFocus (false);
  268. // (call the Component method directly to avoid the assertion in ResizableWindow)
  269. Component::addAndMakeVisible (titleBarButtons[i]);
  270. }
  271. }
  272. if (getCloseButton() != nullptr)
  273. {
  274. #if JUCE_MAC
  275. getCloseButton()->addShortcut (KeyPress ('w', ModifierKeys::commandModifier, 0));
  276. #else
  277. getCloseButton()->addShortcut (KeyPress (KeyPress::F4Key, ModifierKeys::altModifier, 0));
  278. #endif
  279. }
  280. }
  281. activeWindowStatusChanged();
  282. ResizableWindow::lookAndFeelChanged();
  283. }
  284. void DocumentWindow::parentHierarchyChanged()
  285. {
  286. lookAndFeelChanged();
  287. }
  288. void DocumentWindow::activeWindowStatusChanged()
  289. {
  290. ResizableWindow::activeWindowStatusChanged();
  291. for (int i = numElementsInArray (titleBarButtons); --i >= 0;)
  292. if (titleBarButtons[i] != nullptr)
  293. titleBarButtons[i]->setEnabled (isActiveWindow());
  294. if (menuBar != nullptr)
  295. menuBar->setEnabled (isActiveWindow());
  296. }
  297. void DocumentWindow::mouseDoubleClick (const MouseEvent& e)
  298. {
  299. if (getTitleBarArea().contains (e.x, e.y)
  300. && getMaximiseButton() != nullptr)
  301. {
  302. getMaximiseButton()->triggerClick();
  303. }
  304. }
  305. void DocumentWindow::userTriedToCloseWindow()
  306. {
  307. closeButtonPressed();
  308. }
  309. END_JUCE_NAMESPACE