2
0

ScrollView.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // ScrollView.cs: ScrollView and ScrollBarView views.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // TODO:
  9. // - Mouse handling in scrollbarview
  10. // - focus in scrollview
  11. // - keyboard handling in scrollview to scroll
  12. // - focus handling in scrollview to auto scroll to focused view
  13. // - Raise events
  14. using System;
  15. namespace Terminal.Gui {
  16. /// <summary>
  17. /// ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical
  18. /// </summary>
  19. /// <remarks>
  20. /// The scrollbar is drawn to be a representation of the Size, assuming that the
  21. /// scroll position is set at Position.
  22. /// </remarks>
  23. public class ScrollBarView : View {
  24. bool vertical;
  25. int size, position;
  26. /// <summary>
  27. /// The size that this scrollbar represents
  28. /// </summary>
  29. /// <value>The size.</value>
  30. public int Size {
  31. get => size;
  32. set {
  33. size = value;
  34. SetNeedsDisplay ();
  35. }
  36. }
  37. /// <summary>
  38. /// The position to show the scrollbar at.
  39. /// </summary>
  40. /// <value>The position.</value>
  41. public int Position {
  42. get => position;
  43. set {
  44. position = value;
  45. SetNeedsDisplay ();
  46. }
  47. }
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="T:Terminal.Gui.ScrollBarView"/> class.
  50. /// </summary>
  51. /// <param name="rect">Frame for the scrollbar.</param>
  52. /// <param name="size">The size that this scrollbar represents.</param>
  53. /// <param name="position">The position within this scrollbar.</param>
  54. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwize, the scrollbar is horizontal.</param>
  55. public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect)
  56. {
  57. vertical = isVertical;
  58. this.position = position;
  59. this.size = size;
  60. }
  61. /// <summary>
  62. /// Redraw the scrollbar
  63. /// </summary>
  64. /// <param name="region">Region to be redrawn.</param>
  65. public override void Redraw(Rect region)
  66. {
  67. Driver.SetAttribute (ColorScheme.Normal);
  68. if (vertical) {
  69. if (region.Right < Bounds.Width - 1)
  70. return;
  71. var col = Bounds.Width - 1;
  72. var bh = Bounds.Height;
  73. SpecialChar special;
  74. if (bh < 3) {
  75. var by1 = position * bh / Size;
  76. var by2 = (position + bh) * bh / Size;
  77. for (int y = 0; y < bh; y++) {
  78. Move (col, y);
  79. if (y < by1 || y > by2)
  80. special = SpecialChar.Stipple;
  81. else
  82. special = SpecialChar.Diamond;
  83. Driver.AddSpecial (special);
  84. }
  85. } else {
  86. bh -= 2;
  87. var by1 = position * bh / Size;
  88. var by2 = (position + bh) * bh / Size;
  89. Move (col, 0);
  90. Driver.AddRune ('^');
  91. Move (col, Bounds.Height - 1);
  92. Driver.AddRune ('v');
  93. for (int y = 0; y < bh; y++) {
  94. Move (col, y+1);
  95. if (y < by1 || y > by2)
  96. special = SpecialChar.Stipple;
  97. else {
  98. if (by2 - by1 == 0)
  99. special = SpecialChar.Diamond;
  100. else {
  101. if (y == by1)
  102. special = SpecialChar.TopTee;
  103. else if (y == by2)
  104. special = SpecialChar.BottomTee;
  105. else
  106. special = SpecialChar.VLine;
  107. }
  108. }
  109. Driver.AddSpecial (special);
  110. }
  111. }
  112. } else {
  113. if (region.Bottom < Bounds.Height - 1)
  114. return;
  115. var row = Bounds.Height - 1;
  116. var bw = Bounds.Width;
  117. if (bw < 3) {
  118. } else {
  119. bw -= 2;
  120. var bx1 = position * bw / Size;
  121. var bx2 = (position + bw) * bw / Size;
  122. Move (0, row);
  123. Driver.AddRune ('<');
  124. for (int x = 0; x < bw; x++) {
  125. SpecialChar special;
  126. if (x < bx1 || x > bx2) {
  127. special = SpecialChar.Stipple;
  128. } else {
  129. if (bx2 - bx1 == 0)
  130. special = SpecialChar.Diamond;
  131. else {
  132. if (x == bx1)
  133. special = SpecialChar.LeftTee;
  134. else if (x == bx2)
  135. special = SpecialChar.RightTee;
  136. else
  137. special = SpecialChar.HLine;
  138. }
  139. }
  140. Driver.AddSpecial (special);
  141. }
  142. Driver.AddRune ('>');
  143. }
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// Scrollviews are views that present a window into a virtual space where children views are added. Similar to the iOS UIScrollView.
  149. /// </summary>
  150. /// <remarks>
  151. /// <para>
  152. /// The subviews that are added to this scrollview are offset by the
  153. /// ContentOffset property. The view itself is a window into the
  154. /// space represented by the ContentSize.
  155. /// </para>
  156. /// <para>
  157. ///
  158. /// </para>
  159. /// </remarks>
  160. public class ScrollView : View {
  161. View contentView;
  162. ScrollBarView vertical, horizontal;
  163. public ScrollView (Rect frame) : base (frame)
  164. {
  165. contentView = new View (frame);
  166. vertical = new ScrollBarView (new Rect (frame.Width - 1, 0, 1, frame.Height), frame.Height, 0, isVertical: true);
  167. horizontal = new ScrollBarView (new Rect (0, frame.Height-1, frame.Width-1, 1), frame.Width-1, 0, isVertical: false);
  168. base.Add (contentView);
  169. CanFocus = true;
  170. }
  171. Size contentSize;
  172. Point contentOffset;
  173. bool showHorizontalScrollIndicator;
  174. bool showVerticalScrollIndicator;
  175. /// <summary>
  176. /// Represents the contents of the data shown inside the scrolview
  177. /// </summary>
  178. /// <value>The size of the content.</value>
  179. public Size ContentSize {
  180. get {
  181. return contentSize;
  182. }
  183. set {
  184. contentSize = value;
  185. contentView.Frame = new Rect (contentOffset, value);
  186. vertical.Size = contentSize.Height;
  187. horizontal.Size = contentSize.Width;
  188. }
  189. }
  190. /// <summary>
  191. /// Represents the top left corner coordinate that is displayed by the scrollview
  192. /// </summary>
  193. /// <value>The content offset.</value>
  194. public Point ContentOffset {
  195. get {
  196. return contentOffset;
  197. }
  198. set {
  199. contentOffset = new Point (-value.X, -value.Y);
  200. contentView.Frame = new Rect (contentOffset, contentSize);
  201. vertical.Position = Math.Max (0, -contentOffset.Y);
  202. horizontal.Position = Math.Max (0, -contentOffset.X);
  203. }
  204. }
  205. /// <summary>
  206. /// Adds the view to the scrollview.
  207. /// </summary>
  208. /// <param name="view">The view to add to the scrollview.</param>
  209. public override void Add (View view)
  210. {
  211. contentView.Add (view);
  212. }
  213. /// <summary>
  214. /// Gets or sets the visibility for the horizontal scroll indicator.
  215. /// </summary>
  216. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  217. public bool ShowHorizontalScrollIndicator {
  218. get => showHorizontalScrollIndicator;
  219. set {
  220. if (value == showHorizontalScrollIndicator)
  221. return;
  222. showHorizontalScrollIndicator = value;
  223. SetNeedsDisplay ();
  224. if (value)
  225. base.Add (horizontal);
  226. else
  227. Remove (horizontal);
  228. }
  229. }
  230. /// <summary>
  231. /// /// Gets or sets the visibility for the vertical scroll indicator.
  232. /// </summary>
  233. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  234. public bool ShowVerticalScrollIndicator {
  235. get => showVerticalScrollIndicator;
  236. set {
  237. if (value == showVerticalScrollIndicator)
  238. return;
  239. showVerticalScrollIndicator = value;
  240. SetNeedsDisplay ();
  241. if (value)
  242. base.Add (vertical);
  243. else
  244. Remove (vertical);
  245. }
  246. }
  247. /// <summary>
  248. /// This event is raised when the contents have scrolled
  249. /// </summary>
  250. public event Action<ScrollView> Scrolled;
  251. public override void Redraw(Rect region)
  252. {
  253. var oldClip = ClipToBounds ();
  254. base.Redraw(region);
  255. Driver.Clip = oldClip;
  256. Driver.SetAttribute (ColorScheme.Normal);
  257. }
  258. public override void PositionCursor()
  259. {
  260. if (Subviews.Count == 0)
  261. Driver.Move (0, 0);
  262. else
  263. base.PositionCursor ();
  264. }
  265. }
  266. }