ScrollView.cs 18 KB

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