ScrollView.cs 18 KB

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