From 394ace2623f36458637213f72f6736c51b8a0b82 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 3 Apr 2021 17:51:27 +0100 Subject: [PATCH 1/4] Fixes #1171. Delete and Backspace now deletes the selected text. (#1172) * Fixes #1171. Delete and Backspace now deletes the selected text. * Replacing with StopSelecting method. --- Terminal.Gui/Views/TextView.cs | 36 ++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index a53274427..18b90b9e2 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -1340,12 +1340,6 @@ namespace Terminal.Gui { List rest; bool lineRemoved = false; - if (shiftSelecting && selecting && !kb.Key.HasFlag (Key.ShiftMask) - && !kb.Key.HasFlag (Key.CtrlMask | Key.C)) { - shiftSelecting = false; - selecting = false; - } - // Handle some state here - whether the last command was a kill // operation and the column tracking (up/down) switch (kb.Key) { @@ -1370,6 +1364,8 @@ namespace Terminal.Gui { case Key.PageDown | Key.ShiftMask: if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } int nPageDnShift = Frame.Height - 1; if (currentRow < model.Count) { @@ -1390,6 +1386,8 @@ namespace Terminal.Gui { case Key.PageUp | Key.ShiftMask: if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } int nPageUpShift = Frame.Height - 1; if (currentRow > 0) { @@ -1410,6 +1408,8 @@ namespace Terminal.Gui { case Key.CursorDown | Key.ShiftMask: if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } MoveDown (); break; @@ -1419,6 +1419,8 @@ namespace Terminal.Gui { case Key.CursorUp | Key.ShiftMask: if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } MoveUp (); break; @@ -1428,6 +1430,8 @@ namespace Terminal.Gui { case Key.CursorRight | Key.ShiftMask: if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } var currentLine = GetCurrentLine (); if (currentColumn < currentLine.Count) { @@ -1450,6 +1454,8 @@ namespace Terminal.Gui { case Key.CursorLeft | Key.ShiftMask: if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } if (currentColumn > 0) { currentColumn--; @@ -1516,6 +1522,8 @@ namespace Terminal.Gui { case Key.A | Key.CtrlMask: if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } currentColumn = 0; Adjust (); @@ -1555,6 +1563,8 @@ namespace Terminal.Gui { case Key.E | Key.CtrlMask: // End if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } currentLine = GetCurrentLine (); currentColumn = currentLine.Count; @@ -1645,6 +1655,8 @@ namespace Terminal.Gui { case (Key)((int)'B' + Key.AltMask): if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } var newPos = WordBackward (currentColumn, currentRow); if (newPos.HasValue) { @@ -1660,6 +1672,8 @@ namespace Terminal.Gui { case (Key)((int)'F' + Key.AltMask): if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } newPos = WordForward (currentColumn, currentRow); if (newPos.HasValue) { @@ -1703,6 +1717,8 @@ namespace Terminal.Gui { case Key.CtrlMask | Key.End | Key.ShiftMask: if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } MoveEnd (); break; @@ -1711,6 +1727,8 @@ namespace Terminal.Gui { case Key.CtrlMask | Key.Home | Key.ShiftMask: if (kb.Key.HasFlag (Key.ShiftMask)) { StartSelecting (); + } else if (shiftSelecting && selecting) { + StopSelecting (); } MoveHome (); break; @@ -1814,6 +1832,12 @@ namespace Terminal.Gui { selectionStartRow = currentRow; } + void StopSelecting () + { + shiftSelecting = false; + selecting = false; + } + void MoveUp () { if (currentRow > 0) { From cf60d77dd53268d1bc973938d9afc18ff4b119c8 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 3 Apr 2021 17:51:57 +0100 Subject: [PATCH 2/4] Fixes #1173. TextField only need to handle a single line. (#1174) * Fixes #1173. TextField only need to handle a single line. * Also prevents new line on the Text set and on the constructor. --- Terminal.Gui/Views/TextField.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index 3c81a36c6..269546018 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -86,7 +86,7 @@ namespace Terminal.Gui { if (text == null) text = ""; - this.text = TextModel.ToRunes (text); + this.text = TextModel.ToRunes (text.Split ("\n") [0]); point = text.RuneCount; first = point > w ? point - w : 0; CanFocus = true; @@ -139,7 +139,7 @@ namespace Terminal.Gui { if (oldText == value) return; - var newText = OnTextChanging (value); + var newText = OnTextChanging (value.Split ("\n") [0]); if (newText.Cancel) { return; } @@ -826,8 +826,8 @@ namespace Terminal.Gui { (var _, var len) = TextModel.DisplaySize (text, 0, selStart, false); (var _, var len2) = TextModel.DisplaySize (text, selStart, selStart + selLength, false); (var _, var len3) = TextModel.DisplaySize (text, selStart + selLength, actualText.RuneCount, false); - Text = actualText[0, len] + - actualText[len + len2, len + len2 + len3]; + Text = actualText [0, len] + + actualText [len + len2, len + len2 + len3]; ClearAllSelection (); point = selStart >= Text.RuneCount ? Text.RuneCount : selStart; Adjust (); @@ -848,7 +848,7 @@ namespace Terminal.Gui { (int _, int len) = TextModel.DisplaySize (text, 0, selStart, false); (var _, var len2) = TextModel.DisplaySize (text, selStart, selStart + length, false); (var _, var len3) = TextModel.DisplaySize (text, selStart + length, actualText.RuneCount, false); - ustring cbTxt = Clipboard.Contents ?? ""; + ustring cbTxt = Clipboard.Contents.Split ("\n") [0] ?? ""; Text = actualText [0, len] + cbTxt + actualText [len + len2, len + len2 + len3]; @@ -874,12 +874,11 @@ namespace Terminal.Gui { /// /// Get / Set the wished cursor when the field is focused /// - public CursorVisibility DesiredCursorVisibility - { - get => desiredCursorVisibility; + public CursorVisibility DesiredCursorVisibility { + get => desiredCursorVisibility; set { if (desiredCursorVisibility != value && HasFocus) { - Application.Driver.SetCursorVisibility (value); + Application.Driver.SetCursorVisibility (value); } desiredCursorVisibility = value; From 76c292867a64c8686a3032451a55e32f1b883483 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 3 Apr 2021 17:52:39 +0100 Subject: [PATCH 3/4] Fixes #1177. Now is possible to copy or cut on the TextView with the mouse. (#1178) --- .../CursesDriver/CursesDriver.cs | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs b/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs index 2c054920c..1388bc7e5 100644 --- a/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs @@ -50,7 +50,7 @@ namespace Terminal.Gui { Curses.move (crow, ccol); needMove = false; } - Curses.addch ((int)(uint)MakePrintable(rune)); + Curses.addch ((int)(uint)MakePrintable (rune)); } else needMove = true; if (sync) @@ -71,7 +71,8 @@ namespace Terminal.Gui { AddRune (rune); } - public override void Refresh () { + public override void Refresh () + { Curses.refresh (); if (Curses.CheckWinChange ()) { Clip = new Rect (0, 0, Cols, Rows); @@ -87,7 +88,7 @@ namespace Terminal.Gui { } SetCursorVisibility (CursorVisibility.Default); - + Curses.endwin (); // Clear and reset entire screen. Console.Out.Write ("\x1b[2J"); @@ -100,7 +101,8 @@ namespace Terminal.Gui { int currentAttribute; - public override void SetAttribute (Attribute c) { + public override void SetAttribute (Attribute c) + { currentAttribute = c.Value; Curses.attrset (currentAttribute); } @@ -266,7 +268,7 @@ namespace Terminal.Gui { if (cev.ButtonState == Curses.Event.ReportMousePosition) { mouseFlag = MapCursesButton ((Curses.Event)LastMouseButtonPressed) | MouseFlags.ReportMousePosition; point = new Point (); - //cancelButtonClicked = true; + cancelButtonClicked = true; } else { point = new Point () { X = cev.X, @@ -700,23 +702,23 @@ namespace Terminal.Gui { // We are setting Invisible as default so we could ignore XTerm DECSUSR setting // switch (Curses.curs_set (0)) { - case 0: - currentCursorVisibility = initialCursorVisibility = CursorVisibility.Invisible; - break; + case 0: + currentCursorVisibility = initialCursorVisibility = CursorVisibility.Invisible; + break; - case 1: - currentCursorVisibility = initialCursorVisibility = CursorVisibility.Underline; - Curses.curs_set (1); - break; + case 1: + currentCursorVisibility = initialCursorVisibility = CursorVisibility.Underline; + Curses.curs_set (1); + break; - case 2: - currentCursorVisibility = initialCursorVisibility = CursorVisibility.Box; - Curses.curs_set (2); - break; + case 2: + currentCursorVisibility = initialCursorVisibility = CursorVisibility.Box; + Curses.curs_set (2); + break; - default: - currentCursorVisibility = initialCursorVisibility = null; - break; + default: + currentCursorVisibility = initialCursorVisibility = null; + break; } Curses.raw (); @@ -915,13 +917,13 @@ namespace Terminal.Gui { /// public override bool SetCursorVisibility (CursorVisibility visibility) { - if (initialCursorVisibility.HasValue == false) + if (initialCursorVisibility.HasValue == false) return false; - Curses.curs_set (((int) visibility >> 16) & 0x000000FF); + Curses.curs_set (((int)visibility >> 16) & 0x000000FF); if (visibility != CursorVisibility.Invisible) { - Console.Out.Write ("\x1b[{0} q", ((int) visibility >> 24) & 0xFF); + Console.Out.Write ("\x1b[{0} q", ((int)visibility >> 24) & 0xFF); Console.Out.Flush (); } From 99e26575bbb39b99c1c4ef472c2bbe57395262a1 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 3 Apr 2021 17:52:59 +0100 Subject: [PATCH 4/4] Fixes #1175. demo.cs editor now implement "Copy", "Cut" and "Paste". (#1176) --- Example/demo.cs | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/Example/demo.cs b/Example/demo.cs index f31ecd101..766193f07 100644 --- a/Example/demo.cs +++ b/Example/demo.cs @@ -242,17 +242,7 @@ static class Demo { var ntop = Application.Top; - var menu = new MenuBar (new MenuBarItem [] { - new MenuBarItem ("_File", new MenuItem [] { - new MenuItem ("_Close", "", () => { if (Quit ()) { running = MainApp; Application.RequestStop (); } }, null, null, Key.AltMask | Key.Q), - }), - new MenuBarItem ("_Edit", new MenuItem [] { - new MenuItem ("_Copy", "", null, null, null, Key.C | Key.CtrlMask), - new MenuItem ("C_ut", "", null, null, null, Key.X | Key.CtrlMask), - new MenuItem ("_Paste", "", null, null, null, Key.V | Key.CtrlMask) - }), - }); - ntop.Add (menu); + var text = new TextView () { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () }; string fname = GetFileName (); @@ -264,12 +254,43 @@ static class Demo { }; ntop.Add (win); - var text = new TextView () { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () }; - if (fname != null) text.Text = System.IO.File.ReadAllText (fname); win.Add (text); + void Paste () + { + if (text != null) { + text.Paste (); + } + } + + void Cut () + { + if (text != null) { + text.Cut (); + } + } + + void Copy () + { + if (text != null) { + text.Copy (); + } + } + + var menu = new MenuBar (new MenuBarItem [] { + new MenuBarItem ("_File", new MenuItem [] { + new MenuItem ("_Close", "", () => { if (Quit ()) { running = MainApp; Application.RequestStop (); } }, null, null, Key.AltMask | Key.Q), + }), + new MenuBarItem ("_Edit", new MenuItem [] { + new MenuItem ("_Copy", "", Copy, null, null, Key.C | Key.CtrlMask), + new MenuItem ("C_ut", "", Cut, null, null, Key.X | Key.CtrlMask), + new MenuItem ("_Paste", "", Paste, null, null, Key.Y | Key.CtrlMask) + }), + }); + ntop.Add (menu); + Application.Run (ntop); }