Commits

Lars Brubaker committed 0f391bb

Fixed a bug in textedit control that could happen when the mouse selected the
control and it had no content.
Wrote a unit test to be sure this never happens again.
Made the gcode visualizer actually select the text control on startup.

Comments (0)

Files changed (3)

Gui/TextEditWidget.cs

             if (mouseIsDown)
             {
                 selectionIndexToStartBefore = internalTextWidget.Printer.GetCharacterIndexToStartBefore(new Vector2(mouseEvent.X, mouseEvent.Y));
+                if (selectionIndexToStartBefore < 0)
+                {
+                    selectionIndexToStartBefore = 0;
+                }
                 if (selectionIndexToStartBefore != charIndexToInsertBefore)
                 {
                     selecting = true;
 
                 case Keys.End:
                     if (keyEvent.Control)
+                    {
                         charIndexToInsertBefore = internalTextWidget.Text.Length;
+                    }
                     else
+                    {
                         GotoEndOfCurrentLine();
+                    }
+
                     keyEvent.SuppressKeyPress = true;
                     keyEvent.Handled = true;
                     break;
 
                 case Keys.Home:
                     if (keyEvent.Control)
+                    {
                         charIndexToInsertBefore = 0;
+                    }
                     else
+                    {
                         GotoStartOfCurrentLine();
+                    }
+
                     keyEvent.SuppressKeyPress = true;
                     keyEvent.Handled = true;
                     break;
         }
 
         [Test]
-        public void TextEditGetsInput()
+        public void TextEditTextSelectionTests()
+        {
+            RectangleWidget container = new RectangleWidget();
+            container.LocalBounds = new rect_d(0, 0, 200, 200);
+            TextEditWidget editField1 = new TextEditWidget("", 0, 0, pixelWidth: 20);
+            container.AddChild(editField1);
+
+            container.OnLayout();
+
+            // mouse select on the control when it contains nothing
+            container.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 1, 1, 0));
+            container.OnMouseMove(new MouseEventArgs(MouseButtons.Left, 0, -1, 1, 0));
+            container.OnMouseUp(new MouseEventArgs(MouseButtons.Left, -1, 1, 1, 0));
+            SendKey(Keys.D, 'b', container);
+            Assert.IsTrue(editField1.Text == "b", "It should have b a in it.");
+        }
+
+        [Test]
+        public void TextEditGetsFocusTests()
         {
             RectangleWidget container = new RectangleWidget();
             container.LocalBounds = new rect_d(0, 0, 200, 200);
                 buttonTests.WindowBlocksButtonClick();
 
                 TextEditTests textEditTests = new TextEditTests();
-                textEditTests.TextEditGetsInput();
+                textEditTests.TextEditGetsFocusTests();
+                textEditTests.TextEditTextSelectionTests();
             }
         }
     }

examples/GCodeVisualizer/GCodeVisualizer.cs

             currentLayerIndex.Multiline = false;
             keepOnTop.AddChild(currentLayerIndex);
             currentLayerIndex.TextChanged += new TextEditWidget.TextChangedEventHandler(layerCountTextWidget_TextChanged);
-            currentLayerIndex.Focus();
 
             layerCountTextWidget = new TextWidget("/1____", 12);
             keepOnTop.AddChild(layerCountTextWidget);
             ResumeLayout();
         }
 
-        public override void OnParentChanged()
-        {
-            SetAnchor(AnchorFlags.All);
+        public override void  OnLayout()
+        {
+            SetAnchor(AnchorFlags.All);
+            currentLayerIndex.Focus();
+
+            base.OnLayout();
         }
 
         void SetActiveLayer(int layer)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.