Skip to content

Commit 77702d0

Browse files
committed
ScrollableControl is somehow working now, can be used by setting AutoScroll to true;
added control.ControlAdded and OnControlAdded; added control.ControlRemoved and OnControlRemoved; added control.OnLayout; changed form painting. Moved header and system buttons painting to late one. Override uwfOnLatePaint and invoke controls painting if needed (see Form class); changed some stuff to internal; fixed possibility of adding null reference to controls collection;
1 parent 3c5ce90 commit 77702d0

14 files changed

+629
-36
lines changed

Core/ApplicationResources.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace System.Windows.Forms
22
{
3-
public static class ApplicationResources
3+
internal static class ApplicationResources
44
{
55
public static AppGdiImages Items
66
{

System/Windows/Forms/AppGdiImages.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
using System.Drawing;
44

5-
public class AppGdiImages
5+
internal class AppGdiImages
66
{
77
public Bitmap ArrowDown;
88
public Bitmap ArrowLeft;

System/Windows/Forms/Control.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ public class Control : Component, IDropTarget
1717
internal bool shouldFocus;
1818

1919
internal Application uwfAppOwner;
20-
internal bool uwfAutoGroup;
20+
internal bool uwfAutoGroup; // Used by GUI.BeginGroup & GUI.EndGroup. Allows to paint controls using their relevant position Rect(0, 0, Width, Height).
2121
internal bool uwfShadowBox;
2222
internal DrawHandler uwfShadowHandler;
23-
internal Point uwfOffset;
23+
internal Point uwfOffset; // Position for scrolling. Affects rendering.
24+
internal bool uwfSystem; // Indicates that Control can be handled by OS (like scrollbars).
2425

2526
private AnchorStyles anchor = AnchorStyles.Top | AnchorStyles.Left;
2627
private Color backColor = SystemColors.Control;
@@ -72,6 +73,8 @@ public Control()
7273
public event EventHandler AutoSizeChanged;
7374
public event EventHandler Click;
7475
public event EventHandler ClientSizeChanged;
76+
public event ControlEventHandler ControlAdded;
77+
public event ControlEventHandler ControlRemoved;
7578
public event EventHandler DoubleClick;
7679
public event DragEventHandler DragDrop;
7780
public event DragEventHandler DragEnter;
@@ -350,6 +353,7 @@ public virtual void Invalidate(Rectangle rc, bool invalidateChildren)
350353
}
351354
public void PerformLayout()
352355
{
356+
PerformLayout(null);
353357
}
354358
public Point PointToClient(Point p)
355359
{
@@ -486,6 +490,10 @@ internal void PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backC
486490
else if (backColor.A > 0) // Fill back.
487491
PaintBackColor(e, rectangle, backColor);
488492
}
493+
internal void PerformLayout(object args)
494+
{
495+
OnLayout(args);
496+
}
489497
internal virtual void RaiseOnDragDrop(DragEventArgs drgevent)
490498
{
491499
OnDragDrop(drgevent);
@@ -689,6 +697,18 @@ protected virtual void OnClientSizeChanged(EventArgs e)
689697
if (clientSizeChanged != null)
690698
clientSizeChanged(this, e);
691699
}
700+
protected virtual void OnControlAdded(ControlEventArgs e)
701+
{
702+
var handler = ControlAdded;
703+
if (handler != null)
704+
handler(this, e);
705+
}
706+
protected virtual void OnControlRemoved(ControlEventArgs e)
707+
{
708+
var handler = ControlRemoved;
709+
if (handler != null)
710+
handler(this, e);
711+
}
692712
protected virtual void OnDoubleClick(EventArgs e)
693713
{
694714
var doubleClick = DoubleClick;
@@ -750,6 +770,9 @@ protected virtual void OnKeyUp(KeyEventArgs e)
750770
if (keyUp != null)
751771
keyUp(this, e);
752772
}
773+
protected virtual void OnLayout(object levent)
774+
{
775+
}
753776
protected virtual void OnLocationChanged(EventArgs e)
754777
{
755778
var locationChanged = LocationChanged;
@@ -839,6 +862,8 @@ protected virtual void OnResize(EventArgs e)
839862
var resize = Resize;
840863
if (resize != null)
841864
resize(this, e);
865+
866+
PerformLayout();
842867
}
843868
protected virtual void OnSizeChanged(EventArgs e)
844869
{
@@ -919,6 +944,8 @@ protected void UpdateBounds(int argX, int argY, int argWidth, int argHeight, int
919944

920945
OnSizeChanged(EventArgs.Empty);
921946
OnClientSizeChanged(EventArgs.Empty);
947+
948+
PerformLayout();
922949
}
923950

924951
private static void PaintBackColor(PaintEventArgs e, Rectangle rectangle, Color backColor)
@@ -1049,8 +1076,14 @@ IEnumerator IEnumerable.GetEnumerator()
10491076
}
10501077
public virtual void Add(Control value)
10511078
{
1079+
if (value == null)
1080+
return;
1081+
10521082
items.Add(value);
10531083
value.AssignParent(owner);
1084+
1085+
owner.PerformLayout();
1086+
owner.OnControlAdded(new ControlEventArgs(value));
10541087
}
10551088
public virtual void AddRange(Control[] controls)
10561089
{
@@ -1059,7 +1092,8 @@ public virtual void AddRange(Control[] controls)
10591092
}
10601093
public virtual void Clear()
10611094
{
1062-
items.Clear();
1095+
while (Count != 0)
1096+
RemoveAt(Count - 1);
10631097
}
10641098
public bool Contains(Control control)
10651099
{
@@ -1117,11 +1151,16 @@ public void Insert(int index, Control value)
11171151
}
11181152
public virtual void Remove(Control item)
11191153
{
1154+
if (item == null)
1155+
return;
1156+
11201157
items.Remove(item);
1158+
owner.PerformLayout();
1159+
owner.OnControlRemoved(new ControlEventArgs(item));
11211160
}
11221161
public void RemoveAt(int index)
11231162
{
1124-
items.RemoveAt(index);
1163+
Remove(this[index]);
11251164
}
11261165
public virtual void RemoveByKey(string key)
11271166
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace System.Windows.Forms
2+
{
3+
public delegate void ControlEventHandler(object sender, ControlEventArgs e);
4+
5+
public class ControlEventArgs : EventArgs
6+
{
7+
public ControlEventArgs(Control control)
8+
{
9+
Control = control;
10+
}
11+
12+
public Control Control { get; private set; }
13+
}
14+
}

System/Windows/Forms/Form.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Form : ContainerControl, IResizableControl
3131
private static Point nextLocation = new Point(156, 156);
3232

3333
private Color backColor = SystemColors.Control;
34-
private Button closeButton;
34+
private formSystemButton closeButton;
3535
private Action<Form, DialogResult> dialogCallback;
3636
private MenuStrip mainMenuStrip;
3737
private Point windowMove_StartPosition;
@@ -129,6 +129,8 @@ public SizeGripStyle SizeGripStyle
129129
}
130130
else
131131
_MakeButtonResize();
132+
133+
PerformLayout();
132134
}
133135
}
134136
public FormStartPosition StartPosition
@@ -355,14 +357,35 @@ internal void CloseInternal(CloseReason closeReason)
355357
if (dialog && dialogCallback != null)
356358
dialogCallback.Invoke(this, DialogResult);
357359
}
360+
internal void ResetGripRendererLocation()
361+
{
362+
if (uwfSizeGripRenderer == null)
363+
return;
364+
365+
var img = uwfSizeGripRenderer.Image;
366+
uwfSizeGripRenderer.Location = new Point(Width - img.Width - 2, Height - img.Height - 2);
367+
}
358368

