Audio plugin host https://kx.studio/carla
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.

368 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. namespace juce
  20. {
  21. class DocumentWindow::ButtonListenerProxy : public Button::Listener
  22. {
  23. public:
  24. ButtonListenerProxy (DocumentWindow& w) : owner (w) {}
  25. void buttonClicked (Button* button) override
  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. Colour backgroundColour,
  38. int requiredButtons_,
  39. 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 buttons, const bool onLeft)
  93. {
  94. requiredButtons = buttons;
  95. positionTitleBarButtonsOnLeft = onLeft;
  96. lookAndFeelChanged();
  97. }
  98. void DocumentWindow::setTitleBarTextCentred (const bool textShouldBeCentred)
  99. {
  100. drawTitleTextCentred = textShouldBeCentred;
  101. repaintTitleBar();
  102. }
  103. //==============================================================================
  104. void DocumentWindow::setMenuBar (MenuBarModel* newMenuBarModel, const int newMenuBarHeight)
  105. {
  106. if (menuBarModel != newMenuBarModel)
  107. {
  108. menuBar = nullptr;
  109. menuBarModel = newMenuBarModel;
  110. menuBarHeight = newMenuBarHeight > 0 ? newMenuBarHeight
  111. : getLookAndFeel().getDefaultMenuBarHeight();
  112. if (menuBarModel != nullptr)
  113. setMenuBarComponent (new MenuBarComponent (menuBarModel));
  114. resized();
  115. }
  116. }
  117. Component* DocumentWindow::getMenuBarComponent() const noexcept
  118. {
  119. return menuBar;
  120. }
  121. void DocumentWindow::setMenuBarComponent (Component* newMenuBarComponent)
  122. {
  123. // (call the Component method directly to avoid the assertion in ResizableWindow)
  124. Component::addAndMakeVisible (menuBar = newMenuBarComponent);
  125. if (menuBar != nullptr)
  126. menuBar->setEnabled (isActiveWindow());
  127. resized();
  128. }
  129. //==============================================================================
  130. void DocumentWindow::closeButtonPressed()
  131. {
  132. /* If you've got a close button, you have to override this method to get
  133. rid of your window!
  134. If the window is just a pop-up, you should override this method and make
  135. it delete the window in whatever way is appropriate for your app. E.g. you
  136. might just want to call "delete this".
  137. If your app is centred around this window such that the whole app should quit when
  138. the window is closed, then you will probably want to use this method as an opportunity
  139. to call JUCEApplicationBase::quit(), and leave the window to be deleted later by your
  140. JUCEApplicationBase::shutdown() method. (Doing it this way means that your window will
  141. still get cleaned-up if the app is quit by some other means (e.g. a cmd-Q on the mac
  142. or closing it via the taskbar icon on Windows).
  143. */
  144. jassertfalse;
  145. }
  146. void DocumentWindow::minimiseButtonPressed()
  147. {
  148. setMinimised (true);
  149. }
  150. void DocumentWindow::maximiseButtonPressed()
  151. {
  152. setFullScreen (! isFullScreen());
  153. }
  154. //==============================================================================
  155. void DocumentWindow::paint (Graphics& g)
  156. {
  157. ResizableWindow::paint (g);
  158. const Rectangle<int> titleBarArea (getTitleBarArea());
  159. g.reduceClipRegion (titleBarArea);
  160. g.setOrigin (titleBarArea.getPosition());
  161. int titleSpaceX1 = 6;
  162. int titleSpaceX2 = titleBarArea.getWidth() - 6;
  163. for (int i = 0; i < 3; ++i)
  164. {
  165. if (Button* const b = titleBarButtons[i])
  166. {
  167. if (positionTitleBarButtonsOnLeft)
  168. titleSpaceX1 = jmax (titleSpaceX1, b->getRight() + (getWidth() - b->getRight()) / 8);
  169. else
  170. titleSpaceX2 = jmin (titleSpaceX2, b->getX() - (b->getX() / 8));
  171. }
  172. }
  173. getLookAndFeel().drawDocumentWindowTitleBar (*this, g,
  174. titleBarArea.getWidth(),
  175. titleBarArea.getHeight(),
  176. titleSpaceX1,
  177. jmax (1, titleSpaceX2 - titleSpaceX1),
  178. titleBarIcon.isValid() ? &titleBarIcon : 0,
  179. ! drawTitleTextCentred);
  180. }
  181. void DocumentWindow::resized()
  182. {
  183. ResizableWindow::resized();
  184. if (Button* const b = getMaximiseButton())
  185. b->setToggleState (isFullScreen(), dontSendNotification);
  186. const Rectangle<int> titleBarArea (getTitleBarArea());
  187. getLookAndFeel()
  188. .positionDocumentWindowButtons (*this,
  189. titleBarArea.getX(), titleBarArea.getY(),
  190. titleBarArea.getWidth(), titleBarArea.getHeight(),
  191. titleBarButtons[0],
  192. titleBarButtons[1],
  193. titleBarButtons[2],
  194. positionTitleBarButtonsOnLeft);
  195. if (menuBar != nullptr)
  196. menuBar->setBounds (titleBarArea.getX(), titleBarArea.getBottom(),
  197. titleBarArea.getWidth(), menuBarHeight);
  198. }
  199. BorderSize<int> DocumentWindow::getBorderThickness()
  200. {
  201. return ResizableWindow::getBorderThickness();
  202. }
  203. BorderSize<int> DocumentWindow::getContentComponentBorder()
  204. {
  205. BorderSize<int> border (getBorderThickness());
  206. if (! isKioskMode())
  207. border.setTop (border.getTop()
  208. + (isUsingNativeTitleBar() ? 0 : titleBarHeight)
  209. + (menuBar != nullptr ? menuBarHeight : 0));
  210. return border;
  211. }
  212. int DocumentWindow::getTitleBarHeight() const
  213. {
  214. return isUsingNativeTitleBar() ? 0 : jmin (titleBarHeight, getHeight() - 4);
  215. }
  216. Rectangle<int> DocumentWindow::getTitleBarArea()
  217. {
  218. const BorderSize<int> border (getBorderThickness());
  219. if (isKioskMode())
  220. return Rectangle<int>();
  221. return Rectangle<int> (border.getLeft(), border.getTop(),
  222. getWidth() - border.getLeftAndRight(), getTitleBarHeight());
  223. }
  224. Button* DocumentWindow::getCloseButton() const noexcept { return titleBarButtons[2]; }
  225. Button* DocumentWindow::getMinimiseButton() const noexcept { return titleBarButtons[0]; }
  226. Button* DocumentWindow::getMaximiseButton() const noexcept { return titleBarButtons[1]; }
  227. int DocumentWindow::getDesktopWindowStyleFlags() const
  228. {
  229. int styleFlags = ResizableWindow::getDesktopWindowStyleFlags();
  230. if ((requiredButtons & minimiseButton) != 0) styleFlags |= ComponentPeer::windowHasMinimiseButton;
  231. if ((requiredButtons & maximiseButton) != 0) styleFlags |= ComponentPeer::windowHasMaximiseButton;
  232. if ((requiredButtons & closeButton) != 0) styleFlags |= ComponentPeer::windowHasCloseButton;
  233. return styleFlags;
  234. }
  235. void DocumentWindow::lookAndFeelChanged()
  236. {
  237. for (int i = numElementsInArray (titleBarButtons); --i >= 0;)
  238. titleBarButtons[i] = nullptr;
  239. if (! isUsingNativeTitleBar())
  240. {
  241. LookAndFeel& lf = getLookAndFeel();
  242. if ((requiredButtons & minimiseButton) != 0) titleBarButtons[0] = lf.createDocumentWindowButton (minimiseButton);
  243. if ((requiredButtons & maximiseButton) != 0) titleBarButtons[1] = lf.createDocumentWindowButton (maximiseButton);
  244. if ((requiredButtons & closeButton) != 0) titleBarButtons[2] = lf.createDocumentWindowButton (closeButton);
  245. for (int i = 0; i < 3; ++i)
  246. {
  247. if (Button* const b = titleBarButtons[i])
  248. {
  249. if (buttonListener == nullptr)
  250. buttonListener = new ButtonListenerProxy (*this);
  251. b->addListener (buttonListener);
  252. b->setWantsKeyboardFocus (false);
  253. // (call the Component method directly to avoid the assertion in ResizableWindow)
  254. Component::addAndMakeVisible (b);
  255. }
  256. }
  257. if (Button* const b = getCloseButton())
  258. {
  259. #if JUCE_MAC
  260. b->addShortcut (KeyPress ('w', ModifierKeys::commandModifier, 0));
  261. #else
  262. b->addShortcut (KeyPress (KeyPress::F4Key, ModifierKeys::altModifier, 0));
  263. #endif
  264. }
  265. }
  266. activeWindowStatusChanged();
  267. ResizableWindow::lookAndFeelChanged();
  268. }
  269. void DocumentWindow::parentHierarchyChanged()
  270. {
  271. lookAndFeelChanged();
  272. }
  273. void DocumentWindow::activeWindowStatusChanged()
  274. {
  275. ResizableWindow::activeWindowStatusChanged();
  276. for (int i = numElementsInArray (titleBarButtons); --i >= 0;)
  277. if (Button* const b = titleBarButtons[i])
  278. b->setEnabled (isActiveWindow());
  279. if (menuBar != nullptr)
  280. menuBar->setEnabled (isActiveWindow());
  281. }
  282. void DocumentWindow::mouseDoubleClick (const MouseEvent& e)
  283. {
  284. Button* const maximise = getMaximiseButton();
  285. if (maximise != nullptr && getTitleBarArea().contains (e.x, e.y))
  286. maximise->triggerClick();
  287. }
  288. void DocumentWindow::userTriedToCloseWindow()
  289. {
  290. closeButtonPressed();
  291. }
  292. } // namespace juce