Menu.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. //
  25. // TODO:
  26. // - Merge menus
  27. // - MDI integration
  28. // - ShortCut navigation
  29. //
  30. // NOT COMPLETE
  31. using System.Collections;
  32. using System.ComponentModel;
  33. namespace System.Windows.Forms
  34. {
  35. public abstract class Menu : Component
  36. {
  37. internal MenuItemCollection menu_items;
  38. internal IntPtr menu_handle = IntPtr.Zero;
  39. internal bool is_dirty = true;
  40. internal bool creating = false;
  41. public const int FindHandle = 0;
  42. public const int FindShortcut = 1;
  43. protected Menu (MenuItem[] items)
  44. {
  45. menu_items = new MenuItemCollection (this);
  46. if (items != null)
  47. menu_items.AddRange (items);
  48. }
  49. #region Public Properties
  50. public IntPtr Handle {
  51. get {
  52. if (IsDirty && creating == false) {
  53. Dispose (true);
  54. }
  55. if (menu_handle == IntPtr.Zero) {
  56. menu_handle = CreateMenuHandle ();
  57. CreateItems ();
  58. IsDirty = false;
  59. }
  60. return menu_handle;
  61. }
  62. }
  63. public virtual bool IsParent {
  64. get {
  65. if (menu_items != null && menu_items.Count > 0)
  66. return true;
  67. else
  68. return false;
  69. }
  70. }
  71. public MenuItem MdiListItem {
  72. get {
  73. throw new NotImplementedException ();
  74. }
  75. }
  76. public MenuItemCollection MenuItems {
  77. get { return menu_items; }
  78. }
  79. #endregion Public Properties
  80. #region Private Properties
  81. internal bool IsDirty {
  82. get { return is_dirty; }
  83. set { is_dirty = value; }
  84. }
  85. #endregion Private Properties
  86. #region Public Methods
  87. protected void CloneMenu (Menu menuSrc)
  88. {
  89. Dispose (true);
  90. menu_items = new MenuItemCollection (this);
  91. for (int i = 0; i < menuSrc.MenuItems.Count ; i++)
  92. menu_items.Add (menuSrc.MenuItems [i]);
  93. }
  94. protected virtual IntPtr CreateMenuHandle ()
  95. {
  96. IntPtr menu;
  97. menu = MenuAPI.CreatePopupMenu (this);
  98. return menu;
  99. }
  100. protected override void Dispose (bool disposing)
  101. {
  102. if (disposing) {
  103. if (menu_handle != IntPtr.Zero)
  104. MenuAPI.DestroyMenu (menu_handle);
  105. menu_handle = IntPtr.Zero;
  106. }
  107. }
  108. public MenuItem FindMenuItem (int type, IntPtr value)
  109. {
  110. throw new NotImplementedException ();
  111. }
  112. protected int FindMergePosition (int mergeOrder)
  113. {
  114. throw new NotImplementedException ();
  115. }
  116. public ContextMenu GetContextMenu ()
  117. {
  118. if (this is ContextMenu)
  119. return (ContextMenu) this;
  120. else
  121. return null;
  122. }
  123. public MainMenu GetMainMenu ()
  124. {
  125. if (this is MainMenu)
  126. return (MainMenu) this;
  127. else
  128. return null;
  129. }
  130. public virtual void MergeMenu (Menu menuSrc)
  131. {
  132. if (menuSrc == this)
  133. throw new ArgumentException ("The menu cannot be merged with itself");
  134. }
  135. protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData)
  136. {
  137. return false;
  138. }
  139. public override string ToString ()
  140. {
  141. return base.ToString ();
  142. }
  143. #endregion Public Methods
  144. #region Private Methods
  145. internal void CreateItems ()
  146. {
  147. creating = true;
  148. for (int i = 0; i < menu_items.Count; i++)
  149. menu_items[i].Create ();
  150. creating = false;
  151. }
  152. #endregion Private Methods
  153. public class MenuItemCollection : IList, ICollection, IEnumerable
  154. {
  155. private Menu owner;
  156. private ArrayList items = new ArrayList ();
  157. public MenuItemCollection (Menu owner)
  158. {
  159. this.owner = owner;
  160. }
  161. #region Public Properties
  162. public virtual int Count {
  163. get { return items.Count;}
  164. }
  165. public virtual bool IsReadOnly {
  166. get { return false;}
  167. }
  168. bool ICollection.IsSynchronized {
  169. get { return false;}
  170. }
  171. object ICollection.SyncRoot {
  172. get { return this;}
  173. }
  174. bool IList.IsFixedSize {
  175. get { return false;}
  176. }
  177. public MenuItem this [int index] {
  178. get {
  179. if (index < 0 || index >= Count)
  180. throw new ArgumentOutOfRangeException ("Index of out range");
  181. return (MenuItem) items[index];
  182. }
  183. }
  184. object IList.this[int index] {
  185. get { return items[index]; }
  186. set { throw new NotSupportedException (); }
  187. }
  188. #endregion Public Properties
  189. #region Public Methods
  190. public virtual int Add (MenuItem mi)
  191. {
  192. mi.parent_menu = owner;
  193. mi.Index = items.Count;
  194. items.Add (mi);
  195. owner.IsDirty = true;
  196. return items.Count - 1;
  197. }
  198. public virtual MenuItem Add (string s)
  199. {
  200. MenuItem item = new MenuItem (s);
  201. Add (item);
  202. return item;
  203. }
  204. public virtual int Add (int index, MenuItem mi)
  205. {
  206. if (index < 0 || index >= Count)
  207. throw new ArgumentOutOfRangeException ("Index of out range");
  208. ArrayList new_items = new ArrayList (Count + 1);
  209. for (int i = 0; i < index; i++)
  210. new_items.Add (items[i]);
  211. new_items.Add (mi);
  212. for (int i = index; i < Count; i++)
  213. new_items.Add (items[i]);
  214. items = new_items;
  215. UpdateItemsIndices ();
  216. owner.IsDirty = true;
  217. return index;
  218. }
  219. public virtual MenuItem Add (string s, EventHandler e)
  220. {
  221. MenuItem item = new MenuItem (s, e);
  222. Add (item);
  223. return item;
  224. }
  225. public virtual MenuItem Add (string s, MenuItem[] items)
  226. {
  227. MenuItem item = new MenuItem (s, items);
  228. Add (item);
  229. return item;
  230. }
  231. public virtual void AddRange (MenuItem[] items)
  232. {
  233. if (items == null)
  234. throw new ArgumentNullException ("items");
  235. foreach (MenuItem mi in items)
  236. Add (mi);
  237. }
  238. public virtual void Clear ()
  239. {
  240. items.Clear ();
  241. owner.IsDirty = true;
  242. }
  243. public bool Contains (MenuItem value)
  244. {
  245. return items.Contains (value);
  246. }
  247. public virtual void CopyTo (Array dest, int index)
  248. {
  249. items.CopyTo (dest, index);
  250. }
  251. public virtual IEnumerator GetEnumerator ()
  252. {
  253. return items.GetEnumerator ();
  254. }
  255. int IList.Add (object value)
  256. {
  257. return Add ((MenuItem)value);
  258. }
  259. bool IList.Contains (object value)
  260. {
  261. return Contains ((MenuItem)value);
  262. }
  263. int IList.IndexOf (object value)
  264. {
  265. return IndexOf ((MenuItem)value);
  266. }
  267. void IList.Insert (int index, object value)
  268. {
  269. Add (index, (MenuItem) value);
  270. }
  271. void IList.Remove (object value)
  272. {
  273. Remove ((MenuItem) value);
  274. }
  275. public int IndexOf (MenuItem value)
  276. {
  277. return items.IndexOf (value);
  278. }
  279. public virtual void Remove (MenuItem item)
  280. {
  281. RemoveAt (item.Index);
  282. }
  283. public virtual void RemoveAt (int index)
  284. {
  285. if (index < 0 || index >= Count)
  286. throw new ArgumentOutOfRangeException ("Index of out range");
  287. items.RemoveAt (index);
  288. UpdateItemsIndices ();
  289. owner.IsDirty = true;
  290. }
  291. #endregion Public Methods
  292. #region Private Methods
  293. private void UpdateItemsIndices ()
  294. {
  295. for (int i = 0; i < Count; i++) // Recalculate indeces
  296. ((MenuItem)items[i]).Index = i;
  297. }
  298. #endregion Private Methods
  299. }
  300. }
  301. }