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