From f2426cc7dee4c8ca941fdc278ab36833d4f605a6 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 12 Jun 2012 11:54:32 +0100 Subject: [PATCH] Minor additions to OptionalScopedPointer. Internal introjucer tweaks. --- .../Source/Application/jucer_Application.h | 2 + .../Application/jucer_OpenDocumentManager.cpp | 73 +----------------- .../Application/jucer_OpenDocumentManager.h | 74 ++++++++++++++++++- .../Code Editor/jucer_SourceCodeEditor.cpp | 18 +++-- .../Code Editor/jucer_SourceCodeEditor.h | 2 + .../Source/Project/jucer_Project.cpp | 7 ++ .../memory/juce_OptionalScopedPointer.h | 21 ++++++ 7 files changed, 117 insertions(+), 80 deletions(-) diff --git a/extras/Introjucer/Source/Application/jucer_Application.h b/extras/Introjucer/Source/Application/jucer_Application.h index 1fc8e2e576..3b248116f7 100644 --- a/extras/Introjucer/Source/Application/jucer_Application.h +++ b/extras/Introjucer/Source/Application/jucer_Application.h @@ -159,6 +159,8 @@ public: } virtual void doExtraInitialisation() {} + virtual void projectOpened (Project*) {} + virtual void projectClosed (Project*) {} static JucerApplication* getApp() { diff --git a/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.cpp b/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.cpp index 243bf57142..27f95be7e3 100644 --- a/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.cpp +++ b/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.cpp @@ -27,80 +27,9 @@ #include "jucer_FilePreviewComponent.h" #include "../Code Editor/jucer_SourceCodeEditor.h" - //============================================================================== -class SourceCodeDocument : public OpenDocumentManager::Document -{ -public: - //============================================================================== - SourceCodeDocument (Project* project_, const File& file_) - : modDetector (file_), project (project_) - { - codeDoc = new CodeDocument(); - reloadFromFile(); - } - - //============================================================================== - struct Type : public OpenDocumentManager::DocumentType - { - 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"); } - Document* openFile (Project* project, const File& file) { return new SourceCodeDocument (project, file); } - }; - - //============================================================================== - bool loadedOk() const { return true; } - bool isForFile (const File& file) const { return getFile() == file; } - bool isForNode (const ValueTree& node) const { return false; } - bool refersToProject (Project& p) const { return project == &p; } - Project* getProject() const { return project; } - bool canSaveAs() const { return true; } - String getName() const { return getFile().getFileName(); } - String getType() const { return getFile().getFileExtension() + " file"; } - File getFile() const { return modDetector.getFile(); } - bool needsSaving() const { return codeDoc != nullptr && codeDoc->hasChangedSinceSavePoint(); } - bool hasFileBeenModifiedExternally() { return modDetector.hasBeenModified(); } - void fileHasBeenRenamed (const File& newFile) { modDetector.fileHasBeenRenamed (newFile); } - - void reloadFromFile() - { - modDetector.updateHash(); - - ScopedPointer in (modDetector.getFile().createInputStream()); +Component* SourceCodeDocument::createEditor() { return SourceCodeEditor::createFor (this, *codeDoc); } - if (in != nullptr) - codeDoc->loadFromStream (*in); - } - - bool save() - { - TemporaryFile temp (modDetector.getFile()); - ScopedPointer out (temp.getFile().createOutputStream()); - - if (out == nullptr || ! codeDoc->writeToStream (*out)) - return false; - - out = nullptr; - if (! temp.overwriteTargetFileWithTemporary()) - return false; - - modDetector.updateHash(); - return true; - } - - bool saveAs() - { - jassertfalse; //xxx todo - return false; - } - - Component* createEditor() { return SourceCodeEditor::createFor (this, *codeDoc); } - Component* createViewer() { return createEditor(); } - -private: - FileModificationDetector modDetector; - ScopedPointer codeDoc; - Project* project; -}; //============================================================================== class UnknownDocument : public OpenDocumentManager::Document diff --git a/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.h b/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.h index d213a7704b..06c3e6a789 100644 --- a/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.h +++ b/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.h @@ -28,7 +28,6 @@ #include "../Project/jucer_Project.h" - //============================================================================== /** */ @@ -120,5 +119,78 @@ private: JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenDocumentManager); }; +//============================================================================== +class SourceCodeDocument : public OpenDocumentManager::Document +{ +public: + //============================================================================== + SourceCodeDocument (Project* project_, const File& file_) + : modDetector (file_), project (project_) + { + codeDoc = new CodeDocument(); + reloadFromFile(); + } + + //============================================================================== + struct Type : public OpenDocumentManager::DocumentType + { + 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"); } + Document* openFile (Project* project, const File& file) { return new SourceCodeDocument (project, file); } + }; + + //============================================================================== + bool loadedOk() const { return true; } + bool isForFile (const File& file) const { return getFile() == file; } + bool isForNode (const ValueTree& node) const { return false; } + bool refersToProject (Project& p) const { return project == &p; } + Project* getProject() const { return project; } + bool canSaveAs() const { return true; } + String getName() const { return getFile().getFileName(); } + String getType() const { return getFile().getFileExtension() + " file"; } + File getFile() const { return modDetector.getFile(); } + bool needsSaving() const { return codeDoc != nullptr && codeDoc->hasChangedSinceSavePoint(); } + bool hasFileBeenModifiedExternally() { return modDetector.hasBeenModified(); } + void fileHasBeenRenamed (const File& newFile) { modDetector.fileHasBeenRenamed (newFile); } + + void reloadFromFile() + { + modDetector.updateHash(); + + ScopedPointer in (modDetector.getFile().createInputStream()); + + if (in != nullptr) + codeDoc->loadFromStream (*in); + } + + bool save() + { + TemporaryFile temp (modDetector.getFile()); + ScopedPointer out (temp.getFile().createOutputStream()); + + if (out == nullptr || ! codeDoc->writeToStream (*out)) + return false; + + out = nullptr; + if (! temp.overwriteTargetFileWithTemporary()) + return false; + + modDetector.updateHash(); + return true; + } + + bool saveAs() + { + jassertfalse; //xxx todo + return false; + } + + Component* createEditor(); + Component* createViewer() { return createEditor(); } + +protected: + FileModificationDetector modDetector; + ScopedPointer codeDoc; + Project* project; +}; #endif // __JUCER_OPENDOCUMENTMANAGER_JUCEHEADER__ diff --git a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp index a8859f029d..156f246b02 100644 --- a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp +++ b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp @@ -57,16 +57,20 @@ void SourceCodeEditor::resized() editor.setBounds (getLocalBounds()); } -SourceCodeEditor* SourceCodeEditor::createFor (OpenDocumentManager::Document* document, - CodeDocument& codeDocument) +CodeTokeniser* SourceCodeEditor::getTokeniserFor (const File& file) { - CodeTokeniser* tokeniser = nullptr; - - if (document->getFile().hasFileExtension (sourceOrHeaderFileExtensions)) + if (file.hasFileExtension (sourceOrHeaderFileExtensions)) { static CPlusPlusCodeTokeniser cppTokeniser; - tokeniser = &cppTokeniser; + return &cppTokeniser; } - return new SourceCodeEditor (document, codeDocument, tokeniser); + return nullptr; +} + +SourceCodeEditor* SourceCodeEditor::createFor (OpenDocumentManager::Document* document, + CodeDocument& codeDocument) +{ + return new SourceCodeEditor (document, codeDocument, + getTokeniserFor (document->getFile())); } diff --git a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h index 9831ee6d1e..e1e9bcefca 100644 --- a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h +++ b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h @@ -46,6 +46,8 @@ public: static SourceCodeEditor* createFor (OpenDocumentManager::Document* document, CodeDocument& codeDocument); + static CodeTokeniser* getTokeniserFor (const File& file); + //============================================================================== void resized(); diff --git a/extras/Introjucer/Source/Project/jucer_Project.cpp b/extras/Introjucer/Source/Project/jucer_Project.cpp index ba5320ffb9..c72f7d9399 100644 --- a/extras/Introjucer/Source/Project/jucer_Project.cpp +++ b/extras/Introjucer/Source/Project/jucer_Project.cpp @@ -28,6 +28,7 @@ #include "../Project Saving/jucer_ProjectExporter.h" #include "../Project Saving/jucer_ProjectSaver.h" #include "../Application/jucer_OpenDocumentManager.h" +#include "../Application/jucer_Application.h" //============================================================================== @@ -62,10 +63,14 @@ Project::Project (const File& file_) mainProjectIcon.setImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize)); projectRoot.addListener (this); + + JucerApplication::getApp()->projectOpened (this); } Project::~Project() { + JucerApplication::getApp()->projectClosed (this); + projectRoot.removeListener (this); OpenDocumentManager::getInstance()->closeAllDocumentsUsingProject (*this, false); } @@ -228,7 +233,9 @@ Result Project::loadDocument (const File& file) return Result::fail ("The document contains errors and couldn't be parsed!"); registerRecentFile (file); + JucerApplication::getApp()->projectClosed (this); projectRoot = newTree; + JucerApplication::getApp()->projectOpened (this); removeDefunctExporters(); setMissingDefaultValues(); diff --git a/modules/juce_core/memory/juce_OptionalScopedPointer.h b/modules/juce_core/memory/juce_OptionalScopedPointer.h index 10b2526f50..3a665656a2 100644 --- a/modules/juce_core/memory/juce_OptionalScopedPointer.h +++ b/modules/juce_core/memory/juce_OptionalScopedPointer.h @@ -106,6 +106,9 @@ public: /** Returns the object that this pointer is managing. */ inline operator ObjectType*() const noexcept { return object; } + /** Returns the object that this pointer is managing. */ + inline ObjectType* get() const noexcept { return object; } + /** Returns the object that this pointer is managing. */ inline ObjectType& operator*() const noexcept { return *object; } @@ -127,6 +130,24 @@ public: object.release(); } + /** Makes this OptionalScopedPointer point at a new object, specifying whether the + OptionalScopedPointer will take ownership of the object. + + If takeOwnership is true, then the OptionalScopedPointer will act like a ScopedPointer, + deleting the object when it is itself deleted. If this parameter is false, then the + OptionalScopedPointer just holds a normal pointer to the object, and won't delete it. + */ + void set (ObjectType* newObject, bool takeOwnership) + { + if (object != newObject) + { + clear(); + object = newObject; + } + + shouldDelete = takeOwnership; + } + /** Returns true if the target object will be deleted when this pointer object is deleted. */