ScrollableControl.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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, (string)null)]
  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. auto_scroll = false;
  172. auto_hscroll = false;
  173. auto_vscroll = false;
  174. hscroll_visible = false;
  175. vscroll_visible = false;
  176. force_hscroll_visible = false;
  177. force_vscroll_visible = false;
  178. auto_scroll_margin = new Size(0, 0);
  179. auto_scroll_min_size = new Size(0, 0);
  180. scroll_position = new Point(0, 0);
  181. dock_padding = new DockPaddingEdges();
  182. SizeChanged +=new EventHandler(Recalculate);
  183. VisibleChanged += new EventHandler(Recalculate);
  184. }
  185. #endregion // Public Constructors
  186. #region Protected Static Fields
  187. protected const int ScrollStateAutoScrolling = 1;
  188. protected const int ScrollStateFullDrag = 16;
  189. protected const int ScrollStateHScrollVisible = 2;
  190. protected const int ScrollStateUserHasScrolled = 8;
  191. protected const int ScrollStateVScrollVisible = 4;
  192. #endregion // Protected Static Fields
  193. #region Public Instance Properties
  194. [DefaultValue(false)]
  195. [Localizable(true)]
  196. public virtual bool AutoScroll {
  197. get {
  198. return auto_scroll;
  199. }
  200. set {
  201. if (auto_scroll == value) {
  202. return;
  203. }
  204. auto_scroll = value;
  205. if (!auto_scroll) {
  206. Controls.Remove(hscrollbar);
  207. hscrollbar.Dispose();
  208. hscrollbar = null;
  209. Controls.Remove(vscrollbar);
  210. vscrollbar.Dispose();
  211. vscrollbar = null;
  212. Controls.Remove(sizegrip);
  213. sizegrip.Dispose();
  214. sizegrip = null;
  215. } else {
  216. hscrollbar = new HScrollBar();
  217. hscrollbar.Visible = false;
  218. hscrollbar.ValueChanged += new EventHandler(HandleScrollBar);
  219. hscrollbar.Height = SystemInformation.HorizontalScrollBarHeight;
  220. this.Controls.Add(hscrollbar);
  221. vscrollbar = new VScrollBar();
  222. vscrollbar.Visible = false;
  223. vscrollbar.ValueChanged += new EventHandler(HandleScrollBar);
  224. vscrollbar.Width = SystemInformation.VerticalScrollBarWidth;
  225. this.Controls.Add(vscrollbar);
  226. sizegrip = new SizeGrip();
  227. sizegrip.Visible = false;
  228. this.Controls.Add(sizegrip);
  229. }
  230. }
  231. }
  232. [Localizable(true)]
  233. public Size AutoScrollMargin {
  234. get {
  235. return auto_scroll_margin;
  236. }
  237. set {
  238. if (value.Width < 0) {
  239. throw new ArgumentException("Width is assigned less than 0", "value.Width");
  240. }
  241. if (value.Height < 0) {
  242. throw new ArgumentException("Height is assigned less than 0", "value.Height");
  243. }
  244. auto_scroll_margin = value;
  245. }
  246. }
  247. [Localizable(true)]
  248. public Size AutoScrollMinSize {
  249. get {
  250. return auto_scroll_min_size;
  251. }
  252. set {
  253. auto_scroll_min_size = value;
  254. }
  255. }
  256. [Browsable(false)]
  257. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  258. public Point AutoScrollPosition {
  259. get {
  260. return new Point(-scroll_position.X, -scroll_position.Y);
  261. }
  262. set {
  263. if ((value.X != scroll_position.X) || (value.Y != scroll_position.Y)) {
  264. int shift_x;
  265. int shift_y;
  266. shift_x = 0;
  267. shift_y = 0;
  268. if (hscroll_visible) {
  269. shift_x = value.X - scroll_position.X;
  270. }
  271. if (vscroll_visible) {
  272. shift_y = value.Y - scroll_position.Y;
  273. }
  274. ScrollWindow(shift_x, shift_y);
  275. if (hscroll_visible) {
  276. hscrollbar.Value = scroll_position.X;
  277. }
  278. if (vscroll_visible) {
  279. vscrollbar.Value = scroll_position.Y;
  280. }
  281. }
  282. }
  283. }
  284. public override Rectangle DisplayRectangle {
  285. get {
  286. Rectangle rect;
  287. rect = base.DisplayRectangle;
  288. if (vscroll_visible) {
  289. rect.Width -= vscrollbar.Width;
  290. if (rect.Width < 0) {
  291. rect.Width = 0;
  292. }
  293. }
  294. if (hscroll_visible) {
  295. rect.Height -= hscrollbar.Height;
  296. if (rect.Height < 0) {
  297. rect.Height = 0;
  298. }
  299. }
  300. return rect;
  301. }
  302. }
  303. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  304. [Localizable(true)]
  305. public DockPaddingEdges DockPadding {
  306. get {
  307. return dock_padding;
  308. }
  309. // DockPadding is documented as 'get' only ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsScrollableControlClassAutoScrollTopic.asp )
  310. // but Microsoft's examples on that page show 'set' usage
  311. // set {
  312. // dock_padding = value;
  313. // }
  314. }
  315. #endregion // Public Instance Properties
  316. #region Protected Instance Methods
  317. protected override CreateParams CreateParams {
  318. get {
  319. CreateParams ret;
  320. ret = base.CreateParams;
  321. ret.Style |= (int)(WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_VISIBLE);
  322. return ret;
  323. }
  324. }
  325. protected bool HScroll {
  326. get {
  327. return hscroll_visible;
  328. }
  329. set {
  330. if (hscroll_visible != value) {
  331. force_hscroll_visible = value;
  332. Recalculate(this, EventArgs.Empty);
  333. }
  334. }
  335. }
  336. protected bool VScroll {
  337. get {
  338. return vscroll_visible;
  339. }
  340. set {
  341. if (vscroll_visible != value) {
  342. force_vscroll_visible = value;
  343. Recalculate(this, EventArgs.Empty);
  344. }
  345. }
  346. }
  347. #endregion // Protected Instance Methods
  348. #region Public Instance Methods
  349. public void ScrollControlIntoView(Control activeControl) {
  350. }
  351. public void SetAutoScrollMargin(int x, int y) {
  352. if (x < 0) {
  353. x = 0;
  354. }
  355. if (y < 0) {
  356. y = 0;
  357. }
  358. auto_scroll_margin = new Size(x, y);
  359. Recalculate(this, EventArgs.Empty);
  360. }
  361. #endregion // Public Instance Methods
  362. #region Protected Instance Methods
  363. [EditorBrowsable(EditorBrowsableState.Advanced)]
  364. protected virtual void AdjustFormScrollbars(bool displayScrollbars) {
  365. // Internal MS
  366. }
  367. [EditorBrowsable(EditorBrowsableState.Advanced)]
  368. protected bool GetScrollState(int bit) {
  369. return false;
  370. // Internal MS
  371. }
  372. [EditorBrowsable(EditorBrowsableState.Advanced)]
  373. protected override void OnLayout(LayoutEventArgs levent) {
  374. base.OnLayout(levent);
  375. }
  376. [EditorBrowsable(EditorBrowsableState.Advanced)]
  377. protected override void OnMouseWheel(MouseEventArgs e) {
  378. if (vscroll_visible) {
  379. if (e.Delta > 0) {
  380. if (vscrollbar.Minimum < (vscrollbar.Value - vscrollbar.LargeChange)) {
  381. vscrollbar.Value -= vscrollbar.LargeChange;
  382. } else {
  383. vscrollbar.Value = vscrollbar.Minimum;
  384. }
  385. } else {
  386. if (vscrollbar.Maximum > (vscrollbar.Value + vscrollbar.LargeChange)) {
  387. vscrollbar.Value += vscrollbar.LargeChange;
  388. } else {
  389. vscrollbar.Value = vscrollbar.Maximum;
  390. }
  391. }
  392. }
  393. base.OnMouseWheel(e);
  394. }
  395. [EditorBrowsable(EditorBrowsableState.Advanced)]
  396. protected override void OnVisibleChanged(EventArgs e) {
  397. base.OnVisibleChanged(e);
  398. }
  399. protected override void ScaleCore(float dx, float dy) {
  400. base.ScaleCore(dx, dy);
  401. }
  402. protected void SetDisplayRectLocation(int x, int y) {
  403. throw new NotImplementedException();
  404. }
  405. protected void SetScrollState(int bit, bool value) {
  406. throw new NotImplementedException();
  407. }
  408. [EditorBrowsable(EditorBrowsableState.Advanced)]
  409. protected override void WndProc(ref Message m) {
  410. base.WndProc(ref m);
  411. }
  412. #endregion // Protected Instance Methods
  413. #region Internal & Private Methods
  414. private Size Canvas {
  415. get {
  416. int num_of_children;
  417. int width;
  418. int height;
  419. num_of_children = child_controls.Count;
  420. width = 0;
  421. height = 0;
  422. for (int i = 0; i < num_of_children; i++) {
  423. if ((child_controls[i].Visible == false) || (child_controls[i] == hscrollbar) || (child_controls[i] == vscrollbar) || (child_controls[i] == sizegrip)) {
  424. continue;
  425. }
  426. if (child_controls[i].Right > width) {
  427. width = child_controls[i].Right;
  428. }
  429. if (child_controls[i].Bottom > height) {
  430. height = child_controls[i].Bottom;
  431. }
  432. }
  433. return new Size(width, height);
  434. }
  435. }
  436. private void Recalculate(object sender, EventArgs e) {
  437. Size canvas;
  438. Size client;
  439. // FIXME - this whole function begs for optimizations, all the math
  440. // shouldn't have to be done over and over
  441. // Check if we need scrollbars
  442. if (!this.auto_scroll && !force_hscroll_visible && !force_vscroll_visible) {
  443. return;
  444. }
  445. canvas = Canvas;
  446. client = ClientRectangle.Size;
  447. canvas.Width += auto_scroll_margin.Width + SystemInformation.VerticalScrollBarWidth;
  448. canvas.Height += auto_scroll_margin.Height + SystemInformation.HorizontalScrollBarHeight;
  449. // || (scroll_position.X == 0 && scroll_position.Y == 0)
  450. if ((canvas.Width >= client.Width) || (auto_scroll_min_size.Width > client.Width) || force_hscroll_visible) {
  451. // Need horz
  452. hscrollbar.Left = 0;
  453. hscrollbar.Top = client.Height - SystemInformation.HorizontalScrollBarHeight;
  454. hscrollbar.Maximum = Math.Max(0, canvas.Width - client.Width + SystemInformation.VerticalScrollBarWidth);
  455. hscroll_visible = true;
  456. } else {
  457. hscroll_visible = false;
  458. scroll_position.X = 0;
  459. }
  460. if ((canvas.Height >= client.Height) || (auto_scroll_min_size.Height > client.Height) || force_vscroll_visible) {
  461. // Need vert
  462. vscrollbar.Left = client.Width - SystemInformation.VerticalScrollBarWidth;
  463. vscrollbar.Top = 0;
  464. // FIXME - Working around some scrollbar bugs here; shouldn't have to add the height again (see canvas+= above)
  465. vscrollbar.Maximum = Math.Max(0, canvas.Height - client.Height + SystemInformation.HorizontalScrollBarHeight);
  466. vscroll_visible = true;
  467. } else {
  468. vscroll_visible = false;
  469. scroll_position.Y = 0;
  470. }
  471. if (hscroll_visible && vscroll_visible) {
  472. hscrollbar.Width = ClientRectangle.Width - SystemInformation.VerticalScrollBarWidth;
  473. vscrollbar.Height = ClientRectangle.Height - SystemInformation.HorizontalScrollBarHeight;
  474. sizegrip.Left = hscrollbar.Right;
  475. sizegrip.Top = vscrollbar.Bottom;
  476. sizegrip.Width = SystemInformation.VerticalScrollBarWidth;
  477. sizegrip.Height = SystemInformation.HorizontalScrollBarHeight;
  478. hscrollbar.Visible = true;
  479. vscrollbar.Visible = true;
  480. sizegrip.Visible = true;
  481. } else {
  482. sizegrip.Visible = false;
  483. if (hscroll_visible) {
  484. hscrollbar.Width = ClientRectangle.Width;
  485. hscrollbar.Visible = true;
  486. } else {
  487. hscrollbar.Visible = false;
  488. }
  489. if (vscroll_visible) {
  490. vscrollbar.Height = ClientRectangle.Height;
  491. vscrollbar.Visible = true;
  492. } else {
  493. vscrollbar.Visible = false;
  494. }
  495. }
  496. }
  497. private void HandleScrollBar(object sender, EventArgs e) {
  498. if (sender == vscrollbar) {
  499. ScrollWindow(0, vscrollbar.Value- scroll_position.Y);
  500. } else {
  501. ScrollWindow(hscrollbar.Value - scroll_position.X, 0);
  502. }
  503. }
  504. private void ScrollWindow(int XOffset, int YOffset) {
  505. int num_of_children;
  506. SuspendLayout();
  507. num_of_children = child_controls.Count;
  508. for (int i = 0; i < num_of_children; i++) {
  509. if (child_controls[i] == hscrollbar || child_controls[i] == vscrollbar || child_controls[i] == sizegrip) {
  510. continue;
  511. }
  512. child_controls[i].Left -= XOffset;
  513. child_controls[i].Top -= YOffset;
  514. // Is this faster? child_controls[i].Location -= new Size(XOffset, YOffset);
  515. }
  516. scroll_position.X += XOffset;
  517. scroll_position.Y += YOffset;
  518. // Should we call XplatUI.ScrollWindow???
  519. Invalidate();
  520. ResumeLayout();
  521. }
  522. #endregion // Internal & Private Methods
  523. }
  524. }