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 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
28 // - ShortCut navigation
32 using System.Collections;
33 using System.ComponentModel;
35 namespace System.Windows.Forms
37 public abstract class Menu : Component
39 internal MenuItemCollection menu_items;
40 internal IntPtr menu_handle = IntPtr.Zero;
41 internal bool is_dirty = true;
42 internal bool creating = false;
44 public const int FindHandle = 0;
45 public const int FindShortcut = 1;
47 protected Menu (MenuItem[] items)
49 menu_items = new MenuItemCollection (this);
52 menu_items.AddRange (items);
56 #region Public Properties
57 public IntPtr Handle {
59 if (IsDirty && creating == false) {
63 if (menu_handle == IntPtr.Zero) {
64 menu_handle = CreateMenuHandle ();
73 public virtual bool IsParent {
75 if (menu_items != null && menu_items.Count > 0)
82 public MenuItem MdiListItem {
84 throw new NotImplementedException ();
88 public MenuItemCollection MenuItems {
89 get { return menu_items; }
92 #endregion Public Properties
94 #region Private Properties
96 internal bool IsDirty {
97 get { return is_dirty; }
98 set { is_dirty = value; }
101 #endregion Private Properties
103 #region Public Methods
105 protected void CloneMenu (Menu menuSrc)
109 menu_items = new MenuItemCollection (this);
111 for (int i = 0; i < menuSrc.MenuItems.Count ; i++)
112 menu_items.Add (menuSrc.MenuItems [i]);
115 protected virtual IntPtr CreateMenuHandle ()
119 menu = MenuAPI.CreatePopupMenu (this);
123 protected override void Dispose (bool disposing)
126 if (menu_handle != IntPtr.Zero)
127 MenuAPI.DestroyMenu (menu_handle);
128 menu_handle = IntPtr.Zero;
132 public MenuItem FindMenuItem (int type, IntPtr value)
134 throw new NotImplementedException ();
137 protected int FindMergePosition (int mergeOrder)
139 throw new NotImplementedException ();
142 public ContextMenu GetContextMenu ()
144 if (this is ContextMenu)
145 return (ContextMenu) this;
150 public MainMenu GetMainMenu ()
152 if (this is MainMenu)
153 return (MainMenu) this;
158 public virtual void MergeMenu (Menu menuSrc)
161 throw new ArgumentException ("The menu cannot be merged with itself");
165 protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData)
170 public override string ToString ()
172 return base.ToString ();
175 #endregion Public Methods
177 #region Private Methods
179 internal void CreateItems ()
183 for (int i = 0; i < menu_items.Count; i++)
184 menu_items[i].Create ();
189 #endregion Private Methods
191 public class MenuItemCollection : IList, ICollection, IEnumerable
194 private ArrayList items = new ArrayList ();
196 public MenuItemCollection (Menu owner)
201 #region Public Properties
203 public virtual int Count {
204 get { return items.Count;}
207 public virtual bool IsReadOnly {
211 bool ICollection.IsSynchronized {
215 object ICollection.SyncRoot {
219 bool IList.IsFixedSize {
223 public MenuItem this [int index] {
225 if (index < 0 || index >= Count)
226 throw new ArgumentOutOfRangeException ("Index of out range");
228 return (MenuItem) items[index];
232 object IList.this[int index] {
233 get { return items[index]; }
234 set { throw new NotSupportedException (); }
237 #endregion Public Properties
239 #region Public Methods
241 public virtual int Add (MenuItem mi)
243 mi.parent_menu = owner;
244 mi.Index = items.Count;
247 owner.IsDirty = true;
248 return items.Count - 1;
251 public virtual MenuItem Add (string s)
253 MenuItem item = new MenuItem (s);
258 public virtual int Add (int index, MenuItem mi)
260 if (index < 0 || index >= Count)
261 throw new ArgumentOutOfRangeException ("Index of out range");
263 ArrayList new_items = new ArrayList (Count + 1);
265 for (int i = 0; i < index; i++)
266 new_items.Add (items[i]);
270 for (int i = index; i < Count; i++)
271 new_items.Add (items[i]);
274 UpdateItemsIndices ();
275 owner.IsDirty = true;
280 public virtual MenuItem Add (string s, EventHandler e)
282 MenuItem item = new MenuItem (s, e);
288 public virtual MenuItem Add (string s, MenuItem[] items)
290 MenuItem item = new MenuItem (s, items);
296 public virtual void AddRange (MenuItem[] items)
299 throw new ArgumentNullException ("items");
301 foreach (MenuItem mi in items)
305 public virtual void Clear ()
308 owner.IsDirty = true;
311 public bool Contains (MenuItem value)
313 return items.Contains (value);
316 public virtual void CopyTo (Array dest, int index)
318 items.CopyTo (dest, index);
321 public virtual IEnumerator GetEnumerator ()
323 return items.GetEnumerator ();
326 int IList.Add (object value)
328 return Add ((MenuItem)value);
331 bool IList.Contains (object value)
333 return Contains ((MenuItem)value);
336 int IList.IndexOf (object value)
338 return IndexOf ((MenuItem)value);
341 void IList.Insert (int index, object value)
343 Add (index, (MenuItem) value);
346 void IList.Remove (object value)
348 Remove ((MenuItem) value);
351 public int IndexOf (MenuItem value)
353 return items.IndexOf (value);
356 public virtual void Remove (MenuItem item)
358 RemoveAt (item.Index);
361 public virtual void RemoveAt (int index)
363 if (index < 0 || index >= Count)
364 throw new ArgumentOutOfRangeException ("Index of out range");
366 items.RemoveAt (index);
368 UpdateItemsIndices ();
369 owner.IsDirty = true;
372 #endregion Public Methods
374 #region Private Methods
376 private void UpdateItemsIndices ()
378 for (int i = 0; i < Count; i++) // Recalculate indeces
379 ((MenuItem)items[i]).Index = i;
382 #endregion Private Methods