ScrollView.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. // - Perhaps allow an option to not display the scrollbar arrow indicators?
  15. using System;
  16. namespace Terminal.Gui {
  17. /// <summary>
  18. /// ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical
  19. /// </summary>
  20. /// <remarks>
  21. /// <para>
  22. /// The scrollbar is drawn to be a representation of the Size, assuming that the
  23. /// scroll position is set at Position.
  24. /// </para>
  25. /// <para>
  26. /// If the region to display the scrollbar is larger than three characters,
  27. /// arrow indicators are drawn.
  28. /// </para>
  29. /// </remarks>
  30. public class ScrollBarView : View {
  31. bool vertical;
  32. int size, position;
  33. /// <summary>
  34. /// The size that this scrollbar represents
  35. /// </summary>
  36. /// <value>The size.</value>
  37. public int Size {
  38. get => size;
  39. set {
  40. size = value;
  41. SetNeedsDisplay ();
  42. }
  43. }
  44. /// <summary>
  45. /// This event is raised when the position on the scrollbar has changed.
  46. /// </summary>
  47. public event Action ChangedPosition;
  48. /// <summary>
  49. /// The position to show the scrollbar at.
  50. /// </summary>
  51. /// <value>The position.</value>
  52. public int Position {
  53. get => position;
  54. set {
  55. position = value;
  56. SetNeedsDisplay ();
  57. }
  58. }
  59. void SetPosition (int newPos)
  60. {
  61. Position = newPos;
  62. ChangedPosition?.Invoke ();
  63. }
  64. /// <summary>
  65. /// Initializes a new instance of the <see cref="T:Terminal.Gui.Gui.ScrollBarView"/> class.
  66. /// </summary>
  67. /// <param name="rect">Frame for the scrollbar.</param>
  68. /// <param name="size">The size that this scrollbar represents.</param>
  69. /// <param name="position">The position within this scrollbar.</param>
  70. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwize, the scrollbar is horizontal.</param>
  71. public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect)
  72. {
  73. vertical = isVertical;
  74. this.position = position;
  75. this.size = size;
  76. }
  77. /// <summary>
  78. /// Redraw the scrollbar
  79. /// </summary>
  80. /// <param name="region">Region to be redrawn.</param>
  81. public override void Redraw(Rect region)
  82. {
  83. Driver.SetAttribute (ColorScheme.Normal);
  84. if (vertical) {
  85. if (region.Right < Bounds.Width - 1)
  86. return;
  87. var col = Bounds.Width - 1;
  88. var bh = Bounds.Height;
  89. SpecialChar special;
  90. if (bh < 4) {
  91. var by1 = position * bh / Size;
  92. var by2 = (position + bh) * bh / Size;
  93. for (int y = 0; y < bh; y++) {
  94. Move (col, y);
  95. if (y < by1 || y > by2)
  96. special = SpecialChar.Stipple;
  97. else
  98. special = SpecialChar.Diamond;
  99. Driver.AddSpecial (special);
  100. }
  101. } else {
  102. bh -= 2;
  103. var by1 = position * bh / Size;
  104. var by2 = (position + bh) * bh / Size;
  105. Move (col, 0);
  106. Driver.AddRune ('^');
  107. Move (col, Bounds.Height - 1);
  108. Driver.AddRune ('v');
  109. for (int y = 0; y < bh; y++) {
  110. Move (col, y+1);
  111. if (y < by1 || y > by2)
  112. special = SpecialChar.Stipple;
  113. else {
  114. if (by2 - by1 == 0)
  115. special = SpecialChar.Diamond;
  116. else {
  117. if (y == by1)
  118. special = SpecialChar.TopTee;
  119. else if (y == by2)
  120. special = SpecialChar.BottomTee;
  121. else
  122. special = SpecialChar.VLine;
  123. }
  124. }
  125. Driver.AddSpecial (special);
  126. }
  127. }
  128. } else {
  129. if (region.Bottom < Bounds.Height - 1)
  130. return;
  131. var row = Bounds.Height - 1;
  132. var bw = Bounds.Width;
  133. SpecialChar special;
  134. if (bw < 4) {
  135. var bx1 = position * bw / Size;
  136. var bx2 = (position + bw) * bw / Size;
  137. for (int x = 0; x < bw; x++) {
  138. Move (0, x);
  139. if (x < bx1 || x > bx2)
  140. special = SpecialChar.Stipple;
  141. else
  142. special = SpecialChar.Diamond;
  143. Driver.AddSpecial (special);
  144. }
  145. } else {
  146. bw -= 2;
  147. var bx1 = position * bw / Size;
  148. var bx2 = (position + bw) * bw / Size;
  149. Move (0, row);
  150. Driver.AddRune ('<');
  151. for (int x = 0; x < bw; x++) {
  152. if (x < bx1 || x > bx2) {
  153. special = SpecialChar.Stipple;
  154. } else {
  155. if (bx2 - bx1 == 0)
  156. special = SpecialChar.Diamond;
  157. else {
  158. if (x == bx1)
  159. special = SpecialChar.LeftTee;
  160. else if (x == bx2)
  161. special = SpecialChar.RightTee;
  162. else
  163. special = SpecialChar.HLine;
  164. }
  165. }
  166. Driver.AddSpecial (special);
  167. }
  168. Driver.AddRune ('>');
  169. }
  170. }
  171. }
  172. public override bool MouseEvent(MouseEvent me)
  173. {
  174. if (me.Flags != MouseFlags.Button1Clicked)
  175. return false;
  176. int location = vertical ? me.Y : me.X;
  177. int barsize = vertical ? Bounds.Height : Bounds.Width;
  178. if (barsize < 4) {
  179. // Handle scrollbars with no buttons
  180. Console.WriteLine ("TODO at ScrollBarView2");
  181. } else {
  182. barsize -= 2;
  183. // Handle scrollbars with arrow buttons
  184. var pos = Position;
  185. if (location == 0) {
  186. if (pos > 0)
  187. SetPosition (pos - 1);
  188. } else if (location == Bounds.Width - 1){
  189. if (pos + 1 + barsize < Size)
  190. SetPosition (pos + 1);
  191. } else {
  192. Console.WriteLine ("TODO at ScrollBarView");
  193. }
  194. }
  195. return true;
  196. }
  197. }
  198. /// <summary>
  199. /// Scrollviews are views that present a window into a virtual space where children views are added. Similar to the iOS UIScrollView.
  200. /// </summary>
  201. /// <remarks>
  202. /// <para>
  203. /// The subviews that are added to this scrollview are offset by the
  204. /// ContentOffset property. The view itself is a window into the
  205. /// space represented by the ContentSize.
  206. /// </para>
  207. /// <para>
  208. ///
  209. /// </para>
  210. /// </remarks>
  211. public class ScrollView : View {
  212. View contentView;
  213. ScrollBarView vertical, horizontal;
  214. public ScrollView (Rect frame) : base (frame)
  215. {
  216. contentView = new View (frame);
  217. vertical = new ScrollBarView (new Rect (frame.Width - 1, 0, 1, frame.Height), frame.Height, 0, isVertical: true);
  218. vertical.ChangedPosition += delegate {
  219. ContentOffset = new Point (ContentOffset.X, vertical.Position);
  220. };
  221. horizontal = new ScrollBarView (new Rect (0, frame.Height-1, frame.Width-1, 1), frame.Width-1, 0, isVertical: false);
  222. horizontal.ChangedPosition += delegate {
  223. ContentOffset = new Point (horizontal.Position, ContentOffset.Y);
  224. };
  225. base.Add (contentView);
  226. CanFocus = true;
  227. }
  228. Size contentSize;
  229. Point contentOffset;
  230. bool showHorizontalScrollIndicator;
  231. bool showVerticalScrollIndicator;
  232. /// <summary>
  233. /// Represents the contents of the data shown inside the scrolview
  234. /// </summary>
  235. /// <value>The size of the content.</value>
  236. public Size ContentSize {
  237. get {
  238. return contentSize;
  239. }
  240. set {
  241. contentSize = value;
  242. contentView.Frame = new Rect (contentOffset, value);
  243. vertical.Size = contentSize.Height;
  244. horizontal.Size = contentSize.Width;
  245. }
  246. }
  247. /// <summary>
  248. /// Represents the top left corner coordinate that is displayed by the scrollview
  249. /// </summary>
  250. /// <value>The content offset.</value>
  251. public Point ContentOffset {
  252. get {
  253. return contentOffset;
  254. }
  255. set {
  256. contentOffset = new Point (-value.X, -value.Y);
  257. contentView.Frame = new Rect (contentOffset, contentSize);
  258. vertical.Position = Math.Max (0, -contentOffset.Y);
  259. horizontal.Position = Math.Max (0, -contentOffset.X);
  260. }
  261. }
  262. /// <summary>
  263. /// Adds the view to the scrollview.
  264. /// </summary>
  265. /// <param name="view">The view to add to the scrollview.</param>
  266. public override void Add (View view)
  267. {
  268. contentView.Add (view);
  269. }
  270. /// <summary>
  271. /// Gets or sets the visibility for the horizontal scroll indicator.
  272. /// </summary>
  273. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  274. public bool ShowHorizontalScrollIndicator {
  275. get => showHorizontalScrollIndicator;
  276. set {
  277. if (value == showHorizontalScrollIndicator)
  278. return;
  279. showHorizontalScrollIndicator = value;
  280. SetNeedsDisplay ();
  281. if (value)
  282. base.Add (horizontal);
  283. else
  284. Remove (horizontal);
  285. }
  286. }
  287. /// <summary>
  288. /// /// Gets or sets the visibility for the vertical scroll indicator.
  289. /// </summary>
  290. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  291. public bool ShowVerticalScrollIndicator {
  292. get => showVerticalScrollIndicator;
  293. set {
  294. if (value == showVerticalScrollIndicator)
  295. return;
  296. showVerticalScrollIndicator = value;
  297. SetNeedsDisplay ();
  298. if (value)
  299. base.Add (vertical);
  300. else
  301. Remove (vertical);
  302. }
  303. }
  304. /// <summary>
  305. /// This event is raised when the contents have scrolled
  306. /// </summary>
  307. public event Action<ScrollView> Scrolled;
  308. public override void Redraw(Rect region)
  309. {
  310. var oldClip = ClipToBounds ();
  311. Driver.SetAttribute (ColorScheme.Normal);
  312. Clear ();
  313. base.Redraw(region);
  314. Driver.Clip = oldClip;
  315. Driver.SetAttribute (ColorScheme.Normal);
  316. }
  317. public override void PositionCursor()
  318. {
  319. if (Subviews.Count == 0)
  320. Driver.Move (0, 0);
  321. else
  322. base.PositionCursor ();
  323. }
  324. }
  325. }