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.

296 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A resizable window with a title bar and maximise, minimise and close buttons.
  23. This subclass of ResizableWindow creates a fairly standard type of window with
  24. a title bar and various buttons. The name of the component is shown in the
  25. title bar, and an icon can optionally be specified with setIcon().
  26. All the methods available to a ResizableWindow are also available to this,
  27. so it can easily be made resizable, minimised, maximised, etc.
  28. It's not advisable to add child components directly to a DocumentWindow: put them
  29. inside your content component instead. And overriding methods like resized(), moved(), etc
  30. is also not recommended - instead override these methods for your content component.
  31. (If for some obscure reason you do need to override these methods, always remember to
  32. call the super-class's resized() method too, otherwise it'll fail to lay out the window
  33. decorations correctly).
  34. You can also automatically add a menu bar to the window, using the setMenuBar()
  35. method.
  36. @see ResizableWindow, DialogWindow
  37. @tags{GUI}
  38. */
  39. class JUCE_API DocumentWindow : public ResizableWindow
  40. {
  41. public:
  42. //==============================================================================
  43. /** The set of available button-types that can be put on the title bar.
  44. @see setTitleBarButtonsRequired
  45. */
  46. enum TitleBarButtons
  47. {
  48. minimiseButton = 1,
  49. maximiseButton = 2,
  50. closeButton = 4,
  51. /** A combination of all the buttons above. */
  52. allButtons = 7
  53. };
  54. //==============================================================================
  55. /** Creates a DocumentWindow.
  56. @param name the name to give the component - this is also
  57. the title shown at the top of the window. To change
  58. this later, use setName()
  59. @param backgroundColour the colour to use for filling the window's background.
  60. @param requiredButtons specifies which of the buttons (close, minimise, maximise)
  61. should be shown on the title bar. This value is a bitwise
  62. combination of values from the TitleBarButtons enum. Note
  63. that it can be "allButtons" to get them all. You
  64. can change this later with the setTitleBarButtonsRequired()
  65. method, which can also specify where they are positioned.
  66. @param addToDesktop if true, the window will be automatically added to the
  67. desktop; if false, you can use it as a child component
  68. @see TitleBarButtons
  69. */
  70. DocumentWindow (const String& name,
  71. Colour backgroundColour,
  72. int requiredButtons,
  73. bool addToDesktop = true);
  74. /** Destructor.
  75. If a content component has been set with setContentOwned(), it will be deleted.
  76. */
  77. ~DocumentWindow() override;
  78. //==============================================================================
  79. /** Changes the component's name.
  80. (This is overridden from Component::setName() to cause a repaint, as
  81. the name is what gets drawn across the window's title bar).
  82. */
  83. void setName (const String& newName) override;
  84. /** Sets an icon to show in the title bar, next to the title.
  85. A copy is made internally of the image, so the caller can delete the
  86. image after calling this. If an empty Image is passed-in, any existing icon
  87. will be removed.
  88. */
  89. void setIcon (const Image& imageToUse);
  90. /** Changes the height of the title-bar. */
  91. void setTitleBarHeight (int newHeight);
  92. /** Returns the current title bar height. */
  93. int getTitleBarHeight() const;
  94. /** Changes the set of title-bar buttons being shown.
  95. @param requiredButtons specifies which of the buttons (close, minimise, maximise)
  96. should be shown on the title bar. This value is a bitwise
  97. combination of values from the TitleBarButtons enum. Note
  98. that it can be "allButtons" to get them all.
  99. @param positionTitleBarButtonsOnLeft if true, the buttons should go at the
  100. left side of the bar; if false, they'll be placed at the right
  101. */
  102. void setTitleBarButtonsRequired (int requiredButtons,
  103. bool positionTitleBarButtonsOnLeft);
  104. /** Sets whether the title should be centred within the window.
  105. If true, the title text is shown in the middle of the title-bar; if false,
  106. it'll be shown at the left of the bar.
  107. */
  108. void setTitleBarTextCentred (bool textShouldBeCentred);
  109. //==============================================================================
  110. /** Creates a menu inside this window.
  111. @param menuBarModel this specifies a MenuBarModel that should be used to
  112. generate the contents of a menu bar that will be placed
  113. just below the title bar, and just above any content
  114. component. If this value is a nullptr, any existing menu bar
  115. will be removed from the component; if it is not a nullptr,
  116. one will be added if it's required.
  117. @param menuBarHeight the height of the menu bar component, if one is needed. Pass a value of zero
  118. or less to use the look-and-feel's default size.
  119. */
  120. void setMenuBar (MenuBarModel* menuBarModel,
  121. int menuBarHeight = 0);
  122. /** Returns the current menu bar component, or null if there isn't one.
  123. This is probably a MenuBarComponent, unless a custom one has been set using
  124. setMenuBarComponent().
  125. */
  126. Component* getMenuBarComponent() const noexcept;
  127. /** Replaces the current menu bar with a custom component.
  128. The component will be owned and deleted by the document window.
  129. */
  130. void setMenuBarComponent (Component* newMenuBarComponent);
  131. //==============================================================================
  132. /** This method is called when the user tries to close the window.
  133. This is triggered by the user clicking the close button, or using some other
  134. OS-specific key shortcut or OS menu for getting rid of a window.
  135. If the window is just a pop-up, you should override this closeButtonPressed()
  136. method and make it delete the window in whatever way is appropriate for your
  137. app. E.g. you might just want to call "delete this".
  138. If your app is centred around this window such that the whole app should quit when
  139. the window is closed, then you will probably want to use this method as an opportunity
  140. to call JUCEApplicationBase::quit(), and leave the window to be deleted later by your
  141. JUCEApplicationBase::shutdown() method. (Doing it this way means that your window will
  142. still get cleaned-up if the app is quit by some other means (e.g. a cmd-Q on the mac
  143. or closing it via the taskbar icon on Windows).
  144. (Note that the DocumentWindow class overrides Component::userTriedToCloseWindow() and
  145. redirects it to call this method, so any methods of closing the window that are
  146. caught by userTriedToCloseWindow() will also end up here).
  147. */
  148. virtual void closeButtonPressed();
  149. /** Callback that is triggered when the minimise button is pressed.
  150. The default implementation of this calls ResizableWindow::setMinimised(), but
  151. you can override it to do more customised behaviour.
  152. */
  153. virtual void minimiseButtonPressed();
  154. /** Callback that is triggered when the maximise button is pressed, or when the
  155. title-bar is double-clicked.
  156. The default implementation of this calls ResizableWindow::setFullScreen(), but
  157. you can override it to do more customised behaviour.
  158. */
  159. virtual void maximiseButtonPressed();
  160. //==============================================================================
  161. /** Returns the close button, (or nullptr if there isn't one). */
  162. Button* getCloseButton() const noexcept;
  163. /** Returns the minimise button, (or nullptr if there isn't one). */
  164. Button* getMinimiseButton() const noexcept;
  165. /** Returns the maximise button, (or nullptr if there isn't one). */
  166. Button* getMaximiseButton() const noexcept;
  167. //==============================================================================
  168. /** A set of colour IDs to use to change the colour of various aspects of the window.
  169. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  170. methods.
  171. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  172. */
  173. enum ColourIds
  174. {
  175. textColourId = 0x1005701, /**< The colour to draw any text with. It's up to the look
  176. and feel class how this is used. */
  177. };
  178. //==============================================================================
  179. /** This abstract base class is implemented by LookAndFeel classes to provide
  180. window drawing functionality.
  181. */
  182. struct JUCE_API LookAndFeelMethods
  183. {
  184. virtual ~LookAndFeelMethods() = default;
  185. virtual void drawDocumentWindowTitleBar (DocumentWindow&,
  186. Graphics&, int w, int h,
  187. int titleSpaceX, int titleSpaceW,
  188. const Image* icon,
  189. bool drawTitleTextOnLeft) = 0;
  190. virtual Button* createDocumentWindowButton (int buttonType) = 0;
  191. virtual void positionDocumentWindowButtons (DocumentWindow&,
  192. int titleBarX, int titleBarY, int titleBarW, int titleBarH,
  193. Button* minimiseButton,
  194. Button* maximiseButton,
  195. Button* closeButton,
  196. bool positionTitleBarButtonsOnLeft) = 0;
  197. };
  198. //==============================================================================
  199. #ifndef DOXYGEN
  200. /** @internal */
  201. void paint (Graphics&) override;
  202. /** @internal */
  203. void resized() override;
  204. /** @internal */
  205. void lookAndFeelChanged() override;
  206. /** @internal */
  207. BorderSize<int> getBorderThickness() override;
  208. /** @internal */
  209. BorderSize<int> getContentComponentBorder() override;
  210. /** @internal */
  211. void mouseDoubleClick (const MouseEvent&) override;
  212. /** @internal */
  213. void userTriedToCloseWindow() override;
  214. /** @internal */
  215. void activeWindowStatusChanged() override;
  216. /** @internal */
  217. int getDesktopWindowStyleFlags() const override;
  218. /** @internal */
  219. void parentHierarchyChanged() override;
  220. /** @internal */
  221. Rectangle<int> getTitleBarArea();
  222. #endif
  223. private:
  224. //==============================================================================
  225. int titleBarHeight = 26, menuBarHeight = 24, requiredButtons;
  226. bool positionTitleBarButtonsOnLeft, drawTitleTextCentred = true;
  227. std::unique_ptr<Button> titleBarButtons [3];
  228. Image titleBarIcon;
  229. std::unique_ptr<Component> menuBar;
  230. MenuBarModel* menuBarModel = nullptr;
  231. class ButtonListenerProxy;
  232. std::unique_ptr<ButtonListenerProxy> buttonListener;
  233. void repaintTitleBar();
  234. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DocumentWindow)
  235. };
  236. } // namespace juce