2 using System.Collections;
9 public Cursor(int x, int y) {
19 if (value < 0) _x = 0;
29 if (value < 0) _y = 0;
35 public class TextArray {
36 private ArrayList lines;
39 lines = new ArrayList();
43 public TextArray(string filename) {
44 lines = new ArrayList();
48 file = new StreamReader(filename);
49 while ((line = file.ReadLine()) != null) {
54 public ArrayList Lines {
60 public void Insert(int x, int y, char c) {
61 while (lines.Count < y+1)
63 string s = (string)lines[y];
65 if (x >= s.Length && (c == ' ' || c == '\t')) return;
67 lines[y] = s.PadRight(x) + c;
68 } else if (x <= s.Length) {
69 lines[y] = s.Insert(x, new string(c, 1));
73 public void Delete(int x, int y) {
74 if (y >= lines.Count) return;
76 string s = (string)lines[y];
77 if (x >= s.Length) return;
79 if (s.Length == 0 && x == 0) {
86 public void Backspace(int x, int y) {
87 if (x == 0 && y == 0) return;
88 if (y >= lines.Count) return;
90 string s = (string)lines[y];
91 if (x > s.Length) return;
93 if (s.Length == 0 && x == 0) {
95 } else if (x == 0 && y > 0) {
99 lines[y] = s.Remove(x-1, 1);
103 public void Newline(int x, int y) {
104 if (y >= lines.Count) return;
105 string s = (string) lines[y];
107 lines[y] = s.Substring(0, x);
108 lines.Insert(y + 1, s.Substring(x));
110 if (y == lines.Count - 1) return;
111 lines.Insert(y + 1, "");
115 public int LineLength(int y) {
116 if (y >= lines.Count) return 0;
117 return ((string)lines[y]).Length;
121 public class TextDocument {
122 private TextArray text;
123 public Cursor cursor;
125 public ArrayList Lines {
131 public TextDocument() {
132 text = new TextArray();
135 public TextDocument(string filename) {
136 text = new TextArray(filename);
139 public void MoveCursor(Gdk.Key k) {
154 cursor.x = text.LineLength(cursor.y);
162 public void AddChar(int c) {
169 if (cursor.y > 0) l = text.LineLength(cursor.y-1);
170 text.Backspace(cursor.x, cursor.y);
173 if (cursor.y >= text.Lines.Count) return;
180 text.Newline(cursor.x, cursor.y);
185 text.Insert(cursor.x, cursor.y, (char)c);