MenuItem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 abstract 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. public MenuItem (): base (null)
  48. {
  49. CommonConstructor (string.Empty);
  50. shortcut = Shortcut.None;
  51. }
  52. public MenuItem (string text) : base (null)
  53. {
  54. CommonConstructor (text);
  55. shortcut = Shortcut.None;
  56. }
  57. public MenuItem (string text, EventHandler e) : base (null)
  58. {
  59. CommonConstructor (text);
  60. shortcut = Shortcut.None;
  61. }
  62. public MenuItem (string text, MenuItem[] items) : base (items)
  63. {
  64. CommonConstructor (text);
  65. shortcut = Shortcut.None;
  66. }
  67. public MenuItem (string text, EventHandler onClick, Shortcut shortcut) : base (null)
  68. {
  69. CommonConstructor (text);
  70. Click += onClick;
  71. this.shortcut = shortcut;
  72. }
  73. public MenuItem (MenuMerge mergeType, int mergeOrder, Shortcut shortcut, string text,
  74. EventHandler onClick, EventHandler onPopup, EventHandler onSelect, MenuItem[] items)
  75. : base (items)
  76. {
  77. CommonConstructor (text);
  78. shortcut = Shortcut.None;
  79. throw new NotImplementedException ();
  80. }
  81. private void CommonConstructor (string text)
  82. {
  83. separator = false;
  84. break_ = false;
  85. bar_break = false;
  86. checked_ = false;
  87. radiocheck = false;
  88. enabled = true;
  89. showshortcut = true;
  90. index = -1;
  91. mnemonic = '\0';
  92. Text = text; // Text can change separator status
  93. }
  94. #region Events
  95. public event EventHandler Click;
  96. //public event DrawItemEventHandler DrawItem;
  97. public event MeasureItemEventHandler MeasureItem;
  98. public event EventHandler Popup;
  99. public event EventHandler Select;
  100. #endregion // Events
  101. #region Public Properties
  102. public bool BarBreak {
  103. get { return break_; }
  104. set { break_ = value; }
  105. }
  106. public bool Break {
  107. get { return bar_break; }
  108. set { bar_break = value; }
  109. }
  110. public bool Checked {
  111. get { return checked_; }
  112. set { checked_ = value; }
  113. }
  114. public bool DefaultItem {
  115. get { return defaut_item; }
  116. set { defaut_item = value; }
  117. }
  118. public bool Enabled {
  119. get { return enabled; }
  120. set { enabled = value; }
  121. }
  122. public int Index {
  123. get { return index; }
  124. set { index = value; }
  125. }
  126. public override bool IsParent {
  127. get {
  128. return IsPopup;
  129. }
  130. }
  131. public bool MdiList {
  132. get { return mdilist; }
  133. set { mdilist = value; }
  134. }
  135. protected int MenuID {
  136. get {
  137. throw new NotImplementedException ();
  138. }
  139. set{
  140. throw new NotImplementedException ();
  141. }
  142. }
  143. public int MergeOrder{
  144. get {
  145. throw new NotImplementedException ();
  146. }
  147. set{
  148. throw new NotImplementedException ();
  149. }
  150. }
  151. public MenuMerge MergeType {
  152. get {
  153. throw new NotImplementedException ();
  154. }
  155. set{
  156. throw new NotImplementedException ();
  157. }
  158. }
  159. public char Mnemonic {
  160. get { return mnemonic; }
  161. }
  162. public bool OwnerDraw {
  163. get {
  164. throw new NotImplementedException ();
  165. }
  166. set{
  167. throw new NotImplementedException ();
  168. }
  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 {
  202. throw new NotImplementedException ();
  203. }
  204. set{
  205. throw new NotImplementedException ();
  206. }
  207. }
  208. #endregion Public Properties
  209. #region Private Properties
  210. internal bool IsPopup {
  211. get {
  212. if (menu_items.Count > 0)
  213. return true;
  214. else
  215. return false;
  216. }
  217. }
  218. internal bool Separator {
  219. get { return separator; }
  220. set { separator = value; }
  221. }
  222. #endregion Private Properties
  223. #region Public Methods
  224. public virtual MenuItem CloneMenu ()
  225. {
  226. throw new NotImplementedException ();
  227. }
  228. protected void CloneMenu (MenuItem menuitem)
  229. {
  230. throw new NotImplementedException ();
  231. }
  232. protected override void Dispose (bool disposing)
  233. {
  234. throw new NotImplementedException ();
  235. }
  236. public virtual void MergeMenu ()
  237. {
  238. throw new NotImplementedException ();
  239. }
  240. public void MergeMenu (MenuItem menuteim)
  241. {
  242. throw new NotImplementedException ();
  243. }
  244. protected virtual void OnClick (EventArgs e)
  245. {
  246. if (Click != null)
  247. Click (this, e);
  248. }
  249. protected virtual void OnDrawItem (DrawItemEventArgs e)
  250. {
  251. }
  252. protected virtual void OnInitMenuPopup (EventArgs e)
  253. {
  254. }
  255. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  256. {
  257. }
  258. protected virtual void OnPopup (EventArgs e)
  259. {
  260. }
  261. protected virtual void OnSelect (EventArgs e)
  262. {
  263. if (Select != null)
  264. Select (this, e);
  265. }
  266. public void PerformClick ()
  267. {
  268. OnClick (EventArgs.Empty);
  269. }
  270. public virtual void PerformSelect ()
  271. {
  272. OnSelect (EventArgs.Empty);
  273. }
  274. public override string ToString ()
  275. {
  276. return "item:" + text;
  277. }
  278. #endregion Public Methods
  279. #region Private Methods
  280. internal void Create ()
  281. {
  282. IntPtr hSubMenu = IntPtr.Zero;
  283. //Console.WriteLine ("MenuItem.Created:" + Text + " parent:" + Parent.menu_handle/* + " " +
  284. // Environment.StackTrace*/);
  285. index = MenuAPI.InsertMenuItem (Parent.menu_handle, -1, true, this, ref hSubMenu);
  286. if (IsPopup) {
  287. Console.WriteLine ("MenuItem.Create Popup:" + hSubMenu);
  288. menu_handle = hSubMenu;
  289. CreateItems ();
  290. }
  291. }
  292. private void ProcessMnemonic ()
  293. {
  294. if (text.Length < 2) {
  295. mnemonic = '\0';
  296. return;
  297. }
  298. bool bPrevAmp = false;
  299. for (int i = 0; i < text.Length -1 ; i++) {
  300. if (text[i] == '&') {
  301. if (bPrevAmp == false && (text[i+1] != '&')) {
  302. mnemonic = Char.ToUpper (text[i+1]);
  303. return;
  304. }
  305. bPrevAmp = true;
  306. }
  307. else
  308. bPrevAmp = false;
  309. }
  310. mnemonic = '\0';
  311. }
  312. private string GetShortCutTextCtrl () { return "Ctrl"; }
  313. internal string GetShortCutText ()
  314. {
  315. //TODO: Complete the table
  316. switch (Shortcut)
  317. {
  318. case Shortcut.Ctrl0:
  319. return GetShortCutTextCtrl () + "+0";
  320. case Shortcut.Ctrl1:
  321. return GetShortCutTextCtrl () + "+1";
  322. case Shortcut.Ctrl2:
  323. return GetShortCutTextCtrl () + "+2";
  324. case Shortcut.Ctrl3:
  325. return GetShortCutTextCtrl () + "+3";
  326. case Shortcut.Ctrl4:
  327. return GetShortCutTextCtrl () + "+4";
  328. case Shortcut.Ctrl5:
  329. return GetShortCutTextCtrl () + "+5";
  330. case Shortcut.Ctrl6:
  331. return GetShortCutTextCtrl () + "+6";
  332. case Shortcut.Ctrl7:
  333. return GetShortCutTextCtrl () + "+7";
  334. case Shortcut.Ctrl8:
  335. return GetShortCutTextCtrl () + "+8";
  336. case Shortcut.Ctrl9:
  337. return GetShortCutTextCtrl () + "+9";
  338. case Shortcut.CtrlA:
  339. return GetShortCutTextCtrl () + "+A";
  340. case Shortcut.CtrlB:
  341. return GetShortCutTextCtrl () + "+B";
  342. case Shortcut.CtrlC:
  343. return GetShortCutTextCtrl () + "+C";
  344. case Shortcut.CtrlD:
  345. return GetShortCutTextCtrl () + "+D";
  346. case Shortcut.CtrlDel:
  347. return GetShortCutTextCtrl () + "+Del";
  348. case Shortcut.CtrlE:
  349. return GetShortCutTextCtrl () + "+E";
  350. case Shortcut.CtrlF:
  351. return GetShortCutTextCtrl () + "+F";
  352. case Shortcut.CtrlF1:
  353. return GetShortCutTextCtrl () + "+F1";
  354. case Shortcut.CtrlF10:
  355. return GetShortCutTextCtrl () + "+F10";
  356. case Shortcut.CtrlF11:
  357. return GetShortCutTextCtrl () + "+F11";
  358. case Shortcut.CtrlF12:
  359. return GetShortCutTextCtrl () + "+F12";
  360. case Shortcut.CtrlF2:
  361. return GetShortCutTextCtrl () + "+F2";
  362. case Shortcut.CtrlF3:
  363. return GetShortCutTextCtrl () + "+F3";
  364. case Shortcut.CtrlF4:
  365. return GetShortCutTextCtrl () + "+F4";
  366. case Shortcut.CtrlF5:
  367. return GetShortCutTextCtrl () + "+F5";
  368. case Shortcut.CtrlF6:
  369. return GetShortCutTextCtrl () + "+F6";
  370. case Shortcut.CtrlF7:
  371. return GetShortCutTextCtrl () + "+F7";
  372. case Shortcut.CtrlF8:
  373. return GetShortCutTextCtrl () + "+F8";
  374. case Shortcut.CtrlF9:
  375. return GetShortCutTextCtrl () + "+F9";
  376. case Shortcut.CtrlG:
  377. return GetShortCutTextCtrl () + "+G";
  378. case Shortcut.CtrlH:
  379. return GetShortCutTextCtrl () + "+H";
  380. case Shortcut.CtrlI:
  381. return GetShortCutTextCtrl () + "+I";
  382. case Shortcut.CtrlIns:
  383. return GetShortCutTextCtrl () + "+Ins";
  384. case Shortcut.CtrlJ:
  385. return GetShortCutTextCtrl () + "+J";
  386. case Shortcut.CtrlK:
  387. return GetShortCutTextCtrl () + "+K";
  388. case Shortcut.CtrlL:
  389. return GetShortCutTextCtrl () + "+L";
  390. case Shortcut.CtrlM:
  391. return GetShortCutTextCtrl () + "+M";
  392. case Shortcut.CtrlN:
  393. return GetShortCutTextCtrl () + "+N";
  394. case Shortcut.CtrlO:
  395. return GetShortCutTextCtrl () + "+O";
  396. case Shortcut.CtrlP:
  397. return GetShortCutTextCtrl () + "+P";
  398. case Shortcut.CtrlQ:
  399. return GetShortCutTextCtrl () + "+Q";
  400. case Shortcut.CtrlR:
  401. return GetShortCutTextCtrl () + "+R";
  402. case Shortcut.CtrlS:
  403. return GetShortCutTextCtrl () + "+S";
  404. default:
  405. return "";
  406. }
  407. }
  408. #endregion Private Methods
  409. }
  410. }