Menu key navigation, itemcollection completion, menu fixes
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / MenuAPI.cs
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, jordi@ximian.com
24 //
25
26 // NOT COMPLETE
27
28 using System.Drawing;
29 using System.Drawing.Text;
30 using System.Collections;
31
32 namespace System.Windows.Forms
33 {
34
35         /*
36                 This class mimics the Win32 API Menu functionality
37         */
38         internal class MenuAPI
39         {
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
57
58                 public class MENU
59                 {
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
69                         public bool             bMenubar;
70
71                         public MENU ()
72                         {
73                                 Wnd = null;
74                                 hParent = IntPtr.Zero;
75                                 items = new ArrayList ();
76                                 Flags = MF.MF_INSERT;
77                                 Width = Height = FocusedItem = 0;
78                                 tracker = new TRACKER ();
79                                 bMenubar = false;
80                         }
81
82                 }
83
84                 public class MENUITEM
85                 {
86                         public  MenuItem        item;
87                         public  Rectangle       rect;
88                         public  int             fMask;
89                         public  int             fType;
90                         public  MF              fState;
91                         public  int             wID;
92                         public  IntPtr          hSubMenu;
93                         public  int             xTab;
94                         public  int             pos;    /* Position in the menuitems array*/
95
96                         public MENUITEM ()
97                         {
98                                 xTab = 0;
99                                 fMask = 0;
100                                 wID = 0;
101                                 pos = 0;
102                                 rect = new Rectangle ();
103                         }
104
105                 };
106
107                 public class TRACKER
108                 {
109                         public  IntPtr  hCurrentMenu;
110                         public  IntPtr  hTopMenu;
111
112                         public TRACKER ()
113                         {
114                                 hCurrentMenu = hTopMenu = IntPtr.Zero;
115                         }
116                 };
117
118                 static void DumpMenuItems (ArrayList list)
119                 {
120                         Console.WriteLine ("Menu items dump start--- ");
121
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);
124
125                         Console.WriteLine ("Menu items dump end --- ");
126                 }
127
128                 public enum MenuMouseEvent
129                 {
130                         Down,
131                         Move,
132                 }
133
134                 internal enum ItemNavigation
135                 {
136                         First,
137                         Last,
138                         Next,
139                         Previous
140                 }
141
142                 internal enum MF
143                 {
144                         MF_INSERT           = 0x0,
145                         MF_APPEND           = 0x100,
146                         MF_DELETE           = 0x200,
147                         MF_REMOVE           = 0x1000,
148                         MF_BYCOMMAND        = 0,
149                         MF_BYPOSITION       = 0x400,
150                         MF_SEPARATOR        = 0x800,
151                         MF_ENABLED          = 0,
152                         MF_GRAYED           = 1,
153                         MF_DISABLED         = 2,
154                         MF_UNCHECKED        = 0,
155                         MF_CHECKED          = 8,
156                         MF_USECHECKBITMAPS  = 0x200,
157                         MF_STRING           = 0,
158                         MF_BITMAP           = 4,
159                         MF_OWNERDRAW        = 0x100,
160                         MF_POPUP            = 0x10,
161                         MF_MENUBARBREAK     = 0x20,
162                         MF_MENUBREAK        = 0x40,
163                         MF_UNHILITE         = 0,
164                         MF_HILITE           = 0x80,
165                         MF_DEFAULT          = 0x1000,
166                         MF_SYSMENU          = 0x2000,
167                         MF_HELP             = 0x4000,
168                         MF_RIGHTJUSTIFY     = 0x4000,
169                         MF_MENUBAR          = 0x8000    // Internal
170                 }
171
172                 static MenuAPI ()
173                 {
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;
178
179                         string_format_shortcut.LineAlignment = StringAlignment.Center;
180                         string_format_shortcut.Alignment = StringAlignment.Far;
181
182                         string_format_menubar_text.LineAlignment = StringAlignment.Center;
183                         string_format_menubar_text.Alignment = StringAlignment.Center;
184                         string_format_menubar_text.HotkeyPrefix = HotkeyPrefix.Show;
185
186                 }
187
188                 static public IntPtr StoreMenuID (MENU menu)
189                 {
190                         int id = menu_list.Add (menu);
191                         //Console.WriteLine ("StoreMenuID:" + id + 1);
192                         return (IntPtr)(id + 1);
193                 }
194
195                 static public MENU GetMenuFromID (IntPtr ptr)
196                 {
197                         int id = (int)ptr;
198
199                         id = id - 1;
200                         return (MENU) menu_list[id];
201                 }
202
203                 static public IntPtr CreateMenu ()
204                 {
205                         MENU menu = new MENU ();
206                         return StoreMenuID (menu);
207                 }
208
209                 static public IntPtr CreatePopupMenu ()
210                 {
211                         Console.WriteLine ("MenuAPI.CreatePopupMenu");
212
213                         MENU popMenu = new MENU ();
214                         popMenu.Flags |= MF.MF_POPUP;
215                         return StoreMenuID (popMenu);
216                 }
217
218                 static public int InsertMenuItem (IntPtr hMenu, int uItem, bool fByPosition, MenuItem item, ref IntPtr hSubMenu)
219                 {
220                         int id;
221
222                         if (fByPosition == false)
223                                 throw new NotImplementedException ();
224
225                         // Insert the item
226
227                         MENU menu = GetMenuFromID (hMenu);
228                         if ((uint)uItem > menu.items.Count)
229                                 uItem =  menu.items.Count;
230
231                         MENUITEM menu_item = new MENUITEM ();
232                         menu_item.item = item;
233
234                         if (item.IsPopup) {
235                                 menu_item.hSubMenu = CreatePopupMenu ();
236                                 MENU submenu = GetMenuFromID (menu_item.hSubMenu);
237                                 submenu.hParent = hMenu;
238                         }
239                         else
240                                 menu_item.hSubMenu = IntPtr.Zero;
241
242                         //menu_item.Flags |= MF.MF_POPUP;
243
244                         hSubMenu = menu_item.hSubMenu;
245
246                         id = menu.items.Count;
247                         menu_item.pos = menu.items.Count;
248                         menu.items.Insert (uItem, menu_item);
249
250                         //Console.WriteLine ("InsertMenuItem {0} {1} {2}" + menu.items.Count,
251                         //);
252                         return id;
253                 }
254
255                 // X and Y are screen coordinates
256                 static public bool TrackPopupMenu (IntPtr hTopMenu, IntPtr hMenu, Point pnt, bool bMenubar, Control Wnd)
257                 {
258                         MENU menu = GetMenuFromID (hMenu);
259                         PopUpWindow popup = new PopUpWindow (hMenu);
260                         menu.Wnd = popup;
261                         menu.tracker.hCurrentMenu = hMenu;
262                         menu.tracker.hTopMenu = hTopMenu;
263                         //menu.bMenubar = bMenubar;
264
265                         MENUITEM select_item = GetNextItem (hMenu, ItemNavigation.First);
266
267                         if (select_item != null)
268                                 MenuAPI.SelectItem (hMenu, select_item, false);
269
270                         popup.Location =  popup.PointToClient (pnt);
271                         popup.ShowWindow ();
272                         menu.Wnd.Refresh ();
273
274                         Application.Run ();
275
276                         //popup.DestroyHandle ();
277                         menu.Wnd = null;
278                         return true;
279                 }
280
281                 /*
282                         Menu drawing API
283                 */
284
285                 static public void CalcItemSize (Graphics dc, MENUITEM item, int y, int x, bool menuBar)
286                 {
287                         item.rect.Y = y;
288                         item.rect.X = x;
289
290                         if (item.item.Visible == false)
291                                 return;
292
293                         if (item.item.Separator == true) {
294                                 item.rect.Height = SEPARATOR_HEIGHT / 2;
295                                 item.rect.Width = -1;
296                                 return;
297                         }
298
299                         SizeF size;
300                         size =  dc.MeasureString (item.item.Text, MENU_FONT);
301                         item.rect.Width = (int) size.Width;
302                         item.rect.Height = (int) size.Height;
303
304                         if (!menuBar) {
305
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;
310                                 }
311
312                                 item.rect.Width += 4 + (SM_CXMENUCHECK * 2);
313                         }
314                         else {
315                                 //item.rect.Width += MENU_BAR_ITEMS_SPACE;
316                                 x += item.rect.Width;
317                         }
318
319                         if (item.rect.Height < SM_CYMENU - 1)
320                                 item.rect.Height = SM_CYMENU - 1;
321
322                         //Console.WriteLine ("CalcItemSize " + item.rect);
323                 }
324
325
326                 static public void CalcPopupMenuSize (Graphics dc, IntPtr hMenu)
327                 {
328                         int x = 3;
329                         int start = 0;
330                         int i, n, y, max;
331
332                         MENU menu = GetMenuFromID (hMenu);
333                         menu.Height = 0;
334
335                         while (start < menu.items.Count) {
336                                 y = 2;
337                                 max = 0;
338                                 for (i = start; i < menu.items.Count; i++) {
339                                         MENUITEM item = (MENUITEM) menu.items[i];
340
341                                         if ((i != start) && (item.item.Break || item.item.BarBreak))
342                                                 break;
343
344                                         CalcItemSize (dc, item, y, x, false);
345                                         y += item.rect.Height;
346
347                                         if (item.rect.Width > max)
348                                                 max = item.rect.Width;
349                                 }
350
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;
355                                 }
356
357                                 if (y > menu.Height)
358                                         menu.Height = y;
359
360                                 x+= max;
361                         }
362
363                         menu.Width = x;
364
365                         //space for border
366                         menu.Width += 2;
367                         menu.Height += 2;
368
369                         menu.Width += SM_CXBORDER;
370                         menu.Height += SM_CYBORDER;
371
372                         //Console.WriteLine ("CalcPopupMenuSize {0} {1}", menu.Width, menu.Height);
373                 }
374
375                 static public void DrawMenuItem (Graphics dc, MENUITEM item, int menu_height, bool menuBar)
376                 {
377                         StringFormat string_format;
378
379                         if (item.item.Visible == false)
380                                 return;
381
382                         if (menuBar)
383                                 string_format = string_format_menubar_text;
384                         else
385                                 string_format = string_format_text;
386
387                         if (item.item.Separator == true) {
388
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);
391
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);
394
395                                 return;
396                         }
397
398                         Rectangle rect_text = item.rect;
399
400                         if (!menuBar)
401                                 rect_text.X += SM_CXMENUCHECK;
402
403                         if (item.item.BarBreak) { /* Draw vertical break bar*/
404
405                                 Rectangle rect = item.rect;
406                                 rect.Y++;
407                                 rect.Width = 3;
408                                 rect.Height = menu_height - 6;
409
410                                 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonShadow),
411                                         rect.X, rect.Y , rect.X, rect.Y + rect.Height);
412
413                                 dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorButtonHilight),
414                                         rect.X + 1, rect.Y , rect.X +1, rect.Y + rect.Height);
415
416                         }
417
418                         //Console.WriteLine ("!{0}, {1}, {2}", item.item.Text, item.rect, rect_text);
419
420                         if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE) {
421                                 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
422                                         (ThemeEngine.Current.ColorHilight), item.rect);
423                         }
424
425                         if (item.item.Enabled) {
426
427                                 Color color_text;
428
429                                 if ((item.fState & MF.MF_HILITE) == MF.MF_HILITE)
430                                         color_text = ThemeEngine.Current.ColorHilightText;
431                                 else
432                                         color_text = ThemeEngine.Current.ColorMenuText;
433
434
435                                 dc.DrawString (item.item.Text, MENU_FONT,
436                                         ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
437                                         rect_text, string_format);
438
439                                 if (!menuBar && item.item.Shortcut != Shortcut.None && item.item.ShowShortcut) {
440
441                                         string str = item.item.GetShortCutText ();
442                                         Rectangle rect = rect_text;
443                                         rect.X = item.xTab;
444                                         rect.Width -= item.xTab;
445
446                                         dc.DrawString (str, MENU_FONT, ThemeEngine.Current.ResPool.GetSolidBrush (color_text),
447                                                 rect, string_format_shortcut);
448
449                                 }
450                         }
451                         else {
452                                 ControlPaint.DrawStringDisabled (dc,
453                                         item.item.Text, MENU_FONT, Color.Black, rect_text,
454                                         string_format);
455                         }
456
457                         /* Draw arrow */
458                         if (menuBar == false && item.item.IsPopup) {
459
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));
467
468                                 gr.Dispose ();
469                                 bmp.Dispose ();
470                         }
471
472                         /* Draw checked or radio */
473                         if (menuBar == false && item.item.Checked) {
474
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);
479
480                                 if (item.item.RadioCheck)
481                                         ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Bullet);
482                                 else
483                                         ControlPaint.DrawMenuGlyph (gr, rect_arrow, MenuGlyph.Checkmark);
484
485                                 bmp.MakeTransparent ();
486                                 dc.DrawImage (bmp, area.X, item.rect.Y + (item.rect.Height /2));
487
488                                 gr.Dispose ();
489                                 bmp.Dispose ();
490                         }
491
492                 }
493
494                 static public void DrawPopupMenu (Graphics dc, IntPtr hMenu, Rectangle cliparea, Rectangle rect)
495                 {
496                         MENU menu = GetMenuFromID (hMenu);
497
498                         dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
499                                 (ThemeEngine.Current.ColorMenu), cliparea);
500
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);
504
505                         dc.DrawLine (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorHilightText),
506                                 rect.X, rect.Y, rect.X, rect.Y + rect.Height);
507
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);
510
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);
513
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);
516
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);
519
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);
523
524                         }
525                 }
526
527                 // Updates the menu rect and returns the height
528                 static public int MenuBarCalcSize (Graphics dc, IntPtr hMenu, int width)
529                 {
530                         int x = 0;
531                         int i = 0;
532                         MENU menu = GetMenuFromID (hMenu);
533                         MENUITEM item;
534
535                         while (i < menu.items.Count) {
536
537                                 item = (MENUITEM) menu.items[i];
538                                 CalcItemSize (dc, item, 0, x, true);
539                                 i = i + 1;
540                                 x += item.rect.Width;
541                                 item.fState |= MF.MF_MENUBAR;
542
543                                 if (item.rect.Height > menu.Height)
544                                         menu.Height = item.rect.Height;
545                         }
546
547                         menu.Width = width;
548                         return menu.Height;
549                 }
550
551                 // Draws a menu bar in a Window
552                 static public void DrawMenuBar (Graphics dc, IntPtr hMenu, Rectangle rect)
553                 {
554                         MENU menu = GetMenuFromID (hMenu);
555                         Rectangle rect_menu = new Rectangle ();
556
557                         if (menu.Height == 0)
558                                 MenuBarCalcSize (dc, hMenu, rect_menu.Width);
559
560                         rect.Height = menu.Height;
561                         rect.Width = menu.Width;
562
563                         Console.WriteLine ("DrawMenuBar {0}", rect);
564
565                         for (int i = 0; i < menu.items.Count; i++)
566                                 DrawMenuItem (dc, (MENUITEM) menu.items[i], menu.Height, true);
567                 }
568
569                 /*
570                         Menu handeling API
571                 */
572                 static public MENUITEM FindItemByCoords (IntPtr hMenu, Point pt)
573                 {
574                         MENU menu = GetMenuFromID (hMenu);
575
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);
580                                         return item;
581                                 }
582                         }
583
584                         //Console.WriteLine ("FindItemByCoords none ");
585                         return null;
586                 }
587
588                 static public void SelectItem (IntPtr hMenu, MENUITEM item, bool execute)
589                 {
590                         MENU menu = GetMenuFromID (hMenu);
591                         //int pos = 0;
592
593                         //Console.WriteLine ("Current: {0} select {1}", menu_parent.hCurrent, hMenu);
594                         MENUITEM highlight_item = null;
595
596                         /* Already selected */
597                         for (int i = 0; i < menu.items.Count; i++) {
598                                 MENUITEM it = (MENUITEM) menu.items[i];
599
600                                 if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
601                                         if (item.rect == it.rect)
602                                                 return;
603
604                                         highlight_item = item;
605                                 }
606                         }
607
608                         //Console.WriteLine ("SelectItem:Current is {0} {1}", tracker.hCurrentMenu, hMenu);
609
610                         if (menu.tracker.hCurrentMenu != hMenu) {
611                                 Console.WriteLine ("Changing current menu!");
612                                 HideSubPopups (hMenu);
613                                 menu.tracker.hCurrentMenu = hMenu;
614                         }
615
616                         /* Unselect previous item*/
617                         for (int i = 0; i < menu.items.Count; i++) {
618                                 MENUITEM it = (MENUITEM) menu.items[i];
619
620                                 if ((it.fState & MF.MF_HILITE) == MF.MF_HILITE) {
621                                         it.fState = item.fState & ~MF.MF_HILITE;
622                                         menu.Wnd.Invalidate (it.rect);
623
624                                         if (menu.bMenubar)
625                                                 Console.WriteLine ("**Select item invalidate {0}", it.rect);
626
627                                 }
628
629                                 //if (menu.items[i].Equals(item))
630                                 //      pos = i;
631                         }
632
633                         menu.SelectedItem = item;
634                         item.fState |= MF.MF_HILITE;
635                         //menu.items[pos] = item;
636
637                         if (menu.bMenubar)
638                                 Console.WriteLine ("**Select item invalidate {0}", item.rect);
639
640                         menu.Wnd.Invalidate (item.rect);
641
642                         if (execute)
643                                 ExecFocusedItem (hMenu, item);
644                 }
645
646                 /*
647                         Used when the user executes the action of an item (press enter, shortcut)
648                         or a sub-popup menu has to be shown
649                 */
650                 static public void ExecFocusedItem (IntPtr hMenu, MENUITEM item)
651                 {
652                         if (item.item.IsPopup) {
653                                 ShowSubPopup (hMenu, item.hSubMenu, item);
654                         }
655                         else {
656                                 // Execute function
657                         }
658                 }
659
660                 static public void ShowSubPopup (IntPtr hParent, IntPtr hMenu, MENUITEM item)
661                 {
662                         MENU menu = GetMenuFromID (hMenu);
663
664                         if (menu.Wnd != null) /* Already showing */
665                                 return;
666
667                         if (item.item.Enabled == false)
668                                 return;
669
670                         MENU menu_parent = GetMenuFromID (hParent);
671                         PopUpWindow popup = new PopUpWindow (hMenu);
672                         ((PopUpWindow)menu_parent.Wnd).LostFocus ();
673                         menu.Wnd = popup;
674                         menu.tracker.hCurrentMenu = hMenu;
675
676                         //Console.WriteLine ("ShowSubPopup:Setting current to {0}", menu.tracker.hCurrentMenu);
677
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;
684
685                         MENUITEM select_item = GetNextItem (hMenu, ItemNavigation.First);
686
687                         if (select_item != null)
688                                 MenuAPI.SelectItem (hMenu, select_item, false);
689
690                         popup.ShowWindow ();
691                         popup.Refresh ();
692
693                         //Console.WriteLine ("ShowSubPopup location:" + popup.Location);
694                 }
695
696                 /* Hides all the submenus open in a menu */
697                 static public void HideSubPopups (IntPtr hMenu)
698                 {
699                         //Console.WriteLine ("HideSubPopups: " + hMenu);
700
701                         MENU menu = GetMenuFromID (hMenu);
702                         MENUITEM item;
703
704                         for (int i = 0; i < menu.items.Count; i++) {
705                                 item = (MENUITEM) menu.items[i];
706                                 if (item.item.IsPopup) {
707
708                                         MENU sub_menu = GetMenuFromID (item.hSubMenu);
709
710                                         if (sub_menu.Wnd != null) {
711                                                 //Console.WriteLine ("Hiding!");
712                                                 HideSubPopups (item.hSubMenu);
713                                                 ((PopUpWindow)sub_menu.Wnd).Destroy ();
714                                                 sub_menu.Wnd = null;
715                                         }
716                                 }
717                         }
718                 }
719
720                 static public void DestroyMenu (IntPtr hMenu)
721                 {
722                         if (hMenu == IntPtr.Zero)
723                                 return;
724
725                         MENU menu = GetMenuFromID (hMenu);
726                         MENUITEM item;
727
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);
737                                         }
738                                 }
739                         }
740
741                         // TODO: Remove from list
742
743                         // Do not destroy the window of a Menubar
744                         if (menu.Wnd != null && menu.bMenubar == false) {
745                                 ((PopUpWindow)menu.Wnd).Destroy ();
746                                 menu.Wnd = null;
747                         }
748                 }
749
750                 static public void SetMenuBarWindow (IntPtr hMenu, Control wnd)
751                 {
752                         Console.WriteLine ("SetMenuBarWindow {0}", hMenu);
753                         MENU menu = GetMenuFromID (hMenu);
754                         menu.Wnd = wnd;
755                         menu.bMenubar = true;
756                 }
757
758                 static private void MenuBarMove (IntPtr hMenu, MENUITEM item)
759                 {
760
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);
764
765                         Console.WriteLine ("MenuBarMove {0} {1}", item.item.Text, menu.tracker.hCurrentMenu);
766
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);
771                 }
772
773                 // Function that process all menubar mouse events
774                 static public void TrackBarMouseEvent (IntPtr hMenu, Control wnd, MouseEventArgs e, MenuMouseEvent eventype)
775                 {
776                         MENU menu = GetMenuFromID (hMenu);
777
778                         switch (eventype) {
779                                 case MenuMouseEvent.Down: {
780
781                                         MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
782
783                                         //Console.WriteLine ("menubar: {0} {1}",item.rect.X,
784                                         //      item.rect.Y + item.rect.Height + 1);
785
786                                         if (item != null && menu.SelectedItem != item)
787                                                 MenuBarMove (hMenu, item);
788
789                                         break;
790                                 }
791
792                                 case MenuMouseEvent.Move: {
793
794                                         if (menu.tracker.hCurrentMenu != IntPtr.Zero) {
795
796                                                 MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
797
798                                                 if (item != null && menu.SelectedItem != item)
799                                                 MenuBarMove (hMenu, item);
800                                         }
801                                         break;
802                                 }
803
804                                 default:
805                                         break;
806                         }
807                 }
808
809                 static public MENUITEM FindItemByKey (IntPtr hMenu, IntPtr key)
810                 {
811                         MENU menu = GetMenuFromID (hMenu);
812                         MENUITEM item;
813
814                         char key_char = (char ) (key.ToInt32() & 0xff);
815                         key_char = Char.ToUpper (key_char);
816
817                         for (int i = 0; i < menu.items.Count; i++) {
818                                 item = (MENUITEM) menu.items[i];
819
820                                 //Console.WriteLine ("text {0} mnenonic {1} {2}", item.item.Text, item.item.Mnemonic, key_char);
821
822                                 if (item.item.Mnemonic == '\0')
823                                         continue;
824
825                                 if (item.item.Mnemonic == key_char)
826                                         return item;
827                         }
828
829                         return null;
830                 }
831
832                 // Get the next or previous selectable item on a menu
833                 static public MENUITEM GetNextItem (IntPtr hMenu, ItemNavigation navigation)
834                 {
835                         MENU menu = GetMenuFromID (hMenu);
836                         int pos = 0;
837                         bool selectable_items = false;
838                         MENUITEM item;
839
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;
845                                         break;
846                                 }
847                         }
848
849                         if (selectable_items == false)
850                                 return null;
851
852                         switch (navigation) {
853                         case ItemNavigation.First: {
854                                 pos = 0;
855
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)
860                                                 break;
861                                 }
862
863                                 if (pos >= menu.items.Count) { /* Jump at the start of the menu */
864                                         pos = 0;
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)
869                                                         break;
870                                         }
871                                 }
872
873                                 break;
874                         }
875
876                         case ItemNavigation.Last: { // Not used
877                                 break;
878                         }
879
880                         case ItemNavigation.Next: {
881
882                                 if (menu.SelectedItem != null)
883                                         pos = menu.SelectedItem.pos;
884
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)
889                                                 break;
890                                 }
891
892                                 if (pos >= menu.items.Count) { /* Jump at the start of the menu */
893                                         pos = 0;
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)
898                                                         break;
899                                         }
900                                 }
901                                 break;
902                         }
903
904                         case ItemNavigation.Previous: {
905
906                                 if (menu.SelectedItem != null)
907                                         pos = menu.SelectedItem.pos;
908
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)
913                                                 break;
914                                 }
915
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)
922                                                         break;
923                                         }
924                                 }
925
926                                 break;
927                         }
928
929                         default:
930                                 break;
931                         }
932
933                         return (MENUITEM)menu.items[pos];
934                 }
935
936                 static public bool ProcessKeys (IntPtr hMenu, ref Message msg, Keys keyData)
937                 {
938                         MENU menu = GetMenuFromID (hMenu);
939                         Console.WriteLine ("Menu.ProcessCmdKey {0} {1} {2}",keyData, msg.LParam, msg.WParam);
940                         MENUITEM item;
941
942                         switch (keyData) {
943                                 case Keys.Up: {
944                                         item = GetNextItem (hMenu, ItemNavigation.Previous);
945                                         if (item != null)
946                                                 MenuAPI.SelectItem (hMenu, item, false);
947
948                                         break;
949                                 }
950
951                                 case Keys.Down: {
952                                         item = GetNextItem (hMenu, ItemNavigation.Next);
953
954                                         if (item != null)
955                                                 MenuAPI.SelectItem (hMenu, item, false);
956                                         break;
957                                 }
958
959                                 /* Menubar selects and opens next. Popups next or open*/
960                                 case Keys.Right: {
961                                         //Console.WriteLine ("Key.Right next menubar item info {0}", menu.hParent);
962
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");
967                                         } else {
968
969                                                 MENU parent = null;
970                                                 if (menu.hParent != IntPtr.Zero)
971                                                         parent = GetMenuFromID (menu.hParent);
972
973                                                 if (parent != null && parent.bMenubar == true) {
974                                                         MENUITEM select_item = GetNextItem (menu.hParent, ItemNavigation.Next);
975                                                         MenuBarMove (menu.hParent, select_item);
976                                                 }
977                                         }
978
979                                         break;
980                                 }
981
982                                 case Keys.Left: {
983                                         //Console.WriteLine ("Key.Right next menubar item info {0}", menu.hParent);
984
985                                         // Try to Collapse popup first
986                                         if (menu.SelectedItem.item.IsPopup) {
987
988                                         } else {
989
990                                                 MENU parent = null;
991                                                 if (menu.hParent != IntPtr.Zero)
992                                                         parent = GetMenuFromID (menu.hParent);
993
994                                                 if (parent != null && parent.bMenubar == true) {
995                                                         MENUITEM select_item = GetNextItem (menu.hParent, ItemNavigation.Previous);
996                                                         MenuBarMove (menu.hParent, select_item);
997                                                 }
998                                         }
999
1000                                         break;
1001                                 }
1002
1003                                 default:
1004                                         break;
1005                         }
1006
1007                         /* Try if it is a menu hot key */
1008                         item = MenuAPI.FindItemByKey (hMenu, msg.WParam);
1009
1010                         //Console.WriteLine ("menu item is null: " + (item == null));
1011
1012                         if (item != null) {
1013                                 Console.WriteLine ("HotKey found: item.text" + item.item.Text);
1014                                 MenuAPI.SelectItem (hMenu, item, false);
1015                                 return true;
1016                         }
1017
1018                         return false;
1019                 }
1020         }
1021
1022         /*
1023
1024                 class PopUpWindow
1025
1026         */
1027         internal class PopUpWindow : Control
1028         {
1029                 private IntPtr hMenu;
1030
1031                 public PopUpWindow (IntPtr hMenu): base ()
1032                 {
1033                         this.hMenu = hMenu;
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);
1040                 }
1041
1042                 protected override CreateParams CreateParams
1043                 {
1044                         get {
1045                                 CreateParams cp = base.CreateParams;
1046                                 cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE));
1047                                 cp.ExStyle |= (int)WindowStyles.WS_EX_TOOLWINDOW;
1048                                 return cp;
1049                         }
1050                 }
1051
1052                 public void ShowWindow ()
1053                 {
1054                         Capture = true;
1055                         Show ();
1056                         Refresh ();
1057                 }
1058
1059                 public void Destroy ()
1060                 {
1061                         Capture = false;
1062                         DestroyHandle ();
1063                 }
1064
1065                 public void LostFocus ()
1066                 {
1067                         Capture = false;
1068                 }
1069
1070                 protected override void OnResize(EventArgs e)
1071                 {
1072                         base.OnResize (e);
1073                         //Console.WriteLine ("OnResize {0} {1} ", Width, Height);
1074                 }
1075
1076                 private void OnPaintPUW (Object o, PaintEventArgs pevent)
1077                 {
1078                         if (Width <= 0 || Height <=  0 || Visible == false)
1079                                 return;
1080
1081                         Draw (pevent.ClipRectangle);
1082                         pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
1083                 }
1084
1085                 private void OnMouseDownPUW (object sender, MouseEventArgs e)
1086                 {
1087                         //Console.WriteLine ("OnMouseDownPUW");
1088                         /* Click outside the client area*/
1089                         if (ClientRectangle.Contains (e.X, e.Y) == false) {
1090                                 Console.WriteLine ("Hide");
1091                                 Capture = false;
1092                                 Hide ();
1093                         }
1094                 }
1095
1096                 private void OnMouseUpPUW (object sender, MouseEventArgs e)
1097                 {
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);
1102
1103                         if (item != null && item.item.Enabled) {
1104                                 item.item.PerformClick ();
1105                                 MenuAPI.DestroyMenu (menu.tracker.hTopMenu);
1106
1107                                 Capture = false;
1108                                 Refresh ();
1109                         }
1110
1111                 }
1112
1113                 private void OnMouseMovePUW (object sender, MouseEventArgs e)
1114                 {
1115                         //Console.WriteLine ("OnMouseMovePUW");
1116                         MenuAPI.MENUITEM item = MenuAPI.FindItemByCoords (hMenu, new Point (e.X, e.Y));
1117                         MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
1118
1119                         if (item != null) {
1120                                 MenuAPI.SelectItem (hMenu, item, true);
1121                         } else {
1122
1123                                 if (menu.bMenubar) {
1124                                         //Console.WriteLine ("MenuBar tracker move " + e.Y);
1125                                         //MenuAPI.TrackBarMouseEvent (tracker.hTopMenu,
1126                                         //      this, e, MenuAPI.MenuMouseEvent.Move);
1127
1128                                         Point pnt = PointToClient (MousePosition);
1129
1130                                         MenuAPI.TrackBarMouseEvent (menu.tracker.hTopMenu,
1131                                                 this, new MouseEventArgs(e.Button, e.Clicks, pnt.X, pnt.Y, e.Delta),
1132                                                 MenuAPI.MenuMouseEvent.Move);
1133                                 }
1134                         }
1135                 }
1136
1137                 protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
1138                 {
1139                         Console.WriteLine ("PopUpWindow.ProcessCmdKey");
1140                         return MenuAPI.ProcessKeys (hMenu, ref msg, keyData);
1141                 }
1142
1143                 protected override void CreateHandle ()
1144                 {
1145                         base.CreateHandle ();
1146
1147                         MenuAPI.MENU menu = MenuAPI.GetMenuFromID (hMenu);
1148                         MenuAPI.CalcPopupMenuSize (DeviceContext, hMenu);
1149
1150                         Width = menu.Width;
1151                         Height = menu.Height;
1152                         Console.WriteLine ("CreateHandle {0} {1}", Width, Height);
1153                 }
1154
1155
1156                 private void Draw (Rectangle clip)
1157                 {
1158                         MenuAPI.DrawPopupMenu  (DeviceContext, hMenu, clip, ClientRectangle);
1159                 }
1160         }
1161
1162 }
1163
1164