TextField.cs 22 KB

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