MenuAPI.cs 28 KB

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