ScrollableControl.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. // NOT COMPLETE
  26. using System;
  27. using System.ComponentModel;
  28. using System.ComponentModel.Design;
  29. using System.Drawing;
  30. namespace System.Windows.Forms {
  31. [Designer ("System.Windows.Forms.Design.ScrollableControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  32. public class ScrollableControl : Control {
  33. #region Local Variables
  34. private bool auto_vscroll;
  35. private bool auto_hscroll;
  36. private bool hscroll_visible;
  37. private bool vscroll_visible;
  38. private bool force_hscroll_visible;
  39. private bool force_vscroll_visible;
  40. private bool auto_scroll;
  41. private Size auto_scroll_margin;
  42. private Size auto_scroll_min_size;
  43. private Point scroll_position;
  44. private DockPaddingEdges dock_padding;
  45. private SizeGrip sizegrip;
  46. private HScrollBar hscrollbar;
  47. private VScrollBar vscrollbar;
  48. #endregion // Local Variables
  49. [MonoTODO("Need to use the edge values when performing the layout")]
  50. [TypeConverter(typeof(ScrollableControl.DockPaddingEdgesConverter))]
  51. #region Subclass DockPaddingEdges
  52. public class DockPaddingEdges : ICloneable {
  53. #region DockPaddingEdges Local Variables
  54. private int all;
  55. private int left;
  56. private int right;
  57. private int top;
  58. private int bottom;
  59. #endregion // DockPaddingEdges Local Variables
  60. #region DockPaddingEdges Constructor
  61. internal DockPaddingEdges() {
  62. all = 0;
  63. left = 0;
  64. right = 0;
  65. top = 0;
  66. bottom = 0;
  67. }
  68. #endregion // DockPaddingEdges Constructor
  69. #region DockPaddingEdges Public Instance Properties
  70. [RefreshProperties(RefreshProperties.All)]
  71. public int All {
  72. get {
  73. return all;
  74. }
  75. set {
  76. all = value;
  77. left = value;
  78. right = value;
  79. top = value;
  80. bottom = value;
  81. }
  82. }
  83. [RefreshProperties(RefreshProperties.All)]
  84. public int Bottom {
  85. get {
  86. return bottom;
  87. }
  88. set {
  89. bottom = value;
  90. all = 0;
  91. }
  92. }
  93. [RefreshProperties(RefreshProperties.All)]
  94. public int Left {
  95. get {
  96. return left;
  97. }
  98. set {
  99. left=value;
  100. all = 0;
  101. }
  102. }
  103. [RefreshProperties(RefreshProperties.All)]
  104. public int Right {
  105. get {
  106. return right;
  107. }
  108. set {
  109. right=value;
  110. all = 0;
  111. }
  112. }
  113. [RefreshProperties(RefreshProperties.All)]
  114. public int Top {
  115. get {
  116. return top;
  117. }
  118. set {
  119. top=value;
  120. all = 0;
  121. }
  122. }
  123. #endregion // DockPaddingEdges Public Instance Properties
  124. // Public Instance Methods
  125. public override bool Equals(object other) {
  126. if (! (other is DockPaddingEdges)) {
  127. return false;
  128. }
  129. if ( (this.all == ((DockPaddingEdges)other).all) && (this.left == ((DockPaddingEdges)other).left) &&
  130. (this.right == ((DockPaddingEdges)other).right) && (this.top == ((DockPaddingEdges)other).top) &&
  131. (this.bottom == ((DockPaddingEdges)other).bottom)) {
  132. return true;
  133. }
  134. return false;
  135. }
  136. public override int GetHashCode() {
  137. return all*top*bottom*right*left;
  138. }
  139. public override string ToString() {
  140. return "All = "+all.ToString()+" Top = "+top.ToString()+" Left = "+left.ToString()+" Bottom = "+bottom.ToString()+" Right = "+right.ToString();
  141. }
  142. object ICloneable.Clone() {
  143. DockPaddingEdges padding_edge;
  144. padding_edge=new DockPaddingEdges();
  145. padding_edge.all=all;
  146. padding_edge.left=left;
  147. padding_edge.right=right;
  148. padding_edge.top=top;
  149. padding_edge.bottom=bottom;
  150. return padding_edge;
  151. }
  152. }
  153. #endregion // Subclass DockPaddingEdges
  154. #region Subclass DockPaddingEdgesConverter
  155. public class DockPaddingEdgesConverter : System.ComponentModel.TypeConverter {
  156. // Public Constructors
  157. public DockPaddingEdgesConverter() {
  158. }
  159. // Public Instance Methods
  160. public override PropertyDescriptorCollection GetProperties(System.ComponentModel.ITypeDescriptorContext context, object value, Attribute[] attributes) {
  161. return TypeDescriptor.GetProperties(typeof(DockPaddingEdges), attributes);
  162. }
  163. public override bool GetPropertiesSupported(System.ComponentModel.ITypeDescriptorContext context) {
  164. return true;
  165. }
  166. }
  167. #endregion // Subclass DockPaddingEdgesConverter
  168. #region Public Constructors
  169. public ScrollableControl() {
  170. SetStyle(ControlStyles.ContainerControl, true);
  171. SetStyle(ControlStyles.AllPaintingInWmPaint, false);
  172. auto_scroll = false;
  173. auto_hscroll = false;
  174. auto_vscroll = false;
  175. hscroll_visible = false;
  176. vscroll_visible = false;
  177. force_hscroll_visible = false;
  178. force_vscroll_visible = false;
  179. auto_scroll_margin = new Size(0, 0);
  180. auto_scroll_min_size = new Size(0, 0);
  181. scroll_position = new Point(0, 0);
  182. dock_padding = new DockPaddingEdges();
  183. SizeChanged +=new EventHandler(Recalculate);
  184. VisibleChanged += new EventHandler(Recalculate);
  185. }
  186. #endregion // Public Constructors
  187. #region Protected Static Fields
  188. protected const int ScrollStateAutoScrolling = 1;
  189. protected const int ScrollStateFullDrag = 16;
  190. protected const int ScrollStateHScrollVisible = 2;
  191. protected const int ScrollStateUserHasScrolled = 8;
  192. protected const int ScrollStateVScrollVisible = 4;
  193. #endregion // Protected Static Fields
  194. #region Public Instance Properties
  195. [DefaultValue(false)]
  196. [Localizable(true)]
  197. public virtual bool AutoScroll {
  198. get {
  199. return auto_scroll;
  200. }
  201. set {
  202. if (auto_scroll == value) {
  203. return;
  204. }
  205. auto_scroll = value;
  206. if (!auto_scroll) {
  207. SuspendLayout ();
  208. Controls.RemoveImplicit (hscrollbar);
  209. hscrollbar.Dispose();
  210. hscrollbar = null;
  211. hscroll_visible = false;
  212. Controls.RemoveImplicit (vscrollbar);
  213. vscrollbar.Dispose();
  214. vscrollbar = null;
  215. vscroll_visible = false;
  216. Controls.RemoveImplicit (sizegrip);
  217. sizegrip.Dispose();
  218. sizegrip = null;
  219. ResumeLayout ();
  220. } else {
  221. SuspendLayout ();
  222. hscrollbar = new HScrollBar();
  223. hscrollbar.Visible = false;
  224. hscrollbar.ValueChanged += new EventHandler(HandleScrollBar);
  225. hscrollbar.Height = SystemInformation.HorizontalScrollBarHeight;
  226. this.Controls.AddImplicit (hscrollbar);
  227. vscrollbar = new VScrollBar();
  228. vscrollbar.Visible = false;
  229. vscrollbar.ValueChanged += new EventHandler(HandleScrollBar);
  230. vscrollbar.Width = SystemInformation.VerticalScrollBarWidth;
  231. this.Controls.AddImplicit (vscrollbar);
  232. sizegrip = new SizeGrip();
  233. sizegrip.Visible = false;
  234. this.Controls.AddImplicit (sizegrip);
  235. ResumeLayout ();
  236. }
  237. }
  238. }
  239. [Localizable(true)]
  240. public Size AutoScrollMargin {
  241. get {
  242. return auto_scroll_margin;
  243. }
  244. set {
  245. if (value.Width < 0) {
  246. throw new ArgumentException("Width is assigned less than 0", "value.Width");
  247. }
  248. if (value.Height < 0) {
  249. throw new ArgumentException("Height is assigned less than 0", "value.Height");
  250. }
  251. auto_scroll_margin = value;
  252. }
  253. }
  254. [Localizable(true)]
  255. public Size AutoScrollMinSize {
  256. get {
  257. return auto_scroll_min_size;
  258. }
  259. set {
  260. auto_scroll_min_size = value;
  261. }
  262. }
  263. [Browsable(false)]
  264. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  265. public Point AutoScrollPosition {
  266. get {
  267. return new Point(-scroll_position.X, -scroll_position.Y);
  268. }
  269. set {
  270. if ((value.X != scroll_position.X) || (value.Y != scroll_position.Y)) {
  271. int shift_x;
  272. int shift_y;
  273. shift_x = 0;
  274. shift_y = 0;
  275. if (hscroll_visible) {
  276. shift_x = value.X - scroll_position.X;
  277. }
  278. if (vscroll_visible) {
  279. shift_y = value.Y - scroll_position.Y;
  280. }
  281. ScrollWindow(shift_x, shift_y);
  282. if (hscroll_visible) {
  283. hscrollbar.Value = scroll_position.X;
  284. }
  285. if (vscroll_visible) {
  286. vscrollbar.Value = scroll_position.Y;
  287. }
  288. }
  289. }
  290. }
  291. public override Rectangle DisplayRectangle {
  292. get {
  293. Rectangle rect;
  294. rect = base.DisplayRectangle;
  295. if (vscroll_visible) {
  296. rect.Width -= vscrollbar.Width;
  297. if (rect.Width < 0) {
  298. rect.Width = 0;
  299. }
  300. }
  301. if (hscroll_visible) {
  302. rect.Height -= hscrollbar.Height;
  303. if (rect.Height < 0) {
  304. rect.Height = 0;
  305. }
  306. }
  307. return rect;
  308. //return new Rectangle(-scroll_position.X, -scroll_position.Y, auto_scroll_min_size.Width, auto_scroll_min_size.Height);
  309. }
  310. }
  311. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  312. [Localizable(true)]
  313. public DockPaddingEdges DockPadding {
  314. get {
  315. return dock_padding;
  316. }
  317. // DockPadding is documented as 'get' only ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsScrollableControlClassAutoScrollTopic.asp )
  318. // but Microsoft's examples on that page show 'set' usage
  319. // set {
  320. // dock_padding = value;
  321. // }
  322. }
  323. #endregion // Public Instance Properties
  324. #region Protected Instance Methods
  325. protected override CreateParams CreateParams {
  326. get {
  327. CreateParams ret;
  328. ret = base.CreateParams;
  329. ret.Style |= (int)(WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_VISIBLE);
  330. return ret;
  331. }
  332. }
  333. protected bool HScroll {
  334. get {
  335. return hscroll_visible;
  336. }
  337. set {
  338. if (hscroll_visible != value) {
  339. force_hscroll_visible = value;
  340. Recalculate(this, EventArgs.Empty);
  341. }
  342. }
  343. }
  344. protected bool VScroll {
  345. get {
  346. return vscroll_visible;
  347. }
  348. set {
  349. if (vscroll_visible != value) {
  350. force_vscroll_visible = value;
  351. Recalculate(this, EventArgs.Empty);
  352. }
  353. }
  354. }
  355. #endregion // Protected Instance Methods
  356. #region Public Instance Methods
  357. public void ScrollControlIntoView(Control activeControl) {
  358. }
  359. public void SetAutoScrollMargin(int x, int y) {
  360. if (x < 0) {
  361. x = 0;
  362. }
  363. if (y < 0) {
  364. y = 0;
  365. }
  366. auto_scroll_margin = new Size(x, y);
  367. Recalculate(this, EventArgs.Empty);
  368. }
  369. #endregion // Public Instance Methods
  370. #region Protected Instance Methods
  371. [EditorBrowsable(EditorBrowsableState.Advanced)]
  372. protected virtual void AdjustFormScrollbars(bool displayScrollbars) {
  373. // Internal MS
  374. }
  375. [EditorBrowsable(EditorBrowsableState.Advanced)]
  376. protected bool GetScrollState(int bit) {
  377. return false;
  378. // Internal MS
  379. }
  380. [EditorBrowsable(EditorBrowsableState.Advanced)]
  381. protected override void OnLayout(LayoutEventArgs levent) {
  382. base.OnLayout(levent);
  383. }
  384. [EditorBrowsable(EditorBrowsableState.Advanced)]
  385. protected override void OnMouseWheel(MouseEventArgs e) {
  386. if (vscroll_visible) {
  387. if (e.Delta > 0) {
  388. if (vscrollbar.Minimum < (vscrollbar.Value - vscrollbar.LargeChange)) {
  389. vscrollbar.Value -= vscrollbar.LargeChange;
  390. } else {
  391. vscrollbar.Value = vscrollbar.Minimum;
  392. }
  393. } else {
  394. if (vscrollbar.Maximum > (vscrollbar.Value + vscrollbar.LargeChange)) {
  395. vscrollbar.Value += vscrollbar.LargeChange;
  396. } else {
  397. vscrollbar.Value = vscrollbar.Maximum;
  398. }
  399. }
  400. }
  401. base.OnMouseWheel(e);
  402. }
  403. [EditorBrowsable(EditorBrowsableState.Advanced)]
  404. protected override void OnVisibleChanged(EventArgs e) {
  405. base.OnVisibleChanged(e);
  406. }
  407. protected override void ScaleCore(float dx, float dy) {
  408. base.ScaleCore(dx, dy);
  409. }
  410. protected void SetDisplayRectLocation(int x, int y) {
  411. throw new NotImplementedException();
  412. }
  413. protected void SetScrollState(int bit, bool value) {
  414. throw new NotImplementedException();
  415. }
  416. [EditorBrowsable(EditorBrowsableState.Advanced)]
  417. protected override void WndProc(ref Message m) {
  418. base.WndProc(ref m);
  419. }
  420. #endregion // Protected Instance Methods
  421. #region Internal & Private Methods
  422. private Size Canvas {
  423. get {
  424. int num_of_children;
  425. int width;
  426. int height;
  427. num_of_children = child_controls.Count;
  428. width = 0;
  429. height = 0;
  430. for (int i = 0; i < num_of_children; i++) {
  431. if ((child_controls[i].Visible == false) || (child_controls[i] == hscrollbar) || (child_controls[i] == vscrollbar) || (child_controls[i] == sizegrip)) {
  432. continue;
  433. }
  434. if (child_controls[i].Right > width) {
  435. width = child_controls[i].Right;
  436. }
  437. if (child_controls[i].Bottom > height) {
  438. height = child_controls[i].Bottom;
  439. }
  440. }
  441. return new Size(width, height);
  442. }
  443. }
  444. private void Recalculate(object sender, EventArgs e) {
  445. Size canvas;
  446. Size client;
  447. // FIXME - this whole function begs for optimizations, all the math
  448. // shouldn't have to be done over and over
  449. // Check if we need scrollbars
  450. if (!this.auto_scroll && !force_hscroll_visible && !force_vscroll_visible) {
  451. return;
  452. }
  453. canvas = Canvas;
  454. client = ClientRectangle.Size;
  455. canvas.Width += auto_scroll_margin.Width + SystemInformation.VerticalScrollBarWidth;
  456. canvas.Height += auto_scroll_margin.Height + SystemInformation.HorizontalScrollBarHeight;
  457. // || (scroll_position.X == 0 && scroll_position.Y == 0)
  458. if ((canvas.Width >= client.Width) || (auto_scroll_min_size.Width > client.Width) || force_hscroll_visible) {
  459. // Need horz
  460. hscrollbar.Left = 0;
  461. hscrollbar.Top = client.Height - SystemInformation.HorizontalScrollBarHeight;
  462. hscrollbar.Maximum = Math.Max(0, canvas.Width - client.Width + SystemInformation.VerticalScrollBarWidth);
  463. hscroll_visible = true;
  464. } else {
  465. hscroll_visible = false;
  466. scroll_position.X = 0;
  467. }
  468. if ((canvas.Height >= client.Height) || (auto_scroll_min_size.Height > client.Height) || force_vscroll_visible) {
  469. // Need vert
  470. vscrollbar.Left = client.Width - SystemInformation.VerticalScrollBarWidth;
  471. vscrollbar.Top = 0;
  472. // FIXME - Working around some scrollbar bugs here; shouldn't have to add the height again (see canvas+= above)
  473. vscrollbar.Maximum = Math.Max(0, canvas.Height - client.Height + SystemInformation.HorizontalScrollBarHeight);
  474. vscroll_visible = true;
  475. } else {
  476. vscroll_visible = false;
  477. scroll_position.Y = 0;
  478. }
  479. if (hscroll_visible && vscroll_visible) {
  480. hscrollbar.Width = ClientRectangle.Width - SystemInformation.VerticalScrollBarWidth;
  481. vscrollbar.Height = ClientRectangle.Height - SystemInformation.HorizontalScrollBarHeight;
  482. sizegrip.Left = hscrollbar.Right;
  483. sizegrip.Top = vscrollbar.Bottom;
  484. sizegrip.Width = SystemInformation.VerticalScrollBarWidth;
  485. sizegrip.Height = SystemInformation.HorizontalScrollBarHeight;
  486. hscrollbar.Visible = true;
  487. vscrollbar.Visible = true;
  488. sizegrip.Visible = true;
  489. } else {
  490. sizegrip.Visible = false;
  491. if (hscroll_visible) {
  492. hscrollbar.Width = ClientRectangle.Width;
  493. hscrollbar.Visible = true;
  494. } else {
  495. hscrollbar.Visible = false;
  496. }
  497. if (vscroll_visible) {
  498. vscrollbar.Height = ClientRectangle.Height;
  499. vscrollbar.Visible = true;
  500. } else {
  501. vscrollbar.Visible = false;
  502. }
  503. }
  504. }
  505. private void HandleScrollBar(object sender, EventArgs e) {
  506. if (sender == vscrollbar) {
  507. ScrollWindow(0, vscrollbar.Value- scroll_position.Y);
  508. } else {
  509. ScrollWindow(hscrollbar.Value - scroll_position.X, 0);
  510. }
  511. }
  512. private void ScrollWindow(int XOffset, int YOffset) {
  513. int num_of_children;
  514. SuspendLayout();
  515. num_of_children = child_controls.Count;
  516. for (int i = 0; i < num_of_children; i++) {
  517. if (child_controls[i] == hscrollbar || child_controls[i] == vscrollbar || child_controls[i] == sizegrip) {
  518. continue;
  519. }
  520. child_controls[i].Left -= XOffset;
  521. child_controls[i].Top -= YOffset;
  522. // Is this faster? child_controls[i].Location -= new Size(XOffset, YOffset);
  523. }
  524. scroll_position.X += XOffset;
  525. scroll_position.Y += YOffset;
  526. // Should we call XplatUI.ScrollWindow???
  527. Invalidate();
  528. ResumeLayout();
  529. }
  530. #endregion // Internal & Private Methods
  531. }
  532. }