Browse Source

Updated HexEditor.
Fixed HexView Frame->Viewport

Tig 1 year ago
parent
commit
58dbdf4758
2 changed files with 48 additions and 53 deletions
  1. 10 19
      Terminal.Gui/Views/HexView.cs
  2. 38 34
      UICatalog/Scenarios/HexEditor.cs

+ 10 - 19
Terminal.Gui/Views/HexView.cs

@@ -249,8 +249,6 @@ public class HexView : View
     /// <inheritdoc/>
     protected internal override bool OnMouseEvent  (MouseEvent me)
     {
-        // BUGBUG: Test this with a border! Assumes Frame == Viewport!
-
         if (!me.Flags.HasFlag (MouseFlags.Button1Clicked)
             && !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
             && !me.Flags.HasFlag (MouseFlags.WheeledDown)
@@ -343,20 +341,17 @@ public class HexView : View
         Driver.SetAttribute (current);
         Move (0, 0);
 
-        // BUGBUG: Viewport!!!!
-        Rectangle frame = Frame;
-
         int nblocks = bytesPerLine / bsize;
-        var data = new byte [nblocks * bsize * frame.Height];
+        var data = new byte [nblocks * bsize * viewport.Height];
         Source.Position = displayStart;
         int n = source.Read (data, 0, data.Length);
 
         Attribute activeColor = ColorScheme.HotNormal;
         Attribute trackingColor = ColorScheme.HotFocus;
 
-        for (var line = 0; line < frame.Height; line++)
+        for (var line = 0; line < viewport.Height; line++)
         {
-            Rectangle lineRect = new (0, line, frame.Width, 1);
+            Rectangle lineRect = new (0, line, viewport.Width, 1);
 
             if (!Viewport.Contains (lineRect))
             {
@@ -597,16 +592,15 @@ public class HexView : View
 
     private bool MoveDown (int bytes)
     {
-        // BUGBUG: Viewport!
         RedisplayLine (position);
 
         if (position + bytes < source.Length)
         {
             position += bytes;
         }
-        else if ((bytes == bytesPerLine * Frame.Height && source.Length >= DisplayStart + bytesPerLine * Frame.Height)
-                 || (bytes <= bytesPerLine * Frame.Height - bytesPerLine
-                     && source.Length <= DisplayStart + bytesPerLine * Frame.Height))
+        else if ((bytes == bytesPerLine * Viewport.Height && source.Length >= DisplayStart + bytesPerLine * Viewport.Height)
+                 || (bytes <= bytesPerLine * Viewport.Height - bytesPerLine
+                     && source.Length <= DisplayStart + bytesPerLine * Viewport.Height))
         {
             long p = position;
 
@@ -618,7 +612,7 @@ public class HexView : View
             position = p;
         }
 
-        if (position >= DisplayStart + bytesPerLine * Frame.Height)
+        if (position >= DisplayStart + bytesPerLine * Viewport.Height)
         {
             SetDisplayStart (DisplayStart + bytes);
             SetNeedsDisplay ();
@@ -635,8 +629,7 @@ public class HexView : View
     {
         position = source.Length;
 
-        // BUGBUG: Viewport!
-        if (position >= DisplayStart + bytesPerLine * Frame.Height)
+        if (position >= DisplayStart + bytesPerLine * Viewport.Height)
         {
             SetDisplayStart (position);
             SetNeedsDisplay ();
@@ -722,8 +715,7 @@ public class HexView : View
             position++;
         }
 
-        // BUGBUG: Viewport!
-        if (position >= DisplayStart + bytesPerLine * Frame.Height)
+        if (position >= DisplayStart + bytesPerLine * Viewport.Height)
         {
             SetDisplayStart (DisplayStart + bytesPerLine);
             SetNeedsDisplay ();
@@ -771,8 +763,7 @@ public class HexView : View
         var delta = (int)(pos - DisplayStart);
         int line = delta / bytesPerLine;
 
-        // BUGBUG: Viewport!
-        SetNeedsDisplay (new (0, line, Frame.Width, 1));
+        SetNeedsDisplay (new (0, line, Viewport.Width, 1));
     }
 
     private bool ToggleSide ()

+ 38 - 34
UICatalog/Scenarios/HexEditor.cs

@@ -16,23 +16,30 @@ public class HexEditor : Scenario
     private HexView _hexView;
     private MenuItem _miAllowEdits;
     private bool _saved = true;
-#if V2_STATUSBAR
-    private StatusItem _siPositionChanged;
+    private Shortcut _siPositionChanged;
     private StatusBar _statusBar;
-#endif
 
-    public override void Setup ()
+    public override void Main ()
     {
-        Win.Title = GetName () + "-" + _fileName ?? "Untitled";
+        Toplevel app = new Toplevel ()
+        {
+            ColorScheme = Colors.ColorSchemes ["Base"]
+        };
 
         CreateDemoFile (_fileName);
 
-        //CreateUnicodeDemoFile (_fileName);
-
-        _hexView = new HexView (LoadFile ()) { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
+        _hexView = new HexView (new MemoryStream (Encoding.UTF8.GetBytes ("Demo text.")))
+        {
+            X = 0,
+            Y = 1,
+            Width = Dim.Fill (),
+            Height = Dim.Fill (1),
+            Title = _fileName ?? "Untitled",
+            BorderStyle = LineStyle.Rounded,
+        };
         _hexView.Edited += _hexView_Edited;
         _hexView.PositionChanged += _hexView_PositionChanged;
-        Win.Add (_hexView);
+        app.Add (_hexView);
 
         var menu = new MenuBar
         {
@@ -76,20 +83,20 @@ public class HexEditor : Scenario
                                 )
             ]
         };
-        Top.Add (menu);
-#if V2_STATUSBAR
+        app.Add (menu);
+
         _statusBar = new StatusBar (
                                     new []
                                     {
-                                        new (KeyCode.F2, "~F2~ Open", () => Open ()),
-                                        new (KeyCode.F3, "~F3~ Save", () => Save ()),
+                                        new (Key.F2, "Open", () => Open ()),
+                                        new (Key.F3, "Save", () => Save ()),
                                         new (
                                              Application.QuitKey,
-                                             $"{Application.QuitKey} to Quit",
+                                             $"Quit",
                                              () => Quit ()
                                             ),
-                                        _siPositionChanged = new StatusItem (
-                                                                             KeyCode.Null,
+                                        _siPositionChanged = new Shortcut (
+                                                                             Key.Empty,
                                                                              $"Position: {
                                                                                  _hexView.Position
                                                                              } Line: {
@@ -102,28 +109,25 @@ public class HexEditor : Scenario
                                                                              () => { }
                                                                             )
                                     }
-                                   );
-        Top.Add (_statusBar);
-#endif
+                                   )
+        {
+            AlignmentModes = AlignmentModes.IgnoreFirstOrLast
+        };
+        app.Add (_statusBar);
+
+        _hexView.Source = LoadFile ();
+
+        Application.Run (app);
+        app.Dispose ();
+        Application.Shutdown ();
     }
 
     private void _hexView_Edited (object sender, HexViewEditEventArgs e) { _saved = false; }
 
     private void _hexView_PositionChanged (object sender, HexViewEventArgs obj)
     {
-#if V2_STATUSBAR
         _siPositionChanged.Title =
-            $"Position: {
-                obj.Position
-            } Line: {
-                obj.CursorPosition.Y
-            } Col: {
-                obj.CursorPosition.X
-            } Line length: {
-                obj.BytesPerLine
-            }";
-        _statusBar.SetNeedsDisplay ();
-#endif
+            $"Position: {obj.Position} Line: {obj.CursorPosition.Y} Col: {obj.CursorPosition.X} Line length: {obj.BytesPerLine}";
     }
 
     private void Copy () { MessageBox.ErrorQuery ("Not Implemented", "Functionality not yet implemented.", "Ok"); }
@@ -159,7 +163,7 @@ public class HexEditor : Scenario
     {
         var stream = new MemoryStream ();
 
-        if (!_saved && _hexView != null && _hexView.Edits.Count > 0)
+        if (!_saved && _hexView.Edits.Count > 0)
         {
             if (MessageBox.ErrorQuery (
                                        "Save",
@@ -180,12 +184,12 @@ public class HexEditor : Scenario
         {
             byte [] bin = File.ReadAllBytes (_fileName);
             stream.Write (bin);
-            Win.Title = GetName () + "-" + _fileName;
+            _hexView.Title = _fileName;
             _saved = true;
         }
         else
         {
-            Win.Title = GetName () + "-" + (_fileName ?? "Untitled");
+            _hexView.Title = (_fileName ?? "Untitled");
         }
 
         return stream;