ScrollView.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. /// <summary>
  354. /// Represents the contents of the data shown inside the scrolview
  355. /// </summary>
  356. /// <value>The size of the content.</value>
  357. public Size ContentSize {
  358. get {
  359. return contentSize;
  360. }
  361. set {
  362. if (contentSize != value) {
  363. contentSize = value;
  364. contentView.Frame = new Rect (contentOffset, value);
  365. vertical.Size = contentSize.Height;
  366. horizontal.Size = contentSize.Width;
  367. SetNeedsDisplay ();
  368. }
  369. }
  370. }
  371. /// <summary>
  372. /// Represents the top left corner coordinate that is displayed by the scrollview
  373. /// </summary>
  374. /// <value>The content offset.</value>
  375. public Point ContentOffset {
  376. get {
  377. return contentOffset;
  378. }
  379. set {
  380. contentOffset = new Point (-Math.Abs (value.X), -Math.Abs (value.Y));
  381. contentView.Frame = new Rect (contentOffset, contentSize);
  382. vertical.Position = Math.Max (0, -contentOffset.Y);
  383. horizontal.Position = Math.Max (0, -contentOffset.X);
  384. SetNeedsDisplay ();
  385. }
  386. }
  387. /// <summary>
  388. /// If true the vertical/horizontal scroll bars won't be showed if it's not needed.
  389. /// </summary>
  390. public bool AutoHideScrollBars { get; set; } = true;
  391. /// <summary>
  392. /// Get or sets if the view-port is kept always visible in the area of this <see cref="ScrollView"/>
  393. /// </summary>
  394. public bool KeepContentAlwaysInViewport {
  395. get { return keepContentAlwaysInViewport; }
  396. set {
  397. if (keepContentAlwaysInViewport != value) {
  398. keepContentAlwaysInViewport = value;
  399. Point p = default;
  400. if (value && -contentOffset.X + Bounds.Width > contentSize.Width) {
  401. p = new Point (contentSize.Width - Bounds.Width + (showVerticalScrollIndicator ? 1 : 0), -contentOffset.Y);
  402. }
  403. if (value && -contentOffset.Y + Bounds.Height > contentSize.Height) {
  404. if (p == default) {
  405. p = new Point (-contentOffset.X, contentSize.Height - Bounds.Height + (showHorizontalScrollIndicator ? 1 : 0));
  406. } else {
  407. p.Y = contentSize.Height - Bounds.Height + (showHorizontalScrollIndicator ? 1 : 0);
  408. }
  409. }
  410. if (p != default) {
  411. ContentOffset = p;
  412. }
  413. }
  414. }
  415. }
  416. /// <summary>
  417. /// Adds the view to the scrollview.
  418. /// </summary>
  419. /// <param name="view">The view to add to the scrollview.</param>
  420. public override void Add (View view)
  421. {
  422. if (!IsOverridden (view)) {
  423. view.MouseEnter += View_MouseEnter;
  424. view.MouseLeave += View_MouseLeave;
  425. }
  426. contentView.Add (view);
  427. SetNeedsLayout ();
  428. }
  429. void View_MouseLeave (MouseEventArgs e)
  430. {
  431. Application.UngrabMouse ();
  432. }
  433. void View_MouseEnter (MouseEventArgs e)
  434. {
  435. Application.GrabMouse (this);
  436. }
  437. bool IsOverridden (View view)
  438. {
  439. Type t = view.GetType ();
  440. MethodInfo m = t.GetMethod ("MouseEvent");
  441. return m.DeclaringType == t && m.GetBaseDefinition ().DeclaringType == typeof (Responder);
  442. }
  443. /// <summary>
  444. /// Gets or sets the visibility for the horizontal scroll indicator.
  445. /// </summary>
  446. /// <value><c>true</c> if show horizontal scroll indicator; otherwise, <c>false</c>.</value>
  447. public bool ShowHorizontalScrollIndicator {
  448. get => showHorizontalScrollIndicator;
  449. set {
  450. if (value == showHorizontalScrollIndicator)
  451. return;
  452. showHorizontalScrollIndicator = value;
  453. SetNeedsLayout ();
  454. if (value) {
  455. base.Add (horizontal);
  456. horizontal.MouseEnter += View_MouseEnter;
  457. horizontal.MouseLeave += View_MouseLeave;
  458. } else {
  459. Remove (horizontal);
  460. horizontal.MouseEnter -= View_MouseEnter;
  461. horizontal.MouseLeave -= View_MouseLeave;
  462. }
  463. vertical.Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0);
  464. }
  465. }
  466. /// <summary>
  467. /// Removes all widgets from this container.
  468. /// </summary>
  469. /// <remarks>
  470. /// </remarks>
  471. public override void RemoveAll ()
  472. {
  473. contentView.RemoveAll ();
  474. }
  475. /// <summary>
  476. /// /// Gets or sets the visibility for the vertical scroll indicator.
  477. /// </summary>
  478. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  479. public bool ShowVerticalScrollIndicator {
  480. get => showVerticalScrollIndicator;
  481. set {
  482. if (value == showVerticalScrollIndicator)
  483. return;
  484. showVerticalScrollIndicator = value;
  485. SetNeedsLayout ();
  486. if (value) {
  487. base.Add (vertical);
  488. vertical.MouseEnter += View_MouseEnter;
  489. vertical.MouseLeave += View_MouseLeave;
  490. } else {
  491. Remove (vertical);
  492. vertical.MouseEnter -= View_MouseEnter;
  493. vertical.MouseLeave -= View_MouseLeave;
  494. }
  495. horizontal.Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0);
  496. }
  497. }
  498. /// <inheritdoc/>
  499. public override void Redraw (Rect region)
  500. {
  501. Driver.SetAttribute (ColorScheme.Normal);
  502. SetViewsNeedsDisplay ();
  503. Clear ();
  504. var savedClip = ClipToBounds ();
  505. OnDrawContent (new Rect (ContentOffset,
  506. new Size (Math.Max (Bounds.Width - (ShowVerticalScrollIndicator ? 1 : 0), 0),
  507. Math.Max (Bounds.Height - (ShowHorizontalScrollIndicator ? 1 : 0), 0))));
  508. contentView.Redraw (contentView.Frame);
  509. Driver.Clip = savedClip;
  510. if (AutoHideScrollBars) {
  511. ShowHideScrollBars ();
  512. } else {
  513. if (ShowVerticalScrollIndicator) {
  514. vertical.Redraw (vertical.Bounds);
  515. }
  516. if (ShowHorizontalScrollIndicator) {
  517. horizontal.Redraw (horizontal.Bounds);
  518. }
  519. }
  520. // Fill in the bottom left corner
  521. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator) {
  522. AddRune (Bounds.Width - 1, Bounds.Height - 1, ' ');
  523. }
  524. Driver.SetAttribute (ColorScheme.Normal);
  525. }
  526. void ShowHideScrollBars ()
  527. {
  528. bool v = false, h = false; bool p = false;
  529. if (Bounds.Height == 0 || Bounds.Height > contentSize.Height) {
  530. if (ShowVerticalScrollIndicator) {
  531. ShowVerticalScrollIndicator = false;
  532. }
  533. v = false;
  534. } else if (Bounds.Height > 0 && Bounds.Height == contentSize.Height) {
  535. p = true;
  536. } else {
  537. if (!ShowVerticalScrollIndicator) {
  538. ShowVerticalScrollIndicator = true;
  539. }
  540. v = true;
  541. }
  542. if (Bounds.Width == 0 || Bounds.Width > contentSize.Width) {
  543. if (ShowHorizontalScrollIndicator) {
  544. ShowHorizontalScrollIndicator = false;
  545. }
  546. h = false;
  547. } else if (Bounds.Width > 0 && Bounds.Width == contentSize.Width && p) {
  548. if (ShowHorizontalScrollIndicator) {
  549. ShowHorizontalScrollIndicator = false;
  550. }
  551. h = false;
  552. if (ShowVerticalScrollIndicator) {
  553. ShowVerticalScrollIndicator = false;
  554. }
  555. v = false;
  556. } else {
  557. if (p) {
  558. if (!ShowVerticalScrollIndicator) {
  559. ShowVerticalScrollIndicator = true;
  560. }
  561. v = true;
  562. }
  563. if (!ShowHorizontalScrollIndicator) {
  564. ShowHorizontalScrollIndicator = true;
  565. }
  566. h = true;
  567. }
  568. vertical.Height = Dim.Fill (h ? 1 : 0);
  569. horizontal.Width = Dim.Fill (v ? 1 : 0);
  570. if (v) {
  571. vertical.SetRelativeLayout (Bounds);
  572. vertical.Redraw (vertical.Bounds);
  573. }
  574. if (h) {
  575. horizontal.SetRelativeLayout (Bounds);
  576. horizontal.Redraw (horizontal.Bounds);
  577. }
  578. }
  579. void SetViewsNeedsDisplay ()
  580. {
  581. foreach (View view in contentView.Subviews) {
  582. view.SetNeedsDisplay ();
  583. }
  584. }
  585. ///<inheritdoc/>
  586. public override void PositionCursor ()
  587. {
  588. if (InternalSubviews.Count == 0)
  589. Move (0, 0);
  590. else
  591. base.PositionCursor ();
  592. }
  593. /// <summary>
  594. /// Scrolls the view up.
  595. /// </summary>
  596. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  597. /// <param name="lines">Number of lines to scroll.</param>
  598. public bool ScrollUp (int lines)
  599. {
  600. if (contentOffset.Y < 0) {
  601. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  602. return true;
  603. }
  604. return false;
  605. }
  606. /// <summary>
  607. /// Scrolls the view to the left
  608. /// </summary>
  609. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  610. /// <param name="cols">Number of columns to scroll by.</param>
  611. public bool ScrollLeft (int cols)
  612. {
  613. if (contentOffset.X < 0) {
  614. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  615. return true;
  616. }
  617. return false;
  618. }
  619. /// <summary>
  620. /// Scrolls the view down.
  621. /// </summary>
  622. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  623. /// <param name="lines">Number of lines to scroll.</param>
  624. public bool ScrollDown (int lines)
  625. {
  626. if (CanScroll (lines, out _, true)) {
  627. ContentOffset = new Point (contentOffset.X, contentOffset.Y - lines);
  628. return true;
  629. }
  630. return false;
  631. }
  632. /// <summary>
  633. /// Scrolls the view to the right.
  634. /// </summary>
  635. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  636. /// <param name="cols">Number of columns to scroll by.</param>
  637. public bool ScrollRight (int cols)
  638. {
  639. if (CanScroll (cols, out _)) {
  640. ContentOffset = new Point (contentOffset.X - cols, contentOffset.Y);
  641. return true;
  642. }
  643. return false;
  644. }
  645. internal bool CanScroll (int n, out int max, bool isVertical = false)
  646. {
  647. var size = isVertical ?
  648. (KeepContentAlwaysInViewport ? Bounds.Height + (showHorizontalScrollIndicator ? -2 : -1) : 0) :
  649. (KeepContentAlwaysInViewport ? Bounds.Width + (showVerticalScrollIndicator ? -2 : -1) : 0);
  650. var cSize = isVertical ? -contentSize.Height : -contentSize.Width;
  651. var cOffSet = isVertical ? contentOffset.Y : contentOffset.X;
  652. var newSize = Math.Max (cSize, cOffSet - n);
  653. max = cSize < newSize - size ? n : -cSize + (cOffSet - size) - 1;
  654. if (cSize < newSize - size) {
  655. return true;
  656. }
  657. return false;
  658. }
  659. ///<inheritdoc/>
  660. public override bool ProcessKey (KeyEvent kb)
  661. {
  662. if (base.ProcessKey (kb))
  663. return true;
  664. switch (kb.Key) {
  665. case Key.CursorUp:
  666. return ScrollUp (1);
  667. case (Key)'v' | Key.AltMask:
  668. case Key.PageUp:
  669. return ScrollUp (Bounds.Height);
  670. case Key.V | Key.CtrlMask:
  671. case Key.PageDown:
  672. return ScrollDown (Bounds.Height);
  673. case Key.CursorDown:
  674. return ScrollDown (1);
  675. case Key.CursorLeft:
  676. return ScrollLeft (1);
  677. case Key.CursorRight:
  678. return ScrollRight (1);
  679. case Key.Home:
  680. return ScrollUp (contentSize.Height);
  681. case Key.End:
  682. return ScrollDown (contentSize.Height);
  683. }
  684. return false;
  685. }
  686. ///<inheritdoc/>
  687. public override bool MouseEvent (MouseEvent me)
  688. {
  689. if (me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
  690. me.Flags != MouseFlags.WheeledRight && me.Flags != MouseFlags.WheeledLeft &&
  691. me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  692. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  693. return false;
  694. }
  695. if (me.Flags == MouseFlags.WheeledDown && ShowVerticalScrollIndicator) {
  696. ScrollDown (1);
  697. } else if (me.Flags == MouseFlags.WheeledUp && ShowVerticalScrollIndicator) {
  698. ScrollUp (1);
  699. } else if (me.Flags == MouseFlags.WheeledRight && showHorizontalScrollIndicator) {
  700. ScrollRight (1);
  701. } else if (me.Flags == MouseFlags.WheeledLeft && ShowVerticalScrollIndicator) {
  702. ScrollLeft (1);
  703. } else if (me.X == vertical.Frame.X && ShowVerticalScrollIndicator) {
  704. vertical.MouseEvent (me);
  705. } else if (me.Y == horizontal.Frame.Y && ShowHorizontalScrollIndicator) {
  706. horizontal.MouseEvent (me);
  707. } else if (IsOverridden (me.View)) {
  708. Application.UngrabMouse ();
  709. return false;
  710. }
  711. return true;
  712. }
  713. ///<inheritdoc/>
  714. protected override void Dispose (bool disposing)
  715. {
  716. if (!showVerticalScrollIndicator) {
  717. // It was not added to SuperView, so it won't get disposed automatically
  718. vertical?.Dispose ();
  719. }
  720. if (!showHorizontalScrollIndicator) {
  721. // It was not added to SuperView, so it won't get disposed automatically
  722. horizontal?.Dispose ();
  723. }
  724. base.Dispose (disposing);
  725. }
  726. }
  727. }