359369
protected internal override void uwfOnLatePaint(PaintEventArgs e)
360370
{
361371
base.uwfOnLatePaint(e);
362372

363373
var g = e.Graphics;
374+
var headerHeight = uwfHeaderHeight;
375+
var headerPadding = uwfHeaderPadding;
364376
var width = Width;
365377

378+
g.uwfFillRectangle(uwfHeaderColor, 0, 0, width, headerHeight);
379+
g.uwfDrawString(Text, uwfHeaderFont, uwfHeaderTextColor, headerPadding.Left, headerPadding.Top, width - headerPadding.Horizontal, headerHeight - headerPadding.Vertical, uwfHeaderTextAlign);
380+
381+
// System controls.
382+
if (closeButton != null)
383+
{
384+
closeButton.formPainting = true;
385+
closeButton.RaiseOnPaint(e);
386+
closeButton.formPainting = false;
387+
}
388+
366389
g.DrawLine(innerBorderPen, 0, uwfHeaderHeight - 1, width, uwfHeaderHeight - 1);
367390
g.DrawRectangle(borderPen, 0, 0, width, Height);
368391
}
@@ -439,13 +462,9 @@ protected override void OnMouseMove(MouseEventArgs e)
439462
protected override void OnPaint(PaintEventArgs e)
440463
{
441464
var g = e.Graphics;
442-
443465
var headerHeight = uwfHeaderHeight;
444-
var headerPadding = uwfHeaderPadding;
445466
var width = Width;
446467

447-
g.uwfFillRectangle(uwfHeaderColor, 0, 0, width, headerHeight);
448-
g.uwfDrawString(Text, uwfHeaderFont, uwfHeaderTextColor, headerPadding.Left, headerPadding.Top, width - headerPadding.Horizontal, headerHeight - headerPadding.Vertical, uwfHeaderTextAlign);
449468
g.uwfFillRectangle(BackColor, 0, headerHeight, width, Height - headerHeight);
450469
}
451470
protected virtual void OnShown(EventArgs e)
@@ -493,6 +512,7 @@ private void _MakeButtonClose()
493512
closeButton.uwfHoverColor = Color.FromArgb(64, 252, 252, 252);
494513
closeButton.uwfImageColor = Color.FromArgb(64, 64, 64);
495514
closeButton.uwfImageHoverColor = Color.FromArgb(128, 128, 128);
515+
closeButton.uwfSystem = true;
496516

