[jit] Enable partial generic sharing when not using AOT as an experiment.
[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 using System;
30 using System.Drawing;
31 using System.ComponentModel;
32 using System.Windows.Forms.Design;
33
34 namespace System.Windows.Forms
35 {
36         [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip)]
37         public class ToolStripButton : ToolStripItem
38         {
39                 private CheckState checked_state;
40                 private bool check_on_click;
41
42                 #region Public Constructors
43                 public ToolStripButton ()
44                         : this (null, null, null, String.Empty)
45                 {
46                 }
47
48                 public ToolStripButton (Image image)
49                         : this (null, image, null, String.Empty)
50                 {
51                 }
52
53                 public ToolStripButton (string text)
54                         : this (text, null, null, String.Empty)
55                 {
56                 }
57
58                 public ToolStripButton (string text, Image image)
59                         : this (text, image, null, String.Empty)
60                 {
61                 }
62
63                 public ToolStripButton (string text, Image image, EventHandler onClick)
64                         : this (text, image, onClick, String.Empty)
65                 {
66                 }
67
68                 public ToolStripButton (string text, Image image, EventHandler onClick, string name)
69                         : base (text, image, onClick, name)
70                 {
71                         this.checked_state = CheckState.Unchecked;
72                         this.ToolTipText = String.Empty;
73                 }
74                 #endregion
75
76                 #region Public Properties
77                 [DefaultValue (true)]
78                 public new bool AutoToolTip {
79                         get { return base.AutoToolTip; }
80                         set { base.AutoToolTip = value; }
81                 }
82
83                 public override bool CanSelect {
84                         get { return true; }
85                 }
86
87                 [DefaultValue (false)]
88                 public bool Checked {
89                         get {
90                                 switch (this.checked_state) {
91                                         case CheckState.Unchecked:
92                                         default:
93                                                 return false;
94                                         case CheckState.Checked:
95                                         case CheckState.Indeterminate:
96                                                 return true;
97                                 }
98                         }
99                         set {
100                                 if (this.checked_state != (value ? CheckState.Checked : CheckState.Unchecked)) {
101                                         this.checked_state = value ? CheckState.Checked : CheckState.Unchecked;
102                                         this.OnCheckedChanged (EventArgs.Empty);
103                                         this.OnCheckStateChanged (EventArgs.Empty);
104                                         this.Invalidate ();
105                                 }
106                         }
107                 }
108
109                 [DefaultValue (false)]
110                 public bool CheckOnClick {
111                         get { return this.check_on_click; }
112                         set {
113                                 if (this.check_on_click != value) {
114                                         this.check_on_click = value;
115                                         OnUIACheckOnClickChangedEvent (EventArgs.Empty);
116                                 }
117                         }
118                 }
119
120                 [DefaultValue (CheckState.Unchecked)]
121                 public CheckState CheckState {
122                         get { return this.checked_state; }
123                         set {
124                                 if (this.checked_state != value) {
125                                         if (!Enum.IsDefined (typeof (CheckState), value))
126                                                 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
127
128                                         this.checked_state = value;
129                                         this.OnCheckedChanged (EventArgs.Empty);
130                                         this.OnCheckStateChanged (EventArgs.Empty);
131                                         this.Invalidate ();
132                                 }
133                         }
134                 }
135                 #endregion
136
137                 #region Protected Properties
138                 protected override bool DefaultAutoToolTip { get { return true; } }
139                 #endregion
140
141                 #region Public Methods
142                 public override Size GetPreferredSize (Size constrainingSize)
143                 {
144                         Size retval = base.GetPreferredSize (constrainingSize);
145                         
146                         if (retval.Width < 23)
147                                 retval.Width = 23;
148                                 
149                         return retval;
150                 }
151                 #endregion
152
153                 #region Protected Methods
154                 [EditorBrowsable (EditorBrowsableState.Advanced)]
155                 protected override AccessibleObject CreateAccessibilityInstance ()
156                 {
157                         ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
158
159                         ao.default_action = "Press";
160                         ao.role = AccessibleRole.PushButton;
161                         ao.state = AccessibleStates.Focusable;  
162
163                         return ao;
164                 }
165
166                 protected virtual void OnCheckedChanged (EventArgs e)
167                 {
168                         EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
169                         if (eh != null)
170                                 eh (this, e);
171                 }
172
173                 protected virtual void OnCheckStateChanged (EventArgs e)
174                 {
175                         EventHandler eh = (EventHandler)(Events [CheckStateChangedEvent]);
176                         if (eh != null)
177                                 eh (this, e);
178                 }
179
180                 protected override void OnClick (EventArgs e)
181                 {
182                         if (this.check_on_click)
183                                 this.Checked = !this.Checked;
184
185                         base.OnClick (e);
186
187                         ToolStrip ts = this.GetTopLevelToolStrip ();
188                         
189                         if (ts != null)
190                                 ts.Dismiss (ToolStripDropDownCloseReason.ItemClicked);
191                 }
192
193                 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
194                 {
195                         base.OnPaint (e);
196
197                         if (this.Owner != null) {
198                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
199                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
200
201                                 this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
202
203                                 Rectangle text_layout_rect;
204                                 Rectangle image_layout_rect;
205
206                                 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
207
208                                 if (text_layout_rect != Rectangle.Empty)
209                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
210                                 if (image_layout_rect != Rectangle.Empty)
211                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
212
213                                 return;
214                         }
215                 }
216                 #endregion
217
218                 #region Public Events
219                 static object CheckedChangedEvent = new object ();
220                 static object CheckStateChangedEvent = new object ();
221
222                 public event EventHandler CheckedChanged {
223                         add { Events.AddHandler (CheckedChangedEvent, value); }
224                         remove { Events.RemoveHandler (CheckedChangedEvent, value); }
225                 }
226
227                 public event EventHandler CheckStateChanged {
228                         add { Events.AddHandler (CheckStateChangedEvent, value); }
229                         remove { Events.RemoveHandler (CheckStateChangedEvent, value); }
230                 }
231                 #endregion
232
233                 #region UIA Framework Events
234                 static object UIACheckOnClickChangedEvent = new object ();
235                 
236                 internal event EventHandler UIACheckOnClickChanged {
237                         add { Events.AddHandler (UIACheckOnClickChangedEvent, value); }
238                         remove { Events.RemoveHandler (UIACheckOnClickChangedEvent, value); }
239                 }
240
241                 internal void OnUIACheckOnClickChangedEvent (EventArgs args)
242                 {
243                         EventHandler eh
244                                 = (EventHandler) Events [UIACheckOnClickChangedEvent];
245                         if (eh != null)
246                                 eh (this, args);
247                 }
248                 #endregion
249         }
250 }