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.

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