TextField.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // TextField.cs: single-line text editor with Emacs keybindings
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using NStack;
  11. namespace Terminal.Gui {
  12. /// <summary>
  13. /// Text data entry widget
  14. /// </summary>
  15. /// <remarks>
  16. /// The Entry widget provides Emacs-like editing
  17. /// functionality, and mouse support.
  18. /// </remarks>
  19. public class TextField : View {
  20. ustring text, kill;
  21. int first, point;
  22. bool used;
  23. /// <summary>
  24. /// Changed event, raised when the text has clicked.
  25. /// </summary>
  26. /// <remarks>
  27. /// Client code can hook up to this event, it is
  28. /// raised when the text in the entry changes.
  29. /// </remarks>
  30. public event EventHandler Changed;
  31. /// <summary>
  32. /// Public constructor.
  33. /// </summary>
  34. /// <remarks>
  35. /// </remarks>
  36. public TextField (int x, int y, int w, string s) : base (new Rect (x, y, w, 1))
  37. {
  38. if (s == null)
  39. s = "";
  40. text = s;
  41. point = s.Length;
  42. first = point > w ? point - w : 0;
  43. CanFocus = true;
  44. }
  45. /// <summary>
  46. /// Sets or gets the text in the entry.
  47. /// </summary>
  48. /// <remarks>
  49. /// </remarks>
  50. public ustring Text {
  51. get {
  52. return text;
  53. }
  54. set {
  55. text = value;
  56. if (point > text.Length)
  57. point = text.Length;
  58. first = point > Frame.Width ? point - Frame.Width : 0;
  59. SetNeedsDisplay ();
  60. }
  61. }
  62. /// <summary>
  63. /// Sets the secret property.
  64. /// </summary>
  65. /// <remarks>
  66. /// This makes the text entry suitable for entering passwords.
  67. /// </remarks>
  68. public bool Secret { get; set; }
  69. /// <summary>
  70. /// The current cursor position.
  71. /// </summary>
  72. public int CursorPosition { get { return point; } }
  73. /// <summary>
  74. /// Sets the cursor position.
  75. /// </summary>
  76. public override void PositionCursor ()
  77. {
  78. Move (point - first, 0);
  79. }
  80. public override void Redraw (Rect region)
  81. {
  82. Driver.SetAttribute (ColorScheme.Focus);
  83. Move (0, 0);
  84. for (int i = 0; i < Frame.Width; i++) {
  85. int p = first + i;
  86. if (p < text.Length) {
  87. Driver.AddRune (Secret ? (Rune)'*' : text [p]);
  88. } else
  89. Driver.AddRune (' ');
  90. }
  91. PositionCursor ();
  92. }
  93. void Adjust ()
  94. {
  95. if (point < first)
  96. first = point;
  97. else if (first + point >= Frame.Width)
  98. first = point - (Frame.Width / 3);
  99. SetNeedsDisplay ();
  100. }
  101. void SetText (ustring new_text)
  102. {
  103. text = new_text;
  104. if (Changed != null)
  105. Changed (this, EventArgs.Empty);
  106. }
  107. public override bool CanFocus {
  108. get => true;
  109. set { base.CanFocus = value; }
  110. }
  111. public override bool ProcessKey (KeyEvent kb)
  112. {
  113. switch (kb.Key) {
  114. case Key.Delete:
  115. case Key.Backspace:
  116. if (point == 0)
  117. return true;
  118. SetText (text [0, point - 1] + text [point, null]);
  119. point--;
  120. Adjust ();
  121. break;
  122. // Home, C-A
  123. case Key.Home:
  124. case Key.ControlA:
  125. point = 0;
  126. Adjust ();
  127. break;
  128. case Key.CursorLeft:
  129. case Key.ControlB:
  130. if (point > 0) {
  131. point--;
  132. Adjust ();
  133. }
  134. break;
  135. case Key.ControlD: // Delete
  136. if (point == text.Length)
  137. break;
  138. SetText (text [0, point] + text [point + 1, null]);
  139. Adjust ();
  140. break;
  141. case Key.ControlE: // End
  142. point = text.Length;
  143. Adjust ();
  144. break;
  145. case Key.CursorRight:
  146. case Key.ControlF:
  147. if (point == text.Length)
  148. break;
  149. point++;
  150. Adjust ();
  151. break;
  152. case Key.ControlK: // kill-to-end
  153. kill = text.Substring (point);
  154. SetText (text [0, point]);
  155. Adjust ();
  156. break;
  157. case Key.ControlY: // Control-y, yank
  158. if (kill == null)
  159. return true;
  160. if (point == text.Length) {
  161. SetText (text + kill);
  162. point = text.Length;
  163. } else {
  164. SetText (text [0, point] + kill + text.Substring (point));
  165. point += kill.Length;
  166. }
  167. Adjust ();
  168. break;
  169. case (Key)((int)'b' + Key.AltMask):
  170. int bw = WordBackward (point);
  171. if (bw != -1)
  172. point = bw;
  173. Adjust ();
  174. break;
  175. case (Key)((int)'f' + Key.AltMask):
  176. int fw = WordForward (point);
  177. if (fw != -1)
  178. point = fw;
  179. Adjust ();
  180. break;
  181. default:
  182. // Ignore other control characters.
  183. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  184. return false;
  185. var kbstr = ustring.Make ((uint)kb.Key);
  186. if (used) {
  187. if (point == text.Length) {
  188. SetText (text + kbstr);
  189. } else {
  190. SetText (text [0, point] + kbstr + text [point, null]);
  191. }
  192. point++;
  193. } else {
  194. SetText (kbstr);
  195. first = 0;
  196. point = 1;
  197. }
  198. used = true;
  199. Adjust ();
  200. return true;
  201. }
  202. used = true;
  203. return true;
  204. }
  205. int WordForward (int p)
  206. {
  207. if (p >= text.Length)
  208. return -1;
  209. int i = p;
  210. if (Rune.IsPunctuation (text [p]) || Rune.IsWhiteSpace(text [p])) {
  211. for (; i < text.Length; i++) {
  212. var r = text [i];
  213. if (Rune.IsLetterOrDigit(r))
  214. break;
  215. }
  216. for (; i < text.Length; i++) {
  217. var r = text [i];
  218. if (!Rune.IsLetterOrDigit (r))
  219. break;
  220. }
  221. } else {
  222. for (; i < text.Length; i++) {
  223. var r = text [i];
  224. if (!Rune.IsLetterOrDigit (r))
  225. break;
  226. }
  227. }
  228. if (i != p)
  229. return i;
  230. return -1;
  231. }
  232. int WordBackward (int p)
  233. {
  234. if (p == 0)
  235. return -1;
  236. int i = p - 1;
  237. if (i == 0)
  238. return 0;
  239. var ti = text [i];
  240. if (Rune.IsPunctuation (ti) || Rune.IsSymbol(ti) || Rune.IsWhiteSpace(ti)) {
  241. for (; i >= 0; i--) {
  242. if (Rune.IsLetterOrDigit (text [i]))
  243. break;
  244. }
  245. for (; i >= 0; i--) {
  246. if (!Rune.IsLetterOrDigit (text [i]))
  247. break;
  248. }
  249. } else {
  250. for (; i >= 0; i--) {
  251. if (!Rune.IsLetterOrDigit (text [i]))
  252. break;
  253. }
  254. }
  255. i++;
  256. if (i != p)
  257. return i;
  258. return -1;
  259. }
  260. public override bool MouseEvent (MouseEvent ev)
  261. {
  262. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
  263. return false;
  264. if (!HasFocus)
  265. SuperView.SetFocus (this);
  266. // We could also set the cursor position.
  267. point = first + ev.X;
  268. if (point > text.Length)
  269. point = text.Length;
  270. if (point < first)
  271. point = 0;
  272. SetNeedsDisplay ();
  273. return true;
  274. }
  275. }
  276. }