TextField.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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. /// Text data entry widget
  14. /// </summary>
  15. /// <remarks>
  16. /// The Entry widget provides Emacs-like editing
  17. /// functionality, and mouse support.
  18. /// </remarks>
  19. public class TextField : View {
  20. List<Rune> text;
  21. int first, point;
  22. bool used;
  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 => used; set { used = value; } }
  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. /// Changed event, raised when the text has clicked.
  33. /// </summary>
  34. /// <remarks>
  35. /// Client code can hook up to this event, it is
  36. /// raised when the text in the entry changes.
  37. /// </remarks>
  38. public event EventHandler<ustring> Changed;
  39. /// <summary>
  40. /// Public constructor that creates a text field, with layout controlled with X, Y, Width and Height.
  41. /// </summary>
  42. /// <param name="text">Initial text contents.</param>
  43. public TextField (string text) : this (ustring.Make (text))
  44. {
  45. Height = 1;
  46. }
  47. /// <summary>
  48. /// Public constructor that creates a text field, with layout controlled with X, Y, Width and Height.
  49. /// </summary>
  50. /// <param name="text">Initial text contents.</param>
  51. public TextField (ustring text)
  52. {
  53. Initialize (text, Frame.Width);
  54. }
  55. /// <summary>
  56. /// Public constructor that creates a text field at an absolute position and size.
  57. /// </summary>
  58. /// <param name="x">The x coordinate.</param>
  59. /// <param name="y">The y coordinate.</param>
  60. /// <param name="w">The width.</param>
  61. /// <param name="text">Initial text contents.</param>
  62. public TextField (int x, int y, int w, ustring text) : base (new Rect (x, y, w, 1))
  63. {
  64. Initialize (text, w);
  65. }
  66. void Initialize (ustring text, int w)
  67. {
  68. if (text == null)
  69. text = "";
  70. this.text = TextModel.ToRunes (text);
  71. point = text.Length;
  72. first = point > w ? point - w : 0;
  73. CanFocus = true;
  74. Used = true;
  75. WantMousePositionReports = true;
  76. }
  77. public override bool OnLeave ()
  78. {
  79. if (Application.mouseGrabView != null && Application.mouseGrabView == this)
  80. Application.UngrabMouse ();
  81. if (SelectedLength != 0 && !(Application.mouseGrabView is MenuBar))
  82. ClearAllSelection ();
  83. return base.OnLeave ();
  84. }
  85. public override Rect Frame {
  86. get => base.Frame;
  87. set {
  88. base.Frame = value;
  89. var w = base.Frame.Width;
  90. first = point > w ? point - w : 0;
  91. Adjust ();
  92. }
  93. }
  94. List<ustring> historyText;
  95. int idxhistoryText;
  96. bool isFromHistory;
  97. /// <summary>
  98. /// Sets or gets the text in the entry.
  99. /// </summary>
  100. /// <remarks>
  101. /// </remarks>
  102. public ustring Text {
  103. get {
  104. return ustring.Make (text);
  105. }
  106. set {
  107. if (ReadOnly)
  108. return;
  109. var oldText = ustring.Make (text);
  110. text = TextModel.ToRunes (value);
  111. if (!Secret && !isFromHistory) {
  112. if (historyText == null)
  113. historyText = new List<ustring> () { oldText };
  114. if (idxhistoryText > 0 && idxhistoryText + 1 < historyText.Count)
  115. historyText.RemoveRange (idxhistoryText + 1, historyText.Count - idxhistoryText - 1);
  116. historyText.Add (ustring.Make (text));
  117. idxhistoryText++;
  118. }
  119. Changed?.Invoke (this, oldText);
  120. if (point > text.Count)
  121. point = Math.Max (DisplaySize (text, 0) - 1, 0);
  122. // FIXME: this needs to be updated to use Rune.ColumnWidth
  123. //first = point > Frame.Width ? point - Frame.Width : 0;
  124. Adjust ();
  125. SetNeedsDisplay ();
  126. }
  127. }
  128. /// <summary>
  129. /// Sets the secret property.
  130. /// </summary>
  131. /// <remarks>
  132. /// This makes the text entry suitable for entering passwords.
  133. /// </remarks>
  134. public bool Secret { get; set; }
  135. /// <summary>
  136. /// Sets or gets the current cursor position.
  137. /// </summary>
  138. public int CursorPosition {
  139. get { return point; }
  140. set {
  141. point = value;
  142. Adjust ();
  143. SetNeedsDisplay ();
  144. }
  145. }
  146. /// <summary>
  147. /// Sets the cursor position.
  148. /// </summary>
  149. public override void PositionCursor ()
  150. {
  151. var col = 0;
  152. for (int idx = first < 0 ? 0 : first; idx < text.Count; idx++) {
  153. if (idx == point)
  154. break;
  155. var cols = Rune.ColumnWidth (text [idx]);
  156. col += cols;
  157. }
  158. Move (col, 0);
  159. }
  160. public override void Redraw (Rect region)
  161. {
  162. ColorScheme color = Colors.Menu;
  163. SetSelectedStartSelectedLength ();
  164. Driver.SetAttribute (ColorScheme.Focus);
  165. Move (0, 0);
  166. int p = first;
  167. int col = 0;
  168. int width = Frame.Width;
  169. var tcount = text.Count;
  170. var roc = new Attribute (Color.DarkGray, Color.Gray);
  171. for (int idx = 0; idx < tcount; idx++){
  172. var rune = text [idx];
  173. if (idx < p)
  174. continue;
  175. var cols = Rune.ColumnWidth (rune);
  176. if (col == point && HasFocus && !Used && SelectedLength == 0 && !ReadOnly)
  177. Driver.SetAttribute (Colors.Menu.HotFocus);
  178. else if (ReadOnly)
  179. Driver.SetAttribute (idx >= start && length > 0 && idx < start + length ? color.Focus : roc);
  180. else
  181. Driver.SetAttribute (idx >= start && length > 0 && idx < start + length ? color.Focus : ColorScheme.Focus);
  182. if (col + cols <= width)
  183. Driver.AddRune ((Rune)(Secret ? '*' : rune));
  184. col += cols;
  185. }
  186. Driver.SetAttribute (ColorScheme.Focus);
  187. for (int i = col; i < Frame.Width; i++)
  188. Driver.AddRune (' ');
  189. PositionCursor ();
  190. }
  191. // Returns the size of the string starting at position start
  192. int DisplaySize (List<Rune> t, int start)
  193. {
  194. int size = 0;
  195. int tcount = t.Count;
  196. for (int i = start; i < tcount; i++) {
  197. var rune = t [i];
  198. size += Rune.ColumnWidth (rune);
  199. }
  200. return size;
  201. }
  202. void Adjust ()
  203. {
  204. int offB = 0;
  205. if (SuperView != null && SuperView.Frame.Right - Frame.Right < 0)
  206. offB = SuperView.Frame.Right - Frame.Right - 1;
  207. if (point < first)
  208. first = point;
  209. else if (first + point >= Frame.Width + offB) {
  210. first = point - (Frame.Width - 1 + offB);
  211. }
  212. SetNeedsDisplay ();
  213. }
  214. void SetText (List<Rune> newText)
  215. {
  216. Text = ustring.Make (newText);
  217. }
  218. void SetText (IEnumerable<Rune> newText)
  219. {
  220. SetText (newText.ToList ());
  221. }
  222. public override bool CanFocus {
  223. get => true;
  224. set { base.CanFocus = value; }
  225. }
  226. void SetClipboard (IEnumerable<Rune> text)
  227. {
  228. if (!Secret)
  229. Clipboard.Contents = ustring.Make (text.ToList ());
  230. }
  231. public override bool ProcessKey (KeyEvent kb)
  232. {
  233. // remember current cursor position
  234. // because the new calculated cursor position is needed to be set BEFORE the change event is triggest
  235. // Needed for the Elmish Wrapper issue https://github.com/DieselMeister/Terminal.Gui.Elmish/issues/2
  236. var oldCursorPos = point;
  237. switch (kb.Key) {
  238. case Key.DeleteChar:
  239. case Key.ControlD:
  240. if (ReadOnly)
  241. return true;
  242. if (SelectedLength == 0) {
  243. if (text.Count == 0 || text.Count == point)
  244. return true;
  245. SetText (text.GetRange (0, point).Concat (text.GetRange (point + 1, text.Count - (point + 1))));
  246. Adjust ();
  247. } else {
  248. DeleteSelectedText ();
  249. }
  250. break;
  251. case Key.Delete:
  252. case Key.Backspace:
  253. if (ReadOnly)
  254. return true;
  255. if (SelectedLength == 0) {
  256. if (point == 0)
  257. return true;
  258. point--;
  259. SetText (text.GetRange (0, oldCursorPos - 1).Concat (text.GetRange (oldCursorPos, text.Count - (oldCursorPos))));
  260. Adjust ();
  261. } else {
  262. DeleteSelectedText ();
  263. }
  264. break;
  265. case Key.Home | Key.ShiftMask:
  266. if (point > 0) {
  267. int x = point;
  268. point = 0;
  269. PrepareSelection (x, point - x);
  270. }
  271. break;
  272. case Key.End | Key.ShiftMask:
  273. if (point < text.Count) {
  274. int x = point;
  275. point = text.Count;
  276. PrepareSelection (x, point - x);
  277. }
  278. break;
  279. // Home, C-A
  280. case Key.Home:
  281. case Key.ControlA:
  282. ClearAllSelection ();
  283. point = 0;
  284. Adjust ();
  285. break;
  286. case Key.CursorLeft | Key.ShiftMask:
  287. case Key.CursorUp | Key.ShiftMask:
  288. if (point > 0) {
  289. PrepareSelection (point--, -1);
  290. }
  291. break;
  292. case Key.CursorRight | Key.ShiftMask:
  293. case Key.CursorDown | Key.ShiftMask:
  294. if (point < text.Count) {
  295. PrepareSelection (point++, 1);
  296. }
  297. break;
  298. case Key.CursorLeft | Key.ShiftMask | Key.CtrlMask:
  299. case Key.CursorUp | Key.ShiftMask | Key.CtrlMask:
  300. if (point > 0) {
  301. int x = start > -1 ? start : point;
  302. int sbw = WordBackward (point);
  303. if (sbw != -1)
  304. point = sbw;
  305. PrepareSelection (x, sbw - x);
  306. }
  307. break;
  308. case Key.CursorRight | Key.ShiftMask | Key.CtrlMask:
  309. case Key.CursorDown | Key.ShiftMask | Key.CtrlMask:
  310. if (point < text.Count) {
  311. int x = start > -1 ? start : point;
  312. int sfw = WordForward (point);
  313. if (sfw != -1)
  314. point = sfw;
  315. PrepareSelection (x, sfw - x);
  316. }
  317. break;
  318. case Key.CursorLeft:
  319. case Key.ControlB:
  320. ClearAllSelection ();
  321. if (point > 0) {
  322. point--;
  323. Adjust ();
  324. }
  325. break;
  326. case Key.End:
  327. case Key.ControlE: // End
  328. ClearAllSelection ();
  329. point = text.Count;
  330. Adjust ();
  331. break;
  332. case Key.CursorRight:
  333. case Key.ControlF:
  334. ClearAllSelection ();
  335. if (point == text.Count)
  336. break;
  337. point++;
  338. Adjust ();
  339. break;
  340. case Key.ControlK: // kill-to-end
  341. if (ReadOnly)
  342. return true;
  343. ClearAllSelection ();
  344. if (point >= text.Count)
  345. return true;
  346. SetClipboard (text.GetRange (point, text.Count - point));
  347. SetText (text.GetRange (0, point));
  348. Adjust ();
  349. break;
  350. // Undo
  351. case Key.ControlZ:
  352. if (ReadOnly)
  353. return true;
  354. if (historyText != null && historyText.Count > 0) {
  355. isFromHistory = true;
  356. if (idxhistoryText > 0)
  357. idxhistoryText--;
  358. if (idxhistoryText > -1)
  359. Text = historyText [idxhistoryText];
  360. point = text.Count;
  361. isFromHistory = false;
  362. }
  363. break;
  364. //Redo
  365. case Key.ControlY: // Control-y, yank
  366. if (ReadOnly)
  367. return true;
  368. if (historyText != null && historyText.Count > 0) {
  369. isFromHistory = true;
  370. if (idxhistoryText < historyText.Count - 1) {
  371. idxhistoryText++;
  372. if (idxhistoryText < historyText.Count) {
  373. Text = historyText [idxhistoryText];
  374. } else if (idxhistoryText == historyText.Count - 1) {
  375. Text = historyText [historyText.Count - 1];
  376. }
  377. point = text.Count;
  378. }
  379. isFromHistory = false;
  380. }
  381. //if (Clipboard.Contents == null)
  382. // return true;
  383. //var clip = TextModel.ToRunes (Clipboard.Contents);
  384. //if (clip == null)
  385. // return true;
  386. //if (point == text.Count) {
  387. // point = text.Count;
  388. // SetText(text.Concat(clip).ToList());
  389. //} else {
  390. // point += clip.Count;
  391. // SetText(text.GetRange(0, oldCursorPos).Concat(clip).Concat(text.GetRange(oldCursorPos, text.Count - oldCursorPos)));
  392. //}
  393. //Adjust ();
  394. break;
  395. case Key.CursorLeft | Key.CtrlMask:
  396. case (Key)((int)'b' + Key.AltMask):
  397. ClearAllSelection ();
  398. int bw = WordBackward (point);
  399. if (bw != -1)
  400. point = bw;
  401. Adjust ();
  402. break;
  403. case Key.CursorRight | Key.CtrlMask:
  404. case (Key)((int)'f' + Key.AltMask):
  405. ClearAllSelection ();
  406. int fw = WordForward (point);
  407. if (fw != -1)
  408. point = fw;
  409. Adjust ();
  410. break;
  411. case Key.InsertChar:
  412. Used = !Used;
  413. SetNeedsDisplay ();
  414. break;
  415. case Key.ControlC:
  416. Copy ();
  417. break;
  418. case Key.ControlX:
  419. if (ReadOnly)
  420. return true;
  421. Cut ();
  422. break;
  423. case Key.ControlV:
  424. Paste ();
  425. break;
  426. // MISSING:
  427. // Alt-D, Alt-backspace
  428. // Alt-Y
  429. // Delete adding to kill buffer
  430. default:
  431. // Ignore other control characters.
  432. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  433. return false;
  434. if (ReadOnly)
  435. return true;
  436. if (SelectedLength != 0) {
  437. DeleteSelectedText ();
  438. oldCursorPos = point;
  439. }
  440. var kbstr = TextModel.ToRunes (ustring.Make ((uint)kb.Key));
  441. if (used) {
  442. point++;
  443. if (point == text.Count + 1) {
  444. SetText (text.Concat (kbstr).ToList ());
  445. } else {
  446. SetText (text.GetRange (0, oldCursorPos).Concat (kbstr).Concat (text.GetRange (oldCursorPos, Math.Min (text.Count - oldCursorPos, text.Count))));
  447. }
  448. } else {
  449. SetText (text.GetRange (0, oldCursorPos).Concat (kbstr).Concat (text.GetRange (Math.Min (oldCursorPos + 1, text.Count), Math.Max (text.Count - oldCursorPos - 1, 0))));
  450. point++;
  451. }
  452. Adjust ();
  453. return true;
  454. }
  455. return true;
  456. }
  457. int WordForward (int p)
  458. {
  459. if (p >= text.Count)
  460. return -1;
  461. int i = p;
  462. if (Rune.IsPunctuation (text [p]) || Rune.IsWhiteSpace(text [p])) {
  463. for (; i < text.Count; i++) {
  464. var r = text [i];
  465. if (Rune.IsLetterOrDigit(r))
  466. break;
  467. }
  468. for (; i < text.Count; i++) {
  469. var r = text [i];
  470. if (!Rune.IsLetterOrDigit (r))
  471. break;
  472. }
  473. } else {
  474. for (; i < text.Count; i++) {
  475. var r = text [i];
  476. if (!Rune.IsLetterOrDigit (r))
  477. break;
  478. }
  479. }
  480. if (i != p)
  481. return i;
  482. return -1;
  483. }
  484. int WordBackward (int p)
  485. {
  486. if (p == 0)
  487. return -1;
  488. int i = p - 1;
  489. if (i == 0)
  490. return 0;
  491. var ti = text [i];
  492. if (Rune.IsPunctuation (ti) || Rune.IsSymbol(ti) || Rune.IsWhiteSpace(ti)) {
  493. for (; i >= 0; i--) {
  494. if (Rune.IsLetterOrDigit (text [i]))
  495. break;
  496. }
  497. for (; i >= 0; i--) {
  498. if (!Rune.IsLetterOrDigit (text [i]))
  499. break;
  500. }
  501. } else {
  502. for (; i >= 0; i--) {
  503. if (!Rune.IsLetterOrDigit (text [i]))
  504. break;
  505. }
  506. }
  507. i++;
  508. if (i != p)
  509. return i;
  510. return -1;
  511. }
  512. /// <summary>
  513. /// Start position of the selected text.
  514. /// </summary>
  515. public int SelectedStart { get; set; } = -1;
  516. /// <summary>
  517. /// Length of the selected text.
  518. /// </summary>
  519. public int SelectedLength { get; set; } = 0;
  520. /// <summary>
  521. /// The selected text.
  522. /// </summary>
  523. public ustring SelectedText { get; set; }
  524. int start, length;
  525. bool isButtonReleased = true;
  526. public override bool MouseEvent (MouseEvent ev)
  527. {
  528. if (!ev.Flags.HasFlag (MouseFlags.Button1Pressed) && !ev.Flags.HasFlag (MouseFlags.ReportMousePosition) &&
  529. !ev.Flags.HasFlag (MouseFlags.Button1Released) && !ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked) &&
  530. !ev.Flags.HasFlag (MouseFlags.Button1TripleClicked))
  531. return false;
  532. if (ev.Flags == MouseFlags.Button1Pressed) {
  533. if (!HasFocus)
  534. SuperView.SetFocus (this);
  535. PositionCursor (ev);
  536. if (isButtonReleased)
  537. ClearAllSelection ();
  538. isButtonReleased = true;
  539. } else if (ev.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  540. int x = PositionCursor (ev);
  541. isButtonReleased = false;
  542. PrepareSelection (x);
  543. if (Application.mouseGrabView == null) {
  544. Application.GrabMouse (this);
  545. }
  546. } else if (ev.Flags == MouseFlags.Button1Released) {
  547. isButtonReleased = true;
  548. Application.UngrabMouse ();
  549. } else if (ev.Flags == MouseFlags.Button1DoubleClicked) {
  550. int x = PositionCursor (ev);
  551. int sbw = x;
  552. if (x > 0 && (char)Text [x - 1] != ' ')
  553. sbw = WordBackward (x);
  554. if (sbw != -1) {
  555. x = sbw;
  556. PositionCursor (x);
  557. }
  558. int sfw = WordForward (x);
  559. ClearAllSelection ();
  560. PrepareSelection (sbw, sfw - sbw);
  561. } else if (ev.Flags == MouseFlags.Button1TripleClicked) {
  562. PositionCursor (0);
  563. ClearAllSelection ();
  564. PrepareSelection (0, text.Count);
  565. }
  566. SetNeedsDisplay ();
  567. return true;
  568. }
  569. int PositionCursor (MouseEvent ev)
  570. {
  571. // We could also set the cursor position.
  572. int x;
  573. if (text.Count == 0)
  574. x = ev.X - ev.OfX;
  575. else
  576. x = ev.X;
  577. return PositionCursor (x);
  578. }
  579. private int PositionCursor (int x)
  580. {
  581. point = first + x;
  582. if (point > text.Count)
  583. point = text.Count;
  584. if (point < first)
  585. point = 0;
  586. return point;
  587. }
  588. void PrepareSelection (int x, int direction = 0)
  589. {
  590. x = x + first < 0 ? 0 : x;
  591. SelectedStart = SelectedStart == -1 && text.Count > 0 && x >= 0 && x <= text.Count ? x : SelectedStart;
  592. if (SelectedStart > -1) {
  593. SelectedLength = x + direction <= text.Count ? x + direction - SelectedStart : text.Count - SelectedStart;
  594. SetSelectedStartSelectedLength ();
  595. SelectedText = length > 0 ? ustring.Make (text).ToString ().Substring (
  596. start < 0 ? 0 : start, length > text.Count ? text.Count : length) : "";
  597. }
  598. Adjust ();
  599. }
  600. /// <summary>
  601. /// Clear the selected text.
  602. /// </summary>
  603. public void ClearAllSelection ()
  604. {
  605. if (SelectedStart == -1 && SelectedLength == 0)
  606. return;
  607. SelectedStart = -1;
  608. SelectedLength = 0;
  609. SelectedText = "";
  610. start = 0;
  611. }
  612. void SetSelectedStartSelectedLength ()
  613. {
  614. if (SelectedLength < 0) {
  615. start = SelectedLength + SelectedStart;
  616. length = Math.Abs (SelectedLength);
  617. } else {
  618. start = SelectedStart;
  619. length = SelectedLength;
  620. }
  621. }
  622. /// <summary>
  623. /// Copy the selected text to the clipboard.
  624. /// </summary>
  625. public virtual void Copy ()
  626. {
  627. if (Secret)
  628. return;
  629. if (SelectedLength != 0) {
  630. Clipboard.Contents = SelectedText;
  631. }
  632. }
  633. /// <summary>
  634. /// Cut the selected text to the clipboard.
  635. /// </summary>
  636. public virtual void Cut ()
  637. {
  638. if (SelectedLength != 0) {
  639. Clipboard.Contents = SelectedText;
  640. DeleteSelectedText ();
  641. }
  642. }
  643. void DeleteSelectedText ()
  644. {
  645. string actualText = Text.ToString ();
  646. int selStart = SelectedLength < 0 ? SelectedLength + SelectedStart : SelectedStart;
  647. int selLength = Math.Abs (SelectedLength);
  648. Text = actualText.Substring (0, selStart) +
  649. actualText.Substring (selStart + selLength, actualText.Length - selStart - selLength);
  650. ClearAllSelection ();
  651. CursorPosition = selStart >= Text.Length ? Text.Length : selStart;
  652. SetNeedsDisplay ();
  653. }
  654. /// <summary>
  655. /// Paste the selected text from the clipboard.
  656. /// </summary>
  657. public virtual void Paste ()
  658. {
  659. if (ReadOnly)
  660. return;
  661. string actualText = Text.ToString ();
  662. int start = SelectedStart == -1 ? CursorPosition : SelectedStart;
  663. ustring cbTxt = Clipboard.Contents?.ToString () ?? "";
  664. Text = actualText.Substring (0, start) +
  665. cbTxt +
  666. actualText.Substring (start + SelectedLength, actualText.Length - start - SelectedLength);
  667. point = start + cbTxt.Length;
  668. SelectedLength = 0;
  669. ClearAllSelection ();
  670. SetNeedsDisplay ();
  671. }
  672. }
  673. }