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;
36 namespace System.Windows.Forms
39 [Designer ("Microsoft.VisualStudio.Windows.Forms.MenuDesigner, " + Consts.AssemblyMicrosoft_VisualStudio, "System.ComponentModel.Design.IDesigner")]
41 [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
43 public abstract class Menu : Component
45 internal MenuItemCollection menu_items;
46 internal IntPtr menu_handle = IntPtr.Zero;
47 internal Menu parent_menu = null;
48 System.Drawing.Rectangle rect;
50 internal MenuTracker tracker;
53 private string control_name;
54 private object control_tag;
57 public const int FindHandle = 0;
58 public const int FindShortcut = 1;
60 protected Menu (MenuItem[] items)
62 menu_items = new MenuItemCollection (this);
65 menu_items.AddRange (items);
68 #region Public Properties
70 [BrowsableAttribute(false)]
71 [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
72 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
73 public IntPtr Handle {
74 get { return menu_handle; }
77 internal virtual void OnMenuChanged (EventArgs e)
79 EventHandler eh = (EventHandler)(Events [MenuChangedEvent]);
84 [BrowsableAttribute(false)]
85 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
86 public virtual bool IsParent {
88 if (menu_items != null && menu_items.Count > 0)
95 [BrowsableAttribute(false)]
96 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
97 public MenuItem MdiListItem {
99 throw new NotImplementedException ();
103 [BrowsableAttribute(false)]
104 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
105 [MergableProperty(false)]
106 public MenuItemCollection MenuItems {
107 get { return menu_items; }
112 [BrowsableAttribute(false)]
113 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
115 get { return control_name; }
116 set { control_name = value; }
121 [TypeConverter(typeof(StringConverter))]
123 [MWFCategory("Data")]
125 get { return control_tag; }
126 set { control_tag = value; }
130 #endregion Public Properties
132 #region Private Properties
134 internal System.Drawing.Rectangle Rect {
138 internal MenuItem SelectedItem {
140 foreach (MenuItem item in MenuItems)
148 internal int Height {
149 get { return rect.Height; }
150 set { rect.Height = value; }
154 get { return rect.Width; }
155 set { rect.Width = value; }
159 get { return rect.X; }
160 set { rect.X = value; }
164 get { return rect.Y; }
165 set { rect.Y = value; }
168 internal MenuTracker Tracker {
171 while (top.parent_menu != null)
172 top = top.parent_menu;
177 #endregion Private Properties
179 #region Public Methods
181 protected void CloneMenu (Menu menuSrc)
185 menu_items = new MenuItemCollection (this);
187 for (int i = 0; i < menuSrc.MenuItems.Count ; i++)
188 menu_items.Add (menuSrc.MenuItems [i].CloneMenu ());
191 protected virtual IntPtr CreateMenuHandle ()
196 protected override void Dispose (bool disposing)
199 if (menu_handle != IntPtr.Zero) {
200 menu_handle = IntPtr.Zero;
205 // From Microsoft documentation is impossible to guess that
206 // this method is supossed to do
208 // update: according to MS documentation, first parameter is on of this
209 // constant values FindHandle or FindShortcut, value depends from what
210 // you what to search, by shortcut or handle. FindHandle and FindShortcut
211 // is a constant fields and was defined for this class.
212 public MenuItem FindMenuItem (int type, IntPtr value)
217 protected int FindMergePosition (int mergeOrder)
219 int cnt = MenuItems.Count, cur, pos;
221 for (pos = 0; pos < cnt; ) {
222 cur = (pos + cnt) /2;
223 if (MenuItems[cur].MergeOrder > mergeOrder) {
233 public ContextMenu GetContextMenu ()
235 for (Menu item = this; item != null; item = item.parent_menu) {
236 if (item is ContextMenu) {
237 return (ContextMenu) item;
244 public MainMenu GetMainMenu ()
246 for (Menu item = this; item != null; item = item.parent_menu) {
247 if (item is MainMenu) {
248 return (MainMenu) item;
255 internal virtual void InvalidateItem (MenuItem item)
258 Wnd.Invalidate (item.bounds);
261 public virtual void MergeMenu (Menu menuSrc)
264 throw new ArgumentException ("The menu cannot be merged with itself");
269 for (int i = 0; i < menuSrc.MenuItems.Count; i++) {
271 MenuItem sourceitem = menuSrc.MenuItems[i];
273 switch (sourceitem.MergeType) {
274 case MenuMerge.Remove: // Item not included
279 int pos = FindMergePosition (sourceitem.MergeOrder);
280 MenuItems.Add (pos, sourceitem.CloneMenu ());
284 case MenuMerge.Replace:
285 case MenuMerge.MergeItems:
287 for (int pos = FindMergePosition (sourceitem.MergeOrder-1); pos <= MenuItems.Count; pos++) {
289 if ((pos >= MenuItems.Count) || (MenuItems[pos].MergeOrder != sourceitem.MergeOrder)) {
290 MenuItems.Add (pos, sourceitem.CloneMenu ());
294 MenuItem mergeitem = MenuItems[pos];
296 if (mergeitem.MergeType != MenuMerge.Add) {
297 if ((sourceitem.MergeType == MenuMerge.MergeItems) && (mergeitem.MergeType == MenuMerge.MergeItems)) {
298 mergeitem.MergeMenu (sourceitem);
300 MenuItems.Remove (sourceitem);
301 MenuItems.Add (pos, sourceitem.CloneMenu ());
316 protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData)
320 return tracker.ProcessKeys (ref msg, keyData);
323 public override string ToString ()
325 return base.ToString () + ", Items.Count: " + MenuItems.Count;
328 #endregion Public Methods
329 static object MenuChangedEvent = new object ();
331 internal event EventHandler MenuChanged {
332 add { Events.AddHandler (MenuChangedEvent, value); }
333 remove { Events.RemoveHandler (MenuChangedEvent, value); }
336 [ListBindable(false)]
337 public class MenuItemCollection : IList, ICollection, IEnumerable
340 private ArrayList items = new ArrayList ();
342 public MenuItemCollection (Menu owner)
347 #region Public Properties
350 get { return items.Count;}
353 public bool IsReadOnly {
354 get { return false; }
357 bool ICollection.IsSynchronized {
361 object ICollection.SyncRoot {
365 bool IList.IsFixedSize {
369 public virtual MenuItem this [int index] {
371 if (index < 0 || index >= Count)
372 throw new ArgumentOutOfRangeException ("Index of out range");
374 return (MenuItem) items[index];
378 object IList.this[int index] {
379 get { return items[index]; }
380 set { throw new NotSupportedException (); }
383 #endregion Public Properties
385 #region Public Methods
387 public virtual int Add (MenuItem mi)
389 if (mi.Parent != null)
390 mi.Parent.MenuItems.Remove (mi);
393 mi.Index = items.Count - 1;
396 owner.OnMenuChanged (EventArgs.Empty);
397 if (owner.parent_menu != null)
398 owner.parent_menu.OnMenuChanged (EventArgs.Empty);
399 return items.Count - 1;
402 internal void AddNoEvents (MenuItem mi)
404 if (mi.Parent != null)
405 mi.Parent.MenuItems.Remove (mi);
408 mi.Index = items.Count - 1;
409 mi.parent_menu = owner;
412 public virtual MenuItem Add (string s)
414 MenuItem item = new MenuItem (s);
419 public virtual int Add (int index, MenuItem mi)
421 if (index < 0 || index > Count)
422 throw new ArgumentOutOfRangeException ("Index of out range");
424 ArrayList new_items = new ArrayList (Count + 1);
426 for (int i = 0; i < index; i++)
427 new_items.Add (items[i]);
431 for (int i = index; i < Count; i++)
432 new_items.Add (items[i]);
435 UpdateItemsIndices ();
441 private void UpdateItem (MenuItem mi)
443 mi.parent_menu = owner;
444 owner.OnMenuChanged (EventArgs.Empty);
445 if (owner.parent_menu != null)
446 owner.parent_menu.OnMenuChanged (EventArgs.Empty);
447 if (owner.Tracker != null)
448 owner.Tracker.AddShortcuts (mi);
451 internal void Insert (int index, MenuItem mi)
453 if (index < 0 || index > Count)
454 throw new ArgumentOutOfRangeException ("Index of out range");
456 items.Insert (index, mi);
458 UpdateItemsIndices ();
462 public virtual MenuItem Add (string s, EventHandler e)
464 MenuItem item = new MenuItem (s, e);
470 public virtual MenuItem Add (string s, MenuItem[] items)
472 MenuItem item = new MenuItem (s, items);
478 public virtual void AddRange (MenuItem[] items)
481 throw new ArgumentNullException ("items");
483 foreach (MenuItem mi in items)
487 public virtual void Clear ()
489 MenuTracker tracker = owner.Tracker;
490 foreach (MenuItem item in items) {
492 tracker.RemoveShortcuts (item);
493 item.parent_menu = null;
496 owner.OnMenuChanged (EventArgs.Empty);
499 public bool Contains (MenuItem value)
501 return items.Contains (value);
504 public void CopyTo (Array dest, int index)
506 items.CopyTo (dest, index);
509 public IEnumerator GetEnumerator ()
511 return items.GetEnumerator ();
514 int IList.Add (object value)
516 return Add ((MenuItem)value);
519 bool IList.Contains (object value)
521 return Contains ((MenuItem)value);
524 int IList.IndexOf (object value)
526 return IndexOf ((MenuItem)value);
529 void IList.Insert (int index, object value)
531 Insert (index, (MenuItem) value);
534 void IList.Remove (object value)
536 Remove ((MenuItem) value);
539 public int IndexOf (MenuItem value)
541 return items.IndexOf (value);
544 public virtual void Remove (MenuItem item)
546 RemoveAt (item.Index);
549 public virtual void RemoveAt (int index)
551 if (index < 0 || index >= Count)
552 throw new ArgumentOutOfRangeException ("Index of out range");
554 MenuItem item = (MenuItem) items [index];
555 MenuTracker tracker = owner.Tracker;
557 tracker.RemoveShortcuts (item);
558 item.parent_menu = null;
560 items.RemoveAt (index);
562 UpdateItemsIndices ();
563 owner.OnMenuChanged (EventArgs.Empty);
566 #endregion Public Methods
568 #region Private Methods
570 private void UpdateItemsIndices ()
572 for (int i = 0; i < Count; i++) // Recalculate indeces
573 ((MenuItem)items[i]).Index = i;
576 #endregion Private Methods