ScrollView.cs 14 KB

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