summaryrefslogtreecommitdiff
path: root/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ToolStripMenuItem.cs
blob: 4df33979088fc1fc0fcd884ff0a01ec3403f49a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//
// ToolStripMenuItem.cs
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2006 Jonathan Pobst
//
// Authors:
//	Jonathan Pobst (monkey@jpobst.com)
//

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.ComponentModel.Design.Serialization;

namespace System.Windows.Forms
{
	[ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.MenuStrip | ToolStripItemDesignerAvailability.ContextMenuStrip)]
	[DesignerSerializer ("System.Windows.Forms.Design.ToolStripMenuItemCodeDomSerializer, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
	public class ToolStripMenuItem : ToolStripDropDownItem
	{
		private CheckState checked_state;
		private bool check_on_click;
		private bool close_on_mouse_release;
		private string shortcut_display_string;
		private Keys shortcut_keys = Keys.None;
		private bool show_shortcut_keys = true;
		private Form mdi_client_form;

		#region Public Constructors
		public ToolStripMenuItem ()
			: this (null, null, null, string.Empty)
		{
		}

		public ToolStripMenuItem (Image image)
			: this (null, image, null, string.Empty)
		{
		}

		public ToolStripMenuItem (string text)
			: this (text, null, null, string.Empty)
		{
		}

		public ToolStripMenuItem (string text, Image image)
			: this (text, image, null, string.Empty)
		{
		}

		public ToolStripMenuItem (string text, Image image, EventHandler onClick)
			: this (text, image, onClick, string.Empty)
		{
		}

		public ToolStripMenuItem (string text, Image image, params ToolStripItem[] dropDownItems)
			: this (text, image, null, string.Empty)
		{
			if (dropDownItems != null)
				foreach (ToolStripItem tsi in dropDownItems)
					this.DropDownItems.Add (tsi);
		}

		public ToolStripMenuItem (string text, Image image, EventHandler onClick, Keys shortcutKeys)
			: this (text, image, onClick, string.Empty)
		{
		}

		public ToolStripMenuItem (string text, Image image, EventHandler onClick, string name)
			: base (text, image, onClick, name)
		{
			base.Overflow = ToolStripItemOverflow.Never;
		}
		#endregion

		#region Public Properties
		[Bindable (true)]
		[DefaultValue (false)]
		[RefreshProperties (RefreshProperties.All)]
		public bool Checked {
			get {
				switch (this.checked_state) {
					case CheckState.Unchecked:
					default:
						return false;
					case CheckState.Checked:
					case CheckState.Indeterminate:
						return true;
				}
			}
			set {
				CheckState = value ? CheckState.Checked : CheckState.Unchecked;
			}
		}

		[DefaultValue (false)]
		public bool CheckOnClick {
			get { return this.check_on_click; }
			set {
				if (this.check_on_click != value) {
					this.check_on_click = value;
					OnUIACheckOnClickChangedEvent (EventArgs.Empty);
				}
			}
		}

		[Bindable (true)]
		[DefaultValue (CheckState.Unchecked)]
		[RefreshProperties (RefreshProperties.All)]
		public CheckState CheckState {
			get { return this.checked_state; }
			set
			{
				if (!Enum.IsDefined (typeof (CheckState), value))
					throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));

				if (value == checked_state)
					return;

				this.checked_state = value;
				this.Invalidate ();
				this.OnCheckedChanged (EventArgs.Empty);
				this.OnCheckStateChanged (EventArgs.Empty);
			}
		}

		public override bool Enabled {
			get { return base.Enabled; }
			set { base.Enabled = value; }
		}

		[Browsable (false)]
		public bool IsMdiWindowListEntry {
			get { return this.mdi_client_form != null; }
		}
		
		[DefaultValue (ToolStripItemOverflow.Never)]
		public new ToolStripItemOverflow Overflow {
			get { return base.Overflow; }
			set { base.Overflow = value; }
		}
		
		[Localizable (true)]
		[DefaultValue (true)]
		public bool ShowShortcutKeys {
			get { return this.show_shortcut_keys; }
			set { this.show_shortcut_keys = value; }
		}
		
		[Localizable (true)]
		[DefaultValue (null)]
		public string ShortcutKeyDisplayString {
			get { return this.shortcut_display_string; }
			set { this.shortcut_display_string = value; }
		}
		
		[Localizable (true)]
		[DefaultValue (Keys.None)]
		public Keys ShortcutKeys {
			get { return this.shortcut_keys; }
			set { 
				if (this.shortcut_keys != value) {
					this.shortcut_keys = value;
					
					if (this.Parent != null)
						ToolStripManager.AddToolStripMenuItem (this);
				}
			 }
		}
		#endregion

		#region Protected Properties
		protected internal override Padding DefaultMargin {
			get { return new Padding (0); }
		}

		protected override Padding DefaultPadding {
			get { return new Padding (4, 0, 4, 0); }
		}

		protected override Size DefaultSize {
			get { return new Size (32, 19); }
		}
		#endregion

		#region Protected Methods
		[EditorBrowsable (EditorBrowsableState.Advanced)]
		protected override AccessibleObject CreateAccessibilityInstance ()
		{
			return new ToolStripMenuItemAccessibleObject ();
		}
		
		protected override ToolStripDropDown CreateDefaultDropDown ()
		{
			ToolStripDropDownMenu tsddm = new ToolStripDropDownMenu ();
			tsddm.OwnerItem = this;
			return tsddm;
		}

		protected override void Dispose (bool disposing)
		{
			base.Dispose (disposing);
		}

		protected virtual void OnCheckedChanged (EventArgs e)
		{
			EventHandler eh = (EventHandler)Events [CheckedChangedEvent];
			if (eh != null)
				eh (this, e);
		}

		protected virtual void OnCheckStateChanged (EventArgs e)
		{
			EventHandler eh = (EventHandler)Events [CheckStateChangedEvent];
			if (eh != null)
				eh (this, e);
		}

		protected override void OnClick (EventArgs e)
		{
			if (!this.Enabled)
				return;
				
			if (this.HasDropDownItems) {
				base.OnClick (e);
				return;
			}
				
			if (this.OwnerItem is ToolStripDropDownItem)
				(this.OwnerItem as ToolStripDropDownItem).OnDropDownItemClicked (new ToolStripItemClickedEventArgs (this));

			if (this.IsOnDropDown) {
				ToolStrip ts = this.GetTopLevelToolStrip ();
				
				if (ts != null)
					ts.Dismiss (ToolStripDropDownCloseReason.ItemClicked);
			}

			if (this.IsMdiWindowListEntry) {
				this.mdi_client_form.MdiParent.MdiContainer.ActivateChild (this.mdi_client_form);
				return;
			}
			
			if (this.check_on_click)
				this.Checked = !this.Checked;

			base.OnClick (e);
			
			if (!this.IsOnDropDown && !this.HasDropDownItems) {
				ToolStrip ts = this.GetTopLevelToolStrip ();

				if (ts != null)
					ts.Dismiss (ToolStripDropDownCloseReason.ItemClicked);
			}
		}

		protected override void OnDropDownHide (EventArgs e)
		{
			base.OnDropDownHide (e);
		}

		protected override void OnDropDownShow (EventArgs e)
		{
			base.OnDropDownShow (e);
		}

		protected override void OnFontChanged (EventArgs e)
		{
			base.OnFontChanged (e);
		}

		protected override void OnMouseDown (MouseEventArgs e)
		{
			if (!this.IsOnDropDown && this.HasDropDownItems && this.DropDown.Visible)
				this.close_on_mouse_release = true;
				
			if (Enabled && !this.DropDown.Visible)
				this.ShowDropDown ();

			base.OnMouseDown (e);
		}

		protected override void OnMouseEnter (EventArgs e)
		{
			if (this.IsOnDropDown && this.HasDropDownItems && Enabled)
				this.ShowDropDown ();

			base.OnMouseEnter (e);
		}

		protected override void OnMouseLeave (EventArgs e)
		{
			base.OnMouseLeave (e);
		}

		protected override void OnMouseUp (MouseEventArgs e)
		{
			if (this.close_on_mouse_release) {
				this.Parent.Dismiss (ToolStripDropDownCloseReason.ItemClicked);
				this.Invalidate ();
				this.close_on_mouse_release = false;
			}
				
			if (!this.HasDropDownItems && Enabled)
				base.OnMouseUp (e);
		}

		protected override void OnOwnerChanged (EventArgs e)
		{
			base.OnOwnerChanged (e);
		}

		protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
		{
			base.OnPaint (e);

			// Can't render without an owner
			if (this.Owner == null)
				return;
				
			// If DropDown.ShowImageMargin is false, we don't display the image
			Image draw_image = this.UseImageMargin ? this.Image : null;
			
			// Disable this color detection until we do the color detection for ToolStrip *completely*
			// Color font_color = this.ForeColor == SystemColors.ControlText ? SystemColors.MenuText : this.ForeColor;
			Color font_color = ForeColor;
			
			if ((this.Selected || this.Pressed) && this.IsOnDropDown && font_color == SystemColors.MenuText)
				font_color = SystemColors.HighlightText;
			
			if (!this.Enabled && this.ForeColor == SystemColors.ControlText)
				font_color = SystemColors.GrayText;
			
			// Gray stuff out if we're disabled
			draw_image = this.Enabled ? draw_image : ToolStripRenderer.CreateDisabledImage (draw_image);
				
			// Draw our background
			this.Owner.Renderer.DrawMenuItemBackground (new ToolStripItemRenderEventArgs (e.Graphics, this));

			// Figure out where our text and image go
			Rectangle text_layout_rect;
			Rectangle image_layout_rect;

			this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);

			if (this.IsOnDropDown) {
				if (!this.UseImageMargin) {
					image_layout_rect = Rectangle.Empty;
					text_layout_rect = new Rectangle (8, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
				} else {
					text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
				
					if (image_layout_rect != Rectangle.Empty)
						image_layout_rect = new Rectangle (new Point (4, 3), base.GetImageSize ());
				}

				if (this.Checked && this.ShowMargin)
					this.Owner.Renderer.DrawItemCheck (new ToolStripItemImageRenderEventArgs (e.Graphics, this, new Rectangle (2, 1, 19, 19)));
			}
			if (text_layout_rect != Rectangle.Empty)
				this.Owner.Renderer.DrawItemText (new ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));

			string key_string = GetShortcutDisplayString ();
			
			if (!string.IsNullOrEmpty (key_string) && !this.HasDropDownItems) {
				int offset = 15;
				Size key_string_size = TextRenderer.MeasureText (key_string, this.Font);
				Rectangle key_string_rect = new Rectangle (this.ContentRectangle.Right - key_string_size.Width - offset, text_layout_rect.Top, key_string_size.Width, text_layout_rect.Height);
				this.Owner.Renderer.DrawItemText (new ToolStripItemTextRenderEventArgs (e.Graphics, this, key_string, key_string_rect, font_color, this.Font, this.TextAlign));
			}
				
			if (image_layout_rect != Rectangle.Empty)
				this.Owner.Renderer.DrawItemImage (new ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));

			if (this.IsOnDropDown && this.HasDropDownItems && this.Parent is ToolStripDropDownMenu)
				this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Bounds.Width - 17, 2, 10, 20), Color.Black, ArrowDirection.Right));
			
			return;
		}

		protected internal override bool ProcessCmdKey (ref Message m, Keys keyData)
		{
			Control source = Control.FromHandle (m.HWnd);
			Form f = source == null ? null : (Form)source.TopLevelControl;

			if (this.Enabled && keyData == this.shortcut_keys && GetTopLevelControl () == f) {
				this.FireEvent (EventArgs.Empty, ToolStripItemEventType.Click);
				return true;
			}
				
			return base.ProcessCmdKey (ref m, keyData);
		}

		Control GetTopLevelControl ()
		{
			ToolStripItem item = this;
			while (item.OwnerItem != null)
				item = item.OwnerItem;

			if (item.Owner == null)
				return null;

			if (item.Owner is ContextMenuStrip) {
				Control container = ((ContextMenuStrip)item.Owner).container;
				return container == null ? null : container.TopLevelControl;
			}

			// MainMenuStrip
			return item.Owner.TopLevelControl;
		}

		protected internal override bool ProcessMnemonic (char charCode)
		{
			if (!this.Selected)
				this.Parent.ChangeSelection (this);
				
			if (this.HasDropDownItems) {
				ToolStripManager.SetActiveToolStrip (this.Parent, true);
				this.ShowDropDown ();
				this.DropDown.SelectNextToolStripItem (null, true);
			} else
				this.PerformClick ();
			
			return true;
		}
		
		protected internal override void SetBounds (Rectangle rect)
		{
			base.SetBounds (rect);
		}
		#endregion

		#region Public Events
		static object CheckedChangedEvent = new object ();
		static object CheckStateChangedEvent = new object ();

		public event EventHandler CheckedChanged {
			add { Events.AddHandler (CheckedChangedEvent, value); }
			remove {Events.RemoveHandler (CheckedChangedEvent, value); }
		}

		public event EventHandler CheckStateChanged {
			add { Events.AddHandler (CheckStateChangedEvent, value); }
			remove {Events.RemoveHandler (CheckStateChangedEvent, value); }
		}
		#endregion

		#region UIA Framework Events
		static object UIACheckOnClickChangedEvent = new object ();
		
		internal event EventHandler UIACheckOnClickChanged {
			add { Events.AddHandler (UIACheckOnClickChangedEvent, value); }
			remove { Events.RemoveHandler (UIACheckOnClickChangedEvent, value); }
		}

		internal void OnUIACheckOnClickChangedEvent (EventArgs args)
		{
			EventHandler eh
				= (EventHandler) Events [UIACheckOnClickChangedEvent];
			if (eh != null)
				eh (this, args);
		}
		#endregion

		#region Internal Properties
		internal Form MdiClientForm {
			get { return this.mdi_client_form; }
			set { this.mdi_client_form = value; }
		}
		#endregion

		#region Internal Methods
		internal override Size CalculatePreferredSize (Size constrainingSize)
		{
			Size base_size = base.CalculatePreferredSize (constrainingSize);
			
			string key_string = GetShortcutDisplayString ();
			
			if (string.IsNullOrEmpty (key_string))
				return base_size;
			
			Size text_size = TextRenderer.MeasureText (key_string, this.Font);
			
			return new Size (base_size.Width + text_size.Width - 25, base_size.Height);
		}
		
		internal string GetShortcutDisplayString ()
		{
			if (this.show_shortcut_keys == false)
				return string.Empty;
			if (this.Parent == null || !(this.Parent is ToolStripDropDownMenu))
				return string.Empty;
				
			string key_string = string.Empty;

			if (!string.IsNullOrEmpty (this.shortcut_display_string))
				key_string = this.shortcut_display_string;
			else if (this.shortcut_keys != Keys.None) {
				KeysConverter kc = new KeysConverter ();
				key_string = kc.ConvertToString (this.shortcut_keys);
			}
			
			return key_string;
		}
		
		internal void HandleAutoExpansion ()
		{
			if (this.HasDropDownItems) {
				this.ShowDropDown ();
				this.DropDown.SelectNextToolStripItem (null, true);
			}
		}

		internal override void HandleClick (int mouse_clicks, EventArgs e)
		{
			this.OnClick (e);
			
			if (Parent != null)
				Parent.Invalidate ();
		}
		#endregion

		#region ToolStripMenuItemAccessibleObject
		private class ToolStripMenuItemAccessibleObject : AccessibleObject
		{
		}
		#endregion
	}
}