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.

370 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. if (resizableBorder == nullptr)
  155. {
  156. RectangleList<int> border (getLocalBounds());
  157. border.subtract (getBorderThickness().subtractedFrom (getLocalBounds()));
  158. g.setColour (getBackgroundColour().overlaidWith (Colour (0x80000000)));
  159. g.fillRectList (border);
  160. }
  161. const Rectangle<int> titleBarArea (getTitleBarArea());
  162. g.reduceClipRegion (titleBarArea);
  163. g.setOrigin (titleBarArea.getPosition());
  164. int titleSpaceX1 = 6;
  165. int titleSpaceX2 = titleBarArea.getWidth() - 6;
  166. for (int i = 0; i < 3; ++i)
  167. {
  168. if (Button* const b = titleBarButtons[i])
  169. {
  170. if (positionTitleBarButtonsOnLeft)
  171. titleSpaceX1 = jmax (titleSpaceX1, b->getRight() + (getWidth() - b->getRight()) / 8);
  172. else
  173. titleSpaceX2 = jmin (titleSpaceX2, b->getX() - (b->getX() / 8));
  174. }
  175. }
  176. getLookAndFeel().drawDocumentWindowTitleBar (*this, g,
  177. titleBarArea.getWidth(),
  178. titleBarArea.getHeight(),
  179. titleSpaceX1,
  180. jmax (1, titleSpaceX2 - titleSpaceX1),
  181. titleBarIcon.isValid() ? &titleBarIcon : 0,
  182. ! drawTitleTextCentred);
  183. }
  184. void DocumentWindow::resized()
  185. {
  186. ResizableWindow::resized();
  187. if (Button* const b = getMaximiseButton())
  188. b->setToggleState (isFullScreen(), dontSendNotification);
  189. const Rectangle<int> titleBarArea (getTitleBarArea());
  190. getLookAndFeel()
  191. .positionDocumentWindowButtons (*this,
  192. titleBarArea.getX(), titleBarArea.getY(),
  193. titleBarArea.getWidth(), titleBarArea.getHeight(),
  194. titleBarButtons[0],
  195. titleBarButtons[1],
  196. titleBarButtons[2],
  197. positionTitleBarButtonsOnLeft);
  198. if (menuBar != nullptr)
  199. menuBar->setBounds (titleBarArea.getX(), titleBarArea.getBottom(),
  200. titleBarArea.getWidth(), menuBarHeight);
  201. }
  202. BorderSize<int> DocumentWindow::getBorderThickness()
  203. {
  204. return ResizableWindow::getBorderThickness();
  205. }
  206. BorderSize<int> DocumentWindow::getContentComponentBorder()
  207. {
  208. BorderSize<int> border (getBorderThickness());
  209. if (! isKioskMode())
  210. border.setTop (border.getTop()
  211. + (isUsingNativeTitleBar() ? 0 : titleBarHeight)
  212. + (menuBar != nullptr ? menuBarHeight : 0));
  213. return border;
  214. }
  215. int DocumentWindow::getTitleBarHeight() const
  216. {
  217. return isUsingNativeTitleBar() ? 0 : jmin (titleBarHeight, getHeight() - 4);
  218. }
  219. Rectangle<int> DocumentWindow::getTitleBarArea()
  220. {
  221. const BorderSize<int> border (getBorderThickness());
  222. if (isKioskMode())
  223. return Rectangle<int>();
  224. return Rectangle<int> (border.getLeft(), border.getTop(),
  225. getWidth() - border.getLeftAndRight(), getTitleBarHeight());
  226. }
  227. Button* DocumentWindow::getCloseButton() const noexcept { return titleBarButtons[2]; }
  228. Button* DocumentWindow::getMinimiseButton() const noexcept { return titleBarButtons[0]; }
  229. Button* DocumentWindow::getMaximiseButton() const noexcept { return titleBarButtons[1]; }
  230. int DocumentWindow::getDesktopWindowStyleFlags() const
  231. {
  232. int styleFlags = ResizableWindow::getDesktopWindowStyleFlags();
  233. if ((requiredButtons & minimiseButton) != 0) styleFlags |= ComponentPeer::windowHasMinimiseButton;
  234. if ((requiredButtons & maximiseButton) != 0) styleFlags |= ComponentPeer::windowHasMaximiseButton;
  235. if ((requiredButtons & closeButton) != 0) styleFlags |= ComponentPeer::windowHasCloseButton;
  236. return styleFlags;
  237. }
  238. void DocumentWindow::lookAndFeelChanged()
  239. {
  240. for (int i = numElementsInArray (titleBarButtons); --i >= 0;)
  241. titleBarButtons[i] = nullptr;
  242. if (! isUsingNativeTitleBar())
  243. {
  244. LookAndFeel& lf = getLookAndFeel();
  245. if ((requiredButtons & minimiseButton) != 0) titleBarButtons[0] = lf.createDocumentWindowButton (minimiseButton);
  246. if ((requiredButtons & maximiseButton) != 0) titleBarButtons[1] = lf.createDocumentWindowButton (maximiseButton);
  247. if ((requiredButtons & closeButton) != 0) titleBarButtons[2] = lf.createDocumentWindowButton (closeButton);
  248. for (int i = 0; i < 3; ++i)
  249. {
  250. if (Button* const b = titleBarButtons[i])
  251. {
  252. if (buttonListener == nullptr)
  253. buttonListener = new ButtonListenerProxy (*this);
  254. b->addListener (buttonListener);
  255. b->setWantsKeyboardFocus (false);
  256. // (call the Component method directly to avoid the assertion in ResizableWindow)
  257. Component::addAndMakeVisible (b);
  258. }
  259. }
  260. if (Button* const b = getCloseButton())
  261. {
  262. #if JUCE_MAC
  263. b->addShortcut (KeyPress ('w', ModifierKeys::commandModifier, 0));
  264. #else
  265. b->addShortcut (KeyPress (KeyPress::F4Key, ModifierKeys::altModifier, 0));
  266. #endif
  267. }
  268. }
  269. activeWindowStatusChanged();
  270. ResizableWindow::lookAndFeelChanged();
  271. }
  272. void DocumentWindow::parentHierarchyChanged()
  273. {
  274. lookAndFeelChanged();
  275. }
  276. void DocumentWindow::activeWindowStatusChanged()
  277. {
  278. ResizableWindow::activeWindowStatusChanged();
  279. for (int i = numElementsInArray (titleBarButtons); --i >= 0;)
  280. if (Button* const b = titleBarButtons[i])
  281. b->setEnabled (isActiveWindow());
  282. if (menuBar != nullptr)
  283. menuBar->setEnabled (isActiveWindow());
  284. }
  285. void DocumentWindow::mouseDoubleClick (const MouseEvent& e)
  286. {
  287. Button* const maximise = getMaximiseButton();
  288. if (maximise != nullptr && getTitleBarArea().contains (e.x, e.y))
  289. maximise->triggerClick();
  290. }
  291. void DocumentWindow::userTriedToCloseWindow()
  292. {
  293. closeButtonPressed();
  294. }