| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157 |
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (c) 2004 Novell, Inc.
- //
- // Authors:
- // Jordi Mas i Hernandez, [email protected]
- //
- // NOT COMPLETE
- using System.Drawing;
- using System.Drawing.Text;
- using System.Collections;
- namespace System.Windows.Forms
- {
- /*
- This class mimics the Win32 API Menu functionality
- When writing this code the Wine project was of great help to
- understand the logic behind some Win32 issues. Thanks to them. Jordi,
- */
- internal class MenuAPI
- {
- static StringFormat string_format_text = new StringFormat ();
- static StringFormat string_format_shortcut = new StringFormat ();
- static StringFormat string_format_menubar_text = new StringFormat ();
- static ArrayList menu_list = new ArrayList ();
- static Font MENU_FONT = new Font (FontFamily.GenericSansSerif, 8.25f);
- static int POPUP_ARROW_WITDH;
- static int POPUP_ARROW_HEIGHT;
- const int SEPARATOR_HEIGHT = 5;
- const int SM_CXBORDER = 1;
- const int SM_CYBORDER = 1;
- const int SM_CXMENUCHECK = 14; // Width of the menu check
- const int SM_CYMENUCHECK = 14; // Height of the menu check
- const int SM_CXARROWCHECK = 16; // Width of the arrow
- const int SM_CYARROWCHECK = 16; // Height of the arrow
- const int SM_CYMENU = 18; // Minimum height of a menu
- const int MENU_TAB_SPACE = 8; // Pixels added to the width of an item because of a tab
- const int MENU_BAR_ITEMS_SPACE = 8; // Space between menu bar items
- public class MENU
- {
- public MF Flags; // Menu flags (MF_POPUP, MF_SYSMENU)
- public int Width; // Width of the whole menu
- public int Height; // Height of the whole menu
- public Control Wnd; // In a Popup menu is the PopupWindow and in a MenuBar the Form
- public ArrayList items; // Array of menu items
- public int FocusedItem; // Currently focused item
- public IntPtr hParent;
- public MENUITEM SelectedItem; // Currently focused item
- public bool bMenubar;
- public Menu menu; // SWF.Menu
- public MENU (Menu menu_obj)
- {
- Wnd = null;
- hParent = IntPtr.Zero;
- items = new ArrayList ();
- Flags = MF.MF_INSERT;
- Width = Height = FocusedItem = 0;
- bMenubar = false;
- menu = menu_obj;
- }
- }
- public class MENUITEM
- {
- public MenuItem item;
- public Rectangle rect;
- public MF fState;
- public int wID;
- public IntPtr hSubMenu;
- public int xTab;
- public int pos; /* Position in the menuitems array*/
- public MENUITEM ()
- {
- xTab = 0;
- wID = 0;
- pos = 0;
- rect = new Rectangle ();
- }
- };
- public class TRACKER
- {
- public IntPtr hCurrentMenu;
- public IntPtr hTopMenu;
- public TRACKER ()
- {
- hCurrentMenu = hTopMenu = IntPtr.Zero;
- }
- };
- public enum MenuMouseEvent
- {
- Down,
- Move,
- }
- internal enum ItemNavigation
- {
- First,
- Last,
- Next,
- Previous,
- }
- internal enum MF
- {
- MF_INSERT = 0x0,
- MF_APPEND = 0x100,
- MF_DELETE = 0x200,
- MF_REMOVE = 0x1000,
- MF_BYCOMMAND = 0,
- MF_BYPOSITION = 0x400,
- MF_SEPARATOR = 0x800,
- MF_ENABLED = 0,
- MF_GRAYED = 1,
- MF_DISABLED = 2,
- MF_UNCHECKED = 0,
- MF_CHECKED = 8,
- MF_USECHECKBITMAPS = 0x200,
- MF_STRING = 0,
- MF_BITMAP = 4,
- MF_OWNERDRAW = 0x100,
- MF_POPUP = 0x10,
- MF_MENUBARBREAK = 0x20,
- MF_MENUBREAK = 0x40,
- MF_UNHILITE = 0,
- MF_HILITE = 0x80,
- MF_DEFAULT = 0x1000,
- MF_SYSMENU = 0x2000,
- MF_HELP = 0x4000,
- MF_RIGHTJUSTIFY = 0x4000,
- MF_MENUBAR = 0x8000 // Internal
- }
- static MenuAPI ()
- {
- string_format_text.LineAlignment = StringAlignment.Center;
- string_format_text.Alignment = StringAlignment.Near;
- string_format_text.HotkeyPrefix = HotkeyPrefix.Show;
- string_format_shortcut.LineAlignment = StringAlignment.Center;
- string_format_shortcut.Alignment = StringAlignment.Far;
- string_format_menubar_text.LineAlignment = StringAlignment.Center;
- string_format_menubar_text.Alignment = StringAlignment.Center;
- string_format_menubar_text.HotkeyPrefix = HotkeyPrefix.Show;
- }
-
- static public IntPtr StoreMenuID (MENU menu)
- {
- int id = menu_list.Add (menu);
- return (IntPtr)(id + 1);
- }
- static public MENU GetMenuFromID (IntPtr ptr)
- {
- int id = (int)ptr;
- id = id - 1;
- if (menu_list[id] == null) // It has been delete it
- return null;
- return (MENU) menu_list[id];
- }
- static public IntPtr CreateMenu (Menu menu_obj)
- {
- MENU menu = new MENU (menu_obj);
- return StoreMenuID (menu);
- }
- static public IntPtr CreatePopupMenu (Menu menu_obj)
- {
- MENU popMenu = new MENU (menu_obj);
- popMenu.Flags |= MF.MF_POPUP;
- return StoreMenuID (popMenu);
- }
- static public int InsertMenuItem (IntPtr hMenu, int uItem, bool fByPosition, MenuItem item, ref IntPtr hSubMenu)
- {
- int id;
-
- if (fByPosition == false)
- throw new NotImplementedException ();
- MENU menu = GetMenuFromID (hMenu);
- if ((uint)uItem > menu.items.Count)
- uItem = menu.items.Count;
- MENUITEM menu_item = new MENUITEM ();
- menu_item.item = item;
- if (item.IsPopup) {
- menu_item.hSubMenu = CreatePopupMenu (menu_item.item);
- MENU submenu = GetMenuFromID (menu_item.hSubMenu);
- submenu.hParent = hMenu;
- }
- else
- menu_item.hSubMenu = IntPtr.Zero;
- hSubMenu = menu_item.hSubMenu;
- id = menu.items.Count;
- menu_item.pos = menu.items.Count;
- menu.items.Insert (uItem, menu_item);
- return id;
- }
- // The Point object contains screen coordinates
- static public bool TrackPopupMenu (IntPtr hTopMenu, IntPtr hMenu, Point pnt, bool bMenubar, Control Wnd)
- {
- TRACKER tracker = new TRACKER ();
- MENU menu = GetMenuFromID (hMenu);
- menu.Wnd = new PopUpWindow (hMenu, tracker);
- tracker.hCurrentMenu = hMenu;
- tracker.hTopMenu = hTopMenu;
- MENUITEM select_item = GetNextItem (hMenu, ItemNavigation.First);
- if (select_item != null)
- MenuAPI.SelectItem (hMenu, select_item, false, tracker);
- menu.Wnd.Location = menu.Wnd.PointToClient (pnt);
-
- if (menu.menu.IsDirty) {
- menu.items.Clear ();
- menu.menu.CreateItems ();
- ((PopUpWindow)menu.Wnd).RefreshItems ();
- menu.menu.IsDirty = false;
- }
-
- ((PopUpWindow)menu.Wnd).ShowWindow ();
- Application.Run ();
- if (menu.Wnd == null) {
- menu.Wnd.Dispose ();
- menu.Wnd = null;
- }
- return true;
- }
- /*
- Menu drawing API
- */
- static public void CalcItemSize (Graphics dc, MENUITEM item, int y, int x, bool menuBar)
- {
- item.rect.Y = y;
- item.rect.X = x;
- if (item.item.Visible == false)
- return;
- if (item.item.Separator == true) {
- item.rect.Height = SEPARATOR_HEIGHT / 2;
- item.rect.Width = -1;
- return;
- }
- SizeF size;
- size = dc.MeasureString (item.item.Text, MENU_FONT);
- item.rect.Width = (int) size.Width;
- item.rect.Height = (int) size.Height;
- if (!menuBar) {
- if (item.item.Shortcut != Shortcut.None && item.item.ShowShortcut) {
- item.xTab = SM_CXMENUCHECK + MENU_TAB_SPACE + (int) size.Width;
- size = dc.MeasureString (" " + item.item.GetShortCutText (), MENU_FONT);
- item.rect.Width += MENU_TAB_SPACE + (int) size.Width;
- }
- item.rect.Width += 4 + (SM_CXMENUCHECK * 2);
- }
- else {
- item.rect.Width += MENU_BAR_ITEMS_SPACE;
- x += item.rect.Width;
- }
- if (item.rect.Height < SM_CYMENU - 1)
- item.rect.Height = SM_CYMENU - 1;
- }
- static public void CalcPopupMenuSize (Graphics dc, IntPtr hMenu)
- {
- int x = 3;
- int start = 0;
- int i, n, y, max;
- MENU menu = GetMenuFromID (hMenu);
- menu.Height = 0;
- while (start < menu.items.Count) {
- y = 2;
- max = 0;
- for (i = start; i < menu.items.Count; i++) {
- MENUITEM item = (MENUITEM) menu.items[i];
- if ((i != start) && (item.item.Break || item.item.BarBreak))
- break;
- CalcItemSize (dc, item, y, x, false);
- y += item.rect.Height;
- if (item.rect.Width > max)
- max = item.rect.Width;
- }
- // Reemplace the -1 by the menu width (separators)
- for (n = start; n < i; n++, start++) {
- MENUITEM item = (MENUITEM) menu.items[n];
- item.rect.Width = max;
- }
- if (y > menu.Height)
- menu.Height = y;
- x+= max;
- }
- menu.Width = x;
- //space for border
- menu.Width += 2;
- menu.Height += 2;
- menu.Width += SM_CXBORDER;
- menu.Height += SM_CYBORDER;
- }
- static public void DrawMenuItem (Graphics dc, MENUITEM item, int menu_height, bool menuBar)
- {
- StringFormat string_format;
- if (item.item.Visible == false)
- return;
- if (menuBar)
- string_format = string_format_menubar_text;
- else
- string_format = string_format_text;
- if (item.item.Separator == true) {
- dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
- item.rect.X, item.rect.Y, item.rect.X + item.rect.Width, item.rect.Y);
- dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
- item.rect.X, item.rect.Y + 1, item.rect.X + item.rect.Width, item.rect.Y + 1);
- return;
- }
- Rectangle rect_text = item.rect;
- if (!menuBar)
- rect_text.X += SM_CXMENUCHECK;
- if (item.item.BarBreak) { /* Draw vertical break bar*/
- Rectangle rect = item.rect;
- rect.Y++;
- rect.Width = 3;
- rect.Height = menu_height - 6;
- dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
- rect.X, rect.Y , rect.X, rect.Y + rect.Height);
- dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
- rect.X + 1, rect.Y , rect.X +1, rect.Y + rect.Height);
- }
- if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE) {
- Rectangle rect = item.rect;
- rect.X++;
- rect.Width -=2;
- dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
- (ThemeEngine.Current.ColorHilight), rect);
- }
- if (item.item.Enabled) {
- Color color_text;
- if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE)
- color_text = ThemeEngine.Current.ColorHilightText;
- else
- color_text = ThemeEngine.Current.ColorMenuText;
- dc.DrawString (item.item.Text, MENU_FONT,
- ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
- rect_text, string_format);
- if (!menuBar && item.item.Shortcut != Shortcut.None && item.item.ShowShortcut) {
- string str = item.item.GetShortCutText ();
- Rectangle rect = rect_text;
- rect.X = item.xTab;
- rect.Width -= item.xTab;
- dc.DrawString (str, MENU_FONT, ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
- rect, string_format_shortcut);
- }
- }
- else {
- ControlPaint.DrawStringDisabled (dc,
- item.item.Text, MENU_FONT, Color.Black, rect_text,
- string_format);
- }
- /* Draw arrow */
- if (menuBar == false && item.item.IsPopup) {
- Bitmap bmp = new Bitmap (SM_CXARROWCHECK, SM_CYARROWCHECK);
- Graphics gr = Graphics.FromImage (bmp);
- Rectangle rect_arrow = new Rectangle (0, 0, SM_CXARROWCHECK, SM_CYARROWCHECK);
- ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Arrow);
- bmp.MakeTransparent ();
- dc.DrawImage (bmp, item.rect.X + item.rect.Width - SM_CXARROWCHECK,
- item.rect.Y + ((item.rect.Height - SM_CYARROWCHECK) /2));
- gr.Dispose ();
- bmp.Dispose ();
- }
- /* Draw checked or radio */
- if (menuBar == false && item.item.Checked) {
- Rectangle area = item.rect;
- Bitmap bmp = new Bitmap (SM_CXMENUCHECK, SM_CYMENUCHECK);
- Graphics gr = Graphics.FromImage (bmp);
- Rectangle rect_arrow = new Rectangle (0, 0, SM_CXMENUCHECK, SM_CYMENUCHECK);
- if (item.item.RadioCheck)
- ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Bullet);
- else
- ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Checkmark);
- bmp.MakeTransparent ();
- dc.DrawImage (bmp, area.X, item.rect.Y + ((item.rect.Height - SM_CYMENUCHECK) / 2));
- gr.Dispose ();
- bmp.Dispose ();
- }
- }
- static public void DrawPopupMenu (Graphics dc, IntPtr hMenu, Rectangle cliparea, Rectangle rect)
- {
- MENU menu = GetMenuFromID (hMenu);
- dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
- (ThemeEngine.Current.ColorMenu), cliparea);
- /* Draw menu borders */
- dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorHilightText),
- rect.X, rect.Y, rect.X + rect.Width, rect.Y);
- dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorHilightText),
- rect.X, rect.Y, rect.X, rect.Y + rect.Height);
- dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
- rect.X + rect.Width - 1 , rect.Y , rect.X + rect.Width - 1, rect.Y + rect.Height);
- dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonDkShadow),
- rect.X + rect.Width, rect.Y , rect.X + rect.Width, rect.Y + rect.Height);
- dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
- rect.X , rect.Y + rect.Height - 1 , rect.X + rect.Width - 1, rect.Y + rect.Height -1);
- dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonDkShadow),
- rect.X , rect.Y + rect.Height, rect.X + rect.Width - 1, rect.Y + rect.Height);
- for (int i = 0; i < menu.items.Count; i++)
- if (cliparea.IntersectsWith (((MENUITEM) menu.items[i]).rect)) {
- DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, menu.bMenubar);
- }
- }
- // Updates the menu rect and returns the height
- static public int MenuBarCalcSize (Graphics dc, IntPtr hMenu, int width)
- {
- int x = 0;
- int i = 0;
- int y = 0;
- MENU menu = GetMenuFromID (hMenu);
- menu.Height = 0;
- MENUITEM item;
- while (i < menu.items.Count) {
- item = (MENUITEM) menu.items[i];
- CalcItemSize (dc, item, y, x, true);
- i = i + 1;
- if (x + item.rect.Width > width) {
- item.rect.X = 0;
- y += item.rect.Height;
- item.rect.Y = y;
- x = 0;
- }
- x += item.rect.Width;
- item.fState |= MF.MF_MENUBAR;
- if (y + item.rect.Height > menu.Height)
- menu.Height = item.rect.Height + y;
- }
- menu.Width = width;
- return menu.Height;
- }
- // Draws a menu bar in a Window
- static public void DrawMenuBar (Graphics dc, IntPtr hMenu, Rectangle rect)
- {
- MENU menu = GetMenuFromID (hMenu);
- Rectangle rect_menu = new Rectangle ();
- if (menu.Height == 0)
- MenuBarCalcSize (dc, hMenu, rect_menu.Width);
- rect.Height = menu.Height;
- rect.Width = menu.Width;
- for (int i = 0; i < menu.items.Count; i++)
- DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, true);
- }
- /*
- Menu handeling API
- */
- static public MENUITEM FindItemByCoords (IntPtr hMenu, Point pt)
- {
- MENU menu = GetMenuFromID (hMenu);
- for (int i = 0; i < menu.items.Count; i++) {
- MENUITEM item = (MENUITEM) menu.items[i];
- if (item.rect.Contains (pt)) {
- return item;
- }
- }
- return null;
- }
- // Select the item and unselect the previous selecte item
- static public void SelectItem (IntPtr hMenu, MENUITEM item, bool execute, TRACKER tracker)
- {
- MENU menu = GetMenuFromID (hMenu);
- MENUITEM previous_selitem = null;
-
- /* Already selected */
- for (int i = 0; i < menu.items.Count; i++) {
- MENUITEM it = (MENUITEM) menu.items[i];
- if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
- if (item.rect == it.rect) {
- return;
- }
- /* Unselect previous item*/
- previous_selitem = it;
- it.fState = it.fState & ~MF.MF_HILITE;
- menu.Wnd.Invalidate (previous_selitem.rect);
- break;
- }
- }
-
- // If the previous item had subitems, hide them
- if (previous_selitem != null && previous_selitem.item.IsPopup)
- HideSubPopups (hMenu);
- if (tracker.hCurrentMenu != hMenu) {
- menu.Wnd.Capture = true;
- tracker.hCurrentMenu = hMenu;
- }
- menu.SelectedItem = item;
- item.fState |= MF.MF_HILITE;
- menu.Wnd.Invalidate (item.rect);
- item.item.PerformSelect ();
-
- if (execute)
- ExecFocusedItem (hMenu, item, tracker);
- }
- // Used when the user executes the action of an item (press enter, shortcut)
- // or a sub-popup menu has to be shown
- static public void ExecFocusedItem (IntPtr hMenu, MENUITEM item, TRACKER tracker)
- {
- if (item.item.Enabled == false)
- return;
-
- if (item.item.IsPopup) {
- ShowSubPopup (hMenu, item.hSubMenu, item, tracker);
- }
- else {
- // Execute function
- }
- }
- // Create a popup window and show it or only show it if it is already created
- static public void ShowSubPopup (IntPtr hParent, IntPtr hMenu, MENUITEM item, TRACKER tracker)
- {
- MENU menu = GetMenuFromID (hMenu);
- Point pnt = new Point ();
- if (item.item.Enabled == false)
- return;
- MENU menu_parent = GetMenuFromID (hParent);
- ((PopUpWindow)menu_parent.Wnd).LostFocus ();
- tracker.hCurrentMenu = hMenu;
- if (menu.Wnd == null)
- menu.Wnd = new PopUpWindow (hMenu, tracker);
-
- pnt.X = item.rect.X + item.rect.Width;
- pnt.Y = item.rect.Y + 1;
- pnt = menu_parent.Wnd.PointToScreen (pnt);
- menu.Wnd.Location = pnt;
- MENUITEM select_item = GetNextItem (hMenu, ItemNavigation.First);
- if (select_item != null)
- MenuAPI.SelectItem (hMenu, select_item, false, tracker);
- ((PopUpWindow)menu.Wnd).ShowWindow ();
- Console.WriteLine ("ShowSubPopup end {0} {1}", ((PopUpWindow)menu.Wnd).Location,
- ((PopUpWindow)menu.Wnd).Size);
- }
- /* Hides all the submenus open in a menu */
- static public void HideSubPopups (IntPtr hMenu)
- {
- MENU menu = GetMenuFromID (hMenu);
- MENUITEM item;
-
- for (int i = 0; i < menu.items.Count; i++) {
- item = (MENUITEM) menu.items[i];
- if (!item.item.IsPopup)
- continue;
- MENU sub_menu = GetMenuFromID (item.hSubMenu);
- if (sub_menu.Wnd != null) {
- HideSubPopups (item.hSubMenu);
- ((PopUpWindow)sub_menu.Wnd).Hide ();
- }
- }
- }
- static public void DestroyMenu (IntPtr hMenu)
- {
- if (hMenu == IntPtr.Zero)
- return;
- MENU menu = GetMenuFromID (hMenu);
- MENUITEM item;
- for (int i = 0; i < menu.items.Count; i++) {
- item = (MENUITEM) menu.items[i];
- if (item.item.IsPopup) {
- MENU sub_menu = GetMenuFromID (item.hSubMenu);
- if (sub_menu != null && sub_menu.Wnd != null)
- HideSubPopups (item.hSubMenu);
-
- DestroyMenu (item.hSubMenu);
- }
- }
- // Do not destroy the window of a Menubar
- if (menu.Wnd != null && menu.bMenubar == false) {
- ((PopUpWindow)menu.Wnd).Dispose ();
- menu.Wnd = null;
- }
-
- /* Unreference from the array list */
- menu_list[((int)hMenu)-1] = null;
- }
-
- // Find item by screen coordinates
- static public bool FindSubItemByCoord (IntPtr hMenu, Point pnt, ref IntPtr hMenuItem, ref MENUITEM itemfound)
- {
- Point pnt_client;
- Rectangle rect;
- MENU menu = GetMenuFromID (hMenu);
- MENUITEM item;
-
- for (int i = 0; i < menu.items.Count; i++) {
- item = (MENUITEM) menu.items[i];
-
- if (item.item.IsPopup)
- if (FindSubItemByCoord (item.hSubMenu, pnt, ref hMenuItem, ref itemfound) == true)
- return true;
-
- if (menu.Wnd == null) // Menu has not been created yet
- continue;
-
- rect = item.rect;
- pnt_client = menu.Wnd.PointToScreen (new Point (item.rect.X, item.rect.Y));
- rect.X = pnt_client.X;
- rect.Y = pnt_client.Y;
-
- if (rect.Contains (pnt) == true) {
- itemfound = item;
- hMenuItem = hMenu;
- return true;
- }
- }
-
- return false;
- }
- static public void SetMenuBarWindow (IntPtr hMenu, Control wnd)
- {
- MENU menu = GetMenuFromID (hMenu);
- menu.Wnd = wnd;
- menu.bMenubar = true;
- }
- static private void MenuBarMove (IntPtr hMenu, MENUITEM item, TRACKER tracker)
- {
- MENU menu = GetMenuFromID (hMenu);
- Point pnt = new Point (item.rect.X, item.rect.Y + item.rect.Height);
- pnt = menu.Wnd.PointToScreen (pnt);
- MenuAPI.SelectItem (hMenu, item, false, tracker);
- HideSubPopups (tracker.hCurrentMenu);
- tracker.hCurrentMenu = hMenu;
- MenuAPI.TrackPopupMenu (hMenu, item.hSubMenu, pnt, false, null);
- }
- // Function that process all menubar mouse events
- static public void TrackBarMouseEvent (IntPtr hMenu, Control wnd, MouseEventArgs e, MenuMouseEvent eventype, TRACKER tracker)
- {
- MENU menu = GetMenuFromID (hMenu);
- switch (eventype) {
- case MenuMouseEvent.Down: {
- MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
- if (item != null)
- MenuBarMove (hMenu, item, tracker);
- break;
- }
- case MenuMouseEvent.Move: { /* Coordinates in screen position*/
- if (tracker.hCurrentMenu != IntPtr.Zero) {
- Point pnt = new Point (e.X, e.Y);
- pnt = menu.Wnd.PointToClient (pnt);
- MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, pnt);
- if (item != null && menu.SelectedItem != item)
- MenuBarMove (hMenu, item, tracker);
- }
- break;
- }
- default:
- break;
- }
- }
- static public MENUITEM FindItemByKey (IntPtr hMenu, IntPtr key)
- {
- MENU menu = GetMenuFromID (hMenu);
- MENUITEM item;
- char key_char = (char ) (key.ToInt32() & 0xff);
- key_char = Char.ToUpper (key_char);
- for (int i = 0; i < menu.items.Count; i++) {
- item = (MENUITEM) menu.items[i];
- if (item.item.Mnemonic == '\0')
- continue;
- if (item.item.Mnemonic == key_char)
- return item;
- }
- return null;
- }
- // Get the next or previous selectable item on a menu
- static public MENUITEM GetNextItem (IntPtr hMenu, ItemNavigation navigation)
- {
- MENU menu = GetMenuFromID (hMenu);
- int pos = 0;
- bool selectable_items = false;
- MENUITEM item;
- // Check if there is at least a selectable item
- for (int i = 0; i < menu.items.Count; i++) {
- item = (MENUITEM)menu.items[i];
- if (item.item.Separator == false && item.item.Visible == true) {
- selectable_items = true;
- break;
- }
- }
- if (selectable_items == false)
- return null;
- switch (navigation) {
- case ItemNavigation.First: {
- pos = 0;
- /* Next item that is not separator and it is visible*/
- for (; pos < menu.items.Count; pos++) {
- item = (MENUITEM)menu.items[pos];
- if (item.item.Separator == false && item.item.Visible == true)
- break;
- }
- if (pos >= menu.items.Count) { /* Jump at the start of the menu */
- pos = 0;
- /* Next item that is not separator and it is visible*/
- for (; pos < menu.items.Count; pos++) {
- item = (MENUITEM)menu.items[pos];
- if (item.item.Separator == false && item.item.Visible == true)
- break;
- }
- }
- break;
- }
- case ItemNavigation.Last: { // Not used
- break;
- }
- case ItemNavigation.Next: {
- if (menu.SelectedItem != null)
- pos = menu.SelectedItem.pos;
- /* Next item that is not separator and it is visible*/
- for (pos++; pos < menu.items.Count; pos++) {
- item = (MENUITEM)menu.items[pos];
- if (item.item.Separator == false && item.item.Visible == true)
- break;
- }
- if (pos >= menu.items.Count) { /* Jump at the start of the menu */
- pos = 0;
- /* Next item that is not separator and it is visible*/
- for (; pos < menu.items.Count; pos++) {
- item = (MENUITEM)menu.items[pos];
- if (item.item.Separator == false && item.item.Visible == true)
- break;
- }
- }
- break;
- }
- case ItemNavigation.Previous: {
- if (menu.SelectedItem != null)
- pos = menu.SelectedItem.pos;
- /* Previous item that is not separator and it is visible*/
- for (pos--; pos >= 0; pos--) {
- item = (MENUITEM)menu.items[pos];
- if (item.item.Separator == false && item.item.Visible == true)
- break;
- }
- if (pos < 0 ) { /* Jump at the end of the menu*/
- pos = menu.items.Count - 1;
- /* Previous item that is not separator and it is visible*/
- for (; pos >= 0; pos--) {
- item = (MENUITEM)menu.items[pos];
- if (item.item.Separator == false && item.item.Visible == true)
- break;
- }
- }
- break;
- }
- default:
- break;
- }
- return (MENUITEM)menu.items[pos];
- }
- static public bool ProcessKeys (IntPtr hMenu, ref Message msg, Keys keyData, TRACKER tracker)
- {
- MENU menu = GetMenuFromID (hMenu);
- MENUITEM item;
- switch (keyData) {
- case Keys.Up: {
- item = GetNextItem (hMenu, ItemNavigation.Previous);
- if (item != null)
- MenuAPI.SelectItem (hMenu, item, false, tracker);
- break;
- }
- case Keys.Down: {
- item = GetNextItem (hMenu, ItemNavigation.Next);
- if (item != null)
- MenuAPI.SelectItem (hMenu, item, false, tracker);
- break;
- }
- /* Menubar selects and opens next. Popups next or open*/
- case Keys.Right: {
- // Try to Expand popup first
- if (menu.SelectedItem.item.IsPopup) {
- ShowSubPopup (hMenu, menu.SelectedItem.hSubMenu, menu.SelectedItem, tracker);
- } else {
- MENU parent = null;
- if (menu.hParent != IntPtr.Zero)
- parent = GetMenuFromID (menu.hParent);
- if (parent != null && parent.bMenubar == true) {
- MENUITEM select_item = GetNextItem (menu.hParent, ItemNavigation.Next);
- MenuBarMove (menu.hParent, select_item, tracker);
- }
- }
- break;
- }
- case Keys.Left: {
- // Try to Collapse popup first
- if (menu.SelectedItem.item.IsPopup) {
- } else {
- MENU parent = null;
- if (menu.hParent != IntPtr.Zero)
- parent = GetMenuFromID (menu.hParent);
- if (parent != null && parent.bMenubar == true) {
- MENUITEM select_item = GetNextItem (menu.hParent, ItemNavigation.Previous);
- MenuBarMove (menu.hParent, select_item, tracker);
- }
- }
- break;
- }
-
- case Keys.Return: {
- Console.WriteLine ("Return key: "+ menu.SelectedItem.item.Text);
- MenuAPI.ExecFocusedItem (hMenu, menu.SelectedItem, tracker);
- break;
- }
- default:
- break;
- }
- /* Try if it is a menu hot key */
- item = MenuAPI.FindItemByKey (hMenu, msg.WParam);
- if (item != null) {
- MenuAPI.SelectItem (hMenu, item, false, tracker);
- return true;
- }
- return false;
- }
- }
- /*
- class PopUpWindow
- */
- internal class PopUpWindow : Control
- {
- private IntPtr hMenu;
- private MenuAPI.TRACKER tracker;
- public PopUpWindow (IntPtr hMenu, MenuAPI.TRACKER tracker): base ()
- {
- this.hMenu = hMenu;
- this.tracker = tracker;
- MouseDown += new MouseEventHandler (OnMouseDownPUW);
- MouseMove += new MouseEventHandler (OnMouseMovePUW);
- MouseUp += new MouseEventHandler (OnMouseUpPUW);
- Paint += new PaintEventHandler (OnPaintPUW);
- SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
- SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
- }
- protected override CreateParams CreateParams
- {
- get {
- CreateParams cp = base.CreateParams;
- cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN));
- cp.ExStyle |= (int)WindowStyles.WS_EX_TOOLWINDOW;
- return cp;
- }
- }
- public void ShowWindow ()
- {
- Capture = true;
- Show ();
- Refresh ();
- }
-
- public void LostFocus ()
- {
- Capture = false;
- }
- protected override void OnResize (EventArgs e)
- {
- base.OnResize (e);
- }
- private void OnPaintPUW (Object o, PaintEventArgs pevent)
- {
- if (Width <= 0 || Height <= 0 || Visible == false)
- return;
- Draw (pevent.ClipRectangle);
- pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
- }
- private void OnMouseDownPUW (object sender, MouseEventArgs e)
- {
- /* Click outside the client area*/
- if (ClientRectangle.Contains (e.X, e.Y) == false) {
- Capture = false;
- Hide ();
- }
- }
- private void OnMouseUpPUW (object sender, MouseEventArgs e)
- {
- /* Click outside the client area*/
- MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
- if (item != null) {
- item.item.PerformClick ();
- Hide ();
- Capture = false;
- }
- }
- private void OnMouseMovePUW (object sender, MouseEventArgs e)
- {
- MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
-
- if (item != null) {
- MenuAPI.SelectItem (hMenu, item, true, tracker);
- } else {
- MenuAPI.MENU menu_parent = null;
- if (tracker.hTopMenu != IntPtr.Zero)
- menu_parent = MenuAPI.GetMenuFromID (tracker.hTopMenu);
-
- if (menu_parent == null)
- return;
-
- if (menu_parent.bMenubar) {
- MenuAPI.TrackBarMouseEvent (tracker.hTopMenu,
- this, new MouseEventArgs(e.Button, e.Clicks, MousePosition.X, MousePosition.Y, e.Delta),
- MenuAPI.MenuMouseEvent.Move, tracker);
- }
-
- IntPtr hMenuItem = IntPtr.Zero;
- MenuAPI.MENUITEM item_found = null;
-
- if (MenuAPI.FindSubItemByCoord (tracker.hTopMenu, MousePosition, ref hMenuItem, ref item_found) == false)
- return;
-
- MenuAPI.SelectItem (hMenuItem, item_found, false, tracker);
- }
- }
- protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
- {
- return MenuAPI.ProcessKeys (hMenu, ref msg, keyData, tracker);
- }
- protected override void CreateHandle ()
- {
- base.CreateHandle ();
- RefreshItems ();
- }
-
- // Called when the number of items has changed
- internal void RefreshItems ()
- {
- MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
- MenuAPI.CalcPopupMenuSize (DeviceContext, hMenu);
- Width = menu.Width;
- Height = menu.Height;
- }
- private void Draw (Rectangle clip)
- {
- MenuAPI.DrawPopupMenu (DeviceContext, hMenu, clip, ClientRectangle);
- }
- }
- }
|