Menu.cs 7.7 KB

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