ScrollBarView.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. //
  2. // ScrollBarView.cs: ScrollBarView view.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. namespace Terminal.Gui {
  9. /// <summary>
  10. /// ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// The scrollbar is drawn to be a representation of the Size, assuming that the
  15. /// scroll position is set at Position.
  16. /// </para>
  17. /// <para>
  18. /// If the region to display the scrollbar is larger than three characters,
  19. /// arrow indicators are drawn.
  20. /// </para>
  21. /// </remarks>
  22. public class ScrollBarView : View {
  23. bool vertical;
  24. int size, position;
  25. bool showScrollIndicator;
  26. bool keepContentAlwaysInViewport = true;
  27. bool autoHideScrollBars = true;
  28. bool hosted;
  29. ScrollBarView otherScrollBarView;
  30. View contentBottomRightCorner;
  31. bool showBothScrollIndicator => OtherScrollBarView != null && OtherScrollBarView.showScrollIndicator && showScrollIndicator;
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  34. /// </summary>
  35. /// <param name="rect">Frame for the scrollbar.</param>
  36. public ScrollBarView (Rect rect) : this (rect, 0, 0, false) { }
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  39. /// </summary>
  40. /// <param name="rect">Frame for the scrollbar.</param>
  41. /// <param name="size">The size that this scrollbar represents. Sets the <see cref="Size"/> property.</param>
  42. /// <param name="position">The position within this scrollbar. Sets the <see cref="Position"/> property.</param>
  43. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal. Sets the <see cref="IsVertical"/> property.</param>
  44. public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect)
  45. {
  46. Init (size, position, isVertical);
  47. }
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  50. /// </summary>
  51. public ScrollBarView () : this (0, 0, false) { }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  54. /// </summary>
  55. /// <param name="size">The size that this scrollbar represents.</param>
  56. /// <param name="position">The position within this scrollbar.</param>
  57. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  58. public ScrollBarView (int size, int position, bool isVertical) : base ()
  59. {
  60. Init (size, position, isVertical);
  61. }
  62. /// <summary>
  63. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  64. /// </summary>
  65. /// <param name="host">The view that will host this scrollbar.</param>
  66. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  67. /// <param name="showBothScrollIndicator">If set to <c>true (default)</c> will have the other scrollbar, otherwise will have only one.</param>
  68. public ScrollBarView (View host, bool isVertical, bool showBothScrollIndicator = true) : this (0, 0, isVertical)
  69. {
  70. if (host == null) {
  71. throw new ArgumentNullException ("The host parameter can't be null.");
  72. } else if (host.SuperView == null) {
  73. throw new ArgumentNullException ("The host SuperView parameter can't be null.");
  74. }
  75. hosted = true;
  76. ColorScheme = host.ColorScheme;
  77. X = isVertical ? Pos.Right (host) - 1 : Pos.Left (host);
  78. Y = isVertical ? Pos.Top (host) : Pos.Bottom (host) - 1;
  79. Host = host;
  80. CanFocus = false;
  81. Enabled = host.Enabled;
  82. Visible = host.Visible;
  83. //Host.CanFocusChanged += Host_CanFocusChanged;
  84. Host.EnabledChanged += Host_EnabledChanged;
  85. Host.VisibleChanged += Host_VisibleChanged;
  86. Host.SuperView.Add (this);
  87. AutoHideScrollBars = true;
  88. if (showBothScrollIndicator) {
  89. OtherScrollBarView = new ScrollBarView (0, 0, !isVertical) {
  90. ColorScheme = host.ColorScheme,
  91. Host = host,
  92. CanFocus = false,
  93. Enabled = host.Enabled,
  94. Visible = host.Visible,
  95. OtherScrollBarView = this
  96. };
  97. OtherScrollBarView.hosted = true;
  98. OtherScrollBarView.X = OtherScrollBarView.IsVertical ? Pos.Right (host) - 1 : Pos.Left (host);
  99. OtherScrollBarView.Y = OtherScrollBarView.IsVertical ? Pos.Top (host) : Pos.Bottom (host) - 1;
  100. OtherScrollBarView.Host.SuperView.Add (OtherScrollBarView);
  101. OtherScrollBarView.ShowScrollIndicator = true;
  102. }
  103. ShowScrollIndicator = true;
  104. contentBottomRightCorner = new View (" ") { Visible = host.Visible, ColorScheme = host.ColorScheme };
  105. Host.SuperView.Add (contentBottomRightCorner);
  106. contentBottomRightCorner.X = Pos.Right (host) - 1;
  107. contentBottomRightCorner.Y = Pos.Bottom (host) - 1;
  108. contentBottomRightCorner.Width = 1;
  109. contentBottomRightCorner.Height = 1;
  110. contentBottomRightCorner.MouseClick += ContentBottomRightCorner_MouseClick;
  111. contentBottomRightCorner.DrawContent += ContentBottomRightCorner_DrawContent;
  112. ClearOnVisibleFalse = false;
  113. }
  114. private void Host_VisibleChanged ()
  115. {
  116. if (!Host.Visible) {
  117. Visible = Host.Visible;
  118. if (otherScrollBarView != null) {
  119. otherScrollBarView.Visible = Visible;
  120. }
  121. contentBottomRightCorner.Visible = Visible;
  122. } else {
  123. ShowHideScrollBars ();
  124. }
  125. }
  126. private void Host_EnabledChanged ()
  127. {
  128. Enabled = Host.Enabled;
  129. if (otherScrollBarView != null) {
  130. otherScrollBarView.Enabled = Enabled;
  131. }
  132. contentBottomRightCorner.Enabled = Enabled;
  133. }
  134. //private void Host_CanFocusChanged ()
  135. //{
  136. // CanFocus = Host.CanFocus;
  137. // if (otherScrollBarView != null) {
  138. // otherScrollBarView.CanFocus = CanFocus;
  139. // }
  140. //}
  141. void ContentBottomRightCorner_MouseClick (MouseEventArgs me)
  142. {
  143. if (me.MouseEvent.Flags == MouseFlags.WheeledDown || me.MouseEvent.Flags == MouseFlags.WheeledUp
  144. || me.MouseEvent.Flags == MouseFlags.WheeledRight || me.MouseEvent.Flags == MouseFlags.WheeledLeft) {
  145. me.Handled = true;
  146. MouseEvent (me.MouseEvent);
  147. } else if (me.MouseEvent.Flags == MouseFlags.Button1Clicked) {
  148. me.Handled = true;
  149. Host.SetFocus ();
  150. }
  151. }
  152. private void ContentBottomRightCorner_DrawContent (Rect obj)
  153. {
  154. Driver.SetAttribute (Host.HasFocus ? GetFocusColor () : GetNormalColor ());
  155. Host.SuperView.AddRune (contentBottomRightCorner.Frame.X, contentBottomRightCorner.Frame.Y, ' ');
  156. }
  157. void Init (int size, int position, bool isVertical)
  158. {
  159. vertical = isVertical;
  160. this.position = position;
  161. this.size = size;
  162. WantContinuousButtonPressed = true;
  163. }
  164. /// <summary>
  165. /// If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.
  166. /// </summary>
  167. public bool IsVertical {
  168. get => vertical;
  169. set {
  170. vertical = value;
  171. SetNeedsDisplay ();
  172. }
  173. }
  174. /// <summary>
  175. /// The size of content the scrollbar represents.
  176. /// </summary>
  177. /// <value>The size.</value>
  178. /// <remarks>The <see cref="Size"/> is typically the size of the virtual content. E.g. when a Scrollbar is
  179. /// part of a <see cref="View"/> the Size is set to the appropriate dimension of <see cref="Host"/>.</remarks>
  180. public int Size {
  181. get => size;
  182. set {
  183. size = value;
  184. SetRelativeLayout (Bounds);
  185. ShowHideScrollBars (false);
  186. SetNeedsDisplay ();
  187. }
  188. }
  189. /// <summary>
  190. /// This event is raised when the position on the scrollbar has changed.
  191. /// </summary>
  192. public event Action ChangedPosition;
  193. /// <summary>
  194. /// The position, relative to <see cref="Size"/>, to set the scrollbar at.
  195. /// </summary>
  196. /// <value>The position.</value>
  197. public int Position {
  198. get => position;
  199. set {
  200. if (position != value) {
  201. if (CanScroll (value - position, out int max, vertical)) {
  202. if (max == value - position) {
  203. position = value;
  204. } else {
  205. position = Math.Max (position + max, 0);
  206. }
  207. } else if (max < 0) {
  208. position = Math.Max (position + max, 0);
  209. }
  210. var s = GetBarsize (vertical);
  211. OnChangedPosition ();
  212. SetNeedsDisplay ();
  213. }
  214. }
  215. }
  216. /// <summary>
  217. /// Get or sets the view that host this <see cref="View"/>
  218. /// </summary>
  219. public View Host { get; internal set; }
  220. /// <summary>
  221. /// Represent a vertical or horizontal ScrollBarView other than this.
  222. /// </summary>
  223. public ScrollBarView OtherScrollBarView {
  224. get => otherScrollBarView;
  225. set {
  226. if (value != null && (value.IsVertical && vertical || !value.IsVertical && !vertical)) {
  227. throw new ArgumentException ($"There is already a {(vertical ? "vertical" : "horizontal")} ScrollBarView.");
  228. }
  229. otherScrollBarView = value;
  230. }
  231. }
  232. /// <summary>
  233. /// Gets or sets the visibility for the vertical or horizontal scroll indicator.
  234. /// </summary>
  235. /// <value><c>true</c> if show vertical or horizontal scroll indicator; otherwise, <c>false</c>.</value>
  236. public bool ShowScrollIndicator {
  237. get => showScrollIndicator;
  238. set {
  239. if (value == showScrollIndicator) {
  240. return;
  241. }
  242. showScrollIndicator = value;
  243. SetNeedsLayout ();
  244. if (value) {
  245. Visible = true;
  246. } else {
  247. Visible = false;
  248. Position = 0;
  249. }
  250. SetWidthHeight ();
  251. }
  252. }
  253. /// <summary>
  254. /// Get or sets if the view-port is kept always visible in the area of this <see cref="ScrollBarView"/>
  255. /// </summary>
  256. public bool KeepContentAlwaysInViewport {
  257. get { return keepContentAlwaysInViewport; }
  258. set {
  259. if (keepContentAlwaysInViewport != value) {
  260. keepContentAlwaysInViewport = value;
  261. int pos = 0;
  262. if (value && !vertical && position + Host.Bounds.Width > size) {
  263. pos = size - Host.Bounds.Width + (showBothScrollIndicator ? 1 : 0);
  264. }
  265. if (value && vertical && position + Host.Bounds.Height > size) {
  266. pos = size - Host.Bounds.Height + (showBothScrollIndicator ? 1 : 0);
  267. }
  268. if (pos != 0) {
  269. Position = pos;
  270. }
  271. if (OtherScrollBarView != null && OtherScrollBarView.keepContentAlwaysInViewport != value) {
  272. OtherScrollBarView.KeepContentAlwaysInViewport = value;
  273. }
  274. if (pos == 0) {
  275. Refresh ();
  276. }
  277. }
  278. }
  279. }
  280. /// <summary>
  281. /// If true the vertical/horizontal scroll bars won't be showed if it's not needed.
  282. /// </summary>
  283. public bool AutoHideScrollBars {
  284. get => autoHideScrollBars;
  285. set {
  286. if (autoHideScrollBars != value) {
  287. autoHideScrollBars = value;
  288. SetNeedsDisplay ();
  289. }
  290. }
  291. }
  292. /// <summary>
  293. /// Virtual method to invoke the <see cref="ChangedPosition"/> action event.
  294. /// </summary>
  295. public virtual void OnChangedPosition ()
  296. {
  297. ChangedPosition?.Invoke ();
  298. }
  299. /// <summary>
  300. /// Only used for a hosted view that will update and redraw the scrollbars.
  301. /// </summary>
  302. public virtual void Refresh ()
  303. {
  304. ShowHideScrollBars ();
  305. }
  306. void ShowHideScrollBars (bool redraw = true)
  307. {
  308. if (!hosted || (hosted && !autoHideScrollBars)) {
  309. if (contentBottomRightCorner != null && contentBottomRightCorner.Visible) {
  310. contentBottomRightCorner.Visible = false;
  311. } else if (otherScrollBarView != null && otherScrollBarView.contentBottomRightCorner != null && otherScrollBarView.contentBottomRightCorner.Visible) {
  312. otherScrollBarView.contentBottomRightCorner.Visible = false;
  313. }
  314. return;
  315. }
  316. var pending = CheckBothScrollBars (this);
  317. if (otherScrollBarView != null) {
  318. CheckBothScrollBars (otherScrollBarView, pending);
  319. }
  320. SetWidthHeight ();
  321. SetRelativeLayout (Bounds);
  322. if (otherScrollBarView != null) {
  323. OtherScrollBarView.SetRelativeLayout (OtherScrollBarView.Bounds);
  324. }
  325. if (showBothScrollIndicator) {
  326. if (contentBottomRightCorner != null && !contentBottomRightCorner.Visible) {
  327. contentBottomRightCorner.Visible = true;
  328. } else if (otherScrollBarView != null && otherScrollBarView.contentBottomRightCorner != null && !otherScrollBarView.contentBottomRightCorner.Visible) {
  329. otherScrollBarView.contentBottomRightCorner.Visible = true;
  330. }
  331. } else if (!showScrollIndicator) {
  332. if (contentBottomRightCorner != null && contentBottomRightCorner.Visible) {
  333. contentBottomRightCorner.Visible = false;
  334. } else if (otherScrollBarView != null && otherScrollBarView.contentBottomRightCorner != null && otherScrollBarView.contentBottomRightCorner.Visible) {
  335. otherScrollBarView.contentBottomRightCorner.Visible = false;
  336. }
  337. if (Application.MouseGrabView != null && Application.MouseGrabView == this) {
  338. Application.UngrabMouse ();
  339. }
  340. } else if (contentBottomRightCorner != null && contentBottomRightCorner.Visible) {
  341. contentBottomRightCorner.Visible = false;
  342. } else if (otherScrollBarView != null && otherScrollBarView.contentBottomRightCorner != null && otherScrollBarView.contentBottomRightCorner.Visible) {
  343. otherScrollBarView.contentBottomRightCorner.Visible = false;
  344. }
  345. if (Host?.Visible == true && showScrollIndicator && !Visible) {
  346. Visible = true;
  347. }
  348. if (Host?.Visible == true && otherScrollBarView?.showScrollIndicator == true && !otherScrollBarView.Visible) {
  349. otherScrollBarView.Visible = true;
  350. }
  351. if (!redraw) {
  352. return;
  353. }
  354. if (showScrollIndicator) {
  355. Redraw (Bounds);
  356. }
  357. if (otherScrollBarView != null && otherScrollBarView.showScrollIndicator) {
  358. otherScrollBarView.Redraw (otherScrollBarView.Bounds);
  359. }
  360. }
  361. bool CheckBothScrollBars (ScrollBarView scrollBarView, bool pending = false)
  362. {
  363. int barsize = scrollBarView.vertical ? scrollBarView.Bounds.Height : scrollBarView.Bounds.Width;
  364. if (barsize == 0 || barsize >= scrollBarView.size) {
  365. if (scrollBarView.showScrollIndicator) {
  366. scrollBarView.ShowScrollIndicator = false;
  367. }
  368. if (scrollBarView.Visible) {
  369. scrollBarView.Visible = false;
  370. }
  371. } else if (barsize > 0 && barsize == scrollBarView.size && scrollBarView.OtherScrollBarView != null && pending) {
  372. if (scrollBarView.showScrollIndicator) {
  373. scrollBarView.ShowScrollIndicator = false;
  374. }
  375. if (scrollBarView.Visible) {
  376. scrollBarView.Visible = false;
  377. }
  378. if (scrollBarView.OtherScrollBarView != null && scrollBarView.showBothScrollIndicator) {
  379. scrollBarView.OtherScrollBarView.ShowScrollIndicator = false;
  380. }
  381. if (scrollBarView.OtherScrollBarView.Visible) {
  382. scrollBarView.OtherScrollBarView.Visible = false;
  383. }
  384. } else if (barsize > 0 && barsize == size && scrollBarView.OtherScrollBarView != null && !pending) {
  385. pending = true;
  386. } else {
  387. if (scrollBarView.OtherScrollBarView != null && pending) {
  388. if (!scrollBarView.showBothScrollIndicator) {
  389. scrollBarView.OtherScrollBarView.ShowScrollIndicator = true;
  390. }
  391. if (!scrollBarView.OtherScrollBarView.Visible) {
  392. scrollBarView.OtherScrollBarView.Visible = true;
  393. }
  394. }
  395. if (!scrollBarView.showScrollIndicator) {
  396. scrollBarView.ShowScrollIndicator = true;
  397. }
  398. if (!scrollBarView.Visible) {
  399. scrollBarView.Visible = true;
  400. }
  401. }
  402. return pending;
  403. }
  404. void SetWidthHeight ()
  405. {
  406. if (showBothScrollIndicator) {
  407. Width = vertical ? 1 : Dim.Width (Host) - 1;
  408. Height = vertical ? Dim.Height (Host) - 1 : 1;
  409. otherScrollBarView.Width = otherScrollBarView.vertical ? 1 : Dim.Width (Host) - 1;
  410. otherScrollBarView.Height = otherScrollBarView.vertical ? Dim.Height (Host) - 1 : 1;
  411. } else if (showScrollIndicator) {
  412. Width = vertical ? 1 : Dim.Width (Host) - 0;
  413. Height = vertical ? Dim.Height (Host) - 0 : 1;
  414. } else if (otherScrollBarView?.showScrollIndicator == true) {
  415. otherScrollBarView.Width = otherScrollBarView.vertical ? 1 : Dim.Width (Host) - 0;
  416. otherScrollBarView.Height = otherScrollBarView.vertical ? Dim.Height (Host) - 0 : 1;
  417. }
  418. }
  419. int posTopTee;
  420. int posLeftTee;
  421. int posBottomTee;
  422. int posRightTee;
  423. ///<inheritdoc/>
  424. public override void Redraw (Rect region)
  425. {
  426. if (ColorScheme == null || ((!showScrollIndicator || Size == 0) && AutoHideScrollBars && Visible)) {
  427. if ((!showScrollIndicator || Size == 0) && AutoHideScrollBars && Visible) {
  428. ShowHideScrollBars (false);
  429. }
  430. return;
  431. }
  432. Driver.SetAttribute (Host.HasFocus ? ColorScheme.Focus : GetNormalColor ());
  433. if ((vertical && Bounds.Height == 0) || (!vertical && Bounds.Width == 0)) {
  434. return;
  435. }
  436. if (vertical) {
  437. if (region.Right < Bounds.Width - 1) {
  438. return;
  439. }
  440. var col = Bounds.Width - 1;
  441. var bh = Bounds.Height;
  442. Rune special;
  443. if (bh < 4) {
  444. var by1 = position * bh / Size;
  445. var by2 = (position + bh) * bh / Size;
  446. Move (col, 0);
  447. if (Bounds.Height == 1) {
  448. Driver.AddRune (Driver.Diamond);
  449. } else {
  450. Driver.AddRune (Driver.UpArrow);
  451. }
  452. if (Bounds.Height == 3) {
  453. Move (col, 1);
  454. Driver.AddRune (Driver.Diamond);
  455. }
  456. if (Bounds.Height > 1) {
  457. Move (col, Bounds.Height - 1);
  458. Driver.AddRune (Driver.DownArrow);
  459. }
  460. } else {
  461. bh -= 2;
  462. var by1 = KeepContentAlwaysInViewport ? position * bh / Size : position * bh / (Size + bh);
  463. var by2 = KeepContentAlwaysInViewport ? Math.Min (((position + bh) * bh / Size) + 1, bh - 1) : (position + bh) * bh / (Size + bh);
  464. if (KeepContentAlwaysInViewport && by1 == by2) {
  465. by1 = Math.Max (by1 - 1, 0);
  466. }
  467. Move (col, 0);
  468. Driver.AddRune (Driver.UpArrow);
  469. Move (col, Bounds.Height - 1);
  470. Driver.AddRune (Driver.DownArrow);
  471. bool hasTopTee = false;
  472. bool hasDiamond = false;
  473. bool hasBottomTee = false;
  474. for (int y = 0; y < bh; y++) {
  475. Move (col, y + 1);
  476. if ((y < by1 || y > by2) && ((position > 0 && !hasTopTee) || (hasTopTee && hasBottomTee))) {
  477. special = Driver.Stipple;
  478. } else {
  479. if (y != by2 && y > 1 && by2 - by1 == 0 && by1 < bh - 1 && hasTopTee && !hasDiamond) {
  480. hasDiamond = true;
  481. special = Driver.Diamond;
  482. } else {
  483. if (y == by1 && !hasTopTee) {
  484. hasTopTee = true;
  485. posTopTee = y;
  486. special = Driver.TopTee;
  487. } else if ((position == 0 && y == bh - 1 || y >= by2 || by2 == 0) && !hasBottomTee) {
  488. hasBottomTee = true;
  489. posBottomTee = y;
  490. special = Driver.BottomTee;
  491. } else {
  492. special = Driver.VLine;
  493. }
  494. }
  495. }
  496. Driver.AddRune (special);
  497. }
  498. if (!hasTopTee) {
  499. Move (col, Bounds.Height - 2);
  500. Driver.AddRune (Driver.TopTee);
  501. }
  502. }
  503. } else {
  504. if (region.Bottom < Bounds.Height - 1) {
  505. return;
  506. }
  507. var row = Bounds.Height - 1;
  508. var bw = Bounds.Width;
  509. Rune special;
  510. if (bw < 4) {
  511. var bx1 = position * bw / Size;
  512. var bx2 = (position + bw) * bw / Size;
  513. Move (0, row);
  514. Driver.AddRune (Driver.LeftArrow);
  515. Driver.AddRune (Driver.RightArrow);
  516. } else {
  517. bw -= 2;
  518. var bx1 = KeepContentAlwaysInViewport ? position * bw / Size : position * bw / (Size + bw);
  519. var bx2 = KeepContentAlwaysInViewport ? Math.Min (((position + bw) * bw / Size) + 1, bw - 1) : (position + bw) * bw / (Size + bw);
  520. if (KeepContentAlwaysInViewport && bx1 == bx2) {
  521. bx1 = Math.Max (bx1 - 1, 0);
  522. }
  523. Move (0, row);
  524. Driver.AddRune (Driver.LeftArrow);
  525. bool hasLeftTee = false;
  526. bool hasDiamond = false;
  527. bool hasRightTee = false;
  528. for (int x = 0; x < bw; x++) {
  529. if ((x < bx1 || x >= bx2 + 1) && ((position > 0 && !hasLeftTee) || (hasLeftTee && hasRightTee))) {
  530. special = Driver.Stipple;
  531. } else {
  532. if (x != bx2 && x > 1 && bx2 - bx1 == 0 && bx1 < bw - 1 && hasLeftTee && !hasDiamond) {
  533. hasDiamond = true;
  534. special = Driver.Diamond;
  535. } else {
  536. if (x == bx1 && !hasLeftTee) {
  537. hasLeftTee = true;
  538. posLeftTee = x;
  539. special = Driver.LeftTee;
  540. } else if ((position == 0 && x == bw - 1 || x >= bx2 || bx2 == 0) && !hasRightTee) {
  541. hasRightTee = true;
  542. posRightTee = x;
  543. special = Driver.RightTee;
  544. } else {
  545. special = Driver.HLine;
  546. }
  547. }
  548. }
  549. Driver.AddRune (special);
  550. }
  551. if (!hasLeftTee) {
  552. Move (Bounds.Width - 2, row);
  553. Driver.AddRune (Driver.LeftTee);
  554. }
  555. Driver.AddRune (Driver.RightArrow);
  556. }
  557. }
  558. }
  559. int lastLocation = -1;
  560. int posBarOffset;
  561. ///<inheritdoc/>
  562. public override bool MouseEvent (MouseEvent mouseEvent)
  563. {
  564. if (mouseEvent.Flags != MouseFlags.Button1Pressed && mouseEvent.Flags != MouseFlags.Button1DoubleClicked &&
  565. !mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) &&
  566. mouseEvent.Flags != MouseFlags.Button1Released && mouseEvent.Flags != MouseFlags.WheeledDown &&
  567. mouseEvent.Flags != MouseFlags.WheeledUp && mouseEvent.Flags != MouseFlags.WheeledRight &&
  568. mouseEvent.Flags != MouseFlags.WheeledLeft && mouseEvent.Flags != MouseFlags.Button1TripleClicked) {
  569. return false;
  570. }
  571. if (!Host.CanFocus) {
  572. return true;
  573. }
  574. if (Host?.HasFocus == false) {
  575. Host.SetFocus ();
  576. }
  577. int location = vertical ? mouseEvent.Y : mouseEvent.X;
  578. int barsize = vertical ? Bounds.Height : Bounds.Width;
  579. int posTopLeftTee = vertical ? posTopTee + 1 : posLeftTee + 1;
  580. int posBottomRightTee = vertical ? posBottomTee + 1 : posRightTee + 1;
  581. barsize -= 2;
  582. var pos = Position;
  583. if (mouseEvent.Flags != MouseFlags.Button1Released
  584. && (Application.MouseGrabView == null || Application.MouseGrabView != this)) {
  585. Application.GrabMouse (this);
  586. } else if (mouseEvent.Flags == MouseFlags.Button1Released && Application.MouseGrabView != null && Application.MouseGrabView == this) {
  587. lastLocation = -1;
  588. Application.UngrabMouse ();
  589. return true;
  590. }
  591. if (showScrollIndicator && (mouseEvent.Flags == MouseFlags.WheeledDown || mouseEvent.Flags == MouseFlags.WheeledUp ||
  592. mouseEvent.Flags == MouseFlags.WheeledRight || mouseEvent.Flags == MouseFlags.WheeledLeft)) {
  593. return Host.MouseEvent (mouseEvent);
  594. }
  595. if (location == 0) {
  596. if (pos > 0) {
  597. Position = pos - 1;
  598. }
  599. } else if (location == barsize + 1) {
  600. if (CanScroll (1, out _, vertical)) {
  601. Position = pos + 1;
  602. }
  603. } else if (location > 0 && location < barsize + 1) {
  604. //var b1 = pos * (Size > 0 ? barsize / Size : 0);
  605. //var b2 = Size > 0
  606. // ? (KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size)
  607. // : 0;
  608. //if (KeepContentAlwaysInViewport && b1 == b2) {
  609. // b1 = Math.Max (b1 - 1, 0);
  610. //}
  611. if (lastLocation > -1 || (location >= posTopLeftTee && location <= posBottomRightTee
  612. && mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  613. if (lastLocation == -1) {
  614. lastLocation = location;
  615. posBarOffset = keepContentAlwaysInViewport ? Math.Max (location - posTopLeftTee, 1) : 0;
  616. return true;
  617. }
  618. if (location > lastLocation) {
  619. if (location - posBarOffset < barsize) {
  620. var np = ((location - posBarOffset) * Size / barsize) + (Size / barsize);
  621. if (CanScroll (np - pos, out int nv, vertical)) {
  622. Position = pos + nv;
  623. }
  624. } else if (CanScroll (Size - pos, out int nv, vertical)) {
  625. Position = Math.Min (pos + nv, Size);
  626. }
  627. } else if (location < lastLocation) {
  628. if (location - posBarOffset > 0) {
  629. var np = ((location - posBarOffset) * Size / barsize) - (Size / barsize);
  630. if (CanScroll (np - pos, out int nv, vertical)) {
  631. Position = pos + nv;
  632. }
  633. } else {
  634. Position = 0;
  635. }
  636. } else if (location - posBarOffset >= barsize && posBottomRightTee - posTopLeftTee >= 3 && CanScroll (Size - pos, out int nv, vertical)) {
  637. Position = Math.Min (pos + nv, Size);
  638. } else if (location - posBarOffset >= barsize - 1 && posBottomRightTee - posTopLeftTee <= 3 && CanScroll (Size - pos, out nv, vertical)) {
  639. Position = Math.Min (pos + nv, Size);
  640. } else if (location - posBarOffset <= 0 && posBottomRightTee - posTopLeftTee <= 3) {
  641. Position = 0;
  642. }
  643. } else if (location > posBottomRightTee) {
  644. if (CanScroll (barsize, out int nv, vertical)) {
  645. Position = pos + nv;
  646. }
  647. } else if (location < posTopLeftTee) {
  648. if (CanScroll (-barsize, out int nv, vertical)) {
  649. Position = pos + nv;
  650. }
  651. } else if (location == 1 && posTopLeftTee <= 3) {
  652. Position = 0;
  653. } else if (location == barsize) {
  654. if (CanScroll (Size - pos, out int nv, vertical)) {
  655. Position = Math.Min (pos + nv, Size);
  656. }
  657. }
  658. }
  659. return true;
  660. }
  661. internal bool CanScroll (int n, out int max, bool isVertical = false)
  662. {
  663. if (Host?.Bounds.IsEmpty != false) {
  664. max = 0;
  665. return false;
  666. }
  667. int s = GetBarsize (isVertical);
  668. var newSize = Math.Max (Math.Min (size - s, position + n), 0);
  669. max = size > s + newSize ? (newSize == 0 ? -position : n) : size - (s + position) - 1;
  670. if (size >= s + newSize && max != 0) {
  671. return true;
  672. }
  673. return false;
  674. }
  675. int GetBarsize (bool isVertical)
  676. {
  677. if (Host?.Bounds.IsEmpty != false) {
  678. return 0;
  679. }
  680. return isVertical ?
  681. (KeepContentAlwaysInViewport ? Host.Bounds.Height + (showBothScrollIndicator ? -2 : -1) : 0) :
  682. (KeepContentAlwaysInViewport ? Host.Bounds.Width + (showBothScrollIndicator ? -2 : -1) : 0);
  683. }
  684. ///<inheritdoc/>
  685. public override bool OnEnter (View view)
  686. {
  687. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  688. return base.OnEnter (view);
  689. }
  690. }
  691. }