Added save functionality
authorChip Black <bytex64@bytex64.net>
Mon, 29 Sep 2008 08:00:58 +0000 (03:00 -0500)
committerChip Black <bytex64@bytex64.net>
Mon, 29 Sep 2008 08:00:58 +0000 (03:00 -0500)
TextDisplay.cs
TextDocument.cs
TextInput.cs
main.cs

index 9389f00..4e753de 100644 (file)
@@ -39,7 +39,7 @@ public class TextDisplay : DrawingArea, IKeyPress {
 
                gr.Operator = Operator.Xor;
                gr.NewPath();
-               gr.Rectangle(new Cairo.Rectangle((doc.cursor.x - doc.textorigin.x)*fontwidth, (doc.cursor.y - doc.textorigin.y)*fontheight, fontwidth, fontheight));
+               gr.Rectangle((doc.cursor.x - doc.textorigin.x)*fontwidth, (doc.cursor.y - doc.textorigin.y)*fontheight, fontwidth, fontheight);
                gr.Fill();
         }
 
index f554af7..4f1103f 100644 (file)
@@ -49,6 +49,7 @@ public class TextArray {
                while ((line = file.ReadLine()) != null) {
                        lines.Add(line);
                }
+               file.Close();
        }
 
        public ArrayList Lines {
@@ -78,7 +79,7 @@ public class TextArray {
                        if (y == lines.Count - 1) return;
                        if (x > s.Length)
                                s = s.PadRight(x);
-                       lines[y] = s + (String)lines[y+1];
+                       lines[y] = s + (string)lines[y+1];
                        lines.RemoveAt(y+1);
                } else if (s.Length == 0 && x == 0) {
                        lines.RemoveAt(y);
@@ -120,10 +121,20 @@ public class TextArray {
                if (y >= lines.Count) return 0;
                return ((string)lines[y]).Length;
        }
+
+       public void Save(string filename) {
+               StreamWriter file = new StreamWriter(filename);
+               for (int i = 0; i < lines.Count; i++) {
+                       file.Write((string)lines[i]);
+                       file.Write('\n');
+               }
+               file.Close();
+       }
 }
 
 public class TextDocument : IKeyPress {
        private TextArray text;
+       public string filename = null;
        public Cursor cursor;
        public Cursor textorigin;
        private int pagewidth, pageheight;
@@ -134,13 +145,14 @@ public class TextDocument : IKeyPress {
 
        public TextDocument(string filename) {
                text = new TextArray(filename);
+               this.filename = filename;
        }
 
        public string GetTextWindow() {
                string s = "";
 
                for (int i = textorigin.y; textorigin.y + i < text.Lines.Count && i < pageheight; i++) {
-                       String l = (String)text.Lines[i];
+                       string l = (string)text.Lines[i];
                        s += l.Substring(textorigin.x) + "\n";
                }
                return s;
@@ -239,4 +251,15 @@ public class TextDocument : IKeyPress {
                text.Insert(cursor.x, cursor.y, (char)c);
                cursor.x++;
        }
+
+       public void Save() {
+               if (filename == null)
+                       throw new InvalidOperationException("Cannot Save() without filename");
+               text.Save(filename);
+       }
+
+       public void Save(string filename) {
+               this.filename = filename;
+               Save();
+       }
 }
index 4934d20..54024fb 100644 (file)
@@ -47,7 +47,7 @@ public class TextInput : DrawingArea, IKeyPress {
 
                gr.NewPath();
                int h = Pango.Units.ToPixels(layout.FontDescription.Size) + 1;
-               gr.Rectangle(new Cairo.Rectangle(cursor*8, 0, 7, h));
+               gr.Rectangle(cursor*8, 0, 7, h);
                gr.Fill();
         }
 
diff --git a/main.cs b/main.cs
index bdfdd49..f9570c5 100644 (file)
--- a/main.cs
+++ b/main.cs
@@ -46,6 +46,7 @@ public class Nebula {
 
        [GLib.ConnectBefore ()]
        static void KeyPress(object o, KeyPressEventArgs args) {
+               TextInput input;
                if ((args.Event.State & Gdk.ModifierType.ControlMask) != 0) {
                        switch(args.Event.Key) {
                        case Gdk.Key.n:
@@ -53,20 +54,38 @@ public class Nebula {
                                display.SetDocument(doc);
                                break;
                        case Gdk.Key.o:
-                               TextInput filename = new FileSelector();
-                               stack.PackEnd(filename);
-                               SelectInput(filename);
-                               filename.Show();
-                               filename.Selected += delegate(object o2, TextInputEventArgs t) {
+                               input = new FileSelector();
+                               stack.PackEnd(input);
+                               SelectInput(input);
+                               input.Show();
+                               input.Selected += delegate(object o2, TextInputEventArgs t) {
                                        if (t.Value != null) {
                                                doc = new TextDocument(t.Value);
                                                display.SetDocument(doc);
                                        }
                                        SelectInput(display);
-                                       stack.Remove(filename);
-                                       filename.Destroy();
+                                       stack.Remove(input);
+                                       input.Destroy();
                                };
                                break;
+                       case Gdk.Key.s:
+                               if (doc.filename == null) {
+                                       input = new FileSelector();
+                                       stack.PackEnd(input);
+                                       SelectInput(input);
+                                       input.Show();
+                                       input.Selected += delegate(object o2, TextInputEventArgs t) {
+                                               if (t.Value != null) {
+                                                       doc.Save(t.Value);
+                                               }
+                                               SelectInput(display);
+                                               stack.Remove(input);
+                                               input.Destroy();
+                                       };
+                               } else {
+                                       doc.Save();
+                               }
+                               break;
                        case Gdk.Key.q:
                                Application.Quit();
                                break;