Give the context menu its proper behaviour and modify TrackPopupMenu
[wine] / controls / menu.c
1 /*
2  * Menu functions
3  *
4  * Copyright 1993 Martin Ayotte
5  * Copyright 1994 Alexandre Julliard
6  * Copyright 1997 Morten Welinder
7  */
8
9 /*
10  * Note: the style MF_MOUSESELECT is used to mark popup items that
11  * have been selected, i.e. their popup menu is currently displayed.
12  * This is probably not the meaning this style has in MS-Windows.
13  */
14
15 #include <assert.h>
16 #include <ctype.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "bitmap.h"
21 #include "win.h"
22 #include "wine/winbase16.h"
23 #include "sysmetrics.h"
24 #include "task.h"
25 #include "heap.h"
26 #include "menu.h"
27 #include "module.h"
28 #include "neexe.h"
29 #include "nonclient.h"
30 #include "user.h"
31 #include "message.h"
32 #include "resource.h"
33 #include "tweak.h"
34
35 #include "debug.h"
36
37
38 /* internal popup menu window messages */
39
40 #define MM_SETMENUHANDLE        (WM_USER + 0)
41 #define MM_GETMENUHANDLE        (WM_USER + 1)
42
43 /* Menu item structure */
44 typedef struct {
45     /* ----------- MENUITEMINFO Stuff ----------- */
46     UINT32 fType;               /* Item type. */
47     UINT32 fState;              /* Item state.  */
48     UINT32 wID;                 /* Item id.  */
49     HMENU32 hSubMenu;           /* Pop-up menu.  */
50     HBITMAP32 hCheckBit;        /* Bitmap when checked.  */
51     HBITMAP32 hUnCheckBit;      /* Bitmap when unchecked.  */
52     LPSTR text;                 /* Item text or bitmap handle.  */
53     DWORD dwItemData;           /* Application defined.  */
54     /* ----------- Wine stuff ----------- */
55     RECT32      rect;          /* Item area (relative to menu window) */
56     UINT32      xTab;          /* X position of text after Tab */
57 } MENUITEM;
58
59 /* Popup menu structure */
60 typedef struct {
61     WORD        wFlags;       /* Menu flags (MF_POPUP, MF_SYSMENU) */
62     WORD        wMagic;       /* Magic number */
63     HQUEUE16    hTaskQ;       /* Task queue for this menu */
64     WORD        Width;        /* Width of the whole menu */
65     WORD        Height;       /* Height of the whole menu */
66     WORD        nItems;       /* Number of items in the menu */
67     HWND32      hWnd;         /* Window containing the menu */
68     MENUITEM   *items;        /* Array of menu items */
69     UINT32      FocusedItem;  /* Currently focused item */
70     WORD        defitem;      /* default item position. Unused (except for set/get)*/
71 } POPUPMENU, *LPPOPUPMENU;
72
73 /* internal flags for menu tracking */
74
75 #define TF_ENDMENU              0x0001
76 #define TF_SUSPENDPOPUP         0x0002
77 #define TF_SKIPREMOVE           0x0004
78
79 typedef struct
80 {
81     UINT32      trackFlags;
82     HMENU32     hCurrentMenu; /* current submenu (can be equal to hTopMenu)*/
83     HMENU32     hTopMenu;     /* initial menu */
84     HWND32      hOwnerWnd;    /* where notifications are sent */
85     POINT32     pt;
86 } MTRACKER;
87
88 #define MENU_MAGIC   0x554d  /* 'MU' */
89 #define IS_A_MENU(pmenu) ((pmenu) && (pmenu)->wMagic == MENU_MAGIC)
90
91 #define ITEM_PREV               -1
92 #define ITEM_NEXT                1
93
94   /* Internal MENU_TrackMenu() flags */
95 #define TPM_INTERNAL            0xF0000000
96 #define TPM_ENTERIDLEEX         0x80000000              /* set owner window for WM_ENTERIDLE */
97 #define TPM_BUTTONDOWN          0x40000000              /* menu was clicked before tracking */
98 #define TPM_POPUPMENU           0x20000000              /* menu is a popup menu */
99
100   /* popup menu shade thickness */
101 #define POPUP_XSHADE            4
102 #define POPUP_YSHADE            4
103
104   /* Space between 2 menu bar items */
105 #define MENU_BAR_ITEMS_SPACE 12
106
107   /* Minimum width of a tab character */
108 #define MENU_TAB_SPACE 8
109
110   /* Height of a separator item */
111 #define SEPARATOR_HEIGHT 5
112
113   /* (other menu->FocusedItem values give the position of the focused item) */
114 #define NO_SELECTED_ITEM  0xffff
115
116 #define MENU_ITEM_TYPE(flags) \
117   ((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR))
118
119 #define IS_STRING_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_STRING)
120
121 #define IS_SYSTEM_MENU(menu)  \
122         (!((menu)->wFlags & MF_POPUP) && (menu)->wFlags & MF_SYSMENU)
123 #define IS_SYSTEM_POPUP(menu) \
124         ((menu)->wFlags & MF_POPUP && (menu)->wFlags & MF_SYSMENU)
125
126 #define TYPE_MASK (MFT_STRING | MFT_BITMAP | MFT_OWNERDRAW | MFT_SEPARATOR | \
127                    MFT_MENUBARBREAK | MFT_MENUBREAK | MFT_RADIOCHECK | \
128                    MFT_RIGHTORDER | MFT_RIGHTJUSTIFY | \
129                    MF_POPUP | MF_SYSMENU | MF_HELP)
130 #define STATE_MASK (~TYPE_MASK)
131
132   /* Dimension of the menu bitmaps */
133 static WORD check_bitmap_width = 0, check_bitmap_height = 0;
134 static WORD arrow_bitmap_width = 0, arrow_bitmap_height = 0;
135
136 static HBITMAP32 hStdRadioCheck = 0;
137 static HBITMAP32 hStdCheck = 0;
138 static HBITMAP32 hStdMnArrow = 0;
139
140 // Minimze/restore/close buttons to be inserted in menubar
141 static HBITMAP32 hBmpMinimize = 0;
142 static HBITMAP32 hBmpMinimizeD = 0;
143 static HBITMAP32 hBmpMaximize = 0;
144 static HBITMAP32 hBmpMaximizeD = 0;
145 static HBITMAP32 hBmpClose = 0;
146
147 static HBRUSH32 hShadeBrush = 0;
148 static HMENU32 MENU_DefSysPopup = 0;  /* Default system menu popup */
149
150 /* Use global popup window because there's no way 2 menus can
151  * be tracked at the same time.  */ 
152
153 static WND* pTopPopupWnd   = 0;
154 static UINT32 uSubPWndLevel = 0;
155
156   /* Flag set by EndMenu() to force an exit from menu tracking */
157 static BOOL32 fEndMenu = FALSE;
158
159
160 /***********************************************************************
161  *           debug_print_menuitem
162  *
163  * Print a menuitem in readable form.
164  */
165
166 #define debug_print_menuitem(pre, mp, post) \
167   if(!TRACE_ON(menu)) ; else do_debug_print_menuitem(pre, mp, post)
168
169 #define MENUOUT(text) \
170   dsprintf(menu, "%s%s", (count++ ? "," : ""), (text))
171
172 #define MENUFLAG(bit,text) \
173   do { \
174     if (flags & (bit)) { flags &= ~(bit); MENUOUT ((text)); } \
175   } while (0)
176
177 static void do_debug_print_menuitem(const char *prefix, MENUITEM * mp, 
178                                     const char *postfix)
179 {
180     dbg_decl_str(menu, 256);
181
182     if (mp) {
183         UINT32 flags = mp->fType;
184         int typ = MENU_ITEM_TYPE(flags);
185         dsprintf(menu, "{ ID=0x%x", mp->wID);
186         if (flags & MF_POPUP)
187             dsprintf(menu, ", Sub=0x%x", mp->hSubMenu);
188         if (flags) {
189             int count = 0;
190             dsprintf(menu, ", Typ=");
191             if (typ == MFT_STRING)
192                 /* Nothing */ ;
193             else if (typ == MFT_SEPARATOR)
194                 MENUOUT("sep");
195             else if (typ == MFT_OWNERDRAW)
196                 MENUOUT("own");
197             else if (typ == MFT_BITMAP)
198                 MENUOUT("bit");
199             else
200                 MENUOUT("???");
201             flags -= typ;
202
203             MENUFLAG(MF_POPUP, "pop");
204             MENUFLAG(MFT_MENUBARBREAK, "barbrk");
205             MENUFLAG(MFT_MENUBREAK, "brk");
206             MENUFLAG(MFT_RADIOCHECK, "radio");
207             MENUFLAG(MFT_RIGHTORDER, "rorder");
208             MENUFLAG(MF_SYSMENU, "sys");
209             MENUFLAG(MFT_RIGHTJUSTIFY, "right");
210
211             if (flags)
212                 dsprintf(menu, "+0x%x", flags);
213         }
214         flags = mp->fState;
215         if (flags) {
216             int count = 0;
217             dsprintf(menu, ", State=");
218             MENUFLAG(MFS_GRAYED, "grey");
219             MENUFLAG(MFS_DISABLED, "dis");
220             MENUFLAG(MFS_CHECKED, "check");
221             MENUFLAG(MFS_HILITE, "hi");
222             MENUFLAG(MF_USECHECKBITMAPS, "usebit");
223             MENUFLAG(MF_MOUSESELECT, "mouse");
224             if (flags)
225                 dsprintf(menu, "+0x%x", flags);
226         }
227         if (mp->hCheckBit)
228             dsprintf(menu, ", Chk=0x%x", mp->hCheckBit);
229         if (mp->hUnCheckBit)
230             dsprintf(menu, ", Unc=0x%x", mp->hUnCheckBit);
231
232         if (typ == MFT_STRING) {
233             if (mp->text)
234                 dsprintf(menu, ", Text=\"%s\"", mp->text);
235             else
236                 dsprintf(menu, ", Text=Null");
237         } else if (mp->text == NULL)
238             /* Nothing */ ;
239         else
240             dsprintf(menu, ", Text=%p", mp->text);
241         dsprintf(menu, " }");
242     } else {
243         dsprintf(menu, "NULL");
244     }
245
246     TRACE(menu, "%s %s %s\n", prefix, dbg_str(menu), postfix);
247 }
248
249 #undef MENUOUT
250 #undef MENUFLAG
251
252 /***********************************************************************
253  *           MENU_CopySysPopup
254  *
255  * Return the default system menu.
256  */
257 static HMENU32 MENU_CopySysPopup(void)
258 {
259     HMENU32 hMenu = LoadMenuIndirect32A(SYSRES_GetResPtr(SYSRES_MENU_SYSMENU));
260
261     if( hMenu ) {
262         POPUPMENU* menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hMenu);
263         menu->wFlags |= MF_SYSMENU | MF_POPUP;
264     }
265     else {
266         hMenu = 0;
267         ERR(menu, "Unable to load default system menu\n" );
268     }
269
270     TRACE(menu, "returning %x.\n", hMenu );
271
272     return hMenu;
273 }
274
275
276 /**********************************************************************
277  *           MENU_GetSysMenu
278  *
279  * Create a copy of the system menu. System menu in Windows is
280  * a special menu-bar with the single entry - system menu popup.
281  * This popup is presented to the outside world as a "system menu". 
282  * However, the real system menu handle is sometimes seen in the 
283  * WM_MENUSELECT paramemters (and Word 6 likes it this way).
284  */
285 HMENU32 MENU_GetSysMenu( HWND32 hWnd, HMENU32 hPopupMenu )
286 {
287     HMENU32 hMenu;
288
289     if ((hMenu = CreateMenu32()))
290     {
291         POPUPMENU *menu = (POPUPMENU*) USER_HEAP_LIN_ADDR(hMenu);
292         menu->wFlags = MF_SYSMENU;
293         menu->hWnd = hWnd;
294
295         if (hPopupMenu == (HMENU32)(-1))
296             hPopupMenu = MENU_CopySysPopup();
297         else if( !hPopupMenu ) hPopupMenu = MENU_DefSysPopup; 
298
299         if (hPopupMenu)
300         {
301             InsertMenu32A( hMenu, -1, MF_SYSMENU | MF_POPUP | MF_BYPOSITION, hPopupMenu, NULL );
302
303             menu->items[0].fType = MF_SYSMENU | MF_POPUP;
304             menu->items[0].fState = 0;
305             menu = (POPUPMENU*) USER_HEAP_LIN_ADDR(hPopupMenu);
306             menu->wFlags |= MF_SYSMENU;
307
308             TRACE(menu,"GetSysMenu hMenu=%04x (%04x)\n", hMenu, hPopupMenu );
309             return hMenu;
310         }
311         DestroyMenu32( hMenu );
312     }
313     ERR(menu, "failed to load system menu!\n");
314     return 0;
315 }
316
317
318 /***********************************************************************
319  *           MENU_Init
320  *
321  * Menus initialisation.
322  */
323 BOOL32 MENU_Init()
324 {
325     HBITMAP32 hBitmap;
326     static unsigned char shade_bits[16] = { 0x55, 0, 0xAA, 0,
327                                             0x55, 0, 0xAA, 0,
328                                             0x55, 0, 0xAA, 0,
329                                             0x55, 0, 0xAA, 0 };
330
331     /* Load menu bitmaps */
332     hStdCheck = LoadBitmap32A(0, MAKEINTRESOURCE32A(OBM_CHECK));
333     hStdRadioCheck = LoadBitmap32A(0, MAKEINTRESOURCE32A(OBM_RADIOCHECK));
334     hStdMnArrow = LoadBitmap32A(0, MAKEINTRESOURCE32A(OBM_MNARROW));
335     /* Load system buttons bitmaps */
336     hBmpMinimize = LoadBitmap32A(0,MAKEINTRESOURCE32A(OBM_REDUCE));
337     hBmpMinimizeD = LoadBitmap32A(0,MAKEINTRESOURCE32A(OBM_REDUCED));
338     hBmpMaximize = LoadBitmap32A(0,MAKEINTRESOURCE32A(OBM_RESTORE));
339     hBmpMaximizeD = LoadBitmap32A(0,MAKEINTRESOURCE32A(OBM_RESTORED));
340     hBmpClose = LoadBitmap32A(0,MAKEINTRESOURCE32A(OBM_CLOSE));
341
342     if (hStdCheck)
343     {
344         BITMAP32 bm;
345         GetObject32A( hStdCheck, sizeof(bm), &bm );
346         check_bitmap_width = bm.bmWidth;
347         check_bitmap_height = bm.bmHeight;
348     } else
349          return FALSE;
350
351     /* Assume that radio checks have the same size as regular check.  */
352     if (!hStdRadioCheck)
353          return FALSE;
354
355     if (hStdMnArrow)
356         {
357          BITMAP32 bm;
358             GetObject32A( hStdMnArrow, sizeof(bm), &bm );
359             arrow_bitmap_width = bm.bmWidth;
360             arrow_bitmap_height = bm.bmHeight;
361     } else
362          return FALSE;
363
364     if ((hBitmap = CreateBitmap32( 8, 8, 1, 1, shade_bits)))
365             {
366                 if((hShadeBrush = CreatePatternBrush32( hBitmap )))
367                 {
368                     DeleteObject32( hBitmap );
369               if ((MENU_DefSysPopup = MENU_CopySysPopup()))
370                    return TRUE;
371         }
372     }
373
374     return FALSE;
375 }
376
377 /***********************************************************************
378  *           MENU_InitSysMenuPopup
379  *
380  * Grey the appropriate items in System menu.
381  */
382 static void MENU_InitSysMenuPopup( HMENU32 hmenu, DWORD style, DWORD clsStyle )
383 {
384     BOOL32 gray;
385
386     gray = !(style & WS_THICKFRAME) || (style & (WS_MAXIMIZE | WS_MINIMIZE));
387     EnableMenuItem32( hmenu, SC_SIZE, (gray ? MF_GRAYED : MF_ENABLED) );
388     gray = ((style & WS_MAXIMIZE) != 0);
389     EnableMenuItem32( hmenu, SC_MOVE, (gray ? MF_GRAYED : MF_ENABLED) );
390     gray = !(style & WS_MINIMIZEBOX) || (style & WS_MINIMIZE);
391     EnableMenuItem32( hmenu, SC_MINIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
392     gray = !(style & WS_MAXIMIZEBOX) || (style & WS_MAXIMIZE);
393     EnableMenuItem32( hmenu, SC_MAXIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
394     gray = !(style & (WS_MAXIMIZE | WS_MINIMIZE));
395     EnableMenuItem32( hmenu, SC_RESTORE, (gray ? MF_GRAYED : MF_ENABLED) );
396     gray = (clsStyle & CS_NOCLOSE) != 0;
397     EnableMenuItem32( hmenu, SC_CLOSE, (gray ? MF_GRAYED : MF_ENABLED) );
398 }
399
400
401 /******************************************************************************
402  *
403  *   UINT32  MENU_GetStartOfNextColumn(
404  *     HMENU32  hMenu )
405  *
406  *****************************************************************************/
407
408 static UINT32  MENU_GetStartOfNextColumn(
409     HMENU32  hMenu )
410 {
411     POPUPMENU  *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu);
412     UINT32  i = menu->FocusedItem + 1;
413
414     if(!menu)
415         return NO_SELECTED_ITEM;
416
417     if( i == NO_SELECTED_ITEM )
418         return i;
419
420     for( ; i < menu->nItems; ++i ) {
421         if (menu->items[i].fType & MF_MENUBARBREAK)
422             return i;
423     }
424
425     return NO_SELECTED_ITEM;
426 }
427
428
429 /******************************************************************************
430  *
431  *   UINT32  MENU_GetStartOfPrevColumn(
432  *     HMENU32  hMenu )
433  *
434  *****************************************************************************/
435
436 static UINT32  MENU_GetStartOfPrevColumn(
437     HMENU32  hMenu )
438 {
439     POPUPMENU const  *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu);
440     UINT32  i;
441
442     if( !menu )
443         return NO_SELECTED_ITEM;
444
445     if( menu->FocusedItem == 0 || menu->FocusedItem == NO_SELECTED_ITEM )
446         return NO_SELECTED_ITEM;
447
448     /* Find the start of the column */
449
450     for(i = menu->FocusedItem; i != 0 &&
451          !(menu->items[i].fType & MF_MENUBARBREAK);
452         --i); /* empty */
453
454     if(i == 0)
455         return NO_SELECTED_ITEM;
456
457     for(--i; i != 0; --i) {
458         if (menu->items[i].fType & MF_MENUBARBREAK)
459             break;
460     }
461
462     TRACE(menu, "ret %d.\n", i );
463
464     return i;
465 }
466
467
468
469 /***********************************************************************
470  *           MENU_FindItem
471  *
472  * Find a menu item. Return a pointer on the item, and modifies *hmenu
473  * in case the item was in a sub-menu.
474  */
475 static MENUITEM *MENU_FindItem( HMENU32 *hmenu, UINT32 *nPos, UINT32 wFlags )
476 {
477     POPUPMENU *menu;
478     UINT32 i;
479
480     if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(*hmenu))) return NULL;
481     if (wFlags & MF_BYPOSITION)
482     {
483         if (*nPos >= menu->nItems) return NULL;
484         return &menu->items[*nPos];
485     }
486     else
487     {
488         MENUITEM *item = menu->items;
489         for (i = 0; i < menu->nItems; i++, item++)
490         {
491             if (item->wID == *nPos)
492             {
493                 *nPos = i;
494                 return item;
495             }
496             else if (item->fType & MF_POPUP)
497             {
498                 HMENU32 hsubmenu = item->hSubMenu;
499                 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
500                 if (subitem)
501                 {
502                     *hmenu = hsubmenu;
503                     return subitem;
504                 }
505             }
506         }
507     }
508     return NULL;
509 }
510
511 /***********************************************************************
512  *           MENU_FreeItemData
513  */
514 static void MENU_FreeItemData( MENUITEM* item )
515 {
516     /* delete text */
517     if (IS_STRING_ITEM(item->fType) && item->text)
518         HeapFree( SystemHeap, 0, item->text );
519 }
520
521 /***********************************************************************
522  *           MENU_FindItemByCoords
523  *
524  * Find the item at the specified coordinates (screen coords). Does 
525  * not work for child windows and therefore should not be called for 
526  * an arbitrary system menu.
527  */
528 static MENUITEM *MENU_FindItemByCoords( POPUPMENU *menu, 
529                                         POINT32 pt, UINT32 *pos )
530 {
531     MENUITEM *item;
532     WND *wndPtr;
533     UINT32 i;
534
535     if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return NULL;
536     pt.x -= wndPtr->rectWindow.left;
537     pt.y -= wndPtr->rectWindow.top;
538     item = menu->items;
539     for (i = 0; i < menu->nItems; i++, item++)
540     {
541         if ((pt.x >= item->rect.left) && (pt.x < item->rect.right) &&
542             (pt.y >= item->rect.top) && (pt.y < item->rect.bottom))
543         {
544             if (pos) *pos = i;
545             return item;
546         }
547     }
548     return NULL;
549 }
550
551
552 /***********************************************************************
553  *           MENU_FindItemByKey
554  *
555  * Find the menu item selected by a key press.
556  * Return item id, -1 if none, -2 if we should close the menu.
557  */
558 static UINT32 MENU_FindItemByKey( HWND32 hwndOwner, HMENU32 hmenu, 
559                                   UINT32 key, BOOL32 forceMenuChar )
560 {
561     TRACE(menu,"\tlooking for '%c' in [%04x]\n", (char)key, (UINT16)hmenu );
562
563     if (!IsMenu32( hmenu )) 
564     {
565         WND* w = WIN_FindWndPtr(hwndOwner);
566         hmenu = GetSubMenu32(w->hSysMenu, 0);
567     }
568
569     if (hmenu)
570     {
571         POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
572         MENUITEM *item = menu->items;
573         LONG menuchar;
574
575         if( !forceMenuChar )
576         {
577              UINT32 i;
578
579              key = toupper(key);
580              for (i = 0; i < menu->nItems; i++, item++)
581              {
582                 if (item->text && (IS_STRING_ITEM(item->fType)))
583                 {
584                     char *p = item->text - 2;
585                     do
586                     {
587                         p = strchr (p + 2, '&');
588                     }
589                     while (p != NULL && p [1] == '&');
590                     if (p && (toupper(p[1]) == key)) return i;
591                 }
592              }
593         }
594         menuchar = SendMessage32A( hwndOwner, WM_MENUCHAR, 
595                                    MAKEWPARAM( key, menu->wFlags ), hmenu );
596         if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
597         if (HIWORD(menuchar) == 1) return (UINT32)(-2);
598     }
599     return (UINT32)(-1);
600 }
601 /***********************************************************************
602  *           MENU_LoadMagicItem
603  *
604  * Load the bitmap associated with the magic menu item and its style
605  */
606
607 static HBITMAP32 MENU_LoadMagicItem(UINT32 id,BOOL32 hilite)
608 {
609     // Magic menu item id's section
610     // These magic id's are used by windows to insert "standard" mdi
611     // buttons (minimize,restore,close) on menu. Under windows,
612     // these magic id's make sure the right things appear when those
613     // bitmap buttons are pressed/selected/released.
614
615     switch(id)
616     {
617     case MAGIC_REDUCE   : return (hilite ? hBmpMinimizeD : hBmpMinimize);
618     case MAGIC_RESTORE  : return (hilite ? hBmpMaximizeD: hBmpMaximize);
619     case MAGIC_CLOSE    : return (hilite ? hBmpClose : hBmpClose);
620     default : return 0;
621     }
622
623 }
624
625 /***********************************************************************
626  *           MENU_CalcItemSize
627  *
628  * Calculate the size of the menu item and store it in lpitem->rect.
629  */
630 static void MENU_CalcItemSize( HDC32 hdc, MENUITEM *lpitem, HWND32 hwndOwner,
631                                INT32 orgX, INT32 orgY, BOOL32 menuBar )
632 {
633     DWORD dwSize;
634     char *p;
635
636     TRACE(menu, "HDC 0x%x at (%d,%d)\n",
637                  hdc, orgX, orgY);
638     debug_print_menuitem("MENU_CalcItemSize: menuitem:", lpitem, 
639                          (menuBar ? " (MenuBar)" : ""));
640
641     SetRect32( &lpitem->rect, orgX, orgY, orgX, orgY );
642
643     if (lpitem->fType & MF_OWNERDRAW)
644     {
645         MEASUREITEMSTRUCT32 mis;
646         mis.CtlType    = ODT_MENU;
647         mis.itemID     = lpitem->wID;
648         mis.itemData   = (DWORD)lpitem->text;
649         mis.itemHeight = 16;
650         mis.itemWidth  = 30;
651         SendMessage32A( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)&mis );
652         lpitem->rect.bottom += mis.itemHeight;
653         lpitem->rect.right  += mis.itemWidth;
654         TRACE(menu, "%08x %dx%d\n",
655                      lpitem->wID, mis.itemWidth, mis.itemHeight);
656         return;
657     } 
658
659     if (lpitem->fType & MF_SEPARATOR)
660     {
661         lpitem->rect.bottom += SEPARATOR_HEIGHT;
662         return;
663     }
664
665     if (!menuBar)
666     {
667         lpitem->rect.right += 2 * check_bitmap_width;
668         if (lpitem->fType & MF_POPUP)
669             lpitem->rect.right += arrow_bitmap_width;
670     }
671
672     if (lpitem->fType & MF_BITMAP)
673     {
674         BITMAP32 bm;
675         HBITMAP32 resBmp = 0;
676
677         // Check if there is a magic menu item associated with this item
678         if((LOWORD((int)lpitem->text))<6)
679         {
680             resBmp = MENU_LoadMagicItem((int)lpitem->text,
681                                         (lpitem->fType & MF_HILITE));
682         }
683         else
684             resBmp = (HBITMAP32)lpitem->text;
685
686         if (GetObject32A(resBmp, sizeof(bm), &bm ))
687         {
688             lpitem->rect.right  += bm.bmWidth;
689             lpitem->rect.bottom += bm.bmHeight;
690
691         }
692         
693         return;
694         
695     }
696     
697     /* If we get here, then it must be a text item */
698
699     if (IS_STRING_ITEM( lpitem->fType ))
700     {
701         dwSize = GetTextExtent( hdc, lpitem->text, strlen(lpitem->text) );
702         lpitem->rect.right  += LOWORD(dwSize);
703         if (TWEAK_WineLook == WIN31_LOOK)
704             lpitem->rect.bottom += MAX( HIWORD(dwSize), SYSMETRICS_CYMENU );
705         else
706             lpitem->rect.bottom += MAX (HIWORD(dwSize), sysMetrics[SM_CYMENU]- 1);
707         lpitem->xTab = 0;
708
709         if (menuBar) lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
710         else if ((p = strchr( lpitem->text, '\t' )) != NULL)
711         {
712             /* Item contains a tab (only meaningful in popup menus) */
713             lpitem->xTab = check_bitmap_width + MENU_TAB_SPACE + 
714                 LOWORD( GetTextExtent( hdc, lpitem->text,
715                                        (int)(p - lpitem->text) ));
716             lpitem->rect.right += MENU_TAB_SPACE;
717         }
718         else
719         {
720             if (strchr( lpitem->text, '\b' ))
721                 lpitem->rect.right += MENU_TAB_SPACE;
722             lpitem->xTab = lpitem->rect.right - check_bitmap_width 
723                            - arrow_bitmap_width;
724         }
725     }
726 }
727
728
729 /***********************************************************************
730  *           MENU_PopupMenuCalcSize
731  *
732  * Calculate the size of a popup menu.
733  */
734 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, HWND32 hwndOwner )
735 {
736     MENUITEM *lpitem;
737     HDC32 hdc;
738     int start, i;
739     int orgX, orgY, maxX, maxTab, maxTabWidth;
740
741     lppop->Width = lppop->Height = 0;
742     if (lppop->nItems == 0) return;
743     hdc = GetDC32( 0 );
744     start = 0;
745     maxX = SYSMETRICS_CXBORDER;
746     while (start < lppop->nItems)
747     {
748         lpitem = &lppop->items[start];
749         orgX = maxX;
750         orgY = SYSMETRICS_CYBORDER;
751
752         maxTab = maxTabWidth = 0;
753
754           /* Parse items until column break or end of menu */
755         for (i = start; i < lppop->nItems; i++, lpitem++)
756         {
757             if ((i != start) &&
758                 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
759
760             if (TWEAK_WineLook > WIN31_LOOK)
761                 ++orgY;
762
763             MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, FALSE );
764
765             if (lpitem->fType & MF_MENUBARBREAK) orgX++;
766             maxX = MAX( maxX, lpitem->rect.right );
767             orgY = lpitem->rect.bottom;
768             if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
769             {
770                 maxTab = MAX( maxTab, lpitem->xTab );
771                 maxTabWidth = MAX(maxTabWidth,lpitem->rect.right-lpitem->xTab);
772             }
773         }
774
775           /* Finish the column (set all items to the largest width found) */
776         maxX = MAX( maxX, maxTab + maxTabWidth );
777         for (lpitem = &lppop->items[start]; start < i; start++, lpitem++)
778         {
779             lpitem->rect.right = maxX;
780             if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
781                 lpitem->xTab = maxTab;
782         }
783         lppop->Height = MAX( lppop->Height, orgY );
784     }
785
786     if(TWEAK_WineLook > WIN31_LOOK)
787         lppop->Height++;
788
789     lppop->Width  = maxX;
790     ReleaseDC32( 0, hdc );
791 }
792
793
794 /***********************************************************************
795  *           MENU_MenuBarCalcSize
796  *
797  * FIXME: Word 6 implements its own MDI and its own 'close window' bitmap
798  * height is off by 1 pixel which causes lengthy window relocations when
799  * active document window is maximized/restored.
800  *
801  * Calculate the size of the menu bar.
802  */
803 static void MENU_MenuBarCalcSize( HDC32 hdc, LPRECT32 lprect,
804                                   LPPOPUPMENU lppop, HWND32 hwndOwner )
805 {
806     MENUITEM *lpitem;
807     int start, i, orgX, orgY, maxY, helpPos;
808
809     if ((lprect == NULL) || (lppop == NULL)) return;
810     if (lppop->nItems == 0) return;
811     TRACE(menu,"left=%d top=%d right=%d bottom=%d\n", 
812                  lprect->left, lprect->top, lprect->right, lprect->bottom);
813     lppop->Width  = lprect->right - lprect->left;
814     lppop->Height = 0;
815     maxY = lprect->top;
816     start = 0;
817     helpPos = -1;
818     while (start < lppop->nItems)
819     {
820         lpitem = &lppop->items[start];
821         orgX = lprect->left;
822         orgY = maxY;
823
824           /* Parse items until line break or end of menu */
825         for (i = start; i < lppop->nItems; i++, lpitem++)
826         {
827             if ((helpPos == -1) && (lpitem->fType & MF_HELP)) helpPos = i;
828             if ((i != start) &&
829                 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
830
831             TRACE(menu, "calling MENU_CalcItemSize org=(%d, %d)\n", 
832                          orgX, orgY );
833             debug_print_menuitem ("  item: ", lpitem, "");
834             MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE );
835
836             if (lpitem->rect.right > lprect->right)
837             {
838                 if (i != start) break;
839                 else lpitem->rect.right = lprect->right;
840             }
841             maxY = MAX( maxY, lpitem->rect.bottom );
842             orgX = lpitem->rect.right;
843         }
844
845           /* Finish the line (set all items to the largest height found) */
846         while (start < i) lppop->items[start++].rect.bottom = maxY;
847     }
848
849     lprect->bottom = maxY;
850     lppop->Height = lprect->bottom - lprect->top;
851
852       /* Flush right all items between the MF_HELP and the last item */
853       /* (if several lines, only move the last line) */
854     if (helpPos != -1)
855     {
856         lpitem = &lppop->items[lppop->nItems-1];
857         orgY = lpitem->rect.top;
858         orgX = lprect->right;
859         for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--)
860         {
861             if (lpitem->rect.top != orgY) break;    /* Other line */
862             if (lpitem->rect.right >= orgX) break;  /* Too far right already */
863             lpitem->rect.left += orgX - lpitem->rect.right;
864             lpitem->rect.right = orgX;
865             orgX = lpitem->rect.left;
866         }
867     }
868 }
869
870 /***********************************************************************
871  *           MENU_DrawMenuItem
872  *
873  * Draw a single menu item.
874  */
875 static void MENU_DrawMenuItem( HWND32 hwnd, HDC32 hdc, MENUITEM *lpitem,
876                                UINT32 height, BOOL32 menuBar, UINT32 odaction )
877 {
878     RECT32 rect;
879
880     debug_print_menuitem("MENU_DrawMenuItem: ", lpitem, "");
881
882     if (lpitem->fType & MF_SYSMENU)
883     {
884         if( !IsIconic32(hwnd) ) {
885             if (TWEAK_WineLook > WIN31_LOOK)
886                 NC_DrawSysButton95( hwnd, hdc,
887                                     lpitem->fState &
888                                     (MF_HILITE | MF_MOUSESELECT) );
889             else
890                 NC_DrawSysButton( hwnd, hdc, 
891                                   lpitem->fState &
892                                   (MF_HILITE | MF_MOUSESELECT) );
893         }
894
895         return;
896     }
897
898     if (lpitem->fType & MF_OWNERDRAW)
899     {
900         DRAWITEMSTRUCT32 dis;
901
902         dis.CtlType   = ODT_MENU;
903         dis.itemID    = lpitem->wID;
904         dis.itemData  = (DWORD)lpitem->text;
905         dis.itemState = 0;
906         if (lpitem->fState & MF_CHECKED) dis.itemState |= ODS_CHECKED;
907         if (lpitem->fState & MF_GRAYED)  dis.itemState |= ODS_GRAYED;
908         if (lpitem->fState & MF_HILITE)  dis.itemState |= ODS_SELECTED;
909         dis.itemAction = odaction; /* ODA_DRAWENTIRE | ODA_SELECT | ODA_FOCUS; */
910         dis.hwndItem   = hwnd;
911         dis.hDC        = hdc;
912         dis.rcItem     = lpitem->rect;
913         TRACE(menu, "Ownerdraw: itemID=%d, itemState=%d, itemAction=%d, "
914               "hwndItem=%04x, hdc=%04x, rcItem={%d,%d,%d,%d}\n",dis.itemID,
915               dis.itemState, dis.itemAction, dis.hwndItem, dis.hDC,
916               dis.rcItem.left, dis.rcItem.top, dis.rcItem.right,
917               dis.rcItem.bottom );
918         SendMessage32A( GetWindow32(hwnd,GW_OWNER), WM_DRAWITEM, 0, (LPARAM)&dis );
919         return;
920     }
921
922     if (menuBar && (lpitem->fType & MF_SEPARATOR)) return;
923     rect = lpitem->rect;
924
925     /* Draw the background */
926     if (TWEAK_WineLook > WIN31_LOOK) {
927         rect.left += 2;
928         rect.right -= 2;
929
930         /*
931         if(menuBar) {
932             --rect.left;
933             ++rect.bottom;
934             --rect.top;
935         }
936         InflateRect32( &rect, -1, -1 );
937         */
938     }
939
940      if ((lpitem->fState & MF_HILITE) && !(lpitem->fType & MF_BITMAP) )
941         FillRect32( hdc, &rect, GetSysColorBrush32(COLOR_HIGHLIGHT) );
942     else
943         FillRect32( hdc, &rect, GetSysColorBrush32(COLOR_MENU) );
944
945     SetBkMode32( hdc, TRANSPARENT );
946
947       /* Draw the separator bar (if any) */
948
949     if (!menuBar && (lpitem->fType & MF_MENUBARBREAK))
950     {
951         /* vertical separator */
952         if (TWEAK_WineLook > WIN31_LOOK) {
953             RECT32 rc = rect;
954             rc.top = 3;
955             rc.bottom = height - 3;
956             DrawEdge32 (hdc, &rc, EDGE_ETCHED, BF_LEFT);
957         }
958         else {
959             SelectObject32( hdc, GetSysColorPen32(COLOR_WINDOWFRAME) );
960             MoveTo( hdc, rect.left, 0 );
961             LineTo32( hdc, rect.left, height );
962         }
963     }
964     if (lpitem->fType & MF_SEPARATOR)
965     {
966         /* horizontal separator */
967         if (TWEAK_WineLook > WIN31_LOOK) {
968             RECT32 rc = rect;
969             rc.left++;
970             rc.right--;
971             rc.top += SEPARATOR_HEIGHT / 2;
972             DrawEdge32 (hdc, &rc, EDGE_ETCHED, BF_TOP);
973         }
974         else {
975             SelectObject32( hdc, GetSysColorPen32(COLOR_WINDOWFRAME) );
976             MoveTo( hdc, rect.left, rect.top + SEPARATOR_HEIGHT/2 );
977             LineTo32( hdc, rect.right, rect.top + SEPARATOR_HEIGHT/2 );
978         }
979
980         return;
981     }
982
983       /* Setup colors */
984
985      if ((lpitem->fState & MF_HILITE) && !(lpitem->fType & MF_BITMAP) )
986     {
987         if (lpitem->fState & MF_GRAYED)
988             SetTextColor32( hdc, GetSysColor32( COLOR_GRAYTEXT ) );
989         else
990             SetTextColor32( hdc, GetSysColor32( COLOR_HIGHLIGHTTEXT ) );
991         SetBkColor32( hdc, GetSysColor32( COLOR_HIGHLIGHT ) );
992     }
993     else
994     {
995         if (lpitem->fState & MF_GRAYED)
996             SetTextColor32( hdc, GetSysColor32( COLOR_GRAYTEXT ) );
997         else
998             SetTextColor32( hdc, GetSysColor32( COLOR_MENUTEXT ) );
999         SetBkColor32( hdc, GetSysColor32( COLOR_MENU ) );
1000     }
1001
1002     if (!menuBar)
1003     {
1004         INT32   y = rect.top + rect.bottom;
1005
1006           /* Draw the check mark
1007            *
1008            * FIXME:
1009            * Custom checkmark bitmaps are monochrome but not always 1bpp. 
1010            */
1011
1012         if (lpitem->fState & MF_CHECKED)
1013         {
1014             HBITMAP32 bm =
1015                  lpitem->hCheckBit ? lpitem->hCheckBit :
1016                  ((lpitem->fType & MFT_RADIOCHECK)
1017                   ? hStdRadioCheck : hStdCheck);
1018             HDC32 hdcMem = CreateCompatibleDC32( hdc );
1019
1020             SelectObject32( hdcMem, bm );
1021             BitBlt32( hdc, rect.left, (y - check_bitmap_height) / 2,
1022                       check_bitmap_width, check_bitmap_height,
1023                       hdcMem, 0, 0, SRCCOPY );
1024             DeleteDC32( hdcMem );
1025         } else if (lpitem->hUnCheckBit) {
1026             HDC32 hdcMem = CreateCompatibleDC32( hdc );
1027
1028             SelectObject32( hdcMem, lpitem->hUnCheckBit );
1029             BitBlt32( hdc, rect.left, (y - check_bitmap_height) / 2,
1030                       check_bitmap_width, check_bitmap_height,
1031                       hdcMem, 0, 0, SRCCOPY );
1032             DeleteDC32( hdcMem );
1033         }
1034
1035           /* Draw the popup-menu arrow */
1036
1037         if (lpitem->fType & MF_POPUP)
1038         {
1039             HDC32 hdcMem = CreateCompatibleDC32( hdc );
1040
1041             SelectObject32( hdcMem, hStdMnArrow );
1042             BitBlt32( hdc, rect.right - arrow_bitmap_width - 1,
1043                       (y - arrow_bitmap_height) / 2,
1044                       arrow_bitmap_width, arrow_bitmap_height,
1045                       hdcMem, 0, 0, SRCCOPY );
1046             DeleteDC32( hdcMem );
1047         }
1048
1049         rect.left += check_bitmap_width;
1050         rect.right -= arrow_bitmap_width;
1051     }
1052
1053       /* Draw the item text or bitmap */
1054
1055     if (lpitem->fType & MF_BITMAP)
1056     {
1057         HBITMAP32 resBmp = 0;
1058
1059         HDC32 hdcMem = CreateCompatibleDC32( hdc );
1060
1061         // Check if there is a magic menu item associated with this item
1062         // and load the appropriate bitmap
1063         if((LOWORD((int)lpitem->text)) < 6)
1064         {
1065             resBmp = MENU_LoadMagicItem((int)lpitem->text,
1066                                         (lpitem->fState & MF_HILITE));
1067         }
1068         else
1069             resBmp = (HBITMAP32)lpitem->text;
1070         
1071         SelectObject32(hdcMem,resBmp );
1072         BitBlt32( hdc, rect.left, rect.top+3, rect.right - rect.left+3,
1073                   rect.bottom - rect.top, hdcMem, 0, 0, SRCCOPY );
1074         DeleteDC32( hdcMem );
1075         
1076         return;
1077
1078     }
1079     /* No bitmap - process text if present */
1080     else if (IS_STRING_ITEM(lpitem->fType))
1081     {
1082         register int i;
1083
1084         if (menuBar)
1085         {
1086             rect.left += MENU_BAR_ITEMS_SPACE / 2;
1087             rect.right -= MENU_BAR_ITEMS_SPACE / 2;
1088             i = strlen( lpitem->text );
1089         }
1090         else
1091         {
1092             for (i = 0; lpitem->text[i]; i++)
1093                 if ((lpitem->text[i] == '\t') || (lpitem->text[i] == '\b'))
1094                     break;
1095         }
1096
1097         if((TWEAK_WineLook == WIN31_LOOK) || !(lpitem->fState & MF_GRAYED)) {
1098             DrawText32A( hdc, lpitem->text, i, &rect,
1099                          DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1100         }
1101         else {
1102             if (!(lpitem->fState & MF_HILITE))
1103             {
1104                 ++rect.left;
1105                 ++rect.top;
1106                 ++rect.right;
1107                 ++rect.bottom;
1108                 SetTextColor32(hdc, RGB(0xff, 0xff, 0xff));
1109                 DrawText32A( hdc, lpitem->text, i, &rect,
1110                              DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1111                 --rect.left;
1112                 --rect.top;
1113                 --rect.right;
1114                 --rect.bottom;
1115             }
1116             SetTextColor32(hdc, RGB(0x80, 0x80, 0x80));
1117             DrawText32A( hdc, lpitem->text, i, &rect,
1118                          DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1119         }
1120
1121         if (lpitem->text[i])  /* There's a tab or flush-right char */
1122         {
1123             if (lpitem->text[i] == '\t')
1124             {
1125                 rect.left = lpitem->xTab;
1126                 DrawText32A( hdc, lpitem->text + i + 1, -1, &rect,
1127                              DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1128             }
1129             else DrawText32A( hdc, lpitem->text + i + 1, -1, &rect,
1130                               DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
1131         }
1132     }
1133 }
1134
1135
1136 /***********************************************************************
1137  *           MENU_DrawPopupMenu
1138  *
1139  * Paint a popup menu.
1140  */
1141 static void MENU_DrawPopupMenu( HWND32 hwnd, HDC32 hdc, HMENU32 hmenu )
1142 {
1143     HBRUSH32 hPrevBrush = 0;
1144     RECT32 rect;
1145
1146     GetClientRect32( hwnd, &rect );
1147
1148 /*    if(!TWEAK_Win95Look) { */
1149         rect.bottom -= POPUP_YSHADE * SYSMETRICS_CYBORDER;
1150         rect.right -= POPUP_XSHADE * SYSMETRICS_CXBORDER;
1151 /*    } */
1152
1153     if((hPrevBrush = SelectObject32( hdc, GetSysColorBrush32(COLOR_MENU) )))
1154     {
1155         HPEN32 hPrevPen;
1156         
1157         Rectangle32( hdc, rect.left, rect.top, rect.right, rect.bottom );
1158
1159         hPrevPen = SelectObject32( hdc, GetStockObject32( NULL_PEN ) );
1160         if( hPrevPen )
1161         {
1162             INT32 ropPrev, i;
1163             POPUPMENU *menu;
1164
1165             /* draw 3-d shade */
1166             if(TWEAK_WineLook == WIN31_LOOK) {
1167                 SelectObject32( hdc, hShadeBrush );
1168                 SetBkMode32( hdc, TRANSPARENT );
1169                 ropPrev = SetROP232( hdc, R2_MASKPEN );
1170
1171                 i = rect.right;         /* why SetBrushOrg() doesn't? */
1172                 PatBlt32( hdc, i & 0xfffffffe,
1173                           rect.top + POPUP_YSHADE*SYSMETRICS_CYBORDER, 
1174                           i%2 + POPUP_XSHADE*SYSMETRICS_CXBORDER,
1175                           rect.bottom - rect.top, 0x00a000c9 );
1176                 i = rect.bottom;
1177                 PatBlt32( hdc, rect.left + POPUP_XSHADE*SYSMETRICS_CXBORDER,
1178                           i & 0xfffffffe,rect.right - rect.left,
1179                           i%2 + POPUP_YSHADE*SYSMETRICS_CYBORDER, 0x00a000c9 );
1180                 SelectObject32( hdc, hPrevPen );
1181                 SelectObject32( hdc, hPrevBrush );
1182                 SetROP232( hdc, ropPrev );
1183             }
1184             else
1185                 DrawEdge32 (hdc, &rect, EDGE_RAISED, BF_RECT);
1186
1187             /* draw menu items */
1188
1189             menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1190             if (menu && menu->nItems)
1191             {
1192                 MENUITEM *item;
1193                 UINT32 u;
1194
1195                 for (u = menu->nItems, item = menu->items; u > 0; u--, item++)
1196                     MENU_DrawMenuItem( hwnd, hdc, item, menu->Height, FALSE,
1197                                        ODA_DRAWENTIRE );
1198
1199             }
1200         } else SelectObject32( hdc, hPrevBrush );
1201     }
1202 }
1203
1204
1205 /***********************************************************************
1206  *           MENU_DrawMenuBar
1207  *
1208  * Paint a menu bar. Returns the height of the menu bar.
1209  */
1210 UINT32 MENU_DrawMenuBar( HDC32 hDC, LPRECT32 lprect, HWND32 hwnd,
1211                          BOOL32 suppress_draw)
1212 {
1213     LPPOPUPMENU lppop;
1214     UINT32 i;
1215     WND *wndPtr = WIN_FindWndPtr( hwnd );
1216     
1217     lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR( (HMENU16)wndPtr->wIDmenu );
1218     if (lppop == NULL || lprect == NULL) return SYSMETRICS_CYMENU;
1219     TRACE(menu,"(%04x, %p, %p); !\n", 
1220                  hDC, lprect, lppop);
1221     if (lppop->Height == 0) MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
1222     lprect->bottom = lprect->top + lppop->Height;
1223     if (suppress_draw) return lppop->Height;
1224     
1225     FillRect32(hDC, lprect, GetSysColorBrush32(COLOR_MENU) );
1226
1227     if (TWEAK_WineLook == WIN31_LOOK) {
1228         SelectObject32( hDC, GetSysColorPen32(COLOR_WINDOWFRAME) );
1229         MoveTo( hDC, lprect->left, lprect->bottom );
1230         LineTo32( hDC, lprect->right, lprect->bottom );
1231     }
1232     else {
1233         SelectObject32( hDC, GetSysColorPen32(COLOR_3DFACE));
1234         MoveTo( hDC, lprect->left, lprect->bottom );
1235         LineTo32( hDC, lprect->right, lprect->bottom );
1236     }
1237
1238     if (lppop->nItems == 0) return SYSMETRICS_CYMENU;
1239     for (i = 0; i < lppop->nItems; i++)
1240     {
1241         MENU_DrawMenuItem( hwnd, hDC, &lppop->items[i], lppop->Height, TRUE,
1242                            ODA_DRAWENTIRE );
1243     }
1244     return lppop->Height;
1245
1246
1247
1248 /***********************************************************************
1249  *           MENU_PatchResidentPopup
1250  */
1251 BOOL32 MENU_PatchResidentPopup( HQUEUE16 checkQueue, WND* checkWnd )
1252 {
1253     if( pTopPopupWnd )
1254     {
1255         HTASK16 hTask = 0;
1256
1257         TRACE(menu,"patching resident popup: %04x %04x [%04x %04x]\n", 
1258                 checkQueue, checkWnd ? checkWnd->hwndSelf : 0, pTopPopupWnd->hmemTaskQ, 
1259                 pTopPopupWnd->owner ? pTopPopupWnd->owner->hwndSelf : 0);
1260
1261         switch( checkQueue )
1262         {
1263             case 0: /* checkWnd is the new popup owner */
1264                  if( checkWnd )
1265                  {
1266                      pTopPopupWnd->owner = checkWnd;
1267                      if( pTopPopupWnd->hmemTaskQ != checkWnd->hmemTaskQ )
1268                          hTask = QUEUE_GetQueueTask( checkWnd->hmemTaskQ );
1269                  }
1270                  break;
1271
1272             case 0xFFFF: /* checkWnd is destroyed */
1273                  if( pTopPopupWnd->owner == checkWnd )
1274                      pTopPopupWnd->owner = NULL;
1275                  return TRUE; 
1276
1277             default: /* checkQueue is exiting */
1278                  if( pTopPopupWnd->hmemTaskQ == checkQueue )
1279                  {
1280                      hTask = QUEUE_GetQueueTask( pTopPopupWnd->hmemTaskQ );
1281                      hTask = TASK_GetNextTask( hTask );
1282                  }
1283                  break;
1284         }
1285
1286         if( hTask )
1287         {
1288             TDB* task = (TDB*)GlobalLock16( hTask );
1289             if( task )
1290             {
1291                 pTopPopupWnd->hInstance = task->hInstance;
1292                 pTopPopupWnd->hmemTaskQ = task->hQueue;
1293                 return TRUE;
1294             } 
1295             else WARN(menu,"failed to patch resident popup.\n");
1296         } 
1297     }
1298     return FALSE;
1299 }
1300
1301 /***********************************************************************
1302  *           MENU_ShowPopup
1303  *
1304  * Display a popup menu.
1305  */
1306 static BOOL32 MENU_ShowPopup( HWND32 hwndOwner, HMENU32 hmenu, UINT32 id,
1307                               INT32 x, INT32 y, INT32 xanchor, INT32 yanchor )
1308 {
1309     POPUPMENU   *menu;
1310     WND         *wndOwner = NULL;
1311
1312     if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return FALSE;
1313     if (menu->FocusedItem != NO_SELECTED_ITEM)
1314     {
1315         menu->items[menu->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1316         menu->FocusedItem = NO_SELECTED_ITEM;
1317     }
1318
1319     if( (wndOwner = WIN_FindWndPtr( hwndOwner )) )
1320     {
1321         UINT32  width, height;
1322
1323         MENU_PopupMenuCalcSize( menu, hwndOwner );
1324
1325         /* adjust popup menu pos so that it fits within the desktop */
1326
1327         width = menu->Width + SYSMETRICS_CXBORDER;
1328         height = menu->Height + SYSMETRICS_CYBORDER; 
1329
1330         if( x + width > SYSMETRICS_CXSCREEN )
1331         {
1332             if( xanchor )
1333                 x -= width - xanchor;
1334             if( x + width > SYSMETRICS_CXSCREEN)
1335                 x = SYSMETRICS_CXSCREEN - width;
1336         }
1337         if( x < 0 ) x = 0;
1338
1339         if( y + height > SYSMETRICS_CYSCREEN )
1340         { 
1341             if( yanchor )
1342                 y -= height + yanchor;
1343             if( y + height > SYSMETRICS_CYSCREEN )
1344                 y = SYSMETRICS_CYSCREEN - height;
1345         }
1346         if( y < 0 ) y = 0;
1347
1348         width += POPUP_XSHADE * SYSMETRICS_CXBORDER;    /* add space for shading */
1349         height += POPUP_YSHADE * SYSMETRICS_CYBORDER;
1350
1351         /* NOTE: In Windows, top menu popup is not owned. */
1352         if (!pTopPopupWnd)      /* create top level popup menu window */
1353         {
1354             assert( uSubPWndLevel == 0 );
1355
1356             pTopPopupWnd = WIN_FindWndPtr(CreateWindow32A( POPUPMENU_CLASS_ATOM, NULL,
1357                                           WS_POPUP, x, y, width, height,
1358                                           hwndOwner, 0, wndOwner->hInstance,
1359                                           (LPVOID)hmenu ));
1360             if (!pTopPopupWnd) return FALSE;
1361             menu->hWnd = pTopPopupWnd->hwndSelf;
1362         } 
1363         else
1364             if( uSubPWndLevel )
1365             {
1366                 /* create a new window for the submenu */
1367
1368                 menu->hWnd = CreateWindow32A( POPUPMENU_CLASS_ATOM, NULL,
1369                                           WS_POPUP, x, y, width, height,
1370                                           menu->hWnd, 0, wndOwner->hInstance,
1371                                           (LPVOID)hmenu );
1372                 if( !menu->hWnd ) return FALSE;
1373             }
1374             else /* top level popup menu window already exists */
1375             {
1376                 menu->hWnd = pTopPopupWnd->hwndSelf;
1377
1378                 MENU_PatchResidentPopup( 0, wndOwner );
1379                 SendMessage16( pTopPopupWnd->hwndSelf, MM_SETMENUHANDLE, (WPARAM16)hmenu, 0L);  
1380
1381                 /* adjust its size */
1382
1383                 SetWindowPos32( menu->hWnd, 0, x, y, width, height,
1384                                 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW);
1385             }
1386
1387         uSubPWndLevel++;        /* menu level counter */
1388
1389       /* Display the window */
1390
1391         SetWindowPos32( menu->hWnd, HWND_TOP, 0, 0, 0, 0,
1392                         SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1393         UpdateWindow32( menu->hWnd );
1394         return TRUE;
1395     }
1396     return FALSE;
1397 }
1398
1399
1400 /***********************************************************************
1401  *           MENU_SelectItem
1402  */
1403 static void MENU_SelectItem( HWND32 hwndOwner, HMENU32 hmenu, UINT32 wIndex,
1404                              BOOL32 sendMenuSelect )
1405 {
1406     LPPOPUPMENU lppop;
1407     HDC32 hdc;
1408
1409     lppop = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1410     if (!lppop->nItems) return;
1411
1412     if ((wIndex != NO_SELECTED_ITEM) && 
1413         (lppop->items[wIndex].fType & MF_SEPARATOR))
1414         wIndex = NO_SELECTED_ITEM;
1415
1416     if (lppop->FocusedItem == wIndex) return;
1417     if (lppop->wFlags & MF_POPUP) hdc = GetDC32( lppop->hWnd );
1418     else hdc = GetDCEx32( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1419
1420       /* Clear previous highlighted item */
1421     if (lppop->FocusedItem != NO_SELECTED_ITEM) 
1422     {
1423         lppop->items[lppop->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1424         MENU_DrawMenuItem(lppop->hWnd,hdc,&lppop->items[lppop->FocusedItem],
1425                           lppop->Height, !(lppop->wFlags & MF_POPUP),
1426                           ODA_SELECT );
1427     }
1428
1429       /* Highlight new item (if any) */
1430     lppop->FocusedItem = wIndex;
1431     if (lppop->FocusedItem != NO_SELECTED_ITEM) 
1432     {
1433         lppop->items[lppop->FocusedItem].fState |= MF_HILITE;
1434         MENU_DrawMenuItem( lppop->hWnd, hdc, &lppop->items[lppop->FocusedItem],
1435                            lppop->Height, !(lppop->wFlags & MF_POPUP),
1436                            ODA_SELECT );
1437         if (sendMenuSelect)
1438         {
1439             MENUITEM *ip = &lppop->items[lppop->FocusedItem];
1440             SendMessage16( hwndOwner, WM_MENUSELECT, ip->wID,
1441                            MAKELONG(ip->fType | (ip->fState | MF_MOUSESELECT),
1442                                     hmenu) );
1443         }
1444     }
1445     else if (sendMenuSelect) {
1446         SendMessage16( hwndOwner, WM_MENUSELECT, hmenu,
1447                        MAKELONG( lppop->wFlags | MF_MOUSESELECT, hmenu ) ); 
1448     }
1449     ReleaseDC32( lppop->hWnd, hdc );
1450 }
1451
1452
1453 /***********************************************************************
1454  *           MENU_MoveSelection
1455  *
1456  * Moves currently selected item according to the offset parameter.
1457  * If there is no selection then it should select the last item if
1458  * offset is ITEM_PREV or the first item if offset is ITEM_NEXT.
1459  */
1460 static void MENU_MoveSelection( HWND32 hwndOwner, HMENU32 hmenu, INT32 offset )
1461 {
1462     INT32 i;
1463     POPUPMENU *menu;
1464
1465     menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1466     if (!menu->items) return;
1467
1468     if ( menu->FocusedItem != NO_SELECTED_ITEM )
1469     {
1470         if( menu->nItems == 1 ) return; else
1471         for (i = menu->FocusedItem + offset ; i >= 0 && i < menu->nItems 
1472                                             ; i += offset)
1473             if (!(menu->items[i].fType & MF_SEPARATOR))
1474             {
1475                 MENU_SelectItem( hwndOwner, hmenu, i, TRUE );
1476                 return;
1477             }
1478     }
1479
1480     for ( i = (offset > 0) ? 0 : menu->nItems - 1; 
1481                   i >= 0 && i < menu->nItems ; i += offset)
1482         if (!(menu->items[i].fType & MF_SEPARATOR))
1483         {
1484             MENU_SelectItem( hwndOwner, hmenu, i, TRUE );
1485             return;
1486         }
1487 }
1488
1489
1490 /**********************************************************************
1491  *         MENU_SetItemData
1492  *
1493  * Set an item flags, id and text ptr. Called by InsertMenu() and
1494  * ModifyMenu().
1495  */
1496 static BOOL32 MENU_SetItemData( MENUITEM *item, UINT32 flags, UINT32 id,
1497                                 LPCSTR str )
1498 {
1499     LPSTR prevText = IS_STRING_ITEM(item->fType) ? item->text : NULL;
1500
1501     debug_print_menuitem("MENU_SetItemData from: ", item, "");
1502
1503     if (IS_STRING_ITEM(flags))
1504     {
1505         if (!str || !*str)
1506         {
1507             flags |= MF_SEPARATOR;
1508             item->text = NULL;
1509         }
1510         else
1511         {
1512             LPSTR text;
1513             /* Item beginning with a backspace is a help item */
1514             if (*str == '\b')
1515             {
1516                 flags |= MF_HELP;
1517                 str++;
1518             }
1519             if (!(text = HEAP_strdupA( SystemHeap, 0, str ))) return FALSE;
1520             item->text = text;
1521         }
1522     }
1523     else if (flags & MF_BITMAP) item->text = (LPSTR)(HBITMAP32)LOWORD(str);
1524     else item->text = NULL;
1525
1526     if (flags & MF_OWNERDRAW) 
1527         item->dwItemData = (DWORD)str;
1528     else
1529         item->dwItemData = 0;
1530
1531     if ((item->fType & MF_POPUP) && (flags & MF_POPUP) && (item->hSubMenu != id) )
1532         DestroyMenu32( item->hSubMenu );   /* ModifyMenu() spec */
1533
1534     if (flags & MF_POPUP)
1535     {
1536         POPUPMENU *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR((UINT16)id);
1537         if (IS_A_MENU(menu)) menu->wFlags |= MF_POPUP;
1538         else
1539         {
1540             item->wID = 0;
1541             item->hSubMenu = 0;
1542             item->fType = 0;
1543             item->fState = 0;
1544             return FALSE;
1545         }
1546     } 
1547
1548     item->wID = id;
1549     if (flags & MF_POPUP)
1550       item->hSubMenu = id;
1551
1552     if ((item->fType & MF_POPUP) && !(flags & MF_POPUP) )
1553       flags |= MF_POPUP; /* keep popup */
1554
1555     item->fType = flags & TYPE_MASK;
1556     item->fState = (flags & STATE_MASK) &
1557         ~(MF_HILITE | MF_MOUSESELECT | MF_BYPOSITION);
1558
1559
1560     /* Don't call SetRectEmpty here! */
1561
1562
1563     if (prevText) HeapFree( SystemHeap, 0, prevText );
1564
1565     debug_print_menuitem("MENU_SetItemData to  : ", item, "");
1566     return TRUE;
1567 }
1568
1569
1570 /**********************************************************************
1571  *         MENU_InsertItem
1572  *
1573  * Insert a new item into a menu.
1574  */
1575 static MENUITEM *MENU_InsertItem( HMENU32 hMenu, UINT32 pos, UINT32 flags )
1576 {
1577     MENUITEM *newItems;
1578     POPUPMENU *menu;
1579
1580     if (!(menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu))) 
1581     {
1582         WARN(menu, "%04x not a menu handle\n",
1583                       hMenu );
1584         return NULL;
1585     }
1586
1587     /* Find where to insert new item */
1588
1589     if ((flags & MF_BYPOSITION) &&
1590         ((pos == (UINT32)-1) || (pos == menu->nItems)))
1591     {
1592         /* Special case: append to menu */
1593         /* Some programs specify the menu length to do that */
1594         pos = menu->nItems;
1595     }
1596     else
1597     {
1598         if (!MENU_FindItem( &hMenu, &pos, flags )) 
1599         {
1600             WARN(menu, "item %x not found\n",
1601                           pos );
1602             return NULL;
1603         }
1604         if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu)))
1605         {
1606             WARN(menu,"%04x not a menu handle\n",
1607                          hMenu);
1608             return NULL;
1609         }
1610     }
1611
1612     /* Create new items array */
1613
1614     newItems = HeapAlloc( SystemHeap, 0, sizeof(MENUITEM) * (menu->nItems+1) );
1615     if (!newItems)
1616     {
1617         WARN(menu, "allocation failed\n" );
1618         return NULL;
1619     }
1620     if (menu->nItems > 0)
1621     {
1622           /* Copy the old array into the new */
1623         if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
1624         if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
1625                                         (menu->nItems-pos)*sizeof(MENUITEM) );
1626         HeapFree( SystemHeap, 0, menu->items );
1627     }
1628     menu->items = newItems;
1629     menu->nItems++;
1630     memset( &newItems[pos], 0, sizeof(*newItems) );
1631     return &newItems[pos];
1632 }
1633
1634
1635 /**********************************************************************
1636  *         MENU_ParseResource
1637  *
1638  * Parse a standard menu resource and add items to the menu.
1639  * Return a pointer to the end of the resource.
1640  */
1641 static LPCSTR MENU_ParseResource( LPCSTR res, HMENU32 hMenu, BOOL32 unicode )
1642 {
1643     WORD flags, id = 0;
1644     LPCSTR str;
1645
1646     do
1647     {
1648         flags = GET_WORD(res);
1649         res += sizeof(WORD);
1650         if (!(flags & MF_POPUP))
1651         {
1652             id = GET_WORD(res);
1653             res += sizeof(WORD);
1654         }
1655         if (!IS_STRING_ITEM(flags))
1656             ERR(menu, "not a string item %04x\n", flags );
1657         str = res;
1658         if (!unicode) res += strlen(str) + 1;
1659         else res += (lstrlen32W((LPCWSTR)str) + 1) * sizeof(WCHAR);
1660         if (flags & MF_POPUP)
1661         {
1662             HMENU32 hSubMenu = CreatePopupMenu32();
1663             if (!hSubMenu) return NULL;
1664             if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
1665                 return NULL;
1666             if (!unicode) AppendMenu32A( hMenu, flags, (UINT32)hSubMenu, str );
1667             else AppendMenu32W( hMenu, flags, (UINT32)hSubMenu, (LPCWSTR)str );
1668         }
1669         else  /* Not a popup */
1670         {
1671             if (!unicode) AppendMenu32A( hMenu, flags, id, *str ? str : NULL );
1672             else AppendMenu32W( hMenu, flags, id,
1673                                 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
1674         }
1675     } while (!(flags & MF_END));
1676     return res;
1677 }
1678
1679
1680 /**********************************************************************
1681  *         MENUEX_ParseResource
1682  *
1683  * Parse an extended menu resource and add items to the menu.
1684  * Return a pointer to the end of the resource.
1685  */
1686 static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU32 hMenu)
1687 {
1688     WORD resinfo;
1689     do {
1690         MENUITEMINFO32W mii;
1691
1692         mii.cbSize = sizeof(mii);
1693         mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
1694         mii.fType = GET_DWORD(res);
1695         res += sizeof(DWORD);
1696         mii.fState = GET_DWORD(res);
1697         res += sizeof(DWORD);
1698         mii.wID = GET_DWORD(res);
1699         res += sizeof(DWORD);
1700         resinfo = GET_WORD(res); /* FIXME: for 16-bit apps this is a byte.  */
1701         res += sizeof(WORD);
1702         /* Align the text on a word boundary.  */
1703         res += (~((int)res - 1)) & 1;
1704         mii.dwTypeData = (LPWSTR) res;
1705         res += (1 + lstrlen32W(mii.dwTypeData)) * sizeof(WCHAR);
1706         /* Align the following fields on a dword boundary.  */
1707         res += (~((int)res - 1)) & 3;
1708
1709         /* FIXME: This is inefficient and cannot be optimised away by gcc.  */
1710         {
1711             LPSTR newstr = HEAP_strdupWtoA(GetProcessHeap(),
1712                                            0, mii.dwTypeData);
1713             TRACE(menu, "Menu item: [%08x,%08x,%04x,%04x,%s]\n",
1714                          mii.fType, mii.fState, mii.wID, resinfo, newstr);
1715             HeapFree( GetProcessHeap(), 0, newstr );
1716         }
1717
1718         if (resinfo & 1) {      /* Pop-up? */
1719             DWORD helpid = GET_DWORD(res); /* FIXME: use this.  */
1720             res += sizeof(DWORD);
1721             mii.hSubMenu = CreatePopupMenu32();
1722             if (!mii.hSubMenu)
1723                 return NULL;
1724             if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
1725                 DestroyMenu32(mii.hSubMenu);
1726                 return NULL;
1727         }
1728             mii.fMask |= MIIM_SUBMENU;
1729             mii.fType |= MF_POPUP;
1730         }
1731         InsertMenuItem32W(hMenu, -1, MF_BYPOSITION, &mii);
1732     } while (!(resinfo & MF_END));
1733     return res;
1734 }
1735
1736
1737 /***********************************************************************
1738  *           MENU_GetSubPopup
1739  *
1740  * Return the handle of the selected sub-popup menu (if any).
1741  */
1742 static HMENU32 MENU_GetSubPopup( HMENU32 hmenu )
1743 {
1744     POPUPMENU *menu;
1745     MENUITEM *item;
1746
1747     menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1748
1749     if (menu->FocusedItem == NO_SELECTED_ITEM) return 0;
1750
1751     item = &menu->items[menu->FocusedItem];
1752     if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
1753         return item->hSubMenu;
1754     return 0;
1755 }
1756
1757
1758 /***********************************************************************
1759  *           MENU_HideSubPopups
1760  *
1761  * Hide the sub-popup menus of this menu.
1762  */
1763 static void MENU_HideSubPopups( HWND32 hwndOwner, HMENU32 hmenu,
1764                                 BOOL32 sendMenuSelect )
1765 {
1766     POPUPMENU *menu = (POPUPMENU*) USER_HEAP_LIN_ADDR( hmenu );;
1767
1768     if (menu && uSubPWndLevel)
1769     {
1770         HMENU32 hsubmenu;
1771         POPUPMENU *submenu;
1772         MENUITEM *item;
1773
1774         if (menu->FocusedItem != NO_SELECTED_ITEM)
1775         {
1776             item = &menu->items[menu->FocusedItem];
1777             if (!(item->fType & MF_POPUP) ||
1778                 !(item->fState & MF_MOUSESELECT)) return;
1779             item->fState &= ~MF_MOUSESELECT;
1780             hsubmenu = item->hSubMenu;
1781         } else return;
1782
1783         submenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hsubmenu );
1784         MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE );
1785         MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect );
1786
1787         if (submenu->hWnd == pTopPopupWnd->hwndSelf ) 
1788         {
1789             ShowWindow32( submenu->hWnd, SW_HIDE );
1790             uSubPWndLevel = 0;
1791         }
1792         else
1793         {
1794             DestroyWindow32( submenu->hWnd );
1795             submenu->hWnd = 0;
1796         }
1797     }
1798 }
1799
1800
1801 /***********************************************************************
1802  *           MENU_ShowSubPopup
1803  *
1804  * Display the sub-menu of the selected item of this menu.
1805  * Return the handle of the submenu, or hmenu if no submenu to display.
1806  */
1807 static HMENU32 MENU_ShowSubPopup( HWND32 hwndOwner, HMENU32 hmenu,
1808                                   BOOL32 selectFirst )
1809 {
1810     RECT32 rect;
1811     POPUPMENU *menu;
1812     MENUITEM *item;
1813     WND *wndPtr;
1814     HDC32 hdc;
1815
1816     if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return hmenu;
1817
1818     if (!(wndPtr = WIN_FindWndPtr( menu->hWnd )) ||
1819          (menu->FocusedItem == NO_SELECTED_ITEM)) return hmenu;
1820
1821     item = &menu->items[menu->FocusedItem];
1822     if (!(item->fType & MF_POPUP) ||
1823          (item->fState & (MF_GRAYED | MF_DISABLED))) return hmenu;
1824
1825     /* message must be send before using item,
1826        because nearly everything may by changed by the application ! */
1827
1828     SendMessage16( hwndOwner, WM_INITMENUPOPUP, (WPARAM16)item->hSubMenu,
1829                    MAKELONG( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
1830
1831     item = &menu->items[menu->FocusedItem];
1832     rect = item->rect;
1833
1834     /* correct item if modified as a reaction to WM_INITMENUPOPUP-message */
1835     if (!(item->fState & MF_HILITE)) 
1836     {
1837         if (menu->wFlags & MF_POPUP) hdc = GetDC32( menu->hWnd );
1838         else hdc = GetDCEx32( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1839         item->fState |= MF_HILITE;
1840         MENU_DrawMenuItem( menu->hWnd, hdc, item, menu->Height, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE ); 
1841         ReleaseDC32( menu->hWnd, hdc );
1842     }
1843     if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
1844       item->rect = rect;
1845
1846     item->fState |= MF_MOUSESELECT;
1847
1848     if (IS_SYSTEM_MENU(menu))
1849     {
1850         MENU_InitSysMenuPopup(item->hSubMenu, wndPtr->dwStyle, wndPtr->class->style);
1851
1852         NC_GetSysPopupPos( wndPtr, &rect );
1853         rect.top = rect.bottom;
1854         rect.right = SYSMETRICS_CXSIZE;
1855         rect.bottom = SYSMETRICS_CYSIZE;
1856     }
1857     else
1858     {
1859         if (menu->wFlags & MF_POPUP)
1860         {
1861             rect.left = wndPtr->rectWindow.left + item->rect.right-arrow_bitmap_width;
1862             rect.top = wndPtr->rectWindow.top + item->rect.top;
1863             rect.right = item->rect.left - item->rect.right + 2*arrow_bitmap_width;
1864             rect.bottom = item->rect.top - item->rect.bottom;
1865         }
1866         else
1867         {
1868             rect.left = wndPtr->rectWindow.left + item->rect.left;
1869             rect.top = wndPtr->rectWindow.top + item->rect.bottom;
1870             rect.right = item->rect.right - item->rect.left;
1871             rect.bottom = item->rect.bottom - item->rect.top;
1872         }
1873     }
1874
1875     MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem,
1876                     rect.left, rect.top, rect.right, rect.bottom );
1877     if (selectFirst)
1878         MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
1879     return item->hSubMenu;
1880 }
1881
1882 /***********************************************************************
1883  *           MENU_PtMenu
1884  *
1885  * Walks menu chain trying to find a menu pt maps to.
1886  */
1887 static HMENU32 MENU_PtMenu( HMENU32 hMenu, POINT16 pt )
1888 {
1889    POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hMenu );
1890    register UINT32 ht = menu->FocusedItem;
1891
1892    /* try subpopup first (if any) */
1893     ht = (ht != NO_SELECTED_ITEM &&
1894           (menu->items[ht].fType & MF_POPUP) &&
1895           (menu->items[ht].fState & MF_MOUSESELECT))
1896         ? (UINT32) MENU_PtMenu(menu->items[ht].hSubMenu, pt) : 0;
1897
1898    if( !ht )    /* check the current window (avoiding WM_HITTEST) */
1899    {
1900         ht = (UINT32)NC_HandleNCHitTest( menu->hWnd, pt );
1901         if( menu->wFlags & MF_POPUP )
1902             ht =  (ht != (UINT32)HTNOWHERE && 
1903                    ht != (UINT32)HTERROR) ? (UINT32)hMenu : 0;
1904         else
1905         {
1906             WND* wndPtr = WIN_FindWndPtr(menu->hWnd);
1907
1908             ht = ( ht == HTSYSMENU ) ? (UINT32)(wndPtr->hSysMenu)
1909                  : ( ht == HTMENU ) ? (UINT32)(wndPtr->wIDmenu) : 0;
1910         }
1911    }
1912    return (HMENU32)ht;
1913 }
1914 /***********************************************************************
1915  *           MENU_ExecFocusedItem
1916  *
1917  * Execute a menu item (for instance when user pressed Enter).
1918  * Return the wID of the executed item. Otherwise, zero indicating
1919  * that no menu item wase executed;
1920  */
1921 static INT32 MENU_ExecFocusedItem( MTRACKER* pmt, HMENU32 hMenu )
1922 {
1923     MENUITEM *item;
1924     POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hMenu );
1925     if (!menu || !menu->nItems || 
1926         (menu->FocusedItem == NO_SELECTED_ITEM)) return 0;
1927
1928     item = &menu->items[menu->FocusedItem];
1929
1930     TRACE(menu, "%08x %08x %08x\n",
1931                  hMenu, item->wID, item->hSubMenu);
1932
1933     if (!(item->fType & MF_POPUP))
1934     {
1935         if (!(item->fState & (MF_GRAYED | MF_DISABLED)))
1936         {
1937             if( menu->wFlags & MF_SYSMENU )
1938             {
1939                 PostMessage16( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
1940                                MAKELPARAM((INT16)pmt->pt.x, (INT16)pmt->pt.y) );
1941             }
1942             else
1943             {
1944                 PostMessage16( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
1945             }
1946             return item->wID;
1947         }
1948         else return 0;
1949     }
1950     else
1951     {
1952         pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hMenu, TRUE );
1953         return 0;
1954     }
1955 }
1956
1957 /***********************************************************************
1958  *           MENU_SwitchTracking
1959  *
1960  * Helper function for menu navigation routines.
1961  */
1962 static void MENU_SwitchTracking( MTRACKER* pmt, HMENU32 hPtMenu, UINT32 id )
1963 {
1964     POPUPMENU *ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
1965     POPUPMENU *topmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( pmt->hTopMenu );
1966
1967     if( pmt->hTopMenu != hPtMenu &&
1968         !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
1969     {
1970         /* both are top level menus (system and menu-bar) */
1971
1972         MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
1973         MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE );
1974         pmt->hTopMenu = hPtMenu;
1975     }
1976     else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE );
1977     MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE );
1978 }
1979
1980
1981 /***********************************************************************
1982  *           MENU_ButtonDown
1983  *
1984  * Return TRUE if we can go on with menu tracking.
1985  */
1986 static BOOL32 MENU_ButtonDown( MTRACKER* pmt, HMENU32 hPtMenu )
1987 {
1988     if (hPtMenu)
1989     {
1990         UINT32 id = 0;
1991         POPUPMENU *ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
1992         MENUITEM *item;
1993
1994         if( IS_SYSTEM_MENU(ptmenu) )
1995             item = ptmenu->items;
1996         else
1997             item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
1998
1999         if( item )
2000         {
2001             if( ptmenu->FocusedItem == id )
2002             {
2003                 /* nothing to do with already selected non-popup */
2004                 if( !(item->fType & MF_POPUP) ) return TRUE;
2005
2006                 if( item->fState & MF_MOUSESELECT )
2007                 {
2008                     if( ptmenu->wFlags & MF_POPUP )
2009                     {
2010                         /* hide selected subpopup */
2011
2012                         MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, TRUE );
2013                         pmt->hCurrentMenu = hPtMenu;
2014                         return TRUE;
2015                     }
2016                     return FALSE; /* shouldn't get here */
2017                 } 
2018             }
2019             else MENU_SwitchTracking( pmt, hPtMenu, id );
2020
2021             /* try to display a subpopup */
2022
2023             pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE );
2024             return TRUE;
2025         } 
2026         else WARN(menu, "\tunable to find clicked item!\n");
2027     }
2028     return FALSE;
2029 }
2030
2031 /***********************************************************************
2032  *           MENU_ButtonUp
2033  *
2034  * Return the the value of MENU_ExecFocusedItem if
2035  * the selected item was not a popup
2036  * 1 if the item was a popup
2037  * 0 otherwise.
2038  * A zero return value indicates that we can't go on with menu tracking.
2039  */
2040 static INT32 MENU_ButtonUp( MTRACKER* pmt, HMENU32 hPtMenu )
2041 {
2042     if (hPtMenu)
2043     {
2044         UINT32 id = 0;
2045         POPUPMENU *ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
2046         MENUITEM *item;
2047
2048         if( IS_SYSTEM_MENU(ptmenu) )
2049             item = ptmenu->items;
2050         else
2051             item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2052
2053         if( item && (ptmenu->FocusedItem == id ))
2054         {
2055             if( !(item->fType & MF_POPUP) )
2056                 return MENU_ExecFocusedItem( pmt, hPtMenu );
2057             hPtMenu = item->hSubMenu;
2058             if( hPtMenu == pmt->hCurrentMenu )
2059             {
2060                 /* Select first item of sub-popup */    
2061
2062                 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, NO_SELECTED_ITEM, FALSE );
2063                 MENU_MoveSelection( pmt->hOwnerWnd, hPtMenu, ITEM_NEXT );
2064             }
2065             return 1;
2066         }
2067     }
2068     return 0;
2069 }
2070
2071
2072 /***********************************************************************
2073  *           MENU_MouseMove
2074  *
2075  * Return TRUE if we can go on with menu tracking.
2076  */
2077 static BOOL32 MENU_MouseMove( MTRACKER* pmt, HMENU32 hPtMenu )
2078 {
2079     UINT32 id = NO_SELECTED_ITEM;
2080     POPUPMENU *ptmenu = NULL;
2081
2082     if( hPtMenu )
2083     {
2084         ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
2085         if( IS_SYSTEM_MENU(ptmenu) )
2086             id = 0;
2087         else
2088             MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2089     } 
2090
2091     if( id == NO_SELECTED_ITEM )
2092     {
2093         MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu, 
2094                          NO_SELECTED_ITEM, TRUE );
2095     }
2096     else if( ptmenu->FocusedItem != id )
2097     {
2098             MENU_SwitchTracking( pmt, hPtMenu, id );
2099             pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE );
2100     }
2101     return TRUE;
2102 }
2103
2104
2105 /***********************************************************************
2106  *           MENU_DoNextMenu
2107  *
2108  * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2109  */
2110 static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT32 vk )
2111 {
2112     POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( pmt->hTopMenu );
2113
2114     if( (vk == VK_LEFT &&  menu->FocusedItem == 0 ) ||
2115         (vk == VK_RIGHT && menu->FocusedItem == menu->nItems - 1))
2116     {
2117         WND*    wndPtr;
2118         HMENU32 hNewMenu;
2119         HWND32  hNewWnd;
2120         UINT32  id = 0;
2121         LRESULT l = SendMessage16( pmt->hOwnerWnd, WM_NEXTMENU, (WPARAM16)vk, 
2122                 (IS_SYSTEM_MENU(menu)) ? GetSubMenu16(pmt->hTopMenu,0) : pmt->hTopMenu );
2123
2124         TRACE(menu,"%04x [%04x] -> %04x [%04x]\n",
2125                      (UINT16)pmt->hCurrentMenu, (UINT16)pmt->hOwnerWnd, LOWORD(l), HIWORD(l) );
2126
2127         if( l == 0 )
2128         {
2129             wndPtr = WIN_FindWndPtr(pmt->hOwnerWnd);
2130
2131             hNewWnd = pmt->hOwnerWnd;
2132             if( IS_SYSTEM_MENU(menu) )
2133             {
2134                 /* switch to the menu bar */
2135
2136                 if( wndPtr->dwStyle & WS_CHILD || !wndPtr->wIDmenu ) 
2137                     return FALSE;
2138
2139                 hNewMenu = wndPtr->wIDmenu;
2140                 if( vk == VK_LEFT )
2141                 {
2142                     menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hNewMenu );
2143                     id = menu->nItems - 1;
2144                 }
2145             }
2146             else if( wndPtr->dwStyle & WS_SYSMENU ) 
2147             {
2148                 /* switch to the system menu */
2149                 hNewMenu = wndPtr->hSysMenu; 
2150             }
2151             else return FALSE;
2152         }
2153         else    /* application returned a new menu to switch to */
2154         {
2155             hNewMenu = LOWORD(l); hNewWnd = HIWORD(l);
2156
2157             if( IsMenu32(hNewMenu) && IsWindow32(hNewWnd) )
2158             {
2159                 wndPtr = WIN_FindWndPtr(hNewWnd);
2160
2161                 if( wndPtr->dwStyle & WS_SYSMENU &&
2162                     GetSubMenu16(wndPtr->hSysMenu, 0) == hNewMenu )
2163                 {
2164                     /* get the real system menu */
2165                     hNewMenu =  wndPtr->hSysMenu;
2166                 }
2167                 else if( wndPtr->dwStyle & WS_CHILD || wndPtr->wIDmenu != hNewMenu )
2168                 {
2169                     /* FIXME: Not sure what to do here, perhaps,
2170                      * try to track hNewMenu as a popup? */
2171
2172                     TRACE(menu," -- got confused.\n");
2173                     return FALSE;
2174                 }
2175             }
2176             else return FALSE;
2177         }
2178
2179         if( hNewMenu != pmt->hTopMenu )
2180         {
2181             MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE );
2182             if( pmt->hCurrentMenu != pmt->hTopMenu ) 
2183                 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2184         }
2185
2186         if( hNewWnd != pmt->hOwnerWnd )
2187         {
2188             ReleaseCapture(); 
2189             pmt->hOwnerWnd = hNewWnd;
2190             EVENT_Capture( pmt->hOwnerWnd, HTMENU );
2191         }
2192
2193         pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2194         MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE ); 
2195
2196         return TRUE;
2197     }
2198     return FALSE;
2199 }
2200
2201 /***********************************************************************
2202  *           MENU_SuspendPopup
2203  *
2204  * The idea is not to show the popup if the next input message is
2205  * going to hide it anyway.
2206  */
2207 static BOOL32 MENU_SuspendPopup( MTRACKER* pmt, UINT16 uMsg )
2208 {
2209     MSG16 msg;
2210
2211     msg.hwnd = pmt->hOwnerWnd;
2212
2213     PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2214     pmt->trackFlags |= TF_SKIPREMOVE;
2215
2216     switch( uMsg )
2217     {
2218         case WM_KEYDOWN:
2219              PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2220              if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2221              {
2222                  PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2223                  PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2224                  if( msg.message == WM_KEYDOWN &&
2225                     (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2226                  {
2227                      pmt->trackFlags |= TF_SUSPENDPOPUP;
2228                      return TRUE;
2229                  }
2230              }
2231              break;
2232     }
2233
2234     /* failures go through this */
2235     pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2236     return FALSE;
2237 }
2238
2239 /***********************************************************************
2240  *           MENU_KeyLeft
2241  *
2242  * Handle a VK_LEFT key event in a menu. 
2243  */
2244 static void MENU_KeyLeft( MTRACKER* pmt )
2245 {
2246     POPUPMENU *menu;
2247     HMENU32 hmenutmp, hmenuprev;
2248     UINT32  prevcol;
2249
2250     hmenuprev = hmenutmp = pmt->hTopMenu;
2251     menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenutmp );
2252
2253     /* Try to move 1 column left (if possible) */
2254     if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) != 
2255         NO_SELECTED_ITEM ) {
2256         
2257         MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2258                          prevcol, TRUE );
2259         return;
2260     }
2261
2262     /* close topmost popup */
2263     while (hmenutmp != pmt->hCurrentMenu)
2264     {
2265         hmenuprev = hmenutmp;
2266         hmenutmp = MENU_GetSubPopup( hmenuprev );
2267     }
2268
2269     MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2270     pmt->hCurrentMenu = hmenuprev; 
2271
2272     if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2273     {
2274         /* move menu bar selection if no more popups are left */
2275
2276         if( !MENU_DoNextMenu( pmt, VK_LEFT) )
2277              MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2278
2279         if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2280         {
2281            /* A sublevel menu was displayed - display the next one
2282             * unless there is another displacement coming up */
2283
2284             if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2285                 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd,
2286                                                 pmt->hTopMenu, TRUE );
2287         }
2288     }
2289 }
2290
2291
2292 /***********************************************************************
2293  *           MENU_KeyRight
2294  *
2295  * Handle a VK_RIGHT key event in a menu.
2296  */
2297 static void MENU_KeyRight( MTRACKER* pmt )
2298 {
2299     HMENU32 hmenutmp;
2300     POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( pmt->hTopMenu );
2301     UINT32  nextcol;
2302
2303     TRACE(menu, "MENU_KeyRight called, cur %x (%s), top %x (%s).\n",
2304                   pmt->hCurrentMenu,
2305                   ((POPUPMENU *)USER_HEAP_LIN_ADDR(pmt->hCurrentMenu))->
2306                      items[0].text,
2307                   pmt->hTopMenu, menu->items[0].text );
2308
2309     if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2310     {
2311         /* If already displaying a popup, try to display sub-popup */
2312
2313         hmenutmp = pmt->hCurrentMenu;
2314         pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hmenutmp, TRUE );
2315
2316         /* if subpopup was displayed then we are done */
2317         if (hmenutmp != pmt->hCurrentMenu) return;
2318     }
2319
2320     /* Check to see if there's another column */
2321     if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) != 
2322         NO_SELECTED_ITEM ) {
2323         TRACE(menu, "Going to %d.\n", nextcol );
2324         MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2325                          nextcol, TRUE );
2326         return;
2327     }
2328
2329     if (!(menu->wFlags & MF_POPUP))     /* menu bar tracking */
2330     {
2331         if( pmt->hCurrentMenu != pmt->hTopMenu )
2332         {
2333             MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2334             hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2335         } else hmenutmp = 0;
2336
2337         /* try to move to the next item */
2338         if( !MENU_DoNextMenu( pmt, VK_RIGHT) )
2339              MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
2340
2341         if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2342             if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
2343                 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, 
2344                                                        pmt->hTopMenu, TRUE );
2345     }
2346 }
2347
2348 /***********************************************************************
2349  *           MENU_TrackMenu
2350  *
2351  * Menu tracking code.
2352  */
2353 static INT32 MENU_TrackMenu( HMENU32 hmenu, UINT32 wFlags, INT32 x, INT32 y,
2354                               HWND32 hwnd, const RECT32 *lprect )
2355 {
2356     MSG32 msg;
2357     POPUPMENU *menu;
2358     BOOL32 fRemove;
2359     INT32 executedMenuId = 0;
2360     MTRACKER mt = { 0, hmenu, hmenu, hwnd, {x, y} };    /* control struct */
2361
2362     fEndMenu = FALSE;
2363     if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return FALSE;
2364
2365     if (wFlags & TPM_BUTTONDOWN) MENU_ButtonDown( &mt, hmenu );
2366
2367     EVENT_Capture( mt.hOwnerWnd, HTMENU );
2368
2369     while (!fEndMenu)
2370     {
2371         menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( mt.hCurrentMenu );
2372         msg.hwnd = (wFlags & TPM_ENTERIDLEEX && menu->wFlags & MF_POPUP) ? menu->hWnd : 0;
2373
2374         /* we have to keep the message in the queue until it's
2375          * clear that menu loop is not over yet. */
2376
2377         if (!MSG_InternalGetMessage( &msg, msg.hwnd, mt.hOwnerWnd,
2378                                      MSGF_MENU, PM_NOREMOVE, TRUE )) break;
2379
2380         TranslateMessage32( &msg );
2381         mt.pt = msg.pt;
2382
2383         fRemove = FALSE;
2384         if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
2385         {
2386             /* Find a menu for this mouse event */
2387             POINT16 pt16;
2388             CONV_POINT32TO16( &msg.pt, &pt16 );
2389             hmenu = MENU_PtMenu( mt.hTopMenu, pt16 );
2390
2391             switch(msg.message)
2392             {
2393                 /* no WM_NC... messages in captured state */
2394
2395                 case WM_RBUTTONDBLCLK:
2396                 case WM_RBUTTONDOWN:
2397                     if (!(wFlags & TPM_RIGHTBUTTON)) break;
2398                     /* fall through */
2399                 case WM_LBUTTONDBLCLK:
2400                 case WM_LBUTTONDOWN:
2401                     fEndMenu |= !MENU_ButtonDown( &mt, hmenu );
2402                     break;
2403                 
2404                 case WM_RBUTTONUP:
2405                     if (!(wFlags & TPM_RIGHTBUTTON)) break;
2406                     /* fall through */
2407                 case WM_LBUTTONUP:
2408                     /* Check if a menu was selected by the mouse */
2409                     if (hmenu)
2410                     {
2411                         executedMenuId = MENU_ButtonUp( &mt, hmenu );
2412
2413                         /* the executedMenuId higher than one means that it contains
2414                          the id of the selected item so we have to put the fEndMenu to TRUE.
2415                          Otherwise, it contains a continue
2416                          flag returned by MENU_ButtonUp indicating if we can continue with
2417                          menu tracking or not*/
2418                         fEndMenu = ((executedMenuId > 1) ? TRUE : FALSE);
2419                         
2420                     }
2421                     /* No menu was selected by the mouse */
2422                     /* if the function was called by TrackPopupMenu, continue
2423                        with the menu tracking. If not, stop it */
2424                     else
2425                         fEndMenu = ((wFlags & TPM_POPUPMENU) ? FALSE : TRUE);
2426                     
2427                     break;
2428                 
2429                 case WM_MOUSEMOVE:
2430                     /* In win95 winelook, the selected menu item must be changed every time the
2431                        mouse moves. In Win31 winelook, the mouse button has to be held down */
2432                      
2433                     if ( (TWEAK_WineLook > WIN31_LOOK) ||
2434                          ( (msg.wParam & MK_LBUTTON) ||
2435                            ((wFlags & TPM_RIGHTBUTTON) && (msg.wParam & MK_RBUTTON))) )
2436
2437                         fEndMenu |= !MENU_MouseMove( &mt, hmenu );
2438
2439             } /* switch(msg.message) - mouse */
2440         }
2441         else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
2442         {
2443             fRemove = TRUE;  /* Keyboard messages are always removed */
2444             switch(msg.message)
2445             {
2446             case WM_KEYDOWN:
2447                 switch(msg.wParam)
2448                 {
2449                 case VK_HOME:
2450                 case VK_END:
2451                     MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, 
2452                                      NO_SELECTED_ITEM, FALSE );
2453                 /* fall through */
2454                 case VK_UP:
2455                     MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, 
2456                                        (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
2457                     break;
2458
2459                 case VK_DOWN: /* If on menu bar, pull-down the menu */
2460
2461                     menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( mt.hCurrentMenu );
2462                     if (!(menu->wFlags & MF_POPUP))
2463                         mt.hCurrentMenu = MENU_ShowSubPopup( mt.hOwnerWnd, mt.hTopMenu, TRUE );
2464                     else      /* otherwise try to move selection */
2465                         MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, ITEM_NEXT );
2466                     break;
2467
2468                 case VK_LEFT:
2469                     MENU_KeyLeft( &mt );
2470                     break;
2471                     
2472                 case VK_RIGHT:
2473                     MENU_KeyRight( &mt );
2474                     break;
2475                     
2476                 case VK_ESCAPE:
2477                     fEndMenu = TRUE;
2478                     break;
2479
2480                 default:
2481                     break;
2482                 }
2483                 break;  /* WM_KEYDOWN */
2484
2485             case WM_SYSKEYDOWN:
2486                 switch(msg.wParam)
2487                 {
2488                 case VK_MENU:
2489                     fEndMenu = TRUE;
2490                     break;
2491                     
2492                 }
2493                 break;  /* WM_SYSKEYDOWN */
2494
2495             case WM_CHAR:
2496                 {
2497                     UINT32      pos;
2498
2499                     if (msg.wParam == '\r' || msg.wParam == ' ')
2500                     {
2501                         executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu);
2502                         fEndMenu = ((executedMenuId != 0) ? TRUE:FALSE);
2503
2504                         break;
2505                     }
2506
2507                       /* Hack to avoid control chars. */
2508                       /* We will find a better way real soon... */
2509                     if ((msg.wParam <= 32) || (msg.wParam >= 127)) break;
2510
2511                     pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu, 
2512                                               LOWORD(msg.wParam), FALSE );
2513                     if (pos == (UINT32)-2) fEndMenu = TRUE;
2514                     else if (pos == (UINT32)-1) MessageBeep32(0);
2515                     else
2516                     {
2517                         MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos, TRUE );
2518                         executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu);
2519                         fEndMenu = ((executedMenuId != 0) ? TRUE:FALSE);
2520                     }
2521                 }                   
2522                 break;
2523             }  /* switch(msg.message) - kbd */
2524         }
2525         else
2526         {
2527             DispatchMessage32A( &msg );
2528         }
2529
2530         if (!fEndMenu) fRemove = TRUE;
2531
2532         /* finally remove message from the queue */
2533
2534         if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
2535             PeekMessage32A( &msg, 0, msg.message, msg.message, PM_REMOVE );
2536         else mt.trackFlags &= ~TF_SKIPREMOVE;
2537     }
2538
2539     ReleaseCapture();
2540     if( IsWindow32( mt.hOwnerWnd ) )
2541     {
2542         MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE );
2543
2544         menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( mt.hTopMenu );
2545         if (menu && menu->wFlags & MF_POPUP) 
2546         {
2547             ShowWindow32( menu->hWnd, SW_HIDE );
2548             uSubPWndLevel = 0;
2549         }
2550         MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE );
2551         SendMessage16( mt.hOwnerWnd, WM_MENUSELECT, 0, MAKELONG( 0xffff, 0 ) );
2552     }
2553
2554     /* returning the id of the selected menu.
2555       The return value is only used by TrackPopupMenu */
2556     return executedMenuId;
2557 }
2558
2559 /***********************************************************************
2560  *           MENU_InitTracking
2561  */
2562 static BOOL32 MENU_InitTracking(HWND32 hWnd, HMENU32 hMenu)
2563 {
2564     HideCaret32(0);
2565     SendMessage16( hWnd, WM_ENTERMENULOOP, 0, 0 );
2566     SendMessage16( hWnd, WM_SETCURSOR, hWnd, HTCAPTION );
2567     SendMessage16( hWnd, WM_INITMENU, hMenu, 0 );
2568     return TRUE;
2569 }
2570
2571 /***********************************************************************
2572  *           MENU_TrackMouseMenuBar
2573  *
2574  * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
2575  */
2576 void MENU_TrackMouseMenuBar( WND* wndPtr, INT32 ht, POINT32 pt )
2577 {
2578     HWND32  hWnd = wndPtr->hwndSelf;
2579     HMENU32 hMenu = (ht == HTSYSMENU) ? wndPtr->hSysMenu : wndPtr->wIDmenu;
2580
2581     if (IsMenu32(hMenu))
2582     {
2583         MENU_InitTracking( hWnd, hMenu );
2584         MENU_TrackMenu( hMenu, TPM_ENTERIDLEEX | TPM_BUTTONDOWN | 
2585                         TPM_LEFTALIGN | TPM_LEFTBUTTON, pt.x, pt.y, hWnd, NULL );
2586
2587         SendMessage16( hWnd, WM_EXITMENULOOP, 0, 0 );
2588         ShowCaret32(0);
2589     }
2590 }
2591
2592
2593 /***********************************************************************
2594  *           MENU_TrackKbdMenuBar
2595  *
2596  * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
2597  */
2598 void MENU_TrackKbdMenuBar( WND* wndPtr, UINT32 wParam, INT32 vkey)
2599 {
2600    UINT32 uItem = NO_SELECTED_ITEM;
2601    HMENU32 hTrackMenu; 
2602
2603     /* find window that has a menu */
2604  
2605     while( wndPtr->dwStyle & WS_CHILD && !(wndPtr->dwStyle & WS_SYSMENU) )
2606         if( !(wndPtr = wndPtr->parent) ) return;
2607
2608     /* check if we have to track a system menu */
2609           
2610     if( (wndPtr->dwStyle & (WS_CHILD | WS_MINIMIZE)) || 
2611         !wndPtr->wIDmenu || vkey == VK_SPACE )
2612     {
2613         if( !(wndPtr->dwStyle & WS_SYSMENU) ) return;
2614         hTrackMenu = wndPtr->hSysMenu;
2615         uItem = 0;
2616         wParam |= HTSYSMENU;    /* prevent item lookup */
2617     }
2618     else
2619         hTrackMenu = wndPtr->wIDmenu;
2620
2621     if (IsMenu32( hTrackMenu ))
2622     {
2623         MENU_InitTracking( wndPtr->hwndSelf, hTrackMenu );
2624
2625         if( vkey && vkey != VK_SPACE )
2626         {
2627             uItem = MENU_FindItemByKey( wndPtr->hwndSelf, hTrackMenu, 
2628                                         vkey, (wParam & HTSYSMENU) );
2629             if( uItem >= (UINT32)(-2) )
2630             {
2631                 if( uItem == (UINT32)(-1) ) MessageBeep32(0);
2632                 hTrackMenu = 0;
2633             }
2634         }
2635
2636         if( hTrackMenu )
2637         {
2638             MENU_SelectItem( wndPtr->hwndSelf, hTrackMenu, uItem, TRUE );
2639
2640             if( uItem == NO_SELECTED_ITEM )
2641                 MENU_MoveSelection( wndPtr->hwndSelf, hTrackMenu, ITEM_NEXT );
2642             else if( vkey )
2643                 PostMessage16( wndPtr->hwndSelf, WM_KEYDOWN, VK_DOWN, 0L );
2644
2645             MENU_TrackMenu( hTrackMenu, TPM_ENTERIDLEEX | TPM_LEFTALIGN | TPM_LEFTBUTTON,
2646                             0, 0, wndPtr->hwndSelf, NULL );
2647         }
2648         SendMessage16( wndPtr->hwndSelf, WM_EXITMENULOOP, 0, 0 );
2649         ShowCaret32(0);
2650     }
2651 }
2652
2653
2654 /**********************************************************************
2655  *           TrackPopupMenu16   (USER.416)
2656  */
2657 BOOL16 WINAPI TrackPopupMenu16( HMENU16 hMenu, UINT16 wFlags, INT16 x, INT16 y,
2658                            INT16 nReserved, HWND16 hWnd, const RECT16 *lpRect )
2659 {
2660     RECT32 r;
2661     if (lpRect)
2662          CONV_RECT16TO32( lpRect, &r );
2663     return TrackPopupMenu32( hMenu, wFlags, x, y, nReserved, hWnd,
2664                              lpRect ? &r : NULL );
2665 }
2666
2667
2668 /**********************************************************************
2669  *           TrackPopupMenu32   (USER32.549)
2670  */
2671 BOOL32 WINAPI TrackPopupMenu32( HMENU32 hMenu, UINT32 wFlags, INT32 x, INT32 y,
2672                            INT32 nReserved, HWND32 hWnd, const RECT32 *lpRect )
2673 {
2674     BOOL32 ret = FALSE;
2675
2676     HideCaret32(0);
2677     SendMessage16( hWnd, WM_INITMENUPOPUP, (WPARAM16)hMenu, 0);
2678     if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 )) 
2679         ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd, lpRect );
2680     ShowCaret32(0);
2681     return ret;
2682 }
2683
2684 /**********************************************************************
2685  *           TrackPopupMenuEx   (USER32.550)
2686  */
2687 BOOL32 WINAPI TrackPopupMenuEx( HMENU32 hMenu, UINT32 wFlags, INT32 x, INT32 y,
2688                                 HWND32 hWnd, LPTPMPARAMS lpTpm )
2689 {
2690     FIXME(menu, "not fully implemented\n" );
2691     return TrackPopupMenu32( hMenu, wFlags, x, y, 0, hWnd,
2692                              lpTpm ? &lpTpm->rcExclude : NULL );
2693 }
2694
2695 /***********************************************************************
2696  *           PopupMenuWndProc
2697  *
2698  * NOTE: Windows has totally different (and undocumented) popup wndproc.
2699  */
2700 LRESULT WINAPI PopupMenuWndProc( HWND32 hwnd, UINT32 message, WPARAM32 wParam,
2701                                  LPARAM lParam )
2702 {    
2703     WND* wndPtr = WIN_FindWndPtr(hwnd);
2704
2705     switch(message)
2706     {
2707     case WM_CREATE:
2708         {
2709             CREATESTRUCT32A *cs = (CREATESTRUCT32A*)lParam;
2710             SetWindowLong32A( hwnd, 0, (LONG)cs->lpCreateParams );
2711             return 0;
2712         }
2713
2714     case WM_MOUSEACTIVATE:  /* We don't want to be activated */
2715         return MA_NOACTIVATE;
2716
2717     case WM_PAINT:
2718         {
2719             PAINTSTRUCT32 ps;
2720             BeginPaint32( hwnd, &ps );
2721             MENU_DrawPopupMenu( hwnd, ps.hdc,
2722                                 (HMENU32)GetWindowLong32A( hwnd, 0 ) );
2723             EndPaint32( hwnd, &ps );
2724             return 0;
2725         }
2726     case WM_ERASEBKGND:
2727         return 1;
2728
2729     case WM_DESTROY:
2730
2731         /* zero out global pointer in case resident popup window
2732          * was somehow destroyed. */
2733
2734         if( pTopPopupWnd )
2735         {
2736             if( hwnd == pTopPopupWnd->hwndSelf )
2737             {
2738                 ERR(menu, "resident popup destroyed!\n");
2739
2740                 pTopPopupWnd = NULL;
2741                 uSubPWndLevel = 0;
2742             }
2743             else
2744                 uSubPWndLevel--;
2745         }
2746         break;
2747
2748     case WM_SHOWWINDOW:
2749
2750         if( wParam )
2751         {
2752             if( !(*(HMENU32*)wndPtr->wExtra) )
2753                 ERR(menu,"no menu to display\n");
2754         }
2755         else
2756             *(HMENU32*)wndPtr->wExtra = 0;
2757         break;
2758
2759     case MM_SETMENUHANDLE:
2760
2761         *(HMENU32*)wndPtr->wExtra = (HMENU32)wParam;
2762         break;
2763
2764     case MM_GETMENUHANDLE:
2765
2766         return *(HMENU32*)wndPtr->wExtra;
2767
2768     default:
2769         return DefWindowProc32A( hwnd, message, wParam, lParam );
2770     }
2771     return 0;
2772 }
2773
2774
2775 /***********************************************************************
2776  *           MENU_GetMenuBarHeight
2777  *
2778  * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
2779  */
2780 UINT32 MENU_GetMenuBarHeight( HWND32 hwnd, UINT32 menubarWidth,
2781                               INT32 orgX, INT32 orgY )
2782 {
2783     HDC32 hdc;
2784     RECT32 rectBar;
2785     WND *wndPtr;
2786     LPPOPUPMENU lppop;
2787
2788     TRACE(menu, "HWND 0x%x, width %d, "
2789                   "at (%d, %d).\n", hwnd, menubarWidth, orgX, orgY );
2790     
2791     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
2792     if (!(lppop = (LPPOPUPMENU)USER_HEAP_LIN_ADDR((HMENU16)wndPtr->wIDmenu)))
2793       return 0;
2794     hdc = GetDCEx32( hwnd, 0, DCX_CACHE | DCX_WINDOW );
2795     SetRect32(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+SYSMETRICS_CYMENU);
2796     MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
2797     ReleaseDC32( hwnd, hdc );
2798     return lppop->Height;
2799 }
2800
2801
2802 /*******************************************************************
2803  *         ChangeMenu16    (USER.153)
2804  */
2805 BOOL16 WINAPI ChangeMenu16( HMENU16 hMenu, UINT16 pos, SEGPTR data,
2806                             UINT16 id, UINT16 flags )
2807 {
2808     TRACE(menu,"menu=%04x pos=%d data=%08lx id=%04x flags=%04x\n",
2809                   hMenu, pos, (DWORD)data, id, flags );
2810     if (flags & MF_APPEND) return AppendMenu16( hMenu, flags & ~MF_APPEND,
2811                                                 id, data );
2812
2813     /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
2814     /* for MF_DELETE. We should check the parameters for all others */
2815     /* MF_* actions also (anybody got a doc on ChangeMenu?). */
2816
2817     if (flags & MF_DELETE) return DeleteMenu16(hMenu, pos, flags & ~MF_DELETE);
2818     if (flags & MF_CHANGE) return ModifyMenu16(hMenu, pos, flags & ~MF_CHANGE,
2819                                                id, data );
2820     if (flags & MF_REMOVE) return RemoveMenu16(hMenu,
2821                                               flags & MF_BYPOSITION ? pos : id,
2822                                               flags & ~MF_REMOVE );
2823     /* Default: MF_INSERT */
2824     return InsertMenu16( hMenu, pos, flags, id, data );
2825 }
2826
2827
2828 /*******************************************************************
2829  *         ChangeMenu32A    (USER32.23)
2830  */
2831 BOOL32 WINAPI ChangeMenu32A( HMENU32 hMenu, UINT32 pos, LPCSTR data,
2832                              UINT32 id, UINT32 flags )
2833 {
2834     TRACE(menu,"menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
2835                   hMenu, pos, (DWORD)data, id, flags );
2836     if (flags & MF_APPEND) return AppendMenu32A( hMenu, flags & ~MF_APPEND,
2837                                                  id, data );
2838     if (flags & MF_DELETE) return DeleteMenu32(hMenu, pos, flags & ~MF_DELETE);
2839     if (flags & MF_CHANGE) return ModifyMenu32A(hMenu, pos, flags & ~MF_CHANGE,
2840                                                 id, data );
2841     if (flags & MF_REMOVE) return RemoveMenu32( hMenu,
2842                                               flags & MF_BYPOSITION ? pos : id,
2843                                               flags & ~MF_REMOVE );
2844     /* Default: MF_INSERT */
2845     return InsertMenu32A( hMenu, pos, flags, id, data );
2846 }
2847
2848
2849 /*******************************************************************
2850  *         ChangeMenu32W    (USER32.24)
2851  */
2852 BOOL32 WINAPI ChangeMenu32W( HMENU32 hMenu, UINT32 pos, LPCWSTR data,
2853                              UINT32 id, UINT32 flags )
2854 {
2855     TRACE(menu,"menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
2856                   hMenu, pos, (DWORD)data, id, flags );
2857     if (flags & MF_APPEND) return AppendMenu32W( hMenu, flags & ~MF_APPEND,
2858                                                  id, data );
2859     if (flags & MF_DELETE) return DeleteMenu32(hMenu, pos, flags & ~MF_DELETE);
2860     if (flags & MF_CHANGE) return ModifyMenu32W(hMenu, pos, flags & ~MF_CHANGE,
2861                                                 id, data );
2862     if (flags & MF_REMOVE) return RemoveMenu32( hMenu,
2863                                               flags & MF_BYPOSITION ? pos : id,
2864                                               flags & ~MF_REMOVE );
2865     /* Default: MF_INSERT */
2866     return InsertMenu32W( hMenu, pos, flags, id, data );
2867 }
2868
2869
2870 /*******************************************************************
2871  *         CheckMenuItem16    (USER.154)
2872  */
2873 BOOL16 WINAPI CheckMenuItem16( HMENU16 hMenu, UINT16 id, UINT16 flags )
2874 {
2875     return (BOOL16)CheckMenuItem32( hMenu, id, flags );
2876 }
2877
2878
2879 /*******************************************************************
2880  *         CheckMenuItem32    (USER32.46)
2881  */
2882 DWORD WINAPI CheckMenuItem32( HMENU32 hMenu, UINT32 id, UINT32 flags )
2883 {
2884     MENUITEM *item;
2885     DWORD ret;
2886
2887     TRACE(menu,"%04x %04x %04x\n", hMenu, id, flags );
2888     if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
2889     ret = item->fState & MF_CHECKED;
2890     if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
2891     else item->fState &= ~MF_CHECKED;
2892     return ret;
2893 }
2894
2895
2896 /**********************************************************************
2897  *         EnableMenuItem16    (USER.155)
2898  */
2899 UINT16 WINAPI EnableMenuItem16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
2900 {
2901     return EnableMenuItem32( hMenu, wItemID, wFlags );
2902 }
2903
2904
2905 /**********************************************************************
2906  *         EnableMenuItem32    (USER32.170)
2907  */
2908 UINT32 WINAPI EnableMenuItem32( HMENU32 hMenu, UINT32 wItemID, UINT32 wFlags )
2909 {
2910     UINT32    oldflags;
2911     MENUITEM *item;
2912
2913     TRACE(menu,"(%04x, %04X, %04X) !\n", 
2914                  hMenu, wItemID, wFlags);
2915
2916     if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
2917         return (UINT32)-1;
2918
2919     oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
2920     item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
2921     return oldflags;
2922 }
2923
2924
2925 /*******************************************************************
2926  *         GetMenuString16    (USER.161)
2927  */
2928 INT16 WINAPI GetMenuString16( HMENU16 hMenu, UINT16 wItemID,
2929                               LPSTR str, INT16 nMaxSiz, UINT16 wFlags )
2930 {
2931     return GetMenuString32A( hMenu, wItemID, str, nMaxSiz, wFlags );
2932 }
2933
2934
2935 /*******************************************************************
2936  *         GetMenuString32A    (USER32.268)
2937  */
2938 INT32 WINAPI GetMenuString32A( HMENU32 hMenu, UINT32 wItemID,
2939                                LPSTR str, INT32 nMaxSiz, UINT32 wFlags )
2940 {
2941     MENUITEM *item;
2942
2943     TRACE(menu, "menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
2944                  hMenu, wItemID, str, nMaxSiz, wFlags );
2945     if (!str || !nMaxSiz) return 0;
2946     str[0] = '\0';
2947     if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
2948     if (!IS_STRING_ITEM(item->fType)) return 0;
2949     lstrcpyn32A( str, item->text, nMaxSiz );
2950     TRACE(menu, "returning '%s'\n", str );
2951     return strlen(str);
2952 }
2953
2954
2955 /*******************************************************************
2956  *         GetMenuString32W    (USER32.269)
2957  */
2958 INT32 WINAPI GetMenuString32W( HMENU32 hMenu, UINT32 wItemID,
2959                                LPWSTR str, INT32 nMaxSiz, UINT32 wFlags )
2960 {
2961     MENUITEM *item;
2962
2963     TRACE(menu, "menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
2964                  hMenu, wItemID, str, nMaxSiz, wFlags );
2965     if (!str || !nMaxSiz) return 0;
2966     str[0] = '\0';
2967     if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
2968     if (!IS_STRING_ITEM(item->fType)) return 0;
2969     lstrcpynAtoW( str, item->text, nMaxSiz );
2970     return lstrlen32W(str);
2971 }
2972
2973
2974 /**********************************************************************
2975  *         HiliteMenuItem16    (USER.162)
2976  */
2977 BOOL16 WINAPI HiliteMenuItem16( HWND16 hWnd, HMENU16 hMenu, UINT16 wItemID,
2978                                 UINT16 wHilite )
2979 {
2980     return HiliteMenuItem32( hWnd, hMenu, wItemID, wHilite );
2981 }
2982
2983
2984 /**********************************************************************
2985  *         HiliteMenuItem32    (USER32.318)
2986  */
2987 BOOL32 WINAPI HiliteMenuItem32( HWND32 hWnd, HMENU32 hMenu, UINT32 wItemID,
2988                                 UINT32 wHilite )
2989 {
2990     LPPOPUPMENU menu;
2991     TRACE(menu,"(%04x, %04x, %04x, %04x);\n", 
2992                  hWnd, hMenu, wItemID, wHilite);
2993     if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
2994     if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
2995     if (menu->FocusedItem == wItemID) return TRUE;
2996     MENU_HideSubPopups( hWnd, hMenu, FALSE );
2997     MENU_SelectItem( hWnd, hMenu, wItemID, TRUE );
2998     return TRUE;
2999 }
3000
3001
3002 /**********************************************************************
3003  *         GetMenuState16    (USER.250)
3004  */
3005 UINT16 WINAPI GetMenuState16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
3006 {
3007     return GetMenuState32( hMenu, wItemID, wFlags );
3008 }
3009
3010
3011 /**********************************************************************
3012  *         GetMenuState32    (USER32.267)
3013  */
3014 UINT32 WINAPI GetMenuState32( HMENU32 hMenu, UINT32 wItemID, UINT32 wFlags )
3015 {
3016     MENUITEM *item;
3017     TRACE(menu,"(%04x, %04x, %04x);\n", 
3018                  hMenu, wItemID, wFlags);
3019     if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3020     debug_print_menuitem ("  item: ", item, "");
3021     if (item->fType & MF_POPUP)
3022     {
3023         POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( item->hSubMenu );
3024         if (!menu) return -1;
3025         else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3026     }
3027     else
3028     {
3029         /* We used to (from way back then) mask the result to 0xff.  */
3030         /* I don't know why and it seems wrong as the documented */
3031         /* return flag MF_SEPARATOR is outside that mask.  */
3032         return (item->fType | item->fState);
3033     }
3034 }
3035
3036
3037 /**********************************************************************
3038  *         GetMenuItemCount16    (USER.263)
3039  */
3040 INT16 WINAPI GetMenuItemCount16( HMENU16 hMenu )
3041 {
3042     LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3043     if (!IS_A_MENU(menu)) return -1;
3044     TRACE(menu,"(%04x) returning %d\n", 
3045                   hMenu, menu->nItems );
3046     return menu->nItems;
3047 }
3048
3049
3050 /**********************************************************************
3051  *         GetMenuItemCount32    (USER32.262)
3052  */
3053 INT32 WINAPI GetMenuItemCount32( HMENU32 hMenu )
3054 {
3055     LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3056     if (!IS_A_MENU(menu)) return -1;
3057     TRACE(menu,"(%04x) returning %d\n", 
3058                   hMenu, menu->nItems );
3059     return menu->nItems;
3060 }
3061
3062
3063 /**********************************************************************
3064  *         GetMenuItemID16    (USER.264)
3065  */
3066 UINT16 WINAPI GetMenuItemID16( HMENU16 hMenu, INT16 nPos )
3067 {
3068     LPPOPUPMENU menu;
3069
3070     if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return -1;
3071     if ((nPos < 0) || ((UINT16) nPos >= menu->nItems)) return -1;
3072     if (menu->items[nPos].fType & MF_POPUP) return -1;
3073     return menu->items[nPos].wID;
3074 }
3075
3076
3077 /**********************************************************************
3078  *         GetMenuItemID32    (USER32.263)
3079  */
3080 UINT32 WINAPI GetMenuItemID32( HMENU32 hMenu, INT32 nPos )
3081 {
3082     LPPOPUPMENU menu;
3083
3084     if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return -1;
3085     if ((nPos < 0) || (nPos >= menu->nItems)) return -1;
3086     if (menu->items[nPos].fType & MF_POPUP) return -1; 
3087     return menu->items[nPos].wID;
3088 }
3089
3090
3091 /*******************************************************************
3092  *         InsertMenu16    (USER.410)
3093  */
3094 BOOL16 WINAPI InsertMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
3095                             UINT16 id, SEGPTR data )
3096 {
3097     UINT32 pos32 = (UINT32)pos;
3098     if ((pos == (UINT16)-1) && (flags & MF_BYPOSITION)) pos32 = (UINT32)-1;
3099     if (IS_STRING_ITEM(flags) && data)
3100         return InsertMenu32A( hMenu, pos32, flags, id,
3101                               (LPSTR)PTR_SEG_TO_LIN(data) );
3102     return InsertMenu32A( hMenu, pos32, flags, id, (LPSTR)data );
3103 }
3104
3105
3106 /*******************************************************************
3107  *         InsertMenu32A    (USER32.322)
3108  */
3109 BOOL32 WINAPI InsertMenu32A( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3110                              UINT32 id, LPCSTR str )
3111 {
3112     MENUITEM *item;
3113
3114     if (IS_STRING_ITEM(flags) && str)
3115         TRACE(menu, "hMenu %04x, pos %d, flags %08x, "
3116                       "id %04x, str '%s'\n",
3117                       hMenu, pos, flags, id, str );
3118     else TRACE(menu, "hMenu %04x, pos %d, flags %08x, "
3119                        "id %04x, str %08lx (not a string)\n",
3120                        hMenu, pos, flags, id, (DWORD)str );
3121
3122     if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3123
3124     if (!(MENU_SetItemData( item, flags, id, str )))
3125     {
3126         RemoveMenu32( hMenu, pos, flags );
3127         return FALSE;
3128     }
3129
3130     if (flags & MF_POPUP)  /* Set the MF_POPUP flag on the popup-menu */
3131         ((POPUPMENU *)USER_HEAP_LIN_ADDR((HMENU16)id))->wFlags |= MF_POPUP;
3132
3133     item->hCheckBit = item->hUnCheckBit = 0;
3134     return TRUE;
3135 }
3136
3137
3138 /*******************************************************************
3139  *         InsertMenu32W    (USER32.325)
3140  */
3141 BOOL32 WINAPI InsertMenu32W( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3142                              UINT32 id, LPCWSTR str )
3143 {
3144     BOOL32 ret;
3145
3146     if (IS_STRING_ITEM(flags) && str)
3147     {
3148         LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3149         ret = InsertMenu32A( hMenu, pos, flags, id, newstr );
3150         HeapFree( GetProcessHeap(), 0, newstr );
3151         return ret;
3152     }
3153     else return InsertMenu32A( hMenu, pos, flags, id, (LPCSTR)str );
3154 }
3155
3156
3157 /*******************************************************************
3158  *         AppendMenu16    (USER.411)
3159  */
3160 BOOL16 WINAPI AppendMenu16(HMENU16 hMenu, UINT16 flags, UINT16 id, SEGPTR data)
3161 {
3162     return InsertMenu16( hMenu, -1, flags | MF_BYPOSITION, id, data );
3163 }
3164
3165
3166 /*******************************************************************
3167  *         AppendMenu32A    (USER32.5)
3168  */
3169 BOOL32 WINAPI AppendMenu32A( HMENU32 hMenu, UINT32 flags,
3170                              UINT32 id, LPCSTR data )
3171 {
3172     return InsertMenu32A( hMenu, -1, flags | MF_BYPOSITION, id, data );
3173 }
3174
3175
3176 /*******************************************************************
3177  *         AppendMenu32W    (USER32.6)
3178  */
3179 BOOL32 WINAPI AppendMenu32W( HMENU32 hMenu, UINT32 flags,
3180                              UINT32 id, LPCWSTR data )
3181 {
3182     return InsertMenu32W( hMenu, -1, flags | MF_BYPOSITION, id, data );
3183 }
3184
3185
3186 /**********************************************************************
3187  *         RemoveMenu16    (USER.412)
3188  */
3189 BOOL16 WINAPI RemoveMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3190 {
3191     return RemoveMenu32( hMenu, nPos, wFlags );
3192 }
3193
3194
3195 /**********************************************************************
3196  *         RemoveMenu32    (USER32.441)
3197  */
3198 BOOL32 WINAPI RemoveMenu32( HMENU32 hMenu, UINT32 nPos, UINT32 wFlags )
3199 {
3200     LPPOPUPMENU menu;
3201     MENUITEM *item;
3202
3203     TRACE(menu,"(%04x, %04x, %04x)\n",hMenu, nPos, wFlags);
3204     if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3205     if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
3206     
3207       /* Remove item */
3208
3209     MENU_FreeItemData( item );
3210
3211     if (--menu->nItems == 0)
3212     {
3213         HeapFree( SystemHeap, 0, menu->items );
3214         menu->items = NULL;
3215     }
3216     else
3217     {
3218         while(nPos < menu->nItems)
3219         {
3220             *item = *(item+1);
3221             item++;
3222             nPos++;
3223         }
3224         menu->items = HeapReAlloc( SystemHeap, 0, menu->items,
3225                                    menu->nItems * sizeof(MENUITEM) );
3226     }
3227     return TRUE;
3228 }
3229
3230
3231 /**********************************************************************
3232  *         DeleteMenu16    (USER.413)
3233  */
3234 BOOL16 WINAPI DeleteMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3235 {
3236     return DeleteMenu32( hMenu, nPos, wFlags );
3237 }
3238
3239
3240 /**********************************************************************
3241  *         DeleteMenu32    (USER32.129)
3242  */
3243 BOOL32 WINAPI DeleteMenu32( HMENU32 hMenu, UINT32 nPos, UINT32 wFlags )
3244 {
3245     MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3246     if (!item) return FALSE;
3247     if (item->fType & MF_POPUP) DestroyMenu32( item->hSubMenu );
3248       /* nPos is now the position of the item */
3249     RemoveMenu32( hMenu, nPos, wFlags | MF_BYPOSITION );
3250     return TRUE;
3251 }
3252
3253
3254 /*******************************************************************
3255  *         ModifyMenu16    (USER.414)
3256  */
3257 BOOL16 WINAPI ModifyMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
3258                             UINT16 id, SEGPTR data )
3259 {
3260     if (IS_STRING_ITEM(flags))
3261         return ModifyMenu32A( hMenu, pos, flags, id,
3262                               (LPSTR)PTR_SEG_TO_LIN(data) );
3263     return ModifyMenu32A( hMenu, pos, flags, id, (LPSTR)data );
3264 }
3265
3266
3267 /*******************************************************************
3268  *         ModifyMenu32A    (USER32.397)
3269  */
3270 BOOL32 WINAPI ModifyMenu32A( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3271                              UINT32 id, LPCSTR str )
3272 {
3273     MENUITEM *item;
3274
3275     if (IS_STRING_ITEM(flags))
3276     {
3277         TRACE(menu, "%04x %d %04x %04x '%s'\n",
3278                       hMenu, pos, flags, id, str ? str : "#NULL#" );
3279         if (!str) return FALSE;
3280     }
3281     else
3282     {
3283         TRACE(menu, "%04x %d %04x %04x %08lx\n",
3284                       hMenu, pos, flags, id, (DWORD)str );
3285     }
3286
3287     if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3288     return MENU_SetItemData( item, flags, id, str );
3289 }
3290
3291
3292 /*******************************************************************
3293  *         ModifyMenu32W    (USER32.398)
3294  */
3295 BOOL32 WINAPI ModifyMenu32W( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3296                              UINT32 id, LPCWSTR str )
3297 {
3298     BOOL32 ret;
3299
3300     if (IS_STRING_ITEM(flags) && str)
3301     {
3302         LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3303         ret = ModifyMenu32A( hMenu, pos, flags, id, newstr );
3304         HeapFree( GetProcessHeap(), 0, newstr );
3305         return ret;
3306     }
3307     else return ModifyMenu32A( hMenu, pos, flags, id, (LPCSTR)str );
3308 }
3309
3310
3311 /**********************************************************************
3312  *         CreatePopupMenu16    (USER.415)
3313  */
3314 HMENU16 WINAPI CreatePopupMenu16(void)
3315 {
3316     return CreatePopupMenu32();
3317 }
3318
3319
3320 /**********************************************************************
3321  *         CreatePopupMenu32    (USER32.82)
3322  */
3323 HMENU32 WINAPI CreatePopupMenu32(void)
3324 {
3325     HMENU32 hmenu;
3326     POPUPMENU *menu;
3327
3328     if (!(hmenu = CreateMenu32())) return 0;
3329     menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
3330     menu->wFlags |= MF_POPUP;
3331     return hmenu;
3332 }
3333
3334
3335 /**********************************************************************
3336  *         GetMenuCheckMarkDimensions    (USER.417) (USER32.258)
3337  */
3338 DWORD WINAPI GetMenuCheckMarkDimensions(void)
3339 {
3340     return MAKELONG( check_bitmap_width, check_bitmap_height );
3341 }
3342
3343
3344 /**********************************************************************
3345  *         SetMenuItemBitmaps16    (USER.418)
3346  */
3347 BOOL16 WINAPI SetMenuItemBitmaps16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags,
3348                                     HBITMAP16 hNewUnCheck, HBITMAP16 hNewCheck)
3349 {
3350     return SetMenuItemBitmaps32( hMenu, nPos, wFlags, hNewUnCheck, hNewCheck );
3351 }
3352
3353
3354 /**********************************************************************
3355  *         SetMenuItemBitmaps32    (USER32.490)
3356  */
3357 BOOL32 WINAPI SetMenuItemBitmaps32( HMENU32 hMenu, UINT32 nPos, UINT32 wFlags,
3358                                     HBITMAP32 hNewUnCheck, HBITMAP32 hNewCheck)
3359 {
3360     MENUITEM *item;
3361     TRACE(menu,"(%04x, %04x, %04x, %04x, %04x)\n",
3362                  hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3363     if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3364
3365     if (!hNewCheck && !hNewUnCheck)
3366     {
3367         item->fState &= ~MF_USECHECKBITMAPS;
3368     }
3369     else  /* Install new bitmaps */
3370     {
3371         item->hCheckBit = hNewCheck;
3372         item->hUnCheckBit = hNewUnCheck;
3373         item->fState |= MF_USECHECKBITMAPS;
3374     }
3375     return TRUE;
3376 }
3377
3378
3379 /**********************************************************************
3380  *         CreateMenu16    (USER.151)
3381  */
3382 HMENU16 WINAPI CreateMenu16(void)
3383 {
3384     return CreateMenu32();
3385 }
3386
3387
3388 /**********************************************************************
3389  *         CreateMenu32    (USER32.81)
3390  */
3391 HMENU32 WINAPI CreateMenu32(void)
3392 {
3393     HMENU32 hMenu;
3394     LPPOPUPMENU menu;
3395     if (!(hMenu = USER_HEAP_ALLOC( sizeof(POPUPMENU) ))) return 0;
3396     menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3397     menu->wFlags = 0;
3398     menu->wMagic = MENU_MAGIC;
3399     menu->hTaskQ = 0;
3400     menu->Width  = 0;
3401     menu->Height = 0;
3402     menu->nItems = 0;
3403     menu->hWnd   = 0;
3404     menu->items  = NULL;
3405     menu->FocusedItem = NO_SELECTED_ITEM;
3406     TRACE(menu, "return %04x\n", hMenu );
3407     return hMenu;
3408 }
3409
3410
3411 /**********************************************************************
3412  *         DestroyMenu16    (USER.152)
3413  */
3414 BOOL16 WINAPI DestroyMenu16( HMENU16 hMenu )
3415 {
3416     return DestroyMenu32( hMenu );
3417 }
3418
3419
3420 /**********************************************************************
3421  *         DestroyMenu32    (USER32.134)
3422  */
3423 BOOL32 WINAPI DestroyMenu32( HMENU32 hMenu )
3424 {
3425     TRACE(menu,"(%04x)\n", hMenu);
3426
3427     /* Silently ignore attempts to destroy default system popup */
3428
3429     if (hMenu && hMenu != MENU_DefSysPopup)
3430     {
3431         LPPOPUPMENU lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3432
3433         if( pTopPopupWnd && (hMenu == *(HMENU32*)pTopPopupWnd->wExtra) )
3434           *(UINT32*)pTopPopupWnd->wExtra = 0;
3435
3436         if (IS_A_MENU( lppop ))
3437         {
3438             lppop->wMagic = 0;  /* Mark it as destroyed */
3439
3440             if ((lppop->wFlags & MF_POPUP) && lppop->hWnd &&
3441                 (!pTopPopupWnd || (lppop->hWnd != pTopPopupWnd->hwndSelf)))
3442                 DestroyWindow32( lppop->hWnd );
3443
3444             if (lppop->items)   /* recursively destroy submenus */
3445             {
3446                 int i;
3447                 MENUITEM *item = lppop->items;
3448                 for (i = lppop->nItems; i > 0; i--, item++)
3449                 {
3450                     if (item->fType & MF_POPUP) DestroyMenu32(item->hSubMenu);
3451                     MENU_FreeItemData( item );
3452                 }
3453                 HeapFree( SystemHeap, 0, lppop->items );
3454             }
3455             USER_HEAP_FREE( hMenu );
3456         }
3457         else return FALSE;
3458     }
3459     return (hMenu != MENU_DefSysPopup);
3460 }
3461
3462
3463 /**********************************************************************
3464  *         GetSystemMenu16    (USER.156)
3465  */
3466 HMENU16 WINAPI GetSystemMenu16( HWND16 hWnd, BOOL16 bRevert )
3467 {
3468     return GetSystemMenu32( hWnd, bRevert );
3469 }
3470
3471
3472 /**********************************************************************
3473  *         GetSystemMenu32    (USER32.291)
3474  */
3475 HMENU32 WINAPI GetSystemMenu32( HWND32 hWnd, BOOL32 bRevert )
3476 {
3477     WND *wndPtr = WIN_FindWndPtr( hWnd );
3478
3479     if (wndPtr)
3480     {
3481         if( wndPtr->hSysMenu )
3482         {
3483             if( bRevert )
3484             {
3485                 DestroyMenu32(wndPtr->hSysMenu); 
3486                 wndPtr->hSysMenu = 0;
3487             }
3488             else
3489             {
3490                 POPUPMENU *menu = (POPUPMENU*) 
3491                            USER_HEAP_LIN_ADDR(wndPtr->hSysMenu);
3492                 if( menu->items[0].hSubMenu == MENU_DefSysPopup )
3493                     menu->items[0].hSubMenu = MENU_CopySysPopup();
3494             }
3495         }
3496
3497         if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
3498             wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, (HMENU32)(-1) );
3499
3500         if( wndPtr->hSysMenu )
3501             return GetSubMenu16(wndPtr->hSysMenu, 0);
3502     }
3503     return 0;
3504 }
3505
3506
3507 /*******************************************************************
3508  *         SetSystemMenu16    (USER.280)
3509  */
3510 BOOL16 WINAPI SetSystemMenu16( HWND16 hwnd, HMENU16 hMenu )
3511 {
3512     return SetSystemMenu32( hwnd, hMenu );
3513 }
3514
3515
3516 /*******************************************************************
3517  *         SetSystemMenu32    (USER32.508)
3518  */
3519 BOOL32 WINAPI SetSystemMenu32( HWND32 hwnd, HMENU32 hMenu )
3520 {
3521     WND *wndPtr = WIN_FindWndPtr(hwnd);
3522
3523     if (wndPtr)
3524     {
3525         if (wndPtr->hSysMenu) DestroyMenu32( wndPtr->hSysMenu );
3526         wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
3527         return TRUE;
3528     }
3529     return FALSE;
3530 }
3531
3532
3533 /**********************************************************************
3534  *         GetMenu16    (USER.157)
3535  */
3536 HMENU16 WINAPI GetMenu16( HWND16 hWnd ) 
3537 {
3538     WND * wndPtr = WIN_FindWndPtr(hWnd);
3539     if (wndPtr && !(wndPtr->dwStyle & WS_CHILD)) 
3540         return (HMENU16)wndPtr->wIDmenu;
3541     return 0;
3542 }
3543
3544
3545 /**********************************************************************
3546  *         GetMenu32    (USER32.257)
3547  */
3548 HMENU32 WINAPI GetMenu32( HWND32 hWnd ) 
3549
3550     WND * wndPtr = WIN_FindWndPtr(hWnd);
3551     if (wndPtr && !(wndPtr->dwStyle & WS_CHILD)) 
3552         return (HMENU32)wndPtr->wIDmenu;
3553     return 0;
3554 }
3555
3556
3557 /**********************************************************************
3558  *         SetMenu16    (USER.158)
3559  */
3560 BOOL16 WINAPI SetMenu16( HWND16 hWnd, HMENU16 hMenu )
3561 {
3562     return SetMenu32( hWnd, hMenu );
3563 }
3564
3565
3566 /**********************************************************************
3567  *         SetMenu32    (USER32.487)
3568  */
3569 BOOL32 WINAPI SetMenu32( HWND32 hWnd, HMENU32 hMenu )
3570 {
3571     WND * wndPtr = WIN_FindWndPtr(hWnd);
3572
3573     TRACE(menu,"(%04x, %04x);\n", hWnd, hMenu);
3574
3575     if (wndPtr && !(wndPtr->dwStyle & WS_CHILD))
3576     {
3577         if (GetCapture32() == hWnd) ReleaseCapture();
3578
3579         wndPtr->wIDmenu = (UINT32)hMenu;
3580         if (hMenu != 0)
3581         {
3582             LPPOPUPMENU lpmenu;
3583
3584             if (!(lpmenu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
3585             lpmenu->hWnd = hWnd;
3586             lpmenu->wFlags &= ~MF_POPUP;  /* Can't be a popup */
3587             lpmenu->Height = 0;  /* Make sure we recalculate the size */
3588         }
3589         if (IsWindowVisible32(hWnd))
3590             SetWindowPos32( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3591                         SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3592         return TRUE;
3593     }
3594     return FALSE;
3595 }
3596
3597
3598
3599 /**********************************************************************
3600  *         GetSubMenu16    (USER.159)
3601  */
3602 HMENU16 WINAPI GetSubMenu16( HMENU16 hMenu, INT16 nPos )
3603 {
3604     return GetSubMenu32( hMenu, nPos );
3605 }
3606
3607
3608 /**********************************************************************
3609  *         GetSubMenu32    (USER32.288)
3610  */
3611 HMENU32 WINAPI GetSubMenu32( HMENU32 hMenu, INT32 nPos )
3612 {
3613     LPPOPUPMENU lppop;
3614
3615     if (!(lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return 0;
3616     if ((UINT32)nPos >= lppop->nItems) return 0;
3617     if (!(lppop->items[nPos].fType & MF_POPUP)) return 0;
3618     return lppop->items[nPos].hSubMenu;
3619 }
3620
3621
3622 /**********************************************************************
3623  *         DrawMenuBar16    (USER.160)
3624  */
3625 void WINAPI DrawMenuBar16( HWND16 hWnd )
3626 {
3627     DrawMenuBar32( hWnd );
3628 }
3629
3630
3631 /**********************************************************************
3632  *         DrawMenuBar32    (USER32.161)
3633  */
3634 BOOL32 WINAPI DrawMenuBar32( HWND32 hWnd )
3635 {
3636     LPPOPUPMENU lppop;
3637     WND *wndPtr = WIN_FindWndPtr(hWnd);
3638     if (wndPtr && !(wndPtr->dwStyle & WS_CHILD) && wndPtr->wIDmenu)
3639     {
3640         lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR((HMENU16)wndPtr->wIDmenu);
3641         if (lppop == NULL) return FALSE;
3642
3643         lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
3644         SetWindowPos32( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3645                         SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3646         return TRUE;
3647     }
3648     return FALSE;
3649 }
3650
3651
3652 /***********************************************************************
3653  *           EndMenu   (USER.187) (USER32.175)
3654  */
3655 void WINAPI EndMenu(void)
3656 {
3657     fEndMenu = TRUE;
3658 }
3659
3660
3661 /***********************************************************************
3662  *           LookupMenuHandle   (USER.217)
3663  */
3664 HMENU16 WINAPI LookupMenuHandle( HMENU16 hmenu, INT16 id )
3665 {
3666     HMENU32 hmenu32 = hmenu;
3667     UINT32 id32 = id;
3668     if (!MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND )) return 0;
3669     else return hmenu32;
3670 }
3671
3672
3673 /**********************************************************************
3674  *          LoadMenu16    (USER.150)
3675  */
3676 HMENU16 WINAPI LoadMenu16( HINSTANCE16 instance, SEGPTR name )
3677 {
3678     HRSRC16 hRsrc;
3679     HGLOBAL16 handle;
3680     HMENU16 hMenu;
3681
3682     if (HIWORD(name))
3683     {
3684         char *str = (char *)PTR_SEG_TO_LIN( name );
3685         TRACE(menu, "(%04x,'%s')\n", instance, str );
3686         if (str[0] == '#') name = (SEGPTR)atoi( str + 1 );
3687     }
3688     else
3689         TRACE(resource,"(%04x,%04x)\n",instance,LOWORD(name));
3690
3691     if (!name) return 0;
3692     
3693     /* check for Win32 module */
3694     if (HIWORD(instance))
3695         return LoadMenu32A(instance,PTR_SEG_TO_LIN(name));
3696     instance = GetExePtr( instance );
3697
3698     if (!(hRsrc = FindResource16( instance, name, RT_MENU16 ))) return 0;
3699     if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
3700     hMenu = LoadMenuIndirect16(LockResource16(handle));
3701     FreeResource16( handle );
3702     return hMenu;
3703 }
3704
3705
3706 /*****************************************************************
3707  *        LoadMenu32A   (USER32.370)
3708  */
3709 HMENU32 WINAPI LoadMenu32A( HINSTANCE32 instance, LPCSTR name )
3710 {
3711     HRSRC32 hrsrc = FindResource32A( instance, name, RT_MENU32A );
3712     if (!hrsrc) return 0;
3713     return LoadMenuIndirect32A( (LPCVOID)LoadResource32( instance, hrsrc ));
3714 }
3715
3716
3717 /*****************************************************************
3718  *        LoadMenu32W   (USER32.373)
3719  */
3720 HMENU32 WINAPI LoadMenu32W( HINSTANCE32 instance, LPCWSTR name )
3721 {
3722     HRSRC32 hrsrc = FindResource32W( instance, name, RT_MENU32W );
3723     if (!hrsrc) return 0;
3724     return LoadMenuIndirect32W( (LPCVOID)LoadResource32( instance, hrsrc ));
3725 }
3726
3727
3728 /**********************************************************************
3729  *          LoadMenuIndirect16    (USER.220)
3730  */
3731 HMENU16 WINAPI LoadMenuIndirect16( LPCVOID template )
3732 {
3733     HMENU16 hMenu;
3734     WORD version, offset;
3735     LPCSTR p = (LPCSTR)template;
3736
3737     TRACE(menu,"(%p)\n", template );
3738     version = GET_WORD(p);
3739     p += sizeof(WORD);
3740     if (version)
3741     {
3742         WARN(menu, "version must be 0 for Win16\n" );
3743         return 0;
3744     }
3745     offset = GET_WORD(p);
3746     p += sizeof(WORD) + offset;
3747     if (!(hMenu = CreateMenu32())) return 0;
3748     if (!MENU_ParseResource( p, hMenu, FALSE ))
3749     {
3750         DestroyMenu32( hMenu );
3751         return 0;
3752     }
3753     return hMenu;
3754 }
3755
3756
3757 /**********************************************************************
3758  *          LoadMenuIndirect32A    (USER32.371)
3759  */
3760 HMENU32 WINAPI LoadMenuIndirect32A( LPCVOID template )
3761 {
3762     HMENU16 hMenu;
3763     WORD version, offset;
3764     LPCSTR p = (LPCSTR)template;
3765
3766     TRACE(menu,"%p\n", template );
3767     version = GET_WORD(p);
3768     p += sizeof(WORD);
3769     switch (version)
3770       {
3771       case 0:
3772         offset = GET_WORD(p);
3773         p += sizeof(WORD) + offset;
3774         if (!(hMenu = CreateMenu32())) return 0;
3775         if (!MENU_ParseResource( p, hMenu, TRUE ))
3776           {
3777             DestroyMenu32( hMenu );
3778             return 0;
3779           }
3780         return hMenu;
3781       case 1:
3782         offset = GET_WORD(p);
3783         p += sizeof(WORD) + offset;
3784         if (!(hMenu = CreateMenu32())) return 0;
3785         if (!MENUEX_ParseResource( p, hMenu))
3786           {
3787             DestroyMenu32( hMenu );
3788             return 0;
3789           }
3790         return hMenu;
3791       default:
3792         ERR(menu, "version %d not supported.\n", version);
3793         return 0;
3794       }
3795 }
3796
3797
3798 /**********************************************************************
3799  *          LoadMenuIndirect32W    (USER32.372)
3800  */
3801 HMENU32 WINAPI LoadMenuIndirect32W( LPCVOID template )
3802 {
3803     /* FIXME: is there anything different between A and W? */
3804     return LoadMenuIndirect32A( template );
3805 }
3806
3807
3808 /**********************************************************************
3809  *              IsMenu16    (USER.358)
3810  */
3811 BOOL16 WINAPI IsMenu16( HMENU16 hmenu )
3812 {
3813     LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
3814     return IS_A_MENU(menu);
3815 }
3816
3817
3818 /**********************************************************************
3819  *              IsMenu32    (USER32.346)
3820  */
3821 BOOL32 WINAPI IsMenu32(HMENU32 hmenu)
3822 {
3823     LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
3824     return IS_A_MENU(menu);
3825 }
3826
3827 /**********************************************************************
3828  *              GetMenuItemInfo32_common
3829  */
3830
3831 static BOOL32 GetMenuItemInfo32_common ( HMENU32 hmenu, UINT32 item,
3832                                          BOOL32 bypos,
3833                                          LPMENUITEMINFO32A lpmii,
3834                                          BOOL32 unicode)
3835 {
3836   MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
3837     debug_print_menuitem("GetMenuItemInfo32_common: ", menu, "");
3838     if (!menu)
3839         return FALSE;
3840
3841     if (lpmii->fMask & MIIM_TYPE) {
3842         lpmii->fType = menu->fType;
3843         switch (MENU_ITEM_TYPE(menu->fType)) {
3844         case MF_STRING:
3845             if (menu->text && lpmii->dwTypeData && lpmii->cch) {
3846           if (unicode)
3847                     lstrcpynAtoW((LPWSTR) lpmii->dwTypeData,
3848                                  menu->text,
3849                                  lpmii->cch);
3850                 else
3851                     lstrcpyn32A(lpmii->dwTypeData,
3852                                 menu->text,
3853                                 lpmii->cch);
3854         }
3855             break;
3856         case MF_OWNERDRAW:
3857         case MF_BITMAP:
3858             lpmii->dwTypeData = menu->text;
3859             break;
3860         default:
3861             break;
3862     }
3863     }
3864     if (lpmii->fMask & MIIM_STATE)
3865         lpmii->fState = menu->fState;
3866
3867     if (lpmii->fMask & MIIM_ID)
3868         lpmii->wID = menu->wID;
3869
3870     if (lpmii->fMask & MIIM_SUBMENU)
3871         lpmii->hSubMenu = menu->hSubMenu;
3872
3873     if (lpmii->fMask & MIIM_CHECKMARKS) {
3874         lpmii->hbmpChecked = menu->hCheckBit;
3875         lpmii->hbmpUnchecked = menu->hUnCheckBit;
3876     }
3877     if (lpmii->fMask & MIIM_DATA)
3878         lpmii->dwItemData = menu->dwItemData;
3879
3880   return TRUE;
3881 }
3882
3883 /**********************************************************************
3884  *              GetMenuItemInfo32A    (USER32.264)
3885  */
3886 BOOL32 WINAPI GetMenuItemInfo32A( HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3887                                   LPMENUITEMINFO32A lpmii)
3888 {
3889     return GetMenuItemInfo32_common (hmenu, item, bypos, lpmii, FALSE);
3890 }
3891
3892 /**********************************************************************
3893  *              GetMenuItemInfo32W    (USER32.265)
3894  */
3895 BOOL32 WINAPI GetMenuItemInfo32W( HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3896                                   LPMENUITEMINFO32W lpmii)
3897 {
3898     return GetMenuItemInfo32_common (hmenu, item, bypos,
3899                                      (LPMENUITEMINFO32A)lpmii, TRUE);
3900 }
3901
3902 /**********************************************************************
3903  *              SetMenuItemInfo32_common
3904  */
3905
3906 static BOOL32 SetMenuItemInfo32_common(MENUITEM * menu,
3907                                        const MENUITEMINFO32A *lpmii,
3908                                        BOOL32 unicode)
3909 {
3910     if (!menu) return FALSE;
3911
3912     if (lpmii->fMask & MIIM_TYPE) {
3913         /* Get rid of old string.  */
3914         if (IS_STRING_ITEM(menu->fType) && menu->text)
3915             HeapFree(SystemHeap, 0, menu->text);
3916
3917         menu->fType = lpmii->fType;
3918         menu->text = lpmii->dwTypeData;
3919         if (IS_STRING_ITEM(menu->fType) && menu->text) {
3920             menu->text =
3921                 unicode
3922                 ? HEAP_strdupWtoA(SystemHeap, 0,
3923                                   (LPWSTR) lpmii->dwTypeData)
3924                 : HEAP_strdupA(SystemHeap, 0, lpmii->dwTypeData);
3925         }
3926     }
3927     if (lpmii->fMask & MIIM_STATE)
3928         menu->fState = lpmii->fState;
3929
3930     if (lpmii->fMask & MIIM_ID)
3931         menu->wID = lpmii->wID;
3932
3933     if (lpmii->fMask & MIIM_SUBMENU) {
3934         menu->hSubMenu = lpmii->hSubMenu;
3935         if (menu->hSubMenu) {
3936             POPUPMENU *subMenu = (POPUPMENU *)USER_HEAP_LIN_ADDR((UINT16)menu->hSubMenu);
3937             if (IS_A_MENU(subMenu)) {
3938                 subMenu->wFlags |= MF_POPUP;
3939                 menu->fType |= MF_POPUP;
3940             }
3941             else
3942                 /* FIXME: Return an error ? */
3943                 menu->fType &= ~MF_POPUP;
3944         }
3945         else
3946             menu->fType &= ~MF_POPUP;
3947     }
3948
3949     if (lpmii->fMask & MIIM_CHECKMARKS)
3950     {
3951         menu->hCheckBit = lpmii->hbmpChecked;
3952         menu->hUnCheckBit = lpmii->hbmpUnchecked;
3953     }
3954     if (lpmii->fMask & MIIM_DATA)
3955         menu->dwItemData = lpmii->dwItemData;
3956
3957     debug_print_menuitem("SetMenuItemInfo32_common: ", menu, "");
3958     return TRUE;
3959 }
3960
3961 /**********************************************************************
3962  *              SetMenuItemInfo32A    (USER32.491)
3963  */
3964 BOOL32 WINAPI SetMenuItemInfo32A(HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3965                                  const MENUITEMINFO32A *lpmii) 
3966 {
3967     return SetMenuItemInfo32_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
3968                                     lpmii, FALSE);
3969 }
3970
3971 /**********************************************************************
3972  *              SetMenuItemInfo32W    (USER32.492)
3973  */
3974 BOOL32 WINAPI SetMenuItemInfo32W(HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3975                                  const MENUITEMINFO32W *lpmii)
3976 {
3977     return SetMenuItemInfo32_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
3978                                     (const MENUITEMINFO32A*)lpmii, TRUE);
3979 }
3980
3981 /**********************************************************************
3982  *              SetMenuDefaultItem32    (USER32.489)
3983  */
3984 BOOL32 WINAPI SetMenuDefaultItem32(HMENU32 hmenu, UINT32 item, UINT32 bypos)
3985 {
3986     MENUITEM *menuitem = MENU_FindItem(&hmenu, &item, bypos);
3987     POPUPMENU *menu;
3988
3989     if (!menuitem) return FALSE;
3990     if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hmenu))) return FALSE;
3991
3992     menu->defitem = item; /* position */
3993
3994     debug_print_menuitem("SetMenuDefaultItem32: ", menuitem, "");
3995     FIXME(menu, "(0x%x,%d,%d), empty stub!\n",
3996                   hmenu, item, bypos);
3997     return TRUE;
3998 }
3999
4000 /**********************************************************************
4001  *              GetMenuDefaultItem32    (USER32.260)
4002  */
4003 UINT32 WINAPI GetMenuDefaultItem32(HMENU32 hmenu, UINT32 bypos, UINT32 flags)
4004 {
4005     POPUPMENU *menu;
4006
4007     if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hmenu)))
4008         return -1;
4009
4010     FIXME(menu, "(0x%x,%d,%d), stub!\n", hmenu, bypos, flags);
4011     if (bypos & MF_BYPOSITION)
4012         return menu->defitem;
4013     else {
4014         FIXME (menu, "default item 0x%x\n", menu->defitem);
4015         if ((menu->defitem > 0) && (menu->defitem < menu->nItems))
4016             return menu->items[menu->defitem].wID;
4017     }
4018     return -1;
4019 }
4020
4021 /*******************************************************************
4022  *              InsertMenuItem16   (USER.441)
4023  *
4024  * FIXME: untested
4025  */
4026 BOOL16 WINAPI InsertMenuItem16( HMENU16 hmenu, UINT16 pos, BOOL16 byposition,
4027                                 const MENUITEMINFO16 *mii )
4028 {
4029     MENUITEMINFO32A miia;
4030
4031     miia.cbSize        = sizeof(miia);
4032     miia.fMask         = mii->fMask;
4033     miia.dwTypeData    = mii->dwTypeData;
4034     miia.fType         = mii->fType;
4035     miia.fState        = mii->fState;
4036     miia.wID           = mii->wID;
4037     miia.hSubMenu      = mii->hSubMenu;
4038     miia.hbmpChecked   = mii->hbmpChecked;
4039     miia.hbmpUnchecked = mii->hbmpUnchecked;
4040     miia.dwItemData    = mii->dwItemData;
4041     miia.cch           = mii->cch;
4042     if (IS_STRING_ITEM(miia.fType))
4043         miia.dwTypeData = PTR_SEG_TO_LIN(miia.dwTypeData);
4044     return InsertMenuItem32A( hmenu, pos, byposition, &miia );
4045 }
4046
4047
4048 /**********************************************************************
4049  *              InsertMenuItem32A    (USER32.323)
4050  */
4051 BOOL32 WINAPI InsertMenuItem32A(HMENU32 hMenu, UINT32 uItem, BOOL32 bypos,
4052                                 const MENUITEMINFO32A *lpmii)
4053 {
4054     MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4055     return SetMenuItemInfo32_common(item, lpmii, FALSE);
4056 }
4057
4058
4059 /**********************************************************************
4060  *              InsertMenuItem32W    (USER32.324)
4061  */
4062 BOOL32 WINAPI InsertMenuItem32W(HMENU32 hMenu, UINT32 uItem, BOOL32 bypos,
4063                                 const MENUITEMINFO32W *lpmii)
4064 {
4065     MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4066     return SetMenuItemInfo32_common(item, (const MENUITEMINFO32A*)lpmii, TRUE);
4067 }
4068
4069 /**********************************************************************
4070  *              CheckMenuRadioItem32    (USER32.47)
4071  */
4072
4073 BOOL32 WINAPI CheckMenuRadioItem32(HMENU32 hMenu,
4074                                    UINT32 first, UINT32 last, UINT32 check,
4075                                    UINT32 bypos)
4076 {
4077      MENUITEM *mifirst, *milast, *micheck;
4078      HMENU32 mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
4079
4080      TRACE(menu, "ox%x: %d-%d, check %d, bypos=%d\n",
4081                   hMenu, first, last, check, bypos);
4082
4083      mifirst = MENU_FindItem (&mfirst, &first, bypos);
4084      milast = MENU_FindItem (&mlast, &last, bypos);
4085      micheck = MENU_FindItem (&mcheck, &check, bypos);
4086
4087      if (mifirst == NULL || milast == NULL || micheck == NULL ||
4088          mifirst > milast || mfirst != mlast || mfirst != mcheck ||
4089          micheck > milast || micheck < mifirst)
4090           return FALSE;
4091
4092      while (mifirst <= milast)
4093      {
4094           if (mifirst == micheck)
4095           {
4096                mifirst->fType |= MFT_RADIOCHECK;
4097                mifirst->fState |= MFS_CHECKED;
4098           } else {
4099                mifirst->fType &= ~MFT_RADIOCHECK;
4100                mifirst->fState &= ~MFS_CHECKED;
4101           }
4102           mifirst++;
4103      }
4104
4105      return TRUE;
4106 }
4107
4108 /**********************************************************************
4109  *              CheckMenuRadioItem16    (not a Windows API)
4110  */
4111
4112 BOOL16 WINAPI CheckMenuRadioItem16(HMENU16 hMenu,
4113                                    UINT16 first, UINT16 last, UINT16 check,
4114                                    BOOL16 bypos)
4115 {
4116      return CheckMenuRadioItem32 (hMenu, first, last, check, bypos);
4117 }
4118
4119 /**********************************************************************
4120  *              GetMenuItemRect32    (USER32.266)
4121  *
4122  *      ATTENTION: Here, the returned values in rect are the screen 
4123  *                 coordinates of the item just like if the menu was 
4124  *                 always on the upper left side of the application.
4125  *                 
4126  */
4127 BOOL32 WINAPI GetMenuItemRect32 (HWND32 hwnd, HMENU32 hMenu, UINT32 uItem,
4128                                  LPRECT32 rect)
4129 {
4130      POPUPMENU *itemMenu;
4131      MENUITEM *item;
4132      HWND32 referenceHwnd;
4133
4134      TRACE(menu, "(0x%x,0x%x,%d,%p)\n", hwnd, hMenu, uItem, rect);
4135
4136      item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4137      referenceHwnd = hwnd;
4138
4139      if(!hwnd)
4140      {
4141          itemMenu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hMenu);
4142          if (itemMenu == NULL) 
4143              return FALSE;
4144
4145          if(itemMenu->hWnd == NULL)
4146              return FALSE;
4147          referenceHwnd = itemMenu->hWnd;
4148      }
4149
4150      if ((rect == NULL) || (item == NULL)) 
4151          return FALSE;
4152
4153      *rect = item->rect;
4154
4155      MapWindowPoints32(referenceHwnd, 0, (LPPOINT32)rect, 2);
4156
4157      return TRUE;
4158 }
4159
4160 /**********************************************************************
4161  *              GetMenuItemRect16    (USER.665)
4162  */
4163
4164 BOOL16 WINAPI GetMenuItemRect16 (HWND16 hwnd, HMENU16 hMenu, UINT16 uItem,
4165                                  LPRECT16 rect)
4166 {
4167      RECT32 r32;
4168      BOOL32 res;
4169
4170      if (!rect) return FALSE;
4171      res = GetMenuItemRect32 (hwnd, hMenu, uItem, &r32);
4172      CONV_RECT32TO16 (&r32, rect);
4173      return res;
4174 }
4175
4176 /**********************************************************************
4177  *         SetMenuContextHelpId16    (USER.384)
4178  */
4179 BOOL16 WINAPI SetMenuContextHelpId16( HMENU16 hMenu, DWORD dwContextHelpId)
4180 {
4181         return SetMenuContextHelpId32( hMenu, dwContextHelpId );
4182 }
4183
4184
4185 /**********************************************************************
4186  *         SetMenuContextHelpId32    (USER32.488)
4187  */
4188 BOOL32 WINAPI SetMenuContextHelpId32( HMENU32 hMenu, DWORD dwContextHelpId)
4189 {
4190         FIXME(menu, "SetMenuContextHelpId, stub\n");
4191         return 0;
4192 }
4193
4194 /**********************************************************************
4195  *         GetMenuContextHelpId16    (USER.385)
4196  */
4197 DWORD WINAPI GetMenuContextHelpId16( HMENU16 hMenu )
4198 {
4199         return GetMenuContextHelpId16( hMenu );
4200 }
4201  
4202 /**********************************************************************
4203  *         GetMenuContextHelpId32    (USER32.488)
4204  */
4205 DWORD WINAPI GetMenuContextHelpId32( HMENU32 hMenu )
4206 {
4207         FIXME(menu, "GetMenuContextHelpId, stub\n");
4208         return 0;
4209 }
4210