TextField.cs 6.3 KB

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