MenuAPI.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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 (!menuBar && 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. // Updates the menu rect and returns the height
  407. static public int MenuBarCalcSize (Graphics dc, IntPtr hMenu, int width)
  408. {
  409. int x = 0;
  410. int i = 0;
  411. MENU menu = GetMenuFromID (hMenu);
  412. MENUITEM item;
  413. menu.Width = 0;
  414. while (i < menu.items.Count) {
  415. item = (MENUITEM) menu.items[i];
  416. CalcItemSize (dc, item, 0, x, true);
  417. i = i + 1;
  418. x += item.rect.Width;
  419. item.fState |= MF.MF_MENUBAR;
  420. if (item.rect.Height > menu.Height)
  421. menu.Height = item.rect.Height;
  422. }
  423. menu.Width = x;
  424. return menu.Height;
  425. }
  426. // Draws a menu bar in a Window
  427. static public void DrawMenuBar (Graphics dc, IntPtr hMenu, Rectangle rect)
  428. {
  429. MENU menu = GetMenuFromID (hMenu);
  430. Rectangle rect_menu = new Rectangle ();
  431. if (menu.Height == 0)
  432. MenuBarCalcSize (dc, hMenu, rect_menu.Width);
  433. rect.Height = menu.Height;
  434. rect.Width = menu.Width;
  435. for (int i = 0; i < menu.items.Count; i++) {
  436. DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, true);
  437. }
  438. }
  439. /*
  440. Menu handeling API
  441. */
  442. static public MENUITEM FindItemByCoords (IntPtr hMenu, Point pt, ref int pos)
  443. {
  444. MENU menu = GetMenuFromID (hMenu);
  445. for (int i = 0; i < menu.items.Count; i++) {
  446. MENUITEM item = (MENUITEM) menu.items[i];
  447. if (item.rect.Contains (pt)) {
  448. //Console.WriteLine ("FindItemByCoords: " + item.item.Text);
  449. pos = i;
  450. return item;
  451. }
  452. }
  453. //Console.WriteLine ("FindItemByCoords none ");
  454. pos = -1;
  455. return null;
  456. }
  457. static public void SelectItem (TRACKER tracker, IntPtr hMenu, MENUITEM item, int pos, bool execute)
  458. {
  459. MENU menu = GetMenuFromID (hMenu);
  460. //Console.WriteLine ("Current: {0} select {1}", menu_parent.hCurrent, hMenu);
  461. MENUITEM highlight_item = null;
  462. /* Already selected */
  463. for (int i = 0; i < menu.items.Count; i++) {
  464. MENUITEM it = (MENUITEM) menu.items[i];
  465. if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  466. if (item.rect == it.rect)
  467. return;
  468. highlight_item = item;
  469. }
  470. }
  471. //Console.WriteLine ("SelectItem:Current is {0} {1}", tracker.hCurrentMenu, hMenu);
  472. if (tracker.hCurrentMenu != hMenu) {
  473. Console.WriteLine ("Changing current menu!");
  474. HideSubPopups (hMenu);
  475. tracker.hCurrentMenu = hMenu;
  476. }
  477. /* Unselect previous item*/
  478. for (int i = 0; i < menu.items.Count; i++) {
  479. MENUITEM it = (MENUITEM) menu.items[i];
  480. if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  481. it.fState = item.fState & ~MF.MF_HILITE;
  482. menu.items[i] = it;
  483. }
  484. }
  485. item.fState |= MF.MF_HILITE;
  486. menu.items[pos] = item;
  487. //Console.WriteLine ("SelectItem {0} {1} {2} {3}", item.item.Text, item.fState,
  488. // ((MENUITEM)(menu.items[pos])).fState, pos);
  489. if (execute)
  490. ExecFocusedItem (tracker, hMenu, item);
  491. }
  492. /*
  493. Used when the user executes the action of an item (press enter, shortcut)
  494. or a sub-popup menu has to be shown
  495. */
  496. static public void ExecFocusedItem (TRACKER tracker, IntPtr hMenu, MENUITEM item)
  497. {
  498. if (item.item.IsPopup) {
  499. ShowSubPopup (tracker, hMenu, item.hSubMenu, item);
  500. }
  501. else {
  502. // Execute function
  503. }
  504. }
  505. static public void ShowSubPopup (TRACKER tracker, IntPtr hParent, IntPtr hMenu, MENUITEM item)
  506. {
  507. MENU menu = GetMenuFromID (hMenu);
  508. if (menu.Wnd != null) /* Already showing */
  509. return;
  510. if (item.item.Enabled == false)
  511. return;
  512. MENU menu_parent = GetMenuFromID (hParent);
  513. PopUpWindow popup = new PopUpWindow (hMenu, tracker);
  514. ((PopUpWindow)menu_parent.Wnd).LostFocus ();
  515. menu.Wnd = popup;
  516. tracker.hCurrentMenu = hMenu;
  517. Console.WriteLine ("ShowSubPopup:Setting current to {0}", tracker.hCurrentMenu);
  518. Point pnt = new Point ();
  519. pnt.X = item.rect.X + item.rect.Width;
  520. pnt.Y = item.rect.Y + 1;
  521. Console.WriteLine ("ShowSubPopup prev:" + pnt);
  522. pnt = menu_parent.Wnd.PointToScreen (pnt);
  523. popup.Location = pnt;
  524. popup.ShowWindow ();
  525. popup.Refresh ();
  526. Console.WriteLine ("ShowSubPopup location:" + popup.Location);
  527. }
  528. /* Hides all the submenus open in a menu */
  529. static public void HideSubPopups (IntPtr hMenu)
  530. {
  531. Console.WriteLine ("HideSubPopups: " + hMenu);
  532. MENU menu = GetMenuFromID (hMenu);
  533. MENUITEM item;
  534. for (int i = 0; i < menu.items.Count; i++) {
  535. item = (MENUITEM) menu.items[i];
  536. if (item.item.IsPopup) {
  537. MENU sub_menu = GetMenuFromID (item.hSubMenu);
  538. if (sub_menu.Wnd != null) {
  539. Console.WriteLine ("Hiding!");
  540. HideSubPopups (item.hSubMenu);
  541. ((PopUpWindow)sub_menu.Wnd).Destroy ();
  542. sub_menu.Wnd = null;
  543. }
  544. }
  545. }
  546. }
  547. static public void DestroyMenu (IntPtr hMenu)
  548. {
  549. MENU menu = GetMenuFromID (hMenu);
  550. MENUITEM item;
  551. for (int i = 0; i < menu.items.Count; i++) {
  552. item = (MENUITEM) menu.items[i];
  553. //Console.WriteLine ("Destroy item: "+ item.item.Text + " pop:" + item.item.IsPopup);
  554. if (item.item.IsPopup) {
  555. MENU sub_menu = GetMenuFromID (item.hSubMenu);
  556. if (sub_menu != null && sub_menu.Wnd != null) {
  557. // TODO: Remove from list
  558. HideSubPopups (item.hSubMenu);
  559. DestroyMenu (item.hSubMenu);
  560. }
  561. }
  562. }
  563. Console.WriteLine ("Menu.Wnd1: " + menu);
  564. Console.WriteLine ("Menu.Wnd2: " + menu.Wnd);
  565. // TODO: Remove from list
  566. // Do not destroy the window of a Menubar
  567. if (menu.Wnd != null && ((menu.Flags & MF.MF_POPUP) == MF.MF_POPUP)) {
  568. ((PopUpWindow)menu.Wnd).Destroy ();
  569. menu.Wnd = null;
  570. }
  571. }
  572. static public void SetMenuBarWindow (IntPtr hMenu, Control wnd)
  573. {
  574. MENU menu = GetMenuFromID (hMenu);
  575. menu.Wnd = wnd;
  576. }
  577. static TRACKER tracker = new TRACKER ();
  578. //static Control mywnd = null;
  579. // Function that process all menubar mouse events
  580. static public void TrackBarMouseEvent (IntPtr hMenu, Control wnd, MouseEventArgs e, MenuMouseEvent eventype)
  581. {
  582. MENU menu = GetMenuFromID (hMenu);
  583. int pos = 0;
  584. Point pnt;
  585. //if (mywnd == null)
  586. // mywnd = wnd;
  587. switch (eventype) {
  588. case MenuMouseEvent.Down: {
  589. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu,
  590. new Point (e.X, e.Y), ref pos);
  591. //Console.WriteLine ("menubar: {0} {1}",item.rect.X,
  592. // item.rect.Y + item.rect.Height + 1);
  593. if (item != null) {
  594. MenuAPI.SelectItem (tracker, hMenu, item, pos, false);
  595. tracker.hCurrentMenu = hMenu;
  596. pnt = new Point (item.rect.X, item.rect.Y + item.rect.Height);
  597. pnt = wnd.PointToScreen (pnt);
  598. menu.SelectedItem = item;
  599. wnd.Refresh ();
  600. MenuAPI.TrackPopupMenu (hMenu, item.hSubMenu, pnt, true, null);
  601. }
  602. break;
  603. }
  604. case MenuMouseEvent.Move: { //aki
  605. if (tracker.hCurrentMenu != IntPtr.Zero) {
  606. Point p;
  607. p = new Point (e.X, e.Y);
  608. p = wnd.PointToScreen (p);
  609. p = menu.Wnd.PointToClient (p);
  610. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, p, ref pos);
  611. if (item != null && menu.SelectedItem != item) {
  612. //Console.WriteLine ("Changing from MenuMouseEvent.Move {0} {1} {2} {3}",
  613. // hMenu, item.item.Text, tracker.hCurrentMenu, hMenu);
  614. menu.SelectedItem = item;
  615. pnt = new Point (item.rect.X, item.rect.Y + item.rect.Height);
  616. pnt = menu.Wnd.PointToScreen (pnt);
  617. MenuAPI.SelectItem (tracker, hMenu, item, pos, false);
  618. MenuAPI.DestroyMenu (tracker.hCurrentMenu);
  619. tracker.hCurrentMenu = hMenu;
  620. menu.Wnd.Refresh ();
  621. MenuAPI.TrackPopupMenu (hMenu, item.hSubMenu, pnt, true, null);
  622. }
  623. }
  624. break;
  625. }
  626. default:
  627. break;
  628. }
  629. }
  630. }
  631. /*
  632. class PopUpWindow
  633. */
  634. internal class PopUpWindow : Control
  635. {
  636. private IntPtr hMenu;
  637. private MenuAPI.TRACKER tracker;
  638. public PopUpWindow (IntPtr hMenu, MenuAPI.TRACKER tracker): base ()
  639. {
  640. this.hMenu = hMenu;
  641. this.tracker = tracker;
  642. MouseDown += new MouseEventHandler (OnMouseDownPUW);
  643. MouseMove += new MouseEventHandler (OnMouseMovePUW);
  644. MouseUp += new MouseEventHandler (OnMouseUpPUW);
  645. Paint += new PaintEventHandler (OnPaintPUW);
  646. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  647. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  648. }
  649. protected override CreateParams CreateParams
  650. {
  651. get {
  652. CreateParams cp = base.CreateParams;
  653. cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE));
  654. cp.ExStyle |= (int)WindowStyles.WS_EX_TOOLWINDOW;
  655. return cp;
  656. }
  657. }
  658. public void ShowWindow ()
  659. {
  660. Capture = true;
  661. Show ();
  662. }
  663. public void Destroy ()
  664. {
  665. Capture = false;
  666. DestroyHandle ();
  667. }
  668. public void LostFocus ()
  669. {
  670. Capture = false;
  671. }
  672. protected override void OnResize(EventArgs e)
  673. {
  674. base.OnResize (e);
  675. Console.WriteLine ("OnResize {0} {1} ", Width, Height);
  676. }
  677. private void OnPaintPUW (Object o, PaintEventArgs pevent)
  678. {
  679. //Console.WriteLine ("OnPaintPUW");
  680. if (Width <= 0 || Height <= 0 || Visible == false)
  681. return;
  682. Draw ();
  683. pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
  684. }
  685. private void OnMouseDownPUW (object sender, MouseEventArgs e)
  686. {
  687. Console.WriteLine ("OnMouseDownPUW");
  688. /* Click outside the client area*/
  689. if (ClientRectangle.Contains (e.X, e.Y) == false) {
  690. Console.WriteLine ("Hide");
  691. Capture = false;
  692. Hide ();
  693. }
  694. }
  695. private void OnMouseUpPUW (object sender, MouseEventArgs e)
  696. {
  697. Console.WriteLine ("OnMouseUpPUW");
  698. /* Click outside the client area*/
  699. int pos = 0;
  700. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y), ref pos);
  701. if (item != null && item.item.Enabled) {
  702. item.item.PerformClick ();
  703. MenuAPI.DestroyMenu (tracker.hTopMenu);
  704. Capture = false;
  705. Refresh ();
  706. }
  707. }
  708. private void OnMouseMovePUW (object sender, MouseEventArgs e)
  709. {
  710. //Console.WriteLine ("OnMouseMovePUW");
  711. int pos = 0;
  712. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y), ref pos);
  713. if (item != null) {
  714. MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
  715. MenuAPI.SelectItem (tracker, hMenu, item, pos, true);
  716. Refresh ();
  717. } else {
  718. if (tracker.menubar) {
  719. //Console.WriteLine ("MenuBar tracker move " + e.Y);
  720. //MenuAPI.TrackBarMouseEvent (tracker.hTopMenu,
  721. // this, e, MenuAPI.MenuMouseEvent.Move);
  722. Point pnt = PointToClient (MousePosition);
  723. MenuAPI.TrackBarMouseEvent (tracker.hTopMenu,
  724. this, new MouseEventArgs(e.Button, e.Clicks, pnt.X, pnt.Y, e.Delta),
  725. MenuAPI.MenuMouseEvent.Move); //aku
  726. }
  727. }
  728. }
  729. protected override void CreateHandle ()
  730. {
  731. base.CreateHandle ();
  732. MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
  733. MenuAPI.CalcPopupMenuSize (DeviceContext, hMenu);
  734. Width = menu.Width;
  735. Height = menu.Height;
  736. Console.WriteLine ("CreateHandle {0} {1}", Width, Height);
  737. }
  738. private void Draw ()
  739. {
  740. MenuAPI.DrawPopupMenu (DeviceContext, hMenu, ClientRectangle);
  741. }
  742. }
  743. }