ScrollView.cs 12 KB

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