TextField.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. Redraw (Bounds);
  106. Driver.Refresh ();
  107. }
  108. void SetText (string new_text)
  109. {
  110. text = new_text;
  111. if (Changed != null)
  112. Changed (this, EventArgs.Empty);
  113. }
  114. public override bool CanFocus {
  115. get => true;
  116. set { base.CanFocus = value; }
  117. }
  118. public override bool ProcessKey (KeyEvent kb)
  119. {
  120. switch (kb.Key) {
  121. case Key.Delete:
  122. case Key.Backspace:
  123. if (point == 0)
  124. return true;
  125. SetText (text.Substring (0, point - 1) + text.Substring (point));
  126. point--;
  127. Adjust ();
  128. break;
  129. // Home, C-A
  130. case Key.Home:
  131. case Key.ControlA:
  132. point = 0;
  133. Adjust ();
  134. break;
  135. case Key.CursorLeft:
  136. case Key.ControlB:
  137. if (point > 0) {
  138. point--;
  139. Adjust ();
  140. }
  141. break;
  142. case Key.ControlD: // Delete
  143. if (point == text.Length)
  144. break;
  145. SetText (text.Substring (0, point) + text.Substring (point + 1));
  146. Adjust ();
  147. break;
  148. case Key.ControlE: // End
  149. point = text.Length;
  150. Adjust ();
  151. break;
  152. case Key.CursorRight:
  153. case Key.ControlF:
  154. if (point == text.Length)
  155. break;
  156. point++;
  157. Adjust ();
  158. break;
  159. case Key.ControlK: // kill-to-end
  160. kill = text.Substring (point);
  161. SetText (text.Substring (0, point));
  162. Adjust ();
  163. break;
  164. case Key.ControlY: // Control-y, yank
  165. if (kill == null)
  166. return true;
  167. if (point == text.Length) {
  168. SetText (text + kill);
  169. point = text.Length;
  170. } else {
  171. SetText (text.Substring (0, point) + kill + text.Substring (point));
  172. point += kill.Length;
  173. }
  174. Adjust ();
  175. break;
  176. case (Key)((int)'b' + Key.AltMask):
  177. int bw = WordBackward (point);
  178. if (bw != -1)
  179. point = bw;
  180. Adjust ();
  181. break;
  182. case (Key)((int)'f' + Key.AltMask):
  183. int fw = WordForward (point);
  184. if (fw != -1)
  185. point = fw;
  186. Adjust ();
  187. break;
  188. default:
  189. // Ignore other control characters.
  190. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  191. return false;
  192. if (used) {
  193. if (point == text.Length) {
  194. SetText (text + (char)kb.Key);
  195. } else {
  196. SetText (text.Substring (0, point) + (char)kb.Key + text.Substring (point));
  197. }
  198. point++;
  199. } else {
  200. SetText ("" + (char)kb.Key);
  201. first = 0;
  202. point = 1;
  203. }
  204. used = true;
  205. Adjust ();
  206. return true;
  207. }
  208. used = true;
  209. return true;
  210. }
  211. int WordForward (int p)
  212. {
  213. if (p >= text.Length)
  214. return -1;
  215. int i = p;
  216. if (Char.IsPunctuation (text [p]) || Char.IsWhiteSpace (text [p])) {
  217. for (; i < text.Length; i++) {
  218. if (Char.IsLetterOrDigit (text [i]))
  219. break;
  220. }
  221. for (; i < text.Length; i++) {
  222. if (!Char.IsLetterOrDigit (text [i]))
  223. break;
  224. }
  225. } else {
  226. for (; i < text.Length; i++) {
  227. if (!Char.IsLetterOrDigit (text [i]))
  228. break;
  229. }
  230. }
  231. if (i != p)
  232. return i;
  233. return -1;
  234. }
  235. int WordBackward (int p)
  236. {
  237. if (p == 0)
  238. return -1;
  239. int i = p - 1;
  240. if (i == 0)
  241. return 0;
  242. if (Char.IsPunctuation (text [i]) || Char.IsSymbol (text [i]) || Char.IsWhiteSpace (text [i])) {
  243. for (; i >= 0; i--) {
  244. if (Char.IsLetterOrDigit (text [i]))
  245. break;
  246. }
  247. for (; i >= 0; i--) {
  248. if (!Char.IsLetterOrDigit (text [i]))
  249. break;
  250. }
  251. } else {
  252. for (; i >= 0; i--) {
  253. if (!Char.IsLetterOrDigit (text [i]))
  254. break;
  255. }
  256. }
  257. i++;
  258. if (i != p)
  259. return i;
  260. return -1;
  261. }
  262. #if false
  263. public override void ProcessMouse (Curses.MouseEvent ev)
  264. {
  265. if ((ev.ButtonState & Curses.Event.Button1Clicked) == 0)
  266. return;
  267. .SetFocus (this);
  268. // We could also set the cursor position.
  269. point = first + (ev.X - x);
  270. if (point > text.Length)
  271. point = text.Length;
  272. if (point < first)
  273. point = 0;
  274. SetNeedsDisplay ();
  275. }
  276. #endif
  277. }
  278. }