2
0

ScrollView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. ///<inheritdoc cref="MouseEvent"/>
  174. public override bool MouseEvent(MouseEvent me)
  175. {
  176. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  177. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  178. return false;
  179. int location = vertical ? me.Y : me.X;
  180. int barsize = vertical ? Bounds.Height : Bounds.Width;
  181. if (barsize < 4) {
  182. // Handle scrollbars with no buttons
  183. Console.WriteLine ("TODO at ScrollBarView2");
  184. } else {
  185. barsize -= 2;
  186. // Handle scrollbars with arrow buttons
  187. var pos = Position;
  188. if (location == 0) {
  189. if (pos > 0)
  190. SetPosition (pos - 1);
  191. } else if (location == barsize + 1) {
  192. if (pos + 1 + barsize < Size)
  193. SetPosition (pos + 1);
  194. } else {
  195. var b1 = pos * barsize / Size;
  196. var b2 = (pos + barsize) * barsize / Size;
  197. if (b2 == 0 && location == 1 && pos == 0 ||
  198. (b2 == barsize && location == barsize) ||
  199. (location > b1 && location < b2)) {
  200. return true;
  201. } else if (location <= barsize) {
  202. if (location > 1 && location >= b2)
  203. SetPosition (Math.Min (pos + barsize, Size));
  204. else if (location <= b2 && pos > 0 || pos > 0)
  205. SetPosition (Math.Max (pos - barsize, 0));
  206. }
  207. }
  208. }
  209. return true;
  210. }
  211. }
  212. /// <summary>
  213. /// Scrollviews are views that present a window into a virtual space where children views are added. Similar to the iOS UIScrollView.
  214. /// </summary>
  215. /// <remarks>
  216. /// <para>
  217. /// The subviews that are added to this scrollview are offset by the
  218. /// ContentOffset property. The view itself is a window into the
  219. /// space represented by the ContentSize.
  220. /// </para>
  221. /// <para>
  222. ///
  223. /// </para>
  224. /// </remarks>
  225. public class ScrollView : View {
  226. View contentView;
  227. ScrollBarView vertical, horizontal;
  228. /// <summary>
  229. /// Constructs a ScrollView
  230. /// </summary>
  231. /// <param name="frame"></param>
  232. public ScrollView (Rect frame) : base (frame)
  233. {
  234. contentView = new View (frame);
  235. vertical = new ScrollBarView (new Rect (frame.Width - 1, 0, 1, frame.Height), frame.Height, 0, isVertical: true);
  236. vertical.ChangedPosition += delegate {
  237. ContentOffset = new Point (ContentOffset.X, vertical.Position);
  238. };
  239. horizontal = new ScrollBarView (new Rect (0, frame.Height-1, frame.Width-1, 1), frame.Width-1, 0, isVertical: false);
  240. horizontal.ChangedPosition += delegate {
  241. ContentOffset = new Point (horizontal.Position, ContentOffset.Y);
  242. };
  243. base.Add (contentView);
  244. CanFocus = true;
  245. }
  246. Size contentSize;
  247. Point contentOffset;
  248. bool showHorizontalScrollIndicator;
  249. bool showVerticalScrollIndicator;
  250. /// <summary>
  251. /// Represents the contents of the data shown inside the scrolview
  252. /// </summary>
  253. /// <value>The size of the content.</value>
  254. public Size ContentSize {
  255. get {
  256. return contentSize;
  257. }
  258. set {
  259. contentSize = value;
  260. contentView.Frame = new Rect (contentOffset, value);
  261. vertical.Size = contentSize.Height;
  262. horizontal.Size = contentSize.Width;
  263. SetNeedsDisplay ();
  264. }
  265. }
  266. /// <summary>
  267. /// Represents the top left corner coordinate that is displayed by the scrollview
  268. /// </summary>
  269. /// <value>The content offset.</value>
  270. public Point ContentOffset {
  271. get {
  272. return contentOffset;
  273. }
  274. set {
  275. contentOffset = new Point (-Math.Abs (value.X), -Math.Abs(value.Y));
  276. contentView.Frame = new Rect (contentOffset, contentSize);
  277. vertical.Position = Math.Max (0, -contentOffset.Y);
  278. horizontal.Position = Math.Max (0, -contentOffset.X);
  279. SetNeedsDisplay ();
  280. }
  281. }
  282. /// <summary>
  283. /// Adds the view to the scrollview.
  284. /// </summary>
  285. /// <param name="view">The view to add to the scrollview.</param>
  286. public override void Add (View view)
  287. {
  288. contentView.Add (view);
  289. }
  290. /// <summary>
  291. /// Gets or sets the visibility for the horizontal scroll indicator.
  292. /// </summary>
  293. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  294. public bool ShowHorizontalScrollIndicator {
  295. get => showHorizontalScrollIndicator;
  296. set {
  297. if (value == showHorizontalScrollIndicator)
  298. return;
  299. showHorizontalScrollIndicator = value;
  300. SetNeedsDisplay ();
  301. if (value)
  302. base.Add (horizontal);
  303. else
  304. Remove (horizontal);
  305. }
  306. }
  307. /// <summary>
  308. /// Removes all widgets from this container.
  309. /// </summary>
  310. /// <remarks>
  311. /// </remarks>
  312. public override void RemoveAll()
  313. {
  314. contentView.RemoveAll();
  315. }
  316. /// <summary>
  317. /// /// Gets or sets the visibility for the vertical scroll indicator.
  318. /// </summary>
  319. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  320. public bool ShowVerticalScrollIndicator {
  321. get => showVerticalScrollIndicator;
  322. set {
  323. if (value == showVerticalScrollIndicator)
  324. return;
  325. showVerticalScrollIndicator = value;
  326. SetNeedsDisplay ();
  327. if (value)
  328. base.Add (vertical);
  329. else
  330. Remove (vertical);
  331. }
  332. }
  333. /// <summary>
  334. /// This event is raised when the contents have scrolled
  335. /// </summary>
  336. //public event Action<ScrollView> Scrolled;
  337. public override void Redraw(Rect region)
  338. {
  339. SetViewsNeedsDisplay ();
  340. var oldClip = ClipToBounds ();
  341. Driver.SetAttribute (ColorScheme.Normal);
  342. Clear ();
  343. base.Redraw(region);
  344. Driver.Clip = oldClip;
  345. Driver.SetAttribute (ColorScheme.Normal);
  346. }
  347. void SetViewsNeedsDisplay ()
  348. {
  349. foreach (View view in contentView) {
  350. view.SetNeedsDisplay ();
  351. }
  352. }
  353. ///<inheritdoc cref="PositionCursor"/>
  354. public override void PositionCursor()
  355. {
  356. if (InternalSubviews.Count == 0)
  357. Driver.Move (0, 0);
  358. else
  359. base.PositionCursor ();
  360. }
  361. /// <summary>
  362. /// Scrolls the view up.
  363. /// </summary>
  364. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  365. /// <param name="lines">Number of lines to scroll.</param>
  366. public bool ScrollUp (int lines)
  367. {
  368. if (contentOffset.Y < 0) {
  369. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  370. return true;
  371. }
  372. return false;
  373. }
  374. /// <summary>
  375. /// Scrolls the view to the left
  376. /// </summary>
  377. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  378. /// <param name="cols">Number of columns to scroll by.</param>
  379. public bool ScrollLeft (int cols)
  380. {
  381. if (contentOffset.X < 0) {
  382. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  383. return true;
  384. }
  385. return false;
  386. }
  387. /// <summary>
  388. /// Scrolls the view down.
  389. /// </summary>
  390. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  391. /// <param name="lines">Number of lines to scroll.</param>
  392. public bool ScrollDown (int lines)
  393. {
  394. var ny = Math.Max (-contentSize.Height, contentOffset.Y - lines);
  395. if (ny == contentOffset.Y)
  396. return false;
  397. ContentOffset = new Point (contentOffset.X, ny);
  398. return true;
  399. }
  400. /// <summary>
  401. /// Scrolls the view to the right.
  402. /// </summary>
  403. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  404. /// <param name="cols">Number of columns to scroll by.</param>
  405. public bool ScrollRight (int cols)
  406. {
  407. var nx = Math.Max (-contentSize.Width, contentOffset.X - cols);
  408. if (nx == contentOffset.X)
  409. return false;
  410. ContentOffset = new Point (nx, contentOffset.Y);
  411. return true;
  412. }
  413. ///<inheritdoc cref="ProcessKey"/>
  414. public override bool ProcessKey(KeyEvent kb)
  415. {
  416. if (base.ProcessKey (kb))
  417. return true;
  418. switch (kb.Key) {
  419. case Key.CursorUp:
  420. return ScrollUp (1);
  421. case (Key) 'v' | Key.AltMask:
  422. case Key.PageUp:
  423. return ScrollUp (Bounds.Height);
  424. case Key.ControlV:
  425. case Key.PageDown:
  426. return ScrollDown (Bounds.Height);
  427. case Key.CursorDown:
  428. return ScrollDown (1);
  429. case Key.CursorLeft:
  430. return ScrollLeft (1);
  431. case Key.CursorRight:
  432. return ScrollRight (1);
  433. case Key.Home:
  434. return ScrollUp (contentSize.Height);
  435. case Key.End:
  436. return ScrollDown (contentSize.Height);
  437. }
  438. return false;
  439. }
  440. }
  441. }