1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
30 using System.Collections;
31 using System.ComponentModel;
32 using System.ComponentModel.Design;
33 using System.Reflection;
34 using System.Runtime.InteropServices;
35 using System.Collections.Generic;
37 namespace System.Windows.Forms
39 [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
41 public abstract class Menu : Component
43 internal MenuItemCollection menu_items;
44 internal IntPtr menu_handle = IntPtr.Zero;
45 internal Menu parent_menu = null;
46 System.Drawing.Rectangle rect;
47 // UIA Framework Note: Used to keep track of expanded menus
49 internal MenuTracker tracker;
50 private string control_name;
51 private object control_tag;
52 public const int FindHandle = 0;
53 public const int FindShortcut = 1;
55 protected Menu (MenuItem[] items)
57 menu_items = new MenuItemCollection (this);
60 menu_items.AddRange (items);
63 #region Public Properties
65 [BrowsableAttribute(false)]
66 [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
67 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
68 public IntPtr Handle {
69 get { return menu_handle; }
72 internal virtual void OnMenuChanged (EventArgs e)
74 EventHandler eh = (EventHandler)(Events [MenuChangedEvent]);
79 [BrowsableAttribute(false)]
80 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
81 public virtual bool IsParent {
83 if (menu_items != null && menu_items.Count > 0)
90 [BrowsableAttribute(false)]
91 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
92 public MenuItem MdiListItem {
94 throw new NotImplementedException ();
98 [BrowsableAttribute(false)]
99 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
100 [MergableProperty(false)]
101 public MenuItemCollection MenuItems {
102 get { return menu_items; }
105 [BrowsableAttribute(false)]
106 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
108 get { return control_name; }
109 set { control_name = value; }
114 [TypeConverter(typeof(StringConverter))]
116 [MWFCategory("Data")]
118 get { return control_tag; }
119 set { control_tag = value; }
122 #endregion Public Properties
124 #region Private Properties
126 // UIA Framework Note: Used to obtain menu bounds
127 internal System.Drawing.Rectangle Rect {
131 internal MenuItem SelectedItem {
133 foreach (MenuItem item in MenuItems)
141 internal int Height {
142 get { return rect.Height; }
143 set { rect.Height = value; }
147 get { return rect.Width; }
148 set { rect.Width = value; }
152 get { return rect.X; }
153 set { rect.X = value; }
157 get { return rect.Y; }
158 set { rect.Y = value; }
161 internal MenuTracker Tracker {
164 while (top.parent_menu != null)
165 top = top.parent_menu;
170 #endregion Private Properties
172 #region Public Methods
174 protected void CloneMenu (Menu menuSrc)
178 menu_items = new MenuItemCollection (this);
180 for (int i = 0; i < menuSrc.MenuItems.Count ; i++)
181 menu_items.Add (menuSrc.MenuItems [i].CloneMenu ());
184 protected virtual IntPtr CreateMenuHandle ()
189 protected override void Dispose (bool disposing)
192 if (menu_handle != IntPtr.Zero) {
193 menu_handle = IntPtr.Zero;
198 // From Microsoft documentation is impossible to guess that
199 // this method is supossed to do
201 // update: according to MS documentation, first parameter is on of this
202 // constant values FindHandle or FindShortcut, value depends from what
203 // you what to search, by shortcut or handle. FindHandle and FindShortcut
204 // is a constant fields and was defined for this class.
205 public MenuItem FindMenuItem (int type, IntPtr value)
210 protected int FindMergePosition (int mergeOrder)
212 int cnt = MenuItems.Count, cur, pos;
214 for (pos = 0; pos < cnt; ) {
215 cur = (pos + cnt) /2;
216 if (MenuItems[cur].MergeOrder > mergeOrder) {
226 public ContextMenu GetContextMenu ()
228 for (Menu item = this; item != null; item = item.parent_menu) {
229 if (item is ContextMenu) {
230 return (ContextMenu) item;
237 public MainMenu GetMainMenu ()
239 for (Menu item = this; item != null; item = item.parent_menu) {
240 if (item is MainMenu) {
241 return (MainMenu) item;
248 internal virtual void InvalidateItem (MenuItem item)
251 Wnd.Invalidate (item.bounds);
254 public virtual void MergeMenu (Menu menuSrc)
257 throw new ArgumentException ("The menu cannot be merged with itself");
262 for (int i = 0; i < menuSrc.MenuItems.Count; i++) {
264 MenuItem sourceitem = menuSrc.MenuItems[i];
266 switch (sourceitem.MergeType) {
267 case MenuMerge.Remove: // Item not included
272 int pos = FindMergePosition (sourceitem.MergeOrder);
273 MenuItems.Add (pos, sourceitem.CloneMenu ());
277 case MenuMerge.Replace:
278 case MenuMerge.MergeItems:
280 for (int pos = FindMergePosition (sourceitem.MergeOrder-1); pos <= MenuItems.Count; pos++) {
282 if ((pos >= MenuItems.Count) || (MenuItems[pos].MergeOrder != sourceitem.MergeOrder)) {
283 MenuItems.Add (pos, sourceitem.CloneMenu ());
287 MenuItem mergeitem = MenuItems[pos];
289 if (mergeitem.MergeType != MenuMerge.Add) {
290 if ((sourceitem.MergeType == MenuMerge.MergeItems) && (mergeitem.MergeType == MenuMerge.MergeItems)) {
291 mergeitem.MergeMenu (sourceitem);
293 MenuItems.Remove (sourceitem);
294 MenuItems.Add (pos, sourceitem.CloneMenu ());
309 protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData)
313 return tracker.ProcessKeys (ref msg, keyData);
316 public override string ToString ()
318 return base.ToString () + ", Items.Count: " + MenuItems.Count;
321 #endregion Public Methods
322 static object MenuChangedEvent = new object ();
324 // UIA Framework Note: Used to track changes in MenuItemCollection
325 internal event EventHandler MenuChanged {
326 add { Events.AddHandler (MenuChangedEvent, value); }
327 remove { Events.RemoveHandler (MenuChangedEvent, value); }
330 [ListBindable(false)]
331 public class MenuItemCollection : IList, ICollection, IEnumerable
334 private ArrayList items = new ArrayList ();
336 public MenuItemCollection (Menu owner)
341 #region Public Properties
344 get { return items.Count;}
347 public bool IsReadOnly {
348 get { return false; }
351 bool ICollection.IsSynchronized {
355 object ICollection.SyncRoot {
359 bool IList.IsFixedSize {
363 public virtual MenuItem this [int index] {
365 if (index < 0 || index >= Count)
366 throw new ArgumentOutOfRangeException ("Index of out range");
368 return (MenuItem) items[index];
372 public virtual MenuItem this [string key] {
374 if (string.IsNullOrEmpty (key))
377 foreach (MenuItem m in items)
378 if (string.Compare (m.Name, key, true) == 0)
385 object IList.this[int index] {
386 get { return items[index]; }
387 set { throw new NotSupportedException (); }
390 #endregion Public Properties
392 #region Public Methods
394 public virtual int Add (MenuItem item)
396 if (item.Parent != null)
397 item.Parent.MenuItems.Remove (item);
400 item.Index = items.Count - 1;
403 owner.OnMenuChanged (EventArgs.Empty);
404 if (owner.parent_menu != null)
405 owner.parent_menu.OnMenuChanged (EventArgs.Empty);
406 return items.Count - 1;
409 internal void AddNoEvents (MenuItem mi)
411 if (mi.Parent != null)
412 mi.Parent.MenuItems.Remove (mi);
415 mi.Index = items.Count - 1;
416 mi.parent_menu = owner;
419 public virtual MenuItem Add (string caption)
421 MenuItem item = new MenuItem (caption);
426 public virtual int Add (int index, MenuItem item)
428 if (index < 0 || index > Count)
429 throw new ArgumentOutOfRangeException ("Index of out range");
431 ArrayList new_items = new ArrayList (Count + 1);
433 for (int i = 0; i < index; i++)
434 new_items.Add (items[i]);
436 new_items.Add (item);
438 for (int i = index; i < Count; i++)
439 new_items.Add (items[i]);
442 UpdateItemsIndices ();
448 private void UpdateItem (MenuItem mi)
450 mi.parent_menu = owner;
451 owner.OnMenuChanged (EventArgs.Empty);
452 if (owner.parent_menu != null)
453 owner.parent_menu.OnMenuChanged (EventArgs.Empty);
454 if (owner.Tracker != null)
455 owner.Tracker.AddShortcuts (mi);
458 internal void Insert (int index, MenuItem mi)
460 if (index < 0 || index > Count)
461 throw new ArgumentOutOfRangeException ("Index of out range");
463 items.Insert (index, mi);
465 UpdateItemsIndices ();
469 public virtual MenuItem Add (string caption, EventHandler onClick)
471 MenuItem item = new MenuItem (caption, onClick);
477 public virtual MenuItem Add (string caption, MenuItem[] items)
479 MenuItem item = new MenuItem (caption, items);
485 public virtual void AddRange (MenuItem[] items)
488 throw new ArgumentNullException ("items");
490 foreach (MenuItem mi in items)
494 public virtual void Clear ()
496 MenuTracker tracker = owner.Tracker;
497 foreach (MenuItem item in items) {
499 tracker.RemoveShortcuts (item);
500 item.parent_menu = null;
503 owner.OnMenuChanged (EventArgs.Empty);
506 public bool Contains (MenuItem value)
508 return items.Contains (value);
511 public virtual bool ContainsKey (string key)
513 return !(this[key] == null);
516 public void CopyTo (Array dest, int index)
518 items.CopyTo (dest, index);
521 public MenuItem[] Find (string key, bool searchAllChildren)
523 if (string.IsNullOrEmpty (key))
524 throw new ArgumentNullException ("key");
526 List<MenuItem> list = new List<MenuItem> ();
528 foreach (MenuItem m in items)
529 if (string.Compare (m.Name, key, true) == 0)
532 if (searchAllChildren)
533 foreach (MenuItem m in items)
534 list.AddRange (m.MenuItems.Find (key, true));
536 return list.ToArray ();
539 public IEnumerator GetEnumerator ()
541 return items.GetEnumerator ();
544 int IList.Add (object value)
546 return Add ((MenuItem)value);
549 bool IList.Contains (object value)
551 return Contains ((MenuItem)value);
554 int IList.IndexOf (object value)
556 return IndexOf ((MenuItem)value);
559 void IList.Insert (int index, object value)
561 Insert (index, (MenuItem) value);
564 void IList.Remove (object value)
566 Remove ((MenuItem) value);
569 public int IndexOf (MenuItem value)
571 return items.IndexOf (value);
574 public virtual int IndexOfKey (string key)
576 if (string.IsNullOrEmpty (key))
579 return IndexOf (this[key]);
582 public virtual void Remove (MenuItem item)
584 RemoveAt (item.Index);
587 public virtual void RemoveAt (int index)
589 if (index < 0 || index >= Count)
590 throw new ArgumentOutOfRangeException ("Index of out range");
592 MenuItem item = (MenuItem) items [index];
593 MenuTracker tracker = owner.Tracker;
595 tracker.RemoveShortcuts (item);
596 item.parent_menu = null;
598 items.RemoveAt (index);
600 UpdateItemsIndices ();
601 owner.OnMenuChanged (EventArgs.Empty);
604 public virtual void RemoveByKey (string key)
609 #endregion Public Methods
611 #region Private Methods
613 private void UpdateItemsIndices ()
615 for (int i = 0; i < Count; i++) // Recalculate indeces
616 ((MenuItem)items[i]).Index = i;
619 #endregion Private Methods