TextField.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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.Delete:
  142. case Key.Backspace:
  143. if (point == 0)
  144. return true;
  145. SetText (text [0, point - 1] + text [point, null]);
  146. point--;
  147. Adjust ();
  148. break;
  149. // Home, C-A
  150. case Key.Home:
  151. case Key.ControlA:
  152. point = 0;
  153. Adjust ();
  154. break;
  155. case Key.CursorLeft:
  156. case Key.ControlB:
  157. if (point > 0) {
  158. point--;
  159. Adjust ();
  160. }
  161. break;
  162. case Key.ControlD: // Delete
  163. if (point == text.Length)
  164. break;
  165. SetText (text [0, point] + text [point + 1, null]);
  166. Adjust ();
  167. break;
  168. case Key.ControlE: // End
  169. point = text.Length;
  170. Adjust ();
  171. break;
  172. case Key.CursorRight:
  173. case Key.ControlF:
  174. if (point == text.Length)
  175. break;
  176. point++;
  177. Adjust ();
  178. break;
  179. case Key.ControlK: // kill-to-end
  180. SetClipboard (text.Substring (point));
  181. SetText (text [0, point]);
  182. Adjust ();
  183. break;
  184. case Key.ControlY: // Control-y, yank
  185. var clip = Clipboard.Contents;
  186. if (clip== null)
  187. return true;
  188. if (point == text.Length) {
  189. SetText (text + clip);
  190. point = text.Length;
  191. } else {
  192. SetText (text [0, point] + clip + text.Substring (point));
  193. point += clip.RuneCount;
  194. }
  195. Adjust ();
  196. break;
  197. case (Key)((int)'b' + Key.AltMask):
  198. int bw = WordBackward (point);
  199. if (bw != -1)
  200. point = bw;
  201. Adjust ();
  202. break;
  203. case (Key)((int)'f' + Key.AltMask):
  204. int fw = WordForward (point);
  205. if (fw != -1)
  206. point = fw;
  207. Adjust ();
  208. break;
  209. // MISSING:
  210. // Alt-D, Alt-backspace
  211. // Alt-Y
  212. // Delete adding to kill buffer
  213. default:
  214. // Ignore other control characters.
  215. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  216. return false;
  217. var kbstr = ustring.Make ((uint)kb.Key);
  218. if (used) {
  219. if (point == text.Length) {
  220. SetText (text + kbstr);
  221. } else {
  222. SetText (text [0, point] + kbstr + text [point, null]);
  223. }
  224. point++;
  225. } else {
  226. SetText (kbstr);
  227. first = 0;
  228. point = 1;
  229. }
  230. used = true;
  231. Adjust ();
  232. return true;
  233. }
  234. used = true;
  235. return true;
  236. }
  237. int WordForward (int p)
  238. {
  239. if (p >= text.Length)
  240. return -1;
  241. int i = p;
  242. if (Rune.IsPunctuation (text [p]) || Rune.IsWhiteSpace(text [p])) {
  243. for (; i < text.Length; i++) {
  244. var r = text [i];
  245. if (Rune.IsLetterOrDigit(r))
  246. break;
  247. }
  248. for (; i < text.Length; i++) {
  249. var r = text [i];
  250. if (!Rune.IsLetterOrDigit (r))
  251. break;
  252. }
  253. } else {
  254. for (; i < text.Length; i++) {
  255. var r = text [i];
  256. if (!Rune.IsLetterOrDigit (r))
  257. break;
  258. }
  259. }
  260. if (i != p)
  261. return i;
  262. return -1;
  263. }
  264. int WordBackward (int p)
  265. {
  266. if (p == 0)
  267. return -1;
  268. int i = p - 1;
  269. if (i == 0)
  270. return 0;
  271. var ti = text [i];
  272. if (Rune.IsPunctuation (ti) || Rune.IsSymbol(ti) || Rune.IsWhiteSpace(ti)) {
  273. for (; i >= 0; i--) {
  274. if (Rune.IsLetterOrDigit (text [i]))
  275. break;
  276. }
  277. for (; i >= 0; i--) {
  278. if (!Rune.IsLetterOrDigit (text [i]))
  279. break;
  280. }
  281. } else {
  282. for (; i >= 0; i--) {
  283. if (!Rune.IsLetterOrDigit (text [i]))
  284. break;
  285. }
  286. }
  287. i++;
  288. if (i != p)
  289. return i;
  290. return -1;
  291. }
  292. public override bool MouseEvent (MouseEvent ev)
  293. {
  294. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
  295. return false;
  296. if (!HasFocus)
  297. SuperView.SetFocus (this);
  298. // We could also set the cursor position.
  299. point = first + ev.X;
  300. if (point > text.Length)
  301. point = text.Length;
  302. if (point < first)
  303. point = 0;
  304. SetNeedsDisplay ();
  305. return true;
  306. }
  307. }
  308. }