The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

398 lines
13KB

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