浏览代码

Merge pull request #488 from BDisp/textfield-position-cursor

Textfield position cursor
Charlie Kindel 5 年之前
父节点
当前提交
1fd7ac0819
共有 1 个文件被更改,包括 44 次插入6 次删除
  1. 44 6
      Terminal.Gui/Views/TextField.cs

+ 44 - 6
Terminal.Gui/Views/TextField.cs

@@ -28,6 +28,11 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// </summary>
 		public bool Used { get => used; set { used = value; } }
 		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>
 		/// <summary>
 		///   Changed event, raised when the text has clicked.
 		///   Changed event, raised when the text has clicked.
 		/// </summary>
 		/// </summary>
@@ -95,7 +100,7 @@ namespace Terminal.Gui {
 			set {
 			set {
 				base.Frame = value;
 				base.Frame = value;
 				var w = base.Frame.Width;
 				var w = base.Frame.Width;
-				//first = point > w ? point - w : 0;
+				first = point > w ? point - w : 0;
 				Adjust ();
 				Adjust ();
 			}
 			}
 		}
 		}
@@ -115,6 +120,9 @@ namespace Terminal.Gui {
 			}
 			}
 
 
 			set {
 			set {
+				if (ReadOnly)
+					return;
+
 				var oldText = ustring.Make (text);
 				var oldText = ustring.Make (text);
 				text = TextModel.ToRunes (value);
 				text = TextModel.ToRunes (value);
 				if (!Secret && !isFromHistory) {
 				if (!Secret && !isFromHistory) {
@@ -184,13 +192,16 @@ namespace Terminal.Gui {
 			int col = 0;
 			int col = 0;
 			int width = Frame.Width;
 			int width = Frame.Width;
 			var tcount = text.Count;
 			var tcount = text.Count;
+			var roc = new Attribute (Color.DarkGray, Color.Gray);
 			for (int idx = 0; idx < tcount; idx++){
 			for (int idx = 0; idx < tcount; idx++){
 				var rune = text [idx];
 				var rune = text [idx];
 				if (idx < p)
 				if (idx < p)
 					continue;
 					continue;
 				var cols = Rune.ColumnWidth (rune);
 				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);
 					Driver.SetAttribute (Colors.Menu.HotFocus);
+				else if (ReadOnly)
+					Driver.SetAttribute (idx >= start && length > 0 && idx < start + length ? color.Focus : roc);
 				else
 				else
 					Driver.SetAttribute (idx >= start && length > 0 && idx < start + length ? color.Focus : ColorScheme.Focus);
 					Driver.SetAttribute (idx >= start && length > 0 && idx < start + length ? color.Focus : ColorScheme.Focus);
 				if (col + cols <= width)
 				if (col + cols <= width)
@@ -261,6 +272,9 @@ namespace Terminal.Gui {
 			switch (kb.Key) {
 			switch (kb.Key) {
 			case Key.DeleteChar:
 			case Key.DeleteChar:
 			case Key.ControlD:
 			case Key.ControlD:
+				if (ReadOnly)
+					return true;
+
 				if (SelectedLength == 0) {
 				if (SelectedLength == 0) {
 					if (text.Count == 0 || text.Count == point)
 					if (text.Count == 0 || text.Count == point)
 						return true;
 						return true;
@@ -275,6 +289,9 @@ namespace Terminal.Gui {
 
 
 			case Key.Delete:
 			case Key.Delete:
 			case Key.Backspace:
 			case Key.Backspace:
+				if (ReadOnly)
+					return true;
+
 				if (SelectedLength == 0) {
 				if (SelectedLength == 0) {
 					if (point == 0)
 					if (point == 0)
 						return true;
 						return true;
@@ -373,6 +390,9 @@ namespace Terminal.Gui {
 				break;
 				break;
 
 
 			case Key.ControlK: // kill-to-end
 			case Key.ControlK: // kill-to-end
+				if (ReadOnly)
+					return true;
+
 				ClearAllSelection ();
 				ClearAllSelection ();
 				if (point >= text.Count)
 				if (point >= text.Count)
 					return true;
 					return true;
@@ -383,6 +403,9 @@ namespace Terminal.Gui {
 
 
 			// Undo
 			// Undo
 			case Key.ControlZ:
 			case Key.ControlZ:
+				if (ReadOnly)
+					return true;
+
 				if (historyText != null && historyText.Count > 0) {
 				if (historyText != null && historyText.Count > 0) {
 					isFromHistory = true;
 					isFromHistory = true;
 					if (idxhistoryText > 0)
 					if (idxhistoryText > 0)
@@ -396,6 +419,9 @@ namespace Terminal.Gui {
 
 
 			//Redo
 			//Redo
 			case Key.ControlY: // Control-y, yank
 			case Key.ControlY: // Control-y, yank
+				if (ReadOnly)
+					return true;
+
 				if (historyText != null && historyText.Count > 0) {
 				if (historyText != null && historyText.Count > 0) {
 					isFromHistory = true;
 					isFromHistory = true;
 					if (idxhistoryText < historyText.Count - 1) {
 					if (idxhistoryText < historyText.Count - 1) {
@@ -455,6 +481,9 @@ namespace Terminal.Gui {
 				break;
 				break;
 
 
 			case Key.ControlX:
 			case Key.ControlX:
+				if (ReadOnly)
+					return true;
+
 				Cut ();
 				Cut ();
 				break;
 				break;
 
 
@@ -472,6 +501,9 @@ namespace Terminal.Gui {
 				if (kb.Key < Key.Space || kb.Key > Key.CharMask)
 				if (kb.Key < Key.Space || kb.Key > Key.CharMask)
 					return false;
 					return false;
 
 
+				if (ReadOnly)
+					return true;
+
 				if (SelectedLength != 0) {
 				if (SelectedLength != 0) {
 					DeleteSelectedText ();
 					DeleteSelectedText ();
 					oldCursorPos = point;
 					oldCursorPos = point;
@@ -639,7 +671,7 @@ namespace Terminal.Gui {
 				point = text.Count;
 				point = text.Count;
 			if (point < first)
 			if (point < first)
 				point = 0;
 				point = 0;
-			return x;
+			return point;
 		}
 		}
 
 
 		void PrepareSelection (int x, int direction = 0)
 		void PrepareSelection (int x, int direction = 0)
@@ -682,8 +714,11 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// <summary>
 		/// Copy the selected text to the clipboard.
 		/// Copy the selected text to the clipboard.
 		/// </summary>
 		/// </summary>
-		public void Copy ()
+		public virtual void Copy ()
 		{
 		{
+			if (Secret)
+				return;
+
 			if (SelectedLength != 0) {
 			if (SelectedLength != 0) {
 				Clipboard.Contents = SelectedText;
 				Clipboard.Contents = SelectedText;
 			}
 			}
@@ -692,7 +727,7 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// <summary>
 		/// Cut the selected text to the clipboard.
 		/// Cut the selected text to the clipboard.
 		/// </summary>
 		/// </summary>
-		public void Cut ()
+		public virtual void Cut ()
 		{
 		{
 			if (SelectedLength != 0) {
 			if (SelectedLength != 0) {
 				Clipboard.Contents = SelectedText;
 				Clipboard.Contents = SelectedText;
@@ -715,8 +750,11 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// <summary>
 		/// Paste the selected text from the clipboard.
 		/// Paste the selected text from the clipboard.
 		/// </summary>
 		/// </summary>
-		public void Paste ()
+		public virtual void Paste ()
 		{
 		{
+			if (ReadOnly)
+				return;
+
 			string actualText = Text.ToString ();
 			string actualText = Text.ToString ();
 			int start = SelectedStart == -1 ? CursorPosition : SelectedStart;
 			int start = SelectedStart == -1 ? CursorPosition : SelectedStart;
 			ustring cbTxt = Clipboard.Contents?.ToString () ?? "";
 			ustring cbTxt = Clipboard.Contents?.ToString () ?? "";