fixed tests
[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 using System.Windows.Forms.Design;
34
35 namespace System.Windows.Forms
36 {
37         [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.MenuStrip | ToolStripItemDesignerAvailability.ContextMenuStrip)]
38         public class ToolStripMenuItem : ToolStripDropDownItem
39         {
40                 private CheckState checked_state;
41                 private bool check_on_click;
42                 private string shortcut_display_string;
43                 private Keys shortcut_keys = Keys.None;
44                 private bool show_shortcut_keys = true;
45                 private Form mdi_client_form;
46
47                 #region Public Constructors
48                 public ToolStripMenuItem ()
49                         : base ()
50                 {
51                 }
52
53                 public ToolStripMenuItem (Image image)
54                         : base (string.Empty, image, null, string.Empty)
55                 {
56                 }
57
58                 public ToolStripMenuItem (string text)
59                         : base (text, null, null, string.Empty)
60                 {
61                 }
62
63                 public ToolStripMenuItem (string text, Image image)
64                         : base (text, image, null, string.Empty)
65                 {
66                 }
67
68                 public ToolStripMenuItem (string text, Image image, EventHandler onClick)
69                         : base (text, image, onClick, string.Empty)
70                 {
71                 }
72
73                 public ToolStripMenuItem (string text, Image image, params ToolStripItem[] dropDownItems)
74                         : base (text, image, null, string.Empty)
75                 {
76                         if (dropDownItems != null)
77                                 foreach (ToolStripItem tsi in dropDownItems)
78                                         this.DropDownItems.Add (tsi);
79                 }
80
81                 public ToolStripMenuItem (string text, Image image, EventHandler onClick, Keys shortcutKeys)
82                         : base (text, image, onClick, string.Empty)
83                 {
84                 }
85
86                 public ToolStripMenuItem (string text, Image image, EventHandler onClick, string name)
87                         : base (text, image, onClick, name)
88                 {
89                 }
90                 #endregion
91
92                 #region Public Properties
93                 [Bindable (true)]
94                 [DefaultValue (false)]
95                 [RefreshProperties (RefreshProperties.Repaint)]
96                 public bool Checked {
97                         get {
98                                 switch (this.checked_state) {
99                                         case CheckState.Unchecked:
100                                         default:
101                                                 return false;
102                                         case CheckState.Checked:
103                                         case CheckState.Indeterminate:
104                                                 return true;
105                                 }
106                         }
107                         set {
108                                 if (this.checked_state != (value ? CheckState.Checked : CheckState.Unchecked)) {
109                                         this.checked_state = value ? CheckState.Checked : CheckState.Unchecked;
110                                         this.Invalidate ();
111                                         this.OnCheckedChanged (EventArgs.Empty);
112                                 }
113                         }
114                 }
115
116                 [DefaultValue (false)]
117                 public bool CheckOnClick {
118                         get { return this.check_on_click; }
119                         set { this.check_on_click = value; }
120                 }
121
122                 [Bindable (true)]
123                 [DefaultValue (CheckState.Unchecked)]
124                 [RefreshProperties (RefreshProperties.Repaint)]
125                 public CheckState CheckState {
126                         get { return this.checked_state; }
127                         set
128                         {
129                                 if (!Enum.IsDefined (typeof (CheckState), value))
130                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
131
132                                 this.checked_state = value;
133                                 this.Invalidate ();
134                                 this.OnCheckStateChanged (EventArgs.Empty);
135                         }
136                 }
137
138                 public override bool Enabled {
139                         get { return base.Enabled; }
140                         set { base.Enabled = value; }
141                 }
142
143                 [Browsable (false)]
144                 public bool IsMdiWindowListEntry {
145                         get { return this.mdi_client_form != null; }
146                 }
147                 
148                 [MonoTODO ("Renderer doesn't support shortcut keys yet, they will never show.")]
149                 [Localizable (true)]
150                 public bool ShowShortcutKeys {
151                         get { return this.show_shortcut_keys; }
152                         set { this.show_shortcut_keys = value; }
153                 }
154                 
155                 [MonoTODO ("Keyboard navigation not implemented.")]
156                 [Localizable (true)]
157                 public string ShortcutKeyDisplayString {
158                         get { return this.shortcut_display_string; }
159                         set { this.shortcut_display_string = value; }
160                 }
161                 
162                 [MonoTODO ("Keyboard navigation not implemented.")]
163                 [Localizable (true)]
164                 public Keys ShortcutKeys {
165                         get { return this.shortcut_keys; }
166                         set { this.shortcut_keys = value; }
167                 }
168                 #endregion
169
170                 #region Protected Properties
171                 protected internal override Padding DefaultMargin {
172                         get { return new Padding (0); }
173                 }
174
175                 protected override Padding DefaultPadding {
176                         get { return new Padding (4, 0, 4, 0); }
177                 }
178
179                 protected override Size DefaultSize {
180                         get { return new Size (32, 19); }
181                 }
182                 #endregion
183
184                 #region Protected Methods
185                 protected override ToolStripDropDown CreateDefaultDropDown ()
186                 {
187                         ToolStripDropDownMenu tsddm = new ToolStripDropDownMenu ();
188                         tsddm.OwnerItem = this;
189                         return tsddm;
190                 }
191
192                 protected override void Dispose (bool disposing)
193                 {
194                         base.Dispose (disposing);
195                 }
196
197                 protected virtual void OnCheckedChanged (EventArgs e)
198                 {
199                         EventHandler eh = (EventHandler)Events [CheckedChangedEvent];
200                         if (eh != null)
201                                 eh (this, e);
202                 }
203
204                 protected virtual void OnCheckStateChanged (EventArgs e)
205                 {
206                         EventHandler eh = (EventHandler)Events [CheckStateChangedEvent];
207                         if (eh != null)
208                                 eh (this, e);
209                 }
210
211                 protected override void OnClick (EventArgs e)
212                 {
213                         if (!this.Enabled)
214                                 return;
215                                 
216                         if (this.IsOnDropDown) {
217                                 if (this.HasDropDownItems)
218                                         return;
219
220                                 this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
221                                 
222                                 (this.Parent as ToolStripDropDown).Close (ToolStripDropDownCloseReason.ItemClicked);
223                                 ToolStripManager.FireAppFocusChanged (this.Parent);
224
225                                 Object parent = this.Parent;
226
227                                 // Find the top level MenuStrip to inform it to close all open
228                                 // dropdowns because this one was clicked
229                                 while (parent != null) {
230                                         if (parent is MenuStrip)
231                                                 (parent as MenuStrip).HideMenus (true, ToolStripDropDownCloseReason.ItemClicked);
232
233                                         if (parent is ToolStripDropDown)
234                                                 parent = (parent as ToolStripDropDown).OwnerItem;
235                                         else if (parent is ToolStripItem)
236                                                 parent = (parent as ToolStripItem).Parent;
237                                         else
238                                                 break;
239                                 }
240                         }
241
242                         if (this.IsMdiWindowListEntry) {
243                                 this.mdi_client_form.MdiParent.MdiContainer.ActivateChild (this.mdi_client_form);
244                                 return;
245                         }
246                         
247                         if (this.check_on_click)
248                                 this.Checked = !this.Checked;
249
250                         base.OnClick (e);
251                 }
252
253                 protected override void OnDropDownHide (EventArgs e)
254                 {
255                         base.OnDropDownHide (e);
256                 }
257
258                 protected override void OnDropDownShow (EventArgs e)
259                 {
260                         base.OnDropDownShow (e);
261                 }
262
263                 protected override void OnFontChanged (EventArgs e)
264                 {
265                         base.OnFontChanged (e);
266                 }
267
268                 protected override void OnMouseDown (MouseEventArgs e)
269                 {
270                         if (this.HasDropDownItems && Enabled)
271                                 if (!this.DropDown.Visible)
272                                         this.ShowDropDown ();
273
274                         base.OnMouseDown (e);
275                 }
276
277                 protected override void OnMouseEnter (EventArgs e)
278                 {
279                         if (this.IsOnDropDown && this.HasDropDownItems && Enabled)
280                                 this.ShowDropDown ();
281
282                         base.OnMouseEnter (e);
283                 }
284
285                 protected override void OnMouseLeave (EventArgs e)
286                 {
287                         base.OnMouseLeave (e);
288                 }
289
290                 protected override void OnMouseUp (MouseEventArgs e)
291                 {
292                         if (!this.HasDropDownItems && Enabled)
293                                 base.OnMouseUp (e);
294                 }
295
296                 protected override void OnOwnerChanged (EventArgs e)
297                 {
298                         base.OnOwnerChanged (e);
299                 }
300
301                 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
302                 {
303                         base.OnPaint (e);
304
305                         if (this.Owner != null) {
306                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
307                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
308
309                                 if (this.IsOnDropDown)
310                                         this.Owner.Renderer.DrawMenuItemBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
311                                 else
312                                         this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
313
314                                 Rectangle text_layout_rect;
315                                 Rectangle image_layout_rect;
316
317                                 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
318
319                                 if (this.IsOnDropDown) {
320                                         text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
321                                         if (image_layout_rect != Rectangle.Empty)
322                                                 image_layout_rect = new Rectangle (4, 3, draw_image.Width, draw_image.Height);
323
324                                         if (this.Checked)
325                                                 this.Owner.Renderer.DrawItemCheck (new ToolStripItemImageRenderEventArgs (e.Graphics, this, new Rectangle (2, 1, 19, 19)));
326                                 }
327                                 if (text_layout_rect != Rectangle.Empty)
328                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
329
330                                 if (image_layout_rect != Rectangle.Empty)
331                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
332
333                                 if (this.IsOnDropDown && this.HasDropDownItems)
334                                         this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Bounds.Width - 17, 2, 10, 20), Color.Black, ArrowDirection.Right));
335                                 return;
336                         }
337                 }
338
339                 protected internal override void SetBounds (Rectangle bounds)
340                 {
341                         base.SetBounds (bounds);
342                 }
343                 #endregion
344
345                 #region Public Events
346                 static object CheckedChangedEvent = new object ();
347                 static object CheckStateChangedEvent = new object ();
348
349                 public event EventHandler CheckedChanged {
350                         add { Events.AddHandler (CheckedChangedEvent, value); }
351                         remove {Events.RemoveHandler (CheckedChangedEvent, value); }
352                 }
353
354                 public event EventHandler CheckStateChanged {
355                         add { Events.AddHandler (CheckStateChangedEvent, value); }
356                         remove {Events.RemoveHandler (CheckStateChangedEvent, value); }
357                 }
358                 #endregion
359
360                 #region Internal Properties
361                 internal Form MdiClientForm {
362                         get { return this.mdi_client_form; }
363                         set { this.mdi_client_form = value; }
364                 }
365                 #endregion
366         }
367 }
368 #endif