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 12KB

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