Label.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 line as justified text in the line.
  30. /// </summary>
  31. Justified
  32. }
  33. /// <summary>
  34. /// Label view, displays a string at a given position, can include multiple lines.
  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. return new Rect (x, y, cols, ml);
  56. }
  57. /// <summary>
  58. /// Public constructor: creates a label at the given
  59. /// coordinate with the given string, computes the bounding box
  60. /// based on the size of the string, assumes that the string contains
  61. /// newlines for multiple lines, no special breaking rules are used.
  62. /// </summary>
  63. public Label (int x, int y, ustring text) : this (CalcRect (x, y, text), text)
  64. {
  65. }
  66. /// <summary>
  67. /// Public constructor: creates a label at the given
  68. /// coordinate with the given string and uses the specified
  69. /// frame for the string.
  70. /// </summary>
  71. public Label (Rect rect, ustring text) : base (rect)
  72. {
  73. this.text = text;
  74. }
  75. /// <summary>
  76. /// Public constructor: creates a label and configures the default Width and Height based on the text, the result is suitable for Computed layout.
  77. /// </summary>
  78. /// <param name="text">Text.</param>
  79. public Label (ustring text) : base ()
  80. {
  81. this.text = text;
  82. var r = CalcRect (0, 0, text);
  83. Width = r.Width;
  84. Height = r.Height;
  85. }
  86. static char [] whitespace = new char [] { ' ', '\t' };
  87. static ustring ClipAndJustify (ustring str, int width, TextAlignment talign)
  88. {
  89. int slen = str.Length;
  90. if (slen > width)
  91. return str [0, width];
  92. else {
  93. if (talign == TextAlignment.Justified) {
  94. // TODO: ustring needs this
  95. var words = str.ToString ().Split (whitespace, StringSplitOptions.RemoveEmptyEntries);
  96. int textCount = words.Sum (arg => arg.Length);
  97. var spaces = (width- textCount) / (words.Length - 1);
  98. var extras = (width - textCount) % words.Length;
  99. var s = new System.Text.StringBuilder ();
  100. //s.Append ($"tc={textCount} sp={spaces},x={extras} - ");
  101. for (int w = 0; w < words.Length; w++) {
  102. var x = words [w];
  103. s.Append (x);
  104. if (w + 1 < words.Length)
  105. for (int i = 0; i < spaces; i++)
  106. s.Append (' ');
  107. if (extras > 0) {
  108. s.Append ('_');
  109. extras--;
  110. }
  111. }
  112. return ustring.Make (s.ToString ());
  113. }
  114. return str;
  115. }
  116. }
  117. void Recalc ()
  118. {
  119. recalcPending = false;
  120. Recalc (text, lines, Frame.Width, textAlignment);
  121. }
  122. static void Recalc (ustring textStr, List<ustring> lineResult, int width, TextAlignment talign)
  123. {
  124. lineResult.Clear ();
  125. if (textStr.IndexOf ('\n') == -1) {
  126. lineResult.Add (ClipAndJustify (textStr, width, talign));
  127. return;
  128. }
  129. int textLen = textStr.Length;
  130. int lp = 0;
  131. for (int i = 0; i < textLen; i++) {
  132. Rune c = textStr [i];
  133. if (c == '\n') {
  134. lineResult.Add (ClipAndJustify (textStr [lp, i], width, talign));
  135. lp = i + 1;
  136. }
  137. }
  138. }
  139. public override void Redraw (Rect region)
  140. {
  141. if (recalcPending)
  142. Recalc ();
  143. if (TextColor != -1)
  144. Driver.SetAttribute (TextColor);
  145. else
  146. Driver.SetAttribute (ColorScheme.Normal);
  147. Clear ();
  148. Move (Frame.X, Frame.Y);
  149. for (int line = 0; line < lines.Count; line++) {
  150. if (line < region.Top || line >= region.Bottom)
  151. continue;
  152. var str = lines [line];
  153. int x;
  154. switch (textAlignment) {
  155. case TextAlignment.Left:
  156. case TextAlignment.Justified:
  157. x = 0;
  158. break;
  159. case TextAlignment.Right:
  160. x = Frame.Right - str.Length;
  161. break;
  162. case TextAlignment.Centered:
  163. x = Frame.Left + (Frame.Width - str.Length) / 2;
  164. break;
  165. default:
  166. throw new ArgumentOutOfRangeException ();
  167. }
  168. Move (x, line);
  169. Driver.AddStr (str);
  170. }
  171. }
  172. /// <summary>
  173. /// Computes the number of lines needed to render the specified text by the Label control
  174. /// </summary>
  175. /// <returns>Number of lines.</returns>
  176. /// <param name="text">Text, may contain newlines.</param>
  177. /// <param name="width">The width for the text.</param>
  178. public static int MeasureLines (ustring text, int width)
  179. {
  180. var result = new List<ustring> ();
  181. Recalc (text, result, width, TextAlignment.Left);
  182. return result.Count;
  183. }
  184. /// <summary>
  185. /// The text displayed by this widget.
  186. /// </summary>
  187. public virtual ustring Text {
  188. get => text;
  189. set {
  190. text = value;
  191. recalcPending = true;
  192. SetNeedsDisplay ();
  193. }
  194. }
  195. /// <summary>
  196. /// Controls the text-alignemtn property of the label, changing it will redisplay the label.
  197. /// </summary>
  198. /// <value>The text alignment.</value>
  199. public TextAlignment TextAlignment {
  200. get => textAlignment;
  201. set {
  202. textAlignment = value;
  203. SetNeedsDisplay ();
  204. }
  205. }
  206. Attribute textColor = -1;
  207. /// <summary>
  208. /// The color used for the label
  209. /// </summary>
  210. public Attribute TextColor {
  211. get => textColor;
  212. set {
  213. textColor = value;
  214. SetNeedsDisplay ();
  215. }
  216. }
  217. }
  218. }