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