ScrollView.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. /// <summary>
  71. /// Get or sets the view that host this <see cref="ScrollView"/>
  72. /// </summary>
  73. public ScrollView Host { get; internal set; }
  74. void SetPosition (int newPos)
  75. {
  76. Position = newPos;
  77. ChangedPosition?.Invoke ();
  78. }
  79. /// <summary>
  80. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  81. /// </summary>
  82. /// <param name="rect">Frame for the scrollbar.</param>
  83. public ScrollBarView (Rect rect) : this (rect, 0, 0, false) { }
  84. /// <summary>
  85. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  86. /// </summary>
  87. /// <param name="rect">Frame for the scrollbar.</param>
  88. /// <param name="size">The size that this scrollbar represents. Sets the <see cref="Size"/> property.</param>
  89. /// <param name="position">The position within this scrollbar. Sets the <see cref="Position"/> property.</param>
  90. /// <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>
  91. public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect)
  92. {
  93. Init (size, position, isVertical);
  94. }
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  97. /// </summary>
  98. public ScrollBarView () : this (0, 0, false) { }
  99. /// <summary>
  100. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  101. /// </summary>
  102. /// <param name="size">The size that this scrollbar represents.</param>
  103. /// <param name="position">The position within this scrollbar.</param>
  104. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  105. public ScrollBarView (int size, int position, bool isVertical) : base ()
  106. {
  107. Init (size, position, isVertical);
  108. }
  109. void Init (int size, int position, bool isVertical)
  110. {
  111. vertical = isVertical;
  112. this.position = position;
  113. this.size = size;
  114. WantContinuousButtonPressed = true;
  115. }
  116. int posTopTee;
  117. int posLeftTee;
  118. int posBottomTee;
  119. int posRightTee;
  120. ///<inheritdoc/>
  121. public override void Redraw (Rect region)
  122. {
  123. if (ColorScheme == null || Size == 0)
  124. return;
  125. Driver.SetAttribute (ColorScheme.Normal);
  126. if (Bounds.Height == 0) {
  127. return;
  128. }
  129. if (vertical) {
  130. if (region.Right < Bounds.Width - 1)
  131. return;
  132. var col = Bounds.Width - 1;
  133. var bh = Bounds.Height;
  134. Rune special;
  135. if (bh < 4) {
  136. var by1 = position * bh / Size;
  137. var by2 = (position + bh) * bh / Size;
  138. Move (col, 0);
  139. if (Bounds.Height == 1) {
  140. Driver.AddRune (Driver.Diamond);
  141. } else {
  142. Driver.AddRune (Driver.UpArrow);
  143. }
  144. if (Bounds.Height == 3) {
  145. Move (col, 1);
  146. Driver.AddRune (Driver.Diamond);
  147. }
  148. if (Bounds.Height > 1) {
  149. Move (col, Bounds.Height - 1);
  150. Driver.AddRune (Driver.DownArrow);
  151. }
  152. } else {
  153. bh -= 2;
  154. var by1 = position * bh / Size;
  155. var by2 = Host.KeepContentAlwaysInViewport ? Math.Min (((position + bh) * bh / Size) + 1, bh - 1) : (position + bh) * bh / Size;
  156. if (Host.KeepContentAlwaysInViewport && by1 == by2) {
  157. by1 = Math.Max (by1 - 1, 0);
  158. }
  159. Move (col, 0);
  160. Driver.AddRune (Driver.UpArrow);
  161. Move (col, Bounds.Height - 1);
  162. Driver.AddRune (Driver.DownArrow);
  163. bool hasTopTee = false;
  164. bool hasDiamond = false;
  165. bool hasBottomTee = false;
  166. for (int y = 0; y < bh; y++) {
  167. Move (col, y + 1);
  168. if ((y < by1 || y > by2) && ((position > 0 && !hasTopTee) || (hasTopTee && hasBottomTee))) {
  169. special = Driver.Stipple;
  170. } else {
  171. if (y != by2 && y > 1 && by2 - by1 == 0 && by1 < bh - 1 && hasTopTee && !hasDiamond) {
  172. hasDiamond = true;
  173. special = Driver.Diamond;
  174. } else {
  175. if (y == by1 && !hasTopTee) {
  176. hasTopTee = true;
  177. posTopTee = y;
  178. special = Driver.TopTee;
  179. } else if ((position == 0 && y == bh - 1 || y >= by2 || by2 == 0) && !hasBottomTee) {
  180. hasBottomTee = true;
  181. posBottomTee = y;
  182. special = Driver.BottomTee;
  183. } else {
  184. special = Driver.VLine;
  185. }
  186. }
  187. }
  188. Driver.AddRune (special);
  189. }
  190. if (!hasTopTee) {
  191. Move (col, Bounds.Height - 2);
  192. Driver.AddRune (Driver.TopTee);
  193. }
  194. }
  195. } else {
  196. if (region.Bottom < Bounds.Height - 1)
  197. return;
  198. var row = Bounds.Height - 1;
  199. var bw = Bounds.Width;
  200. Rune special;
  201. if (bw < 4) {
  202. var bx1 = position * bw / Size;
  203. var bx2 = (position + bw) * bw / Size;
  204. Move (0, row);
  205. Driver.AddRune (Driver.LeftArrow);
  206. Driver.AddRune (Driver.RightArrow);
  207. } else {
  208. bw -= 2;
  209. var bx1 = position * bw / Size;
  210. var bx2 = Host.KeepContentAlwaysInViewport ? Math.Min (((position + bw) * bw / Size) + 1, bw - 1) : (position + bw) * bw / Size;
  211. if (Host.KeepContentAlwaysInViewport && bx1 == bx2) {
  212. bx1 = Math.Max (bx1 - 1, 0);
  213. }
  214. Move (0, row);
  215. Driver.AddRune (Driver.LeftArrow);
  216. bool hasLeftTee = false;
  217. bool hasDiamond = false;
  218. bool hasRightTee = false;
  219. for (int x = 0; x < bw; x++) {
  220. if ((x < bx1 || x >= bx2 + 1) && ((position > 0 && !hasLeftTee) || (hasLeftTee && hasRightTee))) {
  221. special = Driver.Stipple;
  222. } else {
  223. if (x != bx2 && x > 1 && bx2 - bx1 == 0 && bx1 < bw - 1 && hasLeftTee && !hasDiamond) {
  224. hasDiamond = true;
  225. special = Driver.Diamond;
  226. } else {
  227. if (x == bx1 && !hasLeftTee) {
  228. hasLeftTee = true;
  229. posLeftTee = x;
  230. special = Driver.LeftTee;
  231. } else if ((position == 0 && x == bw - 1 || x >= bx2 || bx2 == 0) && !hasRightTee) {
  232. hasRightTee = true;
  233. posRightTee = x;
  234. special = Driver.RightTee;
  235. } else {
  236. special = Driver.HLine;
  237. }
  238. }
  239. }
  240. Driver.AddRune (special);
  241. }
  242. if (!hasLeftTee) {
  243. Move (Bounds.Width -2, row);
  244. Driver.AddRune (Driver.LeftTee);
  245. }
  246. Driver.AddRune (Driver.RightArrow);
  247. }
  248. }
  249. }
  250. int lastLocation = -1;
  251. ///<inheritdoc/>
  252. public override bool MouseEvent (MouseEvent me)
  253. {
  254. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  255. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  256. return false;
  257. }
  258. if (!me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  259. lastLocation = -1;
  260. }
  261. int location = vertical ? me.Y : me.X;
  262. int barsize = vertical ? Bounds.Height : Bounds.Width;
  263. int posTopLeftTee = vertical ? posTopTee : posLeftTee;
  264. int posBottomRightTee = vertical ? posBottomTee : posRightTee;
  265. barsize -= 2;
  266. var pos = Position;
  267. if (location == 0) {
  268. if (pos > 0) {
  269. SetPosition (pos - 1);
  270. }
  271. } else if (location == barsize + 1) {
  272. if (Host.CanScroll (1, out _, vertical)) {
  273. SetPosition (pos + 1);
  274. }
  275. } else if (location > 0 && location < barsize + 1) {
  276. var b1 = pos * barsize / Size;
  277. var b2 = Host.KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size;
  278. if (Host.KeepContentAlwaysInViewport && b1 == b2) {
  279. b1 = Math.Max (b1 - 1, 0);
  280. }
  281. if (location > b1 && location <= b2 + 1) {
  282. if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1Clicked) {
  283. if (location == 1) {
  284. SetPosition (0);
  285. } else if (location == barsize) {
  286. Host.CanScroll (Size - pos, out int nv, vertical);
  287. if (nv > 0) {
  288. SetPosition (Math.Min (pos + nv, Size));
  289. }
  290. }
  291. } else if (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  292. var mb = (b2 - b1) / 2;
  293. var ml = mb + b1 + (mb == 0 ? 1 : 0);
  294. if ((location >= b1 && location <= ml) || (location < lastLocation && lastLocation > -1)) {
  295. lastLocation = location;
  296. var np = b1 * Size / barsize;
  297. SetPosition (np);
  298. } else if (location > lastLocation) {
  299. var np = location * Size / barsize;
  300. Host.CanScroll (np - pos, out int nv, vertical);
  301. if (nv > 0) {
  302. SetPosition (pos + nv);
  303. }
  304. }
  305. }
  306. } else {
  307. if (location >= b2 + 1 && location > posTopLeftTee && location > b1 && location > posBottomRightTee && posBottomRightTee > 0) {
  308. Host.CanScroll (location, out int nv, vertical);
  309. if (nv > 0) {
  310. SetPosition (Math.Min (pos + nv, Size));
  311. }
  312. } else if (location <= b1) {
  313. SetPosition (Math.Max (pos - barsize - location, 0));
  314. }
  315. }
  316. }
  317. return true;
  318. }
  319. }
  320. /// <summary>
  321. /// Scrollviews are views that present a window into a virtual space where subviews are added. Similar to the iOS UIScrollView.
  322. /// </summary>
  323. /// <remarks>
  324. /// <para>
  325. /// The subviews that are added to this <see cref="Gui.ScrollView"/> are offset by the
  326. /// <see cref="ContentOffset"/> property. The view itself is a window into the
  327. /// space represented by the <see cref="ContentSize"/>.
  328. /// </para>
  329. /// <para>
  330. /// Use the
  331. /// </para>
  332. /// </remarks>
  333. public class ScrollView : View {
  334. View contentView = null;
  335. ScrollBarView vertical, horizontal;
  336. /// <summary>
  337. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Absolute"/> positioning.
  338. /// </summary>
  339. /// <param name="frame"></param>
  340. public ScrollView (Rect frame) : base (frame)
  341. {
  342. Init (frame);
  343. }
  344. /// <summary>
  345. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  346. /// </summary>
  347. public ScrollView () : base ()
  348. {
  349. Init (new Rect (0, 0, 0, 0));
  350. }
  351. void Init (Rect frame)
  352. {
  353. contentView = new View (frame);
  354. vertical = new ScrollBarView (1, 0, isVertical: true) {
  355. X = Pos.AnchorEnd (1),
  356. Y = 0,
  357. Width = 1,
  358. Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0)
  359. };
  360. vertical.ChangedPosition += delegate {
  361. ContentOffset = new Point (ContentOffset.X, vertical.Position);
  362. };
  363. vertical.Host = this;
  364. horizontal = new ScrollBarView (1, 0, isVertical: false) {
  365. X = 0,
  366. Y = Pos.AnchorEnd (1),
  367. Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0),
  368. Height = 1
  369. };
  370. horizontal.ChangedPosition += delegate {
  371. ContentOffset = new Point (horizontal.Position, ContentOffset.Y);
  372. };
  373. horizontal.Host = this;
  374. base.Add (contentView);
  375. CanFocus = true;
  376. MouseEnter += View_MouseEnter;
  377. MouseLeave += View_MouseLeave;
  378. }
  379. Size contentSize;
  380. Point contentOffset;
  381. bool showHorizontalScrollIndicator;
  382. bool showVerticalScrollIndicator;
  383. bool keepContentAlwaysInViewport = true;
  384. bool autoHideScrollBars = true;
  385. /// <summary>
  386. /// Represents the contents of the data shown inside the scrollview
  387. /// </summary>
  388. /// <value>The size of the content.</value>
  389. public Size ContentSize {
  390. get {
  391. return contentSize;
  392. }
  393. set {
  394. if (contentSize != value) {
  395. contentSize = value;
  396. contentView.Frame = new Rect (contentOffset, value);
  397. vertical.Size = contentSize.Height;
  398. horizontal.Size = contentSize.Width;
  399. SetNeedsDisplay ();
  400. }
  401. }
  402. }
  403. /// <summary>
  404. /// Represents the top left corner coordinate that is displayed by the scrollview
  405. /// </summary>
  406. /// <value>The content offset.</value>
  407. public Point ContentOffset {
  408. get {
  409. return contentOffset;
  410. }
  411. set {
  412. contentOffset = new Point (-Math.Abs (value.X), -Math.Abs (value.Y));
  413. contentView.Frame = new Rect (contentOffset, contentSize);
  414. vertical.Position = Math.Max (0, -contentOffset.Y);
  415. horizontal.Position = Math.Max (0, -contentOffset.X);
  416. SetNeedsDisplay ();
  417. }
  418. }
  419. /// <summary>
  420. /// If true the vertical/horizontal scroll bars won't be showed if it's not needed.
  421. /// </summary>
  422. public bool AutoHideScrollBars {
  423. get => autoHideScrollBars;
  424. set {
  425. if (autoHideScrollBars != value) {
  426. autoHideScrollBars = value;
  427. SetNeedsDisplay ();
  428. }
  429. }
  430. }
  431. /// <summary>
  432. /// Get or sets if the view-port is kept always visible in the area of this <see cref="ScrollView"/>
  433. /// </summary>
  434. public bool KeepContentAlwaysInViewport {
  435. get { return keepContentAlwaysInViewport; }
  436. set {
  437. if (keepContentAlwaysInViewport != value) {
  438. keepContentAlwaysInViewport = value;
  439. Point p = default;
  440. if (value && -contentOffset.X + Bounds.Width > contentSize.Width) {
  441. p = new Point (contentSize.Width - Bounds.Width + (showVerticalScrollIndicator ? 1 : 0), -contentOffset.Y);
  442. }
  443. if (value && -contentOffset.Y + Bounds.Height > contentSize.Height) {
  444. if (p == default) {
  445. p = new Point (-contentOffset.X, contentSize.Height - Bounds.Height + (showHorizontalScrollIndicator ? 1 : 0));
  446. } else {
  447. p.Y = contentSize.Height - Bounds.Height + (showHorizontalScrollIndicator ? 1 : 0);
  448. }
  449. }
  450. if (p != default) {
  451. ContentOffset = p;
  452. }
  453. }
  454. }
  455. }
  456. /// <summary>
  457. /// Adds the view to the scrollview.
  458. /// </summary>
  459. /// <param name="view">The view to add to the scrollview.</param>
  460. public override void Add (View view)
  461. {
  462. if (!IsOverridden (view)) {
  463. view.MouseEnter += View_MouseEnter;
  464. view.MouseLeave += View_MouseLeave;
  465. }
  466. contentView.Add (view);
  467. SetNeedsLayout ();
  468. }
  469. void View_MouseLeave (MouseEventArgs e)
  470. {
  471. Application.UngrabMouse ();
  472. }
  473. void View_MouseEnter (MouseEventArgs e)
  474. {
  475. Application.GrabMouse (this);
  476. }
  477. bool IsOverridden (View view)
  478. {
  479. Type t = view.GetType ();
  480. MethodInfo m = t.GetMethod ("MouseEvent");
  481. return m.DeclaringType == t && m.GetBaseDefinition ().DeclaringType == typeof (Responder);
  482. }
  483. /// <summary>
  484. /// Gets or sets the visibility for the horizontal scroll indicator.
  485. /// </summary>
  486. /// <value><c>true</c> if show horizontal scroll indicator; otherwise, <c>false</c>.</value>
  487. public bool ShowHorizontalScrollIndicator {
  488. get => showHorizontalScrollIndicator;
  489. set {
  490. if (value == showHorizontalScrollIndicator)
  491. return;
  492. showHorizontalScrollIndicator = value;
  493. SetNeedsLayout ();
  494. if (value) {
  495. base.Add (horizontal);
  496. horizontal.MouseEnter += View_MouseEnter;
  497. horizontal.MouseLeave += View_MouseLeave;
  498. } else {
  499. Remove (horizontal);
  500. horizontal.MouseEnter -= View_MouseEnter;
  501. horizontal.MouseLeave -= View_MouseLeave;
  502. }
  503. vertical.Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0);
  504. }
  505. }
  506. /// <summary>
  507. /// Removes all widgets from this container.
  508. /// </summary>
  509. /// <remarks>
  510. /// </remarks>
  511. public override void RemoveAll ()
  512. {
  513. contentView.RemoveAll ();
  514. }
  515. /// <summary>
  516. /// /// Gets or sets the visibility for the vertical scroll indicator.
  517. /// </summary>
  518. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  519. public bool ShowVerticalScrollIndicator {
  520. get => showVerticalScrollIndicator;
  521. set {
  522. if (value == showVerticalScrollIndicator)
  523. return;
  524. showVerticalScrollIndicator = value;
  525. SetNeedsLayout ();
  526. if (value) {
  527. base.Add (vertical);
  528. vertical.MouseEnter += View_MouseEnter;
  529. vertical.MouseLeave += View_MouseLeave;
  530. } else {
  531. Remove (vertical);
  532. vertical.MouseEnter -= View_MouseEnter;
  533. vertical.MouseLeave -= View_MouseLeave;
  534. }
  535. horizontal.Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0);
  536. }
  537. }
  538. /// <inheritdoc/>
  539. public override void Redraw (Rect region)
  540. {
  541. Driver.SetAttribute (ColorScheme.Normal);
  542. SetViewsNeedsDisplay ();
  543. Clear ();
  544. var savedClip = ClipToBounds ();
  545. OnDrawContent (new Rect (ContentOffset,
  546. new Size (Math.Max (Bounds.Width - (ShowVerticalScrollIndicator ? 1 : 0), 0),
  547. Math.Max (Bounds.Height - (ShowHorizontalScrollIndicator ? 1 : 0), 0))));
  548. contentView.Redraw (contentView.Frame);
  549. Driver.Clip = savedClip;
  550. if (autoHideScrollBars) {
  551. ShowHideScrollBars ();
  552. } else {
  553. if (ShowVerticalScrollIndicator) {
  554. vertical.Redraw (vertical.Bounds);
  555. }
  556. if (ShowHorizontalScrollIndicator) {
  557. horizontal.Redraw (horizontal.Bounds);
  558. }
  559. }
  560. // Fill in the bottom left corner
  561. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator) {
  562. AddRune (Bounds.Width - 1, Bounds.Height - 1, ' ');
  563. }
  564. Driver.SetAttribute (ColorScheme.Normal);
  565. }
  566. void ShowHideScrollBars ()
  567. {
  568. bool v = false, h = false; bool p = false;
  569. if (Bounds.Height == 0 || Bounds.Height > contentSize.Height) {
  570. if (ShowVerticalScrollIndicator) {
  571. ShowVerticalScrollIndicator = false;
  572. }
  573. v = false;
  574. } else if (Bounds.Height > 0 && Bounds.Height == contentSize.Height) {
  575. p = true;
  576. } else {
  577. if (!ShowVerticalScrollIndicator) {
  578. ShowVerticalScrollIndicator = true;
  579. }
  580. v = true;
  581. }
  582. if (Bounds.Width == 0 || Bounds.Width > contentSize.Width) {
  583. if (ShowHorizontalScrollIndicator) {
  584. ShowHorizontalScrollIndicator = false;
  585. }
  586. h = false;
  587. } else if (Bounds.Width > 0 && Bounds.Width == contentSize.Width && p) {
  588. if (ShowHorizontalScrollIndicator) {
  589. ShowHorizontalScrollIndicator = false;
  590. }
  591. h = false;
  592. if (ShowVerticalScrollIndicator) {
  593. ShowVerticalScrollIndicator = false;
  594. }
  595. v = false;
  596. } else {
  597. if (p) {
  598. if (!ShowVerticalScrollIndicator) {
  599. ShowVerticalScrollIndicator = true;
  600. }
  601. v = true;
  602. }
  603. if (!ShowHorizontalScrollIndicator) {
  604. ShowHorizontalScrollIndicator = true;
  605. }
  606. h = true;
  607. }
  608. var dim = Dim.Fill (h ? 1 : 0);
  609. if (!vertical.Height.Equals (dim)) {
  610. vertical.Height = dim;
  611. }
  612. dim = Dim.Fill (v ? 1 : 0);
  613. if (!horizontal.Width.Equals (dim)) {
  614. horizontal.Width = dim;
  615. }
  616. if (v) {
  617. vertical.SetRelativeLayout (Bounds);
  618. vertical.Redraw (vertical.Bounds);
  619. }
  620. if (h) {
  621. horizontal.SetRelativeLayout (Bounds);
  622. horizontal.Redraw (horizontal.Bounds);
  623. }
  624. }
  625. void SetViewsNeedsDisplay ()
  626. {
  627. foreach (View view in contentView.Subviews) {
  628. view.SetNeedsDisplay ();
  629. }
  630. }
  631. ///<inheritdoc/>
  632. public override void PositionCursor ()
  633. {
  634. if (InternalSubviews.Count == 0)
  635. Move (0, 0);
  636. else
  637. base.PositionCursor ();
  638. }
  639. /// <summary>
  640. /// Scrolls the view up.
  641. /// </summary>
  642. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  643. /// <param name="lines">Number of lines to scroll.</param>
  644. public bool ScrollUp (int lines)
  645. {
  646. if (contentOffset.Y < 0) {
  647. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  648. return true;
  649. }
  650. return false;
  651. }
  652. /// <summary>
  653. /// Scrolls the view to the left
  654. /// </summary>
  655. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  656. /// <param name="cols">Number of columns to scroll by.</param>
  657. public bool ScrollLeft (int cols)
  658. {
  659. if (contentOffset.X < 0) {
  660. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  661. return true;
  662. }
  663. return false;
  664. }
  665. /// <summary>
  666. /// Scrolls the view down.
  667. /// </summary>
  668. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  669. /// <param name="lines">Number of lines to scroll.</param>
  670. public bool ScrollDown (int lines)
  671. {
  672. if (CanScroll (lines, out _, true)) {
  673. ContentOffset = new Point (contentOffset.X, contentOffset.Y - lines);
  674. return true;
  675. }
  676. return false;
  677. }
  678. /// <summary>
  679. /// Scrolls the view to the right.
  680. /// </summary>
  681. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  682. /// <param name="cols">Number of columns to scroll by.</param>
  683. public bool ScrollRight (int cols)
  684. {
  685. if (CanScroll (cols, out _)) {
  686. ContentOffset = new Point (contentOffset.X - cols, contentOffset.Y);
  687. return true;
  688. }
  689. return false;
  690. }
  691. internal bool CanScroll (int n, out int max, bool isVertical = false)
  692. {
  693. var size = isVertical ?
  694. (KeepContentAlwaysInViewport ? Bounds.Height + (showHorizontalScrollIndicator ? -2 : -1) : 0) :
  695. (KeepContentAlwaysInViewport ? Bounds.Width + (showVerticalScrollIndicator ? -2 : -1) : 0);
  696. var cSize = isVertical ? -contentSize.Height : -contentSize.Width;
  697. var cOffSet = isVertical ? contentOffset.Y : contentOffset.X;
  698. var newSize = Math.Max (cSize, cOffSet - n);
  699. max = cSize < newSize - size ? n : -cSize + (cOffSet - size) - 1;
  700. if (cSize < newSize - size) {
  701. return true;
  702. }
  703. return false;
  704. }
  705. ///<inheritdoc/>
  706. public override bool ProcessKey (KeyEvent kb)
  707. {
  708. if (base.ProcessKey (kb))
  709. return true;
  710. switch (kb.Key) {
  711. case Key.CursorUp:
  712. return ScrollUp (1);
  713. case (Key)'v' | Key.AltMask:
  714. case Key.PageUp:
  715. return ScrollUp (Bounds.Height);
  716. case Key.V | Key.CtrlMask:
  717. case Key.PageDown:
  718. return ScrollDown (Bounds.Height);
  719. case Key.CursorDown:
  720. return ScrollDown (1);
  721. case Key.CursorLeft:
  722. return ScrollLeft (1);
  723. case Key.CursorRight:
  724. return ScrollRight (1);
  725. case Key.Home:
  726. return ScrollUp (contentSize.Height);
  727. case Key.End:
  728. return ScrollDown (contentSize.Height);
  729. }
  730. return false;
  731. }
  732. ///<inheritdoc/>
  733. public override bool MouseEvent (MouseEvent me)
  734. {
  735. if (me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
  736. me.Flags != MouseFlags.WheeledRight && me.Flags != MouseFlags.WheeledLeft &&
  737. me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  738. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  739. return false;
  740. }
  741. if (me.Flags == MouseFlags.WheeledDown && ShowVerticalScrollIndicator) {
  742. ScrollDown (1);
  743. } else if (me.Flags == MouseFlags.WheeledUp && ShowVerticalScrollIndicator) {
  744. ScrollUp (1);
  745. } else if (me.Flags == MouseFlags.WheeledRight && showHorizontalScrollIndicator) {
  746. ScrollRight (1);
  747. } else if (me.Flags == MouseFlags.WheeledLeft && ShowVerticalScrollIndicator) {
  748. ScrollLeft (1);
  749. } else if (me.X == vertical.Frame.X && ShowVerticalScrollIndicator) {
  750. vertical.MouseEvent (me);
  751. } else if (me.Y == horizontal.Frame.Y && ShowHorizontalScrollIndicator) {
  752. horizontal.MouseEvent (me);
  753. } else if (IsOverridden (me.View)) {
  754. Application.UngrabMouse ();
  755. return false;
  756. }
  757. return true;
  758. }
  759. ///<inheritdoc/>
  760. protected override void Dispose (bool disposing)
  761. {
  762. if (!showVerticalScrollIndicator) {
  763. // It was not added to SuperView, so it won't get disposed automatically
  764. vertical?.Dispose ();
  765. }
  766. if (!showHorizontalScrollIndicator) {
  767. // It was not added to SuperView, so it won't get disposed automatically
  768. horizontal?.Dispose ();
  769. }
  770. base.Dispose (disposing);
  771. }
  772. }
  773. }