public bool CloseFile(ChameleonEditor editor) { if (editor == null) { editor = CurrentEditor; } ModifiedFileResult fileHandled = HandleModifiedFile(editor, ModifiedFileAction.Close); if (fileHandled == ModifiedFileResult.Cancel) { return(false); } FATabStripItem tab = m_editorsToTabs[editor]; if (m_tabStrip.Items.Count > 1 || ChameleonForm.AppClosing) { m_tabStrip.RemoveTab(tab); m_editorsToTabs.Remove(editor); m_tabsToEditors.Remove(tab); tab.Controls.Remove(editor); editor.Dispose(); } // closing out the last buffer, reset it to act as a new one else { m_fileNum = 1; editor.ResetEditor(); string newFileTitle = string.Format("<untitled> {0}", m_fileNum); editor.Filename = newFileTitle; } return(true); }
bool OpenSourceFile(FileInformation fileInfo) { string fileContents = ""; string locationPrefix = (fileInfo.Location == FileLocation.Local) ? "(L)" : "(R)"; ChameleonEditor alreadyOpenedFile = GetEditorByFilename(fileInfo.Filename.GetFullPath()); if (alreadyOpenedFile != null) { ModifiedFileResult mfr = HandleModifiedFile(alreadyOpenedFile, ModifiedFileAction.Reload); // should only have these two choices returned for the Reload case if (mfr == ModifiedFileResult.Cancel) { return(false); } else if (mfr == ModifiedFileResult.NoSave) { SelectEditor(alreadyOpenedFile); return(true); } } if (!GetFileContents(fileInfo, ref fileContents)) { return(false); } ChameleonEditor destinationEditor = null; if (alreadyOpenedFile != null) { destinationEditor = alreadyOpenedFile; } else { // current buffer is empty and untouched, so load the file into it if (!m_currentEditor.Modified && !m_currentEditor.HasBeenSaved() && m_currentEditor.Text.Length == 0) { destinationEditor = m_currentEditor; } // need to create a new buffer for the file; else { NewFile(); FATabStripItem newTab = m_tabStrip.Items[m_tabStrip.Items.Count - 1]; destinationEditor = m_tabsToEditors[newTab]; } } destinationEditor.LoadFileText(fileInfo, fileContents); SelectEditor(destinationEditor); destinationEditor.Modified = false; if (EditorStatusChanged != null) { EditorStatusChanged(this, null); } return(true); }
// returns true if things were handled successfully, and the task should continue private ModifiedFileResult HandleModifiedFile(ChameleonEditor editor, ModifiedFileAction fileAction) { if (!editor.Modified) { return(ModifiedFileResult.NoSave); } string messageEnd = ""; string saveButtonText = ""; string noSaveButtonText = ""; string cancelButtonText = ""; List <string> buttons = new List <string>(); Dictionary <int, ModifiedFileResult> buttonActions = new Dictionary <int, ModifiedFileResult>(); switch (fileAction) { case ModifiedFileAction.Close: { messageEnd = "Do you want to save the file before it is closed?"; saveButtonText = "Save this file, then close it"; noSaveButtonText = "Don't save this file, but still close it"; cancelButtonText = "Cancel closing, and leave this file open"; buttons.Add(saveButtonText); buttons.Add(noSaveButtonText); buttonActions[0] = ModifiedFileResult.Save; buttonActions[1] = ModifiedFileResult.NoSave; break; } case ModifiedFileAction.Reload: { if (editor.FileLocation == FileLocation.Unknown) { return(ModifiedFileResult.Cancel); } if (!editor.Modified) { return(ModifiedFileResult.NoSave); } messageEnd = "Do you want to revert to what was last saved?"; noSaveButtonText = "Lose changes and reload this file"; cancelButtonText = "Cancel reloading, and return to editing"; buttons.Add(noSaveButtonText); buttonActions[0] = ModifiedFileResult.NoSave; break; } case ModifiedFileAction.Compile: { messageEnd = "Do you want to save this file and compile it?"; saveButtonText = "Save and compile this file"; cancelButtonText = "Cancel compiling, and return to editing"; buttons.Add(saveButtonText); buttonActions[0] = ModifiedFileResult.Save; break; } } string message = string.Format("The file '{0}' has unsaved changes. {1}", editor.Filename, messageEnd); buttons.Add(cancelButtonText); buttonActions[buttonActions.Count] = ModifiedFileResult.Cancel; string commandButtonString = string.Join("|", buttons); int selectedButtonID = cTaskDialog.ShowCommandBox("Unsaved Changes", message, "", commandButtonString, false); ModifiedFileResult selectedAction = buttonActions[selectedButtonID]; ModifiedFileResult returnResult = ModifiedFileResult.Cancel; switch (selectedAction) { case ModifiedFileResult.Save: { bool saveResult = SaveFile(editor, editor.FileLocation, false, true); returnResult = saveResult ? ModifiedFileResult.Save : ModifiedFileResult.Cancel; break; } case ModifiedFileResult.NoSave: { returnResult = ModifiedFileResult.NoSave; break; } case ModifiedFileResult.Cancel: { // returnResult is already cancel, no actions needed break; } } return(returnResult); }