1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2004 Novell, Inc.
//
// Authors:
// Peter Bartok pbartok@novell.com
//
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Runtime.InteropServices;
namespace System.Windows.Forms {
[Designer ("System.Windows.Forms.Design.ScrollableControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
[ClassInterface (ClassInterfaceType.AutoDispatch)]
[ComVisible (true)]
public class ScrollableControl : Control {
#region Local Variables
private bool force_hscroll_visible;
private bool force_vscroll_visible;
private bool auto_scroll;
private Size auto_scroll_margin;
private Size auto_scroll_min_size;
private Point scroll_position;
private DockPaddingEdges dock_padding;
private SizeGrip sizegrip;
internal ImplicitHScrollBar hscrollbar;
internal ImplicitVScrollBar vscrollbar;
internal Size canvas_size;
private Rectangle display_rectangle;
private Control old_parent;
private HScrollProperties horizontalScroll;
private VScrollProperties verticalScroll;
private bool autosized_child;
#endregion // Local Variables
[TypeConverter(typeof(ScrollableControl.DockPaddingEdgesConverter))]
#region Subclass DockPaddingEdges
public class DockPaddingEdges : ICloneable
{
private Control owner;
internal DockPaddingEdges (Control owner)
{
this.owner = owner;
}
#region DockPaddingEdges Public Instance Properties
[RefreshProperties (RefreshProperties.All)]
public int All {
get { return owner.Padding.All; }
set { owner.Padding = new Padding (value); }
}
[RefreshProperties (RefreshProperties.All)]
public int Bottom {
get { return owner.Padding.Bottom; }
set { owner.Padding = new Padding (Left, Top, Right, value); }
}
[RefreshProperties (RefreshProperties.All)]
public int Left {
get { return owner.Padding.Left; }
set { owner.Padding = new Padding (value, Top, Right, Bottom); }
}
[RefreshProperties (RefreshProperties.All)]
public int Right {
get { return owner.Padding.Right; }
set { owner.Padding = new Padding (Left, Top, value, Bottom); }
}
[RefreshProperties (RefreshProperties.All)]
public int Top {
get { return owner.Padding.Top; }
set { owner.Padding = new Padding (Left, value, Right, Bottom); }
}
#endregion // DockPaddingEdges Public Instance Properties
// Public Instance Methods
public override bool Equals (object other)
{
if (!(other is DockPaddingEdges)) {
return false;
}
if ((this.All == ((DockPaddingEdges)other).All) && (this.Left == ((DockPaddingEdges)other).Left) &&
(this.Right == ((DockPaddingEdges)other).Right) && (this.Top == ((DockPaddingEdges)other).Top) &&
(this.Bottom == ((DockPaddingEdges)other).Bottom)) {
return true;
}
return false;
}
public override int GetHashCode ()
{
return All * Top * Bottom * Right * Left;
}
public override string ToString ()
{
return "All = " + All.ToString () + " Top = " + Top.ToString () + " Left = " + Left.ToString () + " Bottom = " + Bottom.ToString () + " Right = " + Right.ToString ();
}
internal void Scale (float dx, float dy)
{
Left = (int)(Left * dx);
Right = (int)(Right * dx);
Top = (int)(Top * dy);
Bottom = (int)(Bottom * dy);
}
object ICloneable.Clone ()
{
return new DockPaddingEdges (owner);
}
}
#endregion // Subclass DockPaddingEdges
#region Subclass DockPaddingEdgesConverter
public class DockPaddingEdgesConverter : System.ComponentModel.TypeConverter {
// Public Constructors
public DockPaddingEdgesConverter() {
}
// Public Instance Methods
public override PropertyDescriptorCollection GetProperties(System.ComponentModel.ITypeDescriptorContext context, object value, Attribute[] attributes) {
return TypeDescriptor.GetProperties(typeof(DockPaddingEdges), attributes);
}
public override bool GetPropertiesSupported(System.ComponentModel.ITypeDescriptorContext context) {
return true;
}
}
#endregion // Subclass DockPaddingEdgesConverter
#region Public Constructors
public ScrollableControl() {
SetStyle(ControlStyles.ContainerControl, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, false);
auto_scroll = false;
force_hscroll_visible = false;
force_vscroll_visible = false;
auto_scroll_margin = new Size(0, 0);
auto_scroll_min_size = new Size(0, 0);
scroll_position = new Point(0, 0);
SizeChanged +=new EventHandler(Recalculate);
VisibleChanged += new EventHandler (VisibleChangedHandler);
LocationChanged += new EventHandler (LocationChangedHandler);
ParentChanged += new EventHandler (ParentChangedHandler);
HandleCreated += new EventHandler (AddScrollbars);
CreateScrollbars ();
horizontalScroll = new HScrollProperties (this);
verticalScroll = new VScrollProperties (this);
}
void VisibleChangedHandler (object sender, EventArgs e)
{
Recalculate (false);
}
void LocationChangedHandler (object sender, EventArgs e)
{
UpdateSizeGripVisible ();
}
void ParentChangedHandler (object sender, EventArgs e)
{
if (old_parent == Parent)
return;
if (old_parent != null) {
old_parent.SizeChanged -= new EventHandler (Parent_SizeChanged);
old_parent.PaddingChanged -= new EventHandler (Parent_PaddingChanged);
}
if (Parent != null) {
Parent.SizeChanged += new EventHandler (Parent_SizeChanged);
Parent.PaddingChanged += new EventHandler (Parent_PaddingChanged);
}
old_parent = Parent;
}
void Parent_PaddingChanged (object sender, EventArgs e)
{
UpdateSizeGripVisible ();
}
void Parent_SizeChanged (object sender, EventArgs e)
{
UpdateSizeGripVisible ();
}
#endregion // Public Constructors
#region Protected Static Fields
protected const int ScrollStateAutoScrolling = 1;
protected const int ScrollStateFullDrag = 16;
protected const int ScrollStateHScrollVisible = 2;
protected const int ScrollStateUserHasScrolled = 8;
protected const int ScrollStateVScrollVisible = 4;
#endregion // Protected Static Fields
#region Public Instance Properties
[DefaultValue(false)]
[Localizable(true)]
[MWFCategory("Layout")]
public virtual bool AutoScroll {
get {
return auto_scroll;
}
set {
if (auto_scroll != value) {
auto_scroll = value;
PerformLayout (this, "AutoScroll");
}
}
}
[Localizable(true)]
[MWFCategory("Layout")]
public Size AutoScrollMargin {
get {
return auto_scroll_margin;
}
set {
if (value.Width < 0) {
throw new ArgumentException("Width is assigned less than 0", "value.Width");
}
if (value.Height < 0) {
throw new ArgumentException("Height is assigned less than 0", "value.Height");
}
auto_scroll_margin = value;
}
}
internal bool ShouldSerializeAutoScrollMargin ()
{
return this.AutoScrollMargin != new Size (0, 0);
}
[Localizable(true)]
[MWFCategory("Layout")]
public Size AutoScrollMinSize {
get {
return auto_scroll_min_size;
}
set {
if (value != auto_scroll_min_size) {
auto_scroll_min_size = value;
AutoScroll = true;
PerformLayout (this, "AutoScrollMinSize");
}
}
}
internal bool ShouldSerializeAutoScrollMinSize ()
{
return this.AutoScrollMinSize != new Size (0, 0);
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Point AutoScrollPosition {
get {
return DisplayRectangle.Location;
}
set {
if (value != AutoScrollPosition) {
int shift_x;
int shift_y;
shift_x = 0;
shift_y = 0;
if (hscrollbar.VisibleInternal) {
int max = hscrollbar.Maximum - hscrollbar.LargeChange + 1;
value.X = value.X < hscrollbar.Minimum ? hscrollbar.Minimum : value.X;
value.X = value.X > max ? max : value.X;
shift_x = value.X - scroll_position.X;
}
if (vscrollbar.VisibleInternal) {
int max = vscrollbar.Maximum - vscrollbar.LargeChange + 1;
value.Y = value.Y < vscrollbar.Minimum ? vscrollbar.Minimum : value.Y;
value.Y = value.Y > max ? max : value.Y;
shift_y = value.Y - scroll_position.Y;
}
ScrollWindow(shift_x, shift_y);
if (hscrollbar.VisibleInternal) {
if (scroll_position.X >= hscrollbar.Minimum && scroll_position.X <= hscrollbar.Maximum)
hscrollbar.Value = scroll_position.X;
}
if (vscrollbar.VisibleInternal) {
if (scroll_position.Y >= vscrollbar.Minimum && scroll_position.Y <= vscrollbar.Maximum)
vscrollbar.Value = scroll_position.Y;
}
}
}
}
public override Rectangle DisplayRectangle {
get {
if (auto_scroll) {
int width;
int height;
if (canvas_size.Width <= base.DisplayRectangle.Width) {
width = base.DisplayRectangle.Width;
if (vscrollbar.VisibleInternal) {
width -= vscrollbar.Width;
}
} else {
width = canvas_size.Width;
}
if (canvas_size.Height <= base.DisplayRectangle.Height) {
height = base.DisplayRectangle.Height;
if (hscrollbar.VisibleInternal) {
height -= hscrollbar.Height;
}
} else {
height = canvas_size.Height;
}
display_rectangle.X = -scroll_position.X;
display_rectangle.Y = -scroll_position.Y;
display_rectangle.Width = Math.Max(auto_scroll_min_size.Width, width);
display_rectangle.Height = Math.Max(auto_scroll_min_size.Height, height);
}
else {
display_rectangle = base.DisplayRectangle;
}
// DockPadding is the same as Padding (according to documentation) but is
// calculated lazily, so we use Padding here instead.
if (Padding != Padding.Empty) {
display_rectangle.X += Padding.Left;
display_rectangle.Y += Padding.Top;
display_rectangle.Width -= Padding.Horizontal;
display_rectangle.Height -= Padding.Vertical;
}
return display_rectangle;
}
}
[MWFCategory("Layout")]
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public DockPaddingEdges DockPadding {
get {
if (dock_padding == null)
CreateDockPadding ();
return dock_padding;
}
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Always)]
public HScrollProperties HorizontalScroll {
get { return horizontalScroll; }
}
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Always)]
public VScrollProperties VerticalScroll {
get { return verticalScroll; }
}
#endregion // Public Instance Properties
#region Protected Instance Methods
protected override CreateParams CreateParams {
get {
return base.CreateParams;
}
}
protected bool HScroll {
get {
return hscrollbar.VisibleInternal;
}
set {
if (!AutoScroll && hscrollbar.VisibleInternal != value) {
force_hscroll_visible = value;
Recalculate (false);
}
}
}
protected bool VScroll {
get {
return vscrollbar.VisibleInternal;
}
set {
if (!AutoScroll && vscrollbar.VisibleInternal != value) {
force_vscroll_visible = value;
Recalculate (false);
}
}
}
#endregion // Protected Instance Methods
#region Public Instance Methods
public void ScrollControlIntoView(Control activeControl) {
int corner_x;
int corner_y;
Rectangle within = new Rectangle ();
within.Size = ClientSize;
if (!AutoScroll || (!hscrollbar.VisibleInternal && !vscrollbar.VisibleInternal)) {
return;
}
if (!Contains(activeControl)) {
return;
}
if (vscrollbar.Visible) {
within.Width -= vscrollbar.Width;
}
if (hscrollbar.Visible) {
within.Height -= hscrollbar.Height;
}
// Don't scroll if already visible
if (within.Contains (activeControl.Location) && within.Contains (activeControl.Right, activeControl.Bottom)) {
return;
}
// If the control is above the top or the left, move it down and right until it aligns
// with the top/left.
// If the control is below the bottom or to the right, move it up/left until it aligns
// with the bottom/right, but do never move it further than the top/left side.
int x_diff = 0, y_diff = 0;
if (activeControl.Top <= 0 || activeControl.Height >= within.Height) {
y_diff = -activeControl.Top;
} else if (activeControl.Bottom > within.Height) {
y_diff = within.Height - activeControl.Bottom;
}
if (activeControl.Left <= 0 || activeControl.Width >= within.Width) {
x_diff = -activeControl.Left;
} else if (activeControl.Right > within.Width) {
x_diff = within.Width - activeControl.Right;
}
corner_x = hscrollbar.Value - x_diff;
corner_y = vscrollbar.Value - y_diff;
if (hscrollbar.VisibleInternal) {
if (corner_x > hscrollbar.Maximum) {
corner_x = hscrollbar.Maximum;
} else if (corner_x < hscrollbar.Minimum) {
corner_x = hscrollbar.Minimum;
}
if (corner_x != hscrollbar.Value) {
hscrollbar.Value = corner_x;
}
}
if (vscrollbar.VisibleInternal) {
if (corner_y > vscrollbar.Maximum) {
corner_y = vscrollbar.Maximum;
} else if (corner_y < vscrollbar.Minimum) {
corner_y = vscrollbar.Minimum;
}
if (corner_y != vscrollbar.Value) {
vscrollbar.Value = corner_y;
}
}
}
public void SetAutoScrollMargin(int x, int y) {
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
auto_scroll_margin = new Size(x, y);
Recalculate (false);
}
#endregion // Public Instance Methods
#region Protected Instance Methods
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void AdjustFormScrollbars(bool displayScrollbars) {
Recalculate (false);
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected bool GetScrollState(int bit) {
// Internal MS
return false;
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void OnLayout(LayoutEventArgs levent) {
CalculateCanvasSize (true);
AdjustFormScrollbars(AutoScroll); // Dunno what the logic is. Passing AutoScroll seems to match MS behaviour
base.OnLayout(levent);
// The first time through, we just set the canvas to clientsize
// so we could re-layout everything according to the flow.
// This time we want to actually calculate the canvas.
// If a child is autosized, we need to rethink scrollbars as well. (Xamarin bug 18874)
if (this is FlowLayoutPanel || autosized_child) {
CalculateCanvasSize (false);
AdjustFormScrollbars (AutoScroll);
}
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void OnMouseWheel(MouseEventArgs e) {
if (vscrollbar.VisibleInternal) {
if (e.Delta > 0) {
if (vscrollbar.Minimum < (vscrollbar.Value - vscrollbar.LargeChange)) {
vscrollbar.Value -= vscrollbar.LargeChange;
} else {
vscrollbar.Value = vscrollbar.Minimum;
}
} else {
int maximum_scrollbar_value = vscrollbar.Maximum - vscrollbar.LargeChange + 1;
if (maximum_scrollbar_value > (vscrollbar.Value + vscrollbar.LargeChange)) {
vscrollbar.Value += vscrollbar.LargeChange;
} else {
vscrollbar.Value = maximum_scrollbar_value;
}
}
}
base.OnMouseWheel(e);
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void OnVisibleChanged(EventArgs e) {
if (Visible) {
UpdateChildrenZOrder ();
PerformLayout(this, "Visible");
}
base.OnVisibleChanged(e);
}
[EditorBrowsable (EditorBrowsableState.Never)]
protected override void ScaleCore(float dx, float dy) {
if (dock_padding != null)
dock_padding.Scale(dx, dy);
base.ScaleCore(dx, dy);
}
protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
{
base.ScaleControl (factor, specified);
}
protected virtual Point ScrollToControl (Control activeControl)
{
int corner_x;
int corner_y;
Rectangle within = new Rectangle ();
within.Size = ClientSize;
if (vscrollbar.Visible)
within.Width -= vscrollbar.Width;
if (hscrollbar.Visible)
within.Height -= hscrollbar.Height;
// If the control is above the top or the left, move it down and right until it aligns
// with the top/left.
// If the control is below the bottom or to the right, move it up/left until it aligns
// with the bottom/right, but do never move it further than the top/left side.
int x_diff = 0, y_diff = 0;
if (activeControl.Top <= 0 || activeControl.Height >= within.Height)
y_diff = -activeControl.Top;
else if (activeControl.Bottom > within.Height)
y_diff = within.Height - activeControl.Bottom;
if (activeControl.Left <= 0 || activeControl.Width >= within.Width)
x_diff = -activeControl.Left;
else if (activeControl.Right > within.Width)
x_diff = within.Width - activeControl.Right;
corner_x = AutoScrollPosition.X + x_diff;
corner_y = AutoScrollPosition.Y + y_diff;
return new Point (corner_x, corner_y);
}
protected void SetDisplayRectLocation(int x, int y) {
// This method is weird. MS documents that the scrollbars are not
// updated. We need to move stuff, but leave the scrollbars as is
if (x > 0) {
x = 0;
}
if (y > 0) {
y = 0;
}
ScrollWindow(scroll_position.X - x , scroll_position.Y - y);
}
protected void SetScrollState(int bit, bool value) {
//throw new NotImplementedException();
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
}
#endregion // Protected Instance Methods
#region Internal & Private Methods
internal override IntPtr AfterTopMostControl ()
{
// order of scrollbars:
// top = vertical
// sizegrid
// bottom = horizontal
if (hscrollbar != null && hscrollbar.Visible)
return hscrollbar.Handle;
// no need to check for sizegrip since it will only
// be visible if hbar is visible.
if (vscrollbar != null && vscrollbar.Visible)
return hscrollbar.Handle;
return base.AfterTopMostControl ();
}
internal virtual void CalculateCanvasSize (bool canOverride) {
Control child;
int num_of_children;
int width;
int height;
int extra_width;
int extra_height;
num_of_children = Controls.Count;
width = 0;
height = 0;
extra_width = hscrollbar.Value;
extra_height = vscrollbar.Value;
if (dock_padding != null) {
extra_width += dock_padding.Right;
extra_height += dock_padding.Bottom;
}
autosized_child = false;
for (int i = 0; i < num_of_children; i++) {
child = Controls[i];
if (child.AutoSize)
autosized_child = true;
if (child.Dock == DockStyle.Right) {
extra_width += child.Width;
} else if (child.Dock == DockStyle.Bottom) {
extra_height += child.Height;
}
}
if (!auto_scroll_min_size.IsEmpty) {
width = auto_scroll_min_size.Width;
height = auto_scroll_min_size.Height;
}
for (int i = 0; i < num_of_children; i++) {
child = Controls[i];
switch(child.Dock) {
case DockStyle.Left: {
if ((child.Right + extra_width) > width) {
width = child.Right + extra_width;
}
continue;
}
case DockStyle.Top: {
if ((child.Bottom + extra_height) > height) {
height = child.Bottom + extra_height;
}
continue;
}
case DockStyle.Fill:
case DockStyle.Right:
case DockStyle.Bottom: {
continue;
}
default: {
AnchorStyles anchor;
anchor = child.Anchor;
if (((anchor & AnchorStyles.Left) != 0) && ((anchor & AnchorStyles.Right) == 0)) {
if ((child.Right + extra_width) > width) {
width = child.Right + extra_width;
}
}
if (((anchor & AnchorStyles.Top) != 0) || ((anchor & AnchorStyles.Bottom) == 0)) {
if ((child.Bottom + extra_height) > height) {
height = child.Bottom + extra_height;
}
}
continue;
}
}
}
canvas_size.Width = width;
canvas_size.Height = height;
}
// Normally DockPadding is created lazyly, as observed in the test cases, but some children
// may need to have it always.
internal void CreateDockPadding ()
{
if (dock_padding == null)
dock_padding = new DockPaddingEdges (this);
}
private void Recalculate (object sender, EventArgs e) {
Recalculate (true);
}
private void Recalculate (bool doLayout) {
if (!IsHandleCreated) {
return;
}
Size canvas = canvas_size;
Size client = ClientSize;
canvas.Width += auto_scroll_margin.Width;
canvas.Height += auto_scroll_margin.Height;
int right_edge = client.Width;
int bottom_edge = client.Height;
int prev_right_edge;
int prev_bottom_edge;
bool hscroll_visible;
bool vscroll_visible;
do {
prev_right_edge = right_edge;
prev_bottom_edge = bottom_edge;
if ((force_hscroll_visible || (canvas.Width > right_edge && auto_scroll)) && client.Width > 0) {
hscroll_visible = true;
bottom_edge = client.Height - SystemInformation.HorizontalScrollBarHeight;
} else {
hscroll_visible = false;
bottom_edge = client.Height;
}
if ((force_vscroll_visible || (canvas.Height > bottom_edge && auto_scroll)) && client.Height > 0) {
vscroll_visible = true;
right_edge = client.Width - SystemInformation.VerticalScrollBarWidth;
} else {
vscroll_visible = false;
right_edge = client.Width;
}
} while (right_edge != prev_right_edge || bottom_edge != prev_bottom_edge);
if (right_edge < 0) right_edge = 0;
if (bottom_edge < 0) bottom_edge = 0;
Rectangle hscroll_bounds;
Rectangle vscroll_bounds;
hscroll_bounds = new Rectangle (0, client.Height - SystemInformation.HorizontalScrollBarHeight,
ClientRectangle.Width, SystemInformation.HorizontalScrollBarHeight);
vscroll_bounds = new Rectangle (client.Width - SystemInformation.VerticalScrollBarWidth, 0,
SystemInformation.VerticalScrollBarWidth, ClientRectangle.Height);
/* the ScrollWindow calls here are needed
* because (this explanation sucks):
*
* when we transition from having a scrollbar to
* not having one, we won't receive a scrollbar
* moved (value changed) event, so we need to
* manually scroll the canvas.
*
* if you can fix this without requiring the
* ScrollWindow calls, pdb and toshok will each
* pay you $5.
*/
if (!vscrollbar.Visible) {
vscrollbar.Value = 0;
}
if (!hscrollbar.Visible) {
hscrollbar.Value = 0;
}
/* Manually setting the size of the thumb should be done before
* the other assignments */
if (hscroll_visible) {
hscrollbar.manual_thumb_size = right_edge;
hscrollbar.LargeChange = right_edge;
hscrollbar.SmallChange = 5;
hscrollbar.Maximum = canvas.Width - 1;
} else {
if (hscrollbar != null && hscrollbar.VisibleInternal) {
ScrollWindow (- scroll_position.X, 0);
}
scroll_position.X = 0;
}
if (vscroll_visible) {
vscrollbar.manual_thumb_size = bottom_edge;
vscrollbar.LargeChange = bottom_edge;
vscrollbar.SmallChange = 5;
vscrollbar.Maximum = canvas.Height - 1;
} else {
if (vscrollbar != null && vscrollbar.VisibleInternal) {
ScrollWindow (0, - scroll_position.Y);
}
scroll_position.Y = 0;
}
if (hscroll_visible && vscroll_visible) {
hscroll_bounds.Width -= SystemInformation.VerticalScrollBarWidth;
vscroll_bounds.Height -= SystemInformation.HorizontalScrollBarHeight;
sizegrip.Bounds = new Rectangle (hscroll_bounds.Right,
vscroll_bounds.Bottom,
SystemInformation.VerticalScrollBarWidth,
SystemInformation.HorizontalScrollBarHeight);
}
SuspendLayout ();
hscrollbar.SetBoundsInternal (hscroll_bounds.X, hscroll_bounds.Y, hscroll_bounds.Width, hscroll_bounds.Height, BoundsSpecified.None);
hscrollbar.Visible = hscroll_visible;
if (hscrollbar.Visible)
XplatUI.SetZOrder (hscrollbar.Handle, IntPtr.Zero, true, false);
vscrollbar.SetBoundsInternal (vscroll_bounds.X, vscroll_bounds.Y, vscroll_bounds.Width, vscroll_bounds.Height, BoundsSpecified.None);
vscrollbar.Visible = vscroll_visible;
if (vscrollbar.Visible)
XplatUI.SetZOrder (vscrollbar.Handle, IntPtr.Zero, true, false);
UpdateSizeGripVisible ();
ResumeLayout (doLayout);
// We should now scroll the active control into view,
// the funny part is that ScrollableControl does not have
// the concept of active control.
ContainerControl container = this as ContainerControl;
if (container != null && container.ActiveControl != null) {
ScrollControlIntoView (container.ActiveControl);
}
}
internal void UpdateSizeGripVisible ()
{
if (!IsHandleCreated) {
return;
}
sizegrip.CapturedControl = Parent;
// This is really wierd, the size grip is only showing up
// if the bottom right corner of the scrollable control is within
// two pixels from the bottom right corner of its parent.
bool show_sizegrip = hscrollbar.VisibleInternal && vscrollbar.VisibleInternal;
bool enable_sizegrip = false;
if (show_sizegrip && Parent != null) {
Point diff = new Point (Parent.ClientRectangle.Bottom - Bottom, Parent.ClientRectangle.Right - Right);
enable_sizegrip = diff.X <= 2 && diff.X >= 0 && diff.Y <= 2 && diff.Y >= 0;
}
sizegrip.Visible = show_sizegrip;
sizegrip.Enabled = enable_sizegrip || sizegrip.Capture;
if (sizegrip.Visible)
XplatUI.SetZOrder (sizegrip.Handle, vscrollbar.Handle, false, false);
}
private void HandleScrollBar(object sender, EventArgs e) {
if (sender == vscrollbar) {
if (!vscrollbar.Visible)
return;
ScrollWindow(0, vscrollbar.Value- scroll_position.Y);
} else {
if (!hscrollbar.Visible)
return;
ScrollWindow(hscrollbar.Value - scroll_position.X, 0);
}
}
private void HandleScrollEvent (object sender, ScrollEventArgs args)
{
OnScroll (args);
}
private void AddScrollbars (object o, EventArgs e)
{
Controls.AddRangeImplicit (new Control[] {hscrollbar, vscrollbar, sizegrip});
HandleCreated -= new EventHandler (AddScrollbars);
}
private void CreateScrollbars ()
{
hscrollbar = new ImplicitHScrollBar ();
hscrollbar.Visible = false;
hscrollbar.ValueChanged += new EventHandler (HandleScrollBar);
hscrollbar.Height = SystemInformation.HorizontalScrollBarHeight;
hscrollbar.use_manual_thumb_size = true;
hscrollbar.Scroll += new ScrollEventHandler (HandleScrollEvent);
vscrollbar = new ImplicitVScrollBar ();
vscrollbar.Visible = false;
vscrollbar.ValueChanged += new EventHandler (HandleScrollBar);
vscrollbar.Width = SystemInformation.VerticalScrollBarWidth;
vscrollbar.use_manual_thumb_size = true;
vscrollbar.Scroll += new ScrollEventHandler (HandleScrollEvent);
sizegrip = new SizeGrip (this);
sizegrip.Visible = false;
}
private void ScrollWindow(int XOffset, int YOffset) {
int num_of_children;
if (XOffset == 0 && YOffset == 0) {
return;
}
SuspendLayout();
num_of_children = Controls.Count;
for (int i = 0; i < num_of_children; i++) {
Controls[i].Location = new Point (Controls[i].Left - XOffset, Controls[i].Top - YOffset);
//Controls[i].Left -= XOffset;
//Controls[i].Top -= YOffset;
// Is this faster? Controls[i].Location -= new Size(XOffset, YOffset);
}
scroll_position.X += XOffset;
scroll_position.Y += YOffset;
XplatUI.ScrollWindow (Handle, ClientRectangle, -XOffset, -YOffset, false);
ResumeLayout(false);
}
#endregion // Internal & Private Methods
static object OnScrollEvent = new object ();
protected virtual void OnScroll (ScrollEventArgs se)
{
ScrollEventHandler eh = (ScrollEventHandler) (Events [OnScrollEvent]);
if (eh != null)
eh (this, se);
}
protected override void OnPaddingChanged (EventArgs e)
{
base.OnPaddingChanged (e);
}
protected override void OnPaintBackground (PaintEventArgs e)
{
base.OnPaintBackground (e);
}
[EditorBrowsable (EditorBrowsableState.Advanced)]
protected override void OnRightToLeftChanged (EventArgs e)
{
base.OnRightToLeftChanged (e);
}
public event ScrollEventHandler Scroll {
add { Events.AddHandler (OnScrollEvent, value); }
remove { Events.RemoveHandler (OnScrollEvent, value); }
}
}
}
|