MenuAPI.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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 ArrayList menu_list = new ArrayList ();
  39. static Font MENU_FONT = new Font (FontFamily.GenericSansSerif, 8.25f);
  40. static int POPUP_ARROW_WITDH;
  41. static int POPUP_ARROW_HEIGHT;
  42. const int SEPARATOR_HEIGHT = 5;
  43. const int SM_CXBORDER = 1;
  44. const int SM_CYBORDER = 1;
  45. const int SM_CXMENUCHECK = 14; // Width of the menu check
  46. const int SM_CYMENUCHECK = 14; // Height of the menu check
  47. const int SM_CXARROWCHECK = 16; // Width of the arrow
  48. const int SM_CYARROWCHECK = 16; // Height of the arrow
  49. const int SM_CYMENU = 18; // Minimum height of a menu
  50. const int MENU_TAB_SPACE = 8; // Pixels added to the width of an item because of a tab
  51. public class MENU
  52. {
  53. public MF Flags; // Menu flags (MF_POPUP, MF_SYSMENU)
  54. public int Width; // Width of the whole menu
  55. public int Height; // Height of the whole menu
  56. public PopUpWindow Wnd;
  57. public ArrayList items; // Array of menu items
  58. public int FocusedItem; // Currently focused item
  59. public IntPtr hParent;
  60. public MENU ()
  61. {
  62. Wnd = null;
  63. hParent = IntPtr.Zero;
  64. items = new ArrayList ();
  65. }
  66. }
  67. public class MENUITEM
  68. {
  69. public MenuItem item;
  70. public Rectangle rect;
  71. public int fMask;
  72. public int fType;
  73. public MF fState;
  74. public int wID;
  75. public IntPtr hSubMenu;
  76. public int xTab;
  77. public MENUITEM ()
  78. {
  79. xTab = 0;
  80. fMask = 0;
  81. wID = 0;
  82. rect = new Rectangle ();
  83. }
  84. };
  85. public class TRACKER
  86. {
  87. public IntPtr hCurrentMenu;
  88. public IntPtr hTopMenu;
  89. };
  90. static void DumpMenuItems (ArrayList list)
  91. {
  92. Console.WriteLine ("Menu items dump start--- ");
  93. for (int i = 0; i < list.Count; i++)
  94. Console.WriteLine ("idx:{0} {1} {2}", i, ((MENUITEM)list[i]).item, ((MENUITEM)list[i]).item.Separator);
  95. Console.WriteLine ("Menu items dump end --- ");
  96. }
  97. internal enum MF
  98. {
  99. MF_INSERT = 0x0,
  100. MF_APPEND = 0x100,
  101. MF_DELETE = 0x200,
  102. MF_REMOVE = 0x1000,
  103. MF_BYCOMMAND = 0,
  104. MF_BYPOSITION = 0x400,
  105. MF_SEPARATOR = 0x800,
  106. MF_ENABLED = 0,
  107. MF_GRAYED = 1,
  108. MF_DISABLED = 2,
  109. MF_UNCHECKED = 0,
  110. MF_CHECKED = 8,
  111. MF_USECHECKBITMAPS = 0x200,
  112. MF_STRING = 0,
  113. MF_BITMAP = 4,
  114. MF_OWNERDRAW = 0x100,
  115. MF_POPUP = 0x10,
  116. MF_MENUBARBREAK = 0x20,
  117. MF_MENUBREAK = 0x40,
  118. MF_UNHILITE = 0,
  119. MF_HILITE = 0x80,
  120. MF_DEFAULT = 0x1000,
  121. MF_SYSMENU = 0x2000,
  122. MF_HELP = 0x4000,
  123. MF_RIGHTJUSTIFY = 0x4000
  124. }
  125. static MenuAPI ()
  126. {
  127. Console.WriteLine ("MenuAPI::MenuAPI");
  128. string_format_text.LineAlignment = StringAlignment.Center;
  129. string_format_text.Alignment = StringAlignment.Near;
  130. string_format_text.HotkeyPrefix = HotkeyPrefix.Show;
  131. string_format_shortcut.LineAlignment = StringAlignment.Center;
  132. string_format_shortcut.Alignment = StringAlignment.Far;
  133. }
  134. static public IntPtr StoreMenuID (MENU menu)
  135. {
  136. int id = menu_list.Add (menu);
  137. //Console.WriteLine ("StoreMenuID:" + id + 1);
  138. return (IntPtr)(id + 1);
  139. }
  140. static public MENU GetMenuFromID (IntPtr ptr)
  141. {
  142. int id = (int)ptr;
  143. //if (id == 0)
  144. // return null;
  145. id = id - 1;
  146. return (MENU) menu_list[id];
  147. }
  148. static public IntPtr CreateMenu ()
  149. {
  150. MENU menu = new MENU ();
  151. return StoreMenuID (menu);
  152. }
  153. static public IntPtr CreatePopupMenu ()
  154. {
  155. Console.WriteLine ("MenuAPI.CreatePopupMenu");
  156. MENU popMenu = new MENU ();
  157. popMenu.Flags |= MF.MF_POPUP;
  158. return StoreMenuID (popMenu);
  159. }
  160. static public int InsertMenuItem (IntPtr hMenu, int uItem, bool fByPosition, MenuItem item,
  161. ref IntPtr hSubMenu)
  162. {
  163. int id;
  164. if (fByPosition == false)
  165. throw new NotImplementedException ();
  166. // Insert the item
  167. MENU menu = GetMenuFromID (hMenu);
  168. if ((uint)uItem > menu.items.Count)
  169. uItem = menu.items.Count;
  170. MENUITEM menu_item = new MENUITEM ();
  171. menu_item.item = item;
  172. if (item.IsPopup) {
  173. menu_item.hSubMenu = CreatePopupMenu ();
  174. }
  175. else
  176. menu_item.hSubMenu = IntPtr.Zero;
  177. //menu_item.Flags |= MF.MF_POPUP;
  178. hSubMenu = menu_item.hSubMenu;
  179. id = menu.items.Count;
  180. menu.items.Insert (uItem, menu_item);
  181. //Console.WriteLine ("InsertMenuItem {0} {1} {2}" + menu.items.Count,
  182. //);
  183. return id;
  184. }
  185. static public bool TrackPopupMenu (IntPtr hMenu, int uFlags, int x, int y, int nReserved, Control hWnd)
  186. {
  187. Console.WriteLine ("TrackPopupMenu start");
  188. MENU menu = GetMenuFromID (hMenu);
  189. TRACKER tracker = new TRACKER ();
  190. PopUpWindow popup = new PopUpWindow (hMenu, tracker);
  191. menu.Wnd = popup;
  192. tracker.hCurrentMenu = hMenu;
  193. tracker.hTopMenu = hMenu;
  194. //Console.WriteLine ("TrackPopupMenu:Setting current to {0}", menu.hCurrent);
  195. Point pnt;
  196. pnt = popup.PointToClient (Control.MousePosition);
  197. popup.Location = pnt;
  198. popup.ShowWindow ();
  199. MenuAPI.DumpMenuItems (menu.items);
  200. MSG msg = new MSG();
  201. while (XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
  202. XplatUI.TranslateMessage(ref msg);
  203. XplatUI.DispatchMessage(ref msg);
  204. }
  205. //popup.DestroyHandle ();
  206. Console.WriteLine ("TrackPopupMenu end");
  207. menu.Wnd = null;
  208. return true;
  209. }
  210. /*
  211. Menu drawing API
  212. */
  213. static public void CalcItemSize (Graphics dc, MENUITEM item, int y)
  214. {
  215. item.rect.Y = y;
  216. if (item.item.Separator == true) {
  217. item.rect.Height = SEPARATOR_HEIGHT / 2;
  218. item.rect.Width = -1;
  219. return;
  220. }
  221. SizeF size;
  222. size = dc.MeasureString (item.item.Text, MENU_FONT);
  223. item.rect.Width = (int) size.Width + 4;
  224. item.rect.Height = (int) size.Height;
  225. if (item.item.Shortcut != Shortcut.None) {
  226. item.xTab = SM_CXMENUCHECK + MENU_TAB_SPACE + (int) size.Width;
  227. size = dc.MeasureString ("\t" + item.item.GetShortCutText (), MENU_FONT);
  228. item.rect.Width += MENU_TAB_SPACE + (int) size.Width;
  229. }
  230. item.rect.Width += SM_CXMENUCHECK * 2;
  231. if (item.rect.Height < SM_CYMENU - 1)
  232. item.rect.Height = SM_CYMENU - 1;
  233. //if (item.item.IsPopup)
  234. // item.rect.Width += arrow_bitmap_width;
  235. Console.WriteLine ("CalcItemSize " + item.rect);
  236. }
  237. static public void CalcPopupMenuSize (Graphics dc, IntPtr hMenu)
  238. {
  239. int x = 3;
  240. int start = 0;
  241. int i, n, y, max;
  242. MENU menu = GetMenuFromID (hMenu);
  243. while (start < menu.items.Count) {
  244. y = 2;
  245. max = 0;
  246. for (i = start; i < menu.items.Count; i++) {
  247. MENUITEM item = (MENUITEM) menu.items[i];
  248. if ((i != start) && (item.item.Break || item.item.BarBreak))
  249. break;
  250. CalcItemSize (dc, item, y);
  251. y += item.rect.Height;
  252. item.rect.X = x;
  253. if (item.rect.Width > max)
  254. max = item.rect.Width;
  255. }
  256. // Reemplace the -1 by the menu width (separators)
  257. for (n = start; n < i; n++, start++) {
  258. MENUITEM item = (MENUITEM) menu.items[n];
  259. item.rect.Width = max; //-4
  260. }
  261. if (y > menu.Height)
  262. menu.Height = y;
  263. x+= max;
  264. }
  265. menu.Width = x;
  266. //space for border
  267. menu.Width += 2;
  268. menu.Height += 2;
  269. menu.Width += SM_CXBORDER;
  270. menu.Height += SM_CYBORDER;
  271. //Console.WriteLine ("CalcPopupMenuSize {0} {1}", menu.Width, menu.Height);
  272. }
  273. static public void DrawMenuItem (Graphics dc, MENUITEM item, int menu_height)
  274. {
  275. //Console.WriteLine ("Draw item {0} {1}", item.item.Text,
  276. // item.item.Separator);
  277. if (item.item.Separator == true) {
  278. // TODO: ControlPaint.DrawBorder3D (dc, item.rect,
  279. // Border3DStyle.Etched, BF_TOP);
  280. dc.FillRectangle (new SolidBrush (Color.Black),
  281. item.rect.X, item.rect.Y, item.rect.Width, item.rect.Height);
  282. return;
  283. }
  284. Rectangle rect_text = item.rect;
  285. rect_text.X += SM_CXMENUCHECK;
  286. if (item.item.BarBreak) { /* Draw vertical break bar*/
  287. Rectangle rect = item.rect;
  288. rect.Y++;
  289. rect.Width = 3;
  290. rect.Height = menu_height - 6;
  291. //TODO: ControlPaint.DrawBorder3D (dc, item.rect,
  292. // Border3DStyle.Etched, BF_TOP);
  293. dc.FillRectangle (new SolidBrush (Color.Black),
  294. rect);
  295. }
  296. if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  297. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  298. (ThemeEngine.Current.ColorHilight), item.rect);
  299. }
  300. if (item.item.Enabled) {
  301. Color color_text;
  302. if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE)
  303. color_text = ThemeEngine.Current.ColorHilightText;
  304. else
  305. color_text = ThemeEngine.Current.ColorMenuText;
  306. dc.DrawString (item.item.Text, MENU_FONT,
  307. ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
  308. rect_text, string_format_text);
  309. if (item.item.Shortcut != Shortcut.None) {
  310. string str = item.item.GetShortCutText ();
  311. Rectangle rect = rect_text;
  312. rect.X = item.xTab;
  313. rect.Width -= item.xTab;
  314. dc.DrawString (str, MENU_FONT, ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
  315. rect, string_format_shortcut);
  316. }
  317. }
  318. else {
  319. ControlPaint.DrawStringDisabled (dc,
  320. item.item.Text, MENU_FONT, Color.Black, rect_text,
  321. string_format_text);
  322. }
  323. /* Draw arrow */
  324. if (item.item.IsPopup) {
  325. Bitmap bmp = new Bitmap (SM_CXARROWCHECK, SM_CYARROWCHECK);
  326. Graphics gr = Graphics.FromImage (bmp);
  327. Rectangle rect_arrow = new Rectangle (0, 0, SM_CXARROWCHECK, SM_CYARROWCHECK);
  328. ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Arrow);
  329. bmp.MakeTransparent ();
  330. dc.DrawImage (bmp, item.rect.X + item.rect.Width - SM_CXARROWCHECK,
  331. item.rect.Y + ((item.rect.Height - SM_CYARROWCHECK) /2));
  332. gr.Dispose ();
  333. bmp.Dispose ();
  334. }
  335. /* Draw checked or radio */
  336. if (item.item.Checked) {
  337. Rectangle area = item.rect;
  338. Bitmap bmp = new Bitmap (SM_CXMENUCHECK, SM_CYMENUCHECK);
  339. Graphics gr = Graphics.FromImage (bmp);
  340. Rectangle rect_arrow = new Rectangle (0, 0, SM_CXMENUCHECK, SM_CYMENUCHECK);
  341. if (item.item.RadioCheck)
  342. ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Bullet);
  343. else
  344. ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Checkmark);
  345. bmp.MakeTransparent ();
  346. dc.DrawImage (bmp, area.X + 4,
  347. item.rect.Y + ((item.rect.Height - SM_CYMENUCHECK) /2));
  348. gr.Dispose ();
  349. bmp.Dispose ();
  350. }
  351. }
  352. static public void DrawPopupMenu (Graphics dc, IntPtr hMenu)
  353. {
  354. MENU menu = GetMenuFromID (hMenu);
  355. for (int i = 0; i < menu.items.Count; i++) {
  356. DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height);
  357. }
  358. }
  359. static public MENUITEM FindItemByCoords (IntPtr hMenu, Point pt, ref int pos)
  360. {
  361. MENU menu = GetMenuFromID (hMenu);
  362. for (int i = 0; i < menu.items.Count; i++) {
  363. MENUITEM item = (MENUITEM) menu.items[i];
  364. if (item.rect.Contains (pt)) {
  365. //Console.WriteLine ("FindItemByCoords: " + item.item.Text);
  366. pos = i;
  367. return item;
  368. }
  369. }
  370. //Console.WriteLine ("FindItemByCoords none ");
  371. pos = -1;
  372. return null;
  373. }
  374. static public void SelectItem (TRACKER tracker, IntPtr hMenu, MENUITEM item, int pos)
  375. {
  376. MENU menu = GetMenuFromID (hMenu);
  377. //Console.WriteLine ("Current: {0} select {1}", menu_parent.hCurrent, hMenu);
  378. //if (menu_parent != null)
  379. /* Already selected */
  380. for (int i = 0; i < menu.items.Count; i++) {
  381. MENUITEM it = (MENUITEM) menu.items[i];
  382. if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  383. if (item.rect == it.rect)
  384. return;
  385. }
  386. }
  387. Console.WriteLine ("SelectItem:Current is {0} {1}", tracker.hCurrentMenu, hMenu);
  388. if (tracker.hCurrentMenu != hMenu) {
  389. Console.WriteLine ("Changing current menu!");
  390. HideSubPopups (hMenu);
  391. tracker.hCurrentMenu = hMenu;
  392. }
  393. /* Unselect previous item*/
  394. for (int i = 0; i < menu.items.Count; i++) {
  395. MENUITEM it = (MENUITEM) menu.items[i];
  396. if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
  397. it.fState = item.fState & ~MF.MF_HILITE;
  398. menu.items[i] = it;
  399. }
  400. }
  401. item.fState |= MF.MF_HILITE;
  402. menu.items[pos] = item;
  403. //Console.WriteLine ("SelectItem {0} {1} {2} {3}", item.item.Text, item.fState,
  404. // ((MENUITEM)(menu.items[pos])).fState, pos);
  405. ExecFocusedItem (tracker, hMenu, item);
  406. }
  407. /*
  408. Used when the user executes the action of an item (press enter, shortcut)
  409. or a sub-popup menu has to be shown
  410. */
  411. static public void ExecFocusedItem (TRACKER tracker, IntPtr hMenu, MENUITEM item)
  412. {
  413. if (item.item.IsPopup) {
  414. ShowSubPopup (tracker, hMenu, item.hSubMenu, item);
  415. }
  416. else {
  417. // Execute function
  418. }
  419. }
  420. static public void ShowSubPopup (TRACKER tracker, IntPtr hParent, IntPtr hMenu, MENUITEM item)
  421. {
  422. MENU menu = GetMenuFromID (hMenu);
  423. if (menu.Wnd != null) /* Already showing */
  424. return;
  425. MENU menu_parent = GetMenuFromID (hParent);
  426. PopUpWindow popup = new PopUpWindow (hMenu, tracker);
  427. menu_parent.Wnd.LostFocus ();
  428. menu.Wnd = popup;
  429. tracker.hCurrentMenu = hMenu;
  430. Console.WriteLine ("ShowSubPopup:Setting current to {0}", tracker.hCurrentMenu);
  431. Point pnt = new Point ();
  432. pnt.X = item.rect.X + item.rect.Width;
  433. pnt.Y = item.rect.Y + 1;
  434. Console.WriteLine ("ShowSubPopup prev:" + pnt);
  435. pnt = menu_parent.Wnd.PointToScreen (pnt);
  436. popup.Location = pnt;
  437. popup.ShowWindow ();
  438. popup.Refresh ();
  439. Console.WriteLine ("ShowSubPopup location:" + popup.Location);
  440. }
  441. /* Hides all the submenus open in a menu */
  442. static public void HideSubPopups (IntPtr hMenu)
  443. {
  444. Console.WriteLine ("HideSubPopups: " + hMenu);
  445. MENU menu = GetMenuFromID (hMenu);
  446. MENUITEM item;
  447. for (int i = 0; i < menu.items.Count; i++) {
  448. item = (MENUITEM) menu.items[i];
  449. if (item.item.IsPopup) {
  450. MENU sub_menu = GetMenuFromID (item.hSubMenu);
  451. if (sub_menu.Wnd != null) {
  452. Console.WriteLine ("Hiding!");
  453. HideSubPopups (item.hSubMenu);
  454. sub_menu.Wnd.UnShow ();
  455. sub_menu.Wnd = null;
  456. }
  457. }
  458. }
  459. }
  460. static public void DestroyMenu (IntPtr hMenu)
  461. {
  462. MENU menu = GetMenuFromID (hMenu);
  463. MENUITEM item;
  464. for (int i = 0; i < menu.items.Count; i++) {
  465. item = (MENUITEM) menu.items[i];
  466. if (item.item.IsPopup) {
  467. MENU sub_menu = GetMenuFromID (item.hSubMenu);
  468. if (sub_menu.Wnd != null) {
  469. // TODO: Remove from list
  470. HideSubPopups (item.hSubMenu);
  471. DestroyMenu (item.hSubMenu);
  472. }
  473. }
  474. }
  475. // TODO: Remove from list
  476. menu.Wnd.UnShow ();
  477. menu.Wnd = null;
  478. }
  479. }
  480. /*
  481. class PopUpWindow
  482. */
  483. internal class PopUpWindow : Control
  484. {
  485. private IntPtr hMenu;
  486. private MenuAPI.TRACKER tracker;
  487. public PopUpWindow (IntPtr hMenu, MenuAPI.TRACKER tracker): base ()
  488. {
  489. this.hMenu = hMenu;
  490. this.tracker = tracker;
  491. MouseDown += new MouseEventHandler (OnMouseDownPUW);
  492. MouseMove += new MouseEventHandler (OnMouseMovePUW);
  493. MouseUp += new MouseEventHandler (OnMouseUpPUW);
  494. Paint += new PaintEventHandler (OnPaintPUW);
  495. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  496. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  497. }
  498. protected override CreateParams CreateParams
  499. {
  500. get {
  501. CreateParams cp = base.CreateParams;
  502. cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE));
  503. return cp;
  504. }
  505. }
  506. public void ShowWindow ()
  507. {
  508. Capture = true;
  509. Show ();
  510. }
  511. public void UnShow ()
  512. {
  513. Capture = false;
  514. Hide ();
  515. }
  516. public void LostFocus ()
  517. {
  518. Capture = false;
  519. }
  520. protected override void OnResize(EventArgs e)
  521. {
  522. Console.WriteLine ("OnResize {0} {1} ", Width, Height);
  523. }
  524. private void OnPaintPUW (Object o, PaintEventArgs pevent)
  525. {
  526. //Console.WriteLine ("OnPaintPUW");
  527. if (Width <= 0 || Height <= 0 || Visible == false)
  528. return;
  529. Draw ();
  530. pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
  531. }
  532. private void OnMouseDownPUW (object sender, MouseEventArgs e)
  533. {
  534. Console.WriteLine ("OnMouseDownPUW");
  535. /* Click outside the client area*/
  536. if (ClientRectangle.Contains (e.X, e.Y) == false) {
  537. Console.WriteLine ("Hide");
  538. Capture = false;
  539. Hide ();
  540. }
  541. }
  542. private void OnMouseUpPUW (object sender, MouseEventArgs e)
  543. {
  544. Console.WriteLine ("OnMouseUpPUW");
  545. /* Click outside the client area*/
  546. int pos = 0;
  547. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y), ref pos);
  548. if (item != null && item.item.Enabled) {
  549. item.item.PerformClick ();
  550. MenuAPI.DestroyMenu (tracker.hTopMenu);
  551. Capture = false;
  552. Refresh ();
  553. }
  554. }
  555. private void OnMouseMovePUW (object sender, MouseEventArgs e)
  556. {
  557. //Console.WriteLine ("OnMouseMovePUW");
  558. int pos = 0;
  559. MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y), ref pos);
  560. if (item != null) {
  561. MenuAPI.SelectItem (tracker, hMenu, item, pos);
  562. Refresh ();
  563. }
  564. }
  565. protected override void CreateHandle ()
  566. {
  567. base.CreateHandle ();
  568. MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
  569. MenuAPI.CalcPopupMenuSize (DeviceContext, hMenu);
  570. Width = menu.Width;
  571. Height = menu.Height;
  572. Console.WriteLine ("CreateHandle {0} {1}", Width, Height);
  573. }
  574. private void Draw ()
  575. {
  576. Rectangle rect = ClientRectangle;
  577. rect.Width = rect.Width - 1;
  578. rect.Height = rect.Height - 1;
  579. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  580. (ThemeEngine.Current.ColorMenu), ClientRectangle);
  581. DeviceContext.DrawRectangle (ThemeEngine.Current.ResPool.GetPen
  582. (ThemeEngine.Current.ColorHilightText), rect);
  583. MenuAPI.DrawPopupMenu (DeviceContext, hMenu);
  584. }
  585. }
  586. }