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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. g.setColour (getBackgroundColour().overlaidWith (Colour (0x80000000)));
  157. const BorderSize<int> border (getBorderThickness());
  158. g.fillRect (0, 0, getWidth(), border.getTop());
  159. g.fillRect (0, border.getTop(), border.getLeft(), getHeight() - border.getTopAndBottom());
  160. g.fillRect (getWidth() - border.getRight(), border.getTop(), border.getRight(), getHeight() - border.getTopAndBottom());
  161. g.fillRect (0, getHeight() - border.getBottom(), getWidth(), border.getBottom());
  162. }
  163. const Rectangle<int> titleBarArea (getTitleBarArea());
  164. g.reduceClipRegion (titleBarArea);
  165. g.setOrigin (titleBarArea.getPosition());
  166. int titleSpaceX1 = 6;
  167. int titleSpaceX2 = titleBarArea.getWidth() - 6;
  168. for (int i = 0; i < 3; ++i)
  169. {
  170. if (Button* const b = titleBarButtons[i])
  171. {
  172. if (positionTitleBarButtonsOnLeft)
  173. titleSpaceX1 = jmax (titleSpaceX1, b->getRight() + (getWidth() - b->getRight()) / 8);
  174. else
  175. titleSpaceX2 = jmin (titleSpaceX2, b->getX() - (b->getX() / 8));
  176. }
  177. }
  178. getLookAndFeel().drawDocumentWindowTitleBar (*this, g,
  179. titleBarArea.getWidth(),
  180. titleBarArea.getHeight(),
  181. titleSpaceX1,
  182. jmax (1, titleSpaceX2 - titleSpaceX1),
  183. titleBarIcon.isValid() ? &titleBarIcon : 0,
  184. ! drawTitleTextCentred);
  185. }
  186. void DocumentWindow::resized()
  187. {
  188. ResizableWindow::resized();
  189. if (Button* const b = getMaximiseButton())
  190. b->setToggleState (isFullScreen(), dontSendNotification);
  191. const Rectangle<int> titleBarArea (getTitleBarArea());
  192. getLookAndFeel()
  193. .positionDocumentWindowButtons (*this,
  194. titleBarArea.getX(), titleBarArea.getY(),
  195. titleBarArea.getWidth(), titleBarArea.getHeight(),
  196. titleBarButtons[0],
  197. titleBarButtons[1],
  198. titleBarButtons[2],
  199. positionTitleBarButtonsOnLeft);
  200. if (menuBar != nullptr)
  201. menuBar->setBounds (titleBarArea.getX(), titleBarArea.getBottom(),
  202. titleBarArea.getWidth(), menuBarHeight);
  203. }
  204. BorderSize<int> DocumentWindow::getBorderThickness()
  205. {
  206. return BorderSize<int> ((isFullScreen() || isUsingNativeTitleBar())
  207. ? 0 : (resizableBorder != nullptr ? 4 : 1));
  208. }
  209. BorderSize<int> DocumentWindow::getContentComponentBorder()
  210. {
  211. BorderSize<int> border (getBorderThickness());
  212. border.setTop (border.getTop()
  213. + (isUsingNativeTitleBar() ? 0 : titleBarHeight)
  214. + (menuBar != nullptr ? menuBarHeight : 0));
  215. return border;
  216. }
  217. int DocumentWindow::getTitleBarHeight() const
  218. {
  219. return isUsingNativeTitleBar() ? 0 : jmin (titleBarHeight, getHeight() - 4);
  220. }
  221. Rectangle<int> DocumentWindow::getTitleBarArea()
  222. {
  223. const BorderSize<int> border (getBorderThickness());
  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. }