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.

juce_MultiDocumentPanel.h 15KB

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