--- trunk/RomCheater.ScratchPad/ScratchPadDocument.cs 2013/06/20 22:59:40 775 +++ trunk/RomCheater.ScratchPad/ScratchPadDocument.cs 2013/06/20 23:07:32 776 @@ -51,11 +51,13 @@ namespace RomCheater.ScratchPad private bool ShouldAskSave() { - bool shouldAskSave = false; - return shouldAskSave; + return !DocumentSaved; } - + + private bool _DocumentSaved; + public bool DocumentSaved { get { return _DocumentSaved; } private set { _DocumentSaved = value; } } + private string _DocumentFilename; public string DocumentFilename { get { return _DocumentFilename; } set { _DocumentFilename = value; } } private TabPage _DocumentTab; @@ -63,6 +65,10 @@ namespace RomCheater.ScratchPad private int _DocumentIndex; public int DocumentIndex { get { return _DocumentIndex; } set { _DocumentIndex = value; } } + private void UpdateTabName(string name) + { + this.DocumentTab.Text = name; + } private TabControl GetParentTabControl() { var parent = this.DocumentTab.Parent; @@ -117,6 +123,7 @@ namespace RomCheater.ScratchPad SyntaxLanguage language = SyntaxLanguage.Text; CodeEditorSyntaxLoader.SetSyntax(txtEditor, language); txtEditor.Document.ReParse(); + this.UpdateTabName(fi.Name); } catch (Exception ex) { @@ -141,6 +148,44 @@ namespace RomCheater.ScratchPad { DialogResult result = FileSaver.ShowDialog(); if (result != DialogResult.OK) return; + FileInfo fi = new FileInfo(FileSaver.FileName); + try + { + using (FileStream fs = new FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) + { + try + { + using (StreamWriter sw = new StreamWriter(fs)) + { + try + { + foreach (var t in txtEditor.Document.Lines) + { + sw.WriteLine(t); + } + sw.Flush(); + sw.Close(); + this.DocumentSaved = true; + } + catch (Exception ex) + { + throw ex; + } + } + } + catch (Exception ex) + { + throw ex; + } + } + } + catch (Exception ex) + { + this.DocumentSaved = false; + MessageBox.Show(string.Format("Failed to save: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); + logger.Error.WriteLine("Failed to save: '{0}'", fi.FullName); + logger.Error.WriteLine(ex.ToString()); + } } private void CloseDocument(bool quiting) @@ -156,5 +201,10 @@ namespace RomCheater.ScratchPad this.DocumentClosing.Invoke(new ControlClosingEventArgs<int>(this, this.DocumentIndex)); } } + + private void txtEditor_TextChanged(object sender, EventArgs e) + { + this.DocumentSaved = false; + } } } |