TextView.Selection.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. namespace Terminal.Gui.Views;
  2. public partial class TextView
  3. {
  4. /// <summary>Get or sets whether the user is currently selecting text.</summary>
  5. public bool IsSelecting { get; set; }
  6. /// <summary>
  7. /// Gets or sets whether the word navigation should select only the word itself without spaces around it or with the
  8. /// spaces at right.
  9. /// Default is <c>false</c> meaning that the spaces at right are included in the selection.
  10. /// </summary>
  11. public bool SelectWordOnlyOnDoubleClick { get; set; }
  12. /// <summary>Start row position of the selected text.</summary>
  13. public int SelectionStartRow
  14. {
  15. get => _selectionStartRow;
  16. set
  17. {
  18. _selectionStartRow = value < 0 ? 0 :
  19. value > _model.Count - 1 ? Math.Max (_model.Count - 1, 0) : value;
  20. IsSelecting = true;
  21. SetNeedsDraw ();
  22. Adjust ();
  23. }
  24. }
  25. /// <summary>Start column position of the selected text.</summary>
  26. public int SelectionStartColumn
  27. {
  28. get => _selectionStartColumn;
  29. set
  30. {
  31. List<Cell> line = _model.GetLine (_selectionStartRow);
  32. _selectionStartColumn = value < 0 ? 0 :
  33. value > line.Count ? line.Count : value;
  34. IsSelecting = true;
  35. SetNeedsDraw ();
  36. Adjust ();
  37. }
  38. }
  39. private void StartSelecting ()
  40. {
  41. if (_shiftSelecting && IsSelecting)
  42. {
  43. return;
  44. }
  45. _shiftSelecting = true;
  46. IsSelecting = true;
  47. _selectionStartColumn = CurrentColumn;
  48. _selectionStartRow = CurrentRow;
  49. }
  50. private void StopSelecting ()
  51. {
  52. if (IsSelecting)
  53. {
  54. SetNeedsDraw ();
  55. }
  56. _shiftSelecting = false;
  57. IsSelecting = false;
  58. _isButtonShift = false;
  59. }
  60. /// <summary>Length of the selected text.</summary>
  61. public int SelectedLength => GetSelectedLength ();
  62. /// <summary>
  63. /// Gets the selected text as
  64. /// <see>
  65. /// <cref>List{List{Cell}}</cref>
  66. /// </see>
  67. /// </summary>
  68. public List<List<Cell>> SelectedCellsList
  69. {
  70. get
  71. {
  72. GetRegion (out List<List<Cell>> selectedCellsList);
  73. return selectedCellsList;
  74. }
  75. }
  76. /// <summary>The selected text.</summary>
  77. public string SelectedText
  78. {
  79. get
  80. {
  81. if (!IsSelecting || (_model.Count == 1 && _model.GetLine (0).Count == 0))
  82. {
  83. return string.Empty;
  84. }
  85. return GetSelectedRegion ();
  86. }
  87. }
  88. // Returns an encoded region start..end (top 32 bits are the row, low32 the column)
  89. private void GetEncodedRegionBounds (
  90. out long start,
  91. out long end,
  92. int? startRow = null,
  93. int? startCol = null,
  94. int? cRow = null,
  95. int? cCol = null
  96. )
  97. {
  98. long selection;
  99. long point;
  100. if (startRow is null || startCol is null || cRow is null || cCol is null)
  101. {
  102. selection = ((long)(uint)_selectionStartRow << 32) | (uint)_selectionStartColumn;
  103. point = ((long)(uint)CurrentRow << 32) | (uint)CurrentColumn;
  104. }
  105. else
  106. {
  107. selection = ((long)(uint)startRow << 32) | (uint)startCol;
  108. point = ((long)(uint)cRow << 32) | (uint)cCol;
  109. }
  110. if (selection > point)
  111. {
  112. start = point;
  113. end = selection;
  114. }
  115. else
  116. {
  117. start = selection;
  118. end = point;
  119. }
  120. }
  121. //
  122. // Returns a string with the text in the selected
  123. // region.
  124. //
  125. internal string GetRegion (
  126. out List<List<Cell>> cellsList,
  127. int? sRow = null,
  128. int? sCol = null,
  129. int? cRow = null,
  130. int? cCol = null,
  131. TextModel? model = null
  132. )
  133. {
  134. GetEncodedRegionBounds (out long start, out long end, sRow, sCol, cRow, cCol);
  135. cellsList = [];
  136. if (start == end)
  137. {
  138. return string.Empty;
  139. }
  140. var startRow = (int)(start >> 32);
  141. var maxRow = (int)(end >> 32);
  142. var startCol = (int)(start & 0xffffffff);
  143. var endCol = (int)(end & 0xffffffff);
  144. List<Cell> line = model is null ? _model.GetLine (startRow) : model.GetLine (startRow);
  145. List<Cell> cells;
  146. if (startRow == maxRow)
  147. {
  148. cells = line.GetRange (startCol, endCol - startCol);
  149. cellsList.Add (cells);
  150. return StringFromCells (cells);
  151. }
  152. cells = line.GetRange (startCol, line.Count - startCol);
  153. cellsList.Add (cells);
  154. string res = StringFromCells (cells);
  155. for (int row = startRow + 1; row < maxRow; row++)
  156. {
  157. cellsList.AddRange ([]);
  158. cells = model == null ? _model.GetLine (row) : model.GetLine (row);
  159. cellsList.Add (cells);
  160. res = res
  161. + Environment.NewLine
  162. + StringFromCells (cells);
  163. }
  164. line = model is null ? _model.GetLine (maxRow) : model.GetLine (maxRow);
  165. cellsList.AddRange ([]);
  166. cells = line.GetRange (0, endCol);
  167. cellsList.Add (cells);
  168. res = res + Environment.NewLine + StringFromCells (cells);
  169. return res;
  170. }
  171. private int GetSelectedLength () { return SelectedText.Length; }
  172. private string GetSelectedRegion ()
  173. {
  174. int cRow = CurrentRow;
  175. int cCol = CurrentColumn;
  176. int startRow = _selectionStartRow;
  177. int startCol = _selectionStartColumn;
  178. TextModel model = _model;
  179. if (_wordWrap)
  180. {
  181. cRow = _wrapManager!.GetModelLineFromWrappedLines (CurrentRow);
  182. cCol = _wrapManager.GetModelColFromWrappedLines (CurrentRow, CurrentColumn);
  183. startRow = _wrapManager.GetModelLineFromWrappedLines (_selectionStartRow);
  184. startCol = _wrapManager.GetModelColFromWrappedLines (_selectionStartRow, _selectionStartColumn);
  185. model = _wrapManager.Model;
  186. }
  187. OnUnwrappedCursorPosition (cRow, cCol);
  188. return GetRegion (out _, startRow, startCol, cRow, cCol, model);
  189. }
  190. private string StringFromCells (List<Cell> cells)
  191. {
  192. ArgumentNullException.ThrowIfNull (cells);
  193. var size = 0;
  194. foreach (Cell cell in cells)
  195. {
  196. string t = cell.Grapheme;
  197. size += Encoding.Unicode.GetByteCount (t);
  198. }
  199. byte [] encoded = new byte [size];
  200. var offset = 0;
  201. foreach (Cell cell in cells)
  202. {
  203. string t = cell.Grapheme;
  204. int bytesWritten = Encoding.Unicode.GetBytes (t, 0, t.Length, encoded, offset);
  205. offset += bytesWritten;
  206. }
  207. // decode using the same encoding and the bytes actually written
  208. return Encoding.Unicode.GetString (encoded, 0, offset);
  209. }
  210. /// <inheritdoc />
  211. public bool EnableForDesign ()
  212. {
  213. Text = """
  214. TextView provides a fully featured multi-line text editor.
  215. It supports word wrap and history for undo.
  216. """;
  217. return true;
  218. }
  219. /// <inheritdoc/>
  220. protected override void Dispose (bool disposing)
  221. {
  222. if (disposing && ContextMenu is { })
  223. {
  224. ContextMenu.Visible = false;
  225. ContextMenu.Dispose ();
  226. ContextMenu = null;
  227. }
  228. base.Dispose (disposing);
  229. }
  230. private void ClearRegion ()
  231. {
  232. SetWrapModel ();
  233. long start, end;
  234. long currentEncoded = ((long)(uint)CurrentRow << 32) | (uint)CurrentColumn;
  235. GetEncodedRegionBounds (out start, out end);
  236. var startRow = (int)(start >> 32);
  237. var maxrow = (int)(end >> 32);
  238. var startCol = (int)(start & 0xffffffff);
  239. var endCol = (int)(end & 0xffffffff);
  240. List<Cell> line = _model.GetLine (startRow);
  241. _historyText.Add (new () { new (line) }, new (startCol, startRow));
  242. List<List<Cell>> removedLines = new ();
  243. if (startRow == maxrow)
  244. {
  245. removedLines.Add (new (line));
  246. line.RemoveRange (startCol, endCol - startCol);
  247. CurrentColumn = startCol;
  248. if (_wordWrap)
  249. {
  250. SetNeedsDraw ();
  251. }
  252. else
  253. {
  254. //QUESTION: Is the below comment still relevant?
  255. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  256. //SetNeedsDraw (new (0, startRow - topRow, Viewport.Width, startRow - topRow + 1));
  257. SetNeedsDraw ();
  258. }
  259. _historyText.Add (
  260. new (removedLines),
  261. CursorPosition,
  262. TextEditingLineStatus.Removed
  263. );
  264. UpdateWrapModel ();
  265. return;
  266. }
  267. removedLines.Add (new (line));
  268. line.RemoveRange (startCol, line.Count - startCol);
  269. List<Cell> line2 = _model.GetLine (maxrow);
  270. line.AddRange (line2.Skip (endCol));
  271. for (int row = startRow + 1; row <= maxrow; row++)
  272. {
  273. removedLines.Add (new (_model.GetLine (startRow + 1)));
  274. _model.RemoveLine (startRow + 1);
  275. }
  276. if (currentEncoded == end)
  277. {
  278. CurrentRow -= maxrow - startRow;
  279. }
  280. CurrentColumn = startCol;
  281. _historyText.Add (
  282. new (removedLines),
  283. CursorPosition,
  284. TextEditingLineStatus.Removed
  285. );
  286. UpdateWrapModel ();
  287. SetNeedsDraw ();
  288. }
  289. private void ClearSelectedRegion ()
  290. {
  291. SetWrapModel ();
  292. if (!_isReadOnly)
  293. {
  294. ClearRegion ();
  295. }
  296. UpdateWrapModel ();
  297. IsSelecting = false;
  298. DoNeededAction ();
  299. }
  300. /// <summary>Select all text.</summary>
  301. public void SelectAll ()
  302. {
  303. if (_model.Count == 0)
  304. {
  305. return;
  306. }
  307. StartSelecting ();
  308. _selectionStartColumn = 0;
  309. _selectionStartRow = 0;
  310. CurrentColumn = _model.GetLine (_model.Count - 1).Count;
  311. CurrentRow = _model.Count - 1;
  312. SetNeedsDraw ();
  313. }
  314. private void ProcessSelectAll ()
  315. {
  316. ResetColumnTrack ();
  317. SelectAll ();
  318. }
  319. private bool PointInSelection (int col, int row)
  320. {
  321. long start, end;
  322. GetEncodedRegionBounds (out start, out end);
  323. long q = ((long)(uint)row << 32) | (uint)col;
  324. return q >= start && q <= end - 1;
  325. }
  326. }