**** Merged from MCS ****
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ButtonBase.cs
blobe6e1be9aa567ef24a823f584e4e488e52a49c968
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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.
22 // Authors:
23 // Peter Bartok pbartok@novell.com
25 // $Log: ButtonBase.cs,v $
26 // Revision 1.15 2004/10/15 13:32:45 ravindra
27 // - Renamed Paint() method to Draw() for clarity. Also, moved
28 // DrawImage() to OnPaint().
30 // Revision 1.14 2004/10/15 13:16:10 ravindra
31 // - Redraw () is not virtual now.
32 // - Added an internal virtual method Paint (), so that
33 // derived classes can do their painting on their own.
34 // - Modified OnPaint () to call Paint ().
36 // Revision 1.13 2004/10/14 06:15:57 ravindra
37 // Redraw () related improvements.
39 // Revision 1.12 2004/10/13 22:32:38 pbartok
40 // - Now Redraws on MouseUp for FlatStyle Flat and Popup
42 // Revision 1.11 2004/10/13 20:12:47 pbartok
43 // - Added the Redraw on Resize that got dropped in the last rev
45 // Revision 1.10 2004/10/05 04:56:11 jackson
46 // Let the base Control handle the buffers, derived classes should not have to CreateBuffers themselves.
48 // Revision 1.9 2004/09/28 18:44:25 pbartok
49 // - Streamlined Theme interfaces:
50 // * Each DrawXXX method for a control now is passed the object for the
51 // control to be drawn in order to allow accessing any state the theme
52 // might require
54 // * ControlPaint methods for the theme now have a CP prefix to avoid
55 // name clashes with the Draw methods for controls
57 // * Every control now retrieves it's DefaultSize from the current theme
59 // Revision 1.8 2004/09/02 22:24:35 pbartok
60 // - Fixed selection of text color
61 // - Fixed handling of resize event; now properly recreates double buffering
62 // bitmap
63 // - Added missing assignment of TextAlignment
64 // - Added proper default for TextAlignment
66 // Revision 1.7 2004/09/01 02:07:37 pbartok
67 // - Enabled display of strings
69 // Revision 1.6 2004/09/01 01:55:20 pbartok
70 // - Removed the rather odd split between 'needs redraw' and redrawing
71 // - Now handles the events that require regeneration (ambient properties and
72 // size)
74 // Revision 1.5 2004/08/31 18:49:58 pbartok
75 // - Removed debug
76 // - Minor fixes
78 // Revision 1.4 2004/08/30 20:42:10 pbartok
79 // - Made Redraw() and Redraw() virtual
80 // - Improved mouse up/down/move logic to properly track buttons
82 // Revision 1.3 2004/08/23 23:27:44 pbartok
83 // - Finishing touches. Works now, just needs some optimizations.
85 // Revision 1.2 2004/08/21 21:57:41 pbartok
86 // - Added loads of debug output for development
87 // - Fixed typo in method name
89 // Revision 1.1 2004/08/15 21:31:10 pbartok
90 // - First (mostly) working version
95 // NOT COMPLETE
97 using System.ComponentModel;
98 using System.Drawing;
100 namespace System.Windows.Forms {
101 public abstract class ButtonBase : Control {
102 #region Local Variables
103 internal FlatStyle flat_style;
104 internal int image_index;
105 internal Image image;
106 internal ImageList image_list;
107 internal ContentAlignment image_alignment;
108 internal ContentAlignment text_alignment;
109 private ImeMode ime_mode;
110 private bool is_default;
111 internal bool has_focus;
112 internal bool is_pressed;
113 internal bool is_entered;
114 internal bool redraw;
115 internal StringFormat text_format;
116 #endregion // Local Variables
118 #region Private Properties and Methods
119 internal ButtonState ButtonState {
120 get {
121 ButtonState ret = ButtonState.Normal;
123 if (Enabled) {
124 // Popup style is only followed as long as the mouse isn't "in" the control
125 if (is_entered) {
126 if (flat_style == FlatStyle.Flat) {
127 ret |= ButtonState.Flat;
129 } else {
130 if (flat_style == FlatStyle.Flat || flat_style == FlatStyle.Popup) {
131 ret |= ButtonState.Flat;
135 if (is_entered && is_pressed) {
136 ret |= ButtonState.Pushed;
138 } else {
139 ret |= ButtonState.Inactive;
140 if ((flat_style == FlatStyle.Flat) || (flat_style == FlatStyle.Popup)) {
141 ret |= ButtonState.Flat;
144 return ret;
148 [MonoTODO("Make the FillRectangle use a global brush instead of creating one every time")]
149 internal void Redraw() {
150 redraw = true;
151 Refresh ();
154 // Derived classes should override Draw method and we dont want
155 // to break the control signature, hence this approach.
156 internal virtual void Draw (PaintEventArgs pevent) {
157 if (redraw) {
158 ThemeEngine.Current.DrawButtonBase(this.DeviceContext, pevent.ClipRectangle, this);
159 redraw = false;
163 private void RedrawEvent(object sender, System.EventArgs e) {
164 Redraw();
167 #endregion // Private Properties and Methods
169 #region Public Constructors
170 protected ButtonBase() : base() {
171 flat_style = FlatStyle.Standard;
172 image_index = -1;
173 image = null;
174 image_list = null;
175 image_alignment = ContentAlignment.MiddleCenter;
176 text_alignment = ContentAlignment.MiddleCenter;
177 ime_mode = ImeMode.Inherit;
178 is_default = false;
179 is_entered = false;
180 is_pressed = false;
181 has_focus = false;
182 redraw = true;
183 text_format = new StringFormat();
184 text_format.Alignment = StringAlignment.Center;
185 text_format.LineAlignment = StringAlignment.Center;
187 TextChanged+=new System.EventHandler(RedrawEvent);
188 ForeColorChanged+=new EventHandler(RedrawEvent);
189 BackColorChanged+=new System.EventHandler(RedrawEvent);
190 FontChanged+=new EventHandler(RedrawEvent);
191 SizeChanged+=new EventHandler(RedrawEvent);
193 SetStyle (ControlStyles.ResizeRedraw, true);
195 #endregion // Public Constructors
197 #region Public Instance Properties
198 public FlatStyle FlatStyle {
199 get {
200 return flat_style;
203 set {
204 flat_style = value;
205 Redraw();
209 public Image Image {
210 get {
211 return image;
214 set {
215 image = value;
216 Redraw();
220 public ContentAlignment ImageAlign {
221 get {
222 return image_alignment;
225 set {
226 image_alignment=value;
227 Redraw();
231 public int ImageIndex {
232 get {
233 if (image_list==null) {
234 return -1;
236 return image_index;
239 set {
240 image_index=value;
241 Redraw();
245 public ImageList ImageList {
246 get {
247 return image_list;
250 set {
251 if (image_list != null) {
252 image_list.Dispose();
255 image_list = value;
256 if (value != null) {
257 if (image != null) {
258 image=null;
260 if (image_list.Images.Count >= image_index) {
261 image_index=image_list.Images.Count-1;
264 Redraw();
268 public ImeMode ImeMode {
269 get {
270 return ime_mode;
273 set {
274 ime_mode = value;
278 public virtual ContentAlignment TextAlign {
279 get {
280 return text_alignment;
283 set {
284 if (text_alignment != value) {
285 text_alignment = value;
286 switch(text_alignment) {
287 case ContentAlignment.TopLeft: {
288 text_format.Alignment=StringAlignment.Near;
289 text_format.LineAlignment=StringAlignment.Near;
290 break;
293 case ContentAlignment.TopCenter: {
294 text_format.Alignment=StringAlignment.Center;
295 text_format.LineAlignment=StringAlignment.Near;
296 break;
299 case ContentAlignment.TopRight: {
300 text_format.Alignment=StringAlignment.Far;
301 text_format.LineAlignment=StringAlignment.Near;
302 break;
305 case ContentAlignment.MiddleLeft: {
306 text_format.Alignment=StringAlignment.Near;
307 text_format.LineAlignment=StringAlignment.Center;
308 break;
311 case ContentAlignment.MiddleCenter: {
312 text_format.Alignment=StringAlignment.Center;
313 text_format.LineAlignment=StringAlignment.Center;
314 break;
317 case ContentAlignment.MiddleRight: {
318 text_format.Alignment=StringAlignment.Far;
319 text_format.LineAlignment=StringAlignment.Center;
320 break;
323 case ContentAlignment.BottomLeft: {
324 text_format.Alignment=StringAlignment.Near;
325 text_format.LineAlignment=StringAlignment.Far;
326 break;
329 case ContentAlignment.BottomCenter: {
330 text_format.Alignment=StringAlignment.Center;
331 text_format.LineAlignment=StringAlignment.Far;
332 break;
335 case ContentAlignment.BottomRight: {
336 text_format.Alignment=StringAlignment.Far;
337 text_format.LineAlignment=StringAlignment.Far;
338 break;
341 Redraw();
346 #endregion // Public Instance Properties
348 #region Protected Instance Properties
349 protected override CreateParams CreateParams {
350 get {
351 CreateParams cp;
353 cp=base.CreateParams;
355 cp.Style=(int)WindowStyles.WS_VISIBLE | (int)WindowStyles.WS_CHILD;
357 SetStyle(ControlStyles.UserPaint, true);
358 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
359 return cp;
363 protected override ImeMode DefaultImeMode {
364 get {
365 return ImeMode.Inherit;
369 protected override Size DefaultSize {
370 get {
371 return ThemeEngine.Current.ButtonBaseDefaultSize;
375 protected bool IsDefault {
376 get {
377 return is_default;
380 set {
381 if (is_default != value) {
382 is_default = true;
383 Redraw();
387 #endregion // Public Instance Properties
389 #region Protected Instance Methods
390 [MonoTODO("Finish setting properties of the AccessibleObject")]
391 protected override AccessibleObject CreateAccessibilityInstance() {
392 AccessibleObject ao;
393 ao=base.CreateAccessibilityInstance();
394 ao.description="Button";
396 return ao;
399 protected override void Dispose(bool Disposing) {
400 base.Dispose(Disposing);
403 protected override void OnEnabledChanged(EventArgs e) {
404 Redraw();
405 base.OnEnabledChanged(e);
408 protected override void OnGotFocus(EventArgs e) {
409 has_focus=true;
410 Redraw();
411 base.OnGotFocus(e);
414 protected override void OnKeyDown(KeyEventArgs kevent) {
415 if (is_enabled && (kevent.KeyData == Keys.Enter || kevent.KeyData == Keys.Space)) {
416 OnClick(EventArgs.Empty);
417 kevent.Handled=true;
419 base.OnKeyDown(kevent);
422 protected override void OnKeyUp(KeyEventArgs kevent) {
423 base.OnKeyUp(kevent);
426 protected override void OnLostFocus(EventArgs e) {
427 has_focus=false;
428 Redraw();
429 base.OnLostFocus(e);
432 protected override void OnMouseDown(MouseEventArgs mevent) {
433 if (is_enabled && (mevent.Button == MouseButtons.Left)) {
434 is_pressed = true;
435 this.Capture = true;
436 Redraw();
439 base.OnMouseDown(mevent);
442 protected override void OnMouseEnter(EventArgs e) {
443 is_entered=true;
444 if ((this.flat_style == FlatStyle.Flat) || (this.flat_style == FlatStyle.Popup)) {
445 Redraw();
447 base.OnMouseEnter(e);
450 protected override void OnMouseLeave(EventArgs e) {
451 is_entered=false;
452 if ((this.flat_style == FlatStyle.Flat) || (this.flat_style == FlatStyle.Popup)) {
453 Redraw();
455 base.OnMouseLeave(e);
458 protected override void OnMouseMove(MouseEventArgs mevent) {
459 bool inside = false;
460 bool redraw = false;
462 if (mevent.X>=0 && mevent.Y>=0 && mevent.X<this.client_size.Width && mevent.Y<=this.client_size.Height) {
463 inside = true;
466 // If the button was pressed and we leave, release the button press and vice versa
467 if (this.Capture && (inside != is_pressed)) {
468 is_pressed = inside;
469 redraw = true;
472 if (is_entered != inside) {
473 is_entered = inside;
474 redraw = true;
477 if (redraw) {
478 Redraw();
481 base.OnMouseMove(mevent);
484 protected override void OnMouseUp(MouseEventArgs mevent) {
485 if (this.Capture && mevent.Button == MouseButtons.Left) {
486 this.Capture = false;
487 if (is_pressed) {
488 is_pressed = false;
489 Redraw();
490 } else if ((this.flat_style == FlatStyle.Flat) || (this.flat_style == FlatStyle.Popup)) {
491 Redraw();
494 if (mevent.X>=0 && mevent.Y>=0 && mevent.X<this.client_size.Width && mevent.Y<=this.client_size.Height) {
495 OnClick(EventArgs.Empty);
498 base.OnMouseUp(mevent);
501 protected override void OnPaint(PaintEventArgs pevent) {
502 Draw (pevent);
503 pevent.Graphics.DrawImage(this.ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
504 base.OnPaint (pevent);
507 protected override void OnParentChanged(EventArgs e) {
508 base.OnParentChanged(e);
511 protected override void OnTextChanged(EventArgs e) {
512 Redraw();
513 base.OnTextChanged(e);
516 protected override void OnVisibleChanged(EventArgs e) {
517 if (!Visible) {
518 is_pressed = false;
519 has_focus = false;
520 is_entered = false;
522 base.OnVisibleChanged(e);
525 protected void ResetFlagsandPaint() {
526 // Nothing to do; MS internal
527 // Should we do Redraw (); ?
530 protected override void WndProc(ref Message m) {
531 base.WndProc(ref m);
533 #endregion // Public Instance Properties