1eadd46de969a7ebb9c00480d07b1916028537d4
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripMenuItem.cs
1 //
2 // ToolStripMenuItem.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 #if NET_2_0
29
30 using System;
31 using System.Drawing;
32 using System.ComponentModel;
33
34 namespace System.Windows.Forms
35 {
36         public class ToolStripMenuItem : ToolStripDropDownItem
37         {
38                 private CheckState checked_state;
39                 private bool check_on_click;
40
41                 #region Public Constructors
42                 public ToolStripMenuItem ()
43                         : base ()
44                 {
45                 }
46
47                 public ToolStripMenuItem (Image image)
48                         : base (string.Empty, image, null, string.Empty)
49                 {
50                 }
51
52                 public ToolStripMenuItem (string text)
53                         : base (text, null, null, string.Empty)
54                 {
55                 }
56
57                 public ToolStripMenuItem (string text, Image image)
58                         : base (text, image, null, string.Empty)
59                 {
60                 }
61
62                 public ToolStripMenuItem (string text, Image image, EventHandler onClick)
63                         : base (text, image, onClick, string.Empty)
64                 {
65                 }
66
67                 public ToolStripMenuItem (string text, Image image, params ToolStripItem[] dropDownItems)
68                         : base (text, image, null, string.Empty)
69                 {
70                         if (dropDownItems != null)
71                                 foreach (ToolStripItem tsi in dropDownItems)
72                                         this.DropDownItems.Add (tsi);
73                 }
74
75                 public ToolStripMenuItem (string text, Image image, EventHandler onClick, Keys shortcutKeys)
76                         : base (text, image, onClick, string.Empty)
77                 {
78                 }
79
80                 public ToolStripMenuItem (string text, Image image, EventHandler onClick, string name)
81                         : base (text, image, onClick, name)
82                 {
83                 }
84                 #endregion
85
86                 #region Public Properties
87                 [Bindable (true)]
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.Invalidate ();
105                                 }
106                         }
107                 }
108
109                 [DefaultValue (false)]
110                 public bool CheckOnClick {
111                         get { return this.check_on_click; }
112                         set { this.check_on_click = value; }
113                 }
114
115                 [Bindable (true)]
116                 [DefaultValue (CheckState.Unchecked)]
117                 public CheckState CheckState
118                 {
119                         get { return this.checked_state; }
120                         set
121                         {
122                                 if (!Enum.IsDefined (typeof (CheckState), value))
123                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
124
125                                 this.checked_state = value;
126                                 OnCheckStateChanged (EventArgs.Empty);
127                                 this.Invalidate ();
128                         }
129                 }
130
131                 public override bool Enabled {
132                         get { return base.Enabled; }
133                         set { base.Enabled = value; }
134                 }
135                 #endregion
136
137                 #region Protected Properties
138                 protected internal override Padding DefaultMargin {
139                         get { return new Padding (0); }
140                 }
141
142                 protected override Padding DefaultPadding {
143                         get { return new Padding (4, 0, 4, 0); }
144                 }
145
146                 protected override Size DefaultSize {
147                         get { return new Size (32, 19); }
148                 }
149                 #endregion
150
151                 #region Protected Methods
152                 protected override ToolStripDropDown CreateDefaultDropDown ()
153                 {
154                         return base.CreateDefaultDropDown ();
155                 }
156
157                 protected override void Dispose (bool disposing)
158                 {
159                         base.Dispose (disposing);
160                 }
161
162                 protected virtual void OnCheckedChanged (EventArgs e)
163                 {
164                         if (CheckedChanged != null) CheckedChanged (this, e);
165                 }
166
167                 protected virtual void OnCheckStateChanged (EventArgs e)
168                 {
169                         if (CheckStateChanged != null) CheckStateChanged (this, e);
170                 }
171
172                 protected override void OnClick (EventArgs e)
173                 {
174                         if (!this.Enabled)
175                                 return;
176                                 
177                         base.OnClick (e);
178
179                         if (this.IsOnDropDown) {
180                                 if (this.HasDropDownItems)
181                                         return;
182
183                                 this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
184                                 (this.Parent as ToolStripDropDown).Close (ToolStripDropDownCloseReason.ItemClicked);
185
186                                 Object parent = this.Parent;
187
188                                 // Find the top level MenuStrip to inform it to close all open
189                                 // dropdowns because this one was clicked
190                                 while (parent != null) {
191                                         if (parent is MenuStrip)
192                                                 (parent as MenuStrip).HideMenus (true, ToolStripDropDownCloseReason.ItemClicked);
193
194                                         if (parent is ToolStripDropDown)
195                                                 parent = (parent as ToolStripDropDown).OwnerItem;
196                                         else if (parent is ToolStripItem)
197                                                 parent = (parent as ToolStripItem).Parent;
198                                         else
199                                                 break;
200                                 }
201                         }
202
203                         if (this.check_on_click)
204                                 this.Checked = !this.Checked;
205                 }
206
207                 protected override void OnDropDownHide (EventArgs e)
208                 {
209                         base.OnDropDownHide (e);
210                 }
211
212                 protected override void OnDropDownShow (EventArgs e)
213                 {
214                         base.OnDropDownShow (e);
215                 }
216
217                 protected override void OnFontChanged (EventArgs e)
218                 {
219                         base.OnFontChanged (e);
220                 }
221
222                 protected override void OnMouseDown (MouseEventArgs e)
223                 {
224                         base.OnMouseDown (e);
225
226                         if (this.HasDropDownItems)
227                                 if (!this.DropDown.Visible)
228                                         this.ShowDropDown ();
229                 }
230
231                 protected override void OnMouseEnter (EventArgs e)
232                 {
233                         base.OnMouseEnter (e);
234
235                         if (this.IsOnDropDown && this.HasDropDownItems)
236                                 this.ShowDropDown ();
237                 }
238
239                 protected override void OnMouseLeave (EventArgs e)
240                 {
241                         base.OnMouseLeave (e);
242                 }
243
244                 protected override void OnMouseUp (MouseEventArgs e)
245                 {
246                         base.OnMouseUp (e);
247                 }
248
249                 protected override void OnOwnerChanged (EventArgs e)
250                 {
251                         base.OnOwnerChanged (e);
252                 }
253
254                 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
255                 {
256                         base.OnPaint (e);
257
258                         if (this.Owner != null) {
259                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
260                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
261
262                                 if (this.IsOnDropDown)
263                                         this.Owner.Renderer.DrawMenuItemBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
264                                 else
265                                         this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
266
267                                 Rectangle text_layout_rect;
268                                 Rectangle image_layout_rect;
269
270                                 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
271
272                                 if (this.IsOnDropDown) {
273                                         text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
274                                         if (image_layout_rect != Rectangle.Empty)
275                                                 image_layout_rect = new Rectangle (4, 3, draw_image.Width, draw_image.Height);
276
277                                         if (this.Checked)
278                                                 this.Owner.Renderer.DrawItemCheck (new ToolStripItemImageRenderEventArgs (e.Graphics, this, new Rectangle (2, 1, 19, 19)));
279                                 }
280                                 if (text_layout_rect != Rectangle.Empty)
281                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
282
283                                 if (image_layout_rect != Rectangle.Empty)
284                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
285
286                                 if (this.IsOnDropDown && this.HasDropDownItems)
287                                         this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Bounds.Width - 17, 2, 10, 20), Color.Black, ArrowDirection.Right));
288                                 return;
289                         }
290                 }
291                 #endregion
292
293                 #region Public Events
294                 public event EventHandler CheckedChanged;
295                 public event EventHandler CheckStateChanged;
296                 #endregion
297         }
298 }
299 #endif