Fixed help file parsing
[Nebula.git] / TextDisplay.cs
index 0abb9e9..282fc7f 100644 (file)
@@ -1,14 +1,30 @@
 using System;
+using System.Timers;
 using Cairo;
 using Pango;
+using Gdk;
 using Gtk;
 
 public class TextDisplay : DrawingArea, IKeyPress {
        private TextDocument doc = null;
-       private FontDescription font;
+       private Pango.Font font;
+       // FIXME: Proportional fonts?
+       private float fontWidth;
+       private float fontHeight;
+
+       private Timer helpTimer;
+       private HelpBox controlHelp;
+       private bool showHelp;
 
        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);
+
+               controlHelp = new HelpBox(this.PangoContext, "control_help");
                ModifyBg(StateType.Normal, new Gdk.Color(0xFF, 0xFF, 0xFF));
        }
 
@@ -20,30 +36,26 @@ 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.SetSourceRGB(0.0, 0.0, 0.0);
                gr.NewPath();
-               gr.Rectangle(new Cairo.Rectangle(doc.cursor.x*8, doc.cursor.y*h, 8, h));
+               gr.Rectangle((doc.cursor.x - doc.textorigin.x)*fontWidth, (doc.cursor.y - doc.textorigin.y)*fontHeight, fontWidth, fontHeight);
                gr.Fill();
+
+               if (showHelp) {
+                       controlHelp.draw(gr, width, height);
+               }
         }
 
-        protected override bool OnExposeEvent(Gdk.EventExpose args)
-        {
+        protected override bool OnExposeEvent(Gdk.EventExpose args) {
                 Gdk.Window win = args.Window;                
 
                #if OLD_SYSTEMS
@@ -70,33 +82,64 @@ 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();
        }
 
+       public void ShowHelp() {
+               showHelp = true;
+               helpTimer = null;
+               QueueDraw();
+       }
+
+       public void HideHelp() {
+               if (helpTimer != null) {
+                       helpTimer.Stop();
+                       helpTimer = null;
+               }
+               if (showHelp)
+                       showHelp = false;
+               QueueDraw();
+       }
 
         [GLib.ConnectBefore ()]
         public void KeyPress(object o, KeyPressEventArgs args) {
                if (doc == null) return;
+               if ((args.Event.State & ModifierType.ControlMask) != 0) {
+                       switch(args.Event.Key) {
+                       default:
+                               HideHelp();
+                               break;
+                       }
+               } else {
+                       switch(args.Event.Key) {
+                       case Gdk.Key.Control_L:
+                       case Gdk.Key.Control_R:
+                               helpTimer = new Timer();
+                               helpTimer.Elapsed += delegate(object o2, ElapsedEventArgs e) {
+                                       ShowHelp();
+                               };
+                               helpTimer.AutoReset = false;
+                               helpTimer.Interval = 1000;
+                               helpTimer.Start();
+                               break;
+                       }
+               }
+               doc.KeyPress(o, args);
+       }
 
-                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;
-                        }
-                }
+       public void KeyRelease(object o, KeyReleaseEventArgs e) {
+               switch(e.Event.Key) {
+               case Gdk.Key.Control_L:
+               case Gdk.Key.Control_R:
+                       HideHelp();
+                       break;
+               }
        }
 }