MenuAPI.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. // NOT COMPLETE
  26. using System.Drawing;
  27. using System.Drawing.Text;
  28. using System.Collections;
  29. namespace System.Windows.Forms
  30. {
  31. /*
  32. This class mimics the Win32 API Menu functionality
  33. When writing this code the Wine project was of great help to
  34. understand the logic behind some Win32 issues. Thanks to them. Jordi,
  35. */
  36. internal class MenuAPI
  37. {
  38. static StringFormat string_format_text = new StringFormat ();
  39. static StringFormat string_format_shortcut = new StringFormat ();
  40. static StringFormat string_format_menubar_text = new StringFormat ();
  41. static ArrayList menu_list = new ArrayList ();
  42. static Font MENU_FONT = new Font (FontFamily.GenericSansSerif, 8.25f);
  43. static int POPUP_ARROW_WITDH;
  44. static int POPUP_ARROW_HEIGHT;
  45. const int SEPARATOR_HEIGHT = 5;
  46. const int SM_CXBORDER = 1;
  47. const int SM_CYBORDER = 1;
  48. const int SM_CXMENUCHECK = 14; // Width of the menu check
  49. const int SM_CYMENUCHECK = 14; // Height of the menu check
  50. const int SM_CXARROWCHECK = 16; // Width of the arrow
  51. const int SM_CYARROWCHECK = 16; // Height of the arrow
  52. const int SM_CYMENU = 18; // Minimum height of a menu
  53. const int MENU_TAB_SPACE = 8; // Pixels added to the width of an item because of a tab
  54. const int MENU_BAR_ITEMS_SPACE = 8; // Space between menu bar items
  55. public class MENU
  56. {
  57. public MF Flags; // Menu flags (MF_POPUP, MF_SYSMENU)
  58. public int Width; // Width of the whole menu
  59. public int Height; // Height of the whole menu
  60. public Control Wnd; // In a Popup menu is the PopupWindow and in a MenuBar the Form
  61. public ArrayList items; // Array of menu items
  62. public int FocusedItem; // Currently focused item
  63. public IntPtr hParent;
  64. public MENUITEM SelectedItem; // Currently selected item
  65. public bool bMenubar;
  66. public bool bTracking;
  67. public Menu menu; // SWF.Menu
  68. public MENU (Menu menu_obj)
  69. {
  70. Wnd = null;
  71. hParent = IntPtr.Zero;
  72. items = new ArrayList ();
  73. Flags = MF.MF_INSERT;
  74. Width = Height = FocusedItem = 0;
  75. bMenubar = false;
  76. bTracking = false;
  77. menu = menu_obj;
  78. }
  79. }
  80. public class MENUITEM
  81. {
  82. public MenuItem item;
  83. public Rectangle rect;
  84. public MF fState;
  85. public int wID;
  86. public IntPtr hSubMenu;
  87. public int xTab;
  88. public int pos; /* Position in the menuitems array*/
  89. public MENUITEM ()
  90. {
  91. xTab = 0;
  92. wID = 0;
  93. pos = 0;
  94. rect = new Rectangle ();
  95. }
  96. };
  97. public class TRACKER
  98. {
  99. public IntPtr hCurrentMenu;
  100. public IntPtr hTopMenu;
  101. public TRACKER ()
  102. {
  103. hCurrentMenu = hTopMenu = IntPtr.Zero;
  104. }
  105. };
  106. public enum MenuMouseEvent
  107. {
  108. Down,
  109. Move,
  110. }
  111. internal enum ItemNavigation
  112. {
  113. First,
  114. Last,
  115. Next,
  116. Previous,
  117. }
  118. internal enum MF
  119. {
  120. MF_INSERT = 0x0,
  121. MF_APPEND = 0x100,
  122. MF_DELETE = 0x200,
  123. MF_REMOVE = 0x1000,
  124. MF_BYCOMMAND = 0,
  125. MF_BYPOSITION = 0x400,
  126. MF_SEPARATOR = 0x800,
  127. MF_ENABLED = 0,
  128. MF_GRAYED = 1,
  129. MF_DISABLED = 2,
  130. MF_UNCHECKED = 0,
  131. MF_CHECKED = 8,
  132. MF_USECHECKBITMAPS = 0x200,
  133. MF_STRING = 0,
  134. MF_BITMAP = 4,
  135. MF_OWNERDRAW = 0x100,
  136. MF_POPUP = 0x10,
  137. MF_MENUBARBREAK = 0x20,
  138. MF_MENUBREAK = 0x40,
  139. MF_UNHILITE = 0,
  140. MF_HILITE = 0x80,
  141. MF_DEFAULT = 0x1000,
  142. MF_SYSMENU = 0x2000,
  143. MF_HELP = 0x4000,
  144. MF_RIGHTJUSTIFY = 0x4000,
  145. MF_MENUBAR = 0x8000 // Internal
  146. }
  147. static MenuAPI ()
  148. {
  149. string_format_text.LineAlignment = StringAlignment.Center;
  150. string_format_text.Alignment = StringAlignment.Near;
  151. string_format_text.HotkeyPrefix = HotkeyPrefix.Show;
  152. string_format_shortcut.LineAlignment = StringAlignment.Center;
  153. string_format_shortcut.Alignment = StringAlignment.Far;
  154. string_format_menubar_text.LineAlignment = StringAlignment.Center;
  155. string_format_menubar_text.Alignment = StringAlignment.Center;
  156. string_format_menubar_text.HotkeyPrefix = HotkeyPrefix.Show;
  157. }
  158. static public IntPtr StoreMenuID (MENU menu)
  159. {
  160. int id = menu_list.Add (menu);
  161. return (IntPtr)(id + 1);
  162. }
  163. static public MENU GetMenuFromID (IntPtr ptr)
  164. {
  165. int id = (int)ptr;
  166. id = id - 1;
  167. if (menu_list[id] == null) // It has been delete it
  168. return null;
  169. return (MENU) menu_list[id];
  170. }
  171. static public IntPtr CreateMenu (Menu menu_obj)
  172. {
  173. MENU menu = new MENU (menu_obj);
  174. return StoreMenuID (menu);
  175. }
  176. static public IntPtr CreatePopupMenu (Menu menu_obj)
  177. {
  178. MENU popMenu = new MENU (menu_obj);
  179. popMenu.Flags |= MF.MF_POPUP;
  180. return StoreMenuID (popMenu);
  181. }
  182. static public int InsertMenuItem (IntPtr hMenu, int uItem, bool fByPosition, MenuItem item, ref IntPtr hSubMenu)
  183. {
  184. int id;
  185. if (fByPosition == false)
  186. throw new NotImplementedException ();
  187. MENU menu = GetMenuFromID (hMenu);
  188. if ((uint)uItem > menu.items.Count)
  189. uItem = menu.items.Count;
  190. MENUITEM menu_item = new MENUITEM ();
  191. menu_item.item = item;
  192. if (item.IsPopup) {
  193. menu_item.hSubMenu = CreatePopupMenu (menu_item.item);
  194. MENU submenu = GetMenuFromID (menu_item.hSubMenu);
  195. submenu.hParent = hMenu;
  196. }
  197. else
  198. menu_item.hSubMenu = IntPtr.Zero;
  199. hSubMenu = menu_item.hSubMenu;
  200. id = menu.items.Count;
  201. menu_item.pos = menu.items.Count;
  202. menu.items.Insert (uItem, menu_item);
  203. return id;
  204. }
  205. // The Point object contains screen coordinates
  206. static public bool TrackPopupMenu (IntPtr hTopMenu, IntPtr hMenu, Point pnt, bool bMenubar, Control Wnd)
  207. {
  208. TRACKER tracker = new TRACKER ();
  209. MENU top_menu = GetMenuFromID (hTopMenu);
  210. MENU menu = null;
  211. if (hMenu == IntPtr.Zero) // No submenus to track
  212. return true;
  213. menu = GetMenuFromID (hMenu);
  214. Console.WriteLine ("TrackPopupMenu hTopMenu: {0} hMenu:{1} bMenubar:{2} top_menu.bMenubar: {3} menu.bMenubar: {4}",hTopMenu,
  215. hMenu, bMenubar, top_menu.bMenubar, menu.bMenubar);
  216. menu.Wnd = new PopUpWindow (hMenu, tracker);
  217. tracker.hCurrentMenu = hMenu;
  218. tracker.hTopMenu = hTopMenu;
  219. MENUITEM select_item = GetNextItem (hMenu, ItemNavigation.First);
  220. if (select_item != null) {
  221. MenuAPI.SelectItem (hMenu, select_item, false, tracker);
  222. }
  223. // Make sure the menu is always visible and does not 'leave' the screen
  224. // What is menu.Width/Height? It seemed to be 0/0
  225. if ((pnt.X + menu.Wnd.Width) > SystemInformation.WorkingArea.Width) {
  226. pnt.X -= menu.Wnd.Width;
  227. }
  228. if ((pnt.X + menu.Wnd.Height) > SystemInformation.WorkingArea.Height) {
  229. pnt.Y -= menu.Wnd.Height;
  230. }
  231. menu.Wnd.Location = menu.Wnd.PointToClient (pnt);
  232. if (menu.menu.IsDirty) {
  233. menu.items.Clear ();
  234. menu.menu.CreateItems ();
  235. ((PopUpWindow)menu.Wnd).RefreshItems ();
  236. menu.menu.IsDirty = false;
  237. }
  238. ((PopUpWindow)menu.Wnd).ShowWindow ();
  239. Application.Run ();
  240. if (menu.Wnd == null) {
  241. menu.Wnd.Dispose ();
  242. menu.Wnd = null;
  243. }
  244. return true;
  245. }
  246. /*
  247. Menu drawing API
  248. */
  249. static public void CalcItemSize (Graphics dc, MENUITEM item, int y, int x, bool menuBar)
  250. {
  251. item.rect.Y = y;
  252. item.rect.X = x;
  253. if (item.item.Visible == false)
  254. return;
  255. if (item.item.Separator == true) {
  256. item.rect.Height = SEPARATOR_HEIGHT / 2;
  257. item.rect.Width = -1;
  258. return;
  259. }
  260. SizeF size;
  261. size = dc.MeasureString (item.item.Text, MENU_FONT);
  262. item.rect.Width = (int) size.Width;
  263. item.rect.Height = (int) size.Height;
  264. if (!menuBar) {
  265. if (item.item.Shortcut != Shortcut.None && item.item.ShowShortcut) {
  266. item.xTab = SM_CXMENUCHECK + MENU_TAB_SPACE + (int) size.Width;
  267. size = dc.MeasureString (" " + item.item.GetShortCutText (), MENU_FONT);
  268. item.rect.Width += MENU_TAB_SPACE + (int) size.Width;
  269. }
  270. item.rect.Width += 4 + (SM_CXMENUCHECK * 2);
  271. }
  272. else {
  273. item.rect.Width += MENU_BAR_ITEMS_SPACE;
  274. x += item.rect.Width;
  275. }
  276. if (item.rect.Height < SM_CYMENU - 1)
  277. item.rect.Height = SM_CYMENU - 1;
  278. }
  279. static public void CalcPopupMenuSize (Graphics dc, IntPtr hMenu)
  280. {
  281. int x = 3;
  282. int start = 0;
  283. int i, n, y, max;
  284. MENU menu = GetMenuFromID (hMenu);
  285. menu.Height = 0;
  286. while (start < menu.items.Count) {
  287. y = 2;
  288. max = 0;
  289. for (i = start; i < menu.items.Count; i++) {
  290. MENUITEM item = (MENUITEM) menu.items[i];
  291. if ((i != start) && (item.item.Break || item.item.BarBreak))
  292. break;
  293. CalcItemSize (dc, item, y, x, false);
  294. y += item.rect.Height;
  295. if (item.rect.Width > max)
  296. max = item.rect.Width;
  297. }
  298. // Reemplace the -1 by the menu width (separators)
  299. for (n = start; n < i; n++, start++) {
  300. MENUITEM item = (MENUITEM) menu.items[n];
  301. item.rect.Width = max;
  302. }
  303. if (y > menu.Height)
  304. menu.Height = y;
  305. x+= max;
  306. }
  307. menu.Width = x;
  308. //space for border
  309. menu.Width += 2;
  310. menu.Height += 2;
  311. menu.Width += SM_CXBORDER;
  312. menu.Height += SM_CYBORDER;
  313. }
  314. static public void DrawMenuItem (Graphics dc, MENUITEM item, int menu_height, bool menuBar)
  315. {
  316. StringFormat string_format;
  317. if (item.item.Visible == false)
  318. return;
  319. if (menuBar)
  320. string_format = string_format_menubar_text;
  321. else
  322. string_format = string_format_text;
  323. if (item.item.Separator == true) {
  324. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
  325. item.rect.X, item.rect.Y, item.rect.X + item.rect.Width, item.rect.Y);
  326. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
  327. item.rect.X, item.rect.Y + 1, item.rect.X + item.rect.Width, item.rect.Y + 1);
  328. return;
  329. }
  330. Rectangle rect_text = item.rect;
  331. if (!menuBar)
  332. rect_text.X += SM_CXMENUCHECK;
  333. if (item.item.BarBreak) { /* Draw vertical break bar*/
  334. Rectangle rect = item.rect;
  335. rect.Y++;
  336. rect.Width = 3;
  337. rect.Height = menu_height - 6;
  338. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
  339. rect.X, rect.Y , rect.X, rect.Y + rect.Height);
  340. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
  341. rect.X + 1, rect.Y , rect.X +1, rect.Y + rect.Height);
  342. }
  343. if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  344. Rectangle rect = item.rect;
  345. rect.X++;
  346. rect.Width -=2;
  347. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  348. (ThemeEngine.Current.ColorHilight), rect);
  349. }
  350. if (item.item.Enabled) {
  351. Color color_text;
  352. if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE)
  353. color_text = ThemeEngine.Current.ColorHilightText;
  354. else
  355. color_text = ThemeEngine.Current.ColorMenuText;
  356. dc.DrawString (item.item.Text, MENU_FONT,
  357. ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
  358. rect_text, string_format);
  359. if (!menuBar && item.item.Shortcut != Shortcut.None && item.item.ShowShortcut) {
  360. string str = item.item.GetShortCutText ();
  361. Rectangle rect = rect_text;
  362. rect.X = item.xTab;
  363. rect.Width -= item.xTab;
  364. dc.DrawString (str, MENU_FONT, ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
  365. rect, string_format_shortcut);
  366. }
  367. }
  368. else {
  369. ControlPaint.DrawStringDisabled (dc,
  370. item.item.Text, MENU_FONT, Color.Black, rect_text,
  371. string_format);
  372. }
  373. /* Draw arrow */
  374. if (menuBar == false && item.item.IsPopup) {
  375. Bitmap bmp = new Bitmap (SM_CXARROWCHECK, SM_CYARROWCHECK);
  376. Graphics gr = Graphics.FromImage (bmp);
  377. Rectangle rect_arrow = new Rectangle (0, 0, SM_CXARROWCHECK, SM_CYARROWCHECK);
  378. ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Arrow);
  379. bmp.MakeTransparent ();
  380. dc.DrawImage (bmp, item.rect.X + item.rect.Width - SM_CXARROWCHECK,
  381. item.rect.Y + ((item.rect.Height - SM_CYARROWCHECK) /2));
  382. gr.Dispose ();
  383. bmp.Dispose ();
  384. }
  385. /* Draw checked or radio */
  386. if (menuBar == false && item.item.Checked) {
  387. Rectangle area = item.rect;
  388. Bitmap bmp = new Bitmap (SM_CXMENUCHECK, SM_CYMENUCHECK);
  389. Graphics gr = Graphics.FromImage (bmp);
  390. Rectangle rect_arrow = new Rectangle (0, 0, SM_CXMENUCHECK, SM_CYMENUCHECK);
  391. if (item.item.RadioCheck)
  392. ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Bullet);
  393. else
  394. ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Checkmark);
  395. bmp.MakeTransparent ();
  396. dc.DrawImage (bmp, area.X, item.rect.Y + ((item.rect.Height - SM_CYMENUCHECK) / 2));
  397. gr.Dispose ();
  398. bmp.Dispose ();
  399. }
  400. }
  401. static public void DrawPopupMenu (Graphics dc, IntPtr hMenu, Rectangle cliparea, Rectangle rect)
  402. {
  403. MENU menu = GetMenuFromID (hMenu);
  404. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  405. (ThemeEngine.Current.ColorMenu), cliparea);
  406. /* Draw menu borders */
  407. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorHilightText),
  408. rect.X, rect.Y, rect.X + rect.Width, rect.Y);
  409. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorHilightText),
  410. rect.X, rect.Y, rect.X, rect.Y + rect.Height);
  411. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
  412. rect.X + rect.Width - 1 , rect.Y , rect.X + rect.Width - 1, rect.Y + rect.Height);
  413. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonDkShadow),
  414. rect.X + rect.Width, rect.Y , rect.X + rect.Width, rect.Y + rect.Height);
  415. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
  416. rect.X , rect.Y + rect.Height - 1 , rect.X + rect.Width - 1, rect.Y + rect.Height -1);
  417. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonDkShadow),
  418. rect.X , rect.Y + rect.Height, rect.X + rect.Width - 1, rect.Y + rect.Height);
  419. for (int i = 0; i < menu.items.Count; i++)
  420. if (cliparea.IntersectsWith (((MENUITEM) menu.items[i]).rect)) {
  421. DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, menu.bMenubar);
  422. }
  423. }
  424. // Updates the menu rect and returns the height
  425. static public int MenuBarCalcSize (Graphics dc, IntPtr hMenu, int width)
  426. {
  427. int x = 0;
  428. int i = 0;
  429. int y = 0;
  430. MENU menu = GetMenuFromID (hMenu);
  431. menu.Height = 0;
  432. MENUITEM item;
  433. while (i < menu.items.Count) {
  434. item = (MENUITEM) menu.items[i];
  435. CalcItemSize (dc, item, y, x, true);
  436. i = i + 1;
  437. if (x + item.rect.Width > width) {
  438. item.rect.X = 0;
  439. y += item.rect.Height;
  440. item.rect.Y = y;
  441. x = 0;
  442. }
  443. x += item.rect.Width;
  444. item.fState |= MF.MF_MENUBAR;
  445. if (y + item.rect.Height > menu.Height)
  446. menu.Height = item.rect.Height + y;
  447. }
  448. menu.Width = width;
  449. return menu.Height;
  450. }
  451. // Draws a menu bar in a Window
  452. static public void DrawMenuBar (Graphics dc, IntPtr hMenu, Rectangle rect)
  453. {
  454. MENU menu = GetMenuFromID (hMenu);
  455. Rectangle rect_menu = new Rectangle ();
  456. if (menu.Height == 0)
  457. MenuBarCalcSize (dc, hMenu, rect_menu.Width);
  458. rect.Height = menu.Height;
  459. rect.Width = menu.Width;
  460. for (int i = 0; i < menu.items.Count; i++)
  461. DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, true);
  462. }
  463. /*
  464. Menu handeling API
  465. */
  466. static public MENUITEM FindItemByCoords (IntPtr hMenu, Point pt)
  467. {
  468. MENU menu = GetMenuFromID (hMenu);
  469. for (int i = 0; i < menu.items.Count; i++) {
  470. MENUITEM item = (MENUITEM) menu.items[i];
  471. if (item.rect.Contains (pt)) {
  472. return item;
  473. }
  474. }
  475. return null;
  476. }
  477. // Get the current selected item
  478. static public MENUITEM GetSelected (IntPtr hMenu)
  479. {
  480. MENU menu = GetMenuFromID (hMenu);
  481. MENUITEM it;
  482. /* Loop all items */
  483. for (int i = 0; i < menu.items.Count; i++) {
  484. it = (MENUITEM) menu.items[i];
  485. if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  486. return it;
  487. }
  488. }
  489. return null;
  490. }
  491. static public void UnSelectItem (IntPtr hMenu, MENUITEM item)
  492. {
  493. MENU menu = GetMenuFromID (hMenu);
  494. if (item == null)
  495. return;
  496. item.fState = item.fState & ~MF.MF_HILITE;
  497. menu.Wnd.Invalidate (item.rect);
  498. }
  499. // Select the item and unselect the previous selecte item
  500. static public void SelectItem (IntPtr hMenu, MENUITEM item, bool execute, TRACKER tracker)
  501. {
  502. MENU menu = GetMenuFromID (hMenu);
  503. MENUITEM previous_selitem = GetSelected (hMenu);
  504. /* Already selected */
  505. if (previous_selitem != null && item.rect == previous_selitem.rect) {
  506. return;
  507. }
  508. UnSelectItem (hMenu, previous_selitem);
  509. // If the previous item had subitems, hide them
  510. if (previous_selitem != null && previous_selitem.item.IsPopup)
  511. HideSubPopups (hMenu);
  512. if (tracker.hCurrentMenu != hMenu) {
  513. menu.Wnd.Capture = true;
  514. tracker.hCurrentMenu = hMenu;
  515. }
  516. menu.SelectedItem = item;
  517. item.fState |= MF.MF_HILITE;
  518. menu.Wnd.Invalidate (item.rect);
  519. item.item.PerformSelect ();
  520. if (execute)
  521. ExecFocusedItem (hMenu, item, tracker);
  522. }
  523. // Used when the user executes the action of an item (press enter, shortcut)
  524. // or a sub-popup menu has to be shown
  525. static public void ExecFocusedItem (IntPtr hMenu, MENUITEM item, TRACKER tracker)
  526. {
  527. if (item.item.Enabled == false)
  528. return;
  529. if (item.item.IsPopup) {
  530. ShowSubPopup (hMenu, item.hSubMenu, item, tracker);
  531. }
  532. else {
  533. // Execute function
  534. }
  535. }
  536. // Create a popup window and show it or only show it if it is already created
  537. static public void ShowSubPopup (IntPtr hParent, IntPtr hMenu, MENUITEM item, TRACKER tracker)
  538. {
  539. MENU menu = GetMenuFromID (hMenu);
  540. Point pnt = new Point ();
  541. if (item.item.Enabled == false)
  542. return;
  543. MENU menu_parent = GetMenuFromID (hParent);
  544. ((PopUpWindow)menu_parent.Wnd).LostFocus ();
  545. tracker.hCurrentMenu = hMenu;
  546. if (menu.Wnd == null)
  547. menu.Wnd = new PopUpWindow (hMenu, tracker);
  548. pnt.X = item.rect.X + item.rect.Width;
  549. pnt.Y = item.rect.Y + 1;
  550. pnt = menu_parent.Wnd.PointToScreen (pnt);
  551. menu.Wnd.Location = pnt;
  552. MENUITEM select_item = GetNextItem (hMenu, ItemNavigation.First);
  553. if (select_item != null)
  554. MenuAPI.SelectItem (hMenu, select_item, false, tracker);
  555. ((PopUpWindow)menu.Wnd).ShowWindow ();
  556. Console.WriteLine ("ShowSubPopup end {0} {1}", ((PopUpWindow)menu.Wnd).Location,
  557. ((PopUpWindow)menu.Wnd).Size);
  558. }
  559. /* Hides all the submenus open in a menu */
  560. static public void HideSubPopups (IntPtr hMenu)
  561. {
  562. MENU menu = GetMenuFromID (hMenu);
  563. MENUITEM item;
  564. for (int i = 0; i < menu.items.Count; i++) {
  565. item = (MENUITEM) menu.items[i];
  566. if (!item.item.IsPopup)
  567. continue;
  568. MENU sub_menu = GetMenuFromID (item.hSubMenu);
  569. if (sub_menu.Wnd != null) {
  570. HideSubPopups (item.hSubMenu);
  571. ((PopUpWindow)sub_menu.Wnd).Hide ();
  572. }
  573. }
  574. }
  575. static public void DestroyMenu (IntPtr hMenu)
  576. {
  577. if (hMenu == IntPtr.Zero)
  578. return;
  579. MENU menu = GetMenuFromID (hMenu);
  580. MENUITEM item;
  581. for (int i = 0; i < menu.items.Count; i++) {
  582. item = (MENUITEM) menu.items[i];
  583. if (item.item.IsPopup) {
  584. MENU sub_menu = GetMenuFromID (item.hSubMenu);
  585. if (sub_menu != null && sub_menu.Wnd != null)
  586. HideSubPopups (item.hSubMenu);
  587. DestroyMenu (item.hSubMenu);
  588. }
  589. }
  590. // Do not destroy the window of a Menubar
  591. if (menu.Wnd != null && menu.bMenubar == false) {
  592. ((PopUpWindow)menu.Wnd).Dispose ();
  593. menu.Wnd = null;
  594. }
  595. /* Unreference from the array list */
  596. menu_list[((int)hMenu)-1] = null;
  597. }
  598. // Find item by screen coordinates
  599. static public bool FindSubItemByCoord (IntPtr hMenu, Point pnt, ref IntPtr hMenuItem, ref MENUITEM itemfound)
  600. {
  601. Point pnt_client;
  602. Rectangle rect;
  603. MENU menu = GetMenuFromID (hMenu);
  604. MENUITEM item;
  605. for (int i = 0; i < menu.items.Count; i++) {
  606. item = (MENUITEM) menu.items[i];
  607. if (item.item.IsPopup)
  608. if (FindSubItemByCoord (item.hSubMenu, pnt, ref hMenuItem, ref itemfound) == true)
  609. return true;
  610. if (menu.Wnd == null) // Menu has not been created yet
  611. continue;
  612. rect = item.rect;
  613. pnt_client = menu.Wnd.PointToScreen (new Point (item.rect.X, item.rect.Y));
  614. rect.X = pnt_client.X;
  615. rect.Y = pnt_client.Y;
  616. if (rect.Contains (pnt) == true) {
  617. itemfound = item;
  618. hMenuItem = hMenu;
  619. return true;
  620. }
  621. }
  622. return false;
  623. }
  624. static public void SetMenuBarWindow (IntPtr hMenu, Control wnd)
  625. {
  626. MENU menu = GetMenuFromID (hMenu);
  627. menu.Wnd = wnd;
  628. menu.bMenubar = true;
  629. }
  630. static private void MenuBarMove (IntPtr hMenu, MENUITEM item, TRACKER tracker)
  631. {
  632. MENU menu = GetMenuFromID (hMenu);
  633. Point pnt = new Point (item.rect.X, item.rect.Y + item.rect.Height);
  634. pnt = menu.Wnd.PointToScreen (pnt);
  635. MenuAPI.SelectItem (hMenu, item, false, tracker);
  636. HideSubPopups (tracker.hCurrentMenu);
  637. tracker.hCurrentMenu = hMenu;
  638. MenuAPI.TrackPopupMenu (hMenu, item.hSubMenu, pnt, false, null);
  639. }
  640. // Function that process all menubar mouse events
  641. static public void TrackBarMouseEvent (IntPtr hMenu, Control wnd, MouseEventArgs e, MenuMouseEvent eventype, TRACKER tracker)
  642. {
  643. MENU menu = GetMenuFromID (hMenu);
  644. switch (eventype) {
  645. case MenuMouseEvent.Down: {
  646. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
  647. if (item != null) {
  648. MENU top_menu = GetMenuFromID (tracker.hTopMenu);
  649. top_menu.bTracking = true;
  650. MenuBarMove (hMenu, item, tracker);
  651. if (item != null) {
  652. item.item.PerformClick ();
  653. }
  654. }
  655. break;
  656. }
  657. case MenuMouseEvent.Move: { /* Coordinates in screen position*/
  658. if (tracker.hTopMenu != IntPtr.Zero && tracker.hCurrentMenu != IntPtr.Zero) {
  659. MENU top_menu = GetMenuFromID (tracker.hTopMenu);
  660. if (top_menu.bTracking == false)
  661. break;
  662. Point pnt = new Point (e.X, e.Y);
  663. pnt = menu.Wnd.PointToClient (pnt);
  664. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, pnt);
  665. if (item != null && menu.SelectedItem != item)
  666. MenuBarMove (hMenu, item, tracker);
  667. }
  668. break;
  669. }
  670. default:
  671. break;
  672. }
  673. }
  674. static public MENUITEM FindItemByKey (IntPtr hMenu, IntPtr key)
  675. {
  676. MENU menu = GetMenuFromID (hMenu);
  677. MENUITEM item;
  678. char key_char = (char ) (key.ToInt32() & 0xff);
  679. key_char = Char.ToUpper (key_char);
  680. for (int i = 0; i < menu.items.Count; i++) {
  681. item = (MENUITEM) menu.items[i];
  682. if (item.item.Mnemonic == '\0')
  683. continue;
  684. if (item.item.Mnemonic == key_char)
  685. return item;
  686. }
  687. return null;
  688. }
  689. // Get the next or previous selectable item on a menu
  690. static public MENUITEM GetNextItem (IntPtr hMenu, ItemNavigation navigation)
  691. {
  692. MENU menu = GetMenuFromID (hMenu);
  693. int pos = 0;
  694. bool selectable_items = false;
  695. MENUITEM item;
  696. // Check if there is at least a selectable item
  697. for (int i = 0; i < menu.items.Count; i++) {
  698. item = (MENUITEM)menu.items[i];
  699. if (item.item.Separator == false && item.item.Visible == true) {
  700. selectable_items = true;
  701. break;
  702. }
  703. }
  704. if (selectable_items == false)
  705. return null;
  706. switch (navigation) {
  707. case ItemNavigation.First: {
  708. pos = 0;
  709. /* Next item that is not separator and it is visible*/
  710. for (; pos < menu.items.Count; pos++) {
  711. item = (MENUITEM)menu.items[pos];
  712. if (item.item.Separator == false && item.item.Visible == true)
  713. break;
  714. }
  715. if (pos >= menu.items.Count) { /* Jump at the start of the menu */
  716. pos = 0;
  717. /* Next item that is not separator and it is visible*/
  718. for (; pos < menu.items.Count; pos++) {
  719. item = (MENUITEM)menu.items[pos];
  720. if (item.item.Separator == false && item.item.Visible == true)
  721. break;
  722. }
  723. }
  724. break;
  725. }
  726. case ItemNavigation.Last: { // Not used
  727. break;
  728. }
  729. case ItemNavigation.Next: {
  730. if (menu.SelectedItem != null)
  731. pos = menu.SelectedItem.pos;
  732. /* Next item that is not separator and it is visible*/
  733. for (pos++; pos < menu.items.Count; pos++) {
  734. item = (MENUITEM)menu.items[pos];
  735. if (item.item.Separator == false && item.item.Visible == true)
  736. break;
  737. }
  738. if (pos >= menu.items.Count) { /* Jump at the start of the menu */
  739. pos = 0;
  740. /* Next item that is not separator and it is visible*/
  741. for (; pos < menu.items.Count; pos++) {
  742. item = (MENUITEM)menu.items[pos];
  743. if (item.item.Separator == false && item.item.Visible == true)
  744. break;
  745. }
  746. }
  747. break;
  748. }
  749. case ItemNavigation.Previous: {
  750. if (menu.SelectedItem != null)
  751. pos = menu.SelectedItem.pos;
  752. /* Previous item that is not separator and it is visible*/
  753. for (pos--; pos >= 0; pos--) {
  754. item = (MENUITEM)menu.items[pos];
  755. if (item.item.Separator == false && item.item.Visible == true)
  756. break;
  757. }
  758. if (pos < 0 ) { /* Jump at the end of the menu*/
  759. pos = menu.items.Count - 1;
  760. /* Previous item that is not separator and it is visible*/
  761. for (; pos >= 0; pos--) {
  762. item = (MENUITEM)menu.items[pos];
  763. if (item.item.Separator == false && item.item.Visible == true)
  764. break;
  765. }
  766. }
  767. break;
  768. }
  769. default:
  770. break;
  771. }
  772. return (MENUITEM)menu.items[pos];
  773. }
  774. static public bool ProcessKeys (IntPtr hMenu, ref Message msg, Keys keyData, TRACKER tracker)
  775. {
  776. MENU menu = GetMenuFromID (hMenu);
  777. MENUITEM item;
  778. switch (keyData) {
  779. case Keys.Up: {
  780. item = GetNextItem (hMenu, ItemNavigation.Previous);
  781. if (item != null)
  782. MenuAPI.SelectItem (hMenu, item, false, tracker);
  783. break;
  784. }
  785. case Keys.Down: {
  786. item = GetNextItem (hMenu, ItemNavigation.Next);
  787. if (item != null)
  788. MenuAPI.SelectItem (hMenu, item, false, tracker);
  789. break;
  790. }
  791. /* Menubar selects and opens next. Popups next or open*/
  792. case Keys.Right: {
  793. // Try to Expand popup first
  794. if (menu.SelectedItem.item.IsPopup) {
  795. ShowSubPopup (hMenu, menu.SelectedItem.hSubMenu, menu.SelectedItem, tracker);
  796. } else {
  797. MENU parent = null;
  798. if (menu.hParent != IntPtr.Zero)
  799. parent = GetMenuFromID (menu.hParent);
  800. if (parent != null && parent.bMenubar == true) {
  801. MENUITEM select_item = GetNextItem (menu.hParent, ItemNavigation.Next);
  802. MenuBarMove (menu.hParent, select_item, tracker);
  803. }
  804. }
  805. break;
  806. }
  807. case Keys.Left: {
  808. // Try to Collapse popup first
  809. if (menu.SelectedItem.item.IsPopup) {
  810. } else {
  811. MENU parent = null;
  812. if (menu.hParent != IntPtr.Zero)
  813. parent = GetMenuFromID (menu.hParent);
  814. if (parent != null && parent.bMenubar == true) {
  815. MENUITEM select_item = GetNextItem (menu.hParent, ItemNavigation.Previous);
  816. MenuBarMove (menu.hParent, select_item, tracker);
  817. }
  818. }
  819. break;
  820. }
  821. case Keys.Return: {
  822. Console.WriteLine ("Return key: "+ menu.SelectedItem.item.Text);
  823. MenuAPI.ExecFocusedItem (hMenu, menu.SelectedItem, tracker);
  824. break;
  825. }
  826. default:
  827. break;
  828. }
  829. /* Try if it is a menu hot key */
  830. item = MenuAPI.FindItemByKey (hMenu, msg.WParam);
  831. if (item != null) {
  832. MenuAPI.SelectItem (hMenu, item, false, tracker);
  833. return true;
  834. }
  835. return false;
  836. }
  837. }
  838. /*
  839. class PopUpWindow
  840. */
  841. internal class PopUpWindow : Control
  842. {
  843. private IntPtr hMenu;
  844. private MenuAPI.TRACKER tracker;
  845. public PopUpWindow (IntPtr hMenu, MenuAPI.TRACKER tracker): base ()
  846. {
  847. this.hMenu = hMenu;
  848. this.tracker = tracker;
  849. MouseDown += new MouseEventHandler (OnMouseDownPUW);
  850. MouseMove += new MouseEventHandler (OnMouseMovePUW);
  851. MouseUp += new MouseEventHandler (OnMouseUpPUW);
  852. Paint += new PaintEventHandler (OnPaintPUW);
  853. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  854. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  855. is_visible = false;
  856. }
  857. protected override CreateParams CreateParams
  858. {
  859. get {
  860. CreateParams cp = base.CreateParams;
  861. cp.Caption = "Menu PopUp";
  862. cp.Style = unchecked ((int)(WindowStyles.WS_POPUP));
  863. cp.ExStyle |= (int)(WindowStyles.WS_EX_TOOLWINDOW | WindowStyles.WS_EX_TOPMOST);
  864. return cp;
  865. }
  866. }
  867. public void ShowWindow ()
  868. {
  869. Show ();
  870. Capture = true;
  871. Refresh ();
  872. }
  873. public new void LostFocus ()
  874. {
  875. Capture = false;
  876. }
  877. protected override void OnResize (EventArgs e)
  878. {
  879. base.OnResize (e);
  880. }
  881. private void OnPaintPUW (Object o, PaintEventArgs pevent)
  882. {
  883. if (Width <= 0 || Height <= 0 || Visible == false)
  884. return;
  885. Draw (pevent.ClipRectangle);
  886. pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  887. }
  888. public void HideWindow ()
  889. {
  890. Capture = false;
  891. Hide ();
  892. MenuAPI.MENU top_menu = MenuAPI.GetMenuFromID (tracker.hTopMenu);
  893. top_menu.bTracking = false;
  894. MenuAPI.HideSubPopups (tracker.hTopMenu);
  895. if (top_menu.bMenubar) {
  896. MenuAPI.MENUITEM item = MenuAPI.GetSelected (tracker.hTopMenu);
  897. if (item != null) {
  898. MenuAPI.UnSelectItem (tracker.hTopMenu, item);
  899. }
  900. } else { // Context Menu
  901. Console.WriteLine ("HideWindow context menu {0} ", top_menu.hParent);
  902. ((PopUpWindow)top_menu.Wnd).Hide ();
  903. }
  904. }
  905. private void OnMouseDownPUW (object sender, MouseEventArgs e)
  906. {
  907. /* Click outside the client area*/
  908. if (ClientRectangle.Contains (e.X, e.Y) == false) {
  909. HideWindow ();
  910. }
  911. }
  912. private void OnMouseUpPUW (object sender, MouseEventArgs e)
  913. {
  914. /* Click in an item area*/
  915. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
  916. if (item != null) {
  917. item.item.PerformClick ();
  918. HideWindow ();
  919. }
  920. }
  921. private void OnMouseMovePUW (object sender, MouseEventArgs e)
  922. {
  923. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
  924. if (item != null) {
  925. MenuAPI.SelectItem (hMenu, item, true, tracker);
  926. } else {
  927. MenuAPI.MENU menu_parent = null;
  928. if (tracker.hTopMenu != IntPtr.Zero)
  929. menu_parent = MenuAPI.GetMenuFromID (tracker.hTopMenu);
  930. if (menu_parent == null)
  931. return;
  932. if (menu_parent.bMenubar) {
  933. MenuAPI.TrackBarMouseEvent (tracker.hTopMenu,
  934. this, new MouseEventArgs(e.Button, e.Clicks, MousePosition.X, MousePosition.Y, e.Delta),
  935. MenuAPI.MenuMouseEvent.Move, tracker);
  936. }
  937. IntPtr hMenuItem = IntPtr.Zero;
  938. MenuAPI.MENUITEM item_found = null;
  939. if (MenuAPI.FindSubItemByCoord (tracker.hTopMenu, MousePosition, ref hMenuItem, ref item_found) == false)
  940. return;
  941. MenuAPI.SelectItem (hMenuItem, item_found, false, tracker);
  942. }
  943. }
  944. protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
  945. {
  946. return MenuAPI.ProcessKeys (hMenu, ref msg, keyData, tracker);
  947. }
  948. protected override void CreateHandle ()
  949. {
  950. base.CreateHandle ();
  951. RefreshItems ();
  952. }
  953. // Called when the number of items has changed
  954. internal void RefreshItems ()
  955. {
  956. MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
  957. MenuAPI.CalcPopupMenuSize (DeviceContext, hMenu);
  958. Width = menu.Width;
  959. Height = menu.Height;
  960. }
  961. private void Draw (Rectangle clip)
  962. {
  963. MenuAPI.DrawPopupMenu (DeviceContext, hMenu, clip, ClientRectangle);
  964. }
  965. }
  966. }