ScrollView.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 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. Move (col, 0);
  128. Driver.AddRune (Driver.UpArrow);
  129. Move (col, Bounds.Height - 1);
  130. Driver.AddRune (Driver.DownArrow);
  131. } else {
  132. bh -= 2;
  133. var by1 = position * bh / Size;
  134. var by2 = (position + bh) * bh / Size;
  135. Move (col, 0);
  136. Driver.AddRune (Driver.UpArrow);
  137. Move (col, Bounds.Height - 1);
  138. Driver.AddRune (Driver.DownArrow);
  139. bool hasTopTee = false;
  140. bool hasDiamond = false;
  141. bool hasBottomTee = false;
  142. for (int y = 0; y < bh; y++) {
  143. Move (col, y + 1);
  144. if ((y < by1 || y > by2) && ((position > 0 && !hasTopTee) || (hasTopTee && hasBottomTee))) {
  145. special = Driver.Stipple;
  146. } else {
  147. if (y != by2 && y > 1 && by2 - by1 == 0 && by1 < bh - 1 && hasTopTee && !hasDiamond) {
  148. hasDiamond = true;
  149. special = Driver.Diamond;
  150. } else {
  151. if (y == by1 && !hasTopTee) {
  152. hasTopTee = true;
  153. special = Driver.TopTee;
  154. } else if ((y >= by2 || by2 == 0) && !hasBottomTee) {
  155. hasBottomTee = true;
  156. special = Driver.BottomTee;
  157. } else {
  158. special = Driver.VLine;
  159. }
  160. }
  161. }
  162. Driver.AddRune (special);
  163. }
  164. if (!hasTopTee) {
  165. Move (col, Bounds.Height - 2);
  166. Driver.AddRune (Driver.TopTee);
  167. }
  168. }
  169. } else {
  170. if (region.Bottom < Bounds.Height - 1)
  171. return;
  172. var row = Bounds.Height - 1;
  173. var bw = Bounds.Width;
  174. Rune special;
  175. if (bw < 4) {
  176. var bx1 = position * bw / Size;
  177. var bx2 = (position + bw) * bw / Size;
  178. Move (0, row);
  179. Driver.AddRune (Driver.LeftArrow);
  180. Driver.AddRune (Driver.RightArrow);
  181. } else {
  182. bw -= 2;
  183. var bx1 = position * bw / Size;
  184. var bx2 = (position + bw) * bw / Size;
  185. Move (0, row);
  186. Driver.AddRune (Driver.LeftArrow);
  187. bool hasLeftTee = false;
  188. bool hasDiamond = false;
  189. bool hasRightTee = false;
  190. for (int x = 0; x < bw; x++) {
  191. if ((x < bx1 || x >= bx2 + 1) && ((position > 0 && !hasLeftTee) || (hasLeftTee && hasRightTee))) {
  192. special = Driver.Stipple;
  193. } else {
  194. if (x != bx2 && x > 1 && bx2 - bx1 == 0 && bx1 < bw - 1 && hasLeftTee && !hasDiamond) {
  195. hasDiamond = true;
  196. special = Driver.Diamond;
  197. } else {
  198. if (x == bx1 && !hasLeftTee) {
  199. hasLeftTee = true;
  200. special = Driver.LeftTee;
  201. } else if ((x >= bx2 || bx2 == 0) && !hasRightTee) {
  202. hasRightTee = true;
  203. special = Driver.RightTee;
  204. } else {
  205. special = Driver.HLine;
  206. }
  207. }
  208. }
  209. Driver.AddRune (special);
  210. }
  211. if (!hasLeftTee) {
  212. Move (Bounds.Width -2, row);
  213. Driver.AddRune (Driver.LeftTee);
  214. }
  215. Driver.AddRune (Driver.RightArrow);
  216. }
  217. }
  218. }
  219. ///<inheritdoc/>
  220. public override bool MouseEvent (MouseEvent me)
  221. {
  222. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  223. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  224. return false;
  225. int location = vertical ? me.Y : me.X;
  226. int barsize = vertical ? Bounds.Height : Bounds.Width;
  227. barsize -= 2;
  228. var pos = Position;
  229. if (location == 0) {
  230. if (pos > 0)
  231. SetPosition (pos - 1);
  232. } else if (location == barsize + 1) {
  233. if (pos + 1 < Size)
  234. SetPosition (pos + 1);
  235. } else {
  236. var b1 = pos * barsize / Size;
  237. var b2 = (pos + barsize) * barsize / Size;
  238. if (b2 == 0 && location == 1 && pos == 0 ||
  239. (b2 == barsize && location == barsize) ||
  240. (location > b1 && location < b2)) {
  241. return true;
  242. } else if (location <= barsize) {
  243. if (location > 1 && location >= b2)
  244. SetPosition (Math.Min (pos + (Size / location), Size - 1));
  245. else if (location <= b2 && pos > 0 || pos > 0)
  246. SetPosition (Math.Max (pos - (Size / barsize), 0));
  247. }
  248. }
  249. return true;
  250. }
  251. }
  252. /// <summary>
  253. /// Scrollviews are views that present a window into a virtual space where subviews are added. Similar to the iOS UIScrollView.
  254. /// </summary>
  255. /// <remarks>
  256. /// <para>
  257. /// The subviews that are added to this <see cref="Gui.ScrollView"/> are offset by the
  258. /// <see cref="ContentOffset"/> property. The view itself is a window into the
  259. /// space represented by the <see cref="ContentSize"/>.
  260. /// </para>
  261. /// <para>
  262. /// Use the
  263. /// </para>
  264. /// </remarks>
  265. public class ScrollView : View {
  266. View contentView = null;
  267. ScrollBarView vertical, horizontal;
  268. /// <summary>
  269. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Absolute"/> positioning.
  270. /// </summary>
  271. /// <param name="frame"></param>
  272. public ScrollView (Rect frame) : base (frame)
  273. {
  274. Init (frame);
  275. }
  276. /// <summary>
  277. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  278. /// </summary>
  279. public ScrollView () : base ()
  280. {
  281. Init (new Rect (0, 0, 0, 0));
  282. }
  283. void Init (Rect frame)
  284. {
  285. contentView = new View (frame);
  286. vertical = new ScrollBarView (1, 0, isVertical: true) {
  287. X = Pos.AnchorEnd (1),
  288. Y = 0,
  289. Width = 1,
  290. Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0)
  291. };
  292. vertical.ChangedPosition += delegate {
  293. ContentOffset = new Point (ContentOffset.X, vertical.Position);
  294. };
  295. horizontal = new ScrollBarView (1, 0, isVertical: false) {
  296. X = 0,
  297. Y = Pos.AnchorEnd (1),
  298. Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0),
  299. Height = 1
  300. };
  301. horizontal.ChangedPosition += delegate {
  302. ContentOffset = new Point (horizontal.Position, ContentOffset.Y);
  303. };
  304. base.Add (contentView);
  305. CanFocus = true;
  306. MouseEnter += View_MouseEnter;
  307. MouseLeave += View_MouseLeave;
  308. }
  309. Size contentSize;
  310. Point contentOffset;
  311. bool showHorizontalScrollIndicator;
  312. bool showVerticalScrollIndicator;
  313. /// <summary>
  314. /// Represents the contents of the data shown inside the scrolview
  315. /// </summary>
  316. /// <value>The size of the content.</value>
  317. public Size ContentSize {
  318. get {
  319. return contentSize;
  320. }
  321. set {
  322. contentSize = value;
  323. contentView.Frame = new Rect (contentOffset, value);
  324. vertical.Size = contentSize.Height;
  325. horizontal.Size = contentSize.Width;
  326. SetNeedsDisplay ();
  327. }
  328. }
  329. /// <summary>
  330. /// Represents the top left corner coordinate that is displayed by the scrollview
  331. /// </summary>
  332. /// <value>The content offset.</value>
  333. public Point ContentOffset {
  334. get {
  335. return contentOffset;
  336. }
  337. set {
  338. contentOffset = new Point (-Math.Abs (value.X), -Math.Abs (value.Y));
  339. contentView.Frame = new Rect (contentOffset, contentSize);
  340. vertical.Position = Math.Max (0, -contentOffset.Y);
  341. horizontal.Position = Math.Max (0, -contentOffset.X);
  342. SetNeedsDisplay ();
  343. }
  344. }
  345. /// <summary>
  346. /// Adds the view to the scrollview.
  347. /// </summary>
  348. /// <param name="view">The view to add to the scrollview.</param>
  349. public override void Add (View view)
  350. {
  351. if (!IsOverridden (view)) {
  352. view.MouseEnter += View_MouseEnter;
  353. view.MouseLeave += View_MouseLeave;
  354. }
  355. contentView.Add (view);
  356. SetNeedsLayout ();
  357. }
  358. void View_MouseLeave (MouseEventArgs e)
  359. {
  360. Application.UngrabMouse ();
  361. }
  362. void View_MouseEnter (MouseEventArgs e)
  363. {
  364. Application.GrabMouse (this);
  365. }
  366. bool IsOverridden (View view)
  367. {
  368. Type t = view.GetType ();
  369. MethodInfo m = t.GetMethod ("MouseEvent");
  370. return m.DeclaringType == t && m.GetBaseDefinition ().DeclaringType == typeof (Responder);
  371. }
  372. /// <summary>
  373. /// Gets or sets the visibility for the horizontal scroll indicator.
  374. /// </summary>
  375. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  376. public bool ShowHorizontalScrollIndicator {
  377. get => showHorizontalScrollIndicator;
  378. set {
  379. if (value == showHorizontalScrollIndicator)
  380. return;
  381. showHorizontalScrollIndicator = value;
  382. SetNeedsLayout ();
  383. if (value) {
  384. base.Add (horizontal);
  385. horizontal.MouseEnter += View_MouseEnter;
  386. horizontal.MouseLeave += View_MouseLeave;
  387. } else {
  388. Remove (horizontal);
  389. horizontal.MouseEnter -= View_MouseEnter;
  390. horizontal.MouseLeave -= View_MouseLeave;
  391. }
  392. vertical.Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0);
  393. }
  394. }
  395. /// <summary>
  396. /// Removes all widgets from this container.
  397. /// </summary>
  398. /// <remarks>
  399. /// </remarks>
  400. public override void RemoveAll ()
  401. {
  402. contentView.RemoveAll ();
  403. }
  404. /// <summary>
  405. /// /// Gets or sets the visibility for the vertical scroll indicator.
  406. /// </summary>
  407. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  408. public bool ShowVerticalScrollIndicator {
  409. get => showVerticalScrollIndicator;
  410. set {
  411. if (value == showVerticalScrollIndicator)
  412. return;
  413. showVerticalScrollIndicator = value;
  414. SetNeedsLayout ();
  415. if (value) {
  416. base.Add (vertical);
  417. vertical.MouseEnter += View_MouseEnter;
  418. vertical.MouseLeave += View_MouseLeave;
  419. } else {
  420. Remove (vertical);
  421. vertical.MouseEnter -= View_MouseEnter;
  422. vertical.MouseLeave -= View_MouseLeave;
  423. }
  424. horizontal.Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0);
  425. }
  426. }
  427. /// <inheritdoc/>
  428. public override void Redraw (Rect region)
  429. {
  430. Driver.SetAttribute (ColorScheme.Normal);
  431. SetViewsNeedsDisplay ();
  432. Clear ();
  433. var savedClip = ClipToBounds ();
  434. OnDrawContent (new Rect (ContentOffset,
  435. new Size (Bounds.Width - (ShowVerticalScrollIndicator ? 1 : 0),
  436. Bounds.Height - (ShowHorizontalScrollIndicator ? 1 : 0))));
  437. contentView.Redraw (contentView.Frame);
  438. Driver.Clip = savedClip;
  439. if (ShowVerticalScrollIndicator) {
  440. vertical.Redraw (vertical.Bounds);
  441. }
  442. if (ShowHorizontalScrollIndicator) {
  443. horizontal.Redraw (horizontal.Bounds);
  444. }
  445. // Fill in the bottom left corner
  446. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator) {
  447. AddRune (Bounds.Width - 1, Bounds.Height - 1, ' ');
  448. }
  449. Driver.SetAttribute (ColorScheme.Normal);
  450. }
  451. void SetViewsNeedsDisplay ()
  452. {
  453. foreach (View view in contentView) {
  454. view.SetNeedsDisplay ();
  455. }
  456. }
  457. ///<inheritdoc/>
  458. public override void PositionCursor ()
  459. {
  460. if (InternalSubviews.Count == 0)
  461. Move (0, 0);
  462. else
  463. base.PositionCursor ();
  464. }
  465. /// <summary>
  466. /// Scrolls the view up.
  467. /// </summary>
  468. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  469. /// <param name="lines">Number of lines to scroll.</param>
  470. public bool ScrollUp (int lines)
  471. {
  472. if (contentOffset.Y < 0) {
  473. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  474. return true;
  475. }
  476. return false;
  477. }
  478. /// <summary>
  479. /// Scrolls the view to the left
  480. /// </summary>
  481. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  482. /// <param name="cols">Number of columns to scroll by.</param>
  483. public bool ScrollLeft (int cols)
  484. {
  485. if (contentOffset.X < 0) {
  486. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  487. return true;
  488. }
  489. return false;
  490. }
  491. /// <summary>
  492. /// Scrolls the view down.
  493. /// </summary>
  494. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  495. /// <param name="lines">Number of lines to scroll.</param>
  496. public bool ScrollDown (int lines)
  497. {
  498. var ny = Math.Max (-contentSize.Height, contentOffset.Y - lines);
  499. if (Math.Abs (ny) == contentSize.Height)
  500. return false;
  501. ContentOffset = new Point (contentOffset.X, ny);
  502. return true;
  503. }
  504. /// <summary>
  505. /// Scrolls the view to the right.
  506. /// </summary>
  507. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  508. /// <param name="cols">Number of columns to scroll by.</param>
  509. public bool ScrollRight (int cols)
  510. {
  511. var nx = Math.Max (-contentSize.Width, contentOffset.X - cols);
  512. if (nx == contentOffset.X)
  513. return false;
  514. ContentOffset = new Point (nx, contentOffset.Y);
  515. return true;
  516. }
  517. ///<inheritdoc/>
  518. public override bool ProcessKey (KeyEvent kb)
  519. {
  520. if (base.ProcessKey (kb))
  521. return true;
  522. switch (kb.Key) {
  523. case Key.CursorUp:
  524. return ScrollUp (1);
  525. case (Key)'v' | Key.AltMask:
  526. case Key.PageUp:
  527. return ScrollUp (Bounds.Height);
  528. case Key.ControlV:
  529. case Key.PageDown:
  530. return ScrollDown (Bounds.Height);
  531. case Key.CursorDown:
  532. return ScrollDown (1);
  533. case Key.CursorLeft:
  534. return ScrollLeft (1);
  535. case Key.CursorRight:
  536. return ScrollRight (1);
  537. case Key.Home:
  538. return ScrollUp (contentSize.Height);
  539. case Key.End:
  540. return ScrollDown (contentSize.Height);
  541. }
  542. return false;
  543. }
  544. ///<inheritdoc/>
  545. public override bool MouseEvent (MouseEvent me)
  546. {
  547. if (me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
  548. me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  549. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  550. return false;
  551. if (me.Flags == MouseFlags.WheeledDown)
  552. ScrollDown (1);
  553. else if (me.Flags == MouseFlags.WheeledUp)
  554. ScrollUp (1);
  555. else if (me.X == vertical.Frame.X)
  556. vertical.MouseEvent (me);
  557. else if (me.Y == horizontal.Frame.Y)
  558. horizontal.MouseEvent (me);
  559. else if (IsOverridden (me.View)) {
  560. Application.UngrabMouse ();
  561. return false;
  562. }
  563. return true;
  564. }
  565. protected override void Dispose (bool disposing)
  566. {
  567. vertical?.Dispose ();
  568. horizontal?.Dispose ();
  569. base.Dispose (disposing);
  570. }
  571. }
  572. }