| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (c) 2004 Novell, Inc.
- //
- // Authors:
- // Jordi Mas i Hernandez, [email protected]
- //
- // TODO:
- // - Merge menus
- // - MDI integration
- // - ShortCut navigation
- //
- // NOT COMPLETE
- using System.Collections;
- using System.ComponentModel;
- namespace System.Windows.Forms
- {
- public abstract class Menu : Component
- {
- internal MenuItemCollection menu_items;
- internal IntPtr menu_handle = IntPtr.Zero;
- internal bool is_dirty = true;
- internal bool creating = false;
- public const int FindHandle = 0;
- public const int FindShortcut = 1;
- protected Menu (MenuItem[] items)
- {
- menu_items = new MenuItemCollection (this);
- if (items != null)
- menu_items.AddRange (items);
- }
- #region Public Properties
- public IntPtr Handle {
- get {
- if (is_dirty && creating == false) {
- Dispose (true);
- }
- if (menu_handle == IntPtr.Zero) {
- Console.WriteLine ("Create Handle");
- menu_handle = CreateMenuHandle ();
- CreateItems ();
- is_dirty = false;
- }
- return menu_handle;
- }
- }
- public virtual bool IsParent {
- get {
- if (menu_items != null && menu_items.Count > 0)
- return true;
- else
- return false;
- }
- }
- public MenuItem MdiListItem {
- get {
- throw new NotImplementedException ();
- }
- }
- public MenuItemCollection MenuItems {
- get { return menu_items; }
- }
- #endregion Public Properties
- #region Private Properties
- internal bool IsDirty {
- get { return is_dirty; }
- set { is_dirty = value; }
- }
- #endregion Private Properties
- #region Public Methods
- protected void CloneMenu (Menu menuSrc)
- {
- Dispose (true);
- menu_items = new MenuItemCollection (this);
- for (int i = 0; i < menuSrc.MenuItems.Count ; i++)
- menu_items.Add (menuSrc.MenuItems [i]);
- }
- protected virtual IntPtr CreateMenuHandle ()
- {
- IntPtr menu;
- menu = MenuAPI.CreatePopupMenu ();
- return menu;
- }
- protected override void Dispose (bool disposing)
- {
- if (disposing) {
- if (menu_handle != IntPtr.Zero)
- MenuAPI.DestroyMenu (menu_handle);
- menu_handle = IntPtr.Zero;
- }
- }
- public MenuItem FindMenuItem (int type, IntPtr value)
- {
- throw new NotImplementedException ();
- }
- protected int FindMergePosition (int mergeOrder)
- {
- throw new NotImplementedException ();
- }
- public ContextMenu GetContextMenu ()
- {
- if (this is ContextMenu)
- return (ContextMenu) this;
- else
- return null;
- }
- public MainMenu GetMainMenu ()
- {
- if (this is MainMenu)
- return (MainMenu) this;
- else
- return null;
- }
- public virtual void MergeMenu (Menu menuSrc)
- {
- if (menuSrc == this)
- throw new ArgumentException ("The menu cannot be merged with itself");
- }
- protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData)
- {
- return false;
- }
- public override string ToString ()
- {
- return base.ToString ();
- }
- #endregion Public Methods
- #region Private Methods
- protected void CreateItems ()
- {
- creating = true;
- for (int i = 0; i < menu_items.Count; i++)
- menu_items[i].Create ();
- creating = false;
- }
- #endregion Private Methods
- public class MenuItemCollection : IList, ICollection, IEnumerable
- {
- private Menu owner;
- private ArrayList items = new ArrayList ();
- public MenuItemCollection (Menu owner)
- {
- this.owner = owner;
- }
- #region Public Properties
- public virtual int Count {
- get { return items.Count;}
- }
- public virtual bool IsReadOnly {
- get { return false;}
- }
- bool ICollection.IsSynchronized {
- get { return false;}
- }
- object ICollection.SyncRoot {
- get { return this;}
- }
- bool IList.IsFixedSize {
- get { return false;}
- }
- public MenuItem this [int index] {
- get {
- if (index < 0 || index >= Count)
- throw new ArgumentOutOfRangeException ("Index of out range");
- return (MenuItem) items[index];
- }
- }
- object IList.this[int index] {
- get { return items[index]; }
- set { throw new NotSupportedException (); }
- }
- #endregion Public Properties
- #region Public Methods
- public virtual int Add (MenuItem mi)
- {
- mi.parent_menu = owner;
- mi.Index = items.Count;
- items.Add (mi);
- owner.IsDirty = true;
- return items.Count - 1;
- }
- public virtual MenuItem Add (string s)
- {
- MenuItem item = new MenuItem (s);
- Add (item);
- return item;
- }
- public virtual int Add (int index, MenuItem mi)
- {
- if (index < 0 || index >= Count)
- throw new ArgumentOutOfRangeException ("Index of out range");
- ArrayList new_items = new ArrayList (Count + 1);
- for (int i = 0; i < index; i++)
- new_items.Add (items[i]);
- new_items.Add (mi);
- for (int i = index; i < Count; i++)
- new_items.Add (items[i]);
- items = new_items;
- UpdateItemsIndices ();
- owner.IsDirty = true;
- return index;
- }
- public virtual MenuItem Add (string s, EventHandler e)
- {
- MenuItem item = new MenuItem (s, e);
- Add (item);
- return item;
- }
- public virtual MenuItem Add (string s, MenuItem[] items)
- {
- MenuItem item = new MenuItem (s, items);
- Add (item);
- return item;
- }
- public virtual void AddRange (MenuItem[] items)
- {
- if (items == null)
- throw new ArgumentNullException ("items");
- foreach (MenuItem mi in items)
- Add (mi);
- }
- public virtual void Clear ()
- {
- items.Clear ();
- owner.IsDirty = true;
- }
- public bool Contains (MenuItem value)
- {
- return items.Contains (value);
- }
- public virtual void CopyTo (Array dest, int index)
- {
- items.CopyTo (dest, index);
- }
- public virtual IEnumerator GetEnumerator ()
- {
- return items.GetEnumerator ();
- }
- int IList.Add (object value)
- {
- return Add ((MenuItem)value);
- }
- bool IList.Contains (object value)
- {
- return Contains ((MenuItem)value);
- }
- int IList.IndexOf (object value)
- {
- return IndexOf ((MenuItem)value);
- }
- void IList.Insert (int index, object value)
- {
- Add (index, (MenuItem) value);
- }
- void IList.Remove (object value)
- {
- Remove ((MenuItem) value);
- }
- public int IndexOf (MenuItem value)
- {
- return items.IndexOf (value);
- }
- public virtual void Remove (MenuItem item)
- {
- RemoveAt (item.Index);
- }
- public virtual void RemoveAt (int index)
- {
- if (index < 0 || index >= Count)
- throw new ArgumentOutOfRangeException ("Index of out range");
- items.RemoveAt (index);
- UpdateItemsIndices ();
- owner.IsDirty = true;
- }
- #endregion Public Methods
- #region Private Methods
- private void UpdateItemsIndices ()
- {
- for (int i = 0; i < Count; i++) // Recalculate indeces
- ((MenuItem)items[i]).Index = i;
- }
- #endregion Private Methods
- }
- }
- }
|