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.

juce_DocumentWindow.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. requiredButtons (requiredButtons_),
  42. #if JUCE_MAC
  43. positionTitleBarButtonsOnLeft (true)
  44. #else
  45. positionTitleBarButtonsOnLeft (false)
  46. #endif
  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 (auto& b : titleBarButtons)
  61. b = 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. auto titleBarArea = getTitleBarArea();
  155. g.reduceClipRegion (titleBarArea);
  156. g.setOrigin (titleBarArea.getPosition());
  157. int titleSpaceX1 = 6;
  158. int titleSpaceX2 = titleBarArea.getWidth() - 6;
  159. for (auto& b : titleBarButtons)
  160. {
  161. if (b != nullptr)
  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 (auto* b = getMaximiseButton())
  181. b->setToggleState (isFullScreen(), dontSendNotification);
  182. auto 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. auto 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. if (isKioskMode())
  215. return {};
  216. auto border = getBorderThickness();
  217. return { border.getLeft(), border.getTop(), getWidth() - border.getLeftAndRight(), getTitleBarHeight() };
  218. }
  219. Button* DocumentWindow::getCloseButton() const noexcept { return titleBarButtons[2]; }
  220. Button* DocumentWindow::getMinimiseButton() const noexcept { return titleBarButtons[0]; }
  221. Button* DocumentWindow::getMaximiseButton() const noexcept { return titleBarButtons[1]; }
  222. int DocumentWindow::getDesktopWindowStyleFlags() const
  223. {
  224. auto styleFlags = ResizableWindow::getDesktopWindowStyleFlags();
  225. if ((requiredButtons & minimiseButton) != 0) styleFlags |= ComponentPeer::windowHasMinimiseButton;
  226. if ((requiredButtons & maximiseButton) != 0) styleFlags |= ComponentPeer::windowHasMaximiseButton;
  227. if ((requiredButtons & closeButton) != 0) styleFlags |= ComponentPeer::windowHasCloseButton;
  228. return styleFlags;
  229. }
  230. void DocumentWindow::lookAndFeelChanged()
  231. {
  232. for (auto& b : titleBarButtons)
  233. b = nullptr;
  234. if (! isUsingNativeTitleBar())
  235. {
  236. LookAndFeel& lf = getLookAndFeel();
  237. if ((requiredButtons & minimiseButton) != 0) titleBarButtons[0] = lf.createDocumentWindowButton (minimiseButton);
  238. if ((requiredButtons & maximiseButton) != 0) titleBarButtons[1] = lf.createDocumentWindowButton (maximiseButton);
  239. if ((requiredButtons & closeButton) != 0) titleBarButtons[2] = lf.createDocumentWindowButton (closeButton);
  240. for (auto& b : titleBarButtons)
  241. {
  242. if (b != nullptr)
  243. {
  244. if (buttonListener == nullptr)
  245. buttonListener = new ButtonListenerProxy (*this);
  246. b->addListener (buttonListener);
  247. b->setWantsKeyboardFocus (false);
  248. // (call the Component method directly to avoid the assertion in ResizableWindow)
  249. Component::addAndMakeVisible (b);
  250. }
  251. }
  252. if (auto* b = getCloseButton())
  253. {
  254. #if JUCE_MAC
  255. b->addShortcut (KeyPress ('w', ModifierKeys::commandModifier, 0));
  256. #else
  257. b->addShortcut (KeyPress (KeyPress::F4Key, ModifierKeys::altModifier, 0));
  258. #endif
  259. }
  260. }
  261. activeWindowStatusChanged();
  262. ResizableWindow::lookAndFeelChanged();
  263. }
  264. void DocumentWindow::parentHierarchyChanged()
  265. {
  266. lookAndFeelChanged();
  267. }
  268. void DocumentWindow::activeWindowStatusChanged()
  269. {
  270. ResizableWindow::activeWindowStatusChanged();
  271. bool isActive = isActiveWindow();
  272. for (auto& b : titleBarButtons)
  273. if (b != nullptr)
  274. b->setEnabled (isActive);
  275. if (menuBar != nullptr)
  276. menuBar->setEnabled (isActive);
  277. }
  278. void DocumentWindow::mouseDoubleClick (const MouseEvent& e)
  279. {
  280. if (getTitleBarArea().contains (e.x, e.y))
  281. if (auto* maximise = getMaximiseButton())
  282. maximise->triggerClick();
  283. }
  284. void DocumentWindow::userTriedToCloseWindow()
  285. {
  286. closeButtonPressed();
  287. }
  288. } // namespace juce