/[RomCheater]/trunk/RomCheater.ScratchPad/ScratchPadDocument.cs
ViewVC logotype

Diff of /trunk/RomCheater.ScratchPad/ScratchPadDocument.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/RomCheater.ScratchPad/ScratchPadDocument.cs	2013/06/20 22:59:40	775
+++ trunk/RomCheater.ScratchPad/ScratchPadDocument.cs	2013/06/21 04:26:36	783
@@ -10,15 +10,15 @@
 using RomCheater.Core;
 using System.IO;
 using RomCheater.Logging;
-using Fireball.CodeEditor.SyntaxFiles;
 
 namespace RomCheater.ScratchPad
 {
     public partial class ScratchPadDocument : UserControl
-    {
+    {        
+        public event EventHandler<LinkClickedEventArgs> LinkClicked;
         public event BaseEventHandler<ControlClosingEventArgs<int>> DocumentClosing;
 
-        const string NewDocumentFilename = "New";
+        const string NewDocumentFilename = "ScratchPad";
         public ScratchPadDocument() : this(NewDocumentFilename, new TabPage(), 0) { }
         public ScratchPadDocument(TabPage tp, int index) : this(NewDocumentFilename, tp, index) { }
         public ScratchPadDocument(int index) : this(NewDocumentFilename, new TabPage(), index) { }
@@ -31,6 +31,8 @@
             this.DocumentTab.Name = string.Format("tp{0}", this.DocumentIndex);
             this.DocumentTab.Text = string.Format("{0}{1}", this.DocumentFilename, this.DocumentIndex);
             //this.DocumentClosing += new BaseEventHandler<ControlClosingEventArgs<int>>(ScratchPadDocument_DocumentClosing);
+            this.IsDefaultDocument = true;
+            this.DocumentSaved = true;
             this.DocumentTab.Controls.Add(this);
         }
 
@@ -51,11 +53,15 @@
 
         private bool ShouldAskSave()
         {
-            bool shouldAskSave = false;
-            return shouldAskSave;
+            return !DocumentSaved;
         }
 
-      
+        private bool _IsDefaultDocument;
+        public bool IsDefaultDocument { get { return _IsDefaultDocument; } private set { _IsDefaultDocument = value; } }
+
+        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 +69,10 @@
         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;
@@ -77,9 +87,26 @@
             }
         }
 
+        public override Font Font
+        {
+            get
+            {
+                return base.Font;
+            }
+            set
+            {
+                base.Font = value;
+                txtEditor.Font = base.Font;
+                mnuItemClose.Font = base.Font;
+                mnuItemOpen.Font = base.Font;
+                mnuItemSave.Font = base.Font;
+            }
+        }
+
         private void mnuItemOpen_Click(object sender, EventArgs e)
         {
             OpenDocument();
+            this.DocumentSaved = true; // the file was just opened
         }
 
         private void mnuItemSave_Click(object sender, EventArgs e)
@@ -98,6 +125,7 @@
             DialogResult result = FileLoader.ShowDialog();
             if (result != DialogResult.OK) return;
             FileInfo fi = new FileInfo(FileLoader.FileName);
+            this.DocumentFilename = fi.FullName;
             try
             {
                 
@@ -111,12 +139,10 @@
                             {
                                 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();
+                                txtEditor.Text = text;                                
+                                this.UpdateTabName(fi.Name);
+                                this.IsDefaultDocument = false;
+                                this.DocumentSaved = true;
                             }
                             catch (Exception ex)
                             {
@@ -132,6 +158,7 @@
             }
             catch (Exception ex)
             {
+                this.DocumentSaved = false;
                 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());
@@ -139,8 +166,54 @@
         }
         private void SaveDocument()
         {
-            DialogResult result = FileSaver.ShowDialog();
-            if (result != DialogResult.OK) return;
+            FileInfo fi = null;
+            if (this.IsDefaultDocument)
+            {
+                DialogResult result = FileSaver.ShowDialog();
+                if (result != DialogResult.OK) return;
+                fi = new FileInfo(FileSaver.FileName);
+            }
+            else
+            {
+                fi = new FileInfo(this.DocumentFilename);
+            }           
+            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.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 +229,18 @@
                 this.DocumentClosing.Invoke(new ControlClosingEventArgs<int>(this, this.DocumentIndex));
             }
         }
+
+        private void txtEditor_TextChanged(object sender, EventArgs e)
+        {
+            this.DocumentSaved = false;
+        }
+
+        private void txtEditor_LinkClicked(object sender, LinkClickedEventArgs e)
+        {
+            if (this.LinkClicked != null)
+            {
+                this.LinkClicked.Invoke(sender, e);
+            }
+        }
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22