ScrollView.cs 12 KB

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