Group commit for recovery after disk crash.
[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                     /* In win95 winelook, the selected menu item must be changed every time the
2343                        mouse moves. In Win31 winelook, the mouse button has to be held down */
2344                      
2345                     if ( (TWEAK_WineLook > WIN31_LOOK) ||
2346                          ( (msg.wParam & MK_LBUTTON) ||
2347                            ((wFlags & TPM_RIGHTBUTTON) && (msg.wParam & MK_RBUTTON))) )
2348
2349                         fEndMenu |= !MENU_MouseMove( &mt, hmenu );
2350
2351             } /* switch(msg.message) - mouse */
2352         }
2353         else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
2354         {
2355             fRemove = TRUE;  /* Keyboard messages are always removed */
2356             switch(msg.message)
2357             {
2358             case WM_KEYDOWN:
2359                 switch(msg.wParam)
2360                 {
2361                 case VK_HOME:
2362                 case VK_END:
2363                     MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, 
2364                                      NO_SELECTED_ITEM, FALSE );
2365                 /* fall through */
2366                 case VK_UP:
2367                     MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, 
2368                                        (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
2369                     break;
2370
2371                 case VK_DOWN: /* If on menu bar, pull-down the menu */
2372
2373                     menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( mt.hCurrentMenu );
2374                     if (!(menu->wFlags & MF_POPUP))
2375                         mt.hCurrentMenu = MENU_ShowSubPopup( mt.hOwnerWnd, mt.hTopMenu, TRUE );
2376                     else      /* otherwise try to move selection */
2377                         MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, ITEM_NEXT );
2378                     break;
2379
2380                 case VK_LEFT:
2381                     MENU_KeyLeft( &mt );
2382                     break;
2383                     
2384                 case VK_RIGHT:
2385                     MENU_KeyRight( &mt );
2386                     break;
2387                     
2388                 case VK_ESCAPE:
2389                     fEndMenu = TRUE;
2390                     break;
2391
2392                 default:
2393                     break;
2394                 }
2395                 break;  /* WM_KEYDOWN */
2396
2397             case WM_SYSKEYDOWN:
2398                 switch(msg.wParam)
2399                 {
2400                 case VK_MENU:
2401                     fEndMenu = TRUE;
2402                     break;
2403                     
2404                 }
2405                 break;  /* WM_SYSKEYDOWN */
2406
2407             case WM_CHAR:
2408                 {
2409                     UINT32      pos;
2410
2411                     if (msg.wParam == '\r' || msg.wParam == ' ')
2412                     {
2413                         fEndMenu |= !MENU_ExecFocusedItem( &mt, mt.hCurrentMenu );
2414                         break;
2415                     }
2416
2417                       /* Hack to avoid control chars. */
2418                       /* We will find a better way real soon... */
2419                     if ((msg.wParam <= 32) || (msg.wParam >= 127)) break;
2420
2421                     pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu, 
2422                                                         msg.wParam, FALSE );
2423                     if (pos == (UINT32)-2) fEndMenu = TRUE;
2424                     else if (pos == (UINT32)-1) MessageBeep32(0);
2425                     else
2426                     {
2427                         MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos, TRUE );
2428                         fEndMenu |= !MENU_ExecFocusedItem( &mt, mt.hCurrentMenu );
2429                     }
2430                 }                   
2431                 break;
2432             }  /* switch(msg.message) - kbd */
2433         }
2434         else
2435         {
2436             DispatchMessage16( &msg );
2437         }
2438
2439         if (!fEndMenu) fRemove = TRUE;
2440
2441         /* finally remove message from the queue */
2442
2443         if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
2444             PeekMessage16( &msg, 0, msg.message, msg.message, PM_REMOVE );
2445         else mt.trackFlags &= ~TF_SKIPREMOVE;
2446     }
2447
2448     ReleaseCapture();
2449     if( IsWindow32( mt.hOwnerWnd ) )
2450     {
2451         MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE );
2452
2453         menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( mt.hTopMenu );
2454         if (menu && menu->wFlags & MF_POPUP) 
2455         {
2456             ShowWindow32( menu->hWnd, SW_HIDE );
2457             uSubPWndLevel = 0;
2458         }
2459         MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE );
2460         SendMessage16( mt.hOwnerWnd, WM_MENUSELECT, 0, MAKELONG( 0xffff, 0 ) );
2461     }
2462     fEndMenu = FALSE;
2463     return TRUE;
2464 }
2465
2466 /***********************************************************************
2467  *           MENU_InitTracking
2468  */
2469 static BOOL32 MENU_InitTracking(HWND32 hWnd, HMENU32 hMenu)
2470 {
2471     HideCaret32(0);
2472     SendMessage16( hWnd, WM_ENTERMENULOOP, 0, 0 );
2473     SendMessage16( hWnd, WM_SETCURSOR, hWnd, HTCAPTION );
2474     SendMessage16( hWnd, WM_INITMENU, hMenu, 0 );
2475     return TRUE;
2476 }
2477
2478 /***********************************************************************
2479  *           MENU_TrackMouseMenuBar
2480  *
2481  * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
2482  */
2483 void MENU_TrackMouseMenuBar( WND* wndPtr, INT32 ht, POINT32 pt )
2484 {
2485     HWND32  hWnd = wndPtr->hwndSelf;
2486     HMENU32 hMenu = (ht == HTSYSMENU) ? wndPtr->hSysMenu : wndPtr->wIDmenu;
2487
2488     if (IsMenu32(hMenu))
2489     {
2490         MENU_InitTracking( hWnd, hMenu );
2491         MENU_TrackMenu( hMenu, TPM_ENTERIDLEEX | TPM_BUTTONDOWN | 
2492                         TPM_LEFTALIGN | TPM_LEFTBUTTON, pt.x, pt.y, hWnd, NULL );
2493
2494         SendMessage16( hWnd, WM_EXITMENULOOP, 0, 0 );
2495         ShowCaret32(0);
2496     }
2497 }
2498
2499
2500 /***********************************************************************
2501  *           MENU_TrackKbdMenuBar
2502  *
2503  * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
2504  */
2505 void MENU_TrackKbdMenuBar( WND* wndPtr, UINT32 wParam, INT32 vkey)
2506 {
2507    UINT32 uItem = NO_SELECTED_ITEM;
2508    HMENU32 hTrackMenu; 
2509
2510     /* find window that has a menu */
2511  
2512     while( wndPtr->dwStyle & WS_CHILD && !(wndPtr->dwStyle & WS_SYSMENU) )
2513         if( !(wndPtr = wndPtr->parent) ) return;
2514
2515     /* check if we have to track a system menu */
2516           
2517     if( (wndPtr->dwStyle & (WS_CHILD | WS_MINIMIZE)) || 
2518         !wndPtr->wIDmenu || vkey == VK_SPACE )
2519     {
2520         if( !(wndPtr->dwStyle & WS_SYSMENU) ) return;
2521         hTrackMenu = wndPtr->hSysMenu;
2522         uItem = 0;
2523         wParam |= HTSYSMENU;    /* prevent item lookup */
2524     }
2525     else
2526         hTrackMenu = wndPtr->wIDmenu;
2527
2528     if (IsMenu32( hTrackMenu ))
2529     {
2530         MENU_InitTracking( wndPtr->hwndSelf, hTrackMenu );
2531
2532         if( vkey && vkey != VK_SPACE )
2533         {
2534             uItem = MENU_FindItemByKey( wndPtr->hwndSelf, hTrackMenu, 
2535                                         vkey, (wParam & HTSYSMENU) );
2536             if( uItem >= (UINT32)(-2) )
2537             {
2538                 if( uItem == (UINT32)(-1) ) MessageBeep32(0);
2539                 hTrackMenu = 0;
2540             }
2541         }
2542
2543         if( hTrackMenu )
2544         {
2545             MENU_SelectItem( wndPtr->hwndSelf, hTrackMenu, uItem, TRUE );
2546
2547             if( uItem == NO_SELECTED_ITEM )
2548                 MENU_MoveSelection( wndPtr->hwndSelf, hTrackMenu, ITEM_NEXT );
2549             else if( vkey )
2550                 PostMessage16( wndPtr->hwndSelf, WM_KEYDOWN, VK_DOWN, 0L );
2551
2552             MENU_TrackMenu( hTrackMenu, TPM_ENTERIDLEEX | TPM_LEFTALIGN | TPM_LEFTBUTTON,
2553                             0, 0, wndPtr->hwndSelf, NULL );
2554         }
2555         SendMessage16( wndPtr->hwndSelf, WM_EXITMENULOOP, 0, 0 );
2556         ShowCaret32(0);
2557     }
2558 }
2559
2560
2561 /**********************************************************************
2562  *           TrackPopupMenu16   (USER.416)
2563  */
2564 BOOL16 WINAPI TrackPopupMenu16( HMENU16 hMenu, UINT16 wFlags, INT16 x, INT16 y,
2565                            INT16 nReserved, HWND16 hWnd, const RECT16 *lpRect )
2566 {
2567     RECT32 r;
2568     if (lpRect)
2569          CONV_RECT16TO32( lpRect, &r );
2570     return TrackPopupMenu32( hMenu, wFlags, x, y, nReserved, hWnd,
2571                              lpRect ? &r : NULL );
2572 }
2573
2574
2575 /**********************************************************************
2576  *           TrackPopupMenu32   (USER32.549)
2577  */
2578 BOOL32 WINAPI TrackPopupMenu32( HMENU32 hMenu, UINT32 wFlags, INT32 x, INT32 y,
2579                            INT32 nReserved, HWND32 hWnd, const RECT32 *lpRect )
2580 {
2581     BOOL32 ret = FALSE;
2582
2583     HideCaret32(0);
2584     SendMessage16( hWnd, WM_INITMENUPOPUP, (WPARAM16)hMenu, 0);
2585     if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 )) 
2586         ret = MENU_TrackMenu( hMenu, wFlags & ~TPM_INTERNAL, 0, 0, hWnd, lpRect );
2587     ShowCaret32(0);
2588     return ret;
2589 }
2590
2591 /**********************************************************************
2592  *           TrackPopupMenuEx   (USER32.550)
2593  */
2594 BOOL32 WINAPI TrackPopupMenuEx( HMENU32 hMenu, UINT32 wFlags, INT32 x, INT32 y,
2595                                 HWND32 hWnd, LPTPMPARAMS lpTpm )
2596 {
2597     FIXME(menu, "not fully implemented\n" );
2598     return TrackPopupMenu32( hMenu, wFlags, x, y, 0, hWnd,
2599                              lpTpm ? &lpTpm->rcExclude : NULL );
2600 }
2601
2602 /***********************************************************************
2603  *           PopupMenuWndProc
2604  *
2605  * NOTE: Windows has totally different (and undocumented) popup wndproc.
2606  */
2607 LRESULT WINAPI PopupMenuWndProc( HWND32 hwnd, UINT32 message, WPARAM32 wParam,
2608                                  LPARAM lParam )
2609 {    
2610     WND* wndPtr = WIN_FindWndPtr(hwnd);
2611
2612     switch(message)
2613     {
2614     case WM_CREATE:
2615         {
2616             CREATESTRUCT32A *cs = (CREATESTRUCT32A*)lParam;
2617             SetWindowLong32A( hwnd, 0, (LONG)cs->lpCreateParams );
2618             return 0;
2619         }
2620
2621     case WM_MOUSEACTIVATE:  /* We don't want to be activated */
2622         return MA_NOACTIVATE;
2623
2624     case WM_PAINT:
2625         {
2626             PAINTSTRUCT32 ps;
2627             BeginPaint32( hwnd, &ps );
2628             MENU_DrawPopupMenu( hwnd, ps.hdc,
2629                                 (HMENU32)GetWindowLong32A( hwnd, 0 ) );
2630             EndPaint32( hwnd, &ps );
2631             return 0;
2632         }
2633     case WM_ERASEBKGND:
2634         return 1;
2635
2636     case WM_DESTROY:
2637
2638         /* zero out global pointer in case resident popup window
2639          * was somehow destroyed. */
2640
2641         if( pTopPopupWnd )
2642         {
2643             if( hwnd == pTopPopupWnd->hwndSelf )
2644             {
2645                 ERR(menu, "resident popup destroyed!\n");
2646
2647                 pTopPopupWnd = NULL;
2648                 uSubPWndLevel = 0;
2649             }
2650             else
2651                 uSubPWndLevel--;
2652         }
2653         break;
2654
2655     case WM_SHOWWINDOW:
2656
2657         if( wParam )
2658         {
2659             if( !(*(HMENU32*)wndPtr->wExtra) )
2660                 ERR(menu,"no menu to display\n");
2661         }
2662         else
2663             *(HMENU32*)wndPtr->wExtra = 0;
2664         break;
2665
2666     case MM_SETMENUHANDLE:
2667
2668         *(HMENU32*)wndPtr->wExtra = (HMENU32)wParam;
2669         break;
2670
2671     case MM_GETMENUHANDLE:
2672
2673         return *(HMENU32*)wndPtr->wExtra;
2674
2675     default:
2676         return DefWindowProc32A( hwnd, message, wParam, lParam );
2677     }
2678     return 0;
2679 }
2680
2681
2682 /***********************************************************************
2683  *           MENU_GetMenuBarHeight
2684  *
2685  * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
2686  */
2687 UINT32 MENU_GetMenuBarHeight( HWND32 hwnd, UINT32 menubarWidth,
2688                               INT32 orgX, INT32 orgY )
2689 {
2690     HDC32 hdc;
2691     RECT32 rectBar;
2692     WND *wndPtr;
2693     LPPOPUPMENU lppop;
2694
2695     TRACE(menu, "HWND 0x%x, width %d, "
2696                   "at (%d, %d).\n", hwnd, menubarWidth, orgX, orgY );
2697     
2698     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
2699     if (!(lppop = (LPPOPUPMENU)USER_HEAP_LIN_ADDR((HMENU16)wndPtr->wIDmenu)))
2700       return 0;
2701     hdc = GetDCEx32( hwnd, 0, DCX_CACHE | DCX_WINDOW );
2702     SetRect32(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+SYSMETRICS_CYMENU);
2703     MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
2704     ReleaseDC32( hwnd, hdc );
2705     return lppop->Height;
2706 }
2707
2708
2709 /*******************************************************************
2710  *         ChangeMenu16    (USER.153)
2711  */
2712 BOOL16 WINAPI ChangeMenu16( HMENU16 hMenu, UINT16 pos, SEGPTR data,
2713                             UINT16 id, UINT16 flags )
2714 {
2715     TRACE(menu,"menu=%04x pos=%d data=%08lx id=%04x flags=%04x\n",
2716                   hMenu, pos, (DWORD)data, id, flags );
2717     if (flags & MF_APPEND) return AppendMenu16( hMenu, flags & ~MF_APPEND,
2718                                                 id, data );
2719
2720     /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
2721     /* for MF_DELETE. We should check the parameters for all others */
2722     /* MF_* actions also (anybody got a doc on ChangeMenu?). */
2723
2724     if (flags & MF_DELETE) return DeleteMenu16(hMenu, pos, flags & ~MF_DELETE);
2725     if (flags & MF_CHANGE) return ModifyMenu16(hMenu, pos, flags & ~MF_CHANGE,
2726                                                id, data );
2727     if (flags & MF_REMOVE) return RemoveMenu16(hMenu,
2728                                               flags & MF_BYPOSITION ? pos : id,
2729                                               flags & ~MF_REMOVE );
2730     /* Default: MF_INSERT */
2731     return InsertMenu16( hMenu, pos, flags, id, data );
2732 }
2733
2734
2735 /*******************************************************************
2736  *         ChangeMenu32A    (USER32.23)
2737  */
2738 BOOL32 WINAPI ChangeMenu32A( HMENU32 hMenu, UINT32 pos, LPCSTR data,
2739                              UINT32 id, UINT32 flags )
2740 {
2741     TRACE(menu,"menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
2742                   hMenu, pos, (DWORD)data, id, flags );
2743     if (flags & MF_APPEND) return AppendMenu32A( hMenu, flags & ~MF_APPEND,
2744                                                  id, data );
2745     if (flags & MF_DELETE) return DeleteMenu32(hMenu, pos, flags & ~MF_DELETE);
2746     if (flags & MF_CHANGE) return ModifyMenu32A(hMenu, pos, flags & ~MF_CHANGE,
2747                                                 id, data );
2748     if (flags & MF_REMOVE) return RemoveMenu32( hMenu,
2749                                               flags & MF_BYPOSITION ? pos : id,
2750                                               flags & ~MF_REMOVE );
2751     /* Default: MF_INSERT */
2752     return InsertMenu32A( hMenu, pos, flags, id, data );
2753 }
2754
2755
2756 /*******************************************************************
2757  *         ChangeMenu32W    (USER32.24)
2758  */
2759 BOOL32 WINAPI ChangeMenu32W( HMENU32 hMenu, UINT32 pos, LPCWSTR data,
2760                              UINT32 id, UINT32 flags )
2761 {
2762     TRACE(menu,"menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
2763                   hMenu, pos, (DWORD)data, id, flags );
2764     if (flags & MF_APPEND) return AppendMenu32W( hMenu, flags & ~MF_APPEND,
2765                                                  id, data );
2766     if (flags & MF_DELETE) return DeleteMenu32(hMenu, pos, flags & ~MF_DELETE);
2767     if (flags & MF_CHANGE) return ModifyMenu32W(hMenu, pos, flags & ~MF_CHANGE,
2768                                                 id, data );
2769     if (flags & MF_REMOVE) return RemoveMenu32( hMenu,
2770                                               flags & MF_BYPOSITION ? pos : id,
2771                                               flags & ~MF_REMOVE );
2772     /* Default: MF_INSERT */
2773     return InsertMenu32W( hMenu, pos, flags, id, data );
2774 }
2775
2776
2777 /*******************************************************************
2778  *         CheckMenuItem16    (USER.154)
2779  */
2780 BOOL16 WINAPI CheckMenuItem16( HMENU16 hMenu, UINT16 id, UINT16 flags )
2781 {
2782     return (BOOL16)CheckMenuItem32( hMenu, id, flags );
2783 }
2784
2785
2786 /*******************************************************************
2787  *         CheckMenuItem32    (USER32.46)
2788  */
2789 DWORD WINAPI CheckMenuItem32( HMENU32 hMenu, UINT32 id, UINT32 flags )
2790 {
2791     MENUITEM *item;
2792     DWORD ret;
2793
2794     TRACE(menu,"%04x %04x %04x\n", hMenu, id, flags );
2795     if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
2796     ret = item->fState & MF_CHECKED;
2797     if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
2798     else item->fState &= ~MF_CHECKED;
2799     return ret;
2800 }
2801
2802
2803 /**********************************************************************
2804  *         EnableMenuItem16    (USER.155)
2805  */
2806 UINT16 WINAPI EnableMenuItem16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
2807 {
2808     return EnableMenuItem32( hMenu, wItemID, wFlags );
2809 }
2810
2811
2812 /**********************************************************************
2813  *         EnableMenuItem32    (USER32.170)
2814  */
2815 UINT32 WINAPI EnableMenuItem32( HMENU32 hMenu, UINT32 wItemID, UINT32 wFlags )
2816 {
2817     UINT32    oldflags;
2818     MENUITEM *item;
2819
2820     TRACE(menu,"(%04x, %04X, %04X) !\n", 
2821                  hMenu, wItemID, wFlags);
2822
2823     if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
2824         return (UINT32)-1;
2825
2826     oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
2827     item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
2828     return oldflags;
2829 }
2830
2831
2832 /*******************************************************************
2833  *         GetMenuString16    (USER.161)
2834  */
2835 INT16 WINAPI GetMenuString16( HMENU16 hMenu, UINT16 wItemID,
2836                               LPSTR str, INT16 nMaxSiz, UINT16 wFlags )
2837 {
2838     return GetMenuString32A( hMenu, wItemID, str, nMaxSiz, wFlags );
2839 }
2840
2841
2842 /*******************************************************************
2843  *         GetMenuString32A    (USER32.268)
2844  */
2845 INT32 WINAPI GetMenuString32A( HMENU32 hMenu, UINT32 wItemID,
2846                                LPSTR str, INT32 nMaxSiz, UINT32 wFlags )
2847 {
2848     MENUITEM *item;
2849
2850     TRACE(menu, "menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
2851                  hMenu, wItemID, str, nMaxSiz, wFlags );
2852     if (!str || !nMaxSiz) return 0;
2853     str[0] = '\0';
2854     if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
2855     if (!IS_STRING_ITEM(item->fType)) return 0;
2856     lstrcpyn32A( str, item->text, nMaxSiz );
2857     TRACE(menu, "returning '%s'\n", str );
2858     return strlen(str);
2859 }
2860
2861
2862 /*******************************************************************
2863  *         GetMenuString32W    (USER32.269)
2864  */
2865 INT32 WINAPI GetMenuString32W( HMENU32 hMenu, UINT32 wItemID,
2866                                LPWSTR str, INT32 nMaxSiz, UINT32 wFlags )
2867 {
2868     MENUITEM *item;
2869
2870     TRACE(menu, "menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
2871                  hMenu, wItemID, str, nMaxSiz, wFlags );
2872     if (!str || !nMaxSiz) return 0;
2873     str[0] = '\0';
2874     if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
2875     if (!IS_STRING_ITEM(item->fType)) return 0;
2876     lstrcpynAtoW( str, item->text, nMaxSiz );
2877     return lstrlen32W(str);
2878 }
2879
2880
2881 /**********************************************************************
2882  *         HiliteMenuItem16    (USER.162)
2883  */
2884 BOOL16 WINAPI HiliteMenuItem16( HWND16 hWnd, HMENU16 hMenu, UINT16 wItemID,
2885                                 UINT16 wHilite )
2886 {
2887     return HiliteMenuItem32( hWnd, hMenu, wItemID, wHilite );
2888 }
2889
2890
2891 /**********************************************************************
2892  *         HiliteMenuItem32    (USER32.318)
2893  */
2894 BOOL32 WINAPI HiliteMenuItem32( HWND32 hWnd, HMENU32 hMenu, UINT32 wItemID,
2895                                 UINT32 wHilite )
2896 {
2897     LPPOPUPMENU menu;
2898     TRACE(menu,"(%04x, %04x, %04x, %04x);\n", 
2899                  hWnd, hMenu, wItemID, wHilite);
2900     if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
2901     if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
2902     if (menu->FocusedItem == wItemID) return TRUE;
2903     MENU_HideSubPopups( hWnd, hMenu, FALSE );
2904     MENU_SelectItem( hWnd, hMenu, wItemID, TRUE );
2905     return TRUE;
2906 }
2907
2908
2909 /**********************************************************************
2910  *         GetMenuState16    (USER.250)
2911  */
2912 UINT16 WINAPI GetMenuState16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
2913 {
2914     return GetMenuState32( hMenu, wItemID, wFlags );
2915 }
2916
2917
2918 /**********************************************************************
2919  *         GetMenuState32    (USER32.267)
2920  */
2921 UINT32 WINAPI GetMenuState32( HMENU32 hMenu, UINT32 wItemID, UINT32 wFlags )
2922 {
2923     MENUITEM *item;
2924     TRACE(menu,"(%04x, %04x, %04x);\n", 
2925                  hMenu, wItemID, wFlags);
2926     if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
2927     debug_print_menuitem ("  item: ", item, "");
2928     if (item->fType & MF_POPUP)
2929     {
2930         POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( item->hSubMenu );
2931         if (!menu) return -1;
2932         else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
2933     }
2934     else
2935     {
2936         /* We used to (from way back then) mask the result to 0xff.  */
2937         /* I don't know why and it seems wrong as the documented */
2938         /* return flag MF_SEPARATOR is outside that mask.  */
2939         return (item->fType | item->fState);
2940     }
2941 }
2942
2943
2944 /**********************************************************************
2945  *         GetMenuItemCount16    (USER.263)
2946  */
2947 INT16 WINAPI GetMenuItemCount16( HMENU16 hMenu )
2948 {
2949     LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
2950     if (!IS_A_MENU(menu)) return -1;
2951     TRACE(menu,"(%04x) returning %d\n", 
2952                   hMenu, menu->nItems );
2953     return menu->nItems;
2954 }
2955
2956
2957 /**********************************************************************
2958  *         GetMenuItemCount32    (USER32.262)
2959  */
2960 INT32 WINAPI GetMenuItemCount32( HMENU32 hMenu )
2961 {
2962     LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
2963     if (!IS_A_MENU(menu)) return -1;
2964     TRACE(menu,"(%04x) returning %d\n", 
2965                   hMenu, menu->nItems );
2966     return menu->nItems;
2967 }
2968
2969
2970 /**********************************************************************
2971  *         GetMenuItemID16    (USER.264)
2972  */
2973 UINT16 WINAPI GetMenuItemID16( HMENU16 hMenu, INT16 nPos )
2974 {
2975     LPPOPUPMENU menu;
2976
2977     if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return -1;
2978     if ((nPos < 0) || ((UINT16) nPos >= menu->nItems)) return -1;
2979     if (menu->items[nPos].fType & MF_POPUP) return -1;
2980     return menu->items[nPos].wID;
2981 }
2982
2983
2984 /**********************************************************************
2985  *         GetMenuItemID32    (USER32.263)
2986  */
2987 UINT32 WINAPI GetMenuItemID32( HMENU32 hMenu, INT32 nPos )
2988 {
2989     LPPOPUPMENU menu;
2990
2991     if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return -1;
2992     if ((nPos < 0) || (nPos >= menu->nItems)) return -1;
2993     if (menu->items[nPos].fType & MF_POPUP) return -1; 
2994     return menu->items[nPos].wID;
2995 }
2996
2997
2998 /*******************************************************************
2999  *         InsertMenu16    (USER.410)
3000  */
3001 BOOL16 WINAPI InsertMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
3002                             UINT16 id, SEGPTR data )
3003 {
3004     UINT32 pos32 = (UINT32)pos;
3005     if ((pos == (UINT16)-1) && (flags & MF_BYPOSITION)) pos32 = (UINT32)-1;
3006     if (IS_STRING_ITEM(flags) && data)
3007         return InsertMenu32A( hMenu, pos32, flags, id,
3008                               (LPSTR)PTR_SEG_TO_LIN(data) );
3009     return InsertMenu32A( hMenu, pos32, flags, id, (LPSTR)data );
3010 }
3011
3012
3013 /*******************************************************************
3014  *         InsertMenu32A    (USER32.322)
3015  */
3016 BOOL32 WINAPI InsertMenu32A( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3017                              UINT32 id, LPCSTR str )
3018 {
3019     MENUITEM *item;
3020
3021     if (IS_STRING_ITEM(flags) && str)
3022         TRACE(menu, "hMenu %04x, pos %d, flags %08x, "
3023                       "id %04x, str '%s'\n",
3024                       hMenu, pos, flags, id, str );
3025     else TRACE(menu, "hMenu %04x, pos %d, flags %08x, "
3026                        "id %04x, str %08lx (not a string)\n",
3027                        hMenu, pos, flags, id, (DWORD)str );
3028
3029     if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3030
3031     if (!(MENU_SetItemData( item, flags, id, str )))
3032     {
3033         RemoveMenu32( hMenu, pos, flags );
3034         return FALSE;
3035     }
3036
3037     if (flags & MF_POPUP)  /* Set the MF_POPUP flag on the popup-menu */
3038         ((POPUPMENU *)USER_HEAP_LIN_ADDR((HMENU16)id))->wFlags |= MF_POPUP;
3039
3040     item->hCheckBit = item->hUnCheckBit = 0;
3041     return TRUE;
3042 }
3043
3044
3045 /*******************************************************************
3046  *         InsertMenu32W    (USER32.325)
3047  */
3048 BOOL32 WINAPI InsertMenu32W( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3049                              UINT32 id, LPCWSTR str )
3050 {
3051     BOOL32 ret;
3052
3053     if (IS_STRING_ITEM(flags) && str)
3054     {
3055         LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3056         ret = InsertMenu32A( hMenu, pos, flags, id, newstr );
3057         HeapFree( GetProcessHeap(), 0, newstr );
3058         return ret;
3059     }
3060     else return InsertMenu32A( hMenu, pos, flags, id, (LPCSTR)str );
3061 }
3062
3063
3064 /*******************************************************************
3065  *         AppendMenu16    (USER.411)
3066  */
3067 BOOL16 WINAPI AppendMenu16(HMENU16 hMenu, UINT16 flags, UINT16 id, SEGPTR data)
3068 {
3069     return InsertMenu16( hMenu, -1, flags | MF_BYPOSITION, id, data );
3070 }
3071
3072
3073 /*******************************************************************
3074  *         AppendMenu32A    (USER32.5)
3075  */
3076 BOOL32 WINAPI AppendMenu32A( HMENU32 hMenu, UINT32 flags,
3077                              UINT32 id, LPCSTR data )
3078 {
3079     return InsertMenu32A( hMenu, -1, flags | MF_BYPOSITION, id, data );
3080 }
3081
3082
3083 /*******************************************************************
3084  *         AppendMenu32W    (USER32.6)
3085  */
3086 BOOL32 WINAPI AppendMenu32W( HMENU32 hMenu, UINT32 flags,
3087                              UINT32 id, LPCWSTR data )
3088 {
3089     return InsertMenu32W( hMenu, -1, flags | MF_BYPOSITION, id, data );
3090 }
3091
3092
3093 /**********************************************************************
3094  *         RemoveMenu16    (USER.412)
3095  */
3096 BOOL16 WINAPI RemoveMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3097 {
3098     return RemoveMenu32( hMenu, nPos, wFlags );
3099 }
3100
3101
3102 /**********************************************************************
3103  *         RemoveMenu32    (USER32.441)
3104  */
3105 BOOL32 WINAPI RemoveMenu32( HMENU32 hMenu, UINT32 nPos, UINT32 wFlags )
3106 {
3107     LPPOPUPMENU menu;
3108     MENUITEM *item;
3109
3110     TRACE(menu,"(%04x, %04x, %04x)\n",hMenu, nPos, wFlags);
3111     if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3112     if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
3113     
3114       /* Remove item */
3115
3116     MENU_FreeItemData( item );
3117
3118     if (--menu->nItems == 0)
3119     {
3120         HeapFree( SystemHeap, 0, menu->items );
3121         menu->items = NULL;
3122     }
3123     else
3124     {
3125         while(nPos < menu->nItems)
3126         {
3127             *item = *(item+1);
3128             item++;
3129             nPos++;
3130         }
3131         menu->items = HeapReAlloc( SystemHeap, 0, menu->items,
3132                                    menu->nItems * sizeof(MENUITEM) );
3133     }
3134     return TRUE;
3135 }
3136
3137
3138 /**********************************************************************
3139  *         DeleteMenu16    (USER.413)
3140  */
3141 BOOL16 WINAPI DeleteMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3142 {
3143     return DeleteMenu32( hMenu, nPos, wFlags );
3144 }
3145
3146
3147 /**********************************************************************
3148  *         DeleteMenu32    (USER32.129)
3149  */
3150 BOOL32 WINAPI DeleteMenu32( HMENU32 hMenu, UINT32 nPos, UINT32 wFlags )
3151 {
3152     MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3153     if (!item) return FALSE;
3154     if (item->fType & MF_POPUP) DestroyMenu32( item->hSubMenu );
3155       /* nPos is now the position of the item */
3156     RemoveMenu32( hMenu, nPos, wFlags | MF_BYPOSITION );
3157     return TRUE;
3158 }
3159
3160
3161 /*******************************************************************
3162  *         ModifyMenu16    (USER.414)
3163  */
3164 BOOL16 WINAPI ModifyMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
3165                             UINT16 id, SEGPTR data )
3166 {
3167     if (IS_STRING_ITEM(flags))
3168         return ModifyMenu32A( hMenu, pos, flags, id,
3169                               (LPSTR)PTR_SEG_TO_LIN(data) );
3170     return ModifyMenu32A( hMenu, pos, flags, id, (LPSTR)data );
3171 }
3172
3173
3174 /*******************************************************************
3175  *         ModifyMenu32A    (USER32.397)
3176  */
3177 BOOL32 WINAPI ModifyMenu32A( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3178                              UINT32 id, LPCSTR str )
3179 {
3180     MENUITEM *item;
3181
3182     if (IS_STRING_ITEM(flags))
3183     {
3184         TRACE(menu, "%04x %d %04x %04x '%s'\n",
3185                       hMenu, pos, flags, id, str ? str : "#NULL#" );
3186         if (!str) return FALSE;
3187     }
3188     else
3189     {
3190         TRACE(menu, "%04x %d %04x %04x %08lx\n",
3191                       hMenu, pos, flags, id, (DWORD)str );
3192     }
3193
3194     if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3195     return MENU_SetItemData( item, flags, id, str );
3196 }
3197
3198
3199 /*******************************************************************
3200  *         ModifyMenu32W    (USER32.398)
3201  */
3202 BOOL32 WINAPI ModifyMenu32W( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3203                              UINT32 id, LPCWSTR str )
3204 {
3205     BOOL32 ret;
3206
3207     if (IS_STRING_ITEM(flags) && str)
3208     {
3209         LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3210         ret = ModifyMenu32A( hMenu, pos, flags, id, newstr );
3211         HeapFree( GetProcessHeap(), 0, newstr );
3212         return ret;
3213     }
3214     else return ModifyMenu32A( hMenu, pos, flags, id, (LPCSTR)str );
3215 }
3216
3217
3218 /**********************************************************************
3219  *         CreatePopupMenu16    (USER.415)
3220  */
3221 HMENU16 WINAPI CreatePopupMenu16(void)
3222 {
3223     return CreatePopupMenu32();
3224 }
3225
3226
3227 /**********************************************************************
3228  *         CreatePopupMenu32    (USER32.82)
3229  */
3230 HMENU32 WINAPI CreatePopupMenu32(void)
3231 {
3232     HMENU32 hmenu;
3233     POPUPMENU *menu;
3234
3235     if (!(hmenu = CreateMenu32())) return 0;
3236     menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
3237     menu->wFlags |= MF_POPUP;
3238     return hmenu;
3239 }
3240
3241
3242 /**********************************************************************
3243  *         GetMenuCheckMarkDimensions    (USER.417) (USER32.258)
3244  */
3245 DWORD WINAPI GetMenuCheckMarkDimensions(void)
3246 {
3247     return MAKELONG( check_bitmap_width, check_bitmap_height );
3248 }
3249
3250
3251 /**********************************************************************
3252  *         SetMenuItemBitmaps16    (USER.418)
3253  */
3254 BOOL16 WINAPI SetMenuItemBitmaps16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags,
3255                                     HBITMAP16 hNewUnCheck, HBITMAP16 hNewCheck)
3256 {
3257     return SetMenuItemBitmaps32( hMenu, nPos, wFlags, hNewUnCheck, hNewCheck );
3258 }
3259
3260
3261 /**********************************************************************
3262  *         SetMenuItemBitmaps32    (USER32.490)
3263  */
3264 BOOL32 WINAPI SetMenuItemBitmaps32( HMENU32 hMenu, UINT32 nPos, UINT32 wFlags,
3265                                     HBITMAP32 hNewUnCheck, HBITMAP32 hNewCheck)
3266 {
3267     MENUITEM *item;
3268     TRACE(menu,"(%04x, %04x, %04x, %04x, %04x)\n",
3269                  hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3270     if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3271
3272     if (!hNewCheck && !hNewUnCheck)
3273     {
3274         item->fState &= ~MF_USECHECKBITMAPS;
3275     }
3276     else  /* Install new bitmaps */
3277     {
3278         item->hCheckBit = hNewCheck;
3279         item->hUnCheckBit = hNewUnCheck;
3280         item->fState |= MF_USECHECKBITMAPS;
3281     }
3282     return TRUE;
3283 }
3284
3285
3286 /**********************************************************************
3287  *         CreateMenu16    (USER.151)
3288  */
3289 HMENU16 WINAPI CreateMenu16(void)
3290 {
3291     return CreateMenu32();
3292 }
3293
3294
3295 /**********************************************************************
3296  *         CreateMenu32    (USER32.81)
3297  */
3298 HMENU32 WINAPI CreateMenu32(void)
3299 {
3300     HMENU32 hMenu;
3301     LPPOPUPMENU menu;
3302     if (!(hMenu = USER_HEAP_ALLOC( sizeof(POPUPMENU) ))) return 0;
3303     menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3304     menu->wFlags = 0;
3305     menu->wMagic = MENU_MAGIC;
3306     menu->hTaskQ = 0;
3307     menu->Width  = 0;
3308     menu->Height = 0;
3309     menu->nItems = 0;
3310     menu->hWnd   = 0;
3311     menu->items  = NULL;
3312     menu->FocusedItem = NO_SELECTED_ITEM;
3313     TRACE(menu, "return %04x\n", hMenu );
3314     return hMenu;
3315 }
3316
3317
3318 /**********************************************************************
3319  *         DestroyMenu16    (USER.152)
3320  */
3321 BOOL16 WINAPI DestroyMenu16( HMENU16 hMenu )
3322 {
3323     return DestroyMenu32( hMenu );
3324 }
3325
3326
3327 /**********************************************************************
3328  *         DestroyMenu32    (USER32.134)
3329  */
3330 BOOL32 WINAPI DestroyMenu32( HMENU32 hMenu )
3331 {
3332     TRACE(menu,"(%04x)\n", hMenu);
3333
3334     /* Silently ignore attempts to destroy default system popup */
3335
3336     if (hMenu && hMenu != MENU_DefSysPopup)
3337     {
3338         LPPOPUPMENU lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3339
3340         if( pTopPopupWnd && (hMenu == *(HMENU32*)pTopPopupWnd->wExtra) )
3341           *(UINT32*)pTopPopupWnd->wExtra = 0;
3342
3343         if (IS_A_MENU( lppop ))
3344         {
3345             lppop->wMagic = 0;  /* Mark it as destroyed */
3346
3347             if ((lppop->wFlags & MF_POPUP) && lppop->hWnd &&
3348                 (!pTopPopupWnd || (lppop->hWnd != pTopPopupWnd->hwndSelf)))
3349                 DestroyWindow32( lppop->hWnd );
3350
3351             if (lppop->items)   /* recursively destroy submenus */
3352             {
3353                 int i;
3354                 MENUITEM *item = lppop->items;
3355                 for (i = lppop->nItems; i > 0; i--, item++)
3356                 {
3357                     if (item->fType & MF_POPUP) DestroyMenu32(item->hSubMenu);
3358                     MENU_FreeItemData( item );
3359                 }
3360                 HeapFree( SystemHeap, 0, lppop->items );
3361             }
3362             USER_HEAP_FREE( hMenu );
3363         }
3364         else return FALSE;
3365     }
3366     return (hMenu != MENU_DefSysPopup);
3367 }
3368
3369
3370 /**********************************************************************
3371  *         GetSystemMenu16    (USER.156)
3372  */
3373 HMENU16 WINAPI GetSystemMenu16( HWND16 hWnd, BOOL16 bRevert )
3374 {
3375     return GetSystemMenu32( hWnd, bRevert );
3376 }
3377
3378
3379 /**********************************************************************
3380  *         GetSystemMenu32    (USER32.291)
3381  */
3382 HMENU32 WINAPI GetSystemMenu32( HWND32 hWnd, BOOL32 bRevert )
3383 {
3384     WND *wndPtr = WIN_FindWndPtr( hWnd );
3385
3386     if (wndPtr)
3387     {
3388         if( wndPtr->hSysMenu )
3389         {
3390             if( bRevert )
3391             {
3392                 DestroyMenu32(wndPtr->hSysMenu); 
3393                 wndPtr->hSysMenu = 0;
3394             }
3395             else
3396             {
3397                 POPUPMENU *menu = (POPUPMENU*) 
3398                            USER_HEAP_LIN_ADDR(wndPtr->hSysMenu);
3399                 if( menu->items[0].hSubMenu == MENU_DefSysPopup )
3400                     menu->items[0].hSubMenu = MENU_CopySysPopup();
3401             }
3402         }
3403
3404         if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
3405             wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, (HMENU32)(-1) );
3406
3407         if( wndPtr->hSysMenu )
3408             return GetSubMenu16(wndPtr->hSysMenu, 0);
3409     }
3410     return 0;
3411 }
3412
3413
3414 /*******************************************************************
3415  *         SetSystemMenu16    (USER.280)
3416  */
3417 BOOL16 WINAPI SetSystemMenu16( HWND16 hwnd, HMENU16 hMenu )
3418 {
3419     return SetSystemMenu32( hwnd, hMenu );
3420 }
3421
3422
3423 /*******************************************************************
3424  *         SetSystemMenu32    (USER32.508)
3425  */
3426 BOOL32 WINAPI SetSystemMenu32( HWND32 hwnd, HMENU32 hMenu )
3427 {
3428     WND *wndPtr = WIN_FindWndPtr(hwnd);
3429
3430     if (wndPtr)
3431     {
3432         if (wndPtr->hSysMenu) DestroyMenu32( wndPtr->hSysMenu );
3433         wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
3434         return TRUE;
3435     }
3436     return FALSE;
3437 }
3438
3439
3440 /**********************************************************************
3441  *         GetMenu16    (USER.157)
3442  */
3443 HMENU16 WINAPI GetMenu16( HWND16 hWnd ) 
3444 {
3445     WND * wndPtr = WIN_FindWndPtr(hWnd);
3446     if (wndPtr && !(wndPtr->dwStyle & WS_CHILD)) 
3447         return (HMENU16)wndPtr->wIDmenu;
3448     return 0;
3449 }
3450
3451
3452 /**********************************************************************
3453  *         GetMenu32    (USER32.257)
3454  */
3455 HMENU32 WINAPI GetMenu32( HWND32 hWnd ) 
3456
3457     WND * wndPtr = WIN_FindWndPtr(hWnd);
3458     if (wndPtr && !(wndPtr->dwStyle & WS_CHILD)) 
3459         return (HMENU32)wndPtr->wIDmenu;
3460     return 0;
3461 }
3462
3463
3464 /**********************************************************************
3465  *         SetMenu16    (USER.158)
3466  */
3467 BOOL16 WINAPI SetMenu16( HWND16 hWnd, HMENU16 hMenu )
3468 {
3469     return SetMenu32( hWnd, hMenu );
3470 }
3471
3472
3473 /**********************************************************************
3474  *         SetMenu32    (USER32.487)
3475  */
3476 BOOL32 WINAPI SetMenu32( HWND32 hWnd, HMENU32 hMenu )
3477 {
3478     WND * wndPtr = WIN_FindWndPtr(hWnd);
3479
3480     TRACE(menu,"(%04x, %04x);\n", hWnd, hMenu);
3481
3482     if (wndPtr && !(wndPtr->dwStyle & WS_CHILD))
3483     {
3484         if (GetCapture32() == hWnd) ReleaseCapture();
3485
3486         wndPtr->wIDmenu = (UINT32)hMenu;
3487         if (hMenu != 0)
3488         {
3489             LPPOPUPMENU lpmenu;
3490
3491             if (!(lpmenu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
3492             lpmenu->hWnd = hWnd;
3493             lpmenu->wFlags &= ~MF_POPUP;  /* Can't be a popup */
3494             lpmenu->Height = 0;  /* Make sure we recalculate the size */
3495         }
3496         if (IsWindowVisible32(hWnd))
3497             SetWindowPos32( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3498                         SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3499         return TRUE;
3500     }
3501     return FALSE;
3502 }
3503
3504
3505
3506 /**********************************************************************
3507  *         GetSubMenu16    (USER.159)
3508  */
3509 HMENU16 WINAPI GetSubMenu16( HMENU16 hMenu, INT16 nPos )
3510 {
3511     return GetSubMenu32( hMenu, nPos );
3512 }
3513
3514
3515 /**********************************************************************
3516  *         GetSubMenu32    (USER32.288)
3517  */
3518 HMENU32 WINAPI GetSubMenu32( HMENU32 hMenu, INT32 nPos )
3519 {
3520     LPPOPUPMENU lppop;
3521
3522     if (!(lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return 0;
3523     if ((UINT32)nPos >= lppop->nItems) return 0;
3524     if (!(lppop->items[nPos].fType & MF_POPUP)) return 0;
3525     return lppop->items[nPos].hSubMenu;
3526 }
3527
3528
3529 /**********************************************************************
3530  *         DrawMenuBar16    (USER.160)
3531  */
3532 void WINAPI DrawMenuBar16( HWND16 hWnd )
3533 {
3534     DrawMenuBar32( hWnd );
3535 }
3536
3537
3538 /**********************************************************************
3539  *         DrawMenuBar32    (USER32.161)
3540  */
3541 BOOL32 WINAPI DrawMenuBar32( HWND32 hWnd )
3542 {
3543     LPPOPUPMENU lppop;
3544     WND *wndPtr = WIN_FindWndPtr(hWnd);
3545     if (wndPtr && !(wndPtr->dwStyle & WS_CHILD) && wndPtr->wIDmenu)
3546     {
3547         lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR((HMENU16)wndPtr->wIDmenu);
3548         if (lppop == NULL) return FALSE;
3549
3550         lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
3551         SetWindowPos32( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3552                         SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3553         return TRUE;
3554     }
3555     return FALSE;
3556 }
3557
3558
3559 /***********************************************************************
3560  *           EndMenu   (USER.187) (USER32.175)
3561  */
3562 void WINAPI EndMenu(void)
3563 {
3564     fEndMenu = TRUE;
3565 }
3566
3567
3568 /***********************************************************************
3569  *           LookupMenuHandle   (USER.217)
3570  */
3571 HMENU16 WINAPI LookupMenuHandle( HMENU16 hmenu, INT16 id )
3572 {
3573     HMENU32 hmenu32 = hmenu;
3574     UINT32 id32 = id;
3575     if (!MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND )) return 0;
3576     else return hmenu32;
3577 }
3578
3579
3580 /**********************************************************************
3581  *          LoadMenu16    (USER.150)
3582  */
3583 HMENU16 WINAPI LoadMenu16( HINSTANCE16 instance, SEGPTR name )
3584 {
3585     HRSRC16 hRsrc;
3586     HGLOBAL16 handle;
3587     HMENU16 hMenu;
3588
3589     if (HIWORD(name))
3590     {
3591         char *str = (char *)PTR_SEG_TO_LIN( name );
3592         TRACE(menu, "(%04x,'%s')\n", instance, str );
3593         if (str[0] == '#') name = (SEGPTR)atoi( str + 1 );
3594     }
3595     else
3596         TRACE(resource,"(%04x,%04x)\n",instance,LOWORD(name));
3597
3598     if (!name) return 0;
3599     
3600     /* check for Win32 module */
3601     if (HIWORD(instance))
3602         return LoadMenu32A(instance,PTR_SEG_TO_LIN(name));
3603     instance = GetExePtr( instance );
3604
3605     if (!(hRsrc = FindResource16( instance, name, RT_MENU16 ))) return 0;
3606     if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
3607     hMenu = LoadMenuIndirect16(LockResource16(handle));
3608     FreeResource16( handle );
3609     return hMenu;
3610 }
3611
3612
3613 /*****************************************************************
3614  *        LoadMenu32A   (USER32.370)
3615  */
3616 HMENU32 WINAPI LoadMenu32A( HINSTANCE32 instance, LPCSTR name )
3617 {
3618     HRSRC32 hrsrc = FindResource32A( instance, name, RT_MENU32A );
3619     if (!hrsrc) return 0;
3620     return LoadMenuIndirect32A( (LPCVOID)LoadResource32( instance, hrsrc ));
3621 }
3622
3623
3624 /*****************************************************************
3625  *        LoadMenu32W   (USER32.373)
3626  */
3627 HMENU32 WINAPI LoadMenu32W( HINSTANCE32 instance, LPCWSTR name )
3628 {
3629     HRSRC32 hrsrc = FindResource32W( instance, name, RT_MENU32W );
3630     if (!hrsrc) return 0;
3631     return LoadMenuIndirect32W( (LPCVOID)LoadResource32( instance, hrsrc ));
3632 }
3633
3634
3635 /**********************************************************************
3636  *          LoadMenuIndirect16    (USER.220)
3637  */
3638 HMENU16 WINAPI LoadMenuIndirect16( LPCVOID template )
3639 {
3640     HMENU16 hMenu;
3641     WORD version, offset;
3642     LPCSTR p = (LPCSTR)template;
3643
3644     TRACE(menu,"(%p)\n", template );
3645     version = GET_WORD(p);
3646     p += sizeof(WORD);
3647     if (version)
3648     {
3649         WARN(menu, "version must be 0 for Win16\n" );
3650         return 0;
3651     }
3652     offset = GET_WORD(p);
3653     p += sizeof(WORD) + offset;
3654     if (!(hMenu = CreateMenu32())) return 0;
3655     if (!MENU_ParseResource( p, hMenu, FALSE ))
3656     {
3657         DestroyMenu32( hMenu );
3658         return 0;
3659     }
3660     return hMenu;
3661 }
3662
3663
3664 /**********************************************************************
3665  *          LoadMenuIndirect32A    (USER32.371)
3666  */
3667 HMENU32 WINAPI LoadMenuIndirect32A( LPCVOID template )
3668 {
3669     HMENU16 hMenu;
3670     WORD version, offset;
3671     LPCSTR p = (LPCSTR)template;
3672
3673     TRACE(menu,"%p\n", template );
3674     version = GET_WORD(p);
3675     p += sizeof(WORD);
3676     switch (version)
3677       {
3678       case 0:
3679         offset = GET_WORD(p);
3680         p += sizeof(WORD) + offset;
3681         if (!(hMenu = CreateMenu32())) return 0;
3682         if (!MENU_ParseResource( p, hMenu, TRUE ))
3683           {
3684             DestroyMenu32( hMenu );
3685             return 0;
3686           }
3687         return hMenu;
3688       case 1:
3689         offset = GET_WORD(p);
3690         p += sizeof(WORD) + offset;
3691         if (!(hMenu = CreateMenu32())) return 0;
3692         if (!MENUEX_ParseResource( p, hMenu))
3693           {
3694             DestroyMenu32( hMenu );
3695             return 0;
3696           }
3697         return hMenu;
3698       default:
3699         ERR(menu, "version %d not supported.\n", version);
3700         return 0;
3701       }
3702 }
3703
3704
3705 /**********************************************************************
3706  *          LoadMenuIndirect32W    (USER32.372)
3707  */
3708 HMENU32 WINAPI LoadMenuIndirect32W( LPCVOID template )
3709 {
3710     /* FIXME: is there anything different between A and W? */
3711     return LoadMenuIndirect32A( template );
3712 }
3713
3714
3715 /**********************************************************************
3716  *              IsMenu16    (USER.358)
3717  */
3718 BOOL16 WINAPI IsMenu16( HMENU16 hmenu )
3719 {
3720     LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
3721     return IS_A_MENU(menu);
3722 }
3723
3724
3725 /**********************************************************************
3726  *              IsMenu32    (USER32.346)
3727  */
3728 BOOL32 WINAPI IsMenu32(HMENU32 hmenu)
3729 {
3730     LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
3731     return IS_A_MENU(menu);
3732 }
3733
3734 /**********************************************************************
3735  *              GetMenuItemInfo32_common
3736  */
3737
3738 static BOOL32 GetMenuItemInfo32_common ( HMENU32 hmenu, UINT32 item,
3739                                          BOOL32 bypos,
3740                                          LPMENUITEMINFO32A lpmii,
3741                                          BOOL32 unicode)
3742 {
3743   MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
3744     debug_print_menuitem("GetMenuItemInfo32_common: ", menu, "");
3745     if (!menu)
3746         return FALSE;
3747
3748     if (lpmii->fMask & MIIM_TYPE) {
3749         lpmii->fType = menu->fType;
3750         switch (MENU_ITEM_TYPE(menu->fType)) {
3751         case MF_STRING:
3752             if (menu->text && lpmii->dwTypeData && lpmii->cch) {
3753           if (unicode)
3754                     lstrcpynAtoW((LPWSTR) lpmii->dwTypeData,
3755                                  menu->text,
3756                                  lpmii->cch);
3757                 else
3758                     lstrcpyn32A(lpmii->dwTypeData,
3759                                 menu->text,
3760                                 lpmii->cch);
3761         }
3762             break;
3763         case MF_OWNERDRAW:
3764         case MF_BITMAP:
3765             lpmii->dwTypeData = menu->text;
3766             break;
3767         default:
3768             break;
3769     }
3770     }
3771     if (lpmii->fMask & MIIM_STATE)
3772         lpmii->fState = menu->fState;
3773
3774     if (lpmii->fMask & MIIM_ID)
3775         lpmii->wID = menu->wID;
3776
3777     if (lpmii->fMask & MIIM_SUBMENU)
3778         lpmii->hSubMenu = menu->hSubMenu;
3779
3780     if (lpmii->fMask & MIIM_CHECKMARKS) {
3781         lpmii->hbmpChecked = menu->hCheckBit;
3782         lpmii->hbmpUnchecked = menu->hUnCheckBit;
3783     }
3784     if (lpmii->fMask & MIIM_DATA)
3785         lpmii->dwItemData = menu->dwItemData;
3786
3787   return TRUE;
3788 }
3789
3790 /**********************************************************************
3791  *              GetMenuItemInfo32A    (USER32.264)
3792  */
3793 BOOL32 WINAPI GetMenuItemInfo32A( HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3794                                   LPMENUITEMINFO32A lpmii)
3795 {
3796     return GetMenuItemInfo32_common (hmenu, item, bypos, lpmii, FALSE);
3797 }
3798
3799 /**********************************************************************
3800  *              GetMenuItemInfo32W    (USER32.265)
3801  */
3802 BOOL32 WINAPI GetMenuItemInfo32W( HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3803                                   LPMENUITEMINFO32W lpmii)
3804 {
3805     return GetMenuItemInfo32_common (hmenu, item, bypos,
3806                                      (LPMENUITEMINFO32A)lpmii, TRUE);
3807 }
3808
3809 /**********************************************************************
3810  *              SetMenuItemInfo32_common
3811  */
3812
3813 static BOOL32 SetMenuItemInfo32_common(MENUITEM * menu,
3814                                        const MENUITEMINFO32A *lpmii,
3815                                        BOOL32 unicode)
3816 {
3817     if (!menu) return FALSE;
3818
3819     if (lpmii->fMask & MIIM_TYPE) {
3820         /* Get rid of old string.  */
3821         if (IS_STRING_ITEM(menu->fType) && menu->text)
3822             HeapFree(SystemHeap, 0, menu->text);
3823
3824         menu->fType = lpmii->fType;
3825         menu->text = lpmii->dwTypeData;
3826         if (IS_STRING_ITEM(menu->fType) && menu->text) {
3827             menu->text =
3828                 unicode
3829                 ? HEAP_strdupWtoA(SystemHeap, 0,
3830                                   (LPWSTR) lpmii->dwTypeData)
3831                 : HEAP_strdupA(SystemHeap, 0, lpmii->dwTypeData);
3832         }
3833     }
3834     if (lpmii->fMask & MIIM_STATE)
3835         menu->fState = lpmii->fState;
3836
3837     if (lpmii->fMask & MIIM_ID)
3838         menu->wID = lpmii->wID;
3839
3840     if (lpmii->fMask & MIIM_SUBMENU) {
3841         menu->hSubMenu = lpmii->hSubMenu;
3842         if (menu->hSubMenu) {
3843             POPUPMENU *subMenu = (POPUPMENU *)USER_HEAP_LIN_ADDR((UINT16)menu->hSubMenu);
3844             if (IS_A_MENU(subMenu)) {
3845                 subMenu->wFlags |= MF_POPUP;
3846                 menu->fType |= MF_POPUP;
3847             }
3848             else
3849                 /* FIXME: Return an error ? */
3850                 menu->fType &= ~MF_POPUP;
3851         }
3852         else
3853             menu->fType &= ~MF_POPUP;
3854     }
3855
3856     if (lpmii->fMask & MIIM_CHECKMARKS)
3857     {
3858         menu->hCheckBit = lpmii->hbmpChecked;
3859         menu->hUnCheckBit = lpmii->hbmpUnchecked;
3860     }
3861     if (lpmii->fMask & MIIM_DATA)
3862         menu->dwItemData = lpmii->dwItemData;
3863
3864     debug_print_menuitem("SetMenuItemInfo32_common: ", menu, "");
3865     return TRUE;
3866 }
3867
3868 /**********************************************************************
3869  *              SetMenuItemInfo32A    (USER32.491)
3870  */
3871 BOOL32 WINAPI SetMenuItemInfo32A(HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3872                                  const MENUITEMINFO32A *lpmii) 
3873 {
3874     return SetMenuItemInfo32_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
3875                                     lpmii, FALSE);
3876 }
3877
3878 /**********************************************************************
3879  *              SetMenuItemInfo32W    (USER32.492)
3880  */
3881 BOOL32 WINAPI SetMenuItemInfo32W(HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3882                                  const MENUITEMINFO32W *lpmii)
3883 {
3884     return SetMenuItemInfo32_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
3885                                     (const MENUITEMINFO32A*)lpmii, TRUE);
3886 }
3887
3888 /**********************************************************************
3889  *              SetMenuDefaultItem32    (USER32.489)
3890  */
3891 BOOL32 WINAPI SetMenuDefaultItem32(HMENU32 hmenu, UINT32 item, UINT32 bypos)
3892 {
3893     MENUITEM *menuitem = MENU_FindItem(&hmenu, &item, bypos);
3894     POPUPMENU *menu;
3895
3896     if (!menuitem) return FALSE;
3897     if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hmenu))) return FALSE;
3898
3899     menu->defitem = item; /* position */
3900
3901     debug_print_menuitem("SetMenuDefaultItem32: ", menuitem, "");
3902     FIXME(menu, "(0x%x,%d,%d), empty stub!\n",
3903                   hmenu, item, bypos);
3904     return TRUE;
3905 }
3906
3907 /**********************************************************************
3908  *              GetMenuDefaultItem32    (USER32.260)
3909  */
3910 UINT32 WINAPI GetMenuDefaultItem32(HMENU32 hmenu, UINT32 bypos, UINT32 flags)
3911 {
3912     POPUPMENU *menu;
3913
3914     if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hmenu)))
3915         return -1;
3916
3917     FIXME(menu, "(0x%x,%d,%d), stub!\n", hmenu, bypos, flags);
3918     if (bypos & MF_BYPOSITION)
3919         return menu->defitem;
3920     else {
3921         FIXME (menu, "default item 0x%x\n", menu->defitem);
3922         if ((menu->defitem > 0) && (menu->defitem < menu->nItems))
3923             return menu->items[menu->defitem].wID;
3924     }
3925     return -1;
3926 }
3927
3928 /*******************************************************************
3929  *              InsertMenuItem16   (USER.441)
3930  *
3931  * FIXME: untested
3932  */
3933 BOOL16 WINAPI InsertMenuItem16( HMENU16 hmenu, UINT16 pos, BOOL16 byposition,
3934                                 const MENUITEMINFO16 *mii )
3935 {
3936     MENUITEMINFO32A miia;
3937
3938     miia.cbSize        = sizeof(miia);
3939     miia.fMask         = mii->fMask;
3940     miia.dwTypeData    = mii->dwTypeData;
3941     miia.fType         = mii->fType;
3942     miia.fState        = mii->fState;
3943     miia.wID           = mii->wID;
3944     miia.hSubMenu      = mii->hSubMenu;
3945     miia.hbmpChecked   = mii->hbmpChecked;
3946     miia.hbmpUnchecked = mii->hbmpUnchecked;
3947     miia.dwItemData    = mii->dwItemData;
3948     miia.cch           = mii->cch;
3949     if (IS_STRING_ITEM(miia.fType))
3950         miia.dwTypeData = PTR_SEG_TO_LIN(miia.dwTypeData);
3951     return InsertMenuItem32A( hmenu, pos, byposition, &miia );
3952 }
3953
3954
3955 /**********************************************************************
3956  *              InsertMenuItem32A    (USER32.323)
3957  */
3958 BOOL32 WINAPI InsertMenuItem32A(HMENU32 hMenu, UINT32 uItem, BOOL32 bypos,
3959                                 const MENUITEMINFO32A *lpmii)
3960 {
3961     MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
3962     return SetMenuItemInfo32_common(item, lpmii, FALSE);
3963 }
3964
3965
3966 /**********************************************************************
3967  *              InsertMenuItem32W    (USER32.324)
3968  */
3969 BOOL32 WINAPI InsertMenuItem32W(HMENU32 hMenu, UINT32 uItem, BOOL32 bypos,
3970                                 const MENUITEMINFO32W *lpmii)
3971 {
3972     MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
3973     return SetMenuItemInfo32_common(item, (const MENUITEMINFO32A*)lpmii, TRUE);
3974 }
3975
3976 /**********************************************************************
3977  *              CheckMenuRadioItem32    (USER32.47)
3978  */
3979
3980 BOOL32 WINAPI CheckMenuRadioItem32(HMENU32 hMenu,
3981                                    UINT32 first, UINT32 last, UINT32 check,
3982                                    UINT32 bypos)
3983 {
3984      MENUITEM *mifirst, *milast, *micheck;
3985      HMENU32 mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
3986
3987      TRACE(menu, "ox%x: %d-%d, check %d, bypos=%d\n",
3988                   hMenu, first, last, check, bypos);
3989
3990      mifirst = MENU_FindItem (&mfirst, &first, bypos);
3991      milast = MENU_FindItem (&mlast, &last, bypos);
3992      micheck = MENU_FindItem (&mcheck, &check, bypos);
3993
3994      if (mifirst == NULL || milast == NULL || micheck == NULL ||
3995          mifirst > milast || mfirst != mlast || mfirst != mcheck ||
3996          micheck > milast || micheck < mifirst)
3997           return FALSE;
3998
3999      while (mifirst <= milast)
4000      {
4001           if (mifirst == micheck)
4002           {
4003                mifirst->fType |= MFT_RADIOCHECK;
4004                mifirst->fState |= MFS_CHECKED;
4005           } else {
4006                mifirst->fType &= ~MFT_RADIOCHECK;
4007                mifirst->fState &= ~MFS_CHECKED;
4008           }
4009           mifirst++;
4010      }
4011
4012      return TRUE;
4013 }
4014
4015 /**********************************************************************
4016  *              CheckMenuRadioItem16    (not a Windows API)
4017  */
4018
4019 BOOL16 WINAPI CheckMenuRadioItem16(HMENU16 hMenu,
4020                                    UINT16 first, UINT16 last, UINT16 check,
4021                                    BOOL16 bypos)
4022 {
4023      return CheckMenuRadioItem32 (hMenu, first, last, check, bypos);
4024 }
4025
4026 /**********************************************************************
4027  *              GetMenuItemRect32    (USER32.266)
4028  */
4029
4030 BOOL32 WINAPI GetMenuItemRect32 (HWND32 hwnd, HMENU32 hMenu, UINT32 uItem,
4031                                  LPRECT32 rect)
4032 {
4033      RECT32 saverect, clientrect;
4034      BOOL32 barp;
4035      HDC32 hdc;
4036      WND *wndPtr;
4037      MENUITEM *item;
4038      HMENU32 orghMenu = hMenu;
4039
4040      TRACE(menu, "(0x%x,0x%x,%d,%p)\n",
4041                   hwnd, hMenu, uItem, rect);
4042
4043      item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4044      wndPtr = WIN_FindWndPtr (hwnd);
4045      if (!rect || !item || !wndPtr) return FALSE;
4046
4047      GetClientRect32( hwnd, &clientrect );
4048      hdc = GetDCEx32( hwnd, 0, DCX_CACHE | DCX_WINDOW );
4049      barp = (hMenu == orghMenu);
4050
4051      saverect = item->rect;
4052      MENU_CalcItemSize (hdc, item, hwnd,
4053                         clientrect.left, clientrect.top, barp);
4054      *rect = item->rect;
4055      item->rect = saverect;
4056
4057      ReleaseDC32( hwnd, hdc );
4058      return TRUE;
4059 }
4060
4061 /**********************************************************************
4062  *              GetMenuItemRect16    (USER.665)
4063  */
4064
4065 BOOL16 WINAPI GetMenuItemRect16 (HWND16 hwnd, HMENU16 hMenu, UINT16 uItem,
4066                                  LPRECT16 rect)
4067 {
4068      RECT32 r32;
4069      BOOL32 res;
4070
4071      if (!rect) return FALSE;
4072      res = GetMenuItemRect32 (hwnd, hMenu, uItem, &r32);
4073      CONV_RECT32TO16 (&r32, rect);
4074      return res;
4075 }
4076
4077 /**********************************************************************
4078  *         SetMenuContextHelpId16    (USER.384)
4079  */
4080 BOOL16 WINAPI SetMenuContextHelpId16( HMENU16 hMenu, DWORD dwContextHelpId)
4081 {
4082         return SetMenuContextHelpId32( hMenu, dwContextHelpId );
4083 }
4084
4085
4086 /**********************************************************************
4087  *         SetMenuContextHelpId32    (USER32.488)
4088  */
4089 BOOL32 WINAPI SetMenuContextHelpId32( HMENU32 hMenu, DWORD dwContextHelpId)
4090 {
4091         FIXME(menu, "SetMenuContextHelpId, stub\n");
4092         return 0;
4093 }
4094
4095 /**********************************************************************
4096  *         GetMenuContextHelpId16    (USER.385)
4097  */
4098 DWORD WINAPI GetMenuContextHelpId16( HMENU16 hMenu )
4099 {
4100         return GetMenuContextHelpId16( hMenu );
4101 }
4102  
4103 /**********************************************************************
4104  *         GetMenuContextHelpId32    (USER32.488)
4105  */
4106 DWORD WINAPI GetMenuContextHelpId32( HMENU32 hMenu )
4107 {
4108         FIXME(menu, "GetMenuContextHelpId, stub\n");
4109         return 0;
4110 }
4111