* TabControl.cs: Show the tooltip depending on the value
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripButton.cs
1 //
2 // ToolStripButton.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28
29 #if NET_2_0
30 using System;
31 using System.Drawing;
32 using System.ComponentModel;
33 using System.Windows.Forms.Design;
34
35 namespace System.Windows.Forms
36 {
37         [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip)]
38         public class ToolStripButton : ToolStripItem
39         {
40                 private CheckState checked_state;
41                 private bool check_on_click;
42
43                 #region Public Constructors
44                 public ToolStripButton ()
45                         : this (null, null, null, String.Empty)
46                 {
47                 }
48
49                 public ToolStripButton (Image image)
50                         : this (null, image, null, String.Empty)
51                 {
52                 }
53
54                 public ToolStripButton (string text)
55                         : this (text, null, null, String.Empty)
56                 {
57                 }
58
59                 public ToolStripButton (string text, Image image)
60                         : this (text, image, null, String.Empty)
61                 {
62                 }
63
64                 public ToolStripButton (string text, Image image, EventHandler onClick)
65                         : this (text, image, onClick, String.Empty)
66                 {
67                 }
68
69                 public ToolStripButton (string text, Image image, EventHandler onClick, string name)
70                         : base (text, image, onClick, name)
71                 {
72                         this.checked_state = CheckState.Unchecked;
73                         this.ToolTipText = String.Empty;
74                 }
75                 #endregion
76
77                 #region Public Properties
78                 [DefaultValue (true)]
79                 public new bool AutoToolTip {
80                         get { return base.AutoToolTip; }
81                         set { base.AutoToolTip = value; }
82                 }
83
84                 public override bool CanSelect {
85                         get { return true; }
86                 }
87
88                 [DefaultValue (false)]
89                 public bool Checked {
90                         get {
91                                 switch (this.checked_state) {
92                                         case CheckState.Unchecked:
93                                         default:
94                                                 return false;
95                                         case CheckState.Checked:
96                                         case CheckState.Indeterminate:
97                                                 return true;
98                                 }
99                         }
100                         set {
101                                 if (this.checked_state != (value ? CheckState.Checked : CheckState.Unchecked)) {
102                                         this.checked_state = value ? CheckState.Checked : CheckState.Unchecked;
103                                         this.OnCheckedChanged (EventArgs.Empty);
104                                         this.OnCheckStateChanged (EventArgs.Empty);
105                                         this.Invalidate ();
106                                 }
107                         }
108                 }
109
110                 [DefaultValue (false)]
111                 public bool CheckOnClick {
112                         get { return this.check_on_click; }
113                         set {
114                                 if (this.check_on_click != value) {
115                                         this.check_on_click = value;
116                                         OnUIACheckOnClickChangedEvent (EventArgs.Empty);
117                                 }
118                         }
119                 }
120
121                 [DefaultValue (CheckState.Unchecked)]
122                 public CheckState CheckState {
123                         get { return this.checked_state; }
124                         set {
125                                 if (this.checked_state != value) {
126                                         if (!Enum.IsDefined (typeof (CheckState), value))
127                                                 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
128
129                                         this.checked_state = value;
130                                         this.OnCheckedChanged (EventArgs.Empty);
131                                         this.OnCheckStateChanged (EventArgs.Empty);
132                                         this.Invalidate ();
133                                 }
134                         }
135                 }
136                 #endregion
137
138                 #region Protected Properties
139                 protected override bool DefaultAutoToolTip { get { return true; } }
140                 #endregion
141
142                 #region Public Methods
143                 public override Size GetPreferredSize (Size constrainingSize)
144                 {
145                         Size retval = base.GetPreferredSize (constrainingSize);
146                         
147                         if (retval.Width < 23)
148                                 retval.Width = 23;
149                                 
150                         return retval;
151                 }
152                 #endregion
153
154                 #region Protected Methods
155                 [EditorBrowsable (EditorBrowsableState.Advanced)]
156                 protected override AccessibleObject CreateAccessibilityInstance ()
157                 {
158                         ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
159
160                         ao.default_action = "Press";
161                         ao.role = AccessibleRole.PushButton;
162                         ao.state = AccessibleStates.Focusable;  
163
164                         return ao;
165                 }
166
167                 protected virtual void OnCheckedChanged (EventArgs e)
168                 {
169                         EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
170                         if (eh != null)
171                                 eh (this, e);
172                 }
173
174                 protected virtual void OnCheckStateChanged (EventArgs e)
175                 {
176                         EventHandler eh = (EventHandler)(Events [CheckStateChangedEvent]);
177                         if (eh != null)
178                                 eh (this, e);
179                 }
180
181                 protected override void OnClick (EventArgs e)
182                 {
183                         if (this.check_on_click)
184                                 this.Checked = !this.Checked;
185
186                         base.OnClick (e);
187
188                         ToolStrip ts = this.GetTopLevelToolStrip ();
189                         
190                         if (ts != null)
191                                 ts.Dismiss (ToolStripDropDownCloseReason.ItemClicked);
192                 }
193
194                 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
195                 {
196                         base.OnPaint (e);
197
198                         if (this.Owner != null) {
199                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
200                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
201
202                                 this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
203
204                                 Rectangle text_layout_rect;
205                                 Rectangle image_layout_rect;
206
207                                 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
208
209                                 if (text_layout_rect != Rectangle.Empty)
210                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
211                                 if (image_layout_rect != Rectangle.Empty)
212                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
213
214                                 return;
215                         }
216                 }
217                 #endregion
218
219                 #region Public Events
220                 static object CheckedChangedEvent = new object ();
221                 static object CheckStateChangedEvent = new object ();
222
223                 public event EventHandler CheckedChanged {
224                         add { Events.AddHandler (CheckedChangedEvent, value); }
225                         remove { Events.RemoveHandler (CheckedChangedEvent, value); }
226                 }
227
228                 public event EventHandler CheckStateChanged {
229                         add { Events.AddHandler (CheckStateChangedEvent, value); }
230                         remove { Events.RemoveHandler (CheckStateChangedEvent, value); }
231                 }
232                 #endregion
233
234                 #region UIA Framework Events
235                 static object UIACheckOnClickChangedEvent = new object ();
236                 
237                 internal event EventHandler UIACheckOnClickChanged {
238                         add { Events.AddHandler (UIACheckOnClickChangedEvent, value); }
239                         remove { Events.RemoveHandler (UIACheckOnClickChangedEvent, value); }
240                 }
241
242                 internal void OnUIACheckOnClickChangedEvent (EventArgs args)
243                 {
244                         EventHandler eh
245                                 = (EventHandler) Events [UIACheckOnClickChangedEvent];
246                         if (eh != null)
247                                 eh (this, args);
248                 }
249                 #endregion
250         }
251 }
252 #endif