Skip to content

Commit a5f9309

Browse files
committed
added Checked, CheckState for ToolStripButton;
added timeout between AcceptButtons.PerformClick in 'Form' class in case 'Enter' key were pressed and not released (MessageBox is also affected); fixed ToolStripSeparator width in Horizontal mode and also it's painting; fixed ToolStripItem background painting; fixed removing nodes from TreeNodeCollection;
1 parent 5476dff commit a5f9309

File tree

7 files changed

+136
-32
lines changed

7 files changed

+136
-32
lines changed

System/Windows/Forms/Form.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class Form : ContainerControl, IResizableControl
2929
private static readonly Padding defaultFormPadding = new Padding(32, 0, 32, 0);
3030
private static readonly Color defaultFormShadowColor = Color.FromArgb(12, 64, 64, 64);
3131
private static Point nextLocation = new Point(156, 156);
32+
private static Form lastAcceptForm;
33+
private static Timer lastAcceptFormTimer;
3234

3335
private Color backColor = SystemColors.Control;
3436
private formSystemButton closeButton;
@@ -490,10 +492,31 @@ protected override void OnKeyDown(KeyEventArgs e)
490492
{
491493
base.OnKeyDown(e);
492494

493-
if (e.KeyCode == Keys.Enter && AcceptButton != null)
495+
if (e.KeyCode == Keys.Enter && AcceptButton != null && lastAcceptForm == null)
496+
{
497+
lastAcceptForm = this;
498+
499+
// Start timer that will reset the last form with the accept button.
500+
if (lastAcceptFormTimer != null)
501+
lastAcceptFormTimer.Dispose();
502+
503+
lastAcceptFormTimer = new Timer();
504+
lastAcceptFormTimer.Interval = 1000; // Interval between the accepting forms.
505+
lastAcceptFormTimer.Tick += (sender, args) =>
506+
{
507+
lastAcceptForm = null;
508+
lastAcceptFormTimer.Dispose();
509+
lastAcceptFormTimer = null;
510+
};
511+
lastAcceptFormTimer.Start();
512+
494513
AcceptButton.PerformClick();
514+
}
515+
495516
if (e.KeyCode == Keys.Escape && CancelButton != null)
517+
{
496518
CancelButton.PerformClick();
519+
}
497520
}
498521
protected override void OnMouseDown(MouseEventArgs e)
499522
{

System/Windows/Forms/MessageBox.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public static DialogResult Show(string text, string caption, Font textFont)
7171
form.AcceptButton = buttonOk;
7272
form.Controls.Add(buttonOk);
7373
form.ShowDialog();
74+
form.Focus(); // Disable button focus to prevent invoking Click event from the OnKeyDown event.
7475

7576
Last = form;
7677

System/Windows/Forms/ToolStrip.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,6 @@ internal ToolStripItem ItemAt(Point position)
113113

114114
return null;
115115
}
116-
internal void MakeShadow()
117-
{
118-
uwfShadowHandler = (g) =>
119-
{
120-
var loc = PointToScreen(Point.Empty);
121-
var color = Color.FromArgb(12, 64, 64, 64);
122-
g.Graphics.uwfFillRectangle(color, loc.X - 3, loc.Y, Width + 6, Height + 3);
123-
g.Graphics.uwfFillRectangle(color, loc.X - 2, loc.Y, Width + 4, Height + 2);
124-
g.Graphics.uwfFillRectangle(color, loc.X - 1, loc.Y, Width + 2, Height + 1);
125-
};
126-
}
127116
internal void NotifySelectionChange(ToolStripItem item)
128117
{
129118
throw new NotImplementedException();
@@ -348,11 +337,16 @@ protected override void OnPaint(PaintEventArgs e)
348337
if (item.AutoSize && item.boundsChanged) // Check if item size need to be updated.
349338
{
350339
var itemTextWidth = item.GetEstimatedWidth(graphics);
351-
var itemWidth = itemTextWidth + item.Padding.Horizontal + 4;
352-
if (orientation == Orientation.Horizontal)
353-
item.Width = itemWidth;
340+
if (itemTextWidth > 0)
341+
{
342+
var itemWidth = itemTextWidth + item.Padding.Horizontal + 4;
343+
if (orientation == Orientation.Horizontal)
344+
item.Width = itemWidth;
345+
346+
maxWidth = Math.Max(maxWidth, itemWidth);
347+
}
348+
354349
item.boundsChanged = false;
355-
maxWidth = Math.Max(maxWidth, itemWidth);
356350
}
357351

358352
totalWidth += item.Width;

System/Windows/Forms/ToolStripButton.cs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ namespace System.Windows.Forms
22
{
33
using System.Drawing;
44

5+
//// Default size is 23, 22.
56
public class ToolStripButton : ToolStripItem
67
{
7-
//// Default size is 23, 22.
8-
8+
private CheckState checkState = CheckState.Unchecked;
9+
private bool checkOnClick;
10+
911
public ToolStripButton()
1012
{
1113
}
@@ -25,10 +27,41 @@ public ToolStripButton(string text, Image image, EventHandler onClick, string na
2527
{
2628
}
2729

30+
public event EventHandler CheckedChanged;
31+
public event EventHandler CheckStateChanged;
32+
2833
public override bool CanSelect
2934
{
3035
get { return true; }
3136
}
37+
public bool Checked
38+
{
39+
get { return checkState != CheckState.Unchecked; }
40+
set
41+
{
42+
if (value == Checked)
43+
return;
44+
45+
CheckState = value ? CheckState.Checked : CheckState.Unchecked;
46+
}
47+
}
48+
public CheckState CheckState
49+
{
50+
get { return checkState; }
51+
set
52+
{
53+
if (value == checkState)
54+
return;
55+
56+
checkState = value;
57+
OnCheckedChanged(EventArgs.Empty);
58+
OnCheckStateChanged(EventArgs.Empty);
59+
}
60+
}
61+
public bool CheckOnClick {
62+
get { return checkOnClick; }
63+
set { checkOnClick = value; }
64+
}
3265

3366
protected internal override void SetBounds(Rectangle nbounds)
3467
{
@@ -37,5 +70,31 @@ protected internal override void SetBounds(Rectangle nbounds)
3770

3871
base.SetBounds(nbounds);
3972
}
73+
74+
protected override void OnClick(EventArgs e)
75+
{
76+
if (checkOnClick)
77+
Checked = !Checked;
78+
79+
base.OnClick(e);
80+
}
81+
protected virtual void OnCheckedChanged(EventArgs e) {
82+
var handler = CheckedChanged;
83+
if (handler != null)
84+
handler(this,e);
85+
}
86+
protected virtual void OnCheckStateChanged(EventArgs e)
87+
{
88+
var handler = CheckStateChanged;
89+
if (handler != null)
90+
handler(this,e);
91+
}
92+
protected override void OnPaint(PaintEventArgs e)
93+
{
94+
base.OnPaint(e);
95+
96+
if (Checked)
97+
e.Graphics.DrawRectangle(selectPen, Bounds);
98+
}
4099
}
41100
}

System/Windows/Forms/ToolStripItem.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ protected internal virtual void DrawImage(Graphics graphics)
399399
}
400400
protected internal virtual int GetEstimatedWidth(Graphics graphics) // OnPaint only.
401401
{
402+
// ToolStripSeparator?
403+
if (string.IsNullOrEmpty(Text))
404+
return 0;
405+
402406
return (int)graphics.MeasureString(Text, Font).Width;
403407
}
404408
protected internal virtual void SetBounds(Rectangle nbounds)
@@ -483,7 +487,10 @@ protected virtual void OnPaint(PaintEventArgs e)
483487
leftOffset = Owner.Padding.Left;
484488

485489
// Paint back + backImage.
486-
ControlPaint.DrawBackgroundImage(graphics, BackgroundImage, localBackColor, BackgroundImageLayout, rect, rect);
490+
if (BackgroundImage != null)
491+
ControlPaint.DrawBackgroundImage(graphics, BackgroundImage, localBackColor, BackgroundImageLayout, rect, rect);
492+
else
493+
e.Graphics.uwfFillRectangle(localBackColor, rect);
487494

488495
// Draw border for selected state.
489496
if (stateSelected && Enabled)

System/Windows/Forms/ToolStripSeparator.cs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace System.Windows.Forms
44

55
public class ToolStripSeparator : ToolStripItem
66
{
7-
protected Pen borderColor = new Pen(defaultSeparatorBorderColor);
8-
protected Pen borderColor2 = new Pen(Color.White);
7+
protected readonly Pen borderColor = new Pen(defaultSeparatorBorderColor);
8+
protected readonly Pen borderColor2 = new Pen(Color.White);
99

1010
private const int WINBAR_SEPARATORTHICKNESS = 6;
1111
private static readonly Color defaultSeparatorBorderColor = Color.FromArgb(189, 189, 189);
@@ -24,18 +24,41 @@ protected override void OnPaint(PaintEventArgs e)
2424
{
2525
base.OnPaint(e);
2626

27+
if (Owner == null)
28+
return;
29+
2730
var g = e.Graphics;
2831
var bounds = Bounds;
29-
var ex = bounds.X;
30-
var ey = bounds.Y + Height / 2 - 1;
31-
var ex2 = bounds.X + bounds.Width - 8;
32-
var ey2 = ey + 1;
33-
34-
if (Owner != null && Owner.Orientation == Orientation.Vertical)
32+
33+
if (Owner.Orientation == Orientation.Vertical)
34+
{
35+
var ex = bounds.X;
36+
var ey = bounds.Y + Height / 2 - 1;
37+
var ex2 = bounds.X + bounds.Width - 8;
38+
var ey2 = ey + 1;
39+
3540
ex += Owner.Padding.Left;
3641

37-
g.DrawLine(borderColor, ex, ey, ex2, ey);
38-
g.DrawLine(borderColor2, ex, ey2, ex2, ey2);
42+
g.DrawLine(borderColor, ex, ey, ex2, ey);
43+
g.DrawLine(borderColor2, ex, ey2, ex2, ey2);
44+
}
45+
else
46+
{
47+
var ex = bounds.X + Width / 2 -1;
48+
var ey = Owner.Padding.Top;
49+
var ex2 = ex + 1;
50+
var ey2 = Owner.Height - Owner.Padding.Vertical + 1;
51+
52+
g.DrawLine(borderColor, ex, ey, ex, ey2);
53+
g.DrawLine(borderColor2, ex2, ey, ex2, ey2);
54+
}
55+
}
56+
57+
protected internal override void SetBounds(Rectangle nbounds)
58+
{
59+
//UnityEngine.Debug.Log(nbounds.Width);
60+
61+
base.SetBounds(nbounds);
3962
}
4063
}
4164
}

System/Windows/Forms/TreeNode.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,6 @@ internal int GetYIndex()
208208
}
209209
internal void Remove(bool notify)
210210
{
211-
for (int i = 0; i < Nodes.Count; i++)
212-
Nodes[i].Remove(false);
213-
214211
if (parent != null)
215212
{
216213
parent.Nodes.RemoveAt(index);

0 commit comments

Comments
 (0)