Added text scrolling
[Nebula.git] / TextDisplay.cs
index 0abb9e9..9389f00 100644 (file)
@@ -5,10 +5,18 @@ using Gtk;
 
 public class TextDisplay : DrawingArea, IKeyPress {
        private TextDocument doc = null;
-       private FontDescription font;
+       private Font font;
+       // FIXME: Proportional fonts?
+       private float fontwidth;
+       private float fontheight;
 
        public TextDisplay() {
-               font = Pango.FontDescription.FromString("Courier 12");
+               FontDescription fd = Pango.FontDescription.FromString("Courier 12");
+               font = this.PangoContext.LoadFont(fd);
+               FontMetrics fm = font.GetMetrics(null);
+               fontheight = Pango.Units.ToPixels(fm.Ascent + fm.Descent);
+               fontwidth = fontheight * 0.5333f;
+               Console.WriteLine("Using {0} {1}pt ({2}x{3}px)", fd.Family, fd.Size, fontwidth, fontheight);
                ModifyBg(StateType.Normal, new Gdk.Color(0xFF, 0xFF, 0xFF));
        }
 
@@ -20,25 +28,18 @@ public class TextDisplay : DrawingArea, IKeyPress {
                if (doc == null) return;
 
                Pango.Layout layout = new Pango.Layout(this.PangoContext);
-               layout.Width = Pango.Units.FromPixels(width);
-               layout.FontDescription = font;
+               layout.Width = 0x7FFFFFFF; //Pango.Units.FromPixels(width);
+               layout.FontDescription = font.Describe();
 
                //gr.Operator = Operator.Atop;
                gr.SetSourceRGB(0,0,0);
-               int h = Pango.Units.ToPixels(layout.FontDescription.Size) + 1;
-               int y = 0;
-               int line = 0;
-               while (line < doc.Lines.Count && y < height) {
-                       layout.SetText((String)doc.Lines[line]);
-                       gr.MoveTo(0, y);
-                       Pango.CairoHelper.ShowLayout(gr, layout);
-                       y += h;
-                       line++;
-               }
+               layout.SetText(doc.GetTextWindow());
+               gr.MoveTo(0, 0);
+               Pango.CairoHelper.ShowLayout(gr, layout);
 
-               //gr.Operator = Operator.Xor;
+               gr.Operator = Operator.Xor;
                gr.NewPath();
-               gr.Rectangle(new Cairo.Rectangle(doc.cursor.x*8, doc.cursor.y*h, 8, h));
+               gr.Rectangle(new Cairo.Rectangle((doc.cursor.x - doc.textorigin.x)*fontwidth, (doc.cursor.y - doc.textorigin.y)*fontheight, fontwidth, fontheight));
                gr.Fill();
         }
 
@@ -70,6 +71,12 @@ public class TextDisplay : DrawingArea, IKeyPress {
                 return true;
         }
 
+       protected override void OnSizeAllocated(Gdk.Rectangle r) {
+               base.OnSizeAllocated(r);
+               if (doc != null)
+                       doc.SetPageSize((int)(r.Width / fontwidth) - 1, (int)(r.Height / fontheight) - 1);
+       }
+
        public void Update() {
                QueueDraw();
        }
@@ -78,25 +85,6 @@ public class TextDisplay : DrawingArea, IKeyPress {
         [GLib.ConnectBefore ()]
         public void KeyPress(object o, KeyPressEventArgs args) {
                if (doc == null) return;
-
-                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:
-                                doc.MoveCursor(args.Event.Key);
-                                break;
-                        default:
-                                doc.AddChar((int)args.Event.KeyValue);
-                                break;
-                        }
-                }
+               doc.KeyPress(o, args);
        }
 }