ScrollView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. //
  2. // ScrollView.cs: ScrollView and ScrollBarView views.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // TODO:
  9. // - Mouse handling in scrollbarview
  10. // - focus in scrollview
  11. // - keyboard handling in scrollview to scroll
  12. // - focus handling in scrollview to auto scroll to focused view
  13. // - Raise events
  14. // - Perhaps allow an option to not display the scrollbar arrow indicators?
  15. using System;
  16. using System.Reflection;
  17. namespace Terminal.Gui {
  18. /// <summary>
  19. /// ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical
  20. /// </summary>
  21. /// <remarks>
  22. /// <para>
  23. /// The scrollbar is drawn to be a representation of the Size, assuming that the
  24. /// scroll position is set at Position.
  25. /// </para>
  26. /// <para>
  27. /// If the region to display the scrollbar is larger than three characters,
  28. /// arrow indicators are drawn.
  29. /// </para>
  30. /// </remarks>
  31. public class ScrollBarView : View {
  32. bool vertical;
  33. int size, position;
  34. /// <summary>
  35. /// The size that this scrollbar represents
  36. /// </summary>
  37. /// <value>The size.</value>
  38. public int Size {
  39. get => size;
  40. set {
  41. size = value;
  42. SetNeedsDisplay ();
  43. }
  44. }
  45. /// <summary>
  46. /// This event is raised when the position on the scrollbar has changed.
  47. /// </summary>
  48. public event Action ChangedPosition;
  49. /// <summary>
  50. /// The position to show the scrollbar at.
  51. /// </summary>
  52. /// <value>The position.</value>
  53. public int Position {
  54. get => position;
  55. set {
  56. position = value;
  57. SetNeedsDisplay ();
  58. }
  59. }
  60. void SetPosition (int newPos)
  61. {
  62. Position = newPos;
  63. ChangedPosition?.Invoke ();
  64. }
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class.
  67. /// </summary>
  68. /// <param name="rect">Frame for the scrollbar.</param>
  69. /// <param name="size">The size that this scrollbar represents.</param>
  70. /// <param name="position">The position within this scrollbar.</param>
  71. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  72. public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect)
  73. {
  74. vertical = isVertical;
  75. this.position = position;
  76. this.size = size;
  77. WantContinuousButtonPressed = true;
  78. }
  79. /// <summary>
  80. /// Redraw the scrollbar
  81. /// </summary>
  82. /// <param name="region">Region to be redrawn.</param>
  83. public override void Redraw(Rect region)
  84. {
  85. Driver.SetAttribute (ColorScheme.Normal);
  86. if (vertical) {
  87. if (region.Right < Bounds.Width - 1)
  88. return;
  89. var col = Bounds.Width - 1;
  90. var bh = Bounds.Height;
  91. Rune special;
  92. if (bh < 4) {
  93. var by1 = position * bh / Size;
  94. var by2 = (position + bh) * bh / Size;
  95. for (int y = 0; y < bh; y++) {
  96. Move (col, y);
  97. if (y < by1 || y > by2)
  98. special = Driver.Stipple;
  99. else
  100. special = Driver.Diamond;
  101. Driver.AddRune(special);
  102. }
  103. } else {
  104. bh -= 2;
  105. var by1 = position * bh / Size;
  106. var by2 = (position + bh) * bh / Size;
  107. Move (col, 0);
  108. Driver.AddRune ('^');
  109. Move (col, Bounds.Height - 1);
  110. Driver.AddRune ('v');
  111. for (int y = 0; y < bh; y++) {
  112. Move (col, y+1);
  113. if (y < by1 - 1 || y > by2)
  114. special = Driver.Stipple;
  115. else {
  116. if (by2 - by1 == 0 && by1 < bh - 1)
  117. special = Driver.Diamond;
  118. else {
  119. if (y == by1 - 1)
  120. special = Driver.TopTee;
  121. else if (y == by2)
  122. special = Driver.BottomTee;
  123. else
  124. special = Driver.VLine;
  125. }
  126. }
  127. Driver.AddRune (special);
  128. }
  129. }
  130. } else {
  131. if (region.Bottom < Bounds.Height - 1)
  132. return;
  133. var row = Bounds.Height - 1;
  134. var bw = Bounds.Width;
  135. Rune special;
  136. if (bw < 4) {
  137. var bx1 = position * bw / Size;
  138. var bx2 = (position + bw) * bw / Size;
  139. for (int x = 0; x < bw; x++) {
  140. Move (0, x);
  141. if (x < bx1 || x > bx2)
  142. special = Driver.Stipple;
  143. else
  144. special = Driver.Diamond;
  145. Driver.AddRune (special);
  146. }
  147. } else {
  148. bw -= 2;
  149. var bx1 = position * bw / Size;
  150. var bx2 = (position + bw) * bw / Size;
  151. Move (0, row);
  152. Driver.AddRune ('<');
  153. for (int x = 0; x < bw; x++) {
  154. if (x < bx1 || x > bx2) {
  155. special = Driver.Stipple;
  156. } else {
  157. if (bx2 - bx1 == 0)
  158. special = Driver.Diamond;
  159. else {
  160. if (x == bx1)
  161. special = Driver.LeftTee;
  162. else if (x == bx2)
  163. special = Driver.RightTee;
  164. else
  165. special = Driver.HLine;
  166. }
  167. }
  168. Driver.AddRune (special);
  169. }
  170. Driver.AddRune ('>');
  171. }
  172. }
  173. }
  174. ///<inheritdoc cref="MouseEvent"/>
  175. public override bool MouseEvent(MouseEvent me)
  176. {
  177. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  178. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  179. return false;
  180. int location = vertical ? me.Y : me.X;
  181. int barsize = vertical ? Bounds.Height : Bounds.Width;
  182. if (barsize < 4) {
  183. // Handle scrollbars with no buttons
  184. Console.WriteLine ("TODO at ScrollBarView2");
  185. } else {
  186. barsize -= 2;
  187. // Handle scrollbars with arrow buttons
  188. var pos = Position;
  189. if (location == 0) {
  190. if (pos > 0)
  191. SetPosition (pos - 1);
  192. } else if (location == barsize + 1) {
  193. if (pos + 1 + barsize < Size)
  194. SetPosition (pos + 1);
  195. } else {
  196. var b1 = pos * barsize / Size;
  197. var b2 = (pos + barsize) * barsize / Size;
  198. if (b2 == 0 && location == 1 && pos == 0 ||
  199. (b2 == barsize && location == barsize) ||
  200. (location > b1 && location < b2)) {
  201. return true;
  202. } else if (location <= barsize) {
  203. if (location > 1 && location >= b2)
  204. SetPosition (Math.Min (pos + barsize, Size));
  205. else if (location <= b2 && pos > 0 || pos > 0)
  206. SetPosition (Math.Max (pos - barsize, 0));
  207. }
  208. }
  209. }
  210. return true;
  211. }
  212. }
  213. /// <summary>
  214. /// Scrollviews are views that present a window into a virtual space where children views are added. Similar to the iOS UIScrollView.
  215. /// </summary>
  216. /// <remarks>
  217. /// <para>
  218. /// The subviews that are added to this scrollview are offset by the
  219. /// ContentOffset property. The view itself is a window into the
  220. /// space represented by the ContentSize.
  221. /// </para>
  222. /// <para>
  223. ///
  224. /// </para>
  225. /// </remarks>
  226. public class ScrollView : View {
  227. View contentView;
  228. ScrollBarView vertical, horizontal;
  229. /// <summary>
  230. /// Constructs a ScrollView
  231. /// </summary>
  232. /// <param name="frame"></param>
  233. public ScrollView (Rect frame) : base (frame)
  234. {
  235. contentView = new View (frame);
  236. vertical = new ScrollBarView (new Rect (frame.Width - 1, 0, 1, frame.Height), frame.Height, 0, isVertical: true);
  237. vertical.ChangedPosition += delegate {
  238. ContentOffset = new Point (ContentOffset.X, vertical.Position);
  239. };
  240. horizontal = new ScrollBarView (new Rect (0, frame.Height-1, frame.Width-1, 1), frame.Width-1, 0, isVertical: false);
  241. horizontal.ChangedPosition += delegate {
  242. ContentOffset = new Point (horizontal.Position, ContentOffset.Y);
  243. };
  244. base.Add (contentView);
  245. CanFocus = true;
  246. }
  247. Size contentSize;
  248. Point contentOffset;
  249. bool showHorizontalScrollIndicator;
  250. bool showVerticalScrollIndicator;
  251. /// <summary>
  252. /// Represents the contents of the data shown inside the scrolview
  253. /// </summary>
  254. /// <value>The size of the content.</value>
  255. public Size ContentSize {
  256. get {
  257. return contentSize;
  258. }
  259. set {
  260. contentSize = value;
  261. contentView.Frame = new Rect (contentOffset, value);
  262. vertical.Size = contentSize.Height;
  263. horizontal.Size = contentSize.Width;
  264. SetNeedsDisplay ();
  265. }
  266. }
  267. /// <summary>
  268. /// Represents the top left corner coordinate that is displayed by the scrollview
  269. /// </summary>
  270. /// <value>The content offset.</value>
  271. public Point ContentOffset {
  272. get {
  273. return contentOffset;
  274. }
  275. set {
  276. contentOffset = new Point (-Math.Abs (value.X), -Math.Abs(value.Y));
  277. contentView.Frame = new Rect (contentOffset, contentSize);
  278. vertical.Position = Math.Max (0, -contentOffset.Y);
  279. horizontal.Position = Math.Max (0, -contentOffset.X);
  280. SetNeedsDisplay ();
  281. }
  282. }
  283. /// <summary>
  284. /// Adds the view to the scrollview.
  285. /// </summary>
  286. /// <param name="view">The view to add to the scrollview.</param>
  287. public override void Add (View view)
  288. {
  289. if (!IsOverridden (view)) {
  290. view.MouseEnter += View_MouseEnter;
  291. view.MouseLeave += View_MouseLeave;
  292. vertical.MouseEnter += View_MouseEnter;
  293. vertical.MouseLeave += View_MouseLeave;
  294. horizontal.MouseEnter += View_MouseEnter;
  295. horizontal.MouseLeave += View_MouseLeave;
  296. }
  297. contentView.Add (view);
  298. }
  299. void View_MouseLeave (object sender, MouseEventEventArgs e)
  300. {
  301. Application.UngrabMouse ();
  302. }
  303. void View_MouseEnter (object sender, MouseEventEventArgs e)
  304. {
  305. Application.GrabMouse (this);
  306. }
  307. bool IsOverridden (View view)
  308. {
  309. Type t = view.GetType ();
  310. MethodInfo m = t.GetMethod ("MouseEvent");
  311. return m.DeclaringType == t && m.GetBaseDefinition ().DeclaringType == typeof (Responder);
  312. }
  313. /// <summary>
  314. /// Gets or sets the visibility for the horizontal scroll indicator.
  315. /// </summary>
  316. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  317. public bool ShowHorizontalScrollIndicator {
  318. get => showHorizontalScrollIndicator;
  319. set {
  320. if (value == showHorizontalScrollIndicator)
  321. return;
  322. showHorizontalScrollIndicator = value;
  323. SetNeedsDisplay ();
  324. if (value)
  325. base.Add (horizontal);
  326. else
  327. Remove (horizontal);
  328. }
  329. }
  330. /// <summary>
  331. /// Removes all widgets from this container.
  332. /// </summary>
  333. /// <remarks>
  334. /// </remarks>
  335. public override void RemoveAll()
  336. {
  337. contentView.RemoveAll();
  338. }
  339. /// <summary>
  340. /// /// Gets or sets the visibility for the vertical scroll indicator.
  341. /// </summary>
  342. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  343. public bool ShowVerticalScrollIndicator {
  344. get => showVerticalScrollIndicator;
  345. set {
  346. if (value == showVerticalScrollIndicator)
  347. return;
  348. showVerticalScrollIndicator = value;
  349. SetNeedsDisplay ();
  350. if (value)
  351. base.Add (vertical);
  352. else
  353. Remove (vertical);
  354. }
  355. }
  356. /// <summary>
  357. /// This event is raised when the contents have scrolled
  358. /// </summary>
  359. //public event Action<ScrollView> Scrolled;
  360. public override void Redraw(Rect region)
  361. {
  362. SetViewsNeedsDisplay ();
  363. Driver.SetAttribute (ColorScheme.Normal);
  364. Clear ();
  365. var savedClip = ClipToBounds ();
  366. contentView.Redraw (contentView.Bounds);
  367. vertical.Redraw (vertical.Bounds);
  368. horizontal.Redraw (vertical.Bounds);
  369. Driver.Clip = savedClip;
  370. Driver.SetAttribute (ColorScheme.Normal);
  371. }
  372. void SetViewsNeedsDisplay ()
  373. {
  374. foreach (View view in contentView) {
  375. view.SetNeedsDisplay ();
  376. }
  377. }
  378. ///<inheritdoc cref="PositionCursor"/>
  379. public override void PositionCursor()
  380. {
  381. if (InternalSubviews.Count == 0)
  382. Driver.Move (0, 0);
  383. else
  384. base.PositionCursor ();
  385. }
  386. /// <summary>
  387. /// Scrolls the view up.
  388. /// </summary>
  389. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  390. /// <param name="lines">Number of lines to scroll.</param>
  391. public bool ScrollUp (int lines)
  392. {
  393. if (contentOffset.Y < 0) {
  394. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  395. return true;
  396. }
  397. return false;
  398. }
  399. /// <summary>
  400. /// Scrolls the view to the left
  401. /// </summary>
  402. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  403. /// <param name="cols">Number of columns to scroll by.</param>
  404. public bool ScrollLeft (int cols)
  405. {
  406. if (contentOffset.X < 0) {
  407. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  408. return true;
  409. }
  410. return false;
  411. }
  412. /// <summary>
  413. /// Scrolls the view down.
  414. /// </summary>
  415. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  416. /// <param name="lines">Number of lines to scroll.</param>
  417. public bool ScrollDown (int lines)
  418. {
  419. var ny = Math.Max (-contentSize.Height, contentOffset.Y - lines);
  420. if (ny == contentOffset.Y)
  421. return false;
  422. ContentOffset = new Point (contentOffset.X, ny);
  423. return true;
  424. }
  425. /// <summary>
  426. /// Scrolls the view to the right.
  427. /// </summary>
  428. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  429. /// <param name="cols">Number of columns to scroll by.</param>
  430. public bool ScrollRight (int cols)
  431. {
  432. var nx = Math.Max (-contentSize.Width, contentOffset.X - cols);
  433. if (nx == contentOffset.X)
  434. return false;
  435. ContentOffset = new Point (nx, contentOffset.Y);
  436. return true;
  437. }
  438. ///<inheritdoc cref="ProcessKey"/>
  439. public override bool ProcessKey(KeyEvent kb)
  440. {
  441. if (base.ProcessKey (kb))
  442. return true;
  443. switch (kb.Key) {
  444. case Key.CursorUp:
  445. return ScrollUp (1);
  446. case (Key)'v' | Key.AltMask:
  447. case Key.PageUp:
  448. return ScrollUp (Bounds.Height);
  449. case Key.ControlV:
  450. case Key.PageDown:
  451. return ScrollDown (Bounds.Height);
  452. case Key.CursorDown:
  453. return ScrollDown (1);
  454. case Key.CursorLeft:
  455. return ScrollLeft (1);
  456. case Key.CursorRight:
  457. return ScrollRight (1);
  458. case Key.Home:
  459. return ScrollUp (contentSize.Height);
  460. case Key.End:
  461. return ScrollDown (contentSize.Height);
  462. }
  463. return false;
  464. }
  465. ///<inheritdoc cref="MouseEvent(Gui.MouseEvent)"/>
  466. public override bool MouseEvent (MouseEvent me)
  467. {
  468. if (me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
  469. me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  470. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  471. return false;
  472. if (me.Flags == MouseFlags.WheeledDown)
  473. ScrollDown (1);
  474. else if (me.Flags == MouseFlags.WheeledUp)
  475. ScrollUp (1);
  476. else if (me.X == vertical.Frame.X)
  477. vertical.MouseEvent (me);
  478. else if (me.Y == horizontal.Frame.Y)
  479. horizontal.MouseEvent (me);
  480. else if (IsOverridden (me.View)) {
  481. Application.UngrabMouse ();
  482. return false;
  483. }
  484. return true;
  485. }
  486. }
  487. }