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.

363 lines
12KB

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