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
27 // - ShortCut navigation
31 using System.Collections;
32 using System.ComponentModel;
33 using System.ComponentModel.Design;
34 using System.Reflection;
35 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms
39 [Designer ("Microsoft.VisualStudio.Windows.Forms.MenuDesigner, " + Consts.AssemblyMicrosoft_VisualStudio, "System.ComponentModel.Design.IDesigner")]
40 [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
42 public abstract class Menu : Component
44 internal MenuItemCollection menu_items;
45 internal IntPtr menu_handle = IntPtr.Zero;
46 internal bool is_dirty = true;
47 internal bool creating = false;
48 internal Menu parent_menu = null;
50 public const int FindHandle = 0;
51 public const int FindShortcut = 1;
53 protected Menu (MenuItem[] items)
55 menu_items = new MenuItemCollection (this);
58 menu_items.AddRange (items);
62 #region Public Properties
63 [BrowsableAttribute(false)]
64 [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
65 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
66 public IntPtr Handle {
68 if (IsDirty && creating == false) {
72 if (menu_handle == IntPtr.Zero) {
80 internal virtual void MenuChanged ()
82 menu_handle = CreateMenuHandle ();
87 [BrowsableAttribute(false)]
88 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
89 public virtual bool IsParent {
91 if (menu_items != null && menu_items.Count > 0)
98 [BrowsableAttribute(false)]
99 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
100 public MenuItem MdiListItem {
102 throw new NotImplementedException ();
106 [BrowsableAttribute(false)]
107 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
108 [MergableProperty(false)]
109 public MenuItemCollection MenuItems {
110 get { return menu_items; }
113 #endregion Public Properties
115 #region Private Properties
117 internal bool IsDirty {
118 get { return is_dirty; }
119 set { is_dirty = value; }
122 #endregion Private Properties
124 #region Public Methods
126 protected void CloneMenu (Menu menuSrc)
130 menu_items = new MenuItemCollection (this);
132 for (int i = 0; i < menuSrc.MenuItems.Count ; i++)
133 menu_items.Add (menuSrc.MenuItems [i].CloneMenu ());
136 protected virtual IntPtr CreateMenuHandle ()
140 menu = MenuAPI.CreatePopupMenu (this);
144 protected override void Dispose (bool disposing)
147 if (menu_handle != IntPtr.Zero) {
148 MenuAPI.DestroyMenu (menu_handle);
149 menu_handle = IntPtr.Zero;
154 // From Microsoft documentation is impossible to guess that
155 // this method is supossed to do
156 public MenuItem FindMenuItem (int type, IntPtr value)
161 protected int FindMergePosition (int mergeOrder)
163 int cnt = MenuItems.Count, cur, pos;
165 for (pos = 0; pos < cnt; ) {
166 cur = (pos + cnt) /2;
167 if (MenuItems[cur].MergeOrder > mergeOrder) {
177 public ContextMenu GetContextMenu ()
179 for (Menu item = this; item != null; item = item.parent_menu) {
180 if (item is ContextMenu) {
181 return (ContextMenu) item;
188 public MainMenu GetMainMenu ()
190 for (Menu item = this; item != null; item = item.parent_menu) {
191 if (item is MainMenu) {
192 return (MainMenu) item;
199 public virtual void MergeMenu (Menu menuSrc)
202 throw new ArgumentException ("The menu cannot be merged with itself");
204 for (int i = 0; i < menuSrc.MenuItems.Count; i++){
206 switch (menuSrc.MenuItems[i].MergeType) {
207 case MenuMerge.Remove: // Item not included
212 int pos = FindMergePosition (menuSrc.MenuItems[i].MergeOrder);
213 MenuItems.Add (pos, menuSrc.MenuItems[i].CloneMenu ());
217 case MenuMerge.Replace:
218 case MenuMerge.MergeItems:
220 int pos = FindMergePosition (menuSrc.MenuItems[i].MergeOrder - 1);
221 MenuItems.Add (pos, menuSrc.MenuItems[i].CloneMenu ());
232 protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData)
237 public override string ToString ()
239 return base.ToString () + ", Items.Count: " + MenuItems.Count;
242 #endregion Public Methods
244 #region Private Methods
246 internal void CreateItems ()
250 for (int i = 0; i < menu_items.Count; i++)
251 menu_items[i].Create ();
256 #endregion Private Methods
258 [ListBindable(false)]
259 public class MenuItemCollection : IList, ICollection, IEnumerable
262 private ArrayList items = new ArrayList ();
264 public MenuItemCollection (Menu owner)
269 #region Public Properties
271 public virtual int Count {
272 get { return items.Count;}
275 public virtual bool IsReadOnly {
279 bool ICollection.IsSynchronized {
283 object ICollection.SyncRoot {
287 bool IList.IsFixedSize {
291 public MenuItem this [int index] {
293 if (index < 0 || index >= Count)
294 throw new ArgumentOutOfRangeException ("Index of out range");
296 return (MenuItem) items[index];
300 object IList.this[int index] {
301 get { return items[index]; }
302 set { throw new NotSupportedException (); }
305 #endregion Public Properties
307 #region Public Methods
309 public virtual int Add (MenuItem mi)
311 mi.parent_menu = owner;
312 mi.Index = items.Count;
315 owner.IsDirty = true;
316 return items.Count - 1;
319 public virtual MenuItem Add (string s)
321 MenuItem item = new MenuItem (s);
326 public virtual int Add (int index, MenuItem mi)
328 if (index < 0 || index > Count)
329 throw new ArgumentOutOfRangeException ("Index of out range");
331 ArrayList new_items = new ArrayList (Count + 1);
333 for (int i = 0; i < index; i++)
334 new_items.Add (items[i]);
338 for (int i = index; i < Count; i++)
339 new_items.Add (items[i]);
342 UpdateItemsIndices ();
343 owner.IsDirty = true;
348 public virtual MenuItem Add (string s, EventHandler e)
350 MenuItem item = new MenuItem (s, e);
356 public virtual MenuItem Add (string s, MenuItem[] items)
358 MenuItem item = new MenuItem (s, items);
364 public virtual void AddRange (MenuItem[] items)
367 throw new ArgumentNullException ("items");
369 foreach (MenuItem mi in items)
373 public virtual void Clear ()
376 owner.IsDirty = true;
379 public bool Contains (MenuItem value)
381 return items.Contains (value);
384 public virtual void CopyTo (Array dest, int index)
386 items.CopyTo (dest, index);
389 public virtual IEnumerator GetEnumerator ()
391 return items.GetEnumerator ();
394 int IList.Add (object value)
396 return Add ((MenuItem)value);
399 bool IList.Contains (object value)
401 return Contains ((MenuItem)value);
404 int IList.IndexOf (object value)
406 return IndexOf ((MenuItem)value);
409 void IList.Insert (int index, object value)
411 Add (index, (MenuItem) value);
414 void IList.Remove (object value)
416 Remove ((MenuItem) value);
419 public int IndexOf (MenuItem value)
421 return items.IndexOf (value);
424 public virtual void Remove (MenuItem item)
426 RemoveAt (item.Index);
429 public virtual void RemoveAt (int index)
431 if (index < 0 || index >= Count)
432 throw new ArgumentOutOfRangeException ("Index of out range");
434 items.RemoveAt (index);
436 UpdateItemsIndices ();
437 owner.IsDirty = true;
440 #endregion Public Methods
442 #region Private Methods
444 private void UpdateItemsIndices ()
446 for (int i = 0; i < Count; i++) // Recalculate indeces
447 ((MenuItem)items[i]).Index = i;
450 #endregion Private Methods