ScrollBarView.cs 25 KB

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