/[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 21:34:48	769
+++ trunk/RomCheater.ScratchPad/ScratchPadDocument.cs	2013/06/21 04:26:36	783
@@ -7,12 +7,18 @@
 using System.Text;
 using System.Windows.Forms;
 using WeifenLuo.WinFormsUI.Docking;
+using RomCheater.Core;
+using System.IO;
+using RomCheater.Logging;
 
 namespace RomCheater.ScratchPad
 {
     public partial class ScratchPadDocument : UserControl
-    {
-        const string NewDocumentFilename = "New";
+    {        
+        public event EventHandler<LinkClickedEventArgs> LinkClicked;
+        public event BaseEventHandler<ControlClosingEventArgs<int>> DocumentClosing;
+
+        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) { }
@@ -24,14 +30,38 @@
             this.DocumentTab = tp;
             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);
         }
 
+        //void ScratchPadDocument_DocumentClosing(ControlClosingEventArgs<int> e)
+        //{
+        //    CloseDocument(this.Disposing);
+        //}
+
+
+        
 
         public void OnDocumentQuit()
         {
-            var tb = GetParentTabControl();
+            CloseDocument(true);
+        }
+
+       
+
+        private bool 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;
@@ -39,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;
@@ -52,5 +86,161 @@
                 return tb;
             }
         }
+
+        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)
+        {
+            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);
+            this.DocumentFilename = fi.FullName;
+            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.Text = text;                                
+                                this.UpdateTabName(fi.Name);
+                                this.IsDefaultDocument = false;
+                                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 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()
+        {
+            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)
+        {
+            if (ShouldAskSave())
+            {
+                SaveDocument();
+            }
+            var tb = GetParentTabControl();
+            tb.TabPages.RemoveAt(this.DocumentIndex);
+            if (this.DocumentClosing != null && !quiting)
+            {
+                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