TextView.Drawing.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. namespace Terminal.Gui.Views;
  2. public partial class TextView
  3. {
  4. internal void ApplyCellsAttribute (Attribute attribute)
  5. {
  6. if (!ReadOnly && SelectedLength > 0)
  7. {
  8. int startRow = Math.Min (SelectionStartRow, CurrentRow);
  9. int endRow = Math.Max (CurrentRow, SelectionStartRow);
  10. int startCol = SelectionStartRow <= CurrentRow ? SelectionStartColumn : CurrentColumn;
  11. int endCol = CurrentRow >= SelectionStartRow ? CurrentColumn : SelectionStartColumn;
  12. List<List<Cell>> selectedCellsOriginal = [];
  13. List<List<Cell>> selectedCellsChanged = [];
  14. for (int r = startRow; r <= endRow; r++)
  15. {
  16. List<Cell> line = GetLine (r);
  17. selectedCellsOriginal.Add ([.. line]);
  18. for (int c = r == startRow ? startCol : 0;
  19. c < (r == endRow ? endCol : line.Count);
  20. c++)
  21. {
  22. Cell cell = line [c]; // Copy value to a new variable
  23. cell.Attribute = attribute; // Modify the copy
  24. line [c] = cell; // Assign the modified copy back
  25. }
  26. selectedCellsChanged.Add ([.. GetLine (r)]);
  27. }
  28. GetSelectedRegion ();
  29. IsSelecting = false;
  30. _historyText.Add (
  31. [.. selectedCellsOriginal],
  32. new Point (startCol, startRow)
  33. );
  34. _historyText.Add (
  35. [.. selectedCellsChanged],
  36. new Point (startCol, startRow),
  37. TextEditingLineStatus.Attribute
  38. );
  39. }
  40. }
  41. private Attribute? GetSelectedCellAttribute ()
  42. {
  43. List<Cell> line;
  44. if (SelectedLength > 0)
  45. {
  46. line = GetLine (SelectionStartRow);
  47. if (line [Math.Min (SelectionStartColumn, line.Count - 1)].Attribute is { } attributeSel)
  48. {
  49. return new (attributeSel);
  50. }
  51. return GetAttributeForRole (VisualRole.Active);
  52. }
  53. line = GetCurrentLine ();
  54. if (line [Math.Min (CurrentColumn, line.Count - 1)].Attribute is { } attribute)
  55. {
  56. return new (attribute);
  57. }
  58. return GetAttributeForRole (VisualRole.Active);
  59. }
  60. /// <summary>Invoked when the normal color is drawn.</summary>
  61. public event EventHandler<CellEventArgs>? DrawNormalColor;
  62. /// <summary>Invoked when the ready only color is drawn.</summary>
  63. public event EventHandler<CellEventArgs>? DrawReadOnlyColor;
  64. /// <summary>Invoked when the selection color is drawn.</summary>
  65. public event EventHandler<CellEventArgs>? DrawSelectionColor;
  66. /// <summary>
  67. /// Invoked when the used color is drawn. The Used Color is used to indicate if the <see cref="Key.InsertChar"/>
  68. /// was pressed and enabled.
  69. /// </summary>
  70. public event EventHandler<CellEventArgs>? DrawUsedColor;
  71. /// <inheritdoc/>
  72. protected override bool OnDrawingContent ()
  73. {
  74. _isDrawing = true;
  75. SetAttributeForRole (Enabled ? VisualRole.Editable : VisualRole.Disabled);
  76. (int width, int height) offB = OffSetBackground ();
  77. int right = Viewport.Width + offB.width;
  78. int bottom = Viewport.Height + offB.height;
  79. var row = 0;
  80. for (int idxRow = _topRow; idxRow < _model.Count; idxRow++)
  81. {
  82. List<Cell> line = _model.GetLine (idxRow);
  83. int lineRuneCount = line.Count;
  84. var col = 0;
  85. Move (0, row);
  86. for (int idxCol = _leftColumn; idxCol < lineRuneCount; idxCol++)
  87. {
  88. string text = idxCol >= lineRuneCount ? " " : line [idxCol].Grapheme;
  89. int cols = text.GetColumns (false);
  90. if (idxCol < line.Count && IsSelecting && PointInSelection (idxCol, idxRow))
  91. {
  92. OnDrawSelectionColor (line, idxCol, idxRow);
  93. }
  94. else if (idxCol == CurrentColumn && idxRow == CurrentRow && !IsSelecting && !Used && HasFocus && idxCol < lineRuneCount)
  95. {
  96. OnDrawUsedColor (line, idxCol, idxRow);
  97. }
  98. else if (ReadOnly)
  99. {
  100. OnDrawReadOnlyColor (line, idxCol, idxRow);
  101. }
  102. else
  103. {
  104. OnDrawNormalColor (line, idxCol, idxRow);
  105. }
  106. if (text == "\t")
  107. {
  108. cols += TabWidth + 1;
  109. if (col + cols > right)
  110. {
  111. cols = right - col;
  112. }
  113. for (var i = 0; i < cols; i++)
  114. {
  115. if (col + i < right)
  116. {
  117. AddRune (col + i, row, (Rune)' ');
  118. }
  119. }
  120. }
  121. else
  122. {
  123. AddStr (col, row, text);
  124. // Ensures that cols less than 0 to be 1 because it will be converted to a printable rune
  125. cols = Math.Max (cols, 1);
  126. }
  127. if (!TextModel.SetCol (ref col, Viewport.Right, cols))
  128. {
  129. break;
  130. }
  131. if (idxCol + 1 < lineRuneCount && col + line [idxCol + 1].Grapheme.GetColumns () > right)
  132. {
  133. break;
  134. }
  135. }
  136. if (col < right)
  137. {
  138. SetAttributeForRole (ReadOnly ? VisualRole.ReadOnly : VisualRole.Editable);
  139. ClearRegion (col, row, right, row + 1);
  140. }
  141. row++;
  142. }
  143. if (row < bottom)
  144. {
  145. SetAttributeForRole (ReadOnly ? VisualRole.ReadOnly : VisualRole.Editable);
  146. ClearRegion (Viewport.Left, row, right, bottom);
  147. }
  148. _isDrawing = false;
  149. return false;
  150. }
  151. /// <summary>
  152. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  153. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  154. /// <see cref="View.SetAttribute"/> Defaults to <see cref="Scheme.Normal"/>.
  155. /// </summary>
  156. /// <param name="line">The line.</param>
  157. /// <param name="idxCol">The col index.</param>
  158. /// <param name="idxRow">The row index.</param>
  159. protected virtual void OnDrawNormalColor (List<Cell> line, int idxCol, int idxRow)
  160. {
  161. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  162. var ev = new CellEventArgs (line, idxCol, unwrappedPos);
  163. DrawNormalColor?.Invoke (this, ev);
  164. if (line [idxCol].Attribute is { })
  165. {
  166. Attribute? attribute = line [idxCol].Attribute;
  167. SetAttribute ((Attribute)attribute!);
  168. }
  169. else
  170. {
  171. SetAttribute (GetAttributeForRole (VisualRole.Normal));
  172. }
  173. }
  174. /// <summary>
  175. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  176. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  177. /// <see cref="View.SetAttribute(Attribute)"/> Defaults to <see cref="Scheme.Focus"/>.
  178. /// </summary>
  179. /// <param name="line">The line.</param>
  180. /// <param name="idxCol">The col index.</param>
  181. /// ///
  182. /// <param name="idxRow">The row index.</param>
  183. protected virtual void OnDrawReadOnlyColor (List<Cell> line, int idxCol, int idxRow)
  184. {
  185. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  186. var ev = new CellEventArgs (line, idxCol, unwrappedPos);
  187. DrawReadOnlyColor?.Invoke (this, ev);
  188. Attribute? cellAttribute = line [idxCol].Attribute is { } ? line [idxCol].Attribute : GetAttributeForRole (VisualRole.ReadOnly);
  189. if (cellAttribute!.Value.Foreground == cellAttribute.Value.Background)
  190. {
  191. SetAttribute (new (cellAttribute.Value.Foreground, cellAttribute.Value.Background, cellAttribute.Value.Style));
  192. }
  193. else
  194. {
  195. SetAttributeForRole (VisualRole.ReadOnly);
  196. }
  197. }
  198. /// <summary>
  199. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  200. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  201. /// <see cref="View.SetAttribute(Attribute)"/> Defaults to <see cref="Scheme.Focus"/>.
  202. /// </summary>
  203. /// <param name="line">The line.</param>
  204. /// <param name="idxCol">The col index.</param>
  205. /// ///
  206. /// <param name="idxRow">The row index.</param>
  207. protected virtual void OnDrawSelectionColor (List<Cell> line, int idxCol, int idxRow)
  208. {
  209. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  210. var ev = new CellEventArgs (line, idxCol, unwrappedPos);
  211. DrawSelectionColor?.Invoke (this, ev);
  212. if (line [idxCol].Attribute is { })
  213. {
  214. Attribute? attribute = line [idxCol].Attribute;
  215. Attribute? active = GetAttributeForRole (VisualRole.Active);
  216. SetAttribute (new (active!.Value.Foreground, active.Value.Background, attribute!.Value.Style));
  217. }
  218. else
  219. {
  220. SetAttributeForRole (VisualRole.Active);
  221. }
  222. }
  223. /// <summary>
  224. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  225. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  226. /// <see cref="View.SetAttribute(Attribute)"/> Defaults to <see cref="Scheme.HotFocus"/>.
  227. /// </summary>
  228. /// <param name="line">The line.</param>
  229. /// <param name="idxCol">The col index.</param>
  230. /// ///
  231. /// <param name="idxRow">The row index.</param>
  232. protected virtual void OnDrawUsedColor (List<Cell> line, int idxCol, int idxRow)
  233. {
  234. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  235. var ev = new CellEventArgs (line, idxCol, unwrappedPos);
  236. DrawUsedColor?.Invoke (this, ev);
  237. if (line [idxCol].Attribute is { })
  238. {
  239. Attribute? attribute = line [idxCol].Attribute;
  240. SetValidUsedColor (attribute!);
  241. }
  242. else
  243. {
  244. SetValidUsedColor (GetAttributeForRole (VisualRole.Focus));
  245. }
  246. }
  247. private void DoSetNeedsDraw (Rectangle rect)
  248. {
  249. if (_wrapNeeded)
  250. {
  251. SetNeedsDraw ();
  252. }
  253. else
  254. {
  255. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  256. //SetNeedsDraw (rect);
  257. SetNeedsDraw ();
  258. }
  259. }
  260. private Attribute? GetSelectedAttribute (int row, int col)
  261. {
  262. if (!InheritsPreviousAttribute || (Lines == 1 && GetLine (Lines).Count == 0))
  263. {
  264. return null;
  265. }
  266. List<Cell> line = GetLine (row);
  267. int foundRow = row;
  268. while (line.Count == 0)
  269. {
  270. if (foundRow == 0 && line.Count == 0)
  271. {
  272. return null;
  273. }
  274. foundRow--;
  275. line = GetLine (foundRow);
  276. }
  277. int foundCol = foundRow < row ? line.Count - 1 : Math.Min (col, line.Count - 1);
  278. Cell cell = line [foundCol];
  279. return cell.Attribute;
  280. }
  281. /// <inheritdoc/>
  282. protected override bool OnGettingAttributeForRole (in VisualRole role, ref Attribute currentAttribute)
  283. {
  284. if (role == VisualRole.Normal)
  285. {
  286. currentAttribute = GetAttributeForRole (VisualRole.Editable);
  287. return true;
  288. }
  289. return base.OnGettingAttributeForRole (role, ref currentAttribute);
  290. }
  291. }