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