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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
29 using System.Drawing.Text;
30 using System.Collections;
32 namespace System.Windows.Forms
36 This class mimics the Win32 API Menu functionality
38 internal class MenuAPI
40 static StringFormat string_format_text = new StringFormat ();
41 static StringFormat string_format_shortcut = new StringFormat ();
42 static StringFormat string_format_menubar_text = new StringFormat ();
43 static ArrayList menu_list = new ArrayList ();
44 static Font MENU_FONT = new Font (FontFamily.GenericSansSerif, 8.25f);
45 static int POPUP_ARROW_WITDH;
46 static int POPUP_ARROW_HEIGHT;
47 const int SEPARATOR_HEIGHT = 5;
48 const int SM_CXBORDER = 1;
49 const int SM_CYBORDER = 1;
50 const int SM_CXMENUCHECK = 14; // Width of the menu check
51 const int SM_CYMENUCHECK = 14; // Height of the menu check
52 const int SM_CXARROWCHECK = 16; // Width of the arrow
53 const int SM_CYARROWCHECK = 16; // Height of the arrow
54 const int SM_CYMENU = 18; // Minimum height of a menu
55 const int MENU_TAB_SPACE = 8; // Pixels added to the width of an item because of a tab
56 const int MENU_BAR_ITEMS_SPACE = 12; // Space between menu bar items
60 public MF Flags; // Menu flags (MF_POPUP, MF_SYSMENU)
61 public int Width; // Width of the whole menu
62 public int Height; // Height of the whole menu
63 public Control Wnd; // In a Popup menu is the PopupWindow and in a MenuBar the Form
64 public ArrayList items; // Array of menu items
65 public int FocusedItem; // Currently focused item
66 public IntPtr hParent;
67 public TRACKER tracker;
68 public MENUITEM SelectedItem; // Currently focused item
74 hParent = IntPtr.Zero;
75 items = new ArrayList ();
77 Width = Height = FocusedItem = 0;
78 tracker = new TRACKER ();
87 public Rectangle rect;
92 public IntPtr hSubMenu;
94 public int pos; /* Position in the menuitems array*/
102 rect = new Rectangle ();
109 public IntPtr hCurrentMenu;
110 public IntPtr hTopMenu;
114 hCurrentMenu = hTopMenu = IntPtr.Zero;
118 static void DumpMenuItems (ArrayList list)
120 Console.WriteLine ("Menu items dump start--- ");
122 for (int i = 0; i < list.Count; i++)
123 Console.WriteLine ("idx:{0} {1} {2}", i, ((MENUITEM)list[i]).item, ((MENUITEM)list[i]).item.Separator);
125 Console.WriteLine ("Menu items dump end --- ");
128 public enum MenuMouseEvent
134 internal enum ItemNavigation
149 MF_BYPOSITION = 0x400,
150 MF_SEPARATOR = 0x800,
156 MF_USECHECKBITMAPS = 0x200,
159 MF_OWNERDRAW = 0x100,
161 MF_MENUBARBREAK = 0x20,
168 MF_RIGHTJUSTIFY = 0x4000,
169 MF_MENUBAR = 0x8000 // Internal
174 Console.WriteLine ("MenuAPI::MenuAPI");
175 string_format_text.LineAlignment = StringAlignment.Center;
176 string_format_text.Alignment = StringAlignment.Near;
177 string_format_text.HotkeyPrefix = HotkeyPrefix.Show;
179 string_format_shortcut.LineAlignment = StringAlignment.Center;
180 string_format_shortcut.Alignment = StringAlignment.Far;
182 string_format_menubar_text.LineAlignment = StringAlignment.Center;
183 string_format_menubar_text.Alignment = StringAlignment.Center;
184 string_format_menubar_text.HotkeyPrefix = HotkeyPrefix.Show;
188 static public IntPtr StoreMenuID (MENU menu)
190 int id = menu_list.Add (menu);
191 //Console.WriteLine ("StoreMenuID:" + id + 1);
192 return (IntPtr)(id + 1);
195 static public MENU GetMenuFromID (IntPtr ptr)
200 return (MENU) menu_list[id];
203 static public IntPtr CreateMenu ()
205 MENU menu = new MENU ();
206 return StoreMenuID (menu);
209 static public IntPtr CreatePopupMenu ()
211 Console.WriteLine ("MenuAPI.CreatePopupMenu");
213 MENU popMenu = new MENU ();
214 popMenu.Flags |= MF.MF_POPUP;
215 return StoreMenuID (popMenu);
218 static public int InsertMenuItem (IntPtr hMenu, int uItem, bool fByPosition, MenuItem item, ref IntPtr hSubMenu)
222 if (fByPosition == false)
223 throw new NotImplementedException ();
227 MENU menu = GetMenuFromID (hMenu);
228 if ((uint)uItem > menu.items.Count)
229 uItem = menu.items.Count;
231 MENUITEM menu_item = new MENUITEM ();
232 menu_item.item = item;
235 menu_item.hSubMenu = CreatePopupMenu ();
236 MENU submenu = GetMenuFromID (menu_item.hSubMenu);
237 submenu.hParent = hMenu;
240 menu_item.hSubMenu = IntPtr.Zero;
242 //menu_item.Flags |= MF.MF_POPUP;
244 hSubMenu = menu_item.hSubMenu;
246 id = menu.items.Count;
247 menu_item.pos = menu.items.Count;
248 menu.items.Insert (uItem, menu_item);
250 //Console.WriteLine ("InsertMenuItem {0} {1} {2}" + menu.items.Count,
255 // X and Y are screen coordinates
256 static public bool TrackPopupMenu (IntPtr hTopMenu, IntPtr hMenu, Point pnt, bool bMenubar, Control Wnd)
258 MENU menu = GetMenuFromID (hMenu);
259 PopUpWindow popup = new PopUpWindow (hMenu);
261 menu.tracker.hCurrentMenu = hMenu;
262 menu.tracker.hTopMenu = hTopMenu;
263 //menu.bMenubar = bMenubar;
265 MENUITEM select_item = GetNextItem (hMenu, ItemNavigation.First);
267 if (select_item != null)
268 MenuAPI.SelectItem (hMenu, select_item, false);
270 popup.Location = popup.PointToClient (pnt);
276 //popup.DestroyHandle ();
285 static public void CalcItemSize (Graphics dc, MENUITEM item, int y, int x, bool menuBar)
290 if (item.item.Visible == false)
293 if (item.item.Separator == true) {
294 item.rect.Height = SEPARATOR_HEIGHT / 2;
295 item.rect.Width = -1;
300 size = dc.MeasureString (item.item.Text, MENU_FONT);
301 item.rect.Width = (int) size.Width;
302 item.rect.Height = (int) size.Height;
306 if (item.item.Shortcut != Shortcut.None && item.item.ShowShortcut) {
307 item.xTab = SM_CXMENUCHECK + MENU_TAB_SPACE + (int) size.Width;
308 size = dc.MeasureString (" " + item.item.GetShortCutText (), MENU_FONT);
309 item.rect.Width += MENU_TAB_SPACE + (int) size.Width;
312 item.rect.Width += 4 + (SM_CXMENUCHECK * 2);
315 //item.rect.Width += MENU_BAR_ITEMS_SPACE;
316 x += item.rect.Width;
319 if (item.rect.Height < SM_CYMENU - 1)
320 item.rect.Height = SM_CYMENU - 1;
322 //Console.WriteLine ("CalcItemSize " + item.rect);
326 static public void CalcPopupMenuSize (Graphics dc, IntPtr hMenu)
332 MENU menu = GetMenuFromID (hMenu);
335 while (start < menu.items.Count) {
338 for (i = start; i < menu.items.Count; i++) {
339 MENUITEM item = (MENUITEM) menu.items[i];
341 if ((i != start) && (item.item.Break || item.item.BarBreak))
344 CalcItemSize (dc, item, y, x, false);
345 y += item.rect.Height;
347 if (item.rect.Width > max)
348 max = item.rect.Width;
351 // Reemplace the -1 by the menu width (separators)
352 for (n = start; n < i; n++, start++) {
353 MENUITEM item = (MENUITEM) menu.items[n];
354 item.rect.Width = max;
369 menu.Width += SM_CXBORDER;
370 menu.Height += SM_CYBORDER;
372 //Console.WriteLine ("CalcPopupMenuSize {0} {1}", menu.Width, menu.Height);
375 static public void DrawMenuItem (Graphics dc, MENUITEM item, int menu_height, bool menuBar)
377 StringFormat string_format;
379 if (item.item.Visible == false)
383 string_format = string_format_menubar_text;
385 string_format = string_format_text;
387 if (item.item.Separator == true) {
389 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
390 item.rect.X, item.rect.Y, item.rect.X + item.rect.Width, item.rect.Y);
392 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
393 item.rect.X, item.rect.Y + 1, item.rect.X + item.rect.Width, item.rect.Y + 1);
398 Rectangle rect_text = item.rect;
401 rect_text.X += SM_CXMENUCHECK;
403 if (item.item.BarBreak) { /* Draw vertical break bar*/
405 Rectangle rect = item.rect;
408 rect.Height = menu_height - 6;
410 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
411 rect.X, rect.Y , rect.X, rect.Y + rect.Height);
413 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
414 rect.X + 1, rect.Y , rect.X +1, rect.Y + rect.Height);
418 //Console.WriteLine ("!{0}, {1}, {2}", item.item.Text, item.rect, rect_text);
420 if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE) {
421 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
422 (ThemeEngine.Current.ColorHilight), item.rect);
425 if (item.item.Enabled) {
429 if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE)
430 color_text = ThemeEngine.Current.ColorHilightText;
432 color_text = ThemeEngine.Current.ColorMenuText;
435 dc.DrawString (item.item.Text, MENU_FONT,
436 ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
437 rect_text, string_format);
439 if (!menuBar && item.item.Shortcut != Shortcut.None && item.item.ShowShortcut) {
441 string str = item.item.GetShortCutText ();
442 Rectangle rect = rect_text;
444 rect.Width -= item.xTab;
446 dc.DrawString (str, MENU_FONT, ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
447 rect, string_format_shortcut);
452 ControlPaint.DrawStringDisabled (dc,
453 item.item.Text, MENU_FONT, Color.Black, rect_text,
458 if (menuBar == false && item.item.IsPopup) {
460 Bitmap bmp = new Bitmap (SM_CXARROWCHECK, SM_CYARROWCHECK);
461 Graphics gr = Graphics.FromImage (bmp);
462 Rectangle rect_arrow = new Rectangle (0, 0, SM_CXARROWCHECK, SM_CYARROWCHECK);
463 ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Arrow);
464 bmp.MakeTransparent ();
465 dc.DrawImage (bmp, item.rect.X + item.rect.Width - SM_CXARROWCHECK,
466 item.rect.Y + ((item.rect.Height - SM_CYARROWCHECK) /2));
472 /* Draw checked or radio */
473 if (menuBar == false && item.item.Checked) {
475 Rectangle area = item.rect;
476 Bitmap bmp = new Bitmap (SM_CXMENUCHECK, SM_CYMENUCHECK);
477 Graphics gr = Graphics.FromImage (bmp);
478 Rectangle rect_arrow = new Rectangle (0, 0, SM_CXMENUCHECK, SM_CYMENUCHECK);
480 if (item.item.RadioCheck)
481 ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Bullet);
483 ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Checkmark);
485 bmp.MakeTransparent ();
486 dc.DrawImage (bmp, area.X, item.rect.Y + (item.rect.Height /2));
494 static public void DrawPopupMenu (Graphics dc, IntPtr hMenu, Rectangle cliparea, Rectangle rect)
496 MENU menu = GetMenuFromID (hMenu);
498 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
499 (ThemeEngine.Current.ColorMenu), cliparea);
501 /* Draw menu borders */
502 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorHilightText),
503 rect.X, rect.Y, rect.X + rect.Width, rect.Y);
505 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorHilightText),
506 rect.X, rect.Y, rect.X, rect.Y + rect.Height);
508 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
509 rect.X + rect.Width - 1 , rect.Y , rect.X + rect.Width - 1, rect.Y + rect.Height);
511 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonDkShadow),
512 rect.X + rect.Width, rect.Y , rect.X + rect.Width, rect.Y + rect.Height);
514 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
515 rect.X , rect.Y + rect.Height - 1 , rect.X + rect.Width - 1, rect.Y + rect.Height -1);
517 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonDkShadow),
518 rect.X , rect.Y + rect.Height, rect.X + rect.Width - 1, rect.Y + rect.Height);
520 for (int i = 0; i < menu.items.Count; i++)
521 if (cliparea.IntersectsWith (((MENUITEM) menu.items[i]).rect)) {
522 DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, menu.bMenubar);
527 // Updates the menu rect and returns the height
528 static public int MenuBarCalcSize (Graphics dc, IntPtr hMenu, int width)
532 MENU menu = GetMenuFromID (hMenu);
535 while (i < menu.items.Count) {
537 item = (MENUITEM) menu.items[i];
538 CalcItemSize (dc, item, 0, x, true);
540 x += item.rect.Width;
541 item.fState |= MF.MF_MENUBAR;
543 if (item.rect.Height > menu.Height)
544 menu.Height = item.rect.Height;
551 // Draws a menu bar in a Window
552 static public void DrawMenuBar (Graphics dc, IntPtr hMenu, Rectangle rect)
554 MENU menu = GetMenuFromID (hMenu);
555 Rectangle rect_menu = new Rectangle ();
557 if (menu.Height == 0)
558 MenuBarCalcSize (dc, hMenu, rect_menu.Width);
560 rect.Height = menu.Height;
561 rect.Width = menu.Width;
563 Console.WriteLine ("DrawMenuBar {0}", rect);
565 for (int i = 0; i < menu.items.Count; i++)
566 DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, true);
572 static public MENUITEM FindItemByCoords (IntPtr hMenu, Point pt)
574 MENU menu = GetMenuFromID (hMenu);
576 for (int i = 0; i < menu.items.Count; i++) {
577 MENUITEM item = (MENUITEM) menu.items[i];
578 if (item.rect.Contains (pt)) {
579 //Console.WriteLine ("FindItemByCoords: " + item.item.Text);
584 //Console.WriteLine ("FindItemByCoords none ");
588 static public void SelectItem (IntPtr hMenu, MENUITEM item, bool execute)
590 MENU menu = GetMenuFromID (hMenu);
593 //Console.WriteLine ("Current: {0} select {1}", menu_parent.hCurrent, hMenu);
594 MENUITEM highlight_item = null;
596 /* Already selected */
597 for (int i = 0; i < menu.items.Count; i++) {
598 MENUITEM it = (MENUITEM) menu.items[i];
600 if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
601 if (item.rect == it.rect)
604 highlight_item = item;
608 //Console.WriteLine ("SelectItem:Current is {0} {1}", tracker.hCurrentMenu, hMenu);
610 if (menu.tracker.hCurrentMenu != hMenu) {
611 Console.WriteLine ("Changing current menu!");
612 HideSubPopups (hMenu);
613 menu.tracker.hCurrentMenu = hMenu;
616 /* Unselect previous item*/
617 for (int i = 0; i < menu.items.Count; i++) {
618 MENUITEM it = (MENUITEM) menu.items[i];
620 if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
621 it.fState = item.fState & ~MF.MF_HILITE;
622 menu.Wnd.Invalidate (it.rect);
625 Console.WriteLine ("**Select item invalidate {0}", it.rect);
629 //if (menu.items[i].Equals(item))
633 menu.SelectedItem = item;
634 item.fState |= MF.MF_HILITE;
635 //menu.items[pos] = item;
638 Console.WriteLine ("**Select item invalidate {0}", item.rect);
640 menu.Wnd.Invalidate (item.rect);
643 ExecFocusedItem (hMenu, item);
647 Used when the user executes the action of an item (press enter, shortcut)
648 or a sub-popup menu has to be shown
650 static public void ExecFocusedItem (IntPtr hMenu, MENUITEM item)
652 if (item.item.IsPopup) {
653 ShowSubPopup (hMenu, item.hSubMenu, item);
660 static public void ShowSubPopup (IntPtr hParent, IntPtr hMenu, MENUITEM item)
662 MENU menu = GetMenuFromID (hMenu);
664 if (menu.Wnd != null) /* Already showing */
667 if (item.item.Enabled == false)
670 MENU menu_parent = GetMenuFromID (hParent);
671 PopUpWindow popup = new PopUpWindow (hMenu);
672 ((PopUpWindow)menu_parent.Wnd).LostFocus ();
674 menu.tracker.hCurrentMenu = hMenu;
676 //Console.WriteLine ("ShowSubPopup:Setting current to {0}", menu.tracker.hCurrentMenu);
678 Point pnt = new Point ();
679 pnt.X = item.rect.X + item.rect.Width;
680 pnt.Y = item.rect.Y + 1;
681 //Console.WriteLine ("ShowSubPopup prev:" + pnt);
682 pnt = menu_parent.Wnd.PointToScreen (pnt);
683 popup.Location = pnt;
685 MENUITEM select_item = GetNextItem (hMenu, ItemNavigation.First);
687 if (select_item != null)
688 MenuAPI.SelectItem (hMenu, select_item, false);
693 //Console.WriteLine ("ShowSubPopup location:" + popup.Location);
696 /* Hides all the submenus open in a menu */
697 static public void HideSubPopups (IntPtr hMenu)
699 //Console.WriteLine ("HideSubPopups: " + hMenu);
701 MENU menu = GetMenuFromID (hMenu);
704 for (int i = 0; i < menu.items.Count; i++) {
705 item = (MENUITEM) menu.items[i];
706 if (item.item.IsPopup) {
708 MENU sub_menu = GetMenuFromID (item.hSubMenu);
710 if (sub_menu.Wnd != null) {
711 //Console.WriteLine ("Hiding!");
712 HideSubPopups (item.hSubMenu);
713 ((PopUpWindow)sub_menu.Wnd).Destroy ();
720 static public void DestroyMenu (IntPtr hMenu)
722 if (hMenu == IntPtr.Zero)
725 MENU menu = GetMenuFromID (hMenu);
728 for (int i = 0; i < menu.items.Count; i++) {
729 item = (MENUITEM) menu.items[i];
730 //Console.WriteLine ("Destroy item: "+ item.item.Text + " pop:" + item.item.IsPopup);
731 if (item.item.IsPopup) {
732 MENU sub_menu = GetMenuFromID (item.hSubMenu);
733 if (sub_menu != null && sub_menu.Wnd != null) {
734 // TODO: Remove from list
735 HideSubPopups (item.hSubMenu);
736 DestroyMenu (item.hSubMenu);
741 // TODO: Remove from list
743 // Do not destroy the window of a Menubar
744 if (menu.Wnd != null && menu.bMenubar == false) {
745 ((PopUpWindow)menu.Wnd).Destroy ();
750 static public void SetMenuBarWindow (IntPtr hMenu, Control wnd)
752 Console.WriteLine ("SetMenuBarWindow {0}", hMenu);
753 MENU menu = GetMenuFromID (hMenu);
755 menu.bMenubar = true;
758 static private void MenuBarMove (IntPtr hMenu, MENUITEM item)
761 MENU menu = GetMenuFromID (hMenu);
762 Point pnt = new Point (item.rect.X, item.rect.Y + item.rect.Height);
763 pnt = menu.Wnd.PointToScreen (pnt);
765 Console.WriteLine ("MenuBarMove {0} {1}", item.item.Text, menu.tracker.hCurrentMenu);
767 MenuAPI.SelectItem (hMenu, item, false);
768 MenuAPI.DestroyMenu (menu.tracker.hCurrentMenu);
769 menu.tracker.hCurrentMenu = hMenu;
770 MenuAPI.TrackPopupMenu (hMenu, item.hSubMenu, pnt, false, null);
773 // Function that process all menubar mouse events
774 static public void TrackBarMouseEvent (IntPtr hMenu, Control wnd, MouseEventArgs e, MenuMouseEvent eventype)
776 MENU menu = GetMenuFromID (hMenu);
779 case MenuMouseEvent.Down: {
781 MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
783 //Console.WriteLine ("menubar: {0} {1}",item.rect.X,
784 // item.rect.Y + item.rect.Height + 1);
786 if (item != null && menu.SelectedItem != item)
787 MenuBarMove (hMenu, item);
792 case MenuMouseEvent.Move: {
794 if (menu.tracker.hCurrentMenu != IntPtr.Zero) {
796 MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
798 if (item != null && menu.SelectedItem != item)
799 MenuBarMove (hMenu, item);
809 static public MENUITEM FindItemByKey (IntPtr hMenu, IntPtr key)
811 MENU menu = GetMenuFromID (hMenu);
814 char key_char = (char ) (key.ToInt32() & 0xff);
815 key_char = Char.ToUpper (key_char);
817 for (int i = 0; i < menu.items.Count; i++) {
818 item = (MENUITEM) menu.items[i];
820 //Console.WriteLine ("text {0} mnenonic {1} {2}", item.item.Text, item.item.Mnemonic, key_char);
822 if (item.item.Mnemonic == '\0')
825 if (item.item.Mnemonic == key_char)
832 // Get the next or previous selectable item on a menu
833 static public MENUITEM GetNextItem (IntPtr hMenu, ItemNavigation navigation)
835 MENU menu = GetMenuFromID (hMenu);
837 bool selectable_items = false;
840 // Check if there is at least a selectable item
841 for (int i = 0; i < menu.items.Count; i++) {
842 item = (MENUITEM)menu.items[i];
843 if (item.item.Separator == false && item.item.Visible == true) {
844 selectable_items = true;
849 if (selectable_items == false)
852 switch (navigation) {
853 case ItemNavigation.First: {
856 /* Next item that is not separator and it is visible*/
857 for (; pos < menu.items.Count; pos++) {
858 item = (MENUITEM)menu.items[pos];
859 if (item.item.Separator == false && item.item.Visible == true)
863 if (pos >= menu.items.Count) { /* Jump at the start of the menu */
865 /* Next item that is not separator and it is visible*/
866 for (; pos < menu.items.Count; pos++) {
867 item = (MENUITEM)menu.items[pos];
868 if (item.item.Separator == false && item.item.Visible == true)
876 case ItemNavigation.Last: { // Not used
880 case ItemNavigation.Next: {
882 if (menu.SelectedItem != null)
883 pos = menu.SelectedItem.pos;
885 /* Next item that is not separator and it is visible*/
886 for (pos++; pos < menu.items.Count; pos++) {
887 item = (MENUITEM)menu.items[pos];
888 if (item.item.Separator == false && item.item.Visible == true)
892 if (pos >= menu.items.Count) { /* Jump at the start of the menu */
894 /* Next item that is not separator and it is visible*/
895 for (; pos < menu.items.Count; pos++) {
896 item = (MENUITEM)menu.items[pos];
897 if (item.item.Separator == false && item.item.Visible == true)
904 case ItemNavigation.Previous: {
906 if (menu.SelectedItem != null)
907 pos = menu.SelectedItem.pos;
909 /* Previous item that is not separator and it is visible*/
910 for (pos--; pos >= 0; pos--) {
911 item = (MENUITEM)menu.items[pos];
912 if (item.item.Separator == false && item.item.Visible == true)
916 if (pos < 0 ) { /* Jump at the end of the menu*/
917 pos = menu.items.Count - 1;
918 /* Previous item that is not separator and it is visible*/
919 for (; pos >= 0; pos--) {
920 item = (MENUITEM)menu.items[pos];
921 if (item.item.Separator == false && item.item.Visible == true)
933 return (MENUITEM)menu.items[pos];
936 static public bool ProcessKeys (IntPtr hMenu, ref Message msg, Keys keyData)
938 MENU menu = GetMenuFromID (hMenu);
939 Console.WriteLine ("Menu.ProcessCmdKey {0} {1} {2}",keyData, msg.LParam, msg.WParam);
944 item = GetNextItem (hMenu, ItemNavigation.Previous);
946 MenuAPI.SelectItem (hMenu, item, false);
952 item = GetNextItem (hMenu, ItemNavigation.Next);
955 MenuAPI.SelectItem (hMenu, item, false);
959 /* Menubar selects and opens next. Popups next or open*/
961 //Console.WriteLine ("Key.Right next menubar item info {0}", menu.hParent);
963 // Try to Expand popup first
964 if (menu.SelectedItem.item.IsPopup) {
965 ShowSubPopup (hMenu, menu.SelectedItem.hSubMenu, menu.SelectedItem);
966 Console.WriteLine ("Key.Right next menubar item Showing popup");
970 if (menu.hParent != IntPtr.Zero)
971 parent = GetMenuFromID (menu.hParent);
973 if (parent != null && parent.bMenubar == true) {
974 MENUITEM select_item = GetNextItem (menu.hParent, ItemNavigation.Next);
975 MenuBarMove (menu.hParent, select_item);
983 //Console.WriteLine ("Key.Right next menubar item info {0}", menu.hParent);
985 // Try to Collapse popup first
986 if (menu.SelectedItem.item.IsPopup) {
991 if (menu.hParent != IntPtr.Zero)
992 parent = GetMenuFromID (menu.hParent);
994 if (parent != null && parent.bMenubar == true) {
995 MENUITEM select_item = GetNextItem (menu.hParent, ItemNavigation.Previous);
996 MenuBarMove (menu.hParent, select_item);
1007 /* Try if it is a menu hot key */
1008 item = MenuAPI.FindItemByKey (hMenu, msg.WParam);
1010 //Console.WriteLine ("menu item is null: " + (item == null));
1013 Console.WriteLine ("HotKey found: item.text" + item.item.Text);
1014 MenuAPI.SelectItem (hMenu, item, false);
1027 internal class PopUpWindow : Control
1029 private IntPtr hMenu;
1031 public PopUpWindow (IntPtr hMenu): base ()
1034 MouseDown += new MouseEventHandler (OnMouseDownPUW);
1035 MouseMove += new MouseEventHandler (OnMouseMovePUW);
1036 MouseUp += new MouseEventHandler (OnMouseUpPUW);
1037 Paint += new PaintEventHandler (OnPaintPUW);
1038 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
1039 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
1042 protected override CreateParams CreateParams
1045 CreateParams cp = base.CreateParams;
1046 cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE));
1047 cp.ExStyle |= (int)WindowStyles.WS_EX_TOOLWINDOW;
1052 public void ShowWindow ()
1059 public void Destroy ()
1065 public void LostFocus ()
1070 protected override void OnResize(EventArgs e)
1073 //Console.WriteLine ("OnResize {0} {1} ", Width, Height);
1076 private void OnPaintPUW (Object o, PaintEventArgs pevent)
1078 if (Width <= 0 || Height <= 0 || Visible == false)
1081 Draw (pevent.ClipRectangle);
1082 pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
1085 private void OnMouseDownPUW (object sender, MouseEventArgs e)
1087 //Console.WriteLine ("OnMouseDownPUW");
1088 /* Click outside the client area*/
1089 if (ClientRectangle.Contains (e.X, e.Y) == false) {
1090 Console.WriteLine ("Hide");
1096 private void OnMouseUpPUW (object sender, MouseEventArgs e)
1098 //Console.WriteLine ("OnMouseUpPUW");
1099 /* Click outside the client area*/
1100 MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
1101 MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
1103 if (item != null && item.item.Enabled) {
1104 item.item.PerformClick ();
1105 MenuAPI.DestroyMenu (menu.tracker.hTopMenu);
1113 private void OnMouseMovePUW (object sender, MouseEventArgs e)
1115 //Console.WriteLine ("OnMouseMovePUW");
1116 MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
1117 MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
1120 MenuAPI.SelectItem (hMenu, item, true);
1123 if (menu.bMenubar) {
1124 //Console.WriteLine ("MenuBar tracker move " + e.Y);
1125 //MenuAPI.TrackBarMouseEvent (tracker.hTopMenu,
1126 // this, e, MenuAPI.MenuMouseEvent.Move);
1128 Point pnt = PointToClient (MousePosition);
1130 MenuAPI.TrackBarMouseEvent (menu.tracker.hTopMenu,
1131 this, new MouseEventArgs(e.Button, e.Clicks, pnt.X, pnt.Y, e.Delta),
1132 MenuAPI.MenuMouseEvent.Move);
1137 protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
1139 Console.WriteLine ("PopUpWindow.ProcessCmdKey");
1140 return MenuAPI.ProcessKeys (hMenu, ref msg, keyData);
1143 protected override void CreateHandle ()
1145 base.CreateHandle ();
1147 MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
1148 MenuAPI.CalcPopupMenuSize (DeviceContext, hMenu);
1151 Height = menu.Height;
1152 Console.WriteLine ("CreateHandle {0} {1}", Width, Height);
1156 private void Draw (Rectangle clip)
1158 MenuAPI.DrawPopupMenu (DeviceContext, hMenu, clip, ClientRectangle);