ScrollView.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. //
  2. // ScrollView.cs: ScrollView and ScrollBarView views.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // TODO:
  9. // - focus in scrollview
  10. // - focus handling in scrollview to auto scroll to focused view
  11. // - Raise events
  12. // - Perhaps allow an option to not display the scrollbar arrow indicators?
  13. using System;
  14. using System.Reflection;
  15. namespace Terminal.Gui {
  16. /// <summary>
  17. /// ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical
  18. /// </summary>
  19. /// <remarks>
  20. /// <para>
  21. /// The scrollbar is drawn to be a representation of the Size, assuming that the
  22. /// scroll position is set at Position.
  23. /// </para>
  24. /// <para>
  25. /// If the region to display the scrollbar is larger than three characters,
  26. /// arrow indicators are drawn.
  27. /// </para>
  28. /// </remarks>
  29. public class ScrollBarView : View {
  30. bool vertical = false;
  31. int size = 0, position = 0;
  32. /// <summary>
  33. /// If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.
  34. /// </summary>
  35. public bool IsVertical {
  36. get => vertical;
  37. set {
  38. vertical = value;
  39. SetNeedsDisplay ();
  40. }
  41. }
  42. /// <summary>
  43. /// The size of content the scrollbar represents.
  44. /// </summary>
  45. /// <value>The size.</value>
  46. /// <remarks>The <see cref="Size"/> is typically the size of the virtual content. E.g. when a Scrollbar is
  47. /// part of a <see cref="ScrollView"/> the Size is set to the appropriate dimension of <see cref="ScrollView.ContentSize"/>.</remarks>
  48. public int Size {
  49. get => size;
  50. set {
  51. size = value;
  52. SetNeedsDisplay ();
  53. }
  54. }
  55. /// <summary>
  56. /// This event is raised when the position on the scrollbar has changed.
  57. /// </summary>
  58. public Action ChangedPosition;
  59. /// <summary>
  60. /// The position, relative to <see cref="Size"/>, to set the scrollbar at.
  61. /// </summary>
  62. /// <value>The position.</value>
  63. public int Position {
  64. get => position;
  65. set {
  66. position = value;
  67. SetNeedsDisplay ();
  68. }
  69. }
  70. void SetPosition (int newPos)
  71. {
  72. Position = newPos;
  73. ChangedPosition?.Invoke ();
  74. }
  75. /// <summary>
  76. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  77. /// </summary>
  78. /// <param name="rect">Frame for the scrollbar.</param>
  79. public ScrollBarView (Rect rect) : this (rect, 0, 0, false) { }
  80. /// <summary>
  81. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  82. /// </summary>
  83. /// <param name="rect">Frame for the scrollbar.</param>
  84. /// <param name="size">The size that this scrollbar represents. Sets the <see cref="Size"/> property.</param>
  85. /// <param name="position">The position within this scrollbar. Sets the <see cref="Position"/> property.</param>
  86. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal. Sets the <see cref="IsVertical"/> property.</param>
  87. public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect)
  88. {
  89. Init (size, position, isVertical);
  90. }
  91. /// <summary>
  92. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  93. /// </summary>
  94. public ScrollBarView () : this (0, 0, false) { }
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  97. /// </summary>
  98. /// <param name="size">The size that this scrollbar represents.</param>
  99. /// <param name="position">The position within this scrollbar.</param>
  100. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  101. public ScrollBarView (int size, int position, bool isVertical) : base ()
  102. {
  103. Init (size, position, isVertical);
  104. }
  105. void Init (int size, int position, bool isVertical)
  106. {
  107. vertical = isVertical;
  108. this.position = position;
  109. this.size = size;
  110. WantContinuousButtonPressed = true;
  111. }
  112. int posTopTee;
  113. int posLeftTee;
  114. int posBottomTee;
  115. int posRightTee;
  116. ///<inheritdoc/>
  117. public override void Redraw (Rect region)
  118. {
  119. if (ColorScheme == null || Size == 0)
  120. return;
  121. Driver.SetAttribute (ColorScheme.Normal);
  122. if (Bounds.Height == 0) {
  123. return;
  124. }
  125. if (vertical) {
  126. if (region.Right < Bounds.Width - 1)
  127. return;
  128. var col = Bounds.Width - 1;
  129. var bh = Bounds.Height;
  130. Rune special;
  131. if (bh < 4) {
  132. var by1 = position * bh / Size;
  133. var by2 = (position + bh) * bh / Size;
  134. Move (col, 0);
  135. if (Bounds.Height == 1) {
  136. Driver.AddRune (Driver.Diamond);
  137. } else {
  138. Driver.AddRune (Driver.UpArrow);
  139. }
  140. if (Bounds.Height == 3) {
  141. Move (col, 1);
  142. Driver.AddRune (Driver.Diamond);
  143. }
  144. if (Bounds.Height > 1) {
  145. Move (col, Bounds.Height - 1);
  146. Driver.AddRune (Driver.DownArrow);
  147. }
  148. } else {
  149. bh -= 2;
  150. var by1 = position * bh / Size;
  151. var by2 = (position + bh) * bh / Size;
  152. Move (col, 0);
  153. Driver.AddRune (Driver.UpArrow);
  154. Move (col, Bounds.Height - 1);
  155. Driver.AddRune (Driver.DownArrow);
  156. bool hasTopTee = false;
  157. bool hasDiamond = false;
  158. bool hasBottomTee = false;
  159. for (int y = 0; y < bh; y++) {
  160. Move (col, y + 1);
  161. if ((y < by1 || y > by2) && ((position > 0 && !hasTopTee) || (hasTopTee && hasBottomTee))) {
  162. special = Driver.Stipple;
  163. } else {
  164. if (y != by2 && y > 1 && by2 - by1 == 0 && by1 < bh - 1 && hasTopTee && !hasDiamond) {
  165. hasDiamond = true;
  166. special = Driver.Diamond;
  167. } else {
  168. if (y == by1 && !hasTopTee) {
  169. hasTopTee = true;
  170. posTopTee = y;
  171. special = Driver.TopTee;
  172. } else if ((y >= by2 || by2 == 0) && !hasBottomTee) {
  173. hasBottomTee = true;
  174. posBottomTee = y;
  175. special = Driver.BottomTee;
  176. } else {
  177. special = Driver.VLine;
  178. }
  179. }
  180. }
  181. Driver.AddRune (special);
  182. }
  183. if (!hasTopTee) {
  184. Move (col, Bounds.Height - 2);
  185. Driver.AddRune (Driver.TopTee);
  186. }
  187. }
  188. } else {
  189. if (region.Bottom < Bounds.Height - 1)
  190. return;
  191. var row = Bounds.Height - 1;
  192. var bw = Bounds.Width;
  193. Rune special;
  194. if (bw < 4) {
  195. var bx1 = position * bw / Size;
  196. var bx2 = (position + bw) * bw / Size;
  197. Move (0, row);
  198. Driver.AddRune (Driver.LeftArrow);
  199. Driver.AddRune (Driver.RightArrow);
  200. } else {
  201. bw -= 2;
  202. var bx1 = position * bw / Size;
  203. var bx2 = (position + bw) * bw / Size;
  204. Move (0, row);
  205. Driver.AddRune (Driver.LeftArrow);
  206. bool hasLeftTee = false;
  207. bool hasDiamond = false;
  208. bool hasRightTee = false;
  209. for (int x = 0; x < bw; x++) {
  210. if ((x < bx1 || x >= bx2 + 1) && ((position > 0 && !hasLeftTee) || (hasLeftTee && hasRightTee))) {
  211. special = Driver.Stipple;
  212. } else {
  213. if (x != bx2 && x > 1 && bx2 - bx1 == 0 && bx1 < bw - 1 && hasLeftTee && !hasDiamond) {
  214. hasDiamond = true;
  215. special = Driver.Diamond;
  216. } else {
  217. if (x == bx1 && !hasLeftTee) {
  218. hasLeftTee = true;
  219. posLeftTee = x;
  220. special = Driver.LeftTee;
  221. } else if ((x >= bx2 || bx2 == 0) && !hasRightTee) {
  222. hasRightTee = true;
  223. posRightTee = x;
  224. special = Driver.RightTee;
  225. } else {
  226. special = Driver.HLine;
  227. }
  228. }
  229. }
  230. Driver.AddRune (special);
  231. }
  232. if (!hasLeftTee) {
  233. Move (Bounds.Width -2, row);
  234. Driver.AddRune (Driver.LeftTee);
  235. }
  236. Driver.AddRune (Driver.RightArrow);
  237. }
  238. }
  239. }
  240. ///<inheritdoc/>
  241. public override bool MouseEvent (MouseEvent me)
  242. {
  243. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  244. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  245. return false;
  246. int location = vertical ? me.Y : me.X;
  247. int barsize = vertical ? Bounds.Height : Bounds.Width;
  248. int posTopLeftTee = vertical ? posTopTee : posLeftTee;
  249. int posBottomRightTee = vertical ? posBottomTee : posRightTee;
  250. barsize -= 2;
  251. var pos = Position;
  252. if (location == 0) {
  253. if (pos > 0)
  254. SetPosition (pos - 1);
  255. } else if (location == barsize + 1) {
  256. if (pos + 1 < Size)
  257. SetPosition (pos + 1);
  258. } else {
  259. var b1 = pos * barsize / Size;
  260. var b2 = (pos + barsize) * barsize / Size;
  261. if (b1 == 0 && location == 1 && pos == 0 || (location >= posTopLeftTee + 1 && location <= posBottomRightTee + 1 && (pos != 0 || pos != Size - 1) && location != 1 && location != barsize) ||
  262. (b2 == barsize + (b2 - b1 - 1) && location == barsize && pos == Size - 1)) {
  263. return true;
  264. } else if (location <= barsize) {
  265. if (location > 1 && location > posTopLeftTee && location > posBottomRightTee)
  266. SetPosition (Math.Min (pos + (Size / location), Size - 1));
  267. else if (location <= b2 && pos > 0 || pos > 0)
  268. SetPosition (Math.Max (pos - (Size / barsize), 0));
  269. }
  270. }
  271. return true;
  272. }
  273. }
  274. /// <summary>
  275. /// Scrollviews are views that present a window into a virtual space where subviews are added. Similar to the iOS UIScrollView.
  276. /// </summary>
  277. /// <remarks>
  278. /// <para>
  279. /// The subviews that are added to this <see cref="Gui.ScrollView"/> are offset by the
  280. /// <see cref="ContentOffset"/> property. The view itself is a window into the
  281. /// space represented by the <see cref="ContentSize"/>.
  282. /// </para>
  283. /// <para>
  284. /// Use the
  285. /// </para>
  286. /// </remarks>
  287. public class ScrollView : View {
  288. View contentView = null;
  289. ScrollBarView vertical, horizontal;
  290. /// <summary>
  291. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Absolute"/> positioning.
  292. /// </summary>
  293. /// <param name="frame"></param>
  294. public ScrollView (Rect frame) : base (frame)
  295. {
  296. Init (frame);
  297. }
  298. /// <summary>
  299. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  300. /// </summary>
  301. public ScrollView () : base ()
  302. {
  303. Init (new Rect (0, 0, 0, 0));
  304. }
  305. void Init (Rect frame)
  306. {
  307. contentView = new View (frame);
  308. vertical = new ScrollBarView (1, 0, isVertical: true) {
  309. X = Pos.AnchorEnd (1),
  310. Y = 0,
  311. Width = 1,
  312. Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0)
  313. };
  314. vertical.ChangedPosition += delegate {
  315. ContentOffset = new Point (ContentOffset.X, vertical.Position);
  316. };
  317. horizontal = new ScrollBarView (1, 0, isVertical: false) {
  318. X = 0,
  319. Y = Pos.AnchorEnd (1),
  320. Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0),
  321. Height = 1
  322. };
  323. horizontal.ChangedPosition += delegate {
  324. ContentOffset = new Point (horizontal.Position, ContentOffset.Y);
  325. };
  326. base.Add (contentView);
  327. CanFocus = true;
  328. MouseEnter += View_MouseEnter;
  329. MouseLeave += View_MouseLeave;
  330. }
  331. Size contentSize;
  332. Point contentOffset;
  333. bool showHorizontalScrollIndicator;
  334. bool showVerticalScrollIndicator;
  335. /// <summary>
  336. /// Represents the contents of the data shown inside the scrolview
  337. /// </summary>
  338. /// <value>The size of the content.</value>
  339. public Size ContentSize {
  340. get {
  341. return contentSize;
  342. }
  343. set {
  344. contentSize = value;
  345. contentView.Frame = new Rect (contentOffset, value);
  346. vertical.Size = contentSize.Height;
  347. horizontal.Size = contentSize.Width;
  348. SetNeedsDisplay ();
  349. }
  350. }
  351. /// <summary>
  352. /// Represents the top left corner coordinate that is displayed by the scrollview
  353. /// </summary>
  354. /// <value>The content offset.</value>
  355. public Point ContentOffset {
  356. get {
  357. return contentOffset;
  358. }
  359. set {
  360. contentOffset = new Point (-Math.Abs (value.X), -Math.Abs (value.Y));
  361. contentView.Frame = new Rect (contentOffset, contentSize);
  362. vertical.Position = Math.Max (0, -contentOffset.Y);
  363. horizontal.Position = Math.Max (0, -contentOffset.X);
  364. SetNeedsDisplay ();
  365. }
  366. }
  367. /// <summary>
  368. /// If true the vertical/horizontal scroll bars won't be showed if it's not needed.
  369. /// </summary>
  370. public bool AutoHideScrollBars { get; set; } = true;
  371. /// <summary>
  372. /// Adds the view to the scrollview.
  373. /// </summary>
  374. /// <param name="view">The view to add to the scrollview.</param>
  375. public override void Add (View view)
  376. {
  377. if (!IsOverridden (view)) {
  378. view.MouseEnter += View_MouseEnter;
  379. view.MouseLeave += View_MouseLeave;
  380. }
  381. contentView.Add (view);
  382. SetNeedsLayout ();
  383. }
  384. void View_MouseLeave (MouseEventArgs e)
  385. {
  386. Application.UngrabMouse ();
  387. }
  388. void View_MouseEnter (MouseEventArgs e)
  389. {
  390. Application.GrabMouse (this);
  391. }
  392. bool IsOverridden (View view)
  393. {
  394. Type t = view.GetType ();
  395. MethodInfo m = t.GetMethod ("MouseEvent");
  396. return m.DeclaringType == t && m.GetBaseDefinition ().DeclaringType == typeof (Responder);
  397. }
  398. /// <summary>
  399. /// Gets or sets the visibility for the horizontal scroll indicator.
  400. /// </summary>
  401. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  402. public bool ShowHorizontalScrollIndicator {
  403. get => showHorizontalScrollIndicator;
  404. set {
  405. if (value == showHorizontalScrollIndicator)
  406. return;
  407. showHorizontalScrollIndicator = value;
  408. SetNeedsLayout ();
  409. if (value) {
  410. base.Add (horizontal);
  411. horizontal.MouseEnter += View_MouseEnter;
  412. horizontal.MouseLeave += View_MouseLeave;
  413. } else {
  414. Remove (horizontal);
  415. horizontal.MouseEnter -= View_MouseEnter;
  416. horizontal.MouseLeave -= View_MouseLeave;
  417. }
  418. vertical.Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0);
  419. }
  420. }
  421. /// <summary>
  422. /// Removes all widgets from this container.
  423. /// </summary>
  424. /// <remarks>
  425. /// </remarks>
  426. public override void RemoveAll ()
  427. {
  428. contentView.RemoveAll ();
  429. }
  430. /// <summary>
  431. /// /// Gets or sets the visibility for the vertical scroll indicator.
  432. /// </summary>
  433. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  434. public bool ShowVerticalScrollIndicator {
  435. get => showVerticalScrollIndicator;
  436. set {
  437. if (value == showVerticalScrollIndicator)
  438. return;
  439. showVerticalScrollIndicator = value;
  440. SetNeedsLayout ();
  441. if (value) {
  442. base.Add (vertical);
  443. vertical.MouseEnter += View_MouseEnter;
  444. vertical.MouseLeave += View_MouseLeave;
  445. } else {
  446. Remove (vertical);
  447. vertical.MouseEnter -= View_MouseEnter;
  448. vertical.MouseLeave -= View_MouseLeave;
  449. }
  450. horizontal.Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0);
  451. }
  452. }
  453. /// <inheritdoc/>
  454. public override void Redraw (Rect region)
  455. {
  456. Driver.SetAttribute (ColorScheme.Normal);
  457. SetViewsNeedsDisplay ();
  458. Clear ();
  459. var savedClip = ClipToBounds ();
  460. OnDrawContent (new Rect (ContentOffset,
  461. new Size (Math.Max (Bounds.Width - (ShowVerticalScrollIndicator ? 1 : 0), 0),
  462. Math.Max (Bounds.Height - (ShowHorizontalScrollIndicator ? 1 : 0), 0))));
  463. contentView.Redraw (contentView.Frame);
  464. Driver.Clip = savedClip;
  465. if (AutoHideScrollBars) {
  466. ShowHideScrollBars ();
  467. } else {
  468. if (ShowVerticalScrollIndicator) {
  469. vertical.Redraw (vertical.Bounds);
  470. }
  471. if (ShowHorizontalScrollIndicator) {
  472. horizontal.Redraw (horizontal.Bounds);
  473. }
  474. }
  475. // Fill in the bottom left corner
  476. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator) {
  477. AddRune (Bounds.Width - 1, Bounds.Height - 1, ' ');
  478. }
  479. Driver.SetAttribute (ColorScheme.Normal);
  480. }
  481. void ShowHideScrollBars ()
  482. {
  483. bool v = false, h = false; bool p = false;
  484. if (Bounds.Height == 0 || Bounds.Height > contentSize.Height) {
  485. if (ShowVerticalScrollIndicator) {
  486. ShowVerticalScrollIndicator = false;
  487. }
  488. v = false;
  489. } else if (Bounds.Height > 0 && Bounds.Height == contentSize.Height) {
  490. p = true;
  491. } else {
  492. if (!ShowVerticalScrollIndicator) {
  493. ShowVerticalScrollIndicator = true;
  494. }
  495. v = true;
  496. }
  497. if (Bounds.Width == 0 || Bounds.Width > contentSize.Width) {
  498. if (ShowHorizontalScrollIndicator) {
  499. ShowHorizontalScrollIndicator = false;
  500. }
  501. h = false;
  502. } else if (Bounds.Width > 0 && Bounds.Width == contentSize.Width && p) {
  503. if (ShowHorizontalScrollIndicator) {
  504. ShowHorizontalScrollIndicator = false;
  505. }
  506. h = false;
  507. if (ShowVerticalScrollIndicator) {
  508. ShowVerticalScrollIndicator = false;
  509. }
  510. v = false;
  511. } else {
  512. if (p) {
  513. if (!ShowVerticalScrollIndicator) {
  514. ShowVerticalScrollIndicator = true;
  515. }
  516. v = true;
  517. }
  518. if (!ShowHorizontalScrollIndicator) {
  519. ShowHorizontalScrollIndicator = true;
  520. }
  521. h = true;
  522. }
  523. vertical.Height = Dim.Fill (h ? 1 : 0);
  524. horizontal.Width = Dim.Fill (v ? 1 : 0);
  525. if (v) {
  526. vertical.SetRelativeLayout (Bounds);
  527. vertical.Redraw (vertical.Bounds);
  528. }
  529. if (h) {
  530. horizontal.SetRelativeLayout (Bounds);
  531. horizontal.Redraw (horizontal.Bounds);
  532. }
  533. }
  534. void SetViewsNeedsDisplay ()
  535. {
  536. foreach (View view in contentView) {
  537. view.SetNeedsDisplay ();
  538. }
  539. }
  540. ///<inheritdoc/>
  541. public override void PositionCursor ()
  542. {
  543. if (InternalSubviews.Count == 0)
  544. Move (0, 0);
  545. else
  546. base.PositionCursor ();
  547. }
  548. /// <summary>
  549. /// Scrolls the view up.
  550. /// </summary>
  551. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  552. /// <param name="lines">Number of lines to scroll.</param>
  553. public bool ScrollUp (int lines)
  554. {
  555. if (contentOffset.Y < 0) {
  556. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  557. return true;
  558. }
  559. return false;
  560. }
  561. /// <summary>
  562. /// Scrolls the view to the left
  563. /// </summary>
  564. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  565. /// <param name="cols">Number of columns to scroll by.</param>
  566. public bool ScrollLeft (int cols)
  567. {
  568. if (contentOffset.X < 0) {
  569. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  570. return true;
  571. }
  572. return false;
  573. }
  574. /// <summary>
  575. /// Scrolls the view down.
  576. /// </summary>
  577. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  578. /// <param name="lines">Number of lines to scroll.</param>
  579. public bool ScrollDown (int lines)
  580. {
  581. var ny = Math.Max (-contentSize.Height, contentOffset.Y - lines);
  582. if (ny == contentOffset.Y)
  583. return false;
  584. ContentOffset = new Point (contentOffset.X, ny);
  585. return true;
  586. }
  587. /// <summary>
  588. /// Scrolls the view to the right.
  589. /// </summary>
  590. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  591. /// <param name="cols">Number of columns to scroll by.</param>
  592. public bool ScrollRight (int cols)
  593. {
  594. var nx = Math.Max (-contentSize.Width, contentOffset.X - cols);
  595. if (nx == contentOffset.X)
  596. return false;
  597. ContentOffset = new Point (nx, contentOffset.Y);
  598. return true;
  599. }
  600. ///<inheritdoc/>
  601. public override bool ProcessKey (KeyEvent kb)
  602. {
  603. if (base.ProcessKey (kb))
  604. return true;
  605. switch (kb.Key) {
  606. case Key.CursorUp:
  607. return ScrollUp (1);
  608. case (Key)'v' | Key.AltMask:
  609. case Key.PageUp:
  610. return ScrollUp (Bounds.Height);
  611. case Key.ControlV:
  612. case Key.PageDown:
  613. return ScrollDown (Bounds.Height);
  614. case Key.CursorDown:
  615. return ScrollDown (1);
  616. case Key.CursorLeft:
  617. return ScrollLeft (1);
  618. case Key.CursorRight:
  619. return ScrollRight (1);
  620. case Key.Home:
  621. return ScrollUp (contentSize.Height);
  622. case Key.End:
  623. return ScrollDown (contentSize.Height);
  624. }
  625. return false;
  626. }
  627. ///<inheritdoc/>
  628. public override bool MouseEvent (MouseEvent me)
  629. {
  630. if (me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
  631. me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  632. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  633. return false;
  634. if (me.Flags == MouseFlags.WheeledDown && ShowVerticalScrollIndicator)
  635. ScrollDown (1);
  636. else if (me.Flags == MouseFlags.WheeledUp && ShowVerticalScrollIndicator)
  637. ScrollUp (1);
  638. else if (me.X == vertical.Frame.X && ShowVerticalScrollIndicator)
  639. vertical.MouseEvent (me);
  640. else if (me.Y == horizontal.Frame.Y && ShowHorizontalScrollIndicator)
  641. horizontal.MouseEvent (me);
  642. else if (IsOverridden (me.View)) {
  643. Application.UngrabMouse ();
  644. return false;
  645. }
  646. return true;
  647. }
  648. }
  649. }