Label.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Terminal {
  6. public enum TextAlignment {
  7. Left, Right, Centered, Justified
  8. }
  9. /// <summary>
  10. /// Label widget, displays a string at a given position, can include multiple lines.
  11. /// </summary>
  12. public class Label : View {
  13. List<string> lines = new List<string> ();
  14. bool recalcPending = true;
  15. string text;
  16. TextAlignment textAlignment;
  17. static Rect CalcRect (int x, int y, string s)
  18. {
  19. int mw = 0;
  20. int ml = 1;
  21. int cols = 0;
  22. foreach (var c in s) {
  23. if (c == '\n'){
  24. ml++;
  25. if (cols > mw)
  26. mw = cols;
  27. cols = 0;
  28. } else
  29. cols++;
  30. }
  31. return new Rect (x, y, cols, ml);
  32. }
  33. /// <summary>
  34. /// Public constructor: creates a label at the given
  35. /// coordinate with the given string, computes the bounding box
  36. /// based on the size of the string, assumes that the string contains
  37. /// newlines for multiple lines, no special breaking rules are used.
  38. /// </summary>
  39. public Label (int x, int y, string text) : this (CalcRect (x, y, text), text)
  40. {
  41. }
  42. /// <summary>
  43. /// Public constructor: creates a label at the given
  44. /// coordinate with the given string and uses the specified
  45. /// frame for the string.
  46. /// </summary>
  47. public Label (Rect rect, string text) : base (rect)
  48. {
  49. this.text = text;
  50. }
  51. static char [] whitespace = new char [] { ' ', '\t' };
  52. string ClipAndJustify (string str)
  53. {
  54. int slen = str.Length;
  55. if (slen > Frame.Width)
  56. return str.Substring (0, Frame.Width);
  57. else {
  58. if (textAlignment == TextAlignment.Justified) {
  59. var words = str.Split (whitespace, StringSplitOptions.RemoveEmptyEntries);
  60. int textCount = words.Sum ((arg) => arg.Length);
  61. var spaces = (Frame.Width - textCount) / (words.Length - 1);
  62. var extras = (Frame.Width - textCount) % words.Length;
  63. var s = new System.Text.StringBuilder ();
  64. //s.Append ($"tc={textCount} sp={spaces},x={extras} - ");
  65. for (int w = 0; w < words.Length; w++) {
  66. var x = words [w];
  67. s.Append (x);
  68. if (w + 1 < words.Length)
  69. for (int i = 0; i < spaces; i++)
  70. s.Append (' ');
  71. if (extras > 0) {
  72. s.Append ('_');
  73. extras--;
  74. }
  75. }
  76. return s.ToString ();
  77. }
  78. return str;
  79. }
  80. }
  81. void Recalc ()
  82. {
  83. lines.Clear ();
  84. if (text.IndexOf ('\n') == -1) {
  85. lines.Add (ClipAndJustify (text));
  86. return;
  87. }
  88. int textLen = text.Length;
  89. int lp = 0;
  90. for (int i = 0; i < textLen; i++) {
  91. char c = text [i];
  92. if (c == '\n') {
  93. lines.Add (ClipAndJustify (text.Substring (lp, i - lp)));
  94. lp = i + 1;
  95. }
  96. }
  97. recalcPending = false;
  98. }
  99. public override void Redraw (Rect region)
  100. {
  101. if (recalcPending)
  102. Recalc ();
  103. if (TextColor != -1)
  104. Driver.SetAttribute (TextColor);
  105. else
  106. Driver.SetAttribute(Colors.Base.Normal);
  107. Clear ();
  108. Move (Frame.X, Frame.Y);
  109. for (int line = 0; line < lines.Count; line++) {
  110. if (line < region.Top || line >= region.Bottom)
  111. continue;
  112. var str = lines [line];
  113. int x;
  114. switch (textAlignment) {
  115. case TextAlignment.Left:
  116. case TextAlignment.Justified:
  117. x = 0;
  118. break;
  119. case TextAlignment.Right:
  120. x = Frame.Right - str.Length;
  121. break;
  122. case TextAlignment.Centered:
  123. x = Frame.Left + (Frame.Width - str.Length) / 2;
  124. break;
  125. default:
  126. throw new ArgumentOutOfRangeException ();
  127. }
  128. Move (x, line);
  129. Driver.AddStr (str);
  130. }
  131. }
  132. /// <summary>
  133. /// The text displayed by this widget.
  134. /// </summary>
  135. public virtual string Text {
  136. get => text;
  137. set {
  138. text = value;
  139. recalcPending = true;
  140. SetNeedsDisplay ();
  141. }
  142. }
  143. public TextAlignment TextAlignment {
  144. get => textAlignment;
  145. set {
  146. textAlignment = value;
  147. SetNeedsDisplay ();
  148. }
  149. }
  150. /// <summary>
  151. /// The color used for the label
  152. /// </summary>
  153. Attribute textColor = -1;
  154. public Attribute TextColor {
  155. get => textColor;
  156. set {
  157. textColor = value;
  158. SetNeedsDisplay ();
  159. }
  160. }
  161. }
  162. }