497517
closeButton.BringToFront();
498518
closeButton.Click += OnCloseButtonOnClick;
@@ -505,8 +525,11 @@ private void _MakeButtonResize()
505525
if (img == null) return;
506526

507527
uwfSizeGripRenderer = new ResizeButton(this, img);
508-
uwfSizeGripRenderer.Location = new Point(Width - img.Width - 2, Height - img.Height - 2);
528+
uwfSizeGripRenderer.uwfSystem = true;
529+
509530
Controls.Add(uwfSizeGripRenderer);
531+
532+
ResetGripRendererLocation();
510533
}
511534
private void OnCloseButtonOnClick(object o, EventArgs e)
512535
{
@@ -588,10 +611,18 @@ private bool TryFocus()
588611

589612
private class formSystemButton : Button
590613
{
614+
internal bool formPainting;
615+
591616
public formSystemButton()
592617
{
593618
SetStyle(ControlStyles.Selectable, false);
594619
}
620+
621+
protected override void OnPaint(PaintEventArgs e)
622+
{
623+
if (formPainting)
624+
base.OnPaint(e);
625+
}
595626
}
596627
}
597628
}

System/Windows/Forms/HScrollProperties.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,22 @@ public class HScrollProperties : ScrollProperties
55
public HScrollProperties(ScrollableControl container) : base(container)
66
{
77
}
8+
9+
internal override int PageSize
10+
{
11+
get { return ParentControl.ClientRectangle.Width; }
12+
}
13+
internal override int Orientation
14+
{
15+
get { return NativeMethods.SB_HORZ; }
16+
}
17+
internal override int HorizontalDisplayPosition
18+
{
19+
get { return -value; }
20+
}
21+
internal override int VerticalDisplayPosition
22+
{
23+
get { return ParentControl.DisplayRectangle.Y; }
24+
}
825
}
926
}

System/Windows/Forms/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,8 @@ internal static class NativeMethods
121121
WS_EX_NOINHERITLAYOUT = 0x00100000,
122122
WPF_SETMINPOSITION = 0x0001,
123123
WM_CHOOSEFONT_GETLOGFONT = 0x0400 + 1;
124+
125+
public const int SB_HORZ = 0,
126+
SB_VERT = 1;
124127
}
125128
}

0 commit comments

Comments
 (0)