Label.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // Label.cs: Label control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using NStack;
  11. namespace Terminal.Gui {
  12. /// <summary>
  13. /// Text alignment enumeration, controls how text is displayed.
  14. /// </summary>
  15. public enum TextAlignment {
  16. /// <summary>
  17. /// Aligns the text to the left of the frame.
  18. /// </summary>
  19. Left,
  20. /// <summary>
  21. /// Aligns the text to the right side of the frame.
  22. /// </summary>
  23. Right,
  24. /// <summary>
  25. /// Centers the text in the frame.
  26. /// </summary>
  27. Centered,
  28. /// <summary>
  29. /// Shows the text as justified text in the frame.
  30. /// </summary>
  31. Justified
  32. }
  33. /// <summary>
  34. /// The Label <see cref="View"/> displays a string at a given position and supports multiple lines separted by newline characters.
  35. /// </summary>
  36. public class Label : View {
  37. List<ustring> lines = new List<ustring> ();
  38. bool recalcPending = true;
  39. ustring text;
  40. TextAlignment textAlignment;
  41. static Rect CalcRect (int x, int y, ustring s)
  42. {
  43. int mw = 0;
  44. int ml = 1;
  45. int cols = 0;
  46. foreach (var rune in s) {
  47. if (rune == '\n') {
  48. ml++;
  49. if (cols > mw)
  50. mw = cols;
  51. cols = 0;
  52. } else
  53. cols++;
  54. }
  55. if (cols > mw)
  56. mw = cols;
  57. return new Rect (x, y, mw, ml);
  58. }
  59. /// <summary>
  60. /// Initializes a new instance of <see cref="Label"/> at the given
  61. /// coordinate with the given string, computes the bounding box
  62. /// based on the size of the string, assumes that the string contains
  63. /// newlines for multiple lines, no special breaking rules are used.
  64. /// </summary>
  65. public Label (int x, int y, ustring text) : this (CalcRect (x, y, text), text)
  66. {
  67. }
  68. /// <summary>
  69. /// Initializes a new instance of <see cref="Label"/> at the given
  70. /// coordinate with the given string and uses the specified
  71. /// frame for the string.
  72. /// </summary>
  73. public Label (Rect rect, ustring text) : base (rect)
  74. {
  75. this.text = text;
  76. }
  77. /// <summary>
  78. /// Initializes a new instance of <see cref="Label"/> and configures the default Width and Height based on the text, the result is suitable for Computed layout.
  79. /// </summary>
  80. /// <param name="text">Text.</param>
  81. public Label (ustring text) : base ()
  82. {
  83. this.text = text;
  84. var r = CalcRect (0, 0, text);
  85. Width = r.Width;
  86. Height = r.Height;
  87. }
  88. static char [] whitespace = new char [] { ' ', '\t' };
  89. static ustring ClipAndJustify (ustring str, int width, TextAlignment talign)
  90. {
  91. // Get rid of any '\r' added by Windows
  92. str = str.Replace ("\r", ustring.Empty);
  93. int slen = str.RuneCount;
  94. if (slen > width){
  95. var uints = str.ToRunes (width);
  96. var runes = new Rune [uints.Length];
  97. for (int i = 0; i < uints.Length; i++)
  98. runes [i] = uints [i];
  99. return ustring.Make (runes);
  100. } else {
  101. if (talign == TextAlignment.Justified) {
  102. // TODO: ustring needs this
  103. var words = str.ToString ().Split (whitespace, StringSplitOptions.RemoveEmptyEntries);
  104. int textCount = words.Sum (arg => arg.Length);
  105. var spaces = (width- textCount) / (words.Length - 1);
  106. var extras = (width - textCount) % words.Length;
  107. var s = new System.Text.StringBuilder ();
  108. //s.Append ($"tc={textCount} sp={spaces},x={extras} - ");
  109. for (int w = 0; w < words.Length; w++) {
  110. var x = words [w];
  111. s.Append (x);
  112. if (w + 1 < words.Length)
  113. for (int i = 0; i < spaces; i++)
  114. s.Append (' ');
  115. if (extras > 0) {
  116. //s.Append ('_');
  117. extras--;
  118. }
  119. }
  120. return ustring.Make (s.ToString ());
  121. }
  122. return str;
  123. }
  124. }
  125. void Recalc ()
  126. {
  127. recalcPending = false;
  128. Recalc (text, lines, Frame.Width, textAlignment);
  129. }
  130. static void Recalc (ustring textStr, List<ustring> lineResult, int width, TextAlignment talign)
  131. {
  132. lineResult.Clear ();
  133. if (textStr.IndexOf ('\n') == -1) {
  134. lineResult.Add (ClipAndJustify (textStr, width, talign));
  135. return;
  136. }
  137. int textLen = textStr.Length;
  138. int lp = 0;
  139. for (int i = 0; i < textLen; i++) {
  140. Rune c = textStr [i];
  141. if (c == '\n') {
  142. lineResult.Add (ClipAndJustify (textStr [lp, i], width, talign));
  143. lp = i + 1;
  144. }
  145. }
  146. lineResult.Add(ClipAndJustify(textStr[lp, textLen], width, talign));
  147. }
  148. ///<inheritdoc/>
  149. public override void Redraw (Rect bounds)
  150. {
  151. if (recalcPending)
  152. Recalc ();
  153. if (TextColor != -1)
  154. Driver.SetAttribute (TextColor);
  155. else
  156. Driver.SetAttribute (ColorScheme.Normal);
  157. Clear ();
  158. for (int line = 0; line < lines.Count; line++) {
  159. if (line < bounds.Top || line > bounds.Bottom)
  160. continue;
  161. var str = lines [line];
  162. int x;
  163. switch (textAlignment) {
  164. case TextAlignment.Left:
  165. x = 0;
  166. break;
  167. case TextAlignment.Justified:
  168. Recalc ();
  169. x = Bounds.Left;
  170. break;
  171. case TextAlignment.Right:
  172. x = Bounds.Right - str.Length;
  173. break;
  174. case TextAlignment.Centered:
  175. x = Bounds.Left + (Bounds.Width - str.Length) / 2;
  176. break;
  177. default:
  178. throw new ArgumentOutOfRangeException ();
  179. }
  180. Move (x, line);
  181. Driver.AddStr (str);
  182. }
  183. }
  184. /// <summary>
  185. /// Computes the number of lines needed to render the specified text by the <see cref="Label"/> view
  186. /// </summary>
  187. /// <returns>Number of lines.</returns>
  188. /// <param name="text">Text, may contain newlines.</param>
  189. /// <param name="width">The width for the text.</param>
  190. public static int MeasureLines (ustring text, int width)
  191. {
  192. var result = new List<ustring> ();
  193. Recalc (text, result, width, TextAlignment.Left);
  194. return result.Count;
  195. }
  196. /// <summary>
  197. /// Computes the the max width of a line or multilines needed to render by the Label control
  198. /// </summary>
  199. /// <returns>Max width of lines.</returns>
  200. /// <param name="text">Text, may contain newlines.</param>
  201. /// <param name="width">The width for the text.</param>
  202. public static int MaxWidth(ustring text, int width)
  203. {
  204. var result = new List<ustring>();
  205. Recalc(text, result, width, TextAlignment.Left);
  206. return result.Max(s => s.RuneCount);
  207. }
  208. /// <summary>
  209. /// The text displayed by the <see cref="Label"/>.
  210. /// </summary>
  211. public virtual ustring Text {
  212. get => text;
  213. set {
  214. text = value;
  215. recalcPending = true;
  216. SetNeedsDisplay ();
  217. }
  218. }
  219. /// <summary>
  220. /// Controls the text-alignemtn property of the label, changing it will redisplay the <see cref="Label"/>.
  221. /// </summary>
  222. /// <value>The text alignment.</value>
  223. public TextAlignment TextAlignment {
  224. get => textAlignment;
  225. set {
  226. textAlignment = value;
  227. SetNeedsDisplay ();
  228. }
  229. }
  230. Attribute textColor = -1;
  231. /// <summary>
  232. /// The color used for the <see cref="Label"/>.
  233. /// </summary>
  234. public Attribute TextColor {
  235. get => textColor;
  236. set {
  237. textColor = value;
  238. SetNeedsDisplay ();
  239. }
  240. }
  241. }
  242. }