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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
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
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
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
74 // Revision 1.5 2004/08/31 18:49:58 pbartok
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
97 using System
.ComponentModel
;
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
{
121 ButtonState ret
= ButtonState
.Normal
;
124 // Popup style is only followed as long as the mouse isn't "in" the control
126 if (flat_style
== FlatStyle
.Flat
) {
127 ret
|= ButtonState
.Flat
;
130 if (flat_style
== FlatStyle
.Flat
|| flat_style
== FlatStyle
.Popup
) {
131 ret
|= ButtonState
.Flat
;
135 if (is_entered
&& is_pressed
) {
136 ret
|= ButtonState
.Pushed
;
139 ret
|= ButtonState
.Inactive
;
140 if ((flat_style
== FlatStyle
.Flat
) || (flat_style
== FlatStyle
.Popup
)) {
141 ret
|= ButtonState
.Flat
;
148 [MonoTODO("Make the FillRectangle use a global brush instead of creating one every time")]
149 internal void Redraw() {
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
) {
158 ThemeEngine
.Current
.DrawButtonBase(this.DeviceContext
, pevent
.ClipRectangle
, this);
163 private void RedrawEvent(object sender
, System
.EventArgs e
) {
167 #endregion // Private Properties and Methods
169 #region Public Constructors
170 protected ButtonBase() : base() {
171 flat_style
= FlatStyle
.Standard
;
175 image_alignment
= ContentAlignment
.MiddleCenter
;
176 text_alignment
= ContentAlignment
.MiddleCenter
;
177 ime_mode
= ImeMode
.Inherit
;
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
{
220 public ContentAlignment ImageAlign
{
222 return image_alignment
;
226 image_alignment
=value;
231 public int ImageIndex
{
233 if (image_list
==null) {
245 public ImageList ImageList
{
251 if (image_list
!= null) {
252 image_list
.Dispose();
260 if (image_list
.Images
.Count
>= image_index
) {
261 image_index
=image_list
.Images
.Count
-1;
268 public ImeMode ImeMode
{
278 public virtual ContentAlignment TextAlign
{
280 return text_alignment
;
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
;
293 case ContentAlignment
.TopCenter
: {
294 text_format
.Alignment
=StringAlignment
.Center
;
295 text_format
.LineAlignment
=StringAlignment
.Near
;
299 case ContentAlignment
.TopRight
: {
300 text_format
.Alignment
=StringAlignment
.Far
;
301 text_format
.LineAlignment
=StringAlignment
.Near
;
305 case ContentAlignment
.MiddleLeft
: {
306 text_format
.Alignment
=StringAlignment
.Near
;
307 text_format
.LineAlignment
=StringAlignment
.Center
;
311 case ContentAlignment
.MiddleCenter
: {
312 text_format
.Alignment
=StringAlignment
.Center
;
313 text_format
.LineAlignment
=StringAlignment
.Center
;
317 case ContentAlignment
.MiddleRight
: {
318 text_format
.Alignment
=StringAlignment
.Far
;
319 text_format
.LineAlignment
=StringAlignment
.Center
;
323 case ContentAlignment
.BottomLeft
: {
324 text_format
.Alignment
=StringAlignment
.Near
;
325 text_format
.LineAlignment
=StringAlignment
.Far
;
329 case ContentAlignment
.BottomCenter
: {
330 text_format
.Alignment
=StringAlignment
.Center
;
331 text_format
.LineAlignment
=StringAlignment
.Far
;
335 case ContentAlignment
.BottomRight
: {
336 text_format
.Alignment
=StringAlignment
.Far
;
337 text_format
.LineAlignment
=StringAlignment
.Far
;
346 #endregion // Public Instance Properties
348 #region Protected Instance Properties
349 protected override CreateParams CreateParams
{
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);
363 protected override ImeMode DefaultImeMode
{
365 return ImeMode
.Inherit
;
369 protected override Size DefaultSize
{
371 return ThemeEngine
.Current
.ButtonBaseDefaultSize
;
375 protected bool IsDefault
{
381 if (is_default
!= value) {
387 #endregion // Public Instance Properties
389 #region Protected Instance Methods
390 [MonoTODO("Finish setting properties of the AccessibleObject")]
391 protected override AccessibleObject
CreateAccessibilityInstance() {
393 ao
=base.CreateAccessibilityInstance();
394 ao
.description
="Button";
399 protected override void Dispose(bool Disposing
) {
400 base.Dispose(Disposing
);
403 protected override void OnEnabledChanged(EventArgs e
) {
405 base.OnEnabledChanged(e
);
408 protected override void OnGotFocus(EventArgs e
) {
414 protected override void OnKeyDown(KeyEventArgs kevent
) {
415 if (is_enabled
&& (kevent
.KeyData
== Keys
.Enter
|| kevent
.KeyData
== Keys
.Space
)) {
416 OnClick(EventArgs
.Empty
);
419 base.OnKeyDown(kevent
);
422 protected override void OnKeyUp(KeyEventArgs kevent
) {
423 base.OnKeyUp(kevent
);
426 protected override void OnLostFocus(EventArgs e
) {
432 protected override void OnMouseDown(MouseEventArgs mevent
) {
433 if (is_enabled
&& (mevent
.Button
== MouseButtons
.Left
)) {
439 base.OnMouseDown(mevent
);
442 protected override void OnMouseEnter(EventArgs e
) {
444 if ((this.flat_style
== FlatStyle
.Flat
) || (this.flat_style
== FlatStyle
.Popup
)) {
447 base.OnMouseEnter(e
);
450 protected override void OnMouseLeave(EventArgs e
) {
452 if ((this.flat_style
== FlatStyle
.Flat
) || (this.flat_style
== FlatStyle
.Popup
)) {
455 base.OnMouseLeave(e
);
458 protected override void OnMouseMove(MouseEventArgs mevent
) {
462 if (mevent
.X
>=0 && mevent
.Y
>=0 && mevent
.X
<this.client_size
.Width
&& mevent
.Y
<=this.client_size
.Height
) {
466 // If the button was pressed and we leave, release the button press and vice versa
467 if (this.Capture
&& (inside
!= is_pressed
)) {
472 if (is_entered
!= inside
) {
481 base.OnMouseMove(mevent
);
484 protected override void OnMouseUp(MouseEventArgs mevent
) {
485 if (this.Capture
&& mevent
.Button
== MouseButtons
.Left
) {
486 this.Capture
= false;
490 } else if ((this.flat_style
== FlatStyle
.Flat
) || (this.flat_style
== FlatStyle
.Popup
)) {
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
) {
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
) {
513 base.OnTextChanged(e
);
516 protected override void OnVisibleChanged(EventArgs e
) {
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
) {
533 #endregion // Public Instance Properties