Added text scrolling
[Nebula.git] / TextDocument.cs
index e05e992..f554af7 100644 (file)
@@ -74,12 +74,16 @@ public class TextArray {
                if (y >= lines.Count) return;
 
                string s = (string)lines[y];
-               if (x >= s.Length) return;
-
-               if (s.Length == 0 && x == 0) {
+               if (x >= s.Length) {
+                       if (y == lines.Count - 1) return;
+                       if (x > s.Length)
+                               s = s.PadRight(x);
+                       lines[y] = s + (String)lines[y+1];
+                       lines.RemoveAt(y+1);
+               } else if (s.Length == 0 && x == 0) {
                        lines.RemoveAt(y);
                } else {
-                       s.Remove(x, 1);
+                       lines[y] = s.Remove(x, 1);
                }
        }
 
@@ -118,15 +122,11 @@ public class TextArray {
        }
 }
 
-public class TextDocument {
+public class TextDocument : IKeyPress {
        private TextArray text;
        public Cursor cursor;
-
-       public ArrayList Lines {
-               get {
-                       return text.Lines;
-               }
-       }
+       public Cursor textorigin;
+       private int pagewidth, pageheight;
 
        public TextDocument() {
                text = new TextArray();
@@ -136,6 +136,29 @@ public class TextDocument {
                text = new TextArray(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];
+                       s += l.Substring(textorigin.x) + "\n";
+               }
+               return s;
+       }
+
+       private void FixTextWindow() {
+               if (cursor.x < textorigin.x) textorigin.x = cursor.x;
+               if (cursor.y < textorigin.y) textorigin.y = cursor.y;
+               if (cursor.x > textorigin.x + pagewidth) textorigin.x = cursor.x - pagewidth;
+               if (cursor.y > textorigin.y + pageheight) textorigin.y = cursor.y - pageheight;
+       }
+
+       public void SetPageSize(int w, int h) {
+               pagewidth = w;
+               pageheight = h;
+               FixTextWindow();
+       }
+
        public void MoveCursor(Gdk.Key k) {
                switch(k) {
                case Gdk.Key.Up:
@@ -156,35 +179,64 @@ public class TextDocument {
                case Gdk.Key.Home:
                        cursor.x = 0;
                        break;
+               case Gdk.Key.Page_Up:
+                       cursor.y -= pageheight;
+                       break;
+               case Gdk.Key.Page_Down:
+                       cursor.y += pageheight;
+                       break;
                }
+               FixTextWindow();
        }
 
+       [GLib.ConnectBefore ()]
+       public void KeyPress(object o, Gtk.KeyPressEventArgs args) {
+               if ((args.Event.State & Gdk.ModifierType.ControlMask) != 0) {
+               } else if ((args.Event.State & Gdk.ModifierType.Mod1Mask) != 0) {
+               } else {
+                       switch(args.Event.Key) {
+                       case Gdk.Key.Up:
+                       case Gdk.Key.Down:
+                       case Gdk.Key.Left:
+                       case Gdk.Key.Right:
+                       case Gdk.Key.Page_Up:
+                       case Gdk.Key.Page_Down:
+                       case Gdk.Key.Home:
+                       case Gdk.Key.End:
+                               MoveCursor(args.Event.Key);
+                               break;
+                       case Gdk.Key.Delete:
+                               text.Delete(cursor.x, cursor.y);
+                               break;
+                       case Gdk.Key.BackSpace:
+                               int l = 0;
+                               if (cursor.y > 0) l = text.LineLength(cursor.y-1);
+                               text.Backspace(cursor.x, cursor.y);
+                               if (cursor.x == 0) {
+                                       cursor.y--;
+                                       if (cursor.y >= text.Lines.Count) return;
+                                       cursor.x = l;
+                               } else {
+                                       cursor.x--;
+                               }
+                               break;
+                       case Gdk.Key.Return:
+                               text.Newline(cursor.x, cursor.y);
+                               cursor.x = 0;
+                               cursor.y++;
+                               break;
+                       default:
+                               AddChar((int)args.Event.KeyValue);
+                               break;
+                       }
+               }
+       }
+       
        public void AddChar(int c) {
                c = c & 0xFF;
                if (c > 128) return;
 
-               switch(c) {
-               case 8:
-                       int l = 0;
-                       if (cursor.y > 0) l = text.LineLength(cursor.y-1);
-                       text.Backspace(cursor.x, cursor.y);
-                       if (cursor.x == 0) {
-                               cursor.y--;
-                               if (cursor.y >= text.Lines.Count) return;
-                               cursor.x = l;
-                       } else {
-                               cursor.x--;
-                       }
-                       break;
-               case 13:
-                       text.Newline(cursor.x, cursor.y);
-                       cursor.x = 0;
-                       cursor.y++;
-                       break;
-               default:
-                       text.Insert(cursor.x, cursor.y, (char)c);
-                       cursor.x++;
-                       break;
-               }
+               text.Insert(cursor.x, cursor.y, (char)c);
+               cursor.x++;
        }
 }