File: Menu.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (631 lines) | stat: -rw-r--r-- 14,706 bytes parent folder | download | duplicates (9)
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// 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) 2004-2005 Novell, Inc.
//
// Authors:
//	Jordi Mas i Hernandez, jordi@ximian.com
//
//	TODO:
//		- FindMenuItem
//		- MdiListItem
//

using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Drawing;

namespace System.Windows.Forms
{
	[ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
	[ListBindable(false)]
	public abstract class Menu : Component
	{
		internal MenuItemCollection menu_items;
		internal IntPtr menu_handle = IntPtr.Zero;
		internal Menu parent_menu = null;
		System.Drawing.Rectangle rect = new Rectangle ();
		// UIA Framework Note: Used to keep track of expanded menus
		internal Control Wnd;
		internal MenuTracker tracker;
		private string control_name;
		private object control_tag;
		public const int FindHandle = 0;
		public const int FindShortcut = 1;

 		protected Menu (MenuItem[] items)
		{
			menu_items = new MenuItemCollection (this);

			if (items != null)
				menu_items.AddRange (items);
		}

		#region Public Properties
		
		[BrowsableAttribute(false)]
		[EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public IntPtr Handle {
			get { return menu_handle; }
		}

		internal virtual void OnMenuChanged (EventArgs e)
		{
			EventHandler eh = (EventHandler)(Events [MenuChangedEvent]);
			if (eh != null)
				eh (this, e);
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public virtual bool IsParent {
			get {
				if (menu_items != null && menu_items.Count > 0)
					return true;
				else
					return false;
			}
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public MenuItem MdiListItem {
			get {
				throw new NotImplementedException ();
			}
		}

		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
		[MergableProperty(false)]
		public MenuItemCollection MenuItems {
			get { return menu_items; }
		}
		
		[BrowsableAttribute(false)]
		[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
		public string Name { 
			get { return control_name; } 
			set { control_name = value; }
		}

		[Localizable(false)]
		[Bindable(true)]
		[TypeConverter(typeof(StringConverter))]
		[DefaultValue(null)]
		[MWFCategory("Data")]
		public object Tag {
			get { return control_tag; }
			set { control_tag = value; }
		}

		#endregion Public Properties

		#region Private Properties

		// UIA Framework Note: Used to obtain menu bounds
		internal Rectangle Rect {
			get { return rect; }
		}

		internal MenuItem SelectedItem  {
			get {
				foreach (MenuItem item in MenuItems)
					if (item.Selected)
						return item;

				return null;
			}
		}

		internal int Height {
			get { return rect.Height; }
			set { rect.Height = value; }
		}

		internal int Width {
			get { return rect.Width; }
			set { rect.Width = value; }
		}

		internal int X {
			get { return rect.X; }
			set { rect.X = value; }
		}

		internal int Y {
			get { return rect.Y; }
			set { rect.Y = value; }
		}

		internal MenuTracker Tracker {
			get {
				Menu top = this;
				while (top.parent_menu != null)
					top = top.parent_menu;

				return top.tracker;
			}
		}
		#endregion Private Properties

		#region Public Methods

		protected void CloneMenu (Menu menuSrc)
		{
			Dispose (true);

			menu_items = new MenuItemCollection (this);

			for (int i = 0; i < menuSrc.MenuItems.Count ; i++)
				menu_items.Add (menuSrc.MenuItems [i].CloneMenu ());
		}

		protected virtual IntPtr CreateMenuHandle ()
		{
			return IntPtr.Zero;
		}

		protected override void Dispose (bool disposing)
		{
			if (disposing) {
				if (menu_items != null) {
					// MenuItem.Dispose removes the item from the list
					while (menu_items.Count > 0) {
						menu_items [0].Dispose ();
					}
				}
				if (menu_handle != IntPtr.Zero) {
					menu_handle = IntPtr.Zero;
				}
			}
		}

		// From Microsoft documentation is impossible to guess that 
		// this method is supossed to do
		//
		// update: according to MS documentation, first parameter is on of this
		// constant values FindHandle or FindShortcut, value depends from what
		// you what to search, by shortcut or handle. FindHandle and FindShortcut
		// is a constant fields and was defined for this class.  
		public MenuItem FindMenuItem (int type, IntPtr value)
		{
			return null;
		}

		protected int FindMergePosition (int mergeOrder)
		{
			int cnt = MenuItems.Count, cur, pos;
			
			for (pos = 0; pos < cnt; ) {
				cur = (pos + cnt) /2;
				if (MenuItems[cur].MergeOrder > mergeOrder) {
					cnt = cur;
				} else	{
					pos = cur +1;
				}
			}
			
			return pos;
		}

		public ContextMenu GetContextMenu ()
		{
			for (Menu item = this; item != null; item = item.parent_menu) {
				if (item is ContextMenu) {
					return (ContextMenu) item;
				}
			}
			
			return null;
		}

		public MainMenu GetMainMenu ()
		{				
			for (Menu item = this; item != null; item = item.parent_menu) {
				if (item is MainMenu) {
					return (MainMenu) item;
				}				
			}
			
			return null;
		}

		internal virtual void InvalidateItem (MenuItem item)
		{
			if (Wnd != null)
				Wnd.Invalidate (item.bounds);
		}

		public virtual void MergeMenu (Menu menuSrc)
		{
			if (menuSrc == this)
				throw new ArgumentException ("The menu cannot be merged with itself");
			
			if (menuSrc == null)
				return;
				
			for (int i = 0; i < menuSrc.MenuItems.Count; i++) {
				
				MenuItem sourceitem = menuSrc.MenuItems[i];
				
				switch (sourceitem.MergeType) {
					case MenuMerge.Remove:	// Item not included
						break;
						
					case MenuMerge.Add:
					{
						int pos = FindMergePosition (sourceitem.MergeOrder);						
						MenuItems.Add (pos, sourceitem.CloneMenu ());
						break;					
					}
					
					case MenuMerge.Replace:
					case MenuMerge.MergeItems:
					{
						for (int pos = FindMergePosition (sourceitem.MergeOrder-1); pos <= MenuItems.Count; pos++) {
							
							if  ((pos >= MenuItems.Count) || (MenuItems[pos].MergeOrder != sourceitem.MergeOrder)) {
								MenuItems.Add (pos, sourceitem.CloneMenu ());
								break;
							}
							
							MenuItem mergeitem = MenuItems[pos];
							
							if (mergeitem.MergeType != MenuMerge.Add) {
								if ((sourceitem.MergeType == MenuMerge.MergeItems) && (mergeitem.MergeType == MenuMerge.MergeItems)) {
									mergeitem.MergeMenu (sourceitem);
								} else {
									MenuItems.Remove (sourceitem);
									MenuItems.Add (pos, sourceitem.CloneMenu ());
								}
								break;
							}
						}
						
						break;
					}
					
					default:
						break;
				}			
			}		
		}

		protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData)
		{
			if (tracker == null)
				return false;
			return tracker.ProcessKeys (ref msg, keyData);
		}

		public override string ToString ()
		{
			return base.ToString () + ", Items.Count: " + MenuItems.Count;
		}

		#endregion Public Methods
		static object MenuChangedEvent = new object ();

		// UIA Framework Note: Used to track changes in MenuItemCollection
		internal event EventHandler MenuChanged {
			add { Events.AddHandler (MenuChangedEvent, value); }
			remove { Events.RemoveHandler (MenuChangedEvent, value); }
		}

		[ListBindable(false)]
		public class MenuItemCollection : IList, ICollection, IEnumerable
		{
			private Menu owner;
			private ArrayList items = new ArrayList ();

			public MenuItemCollection (Menu owner)
			{
				this.owner = owner;
			}

			#region Public Properties

			public int Count {
				get { return items.Count;}
			}

			public bool IsReadOnly {
				get { return false; }
			}

			bool ICollection.IsSynchronized {
				get { return false;}
			}

			object ICollection.SyncRoot {
				get { return this;}
			}

			bool IList.IsFixedSize {
				get { return false;}
			}

			public virtual MenuItem this [int index] {
				get {
					if (index < 0 || index >= Count)
						throw new ArgumentOutOfRangeException ("Index of out range");

					return (MenuItem) items[index];
				}
			}

			public virtual MenuItem this [string key] {
				get {
					if (string.IsNullOrEmpty (key))
						return null;
						
					foreach (MenuItem m in items)
						if (string.Compare (m.Name, key, true) == 0)
							return m;
							
					return null;
				}
			}

			object IList.this[int index] {
				get { return items[index]; }
				set { throw new NotSupportedException (); }
			}

			#endregion Public Properties

			#region Public Methods

			public virtual int Add (MenuItem item)
			{
				if (item.Parent != null)
					item.Parent.MenuItems.Remove (item);
				
				items.Add (item);
				item.Index = items.Count - 1;
				UpdateItem (item);
				
				owner.OnMenuChanged (EventArgs.Empty);
				if (owner.parent_menu != null)
					owner.parent_menu.OnMenuChanged (EventArgs.Empty);
				return items.Count - 1;
			}

			internal void AddNoEvents (MenuItem mi)
			{
				if (mi.Parent != null)
					mi.Parent.MenuItems.Remove (mi);
				
				items.Add (mi);
				mi.Index = items.Count - 1;
				mi.parent_menu = owner;
			}

			public virtual MenuItem Add (string caption)
			{
				MenuItem item = new MenuItem (caption);
				Add (item);
				return item;
			}

			public virtual int Add (int index, MenuItem item)
			{
				if (index < 0 || index > Count)
					throw new ArgumentOutOfRangeException ("Index of out range");

				ArrayList new_items = new ArrayList (Count + 1);

				for (int i = 0; i < index; i++)
					new_items.Add (items[i]);

				new_items.Add (item);

				for (int i = index; i < Count; i++)
					new_items.Add (items[i]);

				items = new_items;
				UpdateItemsIndices ();				
				UpdateItem (item);

				return index;
			}

			private void UpdateItem (MenuItem mi)
			{
				mi.parent_menu = owner;
				owner.OnMenuChanged (EventArgs.Empty);
				if (owner.parent_menu != null)
					owner.parent_menu.OnMenuChanged (EventArgs.Empty);
				if (owner.Tracker != null)
					owner.Tracker.AddShortcuts (mi);
			}

			internal void Insert (int index, MenuItem mi)
			{
				if (index < 0 || index > Count)
					throw new ArgumentOutOfRangeException ("Index of out range");
				
				items.Insert (index, mi);
				
				UpdateItemsIndices ();
				UpdateItem (mi);
			}

			public virtual MenuItem Add (string caption, EventHandler onClick)
			{
				MenuItem item = new MenuItem (caption, onClick);
				Add (item);

				return item;
			}

			public virtual MenuItem Add (string caption, MenuItem[] items)
			{
				MenuItem item = new MenuItem (caption, items);
				Add (item);

				return item;
			}

			public virtual void AddRange (MenuItem[] items)
			{
				if (items == null)
					throw new ArgumentNullException ("items");

				foreach (MenuItem mi in items)
					Add (mi);
			}

			public virtual void Clear ()
			{
				MenuTracker tracker = owner.Tracker;
				foreach (MenuItem item in items) {
					if (tracker != null)
						tracker.RemoveShortcuts (item);
					item.parent_menu = null;
				}
				items.Clear ();
				owner.OnMenuChanged (EventArgs.Empty);
			}

			public bool Contains (MenuItem value)
			{
				return items.Contains (value);
			}

			public virtual bool ContainsKey (string key)
			{
				return !(this[key] == null);
			}

			public void CopyTo (Array dest, int index)
			{
				items.CopyTo (dest, index);
			}

			public MenuItem[] Find (string key, bool searchAllChildren)
			{
				if (string.IsNullOrEmpty (key))
					throw new ArgumentNullException ("key");
					
				List<MenuItem> list = new List<MenuItem> ();
				
				foreach (MenuItem m in items)
					if (string.Compare (m.Name, key, true) == 0)
						list.Add (m);
				
				if (searchAllChildren)
					foreach (MenuItem m in items)
						list.AddRange (m.MenuItems.Find (key, true));
						
				return list.ToArray ();
			}

			public IEnumerator GetEnumerator ()
			{
				return items.GetEnumerator ();
			}

			int IList.Add (object value)
			{
				return Add ((MenuItem)value);
			}

			bool IList.Contains (object value)
			{
				return Contains ((MenuItem)value);
			}

			int IList.IndexOf (object value)
			{
				return IndexOf ((MenuItem)value);
			}

			void IList.Insert (int index, object value)
			{
				Insert (index, (MenuItem) value);
			}

			void IList.Remove (object value)
			{
				Remove ((MenuItem) value);
			}

			public int IndexOf (MenuItem value)
			{
				return items.IndexOf (value);
			}

			public virtual int IndexOfKey (string key)
			{
				if (string.IsNullOrEmpty (key))
					return -1;
					
				return IndexOf (this[key]);
			}

			public virtual void Remove (MenuItem item)
			{
				RemoveAt (item.Index);
			}

			public virtual void RemoveAt (int index)
			{
				if (index < 0 || index >= Count)
					throw new ArgumentOutOfRangeException ("Index of out range");

				MenuItem item = (MenuItem) items [index];
				MenuTracker tracker = owner.Tracker;
				if (tracker != null)
					tracker.RemoveShortcuts (item);
				item.parent_menu = null;

				items.RemoveAt (index);

				UpdateItemsIndices ();
				owner.OnMenuChanged (EventArgs.Empty);
			}

			public virtual void RemoveByKey (string key)
			{
				Remove (this[key]);
			}

			#endregion Public Methods

			#region Private Methods

			private void UpdateItemsIndices ()
			{
				for (int i = 0; i < Count; i++)	// Recalculate indeces
					((MenuItem)items[i]).Index = i;
			}

			#endregion Private Methods
		}
	}
}