ScrollView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. //
  2. // ScrollView.cs: ScrollView and ScrollBarView views.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // TODO:
  9. // - focus in scrollview
  10. // - focus handling in scrollview to auto scroll to focused view
  11. // - Raise events
  12. // - Perhaps allow an option to not display the scrollbar arrow indicators?
  13. using System;
  14. using System.Reflection;
  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. /// <para>
  21. /// The scrollbar is drawn to be a representation of the Size, assuming that the
  22. /// scroll position is set at Position.
  23. /// </para>
  24. /// <para>
  25. /// If the region to display the scrollbar is larger than three characters,
  26. /// arrow indicators are drawn.
  27. /// </para>
  28. /// </remarks>
  29. public class ScrollBarView : View {
  30. bool vertical = false;
  31. int size = 0, position = 0;
  32. /// <summary>
  33. /// If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.
  34. /// </summary>
  35. public bool IsVertical {
  36. get => vertical;
  37. set {
  38. vertical = value;
  39. SetNeedsDisplay ();
  40. }
  41. }
  42. /// <summary>
  43. /// The size of content the scrollbar represents.
  44. /// </summary>
  45. /// <value>The size.</value>
  46. /// <remarks>The <see cref="Size"/> is typically the size of the virtual content. E.g. when a Scrollbar is
  47. /// part of a <see cref="ScrollView"/> the Size is set to the appropriate dimension of <see cref="ScrollView.ContentSize"/>.</remarks>
  48. public int Size {
  49. get => size;
  50. set {
  51. size = value;
  52. SetNeedsDisplay ();
  53. }
  54. }
  55. /// <summary>
  56. /// This event is raised when the position on the scrollbar has changed.
  57. /// </summary>
  58. public event Action ChangedPosition;
  59. /// <summary>
  60. /// The position, relative to <see cref="Size"/>, to set the scrollbar at.
  61. /// </summary>
  62. /// <value>The position.</value>
  63. public int Position {
  64. get => position;
  65. set {
  66. position = value;
  67. SetNeedsDisplay ();
  68. }
  69. }
  70. void SetPosition (int newPos)
  71. {
  72. Position = newPos;
  73. ChangedPosition?.Invoke ();
  74. }
  75. /// <summary>
  76. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  77. /// </summary>
  78. /// <param name="rect">Frame for the scrollbar.</param>
  79. public ScrollBarView (Rect rect) : this (rect, 0, 0, false) { }
  80. /// <summary>
  81. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  82. /// </summary>
  83. /// <param name="rect">Frame for the scrollbar.</param>
  84. /// <param name="size">The size that this scrollbar represents. Sets the <see cref="Size"/> property.</param>
  85. /// <param name="position">The position within this scrollbar. Sets the <see cref="Position"/> property.</param>
  86. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal. Sets the <see cref="IsVertical"/> property.</param>
  87. public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect)
  88. {
  89. Init (size, position, isVertical);
  90. }
  91. /// <summary>
  92. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  93. /// </summary>
  94. public ScrollBarView () : this (0, 0, false) { }
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  97. /// </summary>
  98. /// <param name="size">The size that this scrollbar represents.</param>
  99. /// <param name="position">The position within this scrollbar.</param>
  100. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  101. public ScrollBarView (int size, int position, bool isVertical) : base ()
  102. {
  103. Init (size, position, isVertical);
  104. }
  105. void Init (int size, int position, bool isVertical)
  106. {
  107. vertical = isVertical;
  108. this.position = position;
  109. this.size = size;
  110. WantContinuousButtonPressed = true;
  111. }
  112. ///<inheritdoc/>
  113. public override void Redraw (Rect region)
  114. {
  115. if (ColorScheme == null || Size == 0)
  116. return;
  117. Driver.SetAttribute (ColorScheme.Normal);
  118. if (vertical) {
  119. if (region.Right < Bounds.Width - 1)
  120. return;
  121. var col = Bounds.Width - 1;
  122. var bh = Bounds.Height;
  123. Rune special;
  124. if (bh < 4) {
  125. var by1 = position * bh / Size;
  126. var by2 = (position + bh) * bh / Size;
  127. for (int y = 0; y < bh; y++) {
  128. Move (col, y);
  129. if (y < by1 || y > by2)
  130. special = Driver.Stipple;
  131. else
  132. special = Driver.Diamond;
  133. Driver.AddRune (special);
  134. }
  135. } else {
  136. bh -= 2;
  137. var by1 = position * bh / Size;
  138. var by2 = (position + bh) * bh / Size;
  139. Move (col, 0);
  140. Driver.AddRune ('^');
  141. Move (col, Bounds.Height - 1);
  142. Driver.AddRune ('v');
  143. for (int y = 0; y < bh; y++) {
  144. Move (col, y + 1);
  145. if (y < by1 - 1 || y > by2)
  146. special = Driver.Stipple;
  147. else {
  148. if (by2 - by1 == 0 && by1 < bh - 1)
  149. special = Driver.Diamond;
  150. else {
  151. if (y == by1 - 1)
  152. special = Driver.TopTee;
  153. else if (y == by2)
  154. special = Driver.BottomTee;
  155. else
  156. special = Driver.VLine;
  157. }
  158. }
  159. Driver.AddRune (special);
  160. }
  161. }
  162. } else {
  163. if (region.Bottom < Bounds.Height - 1)
  164. return;
  165. var row = Bounds.Height - 1;
  166. var bw = Bounds.Width;
  167. Rune special;
  168. if (bw < 4) {
  169. var bx1 = position * bw / Size;
  170. var bx2 = (position + bw) * bw / Size;
  171. for (int x = 0; x < bw; x++) {
  172. Move (0, x);
  173. if (x < bx1 || x > bx2)
  174. special = Driver.Stipple;
  175. else
  176. special = Driver.Diamond;
  177. Driver.AddRune (special);
  178. }
  179. } else {
  180. bw -= 2;
  181. var bx1 = position * bw / Size;
  182. var bx2 = (position + bw) * bw / Size;
  183. Move (0, row);
  184. Driver.AddRune ('<');
  185. for (int x = 0; x < bw; x++) {
  186. if (x < bx1 || x > bx2) {
  187. special = Driver.Stipple;
  188. } else {
  189. if (bx2 - bx1 == 0)
  190. special = Driver.Diamond;
  191. else {
  192. if (x == bx1)
  193. special = Driver.LeftTee;
  194. else if (x == bx2)
  195. special = Driver.RightTee;
  196. else
  197. special = Driver.HLine;
  198. }
  199. }
  200. Driver.AddRune (special);
  201. }
  202. Driver.AddRune ('>');
  203. }
  204. }
  205. }
  206. ///<inheritdoc/>
  207. public override bool MouseEvent (MouseEvent me)
  208. {
  209. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  210. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  211. return false;
  212. int location = vertical ? me.Y : me.X;
  213. int barsize = vertical ? Bounds.Height : Bounds.Width;
  214. if (barsize < 4) {
  215. // Handle scrollbars with no buttons
  216. Console.WriteLine ("TODO at ScrollBarView2");
  217. } else {
  218. barsize -= 2;
  219. // Handle scrollbars with arrow buttons
  220. var pos = Position;
  221. if (location == 0) {
  222. if (pos > 0)
  223. SetPosition (pos - 1);
  224. } else if (location == barsize + 1) {
  225. if (pos + 1 + barsize < Size)
  226. SetPosition (pos + 1);
  227. } else {
  228. var b1 = pos * barsize / Size;
  229. var b2 = (pos + barsize) * barsize / Size;
  230. if (b2 == 0 && location == 1 && pos == 0 ||
  231. (b2 == barsize && location == barsize) ||
  232. (location > b1 && location < b2)) {
  233. return true;
  234. } else if (location <= barsize) {
  235. if (location > 1 && location >= b2)
  236. SetPosition (Math.Min (pos + barsize, Size));
  237. else if (location <= b2 && pos > 0 || pos > 0)
  238. SetPosition (Math.Max (pos - barsize, 0));
  239. }
  240. }
  241. }
  242. return true;
  243. }
  244. }
  245. /// <summary>
  246. /// Scrollviews are views that present a window into a virtual space where subviews are added. Similar to the iOS UIScrollView.
  247. /// </summary>
  248. /// <remarks>
  249. /// <para>
  250. /// The subviews that are added to this <see cref="Gui.ScrollView"/> are offset by the
  251. /// <see cref="ContentOffset"/> property. The view itself is a window into the
  252. /// space represented by the <see cref="ContentSize"/>.
  253. /// </para>
  254. /// <para>
  255. /// Use the
  256. /// </para>
  257. /// </remarks>
  258. public class ScrollView : View {
  259. View contentView = null;
  260. ScrollBarView vertical, horizontal;
  261. /// <summary>
  262. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Absolute"/> positioning.
  263. /// </summary>
  264. /// <param name="frame"></param>
  265. public ScrollView (Rect frame) : base (frame)
  266. {
  267. Init (frame);
  268. }
  269. /// <summary>
  270. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  271. /// </summary>
  272. public ScrollView () : base ()
  273. {
  274. Init (new Rect (0, 0, 0, 0));
  275. }
  276. void Init (Rect frame)
  277. {
  278. contentView = new View (frame);
  279. vertical = new ScrollBarView (1, 0, isVertical: true) {
  280. X = Pos.AnchorEnd (1),
  281. Y = 0,
  282. Width = 1,
  283. Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0)
  284. };
  285. vertical.ChangedPosition += delegate {
  286. ContentOffset = new Point (ContentOffset.X, vertical.Position);
  287. };
  288. horizontal = new ScrollBarView (1, 0, isVertical: false) {
  289. X = 0,
  290. Y = Pos.AnchorEnd (1),
  291. Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0),
  292. Height = 1
  293. };
  294. horizontal.ChangedPosition += delegate {
  295. ContentOffset = new Point (horizontal.Position, ContentOffset.Y);
  296. };
  297. base.Add (contentView);
  298. CanFocus = true;
  299. MouseEnter += View_MouseEnter;
  300. MouseLeave += View_MouseLeave;
  301. }
  302. Size contentSize;
  303. Point contentOffset;
  304. bool showHorizontalScrollIndicator;
  305. bool showVerticalScrollIndicator;
  306. /// <summary>
  307. /// Represents the contents of the data shown inside the scrolview
  308. /// </summary>
  309. /// <value>The size of the content.</value>
  310. public Size ContentSize {
  311. get {
  312. return contentSize;
  313. }
  314. set {
  315. contentSize = value;
  316. contentView.Frame = new Rect (contentOffset, value);
  317. vertical.Size = contentSize.Height;
  318. horizontal.Size = contentSize.Width;
  319. SetNeedsDisplay ();
  320. }
  321. }
  322. /// <summary>
  323. /// Represents the top left corner coordinate that is displayed by the scrollview
  324. /// </summary>
  325. /// <value>The content offset.</value>
  326. public Point ContentOffset {
  327. get {
  328. return contentOffset;
  329. }
  330. set {
  331. contentOffset = new Point (-Math.Abs (value.X), -Math.Abs (value.Y));
  332. contentView.Frame = new Rect (contentOffset, contentSize);
  333. vertical.Position = Math.Max (0, -contentOffset.Y);
  334. horizontal.Position = Math.Max (0, -contentOffset.X);
  335. SetNeedsDisplay ();
  336. }
  337. }
  338. /// <summary>
  339. /// Adds the view to the scrollview.
  340. /// </summary>
  341. /// <param name="view">The view to add to the scrollview.</param>
  342. public override void Add (View view)
  343. {
  344. if (!IsOverridden (view)) {
  345. view.MouseEnter += View_MouseEnter;
  346. view.MouseLeave += View_MouseLeave;
  347. }
  348. contentView.Add (view);
  349. SetNeedsLayout ();
  350. }
  351. void View_MouseLeave (MouseEventEventArgs e)
  352. {
  353. Application.UngrabMouse ();
  354. }
  355. void View_MouseEnter (MouseEventEventArgs e)
  356. {
  357. Application.GrabMouse (this);
  358. }
  359. bool IsOverridden (View view)
  360. {
  361. Type t = view.GetType ();
  362. MethodInfo m = t.GetMethod ("MouseEvent");
  363. return m.DeclaringType == t && m.GetBaseDefinition ().DeclaringType == typeof (Responder);
  364. }
  365. /// <summary>
  366. /// Gets or sets the visibility for the horizontal scroll indicator.
  367. /// </summary>
  368. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  369. public bool ShowHorizontalScrollIndicator {
  370. get => showHorizontalScrollIndicator;
  371. set {
  372. if (value == showHorizontalScrollIndicator)
  373. return;
  374. showHorizontalScrollIndicator = value;
  375. SetNeedsLayout ();
  376. if (value) {
  377. base.Add (horizontal);
  378. horizontal.MouseEnter += View_MouseEnter;
  379. horizontal.MouseLeave += View_MouseLeave;
  380. } else {
  381. Remove (horizontal);
  382. horizontal.MouseEnter -= View_MouseEnter;
  383. horizontal.MouseLeave -= View_MouseLeave;
  384. }
  385. vertical.Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0);
  386. }
  387. }
  388. /// <summary>
  389. /// Removes all widgets from this container.
  390. /// </summary>
  391. /// <remarks>
  392. /// </remarks>
  393. public override void RemoveAll ()
  394. {
  395. contentView.RemoveAll ();
  396. }
  397. /// <summary>
  398. /// /// Gets or sets the visibility for the vertical scroll indicator.
  399. /// </summary>
  400. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  401. public bool ShowVerticalScrollIndicator {
  402. get => showVerticalScrollIndicator;
  403. set {
  404. if (value == showVerticalScrollIndicator)
  405. return;
  406. showVerticalScrollIndicator = value;
  407. SetNeedsLayout ();
  408. if (value) {
  409. base.Add (vertical);
  410. vertical.MouseEnter += View_MouseEnter;
  411. vertical.MouseLeave += View_MouseLeave;
  412. } else {
  413. Remove (vertical);
  414. vertical.MouseEnter -= View_MouseEnter;
  415. vertical.MouseLeave -= View_MouseLeave;
  416. }
  417. horizontal.Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0);
  418. }
  419. }
  420. /// <inheritdoc/>
  421. public override void Redraw (Rect region)
  422. {
  423. Driver.SetAttribute (ColorScheme.Normal);
  424. SetViewsNeedsDisplay ();
  425. Clear ();
  426. var savedClip = ClipToBounds ();
  427. OnDrawContent (new Rect (ContentOffset,
  428. new Size (Bounds.Width - (ShowVerticalScrollIndicator ? 1 : 0),
  429. Bounds.Height - (ShowHorizontalScrollIndicator ? 1 : 0))));
  430. contentView.Redraw (contentView.Bounds);
  431. Driver.Clip = savedClip;
  432. if (ShowVerticalScrollIndicator) {
  433. vertical.Redraw (vertical.Bounds);
  434. }
  435. if (ShowHorizontalScrollIndicator) {
  436. horizontal.Redraw (horizontal.Bounds);
  437. }
  438. // Fill in the bottom left corner
  439. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator) {
  440. AddRune (Bounds.Width - 1, Bounds.Height - 1, ' ');
  441. }
  442. Driver.SetAttribute (ColorScheme.Normal);
  443. }
  444. void SetViewsNeedsDisplay ()
  445. {
  446. foreach (View view in contentView) {
  447. view.SetNeedsDisplay ();
  448. }
  449. }
  450. ///<inheritdoc/>
  451. public override void PositionCursor ()
  452. {
  453. if (InternalSubviews.Count == 0)
  454. Driver.Move (0, 0);
  455. else
  456. base.PositionCursor ();
  457. }
  458. /// <summary>
  459. /// Scrolls the view up.
  460. /// </summary>
  461. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  462. /// <param name="lines">Number of lines to scroll.</param>
  463. public bool ScrollUp (int lines)
  464. {
  465. if (contentOffset.Y < 0) {
  466. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  467. return true;
  468. }
  469. return false;
  470. }
  471. /// <summary>
  472. /// Scrolls the view to the left
  473. /// </summary>
  474. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  475. /// <param name="cols">Number of columns to scroll by.</param>
  476. public bool ScrollLeft (int cols)
  477. {
  478. if (contentOffset.X < 0) {
  479. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  480. return true;
  481. }
  482. return false;
  483. }
  484. /// <summary>
  485. /// Scrolls the view down.
  486. /// </summary>
  487. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  488. /// <param name="lines">Number of lines to scroll.</param>
  489. public bool ScrollDown (int lines)
  490. {
  491. var ny = Math.Max (-contentSize.Height, contentOffset.Y - lines);
  492. if (ny == contentOffset.Y)
  493. return false;
  494. ContentOffset = new Point (contentOffset.X, ny);
  495. return true;
  496. }
  497. /// <summary>
  498. /// Scrolls the view to the right.
  499. /// </summary>
  500. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  501. /// <param name="cols">Number of columns to scroll by.</param>
  502. public bool ScrollRight (int cols)
  503. {
  504. var nx = Math.Max (-contentSize.Width, contentOffset.X - cols);
  505. if (nx == contentOffset.X)
  506. return false;
  507. ContentOffset = new Point (nx, contentOffset.Y);
  508. return true;
  509. }
  510. ///<inheritdoc/>
  511. public override bool ProcessKey (KeyEvent kb)
  512. {
  513. if (base.ProcessKey (kb))
  514. return true;
  515. switch (kb.Key) {
  516. case Key.CursorUp:
  517. return ScrollUp (1);
  518. case (Key)'v' | Key.AltMask:
  519. case Key.PageUp:
  520. return ScrollUp (Bounds.Height);
  521. case Key.ControlV:
  522. case Key.PageDown:
  523. return ScrollDown (Bounds.Height);
  524. case Key.CursorDown:
  525. return ScrollDown (1);
  526. case Key.CursorLeft:
  527. return ScrollLeft (1);
  528. case Key.CursorRight:
  529. return ScrollRight (1);
  530. case Key.Home:
  531. return ScrollUp (contentSize.Height);
  532. case Key.End:
  533. return ScrollDown (contentSize.Height);
  534. }
  535. return false;
  536. }
  537. ///<inheritdoc/>
  538. public override bool MouseEvent (MouseEvent me)
  539. {
  540. if (me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
  541. me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  542. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  543. return false;
  544. if (me.Flags == MouseFlags.WheeledDown)
  545. ScrollDown (1);
  546. else if (me.Flags == MouseFlags.WheeledUp)
  547. ScrollUp (1);
  548. else if (me.X == vertical.Frame.X)
  549. vertical.MouseEvent (me);
  550. else if (me.Y == horizontal.Frame.Y)
  551. horizontal.MouseEvent (me);
  552. else if (IsOverridden (me.View)) {
  553. Application.UngrabMouse ();
  554. return false;
  555. }
  556. return true;
  557. }
  558. }
  559. }