Added help popup, Ctrl-Left and Right cursor movement by word
[Nebula.git] / TextDocument.cs
index 4f1103f..21f813a 100644 (file)
@@ -151,9 +151,13 @@ public class TextDocument : IKeyPress {
        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";
+               for (int i = 0; i <= pageheight + 1 && textorigin.y + i < text.Lines.Count; i++) {
+                       string l = (string)text.Lines[textorigin.y + i];
+                       if (textorigin.x > l.Length) {
+                               s += "\n";
+                       } else {
+                               s += l.Substring(textorigin.x) + "\n";
+                       }
                }
                return s;
        }
@@ -198,12 +202,72 @@ public class TextDocument : IKeyPress {
                        cursor.y += pageheight;
                        break;
                }
-               FixTextWindow();
+       }
+
+       public void NextWord() {
+               if (cursor.x >= text.LineLength(cursor.y)) {
+                       cursor.y++;
+                       cursor.x = 0;
+               } else {
+                       string s = (String)text.Lines[cursor.y];
+                       int i = s.IndexOf(' ', cursor.x);
+                       if (i == -1) {
+                               cursor.y++;
+                               cursor.x = 0;
+                       } else {
+                               cursor.x = i + 1;
+                       }
+               }
+       }
+
+       public void PreviousWord() {
+               if (cursor.y > text.Lines.Count) {
+                       cursor.x = 0;
+                       cursor.y--;
+               } else if (cursor.y == text.Lines.Count) {
+                       string s = (String)text.Lines[text.Lines.Count - 1];
+                       int i = s.LastIndexOf(' ');
+                       if (i == -1) {
+                               cursor.x = 0;
+                       } else {
+                               cursor.x = i + 1;
+                       }
+                       cursor.y--;
+               } else if (cursor.x == 0 && cursor.y > 0) {
+                       string s = (String)text.Lines[cursor.y - 1];
+                       int i = s.LastIndexOf(' ');
+                       if (i == -1) {
+                               cursor.x = 0;
+                       } else {
+                               cursor.x = i + 1;
+                       }
+                       cursor.y--;
+               } else if (cursor.x == 0 && cursor.y == 0) {
+                       return;
+               } else {
+                       string s = (String)text.Lines[cursor.y];
+                       if (cursor.x >= s.Length)
+                               cursor.x = s.Length - 1;
+                       int i = s.LastIndexOf(' ', cursor.x - 2, cursor.x - 2);
+                       if (i == -1) {
+                               cursor.x = 0;
+                       } else {
+                               cursor.x = i + 1;
+                       }
+               }
        }
 
        [GLib.ConnectBefore ()]
        public void KeyPress(object o, Gtk.KeyPressEventArgs args) {
                if ((args.Event.State & Gdk.ModifierType.ControlMask) != 0) {
+                       switch(args.Event.Key) {
+                       case Gdk.Key.Right:
+                               NextWord();
+                               break;
+                       case Gdk.Key.Left:
+                               PreviousWord();
+                               break;
+                       }
                } else if ((args.Event.State & Gdk.ModifierType.Mod1Mask) != 0) {
                } else {
                        switch(args.Event.Key) {
@@ -242,6 +306,7 @@ public class TextDocument : IKeyPress {
                                break;
                        }
                }
+               FixTextWindow();
        }
        
        public void AddChar(int c) {