TextField.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. /// Single-line text entry <see cref="View"/>
  14. /// </summary>
  15. /// <remarks>
  16. /// The <see cref="TextField"/> <see cref="View"/> provides editing functionality and mouse support.
  17. /// </remarks>
  18. public class TextField : View {
  19. List<Rune> text;
  20. int first, point;
  21. int selectedStart = -1; // -1 represents there is no text selection.
  22. ustring selectedText;
  23. /// <summary>
  24. /// Tracks whether the text field should be considered "used", that is, that the user has moved in the entry, so new input should be appended at the cursor position, rather than clearing the entry
  25. /// </summary>
  26. public bool Used { get; set; }
  27. /// <summary>
  28. /// If set to true its not allow any changes in the text.
  29. /// </summary>
  30. public bool ReadOnly { get; set; } = false;
  31. /// <summary>
  32. /// Changing event, raised before the <see cref="Text"/> changes and can be canceled or changing the new text.
  33. /// </summary>
  34. public event Action<TextChangingEventArgs> TextChanging;
  35. /// <summary>
  36. /// Changed event, raised when the text has changed.
  37. /// </summary>
  38. /// <remarks>
  39. /// This event is raised when the <see cref="Text"/> changes.
  40. /// </remarks>
  41. /// <remarks>
  42. /// The passed <see cref="EventArgs"/> is a <see cref="ustring"/> containing the old value.
  43. /// </remarks>
  44. public event Action<ustring> TextChanged;
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="TextField"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  47. /// </summary>
  48. /// <param name="text">Initial text contents.</param>
  49. public TextField (string text) : this (ustring.Make (text)) { }
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="TextField"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  52. /// </summary>
  53. public TextField () : this (string.Empty) { }
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="TextField"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  56. /// </summary>
  57. /// <param name="text">Initial text contents.</param>
  58. public TextField (ustring text) : base (text)
  59. {
  60. Initialize (text, text.RuneCount + 1);
  61. }
  62. /// <summary>
  63. /// Initializes a new instance of the <see cref="TextField"/> class using <see cref="LayoutStyle.Absolute"/> positioning.
  64. /// </summary>
  65. /// <param name="x">The x coordinate.</param>
  66. /// <param name="y">The y coordinate.</param>
  67. /// <param name="w">The width.</param>
  68. /// <param name="text">Initial text contents.</param>
  69. public TextField (int x, int y, int w, ustring text) : base (new Rect (x, y, w, 1))
  70. {
  71. Initialize (text, w);
  72. }
  73. void Initialize (ustring text, int w)
  74. {
  75. Initialize ();
  76. if (text == null)
  77. text = "";
  78. this.text = TextModel.ToRunes (text.Split ("\n") [0]);
  79. point = text.RuneCount;
  80. first = point > w ? point - w : 0;
  81. CanFocus = true;
  82. Used = true;
  83. WantMousePositionReports = true;
  84. }
  85. void Initialize ()
  86. {
  87. Height = 1;
  88. }
  89. ///<inheritdoc/>
  90. public override bool OnLeave (View view)
  91. {
  92. if (Application.mouseGrabView != null && Application.mouseGrabView == this)
  93. Application.UngrabMouse ();
  94. //if (SelectedLength != 0 && !(Application.mouseGrabView is MenuBar))
  95. // ClearAllSelection ();
  96. return base.OnLeave (view);
  97. }
  98. ///<inheritdoc/>
  99. public override Rect Frame {
  100. get => base.Frame;
  101. set {
  102. base.Frame = value;
  103. Adjust ();
  104. }
  105. }
  106. List<ustring> historyText;
  107. int idxhistoryText;
  108. bool isFromHistory;
  109. /// <summary>
  110. /// Sets or gets the text held by the view.
  111. /// </summary>
  112. /// <remarks>
  113. /// </remarks>
  114. public new ustring Text {
  115. get {
  116. return ustring.Make (text);
  117. }
  118. set {
  119. var oldText = ustring.Make (text);
  120. if (oldText == value)
  121. return;
  122. var newText = OnTextChanging (value.Split ("\n") [0]);
  123. if (newText.Cancel) {
  124. if (point > text.Count) {
  125. point = text.Count;
  126. }
  127. return;
  128. }
  129. text = TextModel.ToRunes (newText.NewText);
  130. if (!Secret && !isFromHistory) {
  131. if (historyText == null)
  132. historyText = new List<ustring> () { oldText };
  133. if (idxhistoryText > 0 && idxhistoryText + 1 < historyText.Count)
  134. historyText.RemoveRange (idxhistoryText + 1, historyText.Count - idxhistoryText - 1);
  135. historyText.Add (ustring.Make (text));
  136. idxhistoryText++;
  137. }
  138. TextChanged?.Invoke (oldText);
  139. if (point > text.Count) {
  140. point = Math.Max (TextModel.DisplaySize (text, 0).size - 1, 0);
  141. }
  142. Adjust ();
  143. SetNeedsDisplay ();
  144. }
  145. }
  146. /// <summary>
  147. /// Sets the secret property.
  148. /// </summary>
  149. /// <remarks>
  150. /// This makes the text entry suitable for entering passwords.
  151. /// </remarks>
  152. public bool Secret { get; set; }
  153. /// <summary>
  154. /// Sets or gets the current cursor position.
  155. /// </summary>
  156. public int CursorPosition {
  157. get { return point; }
  158. set {
  159. if (value < 0) {
  160. point = 0;
  161. } else if (value > text.Count) {
  162. point = text.Count;
  163. } else {
  164. point = value;
  165. }
  166. PrepareSelection (selectedStart, point - selectedStart);
  167. }
  168. }
  169. /// <summary>
  170. /// Sets the cursor position.
  171. /// </summary>
  172. public override void PositionCursor ()
  173. {
  174. var col = 0;
  175. for (int idx = first < 0 ? 0 : first; idx < text.Count; idx++) {
  176. if (idx == point)
  177. break;
  178. var cols = Rune.ColumnWidth (text [idx]);
  179. TextModel.SetCol (ref col, Frame.Width - 1, cols);
  180. }
  181. Move (col, 0);
  182. }
  183. ///<inheritdoc/>
  184. public override void Redraw (Rect bounds)
  185. {
  186. var selColor = new Attribute (ColorScheme.Focus.Background, ColorScheme.Focus.Foreground);
  187. SetSelectedStartSelectedLength ();
  188. Driver.SetAttribute (ColorScheme.Focus);
  189. Move (0, 0);
  190. int p = first;
  191. int col = 0;
  192. int width = Frame.Width + OffSetBackground ();
  193. var tcount = text.Count;
  194. var roc = Colors.Menu.Disabled;
  195. for (int idx = p; idx < tcount; idx++) {
  196. var rune = text [idx];
  197. var cols = Rune.ColumnWidth (rune);
  198. if (idx == point && HasFocus && !Used && length == 0 && !ReadOnly) {
  199. Driver.SetAttribute (selColor);
  200. } else if (ReadOnly) {
  201. Driver.SetAttribute (idx >= start && length > 0 && idx < start + length ? selColor : roc);
  202. } else if (!HasFocus) {
  203. Driver.SetAttribute (ColorScheme.Focus);
  204. } else {
  205. Driver.SetAttribute (idx >= start && length > 0 && idx < start + length ? selColor : ColorScheme.Focus);
  206. }
  207. if (col + cols <= width) {
  208. Driver.AddRune ((Rune)(Secret ? '*' : rune));
  209. }
  210. if (!TextModel.SetCol (ref col, width, cols)) {
  211. break;
  212. }
  213. if (idx + 1 < tcount && col + Rune.ColumnWidth (text [idx + 1]) > width) {
  214. break;
  215. }
  216. }
  217. Driver.SetAttribute (ColorScheme.Focus);
  218. for (int i = col; i < width; i++) {
  219. Driver.AddRune (' ');
  220. }
  221. PositionCursor ();
  222. }
  223. void Adjust ()
  224. {
  225. int offB = OffSetBackground ();
  226. if (point < first) {
  227. first = point;
  228. } else if (first + point - (Frame.Width + offB) == 0 ||
  229. TextModel.DisplaySize (text, first, point).size >= Frame.Width + offB) {
  230. first = Math.Max (TextModel.CalculateLeftColumn (text, first,
  231. point, Frame.Width + offB), 0);
  232. }
  233. SetNeedsDisplay ();
  234. }
  235. int OffSetBackground ()
  236. {
  237. int offB = 0;
  238. if (SuperView?.Frame.Right - Frame.Right < 0) {
  239. offB = SuperView.Frame.Right - Frame.Right - 1;
  240. }
  241. return offB;
  242. }
  243. void SetText (List<Rune> newText)
  244. {
  245. Text = ustring.Make (newText);
  246. }
  247. void SetText (IEnumerable<Rune> newText)
  248. {
  249. SetText (newText.ToList ());
  250. }
  251. ///<inheritdoc/>
  252. public override bool CanFocus {
  253. get => base.CanFocus;
  254. set { base.CanFocus = value; }
  255. }
  256. void SetClipboard (IEnumerable<Rune> text)
  257. {
  258. if (!Secret)
  259. Clipboard.Contents = ustring.Make (text.ToList ());
  260. }
  261. /// <summary>
  262. /// Processes key presses for the <see cref="TextField"/>.
  263. /// </summary>
  264. /// <param name="kb"></param>
  265. /// <returns></returns>
  266. /// <remarks>
  267. /// The <see cref="TextField"/> control responds to the following keys:
  268. /// <list type="table">
  269. /// <listheader>
  270. /// <term>Keys</term>
  271. /// <description>Function</description>
  272. /// </listheader>
  273. /// <item>
  274. /// <term><see cref="Key.Delete"/>, <see cref="Key.Backspace"/></term>
  275. /// <description>Deletes the character before cursor.</description>
  276. /// </item>
  277. /// </list>
  278. /// </remarks>
  279. public override bool ProcessKey (KeyEvent kb)
  280. {
  281. // remember current cursor position
  282. // because the new calculated cursor position is needed to be set BEFORE the change event is triggest
  283. // Needed for the Elmish Wrapper issue https://github.com/DieselMeister/Terminal.Gui.Elmish/issues/2
  284. var oldCursorPos = point;
  285. switch (ShortcutHelper.GetModifiersKey (kb)) {
  286. case Key.Delete:
  287. case Key.DeleteChar:
  288. case Key.D | Key.CtrlMask:
  289. if (ReadOnly)
  290. return true;
  291. if (length == 0) {
  292. if (text.Count == 0 || text.Count == point)
  293. return true;
  294. SetText (text.GetRange (0, point).Concat (text.GetRange (point + 1, text.Count - (point + 1))));
  295. Adjust ();
  296. } else {
  297. DeleteSelectedText ();
  298. }
  299. break;
  300. case Key.Backspace:
  301. if (ReadOnly)
  302. return true;
  303. if (length == 0) {
  304. if (point == 0)
  305. return true;
  306. point--;
  307. if (oldCursorPos < text.Count) {
  308. SetText (text.GetRange (0, oldCursorPos - 1).Concat (text.GetRange (oldCursorPos, text.Count - oldCursorPos)));
  309. } else {
  310. SetText (text.GetRange (0, oldCursorPos - 1));
  311. }
  312. Adjust ();
  313. } else {
  314. DeleteSelectedText ();
  315. }
  316. break;
  317. case Key.Home | Key.ShiftMask:
  318. case Key.Home | Key.ShiftMask | Key.CtrlMask:
  319. case Key.A | Key.ShiftMask | Key.CtrlMask:
  320. if (point > 0) {
  321. int x = point;
  322. point = 0;
  323. PrepareSelection (x, point - x);
  324. }
  325. break;
  326. case Key.End | Key.ShiftMask:
  327. case Key.End | Key.ShiftMask | Key.CtrlMask:
  328. case Key.E | Key.ShiftMask | Key.CtrlMask:
  329. if (point <= text.Count) {
  330. int x = point;
  331. point = text.Count;
  332. PrepareSelection (x, point - x);
  333. }
  334. break;
  335. // Home, C-A
  336. case Key.Home:
  337. case Key.Home | Key.CtrlMask:
  338. case Key.A | Key.CtrlMask:
  339. ClearAllSelection ();
  340. point = 0;
  341. Adjust ();
  342. break;
  343. case Key.CursorLeft | Key.ShiftMask:
  344. case Key.CursorUp | Key.ShiftMask:
  345. if (point > 0) {
  346. PrepareSelection (point--, -1);
  347. }
  348. break;
  349. case Key.CursorRight | Key.ShiftMask:
  350. case Key.CursorDown | Key.ShiftMask:
  351. if (point < text.Count) {
  352. PrepareSelection (point++, 1);
  353. }
  354. break;
  355. case Key.CursorLeft | Key.ShiftMask | Key.CtrlMask:
  356. case Key.CursorUp | Key.ShiftMask | Key.CtrlMask:
  357. case (Key)((int)'B' + Key.ShiftMask | Key.AltMask):
  358. if (point > 0) {
  359. int x = Math.Min (start > -1 && start > point ? start : point, text.Count);
  360. if (x > 0) {
  361. int sbw = WordBackward (x);
  362. if (sbw != -1)
  363. point = sbw;
  364. PrepareSelection (x, sbw - x);
  365. }
  366. }
  367. break;
  368. case Key.CursorRight | Key.ShiftMask | Key.CtrlMask:
  369. case Key.CursorDown | Key.ShiftMask | Key.CtrlMask:
  370. case (Key)((int)'F' + Key.ShiftMask | Key.AltMask):
  371. if (point < text.Count) {
  372. int x = start > -1 && start > point ? start : point;
  373. int sfw = WordForward (x);
  374. if (sfw != -1)
  375. point = sfw;
  376. PrepareSelection (x, sfw - x);
  377. }
  378. break;
  379. case Key.CursorLeft:
  380. case Key.B | Key.CtrlMask:
  381. ClearAllSelection ();
  382. if (point > 0) {
  383. point--;
  384. Adjust ();
  385. }
  386. break;
  387. case Key.End:
  388. case Key.End | Key.CtrlMask:
  389. case Key.E | Key.CtrlMask: // End
  390. ClearAllSelection ();
  391. point = text.Count;
  392. Adjust ();
  393. break;
  394. case Key.CursorRight:
  395. case Key.F | Key.CtrlMask:
  396. ClearAllSelection ();
  397. if (point == text.Count)
  398. break;
  399. point++;
  400. Adjust ();
  401. break;
  402. case Key.K | Key.CtrlMask: // kill-to-end
  403. if (ReadOnly)
  404. return true;
  405. ClearAllSelection ();
  406. if (point >= text.Count)
  407. return true;
  408. SetClipboard (text.GetRange (point, text.Count - point));
  409. SetText (text.GetRange (0, point));
  410. Adjust ();
  411. break;
  412. // Undo
  413. case Key.Z | Key.CtrlMask:
  414. if (ReadOnly)
  415. return true;
  416. if (historyText != null && historyText.Count > 0) {
  417. isFromHistory = true;
  418. if (idxhistoryText > 0)
  419. idxhistoryText--;
  420. if (idxhistoryText > -1)
  421. Text = historyText [idxhistoryText];
  422. point = text.Count;
  423. isFromHistory = false;
  424. }
  425. break;
  426. //Redo
  427. case Key.Y | Key.CtrlMask: // Control-y, yank
  428. if (ReadOnly)
  429. return true;
  430. if (historyText != null && historyText.Count > 0) {
  431. isFromHistory = true;
  432. if (idxhistoryText < historyText.Count - 1) {
  433. idxhistoryText++;
  434. if (idxhistoryText < historyText.Count) {
  435. Text = historyText [idxhistoryText];
  436. } else if (idxhistoryText == historyText.Count - 1) {
  437. Text = historyText [historyText.Count - 1];
  438. }
  439. point = text.Count;
  440. }
  441. isFromHistory = false;
  442. }
  443. //if (Clipboard.Contents == null)
  444. // return true;
  445. //var clip = TextModel.ToRunes (Clipboard.Contents);
  446. //if (clip == null)
  447. // return true;
  448. //if (point == text.Count) {
  449. // point = text.Count;
  450. // SetText(text.Concat(clip).ToList());
  451. //} else {
  452. // point += clip.Count;
  453. // SetText(text.GetRange(0, oldCursorPos).Concat(clip).Concat(text.GetRange(oldCursorPos, text.Count - oldCursorPos)));
  454. //}
  455. //Adjust ();
  456. break;
  457. case Key.CursorLeft | Key.CtrlMask:
  458. case Key.CursorUp | Key.CtrlMask:
  459. case (Key)((int)'B' + Key.AltMask):
  460. ClearAllSelection ();
  461. int bw = WordBackward (point);
  462. if (bw != -1)
  463. point = bw;
  464. Adjust ();
  465. break;
  466. case Key.CursorRight | Key.CtrlMask:
  467. case Key.CursorDown | Key.CtrlMask:
  468. case (Key)((int)'F' + Key.AltMask):
  469. ClearAllSelection ();
  470. int fw = WordForward (point);
  471. if (fw != -1)
  472. point = fw;
  473. Adjust ();
  474. break;
  475. case Key.InsertChar:
  476. Used = !Used;
  477. SetNeedsDisplay ();
  478. break;
  479. case Key.C | Key.CtrlMask:
  480. Copy ();
  481. break;
  482. case Key.X | Key.CtrlMask:
  483. if (ReadOnly)
  484. return true;
  485. Cut ();
  486. break;
  487. case Key.V | Key.CtrlMask:
  488. Paste ();
  489. break;
  490. // MISSING:
  491. // Alt-D, Alt-backspace
  492. // Alt-Y
  493. // Delete adding to kill buffer
  494. default:
  495. // Ignore other control characters.
  496. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  497. return false;
  498. if (ReadOnly)
  499. return true;
  500. if (length > 0) {
  501. DeleteSelectedText ();
  502. oldCursorPos = point;
  503. }
  504. var kbstr = TextModel.ToRunes (ustring.Make ((uint)kb.Key));
  505. if (Used) {
  506. point++;
  507. if (point == text.Count + 1) {
  508. SetText (text.Concat (kbstr).ToList ());
  509. } else {
  510. if (oldCursorPos > text.Count) {
  511. oldCursorPos = text.Count;
  512. }
  513. SetText (text.GetRange (0, oldCursorPos).Concat (kbstr).Concat (text.GetRange (oldCursorPos, Math.Min (text.Count - oldCursorPos, text.Count))));
  514. }
  515. } else {
  516. SetText (text.GetRange (0, oldCursorPos).Concat (kbstr).Concat (text.GetRange (Math.Min (oldCursorPos + 1, text.Count), Math.Max (text.Count - oldCursorPos - 1, 0))));
  517. point++;
  518. }
  519. Adjust ();
  520. return true;
  521. }
  522. return true;
  523. }
  524. int WordForward (int p)
  525. {
  526. if (p >= text.Count)
  527. return -1;
  528. int i = p + 1;
  529. if (i == text.Count)
  530. return text.Count;
  531. var ti = text [i];
  532. if (Rune.IsPunctuation (ti) || Rune.IsSymbol (ti) || Rune.IsWhiteSpace (ti)) {
  533. for (; i < text.Count; i++) {
  534. if (Rune.IsLetterOrDigit (text [i]))
  535. return i;
  536. }
  537. } else {
  538. for (; i < text.Count; i++) {
  539. if (!Rune.IsLetterOrDigit (text [i]))
  540. break;
  541. }
  542. for (; i < text.Count; i++) {
  543. if (Rune.IsLetterOrDigit (text [i]))
  544. break;
  545. }
  546. }
  547. if (i != p)
  548. return Math.Min (i, text.Count);
  549. return -1;
  550. }
  551. int WordBackward (int p)
  552. {
  553. if (p == 0)
  554. return -1;
  555. int i = p - 1;
  556. if (i == 0)
  557. return 0;
  558. var ti = text [i];
  559. var lastValidCol = -1;
  560. if (Rune.IsPunctuation (ti) || Rune.IsSymbol (ti) || Rune.IsWhiteSpace (ti)) {
  561. for (; i >= 0; i--) {
  562. if (Rune.IsLetterOrDigit (text [i])) {
  563. lastValidCol = i;
  564. break;
  565. }
  566. if (i - 1 > 0 && !Rune.IsWhiteSpace (text [i]) && Rune.IsWhiteSpace (text [i - 1])) {
  567. return i;
  568. }
  569. }
  570. for (; i >= 0; i--) {
  571. if (!Rune.IsLetterOrDigit (text [i]))
  572. break;
  573. lastValidCol = i;
  574. }
  575. if (lastValidCol > -1) {
  576. return lastValidCol;
  577. }
  578. } else {
  579. for (; i >= 0; i--) {
  580. if (!Rune.IsLetterOrDigit (text [i]))
  581. break;
  582. lastValidCol = i;
  583. }
  584. if (lastValidCol > -1) {
  585. return lastValidCol;
  586. }
  587. }
  588. if (i != p)
  589. return Math.Max (i, 0);
  590. return -1;
  591. }
  592. /// <summary>
  593. /// Start position of the selected text.
  594. /// </summary>
  595. public int SelectedStart {
  596. get => selectedStart;
  597. set {
  598. if (value < -1) {
  599. selectedStart = -1;
  600. } else if (value > text.Count) {
  601. selectedStart = text.Count;
  602. } else {
  603. selectedStart = value;
  604. }
  605. PrepareSelection (selectedStart, point - selectedStart);
  606. }
  607. }
  608. /// <summary>
  609. /// Length of the selected text.
  610. /// </summary>
  611. public int SelectedLength { get => length; }
  612. /// <summary>
  613. /// The selected text.
  614. /// </summary>
  615. public ustring SelectedText {
  616. get => Secret ? null : selectedText;
  617. private set => selectedText = value;
  618. }
  619. int start, length;
  620. bool isButtonPressed;
  621. bool isButtonReleased = true;
  622. ///<inheritdoc/>
  623. public override bool MouseEvent (MouseEvent ev)
  624. {
  625. if (!ev.Flags.HasFlag (MouseFlags.Button1Pressed) && !ev.Flags.HasFlag (MouseFlags.ReportMousePosition) &&
  626. !ev.Flags.HasFlag (MouseFlags.Button1Released) && !ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked) &&
  627. !ev.Flags.HasFlag (MouseFlags.Button1TripleClicked)) {
  628. return false;
  629. }
  630. if (ev.Flags == MouseFlags.Button1Pressed) {
  631. if (!CanFocus) {
  632. return true;
  633. }
  634. if (!HasFocus) {
  635. SetFocus ();
  636. }
  637. PositionCursor (ev);
  638. if (isButtonReleased) {
  639. ClearAllSelection ();
  640. }
  641. isButtonReleased = true;
  642. isButtonPressed = true;
  643. } else if (ev.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && isButtonPressed) {
  644. int x = PositionCursor (ev);
  645. isButtonReleased = false;
  646. PrepareSelection (x);
  647. if (Application.mouseGrabView == null) {
  648. Application.GrabMouse (this);
  649. }
  650. } else if (ev.Flags == MouseFlags.Button1Released) {
  651. isButtonReleased = true;
  652. isButtonPressed = false;
  653. Application.UngrabMouse ();
  654. } else if (ev.Flags == MouseFlags.Button1DoubleClicked) {
  655. int x = PositionCursor (ev);
  656. int sbw = x;
  657. if (x > 0 && (char)Text [x - 1] != ' ') {
  658. sbw = WordBackward (x);
  659. }
  660. if (sbw != -1) {
  661. x = sbw;
  662. PositionCursor (x);
  663. }
  664. int sfw = WordForward (x);
  665. ClearAllSelection ();
  666. if (sfw != -1 && sbw != -1) {
  667. point = sfw;
  668. }
  669. PrepareSelection (sbw, sfw - sbw);
  670. } else if (ev.Flags == MouseFlags.Button1TripleClicked) {
  671. PositionCursor (0);
  672. ClearAllSelection ();
  673. PrepareSelection (0, text.Count);
  674. }
  675. SetNeedsDisplay ();
  676. return true;
  677. }
  678. int PositionCursor (MouseEvent ev)
  679. {
  680. // We could also set the cursor position.
  681. int x;
  682. var pX = TextModel.GetColFromX (text, first, ev.X);
  683. if (text.Count == 0) {
  684. x = pX - ev.OfX;
  685. } else {
  686. x = pX;
  687. }
  688. return PositionCursor (x, false);
  689. }
  690. int PositionCursor (int x, bool getX = true)
  691. {
  692. int pX = x;
  693. if (getX) {
  694. pX = TextModel.GetColFromX (text, first, x);
  695. }
  696. if (first + pX > text.Count) {
  697. point = text.Count;
  698. } else if (first + pX < first) {
  699. point = 0;
  700. } else {
  701. point = first + pX;
  702. }
  703. return point;
  704. }
  705. void PrepareSelection (int x, int direction = 0)
  706. {
  707. x = x + first < -1 ? 0 : x;
  708. selectedStart = selectedStart == -1 && text.Count > 0 && x >= 0 && x <= text.Count ? x : selectedStart;
  709. if (selectedStart > -1) {
  710. length = Math.Abs (x + direction <= text.Count ? x + direction - selectedStart : text.Count - selectedStart);
  711. SetSelectedStartSelectedLength ();
  712. if (start > -1 && length > 0) {
  713. selectedText = length > 0 ? ustring.Make (text).ToString ().Substring (
  714. start < 0 ? 0 : start, length > text.Count ? text.Count : length) : "";
  715. if (first > start) {
  716. first = start;
  717. }
  718. }
  719. } else if (length > 0) {
  720. ClearAllSelection ();
  721. }
  722. Adjust ();
  723. }
  724. /// <summary>
  725. /// Clear the selected text.
  726. /// </summary>
  727. public void ClearAllSelection ()
  728. {
  729. if (selectedStart == -1 && length == 0)
  730. return;
  731. selectedStart = -1;
  732. length = 0;
  733. selectedText = null;
  734. start = 0;
  735. length = 0;
  736. SetNeedsDisplay ();
  737. }
  738. void SetSelectedStartSelectedLength ()
  739. {
  740. if (SelectedStart > -1 && point < SelectedStart) {
  741. start = point;
  742. } else {
  743. start = SelectedStart;
  744. }
  745. }
  746. /// <summary>
  747. /// Copy the selected text to the clipboard.
  748. /// </summary>
  749. public virtual void Copy ()
  750. {
  751. if (Secret || length == 0)
  752. return;
  753. Clipboard.Contents = SelectedText;
  754. }
  755. /// <summary>
  756. /// Cut the selected text to the clipboard.
  757. /// </summary>
  758. public virtual void Cut ()
  759. {
  760. if (Secret || length == 0)
  761. return;
  762. Clipboard.Contents = SelectedText;
  763. DeleteSelectedText ();
  764. }
  765. void DeleteSelectedText ()
  766. {
  767. ustring actualText = Text;
  768. SetSelectedStartSelectedLength ();
  769. int selStart = SelectedStart > -1 ? start : point;
  770. (var _, var len) = TextModel.DisplaySize (text, 0, selStart, false);
  771. (var _, var len2) = TextModel.DisplaySize (text, selStart, selStart + length, false);
  772. (var _, var len3) = TextModel.DisplaySize (text, selStart + length, actualText.RuneCount, false);
  773. Text = actualText [0, len] +
  774. actualText [len + len2, len + len2 + len3];
  775. ClearAllSelection ();
  776. point = selStart >= Text.RuneCount ? Text.RuneCount : selStart;
  777. Adjust ();
  778. }
  779. /// <summary>
  780. /// Paste the selected text from the clipboard.
  781. /// </summary>
  782. public virtual void Paste ()
  783. {
  784. if (ReadOnly || Clipboard.Contents == null) {
  785. return;
  786. }
  787. SetSelectedStartSelectedLength ();
  788. int selStart = start == -1 ? CursorPosition : start;
  789. ustring actualText = Text;
  790. (int _, int len) = TextModel.DisplaySize (text, 0, selStart, false);
  791. (var _, var len2) = TextModel.DisplaySize (text, selStart, selStart + length, false);
  792. (var _, var len3) = TextModel.DisplaySize (text, selStart + length, actualText.RuneCount, false);
  793. ustring cbTxt = Clipboard.Contents.Split ("\n") [0] ?? "";
  794. Text = actualText [0, len] +
  795. cbTxt +
  796. actualText [len + len2, len + len2 + len3];
  797. point = selStart + cbTxt.RuneCount;
  798. ClearAllSelection ();
  799. SetNeedsDisplay ();
  800. Adjust ();
  801. }
  802. /// <summary>
  803. /// Virtual method that invoke the <see cref="TextChanging"/> event if it's defined.
  804. /// </summary>
  805. /// <param name="newText">The new text to be replaced.</param>
  806. /// <returns>Returns the <see cref="TextChangingEventArgs"/></returns>
  807. public virtual TextChangingEventArgs OnTextChanging (ustring newText)
  808. {
  809. var ev = new TextChangingEventArgs (newText);
  810. TextChanging?.Invoke (ev);
  811. return ev;
  812. }
  813. CursorVisibility desiredCursorVisibility = CursorVisibility.Default;
  814. /// <summary>
  815. /// Get / Set the wished cursor when the field is focused
  816. /// </summary>
  817. public CursorVisibility DesiredCursorVisibility {
  818. get => desiredCursorVisibility;
  819. set {
  820. if (desiredCursorVisibility != value && HasFocus) {
  821. Application.Driver.SetCursorVisibility (value);
  822. }
  823. desiredCursorVisibility = value;
  824. }
  825. }
  826. ///<inheritdoc/>
  827. public override bool OnEnter (View view)
  828. {
  829. Application.Driver.SetCursorVisibility (DesiredCursorVisibility);
  830. return base.OnEnter (view);
  831. }
  832. }
  833. /// <summary>
  834. /// An <see cref="EventArgs"/> which allows passing a cancelable new text value event.
  835. /// </summary>
  836. public class TextChangingEventArgs : EventArgs {
  837. /// <summary>
  838. /// The new text to be replaced.
  839. /// </summary>
  840. public ustring NewText { get; set; }
  841. /// <summary>
  842. /// Flag which allows to cancel the new text value.
  843. /// </summary>
  844. public bool Cancel { get; set; }
  845. /// <summary>
  846. /// Initializes a new instance of <see cref="TextChangingEventArgs"/>
  847. /// </summary>
  848. /// <param name="newText">The new <see cref="TextField.Text"/> to be replaced.</param>
  849. public TextChangingEventArgs (ustring newText)
  850. {
  851. NewText = newText;
  852. }
  853. }
  854. }