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