ScrollView.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. ///<inheritdoc/>
  251. public override bool MouseEvent (MouseEvent me)
  252. {
  253. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  254. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  255. return false;
  256. }
  257. int location = vertical ? me.Y : me.X;
  258. int barsize = vertical ? Bounds.Height : Bounds.Width;
  259. int posTopLeftTee = vertical ? posTopTee : posLeftTee;
  260. int posBottomRightTee = vertical ? posBottomTee : posRightTee;
  261. barsize -= 2;
  262. var pos = Position;
  263. if (location == 0) {
  264. if (pos > 0) {
  265. SetPosition (pos - 1);
  266. }
  267. } else if (location == barsize + 1) {
  268. if (Host.CanScroll (1, out _, vertical)) {
  269. SetPosition (pos + 1);
  270. }
  271. } else if (location > 0 && location < barsize + 1) {
  272. var b1 = pos * barsize / Size;
  273. var b2 = Host.KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size;
  274. if (Host.KeepContentAlwaysInViewport && b1 == b2) {
  275. b1 = Math.Max (b1 - 1, 0);
  276. }
  277. if (location > b2 + 1 && location > posTopLeftTee && location > b1 && location > posBottomRightTee && posBottomRightTee > 0) {
  278. Host.CanScroll (location, out int nv, vertical);
  279. if (nv > 0) {
  280. SetPosition (Math.Min (pos + nv, Size));
  281. }
  282. } else if (location <= b1) {
  283. SetPosition (Math.Max (pos - barsize - location, 0));
  284. }
  285. }
  286. return true;
  287. }
  288. }
  289. /// <summary>
  290. /// Scrollviews are views that present a window into a virtual space where subviews are added. Similar to the iOS UIScrollView.
  291. /// </summary>
  292. /// <remarks>
  293. /// <para>
  294. /// The subviews that are added to this <see cref="Gui.ScrollView"/> are offset by the
  295. /// <see cref="ContentOffset"/> property. The view itself is a window into the
  296. /// space represented by the <see cref="ContentSize"/>.
  297. /// </para>
  298. /// <para>
  299. /// Use the
  300. /// </para>
  301. /// </remarks>
  302. public class ScrollView : View {
  303. View contentView = null;
  304. ScrollBarView vertical, horizontal;
  305. /// <summary>
  306. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Absolute"/> positioning.
  307. /// </summary>
  308. /// <param name="frame"></param>
  309. public ScrollView (Rect frame) : base (frame)
  310. {
  311. Init (frame);
  312. }
  313. /// <summary>
  314. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  315. /// </summary>
  316. public ScrollView () : base ()
  317. {
  318. Init (new Rect (0, 0, 0, 0));
  319. }
  320. void Init (Rect frame)
  321. {
  322. contentView = new View (frame);
  323. vertical = new ScrollBarView (1, 0, isVertical: true) {
  324. X = Pos.AnchorEnd (1),
  325. Y = 0,
  326. Width = 1,
  327. Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0)
  328. };
  329. vertical.ChangedPosition += delegate {
  330. ContentOffset = new Point (ContentOffset.X, vertical.Position);
  331. };
  332. vertical.Host = this;
  333. horizontal = new ScrollBarView (1, 0, isVertical: false) {
  334. X = 0,
  335. Y = Pos.AnchorEnd (1),
  336. Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0),
  337. Height = 1
  338. };
  339. horizontal.ChangedPosition += delegate {
  340. ContentOffset = new Point (horizontal.Position, ContentOffset.Y);
  341. };
  342. horizontal.Host = this;
  343. base.Add (contentView);
  344. CanFocus = true;
  345. MouseEnter += View_MouseEnter;
  346. MouseLeave += View_MouseLeave;
  347. }
  348. Size contentSize;
  349. Point contentOffset;
  350. bool showHorizontalScrollIndicator;
  351. bool showVerticalScrollIndicator;
  352. bool keepContentAlwaysInViewport = true;
  353. bool autoHideScrollBars = true;
  354. /// <summary>
  355. /// Represents the contents of the data shown inside the scrolview
  356. /// </summary>
  357. /// <value>The size of the content.</value>
  358. public Size ContentSize {
  359. get {
  360. return contentSize;
  361. }
  362. set {
  363. if (contentSize != value) {
  364. contentSize = value;
  365. contentView.Frame = new Rect (contentOffset, value);
  366. vertical.Size = contentSize.Height;
  367. horizontal.Size = contentSize.Width;
  368. SetNeedsDisplay ();
  369. }
  370. }
  371. }
  372. /// <summary>
  373. /// Represents the top left corner coordinate that is displayed by the scrollview
  374. /// </summary>
  375. /// <value>The content offset.</value>
  376. public Point ContentOffset {
  377. get {
  378. return contentOffset;
  379. }
  380. set {
  381. contentOffset = new Point (-Math.Abs (value.X), -Math.Abs (value.Y));
  382. contentView.Frame = new Rect (contentOffset, contentSize);
  383. vertical.Position = Math.Max (0, -contentOffset.Y);
  384. horizontal.Position = Math.Max (0, -contentOffset.X);
  385. SetNeedsDisplay ();
  386. }
  387. }
  388. /// <summary>
  389. /// If true the vertical/horizontal scroll bars won't be showed if it's not needed.
  390. /// </summary>
  391. public bool AutoHideScrollBars {
  392. get => autoHideScrollBars;
  393. set {
  394. if (autoHideScrollBars != value) {
  395. autoHideScrollBars = value;
  396. SetNeedsDisplay ();
  397. }
  398. }
  399. }
  400. /// <summary>
  401. /// Get or sets if the view-port is kept always visible in the area of this <see cref="ScrollView"/>
  402. /// </summary>
  403. public bool KeepContentAlwaysInViewport {
  404. get { return keepContentAlwaysInViewport; }
  405. set {
  406. if (keepContentAlwaysInViewport != value) {
  407. keepContentAlwaysInViewport = value;
  408. Point p = default;
  409. if (value && -contentOffset.X + Bounds.Width > contentSize.Width) {
  410. p = new Point (contentSize.Width - Bounds.Width + (showVerticalScrollIndicator ? 1 : 0), -contentOffset.Y);
  411. }
  412. if (value && -contentOffset.Y + Bounds.Height > contentSize.Height) {
  413. if (p == default) {
  414. p = new Point (-contentOffset.X, contentSize.Height - Bounds.Height + (showHorizontalScrollIndicator ? 1 : 0));
  415. } else {
  416. p.Y = contentSize.Height - Bounds.Height + (showHorizontalScrollIndicator ? 1 : 0);
  417. }
  418. }
  419. if (p != default) {
  420. ContentOffset = p;
  421. }
  422. }
  423. }
  424. }
  425. /// <summary>
  426. /// Adds the view to the scrollview.
  427. /// </summary>
  428. /// <param name="view">The view to add to the scrollview.</param>
  429. public override void Add (View view)
  430. {
  431. if (!IsOverridden (view)) {
  432. view.MouseEnter += View_MouseEnter;
  433. view.MouseLeave += View_MouseLeave;
  434. }
  435. contentView.Add (view);
  436. SetNeedsLayout ();
  437. }
  438. void View_MouseLeave (MouseEventArgs e)
  439. {
  440. Application.UngrabMouse ();
  441. }
  442. void View_MouseEnter (MouseEventArgs e)
  443. {
  444. Application.GrabMouse (this);
  445. }
  446. bool IsOverridden (View view)
  447. {
  448. Type t = view.GetType ();
  449. MethodInfo m = t.GetMethod ("MouseEvent");
  450. return m.DeclaringType == t && m.GetBaseDefinition ().DeclaringType == typeof (Responder);
  451. }
  452. /// <summary>
  453. /// Gets or sets the visibility for the horizontal scroll indicator.
  454. /// </summary>
  455. /// <value><c>true</c> if show horizontal scroll indicator; otherwise, <c>false</c>.</value>
  456. public bool ShowHorizontalScrollIndicator {
  457. get => showHorizontalScrollIndicator;
  458. set {
  459. if (value == showHorizontalScrollIndicator)
  460. return;
  461. showHorizontalScrollIndicator = value;
  462. SetNeedsLayout ();
  463. if (value) {
  464. base.Add (horizontal);
  465. horizontal.MouseEnter += View_MouseEnter;
  466. horizontal.MouseLeave += View_MouseLeave;
  467. } else {
  468. Remove (horizontal);
  469. horizontal.MouseEnter -= View_MouseEnter;
  470. horizontal.MouseLeave -= View_MouseLeave;
  471. }
  472. vertical.Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0);
  473. }
  474. }
  475. /// <summary>
  476. /// Removes all widgets from this container.
  477. /// </summary>
  478. /// <remarks>
  479. /// </remarks>
  480. public override void RemoveAll ()
  481. {
  482. contentView.RemoveAll ();
  483. }
  484. /// <summary>
  485. /// /// Gets or sets the visibility for the vertical scroll indicator.
  486. /// </summary>
  487. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  488. public bool ShowVerticalScrollIndicator {
  489. get => showVerticalScrollIndicator;
  490. set {
  491. if (value == showVerticalScrollIndicator)
  492. return;
  493. showVerticalScrollIndicator = value;
  494. SetNeedsLayout ();
  495. if (value) {
  496. base.Add (vertical);
  497. vertical.MouseEnter += View_MouseEnter;
  498. vertical.MouseLeave += View_MouseLeave;
  499. } else {
  500. Remove (vertical);
  501. vertical.MouseEnter -= View_MouseEnter;
  502. vertical.MouseLeave -= View_MouseLeave;
  503. }
  504. horizontal.Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0);
  505. }
  506. }
  507. /// <inheritdoc/>
  508. public override void Redraw (Rect region)
  509. {
  510. Driver.SetAttribute (ColorScheme.Normal);
  511. SetViewsNeedsDisplay ();
  512. Clear ();
  513. var savedClip = ClipToBounds ();
  514. OnDrawContent (new Rect (ContentOffset,
  515. new Size (Math.Max (Bounds.Width - (ShowVerticalScrollIndicator ? 1 : 0), 0),
  516. Math.Max (Bounds.Height - (ShowHorizontalScrollIndicator ? 1 : 0), 0))));
  517. contentView.Redraw (contentView.Frame);
  518. Driver.Clip = savedClip;
  519. if (autoHideScrollBars) {
  520. ShowHideScrollBars ();
  521. } else {
  522. if (ShowVerticalScrollIndicator) {
  523. vertical.Redraw (vertical.Bounds);
  524. }
  525. if (ShowHorizontalScrollIndicator) {
  526. horizontal.Redraw (horizontal.Bounds);
  527. }
  528. }
  529. // Fill in the bottom left corner
  530. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator) {
  531. AddRune (Bounds.Width - 1, Bounds.Height - 1, ' ');
  532. }
  533. Driver.SetAttribute (ColorScheme.Normal);
  534. }
  535. void ShowHideScrollBars ()
  536. {
  537. bool v = false, h = false; bool p = false;
  538. if (Bounds.Height == 0 || Bounds.Height > contentSize.Height) {
  539. if (ShowVerticalScrollIndicator) {
  540. ShowVerticalScrollIndicator = false;
  541. }
  542. v = false;
  543. } else if (Bounds.Height > 0 && Bounds.Height == contentSize.Height) {
  544. p = true;
  545. } else {
  546. if (!ShowVerticalScrollIndicator) {
  547. ShowVerticalScrollIndicator = true;
  548. }
  549. v = true;
  550. }
  551. if (Bounds.Width == 0 || Bounds.Width > contentSize.Width) {
  552. if (ShowHorizontalScrollIndicator) {
  553. ShowHorizontalScrollIndicator = false;
  554. }
  555. h = false;
  556. } else if (Bounds.Width > 0 && Bounds.Width == contentSize.Width && p) {
  557. if (ShowHorizontalScrollIndicator) {
  558. ShowHorizontalScrollIndicator = false;
  559. }
  560. h = false;
  561. if (ShowVerticalScrollIndicator) {
  562. ShowVerticalScrollIndicator = false;
  563. }
  564. v = false;
  565. } else {
  566. if (p) {
  567. if (!ShowVerticalScrollIndicator) {
  568. ShowVerticalScrollIndicator = true;
  569. }
  570. v = true;
  571. }
  572. if (!ShowHorizontalScrollIndicator) {
  573. ShowHorizontalScrollIndicator = true;
  574. }
  575. h = true;
  576. }
  577. var dim = Dim.Fill (h ? 1 : 0);
  578. if (!vertical.Height.Equals (dim)) {
  579. vertical.Height = dim;
  580. }
  581. dim = Dim.Fill (v ? 1 : 0);
  582. if (!horizontal.Width.Equals (dim)) {
  583. horizontal.Width = dim;
  584. }
  585. if (v) {
  586. vertical.SetRelativeLayout (Bounds);
  587. vertical.Redraw (vertical.Bounds);
  588. }
  589. if (h) {
  590. horizontal.SetRelativeLayout (Bounds);
  591. horizontal.Redraw (horizontal.Bounds);
  592. }
  593. }
  594. void SetViewsNeedsDisplay ()
  595. {
  596. foreach (View view in contentView.Subviews) {
  597. view.SetNeedsDisplay ();
  598. }
  599. }
  600. ///<inheritdoc/>
  601. public override void PositionCursor ()
  602. {
  603. if (InternalSubviews.Count == 0)
  604. Move (0, 0);
  605. else
  606. base.PositionCursor ();
  607. }
  608. /// <summary>
  609. /// Scrolls the view up.
  610. /// </summary>
  611. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  612. /// <param name="lines">Number of lines to scroll.</param>
  613. public bool ScrollUp (int lines)
  614. {
  615. if (contentOffset.Y < 0) {
  616. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  617. return true;
  618. }
  619. return false;
  620. }
  621. /// <summary>
  622. /// Scrolls the view to the left
  623. /// </summary>
  624. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  625. /// <param name="cols">Number of columns to scroll by.</param>
  626. public bool ScrollLeft (int cols)
  627. {
  628. if (contentOffset.X < 0) {
  629. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  630. return true;
  631. }
  632. return false;
  633. }
  634. /// <summary>
  635. /// Scrolls the view down.
  636. /// </summary>
  637. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  638. /// <param name="lines">Number of lines to scroll.</param>
  639. public bool ScrollDown (int lines)
  640. {
  641. if (CanScroll (lines, out _, true)) {
  642. ContentOffset = new Point (contentOffset.X, contentOffset.Y - lines);
  643. return true;
  644. }
  645. return false;
  646. }
  647. /// <summary>
  648. /// Scrolls the view to the right.
  649. /// </summary>
  650. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  651. /// <param name="cols">Number of columns to scroll by.</param>
  652. public bool ScrollRight (int cols)
  653. {
  654. if (CanScroll (cols, out _)) {
  655. ContentOffset = new Point (contentOffset.X - cols, contentOffset.Y);
  656. return true;
  657. }
  658. return false;
  659. }
  660. internal bool CanScroll (int n, out int max, bool isVertical = false)
  661. {
  662. var size = isVertical ?
  663. (KeepContentAlwaysInViewport ? Bounds.Height + (showHorizontalScrollIndicator ? -2 : -1) : 0) :
  664. (KeepContentAlwaysInViewport ? Bounds.Width + (showVerticalScrollIndicator ? -2 : -1) : 0);
  665. var cSize = isVertical ? -contentSize.Height : -contentSize.Width;
  666. var cOffSet = isVertical ? contentOffset.Y : contentOffset.X;
  667. var newSize = Math.Max (cSize, cOffSet - n);
  668. max = cSize < newSize - size ? n : -cSize + (cOffSet - size) - 1;
  669. if (cSize < newSize - size) {
  670. return true;
  671. }
  672. return false;
  673. }
  674. ///<inheritdoc/>
  675. public override bool ProcessKey (KeyEvent kb)
  676. {
  677. if (base.ProcessKey (kb))
  678. return true;
  679. switch (kb.Key) {
  680. case Key.CursorUp:
  681. return ScrollUp (1);
  682. case (Key)'v' | Key.AltMask:
  683. case Key.PageUp:
  684. return ScrollUp (Bounds.Height);
  685. case Key.V | Key.CtrlMask:
  686. case Key.PageDown:
  687. return ScrollDown (Bounds.Height);
  688. case Key.CursorDown:
  689. return ScrollDown (1);
  690. case Key.CursorLeft:
  691. return ScrollLeft (1);
  692. case Key.CursorRight:
  693. return ScrollRight (1);
  694. case Key.Home:
  695. return ScrollUp (contentSize.Height);
  696. case Key.End:
  697. return ScrollDown (contentSize.Height);
  698. }
  699. return false;
  700. }
  701. ///<inheritdoc/>
  702. public override bool MouseEvent (MouseEvent me)
  703. {
  704. if (me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
  705. me.Flags != MouseFlags.WheeledRight && me.Flags != MouseFlags.WheeledLeft &&
  706. me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  707. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  708. return false;
  709. }
  710. if (me.Flags == MouseFlags.WheeledDown && ShowVerticalScrollIndicator) {
  711. ScrollDown (1);
  712. } else if (me.Flags == MouseFlags.WheeledUp && ShowVerticalScrollIndicator) {
  713. ScrollUp (1);
  714. } else if (me.Flags == MouseFlags.WheeledRight && showHorizontalScrollIndicator) {
  715. ScrollRight (1);
  716. } else if (me.Flags == MouseFlags.WheeledLeft && ShowVerticalScrollIndicator) {
  717. ScrollLeft (1);
  718. } else if (me.X == vertical.Frame.X && ShowVerticalScrollIndicator) {
  719. vertical.MouseEvent (me);
  720. } else if (me.Y == horizontal.Frame.Y && ShowHorizontalScrollIndicator) {
  721. horizontal.MouseEvent (me);
  722. } else if (IsOverridden (me.View)) {
  723. Application.UngrabMouse ();
  724. return false;
  725. }
  726. return true;
  727. }
  728. ///<inheritdoc/>
  729. protected override void Dispose (bool disposing)
  730. {
  731. if (!showVerticalScrollIndicator) {
  732. // It was not added to SuperView, so it won't get disposed automatically
  733. vertical?.Dispose ();
  734. }
  735. if (!showHorizontalScrollIndicator) {
  736. // It was not added to SuperView, so it won't get disposed automatically
  737. horizontal?.Dispose ();
  738. }
  739. base.Dispose (disposing);
  740. }
  741. }
  742. }