TextView.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. //
  2. // TextView.cs: multi-line text editing
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // TODO:
  9. // In ReadOnly mode backspace/space behave like pageup/pagedown
  10. // Attributed text on spans
  11. // Replace insertion with Insert method
  12. // String accumulation (Control-k, control-k is not preserving the last new line, see StringToRunes
  13. // Alt-D, Alt-Backspace
  14. // API to set the cursor position
  15. // API to scroll to a particular place
  16. // keybindings to go to top/bottom
  17. // public API to insert, remove ranges
  18. // Add word forward/word backwards commands
  19. // Save buffer API
  20. // Mouse
  21. //
  22. // Desirable:
  23. // Move all the text manipulation into the TextModel
  24. using System;
  25. using System.Collections.Generic;
  26. using System.IO;
  27. using System.Linq;
  28. using System.Text;
  29. using NStack;
  30. using Rune = System.Rune;
  31. namespace Terminal.Gui {
  32. class TextModel {
  33. List<List<Rune>> lines = new List<List<Rune>> ();
  34. public bool LoadFile (string file)
  35. {
  36. if (file == null)
  37. throw new ArgumentNullException (nameof (file));
  38. try {
  39. FilePath = file;
  40. var stream = File.OpenRead (file);
  41. } catch {
  42. return false;
  43. }
  44. LoadStream (File.OpenRead (file));
  45. return true;
  46. }
  47. public bool CloseFile ()
  48. {
  49. if (FilePath == null)
  50. throw new ArgumentNullException (nameof (FilePath));
  51. try {
  52. FilePath = null;
  53. lines = new List<List<Rune>> ();
  54. } catch {
  55. return false;
  56. }
  57. return true;
  58. }
  59. // Turns the ustring into runes, this does not split the
  60. // contents on a newline if it is present.
  61. internal static List<Rune> ToRunes (ustring str)
  62. {
  63. List<Rune> runes = new List<Rune> ();
  64. foreach (var x in str.ToRunes ()) {
  65. runes.Add (x);
  66. }
  67. return runes;
  68. }
  69. // Splits a string into a List that contains a List<Rune> for each line
  70. public static List<List<Rune>> StringToRunes (ustring content)
  71. {
  72. var lines = new List<List<Rune>> ();
  73. int start = 0, i = 0;
  74. // BUGBUG: I think this is buggy w.r.t Unicode. content.Length is bytes, and content[i] is bytes
  75. // and content[i] == 10 may be the middle of a Rune.
  76. for (; i < content.Length; i++) {
  77. if (content [i] == 10) {
  78. if (i - start > 0)
  79. lines.Add (ToRunes (content [start, i]));
  80. else
  81. lines.Add (ToRunes (ustring.Empty));
  82. start = i + 1;
  83. }
  84. }
  85. if (i - start >= 0)
  86. lines.Add (ToRunes (content [start, null]));
  87. return lines;
  88. }
  89. void Append (List<byte> line)
  90. {
  91. var str = ustring.Make (line.ToArray ());
  92. lines.Add (ToRunes (str));
  93. }
  94. public void LoadStream (Stream input)
  95. {
  96. if (input == null)
  97. throw new ArgumentNullException (nameof (input));
  98. lines = new List<List<Rune>> ();
  99. var buff = new BufferedStream (input);
  100. int v;
  101. var line = new List<byte> ();
  102. while ((v = buff.ReadByte ()) != -1) {
  103. if (v == 10) {
  104. Append (line);
  105. line.Clear ();
  106. continue;
  107. }
  108. line.Add ((byte)v);
  109. }
  110. if (line.Count > 0)
  111. Append (line);
  112. }
  113. public void LoadString (ustring content)
  114. {
  115. lines = StringToRunes (content);
  116. }
  117. public override string ToString ()
  118. {
  119. var sb = new StringBuilder ();
  120. for (int i = 0; i < lines.Count; i++) {
  121. sb.Append (ustring.Make (lines[i]));
  122. if ((i + 1) < lines.Count) {
  123. sb.AppendLine ();
  124. }
  125. }
  126. return sb.ToString ();
  127. }
  128. public string FilePath { get; set; }
  129. /// <summary>
  130. /// The number of text lines in the model
  131. /// </summary>
  132. public int Count => lines.Count;
  133. /// <summary>
  134. /// Returns the specified line as a List of Rune
  135. /// </summary>
  136. /// <returns>The line.</returns>
  137. /// <param name="line">Line number to retrieve.</param>
  138. public List<Rune> GetLine (int line) => line < Count ? lines [line] : lines [Count - 1];
  139. /// <summary>
  140. /// Adds a line to the model at the specified position.
  141. /// </summary>
  142. /// <param name="pos">Line number where the line will be inserted.</param>
  143. /// <param name="runes">The line of text, as a List of Rune.</param>
  144. public void AddLine (int pos, List<Rune> runes)
  145. {
  146. lines.Insert (pos, runes);
  147. }
  148. /// <summary>
  149. /// Removes the line at the specified position
  150. /// </summary>
  151. /// <param name="pos">Position.</param>
  152. public void RemoveLine (int pos)
  153. {
  154. lines.RemoveAt (pos);
  155. }
  156. /// <summary>
  157. /// Returns the maximum line length of the visible lines.
  158. /// </summary>
  159. /// <param name="first">The first line.</param>
  160. /// <param name="last">The last line.</param>
  161. public int GetMaxVisibleLine (int first, int last)
  162. {
  163. int maxLength = 0;
  164. last = last < lines.Count ? last : lines.Count;
  165. for (int i = first; i < last; i++) {
  166. var l = GetLine (i).Count;
  167. if (l > maxLength) {
  168. maxLength = l;
  169. }
  170. }
  171. return maxLength;
  172. }
  173. internal static int SetCol (int col, int width, int cols)
  174. {
  175. if (col + cols <= width) {
  176. col += cols;
  177. }
  178. return col;
  179. }
  180. internal static int GetColFromX (List<Rune> t, int start, int x)
  181. {
  182. if (x < 0) {
  183. return x;
  184. }
  185. int size = start;
  186. var pX = x + start;
  187. for (int i = start; i < t.Count; i++) {
  188. var r = t [i];
  189. size += Rune.ColumnWidth (r);
  190. if (i == pX || (size > pX)) {
  191. return i - start;
  192. }
  193. }
  194. return t.Count - start;
  195. }
  196. // Returns the size and length in a range of the string.
  197. internal static (int size, int length) DisplaySize (List<Rune> t, int start = -1, int end = -1, bool checkNextRune = true)
  198. {
  199. if (t == null || t.Count == 0) {
  200. return (0, 0);
  201. }
  202. int size = 0;
  203. int len = 0;
  204. int tcount = end == -1 ? t.Count : end > t.Count ? t.Count : end;
  205. int i = start == -1 ? 0 : start;
  206. for (; i < tcount; i++) {
  207. var rune = t [i];
  208. size += Rune.ColumnWidth (rune);
  209. len += Rune.RuneLen (rune);
  210. if (checkNextRune && i == tcount - 1 && t.Count > tcount && Rune.ColumnWidth (t [i + 1]) > 1) {
  211. size += Rune.ColumnWidth (t [i + 1]);
  212. len += Rune.RuneLen (t [i + 1]);
  213. }
  214. }
  215. return (size, len);
  216. }
  217. // Returns the left column in a range of the string.
  218. internal static int CalculateLeftColumn (List<Rune> t, int start, int end, int width, int currentColumn)
  219. {
  220. if (t == null) {
  221. return 0;
  222. }
  223. (var dSize, _) = TextModel.DisplaySize (t, start, end);
  224. if (dSize < width) {
  225. return start;
  226. }
  227. int size = 0;
  228. int tcount = end > t.Count - 1 ? t.Count - 1 : end;
  229. int col = 0;
  230. for (int i = tcount; i > start; i--) {
  231. var rune = t [i];
  232. var s = Rune.ColumnWidth (rune);
  233. size += s;
  234. if (size >= dSize - width) {
  235. col = tcount - i + start;
  236. if (start == 0 || col == start || (currentColumn == t.Count && (currentColumn - col > width))) {
  237. col++;
  238. }
  239. break;
  240. }
  241. }
  242. return col;
  243. }
  244. }
  245. /// <summary>
  246. /// Multi-line text editing <see cref="View"/>
  247. /// </summary>
  248. /// <remarks>
  249. /// <para>
  250. /// <see cref="TextView"/> provides a multi-line text editor. Users interact
  251. /// with it with the standard Emacs commands for movement or the arrow
  252. /// keys.
  253. /// </para>
  254. /// <list type="table">
  255. /// <listheader>
  256. /// <term>Shortcut</term>
  257. /// <description>Action performed</description>
  258. /// </listheader>
  259. /// <item>
  260. /// <term>Left cursor, Control-b</term>
  261. /// <description>
  262. /// Moves the editing point left.
  263. /// </description>
  264. /// </item>
  265. /// <item>
  266. /// <term>Right cursor, Control-f</term>
  267. /// <description>
  268. /// Moves the editing point right.
  269. /// </description>
  270. /// </item>
  271. /// <item>
  272. /// <term>Alt-b</term>
  273. /// <description>
  274. /// Moves one word back.
  275. /// </description>
  276. /// </item>
  277. /// <item>
  278. /// <term>Alt-f</term>
  279. /// <description>
  280. /// Moves one word forward.
  281. /// </description>
  282. /// </item>
  283. /// <item>
  284. /// <term>Up cursor, Control-p</term>
  285. /// <description>
  286. /// Moves the editing point one line up.
  287. /// </description>
  288. /// </item>
  289. /// <item>
  290. /// <term>Down cursor, Control-n</term>
  291. /// <description>
  292. /// Moves the editing point one line down
  293. /// </description>
  294. /// </item>
  295. /// <item>
  296. /// <term>Home key, Control-a</term>
  297. /// <description>
  298. /// Moves the cursor to the beginning of the line.
  299. /// </description>
  300. /// </item>
  301. /// <item>
  302. /// <term>End key, Control-e</term>
  303. /// <description>
  304. /// Moves the cursor to the end of the line.
  305. /// </description>
  306. /// </item>
  307. /// <item>
  308. /// <term>Control-Home</term>
  309. /// <description>
  310. /// Scrolls to the first line and moves the cursor there.
  311. /// </description>
  312. /// </item>
  313. /// <item>
  314. /// <term>Control-End</term>
  315. /// <description>
  316. /// Scrolls to the last line and moves the cursor there.
  317. /// </description>
  318. /// </item>
  319. /// <item>
  320. /// <term>Delete, Control-d</term>
  321. /// <description>
  322. /// Deletes the character in front of the cursor.
  323. /// </description>
  324. /// </item>
  325. /// <item>
  326. /// <term>Backspace</term>
  327. /// <description>
  328. /// Deletes the character behind the cursor.
  329. /// </description>
  330. /// </item>
  331. /// <item>
  332. /// <term>Control-k</term>
  333. /// <description>
  334. /// Deletes the text until the end of the line and replaces the kill buffer
  335. /// with the deleted text. You can paste this text in a different place by
  336. /// using Control-y.
  337. /// </description>
  338. /// </item>
  339. /// <item>
  340. /// <term>Control-y</term>
  341. /// <description>
  342. /// Pastes the content of the kill ring into the current position.
  343. /// </description>
  344. /// </item>
  345. /// <item>
  346. /// <term>Alt-d</term>
  347. /// <description>
  348. /// Deletes the word above the cursor and adds it to the kill ring. You
  349. /// can paste the contents of the kill ring with Control-y.
  350. /// </description>
  351. /// </item>
  352. /// <item>
  353. /// <term>Control-q</term>
  354. /// <description>
  355. /// Quotes the next input character, to prevent the normal processing of
  356. /// key handling to take place.
  357. /// </description>
  358. /// </item>
  359. /// </list>
  360. /// </remarks>
  361. public class TextView : View {
  362. TextModel model = new TextModel ();
  363. int topRow;
  364. int leftColumn;
  365. int currentRow;
  366. int currentColumn;
  367. int selectionStartColumn, selectionStartRow;
  368. bool selecting;
  369. //bool used;
  370. /// <summary>
  371. /// Raised when the <see cref="Text"/> of the <see cref="TextView"/> changes.
  372. /// </summary>
  373. public event Action TextChanged;
  374. #if false
  375. /// <summary>
  376. /// Changed event, raised when the text has clicked.
  377. /// </summary>
  378. /// <remarks>
  379. /// Client code can hook up to this event, it is
  380. /// raised when the text in the entry changes.
  381. /// </remarks>
  382. public Action Changed;
  383. #endif
  384. /// <summary>
  385. /// Initializes a <see cref="TextView"/> on the specified area, with absolute position and size.
  386. /// </summary>
  387. /// <remarks>
  388. /// </remarks>
  389. public TextView (Rect frame) : base (frame)
  390. {
  391. CanFocus = true;
  392. }
  393. /// <summary>
  394. /// Initializes a <see cref="TextView"/> on the specified area,
  395. /// with dimensions controlled with the X, Y, Width and Height properties.
  396. /// </summary>
  397. public TextView () : base ()
  398. {
  399. CanFocus = true;
  400. }
  401. void ResetPosition ()
  402. {
  403. topRow = leftColumn = currentRow = currentColumn = 0;
  404. }
  405. /// <summary>
  406. /// Sets or gets the text in the <see cref="TextView"/>.
  407. /// </summary>
  408. /// <remarks>
  409. /// </remarks>
  410. public override ustring Text {
  411. get {
  412. return model.ToString ();
  413. }
  414. set {
  415. ResetPosition ();
  416. model.LoadString (value);
  417. TextChanged?.Invoke ();
  418. SetNeedsDisplay ();
  419. }
  420. }
  421. ///<inheritdoc/>
  422. public override Rect Frame {
  423. get => base.Frame;
  424. set {
  425. base.Frame = value;
  426. Adjust ();
  427. }
  428. }
  429. /// <summary>
  430. /// Gets or sets the top row.
  431. /// </summary>
  432. public int TopRow { get => topRow; set => topRow = Math.Max (Math.Min (value, Lines - 1), 0); }
  433. /// <summary>
  434. /// Gets or sets the left column.
  435. /// </summary>
  436. public int LeftColumn { get => leftColumn; set => leftColumn = Math.Max (Math.Min (value, Maxlength - 1), 0); }
  437. /// <summary>
  438. /// Gets the maximum visible length line.
  439. /// </summary>
  440. public int Maxlength => model.GetMaxVisibleLine (topRow, topRow + Frame.Height);
  441. /// <summary>
  442. /// Gets the number of lines.
  443. /// </summary>
  444. public int Lines => model.Count;
  445. /// <summary>
  446. /// Loads the contents of the file into the <see cref="TextView"/>.
  447. /// </summary>
  448. /// <returns><c>true</c>, if file was loaded, <c>false</c> otherwise.</returns>
  449. /// <param name="path">Path to the file to load.</param>
  450. public bool LoadFile (string path)
  451. {
  452. if (path == null)
  453. throw new ArgumentNullException (nameof (path));
  454. ResetPosition ();
  455. var res = model.LoadFile (path);
  456. SetNeedsDisplay ();
  457. return res;
  458. }
  459. /// <summary>
  460. /// Loads the contents of the stream into the <see cref="TextView"/>.
  461. /// </summary>
  462. /// <returns><c>true</c>, if stream was loaded, <c>false</c> otherwise.</returns>
  463. /// <param name="stream">Stream to load the contents from.</param>
  464. public void LoadStream (Stream stream)
  465. {
  466. if (stream == null)
  467. throw new ArgumentNullException (nameof (stream));
  468. ResetPosition ();
  469. model.LoadStream (stream);
  470. SetNeedsDisplay ();
  471. }
  472. /// <summary>
  473. /// Closes the contents of the stream into the <see cref="TextView"/>.
  474. /// </summary>
  475. /// <returns><c>true</c>, if stream was closed, <c>false</c> otherwise.</returns>
  476. public bool CloseFile ()
  477. {
  478. ResetPosition ();
  479. var res = model.CloseFile ();
  480. SetNeedsDisplay ();
  481. return res;
  482. }
  483. /// <summary>
  484. /// Gets the current cursor row.
  485. /// </summary>
  486. public int CurrentRow => currentRow;
  487. /// <summary>
  488. /// Gets the cursor column.
  489. /// </summary>
  490. /// <value>The cursor column.</value>
  491. public int CurrentColumn => currentColumn;
  492. /// <summary>
  493. /// Positions the cursor on the current row and column
  494. /// </summary>
  495. public override void PositionCursor ()
  496. {
  497. if (selecting) {
  498. var minRow = Math.Min (Math.Max (Math.Min (selectionStartRow, currentRow) - topRow, 0), Frame.Height);
  499. var maxRow = Math.Min (Math.Max (Math.Max (selectionStartRow, currentRow) - topRow, 0), Frame.Height);
  500. SetNeedsDisplay (new Rect (0, minRow, Frame.Width, maxRow));
  501. }
  502. var line = model.GetLine (currentRow);
  503. var retreat = 0;
  504. var col = 0;
  505. if (line.Count > 0) {
  506. retreat = Math.Max ((SpecialRune (line [Math.Max (CurrentColumn - leftColumn - 1, 0)])
  507. ? 1 : 0), 0);
  508. for (int idx = leftColumn < 0 ? 0 : leftColumn; idx < line.Count; idx++) {
  509. if (idx == CurrentColumn)
  510. break;
  511. var cols = Rune.ColumnWidth (line [idx]);
  512. col += cols - 1;
  513. }
  514. }
  515. var ccol = CurrentColumn - leftColumn - retreat + col;
  516. if (leftColumn <= CurrentColumn && ccol < Frame.Width
  517. && topRow <= CurrentRow && CurrentRow - topRow < Frame.Height) {
  518. Move (ccol, CurrentRow - topRow);
  519. }
  520. }
  521. void ClearRegion (int left, int top, int right, int bottom)
  522. {
  523. for (int row = top; row < bottom; row++) {
  524. Move (left, row);
  525. for (int col = left; col < right; col++)
  526. AddRune (col, row, ' ');
  527. }
  528. }
  529. void ColorNormal ()
  530. {
  531. Driver.SetAttribute (ColorScheme.Normal);
  532. }
  533. void ColorSelection ()
  534. {
  535. if (HasFocus)
  536. Driver.SetAttribute (ColorScheme.Focus);
  537. else
  538. Driver.SetAttribute (ColorScheme.Normal);
  539. }
  540. bool isReadOnly = false;
  541. /// <summary>
  542. /// Gets or sets whether the <see cref="TextView"/> is in read-only mode or not
  543. /// </summary>
  544. /// <value>Boolean value(Default false)</value>
  545. public bool ReadOnly {
  546. get => isReadOnly;
  547. set {
  548. isReadOnly = value;
  549. }
  550. }
  551. private CursorVisibility desiredCursorVisibility = CursorVisibility.Default;
  552. /// <summary>
  553. /// Get / Set the wished cursor when the field is focused
  554. /// </summary>
  555. public CursorVisibility DesiredCursorVisibility
  556. {
  557. get => desiredCursorVisibility;
  558. set {
  559. if (desiredCursorVisibility != value && HasFocus) {
  560. Application.Driver.SetCursorVisibility (value);
  561. }
  562. desiredCursorVisibility = value;
  563. }
  564. }
  565. ///<inheritdoc/>
  566. public override bool OnEnter (View view)
  567. {
  568. //TODO: Improve it by handling read only mode of the text field
  569. Application.Driver.SetCursorVisibility (DesiredCursorVisibility);
  570. return base.OnEnter (view);
  571. }
  572. // Returns an encoded region start..end (top 32 bits are the row, low32 the column)
  573. void GetEncodedRegionBounds (out long start, out long end)
  574. {
  575. long selection = ((long)(uint)selectionStartRow << 32) | (uint)selectionStartColumn;
  576. long point = ((long)(uint)currentRow << 32) | (uint)currentColumn;
  577. if (selection > point) {
  578. start = point;
  579. end = selection;
  580. } else {
  581. start = selection;
  582. end = point;
  583. }
  584. }
  585. bool PointInSelection (int col, int row)
  586. {
  587. long start, end;
  588. GetEncodedRegionBounds (out start, out end);
  589. var q = ((long)(uint)row << 32) | (uint)col;
  590. return q >= start && q <= end;
  591. }
  592. //
  593. // Returns a ustring with the text in the selected
  594. // region.
  595. //
  596. ustring GetRegion ()
  597. {
  598. long start, end;
  599. GetEncodedRegionBounds (out start, out end);
  600. int startRow = (int)(start >> 32);
  601. var maxrow = ((int)(end >> 32));
  602. int startCol = (int)(start & 0xffffffff);
  603. var endCol = (int)(end & 0xffffffff);
  604. var line = model.GetLine (startRow);
  605. if (startRow == maxrow)
  606. return StringFromRunes (line.GetRange (startCol, endCol));
  607. ustring res = StringFromRunes (line.GetRange (startCol, line.Count - startCol));
  608. for (int row = startRow + 1; row < maxrow; row++) {
  609. res = res + ustring.Make ((Rune)10) + StringFromRunes (model.GetLine (row));
  610. }
  611. line = model.GetLine (maxrow);
  612. res = res + ustring.Make ((Rune)10) + StringFromRunes (line.GetRange (0, endCol));
  613. return res;
  614. }
  615. //
  616. // Clears the contents of the selected region
  617. //
  618. void ClearRegion ()
  619. {
  620. long start, end;
  621. long currentEncoded = ((long)(uint)currentRow << 32) | (uint)currentColumn;
  622. GetEncodedRegionBounds (out start, out end);
  623. int startRow = (int)(start >> 32);
  624. var maxrow = ((int)(end >> 32));
  625. int startCol = (int)(start & 0xffffffff);
  626. var endCol = (int)(end & 0xffffffff);
  627. var line = model.GetLine (startRow);
  628. if (startRow == maxrow) {
  629. line.RemoveRange (startCol, endCol - startCol);
  630. currentColumn = startCol;
  631. SetNeedsDisplay (new Rect (0, startRow - topRow, Frame.Width, startRow - topRow + 1));
  632. return;
  633. }
  634. line.RemoveRange (startCol, line.Count - startCol);
  635. var line2 = model.GetLine (maxrow);
  636. line.AddRange (line2.Skip (endCol));
  637. for (int row = startRow + 1; row <= maxrow; row++) {
  638. model.RemoveLine (startRow + 1);
  639. }
  640. if (currentEncoded == end) {
  641. currentRow -= maxrow - (startRow);
  642. }
  643. currentColumn = startCol;
  644. SetNeedsDisplay ();
  645. }
  646. ///<inheritdoc/>
  647. public override void Redraw (Rect bounds)
  648. {
  649. ColorNormal ();
  650. int bottom = bounds.Bottom;
  651. int right = bounds.Right;
  652. for (int row = bounds.Top; row < bottom; row++) {
  653. int textLine = topRow + row;
  654. if (textLine >= model.Count) {
  655. ColorNormal ();
  656. ClearRegion (bounds.Left, row, bounds.Right, row + 1);
  657. continue;
  658. }
  659. var line = model.GetLine (textLine);
  660. int lineRuneCount = line.Count;
  661. if (line.Count < bounds.Left) {
  662. ClearRegion (bounds.Left, row, bounds.Right, row + 1);
  663. continue;
  664. }
  665. Move (bounds.Left, row);
  666. var col = 0;
  667. for (int idx = bounds.Left; idx < right; idx++) {
  668. var lineCol = leftColumn + idx;
  669. var rune = lineCol >= lineRuneCount ? ' ' : line [lineCol];
  670. var cols = Rune.ColumnWidth (rune);
  671. if (selecting && PointInSelection (idx, row)) {
  672. ColorSelection ();
  673. } else {
  674. ColorNormal ();
  675. }
  676. if (!SpecialRune (rune)) {
  677. AddRune (col, row, rune);
  678. }
  679. col = TextModel.SetCol (col, bounds.Right, cols);
  680. }
  681. }
  682. PositionCursor ();
  683. }
  684. bool SpecialRune (Rune rune)
  685. {
  686. switch (rune) {
  687. case (uint)Key.Enter:
  688. case 0xd:
  689. return true;
  690. default:
  691. return false; }
  692. }
  693. ///<inheritdoc/>
  694. public override bool CanFocus {
  695. get => base.CanFocus;
  696. set { base.CanFocus = value; }
  697. }
  698. void SetClipboard (ustring text)
  699. {
  700. Clipboard.Contents = text;
  701. }
  702. void AppendClipboard (ustring text)
  703. {
  704. Clipboard.Contents = Clipboard.Contents + text;
  705. }
  706. void Insert (Rune rune)
  707. {
  708. var line = GetCurrentLine ();
  709. line.Insert (currentColumn, rune);
  710. var prow = currentRow - topRow;
  711. SetNeedsDisplay (new Rect (0, prow, Frame.Width, prow + 1));
  712. }
  713. ustring StringFromRunes (List<Rune> runes)
  714. {
  715. if (runes == null)
  716. throw new ArgumentNullException (nameof (runes));
  717. int size = 0;
  718. foreach (var rune in runes) {
  719. size += Utf8.RuneLen (rune);
  720. }
  721. var encoded = new byte [size];
  722. int offset = 0;
  723. foreach (var rune in runes) {
  724. offset += Utf8.EncodeRune (rune, encoded, offset);
  725. }
  726. return ustring.Make (encoded);
  727. }
  728. List<Rune> GetCurrentLine () => model.GetLine (currentRow);
  729. void InsertText (ustring text)
  730. {
  731. if (ustring.IsNullOrEmpty (text)) {
  732. return;
  733. }
  734. var lines = TextModel.StringToRunes (text);
  735. if (lines.Count == 0) {
  736. return;
  737. }
  738. var line = GetCurrentLine ();
  739. // Optimize single line
  740. if (lines.Count == 1) {
  741. line.InsertRange (currentColumn, lines [0]);
  742. currentColumn += lines [0].Count;
  743. if (currentColumn - leftColumn > Frame.Width) {
  744. leftColumn = currentColumn - Frame.Width + 1;
  745. }
  746. SetNeedsDisplay (new Rect (0, currentRow - topRow, Frame.Width, currentRow - topRow + 1));
  747. return;
  748. }
  749. // Keep a copy of the rest of the line
  750. var restCount = line.Count - currentColumn;
  751. var rest = line.GetRange (currentColumn, restCount);
  752. line.RemoveRange (currentColumn, restCount);
  753. // First line is inserted at the current location, the rest is appended
  754. line.InsertRange (currentColumn, lines [0]);
  755. for (int i = 1; i < lines.Count; i++) {
  756. model.AddLine (currentRow + i, lines [i]);
  757. }
  758. var last = model.GetLine (currentRow + lines.Count - 1);
  759. var lastp = last.Count;
  760. last.InsertRange (last.Count, rest);
  761. // Now adjust column and row positions
  762. currentRow += lines.Count - 1;
  763. currentColumn = lastp;
  764. Adjust ();
  765. }
  766. // The column we are tracking, or -1 if we are not tracking any column
  767. int columnTrack = -1;
  768. // Tries to snap the cursor to the tracking column
  769. void TrackColumn ()
  770. {
  771. // Now track the column
  772. var line = GetCurrentLine ();
  773. if (line.Count < columnTrack)
  774. currentColumn = line.Count;
  775. else if (columnTrack != -1)
  776. currentColumn = columnTrack;
  777. else if (currentColumn > line.Count)
  778. currentColumn = line.Count;
  779. Adjust ();
  780. }
  781. void Adjust ()
  782. {
  783. var offB = OffSetBackground ();
  784. var line = GetCurrentLine ();
  785. bool need = false;
  786. if (currentColumn < leftColumn) {
  787. leftColumn = currentColumn;
  788. need = true;
  789. } else if (currentColumn - leftColumn > Frame.Width + offB.width ||
  790. TextModel.DisplaySize (line, leftColumn, currentColumn).size >= Frame.Width + offB.width) {
  791. leftColumn = Math.Max (TextModel.CalculateLeftColumn (line, leftColumn,
  792. currentColumn, Frame.Width - 1 + offB.width, currentColumn), 0);
  793. need = true;
  794. }
  795. if (currentRow < topRow) {
  796. topRow = currentRow;
  797. need = true;
  798. } else if (currentRow - topRow >= Frame.Height + offB.height) {
  799. topRow = Math.Min (Math.Max (currentRow - Frame.Height + 1, 0), currentRow);
  800. need = true;
  801. }
  802. if (need) {
  803. SetNeedsDisplay ();
  804. } else {
  805. PositionCursor ();
  806. }
  807. }
  808. (int width, int height) OffSetBackground ()
  809. {
  810. int w = 0;
  811. int h = 0;
  812. if (SuperView?.Frame.Right - Frame.Right < 0) {
  813. w = SuperView.Frame.Right - Frame.Right - 1;
  814. }
  815. if (SuperView?.Frame.Bottom - Frame.Bottom < 0) {
  816. h = SuperView.Frame.Bottom - Frame.Bottom - 1;
  817. }
  818. return (w, h);
  819. }
  820. /// <summary>
  821. /// Will scroll the <see cref="TextView"/> to display the specified row at the top if <paramref name="isRow"/> is true or
  822. /// will scroll the <see cref="TextView"/> to display the specified column at the left if <paramref name="isRow"/> is false.
  823. /// </summary>
  824. /// <param name="idx">Row that should be displayed at the top or Column that should be displayed at the left,
  825. /// if the value is negative it will be reset to zero</param>
  826. /// <param name="isRow">If true (default) the <paramref name="idx"/> is a row, column otherwise.</param>
  827. public void ScrollTo (int idx, bool isRow = true)
  828. {
  829. if (idx < 0) {
  830. idx = 0;
  831. }
  832. if (isRow) {
  833. topRow = Math.Max (idx > model.Count - 1 ? model.Count - 1 : idx, 0);
  834. } else {
  835. var maxlength = model.GetMaxVisibleLine (topRow, topRow + Frame.Height);
  836. leftColumn = Math.Max (idx > maxlength - 1 ? maxlength - 1 : idx, 0);
  837. }
  838. SetNeedsDisplay ();
  839. }
  840. bool lastWasKill;
  841. ///<inheritdoc/>
  842. public override bool ProcessKey (KeyEvent kb)
  843. {
  844. int restCount;
  845. List<Rune> rest;
  846. // Handle some state here - whether the last command was a kill
  847. // operation and the column tracking (up/down)
  848. switch (kb.Key) {
  849. case Key.N | Key.CtrlMask:
  850. case Key.CursorDown:
  851. case Key.P | Key.CtrlMask:
  852. case Key.CursorUp:
  853. lastWasKill = false;
  854. break;
  855. case Key.K | Key.CtrlMask:
  856. break;
  857. default:
  858. lastWasKill = false;
  859. columnTrack = -1;
  860. break;
  861. }
  862. // Dispatch the command.
  863. switch (kb.Key) {
  864. case Key.PageDown:
  865. case Key.V | Key.CtrlMask:
  866. int nPageDnShift = Frame.Height - 1;
  867. if (currentRow < model.Count) {
  868. if (columnTrack == -1)
  869. columnTrack = currentColumn;
  870. currentRow = (currentRow + nPageDnShift) > model.Count ? model.Count : currentRow + nPageDnShift;
  871. if (topRow < currentRow - nPageDnShift) {
  872. topRow = currentRow >= model.Count ? currentRow - nPageDnShift : topRow + nPageDnShift;
  873. SetNeedsDisplay ();
  874. }
  875. TrackColumn ();
  876. PositionCursor ();
  877. }
  878. break;
  879. case Key.PageUp:
  880. case ((int)'V' + Key.AltMask):
  881. int nPageUpShift = Frame.Height - 1;
  882. if (currentRow > 0) {
  883. if (columnTrack == -1)
  884. columnTrack = currentColumn;
  885. currentRow = currentRow - nPageUpShift < 0 ? 0 : currentRow - nPageUpShift;
  886. if (currentRow < topRow) {
  887. topRow = topRow - nPageUpShift < 0 ? 0 : topRow - nPageUpShift;
  888. SetNeedsDisplay ();
  889. }
  890. TrackColumn ();
  891. PositionCursor ();
  892. }
  893. break;
  894. case Key.N | Key.CtrlMask:
  895. case Key.CursorDown:
  896. MoveDown ();
  897. break;
  898. case Key.P | Key.CtrlMask:
  899. case Key.CursorUp:
  900. MoveUp ();
  901. break;
  902. case Key.F | Key.CtrlMask:
  903. case Key.CursorRight:
  904. var currentLine = GetCurrentLine ();
  905. if (currentColumn < currentLine.Count) {
  906. currentColumn++;
  907. } else {
  908. if (currentRow + 1 < model.Count) {
  909. currentRow++;
  910. currentColumn = 0;
  911. if (currentRow >= topRow + Frame.Height) {
  912. topRow++;
  913. }
  914. }
  915. }
  916. Adjust ();
  917. break;
  918. case Key.B | Key.CtrlMask:
  919. case Key.CursorLeft:
  920. if (currentColumn > 0) {
  921. currentColumn--;
  922. } else {
  923. if (currentRow > 0) {
  924. currentRow--;
  925. if (currentRow < topRow) {
  926. topRow--;
  927. }
  928. currentLine = GetCurrentLine ();
  929. currentColumn = currentLine.Count;
  930. }
  931. }
  932. Adjust ();
  933. break;
  934. case Key.Delete:
  935. case Key.Backspace:
  936. if (isReadOnly)
  937. break;
  938. if (currentColumn > 0) {
  939. // Delete backwards
  940. currentLine = GetCurrentLine ();
  941. currentLine.RemoveAt (currentColumn - 1);
  942. currentColumn--;
  943. if (currentColumn < leftColumn) {
  944. leftColumn--;
  945. SetNeedsDisplay ();
  946. } else
  947. SetNeedsDisplay (new Rect (0, currentRow - topRow, 1, Frame.Width));
  948. } else {
  949. // Merges the current line with the previous one.
  950. if (currentRow == 0)
  951. return true;
  952. var prowIdx = currentRow - 1;
  953. var prevRow = model.GetLine (prowIdx);
  954. var prevCount = prevRow.Count;
  955. model.GetLine (prowIdx).AddRange (GetCurrentLine ());
  956. model.RemoveLine (currentRow);
  957. currentRow--;
  958. currentColumn = prevCount;
  959. Adjust ();
  960. }
  961. break;
  962. // Home, C-A
  963. case Key.Home:
  964. case Key.A | Key.CtrlMask:
  965. currentColumn = 0;
  966. Adjust ();
  967. break;
  968. case Key.DeleteChar:
  969. case Key.D | Key.CtrlMask: // Delete
  970. if (isReadOnly)
  971. break;
  972. currentLine = GetCurrentLine ();
  973. if (currentColumn == currentLine.Count) {
  974. if (currentRow + 1 == model.Count)
  975. break;
  976. var nextLine = model.GetLine (currentRow + 1);
  977. currentLine.AddRange (nextLine);
  978. model.RemoveLine (currentRow + 1);
  979. var sr = currentRow - topRow;
  980. SetNeedsDisplay (new Rect (0, sr, Frame.Width, sr + 1));
  981. } else {
  982. currentLine.RemoveAt (currentColumn);
  983. var r = currentRow - topRow;
  984. SetNeedsDisplay (new Rect (currentColumn - leftColumn, r, Frame.Width, r + 1));
  985. }
  986. break;
  987. case Key.End:
  988. case Key.E | Key.CtrlMask: // End
  989. currentLine = GetCurrentLine ();
  990. currentColumn = currentLine.Count;
  991. int pcol = leftColumn;
  992. Adjust ();
  993. break;
  994. case Key.K | Key.CtrlMask: // kill-to-end
  995. if (isReadOnly)
  996. break;
  997. currentLine = GetCurrentLine ();
  998. if (currentLine.Count == 0) {
  999. model.RemoveLine (currentRow);
  1000. var val = ustring.Make ((Rune)'\n');
  1001. if (lastWasKill)
  1002. AppendClipboard (val);
  1003. else
  1004. SetClipboard (val);
  1005. } else {
  1006. restCount = currentLine.Count - currentColumn;
  1007. rest = currentLine.GetRange (currentColumn, restCount);
  1008. var val = StringFromRunes (rest);
  1009. if (lastWasKill)
  1010. AppendClipboard (val);
  1011. else
  1012. SetClipboard (val);
  1013. currentLine.RemoveRange (currentColumn, restCount);
  1014. }
  1015. SetNeedsDisplay (new Rect (0, currentRow - topRow, Frame.Width, Frame.Height));
  1016. lastWasKill = true;
  1017. break;
  1018. case Key.Y | Key.CtrlMask: // Control-y, yank
  1019. if (isReadOnly)
  1020. break;
  1021. InsertText (Clipboard.Contents);
  1022. selecting = false;
  1023. break;
  1024. case Key.Space | Key.CtrlMask:
  1025. selecting = true;
  1026. selectionStartColumn = currentColumn;
  1027. selectionStartRow = currentRow;
  1028. break;
  1029. case ((int)'W' + Key.AltMask):
  1030. case Key.W | Key.CtrlMask:
  1031. SetClipboard (GetRegion ());
  1032. if (!isReadOnly)
  1033. ClearRegion ();
  1034. selecting = false;
  1035. break;
  1036. case Key.CtrlMask | Key.CursorLeft:
  1037. case (Key)((int)'B' + Key.AltMask):
  1038. var newPos = WordBackward (currentColumn, currentRow);
  1039. if (newPos.HasValue) {
  1040. currentColumn = newPos.Value.col;
  1041. currentRow = newPos.Value.row;
  1042. }
  1043. Adjust ();
  1044. break;
  1045. case Key.CtrlMask | Key.CursorRight:
  1046. case (Key)((int)'F' + Key.AltMask):
  1047. newPos = WordForward (currentColumn, currentRow);
  1048. if (newPos.HasValue) {
  1049. currentColumn = newPos.Value.col;
  1050. currentRow = newPos.Value.row;
  1051. }
  1052. Adjust ();
  1053. break;
  1054. case Key.Enter:
  1055. if (isReadOnly)
  1056. break;
  1057. currentLine = GetCurrentLine ();
  1058. restCount = currentLine.Count - currentColumn;
  1059. rest = currentLine.GetRange (currentColumn, restCount);
  1060. currentLine.RemoveRange (currentColumn, restCount);
  1061. model.AddLine (currentRow + 1, rest);
  1062. currentRow++;
  1063. bool fullNeedsDisplay = false;
  1064. if (currentRow >= topRow + Frame.Height) {
  1065. topRow++;
  1066. fullNeedsDisplay = true;
  1067. }
  1068. currentColumn = 0;
  1069. if (currentColumn < leftColumn) {
  1070. fullNeedsDisplay = true;
  1071. leftColumn = 0;
  1072. }
  1073. if (fullNeedsDisplay)
  1074. SetNeedsDisplay ();
  1075. else
  1076. SetNeedsDisplay (new Rect (0, currentRow - topRow, 2, Frame.Height));
  1077. break;
  1078. case Key.CtrlMask | Key.End:
  1079. MoveEnd ();
  1080. break;
  1081. case Key.CtrlMask | Key.Home:
  1082. MoveHome ();
  1083. break;
  1084. default:
  1085. // Ignore control characters and other special keys
  1086. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  1087. return false;
  1088. //So that special keys like tab can be processed
  1089. if (isReadOnly)
  1090. return true;
  1091. Insert ((uint)kb.Key);
  1092. currentColumn++;
  1093. if (currentColumn >= leftColumn + Frame.Width) {
  1094. leftColumn++;
  1095. SetNeedsDisplay ();
  1096. }
  1097. PositionCursor ();
  1098. return true;
  1099. }
  1100. return true;
  1101. }
  1102. void MoveUp ()
  1103. {
  1104. if (currentRow > 0) {
  1105. if (columnTrack == -1) {
  1106. columnTrack = currentColumn;
  1107. }
  1108. currentRow--;
  1109. if (currentRow < topRow) {
  1110. topRow--;
  1111. SetNeedsDisplay ();
  1112. }
  1113. TrackColumn ();
  1114. PositionCursor ();
  1115. }
  1116. }
  1117. void MoveDown ()
  1118. {
  1119. if (currentRow + 1 < model.Count) {
  1120. if (columnTrack == -1) {
  1121. columnTrack = currentColumn;
  1122. }
  1123. currentRow++;
  1124. if (currentRow >= topRow + Frame.Height) {
  1125. topRow++;
  1126. SetNeedsDisplay ();
  1127. }
  1128. TrackColumn ();
  1129. PositionCursor ();
  1130. } else if (currentRow > Frame.Height) {
  1131. Adjust ();
  1132. }
  1133. }
  1134. IEnumerable<(int col, int row, Rune rune)> ForwardIterator (int col, int row)
  1135. {
  1136. if (col < 0 || row < 0)
  1137. yield break;
  1138. if (row >= model.Count)
  1139. yield break;
  1140. var line = GetCurrentLine ();
  1141. if (col >= line.Count)
  1142. yield break;
  1143. while (row < model.Count) {
  1144. for (int c = col; c < line.Count; c++) {
  1145. yield return (c, row, line [c]);
  1146. }
  1147. col = 0;
  1148. row++;
  1149. line = GetCurrentLine ();
  1150. }
  1151. }
  1152. Rune RuneAt (int col, int row) => model.GetLine (row) [col];
  1153. /// <summary>
  1154. /// Will scroll the <see cref="TextView"/> to the last line and position the cursor there.
  1155. /// </summary>
  1156. public void MoveEnd ()
  1157. {
  1158. currentRow = model.Count - 1;
  1159. TrackColumn ();
  1160. PositionCursor ();
  1161. }
  1162. /// <summary>
  1163. /// Will scroll the <see cref="TextView"/> to the first line and position the cursor there.
  1164. /// </summary>
  1165. public void MoveHome ()
  1166. {
  1167. currentRow = 0;
  1168. TrackColumn ();
  1169. PositionCursor ();
  1170. }
  1171. bool MoveNext (ref int col, ref int row, out Rune rune)
  1172. {
  1173. var line = model.GetLine (row);
  1174. if (col + 1 < line.Count) {
  1175. col++;
  1176. rune = line [col];
  1177. return true;
  1178. }
  1179. while (row + 1 < model.Count) {
  1180. col = 0;
  1181. row++;
  1182. line = model.GetLine (row);
  1183. if (line.Count > 0) {
  1184. rune = line [0];
  1185. return true;
  1186. }
  1187. }
  1188. rune = 0;
  1189. return false;
  1190. }
  1191. bool MovePrev (ref int col, ref int row, out Rune rune)
  1192. {
  1193. var line = model.GetLine (row);
  1194. if (col > 0) {
  1195. col--;
  1196. rune = line [col];
  1197. return true;
  1198. }
  1199. if (row == 0) {
  1200. rune = 0;
  1201. return false;
  1202. }
  1203. while (row > 0) {
  1204. row--;
  1205. line = model.GetLine (row);
  1206. col = line.Count - 1;
  1207. if (col >= 0) {
  1208. rune = line [col];
  1209. return true;
  1210. }
  1211. }
  1212. rune = 0;
  1213. return false;
  1214. }
  1215. (int col, int row)? WordForward (int fromCol, int fromRow)
  1216. {
  1217. var col = fromCol;
  1218. var row = fromRow;
  1219. try {
  1220. var rune = RuneAt (col, row);
  1221. var srow = row;
  1222. if (Rune.IsPunctuation (rune) || Rune.IsWhiteSpace (rune)) {
  1223. while (MoveNext (ref col, ref row, out rune)) {
  1224. if (Rune.IsLetterOrDigit (rune))
  1225. break;
  1226. }
  1227. while (MoveNext (ref col, ref row, out rune)) {
  1228. if (!Rune.IsLetterOrDigit (rune))
  1229. break;
  1230. }
  1231. } else {
  1232. while (MoveNext (ref col, ref row, out rune)) {
  1233. if (!Rune.IsLetterOrDigit (rune))
  1234. break;
  1235. }
  1236. }
  1237. if (fromCol != col || fromRow != row)
  1238. return (col, row);
  1239. return null;
  1240. } catch (Exception) {
  1241. return null;
  1242. }
  1243. }
  1244. (int col, int row)? WordBackward (int fromCol, int fromRow)
  1245. {
  1246. if (fromRow == 0 && fromCol == 0)
  1247. return null;
  1248. var col = fromCol;
  1249. var row = fromRow;
  1250. try {
  1251. var rune = RuneAt (col, row);
  1252. if (Rune.IsPunctuation (rune) || Rune.IsSymbol (rune) || Rune.IsWhiteSpace (rune)) {
  1253. while (MovePrev (ref col, ref row, out rune)) {
  1254. if (Rune.IsLetterOrDigit (rune))
  1255. break;
  1256. }
  1257. while (MovePrev (ref col, ref row, out rune)) {
  1258. if (!Rune.IsLetterOrDigit (rune))
  1259. break;
  1260. }
  1261. } else {
  1262. while (MovePrev (ref col, ref row, out rune)) {
  1263. if (!Rune.IsLetterOrDigit (rune))
  1264. break;
  1265. }
  1266. }
  1267. if (fromCol != col || fromRow != row)
  1268. return (col, row);
  1269. return null;
  1270. } catch (Exception) {
  1271. return null;
  1272. }
  1273. }
  1274. ///<inheritdoc/>
  1275. public override bool MouseEvent (MouseEvent ev)
  1276. {
  1277. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked) &&
  1278. !ev.Flags.HasFlag (MouseFlags.WheeledDown) && !ev.Flags.HasFlag (MouseFlags.WheeledUp)) {
  1279. return false;
  1280. }
  1281. if (!CanFocus) {
  1282. return true;
  1283. }
  1284. if (!HasFocus) {
  1285. SetFocus ();
  1286. }
  1287. if (ev.Flags == MouseFlags.Button1Clicked) {
  1288. if (model.Count > 0) {
  1289. var maxCursorPositionableLine = Math.Max ((model.Count - 1) - topRow, 0);
  1290. if (ev.Y > maxCursorPositionableLine) {
  1291. currentRow = maxCursorPositionableLine;
  1292. } else {
  1293. currentRow = ev.Y + topRow;
  1294. }
  1295. var r = GetCurrentLine ();
  1296. var idx = TextModel.GetColFromX (r, leftColumn, ev.X);
  1297. if (idx - leftColumn >= r.Count) {
  1298. currentColumn = r.Count - leftColumn;
  1299. } else {
  1300. currentColumn = idx + leftColumn;
  1301. }
  1302. }
  1303. PositionCursor ();
  1304. lastWasKill = false;
  1305. columnTrack = currentColumn;
  1306. } else if (ev.Flags == MouseFlags.WheeledDown) {
  1307. lastWasKill = false;
  1308. columnTrack = currentColumn;
  1309. ScrollTo (topRow + 1);
  1310. } else if (ev.Flags == MouseFlags.WheeledUp) {
  1311. lastWasKill = false;
  1312. columnTrack = currentColumn;
  1313. ScrollTo (topRow - 1);
  1314. } else if (ev.Flags == MouseFlags.WheeledRight) {
  1315. lastWasKill = false;
  1316. columnTrack = currentColumn;
  1317. ScrollTo (leftColumn + 1, false);
  1318. } else if (ev.Flags == MouseFlags.WheeledLeft) {
  1319. lastWasKill = false;
  1320. columnTrack = currentColumn;
  1321. ScrollTo (leftColumn - 1, false);
  1322. }
  1323. return true;
  1324. }
  1325. }
  1326. }