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];
78 if (y == lines.Count - 1) return;
81 lines[y] = s + (String)lines[y+1];
83 } else if (s.Length == 0 && x == 0) {
86 lines[y] = s.Remove(x, 1);
90 public void Backspace(int x, int y) {
91 if (x == 0 && y == 0) return;
92 if (y >= lines.Count) return;
94 string s = (string)lines[y];
95 if (x > s.Length) return;
97 if (s.Length == 0 && x == 0) {
99 } else if (x == 0 && y > 0) {
103 lines[y] = s.Remove(x-1, 1);
107 public void Newline(int x, int y) {
108 if (y >= lines.Count) return;
109 string s = (string) lines[y];
111 lines[y] = s.Substring(0, x);
112 lines.Insert(y + 1, s.Substring(x));
114 if (y == lines.Count - 1) return;
115 lines.Insert(y + 1, "");
119 public int LineLength(int y) {
120 if (y >= lines.Count) return 0;
121 return ((string)lines[y]).Length;
125 public class TextDocument : IKeyPress {
126 private TextArray text;
127 public Cursor cursor;
128 public Cursor textorigin;
129 private int pagewidth, pageheight;
131 public TextDocument() {
132 text = new TextArray();
135 public TextDocument(string filename) {
136 text = new TextArray(filename);
139 public string GetTextWindow() {
142 for (int i = textorigin.y; textorigin.y + i < text.Lines.Count && i < pageheight; i++) {
143 String l = (String)text.Lines[i];
144 s += l.Substring(textorigin.x) + "\n";
149 private void FixTextWindow() {
150 if (cursor.x < textorigin.x) textorigin.x = cursor.x;
151 if (cursor.y < textorigin.y) textorigin.y = cursor.y;
152 if (cursor.x > textorigin.x + pagewidth) textorigin.x = cursor.x - pagewidth;
153 if (cursor.y > textorigin.y + pageheight) textorigin.y = cursor.y - pageheight;
156 public void SetPageSize(int w, int h) {
162 public void MoveCursor(Gdk.Key k) {
177 cursor.x = text.LineLength(cursor.y);
182 case Gdk.Key.Page_Up:
183 cursor.y -= pageheight;
185 case Gdk.Key.Page_Down:
186 cursor.y += pageheight;
192 [GLib.ConnectBefore ()]
193 public void KeyPress(object o, Gtk.KeyPressEventArgs args) {
194 if ((args.Event.State & Gdk.ModifierType.ControlMask) != 0) {
195 } else if ((args.Event.State & Gdk.ModifierType.Mod1Mask) != 0) {
197 switch(args.Event.Key) {
202 case Gdk.Key.Page_Up:
203 case Gdk.Key.Page_Down:
206 MoveCursor(args.Event.Key);
209 text.Delete(cursor.x, cursor.y);
211 case Gdk.Key.BackSpace:
213 if (cursor.y > 0) l = text.LineLength(cursor.y-1);
214 text.Backspace(cursor.x, cursor.y);
217 if (cursor.y >= text.Lines.Count) return;
224 text.Newline(cursor.x, cursor.y);
229 AddChar((int)args.Event.KeyValue);
235 public void AddChar(int c) {
239 text.Insert(cursor.x, cursor.y, (char)c);