The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

167 lines
6.2KB

  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. #pragma once
  19. #include "../Project/jucer_Project.h"
  20. //==============================================================================
  21. class OpenDocumentManager
  22. {
  23. public:
  24. //==============================================================================
  25. OpenDocumentManager();
  26. ~OpenDocumentManager();
  27. //==============================================================================
  28. class Document
  29. {
  30. public:
  31. Document() {}
  32. virtual ~Document() {}
  33. virtual bool loadedOk() const = 0;
  34. virtual bool isForFile (const File& file) const = 0;
  35. virtual bool isForNode (const ValueTree& node) const = 0;
  36. virtual bool refersToProject (Project& project) const = 0;
  37. virtual Project* getProject() const = 0;
  38. virtual String getName() const = 0;
  39. virtual String getType() const = 0;
  40. virtual File getFile() const = 0;
  41. virtual bool needsSaving() const = 0;
  42. virtual bool saveSyncWithoutAsking() = 0;
  43. virtual void saveAsync (std::function<void (bool)>) = 0;
  44. virtual void saveAsAsync (std::function<void (bool)>) = 0;
  45. virtual bool hasFileBeenModifiedExternally() = 0;
  46. virtual void reloadFromFile() = 0;
  47. virtual std::unique_ptr<Component> createEditor() = 0;
  48. virtual std::unique_ptr<Component> createViewer() = 0;
  49. virtual void fileHasBeenRenamed (const File& newFile) = 0;
  50. virtual String getState() const = 0;
  51. virtual void restoreState (const String& state) = 0;
  52. virtual File getCounterpartFile() const { return {}; }
  53. };
  54. //==============================================================================
  55. int getNumOpenDocuments() const;
  56. Document* getOpenDocument (int index) const;
  57. void clear();
  58. enum class SaveIfNeeded { no, yes };
  59. bool canOpenFile (const File& file);
  60. Document* openFile (Project* project, const File& file);
  61. void closeDocumentAsync (Document* document, SaveIfNeeded saveIfNeeded, std::function<void (bool)> callback);
  62. bool closeDocumentWithoutSaving (Document* document);
  63. void closeAllAsync (SaveIfNeeded askUserToSave, std::function<void (bool)> callback);
  64. void closeAllDocumentsUsingProjectAsync (Project& project, SaveIfNeeded askUserToSave, std::function<void (bool)> callback);
  65. void closeAllDocumentsUsingProjectWithoutSaving (Project& project);
  66. void closeFileWithoutSaving (const File& f);
  67. bool anyFilesNeedSaving() const;
  68. void saveAllSyncWithoutAsking();
  69. void saveIfNeededAndUserAgrees (Document* doc, std::function<void (FileBasedDocument::SaveResult)>);
  70. void reloadModifiedFiles();
  71. void fileHasBeenRenamed (const File& oldFile, const File& newFile);
  72. //==============================================================================
  73. class DocumentCloseListener
  74. {
  75. public:
  76. DocumentCloseListener() {}
  77. virtual ~DocumentCloseListener() {}
  78. // return false to force it to stop.
  79. virtual bool documentAboutToClose (Document* document) = 0;
  80. };
  81. void addListener (DocumentCloseListener*);
  82. void removeListener (DocumentCloseListener*);
  83. //==============================================================================
  84. class DocumentType
  85. {
  86. public:
  87. virtual ~DocumentType() = default;
  88. virtual bool canOpenFile (const File& file) = 0;
  89. virtual Document* openFile (Project* project, const File& file) = 0;
  90. };
  91. void registerType (DocumentType* type, int index = -1);
  92. private:
  93. //==============================================================================
  94. void closeLastDocumentUsingProjectRecursive (WeakReference<OpenDocumentManager>,
  95. Project*,
  96. SaveIfNeeded,
  97. std::function<void (bool)>);
  98. //==============================================================================
  99. OwnedArray<DocumentType> types;
  100. OwnedArray<Document> documents;
  101. Array<DocumentCloseListener*> listeners;
  102. ScopedMessageBox messageBox;
  103. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenDocumentManager)
  104. JUCE_DECLARE_WEAK_REFERENCEABLE (OpenDocumentManager)
  105. };
  106. //==============================================================================
  107. class RecentDocumentList final : private OpenDocumentManager::DocumentCloseListener
  108. {
  109. public:
  110. RecentDocumentList();
  111. ~RecentDocumentList();
  112. void clear();
  113. void newDocumentOpened (OpenDocumentManager::Document* document);
  114. OpenDocumentManager::Document* getCurrentDocument() const { return previousDocs.getLast(); }
  115. bool canGoToPrevious() const;
  116. bool canGoToNext() const;
  117. bool contains (const File&) const;
  118. OpenDocumentManager::Document* getPrevious();
  119. OpenDocumentManager::Document* getNext();
  120. OpenDocumentManager::Document* getClosestPreviousDocOtherThan (OpenDocumentManager::Document* oneToAvoid) const;
  121. void restoreFromXML (Project& project, const XmlElement& xml);
  122. std::unique_ptr<XmlElement> createXML() const;
  123. private:
  124. bool documentAboutToClose (OpenDocumentManager::Document*);
  125. Array<OpenDocumentManager::Document*> previousDocs, nextDocs;
  126. };