TextField.cs 6.0 KB

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