Browse Source

Added ReadOnly feature to the TextField. Fixed some bugs too.

BDisp 5 years ago
parent
commit
a29ca06fc8
1 changed files with 39 additions and 2 deletions
  1. 39 2
      Terminal.Gui/Views/TextField.cs

+ 39 - 2
Terminal.Gui/Views/TextField.cs

@@ -28,6 +28,11 @@ namespace Terminal.Gui {
 		/// </summary>
 		public bool Used { get => used; set { used = value; } }
 
+		/// <summary>
+		/// If set to true its not allow any changes in the text.
+		/// </summary>
+		public bool ReadOnly { get; set; } = false;
+
 		/// <summary>
 		///   Changed event, raised when the text has clicked.
 		/// </summary>
@@ -95,7 +100,7 @@ namespace Terminal.Gui {
 			set {
 				base.Frame = value;
 				var w = base.Frame.Width;
-				//first = point > w ? point - w : 0;
+				first = point > w ? point - w : 0;
 				Adjust ();
 			}
 		}
@@ -115,6 +120,9 @@ namespace Terminal.Gui {
 			}
 
 			set {
+				if (ReadOnly)
+					return;
+
 				var oldText = ustring.Make (text);
 				text = TextModel.ToRunes (value);
 				if (!Secret && !isFromHistory) {
@@ -189,8 +197,10 @@ namespace Terminal.Gui {
 				if (idx < p)
 					continue;
 				var cols = Rune.ColumnWidth (rune);
-				if (col == point && HasFocus && !Used && SelectedLength == 0)
+				if (col == point && HasFocus && !Used && SelectedLength == 0 && !ReadOnly)
 					Driver.SetAttribute (Colors.Menu.HotFocus);
+				else if (ReadOnly)
+					Driver.SetAttribute (idx >= start && length > 0 && idx < start + length ? color.Focus : color.Disabled);
 				else
 					Driver.SetAttribute (idx >= start && length > 0 && idx < start + length ? color.Focus : ColorScheme.Focus);
 				if (col + cols <= width)
@@ -261,6 +271,9 @@ namespace Terminal.Gui {
 			switch (kb.Key) {
 			case Key.DeleteChar:
 			case Key.ControlD:
+				if (ReadOnly)
+					return true;
+
 				if (SelectedLength == 0) {
 					if (text.Count == 0 || text.Count == point)
 						return true;
@@ -275,6 +288,9 @@ namespace Terminal.Gui {
 
 			case Key.Delete:
 			case Key.Backspace:
+				if (ReadOnly)
+					return true;
+
 				if (SelectedLength == 0) {
 					if (point == 0)
 						return true;
@@ -373,6 +389,9 @@ namespace Terminal.Gui {
 				break;
 
 			case Key.ControlK: // kill-to-end
+				if (ReadOnly)
+					return true;
+
 				ClearAllSelection ();
 				if (point >= text.Count)
 					return true;
@@ -383,6 +402,9 @@ namespace Terminal.Gui {
 
 			// Undo
 			case Key.ControlZ:
+				if (ReadOnly)
+					return true;
+
 				if (historyText != null && historyText.Count > 0) {
 					isFromHistory = true;
 					if (idxhistoryText > 0)
@@ -396,6 +418,9 @@ namespace Terminal.Gui {
 
 			//Redo
 			case Key.ControlY: // Control-y, yank
+				if (ReadOnly)
+					return true;
+
 				if (historyText != null && historyText.Count > 0) {
 					isFromHistory = true;
 					if (idxhistoryText < historyText.Count - 1) {
@@ -455,6 +480,9 @@ namespace Terminal.Gui {
 				break;
 
 			case Key.ControlX:
+				if (ReadOnly)
+					return true;
+
 				Cut ();
 				break;
 
@@ -472,6 +500,9 @@ namespace Terminal.Gui {
 				if (kb.Key < Key.Space || kb.Key > Key.CharMask)
 					return false;
 
+				if (ReadOnly)
+					return true;
+
 				if (SelectedLength != 0) {
 					DeleteSelectedText ();
 					oldCursorPos = point;
@@ -684,6 +715,9 @@ namespace Terminal.Gui {
 		/// </summary>
 		public void Copy ()
 		{
+			if (Secret)
+				return;
+
 			if (SelectedLength != 0) {
 				Clipboard.Contents = SelectedText;
 			}
@@ -717,6 +751,9 @@ namespace Terminal.Gui {
 		/// </summary>
 		public void Paste ()
 		{
+			if (ReadOnly)
+				return;
+
 			string actualText = Text.ToString ();
 			int start = SelectedStart == -1 ? CursorPosition : SelectedStart;
 			ustring cbTxt = Clipboard.Contents?.ToString () ?? "";