commit:822c67be52521bd601cd01b66974bb6c0df967bf
author:Chip Black
committer:Chip Black
date:Mon Sep 29 03:00:58 2008 -0500
parents:1265cb3fd18d7f0144a1634d2f2a87194116455c
Added save functionality
diff --git a/TextDisplay.cs b/TextDisplay.cs
line changes: +1/-1
index 9389f00..4e753de
--- a/TextDisplay.cs
+++ b/TextDisplay.cs
@@ -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();
         }
 

diff --git a/TextDocument.cs b/TextDocument.cs
line changes: +25/-2
index f554af7..4f1103f
--- a/TextDocument.cs
+++ b/TextDocument.cs
@@ -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();
+	}
 }

diff --git a/TextInput.cs b/TextInput.cs
line changes: +1/-1
index 4934d20..54024fb
--- a/TextInput.cs
+++ b/TextInput.cs
@@ -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
line changes: +26/-7
index bdfdd49..f9570c5
--- 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;