TextField.cs 6.2 KB

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