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.

197 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_OPENDOCUMENTMANAGER_JUCEHEADER__
  19. #define __JUCER_OPENDOCUMENTMANAGER_JUCEHEADER__
  20. #include "../Project/jucer_Project.h"
  21. //==============================================================================
  22. /**
  23. */
  24. class OpenDocumentManager
  25. {
  26. public:
  27. //==============================================================================
  28. OpenDocumentManager();
  29. ~OpenDocumentManager();
  30. juce_DeclareSingleton_SingleThreaded_Minimal (OpenDocumentManager);
  31. //==============================================================================
  32. class Document
  33. {
  34. public:
  35. Document() {}
  36. virtual ~Document() {}
  37. virtual bool loadedOk() const = 0;
  38. virtual bool isForFile (const File& file) const = 0;
  39. virtual bool isForNode (const ValueTree& node) const = 0;
  40. virtual bool refersToProject (Project& project) const = 0;
  41. virtual Project* getProject() const = 0;
  42. virtual String getName() const = 0;
  43. virtual String getType() const = 0;
  44. virtual File getFile() const = 0;
  45. virtual bool needsSaving() const = 0;
  46. virtual bool save() = 0;
  47. virtual bool canSaveAs() const = 0;
  48. virtual bool saveAs() = 0;
  49. virtual bool hasFileBeenModifiedExternally() = 0;
  50. virtual void reloadFromFile() = 0;
  51. virtual Component* createEditor() = 0;
  52. virtual Component* createViewer() = 0;
  53. virtual void fileHasBeenRenamed (const File& newFile) = 0;
  54. };
  55. //==============================================================================
  56. int getNumOpenDocuments() const;
  57. Document* getOpenDocument (int index) const;
  58. void moveDocumentToTopOfStack (Document* doc);
  59. bool canOpenFile (const File& file);
  60. Document* openFile (Project* project, const File& file);
  61. bool closeDocument (int index, bool saveIfNeeded);
  62. bool closeDocument (Document* document, bool saveIfNeeded);
  63. bool closeAll (bool askUserToSave);
  64. bool closeAllDocumentsUsingProject (Project& project, bool saveIfNeeded);
  65. void closeFile (const File& f, bool saveIfNeeded);
  66. bool anyFilesNeedSaving() const;
  67. bool saveAll();
  68. FileBasedDocument::SaveResult saveIfNeededAndUserAgrees (Document* doc);
  69. void reloadModifiedFiles();
  70. void fileHasBeenRenamed (const File& oldFile, const File& newFile);
  71. //==============================================================================
  72. class DocumentCloseListener
  73. {
  74. public:
  75. DocumentCloseListener() {}
  76. virtual ~DocumentCloseListener() {}
  77. virtual void documentAboutToClose (Document* document) = 0;
  78. };
  79. void addListener (DocumentCloseListener* listener);
  80. void removeListener (DocumentCloseListener* listener);
  81. //==============================================================================
  82. class DocumentType
  83. {
  84. public:
  85. DocumentType() {}
  86. virtual ~DocumentType() {}
  87. virtual bool canOpenFile (const File& file) = 0;
  88. virtual Document* openFile (Project* project, const File& file) = 0;
  89. };
  90. void registerType (DocumentType* type);
  91. private:
  92. OwnedArray <DocumentType> types;
  93. OwnedArray <Document> documents;
  94. Array <DocumentCloseListener*> listeners;
  95. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenDocumentManager);
  96. };
  97. //==============================================================================
  98. class SourceCodeDocument : public OpenDocumentManager::Document
  99. {
  100. public:
  101. //==============================================================================
  102. SourceCodeDocument (Project* project_, const File& file_)
  103. : modDetector (file_), project (project_)
  104. {
  105. codeDoc = new CodeDocument();
  106. reloadFromFile();
  107. }
  108. //==============================================================================
  109. struct Type : public OpenDocumentManager::DocumentType
  110. {
  111. bool canOpenFile (const File& file) { return file.hasFileExtension ("cpp;h;hpp;mm;m;c;cc;cxx;txt;xml;plist;rtf;html;htm;php;py;rb;cs"); }
  112. Document* openFile (Project* project, const File& file) { return new SourceCodeDocument (project, file); }
  113. };
  114. //==============================================================================
  115. bool loadedOk() const { return true; }
  116. bool isForFile (const File& file) const { return getFile() == file; }
  117. bool isForNode (const ValueTree& node) const { return false; }
  118. bool refersToProject (Project& p) const { return project == &p; }
  119. Project* getProject() const { return project; }
  120. bool canSaveAs() const { return true; }
  121. String getName() const { return getFile().getFileName(); }
  122. String getType() const { return getFile().getFileExtension() + " file"; }
  123. File getFile() const { return modDetector.getFile(); }
  124. bool needsSaving() const { return codeDoc != nullptr && codeDoc->hasChangedSinceSavePoint(); }
  125. bool hasFileBeenModifiedExternally() { return modDetector.hasBeenModified(); }
  126. void fileHasBeenRenamed (const File& newFile) { modDetector.fileHasBeenRenamed (newFile); }
  127. void reloadFromFile()
  128. {
  129. modDetector.updateHash();
  130. ScopedPointer <InputStream> in (modDetector.getFile().createInputStream());
  131. if (in != nullptr)
  132. codeDoc->loadFromStream (*in);
  133. }
  134. bool save()
  135. {
  136. TemporaryFile temp (modDetector.getFile());
  137. ScopedPointer <FileOutputStream> out (temp.getFile().createOutputStream());
  138. if (out == nullptr || ! codeDoc->writeToStream (*out))
  139. return false;
  140. out = nullptr;
  141. if (! temp.overwriteTargetFileWithTemporary())
  142. return false;
  143. modDetector.updateHash();
  144. return true;
  145. }
  146. bool saveAs()
  147. {
  148. jassertfalse; //xxx todo
  149. return false;
  150. }
  151. Component* createEditor();
  152. Component* createViewer() { return createEditor(); }
  153. protected:
  154. FileModificationDetector modDetector;
  155. ScopedPointer <CodeDocument> codeDoc;
  156. Project* project;
  157. };
  158. #endif // __JUCER_OPENDOCUMENTMANAGER_JUCEHEADER__