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.

361 lines
12KB

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