2 // System.Windows.Forms.Button.cs
\r
5 // stubbed out by Jaak Simm (jaaksimm@firm.ee)
\r
6 // implemented for Gtk+ by Rachel Hestilow (hestilow@ximian.com)
\r
7 // Dennis Hayes (dennish@raytek.com)
\r
8 // WINELib implementation started by John Sohn (jsohn@columbus.rr.com)
\r
10 // (C) Ximian, Inc., 2002
\r
13 using System.Drawing;
\r
14 using System.Runtime.InteropServices;
\r
16 namespace System.Windows.Forms {
\r
19 /// Represents a Windows button control.
\r
22 public class Button : ButtonBase, IButtonControl {
\r
25 DialogResult dialogResult;
\r
28 // --- Constructor ---
\r
29 public Button() : base()
\r
31 dialogResult = DialogResult.None;
\r
32 MouseIsOver_ = false;
\r
33 SubClassWndProc_ = true;
\r
36 // --- Properties ---
\r
37 protected override CreateParams CreateParams {
\r
39 // This is a child control, so it must have a parent for creation
\r
40 if( Parent != null) {
\r
41 CreateParams createParams = new CreateParams ();
\r
42 // CHECKME: here we must not overwrite window
\r
43 if( window == null) {
\r
44 window = new ControlNativeWindow (this);
\r
47 createParams.Caption = Text;
\r
48 createParams.ClassName = "BUTTON";
\r
49 createParams.X = Left;
\r
50 createParams.Y = Top;
\r
51 createParams.Width = Width;
\r
52 createParams.Height = Height;
\r
53 createParams.ClassStyle = 0;
\r
54 createParams.ExStyle = 0;
\r
55 createParams.Param = 0;
\r
56 createParams.Parent = Parent.Handle;
\r
57 createParams.Style = (int) (
\r
58 WindowStyles.WS_CHILD |
\r
59 WindowStyles.WS_VISIBLE |
\r
60 WindowStyles.WS_CLIPSIBLINGS );
\r
61 if(FlatStyle != FlatStyle.System) {
\r
62 createParams.Style |= (int) ButtonStyles.BS_OWNERDRAW;
\r
64 createParams.Style |= (int)sysButtonStyles_;
\r
65 // CHECKME : this call is commented because (IMHO) Control.CreateHandle suppose to do this
\r
66 // and this function is CreateParams, not CreateHandle
\r
67 // window.CreateHandle (createParams);
\r
68 return createParams;
\r
74 // --- IButtonControl property ---
\r
75 public virtual DialogResult DialogResult {
\r
76 get { return dialogResult; }
\r
77 set { dialogResult = value; }
\r
80 // --- IButtonControl method ---
\r
82 public virtual void NotifyDefault(bool value)
\r
88 public void PerformClick()
\r
93 // --- Button methods for events ---
\r
94 protected override void OnClick(EventArgs e)
\r
99 protected override void OnMouseUp(MouseEventArgs e)
\r
101 base.OnMouseUp (e);
\r
104 // --- Button methods ---
\r
105 protected override bool ProcessMnemonic (char charCode)
\r
107 return base.ProcessMnemonic (charCode);
\r
111 public override string ToString ()
\r
113 return base.ToString();
\r
116 protected override void OnMouseEnter (EventArgs e) {
\r
117 base.OnMouseEnter(e);
\r
118 if( FlatStyle == FlatStyle.Flat || FlatStyle == FlatStyle.Popup) {
\r
123 protected override void OnMouseLeave (EventArgs e) {
\r
124 base.OnMouseLeave(e);
\r
125 if( FlatStyle == FlatStyle.Flat || FlatStyle == FlatStyle.Popup) {
\r
130 internal void OnDrawItem(DrawItemEventArgs e) {
\r
131 Graphics paintOn = e.Graphics;
\r
132 Rectangle rc = e.Bounds;
\r
133 Rectangle rcImageClip = e.Bounds;
\r
134 rcImageClip.Inflate(-2,-2);
\r
136 if( FlatStyle == FlatStyle.Flat) {
\r
137 if( (e.State & DrawItemState.Selected) != 0) {
\r
138 SolidBrush sb = new SolidBrush(ControlPaint.Light(SystemColors.Control));
\r
139 paintOn.FillRectangle(sb, rc);
\r
143 if( mouseIsInside_) {
\r
144 SolidBrush sb = new SolidBrush(ControlPaint.Dark(SystemColors.Control));
\r
145 paintOn.FillRectangle(sb, rc);
\r
149 paintOn.FillRectangle(SystemBrushes.Control, rc);
\r
153 ControlPaint.DrawBorder(paintOn, rc, SystemColors.ControlText, ButtonBorderStyle.Solid);
\r
156 if( (e.State & DrawItemState.Focus) != 0) {
\r
157 ControlPaint.DrawBorder(paintOn, rc, SystemColors.ControlText, ButtonBorderStyle.Solid);
\r
160 rcImageClip.Inflate(1,1);
\r
164 else if( FlatStyle == FlatStyle.Popup) {
\r
165 if( (e.State & DrawItemState.Selected) != 0) {
\r
166 ControlPaint.DrawBorder(paintOn, rc, SystemColors.ControlText, ButtonBorderStyle.Solid);
\r
168 ControlPaint.DrawBorder(paintOn, rc, SystemColors.ControlText, ButtonBorderStyle.Solid);
\r
172 if( (e.State & DrawItemState.Focus) != 0) {
\r
173 ControlPaint.DrawBorder(paintOn, rc, SystemColors.ControlText, ButtonBorderStyle.Solid);
\r
177 if( mouseIsInside_) {
\r
178 Color colorLight = ControlPaint.Light(SystemColors.Control);
\r
179 ControlPaint.DrawBorder(paintOn, rc, colorLight, 1, ButtonBorderStyle.Solid,
\r
180 colorLight, 1, ButtonBorderStyle.Solid, SystemColors.ControlText, 1, ButtonBorderStyle.Solid,
\r
181 SystemColors.ControlText, 1, ButtonBorderStyle.Solid);
\r
184 ControlPaint.DrawBorder(paintOn, rc, SystemColors.ControlText, ButtonBorderStyle.Solid);
\r
188 paintOn.FillRectangle(SystemBrushes.Control, rc);
\r
193 ButtonState btnState = ButtonState.Normal;
\r
195 if( (e.State & DrawItemState.Selected) != 0) {
\r
196 btnState = ButtonState.Pushed;
\r
199 ControlPaint.DrawButton(paintOn, rc, btnState);
\r
204 // Do not place Text and Images on the borders
\r
205 paintOn.Clip = new Region(rcImageClip);
\r
206 if( BackgroundImage != null) {
\r
207 paintOn.DrawImage(BackgroundImage, e.Bounds.X, e.Bounds.Y, BackgroundImage.Width, BackgroundImage.Height);
\r
210 // Make "Focus" rectangle
\r
212 Rectangle focusRC = rc;
\r
214 if(Image != null) {
\r
218 if( ImageAlign == ContentAlignment.TopCenter ||
\r
219 ImageAlign == ContentAlignment.MiddleCenter ||
\r
220 ImageAlign == ContentAlignment.BottomCenter) {
\r
221 X += (rc.Width - Image.Width) / 2;
\r
223 else if(ImageAlign == ContentAlignment.TopRight ||
\r
224 ImageAlign == ContentAlignment.MiddleRight||
\r
225 ImageAlign == ContentAlignment.BottomRight) {
\r
226 X += (rc.Width - Image.Width);
\r
229 if( ImageAlign == ContentAlignment.BottomCenter ||
\r
230 ImageAlign == ContentAlignment.BottomLeft ||
\r
231 ImageAlign == ContentAlignment.BottomRight) {
\r
232 Y += rc.Height - Image.Height;
\r
234 else if(ImageAlign == ContentAlignment.MiddleCenter ||
\r
235 ImageAlign == ContentAlignment.MiddleLeft ||
\r
236 ImageAlign == ContentAlignment.MiddleRight) {
\r
237 Y += (rc.Height - Image.Height) / 2;
\r
239 paintOn.DrawImage(Image, X, Y, Image.Width, Image.Height);
\r
242 if( (e.State & DrawItemState.Selected) != 0) {
\r
243 // FlatStyle.Flat uses color and not text offset to show state
\r
244 // FIXME: use SysMetrics to determine offset values ?
\r
245 if( FlatStyle != FlatStyle.Flat) {
\r
250 StringFormat format = new StringFormat();
\r
251 format.Alignment = horizontalAlign_;
\r
252 format.LineAlignment = verticalAlign_;
\r
253 paintOn.DrawString(Text, Font, SystemBrushes.ControlText, rc, format);
\r
255 if( (e.State & DrawItemState.Focus) != 0) {
\r
256 // FIXME: Draw focus rectangle in different colors
\r
257 ControlPaint.DrawFocusRectangle( paintOn, focusRC);
\r
261 protected override void WndProc (ref Message m) {
\r
263 case Msg.WM_DRAWITEM: {
\r
264 DRAWITEMSTRUCT dis = new DRAWITEMSTRUCT();
\r
265 dis = (DRAWITEMSTRUCT)Marshal.PtrToStructure(m.LParam, dis.GetType());
\r
266 Rectangle rect = new Rectangle(dis.rcItem.left, dis.rcItem.top, dis.rcItem.right - dis.rcItem.left, dis.rcItem.bottom - dis.rcItem.top);
\r
267 DrawItemEventArgs args = new DrawItemEventArgs(Graphics.FromHdc(dis.hDC), Font,
\r
268 rect, dis.itemID, (DrawItemState)dis.itemState);
\r
270 //Marshal.StructureToPtr(dis, m.LParam, false);
\r
271 m.Result = (IntPtr)1;
\r
275 base.WndProc (ref m);
\r
280 /// --- Button events ---
\r
281 /// commented out, cause it only supports the .NET Framework infrastructure
\r
284 public new event EventHandler DoubleClick {
\r
286 throw new NotImplementedException ();
\r
289 throw new NotImplementedException ();
\r