TextField.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Terminal {
  5. /// <summary>
  6. /// Text data entry widget
  7. /// </summary>
  8. /// <remarks>
  9. /// The Entry widget provides Emacs-like editing
  10. /// functionality, and mouse support.
  11. /// </remarks>
  12. public class TextField : View {
  13. string text, kill;
  14. int first, point;
  15. bool used;
  16. /// <summary>
  17. /// Changed event, raised when the text has clicked.
  18. /// </summary>
  19. /// <remarks>
  20. /// Client code can hook up to this event, it is
  21. /// raised when the text in the entry changes.
  22. /// </remarks>
  23. public event EventHandler Changed;
  24. /// <summary>
  25. /// Public constructor.
  26. /// </summary>
  27. /// <remarks>
  28. /// </remarks>
  29. public TextField (int x, int y, int w, string s) : base (new Rect (x, y, w, 1))
  30. {
  31. if (s == null)
  32. s = "";
  33. text = s;
  34. point = s.Length;
  35. first = point > w ? point - w : 0;
  36. CanFocus = true;
  37. Color = Colors.Dialog.Focus;
  38. }
  39. /// <summary>
  40. /// Sets or gets the text in the entry.
  41. /// </summary>
  42. /// <remarks>
  43. /// </remarks>
  44. public string Text {
  45. get {
  46. return text;
  47. }
  48. set {
  49. text = value;
  50. if (point > text.Length)
  51. point = text.Length;
  52. first = point > Frame.Width ? point - Frame.Width : 0;
  53. SetNeedsDisplay ();
  54. }
  55. }
  56. /// <summary>
  57. /// Sets the secret property.
  58. /// </summary>
  59. /// <remarks>
  60. /// This makes the text entry suitable for entering passwords.
  61. /// </remarks>
  62. public bool Secret { get; set; }
  63. Attribute color;
  64. /// <summary>
  65. /// Sets the color attribute to use (includes foreground and background).
  66. /// </summary>
  67. /// <value>The color.</value>
  68. public Attribute Color {
  69. get => color;
  70. set {
  71. color = value;
  72. SetNeedsDisplay ();
  73. }
  74. }
  75. /// <summary>
  76. /// The current cursor position.
  77. /// </summary>
  78. public int CursorPosition { get { return point; } }
  79. /// <summary>
  80. /// Sets the cursor position.
  81. /// </summary>
  82. public override void PositionCursor ()
  83. {
  84. Move (point - first, 0);
  85. }
  86. public override void Redraw (Rect region)
  87. {
  88. Driver.SetAttribute (Color);
  89. Move (0, 0);
  90. for (int i = 0; i < Frame.Width; i++) {
  91. int p = first + i;
  92. if (p < text.Length) {
  93. Driver.AddCh (Secret ? '*' : text [p]);
  94. } else
  95. Driver.AddCh ('_');
  96. }
  97. PositionCursor ();
  98. }
  99. void Adjust ()
  100. {
  101. if (point < first)
  102. first = point;
  103. else if (first + point >= Frame.Width)
  104. first = point - (Frame.Width / 3);
  105. SetNeedsDisplay ();
  106. }
  107. void SetText (string new_text)
  108. {
  109. text = new_text;
  110. if (Changed != null)
  111. Changed (this, EventArgs.Empty);
  112. }
  113. public override bool CanFocus {
  114. get => true;
  115. set { base.CanFocus = value; }
  116. }
  117. public override bool ProcessKey (KeyEvent kb)
  118. {
  119. switch (kb.Key) {
  120. case Key.Delete:
  121. case Key.Backspace:
  122. if (point == 0)
  123. return true;
  124. SetText (text.Substring (0, point - 1) + text.Substring (point));
  125. point--;
  126. Adjust ();
  127. break;
  128. // Home, C-A
  129. case Key.Home:
  130. case Key.ControlA:
  131. point = 0;
  132. Adjust ();
  133. break;
  134. case Key.CursorLeft:
  135. case Key.ControlB:
  136. if (point > 0) {
  137. point--;
  138. Adjust ();
  139. }
  140. break;
  141. case Key.ControlD: // Delete
  142. if (point == text.Length)
  143. break;
  144. SetText (text.Substring (0, point) + text.Substring (point + 1));
  145. Adjust ();
  146. break;
  147. case Key.ControlE: // End
  148. point = text.Length;
  149. Adjust ();
  150. break;
  151. case Key.CursorRight:
  152. case Key.ControlF:
  153. if (point == text.Length)
  154. break;
  155. point++;
  156. Adjust ();
  157. break;
  158. case Key.ControlK: // kill-to-end
  159. kill = text.Substring (point);
  160. SetText (text.Substring (0, point));
  161. Adjust ();
  162. break;
  163. case Key.ControlY: // Control-y, yank
  164. if (kill == null)
  165. return true;
  166. if (point == text.Length) {
  167. SetText (text + kill);
  168. point = text.Length;
  169. } else {
  170. SetText (text.Substring (0, point) + kill + text.Substring (point));
  171. point += kill.Length;
  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. default:
  188. // Ignore other control characters.
  189. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  190. return false;
  191. if (used) {
  192. if (point == text.Length) {
  193. SetText (text + (char)kb.Key);
  194. } else {
  195. SetText (text.Substring (0, point) + (char)kb.Key + text.Substring (point));
  196. }
  197. point++;
  198. } else {
  199. SetText ("" + (char)kb.Key);
  200. first = 0;
  201. point = 1;
  202. }
  203. used = true;
  204. Adjust ();
  205. return true;
  206. }
  207. used = true;
  208. return true;
  209. }
  210. int WordForward (int p)
  211. {
  212. if (p >= text.Length)
  213. return -1;
  214. int i = p;
  215. if (Char.IsPunctuation (text [p]) || Char.IsWhiteSpace (text [p])) {
  216. for (; i < text.Length; i++) {
  217. if (Char.IsLetterOrDigit (text [i]))
  218. break;
  219. }
  220. for (; i < text.Length; i++) {
  221. if (!Char.IsLetterOrDigit (text [i]))
  222. break;
  223. }
  224. } else {
  225. for (; i < text.Length; i++) {
  226. if (!Char.IsLetterOrDigit (text [i]))
  227. break;
  228. }
  229. }
  230. if (i != p)
  231. return i;
  232. return -1;
  233. }
  234. int WordBackward (int p)
  235. {
  236. if (p == 0)
  237. return -1;
  238. int i = p - 1;
  239. if (i == 0)
  240. return 0;
  241. if (Char.IsPunctuation (text [i]) || Char.IsSymbol (text [i]) || Char.IsWhiteSpace (text [i])) {
  242. for (; i >= 0; i--) {
  243. if (Char.IsLetterOrDigit (text [i]))
  244. break;
  245. }
  246. for (; i >= 0; i--) {
  247. if (!Char.IsLetterOrDigit (text [i]))
  248. break;
  249. }
  250. } else {
  251. for (; i >= 0; i--) {
  252. if (!Char.IsLetterOrDigit (text [i]))
  253. break;
  254. }
  255. }
  256. i++;
  257. if (i != p)
  258. return i;
  259. return -1;
  260. }
  261. #if false
  262. public override void ProcessMouse (Curses.MouseEvent ev)
  263. {
  264. if ((ev.ButtonState & Curses.Event.Button1Clicked) == 0)
  265. return;
  266. .SetFocus (this);
  267. // We could also set the cursor position.
  268. point = first + (ev.X - x);
  269. if (point > text.Length)
  270. point = text.Length;
  271. if (point < first)
  272. point = 0;
  273. SetNeedsDisplay ();
  274. }
  275. #endif
  276. }
  277. }