TextView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. //
  2. // TextView.cs: multi-line text editing
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // TODO:
  9. // Attributed text on spans
  10. // Cursor target track
  11. // Kill-ring, paste
  12. // Render selection
  13. // Mark/Delete/Cut commands
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Text;
  19. using NStack;
  20. namespace Terminal.Gui {
  21. class TextModel {
  22. List<List<Rune>> lines;
  23. List<int> lineLength;
  24. public bool LoadFile (string file)
  25. {
  26. if (file == null)
  27. throw new ArgumentNullException (nameof (file));
  28. try {
  29. var stream = File.OpenRead (file);
  30. if (stream == null)
  31. return false;
  32. } catch {
  33. return false;
  34. }
  35. LoadStream (File.OpenRead (file));
  36. return true;
  37. }
  38. List<Rune> ToRunes (ustring str)
  39. {
  40. List<Rune> runes = new List<Rune> ();
  41. foreach (var x in str.ToRunes ()) {
  42. runes.Add (x);
  43. }
  44. return runes;
  45. }
  46. void Append (List<byte> line)
  47. {
  48. var str = ustring.Make (line.ToArray ());
  49. lines.Add (ToRunes (str));
  50. }
  51. public void LoadStream (Stream input)
  52. {
  53. if (input == null)
  54. throw new ArgumentNullException (nameof (input));
  55. lines = new List<List<Rune>> ();
  56. var buff = new BufferedStream (input);
  57. int v;
  58. var line = new List<byte> ();
  59. while ((v = buff.ReadByte ()) != -1) {
  60. if (v == 10) {
  61. Append (line);
  62. line.Clear ();
  63. continue;
  64. }
  65. line.Add ((byte)v);
  66. }
  67. if (line.Count > 0)
  68. Append (line);
  69. }
  70. public void LoadString (ustring content)
  71. {
  72. lines = new List<List<Rune>> ();
  73. int start = 0, i = 0;
  74. for (; i < content.Length; i++) {
  75. if (content [i] == 10) {
  76. if (i - start > 0)
  77. lines.Add (ToRunes (content [start, i]));
  78. else
  79. lines.Add (ToRunes (ustring.Empty));
  80. start = i + 1;
  81. }
  82. }
  83. if (i - start > 0)
  84. lines.Add (ToRunes (content [start, null]));
  85. }
  86. public override string ToString ()
  87. {
  88. var sb = new StringBuilder ();
  89. foreach (var line in lines) {
  90. sb.Append (line);
  91. sb.AppendLine ();
  92. }
  93. return sb.ToString ();
  94. }
  95. public int Count => lines.Count;
  96. public List<Rune> GetLine (int line) => lines [line];
  97. public void AddLine (int pos, List<Rune> runes)
  98. {
  99. lines.Insert (pos, runes);
  100. }
  101. public void RemoveLine (int pos)
  102. {
  103. lines.RemoveAt (pos);
  104. }
  105. }
  106. /// <summary>
  107. /// Text data entry widget
  108. /// </summary>
  109. /// <remarks>
  110. /// The Entry widget provides Emacs-like editing
  111. /// functionality, and mouse support.
  112. /// </remarks>
  113. public class TextView : View {
  114. TextModel model = new TextModel ();
  115. int topRow;
  116. int leftColumn;
  117. int currentRow;
  118. int currentColumn;
  119. bool used;
  120. /// <summary>
  121. /// Changed event, raised when the text has clicked.
  122. /// </summary>
  123. /// <remarks>
  124. /// Client code can hook up to this event, it is
  125. /// raised when the text in the entry changes.
  126. /// </remarks>
  127. public event EventHandler Changed;
  128. /// <summary>
  129. /// Public constructor.
  130. /// </summary>
  131. /// <remarks>
  132. /// </remarks>
  133. public TextView (Rect frame) : base (frame)
  134. {
  135. CanFocus = true;
  136. }
  137. void ResetPosition ()
  138. {
  139. topRow = leftColumn = currentRow = currentColumn = 0;
  140. }
  141. /// <summary>
  142. /// Sets or gets the text in the entry.
  143. /// </summary>
  144. /// <remarks>
  145. /// </remarks>
  146. public ustring Text {
  147. get {
  148. return model.ToString ();
  149. }
  150. set {
  151. ResetPosition ();
  152. model.LoadString (value);
  153. SetNeedsDisplay ();
  154. }
  155. }
  156. /// <summary>
  157. /// The current cursor row.
  158. /// </summary>
  159. public int CurrentRow => currentRow;
  160. /// <summary>
  161. /// Gets the cursor column.
  162. /// </summary>
  163. /// <value>The cursor column.</value>
  164. public int CurrentColumn => currentColumn;
  165. /// <summary>
  166. /// Sets the cursor position.
  167. /// </summary>
  168. public override void PositionCursor ()
  169. {
  170. Move (CurrentColumn - leftColumn, CurrentRow - topRow);
  171. }
  172. void ClearRegion (int left, int top, int right, int bottom)
  173. {
  174. for (int row = top; row < bottom; row++) {
  175. Move (left, row);
  176. for (int col = left; col < right; col++)
  177. AddRune (col, row, ' ');
  178. }
  179. }
  180. public override void Redraw (Rect region)
  181. {
  182. Driver.SetAttribute (ColorScheme.Focus);
  183. Move (0, 0);
  184. int bottom = region.Bottom;
  185. int right = region.Right;
  186. for (int row = region.Top; row < bottom; row++) {
  187. int textLine = topRow + row;
  188. if (textLine >= model.Count) {
  189. ClearRegion (region.Left, row, region.Right, row + 1);
  190. continue;
  191. }
  192. var line = model.GetLine (textLine);
  193. int lineRuneCount = line.Count;
  194. if (line.Count < region.Left){
  195. ClearRegion (region.Left, row, region.Right, row + 1);
  196. continue;
  197. }
  198. Move (region.Left, row);
  199. for (int col = region.Left; col < right; col++) {
  200. var lineCol = leftColumn + col;
  201. var rune = lineCol >= lineRuneCount ? ' ' : line [lineCol];
  202. AddRune (col, row, rune);
  203. }
  204. }
  205. PositionCursor ();
  206. }
  207. public override bool CanFocus {
  208. get => true;
  209. set { base.CanFocus = value; }
  210. }
  211. void SetClipboard (ustring text)
  212. {
  213. Clipboard.Contents = text;
  214. }
  215. void Insert (Rune rune)
  216. {
  217. var line = model.GetLine (currentRow);
  218. line.Insert (currentColumn, rune);
  219. var prow = currentRow - topRow;
  220. SetNeedsDisplay (new Rect (0, prow, Frame.Width, prow + 1));
  221. }
  222. public override bool ProcessKey (KeyEvent kb)
  223. {
  224. switch (kb.Key) {
  225. case Key.ControlN:
  226. case Key.CursorDown:
  227. if (currentRow + 1 < model.Count) {
  228. currentRow++;
  229. if (currentRow >= topRow + Frame.Height) {
  230. topRow++;
  231. SetNeedsDisplay ();
  232. }
  233. PositionCursor ();
  234. }
  235. break;
  236. case Key.ControlP:
  237. case Key.CursorUp:
  238. if (currentRow > 0) {
  239. currentRow--;
  240. if (currentRow < topRow) {
  241. topRow--;
  242. SetNeedsDisplay ();
  243. }
  244. PositionCursor ();
  245. }
  246. break;
  247. case Key.ControlF:
  248. case Key.CursorRight:
  249. var currentLine = model.GetLine (currentRow);
  250. if (currentColumn < currentLine.Count) {
  251. currentColumn++;
  252. if (currentColumn >= leftColumn + Frame.Width) {
  253. leftColumn++;
  254. SetNeedsDisplay ();
  255. }
  256. PositionCursor ();
  257. } else {
  258. if (currentRow + 1 < model.Count) {
  259. currentRow++;
  260. currentColumn = 0;
  261. leftColumn = 0;
  262. if (currentRow >= topRow + Frame.Height) {
  263. topRow++;
  264. }
  265. SetNeedsDisplay ();
  266. PositionCursor ();
  267. }
  268. break;
  269. }
  270. break;
  271. case Key.ControlB:
  272. case Key.CursorLeft:
  273. if (currentColumn > 0) {
  274. currentColumn--;
  275. if (currentColumn < leftColumn) {
  276. leftColumn--;
  277. SetNeedsDisplay ();
  278. }
  279. PositionCursor ();
  280. } else {
  281. if (currentRow > 0) {
  282. currentRow--;
  283. if (currentRow < topRow) {
  284. topRow--;
  285. }
  286. currentLine = model.GetLine (currentRow);
  287. currentColumn = currentLine.Count;
  288. int prev = leftColumn;
  289. leftColumn = currentColumn - Frame.Width + 1;
  290. if (leftColumn < 0)
  291. leftColumn = 0;
  292. if (prev != leftColumn)
  293. SetNeedsDisplay ();
  294. PositionCursor ();
  295. }
  296. }
  297. break;
  298. case Key.Delete:
  299. case Key.Backspace:
  300. if (currentColumn > 0) {
  301. currentLine = model.GetLine (currentRow);
  302. currentLine.RemoveAt (currentColumn - 1);
  303. currentColumn--;
  304. if (currentColumn < leftColumn) {
  305. leftColumn--;
  306. SetNeedsDisplay ();
  307. } else
  308. SetNeedsDisplay (new Rect (0, currentRow - topRow, 1, Frame.Width));
  309. } else {
  310. // Merges the current line with the previous one.
  311. if (currentRow == 0)
  312. return true;
  313. var prowIdx = currentRow - 1;
  314. var prevRow = model.GetLine (prowIdx);
  315. var prevCount = prevRow.Count;
  316. model.GetLine (prowIdx).AddRange (model.GetLine (currentRow));
  317. currentRow--;
  318. currentColumn = prevCount;
  319. leftColumn = currentColumn - Frame.Width + 1;
  320. if (leftColumn < 0)
  321. leftColumn = 0;
  322. SetNeedsDisplay ();
  323. }
  324. break;
  325. // Home, C-A
  326. case Key.Home:
  327. case Key.ControlA:
  328. currentColumn = 0;
  329. if (currentColumn < leftColumn) {
  330. leftColumn = 0;
  331. SetNeedsDisplay ();
  332. } else
  333. PositionCursor ();
  334. break;
  335. case Key.ControlD: // Delete
  336. currentLine = model.GetLine (currentRow);
  337. if (currentColumn == currentLine.Count) {
  338. if (currentRow + 1 == model.Count)
  339. break;
  340. var nextLine = model.GetLine (currentRow + 1);
  341. currentLine.AddRange (nextLine);
  342. model.RemoveLine (currentRow + 1);
  343. var sr = currentRow - topRow;
  344. SetNeedsDisplay (new Rect (0, sr, Frame.Width, sr + 1));
  345. } else {
  346. currentLine.RemoveAt (currentColumn);
  347. var r = currentRow - topRow;
  348. SetNeedsDisplay (new Rect (currentColumn - leftColumn, r, Frame.Width, r + 1));
  349. }
  350. break;
  351. case Key.ControlE: // End
  352. currentLine = model.GetLine (currentRow);
  353. currentColumn = currentLine.Count;
  354. int pcol = leftColumn;
  355. leftColumn = currentColumn - Frame.Width + 1;
  356. if (leftColumn < 0)
  357. leftColumn = 0;
  358. if (pcol != leftColumn)
  359. SetNeedsDisplay ();
  360. PositionCursor ();
  361. break;
  362. case Key.ControlK: // kill-to-end
  363. break;
  364. case Key.ControlY: // Control-y, yank
  365. case (Key)((int)'b' + Key.AltMask):
  366. break;
  367. case (Key)((int)'f' + Key.AltMask):
  368. break;
  369. case Key.Enter:
  370. var orow = currentRow;
  371. currentLine = model.GetLine (currentRow);
  372. var restCount = currentLine.Count - currentColumn;
  373. var rest = currentLine.GetRange (currentColumn, restCount);
  374. currentLine.RemoveRange (currentColumn, restCount);
  375. model.AddLine (currentRow + 1, rest);
  376. currentRow++;
  377. bool fullNeedsDisplay = false;
  378. if (currentRow >= topRow + Frame.Height) {
  379. topRow++;
  380. fullNeedsDisplay = true;
  381. }
  382. currentColumn = 0;
  383. if (currentColumn < leftColumn) {
  384. fullNeedsDisplay = true;
  385. leftColumn = 0;
  386. }
  387. if (fullNeedsDisplay)
  388. SetNeedsDisplay ();
  389. else
  390. SetNeedsDisplay (new Rect (0, currentRow - topRow, 0, Frame.Height));
  391. break;
  392. default:
  393. // Ignore control characters and other special keys
  394. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  395. return false;
  396. Insert ((uint)kb.Key);
  397. currentColumn++;
  398. if (currentColumn >= leftColumn + Frame.Width) {
  399. leftColumn++;
  400. SetNeedsDisplay ();
  401. }
  402. PositionCursor ();
  403. return true;
  404. }
  405. return true;
  406. }
  407. #if false
  408. int WordForward (int p)
  409. {
  410. if (p >= text.Length)
  411. return -1;
  412. int i = p;
  413. if (Rune.IsPunctuation (text [p]) || Rune.IsWhiteSpace (text [p])) {
  414. for (; i < text.Length; i++) {
  415. var r = text [i];
  416. if (Rune.IsLetterOrDigit (r))
  417. break;
  418. }
  419. for (; i < text.Length; i++) {
  420. var r = text [i];
  421. if (!Rune.IsLetterOrDigit (r))
  422. break;
  423. }
  424. } else {
  425. for (; i < text.Length; i++) {
  426. var r = text [i];
  427. if (!Rune.IsLetterOrDigit (r))
  428. break;
  429. }
  430. }
  431. if (i != p)
  432. return i;
  433. return -1;
  434. }
  435. int WordBackward (int p)
  436. {
  437. if (p == 0)
  438. return -1;
  439. int i = p - 1;
  440. if (i == 0)
  441. return 0;
  442. var ti = text [i];
  443. if (Rune.IsPunctuation (ti) || Rune.IsSymbol (ti) || Rune.IsWhiteSpace (ti)) {
  444. for (; i >= 0; i--) {
  445. if (Rune.IsLetterOrDigit (text [i]))
  446. break;
  447. }
  448. for (; i >= 0; i--) {
  449. if (!Rune.IsLetterOrDigit (text [i]))
  450. break;
  451. }
  452. } else {
  453. for (; i >= 0; i--) {
  454. if (!Rune.IsLetterOrDigit (text [i]))
  455. break;
  456. }
  457. }
  458. i++;
  459. if (i != p)
  460. return i;
  461. return -1;
  462. }
  463. public override bool MouseEvent (MouseEvent ev)
  464. {
  465. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
  466. return false;
  467. if (!HasFocus)
  468. SuperView.SetFocus (this);
  469. // We could also set the cursor position.
  470. point = first + ev.X;
  471. if (point > text.Length)
  472. point = text.Length;
  473. if (point < first)
  474. point = 0;
  475. SetNeedsDisplay ();
  476. return true;
  477. }
  478. #endif
  479. }
  480. }