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.

161 lines
5.9KB

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