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

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