2
0

MenuAPI.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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.Collections;
  28. namespace System.Windows.Forms
  29. {
  30. /*
  31. This class mimics the Win32 API Menu functionality
  32. */
  33. internal class MenuAPI
  34. {
  35. static ArrayList menu_list = new ArrayList ();
  36. static Font MENU_FONT = new Font (FontFamily.GenericSansSerif, 8.25f);
  37. static int POPUP_ARROW_WITDH;
  38. static int POPUP_ARROW_HEIGHT;
  39. const int SEPARATOR_HEIGHT = 5;
  40. const int SM_CXBORDER = 1;
  41. const int SM_CYBORDER = 1;
  42. const int SM_CXMENUCHECK = 14;
  43. const int SM_CYMENUCHECK = 14;
  44. public class MENU
  45. {
  46. public MF Flags; /* Menu flags (MF_POPUP, MF_SYSMENU) */
  47. public int Width; /* Width of the whole menu */
  48. public int Height; /* Height of the whole menu */
  49. public Control Wnd;
  50. public ArrayList items = new ArrayList (); /* Array of menu items */
  51. public int FocusedItem; /* Currently focused item */
  52. }
  53. public class MENUITEM
  54. {
  55. public MenuItem item;
  56. public Rectangle rect = new Rectangle ();
  57. public int fMask;
  58. public int fType;
  59. public MF fState;
  60. public int wID;
  61. public IntPtr hSubMenu;
  62. //HBITMAP hbmpChecked;
  63. //HBITMAP hbmpUnchecked;
  64. //ULONG_PTR dwItemData;
  65. //LPTSTR dwTypeData;
  66. //public int cch;
  67. //HBITMAP hbmpItem;
  68. };
  69. static void DumpMenuItems (ArrayList list)
  70. {
  71. Console.WriteLine ("Menu items dump start--- ");
  72. for (int i = 0; i < list.Count; i++)
  73. Console.WriteLine ("idx:{0} {1} {2}", i, ((MENUITEM)list[i]).item, ((MENUITEM)list[i]).item.Separator);
  74. Console.WriteLine ("Menu items dump end --- ");
  75. }
  76. internal enum MF
  77. {
  78. MF_INSERT = 0x0,
  79. MF_APPEND = 0x100,
  80. MF_DELETE = 0x200,
  81. MF_REMOVE = 0x1000,
  82. MF_BYCOMMAND = 0,
  83. MF_BYPOSITION = 0x400,
  84. MF_SEPARATOR = 0x800,
  85. MF_ENABLED = 0,
  86. MF_GRAYED = 1,
  87. MF_DISABLED = 2,
  88. MF_UNCHECKED = 0,
  89. MF_CHECKED = 8,
  90. MF_USECHECKBITMAPS = 0x200,
  91. MF_STRING = 0,
  92. MF_BITMAP = 4,
  93. MF_OWNERDRAW = 0x100,
  94. MF_POPUP = 0x10,
  95. MF_MENUBARBREAK = 0x20,
  96. MF_MENUBREAK = 0x40,
  97. MF_UNHILITE = 0,
  98. MF_HILITE = 0x80,
  99. MF_DEFAULT = 0x1000,
  100. MF_SYSMENU = 0x2000,
  101. MF_HELP = 0x4000,
  102. MF_RIGHTJUSTIFY = 0x4000
  103. }
  104. static MenuAPI ()
  105. {
  106. //static POPUP_ARROW_WITDH;
  107. //static POPUP_ARROW_HEIGHT;
  108. }
  109. static public IntPtr StoreMenuID (MENU menu)
  110. {
  111. int id = menu_list.Add (menu);
  112. return (IntPtr)(id + 1);
  113. }
  114. static public MENU GetMenuFromID (IntPtr ptr)
  115. {
  116. int id = (int)ptr;
  117. id = id - 1;
  118. return (MENU) menu_list[id];
  119. }
  120. static public IntPtr CreateMenu ()
  121. {
  122. MENU menu = new MENU ();
  123. return StoreMenuID (menu);
  124. }
  125. static public IntPtr CreatePopupMenu ()
  126. {
  127. MENU popMenu = new MENU ();
  128. popMenu.Flags |= MF.MF_POPUP;
  129. return StoreMenuID (popMenu);
  130. }
  131. static public bool InsertMenuItem (IntPtr hMenu, int uItem, bool fByPosition, MenuItem item)
  132. {
  133. if (fByPosition == false)
  134. throw new NotImplementedException ();
  135. // Insert the item
  136. MENU menu = GetMenuFromID (hMenu);
  137. if ((uint)uItem > menu.items.Count)
  138. uItem = menu.items.Count;
  139. MENUITEM menu_item = new MENUITEM ();
  140. menu_item.item = item;
  141. menu.items.Insert (uItem, menu_item);
  142. //Console.WriteLine ("InsertMenuItem {0} {1} {2}" + menu.items.Count,
  143. //);
  144. return true;
  145. }
  146. static public bool TrackPopupMenu (IntPtr hMenu, int uFlags, int x, int y, int nReserved, Control hWnd)
  147. {
  148. Console.WriteLine ("TrackPopupMenu start");
  149. MENU menu = GetMenuFromID (hMenu);
  150. PopUpWindow popup = new PopUpWindow (hMenu);
  151. menu.Wnd = popup;
  152. Point pnt;
  153. pnt = popup.PointToClient (Control.MousePosition);
  154. popup.Location = pnt;
  155. popup.ShowWindow ();
  156. MenuAPI.DumpMenuItems (menu.items);
  157. MSG msg = new MSG();
  158. while (XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
  159. XplatUI.TranslateMessage(ref msg);
  160. XplatUI.DispatchMessage(ref msg);
  161. }
  162. //popup.DestroyHandle ();
  163. Console.WriteLine ("TrackPopupMenu end");
  164. menu.Wnd = null;
  165. return true;
  166. }
  167. /*
  168. Menu drawing API
  169. */
  170. static public void CalcMenuItemSize (Graphics dc, MENUITEM item, int y)
  171. {
  172. item.rect.Y = y;
  173. if (item.item.Separator == true) {
  174. item.rect.Height = SEPARATOR_HEIGHT / 2;
  175. item.rect.Width = -1;
  176. return;
  177. }
  178. SizeF size;
  179. size = dc.MeasureString (item.item.Text, MENU_FONT);
  180. item.rect.Width = (int) size.Width + 4;
  181. item.rect.Height = (int) size.Height;
  182. item.rect.Width += SM_CXMENUCHECK * 2;
  183. //if (item.item.IsPopup)
  184. //item.rect.Width += arrow_bitmap_width;
  185. Console.WriteLine ("CalcMenuItemSize " + item.rect);
  186. }
  187. static public void CalcPopupMenuSize (Graphics dc, IntPtr hMenu)
  188. {
  189. int x = 3;
  190. int start = 0;
  191. int i, n, y, max;
  192. MENU menu = GetMenuFromID (hMenu);
  193. while (start < menu.items.Count) {
  194. y = 2;
  195. max = 0;
  196. for (i = start; i < menu.items.Count; i++) {
  197. MENUITEM item = (MENUITEM) menu.items[i];
  198. if ((i != start) && (item.item.Break || item.item.BarBreak))
  199. break;
  200. CalcMenuItemSize (dc, item, y);
  201. y += item.rect.Height;
  202. item.rect.X = x;
  203. if (item.rect.Width > max)
  204. max = item.rect.Width;
  205. }
  206. // Reemplace the -1 by the menu width (separators)
  207. for (n = start; n < i; n++, start++) {
  208. MENUITEM item = (MENUITEM) menu.items[n];
  209. item.rect.Width = max; //-4
  210. }
  211. if (y > menu.Height)
  212. menu.Height = y;
  213. x+= max;
  214. }
  215. menu.Width = x;
  216. //space for border
  217. menu.Width += 2;
  218. menu.Height += 2;
  219. menu.Width += SM_CXBORDER;
  220. menu.Height += SM_CYBORDER;
  221. Console.WriteLine ("CalcPopupMenuSize {0} {1}", menu.Width, menu.Height);
  222. }
  223. static public void DrawMenuItem (Graphics dc, MENUITEM item, int menu_height)
  224. {
  225. Console.WriteLine ("Draw item {0} {1}", item.item.Text,
  226. item.item.Separator);
  227. if (item.item.Separator == true) {
  228. // TODO: ControlPaint.DrawBorder3D (dc, item.rect,
  229. // Border3DStyle.Etched, BF_TOP);
  230. dc.FillRectangle (new SolidBrush (Color.Black),
  231. item.rect.X, item.rect.Y, item.rect.Width, item.rect.Height);
  232. return;
  233. }
  234. StringFormat string_format = new StringFormat ();
  235. string_format.LineAlignment = StringAlignment.Center;
  236. string_format.Alignment = StringAlignment.Near;
  237. Rectangle rect_text = item.rect;
  238. rect_text.X += SM_CXMENUCHECK;
  239. if (item.item.BarBreak) { /* Draw vertical break bar*/
  240. Rectangle rect = item.rect;
  241. rect.Y++;
  242. rect.Width = 3;
  243. rect.Height = menu_height - 6;
  244. //TODO: ControlPaint.DrawBorder3D (dc, item.rect,
  245. // Border3DStyle.Etched, BF_TOP);
  246. dc.FillRectangle (new SolidBrush (Color.Black),
  247. rect);
  248. }
  249. if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  250. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  251. (ThemeEngine.Current.ColorHilight), item.rect);
  252. dc.DrawString (item.item.Text, MENU_FONT, ThemeEngine.Current.ResPool.GetSolidBrush
  253. (ThemeEngine.Current.ColorHilightText), rect_text, string_format);
  254. }
  255. else
  256. dc.DrawString (item.item.Text, MENU_FONT, new SolidBrush (Color.Black),
  257. rect_text, string_format);
  258. }
  259. static public void DrawPopupMenu (Graphics dc, IntPtr hMenu)
  260. {
  261. MENU menu = GetMenuFromID (hMenu);
  262. for (int i = 0; i < menu.items.Count; i++) {
  263. DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height);
  264. }
  265. }
  266. static public MENUITEM FindItemByCoords (IntPtr hMenu, Point pt, ref int pos)
  267. {
  268. MENU menu = GetMenuFromID (hMenu);
  269. for (int i = 0; i < menu.items.Count; i++) {
  270. MENUITEM item = (MENUITEM) menu.items[i];
  271. if (item.rect.Contains (pt)) {
  272. Console.WriteLine ("FindItemByCoords: " + item.item.Text);
  273. pos = i;
  274. return item;
  275. }
  276. }
  277. Console.WriteLine ("FindItemByCoords none ");
  278. pos = -1;
  279. return null;
  280. }
  281. static public void SelectItem (IntPtr hMenu, MENUITEM item, int pos)
  282. {
  283. MENU menu = GetMenuFromID (hMenu);
  284. /* Unselect previous item*/
  285. for (int i = 0; i < menu.items.Count; i++) {
  286. MENUITEM it = (MENUITEM) menu.items[i];
  287. if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  288. it.fState = item.fState & ~MF.MF_HILITE;
  289. menu.items[i] = it;
  290. }
  291. }
  292. item.fState |= MF.MF_HILITE;
  293. menu.items[pos] = item;
  294. Console.WriteLine ("SelectItem {0} {1} {2} {3}", item.item.Text, item.fState,
  295. ((MENUITEM)(menu.items[pos])).fState, pos);
  296. }
  297. }
  298. internal class PopUpWindow : Control
  299. {
  300. private IntPtr hMenu;
  301. public PopUpWindow (IntPtr hMenu): base ()
  302. {
  303. this.hMenu = hMenu;
  304. MouseDown += new MouseEventHandler (OnMouseDownPUW);
  305. MouseMove += new MouseEventHandler (OnMouseMovePUW);
  306. Paint += new PaintEventHandler (OnPaintPUW);
  307. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  308. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  309. }
  310. protected override CreateParams CreateParams
  311. {
  312. get {
  313. CreateParams cp = base.CreateParams;
  314. cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE));
  315. return cp;
  316. }
  317. }
  318. public void ShowWindow ()
  319. {
  320. Capture = true;
  321. Show ();
  322. }
  323. protected override void OnResize(EventArgs e)
  324. {
  325. Console.WriteLine ("OnResize {0} {1} ", Width, Height);
  326. CreateBuffers (Width, Height);
  327. }
  328. private void OnPaintPUW (Object o, PaintEventArgs pevent)
  329. {
  330. Console.WriteLine ("OnPaintPUW");
  331. if (Width <= 0 || Height <= 0 || Visible == false)
  332. return;
  333. Draw ();
  334. pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
  335. }
  336. private void OnMouseDownPUW (object sender, MouseEventArgs e)
  337. {
  338. Console.WriteLine ("OnMouseDownPUW");
  339. /* Click outside the client area*/
  340. if (ClientRectangle.Contains (e.X, e.Y) == false) {
  341. Console.WriteLine ("Hide");
  342. Capture = false;
  343. Hide ();
  344. }
  345. }
  346. private void OnMouseMovePUW (object sender, MouseEventArgs e)
  347. {
  348. Console.WriteLine ("OnMouseMovePUW");
  349. int pos = 0;
  350. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y), ref pos);
  351. if (item != null) {
  352. MenuAPI.SelectItem (hMenu, item, pos);
  353. Refresh ();
  354. }
  355. }
  356. protected override void CreateHandle ()
  357. {
  358. base.CreateHandle ();
  359. MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
  360. MenuAPI.CalcPopupMenuSize (DeviceContext, hMenu);
  361. Width = menu.Width;
  362. Height = menu.Height;
  363. Console.WriteLine ("CreateHandle {0} {1}", Width, Height);
  364. }
  365. private void Draw ()
  366. {
  367. Rectangle rect = ClientRectangle;
  368. rect.Width = rect.Width - 1;
  369. rect.Height = rect.Height - 1;
  370. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  371. (ThemeEngine.Current.ColorMenu), ClientRectangle);
  372. DeviceContext.DrawRectangle (ThemeEngine.Current.ResPool.GetPen
  373. (ThemeEngine.Current.ColorHilightText), rect);
  374. MenuAPI.DrawPopupMenu (DeviceContext, hMenu);
  375. }
  376. }
  377. }