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");
266 for (int i = 0; i < menuSrc.MenuItems.Count; i++){
268 switch (menuSrc.MenuItems[i].MergeType) {
269 case MenuMerge.Remove: // Item not included
274 int pos = FindMergePosition (menuSrc.MenuItems[i].MergeOrder);
275 MenuItems.Add (pos, menuSrc.MenuItems[i].CloneMenu ());
279 case MenuMerge.Replace:
280 case MenuMerge.MergeItems:
282 int pos = FindMergePosition (menuSrc.MenuItems[i].MergeOrder - 1);
283 MenuItems.Insert (pos, menuSrc.MenuItems[i].CloneMenu ());
294 protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData)
298 return tracker.ProcessKeys (ref msg, keyData);
301 public override string ToString ()
303 return base.ToString () + ", Items.Count: " + MenuItems.Count;
306 #endregion Public Methods
307 static object MenuChangedEvent = new object ();
309 internal event EventHandler MenuChanged {
310 add { Events.AddHandler (MenuChangedEvent, value); }
311 remove { Events.RemoveHandler (MenuChangedEvent, value); }
314 [ListBindable(false)]
315 public class MenuItemCollection : IList, ICollection, IEnumerable
318 private ArrayList items = new ArrayList ();
320 public MenuItemCollection (Menu owner)
325 #region Public Properties
328 get { return items.Count;}
331 public bool IsReadOnly {
332 get { return false; }
335 bool ICollection.IsSynchronized {
339 object ICollection.SyncRoot {
343 bool IList.IsFixedSize {
347 public virtual MenuItem this [int index] {
349 if (index < 0 || index >= Count)
350 throw new ArgumentOutOfRangeException ("Index of out range");
352 return (MenuItem) items[index];
356 object IList.this[int index] {
357 get { return items[index]; }
358 set { throw new NotSupportedException (); }
361 #endregion Public Properties
363 #region Public Methods
365 public virtual int Add (MenuItem mi)
367 if (mi.Parent != null)
368 mi.Parent.MenuItems.Remove (mi);
371 mi.Index = items.Count - 1;
374 owner.OnMenuChanged (EventArgs.Empty);
375 if (owner.parent_menu != null)
376 owner.parent_menu.OnMenuChanged (EventArgs.Empty);
377 return items.Count - 1;
380 internal void AddNoEvents (MenuItem mi)
382 if (mi.Parent != null)
383 mi.Parent.MenuItems.Remove (mi);
386 mi.Index = items.Count - 1;
387 mi.parent_menu = owner;
390 public virtual MenuItem Add (string s)
392 MenuItem item = new MenuItem (s);
397 public virtual int Add (int index, MenuItem mi)
399 if (index < 0 || index > Count)
400 throw new ArgumentOutOfRangeException ("Index of out range");
402 ArrayList new_items = new ArrayList (Count + 1);
404 for (int i = 0; i < index; i++)
405 new_items.Add (items[i]);
409 for (int i = index; i < Count; i++)
410 new_items.Add (items[i]);
413 UpdateItemsIndices ();
419 private void UpdateItem (MenuItem mi)
421 mi.parent_menu = owner;
422 owner.OnMenuChanged (EventArgs.Empty);
423 if (owner.parent_menu != null)
424 owner.parent_menu.OnMenuChanged (EventArgs.Empty);
425 if (owner.Tracker != null)
426 owner.Tracker.AddShortcuts (mi);
429 internal void Insert (int index, MenuItem mi)
431 if (index < 0 || index > Count)
432 throw new ArgumentOutOfRangeException ("Index of out range");
433 UpdateItemsIndices ();
437 public virtual MenuItem Add (string s, EventHandler e)
439 MenuItem item = new MenuItem (s, e);
445 public virtual MenuItem Add (string s, MenuItem[] items)
447 MenuItem item = new MenuItem (s, items);
453 public virtual void AddRange (MenuItem[] items)
456 throw new ArgumentNullException ("items");
458 foreach (MenuItem mi in items)
462 public virtual void Clear ()
464 MenuTracker tracker = owner.Tracker;
465 foreach (MenuItem item in items) {
467 tracker.RemoveShortcuts (item);
468 item.parent_menu = null;
471 owner.OnMenuChanged (EventArgs.Empty);
474 public bool Contains (MenuItem value)
476 return items.Contains (value);
479 public void CopyTo (Array dest, int index)
481 items.CopyTo (dest, index);
484 public IEnumerator GetEnumerator ()
486 return items.GetEnumerator ();
489 int IList.Add (object value)
491 return Add ((MenuItem)value);
494 bool IList.Contains (object value)
496 return Contains ((MenuItem)value);
499 int IList.IndexOf (object value)
501 return IndexOf ((MenuItem)value);
504 void IList.Insert (int index, object value)
506 Insert (index, (MenuItem) value);
509 void IList.Remove (object value)
511 Remove ((MenuItem) value);
514 public int IndexOf (MenuItem value)
516 return items.IndexOf (value);
519 public virtual void Remove (MenuItem item)
521 RemoveAt (item.Index);
524 public virtual void RemoveAt (int index)
526 if (index < 0 || index >= Count)
527 throw new ArgumentOutOfRangeException ("Index of out range");
529 MenuItem item = (MenuItem) items [index];
530 MenuTracker tracker = owner.Tracker;
532 tracker.RemoveShortcuts (item);
533 item.parent_menu = null;
535 items.RemoveAt (index);
537 UpdateItemsIndices ();
538 owner.OnMenuChanged (EventArgs.Empty);
541 #endregion Public Methods
543 #region Private Methods
545 private void UpdateItemsIndices ()
547 for (int i = 0; i < Count; i++) // Recalculate indeces
548 ((MenuItem)items[i]).Index = i;
551 #endregion Private Methods