Label.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.RuneCount;
  90. if (slen > width){
  91. var uints = str.ToRunes (width);
  92. var runes = new Rune [uints.Length];
  93. for (int i = 0; i < uints.Length; i++)
  94. runes [i] = uints [i];
  95. return ustring.Make (runes);
  96. } else {
  97. if (talign == TextAlignment.Justified) {
  98. // TODO: ustring needs this
  99. var words = str.ToString ().Split (whitespace, StringSplitOptions.RemoveEmptyEntries);
  100. int textCount = words.Sum (arg => arg.Length);
  101. var spaces = (width- textCount) / (words.Length - 1);
  102. var extras = (width - textCount) % words.Length;
  103. var s = new System.Text.StringBuilder ();
  104. //s.Append ($"tc={textCount} sp={spaces},x={extras} - ");
  105. for (int w = 0; w < words.Length; w++) {
  106. var x = words [w];
  107. s.Append (x);
  108. if (w + 1 < words.Length)
  109. for (int i = 0; i < spaces; i++)
  110. s.Append (' ');
  111. if (extras > 0) {
  112. s.Append ('_');
  113. extras--;
  114. }
  115. }
  116. return ustring.Make (s.ToString ());
  117. }
  118. return str;
  119. }
  120. }
  121. void Recalc ()
  122. {
  123. recalcPending = false;
  124. Recalc (text, lines, Frame.Width, textAlignment);
  125. }
  126. static void Recalc (ustring textStr, List<ustring> lineResult, int width, TextAlignment talign)
  127. {
  128. lineResult.Clear ();
  129. if (textStr.IndexOf ('\n') == -1) {
  130. lineResult.Add (ClipAndJustify (textStr, width, talign));
  131. return;
  132. }
  133. int textLen = textStr.Length;
  134. int lp = 0;
  135. for (int i = 0; i < textLen; i++) {
  136. Rune c = textStr [i];
  137. if (c == '\n') {
  138. lineResult.Add (ClipAndJustify (textStr [lp, i], width, talign));
  139. lp = i + 1;
  140. }
  141. }
  142. }
  143. public override void Redraw (Rect region)
  144. {
  145. if (recalcPending)
  146. Recalc ();
  147. if (TextColor != -1)
  148. Driver.SetAttribute (TextColor);
  149. else
  150. Driver.SetAttribute (ColorScheme.Normal);
  151. Clear ();
  152. Move (Frame.X, Frame.Y);
  153. for (int line = 0; line < lines.Count; line++) {
  154. if (line < region.Top || line >= region.Bottom)
  155. continue;
  156. var str = lines [line];
  157. int x;
  158. switch (textAlignment) {
  159. case TextAlignment.Left:
  160. case TextAlignment.Justified:
  161. x = 0;
  162. break;
  163. case TextAlignment.Right:
  164. x = Frame.Right - str.Length;
  165. break;
  166. case TextAlignment.Centered:
  167. x = Frame.Left + (Frame.Width - str.Length) / 2;
  168. break;
  169. default:
  170. throw new ArgumentOutOfRangeException ();
  171. }
  172. Move (x, line);
  173. Driver.AddStr (str);
  174. }
  175. }
  176. /// <summary>
  177. /// Computes the number of lines needed to render the specified text by the Label control
  178. /// </summary>
  179. /// <returns>Number of lines.</returns>
  180. /// <param name="text">Text, may contain newlines.</param>
  181. /// <param name="width">The width for the text.</param>
  182. public static int MeasureLines (ustring text, int width)
  183. {
  184. var result = new List<ustring> ();
  185. Recalc (text, result, width, TextAlignment.Left);
  186. return result.Count;
  187. }
  188. /// <summary>
  189. /// The text displayed by this widget.
  190. /// </summary>
  191. public virtual ustring Text {
  192. get => text;
  193. set {
  194. text = value;
  195. recalcPending = true;
  196. SetNeedsDisplay ();
  197. }
  198. }
  199. /// <summary>
  200. /// Controls the text-alignemtn property of the label, changing it will redisplay the label.
  201. /// </summary>
  202. /// <value>The text alignment.</value>
  203. public TextAlignment TextAlignment {
  204. get => textAlignment;
  205. set {
  206. textAlignment = value;
  207. SetNeedsDisplay ();
  208. }
  209. }
  210. Attribute textColor = -1;
  211. /// <summary>
  212. /// The color used for the label
  213. /// </summary>
  214. public Attribute TextColor {
  215. get => textColor;
  216. set {
  217. textColor = value;
  218. SetNeedsDisplay ();
  219. }
  220. }
  221. }
  222. }