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.

371 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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 close all open documents 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 closeAllDocumentsAsync() with
  75. checkItsOkToCloseFirst == true and check the provided callback result is true
  76. before deleting the panel.
  77. */
  78. ~MultiDocumentPanel() override;
  79. //==============================================================================
  80. #if JUCE_MODAL_LOOPS_PERMITTED
  81. /** Tries to close all the documents.
  82. If checkItsOkToCloseFirst is true, then the tryToCloseDocument() method will
  83. be called for each open document, and any of these calls fails, this method
  84. will stop and return false, leaving some documents still open.
  85. If checkItsOkToCloseFirst is false, then all documents will be closed
  86. unconditionally.
  87. @see closeDocument
  88. */
  89. bool closeAllDocuments (bool checkItsOkToCloseFirst);
  90. #endif
  91. /** Tries to close all the documents.
  92. If checkItsOkToCloseFirst is true, then the tryToCloseDocumentAsync() method will
  93. be called for each open document, and any of these calls fails, this method
  94. will stop and provide an argument of false to the callback, leaving some documents
  95. still open.
  96. If checkItsOkToCloseFirst is false, then all documents will be closed
  97. unconditionally.
  98. @see closeDocumentAsync
  99. */
  100. void closeAllDocumentsAsync (bool checkItsOkToCloseFirst,
  101. std::function<void (bool)> callback);
  102. /** Adds a document component to the panel.
  103. If the number of documents would exceed the limit set by setMaximumNumDocuments() then
  104. this will fail and return false. (If it does fail, the component passed-in will not be
  105. deleted, even if deleteWhenRemoved was set to true).
  106. The MultiDocumentPanel will deal with creating a window border to go around your component,
  107. so just pass in the bare content component here, no need to give it a ResizableWindow
  108. or DocumentWindow.
  109. @param component the component to add
  110. @param backgroundColour the background colour to use to fill the component's
  111. window or tab
  112. @param deleteWhenRemoved if true, then when the component is removed by closeDocumentAsync()
  113. or closeAllDocumentsAsync(), then it will be deleted. If false, then
  114. the caller must handle the component's deletion
  115. */
  116. bool addDocument (Component* component,
  117. Colour backgroundColour,
  118. bool deleteWhenRemoved);
  119. #if JUCE_MODAL_LOOPS_PERMITTED
  120. /** Closes one of the documents.
  121. If checkItsOkToCloseFirst is true, then the tryToCloseDocument() method will
  122. be called, and if it fails, this method will return false without closing the
  123. document.
  124. If checkItsOkToCloseFirst is false, then the documents will be closed
  125. unconditionally.
  126. The component will be deleted if the deleteWhenRemoved parameter was set to
  127. true when it was added with addDocument.
  128. @see addDocument, closeAllDocuments
  129. */
  130. bool closeDocument (Component* component,
  131. bool checkItsOkToCloseFirst);
  132. #endif
  133. /** Closes one of the documents.
  134. If checkItsOkToCloseFirst is true, then the tryToCloseDocumentAsync() method will
  135. be called, and if it fails, this method will call the callback with a false
  136. argument without closing the document.
  137. If checkItsOkToCloseFirst is false, then the documents will be closed
  138. unconditionally.
  139. The component will be deleted if the deleteWhenRemoved parameter was set to
  140. true when it was added with addDocument.
  141. @see addDocument, closeAllDocumentsAsync
  142. */
  143. void closeDocumentAsync (Component* component,
  144. bool checkItsOkToCloseFirst,
  145. std::function<void (bool)> callback);
  146. /** Returns the number of open document windows.
  147. @see getDocument
  148. */
  149. int getNumDocuments() const noexcept;
  150. /** Returns one of the open documents.
  151. The order of the documents in this array may change when they are added, removed
  152. or moved around.
  153. @see getNumDocuments
  154. */
  155. Component* getDocument (int index) const noexcept;
  156. /** Returns the document component that is currently focused or on top.
  157. If currently using floating windows, then this will be the component in the currently
  158. active window, or the top component if none are active.
  159. If it's currently in tabbed mode, then it'll return the component in the active tab.
  160. @see setActiveDocument
  161. */
  162. Component* getActiveDocument() const noexcept;
  163. /** Makes one of the components active and brings it to the top.
  164. @see getActiveDocument
  165. */
  166. void setActiveDocument (Component* component);
  167. /** Callback which gets invoked when the currently-active document changes. */
  168. virtual void activeDocumentChanged();
  169. /** Sets a limit on how many windows can be open at once.
  170. If this is zero or less there's no limit (the default). addDocument() will fail
  171. if this number is exceeded.
  172. */
  173. void setMaximumNumDocuments (int maximumNumDocuments);
  174. /** Sets an option to make the document fullscreen if there's only one document open.
  175. If set to true, then if there's only one document, it'll fill the whole of this
  176. component without tabs or a window border. If false, then tabs or a window
  177. will always be shown, even if there's only one document. If there's more than
  178. one document open, then this option makes no difference.
  179. */
  180. void useFullscreenWhenOneDocument (bool shouldUseTabs);
  181. /** Returns the result of the last time useFullscreenWhenOneDocument() was called.
  182. */
  183. bool isFullscreenWhenOneDocument() const noexcept;
  184. //==============================================================================
  185. /** The different layout modes available. */
  186. enum LayoutMode
  187. {
  188. FloatingWindows, /**< In this mode, there are overlapping DocumentWindow components for each document. */
  189. MaximisedWindowsWithTabs /**< In this mode, a TabbedComponent is used to show one document at a time. */
  190. };
  191. /** Changes the panel's mode.
  192. @see LayoutMode, getLayoutMode
  193. */
  194. void setLayoutMode (LayoutMode newLayoutMode);
  195. /** Returns the current layout mode. */
  196. LayoutMode getLayoutMode() const noexcept { return mode; }
  197. /** Sets the background colour for the whole panel.
  198. Each document has its own background colour, but this is the one used to fill the areas
  199. behind them.
  200. */
  201. void setBackgroundColour (Colour newBackgroundColour);
  202. /** Returns the current background colour.
  203. @see setBackgroundColour
  204. */
  205. Colour getBackgroundColour() const noexcept { return backgroundColour; }
  206. /** If the panel is being used in tabbed mode, this returns the TabbedComponent that's involved. */
  207. TabbedComponent* getCurrentTabbedComponent() const noexcept { return tabComponent.get(); }
  208. //==============================================================================
  209. #if JUCE_MODAL_LOOPS_PERMITTED
  210. /** A subclass must override this to say whether its currently ok for a document
  211. to be closed.
  212. This method is called by closeDocument() and closeAllDocuments() to indicate that
  213. a document should be saved if possible, ready for it to be closed.
  214. If this method returns true, then it means the document is ok and can be closed.
  215. If it returns false, then it means that the closeDocument() method should stop
  216. and not close.
  217. Normally, you'd use this method to ask the user if they want to save any changes,
  218. then return true if the save operation went ok. If the user cancelled the save
  219. operation you could return false here to abort the close operation.
  220. If your component is based on the FileBasedDocument class, then you'd probably want
  221. to call FileBasedDocument::saveIfNeededAndUserAgrees() and return true if this returned
  222. FileBasedDocument::savedOk
  223. @see closeDocument, FileBasedDocument::saveIfNeededAndUserAgrees()
  224. */
  225. virtual bool tryToCloseDocument (Component* component);
  226. #endif
  227. /** A subclass must override this to say whether its currently ok for a document
  228. to be closed.
  229. This method is called by closeDocumentAsync() and closeAllDocumentsAsync()
  230. to indicate that a document should be saved if possible, ready for it to be closed.
  231. If the callback is called with a true argument, then it means the document is ok
  232. and can be closed.
  233. If the callback is called with a false argument, then it means that the
  234. closeDocumentAsync() method should stop and not close.
  235. Normally, you'd use this method to ask the user if they want to save any changes,
  236. then return true if the save operation went ok. If the user cancelled the save
  237. operation you could give a value of false to the callback to abort the close operation.
  238. If your component is based on the FileBasedDocument class, then you'd probably want
  239. to call FileBasedDocument::saveIfNeededAndUserAgreesAsync() and call the callback with
  240. true if this returned FileBasedDocument::savedOk.
  241. @see closeDocumentAsync, FileBasedDocument::saveIfNeededAndUserAgreesAsync()
  242. */
  243. virtual void tryToCloseDocumentAsync (Component* component, std::function<void (bool)> callback) = 0;
  244. /** Creates a new window to be used for a document.
  245. The default implementation of this just returns a basic MultiDocumentPanelWindow object,
  246. but you might want to override it to return a custom component.
  247. */
  248. virtual MultiDocumentPanelWindow* createNewDocumentWindow();
  249. //==============================================================================
  250. /** @internal */
  251. void paint (Graphics&) override;
  252. /** @internal */
  253. void resized() override;
  254. /** @internal */
  255. void componentNameChanged (Component&) override;
  256. private:
  257. //==============================================================================
  258. void closeDocumentInternal (Component*);
  259. static void closeLastDocumentRecursive (SafePointer<MultiDocumentPanel>,
  260. bool,
  261. std::function<void (bool)>);
  262. //==============================================================================
  263. struct TabbedComponentInternal;
  264. friend class MultiDocumentPanelWindow;
  265. Component* getContainerComp (Component*) const;
  266. void updateOrder();
  267. void addWindow (Component*);
  268. LayoutMode mode = MaximisedWindowsWithTabs;
  269. Array<Component*> components;
  270. std::unique_ptr<TabbedComponent> tabComponent;
  271. Colour backgroundColour { Colours::lightblue };
  272. int maximumNumDocuments = 0, numDocsBeforeTabsUsed = 0;
  273. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiDocumentPanel)
  274. };
  275. } // namespace juce