--- trunk/RomCheater.ScratchPad/ScratchPadDocument.cs 2013/06/20 22:33:04 774 +++ trunk/RomCheater.ScratchPad/ScratchPadDocument.cs 2013/06/20 22:59:40 775 @@ -8,6 +8,9 @@ using System.Text; using System.Windows.Forms; using WeifenLuo.WinFormsUI.Docking; using RomCheater.Core; +using System.IO; +using RomCheater.Logging; +using Fireball.CodeEditor.SyntaxFiles; namespace RomCheater.ScratchPad { @@ -44,19 +47,7 @@ namespace RomCheater.ScratchPad CloseDocument(true); } - private void CloseDocument(bool quiting) - { - if (ShouldAskSave()) - { - SaveDocument(); - } - var tb = GetParentTabControl(); - tb.TabPages.RemoveAt(this.DocumentIndex); - if (this.DocumentClosing != null && !quiting) - { - this.DocumentClosing.Invoke(new ControlClosingEventArgs(this, this.DocumentIndex)); - } - } + private bool ShouldAskSave() { @@ -64,10 +55,7 @@ namespace RomCheater.ScratchPad return shouldAskSave; } - private void SaveDocument() - { - } - + private string _DocumentFilename; public string DocumentFilename { get { return _DocumentFilename; } set { _DocumentFilename = value; } } private TabPage _DocumentTab; @@ -91,17 +79,82 @@ namespace RomCheater.ScratchPad private void mnuItemOpen_Click(object sender, EventArgs e) { - + OpenDocument(); } private void mnuItemSave_Click(object sender, EventArgs e) { - + SaveDocument(); } private void mnuItemClose_Click(object sender, EventArgs e) { CloseDocument(false); } + + + private void OpenDocument() + { + DialogResult result = FileLoader.ShowDialog(); + if (result != DialogResult.OK) return; + FileInfo fi = new FileInfo(FileLoader.FileName); + try + { + + using (FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)) + { + try + { + using (StreamReader sr = new StreamReader(fs)) + { + try + { + var text = sr.ReadToEnd(); + sr.Close(); + + txtEditor.Document = new Fireball.Syntax.SyntaxDocument(); + txtEditor.Document.Text = text; + SyntaxLanguage language = SyntaxLanguage.Text; + CodeEditorSyntaxLoader.SetSyntax(txtEditor, language); + txtEditor.Document.ReParse(); + } + catch (Exception ex) + { + throw ex; + } + } + } + catch (Exception ex) + { + throw ex; + } + } + } + catch (Exception ex) + { + MessageBox.Show(string.Format("Failed to open: '{0}'", fi.Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); + logger.Error.WriteLine("Failed to open: '{0}'", fi.FullName); + logger.Error.WriteLine(ex.ToString()); + } + } + private void SaveDocument() + { + DialogResult result = FileSaver.ShowDialog(); + if (result != DialogResult.OK) return; + } + + private void CloseDocument(bool quiting) + { + if (ShouldAskSave()) + { + SaveDocument(); + } + var tb = GetParentTabControl(); + tb.TabPages.RemoveAt(this.DocumentIndex); + if (this.DocumentClosing != null && !quiting) + { + this.DocumentClosing.Invoke(new ControlClosingEventArgs(this, this.DocumentIndex)); + } + } } }