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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
27 using System.ComponentModel;
28 using System.ComponentModel.Design;
30 using System.Runtime.InteropServices;
32 namespace System.Windows.Forms {
33 [Designer ("System.Windows.Forms.Design.ScrollableControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
34 [ClassInterface (ClassInterfaceType.AutoDispatch)]
36 public class ScrollableControl : Control {
37 #region Local Variables
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 internal ImplicitHScrollBar hscrollbar;
47 internal ImplicitVScrollBar vscrollbar;
48 internal Size canvas_size;
49 private Rectangle display_rectangle;
50 private Control old_parent;
51 private HScrollProperties horizontalScroll;
52 private VScrollProperties verticalScroll;
53 #endregion // Local Variables
55 [TypeConverter(typeof(ScrollableControl.DockPaddingEdgesConverter))]
56 #region Subclass DockPaddingEdges
57 public class DockPaddingEdges : ICloneable
59 private Control owner;
61 internal DockPaddingEdges (Control owner)
66 #region DockPaddingEdges Public Instance Properties
67 [RefreshProperties (RefreshProperties.All)]
69 get { return owner.Padding.All; }
70 set { owner.Padding = new Padding (value); }
73 [RefreshProperties (RefreshProperties.All)]
75 get { return owner.Padding.Bottom; }
76 set { owner.Padding = new Padding (Left, Top, Right, value); }
79 [RefreshProperties (RefreshProperties.All)]
81 get { return owner.Padding.Left; }
82 set { owner.Padding = new Padding (value, Top, Right, Bottom); }
85 [RefreshProperties (RefreshProperties.All)]
87 get { return owner.Padding.Right; }
88 set { owner.Padding = new Padding (Left, Top, value, Bottom); }
91 [RefreshProperties (RefreshProperties.All)]
93 get { return owner.Padding.Top; }
94 set { owner.Padding = new Padding (Left, value, Right, Bottom); }
96 #endregion // DockPaddingEdges Public Instance Properties
98 // Public Instance Methods
99 public override bool Equals (object other)
101 if (!(other is DockPaddingEdges)) {
105 if ((this.All == ((DockPaddingEdges)other).All) && (this.Left == ((DockPaddingEdges)other).Left) &&
106 (this.Right == ((DockPaddingEdges)other).Right) && (this.Top == ((DockPaddingEdges)other).Top) &&
107 (this.Bottom == ((DockPaddingEdges)other).Bottom)) {
114 public override int GetHashCode ()
116 return All * Top * Bottom * Right * Left;
119 public override string ToString ()
121 return "All = " + All.ToString () + " Top = " + Top.ToString () + " Left = " + Left.ToString () + " Bottom = " + Bottom.ToString () + " Right = " + Right.ToString ();
124 internal void Scale (float dx, float dy)
126 Left = (int)(Left * dx);
127 Right = (int)(Right * dx);
128 Top = (int)(Top * dy);
129 Bottom = (int)(Bottom * dy);
132 object ICloneable.Clone ()
134 return new DockPaddingEdges (owner);
137 #endregion // Subclass DockPaddingEdges
139 #region Subclass DockPaddingEdgesConverter
140 public class DockPaddingEdgesConverter : System.ComponentModel.TypeConverter {
141 // Public Constructors
142 public DockPaddingEdgesConverter() {
145 // Public Instance Methods
146 public override PropertyDescriptorCollection GetProperties(System.ComponentModel.ITypeDescriptorContext context, object value, Attribute[] attributes) {
147 return TypeDescriptor.GetProperties(typeof(DockPaddingEdges), attributes);
150 public override bool GetPropertiesSupported(System.ComponentModel.ITypeDescriptorContext context) {
154 #endregion // Subclass DockPaddingEdgesConverter
156 #region Public Constructors
157 public ScrollableControl() {
158 SetStyle(ControlStyles.ContainerControl, true);
159 SetStyle(ControlStyles.AllPaintingInWmPaint, false);
162 force_hscroll_visible = false;
163 force_vscroll_visible = false;
164 auto_scroll_margin = new Size(0, 0);
165 auto_scroll_min_size = new Size(0, 0);
166 scroll_position = new Point(0, 0);
167 SizeChanged +=new EventHandler(Recalculate);
168 VisibleChanged += new EventHandler (VisibleChangedHandler);
169 LocationChanged += new EventHandler (LocationChangedHandler);
170 ParentChanged += new EventHandler (ParentChangedHandler);
171 HandleCreated += new EventHandler (AddScrollbars);
175 horizontalScroll = new HScrollProperties (this);
176 verticalScroll = new VScrollProperties (this);
179 void VisibleChangedHandler (object sender, EventArgs e)
184 void LocationChangedHandler (object sender, EventArgs e)
186 UpdateSizeGripVisible ();
189 void ParentChangedHandler (object sender, EventArgs e)
192 if (old_parent == Parent)
195 if (old_parent != null) {
196 old_parent.SizeChanged -= new EventHandler (Parent_SizeChanged);
197 old_parent.PaddingChanged -= new EventHandler (Parent_PaddingChanged);
200 if (Parent != null) {
201 Parent.SizeChanged += new EventHandler (Parent_SizeChanged);
202 Parent.PaddingChanged += new EventHandler (Parent_PaddingChanged);
208 void Parent_PaddingChanged (object sender, EventArgs e)
210 UpdateSizeGripVisible ();
213 void Parent_SizeChanged (object sender, EventArgs e)
215 UpdateSizeGripVisible ();
217 #endregion // Public Constructors
219 #region Protected Static Fields
220 protected const int ScrollStateAutoScrolling = 1;
221 protected const int ScrollStateFullDrag = 16;
222 protected const int ScrollStateHScrollVisible = 2;
223 protected const int ScrollStateUserHasScrolled = 8;
224 protected const int ScrollStateVScrollVisible = 4;
225 #endregion // Protected Static Fields
227 #region Public Instance Properties
228 [DefaultValue(false)]
230 [MWFCategory("Layout")]
231 public virtual bool AutoScroll {
237 if (auto_scroll != value) {
239 PerformLayout (this, "AutoScroll");
245 [MWFCategory("Layout")]
246 public Size AutoScrollMargin {
248 return auto_scroll_margin;
252 if (value.Width < 0) {
253 throw new ArgumentException("Width is assigned less than 0", "value.Width");
256 if (value.Height < 0) {
257 throw new ArgumentException("Height is assigned less than 0", "value.Height");
260 auto_scroll_margin = value;
264 internal bool ShouldSerializeAutoScrollMargin ()
266 return this.AutoScrollMargin != new Size (0, 0);
270 [MWFCategory("Layout")]
271 public Size AutoScrollMinSize {
273 return auto_scroll_min_size;
277 if (value != auto_scroll_min_size) {
278 auto_scroll_min_size = value;
280 PerformLayout (this, "AutoScrollMinSize");
285 internal bool ShouldSerializeAutoScrollMinSize ()
287 return this.AutoScrollMinSize != new Size (0, 0);
291 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
292 public Point AutoScrollPosition {
294 return DisplayRectangle.Location;
298 if (value != AutoScrollPosition) {
304 if (hscrollbar.VisibleInternal) {
305 int max = hscrollbar.Maximum - hscrollbar.LargeChange + 1;
306 value.X = value.X < hscrollbar.Minimum ? hscrollbar.Minimum : value.X;
307 value.X = value.X > max ? max : value.X;
308 shift_x = value.X - scroll_position.X;
311 if (vscrollbar.VisibleInternal) {
312 int max = vscrollbar.Maximum - vscrollbar.LargeChange + 1;
313 value.Y = value.Y < vscrollbar.Minimum ? vscrollbar.Minimum : value.Y;
314 value.Y = value.Y > max ? max : value.Y;
315 shift_y = value.Y - scroll_position.Y;
318 ScrollWindow(shift_x, shift_y);
320 if (hscrollbar.VisibleInternal) {
321 if (scroll_position.X >= hscrollbar.Minimum && scroll_position.X <= hscrollbar.Maximum)
322 hscrollbar.Value = scroll_position.X;
325 if (vscrollbar.VisibleInternal) {
326 if (scroll_position.Y >= vscrollbar.Minimum && scroll_position.Y <= vscrollbar.Maximum)
327 vscrollbar.Value = scroll_position.Y;
334 public override Rectangle DisplayRectangle {
340 if (canvas_size.Width <= base.DisplayRectangle.Width) {
341 width = base.DisplayRectangle.Width;
342 if (vscrollbar.VisibleInternal) {
343 width -= vscrollbar.Width;
346 width = canvas_size.Width;
349 if (canvas_size.Height <= base.DisplayRectangle.Height) {
350 height = base.DisplayRectangle.Height;
351 if (hscrollbar.VisibleInternal) {
352 height -= hscrollbar.Height;
355 height = canvas_size.Height;
358 display_rectangle.X = -scroll_position.X;
359 display_rectangle.Y = -scroll_position.Y;
360 display_rectangle.Width = Math.Max(auto_scroll_min_size.Width, width);
361 display_rectangle.Height = Math.Max(auto_scroll_min_size.Height, height);
364 display_rectangle = base.DisplayRectangle;
367 if (dock_padding != null) {
368 display_rectangle.X += dock_padding.Left;
369 display_rectangle.Y += dock_padding.Top;
370 display_rectangle.Width -= dock_padding.Left + dock_padding.Right;
371 display_rectangle.Height -= dock_padding.Top + dock_padding.Bottom;
374 return display_rectangle;
378 [MWFCategory("Layout")]
380 [EditorBrowsable (EditorBrowsableState.Never)]
381 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
382 public DockPaddingEdges DockPadding {
384 if (dock_padding == null)
385 CreateDockPadding ();
392 [EditorBrowsable (EditorBrowsableState.Always)]
393 public HScrollProperties HorizontalScroll {
394 get { return horizontalScroll; }
398 [EditorBrowsable (EditorBrowsableState.Always)]
399 public VScrollProperties VerticalScroll {
400 get { return verticalScroll; }
402 #endregion // Public Instance Properties
404 #region Protected Instance Methods
405 protected override CreateParams CreateParams {
407 return base.CreateParams;
411 protected bool HScroll {
413 return hscrollbar.VisibleInternal;
417 if (!AutoScroll && hscrollbar.VisibleInternal != value) {
418 force_hscroll_visible = value;
424 protected bool VScroll {
426 return vscrollbar.VisibleInternal;
430 if (!AutoScroll && vscrollbar.VisibleInternal != value) {
431 force_vscroll_visible = value;
436 #endregion // Protected Instance Methods
438 #region Public Instance Methods
439 public void ScrollControlIntoView(Control activeControl) {
443 Rectangle within = new Rectangle ();
444 within.Size = ClientSize;
446 if (!AutoScroll || (!hscrollbar.VisibleInternal && !vscrollbar.VisibleInternal)) {
450 if (!Contains(activeControl)) {
454 if (vscrollbar.Visible) {
455 within.Width -= vscrollbar.Width;
457 if (hscrollbar.Visible) {
458 within.Height -= hscrollbar.Height;
461 // Don't scroll if already visible
462 if (within.Contains (activeControl.Location) && within.Contains (activeControl.Right, activeControl.Bottom)) {
466 // If the control is above the top or the left, move it down and right until it aligns
467 // with the top/left.
468 // If the control is below the bottom or to the right, move it up/left until it aligns
469 // with the bottom/right, but do never move it further than the top/left side.
470 int x_diff = 0, y_diff = 0;
471 if (activeControl.Top <= 0 || activeControl.Height >= within.Height) {
472 y_diff = -activeControl.Top;
473 } else if (activeControl.Bottom > within.Height) {
474 y_diff = within.Height - activeControl.Bottom;
476 if (activeControl.Left <= 0 || activeControl.Width >= within.Width) {
477 x_diff = -activeControl.Left;
478 } else if (activeControl.Right > within.Width) {
479 x_diff = within.Width - activeControl.Right;
481 corner_x = hscrollbar.Value - x_diff;
482 corner_y = vscrollbar.Value - y_diff;
484 if (hscrollbar.VisibleInternal) {
485 if (corner_x > hscrollbar.Maximum) {
486 corner_x = hscrollbar.Maximum;
487 } else if (corner_x < hscrollbar.Minimum) {
488 corner_x = hscrollbar.Minimum;
490 if (corner_x != hscrollbar.Value) {
491 hscrollbar.Value = corner_x;
495 if (vscrollbar.VisibleInternal) {
496 if (corner_y > vscrollbar.Maximum) {
497 corner_y = vscrollbar.Maximum;
498 } else if (corner_y < vscrollbar.Minimum) {
499 corner_y = vscrollbar.Minimum;
501 if (corner_y != vscrollbar.Value) {
502 vscrollbar.Value = corner_y;
507 public void SetAutoScrollMargin(int x, int y) {
516 auto_scroll_margin = new Size(x, y);
519 #endregion // Public Instance Methods
521 #region Protected Instance Methods
522 [EditorBrowsable(EditorBrowsableState.Advanced)]
523 protected virtual void AdjustFormScrollbars(bool displayScrollbars) {
527 [EditorBrowsable(EditorBrowsableState.Advanced)]
528 protected bool GetScrollState(int bit) {
533 [EditorBrowsable(EditorBrowsableState.Advanced)]
534 protected override void OnLayout(LayoutEventArgs levent) {
535 CalculateCanvasSize (true);
537 AdjustFormScrollbars(AutoScroll); // Dunno what the logic is. Passing AutoScroll seems to match MS behaviour
538 base.OnLayout(levent);
540 // The first time through, we just set the canvas to clientsize
541 // so we could re-layout everything according to the flow.
542 // This time we want to actually calculate the canvas.
543 if (this is FlowLayoutPanel) {
544 CalculateCanvasSize (false);
545 AdjustFormScrollbars (AutoScroll);
549 [EditorBrowsable(EditorBrowsableState.Advanced)]
550 protected override void OnMouseWheel(MouseEventArgs e) {
551 if (vscrollbar.VisibleInternal) {
553 if (vscrollbar.Minimum < (vscrollbar.Value - vscrollbar.LargeChange)) {
554 vscrollbar.Value -= vscrollbar.LargeChange;
556 vscrollbar.Value = vscrollbar.Minimum;
559 int maximum_scrollbar_value = vscrollbar.Maximum - vscrollbar.LargeChange + 1;
560 if (maximum_scrollbar_value > (vscrollbar.Value + vscrollbar.LargeChange)) {
561 vscrollbar.Value += vscrollbar.LargeChange;
563 vscrollbar.Value = maximum_scrollbar_value;
567 base.OnMouseWheel(e);
570 [EditorBrowsable(EditorBrowsableState.Advanced)]
571 protected override void OnVisibleChanged(EventArgs e) {
573 UpdateChildrenZOrder ();
574 PerformLayout(this, "Visible");
576 base.OnVisibleChanged(e);
579 [EditorBrowsable (EditorBrowsableState.Never)]
580 protected override void ScaleCore(float dx, float dy) {
581 if (dock_padding != null)
582 dock_padding.Scale(dx, dy);
584 base.ScaleCore(dx, dy);
587 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
589 base.ScaleControl (factor, specified);
592 protected virtual Point ScrollToControl (Control activeControl)
597 Rectangle within = new Rectangle ();
598 within.Size = ClientSize;
600 if (vscrollbar.Visible)
601 within.Width -= vscrollbar.Width;
603 if (hscrollbar.Visible)
604 within.Height -= hscrollbar.Height;
606 // If the control is above the top or the left, move it down and right until it aligns
607 // with the top/left.
608 // If the control is below the bottom or to the right, move it up/left until it aligns
609 // with the bottom/right, but do never move it further than the top/left side.
610 int x_diff = 0, y_diff = 0;
612 if (activeControl.Top <= 0 || activeControl.Height >= within.Height)
613 y_diff = -activeControl.Top;
614 else if (activeControl.Bottom > within.Height)
615 y_diff = within.Height - activeControl.Bottom;
617 if (activeControl.Left <= 0 || activeControl.Width >= within.Width)
618 x_diff = -activeControl.Left;
619 else if (activeControl.Right > within.Width)
620 x_diff = within.Width - activeControl.Right;
622 corner_x = AutoScrollPosition.X + x_diff;
623 corner_y = AutoScrollPosition.Y + y_diff;
625 return new Point (corner_x, corner_y);
628 protected void SetDisplayRectLocation(int x, int y) {
629 // This method is weird. MS documents that the scrollbars are not
630 // updated. We need to move stuff, but leave the scrollbars as is
640 ScrollWindow(scroll_position.X - x , scroll_position.Y - y);
643 protected void SetScrollState(int bit, bool value) {
644 //throw new NotImplementedException();
647 [EditorBrowsable(EditorBrowsableState.Advanced)]
648 protected override void WndProc(ref Message m) {
651 #endregion // Protected Instance Methods
653 #region Internal & Private Methods
654 internal override IntPtr AfterTopMostControl ()
656 // order of scrollbars:
659 // bottom = horizontal
660 if (hscrollbar != null && hscrollbar.Visible)
661 return hscrollbar.Handle;
662 // no need to check for sizegrip since it will only
663 // be visible if hbar is visible.
664 if (vscrollbar != null && vscrollbar.Visible)
665 return hscrollbar.Handle;
667 return base.AfterTopMostControl ();
670 internal virtual void CalculateCanvasSize (bool canOverride) {
678 num_of_children = Controls.Count;
681 extra_width = hscrollbar.Value;
682 extra_height = vscrollbar.Value;
683 if (dock_padding != null) {
684 extra_width += dock_padding.Right;
685 extra_height += dock_padding.Bottom;
688 for (int i = 0; i < num_of_children; i++) {
690 if (child.Dock == DockStyle.Right) {
691 extra_width += child.Width;
692 } else if (child.Dock == DockStyle.Bottom) {
693 extra_height += child.Height;
697 if (!auto_scroll_min_size.IsEmpty) {
698 width = auto_scroll_min_size.Width;
699 height = auto_scroll_min_size.Height;
702 for (int i = 0; i < num_of_children; i++) {
706 case DockStyle.Left: {
707 if ((child.Right + extra_width) > width) {
708 width = child.Right + extra_width;
713 case DockStyle.Top: {
714 if ((child.Bottom + extra_height) > height) {
715 height = child.Bottom + extra_height;
721 case DockStyle.Right:
722 case DockStyle.Bottom: {
729 anchor = child.Anchor;
731 if (((anchor & AnchorStyles.Left) != 0) && ((anchor & AnchorStyles.Right) == 0)) {
732 if ((child.Right + extra_width) > width) {
733 width = child.Right + extra_width;
737 if (((anchor & AnchorStyles.Top) != 0) || ((anchor & AnchorStyles.Bottom) == 0)) {
738 if ((child.Bottom + extra_height) > height) {
739 height = child.Bottom + extra_height;
747 canvas_size.Width = width;
748 canvas_size.Height = height;
751 // Normally DockPadding is created lazyly, as observed in the test cases, but some children
752 // may need to have it always.
753 internal void CreateDockPadding ()
755 if (dock_padding == null)
756 dock_padding = new DockPaddingEdges (this);
759 private void Recalculate (object sender, EventArgs e) {
763 private void Recalculate (bool doLayout) {
764 if (!IsHandleCreated) {
768 Size canvas = canvas_size;
769 Size client = ClientSize;
771 canvas.Width += auto_scroll_margin.Width;
772 canvas.Height += auto_scroll_margin.Height;
774 int right_edge = client.Width;
775 int bottom_edge = client.Height;
777 int prev_bottom_edge;
779 bool hscroll_visible;
780 bool vscroll_visible;
783 prev_right_edge = right_edge;
784 prev_bottom_edge = bottom_edge;
786 if ((force_hscroll_visible || (canvas.Width > right_edge && auto_scroll)) && client.Width > 0) {
787 hscroll_visible = true;
788 bottom_edge = client.Height - SystemInformation.HorizontalScrollBarHeight;
790 hscroll_visible = false;
791 bottom_edge = client.Height;
794 if ((force_vscroll_visible || (canvas.Height > bottom_edge && auto_scroll)) && client.Height > 0) {
795 vscroll_visible = true;
796 right_edge = client.Width - SystemInformation.VerticalScrollBarWidth;
798 vscroll_visible = false;
799 right_edge = client.Width;
802 } while (right_edge != prev_right_edge || bottom_edge != prev_bottom_edge);
804 if (right_edge < 0) right_edge = 0;
805 if (bottom_edge < 0) bottom_edge = 0;
807 Rectangle hscroll_bounds;
808 Rectangle vscroll_bounds;
810 hscroll_bounds = new Rectangle (0, client.Height - SystemInformation.HorizontalScrollBarHeight,
811 ClientRectangle.Width, SystemInformation.HorizontalScrollBarHeight);
812 vscroll_bounds = new Rectangle (client.Width - SystemInformation.VerticalScrollBarWidth, 0,
813 SystemInformation.VerticalScrollBarWidth, ClientRectangle.Height);
815 /* the ScrollWindow calls here are needed
816 * because (this explanation sucks):
818 * when we transition from having a scrollbar to
819 * not having one, we won't receive a scrollbar
820 * moved (value changed) event, so we need to
821 * manually scroll the canvas.
823 * if you can fix this without requiring the
824 * ScrollWindow calls, pdb and toshok will each
828 if (!vscrollbar.Visible) {
829 vscrollbar.Value = 0;
831 if (!hscrollbar.Visible) {
832 hscrollbar.Value = 0;
835 /* Manually setting the size of the thumb should be done before
836 * the other assignments */
837 if (hscroll_visible) {
838 hscrollbar.manual_thumb_size = right_edge;
839 hscrollbar.LargeChange = right_edge;
840 hscrollbar.SmallChange = 5;
841 hscrollbar.Maximum = canvas.Width - 1;
843 if (hscrollbar != null && hscrollbar.VisibleInternal) {
844 ScrollWindow (- scroll_position.X, 0);
846 scroll_position.X = 0;
849 if (vscroll_visible) {
850 vscrollbar.manual_thumb_size = bottom_edge;
851 vscrollbar.LargeChange = bottom_edge;
852 vscrollbar.SmallChange = 5;
853 vscrollbar.Maximum = canvas.Height - 1;
855 if (vscrollbar != null && vscrollbar.VisibleInternal) {
856 ScrollWindow (0, - scroll_position.Y);
858 scroll_position.Y = 0;
861 if (hscroll_visible && vscroll_visible) {
862 hscroll_bounds.Width -= SystemInformation.VerticalScrollBarWidth;
863 vscroll_bounds.Height -= SystemInformation.HorizontalScrollBarHeight;
865 sizegrip.Bounds = new Rectangle (hscroll_bounds.Right,
866 vscroll_bounds.Bottom,
867 SystemInformation.VerticalScrollBarWidth,
868 SystemInformation.HorizontalScrollBarHeight);
873 hscrollbar.SetBoundsInternal (hscroll_bounds.X, hscroll_bounds.Y, hscroll_bounds.Width, hscroll_bounds.Height, BoundsSpecified.None);
874 hscrollbar.Visible = hscroll_visible;
875 if (hscrollbar.Visible)
876 XplatUI.SetZOrder (hscrollbar.Handle, IntPtr.Zero, true, false);
878 vscrollbar.SetBoundsInternal (vscroll_bounds.X, vscroll_bounds.Y, vscroll_bounds.Width, vscroll_bounds.Height, BoundsSpecified.None);
879 vscrollbar.Visible = vscroll_visible;
880 if (vscrollbar.Visible)
881 XplatUI.SetZOrder (vscrollbar.Handle, IntPtr.Zero, true, false);
883 UpdateSizeGripVisible ();
885 ResumeLayout (doLayout);
887 // We should now scroll the active control into view,
888 // the funny part is that ScrollableControl does not have
889 // the concept of active control.
890 ContainerControl container = this as ContainerControl;
891 if (container != null && container.ActiveControl != null) {
892 ScrollControlIntoView (container.ActiveControl);
896 internal void UpdateSizeGripVisible ()
898 if (!IsHandleCreated) {
902 sizegrip.CapturedControl = Parent;
903 // This is really wierd, the size grip is only showing up
904 // if the bottom right corner of the scrollable control is within
905 // two pixels from the bottom right corner of its parent.
906 bool show_sizegrip = hscrollbar.VisibleInternal && vscrollbar.VisibleInternal;
907 bool enable_sizegrip = false;
908 if (show_sizegrip && Parent != null) {
909 Point diff = new Point (Parent.ClientRectangle.Bottom - Bottom, Parent.ClientRectangle.Right - Right);
910 enable_sizegrip = diff.X <= 2 && diff.X >= 0 && diff.Y <= 2 && diff.Y >= 0;
912 sizegrip.Visible = show_sizegrip;
913 sizegrip.Enabled = enable_sizegrip || sizegrip.Capture;
914 if (sizegrip.Visible)
915 XplatUI.SetZOrder (sizegrip.Handle, vscrollbar.Handle, false, false);
918 private void HandleScrollBar(object sender, EventArgs e) {
919 if (sender == vscrollbar) {
920 if (!vscrollbar.Visible)
922 ScrollWindow(0, vscrollbar.Value- scroll_position.Y);
924 if (!hscrollbar.Visible)
926 ScrollWindow(hscrollbar.Value - scroll_position.X, 0);
930 private void HandleScrollEvent (object sender, ScrollEventArgs args)
935 private void AddScrollbars (object o, EventArgs e)
937 Controls.AddRangeImplicit (new Control[] {hscrollbar, vscrollbar, sizegrip});
938 HandleCreated -= new EventHandler (AddScrollbars);
941 private void CreateScrollbars ()
943 hscrollbar = new ImplicitHScrollBar ();
944 hscrollbar.Visible = false;
945 hscrollbar.ValueChanged += new EventHandler (HandleScrollBar);
946 hscrollbar.Height = SystemInformation.HorizontalScrollBarHeight;
947 hscrollbar.use_manual_thumb_size = true;
948 hscrollbar.Scroll += new ScrollEventHandler (HandleScrollEvent);
950 vscrollbar = new ImplicitVScrollBar ();
951 vscrollbar.Visible = false;
952 vscrollbar.ValueChanged += new EventHandler (HandleScrollBar);
953 vscrollbar.Width = SystemInformation.VerticalScrollBarWidth;
954 vscrollbar.use_manual_thumb_size = true;
955 vscrollbar.Scroll += new ScrollEventHandler (HandleScrollEvent);
957 sizegrip = new SizeGrip (this);
958 sizegrip.Visible = false;
961 private void ScrollWindow(int XOffset, int YOffset) {
964 if (XOffset == 0 && YOffset == 0) {
970 num_of_children = Controls.Count;
972 for (int i = 0; i < num_of_children; i++) {
973 Controls[i].Location = new Point (Controls[i].Left - XOffset, Controls[i].Top - YOffset);
974 //Controls[i].Left -= XOffset;
975 //Controls[i].Top -= YOffset;
976 // Is this faster? Controls[i].Location -= new Size(XOffset, YOffset);
979 scroll_position.X += XOffset;
980 scroll_position.Y += YOffset;
982 XplatUI.ScrollWindow (Handle, ClientRectangle, -XOffset, -YOffset, false);
985 #endregion // Internal & Private Methods
987 static object OnScrollEvent = new object ();
989 protected virtual void OnScroll (ScrollEventArgs se)
991 ScrollEventHandler eh = (ScrollEventHandler) (Events [OnScrollEvent]);
996 protected override void OnPaddingChanged (EventArgs e)
998 base.OnPaddingChanged (e);
1001 protected override void OnPaintBackground (PaintEventArgs e)
1003 base.OnPaintBackground (e);
1006 [EditorBrowsable (EditorBrowsableState.Advanced)]
1007 protected override void OnRightToLeftChanged (EventArgs e)
1009 base.OnRightToLeftChanged (e);
1012 public event ScrollEventHandler Scroll {
1013 add { Events.AddHandler (OnScrollEvent, value); }
1014 remove { Events.RemoveHandler (OnScrollEvent, value); }