MenuAPI.cs 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. //
  25. // NOT COMPLETE
  26. using System.Drawing;
  27. using System.Drawing.Text;
  28. using System.Collections;
  29. namespace System.Windows.Forms
  30. {
  31. /*
  32. This class mimics the Win32 API Menu functionality
  33. When writing this code the Wine project was of great help to
  34. understand the logic behind some Win32 issues. Thanks to them. Jordi,
  35. */
  36. internal class MenuAPI
  37. {
  38. static StringFormat string_format_text = new StringFormat ();
  39. static StringFormat string_format_shortcut = new StringFormat ();
  40. static StringFormat string_format_menubar_text = new StringFormat ();
  41. static ArrayList menu_list = new ArrayList ();
  42. static Font MENU_FONT = new Font (FontFamily.GenericSansSerif, 8.25f);
  43. static int POPUP_ARROW_WITDH;
  44. static int POPUP_ARROW_HEIGHT;
  45. const int SEPARATOR_HEIGHT = 5;
  46. const int SM_CXBORDER = 1;
  47. const int SM_CYBORDER = 1;
  48. const int SM_CXMENUCHECK = 14; // Width of the menu check
  49. const int SM_CYMENUCHECK = 14; // Height of the menu check
  50. const int SM_CXARROWCHECK = 16; // Width of the arrow
  51. const int SM_CYARROWCHECK = 16; // Height of the arrow
  52. const int SM_CYMENU = 18; // Minimum height of a menu
  53. const int MENU_TAB_SPACE = 8; // Pixels added to the width of an item because of a tab
  54. const int MENU_BAR_ITEMS_SPACE = 8; // Space between menu bar items
  55. public class MENU
  56. {
  57. public MF Flags; // Menu flags (MF_POPUP, MF_SYSMENU)
  58. public int Width; // Width of the whole menu
  59. public int Height; // Height of the whole menu
  60. public Control Wnd; // In a Popup menu is the PopupWindow and in a MenuBar the Form
  61. public ArrayList items; // Array of menu items
  62. public int FocusedItem; // Currently focused item
  63. public IntPtr hParent;
  64. public MENUITEM SelectedItem; // Currently focused item
  65. public bool bMenubar;
  66. public Menu menu; // SWF.Menu
  67. public MENU (Menu menu_obj)
  68. {
  69. Wnd = null;
  70. hParent = IntPtr.Zero;
  71. items = new ArrayList ();
  72. Flags = MF.MF_INSERT;
  73. Width = Height = FocusedItem = 0;
  74. bMenubar = false;
  75. menu = menu_obj;
  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 (Menu menu_obj)
  170. {
  171. MENU menu = new MENU (menu_obj);
  172. return StoreMenuID (menu);
  173. }
  174. static public IntPtr CreatePopupMenu (Menu menu_obj)
  175. {
  176. MENU popMenu = new MENU (menu_obj);
  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 (menu_item.item);
  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. TRACKER tracker = new TRACKER ();
  207. MENU menu = GetMenuFromID (hMenu);
  208. menu.Wnd = new PopUpWindow (hMenu, tracker);
  209. tracker.hCurrentMenu = hMenu;
  210. tracker.hTopMenu = hTopMenu;
  211. MENUITEM select_item = GetNextItem (hMenu, ItemNavigation.First);
  212. if (select_item != null)
  213. MenuAPI.SelectItem (hMenu, select_item, false, tracker);
  214. menu.Wnd.Location = menu.Wnd.PointToClient (pnt);
  215. if (menu.menu.IsDirty) {
  216. menu.items.Clear ();
  217. menu.menu.CreateItems ();
  218. ((PopUpWindow)menu.Wnd).RefreshItems ();
  219. menu.menu.IsDirty = false;
  220. }
  221. ((PopUpWindow)menu.Wnd).ShowWindow ();
  222. Application.Run ();
  223. if (menu.Wnd == null) {
  224. menu.Wnd.Dispose ();
  225. menu.Wnd = null;
  226. }
  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. }
  262. static public void CalcPopupMenuSize (Graphics dc, IntPtr hMenu)
  263. {
  264. int x = 3;
  265. int start = 0;
  266. int i, n, y, max;
  267. MENU menu = GetMenuFromID (hMenu);
  268. menu.Height = 0;
  269. while (start < menu.items.Count) {
  270. y = 2;
  271. max = 0;
  272. for (i = start; i < menu.items.Count; i++) {
  273. MENUITEM item = (MENUITEM) menu.items[i];
  274. if ((i != start) && (item.item.Break || item.item.BarBreak))
  275. break;
  276. CalcItemSize (dc, item, y, x, false);
  277. y += item.rect.Height;
  278. if (item.rect.Width > max)
  279. max = item.rect.Width;
  280. }
  281. // Reemplace the -1 by the menu width (separators)
  282. for (n = start; n < i; n++, start++) {
  283. MENUITEM item = (MENUITEM) menu.items[n];
  284. item.rect.Width = max;
  285. }
  286. if (y > menu.Height)
  287. menu.Height = y;
  288. x+= max;
  289. }
  290. menu.Width = x;
  291. //space for border
  292. menu.Width += 2;
  293. menu.Height += 2;
  294. menu.Width += SM_CXBORDER;
  295. menu.Height += SM_CYBORDER;
  296. }
  297. static public void DrawMenuItem (Graphics dc, MENUITEM item, int menu_height, bool menuBar)
  298. {
  299. StringFormat string_format;
  300. if (item.item.Visible == false)
  301. return;
  302. if (menuBar)
  303. string_format = string_format_menubar_text;
  304. else
  305. string_format = string_format_text;
  306. if (item.item.Separator == true) {
  307. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
  308. item.rect.X, item.rect.Y, item.rect.X + item.rect.Width, item.rect.Y);
  309. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
  310. item.rect.X, item.rect.Y + 1, item.rect.X + item.rect.Width, item.rect.Y + 1);
  311. return;
  312. }
  313. Rectangle rect_text = item.rect;
  314. if (!menuBar)
  315. rect_text.X += SM_CXMENUCHECK;
  316. if (item.item.BarBreak) { /* Draw vertical break bar*/
  317. Rectangle rect = item.rect;
  318. rect.Y++;
  319. rect.Width = 3;
  320. rect.Height = menu_height - 6;
  321. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
  322. rect.X, rect.Y , rect.X, rect.Y + rect.Height);
  323. dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
  324. rect.X + 1, rect.Y , rect.X +1, rect.Y + rect.Height);
  325. }
  326. if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  327. Rectangle rect = item.rect;
  328. rect.X++;
  329. rect.Width -=2;
  330. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  331. (ThemeEngine.Current.ColorHilight), 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 - SM_CYMENUCHECK) / 2));
  380. gr.Dispose ();
  381. bmp.Dispose ();
  382. }
  383. }
  384. static public void DrawPopupMenu (Graphics dc, IntPtr hMenu, Rectangle cliparea, Rectangle rect)
  385. {
  386. MENU menu = GetMenuFromID (hMenu);
  387. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  388. (ThemeEngine.Current.ColorMenu), cliparea);
  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. if (cliparea.IntersectsWith (((MENUITEM) menu.items[i]).rect)) {
  404. DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, menu.bMenubar);
  405. }
  406. }
  407. // Updates the menu rect and returns the height
  408. static public int MenuBarCalcSize (Graphics dc, IntPtr hMenu, int width)
  409. {
  410. int x = 0;
  411. int i = 0;
  412. int y = 0;
  413. MENU menu = GetMenuFromID (hMenu);
  414. menu.Height = 0;
  415. MENUITEM item;
  416. while (i < menu.items.Count) {
  417. item = (MENUITEM) menu.items[i];
  418. CalcItemSize (dc, item, y, x, true);
  419. i = i + 1;
  420. if (x + item.rect.Width > width) {
  421. item.rect.X = 0;
  422. y += item.rect.Height;
  423. item.rect.Y = y;
  424. x = 0;
  425. }
  426. x += item.rect.Width;
  427. item.fState |= MF.MF_MENUBAR;
  428. if (y + item.rect.Height > menu.Height)
  429. menu.Height = item.rect.Height + y;
  430. }
  431. menu.Width = width;
  432. return menu.Height;
  433. }
  434. // Draws a menu bar in a Window
  435. static public void DrawMenuBar (Graphics dc, IntPtr hMenu, Rectangle rect)
  436. {
  437. MENU menu = GetMenuFromID (hMenu);
  438. Rectangle rect_menu = new Rectangle ();
  439. if (menu.Height == 0)
  440. MenuBarCalcSize (dc, hMenu, rect_menu.Width);
  441. rect.Height = menu.Height;
  442. rect.Width = menu.Width;
  443. for (int i = 0; i < menu.items.Count; i++)
  444. DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, true);
  445. }
  446. /*
  447. Menu handeling API
  448. */
  449. static public MENUITEM FindItemByCoords (IntPtr hMenu, Point pt)
  450. {
  451. MENU menu = GetMenuFromID (hMenu);
  452. for (int i = 0; i < menu.items.Count; i++) {
  453. MENUITEM item = (MENUITEM) menu.items[i];
  454. if (item.rect.Contains (pt)) {
  455. return item;
  456. }
  457. }
  458. return null;
  459. }
  460. // Select the item and unselect the previous selecte item
  461. static public void SelectItem (IntPtr hMenu, MENUITEM item, bool execute, TRACKER tracker)
  462. {
  463. MENU menu = GetMenuFromID (hMenu);
  464. MENUITEM previous_selitem = null;
  465. /* Already selected */
  466. for (int i = 0; i < menu.items.Count; i++) {
  467. MENUITEM it = (MENUITEM) menu.items[i];
  468. if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  469. if (item.rect == it.rect) {
  470. return;
  471. }
  472. /* Unselect previous item*/
  473. previous_selitem = it;
  474. it.fState = it.fState & ~MF.MF_HILITE;
  475. menu.Wnd.Invalidate (previous_selitem.rect);
  476. break;
  477. }
  478. }
  479. // If the previous item had subitems, hide them
  480. if (previous_selitem != null && previous_selitem.item.IsPopup)
  481. HideSubPopups (hMenu);
  482. if (tracker.hCurrentMenu != hMenu) {
  483. menu.Wnd.Capture = true;
  484. tracker.hCurrentMenu = hMenu;
  485. }
  486. menu.SelectedItem = item;
  487. item.fState |= MF.MF_HILITE;
  488. menu.Wnd.Invalidate (item.rect);
  489. item.item.PerformSelect ();
  490. if (execute)
  491. ExecFocusedItem (hMenu, item, tracker);
  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. static public void ExecFocusedItem (IntPtr hMenu, MENUITEM item, TRACKER tracker)
  496. {
  497. if (item.item.Enabled == false)
  498. return;
  499. if (item.item.IsPopup) {
  500. ShowSubPopup (hMenu, item.hSubMenu, item, tracker);
  501. }
  502. else {
  503. // Execute function
  504. }
  505. }
  506. // Create a popup window and show it or only show it if it is already created
  507. static public void ShowSubPopup (IntPtr hParent, IntPtr hMenu, MENUITEM item, TRACKER tracker)
  508. {
  509. MENU menu = GetMenuFromID (hMenu);
  510. Point pnt = new Point ();
  511. if (item.item.Enabled == false)
  512. return;
  513. MENU menu_parent = GetMenuFromID (hParent);
  514. ((PopUpWindow)menu_parent.Wnd).LostFocus ();
  515. tracker.hCurrentMenu = hMenu;
  516. if (menu.Wnd == null)
  517. menu.Wnd = new PopUpWindow (hMenu, tracker);
  518. pnt.X = item.rect.X + item.rect.Width;
  519. pnt.Y = item.rect.Y + 1;
  520. pnt = menu_parent.Wnd.PointToScreen (pnt);
  521. menu.Wnd.Location = pnt;
  522. MENUITEM select_item = GetNextItem (hMenu, ItemNavigation.First);
  523. if (select_item != null)
  524. MenuAPI.SelectItem (hMenu, select_item, false, tracker);
  525. ((PopUpWindow)menu.Wnd).ShowWindow ();
  526. Console.WriteLine ("ShowSubPopup end {0} {1}", ((PopUpWindow)menu.Wnd).Location,
  527. ((PopUpWindow)menu.Wnd).Size);
  528. }
  529. /* Hides all the submenus open in a menu */
  530. static public void HideSubPopups (IntPtr hMenu)
  531. {
  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. continue;
  538. MENU sub_menu = GetMenuFromID (item.hSubMenu);
  539. if (sub_menu.Wnd != null) {
  540. HideSubPopups (item.hSubMenu);
  541. ((PopUpWindow)sub_menu.Wnd).Hide ();
  542. }
  543. }
  544. }
  545. static public void DestroyMenu (IntPtr hMenu)
  546. {
  547. if (hMenu == IntPtr.Zero)
  548. return;
  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. if (item.item.IsPopup) {
  554. MENU sub_menu = GetMenuFromID (item.hSubMenu);
  555. if (sub_menu != null && sub_menu.Wnd != null)
  556. HideSubPopups (item.hSubMenu);
  557. DestroyMenu (item.hSubMenu);
  558. }
  559. }
  560. // Do not destroy the window of a Menubar
  561. if (menu.Wnd != null && menu.bMenubar == false) {
  562. ((PopUpWindow)menu.Wnd).Dispose ();
  563. menu.Wnd = null;
  564. }
  565. /* Unreference from the array list */
  566. menu_list[((int)hMenu)-1] = null;
  567. }
  568. // Find item by screen coordinates
  569. static public bool FindSubItemByCoord (IntPtr hMenu, Point pnt, ref IntPtr hMenuItem, ref MENUITEM itemfound)
  570. {
  571. Point pnt_client;
  572. Rectangle rect;
  573. MENU menu = GetMenuFromID (hMenu);
  574. MENUITEM item;
  575. for (int i = 0; i < menu.items.Count; i++) {
  576. item = (MENUITEM) menu.items[i];
  577. if (item.item.IsPopup)
  578. if (FindSubItemByCoord (item.hSubMenu, pnt, ref hMenuItem, ref itemfound) == true)
  579. return true;
  580. if (menu.Wnd == null) // Menu has not been created yet
  581. continue;
  582. rect = item.rect;
  583. pnt_client = menu.Wnd.PointToScreen (new Point (item.rect.X, item.rect.Y));
  584. rect.X = pnt_client.X;
  585. rect.Y = pnt_client.Y;
  586. if (rect.Contains (pnt) == true) {
  587. itemfound = item;
  588. hMenuItem = hMenu;
  589. return true;
  590. }
  591. }
  592. return false;
  593. }
  594. static public void SetMenuBarWindow (IntPtr hMenu, Control wnd)
  595. {
  596. MENU menu = GetMenuFromID (hMenu);
  597. menu.Wnd = wnd;
  598. menu.bMenubar = true;
  599. }
  600. static private void MenuBarMove (IntPtr hMenu, MENUITEM item, TRACKER tracker)
  601. {
  602. MENU menu = GetMenuFromID (hMenu);
  603. Point pnt = new Point (item.rect.X, item.rect.Y + item.rect.Height);
  604. pnt = menu.Wnd.PointToScreen (pnt);
  605. MenuAPI.SelectItem (hMenu, item, false, tracker);
  606. HideSubPopups (tracker.hCurrentMenu);
  607. tracker.hCurrentMenu = hMenu;
  608. MenuAPI.TrackPopupMenu (hMenu, item.hSubMenu, pnt, false, null);
  609. }
  610. // Function that process all menubar mouse events
  611. static public void TrackBarMouseEvent (IntPtr hMenu, Control wnd, MouseEventArgs e, MenuMouseEvent eventype, TRACKER tracker)
  612. {
  613. MENU menu = GetMenuFromID (hMenu);
  614. switch (eventype) {
  615. case MenuMouseEvent.Down: {
  616. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
  617. if (item != null)
  618. MenuBarMove (hMenu, item, tracker);
  619. break;
  620. }
  621. case MenuMouseEvent.Move: { /* Coordinates in screen position*/
  622. if (tracker.hCurrentMenu != IntPtr.Zero) {
  623. Point pnt = new Point (e.X, e.Y);
  624. pnt = menu.Wnd.PointToClient (pnt);
  625. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, pnt);
  626. if (item != null && menu.SelectedItem != item)
  627. MenuBarMove (hMenu, item, tracker);
  628. }
  629. break;
  630. }
  631. default:
  632. break;
  633. }
  634. }
  635. static public MENUITEM FindItemByKey (IntPtr hMenu, IntPtr key)
  636. {
  637. MENU menu = GetMenuFromID (hMenu);
  638. MENUITEM item;
  639. char key_char = (char ) (key.ToInt32() & 0xff);
  640. key_char = Char.ToUpper (key_char);
  641. for (int i = 0; i < menu.items.Count; i++) {
  642. item = (MENUITEM) menu.items[i];
  643. if (item.item.Mnemonic == '\0')
  644. continue;
  645. if (item.item.Mnemonic == key_char)
  646. return item;
  647. }
  648. return null;
  649. }
  650. // Get the next or previous selectable item on a menu
  651. static public MENUITEM GetNextItem (IntPtr hMenu, ItemNavigation navigation)
  652. {
  653. MENU menu = GetMenuFromID (hMenu);
  654. int pos = 0;
  655. bool selectable_items = false;
  656. MENUITEM item;
  657. // Check if there is at least a selectable item
  658. for (int i = 0; i < menu.items.Count; i++) {
  659. item = (MENUITEM)menu.items[i];
  660. if (item.item.Separator == false && item.item.Visible == true) {
  661. selectable_items = true;
  662. break;
  663. }
  664. }
  665. if (selectable_items == false)
  666. return null;
  667. switch (navigation) {
  668. case ItemNavigation.First: {
  669. pos = 0;
  670. /* Next item that is not separator and it is visible*/
  671. for (; pos < menu.items.Count; pos++) {
  672. item = (MENUITEM)menu.items[pos];
  673. if (item.item.Separator == false && item.item.Visible == true)
  674. break;
  675. }
  676. if (pos >= menu.items.Count) { /* Jump at the start of the menu */
  677. pos = 0;
  678. /* Next item that is not separator and it is visible*/
  679. for (; pos < menu.items.Count; pos++) {
  680. item = (MENUITEM)menu.items[pos];
  681. if (item.item.Separator == false && item.item.Visible == true)
  682. break;
  683. }
  684. }
  685. break;
  686. }
  687. case ItemNavigation.Last: { // Not used
  688. break;
  689. }
  690. case ItemNavigation.Next: {
  691. if (menu.SelectedItem != null)
  692. pos = menu.SelectedItem.pos;
  693. /* Next item that is not separator and it is visible*/
  694. for (pos++; pos < menu.items.Count; pos++) {
  695. item = (MENUITEM)menu.items[pos];
  696. if (item.item.Separator == false && item.item.Visible == true)
  697. break;
  698. }
  699. if (pos >= menu.items.Count) { /* Jump at the start of the menu */
  700. pos = 0;
  701. /* Next item that is not separator and it is visible*/
  702. for (; pos < menu.items.Count; pos++) {
  703. item = (MENUITEM)menu.items[pos];
  704. if (item.item.Separator == false && item.item.Visible == true)
  705. break;
  706. }
  707. }
  708. break;
  709. }
  710. case ItemNavigation.Previous: {
  711. if (menu.SelectedItem != null)
  712. pos = menu.SelectedItem.pos;
  713. /* Previous item that is not separator and it is visible*/
  714. for (pos--; pos >= 0; pos--) {
  715. item = (MENUITEM)menu.items[pos];
  716. if (item.item.Separator == false && item.item.Visible == true)
  717. break;
  718. }
  719. if (pos < 0 ) { /* Jump at the end of the menu*/
  720. pos = menu.items.Count - 1;
  721. /* Previous item that is not separator and it is visible*/
  722. for (; pos >= 0; pos--) {
  723. item = (MENUITEM)menu.items[pos];
  724. if (item.item.Separator == false && item.item.Visible == true)
  725. break;
  726. }
  727. }
  728. break;
  729. }
  730. default:
  731. break;
  732. }
  733. return (MENUITEM)menu.items[pos];
  734. }
  735. static public bool ProcessKeys (IntPtr hMenu, ref Message msg, Keys keyData, TRACKER tracker)
  736. {
  737. MENU menu = GetMenuFromID (hMenu);
  738. MENUITEM item;
  739. switch (keyData) {
  740. case Keys.Up: {
  741. item = GetNextItem (hMenu, ItemNavigation.Previous);
  742. if (item != null)
  743. MenuAPI.SelectItem (hMenu, item, false, tracker);
  744. break;
  745. }
  746. case Keys.Down: {
  747. item = GetNextItem (hMenu, ItemNavigation.Next);
  748. if (item != null)
  749. MenuAPI.SelectItem (hMenu, item, false, tracker);
  750. break;
  751. }
  752. /* Menubar selects and opens next. Popups next or open*/
  753. case Keys.Right: {
  754. // Try to Expand popup first
  755. if (menu.SelectedItem.item.IsPopup) {
  756. ShowSubPopup (hMenu, menu.SelectedItem.hSubMenu, menu.SelectedItem, tracker);
  757. } else {
  758. MENU parent = null;
  759. if (menu.hParent != IntPtr.Zero)
  760. parent = GetMenuFromID (menu.hParent);
  761. if (parent != null && parent.bMenubar == true) {
  762. MENUITEM select_item = GetNextItem (menu.hParent, ItemNavigation.Next);
  763. MenuBarMove (menu.hParent, select_item, tracker);
  764. }
  765. }
  766. break;
  767. }
  768. case Keys.Left: {
  769. // Try to Collapse popup first
  770. if (menu.SelectedItem.item.IsPopup) {
  771. } else {
  772. MENU parent = null;
  773. if (menu.hParent != IntPtr.Zero)
  774. parent = GetMenuFromID (menu.hParent);
  775. if (parent != null && parent.bMenubar == true) {
  776. MENUITEM select_item = GetNextItem (menu.hParent, ItemNavigation.Previous);
  777. MenuBarMove (menu.hParent, select_item, tracker);
  778. }
  779. }
  780. break;
  781. }
  782. case Keys.Return: {
  783. Console.WriteLine ("Return key: "+ menu.SelectedItem.item.Text);
  784. MenuAPI.ExecFocusedItem (hMenu, menu.SelectedItem, tracker);
  785. break;
  786. }
  787. default:
  788. break;
  789. }
  790. /* Try if it is a menu hot key */
  791. item = MenuAPI.FindItemByKey (hMenu, msg.WParam);
  792. if (item != null) {
  793. MenuAPI.SelectItem (hMenu, item, false, tracker);
  794. return true;
  795. }
  796. return false;
  797. }
  798. }
  799. /*
  800. class PopUpWindow
  801. */
  802. internal class PopUpWindow : Control
  803. {
  804. private IntPtr hMenu;
  805. private MenuAPI.TRACKER tracker;
  806. public PopUpWindow (IntPtr hMenu, MenuAPI.TRACKER tracker): base ()
  807. {
  808. this.hMenu = hMenu;
  809. this.tracker = tracker;
  810. MouseDown += new MouseEventHandler (OnMouseDownPUW);
  811. MouseMove += new MouseEventHandler (OnMouseMovePUW);
  812. MouseUp += new MouseEventHandler (OnMouseUpPUW);
  813. Paint += new PaintEventHandler (OnPaintPUW);
  814. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  815. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  816. }
  817. protected override CreateParams CreateParams
  818. {
  819. get {
  820. CreateParams cp = base.CreateParams;
  821. cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN));
  822. cp.ExStyle |= (int)WindowStyles.WS_EX_TOOLWINDOW;
  823. return cp;
  824. }
  825. }
  826. public void ShowWindow ()
  827. {
  828. Capture = true;
  829. Show ();
  830. Refresh ();
  831. }
  832. public void LostFocus ()
  833. {
  834. Capture = false;
  835. }
  836. protected override void OnResize (EventArgs e)
  837. {
  838. base.OnResize (e);
  839. }
  840. private void OnPaintPUW (Object o, PaintEventArgs pevent)
  841. {
  842. if (Width <= 0 || Height <= 0 || Visible == false)
  843. return;
  844. Draw (pevent.ClipRectangle);
  845. pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  846. }
  847. private void OnMouseDownPUW (object sender, MouseEventArgs e)
  848. {
  849. /* Click outside the client area*/
  850. if (ClientRectangle.Contains (e.X, e.Y) == false) {
  851. Capture = false;
  852. Hide ();
  853. }
  854. }
  855. private void OnMouseUpPUW (object sender, MouseEventArgs e)
  856. {
  857. /* Click outside the client area*/
  858. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
  859. if (item != null) {
  860. item.item.PerformClick ();
  861. Hide ();
  862. Capture = false;
  863. }
  864. }
  865. private void OnMouseMovePUW (object sender, MouseEventArgs e)
  866. {
  867. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
  868. if (item != null) {
  869. MenuAPI.SelectItem (hMenu, item, true, tracker);
  870. } else {
  871. MenuAPI.MENU menu_parent = null;
  872. if (tracker.hTopMenu != IntPtr.Zero)
  873. menu_parent = MenuAPI.GetMenuFromID (tracker.hTopMenu);
  874. if (menu_parent == null)
  875. return;
  876. if (menu_parent.bMenubar) {
  877. MenuAPI.TrackBarMouseEvent (tracker.hTopMenu,
  878. this, new MouseEventArgs(e.Button, e.Clicks, MousePosition.X, MousePosition.Y, e.Delta),
  879. MenuAPI.MenuMouseEvent.Move, tracker);
  880. }
  881. IntPtr hMenuItem = IntPtr.Zero;
  882. MenuAPI.MENUITEM item_found = null;
  883. if (MenuAPI.FindSubItemByCoord (tracker.hTopMenu, MousePosition, ref hMenuItem, ref item_found) == false)
  884. return;
  885. MenuAPI.SelectItem (hMenuItem, item_found, false, tracker);
  886. }
  887. }
  888. protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
  889. {
  890. return MenuAPI.ProcessKeys (hMenu, ref msg, keyData, tracker);
  891. }
  892. protected override void CreateHandle ()
  893. {
  894. base.CreateHandle ();
  895. RefreshItems ();
  896. }
  897. // Called when the number of items has changed
  898. internal void RefreshItems ()
  899. {
  900. MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
  901. MenuAPI.CalcPopupMenuSize (DeviceContext, hMenu);
  902. Width = menu.Width;
  903. Height = menu.Height;
  904. }
  905. private void Draw (Rectangle clip)
  906. {
  907. MenuAPI.DrawPopupMenu (DeviceContext, hMenu, clip, ClientRectangle);
  908. }
  909. }
  910. }