ScrollBarView.cs 28 KB

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