TextField.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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;
  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 that creates a text field, with layout controlled with X, Y, Width and Height.
  33. /// </summary>
  34. /// <param name="text">Initial text contents.</param>
  35. public TextField (ustring text)
  36. {
  37. if (text == null)
  38. text = "";
  39. this.text = text;
  40. point = text.Length;
  41. CanFocus = true;
  42. }
  43. /// <summary>
  44. /// Public constructor that creates a text field at an absolute position and size.
  45. /// </summary>
  46. /// <param name="x">The x coordinate.</param>
  47. /// <param name="y">The y coordinate.</param>
  48. /// <param name="w">The width.</param>
  49. /// <param name="text">Initial text contents.</param>
  50. public TextField (int x, int y, int w, ustring text) : base (new Rect (x, y, w, 1))
  51. {
  52. if (text == null)
  53. text = "";
  54. this.text = text;
  55. point = text.Length;
  56. first = point > w ? point - w : 0;
  57. CanFocus = true;
  58. }
  59. public override Rect Frame {
  60. get => base.Frame;
  61. set {
  62. base.Frame = value;
  63. var w = base.Frame.Width;
  64. first = point > w ? point - w : 0;
  65. }
  66. }
  67. /// <summary>
  68. /// Sets or gets the text in the entry.
  69. /// </summary>
  70. /// <remarks>
  71. /// </remarks>
  72. public ustring Text {
  73. get {
  74. return text;
  75. }
  76. set {
  77. text = value;
  78. if (point > text.Length)
  79. point = text.Length;
  80. first = point > Frame.Width ? point - Frame.Width : 0;
  81. SetNeedsDisplay ();
  82. }
  83. }
  84. /// <summary>
  85. /// Sets the secret property.
  86. /// </summary>
  87. /// <remarks>
  88. /// This makes the text entry suitable for entering passwords.
  89. /// </remarks>
  90. public bool Secret { get; set; }
  91. /// <summary>
  92. /// The current cursor position.
  93. /// </summary>
  94. public int CursorPosition { get { return point; } }
  95. /// <summary>
  96. /// Sets the cursor position.
  97. /// </summary>
  98. public override void PositionCursor ()
  99. {
  100. Move (point - first, 0);
  101. }
  102. public override void Redraw (Rect region)
  103. {
  104. Driver.SetAttribute (ColorScheme.Focus);
  105. Move (0, 0);
  106. for (int i = 0; i < Frame.Width; i++) {
  107. int p = first + i;
  108. if (p < text.Length) {
  109. Driver.AddRune (Secret ? (Rune)'*' : text [p]);
  110. } else
  111. Driver.AddRune (' ');
  112. }
  113. PositionCursor ();
  114. }
  115. void Adjust ()
  116. {
  117. if (point < first)
  118. first = point;
  119. else if (first + point >= Frame.Width)
  120. first = point - (Frame.Width / 3);
  121. SetNeedsDisplay ();
  122. }
  123. void SetText (ustring new_text)
  124. {
  125. text = new_text;
  126. if (Changed != null)
  127. Changed (this, EventArgs.Empty);
  128. }
  129. public override bool CanFocus {
  130. get => true;
  131. set { base.CanFocus = value; }
  132. }
  133. void SetClipboard (ustring text)
  134. {
  135. if (!Secret)
  136. Clipboard.Contents = text;
  137. }
  138. public override bool ProcessKey (KeyEvent kb)
  139. {
  140. switch (kb.Key) {
  141. case Key.DeleteChar:
  142. if (text.Length == 0 || text.Length == point)
  143. return true;
  144. SetText (text [0, point] + text [point + 1, null]);
  145. Adjust ();
  146. break;
  147. case Key.Delete:
  148. case Key.Backspace:
  149. if (point == 0)
  150. return true;
  151. SetText (text [0, point - 1] + text [point, null]);
  152. point--;
  153. Adjust ();
  154. break;
  155. // Home, C-A
  156. case Key.Home:
  157. case Key.ControlA:
  158. point = 0;
  159. Adjust ();
  160. break;
  161. case Key.CursorLeft:
  162. case Key.ControlB:
  163. if (point > 0) {
  164. point--;
  165. Adjust ();
  166. }
  167. break;
  168. case Key.ControlD: // Delete
  169. if (point == text.Length)
  170. break;
  171. SetText (text [0, point] + text [point + 1, null]);
  172. Adjust ();
  173. break;
  174. case Key.ControlE: // End
  175. point = text.Length;
  176. Adjust ();
  177. break;
  178. case Key.CursorRight:
  179. case Key.ControlF:
  180. if (point == text.Length)
  181. break;
  182. point++;
  183. Adjust ();
  184. break;
  185. case Key.ControlK: // kill-to-end
  186. SetClipboard (text.Substring (point));
  187. SetText (text [0, point]);
  188. Adjust ();
  189. break;
  190. case Key.ControlY: // Control-y, yank
  191. var clip = Clipboard.Contents;
  192. if (clip== null)
  193. return true;
  194. if (point == text.Length) {
  195. SetText (text + clip);
  196. point = text.Length;
  197. } else {
  198. SetText (text [0, point] + clip + text.Substring (point));
  199. point += clip.RuneCount;
  200. }
  201. Adjust ();
  202. break;
  203. case (Key)((int)'b' + Key.AltMask):
  204. int bw = WordBackward (point);
  205. if (bw != -1)
  206. point = bw;
  207. Adjust ();
  208. break;
  209. case (Key)((int)'f' + Key.AltMask):
  210. int fw = WordForward (point);
  211. if (fw != -1)
  212. point = fw;
  213. Adjust ();
  214. break;
  215. // MISSING:
  216. // Alt-D, Alt-backspace
  217. // Alt-Y
  218. // Delete adding to kill buffer
  219. default:
  220. // Ignore other control characters.
  221. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  222. return false;
  223. var kbstr = ustring.Make ((uint)kb.Key);
  224. if (used) {
  225. if (point == text.Length) {
  226. SetText (text + kbstr);
  227. } else {
  228. SetText (text [0, point] + kbstr + text [point, null]);
  229. }
  230. point++;
  231. } else {
  232. SetText (kbstr);
  233. first = 0;
  234. point = 1;
  235. }
  236. used = true;
  237. Adjust ();
  238. return true;
  239. }
  240. used = true;
  241. return true;
  242. }
  243. int WordForward (int p)
  244. {
  245. if (p >= text.Length)
  246. return -1;
  247. int i = p;
  248. if (Rune.IsPunctuation (text [p]) || Rune.IsWhiteSpace(text [p])) {
  249. for (; i < text.Length; i++) {
  250. var r = text [i];
  251. if (Rune.IsLetterOrDigit(r))
  252. break;
  253. }
  254. for (; i < text.Length; i++) {
  255. var r = text [i];
  256. if (!Rune.IsLetterOrDigit (r))
  257. break;
  258. }
  259. } else {
  260. for (; i < text.Length; i++) {
  261. var r = text [i];
  262. if (!Rune.IsLetterOrDigit (r))
  263. break;
  264. }
  265. }
  266. if (i != p)
  267. return i;
  268. return -1;
  269. }
  270. int WordBackward (int p)
  271. {
  272. if (p == 0)
  273. return -1;
  274. int i = p - 1;
  275. if (i == 0)
  276. return 0;
  277. var ti = text [i];
  278. if (Rune.IsPunctuation (ti) || Rune.IsSymbol(ti) || Rune.IsWhiteSpace(ti)) {
  279. for (; i >= 0; i--) {
  280. if (Rune.IsLetterOrDigit (text [i]))
  281. break;
  282. }
  283. for (; i >= 0; i--) {
  284. if (!Rune.IsLetterOrDigit (text [i]))
  285. break;
  286. }
  287. } else {
  288. for (; i >= 0; i--) {
  289. if (!Rune.IsLetterOrDigit (text [i]))
  290. break;
  291. }
  292. }
  293. i++;
  294. if (i != p)
  295. return i;
  296. return -1;
  297. }
  298. public override bool MouseEvent (MouseEvent ev)
  299. {
  300. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
  301. return false;
  302. if (!HasFocus)
  303. SuperView.SetFocus (this);
  304. // We could also set the cursor position.
  305. point = first + ev.X;
  306. if (point > text.Length)
  307. point = text.Length;
  308. if (point < first)
  309. point = 0;
  310. SetNeedsDisplay ();
  311. return true;
  312. }
  313. }
  314. }