MenuAPI.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. */
  34. internal class MenuAPI
  35. {
  36. static StringFormat string_format_text = new StringFormat ();
  37. static StringFormat string_format_shortcut = new StringFormat ();
  38. static StringFormat string_format_menubar_text = new StringFormat ();
  39. static ArrayList menu_list = new ArrayList ();
  40. static Font MENU_FONT = new Font (FontFamily.GenericSansSerif, 8.25f);
  41. static int POPUP_ARROW_WITDH;
  42. static int POPUP_ARROW_HEIGHT;
  43. const int SEPARATOR_HEIGHT = 5;
  44. const int SM_CXBORDER = 1;
  45. const int SM_CYBORDER = 1;
  46. const int SM_CXMENUCHECK = 14; // Width of the menu check
  47. const int SM_CYMENUCHECK = 14; // Height of the menu check
  48. const int SM_CXARROWCHECK = 16; // Width of the arrow
  49. const int SM_CYARROWCHECK = 16; // Height of the arrow
  50. const int SM_CYMENU = 18; // Minimum height of a menu
  51. const int MENU_TAB_SPACE = 8; // Pixels added to the width of an item because of a tab
  52. const int MENU_BAR_ITEMS_SPACE = 12; // Space between menu bar items
  53. public class MENU
  54. {
  55. public MF Flags; // Menu flags (MF_POPUP, MF_SYSMENU)
  56. public int Width; // Width of the whole menu
  57. public int Height; // Height of the whole menu
  58. public Control Wnd; // In a Popup menu is the PopupWindow and in a MenuBar the Form
  59. public ArrayList items; // Array of menu items
  60. public int FocusedItem; // Currently focused item
  61. public IntPtr hParent;
  62. public MENUITEM SelectedItem; // Currently focused item
  63. public MENU ()
  64. {
  65. Wnd = null;
  66. hParent = IntPtr.Zero;
  67. items = new ArrayList ();
  68. Flags = MF.MF_INSERT;
  69. Width = Height = FocusedItem = 0;
  70. }
  71. }
  72. public class MENUITEM
  73. {
  74. public MenuItem item;
  75. public Rectangle rect;
  76. public int fMask;
  77. public int fType;
  78. public MF fState;
  79. public int wID;
  80. public IntPtr hSubMenu;
  81. public int xTab;
  82. public MENUITEM ()
  83. {
  84. xTab = 0;
  85. fMask = 0;
  86. wID = 0;
  87. rect = new Rectangle ();
  88. }
  89. };
  90. public class TRACKER
  91. {
  92. public IntPtr hCurrentMenu;
  93. public IntPtr hTopMenu;
  94. public bool menubar;
  95. public TRACKER ()
  96. {
  97. hCurrentMenu = hTopMenu = IntPtr.Zero;
  98. menubar = false;
  99. }
  100. };
  101. static void DumpMenuItems (ArrayList list)
  102. {
  103. Console.WriteLine ("Menu items dump start--- ");
  104. for (int i = 0; i < list.Count; i++)
  105. Console.WriteLine ("idx:{0} {1} {2}", i, ((MENUITEM)list[i]).item, ((MENUITEM)list[i]).item.Separator);
  106. Console.WriteLine ("Menu items dump end --- ");
  107. }
  108. public enum MenuMouseEvent
  109. {
  110. Down,
  111. Move,
  112. }
  113. internal enum MF
  114. {
  115. MF_INSERT = 0x0,
  116. MF_APPEND = 0x100,
  117. MF_DELETE = 0x200,
  118. MF_REMOVE = 0x1000,
  119. MF_BYCOMMAND = 0,
  120. MF_BYPOSITION = 0x400,
  121. MF_SEPARATOR = 0x800,
  122. MF_ENABLED = 0,
  123. MF_GRAYED = 1,
  124. MF_DISABLED = 2,
  125. MF_UNCHECKED = 0,
  126. MF_CHECKED = 8,
  127. MF_USECHECKBITMAPS = 0x200,
  128. MF_STRING = 0,
  129. MF_BITMAP = 4,
  130. MF_OWNERDRAW = 0x100,
  131. MF_POPUP = 0x10,
  132. MF_MENUBARBREAK = 0x20,
  133. MF_MENUBREAK = 0x40,
  134. MF_UNHILITE = 0,
  135. MF_HILITE = 0x80,
  136. MF_DEFAULT = 0x1000,
  137. MF_SYSMENU = 0x2000,
  138. MF_HELP = 0x4000,
  139. MF_RIGHTJUSTIFY = 0x4000,
  140. MF_MENUBAR = 0x8000 // Internal
  141. }
  142. static MenuAPI ()
  143. {
  144. Console.WriteLine ("MenuAPI::MenuAPI");
  145. string_format_text.LineAlignment = StringAlignment.Center;
  146. string_format_text.Alignment = StringAlignment.Near;
  147. string_format_text.HotkeyPrefix = HotkeyPrefix.Show;
  148. string_format_shortcut.LineAlignment = StringAlignment.Center;
  149. string_format_shortcut.Alignment = StringAlignment.Far;
  150. string_format_menubar_text.LineAlignment = StringAlignment.Center;
  151. string_format_menubar_text.Alignment = StringAlignment.Center;
  152. string_format_menubar_text.HotkeyPrefix = HotkeyPrefix.Show;
  153. }
  154. static public IntPtr StoreMenuID (MENU menu)
  155. {
  156. int id = menu_list.Add (menu);
  157. //Console.WriteLine ("StoreMenuID:" + id + 1);
  158. return (IntPtr)(id + 1);
  159. }
  160. static public MENU GetMenuFromID (IntPtr ptr)
  161. {
  162. int id = (int)ptr;
  163. id = id - 1;
  164. return (MENU) menu_list[id];
  165. }
  166. static public IntPtr CreateMenu ()
  167. {
  168. MENU menu = new MENU ();
  169. return StoreMenuID (menu);
  170. }
  171. static public IntPtr CreatePopupMenu ()
  172. {
  173. Console.WriteLine ("MenuAPI.CreatePopupMenu");
  174. MENU popMenu = new MENU ();
  175. popMenu.Flags |= MF.MF_POPUP;
  176. return StoreMenuID (popMenu);
  177. }
  178. static public int InsertMenuItem (IntPtr hMenu, int uItem, bool fByPosition, MenuItem item,
  179. ref IntPtr hSubMenu)
  180. {
  181. int id;
  182. if (fByPosition == false)
  183. throw new NotImplementedException ();
  184. // Insert the item
  185. MENU menu = GetMenuFromID (hMenu);
  186. if ((uint)uItem > menu.items.Count)
  187. uItem = menu.items.Count;
  188. MENUITEM menu_item = new MENUITEM ();
  189. menu_item.item = item;
  190. if (item.IsPopup) {
  191. menu_item.hSubMenu = CreatePopupMenu ();
  192. }
  193. else
  194. menu_item.hSubMenu = IntPtr.Zero;
  195. //menu_item.Flags |= MF.MF_POPUP;
  196. hSubMenu = menu_item.hSubMenu;
  197. id = menu.items.Count;
  198. menu.items.Insert (uItem, menu_item);
  199. //Console.WriteLine ("InsertMenuItem {0} {1} {2}" + menu.items.Count,
  200. //);
  201. return id;
  202. }
  203. // X and Y are screen coordinates
  204. static public bool TrackPopupMenu (IntPtr hTopMenu, IntPtr hMenu, Point pnt, bool menubar, Control Wnd)
  205. {
  206. Console.WriteLine ("TrackPopupMenu start");
  207. MENU menu = GetMenuFromID (hMenu);
  208. TRACKER tracker = new TRACKER ();
  209. PopUpWindow popup = new PopUpWindow (hMenu, tracker);
  210. menu.Wnd = popup;
  211. tracker.hCurrentMenu = hMenu;
  212. tracker.hTopMenu = hTopMenu;
  213. tracker.menubar = menubar;
  214. //Console.WriteLine ("TrackPopupMenu:Setting current to {0}", menu.hCurrent);
  215. //Console.WriteLine ("menubar: path2 {0}" + pnt);
  216. popup.Location = popup.PointToClient (pnt);
  217. popup.ShowWindow ();
  218. //MenuAPI.DumpMenuItems (menu.items);
  219. MSG msg = new MSG();
  220. while (XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
  221. XplatUI.TranslateMessage(ref msg);
  222. XplatUI.DispatchMessage(ref msg);
  223. }
  224. //popup.DestroyHandle ();
  225. Console.WriteLine ("TrackPopupMenu end");
  226. menu.Wnd = null;
  227. return true;
  228. }
  229. /*
  230. Menu drawing API
  231. */
  232. static public void CalcItemSize (Graphics dc, MENUITEM item, int y, int x, bool menuBar)
  233. {
  234. item.rect.Y = y;
  235. item.rect.X = x;
  236. if (item.item.Visible == false)
  237. return;
  238. if (item.item.Separator == true) {
  239. item.rect.Height = SEPARATOR_HEIGHT / 2;
  240. item.rect.Width = -1;
  241. return;
  242. }
  243. SizeF size;
  244. size = dc.MeasureString (item.item.Text, MENU_FONT);
  245. item.rect.Width = (int) size.Width;
  246. item.rect.Height = (int) size.Height;
  247. if (!menuBar) {
  248. if (item.item.Shortcut != Shortcut.None && item.item.ShowShortcut) {
  249. item.xTab = SM_CXMENUCHECK + MENU_TAB_SPACE + (int) size.Width;
  250. size = dc.MeasureString (" " + item.item.GetShortCutText (), MENU_FONT);
  251. item.rect.Width += MENU_TAB_SPACE + (int) size.Width;
  252. }
  253. item.rect.Width += 4 + (SM_CXMENUCHECK * 2);
  254. }
  255. else {
  256. //item.rect.Width += MENU_BAR_ITEMS_SPACE;
  257. x += item.rect.Width;
  258. }
  259. if (item.rect.Height < SM_CYMENU - 1)
  260. item.rect.Height = SM_CYMENU - 1;
  261. //Console.WriteLine ("CalcItemSize " + item.rect);
  262. }
  263. static public void CalcPopupMenuSize (Graphics dc, IntPtr hMenu)
  264. {
  265. int x = 3;
  266. int start = 0;
  267. int i, n, y, max;
  268. MENU menu = GetMenuFromID (hMenu);
  269. menu.Height = 0;
  270. while (start < menu.items.Count) {
  271. y = 2;
  272. max = 0;
  273. for (i = start; i < menu.items.Count; i++) {
  274. MENUITEM item = (MENUITEM) menu.items[i];
  275. if ((i != start) && (item.item.Break || item.item.BarBreak))
  276. break;
  277. CalcItemSize (dc, item, y, x, false);
  278. y += item.rect.Height;
  279. if (item.rect.Width > max)
  280. max = item.rect.Width;
  281. }
  282. // Reemplace the -1 by the menu width (separators)
  283. for (n = start; n < i; n++, start++) {
  284. MENUITEM item = (MENUITEM) menu.items[n];
  285. item.rect.Width = max;
  286. }
  287. if (y > menu.Height)
  288. menu.Height = y;
  289. x+= max;
  290. }
  291. menu.Width = x;
  292. //space for border
  293. menu.Width += 2;
  294. menu.Height += 2;
  295. menu.Width += SM_CXBORDER;
  296. menu.Height += SM_CYBORDER;
  297. //Console.WriteLine ("CalcPopupMenuSize {0} {1}", menu.Width, menu.Height);
  298. }
  299. static public void DrawMenuItem (Graphics dc, MENUITEM item, int menu_height, bool menuBar)
  300. {
  301. StringFormat string_format;
  302. if (item.item.Visible == false)
  303. return;
  304. if (menuBar)
  305. string_format = string_format_menubar_text;
  306. else
  307. string_format = string_format_text;
  308. if (item.item.Separator == true) {
  309. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
  310. item.rect.X, item.rect.Y, item.rect.X + item.rect.Width, item.rect.Y);
  311. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
  312. item.rect.X, item.rect.Y + 1, item.rect.X + item.rect.Width, item.rect.Y + 1);
  313. return;
  314. }
  315. Rectangle rect_text = item.rect;
  316. if (!menuBar)
  317. rect_text.X += SM_CXMENUCHECK;
  318. if (item.item.BarBreak) { /* Draw vertical break bar*/
  319. Rectangle rect = item.rect;
  320. rect.Y++;
  321. rect.Width = 3;
  322. rect.Height = menu_height - 6;
  323. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
  324. rect.X, rect.Y , rect.X, rect.Y + rect.Height);
  325. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
  326. rect.X + 1, rect.Y , rect.X +1, rect.Y + rect.Height);
  327. }
  328. //Console.WriteLine ("!{0}, {1}, {2}", item.item.Text, item.rect, rect_text);
  329. if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  330. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  331. (ThemeEngine.Current.ColorHilight), item.rect);
  332. }
  333. if (item.item.Enabled) {
  334. Color color_text;
  335. if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE)
  336. color_text = ThemeEngine.Current.ColorHilightText;
  337. else
  338. color_text = ThemeEngine.Current.ColorMenuText;
  339. dc.DrawString (item.item.Text, MENU_FONT,
  340. ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
  341. rect_text, string_format);
  342. if (item.item.Shortcut != Shortcut.None && item.item.ShowShortcut) {
  343. string str = item.item.GetShortCutText ();
  344. Rectangle rect = rect_text;
  345. rect.X = item.xTab;
  346. rect.Width -= item.xTab;
  347. dc.DrawString (str, MENU_FONT, ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
  348. rect, string_format_shortcut);
  349. }
  350. }
  351. else {
  352. ControlPaint.DrawStringDisabled (dc,
  353. item.item.Text, MENU_FONT, Color.Black, rect_text,
  354. string_format);
  355. }
  356. /* Draw arrow */
  357. if (menuBar == false && item.item.IsPopup) {
  358. Bitmap bmp = new Bitmap (SM_CXARROWCHECK, SM_CYARROWCHECK);
  359. Graphics gr = Graphics.FromImage (bmp);
  360. Rectangle rect_arrow = new Rectangle (0, 0, SM_CXARROWCHECK, SM_CYARROWCHECK);
  361. ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Arrow);
  362. bmp.MakeTransparent ();
  363. dc.DrawImage (bmp, item.rect.X + item.rect.Width - SM_CXARROWCHECK,
  364. item.rect.Y + ((item.rect.Height - SM_CYARROWCHECK) /2));
  365. gr.Dispose ();
  366. bmp.Dispose ();
  367. }
  368. /* Draw checked or radio */
  369. if (menuBar == false && item.item.Checked) {
  370. Rectangle area = item.rect;
  371. Bitmap bmp = new Bitmap (SM_CXMENUCHECK, SM_CYMENUCHECK);
  372. Graphics gr = Graphics.FromImage (bmp);
  373. Rectangle rect_arrow = new Rectangle (0, 0, SM_CXMENUCHECK, SM_CYMENUCHECK);
  374. if (item.item.RadioCheck)
  375. ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Bullet);
  376. else
  377. ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Checkmark);
  378. bmp.MakeTransparent ();
  379. dc.DrawImage (bmp, area.X, item.rect.Y + (item.rect.Height /2)); //aki
  380. gr.Dispose ();
  381. bmp.Dispose ();
  382. }
  383. }
  384. static public void DrawPopupMenu (Graphics dc, IntPtr hMenu, Rectangle rect)
  385. {
  386. MENU menu = GetMenuFromID (hMenu);
  387. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  388. (ThemeEngine.Current.ColorMenu), rect);
  389. /* Draw menu borders */
  390. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorHilightText),
  391. rect.X, rect.Y, rect.X + rect.Width, rect.Y);
  392. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorHilightText),
  393. rect.X, rect.Y, rect.X, rect.Y + rect.Height);
  394. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
  395. rect.X + rect.Width - 1 , rect.Y , rect.X + rect.Width - 1, rect.Y + rect.Height);
  396. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonDkShadow),
  397. rect.X + rect.Width, rect.Y , rect.X + rect.Width, rect.Y + rect.Height);
  398. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
  399. rect.X , rect.Y + rect.Height - 1 , rect.X + rect.Width - 1, rect.Y + rect.Height -1);
  400. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonDkShadow),
  401. rect.X , rect.Y + rect.Height, rect.X + rect.Width - 1, rect.Y + rect.Height);
  402. for (int i = 0; i < menu.items.Count; i++) {
  403. DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, false);
  404. }
  405. }
  406. // Updats the menu rect
  407. static public void MenuBarCalcSize (Graphics dc, IntPtr hMenu, Rectangle rect)
  408. {
  409. int x = 3;
  410. int i, y;
  411. MENU menu = GetMenuFromID (hMenu);
  412. MENUITEM item;
  413. x = rect.X;
  414. y = rect.Height + 1;
  415. menu.Width = 0;
  416. i = 0;
  417. while (i < menu.items.Count) {
  418. item = (MENUITEM) menu.items[i];
  419. CalcItemSize (dc, item, y, x, true);
  420. i = i + 1;
  421. x += item.rect.Width;
  422. item.fState |= MF.MF_MENUBAR;
  423. if (item.rect.Height > menu.Height)
  424. menu.Height = item.rect.Height;
  425. //Console.WriteLine ("MenuBarCalcSize {0} {1}", item.item.Text, item.rect.X);
  426. }
  427. menu.Width = x;
  428. //Console.WriteLine ("CalcPopupMenuSize {0} {1}", menu.Width, menu.Height);
  429. }
  430. // Draws a menu bar in a Window
  431. static public void DrawMenuBar (Graphics dc, IntPtr hMenu, Rectangle rect)
  432. {
  433. MENU menu = GetMenuFromID (hMenu);
  434. Rectangle rect_menu = new Rectangle ();
  435. if (menu.Height == 0)
  436. MenuBarCalcSize (dc, hMenu, rect_menu);
  437. rect.Height = menu.Height;
  438. rect.Width = menu.Width;
  439. for (int i = 0; i < menu.items.Count; i++) {
  440. DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, true);
  441. }
  442. }
  443. /*
  444. Menu handeling API
  445. */
  446. static public MENUITEM FindItemByCoords (IntPtr hMenu, Point pt, ref int pos)
  447. {
  448. MENU menu = GetMenuFromID (hMenu);
  449. for (int i = 0; i < menu.items.Count; i++) {
  450. MENUITEM item = (MENUITEM) menu.items[i];
  451. if (item.rect.Contains (pt)) {
  452. //Console.WriteLine ("FindItemByCoords: " + item.item.Text);
  453. pos = i;
  454. return item;
  455. }
  456. }
  457. //Console.WriteLine ("FindItemByCoords none ");
  458. pos = -1;
  459. return null;
  460. }
  461. static public void SelectItem (TRACKER tracker, IntPtr hMenu, MENUITEM item, int pos, bool execute)
  462. {
  463. MENU menu = GetMenuFromID (hMenu);
  464. //Console.WriteLine ("Current: {0} select {1}", menu_parent.hCurrent, hMenu);
  465. MENUITEM highlight_item = null;
  466. /* Already selected */
  467. for (int i = 0; i < menu.items.Count; i++) {
  468. MENUITEM it = (MENUITEM) menu.items[i];
  469. if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  470. if (item.rect == it.rect)
  471. return;
  472. highlight_item = item;
  473. }
  474. }
  475. //Console.WriteLine ("SelectItem:Current is {0} {1}", tracker.hCurrentMenu, hMenu);
  476. if (tracker.hCurrentMenu != hMenu) {
  477. Console.WriteLine ("Changing current menu!");
  478. HideSubPopups (hMenu);
  479. tracker.hCurrentMenu = hMenu;
  480. }
  481. /* Unselect previous item*/
  482. for (int i = 0; i < menu.items.Count; i++) {
  483. MENUITEM it = (MENUITEM) menu.items[i];
  484. if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  485. it.fState = item.fState & ~MF.MF_HILITE;
  486. menu.items[i] = it;
  487. }
  488. }
  489. item.fState |= MF.MF_HILITE;
  490. menu.items[pos] = item;
  491. //Console.WriteLine ("SelectItem {0} {1} {2} {3}", item.item.Text, item.fState,
  492. // ((MENUITEM)(menu.items[pos])).fState, pos);
  493. if (execute)
  494. ExecFocusedItem (tracker, hMenu, item);
  495. }
  496. /*
  497. Used when the user executes the action of an item (press enter, shortcut)
  498. or a sub-popup menu has to be shown
  499. */
  500. static public void ExecFocusedItem (TRACKER tracker, IntPtr hMenu, MENUITEM item)
  501. {
  502. if (item.item.IsPopup) {
  503. ShowSubPopup (tracker, hMenu, item.hSubMenu, item);
  504. }
  505. else {
  506. // Execute function
  507. }
  508. }
  509. static public void ShowSubPopup (TRACKER tracker, IntPtr hParent, IntPtr hMenu, MENUITEM item)
  510. {
  511. MENU menu = GetMenuFromID (hMenu);
  512. if (menu.Wnd != null) /* Already showing */
  513. return;
  514. if (item.item.Enabled == false)
  515. return;
  516. MENU menu_parent = GetMenuFromID (hParent);
  517. PopUpWindow popup = new PopUpWindow (hMenu, tracker);
  518. ((PopUpWindow)menu_parent.Wnd).LostFocus ();
  519. menu.Wnd = popup;
  520. tracker.hCurrentMenu = hMenu;
  521. Console.WriteLine ("ShowSubPopup:Setting current to {0}", tracker.hCurrentMenu);
  522. Point pnt = new Point ();
  523. pnt.X = item.rect.X + item.rect.Width;
  524. pnt.Y = item.rect.Y + 1;
  525. Console.WriteLine ("ShowSubPopup prev:" + pnt);
  526. pnt = menu_parent.Wnd.PointToScreen (pnt);
  527. popup.Location = pnt;
  528. popup.ShowWindow ();
  529. popup.Refresh ();
  530. Console.WriteLine ("ShowSubPopup location:" + popup.Location);
  531. }
  532. /* Hides all the submenus open in a menu */
  533. static public void HideSubPopups (IntPtr hMenu)
  534. {
  535. Console.WriteLine ("HideSubPopups: " + hMenu);
  536. MENU menu = GetMenuFromID (hMenu);
  537. MENUITEM item;
  538. for (int i = 0; i < menu.items.Count; i++) {
  539. item = (MENUITEM) menu.items[i];
  540. if (item.item.IsPopup) {
  541. MENU sub_menu = GetMenuFromID (item.hSubMenu);
  542. if (sub_menu.Wnd != null) {
  543. Console.WriteLine ("Hiding!");
  544. HideSubPopups (item.hSubMenu);
  545. ((PopUpWindow)sub_menu.Wnd).Destroy ();
  546. sub_menu.Wnd = null;
  547. }
  548. }
  549. }
  550. }
  551. static public void DestroyMenu (IntPtr hMenu)
  552. {
  553. MENU menu = GetMenuFromID (hMenu);
  554. MENUITEM item;
  555. for (int i = 0; i < menu.items.Count; i++) {
  556. item = (MENUITEM) menu.items[i];
  557. //Console.WriteLine ("Destroy item: "+ item.item.Text + " pop:" + item.item.IsPopup);
  558. if (item.item.IsPopup) {
  559. MENU sub_menu = GetMenuFromID (item.hSubMenu);
  560. if (sub_menu != null && sub_menu.Wnd != null) {
  561. // TODO: Remove from list
  562. HideSubPopups (item.hSubMenu);
  563. DestroyMenu (item.hSubMenu);
  564. }
  565. }
  566. }
  567. Console.WriteLine ("Menu.Wnd1: " + menu);
  568. Console.WriteLine ("Menu.Wnd2: " + menu.Wnd);
  569. // TODO: Remove from list
  570. // Do not destroy the window of a Menubar
  571. if (menu.Wnd != null && ((menu.Flags & MF.MF_POPUP) == MF.MF_POPUP)) {
  572. ((PopUpWindow)menu.Wnd).Destroy ();
  573. menu.Wnd = null;
  574. }
  575. }
  576. static public void SetMenuBarWindow (IntPtr hMenu, Control wnd)
  577. {
  578. MENU menu = GetMenuFromID (hMenu);
  579. menu.Wnd = wnd;
  580. }
  581. static TRACKER tracker = new TRACKER ();
  582. //static Control mywnd = null;
  583. // Function that process all menubar mouse events
  584. static public void TrackBarMouseEvent (IntPtr hMenu, Control wnd, MouseEventArgs e, MenuMouseEvent eventype)
  585. {
  586. MENU menu = GetMenuFromID (hMenu);
  587. int pos = 0;
  588. Point pnt;
  589. //if (mywnd == null)
  590. // mywnd = wnd;
  591. switch (eventype) {
  592. case MenuMouseEvent.Down: {
  593. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu,
  594. new Point (e.X, e.Y), ref pos);
  595. //Console.WriteLine ("menubar: {0} {1}",item.rect.X,
  596. // item.rect.Y + item.rect.Height + 1);
  597. if (item != null) {
  598. MenuAPI.SelectItem (tracker, hMenu, item, pos, false);
  599. tracker.hCurrentMenu = hMenu;
  600. pnt = new Point (item.rect.X, item.rect.Y + item.rect.Height);
  601. pnt = wnd.PointToScreen (pnt);
  602. menu.SelectedItem = item;
  603. wnd.Refresh ();
  604. MenuAPI.TrackPopupMenu (hMenu, item.hSubMenu, pnt, true, null);
  605. }
  606. break;
  607. }
  608. case MenuMouseEvent.Move: { //aki
  609. if (tracker.hCurrentMenu != IntPtr.Zero) {
  610. Point p;
  611. p = new Point (e.X, e.Y);
  612. p = wnd.PointToScreen (p);
  613. p = menu.Wnd.PointToClient (p);
  614. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, p, ref pos);
  615. if (item != null && menu.SelectedItem != item) {
  616. //Console.WriteLine ("Changing from MenuMouseEvent.Move {0} {1} {2} {3}",
  617. // hMenu, item.item.Text, tracker.hCurrentMenu, hMenu);
  618. menu.SelectedItem = item;
  619. pnt = new Point (item.rect.X, item.rect.Y + item.rect.Height);
  620. pnt = menu.Wnd.PointToScreen (pnt);
  621. MenuAPI.SelectItem (tracker, hMenu, item, pos, false);
  622. MenuAPI.DestroyMenu (tracker.hCurrentMenu);
  623. tracker.hCurrentMenu = hMenu;
  624. menu.Wnd.Refresh ();
  625. MenuAPI.TrackPopupMenu (hMenu, item.hSubMenu, pnt, true, null);
  626. }
  627. }
  628. break;
  629. }
  630. default:
  631. break;
  632. }
  633. }
  634. }
  635. /*
  636. class PopUpWindow
  637. */
  638. internal class PopUpWindow : Control
  639. {
  640. private IntPtr hMenu;
  641. private MenuAPI.TRACKER tracker;
  642. public PopUpWindow (IntPtr hMenu, MenuAPI.TRACKER tracker): base ()
  643. {
  644. this.hMenu = hMenu;
  645. this.tracker = tracker;
  646. MouseDown += new MouseEventHandler (OnMouseDownPUW);
  647. MouseMove += new MouseEventHandler (OnMouseMovePUW);
  648. MouseUp += new MouseEventHandler (OnMouseUpPUW);
  649. Paint += new PaintEventHandler (OnPaintPUW);
  650. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  651. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  652. }
  653. protected override CreateParams CreateParams
  654. {
  655. get {
  656. CreateParams cp = base.CreateParams;
  657. cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE));
  658. return cp;
  659. }
  660. }
  661. public void ShowWindow ()
  662. {
  663. Capture = true;
  664. Show ();
  665. }
  666. public void Destroy ()
  667. {
  668. Capture = false;
  669. DestroyHandle ();
  670. }
  671. public void LostFocus ()
  672. {
  673. Capture = false;
  674. }
  675. protected override void OnResize(EventArgs e)
  676. {
  677. base.OnResize (e);
  678. Console.WriteLine ("OnResize {0} {1} ", Width, Height);
  679. }
  680. private void OnPaintPUW (Object o, PaintEventArgs pevent)
  681. {
  682. //Console.WriteLine ("OnPaintPUW");
  683. if (Width <= 0 || Height <= 0 || Visible == false)
  684. return;
  685. Draw ();
  686. pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
  687. }
  688. private void OnMouseDownPUW (object sender, MouseEventArgs e)
  689. {
  690. Console.WriteLine ("OnMouseDownPUW");
  691. /* Click outside the client area*/
  692. if (ClientRectangle.Contains (e.X, e.Y) == false) {
  693. Console.WriteLine ("Hide");
  694. Capture = false;
  695. Hide ();
  696. }
  697. }
  698. private void OnMouseUpPUW (object sender, MouseEventArgs e)
  699. {
  700. Console.WriteLine ("OnMouseUpPUW");
  701. /* Click outside the client area*/
  702. int pos = 0;
  703. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y), ref pos);
  704. if (item != null && item.item.Enabled) {
  705. item.item.PerformClick ();
  706. MenuAPI.DestroyMenu (tracker.hTopMenu);
  707. Capture = false;
  708. Refresh ();
  709. }
  710. }
  711. private void OnMouseMovePUW (object sender, MouseEventArgs e)
  712. {
  713. //Console.WriteLine ("OnMouseMovePUW");
  714. int pos = 0;
  715. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y), ref pos);
  716. if (item != null) {
  717. MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
  718. MenuAPI.SelectItem (tracker, hMenu, item, pos, true);
  719. Refresh ();
  720. } else {
  721. if (tracker.menubar) {
  722. //Console.WriteLine ("MenuBar tracker move " + e.Y);
  723. //MenuAPI.TrackBarMouseEvent (tracker.hTopMenu,
  724. // this, e, MenuAPI.MenuMouseEvent.Move);
  725. Point pnt = PointToClient (MousePosition);
  726. MenuAPI.TrackBarMouseEvent (tracker.hTopMenu,
  727. this, new MouseEventArgs(e.Button, e.Clicks, pnt.X, pnt.Y, e.Delta),
  728. MenuAPI.MenuMouseEvent.Move); //aku
  729. }
  730. }
  731. }
  732. protected override void CreateHandle ()
  733. {
  734. base.CreateHandle ();
  735. MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
  736. MenuAPI.CalcPopupMenuSize (DeviceContext, hMenu);
  737. Width = menu.Width;
  738. Height = menu.Height;
  739. Console.WriteLine ("CreateHandle {0} {1}", Width, Height);
  740. }
  741. private void Draw ()
  742. {
  743. MenuAPI.DrawPopupMenu (DeviceContext, hMenu, ClientRectangle);
  744. }
  745. }
  746. }