ScrollView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. Rune 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 = Driver.Stipple;
  97. else
  98. special = Driver.Diamond;
  99. Driver.AddRune(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 = Driver.Stipple;
  113. else {
  114. if (by2 - by1 == 0)
  115. special = Driver.Diamond;
  116. else {
  117. if (y == by1)
  118. special = Driver.TopTee;
  119. else if (y == by2)
  120. special = Driver.BottomTee;
  121. else
  122. special = Driver.VLine;
  123. }
  124. }
  125. Driver.AddRune (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. Rune 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 = Driver.Stipple;
  141. else
  142. special = Driver.Diamond;
  143. Driver.AddRune (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 = Driver.Stipple;
  154. } else {
  155. if (bx2 - bx1 == 0)
  156. special = Driver.Diamond;
  157. else {
  158. if (x == bx1)
  159. special = Driver.LeftTee;
  160. else if (x == bx2)
  161. special = Driver.RightTee;
  162. else
  163. special = Driver.HLine;
  164. }
  165. }
  166. Driver.AddRune (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 == barsize + 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. SetNeedsDisplay ();
  246. }
  247. }
  248. /// <summary>
  249. /// Represents the top left corner coordinate that is displayed by the scrollview
  250. /// </summary>
  251. /// <value>The content offset.</value>
  252. public Point ContentOffset {
  253. get {
  254. return contentOffset;
  255. }
  256. set {
  257. contentOffset = new Point (-Math.Abs (value.X), -Math.Abs(value.Y));
  258. contentView.Frame = new Rect (contentOffset, contentSize);
  259. vertical.Position = Math.Max (0, -contentOffset.Y);
  260. horizontal.Position = Math.Max (0, -contentOffset.X);
  261. SetNeedsDisplay ();
  262. }
  263. }
  264. /// <summary>
  265. /// Adds the view to the scrollview.
  266. /// </summary>
  267. /// <param name="view">The view to add to the scrollview.</param>
  268. public override void Add (View view)
  269. {
  270. contentView.Add (view);
  271. }
  272. /// <summary>
  273. /// Gets or sets the visibility for the horizontal scroll indicator.
  274. /// </summary>
  275. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  276. public bool ShowHorizontalScrollIndicator {
  277. get => showHorizontalScrollIndicator;
  278. set {
  279. if (value == showHorizontalScrollIndicator)
  280. return;
  281. showHorizontalScrollIndicator = value;
  282. SetNeedsDisplay ();
  283. if (value)
  284. base.Add (horizontal);
  285. else
  286. Remove (horizontal);
  287. }
  288. }
  289. /// <summary>
  290. /// Removes all widgets from this container.
  291. /// </summary>
  292. /// <remarks>
  293. /// </remarks>
  294. public override void RemoveAll()
  295. {
  296. contentView.RemoveAll();
  297. }
  298. /// <summary>
  299. /// /// Gets or sets the visibility for the vertical scroll indicator.
  300. /// </summary>
  301. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  302. public bool ShowVerticalScrollIndicator {
  303. get => showVerticalScrollIndicator;
  304. set {
  305. if (value == showVerticalScrollIndicator)
  306. return;
  307. showVerticalScrollIndicator = value;
  308. SetNeedsDisplay ();
  309. if (value)
  310. base.Add (vertical);
  311. else
  312. Remove (vertical);
  313. }
  314. }
  315. /// <summary>
  316. /// This event is raised when the contents have scrolled
  317. /// </summary>
  318. public event Action<ScrollView> Scrolled;
  319. public override void Redraw(Rect region)
  320. {
  321. var oldClip = ClipToBounds ();
  322. Driver.SetAttribute (ColorScheme.Normal);
  323. Clear ();
  324. base.Redraw(region);
  325. Driver.Clip = oldClip;
  326. Driver.SetAttribute (ColorScheme.Normal);
  327. }
  328. public override void PositionCursor()
  329. {
  330. if (InternalSubviews.Count == 0)
  331. Driver.Move (0, 0);
  332. else
  333. base.PositionCursor ();
  334. }
  335. /// <summary>
  336. /// Scrolls the view up.
  337. /// </summary>
  338. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  339. /// <param name="lines">Number of lines to scroll.</param>
  340. public bool ScrollUp (int lines)
  341. {
  342. if (contentOffset.Y < 0) {
  343. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  344. return true;
  345. }
  346. return false;
  347. }
  348. /// <summary>
  349. /// Scrolls the view to the left
  350. /// </summary>
  351. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  352. /// <param name="cols">Number of columns to scroll by.</param>
  353. public bool ScrollLeft (int cols)
  354. {
  355. if (contentOffset.X < 0) {
  356. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  357. return true;
  358. }
  359. return false;
  360. }
  361. /// <summary>
  362. /// Scrolls the view down.
  363. /// </summary>
  364. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  365. /// <param name="lines">Number of lines to scroll.</param>
  366. public bool ScrollDown (int lines)
  367. {
  368. var ny = Math.Max (-contentSize.Height, contentOffset.Y - lines);
  369. if (ny == contentOffset.Y)
  370. return false;
  371. ContentOffset = new Point (contentOffset.X, ny);
  372. return true;
  373. }
  374. /// <summary>
  375. /// Scrolls the view to the right.
  376. /// </summary>
  377. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  378. /// <param name="cols">Number of columns to scroll by.</param>
  379. public bool ScrollRight (int cols)
  380. {
  381. var nx = Math.Max (-contentSize.Width, contentOffset.X - cols);
  382. if (nx == contentOffset.X)
  383. return false;
  384. ContentOffset = new Point (nx, contentOffset.Y);
  385. return true;
  386. }
  387. public override bool ProcessKey(KeyEvent kb)
  388. {
  389. if (base.ProcessKey (kb))
  390. return true;
  391. switch (kb.Key) {
  392. case Key.CursorUp:
  393. return ScrollUp (1);
  394. case (Key) 'v' | Key.AltMask:
  395. case Key.PageUp:
  396. return ScrollUp (Bounds.Height);
  397. case Key.ControlV:
  398. case Key.PageDown:
  399. return ScrollDown (Bounds.Height);
  400. case Key.CursorDown:
  401. return ScrollDown (1);
  402. case Key.CursorLeft:
  403. return ScrollLeft (1);
  404. case Key.CursorRight:
  405. return ScrollRight (1);
  406. }
  407. return false;
  408. }
  409. }
  410. }