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) {
55 public ArrayList Lines {
61 public void Insert(int x, int y, char c) {
62 while (lines.Count < y+1)
64 string s = (string)lines[y];
66 if (x >= s.Length && (c == ' ' || c == '\t')) return;
68 lines[y] = s.PadRight(x) + c;
69 } else if (x <= s.Length) {
70 lines[y] = s.Insert(x, new string(c, 1));
74 public void Delete(int x, int y) {
75 if (y >= lines.Count) return;
77 string s = (string)lines[y];
79 if (y == lines.Count - 1) return;
82 lines[y] = s + (string)lines[y+1];
84 } else if (s.Length == 0 && x == 0) {
87 lines[y] = s.Remove(x, 1);
91 public void Backspace(int x, int y) {
92 if (x == 0 && y == 0) return;
93 if (y >= lines.Count) return;
95 string s = (string)lines[y];
96 if (x > s.Length) return;
98 if (s.Length == 0 && x == 0) {
100 } else if (x == 0 && y > 0) {
104 lines[y] = s.Remove(x-1, 1);
108 public void Newline(int x, int y) {
109 if (y >= lines.Count) return;
110 string s = (string) lines[y];
112 lines[y] = s.Substring(0, x);
113 lines.Insert(y + 1, s.Substring(x));
115 if (y == lines.Count - 1) return;
116 lines.Insert(y + 1, "");
120 public int LineLength(int y) {
121 if (y >= lines.Count) return 0;
122 return ((string)lines[y]).Length;
125 public void Save(string filename) {
126 StreamWriter file = new StreamWriter(filename);
127 for (int i = 0; i < lines.Count; i++) {
128 file.Write((string)lines[i]);
135 public class TextDocument : IKeyPress {
136 private TextArray text;
137 public string filename = null;
138 public Cursor cursor;
139 public Cursor textorigin;
140 private int pagewidth, pageheight;
142 public TextDocument() {
143 text = new TextArray();
146 public TextDocument(string filename) {
147 text = new TextArray(filename);
148 this.filename = filename;
151 public string GetTextWindow() {
154 for (int i = 0; i <= pageheight + 1 && textorigin.y + i < text.Lines.Count; i++) {
155 string l = (string)text.Lines[textorigin.y + i];
156 if (textorigin.x > l.Length) {
159 s += l.Substring(textorigin.x) + "\n";
165 private void FixTextWindow() {
166 if (cursor.x < textorigin.x) textorigin.x = cursor.x;
167 if (cursor.y < textorigin.y) textorigin.y = cursor.y;
168 if (cursor.x > textorigin.x + pagewidth) textorigin.x = cursor.x - pagewidth;
169 if (cursor.y > textorigin.y + pageheight) textorigin.y = cursor.y - pageheight;
172 public void SetPageSize(int w, int h) {
178 public void MoveCursor(Gdk.Key k) {
193 cursor.x = text.LineLength(cursor.y);
198 case Gdk.Key.Page_Up:
199 cursor.y -= pageheight;
201 case Gdk.Key.Page_Down:
202 cursor.y += pageheight;
207 public void NextWord() {
208 if (cursor.x >= text.LineLength(cursor.y)) {
212 string s = (String)text.Lines[cursor.y];
213 int i = s.IndexOf(' ', cursor.x);
223 public void PreviousWord() {
224 if (cursor.y > text.Lines.Count) {
227 } else if (cursor.y == text.Lines.Count) {
228 string s = (String)text.Lines[text.Lines.Count - 1];
229 int i = s.LastIndexOf(' ');
236 } else if (cursor.x == 0 && cursor.y > 0) {
237 string s = (String)text.Lines[cursor.y - 1];
238 int i = s.LastIndexOf(' ');
245 } else if (cursor.x == 0 && cursor.y == 0) {
248 string s = (String)text.Lines[cursor.y];
249 if (cursor.x >= s.Length)
250 cursor.x = s.Length - 1;
251 int i = s.LastIndexOf(' ', cursor.x - 2, cursor.x - 2);
260 [GLib.ConnectBefore ()]
261 public void KeyPress(object o, Gtk.KeyPressEventArgs args) {
262 if ((args.Event.State & Gdk.ModifierType.ControlMask) != 0) {
263 switch(args.Event.Key) {
271 } else if ((args.Event.State & Gdk.ModifierType.Mod1Mask) != 0) {
273 switch(args.Event.Key) {
278 case Gdk.Key.Page_Up:
279 case Gdk.Key.Page_Down:
282 MoveCursor(args.Event.Key);
285 text.Delete(cursor.x, cursor.y);
287 case Gdk.Key.BackSpace:
289 if (cursor.y > 0) l = text.LineLength(cursor.y-1);
290 text.Backspace(cursor.x, cursor.y);
293 if (cursor.y >= text.Lines.Count) return;
300 text.Newline(cursor.x, cursor.y);
305 AddChar((int)args.Event.KeyValue);
312 public void AddChar(int c) {
316 text.Insert(cursor.x, cursor.y, (char)c);
321 if (filename == null)
322 throw new InvalidOperationException("Cannot Save() without filename");
326 public void Save(string filename) {
327 this.filename = filename;