TextField.cs 8.5 KB

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