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.

435 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_OpenDocumentManager.h"
  19. #include "jucer_FilePreviewComponent.h"
  20. #include "../Code Editor/jucer_SourceCodeEditor.h"
  21. #include "jucer_Application.h"
  22. //==============================================================================
  23. class UnknownDocument : public OpenDocumentManager::Document
  24. {
  25. public:
  26. UnknownDocument (Project* p, const File& f)
  27. : project (p), file (f)
  28. {
  29. reloadFromFile();
  30. }
  31. //==============================================================================
  32. struct Type : public OpenDocumentManager::DocumentType
  33. {
  34. bool canOpenFile (const File&) override { return true; }
  35. Document* openFile (Project* p, const File& f) override { return new UnknownDocument (p, f); }
  36. };
  37. //==============================================================================
  38. bool loadedOk() const override { return true; }
  39. bool isForFile (const File& f) const override { return file == f; }
  40. bool isForNode (const ValueTree&) const override { return false; }
  41. bool refersToProject (Project& p) const override { return project == &p; }
  42. Project* getProject() const override { return project; }
  43. bool needsSaving() const override { return false; }
  44. bool save() override { return true; }
  45. bool saveAs() override { return false; }
  46. bool hasFileBeenModifiedExternally() override { return fileModificationTime != file.getLastModificationTime(); }
  47. void reloadFromFile() override { fileModificationTime = file.getLastModificationTime(); }
  48. String getName() const override { return file.getFileName(); }
  49. File getFile() const override { return file; }
  50. Component* createEditor() override { return new ItemPreviewComponent (file); }
  51. Component* createViewer() override { return createEditor(); }
  52. void fileHasBeenRenamed (const File& newFile) override { file = newFile; }
  53. String getState() const override { return String(); }
  54. void restoreState (const String&) override {}
  55. String getType() const override
  56. {
  57. if (file.getFileExtension().isNotEmpty())
  58. return file.getFileExtension() + " file";
  59. jassertfalse;
  60. return "Unknown";
  61. }
  62. private:
  63. Project* const project;
  64. File file;
  65. Time fileModificationTime;
  66. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UnknownDocument)
  67. };
  68. //==============================================================================
  69. OpenDocumentManager::DocumentType* createGUIDocumentType();
  70. OpenDocumentManager::OpenDocumentManager()
  71. {
  72. registerType (new UnknownDocument::Type());
  73. registerType (new SourceCodeDocument::Type());
  74. registerType (createGUIDocumentType());
  75. }
  76. OpenDocumentManager::~OpenDocumentManager()
  77. {
  78. }
  79. void OpenDocumentManager::clear()
  80. {
  81. documents.clear();
  82. types.clear();
  83. }
  84. //==============================================================================
  85. void OpenDocumentManager::registerType (DocumentType* type, int index)
  86. {
  87. types.insert (index, type);
  88. }
  89. //==============================================================================
  90. void OpenDocumentManager::addListener (DocumentCloseListener* listener)
  91. {
  92. listeners.addIfNotAlreadyThere (listener);
  93. }
  94. void OpenDocumentManager::removeListener (DocumentCloseListener* listener)
  95. {
  96. listeners.removeFirstMatchingValue (listener);
  97. }
  98. //==============================================================================
  99. bool OpenDocumentManager::canOpenFile (const File& file)
  100. {
  101. for (int i = types.size(); --i >= 0;)
  102. if (types.getUnchecked(i)->canOpenFile (file))
  103. return true;
  104. return false;
  105. }
  106. OpenDocumentManager::Document* OpenDocumentManager::openFile (Project* project, const File& file)
  107. {
  108. for (int i = documents.size(); --i >= 0;)
  109. if (documents.getUnchecked(i)->isForFile (file))
  110. return documents.getUnchecked(i);
  111. Document* d = nullptr;
  112. for (int i = types.size(); --i >= 0 && d == nullptr;)
  113. {
  114. if (types.getUnchecked(i)->canOpenFile (file))
  115. {
  116. d = types.getUnchecked(i)->openFile (project, file);
  117. jassert (d != nullptr);
  118. }
  119. }
  120. jassert (d != nullptr); // should always at least have been picked up by UnknownDocument
  121. documents.add (d);
  122. ProjucerApplication::getCommandManager().commandStatusChanged();
  123. return d;
  124. }
  125. int OpenDocumentManager::getNumOpenDocuments() const
  126. {
  127. return documents.size();
  128. }
  129. OpenDocumentManager::Document* OpenDocumentManager::getOpenDocument (int index) const
  130. {
  131. return documents.getUnchecked (index);
  132. }
  133. FileBasedDocument::SaveResult OpenDocumentManager::saveIfNeededAndUserAgrees (OpenDocumentManager::Document* doc)
  134. {
  135. if (! doc->needsSaving())
  136. return FileBasedDocument::savedOk;
  137. const int r = AlertWindow::showYesNoCancelBox (AlertWindow::QuestionIcon,
  138. TRANS("Closing document..."),
  139. TRANS("Do you want to save the changes to \"")
  140. + doc->getName() + "\"?",
  141. TRANS("Save"),
  142. TRANS("Discard changes"),
  143. TRANS("Cancel"));
  144. if (r == 1) // save changes
  145. return doc->save() ? FileBasedDocument::savedOk
  146. : FileBasedDocument::failedToWriteToFile;
  147. if (r == 2) // discard changes
  148. return FileBasedDocument::savedOk;
  149. return FileBasedDocument::userCancelledSave;
  150. }
  151. bool OpenDocumentManager::closeDocument (int index, bool saveIfNeeded)
  152. {
  153. if (Document* doc = documents [index])
  154. {
  155. if (saveIfNeeded)
  156. if (saveIfNeededAndUserAgrees (doc) != FileBasedDocument::savedOk)
  157. return false;
  158. bool canClose = true;
  159. for (int i = listeners.size(); --i >= 0;)
  160. if (DocumentCloseListener* l = listeners[i])
  161. if (! l->documentAboutToClose (doc))
  162. canClose = false;
  163. if (! canClose)
  164. return false;
  165. documents.remove (index);
  166. ProjucerApplication::getCommandManager().commandStatusChanged();
  167. }
  168. return true;
  169. }
  170. bool OpenDocumentManager::closeDocument (Document* document, bool saveIfNeeded)
  171. {
  172. return closeDocument (documents.indexOf (document), saveIfNeeded);
  173. }
  174. void OpenDocumentManager::closeFile (const File& f, bool saveIfNeeded)
  175. {
  176. for (int i = documents.size(); --i >= 0;)
  177. if (Document* d = documents[i])
  178. if (d->isForFile (f))
  179. closeDocument (i, saveIfNeeded);
  180. }
  181. bool OpenDocumentManager::closeAll (bool askUserToSave)
  182. {
  183. for (int i = getNumOpenDocuments(); --i >= 0;)
  184. if (! closeDocument (i, askUserToSave))
  185. return false;
  186. return true;
  187. }
  188. bool OpenDocumentManager::closeAllDocumentsUsingProject (Project& project, bool saveIfNeeded)
  189. {
  190. for (int i = documents.size(); --i >= 0;)
  191. if (Document* d = documents[i])
  192. if (d->refersToProject (project))
  193. if (! closeDocument (i, saveIfNeeded))
  194. return false;
  195. return true;
  196. }
  197. bool OpenDocumentManager::anyFilesNeedSaving() const
  198. {
  199. for (int i = documents.size(); --i >= 0;)
  200. if (documents.getUnchecked (i)->needsSaving())
  201. return true;
  202. return false;
  203. }
  204. bool OpenDocumentManager::saveAll()
  205. {
  206. for (int i = documents.size(); --i >= 0;)
  207. {
  208. if (! documents.getUnchecked (i)->save())
  209. return false;
  210. ProjucerApplication::getApp().mainWindowList.updateAllWindowTitles();
  211. ProjucerApplication::getCommandManager().commandStatusChanged();
  212. }
  213. return true;
  214. }
  215. void OpenDocumentManager::reloadModifiedFiles()
  216. {
  217. for (int i = documents.size(); --i >= 0;)
  218. {
  219. Document* d = documents.getUnchecked (i);
  220. if (d->hasFileBeenModifiedExternally())
  221. d->reloadFromFile();
  222. }
  223. }
  224. void OpenDocumentManager::fileHasBeenRenamed (const File& oldFile, const File& newFile)
  225. {
  226. for (int i = documents.size(); --i >= 0;)
  227. {
  228. Document* d = documents.getUnchecked (i);
  229. if (d->isForFile (oldFile))
  230. d->fileHasBeenRenamed (newFile);
  231. }
  232. }
  233. //==============================================================================
  234. RecentDocumentList::RecentDocumentList()
  235. {
  236. ProjucerApplication::getApp().openDocumentManager.addListener (this);
  237. }
  238. RecentDocumentList::~RecentDocumentList()
  239. {
  240. ProjucerApplication::getApp().openDocumentManager.removeListener (this);
  241. }
  242. void RecentDocumentList::clear()
  243. {
  244. previousDocs.clear();
  245. nextDocs.clear();
  246. }
  247. void RecentDocumentList::newDocumentOpened (OpenDocumentManager::Document* document)
  248. {
  249. if (document != nullptr && document != getCurrentDocument())
  250. {
  251. nextDocs.clear();
  252. previousDocs.add (document);
  253. }
  254. }
  255. bool RecentDocumentList::canGoToPrevious() const
  256. {
  257. return previousDocs.size() > 1;
  258. }
  259. bool RecentDocumentList::canGoToNext() const
  260. {
  261. return nextDocs.size() > 0;
  262. }
  263. OpenDocumentManager::Document* RecentDocumentList::getPrevious()
  264. {
  265. if (! canGoToPrevious())
  266. return nullptr;
  267. nextDocs.insert (0, previousDocs.removeAndReturn (previousDocs.size() - 1));
  268. return previousDocs.getLast();
  269. }
  270. OpenDocumentManager::Document* RecentDocumentList::getNext()
  271. {
  272. if (! canGoToNext())
  273. return nullptr;
  274. OpenDocumentManager::Document* d = nextDocs.removeAndReturn (0);
  275. previousDocs.add (d);
  276. return d;
  277. }
  278. bool RecentDocumentList::contains (const File& f) const
  279. {
  280. for (int i = previousDocs.size(); --i >= 0;)
  281. if (previousDocs.getUnchecked(i)->getFile() == f)
  282. return true;
  283. return false;
  284. }
  285. OpenDocumentManager::Document* RecentDocumentList::getClosestPreviousDocOtherThan (OpenDocumentManager::Document* oneToAvoid) const
  286. {
  287. for (int i = previousDocs.size(); --i >= 0;)
  288. if (previousDocs.getUnchecked(i) != oneToAvoid)
  289. return previousDocs.getUnchecked(i);
  290. return nullptr;
  291. }
  292. bool RecentDocumentList::documentAboutToClose (OpenDocumentManager::Document* document)
  293. {
  294. previousDocs.removeAllInstancesOf (document);
  295. nextDocs.removeAllInstancesOf (document);
  296. jassert (! previousDocs.contains (document));
  297. jassert (! nextDocs.contains (document));
  298. return true;
  299. }
  300. static void restoreDocList (Project& project, Array <OpenDocumentManager::Document*>& list, const XmlElement* xml)
  301. {
  302. if (xml != nullptr)
  303. {
  304. OpenDocumentManager& odm = ProjucerApplication::getApp().openDocumentManager;
  305. forEachXmlChildElementWithTagName (*xml, e, "DOC")
  306. {
  307. const File file (e->getStringAttribute ("file"));
  308. if (file.exists())
  309. {
  310. if (OpenDocumentManager::Document* doc = odm.openFile (&project, file))
  311. {
  312. doc->restoreState (e->getStringAttribute ("state"));
  313. list.add (doc);
  314. }
  315. }
  316. }
  317. }
  318. }
  319. void RecentDocumentList::restoreFromXML (Project& project, const XmlElement& xml)
  320. {
  321. clear();
  322. if (xml.hasTagName ("RECENT_DOCUMENTS"))
  323. {
  324. restoreDocList (project, previousDocs, xml.getChildByName ("PREVIOUS"));
  325. restoreDocList (project, nextDocs, xml.getChildByName ("NEXT"));
  326. }
  327. }
  328. static void saveDocList (const Array <OpenDocumentManager::Document*>& list, XmlElement& xml)
  329. {
  330. for (int i = 0; i < list.size(); ++i)
  331. {
  332. const OpenDocumentManager::Document& doc = *list.getUnchecked(i);
  333. XmlElement* e = xml.createNewChildElement ("DOC");
  334. e->setAttribute ("file", doc.getFile().getFullPathName());
  335. e->setAttribute ("state", doc.getState());
  336. }
  337. }
  338. XmlElement* RecentDocumentList::createXML() const
  339. {
  340. XmlElement* xml = new XmlElement ("RECENT_DOCUMENTS");
  341. saveDocList (previousDocs, *xml->createNewChildElement ("PREVIOUS"));
  342. saveDocList (nextDocs, *xml->createNewChildElement ("NEXT"));
  343. return xml;
  344. }