TextField.cs 8.6 KB

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