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.

289 lines
13KB

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