TextView.cs 36 KB

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