TextView.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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 wishedCursorVisibility = CursorVisibility.BoxFix;
  552. /// <summary>
  553. /// Get / Set the wished cursor when the field is focused
  554. /// </summary>
  555. public CursorVisibility WishedCursorVisibility
  556. {
  557. get => wishedCursorVisibility;
  558. set {
  559. if (wishedCursorVisibility != value && HasFocus)
  560. {
  561. Application.Driver.SetCursorVisibility (value);
  562. }
  563. wishedCursorVisibility = value;
  564. }
  565. }
  566. ///<inheritdoc/>
  567. public override bool OnEnter (View view)
  568. {
  569. //TODO: Improve it by handling read only mode of the text field
  570. Application.Driver.SetCursorVisibility (WishedCursorVisibility);
  571. return base.OnEnter (view);
  572. }
  573. // Returns an encoded region start..end (top 32 bits are the row, low32 the column)
  574. void GetEncodedRegionBounds (out long start, out long end)
  575. {
  576. long selection = ((long)(uint)selectionStartRow << 32) | (uint)selectionStartColumn;
  577. long point = ((long)(uint)currentRow << 32) | (uint)currentColumn;
  578. if (selection > point) {
  579. start = point;
  580. end = selection;
  581. } else {
  582. start = selection;
  583. end = point;
  584. }
  585. }
  586. bool PointInSelection (int col, int row)
  587. {
  588. long start, end;
  589. GetEncodedRegionBounds (out start, out end);
  590. var q = ((long)(uint)row << 32) | (uint)col;
  591. return q >= start && q <= end;
  592. }
  593. //
  594. // Returns a ustring with the text in the selected
  595. // region.
  596. //
  597. ustring GetRegion ()
  598. {
  599. long start, end;
  600. GetEncodedRegionBounds (out start, out end);
  601. int startRow = (int)(start >> 32);
  602. var maxrow = ((int)(end >> 32));
  603. int startCol = (int)(start & 0xffffffff);
  604. var endCol = (int)(end & 0xffffffff);
  605. var line = model.GetLine (startRow);
  606. if (startRow == maxrow)
  607. return StringFromRunes (line.GetRange (startCol, endCol));
  608. ustring res = StringFromRunes (line.GetRange (startCol, line.Count - startCol));
  609. for (int row = startRow + 1; row < maxrow; row++) {
  610. res = res + ustring.Make ((Rune)10) + StringFromRunes (model.GetLine (row));
  611. }
  612. line = model.GetLine (maxrow);
  613. res = res + ustring.Make ((Rune)10) + StringFromRunes (line.GetRange (0, endCol));
  614. return res;
  615. }
  616. //
  617. // Clears the contents of the selected region
  618. //
  619. void ClearRegion ()
  620. {
  621. long start, end;
  622. long currentEncoded = ((long)(uint)currentRow << 32) | (uint)currentColumn;
  623. GetEncodedRegionBounds (out start, out end);
  624. int startRow = (int)(start >> 32);
  625. var maxrow = ((int)(end >> 32));
  626. int startCol = (int)(start & 0xffffffff);
  627. var endCol = (int)(end & 0xffffffff);
  628. var line = model.GetLine (startRow);
  629. if (startRow == maxrow) {
  630. line.RemoveRange (startCol, endCol - startCol);
  631. currentColumn = startCol;
  632. SetNeedsDisplay (new Rect (0, startRow - topRow, Frame.Width, startRow - topRow + 1));
  633. return;
  634. }
  635. line.RemoveRange (startCol, line.Count - startCol);
  636. var line2 = model.GetLine (maxrow);
  637. line.AddRange (line2.Skip (endCol));
  638. for (int row = startRow + 1; row <= maxrow; row++) {
  639. model.RemoveLine (startRow + 1);
  640. }
  641. if (currentEncoded == end) {
  642. currentRow -= maxrow - (startRow);
  643. }
  644. currentColumn = startCol;
  645. SetNeedsDisplay ();
  646. }
  647. ///<inheritdoc/>
  648. public override void Redraw (Rect bounds)
  649. {
  650. ColorNormal ();
  651. int bottom = bounds.Bottom;
  652. int right = bounds.Right;
  653. for (int row = bounds.Top; row < bottom; row++) {
  654. int textLine = topRow + row;
  655. if (textLine >= model.Count) {
  656. ColorNormal ();
  657. ClearRegion (bounds.Left, row, bounds.Right, row + 1);
  658. continue;
  659. }
  660. var line = model.GetLine (textLine);
  661. int lineRuneCount = line.Count;
  662. if (line.Count < bounds.Left) {
  663. ClearRegion (bounds.Left, row, bounds.Right, row + 1);
  664. continue;
  665. }
  666. Move (bounds.Left, row);
  667. var col = 0;
  668. for (int idx = bounds.Left; idx < right; idx++) {
  669. var lineCol = leftColumn + idx;
  670. var rune = lineCol >= lineRuneCount ? ' ' : line [lineCol];
  671. var cols = Rune.ColumnWidth (rune);
  672. if (selecting && PointInSelection (idx, row)) {
  673. ColorSelection ();
  674. } else {
  675. ColorNormal ();
  676. }
  677. if (!SpecialRune (rune)) {
  678. AddRune (col, row, rune);
  679. }
  680. col = TextModel.SetCol (col, bounds.Right, cols);
  681. }
  682. }
  683. PositionCursor ();
  684. }
  685. bool SpecialRune (Rune rune)
  686. {
  687. switch (rune) {
  688. case (uint)Key.Enter:
  689. case 0xd:
  690. return true;
  691. default:
  692. return false; }
  693. }
  694. ///<inheritdoc/>
  695. public override bool CanFocus {
  696. get => base.CanFocus;
  697. set { base.CanFocus = value; }
  698. }
  699. void SetClipboard (ustring text)
  700. {
  701. Clipboard.Contents = text;
  702. }
  703. void AppendClipboard (ustring text)
  704. {
  705. Clipboard.Contents = Clipboard.Contents + text;
  706. }
  707. void Insert (Rune rune)
  708. {
  709. var line = GetCurrentLine ();
  710. line.Insert (currentColumn, rune);
  711. var prow = currentRow - topRow;
  712. SetNeedsDisplay (new Rect (0, prow, Frame.Width, prow + 1));
  713. }
  714. ustring StringFromRunes (List<Rune> runes)
  715. {
  716. if (runes == null)
  717. throw new ArgumentNullException (nameof (runes));
  718. int size = 0;
  719. foreach (var rune in runes) {
  720. size += Utf8.RuneLen (rune);
  721. }
  722. var encoded = new byte [size];
  723. int offset = 0;
  724. foreach (var rune in runes) {
  725. offset += Utf8.EncodeRune (rune, encoded, offset);
  726. }
  727. return ustring.Make (encoded);
  728. }
  729. List<Rune> GetCurrentLine () => model.GetLine (currentRow);
  730. void InsertText (ustring text)
  731. {
  732. if (ustring.IsNullOrEmpty (text)) {
  733. return;
  734. }
  735. var lines = TextModel.StringToRunes (text);
  736. if (lines.Count == 0) {
  737. return;
  738. }
  739. var line = GetCurrentLine ();
  740. // Optimize single line
  741. if (lines.Count == 1) {
  742. line.InsertRange (currentColumn, lines [0]);
  743. currentColumn += lines [0].Count;
  744. if (currentColumn - leftColumn > Frame.Width) {
  745. leftColumn = currentColumn - Frame.Width + 1;
  746. }
  747. SetNeedsDisplay (new Rect (0, currentRow - topRow, Frame.Width, currentRow - topRow + 1));
  748. return;
  749. }
  750. // Keep a copy of the rest of the line
  751. var restCount = line.Count - currentColumn;
  752. var rest = line.GetRange (currentColumn, restCount);
  753. line.RemoveRange (currentColumn, restCount);
  754. // First line is inserted at the current location, the rest is appended
  755. line.InsertRange (currentColumn, lines [0]);
  756. for (int i = 1; i < lines.Count; i++) {
  757. model.AddLine (currentRow + i, lines [i]);
  758. }
  759. var last = model.GetLine (currentRow + lines.Count - 1);
  760. var lastp = last.Count;
  761. last.InsertRange (last.Count, rest);
  762. // Now adjust column and row positions
  763. currentRow += lines.Count - 1;
  764. currentColumn = lastp;
  765. Adjust ();
  766. }
  767. // The column we are tracking, or -1 if we are not tracking any column
  768. int columnTrack = -1;
  769. // Tries to snap the cursor to the tracking column
  770. void TrackColumn ()
  771. {
  772. // Now track the column
  773. var line = GetCurrentLine ();
  774. if (line.Count < columnTrack)
  775. currentColumn = line.Count;
  776. else if (columnTrack != -1)
  777. currentColumn = columnTrack;
  778. else if (currentColumn > line.Count)
  779. currentColumn = line.Count;
  780. Adjust ();
  781. }
  782. void Adjust ()
  783. {
  784. var offB = OffSetBackground ();
  785. var line = GetCurrentLine ();
  786. bool need = false;
  787. if (currentColumn < leftColumn) {
  788. leftColumn = currentColumn;
  789. need = true;
  790. } else if (currentColumn - leftColumn > Frame.Width + offB.width ||
  791. TextModel.DisplaySize (line, leftColumn, currentColumn).size >= Frame.Width + offB.width) {
  792. leftColumn = Math.Max (TextModel.CalculateLeftColumn (line, leftColumn,
  793. currentColumn, Frame.Width - 1 + offB.width, currentColumn), 0);
  794. need = true;
  795. }
  796. if (currentRow < topRow) {
  797. topRow = currentRow;
  798. need = true;
  799. } else if (currentRow - topRow >= Frame.Height + offB.height) {
  800. topRow = Math.Min (Math.Max (currentRow - Frame.Height + 1, 0), currentRow);
  801. need = true;
  802. }
  803. if (need) {
  804. SetNeedsDisplay ();
  805. } else {
  806. PositionCursor ();
  807. }
  808. }
  809. (int width, int height) OffSetBackground ()
  810. {
  811. int w = 0;
  812. int h = 0;
  813. if (SuperView?.Frame.Right - Frame.Right < 0) {
  814. w = SuperView.Frame.Right - Frame.Right - 1;
  815. }
  816. if (SuperView?.Frame.Bottom - Frame.Bottom < 0) {
  817. h = SuperView.Frame.Bottom - Frame.Bottom - 1;
  818. }
  819. return (w, h);
  820. }
  821. /// <summary>
  822. /// Will scroll the <see cref="TextView"/> to display the specified row at the top if <paramref name="isRow"/> is true or
  823. /// will scroll the <see cref="TextView"/> to display the specified column at the left if <paramref name="isRow"/> is false.
  824. /// </summary>
  825. /// <param name="idx">Row that should be displayed at the top or Column that should be displayed at the left,
  826. /// if the value is negative it will be reset to zero</param>
  827. /// <param name="isRow">If true (default) the <paramref name="idx"/> is a row, column otherwise.</param>
  828. public void ScrollTo (int idx, bool isRow = true)
  829. {
  830. if (idx < 0) {
  831. idx = 0;
  832. }
  833. if (isRow) {
  834. topRow = Math.Max (idx > model.Count - 1 ? model.Count - 1 : idx, 0);
  835. } else {
  836. var maxlength = model.GetMaxVisibleLine (topRow, topRow + Frame.Height);
  837. leftColumn = Math.Max (idx > maxlength - 1 ? maxlength - 1 : idx, 0);
  838. }
  839. SetNeedsDisplay ();
  840. }
  841. bool lastWasKill;
  842. ///<inheritdoc/>
  843. public override bool ProcessKey (KeyEvent kb)
  844. {
  845. int restCount;
  846. List<Rune> rest;
  847. // Handle some state here - whether the last command was a kill
  848. // operation and the column tracking (up/down)
  849. switch (kb.Key) {
  850. case Key.N | Key.CtrlMask:
  851. case Key.CursorDown:
  852. case Key.P | Key.CtrlMask:
  853. case Key.CursorUp:
  854. lastWasKill = false;
  855. break;
  856. case Key.K | Key.CtrlMask:
  857. break;
  858. default:
  859. lastWasKill = false;
  860. columnTrack = -1;
  861. break;
  862. }
  863. // Dispatch the command.
  864. switch (kb.Key) {
  865. case Key.PageDown:
  866. case Key.V | Key.CtrlMask:
  867. int nPageDnShift = Frame.Height - 1;
  868. if (currentRow < model.Count) {
  869. if (columnTrack == -1)
  870. columnTrack = currentColumn;
  871. currentRow = (currentRow + nPageDnShift) > model.Count ? model.Count : currentRow + nPageDnShift;
  872. if (topRow < currentRow - nPageDnShift) {
  873. topRow = currentRow >= model.Count ? currentRow - nPageDnShift : topRow + nPageDnShift;
  874. SetNeedsDisplay ();
  875. }
  876. TrackColumn ();
  877. PositionCursor ();
  878. }
  879. break;
  880. case Key.PageUp:
  881. case ((int)'V' + Key.AltMask):
  882. int nPageUpShift = Frame.Height - 1;
  883. if (currentRow > 0) {
  884. if (columnTrack == -1)
  885. columnTrack = currentColumn;
  886. currentRow = currentRow - nPageUpShift < 0 ? 0 : currentRow - nPageUpShift;
  887. if (currentRow < topRow) {
  888. topRow = topRow - nPageUpShift < 0 ? 0 : topRow - nPageUpShift;
  889. SetNeedsDisplay ();
  890. }
  891. TrackColumn ();
  892. PositionCursor ();
  893. }
  894. break;
  895. case Key.N | Key.CtrlMask:
  896. case Key.CursorDown:
  897. MoveDown ();
  898. break;
  899. case Key.P | Key.CtrlMask:
  900. case Key.CursorUp:
  901. MoveUp ();
  902. break;
  903. case Key.F | Key.CtrlMask:
  904. case Key.CursorRight:
  905. var currentLine = GetCurrentLine ();
  906. if (currentColumn < currentLine.Count) {
  907. currentColumn++;
  908. } else {
  909. if (currentRow + 1 < model.Count) {
  910. currentRow++;
  911. currentColumn = 0;
  912. if (currentRow >= topRow + Frame.Height) {
  913. topRow++;
  914. }
  915. }
  916. }
  917. Adjust ();
  918. break;
  919. case Key.B | Key.CtrlMask:
  920. case Key.CursorLeft:
  921. if (currentColumn > 0) {
  922. currentColumn--;
  923. } else {
  924. if (currentRow > 0) {
  925. currentRow--;
  926. if (currentRow < topRow) {
  927. topRow--;
  928. }
  929. currentLine = GetCurrentLine ();
  930. currentColumn = currentLine.Count;
  931. }
  932. }
  933. Adjust ();
  934. break;
  935. case Key.Delete:
  936. case Key.Backspace:
  937. if (isReadOnly)
  938. break;
  939. if (currentColumn > 0) {
  940. // Delete backwards
  941. currentLine = GetCurrentLine ();
  942. currentLine.RemoveAt (currentColumn - 1);
  943. currentColumn--;
  944. if (currentColumn < leftColumn) {
  945. leftColumn--;
  946. SetNeedsDisplay ();
  947. } else
  948. SetNeedsDisplay (new Rect (0, currentRow - topRow, 1, Frame.Width));
  949. } else {
  950. // Merges the current line with the previous one.
  951. if (currentRow == 0)
  952. return true;
  953. var prowIdx = currentRow - 1;
  954. var prevRow = model.GetLine (prowIdx);
  955. var prevCount = prevRow.Count;
  956. model.GetLine (prowIdx).AddRange (GetCurrentLine ());
  957. model.RemoveLine (currentRow);
  958. currentRow--;
  959. currentColumn = prevCount;
  960. Adjust ();
  961. }
  962. break;
  963. // Home, C-A
  964. case Key.Home:
  965. case Key.A | Key.CtrlMask:
  966. currentColumn = 0;
  967. Adjust ();
  968. break;
  969. case Key.DeleteChar:
  970. case Key.D | Key.CtrlMask: // Delete
  971. if (isReadOnly)
  972. break;
  973. currentLine = GetCurrentLine ();
  974. if (currentColumn == currentLine.Count) {
  975. if (currentRow + 1 == model.Count)
  976. break;
  977. var nextLine = model.GetLine (currentRow + 1);
  978. currentLine.AddRange (nextLine);
  979. model.RemoveLine (currentRow + 1);
  980. var sr = currentRow - topRow;
  981. SetNeedsDisplay (new Rect (0, sr, Frame.Width, sr + 1));
  982. } else {
  983. currentLine.RemoveAt (currentColumn);
  984. var r = currentRow - topRow;
  985. SetNeedsDisplay (new Rect (currentColumn - leftColumn, r, Frame.Width, r + 1));
  986. }
  987. break;
  988. case Key.End:
  989. case Key.E | Key.CtrlMask: // End
  990. currentLine = GetCurrentLine ();
  991. currentColumn = currentLine.Count;
  992. int pcol = leftColumn;
  993. Adjust ();
  994. break;
  995. case Key.K | Key.CtrlMask: // kill-to-end
  996. if (isReadOnly)
  997. break;
  998. currentLine = GetCurrentLine ();
  999. if (currentLine.Count == 0) {
  1000. model.RemoveLine (currentRow);
  1001. var val = ustring.Make ((Rune)'\n');
  1002. if (lastWasKill)
  1003. AppendClipboard (val);
  1004. else
  1005. SetClipboard (val);
  1006. } else {
  1007. restCount = currentLine.Count - currentColumn;
  1008. rest = currentLine.GetRange (currentColumn, restCount);
  1009. var val = StringFromRunes (rest);
  1010. if (lastWasKill)
  1011. AppendClipboard (val);
  1012. else
  1013. SetClipboard (val);
  1014. currentLine.RemoveRange (currentColumn, restCount);
  1015. }
  1016. SetNeedsDisplay (new Rect (0, currentRow - topRow, Frame.Width, Frame.Height));
  1017. lastWasKill = true;
  1018. break;
  1019. case Key.Y | Key.CtrlMask: // Control-y, yank
  1020. if (isReadOnly)
  1021. break;
  1022. InsertText (Clipboard.Contents);
  1023. selecting = false;
  1024. break;
  1025. case Key.Space | Key.CtrlMask:
  1026. selecting = true;
  1027. selectionStartColumn = currentColumn;
  1028. selectionStartRow = currentRow;
  1029. break;
  1030. case ((int)'W' + Key.AltMask):
  1031. case Key.W | Key.CtrlMask:
  1032. SetClipboard (GetRegion ());
  1033. if (!isReadOnly)
  1034. ClearRegion ();
  1035. selecting = false;
  1036. break;
  1037. case Key.CtrlMask | Key.CursorLeft:
  1038. case (Key)((int)'B' + Key.AltMask):
  1039. var newPos = WordBackward (currentColumn, currentRow);
  1040. if (newPos.HasValue) {
  1041. currentColumn = newPos.Value.col;
  1042. currentRow = newPos.Value.row;
  1043. }
  1044. Adjust ();
  1045. break;
  1046. case Key.CtrlMask | Key.CursorRight:
  1047. case (Key)((int)'F' + Key.AltMask):
  1048. newPos = WordForward (currentColumn, currentRow);
  1049. if (newPos.HasValue) {
  1050. currentColumn = newPos.Value.col;
  1051. currentRow = newPos.Value.row;
  1052. }
  1053. Adjust ();
  1054. break;
  1055. case Key.Enter:
  1056. if (isReadOnly)
  1057. break;
  1058. currentLine = GetCurrentLine ();
  1059. restCount = currentLine.Count - currentColumn;
  1060. rest = currentLine.GetRange (currentColumn, restCount);
  1061. currentLine.RemoveRange (currentColumn, restCount);
  1062. model.AddLine (currentRow + 1, rest);
  1063. currentRow++;
  1064. bool fullNeedsDisplay = false;
  1065. if (currentRow >= topRow + Frame.Height) {
  1066. topRow++;
  1067. fullNeedsDisplay = true;
  1068. }
  1069. currentColumn = 0;
  1070. if (currentColumn < leftColumn) {
  1071. fullNeedsDisplay = true;
  1072. leftColumn = 0;
  1073. }
  1074. if (fullNeedsDisplay)
  1075. SetNeedsDisplay ();
  1076. else
  1077. SetNeedsDisplay (new Rect (0, currentRow - topRow, 2, Frame.Height));
  1078. break;
  1079. case Key.CtrlMask | Key.End:
  1080. MoveEnd ();
  1081. break;
  1082. case Key.CtrlMask | Key.Home:
  1083. MoveHome ();
  1084. break;
  1085. default:
  1086. // Ignore control characters and other special keys
  1087. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  1088. return false;
  1089. //So that special keys like tab can be processed
  1090. if (isReadOnly)
  1091. return true;
  1092. Insert ((uint)kb.Key);
  1093. currentColumn++;
  1094. if (currentColumn >= leftColumn + Frame.Width) {
  1095. leftColumn++;
  1096. SetNeedsDisplay ();
  1097. }
  1098. PositionCursor ();
  1099. return true;
  1100. }
  1101. return true;
  1102. }
  1103. void MoveUp ()
  1104. {
  1105. if (currentRow > 0) {
  1106. if (columnTrack == -1) {
  1107. columnTrack = currentColumn;
  1108. }
  1109. currentRow--;
  1110. if (currentRow < topRow) {
  1111. topRow--;
  1112. SetNeedsDisplay ();
  1113. }
  1114. TrackColumn ();
  1115. PositionCursor ();
  1116. }
  1117. }
  1118. void MoveDown ()
  1119. {
  1120. if (currentRow + 1 < model.Count) {
  1121. if (columnTrack == -1) {
  1122. columnTrack = currentColumn;
  1123. }
  1124. currentRow++;
  1125. if (currentRow >= topRow + Frame.Height) {
  1126. topRow++;
  1127. SetNeedsDisplay ();
  1128. }
  1129. TrackColumn ();
  1130. PositionCursor ();
  1131. } else if (currentRow > Frame.Height) {
  1132. Adjust ();
  1133. }
  1134. }
  1135. IEnumerable<(int col, int row, Rune rune)> ForwardIterator (int col, int row)
  1136. {
  1137. if (col < 0 || row < 0)
  1138. yield break;
  1139. if (row >= model.Count)
  1140. yield break;
  1141. var line = GetCurrentLine ();
  1142. if (col >= line.Count)
  1143. yield break;
  1144. while (row < model.Count) {
  1145. for (int c = col; c < line.Count; c++) {
  1146. yield return (c, row, line [c]);
  1147. }
  1148. col = 0;
  1149. row++;
  1150. line = GetCurrentLine ();
  1151. }
  1152. }
  1153. Rune RuneAt (int col, int row) => model.GetLine (row) [col];
  1154. /// <summary>
  1155. /// Will scroll the <see cref="TextView"/> to the last line and position the cursor there.
  1156. /// </summary>
  1157. public void MoveEnd ()
  1158. {
  1159. currentRow = model.Count - 1;
  1160. TrackColumn ();
  1161. PositionCursor ();
  1162. }
  1163. /// <summary>
  1164. /// Will scroll the <see cref="TextView"/> to the first line and position the cursor there.
  1165. /// </summary>
  1166. public void MoveHome ()
  1167. {
  1168. currentRow = 0;
  1169. TrackColumn ();
  1170. PositionCursor ();
  1171. }
  1172. bool MoveNext (ref int col, ref int row, out Rune rune)
  1173. {
  1174. var line = model.GetLine (row);
  1175. if (col + 1 < line.Count) {
  1176. col++;
  1177. rune = line [col];
  1178. return true;
  1179. }
  1180. while (row + 1 < model.Count) {
  1181. col = 0;
  1182. row++;
  1183. line = model.GetLine (row);
  1184. if (line.Count > 0) {
  1185. rune = line [0];
  1186. return true;
  1187. }
  1188. }
  1189. rune = 0;
  1190. return false;
  1191. }
  1192. bool MovePrev (ref int col, ref int row, out Rune rune)
  1193. {
  1194. var line = model.GetLine (row);
  1195. if (col > 0) {
  1196. col--;
  1197. rune = line [col];
  1198. return true;
  1199. }
  1200. if (row == 0) {
  1201. rune = 0;
  1202. return false;
  1203. }
  1204. while (row > 0) {
  1205. row--;
  1206. line = model.GetLine (row);
  1207. col = line.Count - 1;
  1208. if (col >= 0) {
  1209. rune = line [col];
  1210. return true;
  1211. }
  1212. }
  1213. rune = 0;
  1214. return false;
  1215. }
  1216. (int col, int row)? WordForward (int fromCol, int fromRow)
  1217. {
  1218. var col = fromCol;
  1219. var row = fromRow;
  1220. try {
  1221. var rune = RuneAt (col, row);
  1222. var srow = row;
  1223. if (Rune.IsPunctuation (rune) || Rune.IsWhiteSpace (rune)) {
  1224. while (MoveNext (ref col, ref row, out rune)) {
  1225. if (Rune.IsLetterOrDigit (rune))
  1226. break;
  1227. }
  1228. while (MoveNext (ref col, ref row, out rune)) {
  1229. if (!Rune.IsLetterOrDigit (rune))
  1230. break;
  1231. }
  1232. } else {
  1233. while (MoveNext (ref col, ref row, out rune)) {
  1234. if (!Rune.IsLetterOrDigit (rune))
  1235. break;
  1236. }
  1237. }
  1238. if (fromCol != col || fromRow != row)
  1239. return (col, row);
  1240. return null;
  1241. } catch (Exception) {
  1242. return null;
  1243. }
  1244. }
  1245. (int col, int row)? WordBackward (int fromCol, int fromRow)
  1246. {
  1247. if (fromRow == 0 && fromCol == 0)
  1248. return null;
  1249. var col = fromCol;
  1250. var row = fromRow;
  1251. try {
  1252. var rune = RuneAt (col, row);
  1253. if (Rune.IsPunctuation (rune) || Rune.IsSymbol (rune) || Rune.IsWhiteSpace (rune)) {
  1254. while (MovePrev (ref col, ref row, out rune)) {
  1255. if (Rune.IsLetterOrDigit (rune))
  1256. break;
  1257. }
  1258. while (MovePrev (ref col, ref row, out rune)) {
  1259. if (!Rune.IsLetterOrDigit (rune))
  1260. break;
  1261. }
  1262. } else {
  1263. while (MovePrev (ref col, ref row, out rune)) {
  1264. if (!Rune.IsLetterOrDigit (rune))
  1265. break;
  1266. }
  1267. }
  1268. if (fromCol != col || fromRow != row)
  1269. return (col, row);
  1270. return null;
  1271. } catch (Exception) {
  1272. return null;
  1273. }
  1274. }
  1275. ///<inheritdoc/>
  1276. public override bool MouseEvent (MouseEvent ev)
  1277. {
  1278. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked) &&
  1279. !ev.Flags.HasFlag (MouseFlags.WheeledDown) && !ev.Flags.HasFlag (MouseFlags.WheeledUp)) {
  1280. return false;
  1281. }
  1282. if (!CanFocus) {
  1283. return true;
  1284. }
  1285. if (!HasFocus) {
  1286. SetFocus ();
  1287. }
  1288. if (ev.Flags == MouseFlags.Button1Clicked) {
  1289. if (model.Count > 0) {
  1290. var maxCursorPositionableLine = Math.Max ((model.Count - 1) - topRow, 0);
  1291. if (ev.Y > maxCursorPositionableLine) {
  1292. currentRow = maxCursorPositionableLine;
  1293. } else {
  1294. currentRow = ev.Y + topRow;
  1295. }
  1296. var r = GetCurrentLine ();
  1297. var idx = TextModel.GetColFromX (r, leftColumn, ev.X);
  1298. if (idx - leftColumn >= r.Count) {
  1299. currentColumn = r.Count - leftColumn;
  1300. } else {
  1301. currentColumn = idx + leftColumn;
  1302. }
  1303. }
  1304. PositionCursor ();
  1305. lastWasKill = false;
  1306. columnTrack = currentColumn;
  1307. } else if (ev.Flags == MouseFlags.WheeledDown) {
  1308. lastWasKill = false;
  1309. columnTrack = currentColumn;
  1310. ScrollTo (topRow + 1);
  1311. } else if (ev.Flags == MouseFlags.WheeledUp) {
  1312. lastWasKill = false;
  1313. columnTrack = currentColumn;
  1314. ScrollTo (topRow - 1);
  1315. } else if (ev.Flags == MouseFlags.WheeledRight) {
  1316. lastWasKill = false;
  1317. columnTrack = currentColumn;
  1318. ScrollTo (leftColumn + 1, false);
  1319. } else if (ev.Flags == MouseFlags.WheeledLeft) {
  1320. lastWasKill = false;
  1321. columnTrack = currentColumn;
  1322. ScrollTo (leftColumn - 1, false);
  1323. }
  1324. return true;
  1325. }
  1326. }
  1327. }