TextView.cs 37 KB

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