Menu.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. using System.ComponentModel.Design;
  34. using System.Reflection;
  35. using System.Runtime.InteropServices;
  36. namespace System.Windows.Forms
  37. {
  38. [Designer ("System.Windows.Forms.Design.MenuDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  39. [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
  40. [ListBindable(false)]
  41. public abstract class Menu : Component
  42. {
  43. internal MenuItemCollection menu_items;
  44. internal IntPtr menu_handle = IntPtr.Zero;
  45. internal bool is_dirty = true;
  46. internal bool creating = false;
  47. public const int FindHandle = 0;
  48. public const int FindShortcut = 1;
  49. protected Menu (MenuItem[] items)
  50. {
  51. menu_items = new MenuItemCollection (this);
  52. if (items != null)
  53. menu_items.AddRange (items);
  54. }
  55. #region Public Properties
  56. [BrowsableAttribute(false)]
  57. [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
  58. [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
  59. public IntPtr Handle {
  60. get {
  61. if (IsDirty && creating == false) {
  62. Dispose (true);
  63. }
  64. if (menu_handle == IntPtr.Zero) {
  65. menu_handle = CreateMenuHandle ();
  66. CreateItems ();
  67. IsDirty = false;
  68. }
  69. return menu_handle;
  70. }
  71. }
  72. [BrowsableAttribute(false)]
  73. [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
  74. public virtual bool IsParent {
  75. get {
  76. if (menu_items != null && menu_items.Count > 0)
  77. return true;
  78. else
  79. return false;
  80. }
  81. }
  82. [BrowsableAttribute(false)]
  83. [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
  84. public MenuItem MdiListItem {
  85. get {
  86. throw new NotImplementedException ();
  87. }
  88. }
  89. [BrowsableAttribute(false)]
  90. [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
  91. [MergableProperty(false)]
  92. public MenuItemCollection MenuItems {
  93. get { return menu_items; }
  94. }
  95. #endregion Public Properties
  96. #region Private Properties
  97. internal bool IsDirty {
  98. get { return is_dirty; }
  99. set { is_dirty = value; }
  100. }
  101. #endregion Private Properties
  102. #region Public Methods
  103. protected void CloneMenu (Menu menuSrc)
  104. {
  105. Dispose (true);
  106. menu_items = new MenuItemCollection (this);
  107. for (int i = 0; i < menuSrc.MenuItems.Count ; i++)
  108. menu_items.Add (menuSrc.MenuItems [i]);
  109. }
  110. protected virtual IntPtr CreateMenuHandle ()
  111. {
  112. IntPtr menu;
  113. menu = MenuAPI.CreatePopupMenu (this);
  114. return menu;
  115. }
  116. protected override void Dispose (bool disposing)
  117. {
  118. if (disposing) {
  119. if (menu_handle != IntPtr.Zero)
  120. MenuAPI.DestroyMenu (menu_handle);
  121. menu_handle = IntPtr.Zero;
  122. }
  123. }
  124. // From Microsoft documentation is impossible to guess that
  125. // this method is supossed to do
  126. public MenuItem FindMenuItem (int type, IntPtr value)
  127. {
  128. return null;
  129. }
  130. protected int FindMergePosition (int mergeOrder)
  131. {
  132. int cnt = MenuItems.Count, cur, pos;
  133. for (pos = 0; pos < cnt; ) {
  134. cur = (pos + cnt) /2;
  135. if (MenuItems[cur].MergeOrder > mergeOrder) {
  136. cnt = cur;
  137. } else {
  138. pos = cur +1;
  139. }
  140. }
  141. return pos;
  142. }
  143. public ContextMenu GetContextMenu ()
  144. {
  145. if (this is ContextMenu)
  146. return (ContextMenu) this;
  147. else
  148. return null;
  149. }
  150. public MainMenu GetMainMenu ()
  151. {
  152. if (this is MainMenu)
  153. return (MainMenu) this;
  154. else
  155. return null;
  156. }
  157. public virtual void MergeMenu (Menu menuSrc)
  158. {
  159. if (menuSrc == this)
  160. throw new ArgumentException ("The menu cannot be merged with itself");
  161. for (int i = 0; i < menuSrc.MenuItems.Count; i++){
  162. switch (menuSrc.MenuItems[i].MergeType) {
  163. case MenuMerge.Remove: // Item not included
  164. break;
  165. case MenuMerge.Add:
  166. {
  167. int pos = FindMergePosition (menuSrc.MenuItems[i].MergeOrder);
  168. MenuItems.Add (pos, menuSrc.MenuItems[i].CloneMenu ());
  169. break;
  170. }
  171. case MenuMerge.Replace:
  172. case MenuMerge.MergeItems:
  173. {
  174. int pos = FindMergePosition (menuSrc.MenuItems[i].MergeOrder - 1);
  175. MenuItems.Add (pos, menuSrc.MenuItems[i].CloneMenu ());
  176. break;
  177. }
  178. default:
  179. break;
  180. }
  181. }
  182. }
  183. protected internal virtual bool ProcessCmdKey (ref Message msg, Keys keyData)
  184. {
  185. return false;
  186. }
  187. public override string ToString ()
  188. {
  189. return base.ToString ();
  190. }
  191. #endregion Public Methods
  192. #region Private Methods
  193. internal void CreateItems ()
  194. {
  195. creating = true;
  196. for (int i = 0; i < menu_items.Count; i++)
  197. menu_items[i].Create ();
  198. creating = false;
  199. }
  200. #endregion Private Methods
  201. [ListBindable(false)]
  202. public class MenuItemCollection : IList, ICollection, IEnumerable
  203. {
  204. private Menu owner;
  205. private ArrayList items = new ArrayList ();
  206. public MenuItemCollection (Menu owner)
  207. {
  208. this.owner = owner;
  209. }
  210. #region Public Properties
  211. public virtual int Count {
  212. get { return items.Count;}
  213. }
  214. public virtual bool IsReadOnly {
  215. get { return false;}
  216. }
  217. bool ICollection.IsSynchronized {
  218. get { return false;}
  219. }
  220. object ICollection.SyncRoot {
  221. get { return this;}
  222. }
  223. bool IList.IsFixedSize {
  224. get { return false;}
  225. }
  226. public MenuItem this [int index] {
  227. get {
  228. if (index < 0 || index >= Count)
  229. throw new ArgumentOutOfRangeException ("Index of out range");
  230. return (MenuItem) items[index];
  231. }
  232. }
  233. object IList.this[int index] {
  234. get { return items[index]; }
  235. set { throw new NotSupportedException (); }
  236. }
  237. #endregion Public Properties
  238. #region Public Methods
  239. public virtual int Add (MenuItem mi)
  240. {
  241. mi.parent_menu = owner;
  242. mi.Index = items.Count;
  243. items.Add (mi);
  244. owner.IsDirty = true;
  245. return items.Count - 1;
  246. }
  247. public virtual MenuItem Add (string s)
  248. {
  249. MenuItem item = new MenuItem (s);
  250. Add (item);
  251. return item;
  252. }
  253. public virtual int Add (int index, MenuItem mi)
  254. {
  255. if (index < 0 || index > Count)
  256. throw new ArgumentOutOfRangeException ("Index of out range");
  257. ArrayList new_items = new ArrayList (Count + 1);
  258. for (int i = 0; i < index; i++)
  259. new_items.Add (items[i]);
  260. new_items.Add (mi);
  261. for (int i = index; i < Count; i++)
  262. new_items.Add (items[i]);
  263. items = new_items;
  264. UpdateItemsIndices ();
  265. owner.IsDirty = true;
  266. return index;
  267. }
  268. public virtual MenuItem Add (string s, EventHandler e)
  269. {
  270. MenuItem item = new MenuItem (s, e);
  271. Add (item);
  272. return item;
  273. }
  274. public virtual MenuItem Add (string s, MenuItem[] items)
  275. {
  276. MenuItem item = new MenuItem (s, items);
  277. Add (item);
  278. return item;
  279. }
  280. public virtual void AddRange (MenuItem[] items)
  281. {
  282. if (items == null)
  283. throw new ArgumentNullException ("items");
  284. foreach (MenuItem mi in items)
  285. Add (mi);
  286. }
  287. public virtual void Clear ()
  288. {
  289. items.Clear ();
  290. owner.IsDirty = true;
  291. }
  292. public bool Contains (MenuItem value)
  293. {
  294. return items.Contains (value);
  295. }
  296. public virtual void CopyTo (Array dest, int index)
  297. {
  298. items.CopyTo (dest, index);
  299. }
  300. public virtual IEnumerator GetEnumerator ()
  301. {
  302. return items.GetEnumerator ();
  303. }
  304. int IList.Add (object value)
  305. {
  306. return Add ((MenuItem)value);
  307. }
  308. bool IList.Contains (object value)
  309. {
  310. return Contains ((MenuItem)value);
  311. }
  312. int IList.IndexOf (object value)
  313. {
  314. return IndexOf ((MenuItem)value);
  315. }
  316. void IList.Insert (int index, object value)
  317. {
  318. Add (index, (MenuItem) value);
  319. }
  320. void IList.Remove (object value)
  321. {
  322. Remove ((MenuItem) value);
  323. }
  324. public int IndexOf (MenuItem value)
  325. {
  326. return items.IndexOf (value);
  327. }
  328. public virtual void Remove (MenuItem item)
  329. {
  330. RemoveAt (item.Index);
  331. }
  332. public virtual void RemoveAt (int index)
  333. {
  334. if (index < 0 || index >= Count)
  335. throw new ArgumentOutOfRangeException ("Index of out range");
  336. items.RemoveAt (index);
  337. UpdateItemsIndices ();
  338. owner.IsDirty = true;
  339. }
  340. #endregion Public Methods
  341. #region Private Methods
  342. private void UpdateItemsIndices ()
  343. {
  344. for (int i = 0; i < Count; i++) // Recalculate indeces
  345. ((MenuItem)items[i]).Index = i;
  346. }
  347. #endregion Private Methods
  348. }
  349. }
  350. }