MenuItem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. //
  26. // NOT COMPLETE
  27. using System.ComponentModel;
  28. using System.Collections;
  29. namespace System.Windows.Forms
  30. {
  31. public class MenuItem : Menu
  32. {
  33. internal Menu parent_menu = null;
  34. internal bool separator;
  35. internal bool break_;
  36. internal bool bar_break;
  37. private Shortcut shortcut;
  38. private string text;
  39. private bool checked_;
  40. private bool radiocheck;
  41. private bool enabled;
  42. private char mnemonic;
  43. private bool showshortcut;
  44. private int index;
  45. private bool mdilist;
  46. private bool defaut_item;
  47. private bool visible;
  48. private bool ownerdraw;
  49. private int menuid;
  50. private int mergeorder;
  51. private MenuMerge mergetype;
  52. public MenuItem (): base (null)
  53. {
  54. CommonConstructor (string.Empty);
  55. shortcut = Shortcut.None;
  56. }
  57. public MenuItem (string text) : base (null)
  58. {
  59. CommonConstructor (text);
  60. shortcut = Shortcut.None;
  61. }
  62. public MenuItem (string text, EventHandler onClick) : base (null)
  63. {
  64. CommonConstructor (text);
  65. shortcut = Shortcut.None;
  66. Click += onClick;
  67. }
  68. public MenuItem (string text, MenuItem[] items) : base (items)
  69. {
  70. CommonConstructor (text);
  71. shortcut = Shortcut.None;
  72. }
  73. public MenuItem (string text, EventHandler onClick, Shortcut shortcut) : base (null)
  74. {
  75. CommonConstructor (text);
  76. Click += onClick;
  77. this.shortcut = shortcut;
  78. }
  79. public MenuItem (MenuMerge mergeType, int mergeOrder, Shortcut shortcut, string text,
  80. EventHandler onClick, EventHandler onPopup, EventHandler onSelect, MenuItem[] items)
  81. : base (items)
  82. {
  83. CommonConstructor (text);
  84. shortcut = shortcut;
  85. mergeorder = mergeOrder;
  86. mergetype = mergeType;
  87. Click += onClick;
  88. Popup += onPopup;
  89. Select += onSelect;
  90. }
  91. private void CommonConstructor (string text)
  92. {
  93. separator = false;
  94. break_ = false;
  95. bar_break = false;
  96. checked_ = false;
  97. radiocheck = false;
  98. enabled = true;
  99. showshortcut = true;
  100. visible = true;
  101. ownerdraw = false;
  102. index = -1;
  103. mnemonic = '\0';
  104. menuid = -1;
  105. mergeorder = 0;
  106. mergetype = MenuMerge.Add;
  107. Text = text; // Text can change separator status
  108. }
  109. #region Events
  110. public event EventHandler Click;
  111. public event DrawItemEventHandler DrawItem;
  112. public event MeasureItemEventHandler MeasureItem;
  113. public event EventHandler Popup;
  114. public event EventHandler Select;
  115. #endregion // Events
  116. #region Public Properties
  117. public bool BarBreak {
  118. get { return break_; }
  119. set { break_ = value; }
  120. }
  121. public bool Break {
  122. get { return bar_break; }
  123. set { bar_break = value; }
  124. }
  125. public bool Checked {
  126. get { return checked_; }
  127. set { checked_ = value; }
  128. }
  129. public bool DefaultItem {
  130. get { return defaut_item; }
  131. set { defaut_item = value; }
  132. }
  133. public bool Enabled {
  134. get { return enabled; }
  135. set { enabled = value; }
  136. }
  137. public int Index {
  138. get { return index; }
  139. set { index = value; }
  140. }
  141. public override bool IsParent {
  142. get { return IsPopup; }
  143. }
  144. public bool MdiList {
  145. get { return mdilist; }
  146. set { mdilist = value; }
  147. }
  148. protected int MenuID {
  149. get { return menuid; }
  150. }
  151. public int MergeOrder {
  152. get { return mergeorder; }
  153. set { mergeorder = value; }
  154. }
  155. public MenuMerge MergeType {
  156. get { return mergetype; }
  157. set {
  158. if (!Enum.IsDefined (typeof (MenuMerge), value))
  159. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for MenuMerge", value));
  160. mergetype = value;
  161. }
  162. }
  163. public char Mnemonic {
  164. get { return mnemonic; }
  165. }
  166. public bool OwnerDraw {
  167. get { return ownerdraw; }
  168. set { ownerdraw = value; }
  169. }
  170. public Menu Parent {
  171. get { return parent_menu;}
  172. }
  173. public bool RadioCheck {
  174. get { return radiocheck; }
  175. set { radiocheck = value; }
  176. }
  177. public Shortcut Shortcut {
  178. get { return shortcut;}
  179. set {
  180. if (!Enum.IsDefined (typeof (Shortcut), value))
  181. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for Shortcut", value));
  182. shortcut = value;
  183. }
  184. }
  185. public bool ShowShortcut {
  186. get { return showshortcut;}
  187. set { showshortcut = value; }
  188. }
  189. public string Text {
  190. get { return text; }
  191. set {
  192. text = value;
  193. if (text == "-")
  194. separator = true;
  195. else
  196. separator = false;
  197. ProcessMnemonic ();
  198. }
  199. }
  200. public bool Visible {
  201. get { return visible;}
  202. set { visible = value; }
  203. }
  204. #endregion Public Properties
  205. #region Private Properties
  206. internal bool IsPopup {
  207. get {
  208. if (menu_items.Count > 0)
  209. return true;
  210. else
  211. return false;
  212. }
  213. }
  214. internal bool Separator {
  215. get { return separator; }
  216. set { separator = value; }
  217. }
  218. #endregion Private Properties
  219. #region Public Methods
  220. public virtual MenuItem CloneMenu ()
  221. {
  222. MenuItem item = new MenuItem ();
  223. item.CloneMenu (item);
  224. return item;
  225. }
  226. protected void CloneMenu (MenuItem menuitem)
  227. {
  228. base.CloneMenu (menuitem); // Copy subitems
  229. // Properties
  230. BarBreak = menuitem.BarBreak;
  231. Break = menuitem.Break;
  232. Checked = menuitem.Checked;
  233. DefaultItem = menuitem.DefaultItem;
  234. Enabled = menuitem.Enabled;
  235. MergeOrder = menuitem.MergeOrder;
  236. MergeType = menuitem.MergeType;
  237. OwnerDraw = menuitem.OwnerDraw;
  238. //Parent = menuitem.Parent;
  239. RadioCheck = menuitem.RadioCheck;
  240. Shortcut = menuitem.Shortcut;
  241. ShowShortcut = menuitem.ShowShortcut;
  242. Text = menuitem.Text;
  243. Visible = menuitem.Visible;
  244. // Events
  245. Click = menuitem.Click;
  246. DrawItem = menuitem.DrawItem;
  247. MeasureItem = menuitem.MeasureItem;
  248. Popup = menuitem.Popup;
  249. Select = menuitem.Select;
  250. }
  251. protected override void Dispose (bool disposing)
  252. {
  253. base.Dispose (disposing);
  254. }
  255. public virtual void MergeMenu ()
  256. {
  257. base.MergeMenu (this);
  258. }
  259. public void MergeMenu (MenuItem menuitem)
  260. {
  261. base.MergeMenu (menuitem);
  262. }
  263. protected virtual void OnClick (EventArgs e)
  264. {
  265. if (Click != null)
  266. Click (this, e);
  267. }
  268. protected virtual void OnDrawItem (DrawItemEventArgs e)
  269. {
  270. if (DrawItem != null)
  271. DrawItem (this, e);
  272. }
  273. protected virtual void OnInitMenuPopup (EventArgs e)
  274. {
  275. OnPopup (e);
  276. }
  277. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  278. {
  279. if (MeasureItem != null)
  280. MeasureItem (this, e);
  281. }
  282. protected virtual void OnPopup (EventArgs e)
  283. {
  284. if (Popup != null)
  285. Popup (this, e);
  286. }
  287. protected virtual void OnSelect (EventArgs e)
  288. {
  289. if (Select != null)
  290. Select (this, e);
  291. }
  292. public void PerformClick ()
  293. {
  294. OnClick (EventArgs.Empty);
  295. }
  296. public virtual void PerformSelect ()
  297. {
  298. OnSelect (EventArgs.Empty);
  299. }
  300. public override string ToString ()
  301. {
  302. return "item:" + text;
  303. }
  304. #endregion Public Methods
  305. #region Private Methods
  306. internal void Create ()
  307. {
  308. IntPtr hSubMenu = IntPtr.Zero;
  309. menuid = index = MenuAPI.InsertMenuItem (Parent.Handle, -1, true, this, ref hSubMenu);
  310. IsDirty = false;
  311. if (IsPopup) {
  312. menu_handle = hSubMenu;
  313. CreateItems ();
  314. }
  315. }
  316. private void ProcessMnemonic ()
  317. {
  318. if (text.Length < 2) {
  319. mnemonic = '\0';
  320. return;
  321. }
  322. bool bPrevAmp = false;
  323. for (int i = 0; i < text.Length -1 ; i++) {
  324. if (text[i] == '&') {
  325. if (bPrevAmp == false && (text[i+1] != '&')) {
  326. mnemonic = Char.ToUpper (text[i+1]);
  327. return;
  328. }
  329. bPrevAmp = true;
  330. }
  331. else
  332. bPrevAmp = false;
  333. }
  334. mnemonic = '\0';
  335. }
  336. private string GetShortCutTextCtrl () { return "Ctrl"; }
  337. private string GetShortCutTextAlt () { return "Alt"; }
  338. private string GetShortCutTextShift () { return "Shift"; }
  339. internal string GetShortCutText ()
  340. {
  341. /* Ctrl+A - Ctrl+Z */
  342. if (Shortcut >= Shortcut.CtrlA && Shortcut <= Shortcut.CtrlZ)
  343. return GetShortCutTextCtrl () + "+" + (char)((int) 'A' + (int)(Shortcut - Shortcut.CtrlA));
  344. /* Alt+0 - Alt+9 */
  345. if (Shortcut >= Shortcut.Alt0 && Shortcut <= Shortcut.Alt9)
  346. return GetShortCutTextAlt () + "+" + (char)((int) '0' + (int)(Shortcut - Shortcut.Alt0));
  347. /* Alt+F1 - Alt+F2 */
  348. if (Shortcut >= Shortcut.AltF1 && Shortcut <= Shortcut.AltF9)
  349. return GetShortCutTextAlt () + "+F" + (char)((int) '1' + (int)(Shortcut - Shortcut.AltF1));
  350. /* Ctrl+0 - Ctrl+9 */
  351. if (Shortcut >= Shortcut.Ctrl0 && Shortcut <= Shortcut.Ctrl9)
  352. return GetShortCutTextCtrl () + "+" + (char)((int) '0' + (int)(Shortcut - Shortcut.Ctrl0));
  353. /* Ctrl+F0 - Ctrl+F9 */
  354. if (Shortcut >= Shortcut.CtrlF1 && Shortcut <= Shortcut.CtrlF9)
  355. return GetShortCutTextCtrl () + "+F" + (char)((int) '1' + (int)(Shortcut - Shortcut.CtrlF1));
  356. /* Ctrl+Shift+0 - Ctrl+Shift+9 */
  357. if (Shortcut >= Shortcut.CtrlShift0 && Shortcut <= Shortcut.CtrlShift9)
  358. return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+" + (char)((int) '0' + (int)(Shortcut - Shortcut.CtrlShift0));
  359. /* Ctrl+Shift+A - Ctrl+Shift+Z */
  360. if (Shortcut >= Shortcut.CtrlShiftA && Shortcut <= Shortcut.CtrlShiftZ)
  361. return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+" + (char)((int) 'A' + (int)(Shortcut - Shortcut.CtrlShiftA));
  362. /* Ctrl+Shift+F1 - Ctrl+Shift+F9 */
  363. if (Shortcut >= Shortcut.CtrlShiftF1 && Shortcut <= Shortcut.CtrlShiftF9)
  364. return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+F" + (char)((int) '1' + (int)(Shortcut - Shortcut.CtrlShiftF1));
  365. /* F1 - F9 */
  366. if (Shortcut >= Shortcut.F1 && Shortcut <= Shortcut.F9)
  367. return "F" + (char)((int) '1' + (int)(Shortcut - Shortcut.F1));
  368. /* Shift+F1 - Shift+F9 */
  369. if (Shortcut >= Shortcut.ShiftF1 && Shortcut <= Shortcut.ShiftF9)
  370. return GetShortCutTextShift () + "+F" + (char)((int) '1' + (int)(Shortcut - Shortcut.ShiftF1));
  371. /* Special cases */
  372. switch (Shortcut) {
  373. case Shortcut.AltBksp:
  374. return "AltBksp";
  375. case Shortcut.AltF10:
  376. return GetShortCutTextAlt () + "+F10";
  377. case Shortcut.AltF11:
  378. return GetShortCutTextAlt () + "+F11";
  379. case Shortcut.AltF12:
  380. return GetShortCutTextAlt () + "+F12";
  381. case Shortcut.CtrlDel:
  382. return GetShortCutTextCtrl () + "+Del";
  383. case Shortcut.CtrlF10:
  384. return GetShortCutTextCtrl () + "+F10";
  385. case Shortcut.CtrlF11:
  386. return GetShortCutTextCtrl () + "+F11";
  387. case Shortcut.CtrlF12:
  388. return GetShortCutTextCtrl () + "+F12";
  389. case Shortcut.CtrlIns:
  390. return GetShortCutTextCtrl () + "+Ins";
  391. case Shortcut.CtrlShiftF10:
  392. return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+F10";
  393. case Shortcut.CtrlShiftF11:
  394. return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+F11";
  395. case Shortcut.CtrlShiftF12:
  396. return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+F12";
  397. case Shortcut.Del:
  398. return "Del";
  399. case Shortcut.F10:
  400. return "F10";
  401. case Shortcut.F11:
  402. return "F11";
  403. case Shortcut.F12:
  404. return "F12";
  405. case Shortcut.Ins:
  406. return "Ins";
  407. case Shortcut.None:
  408. return "None";
  409. case Shortcut.ShiftDel:
  410. return GetShortCutTextShift () + "+Del";
  411. case Shortcut.ShiftF10:
  412. return GetShortCutTextShift () + "+F10";
  413. case Shortcut.ShiftF11:
  414. return GetShortCutTextShift () + "+F11";
  415. case Shortcut.ShiftF12:
  416. return GetShortCutTextShift () + "+F12";
  417. case Shortcut.ShiftIns:
  418. return GetShortCutTextShift () + "+Ins";
  419. default:
  420. break;
  421. }
  422. return "";
  423. }
  424. #endregion Private Methods
  425. }
  426. }