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.

301 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 MultiDocumentPanel;
  16. //==============================================================================
  17. /**
  18. This is a derivative of DocumentWindow that is used inside a MultiDocumentPanel
  19. component.
  20. It's like a normal DocumentWindow but has some extra functionality to make sure
  21. everything works nicely inside a MultiDocumentPanel.
  22. @see MultiDocumentPanel
  23. @tags{GUI}
  24. */
  25. class JUCE_API MultiDocumentPanelWindow : public DocumentWindow
  26. {
  27. public:
  28. //==============================================================================
  29. /**
  30. */
  31. MultiDocumentPanelWindow (Colour backgroundColour);
  32. /** Destructor. */
  33. ~MultiDocumentPanelWindow() override;
  34. //==============================================================================
  35. /** @internal */
  36. void maximiseButtonPressed() override;
  37. /** @internal */
  38. void closeButtonPressed() override;
  39. /** @internal */
  40. void activeWindowStatusChanged() override;
  41. /** @internal */
  42. void broughtToFront() override;
  43. private:
  44. //==============================================================================
  45. void updateOrder();
  46. MultiDocumentPanel* getOwner() const noexcept;
  47. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiDocumentPanelWindow)
  48. };
  49. //==============================================================================
  50. /**
  51. A component that contains a set of other components either in floating windows
  52. or tabs.
  53. This acts as a panel that can be used to hold a set of open document windows, with
  54. different layout modes.
  55. Use addDocument() and closeDocument() to add or remove components from the
  56. panel - never use any of the Component methods to access the panel's child
  57. components directly, as these are managed internally.
  58. @tags{GUI}
  59. */
  60. class JUCE_API MultiDocumentPanel : public Component,
  61. private ComponentListener
  62. {
  63. public:
  64. //==============================================================================
  65. /** Creates an empty panel.
  66. Use addDocument() and closeDocument() to add or remove components from the
  67. panel - never use any of the Component methods to access the panel's child
  68. components directly, as these are managed internally.
  69. */
  70. MultiDocumentPanel();
  71. /** Destructor.
  72. When deleted, this will call closeAllDocuments (false) to make sure all its
  73. components are deleted. If you need to make sure all documents are saved
  74. before closing, then you should call closeAllDocuments (true) and check that
  75. it returns true before deleting the panel.
  76. */
  77. ~MultiDocumentPanel() override;
  78. //==============================================================================
  79. /** Tries to close all the documents.
  80. If checkItsOkToCloseFirst is true, then the tryToCloseDocument() method will
  81. be called for each open document, and any of these calls fails, this method
  82. will stop and return false, leaving some documents still open.
  83. If checkItsOkToCloseFirst is false, then all documents will be closed
  84. unconditionally.
  85. @see closeDocument
  86. */
  87. bool closeAllDocuments (bool checkItsOkToCloseFirst);
  88. /** Adds a document component to the panel.
  89. If the number of documents would exceed the limit set by setMaximumNumDocuments() then
  90. this will fail and return false. (If it does fail, the component passed-in will not be
  91. deleted, even if deleteWhenRemoved was set to true).
  92. The MultiDocumentPanel will deal with creating a window border to go around your component,
  93. so just pass in the bare content component here, no need to give it a ResizableWindow
  94. or DocumentWindow.
  95. @param component the component to add
  96. @param backgroundColour the background colour to use to fill the component's
  97. window or tab
  98. @param deleteWhenRemoved if true, then when the component is removed by closeDocument()
  99. or closeAllDocuments(), then it will be deleted. If false, then
  100. the caller must handle the component's deletion
  101. */
  102. bool addDocument (Component* component,
  103. Colour backgroundColour,
  104. bool deleteWhenRemoved);
  105. /** Closes one of the documents.
  106. If checkItsOkToCloseFirst is true, then the tryToCloseDocument() method will
  107. be called, and if it fails, this method will return false without closing the
  108. document.
  109. If checkItsOkToCloseFirst is false, then the documents will be closed
  110. unconditionally.
  111. The component will be deleted if the deleteWhenRemoved parameter was set to
  112. true when it was added with addDocument.
  113. @see addDocument, closeAllDocuments
  114. */
  115. bool closeDocument (Component* component,
  116. bool checkItsOkToCloseFirst);
  117. /** Returns the number of open document windows.
  118. @see getDocument
  119. */
  120. int getNumDocuments() const noexcept;
  121. /** Returns one of the open documents.
  122. The order of the documents in this array may change when they are added, removed
  123. or moved around.
  124. @see getNumDocuments
  125. */
  126. Component* getDocument (int index) const noexcept;
  127. /** Returns the document component that is currently focused or on top.
  128. If currently using floating windows, then this will be the component in the currently
  129. active window, or the top component if none are active.
  130. If it's currently in tabbed mode, then it'll return the component in the active tab.
  131. @see setActiveDocument
  132. */
  133. Component* getActiveDocument() const noexcept;
  134. /** Makes one of the components active and brings it to the top.
  135. @see getActiveDocument
  136. */
  137. void setActiveDocument (Component* component);
  138. /** Callback which gets invoked when the currently-active document changes. */
  139. virtual void activeDocumentChanged();
  140. /** Sets a limit on how many windows can be open at once.
  141. If this is zero or less there's no limit (the default). addDocument() will fail
  142. if this number is exceeded.
  143. */
  144. void setMaximumNumDocuments (int maximumNumDocuments);
  145. /** Sets an option to make the document fullscreen if there's only one document open.
  146. If set to true, then if there's only one document, it'll fill the whole of this
  147. component without tabs or a window border. If false, then tabs or a window
  148. will always be shown, even if there's only one document. If there's more than
  149. one document open, then this option makes no difference.
  150. */
  151. void useFullscreenWhenOneDocument (bool shouldUseTabs);
  152. /** Returns the result of the last time useFullscreenWhenOneDocument() was called.
  153. */
  154. bool isFullscreenWhenOneDocument() const noexcept;
  155. //==============================================================================
  156. /** The different layout modes available. */
  157. enum LayoutMode
  158. {
  159. FloatingWindows, /**< In this mode, there are overlapping DocumentWindow components for each document. */
  160. MaximisedWindowsWithTabs /**< In this mode, a TabbedComponent is used to show one document at a time. */
  161. };
  162. /** Changes the panel's mode.
  163. @see LayoutMode, getLayoutMode
  164. */
  165. void setLayoutMode (LayoutMode newLayoutMode);
  166. /** Returns the current layout mode. */
  167. LayoutMode getLayoutMode() const noexcept { return mode; }
  168. /** Sets the background colour for the whole panel.
  169. Each document has its own background colour, but this is the one used to fill the areas
  170. behind them.
  171. */
  172. void setBackgroundColour (Colour newBackgroundColour);
  173. /** Returns the current background colour.
  174. @see setBackgroundColour
  175. */
  176. Colour getBackgroundColour() const noexcept { return backgroundColour; }
  177. /** If the panel is being used in tabbed mode, this returns the TabbedComponent that's involved. */
  178. TabbedComponent* getCurrentTabbedComponent() const noexcept { return tabComponent.get(); }
  179. //==============================================================================
  180. /** A subclass must override this to say whether its currently ok for a document
  181. to be closed.
  182. This method is called by closeDocument() and closeAllDocuments() to indicate that
  183. a document should be saved if possible, ready for it to be closed.
  184. If this method returns true, then it means the document is ok and can be closed.
  185. If it returns false, then it means that the closeDocument() method should stop
  186. and not close.
  187. Normally, you'd use this method to ask the user if they want to save any changes,
  188. then return true if the save operation went ok. If the user cancelled the save
  189. operation you could return false here to abort the close operation.
  190. If your component is based on the FileBasedDocument class, then you'd probably want
  191. to call FileBasedDocument::saveIfNeededAndUserAgrees() and return true if this returned
  192. FileBasedDocument::savedOk
  193. @see closeDocument, FileBasedDocument::saveIfNeededAndUserAgrees()
  194. */
  195. virtual bool tryToCloseDocument (Component* component) = 0;
  196. /** Creates a new window to be used for a document.
  197. The default implementation of this just returns a basic MultiDocumentPanelWindow object,
  198. but you might want to override it to return a custom component.
  199. */
  200. virtual MultiDocumentPanelWindow* createNewDocumentWindow();
  201. //==============================================================================
  202. /** @internal */
  203. void paint (Graphics&) override;
  204. /** @internal */
  205. void resized() override;
  206. /** @internal */
  207. void componentNameChanged (Component&) override;
  208. private:
  209. //==============================================================================
  210. LayoutMode mode = MaximisedWindowsWithTabs;
  211. Array<Component*> components;
  212. std::unique_ptr<TabbedComponent> tabComponent;
  213. Colour backgroundColour { Colours::lightblue };
  214. int maximumNumDocuments = 0, numDocsBeforeTabsUsed = 0;
  215. struct TabbedComponentInternal;
  216. friend class MultiDocumentPanelWindow;
  217. Component* getContainerComp (Component*) const;
  218. void updateOrder();
  219. void addWindow (Component*);
  220. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiDocumentPanel)
  221. };
  222. } // namespace juce