FRScrollablePanel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using FastReport.Utils;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace FastReport.Controls
  7. {
  8. internal class FRScrollablePanel : UserControl
  9. {
  10. #region Fields
  11. private VScrollBar _VScrollBar = null;
  12. private HScrollBar _HScrollBar = null;
  13. private Control _Thumb = null;
  14. private bool _VScrollBarVisible;
  15. private bool _HScrollBarVisible;
  16. private bool _ThumbVisible;
  17. private Point _AutoScrollPosition = Point.Empty;
  18. private bool _AutoScroll = false;
  19. private Size _AutoScrollMinSize = Size.Empty;
  20. #endregion
  21. #region Properties
  22. /// <summary>
  23. /// Gets the reference to internal vertical scroll-bar control if one is created or null if no scrollbar is visible.
  24. /// </summary>
  25. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  26. public VScrollBar VScrollBar
  27. {
  28. get { return _VScrollBar; }
  29. }
  30. /// <summary>
  31. /// Gets the reference to internal horizontal scroll-bar control if one is created or null if no scrollbar is visible.
  32. /// </summary>
  33. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  34. public HScrollBar HScrollBar
  35. {
  36. get { return _HScrollBar; }
  37. }
  38. /// <summary>
  39. /// Gets or sets a value indicating whether the control enables the user to scroll to items placed outside of its visible boundaries.
  40. /// </summary>
  41. [Browsable(true), DefaultValue(false)]
  42. public new bool AutoScroll
  43. {
  44. get { return _AutoScroll; }
  45. set
  46. {
  47. if (_AutoScroll != value)
  48. {
  49. _AutoScroll = value;
  50. UpdateScrollBars();
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// Gets or sets the minimum size of the auto-scroll. Returns a Size that represents the minimum height and width of the scrolling area in pixels.
  56. /// This property is managed internally by control and should not be modified.
  57. /// </summary>
  58. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  59. public new Size AutoScrollMinSize
  60. {
  61. get { return _AutoScrollMinSize; }
  62. set
  63. {
  64. _AutoScrollMinSize = value;
  65. UpdateScrollBars();
  66. }
  67. }
  68. public event EventHandler AutoScrollPositionChanged;
  69. /// <summary>
  70. /// Gets or sets the location of the auto-scroll position.
  71. /// </summary>
  72. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  73. public new Point AutoScrollPosition
  74. {
  75. get { return _AutoScrollPosition; }
  76. set
  77. {
  78. if (!HScrollBarVisible)
  79. value.X = 0;
  80. else
  81. value.X = -Math.Max(Math.Min(-value.X, _HScrollBar.Maximum - _HScrollBar.LargeChange), _HScrollBar.Minimum);
  82. if (!VScrollBarVisible)
  83. value.Y = 0;
  84. else
  85. value.Y = -Math.Max(Math.Min(-value.Y, _VScrollBar.Maximum - _VScrollBar.LargeChange), _VScrollBar.Minimum);
  86. if (_AutoScrollPosition != value)
  87. {
  88. _AutoScrollPosition = value;
  89. if (_AutoScroll)
  90. {
  91. if (VScrollBarVisible && _VScrollBar.Value != -_AutoScrollPosition.Y)
  92. {
  93. _VScrollBar.Value = -_AutoScrollPosition.Y;
  94. }
  95. if (HScrollBarVisible && _HScrollBar.Value != -_AutoScrollPosition.X)
  96. {
  97. _HScrollBar.Value = -_AutoScrollPosition.X;
  98. }
  99. Invalidate();
  100. }
  101. OnAutoScrollPositionChanged();
  102. }
  103. }
  104. }
  105. private bool HScrollBarVisible
  106. {
  107. get { return _HScrollBarVisible; }
  108. set
  109. {
  110. _HScrollBarVisible = value;
  111. _HScrollBar.Visible = value;
  112. }
  113. }
  114. private bool VScrollBarVisible
  115. {
  116. get { return _VScrollBarVisible; }
  117. set
  118. {
  119. _VScrollBarVisible = value;
  120. _VScrollBar.Visible = value;
  121. }
  122. }
  123. private bool ThumbVisible
  124. {
  125. get { return _ThumbVisible; }
  126. set
  127. {
  128. _ThumbVisible = value;
  129. _Thumb.Visible = value;
  130. }
  131. }
  132. #endregion
  133. #region Private Methods
  134. private void UpdateScrollBars()
  135. {
  136. if (!_AutoScroll)
  137. {
  138. VScrollBarVisible = false;
  139. HScrollBarVisible = false;
  140. ThumbVisible = false;
  141. return;
  142. }
  143. Rectangle innerBounds = this.ClientRectangle;
  144. // Check do we need vertical scrollbar
  145. Size scrollSize = _AutoScrollMinSize;
  146. if (scrollSize.Height > innerBounds.Height)
  147. {
  148. VScrollBarVisible = true;
  149. if (_VScrollBar.Minimum != 0)
  150. _VScrollBar.Minimum = 0;
  151. if (_VScrollBar.LargeChange != innerBounds.Height && innerBounds.Height > 0)
  152. _VScrollBar.LargeChange = innerBounds.Height;
  153. _VScrollBar.SmallChange = 100;
  154. if (_VScrollBar.Maximum != _AutoScrollMinSize.Height)
  155. _VScrollBar.Maximum = _AutoScrollMinSize.Height;
  156. if (_VScrollBar.Value != -_AutoScrollPosition.Y)
  157. _VScrollBar.Value = (Math.Min(_VScrollBar.Maximum, Math.Abs(_AutoScrollPosition.Y)));
  158. }
  159. else
  160. {
  161. VScrollBarVisible = false;
  162. }
  163. // Check horizontal scrollbar
  164. if (scrollSize.Width > innerBounds.Width)
  165. {
  166. HScrollBarVisible = true;
  167. if (_HScrollBar.Minimum != 0)
  168. _HScrollBar.Minimum = 0;
  169. if (_HScrollBar.LargeChange != innerBounds.Width && innerBounds.Width > 0)
  170. _HScrollBar.LargeChange = innerBounds.Width;
  171. if (_HScrollBar.Maximum != _AutoScrollMinSize.Width)
  172. _HScrollBar.Maximum = _AutoScrollMinSize.Width;
  173. if (_HScrollBar.Value != -_AutoScrollPosition.X)
  174. _HScrollBar.Value = (Math.Min(_HScrollBar.Maximum, Math.Abs(_AutoScrollPosition.X)));
  175. _HScrollBar.SmallChange = 100;
  176. }
  177. else
  178. {
  179. HScrollBarVisible = false;
  180. }
  181. RepositionScrollBars();
  182. }
  183. private void VScrollBarScroll(object sender, ScrollEventArgs e)
  184. {
  185. if (_AutoScrollPosition.Y != -e.NewValue)
  186. {
  187. _AutoScrollPosition.Y = -e.NewValue;
  188. OnAutoScrollPositionChanged();
  189. this.OnScroll(e);
  190. this.Invalidate();
  191. }
  192. }
  193. private void HScrollBarScroll(object sender, ScrollEventArgs e)
  194. {
  195. if (_AutoScrollPosition.X != -e.NewValue)
  196. {
  197. _AutoScrollPosition.X = -e.NewValue;
  198. OnAutoScrollPositionChanged();
  199. this.OnScroll(e);
  200. this.Invalidate();
  201. }
  202. }
  203. private void RepositionScrollBars()
  204. {
  205. Rectangle innerBounds = this.ClientRectangle;
  206. _VScrollBar.Width = this.LogicalToDevice(SystemInformation.VerticalScrollBarWidth);
  207. _HScrollBar.Height = this.LogicalToDevice(SystemInformation.HorizontalScrollBarHeight);
  208. if (HScrollBarVisible)
  209. {
  210. int width = innerBounds.Width;
  211. if (VScrollBarVisible)
  212. width -= _VScrollBar.Width - 1;
  213. _HScrollBar.Bounds = new Rectangle(innerBounds.X, innerBounds.Bottom - _HScrollBar.Height + 1, width, _HScrollBar.Height);
  214. }
  215. if (VScrollBarVisible)
  216. {
  217. int height = innerBounds.Height;
  218. if (HScrollBarVisible)
  219. height -= _HScrollBar.Height - 1;
  220. _VScrollBar.Bounds = new Rectangle(innerBounds.Right - _VScrollBar.Width + 1, innerBounds.Y, _VScrollBar.Width, height);
  221. }
  222. if (VScrollBarVisible && HScrollBarVisible)
  223. {
  224. ThumbVisible = true;
  225. _Thumb.Bounds = new Rectangle(_HScrollBar.Bounds.Right, _VScrollBar.Bounds.Bottom, _VScrollBar.Width, _HScrollBar.Height);
  226. }
  227. else
  228. {
  229. ThumbVisible = false;
  230. }
  231. }
  232. #endregion
  233. #region Protected Methods
  234. protected virtual void OnAutoScrollPositionChanged()
  235. {
  236. if (AutoScrollPositionChanged != null)
  237. AutoScrollPositionChanged(this, EventArgs.Empty);
  238. }
  239. protected override void Dispose(bool disposing)
  240. {
  241. if (disposing)
  242. {
  243. if (_VScrollBar != null)
  244. _VScrollBar.Dispose();
  245. _VScrollBar = null;
  246. if (_HScrollBar != null)
  247. _HScrollBar.Dispose();
  248. _HScrollBar = null;
  249. if (_Thumb != null)
  250. _Thumb.Dispose();
  251. _Thumb = null;
  252. }
  253. base.Dispose(disposing);
  254. }
  255. protected override void OnMouseWheel(MouseEventArgs e)
  256. {
  257. if (VScrollBarVisible)
  258. {
  259. int newValue = _VScrollBar.Value + _VScrollBar.SmallChange * (e.Delta < 0 ? 1 : -1);
  260. if (newValue < _VScrollBar.Minimum)
  261. newValue = _VScrollBar.Minimum;
  262. if (newValue > _VScrollBar.Maximum - _VScrollBar.LargeChange + 1)
  263. newValue = _VScrollBar.Maximum - _VScrollBar.LargeChange + 1;
  264. ScrollEventType scType = e.Delta < 0 ? ScrollEventType.SmallIncrement : ScrollEventType.SmallDecrement;
  265. VScrollBarScroll(this, new ScrollEventArgs(scType, _VScrollBar.Value, newValue));
  266. _VScrollBar.Value = newValue;
  267. }
  268. base.OnMouseWheel(e);
  269. }
  270. protected override void OnResize(EventArgs e)
  271. {
  272. base.OnResize(e);
  273. UpdateScrollBars();
  274. AutoScrollPosition = AutoScrollPosition;
  275. }
  276. #endregion
  277. public FRScrollablePanel()
  278. {
  279. _VScrollBar = new VScrollBar();
  280. _VScrollBar.Width = this.LogicalToDevice(SystemInformation.VerticalScrollBarWidth);
  281. _VScrollBar.Cursor = Cursors.Default;
  282. this.Controls.Add(_VScrollBar);
  283. _VScrollBar.Scroll += new ScrollEventHandler(VScrollBarScroll);
  284. // CreateControl is needed to avoid error in mdi child mode
  285. _VScrollBar.CreateControl();
  286. _HScrollBar = new HScrollBar();
  287. _HScrollBar.Height = this.LogicalToDevice(SystemInformation.HorizontalScrollBarHeight);
  288. _HScrollBar.Cursor = Cursors.Default;
  289. this.Controls.Add(_HScrollBar);
  290. _HScrollBar.Scroll += new ScrollEventHandler(HScrollBarScroll);
  291. _HScrollBar.CreateControl();
  292. _Thumb = new Control();
  293. _Thumb.BackColor = SystemColors.Control;
  294. _Thumb.TabStop = false;
  295. this.Controls.Add(_Thumb);
  296. _Thumb.CreateControl();
  297. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
  298. }
  299. }
  300. }