ScrollView.cs 22 KB

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