TextField.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. var x = first;
  101. var col = 0;
  102. foreach ((var idx, var rune) in text [first, null].Range ()) {
  103. if (x == point)
  104. break;
  105. var cols = Rune.ColumnWidth (rune);
  106. col += cols;
  107. x++;
  108. }
  109. Move (col, 0);
  110. }
  111. public override void Redraw (Rect region)
  112. {
  113. Driver.SetAttribute (ColorScheme.Focus);
  114. Move (0, 0);
  115. int p = first;
  116. int col = 0;
  117. int width = Frame.Width;
  118. foreach ((var idx, var rune) in text.Range ()) {
  119. if (idx < first)
  120. continue;
  121. var cols = Rune.ColumnWidth (rune);
  122. if (col + cols < width)
  123. Driver.AddRune ((Rune)(Secret ? '*' : rune));
  124. col += cols;
  125. }
  126. for (int i = col; i < Frame.Width; i++)
  127. Driver.AddRune (' ');
  128. PositionCursor ();
  129. }
  130. void Adjust ()
  131. {
  132. if (point < first)
  133. first = point;
  134. else if (first + point >= Frame.Width)
  135. first = point - (Frame.Width / 3);
  136. SetNeedsDisplay ();
  137. }
  138. void SetText (ustring new_text)
  139. {
  140. text = new_text;
  141. if (Changed != null)
  142. Changed (this, EventArgs.Empty);
  143. }
  144. public override bool CanFocus {
  145. get => true;
  146. set { base.CanFocus = value; }
  147. }
  148. void SetClipboard (ustring text)
  149. {
  150. if (!Secret)
  151. Clipboard.Contents = text;
  152. }
  153. // Maps a rune index to the byte index inside the utf8 string
  154. int RuneIndexToByteIndex (int index)
  155. {
  156. var blen = text.Length;
  157. for (int byteIndex = 0, runeIndex = 0; byteIndex < blen; runeIndex++) {
  158. if (index == runeIndex)
  159. return byteIndex;
  160. (var rune, var size) = Utf8.DecodeRune (text, byteIndex, byteIndex - blen);
  161. byteIndex += size;
  162. }
  163. throw new InvalidOperationException ();
  164. }
  165. public override bool ProcessKey (KeyEvent kb)
  166. {
  167. switch (kb.Key) {
  168. case Key.DeleteChar:
  169. if (text.Length == 0 || text.Length == point)
  170. return true;
  171. SetText (text [0, RuneIndexToByteIndex (point)] + text [RuneIndexToByteIndex (point + 1), null]);
  172. Adjust ();
  173. break;
  174. case Key.Delete:
  175. case Key.Backspace:
  176. if (point == 0)
  177. return true;
  178. SetText (text [0, RuneIndexToByteIndex (point - 1)] + text [RuneIndexToByteIndex (point), null]);
  179. point--;
  180. Adjust ();
  181. break;
  182. // Home, C-A
  183. case Key.Home:
  184. case Key.ControlA:
  185. point = 0;
  186. Adjust ();
  187. break;
  188. case Key.CursorLeft:
  189. case Key.ControlB:
  190. if (point > 0) {
  191. point--;
  192. Adjust ();
  193. }
  194. break;
  195. case Key.ControlD: // Delete
  196. if (point == text.Length)
  197. break;
  198. SetText (text [0, RuneIndexToByteIndex (point)] + text [RuneIndexToByteIndex (point + 1), null]);
  199. Adjust ();
  200. break;
  201. case Key.End:
  202. case Key.ControlE: // End
  203. point = text.RuneCount;
  204. Adjust ();
  205. break;
  206. case Key.CursorRight:
  207. case Key.ControlF:
  208. if (point == text.RuneCount)
  209. break;
  210. point++;
  211. Adjust ();
  212. break;
  213. case Key.ControlK: // kill-to-end
  214. SetClipboard (text.Substring (RuneIndexToByteIndex (point)));
  215. SetText (text [0, RuneIndexToByteIndex (point)]);
  216. Adjust ();
  217. break;
  218. case Key.ControlY: // Control-y, yank
  219. var clip = Clipboard.Contents;
  220. if (clip== null)
  221. return true;
  222. if (point == text.RuneCount) {
  223. SetText (text + clip);
  224. point = text.RuneCount;
  225. } else {
  226. SetText (text [0, RuneIndexToByteIndex (point)] + clip + text.Substring (RuneIndexToByteIndex (point)));
  227. point += clip.RuneCount;
  228. }
  229. Adjust ();
  230. break;
  231. case (Key)((int)'b' + Key.AltMask):
  232. int bw = WordBackward (point);
  233. if (bw != -1)
  234. point = bw;
  235. Adjust ();
  236. break;
  237. case (Key)((int)'f' + Key.AltMask):
  238. int fw = WordForward (point);
  239. if (fw != -1)
  240. point = fw;
  241. Adjust ();
  242. break;
  243. // MISSING:
  244. // Alt-D, Alt-backspace
  245. // Alt-Y
  246. // Delete adding to kill buffer
  247. default:
  248. // Ignore other control characters.
  249. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  250. return false;
  251. var kbstr = ustring.Make ((uint)kb.Key);
  252. if (used) {
  253. if (point == text.RuneCount) {
  254. SetText (text + kbstr);
  255. } else {
  256. SetText (text [0, RuneIndexToByteIndex (point)] + kbstr + text [RuneIndexToByteIndex (point), null]);
  257. }
  258. point++;
  259. } else {
  260. SetText (kbstr);
  261. first = 0;
  262. point = 1;
  263. }
  264. used = true;
  265. Adjust ();
  266. return true;
  267. }
  268. used = true;
  269. return true;
  270. }
  271. int WordForward (int p)
  272. {
  273. if (p >= text.Length)
  274. return -1;
  275. int i = p;
  276. if (Rune.IsPunctuation (text [p]) || Rune.IsWhiteSpace(text [p])) {
  277. for (; i < text.Length; i++) {
  278. var r = text [i];
  279. if (Rune.IsLetterOrDigit(r))
  280. break;
  281. }
  282. for (; i < text.Length; i++) {
  283. var r = text [i];
  284. if (!Rune.IsLetterOrDigit (r))
  285. break;
  286. }
  287. } else {
  288. for (; i < text.Length; i++) {
  289. var r = text [i];
  290. if (!Rune.IsLetterOrDigit (r))
  291. break;
  292. }
  293. }
  294. if (i != p)
  295. return i;
  296. return -1;
  297. }
  298. int WordBackward (int p)
  299. {
  300. if (p == 0)
  301. return -1;
  302. int i = p - 1;
  303. if (i == 0)
  304. return 0;
  305. var ti = text [i];
  306. if (Rune.IsPunctuation (ti) || Rune.IsSymbol(ti) || Rune.IsWhiteSpace(ti)) {
  307. for (; i >= 0; i--) {
  308. if (Rune.IsLetterOrDigit (text [i]))
  309. break;
  310. }
  311. for (; i >= 0; i--) {
  312. if (!Rune.IsLetterOrDigit (text [i]))
  313. break;
  314. }
  315. } else {
  316. for (; i >= 0; i--) {
  317. if (!Rune.IsLetterOrDigit (text [i]))
  318. break;
  319. }
  320. }
  321. i++;
  322. if (i != p)
  323. return i;
  324. return -1;
  325. }
  326. public override bool MouseEvent (MouseEvent ev)
  327. {
  328. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
  329. return false;
  330. if (!HasFocus)
  331. SuperView.SetFocus (this);
  332. // We could also set the cursor position.
  333. point = first + ev.X;
  334. if (point > text.Length)
  335. point = text.Length;
  336. if (point < first)
  337. point = 0;
  338. SetNeedsDisplay ();
  339. return true;
  340. }
  341. }
  342. }