diff --git a/extras/Introjucer/Source/Application/jucer_Application.h b/extras/Introjucer/Source/Application/jucer_Application.h index ab3cc27d8c..d776b07b9b 100644 --- a/extras/Introjucer/Source/Application/jucer_Application.h +++ b/extras/Introjucer/Source/Application/jucer_Application.h @@ -250,6 +250,7 @@ public: menu.addSeparator(); menu.addCommandItem (commandManager, CommandIDs::closeDocument); menu.addCommandItem (commandManager, CommandIDs::saveDocument); + menu.addCommandItem (commandManager, CommandIDs::saveDocumentAs); menu.addSeparator(); menu.addCommandItem (commandManager, CommandIDs::closeProject); menu.addCommandItem (commandManager, CommandIDs::saveProject); diff --git a/extras/Introjucer/Source/Application/jucer_CommandIDs.h b/extras/Introjucer/Source/Application/jucer_CommandIDs.h index 33d0934f1b..e6e540143f 100644 --- a/extras/Introjucer/Source/Application/jucer_CommandIDs.h +++ b/extras/Introjucer/Source/Application/jucer_CommandIDs.h @@ -34,6 +34,7 @@ namespace CommandIDs open = 0x200020, closeDocument = 0x200030, saveDocument = 0x200040, + saveDocumentAs = 0x200041, closeProject = 0x200051, saveProject = 0x200060, diff --git a/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.cpp b/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.cpp index bffeff1538..dfdd329de0 100644 --- a/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.cpp +++ b/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.cpp @@ -54,6 +54,7 @@ public: Project* getProject() const { return project; } bool needsSaving() const { return false; } bool save() { return true; } + bool saveAs() { return false; } bool hasFileBeenModifiedExternally() { return fileModificationTime != file.getLastModificationTime(); } void reloadFromFile() { fileModificationTime = file.getLastModificationTime(); } String getName() const { return file.getFileName(); } diff --git a/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.h b/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.h index 23fcda0e4b..248762021f 100644 --- a/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.h +++ b/extras/Introjucer/Source/Application/jucer_OpenDocumentManager.h @@ -55,6 +55,7 @@ public: virtual File getFile() const = 0; virtual bool needsSaving() const = 0; virtual bool save() = 0; + virtual bool saveAs() = 0; virtual bool hasFileBeenModifiedExternally() = 0; virtual void reloadFromFile() = 0; virtual Component* createEditor() = 0; diff --git a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp index 13685aecc0..94523379ff 100644 --- a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp +++ b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp @@ -63,27 +63,44 @@ void SourceCodeDocument::reloadInternal() { jassert (codeDoc != nullptr); modDetector.updateHash(); - codeDoc->applyChanges (modDetector.getFile().loadFileAsString()); + codeDoc->applyChanges (getFile().loadFileAsString()); codeDoc->setSavePoint(); } -bool SourceCodeDocument::save() +static bool writeCodeDocToFile (const File& file, CodeDocument& doc) { - TemporaryFile temp (modDetector.getFile()); + TemporaryFile temp (file); { FileOutputStream fo (temp.getFile()); - if (! (fo.openedOk() && getCodeDocument().writeToStream (fo))) + if (! (fo.openedOk() && doc.writeToStream (fo))) return false; } - if (! temp.overwriteTargetFileWithTemporary()) - return false; + return temp.overwriteTargetFileWithTemporary(); +} - getCodeDocument().setSavePoint(); - modDetector.updateHash(); - return true; +bool SourceCodeDocument::save() +{ + if (writeCodeDocToFile (getFile(), getCodeDocument())) + { + getCodeDocument().setSavePoint(); + modDetector.updateHash(); + return true; + } + + return false; +} + +bool SourceCodeDocument::saveAs() +{ + FileChooser fc (TRANS("Save As..."), getFile(), "*"); + + if (! fc.browseForFileToSave (true)) + return true; + + return writeCodeDocToFile (fc.getResult(), getCodeDocument()); } void SourceCodeDocument::updateLastState (CodeEditorComponent& editor) diff --git a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h index 036ef4fd57..1b09a44631 100644 --- a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h +++ b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h @@ -59,7 +59,8 @@ public: const char* extensions[] = { "h", "hpp", nullptr }; return findCounterpart (file, extensions); } - else if (file.hasFileExtension ("h;hpp")) + + if (file.hasFileExtension ("h;hpp")) { const char* extensions[] = { "cpp", "mm", "cc", "cxx", "c", "m", nullptr }; return findCounterpart (file, extensions); @@ -83,6 +84,7 @@ public: void reloadFromFile(); bool save(); + bool saveAs(); Component* createEditor(); Component* createViewer() { return createEditor(); } diff --git a/extras/Introjucer/Source/Project/jucer_ProjectContentComponent.cpp b/extras/Introjucer/Source/Project/jucer_ProjectContentComponent.cpp index 4f82e809f7..0c963f3dc2 100644 --- a/extras/Introjucer/Source/Project/jucer_ProjectContentComponent.cpp +++ b/extras/Introjucer/Source/Project/jucer_ProjectContentComponent.cpp @@ -419,15 +419,20 @@ void ProjectContentComponent::closeDocument() hideEditor(); } +static void showSaveWarning (OpenDocumentManager::Document* currentDocument) +{ + AlertWindow::showMessageBox (AlertWindow::WarningIcon, + TRANS("Save failed!"), + TRANS("Couldn't save the file:") + + "\n" + currentDocument->getFile().getFullPathName()); +} + void ProjectContentComponent::saveDocument() { if (currentDocument != nullptr) { if (! currentDocument->save()) - AlertWindow::showMessageBox (AlertWindow::WarningIcon, - TRANS("Save failed!"), - TRANS("Couldn't save the file:") - + "\n" + currentDocument->getFile().getFullPathName()); + showSaveWarning (currentDocument); } else saveProject(); @@ -435,6 +440,12 @@ void ProjectContentComponent::saveDocument() updateMainWindowTitle(); } +void ProjectContentComponent::saveAs() +{ + if (currentDocument != nullptr && ! currentDocument->saveAs()) + showSaveWarning (currentDocument); +} + bool ProjectContentComponent::goToPreviousFile() { OpenDocumentManager::Document* doc = recentDocumentList.getCurrentDocument(); @@ -544,6 +555,7 @@ ApplicationCommandTarget* ProjectContentComponent::getNextCommandTarget() void ProjectContentComponent::getAllCommands (Array & commands) { const CommandID ids[] = { CommandIDs::saveDocument, + CommandIDs::saveDocumentAs, CommandIDs::closeDocument, CommandIDs::saveProject, CommandIDs::closeProject, @@ -595,6 +607,14 @@ void ProjectContentComponent::getCommandInfo (const CommandID commandID, Applica result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier, 0)); break; + case CommandIDs::saveDocumentAs: + result.setInfo ("Save As...", + "Saves the current document to a new location", + CommandCategories::general, 0); + result.setActive (currentDocument != nullptr || project != nullptr); + result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0)); + break; + case CommandIDs::closeDocument: result.setInfo ("Close" + documentName, "Closes the current document", @@ -688,6 +708,7 @@ bool ProjectContentComponent::perform (const InvocationInfo& info) case CommandIDs::saveProject: case CommandIDs::closeProject: case CommandIDs::saveDocument: + case CommandIDs::saveDocumentAs: case CommandIDs::closeDocument: case CommandIDs::goToPreviousDoc: case CommandIDs::goToNextDoc: @@ -710,6 +731,7 @@ bool ProjectContentComponent::perform (const InvocationInfo& info) case CommandIDs::saveProject: saveProject(); break; case CommandIDs::closeProject: closeProject(); break; case CommandIDs::saveDocument: saveDocument(); break; + case CommandIDs::saveDocumentAs: saveAs(); break; case CommandIDs::closeDocument: closeDocument(); break; case CommandIDs::goToPreviousDoc: goToPreviousFile(); break; diff --git a/extras/Introjucer/Source/Project/jucer_ProjectContentComponent.h b/extras/Introjucer/Source/Project/jucer_ProjectContentComponent.h index 3371139b63..5a9d0c636b 100644 --- a/extras/Introjucer/Source/Project/jucer_ProjectContentComponent.h +++ b/extras/Introjucer/Source/Project/jucer_ProjectContentComponent.h @@ -58,6 +58,7 @@ public: OpenDocumentManager::Document* getCurrentDocument() const { return currentDocument; } void closeDocument(); void saveDocument(); + void saveAs(); void hideEditor(); bool setEditorComponent (Component* editor, OpenDocumentManager::Document* doc); diff --git a/modules/juce_gui_extra/documents/juce_FileBasedDocument.cpp b/modules/juce_gui_extra/documents/juce_FileBasedDocument.cpp index 46f547aeec..8c8d565cc3 100644 --- a/modules/juce_gui_extra/documents/juce_FileBasedDocument.cpp +++ b/modules/juce_gui_extra/documents/juce_FileBasedDocument.cpp @@ -137,15 +137,11 @@ FileBasedDocument::SaveResult FileBasedDocument::saveAs (const File& newFile, if (newFile == File::nonexistent) { if (askUserForFileIfNotSpecified) - { return saveAsInteractive (true); - } - else - { - // can't save to an unspecified file - jassertfalse; - return failedToWriteToFile; - } + + // can't save to an unspecified file + jassertfalse; + return failedToWriteToFile; } if (warnAboutOverwritingExistingFiles && newFile.exists()) @@ -208,16 +204,11 @@ FileBasedDocument::SaveResult FileBasedDocument::saveIfNeededAndUserAgrees() TRANS("discard changes"), TRANS("cancel")); - if (r == 1) - { - // save changes + if (r == 1) // save changes return save (true, true); - } - else if (r == 2) - { - // discard changes + + if (r == 2) // discard changes return savedOk; - } return userCancelledSave; }