4 * Copyright 1993 Martin Ayotte
5 * Copyright 1994 Alexandre Julliard
6 * Copyright 1997 Morten Welinder
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.
22 #include "sysmetrics.h"
27 #include "nonclient.h"
36 /* internal popup menu window messages */
38 #define MM_SETMENUHANDLE (WM_USER + 0)
39 #define MM_GETMENUHANDLE (WM_USER + 1)
41 /* Menu item structure */
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 */
57 /* Popup menu structure */
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;
71 /* internal flags for menu tracking */
73 #define TF_ENDMENU 0x0001
74 #define TF_SUSPENDPOPUP 0x0002
75 #define TF_SKIPREMOVE 0x0004
80 HMENU32 hCurrentMenu; /* current submenu (can be equal to hTopMenu)*/
81 HMENU32 hTopMenu; /* initial menu */
82 HWND32 hOwnerWnd; /* where notifications are sent */
86 #define MENU_MAGIC 0x554d /* 'MU' */
87 #define IS_A_MENU(pmenu) ((pmenu) && (pmenu)->wMagic == MENU_MAGIC)
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 */
97 /* popup menu shade thickness */
98 #define POPUP_XSHADE 4
99 #define POPUP_YSHADE 4
101 /* Space between 2 menu bar items */
102 #define MENU_BAR_ITEMS_SPACE 12
104 /* Minimum width of a tab character */
105 #define MENU_TAB_SPACE 8
107 /* Height of a separator item */
108 #define SEPARATOR_HEIGHT 5
110 /* (other menu->FocusedItem values give the position of the focused item) */
111 #define NO_SELECTED_ITEM 0xffff
113 #define MENU_ITEM_TYPE(flags) \
114 ((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR))
116 #define IS_STRING_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_STRING)
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)
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)
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;
133 static HBITMAP32 hStdRadioCheck = 0;
134 static HBITMAP32 hStdCheck = 0;
135 static HBITMAP32 hStdMnArrow = 0;
136 static HBRUSH32 hShadeBrush = 0;
137 static HMENU32 MENU_DefSysPopup = 0; /* Default system menu popup */
139 /* Use global popup window because there's no way 2 menus can
140 * be tracked at the same time. */
142 static WND* pTopPopupWnd = 0;
143 static UINT32 uSubPWndLevel = 0;
145 /* Flag set by EndMenu() to force an exit from menu tracking */
146 static BOOL32 fEndMenu = FALSE;
149 /***********************************************************************
150 * debug_print_menuitem
152 * Print a menuitem in readable form.
155 #define debug_print_menuitem(pre, mp, post) \
156 if(!TRACE_ON(menu)) ; else do_debug_print_menuitem(pre, mp, post)
158 #define MENUOUT(text) \
159 dsprintf(menu, "%s%s", (count++ ? "," : ""), (text))
161 #define MENUFLAG(bit,text) \
163 if (flags & (bit)) { flags &= ~(bit); MENUOUT ((text)); } \
166 static void do_debug_print_menuitem(const char *prefix, MENUITEM * mp,
169 dbg_decl_str(menu, 256);
172 UINT32 flags = mp->fType;
173 int typ = MENU_ITEM_TYPE(flags);
174 dsprintf(menu, "{ ID=0x%x", mp->wID);
175 if (flags & MF_POPUP)
176 dsprintf(menu, ", Sub=0x%x", mp->hSubMenu);
179 dsprintf(menu, ", Typ=");
180 if (typ == MFT_STRING)
182 else if (typ == MFT_SEPARATOR)
184 else if (typ == MFT_OWNERDRAW)
186 else if (typ == MFT_BITMAP)
192 MENUFLAG(MF_POPUP, "pop");
193 MENUFLAG(MFT_MENUBARBREAK, "barbrk");
194 MENUFLAG(MFT_MENUBREAK, "brk");
195 MENUFLAG(MFT_RADIOCHECK, "radio");
196 MENUFLAG(MFT_RIGHTORDER, "rorder");
197 MENUFLAG(MF_SYSMENU, "sys");
198 MENUFLAG(MFT_RIGHTJUSTIFY, "right");
201 dsprintf(menu, "+0x%x", flags);
206 dsprintf(menu, ", State=");
207 MENUFLAG(MFS_GRAYED, "grey");
208 MENUFLAG(MFS_DISABLED, "dis");
209 MENUFLAG(MFS_CHECKED, "check");
210 MENUFLAG(MFS_HILITE, "hi");
211 MENUFLAG(MF_USECHECKBITMAPS, "usebit");
212 MENUFLAG(MF_MOUSESELECT, "mouse");
214 dsprintf(menu, "+0x%x", flags);
217 dsprintf(menu, ", Chk=0x%x", mp->hCheckBit);
219 dsprintf(menu, ", Unc=0x%x", mp->hUnCheckBit);
221 if (typ == MFT_STRING) {
223 dsprintf(menu, ", Text=\"%s\"", mp->text);
225 dsprintf(menu, ", Text=Null");
226 } else if (mp->text == NULL)
229 dsprintf(menu, ", Text=%p", mp->text);
230 dsprintf(menu, " }");
232 dsprintf(menu, "NULL");
235 TRACE(menu, "%s %s %s\n", prefix, dbg_str(menu), postfix);
241 /***********************************************************************
244 * Return the default system menu.
246 static HMENU32 MENU_CopySysPopup(void)
248 HMENU32 hMenu = LoadMenuIndirect32A(SYSRES_GetResPtr(SYSRES_MENU_SYSMENU));
251 POPUPMENU* menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hMenu);
252 menu->wFlags |= MF_SYSMENU | MF_POPUP;
256 ERR(menu, "Unable to load default system menu\n" );
259 TRACE(menu, "returning %x.\n", hMenu );
265 /**********************************************************************
268 * Create a copy of the system menu. System menu in Windows is
269 * a special menu-bar with the single entry - system menu popup.
270 * This popup is presented to the outside world as a "system menu".
271 * However, the real system menu handle is sometimes seen in the
272 * WM_MENUSELECT paramemters (and Word 6 likes it this way).
274 HMENU32 MENU_GetSysMenu( HWND32 hWnd, HMENU32 hPopupMenu )
278 if ((hMenu = CreateMenu32()))
280 POPUPMENU *menu = (POPUPMENU*) USER_HEAP_LIN_ADDR(hMenu);
281 menu->wFlags = MF_SYSMENU;
284 if (hPopupMenu == (HMENU32)(-1))
285 hPopupMenu = MENU_CopySysPopup();
286 else if( !hPopupMenu ) hPopupMenu = MENU_DefSysPopup;
290 InsertMenu32A( hMenu, -1, MF_SYSMENU | MF_POPUP | MF_BYPOSITION, hPopupMenu, NULL );
292 menu->items[0].fType = MF_SYSMENU | MF_POPUP;
293 menu->items[0].fState = 0;
294 menu = (POPUPMENU*) USER_HEAP_LIN_ADDR(hPopupMenu);
295 menu->wFlags |= MF_SYSMENU;
297 TRACE(menu,"GetSysMenu hMenu=%04x (%04x)\n", hMenu, hPopupMenu );
300 DestroyMenu32( hMenu );
302 ERR(menu, "failed to load system menu!\n");
307 /***********************************************************************
310 * Menus initialisation.
315 static unsigned char shade_bits[16] = { 0x55, 0, 0xAA, 0,
320 /* Load menu bitmaps */
321 hStdCheck = LoadBitmap32A(0, MAKEINTRESOURCE32A(OBM_CHECK));
322 hStdRadioCheck = LoadBitmap32A(0, MAKEINTRESOURCE32A(OBM_RADIOCHECK));
323 hStdMnArrow = LoadBitmap32A(0, MAKEINTRESOURCE32A(OBM_MNARROW));
328 GetObject32A( hStdCheck, sizeof(bm), &bm );
329 check_bitmap_width = bm.bmWidth;
330 check_bitmap_height = bm.bmHeight;
334 /* Assume that radio checks have the same size as regular check. */
341 GetObject32A( hStdMnArrow, sizeof(bm), &bm );
342 arrow_bitmap_width = bm.bmWidth;
343 arrow_bitmap_height = bm.bmHeight;
347 if ((hBitmap = CreateBitmap32( 8, 8, 1, 1, shade_bits)))
349 if((hShadeBrush = CreatePatternBrush32( hBitmap )))
351 DeleteObject32( hBitmap );
352 if ((MENU_DefSysPopup = MENU_CopySysPopup()))
360 /***********************************************************************
361 * MENU_InitSysMenuPopup
363 * Grey the appropriate items in System menu.
365 static void MENU_InitSysMenuPopup( HMENU32 hmenu, DWORD style, DWORD clsStyle )
369 gray = !(style & WS_THICKFRAME) || (style & (WS_MAXIMIZE | WS_MINIMIZE));
370 EnableMenuItem32( hmenu, SC_SIZE, (gray ? MF_GRAYED : MF_ENABLED) );
371 gray = ((style & WS_MAXIMIZE) != 0);
372 EnableMenuItem32( hmenu, SC_MOVE, (gray ? MF_GRAYED : MF_ENABLED) );
373 gray = !(style & WS_MINIMIZEBOX) || (style & WS_MINIMIZE);
374 EnableMenuItem32( hmenu, SC_MINIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
375 gray = !(style & WS_MAXIMIZEBOX) || (style & WS_MAXIMIZE);
376 EnableMenuItem32( hmenu, SC_MAXIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
377 gray = !(style & (WS_MAXIMIZE | WS_MINIMIZE));
378 EnableMenuItem32( hmenu, SC_RESTORE, (gray ? MF_GRAYED : MF_ENABLED) );
379 gray = (clsStyle & CS_NOCLOSE) != 0;
380 EnableMenuItem32( hmenu, SC_CLOSE, (gray ? MF_GRAYED : MF_ENABLED) );
384 /******************************************************************************
386 * UINT32 MENU_GetStartOfNextColumn(
389 *****************************************************************************/
391 static UINT32 MENU_GetStartOfNextColumn(
394 POPUPMENU *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu);
395 UINT32 i = menu->FocusedItem + 1;
398 return NO_SELECTED_ITEM;
400 if( i == NO_SELECTED_ITEM )
403 for( ; i < menu->nItems; ++i ) {
404 if (menu->items[i].fType & MF_MENUBARBREAK)
408 return NO_SELECTED_ITEM;
412 /******************************************************************************
414 * UINT32 MENU_GetStartOfPrevColumn(
417 *****************************************************************************/
419 static UINT32 MENU_GetStartOfPrevColumn(
422 POPUPMENU const *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu);
426 return NO_SELECTED_ITEM;
428 if( menu->FocusedItem == 0 || menu->FocusedItem == NO_SELECTED_ITEM )
429 return NO_SELECTED_ITEM;
431 /* Find the start of the column */
433 for(i = menu->FocusedItem; i != 0 &&
434 !(menu->items[i].fType & MF_MENUBARBREAK);
438 return NO_SELECTED_ITEM;
440 for(--i; i != 0; --i) {
441 if (menu->items[i].fType & MF_MENUBARBREAK)
445 TRACE(menu, "ret %d.\n", i );
452 /***********************************************************************
455 * Find a menu item. Return a pointer on the item, and modifies *hmenu
456 * in case the item was in a sub-menu.
458 static MENUITEM *MENU_FindItem( HMENU32 *hmenu, UINT32 *nPos, UINT32 wFlags )
463 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(*hmenu))) return NULL;
464 if (wFlags & MF_BYPOSITION)
466 if (*nPos >= menu->nItems) return NULL;
467 return &menu->items[*nPos];
471 MENUITEM *item = menu->items;
472 for (i = 0; i < menu->nItems; i++, item++)
474 if (item->wID == *nPos)
479 else if (item->fType & MF_POPUP)
481 HMENU32 hsubmenu = item->hSubMenu;
482 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
494 /***********************************************************************
497 static void MENU_FreeItemData( MENUITEM* item )
500 if (IS_STRING_ITEM(item->fType) && item->text)
501 HeapFree( SystemHeap, 0, item->text );
504 /***********************************************************************
505 * MENU_FindItemByCoords
507 * Find the item at the specified coordinates (screen coords). Does
508 * not work for child windows and therefore should not be called for
509 * an arbitrary system menu.
511 static MENUITEM *MENU_FindItemByCoords( POPUPMENU *menu,
512 POINT32 pt, UINT32 *pos )
518 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd ))) return NULL;
519 pt.x -= wndPtr->rectWindow.left;
520 pt.y -= wndPtr->rectWindow.top;
522 for (i = 0; i < menu->nItems; i++, item++)
524 if ((pt.x >= item->rect.left) && (pt.x < item->rect.right) &&
525 (pt.y >= item->rect.top) && (pt.y < item->rect.bottom))
535 /***********************************************************************
538 * Find the menu item selected by a key press.
539 * Return item id, -1 if none, -2 if we should close the menu.
541 static UINT32 MENU_FindItemByKey( HWND32 hwndOwner, HMENU32 hmenu,
542 UINT32 key, BOOL32 forceMenuChar )
544 TRACE(menu,"\tlooking for '%c' in [%04x]\n", (char)key, (UINT16)hmenu );
546 if (!IsMenu32( hmenu ))
548 WND* w = WIN_FindWndPtr(hwndOwner);
549 hmenu = GetSubMenu32(w->hSysMenu, 0);
554 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
555 MENUITEM *item = menu->items;
563 for (i = 0; i < menu->nItems; i++, item++)
565 if (item->text && (IS_STRING_ITEM(item->fType)))
567 char *p = item->text - 2;
570 p = strchr (p + 2, '&');
572 while (p != NULL && p [1] == '&');
573 if (p && (toupper(p[1]) == key)) return i;
577 menuchar = SendMessage32A( hwndOwner, WM_MENUCHAR,
578 MAKEWPARAM( key, menu->wFlags ), hmenu );
579 if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
580 if (HIWORD(menuchar) == 1) return (UINT32)(-2);
586 /***********************************************************************
589 * Calculate the size of the menu item and store it in lpitem->rect.
591 static void MENU_CalcItemSize( HDC32 hdc, MENUITEM *lpitem, HWND32 hwndOwner,
592 INT32 orgX, INT32 orgY, BOOL32 menuBar )
597 TRACE(menu, "HDC 0x%x at (%d,%d)\n",
599 debug_print_menuitem("MENU_CalcItemSize: menuitem:", lpitem,
600 (menuBar ? " (MenuBar)" : ""));
602 SetRect32( &lpitem->rect, orgX, orgY, orgX, orgY );
604 if (lpitem->fType & MF_OWNERDRAW)
606 MEASUREITEMSTRUCT32 mis;
607 mis.CtlType = ODT_MENU;
608 mis.itemID = lpitem->wID;
609 mis.itemData = (DWORD)lpitem->text;
612 SendMessage32A( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)&mis );
613 lpitem->rect.bottom += mis.itemHeight;
614 lpitem->rect.right += mis.itemWidth;
615 TRACE(menu, "%08x %dx%d\n",
616 lpitem->wID, mis.itemWidth, mis.itemHeight);
620 if (lpitem->fType & MF_SEPARATOR)
622 lpitem->rect.bottom += SEPARATOR_HEIGHT;
628 lpitem->rect.right += 2 * check_bitmap_width;
629 if (lpitem->fType & MF_POPUP)
630 lpitem->rect.right += arrow_bitmap_width;
633 if (lpitem->fType & MF_BITMAP)
636 if (GetObject32A( (HBITMAP32)lpitem->text, sizeof(bm), &bm ))
638 lpitem->rect.right += bm.bmWidth;
639 lpitem->rect.bottom += bm.bmHeight;
644 /* If we get here, then it must be a text item */
646 if (IS_STRING_ITEM( lpitem->fType ))
648 dwSize = GetTextExtent( hdc, lpitem->text, strlen(lpitem->text) );
649 lpitem->rect.right += LOWORD(dwSize);
650 if (TWEAK_WineLook == WIN31_LOOK)
651 lpitem->rect.bottom += MAX( HIWORD(dwSize), SYSMETRICS_CYMENU );
653 lpitem->rect.bottom += MAX (HIWORD(dwSize), sysMetrics[SM_CYMENU]- 1);
656 if (menuBar) lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
657 else if ((p = strchr( lpitem->text, '\t' )) != NULL)
659 /* Item contains a tab (only meaningful in popup menus) */
660 lpitem->xTab = check_bitmap_width + MENU_TAB_SPACE +
661 LOWORD( GetTextExtent( hdc, lpitem->text,
662 (int)(p - lpitem->text) ));
663 lpitem->rect.right += MENU_TAB_SPACE;
667 if (strchr( lpitem->text, '\b' ))
668 lpitem->rect.right += MENU_TAB_SPACE;
669 lpitem->xTab = lpitem->rect.right - check_bitmap_width
670 - arrow_bitmap_width;
676 /***********************************************************************
677 * MENU_PopupMenuCalcSize
679 * Calculate the size of a popup menu.
681 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, HWND32 hwndOwner )
686 int orgX, orgY, maxX, maxTab, maxTabWidth;
688 lppop->Width = lppop->Height = 0;
689 if (lppop->nItems == 0) return;
692 maxX = SYSMETRICS_CXBORDER;
693 while (start < lppop->nItems)
695 lpitem = &lppop->items[start];
697 orgY = SYSMETRICS_CYBORDER;
699 maxTab = maxTabWidth = 0;
701 /* Parse items until column break or end of menu */
702 for (i = start; i < lppop->nItems; i++, lpitem++)
705 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
707 if (TWEAK_WineLook > WIN31_LOOK)
710 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, FALSE );
711 if (lpitem->fType & MF_MENUBARBREAK) orgX++;
712 maxX = MAX( maxX, lpitem->rect.right );
713 orgY = lpitem->rect.bottom;
714 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
716 maxTab = MAX( maxTab, lpitem->xTab );
717 maxTabWidth = MAX(maxTabWidth,lpitem->rect.right-lpitem->xTab);
721 /* Finish the column (set all items to the largest width found) */
722 maxX = MAX( maxX, maxTab + maxTabWidth );
723 for (lpitem = &lppop->items[start]; start < i; start++, lpitem++)
725 lpitem->rect.right = maxX;
726 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
727 lpitem->xTab = maxTab;
729 lppop->Height = MAX( lppop->Height, orgY );
732 if(TWEAK_WineLook > WIN31_LOOK)
736 ReleaseDC32( 0, hdc );
740 /***********************************************************************
741 * MENU_MenuBarCalcSize
743 * FIXME: Word 6 implements its own MDI and its own 'close window' bitmap
744 * height is off by 1 pixel which causes lengthy window relocations when
745 * active document window is maximized/restored.
747 * Calculate the size of the menu bar.
749 static void MENU_MenuBarCalcSize( HDC32 hdc, LPRECT32 lprect,
750 LPPOPUPMENU lppop, HWND32 hwndOwner )
753 int start, i, orgX, orgY, maxY, helpPos;
755 if ((lprect == NULL) || (lppop == NULL)) return;
756 if (lppop->nItems == 0) return;
757 TRACE(menu,"left=%d top=%d right=%d bottom=%d\n",
758 lprect->left, lprect->top, lprect->right, lprect->bottom);
759 lppop->Width = lprect->right - lprect->left;
764 while (start < lppop->nItems)
766 lpitem = &lppop->items[start];
770 /* Parse items until line break or end of menu */
771 for (i = start; i < lppop->nItems; i++, lpitem++)
773 if ((helpPos == -1) && (lpitem->fType & MF_HELP)) helpPos = i;
775 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
777 TRACE(menu, "calling MENU_CalcItemSize org=(%d, %d)\n",
779 debug_print_menuitem (" item: ", lpitem, "");
780 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE );
781 if (lpitem->rect.right > lprect->right)
783 if (i != start) break;
784 else lpitem->rect.right = lprect->right;
786 maxY = MAX( maxY, lpitem->rect.bottom );
787 orgX = lpitem->rect.right;
790 /* Finish the line (set all items to the largest height found) */
791 while (start < i) lppop->items[start++].rect.bottom = maxY;
794 lprect->bottom = maxY;
795 lppop->Height = lprect->bottom - lprect->top;
797 /* Flush right all items between the MF_HELP and the last item */
798 /* (if several lines, only move the last line) */
801 lpitem = &lppop->items[lppop->nItems-1];
802 orgY = lpitem->rect.top;
803 orgX = lprect->right;
804 for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--)
806 if (lpitem->rect.top != orgY) break; /* Other line */
807 if (lpitem->rect.right >= orgX) break; /* Too far right already */
808 lpitem->rect.left += orgX - lpitem->rect.right;
809 lpitem->rect.right = orgX;
810 orgX = lpitem->rect.left;
815 /***********************************************************************
818 * Draw a single menu item.
820 static void MENU_DrawMenuItem( HWND32 hwnd, HDC32 hdc, MENUITEM *lpitem,
821 UINT32 height, BOOL32 menuBar, UINT32 odaction )
825 debug_print_menuitem("MENU_DrawMenuItem: ", lpitem, "");
827 if (lpitem->fType & MF_SYSMENU)
829 if( !IsIconic32(hwnd) ) {
830 if (TWEAK_WineLook > WIN31_LOOK)
831 NC_DrawSysButton95( hwnd, hdc,
833 (MF_HILITE | MF_MOUSESELECT) );
835 NC_DrawSysButton( hwnd, hdc,
837 (MF_HILITE | MF_MOUSESELECT) );
843 if (lpitem->fType & MF_OWNERDRAW)
845 DRAWITEMSTRUCT32 dis;
847 dis.CtlType = ODT_MENU;
848 dis.itemID = lpitem->wID;
849 dis.itemData = (DWORD)lpitem->text;
851 if (lpitem->fState & MF_CHECKED) dis.itemState |= ODS_CHECKED;
852 if (lpitem->fState & MF_GRAYED) dis.itemState |= ODS_GRAYED;
853 if (lpitem->fState & MF_HILITE) dis.itemState |= ODS_SELECTED;
854 dis.itemAction = odaction; /* ODA_DRAWENTIRE | ODA_SELECT | ODA_FOCUS; */
857 dis.rcItem = lpitem->rect;
858 TRACE(menu, "Ownerdraw: itemID=%d, itemState=%d, itemAction=%d, "
859 "hwndItem=%04x, hdc=%04x, rcItem={%d,%d,%d,%d}\n",dis.itemID,
860 dis.itemState, dis.itemAction, dis.hwndItem, dis.hDC,
861 dis.rcItem.left, dis.rcItem.top, dis.rcItem.right,
863 SendMessage32A( GetWindow32(hwnd,GW_OWNER), WM_DRAWITEM, 0, (LPARAM)&dis );
867 if (menuBar && (lpitem->fType & MF_SEPARATOR)) return;
870 /* Draw the background */
871 if (TWEAK_WineLook > WIN31_LOOK) {
881 InflateRect32( &rect, -1, -1 );
885 if (lpitem->fState & MF_HILITE)
886 FillRect32( hdc, &rect, GetSysColorBrush32(COLOR_HIGHLIGHT) );
888 FillRect32( hdc, &rect, GetSysColorBrush32(COLOR_MENU) );
890 SetBkMode32( hdc, TRANSPARENT );
892 /* Draw the separator bar (if any) */
894 if (!menuBar && (lpitem->fType & MF_MENUBARBREAK))
896 /* vertical separator */
897 if (TWEAK_WineLook > WIN31_LOOK) {
900 rc.bottom = height - 3;
901 DrawEdge32 (hdc, &rc, EDGE_ETCHED, BF_LEFT);
904 SelectObject32( hdc, GetSysColorPen32(COLOR_WINDOWFRAME) );
905 MoveTo( hdc, rect.left, 0 );
906 LineTo32( hdc, rect.left, height );
909 if (lpitem->fType & MF_SEPARATOR)
911 /* horizontal separator */
912 if (TWEAK_WineLook > WIN31_LOOK) {
916 rc.top += SEPARATOR_HEIGHT / 2;
917 DrawEdge32 (hdc, &rc, EDGE_ETCHED, BF_TOP);
920 SelectObject32( hdc, GetSysColorPen32(COLOR_WINDOWFRAME) );
921 MoveTo( hdc, rect.left, rect.top + SEPARATOR_HEIGHT/2 );
922 LineTo32( hdc, rect.right, rect.top + SEPARATOR_HEIGHT/2 );
930 if (lpitem->fState & MF_HILITE)
932 if (lpitem->fState & MF_GRAYED)
933 SetTextColor32( hdc, GetSysColor32( COLOR_GRAYTEXT ) );
935 SetTextColor32( hdc, GetSysColor32( COLOR_HIGHLIGHTTEXT ) );
936 SetBkColor32( hdc, GetSysColor32( COLOR_HIGHLIGHT ) );
940 if (lpitem->fState & MF_GRAYED)
941 SetTextColor32( hdc, GetSysColor32( COLOR_GRAYTEXT ) );
943 SetTextColor32( hdc, GetSysColor32( COLOR_MENUTEXT ) );
944 SetBkColor32( hdc, GetSysColor32( COLOR_MENU ) );
949 INT32 y = rect.top + rect.bottom;
951 /* Draw the check mark
954 * Custom checkmark bitmaps are monochrome but not always 1bpp.
957 if (lpitem->fState & MF_CHECKED)
960 lpitem->hCheckBit ? lpitem->hCheckBit :
961 ((lpitem->fType & MFT_RADIOCHECK)
962 ? hStdRadioCheck : hStdCheck);
963 HDC32 hdcMem = CreateCompatibleDC32( hdc );
965 SelectObject32( hdcMem, bm );
966 BitBlt32( hdc, rect.left, (y - check_bitmap_height) / 2,
967 check_bitmap_width, check_bitmap_height,
968 hdcMem, 0, 0, SRCCOPY );
969 DeleteDC32( hdcMem );
970 } else if (lpitem->hUnCheckBit) {
971 HDC32 hdcMem = CreateCompatibleDC32( hdc );
973 SelectObject32( hdcMem, lpitem->hUnCheckBit );
974 BitBlt32( hdc, rect.left, (y - check_bitmap_height) / 2,
975 check_bitmap_width, check_bitmap_height,
976 hdcMem, 0, 0, SRCCOPY );
977 DeleteDC32( hdcMem );
980 /* Draw the popup-menu arrow */
982 if (lpitem->fType & MF_POPUP)
984 HDC32 hdcMem = CreateCompatibleDC32( hdc );
986 SelectObject32( hdcMem, hStdMnArrow );
987 BitBlt32( hdc, rect.right - arrow_bitmap_width - 1,
988 (y - arrow_bitmap_height) / 2,
989 arrow_bitmap_width, arrow_bitmap_height,
990 hdcMem, 0, 0, SRCCOPY );
991 DeleteDC32( hdcMem );
994 rect.left += check_bitmap_width;
995 rect.right -= arrow_bitmap_width;
998 /* Draw the item text or bitmap */
1000 if (lpitem->fType & MF_BITMAP)
1002 HDC32 hdcMem = CreateCompatibleDC32( hdc );
1004 SelectObject32( hdcMem, (HBITMAP32)lpitem->text );
1005 BitBlt32( hdc, rect.left, rect.top, rect.right - rect.left,
1006 rect.bottom - rect.top, hdcMem, 0, 0, SRCCOPY );
1007 DeleteDC32( hdcMem );
1010 /* No bitmap - process text if present */
1011 else if (IS_STRING_ITEM(lpitem->fType))
1017 rect.left += MENU_BAR_ITEMS_SPACE / 2;
1018 rect.right -= MENU_BAR_ITEMS_SPACE / 2;
1019 i = strlen( lpitem->text );
1023 for (i = 0; lpitem->text[i]; i++)
1024 if ((lpitem->text[i] == '\t') || (lpitem->text[i] == '\b'))
1028 if((TWEAK_WineLook == WIN31_LOOK) || !(lpitem->fState & MF_GRAYED)) {
1029 DrawText32A( hdc, lpitem->text, i, &rect,
1030 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1033 if (!(lpitem->fState & MF_HILITE))
1039 SetTextColor32(hdc, RGB(0xff, 0xff, 0xff));
1040 DrawText32A( hdc, lpitem->text, i, &rect,
1041 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1047 SetTextColor32(hdc, RGB(0x80, 0x80, 0x80));
1048 DrawText32A( hdc, lpitem->text, i, &rect,
1049 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1052 if (lpitem->text[i]) /* There's a tab or flush-right char */
1054 if (lpitem->text[i] == '\t')
1056 rect.left = lpitem->xTab;
1057 DrawText32A( hdc, lpitem->text + i + 1, -1, &rect,
1058 DT_LEFT | DT_VCENTER | DT_SINGLELINE );
1060 else DrawText32A( hdc, lpitem->text + i + 1, -1, &rect,
1061 DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
1067 /***********************************************************************
1068 * MENU_DrawPopupMenu
1070 * Paint a popup menu.
1072 static void MENU_DrawPopupMenu( HWND32 hwnd, HDC32 hdc, HMENU32 hmenu )
1074 HBRUSH32 hPrevBrush = 0;
1077 GetClientRect32( hwnd, &rect );
1079 /* if(!TWEAK_Win95Look) { */
1080 rect.bottom -= POPUP_YSHADE * SYSMETRICS_CYBORDER;
1081 rect.right -= POPUP_XSHADE * SYSMETRICS_CXBORDER;
1084 if((hPrevBrush = SelectObject32( hdc, GetSysColorBrush32(COLOR_MENU) )))
1088 Rectangle32( hdc, rect.left, rect.top, rect.right, rect.bottom );
1090 hPrevPen = SelectObject32( hdc, GetStockObject32( NULL_PEN ) );
1096 /* draw 3-d shade */
1097 if(TWEAK_WineLook == WIN31_LOOK) {
1098 SelectObject32( hdc, hShadeBrush );
1099 SetBkMode32( hdc, TRANSPARENT );
1100 ropPrev = SetROP232( hdc, R2_MASKPEN );
1102 i = rect.right; /* why SetBrushOrg() doesn't? */
1103 PatBlt32( hdc, i & 0xfffffffe,
1104 rect.top + POPUP_YSHADE*SYSMETRICS_CYBORDER,
1105 i%2 + POPUP_XSHADE*SYSMETRICS_CXBORDER,
1106 rect.bottom - rect.top, 0x00a000c9 );
1108 PatBlt32( hdc, rect.left + POPUP_XSHADE*SYSMETRICS_CXBORDER,
1109 i & 0xfffffffe,rect.right - rect.left,
1110 i%2 + POPUP_YSHADE*SYSMETRICS_CYBORDER, 0x00a000c9 );
1111 SelectObject32( hdc, hPrevPen );
1112 SelectObject32( hdc, hPrevBrush );
1113 SetROP232( hdc, ropPrev );
1116 DrawEdge32 (hdc, &rect, EDGE_RAISED, BF_RECT);
1118 /* draw menu items */
1120 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1121 if (menu && menu->nItems)
1126 for (u = menu->nItems, item = menu->items; u > 0; u--, item++)
1127 MENU_DrawMenuItem( hwnd, hdc, item, menu->Height, FALSE,
1131 } else SelectObject32( hdc, hPrevBrush );
1136 /***********************************************************************
1139 * Paint a menu bar. Returns the height of the menu bar.
1141 UINT32 MENU_DrawMenuBar( HDC32 hDC, LPRECT32 lprect, HWND32 hwnd,
1142 BOOL32 suppress_draw)
1146 WND *wndPtr = WIN_FindWndPtr( hwnd );
1148 lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR( (HMENU16)wndPtr->wIDmenu );
1149 if (lppop == NULL || lprect == NULL) return SYSMETRICS_CYMENU;
1150 TRACE(menu,"(%04x, %p, %p); !\n",
1151 hDC, lprect, lppop);
1152 if (lppop->Height == 0) MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
1153 lprect->bottom = lprect->top + lppop->Height;
1154 if (suppress_draw) return lppop->Height;
1156 FillRect32(hDC, lprect, GetSysColorBrush32(COLOR_MENU) );
1158 if (TWEAK_WineLook == WIN31_LOOK) {
1159 SelectObject32( hDC, GetSysColorPen32(COLOR_WINDOWFRAME) );
1160 MoveTo( hDC, lprect->left, lprect->bottom );
1161 LineTo32( hDC, lprect->right, lprect->bottom );
1164 SelectObject32( hDC, GetSysColorPen32(COLOR_3DFACE));
1165 MoveTo( hDC, lprect->left, lprect->bottom );
1166 LineTo32( hDC, lprect->right, lprect->bottom );
1169 if (lppop->nItems == 0) return SYSMETRICS_CYMENU;
1170 for (i = 0; i < lppop->nItems; i++)
1172 MENU_DrawMenuItem( hwnd, hDC, &lppop->items[i], lppop->Height, TRUE,
1175 return lppop->Height;
1179 /***********************************************************************
1180 * MENU_PatchResidentPopup
1182 BOOL32 MENU_PatchResidentPopup( HQUEUE16 checkQueue, WND* checkWnd )
1188 TRACE(menu,"patching resident popup: %04x %04x [%04x %04x]\n",
1189 checkQueue, checkWnd ? checkWnd->hwndSelf : 0, pTopPopupWnd->hmemTaskQ,
1190 pTopPopupWnd->owner ? pTopPopupWnd->owner->hwndSelf : 0);
1192 switch( checkQueue )
1194 case 0: /* checkWnd is the new popup owner */
1197 pTopPopupWnd->owner = checkWnd;
1198 if( pTopPopupWnd->hmemTaskQ != checkWnd->hmemTaskQ )
1199 hTask = QUEUE_GetQueueTask( checkWnd->hmemTaskQ );
1203 case 0xFFFF: /* checkWnd is destroyed */
1204 if( pTopPopupWnd->owner == checkWnd )
1205 pTopPopupWnd->owner = NULL;
1208 default: /* checkQueue is exiting */
1209 if( pTopPopupWnd->hmemTaskQ == checkQueue )
1211 hTask = QUEUE_GetQueueTask( pTopPopupWnd->hmemTaskQ );
1212 hTask = TASK_GetNextTask( hTask );
1219 TDB* task = (TDB*)GlobalLock16( hTask );
1222 pTopPopupWnd->hInstance = task->hInstance;
1223 pTopPopupWnd->hmemTaskQ = task->hQueue;
1226 else WARN(menu,"failed to patch resident popup.\n");
1232 /***********************************************************************
1235 * Display a popup menu.
1237 static BOOL32 MENU_ShowPopup( HWND32 hwndOwner, HMENU32 hmenu, UINT32 id,
1238 INT32 x, INT32 y, INT32 xanchor, INT32 yanchor )
1241 WND *wndOwner = NULL;
1243 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return FALSE;
1244 if (menu->FocusedItem != NO_SELECTED_ITEM)
1246 menu->items[menu->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1247 menu->FocusedItem = NO_SELECTED_ITEM;
1250 if( (wndOwner = WIN_FindWndPtr( hwndOwner )) )
1252 UINT32 width, height;
1254 MENU_PopupMenuCalcSize( menu, hwndOwner );
1256 /* adjust popup menu pos so that it fits within the desktop */
1258 width = menu->Width + SYSMETRICS_CXBORDER;
1259 height = menu->Height + SYSMETRICS_CYBORDER;
1261 if( x + width > SYSMETRICS_CXSCREEN )
1264 x -= width - xanchor;
1265 if( x + width > SYSMETRICS_CXSCREEN)
1266 x = SYSMETRICS_CXSCREEN - width;
1270 if( y + height > SYSMETRICS_CYSCREEN )
1273 y -= height + yanchor;
1274 if( y + height > SYSMETRICS_CYSCREEN )
1275 y = SYSMETRICS_CYSCREEN - height;
1279 width += POPUP_XSHADE * SYSMETRICS_CXBORDER; /* add space for shading */
1280 height += POPUP_YSHADE * SYSMETRICS_CYBORDER;
1282 /* NOTE: In Windows, top menu popup is not owned. */
1283 if (!pTopPopupWnd) /* create top level popup menu window */
1285 assert( uSubPWndLevel == 0 );
1287 pTopPopupWnd = WIN_FindWndPtr(CreateWindow32A( POPUPMENU_CLASS_ATOM, NULL,
1288 WS_POPUP, x, y, width, height,
1289 hwndOwner, 0, wndOwner->hInstance,
1291 if (!pTopPopupWnd) return FALSE;
1292 menu->hWnd = pTopPopupWnd->hwndSelf;
1297 /* create a new window for the submenu */
1299 menu->hWnd = CreateWindow32A( POPUPMENU_CLASS_ATOM, NULL,
1300 WS_POPUP, x, y, width, height,
1301 menu->hWnd, 0, wndOwner->hInstance,
1303 if( !menu->hWnd ) return FALSE;
1305 else /* top level popup menu window already exists */
1307 menu->hWnd = pTopPopupWnd->hwndSelf;
1309 MENU_PatchResidentPopup( 0, wndOwner );
1310 SendMessage16( pTopPopupWnd->hwndSelf, MM_SETMENUHANDLE, (WPARAM16)hmenu, 0L);
1312 /* adjust its size */
1314 SetWindowPos32( menu->hWnd, 0, x, y, width, height,
1315 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW);
1318 uSubPWndLevel++; /* menu level counter */
1320 /* Display the window */
1322 SetWindowPos32( menu->hWnd, HWND_TOP, 0, 0, 0, 0,
1323 SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1324 UpdateWindow32( menu->hWnd );
1331 /***********************************************************************
1334 static void MENU_SelectItem( HWND32 hwndOwner, HMENU32 hmenu, UINT32 wIndex,
1335 BOOL32 sendMenuSelect )
1340 lppop = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1341 if (!lppop->nItems) return;
1343 if ((wIndex != NO_SELECTED_ITEM) &&
1344 (lppop->items[wIndex].fType & MF_SEPARATOR))
1345 wIndex = NO_SELECTED_ITEM;
1347 if (lppop->FocusedItem == wIndex) return;
1348 if (lppop->wFlags & MF_POPUP) hdc = GetDC32( lppop->hWnd );
1349 else hdc = GetDCEx32( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1351 /* Clear previous highlighted item */
1352 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1354 lppop->items[lppop->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1355 MENU_DrawMenuItem(lppop->hWnd,hdc,&lppop->items[lppop->FocusedItem],
1356 lppop->Height, !(lppop->wFlags & MF_POPUP),
1360 /* Highlight new item (if any) */
1361 lppop->FocusedItem = wIndex;
1362 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1364 lppop->items[lppop->FocusedItem].fState |= MF_HILITE;
1365 MENU_DrawMenuItem( lppop->hWnd, hdc, &lppop->items[lppop->FocusedItem],
1366 lppop->Height, !(lppop->wFlags & MF_POPUP),
1370 MENUITEM *ip = &lppop->items[lppop->FocusedItem];
1371 SendMessage16( hwndOwner, WM_MENUSELECT, ip->wID,
1372 MAKELONG(ip->fType | (ip->fState | MF_MOUSESELECT),
1376 else if (sendMenuSelect) {
1377 SendMessage16( hwndOwner, WM_MENUSELECT, hmenu,
1378 MAKELONG( lppop->wFlags | MF_MOUSESELECT, hmenu ) );
1380 ReleaseDC32( lppop->hWnd, hdc );
1384 /***********************************************************************
1385 * MENU_MoveSelection
1387 * Moves currently selected item according to the offset parameter.
1388 * If there is no selection then it should select the last item if
1389 * offset is ITEM_PREV or the first item if offset is ITEM_NEXT.
1391 static void MENU_MoveSelection( HWND32 hwndOwner, HMENU32 hmenu, INT32 offset )
1396 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1397 if (!menu->items) return;
1399 if ( menu->FocusedItem != NO_SELECTED_ITEM )
1401 if( menu->nItems == 1 ) return; else
1402 for (i = menu->FocusedItem + offset ; i >= 0 && i < menu->nItems
1404 if (!(menu->items[i].fType & MF_SEPARATOR))
1406 MENU_SelectItem( hwndOwner, hmenu, i, TRUE );
1411 for ( i = (offset > 0) ? 0 : menu->nItems - 1;
1412 i >= 0 && i < menu->nItems ; i += offset)
1413 if (!(menu->items[i].fType & MF_SEPARATOR))
1415 MENU_SelectItem( hwndOwner, hmenu, i, TRUE );
1421 /**********************************************************************
1424 * Set an item flags, id and text ptr. Called by InsertMenu() and
1427 static BOOL32 MENU_SetItemData( MENUITEM *item, UINT32 flags, UINT32 id,
1430 LPSTR prevText = IS_STRING_ITEM(item->fType) ? item->text : NULL;
1432 debug_print_menuitem("MENU_SetItemData from: ", item, "");
1434 if (IS_STRING_ITEM(flags))
1438 flags |= MF_SEPARATOR;
1444 /* Item beginning with a backspace is a help item */
1450 if (!(text = HEAP_strdupA( SystemHeap, 0, str ))) return FALSE;
1454 else if (flags & MF_BITMAP) item->text = (LPSTR)(HBITMAP32)LOWORD(str);
1455 else item->text = NULL;
1457 if (flags & MF_OWNERDRAW)
1458 item->dwItemData = (DWORD)str;
1460 item->dwItemData = 0;
1462 if ((item->fType & MF_POPUP) && (flags & MF_POPUP) && (item->hSubMenu != id) )
1463 DestroyMenu32( item->hSubMenu ); /* ModifyMenu() spec */
1465 if (flags & MF_POPUP)
1467 POPUPMENU *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR((UINT16)id);
1468 if (IS_A_MENU(menu)) menu->wFlags |= MF_POPUP;
1480 if (flags & MF_POPUP)
1481 item->hSubMenu = id;
1483 if ((item->fType & MF_POPUP) && !(flags & MF_POPUP) )
1484 flags |= MF_POPUP; /* keep popup */
1486 item->fType = flags & TYPE_MASK;
1487 item->fState = (flags & STATE_MASK) &
1488 ~(MF_HILITE | MF_MOUSESELECT | MF_BYPOSITION);
1491 /* Don't call SetRectEmpty here! */
1494 if (prevText) HeapFree( SystemHeap, 0, prevText );
1496 debug_print_menuitem("MENU_SetItemData to : ", item, "");
1501 /**********************************************************************
1504 * Insert a new item into a menu.
1506 static MENUITEM *MENU_InsertItem( HMENU32 hMenu, UINT32 pos, UINT32 flags )
1511 if (!(menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu)))
1513 WARN(menu, "%04x not a menu handle\n",
1518 /* Find where to insert new item */
1520 if ((flags & MF_BYPOSITION) &&
1521 ((pos == (UINT32)-1) || (pos == menu->nItems)))
1523 /* Special case: append to menu */
1524 /* Some programs specify the menu length to do that */
1529 if (!MENU_FindItem( &hMenu, &pos, flags ))
1531 WARN(menu, "item %x not found\n",
1535 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu)))
1537 WARN(menu,"%04x not a menu handle\n",
1543 /* Create new items array */
1545 newItems = HeapAlloc( SystemHeap, 0, sizeof(MENUITEM) * (menu->nItems+1) );
1548 WARN(menu, "allocation failed\n" );
1551 if (menu->nItems > 0)
1553 /* Copy the old array into the new */
1554 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
1555 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
1556 (menu->nItems-pos)*sizeof(MENUITEM) );
1557 HeapFree( SystemHeap, 0, menu->items );
1559 menu->items = newItems;
1561 memset( &newItems[pos], 0, sizeof(*newItems) );
1562 return &newItems[pos];
1566 /**********************************************************************
1567 * MENU_ParseResource
1569 * Parse a standard menu resource and add items to the menu.
1570 * Return a pointer to the end of the resource.
1572 static LPCSTR MENU_ParseResource( LPCSTR res, HMENU32 hMenu, BOOL32 unicode )
1579 flags = GET_WORD(res);
1580 res += sizeof(WORD);
1581 if (!(flags & MF_POPUP))
1584 res += sizeof(WORD);
1586 if (!IS_STRING_ITEM(flags))
1587 ERR(menu, "not a string item %04x\n", flags );
1589 if (!unicode) res += strlen(str) + 1;
1590 else res += (lstrlen32W((LPCWSTR)str) + 1) * sizeof(WCHAR);
1591 if (flags & MF_POPUP)
1593 HMENU32 hSubMenu = CreatePopupMenu32();
1594 if (!hSubMenu) return NULL;
1595 if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
1597 if (!unicode) AppendMenu32A( hMenu, flags, (UINT32)hSubMenu, str );
1598 else AppendMenu32W( hMenu, flags, (UINT32)hSubMenu, (LPCWSTR)str );
1600 else /* Not a popup */
1602 if (!unicode) AppendMenu32A( hMenu, flags, id, *str ? str : NULL );
1603 else AppendMenu32W( hMenu, flags, id,
1604 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
1606 } while (!(flags & MF_END));
1611 /**********************************************************************
1612 * MENUEX_ParseResource
1614 * Parse an extended menu resource and add items to the menu.
1615 * Return a pointer to the end of the resource.
1617 static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU32 hMenu)
1621 MENUITEMINFO32W mii;
1623 mii.cbSize = sizeof(mii);
1624 mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
1625 mii.fType = GET_DWORD(res);
1626 res += sizeof(DWORD);
1627 mii.fState = GET_DWORD(res);
1628 res += sizeof(DWORD);
1629 mii.wID = GET_DWORD(res);
1630 res += sizeof(DWORD);
1631 resinfo = GET_WORD(res); /* FIXME: for 16-bit apps this is a byte. */
1632 res += sizeof(WORD);
1633 /* Align the text on a word boundary. */
1634 res += (~((int)res - 1)) & 1;
1635 mii.dwTypeData = (LPWSTR) res;
1636 res += (1 + lstrlen32W(mii.dwTypeData)) * sizeof(WCHAR);
1637 /* Align the following fields on a dword boundary. */
1638 res += (~((int)res - 1)) & 3;
1640 /* FIXME: This is inefficient and cannot be optimised away by gcc. */
1642 LPSTR newstr = HEAP_strdupWtoA(GetProcessHeap(),
1644 TRACE(menu, "Menu item: [%08x,%08x,%04x,%04x,%s]\n",
1645 mii.fType, mii.fState, mii.wID, resinfo, newstr);
1646 HeapFree( GetProcessHeap(), 0, newstr );
1649 if (resinfo & 1) { /* Pop-up? */
1650 DWORD helpid = GET_DWORD(res); /* FIXME: use this. */
1651 res += sizeof(DWORD);
1652 mii.hSubMenu = CreatePopupMenu32();
1655 if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
1656 DestroyMenu32(mii.hSubMenu);
1659 mii.fMask |= MIIM_SUBMENU;
1660 mii.fType |= MF_POPUP;
1662 InsertMenuItem32W(hMenu, -1, MF_BYPOSITION, &mii);
1663 } while (!(resinfo & MF_END));
1668 /***********************************************************************
1671 * Return the handle of the selected sub-popup menu (if any).
1673 static HMENU32 MENU_GetSubPopup( HMENU32 hmenu )
1678 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
1680 if (menu->FocusedItem == NO_SELECTED_ITEM) return 0;
1682 item = &menu->items[menu->FocusedItem];
1683 if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
1684 return item->hSubMenu;
1689 /***********************************************************************
1690 * MENU_HideSubPopups
1692 * Hide the sub-popup menus of this menu.
1694 static void MENU_HideSubPopups( HWND32 hwndOwner, HMENU32 hmenu,
1695 BOOL32 sendMenuSelect )
1697 POPUPMENU *menu = (POPUPMENU*) USER_HEAP_LIN_ADDR( hmenu );;
1699 if (menu && uSubPWndLevel)
1705 if (menu->FocusedItem != NO_SELECTED_ITEM)
1707 item = &menu->items[menu->FocusedItem];
1708 if (!(item->fType & MF_POPUP) ||
1709 !(item->fState & MF_MOUSESELECT)) return;
1710 item->fState &= ~MF_MOUSESELECT;
1711 hsubmenu = item->hSubMenu;
1714 submenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hsubmenu );
1715 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE );
1716 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect );
1718 if (submenu->hWnd == pTopPopupWnd->hwndSelf )
1720 ShowWindow32( submenu->hWnd, SW_HIDE );
1725 DestroyWindow32( submenu->hWnd );
1732 /***********************************************************************
1735 * Display the sub-menu of the selected item of this menu.
1736 * Return the handle of the submenu, or hmenu if no submenu to display.
1738 static HMENU32 MENU_ShowSubPopup( HWND32 hwndOwner, HMENU32 hmenu,
1739 BOOL32 selectFirst )
1747 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return hmenu;
1749 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd )) ||
1750 (menu->FocusedItem == NO_SELECTED_ITEM)) return hmenu;
1752 item = &menu->items[menu->FocusedItem];
1753 if (!(item->fType & MF_POPUP) ||
1754 (item->fState & (MF_GRAYED | MF_DISABLED))) return hmenu;
1756 /* message must be send before using item,
1757 because nearly everything may by changed by the application ! */
1759 SendMessage16( hwndOwner, WM_INITMENUPOPUP, (WPARAM16)item->hSubMenu,
1760 MAKELONG( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
1762 item = &menu->items[menu->FocusedItem];
1765 /* correct item if modified as a reaction to WM_INITMENUPOPUP-message */
1766 if (!(item->fState & MF_HILITE))
1768 if (menu->wFlags & MF_POPUP) hdc = GetDC32( menu->hWnd );
1769 else hdc = GetDCEx32( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1770 item->fState |= MF_HILITE;
1771 MENU_DrawMenuItem( menu->hWnd, hdc, item, menu->Height, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE );
1772 ReleaseDC32( menu->hWnd, hdc );
1774 if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
1777 item->fState |= MF_MOUSESELECT;
1779 if (IS_SYSTEM_MENU(menu))
1781 MENU_InitSysMenuPopup(item->hSubMenu, wndPtr->dwStyle, wndPtr->class->style);
1783 NC_GetSysPopupPos( wndPtr, &rect );
1784 rect.top = rect.bottom;
1785 rect.right = SYSMETRICS_CXSIZE;
1786 rect.bottom = SYSMETRICS_CYSIZE;
1790 if (menu->wFlags & MF_POPUP)
1792 rect.left = wndPtr->rectWindow.left + item->rect.right-arrow_bitmap_width;
1793 rect.top = wndPtr->rectWindow.top + item->rect.top;
1794 rect.right = item->rect.left - item->rect.right + 2*arrow_bitmap_width;
1795 rect.bottom = item->rect.top - item->rect.bottom;
1799 rect.left = wndPtr->rectWindow.left + item->rect.left;
1800 rect.top = wndPtr->rectWindow.top + item->rect.bottom;
1801 rect.right = item->rect.right - item->rect.left;
1802 rect.bottom = item->rect.bottom - item->rect.top;
1806 MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem,
1807 rect.left, rect.top, rect.right, rect.bottom );
1809 MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
1810 return item->hSubMenu;
1813 /***********************************************************************
1816 * Walks menu chain trying to find a menu pt maps to.
1818 static HMENU32 MENU_PtMenu( HMENU32 hMenu, POINT16 pt )
1820 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hMenu );
1821 register UINT32 ht = menu->FocusedItem;
1823 /* try subpopup first (if any) */
1824 ht = (ht != NO_SELECTED_ITEM &&
1825 (menu->items[ht].fType & MF_POPUP) &&
1826 (menu->items[ht].fState & MF_MOUSESELECT))
1827 ? (UINT32) MENU_PtMenu(menu->items[ht].hSubMenu, pt) : 0;
1829 if( !ht ) /* check the current window (avoiding WM_HITTEST) */
1831 ht = (UINT32)NC_HandleNCHitTest( menu->hWnd, pt );
1832 if( menu->wFlags & MF_POPUP )
1833 ht = (ht != (UINT32)HTNOWHERE &&
1834 ht != (UINT32)HTERROR) ? (UINT32)hMenu : 0;
1837 WND* wndPtr = WIN_FindWndPtr(menu->hWnd);
1839 ht = ( ht == HTSYSMENU ) ? (UINT32)(wndPtr->hSysMenu)
1840 : ( ht == HTMENU ) ? (UINT32)(wndPtr->wIDmenu) : 0;
1846 /***********************************************************************
1847 * MENU_ExecFocusedItem
1849 * Execute a menu item (for instance when user pressed Enter).
1850 * Return TRUE if we can go on with menu tracking.
1852 static BOOL32 MENU_ExecFocusedItem( MTRACKER* pmt, HMENU32 hMenu )
1855 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hMenu );
1856 if (!menu || !menu->nItems ||
1857 (menu->FocusedItem == NO_SELECTED_ITEM)) return TRUE;
1859 item = &menu->items[menu->FocusedItem];
1861 TRACE(menu, "%08x %08x %08x\n",
1862 hMenu, item->wID, item->hSubMenu);
1864 if (!(item->fType & MF_POPUP))
1866 if (!(item->fState & (MF_GRAYED | MF_DISABLED)))
1868 if( menu->wFlags & MF_SYSMENU )
1870 PostMessage16( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
1871 MAKELPARAM((INT16)pmt->pt.x, (INT16)pmt->pt.y) );
1874 PostMessage16( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
1881 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hMenu, TRUE );
1887 /***********************************************************************
1888 * MENU_SwitchTracking
1890 * Helper function for menu navigation routines.
1892 static void MENU_SwitchTracking( MTRACKER* pmt, HMENU32 hPtMenu, UINT32 id )
1894 POPUPMENU *ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
1895 POPUPMENU *topmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( pmt->hTopMenu );
1897 if( pmt->hTopMenu != hPtMenu &&
1898 !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
1900 /* both are top level menus (system and menu-bar) */
1902 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
1903 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE );
1904 pmt->hTopMenu = hPtMenu;
1906 else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE );
1907 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE );
1911 /***********************************************************************
1914 * Return TRUE if we can go on with menu tracking.
1916 static BOOL32 MENU_ButtonDown( MTRACKER* pmt, HMENU32 hPtMenu )
1921 POPUPMENU *ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
1924 if( IS_SYSTEM_MENU(ptmenu) )
1925 item = ptmenu->items;
1927 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
1931 if( ptmenu->FocusedItem == id )
1933 /* nothing to do with already selected non-popup */
1934 if( !(item->fType & MF_POPUP) ) return TRUE;
1936 if( item->fState & MF_MOUSESELECT )
1938 if( ptmenu->wFlags & MF_POPUP )
1940 /* hide selected subpopup */
1942 MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, TRUE );
1943 pmt->hCurrentMenu = hPtMenu;
1946 return FALSE; /* shouldn't get here */
1949 else MENU_SwitchTracking( pmt, hPtMenu, id );
1951 /* try to display a subpopup */
1953 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE );
1956 else WARN(menu, "\tunable to find clicked item!\n");
1961 /***********************************************************************
1964 * Return TRUE if we can go on with menu tracking.
1966 static BOOL32 MENU_ButtonUp( MTRACKER* pmt, HMENU32 hPtMenu )
1971 POPUPMENU *ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
1974 if( IS_SYSTEM_MENU(ptmenu) )
1975 item = ptmenu->items;
1977 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
1979 if( item && (ptmenu->FocusedItem == id ))
1981 if( !(item->fType & MF_POPUP) )
1982 return MENU_ExecFocusedItem( pmt, hPtMenu );
1983 hPtMenu = item->hSubMenu;
1984 if( hPtMenu == pmt->hCurrentMenu )
1986 /* Select first item of sub-popup */
1988 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, NO_SELECTED_ITEM, FALSE );
1989 MENU_MoveSelection( pmt->hOwnerWnd, hPtMenu, ITEM_NEXT );
1998 /***********************************************************************
2001 * Return TRUE if we can go on with menu tracking.
2003 static BOOL32 MENU_MouseMove( MTRACKER* pmt, HMENU32 hPtMenu )
2005 UINT32 id = NO_SELECTED_ITEM;
2006 POPUPMENU *ptmenu = NULL;
2010 ptmenu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hPtMenu );
2011 if( IS_SYSTEM_MENU(ptmenu) )
2014 MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2017 if( id == NO_SELECTED_ITEM )
2019 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2020 NO_SELECTED_ITEM, TRUE );
2022 else if( ptmenu->FocusedItem != id )
2024 MENU_SwitchTracking( pmt, hPtMenu, id );
2025 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE );
2031 /***********************************************************************
2034 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2036 static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT32 vk )
2038 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( pmt->hTopMenu );
2040 if( (vk == VK_LEFT && menu->FocusedItem == 0 ) ||
2041 (vk == VK_RIGHT && menu->FocusedItem == menu->nItems - 1))
2047 LRESULT l = SendMessage16( pmt->hOwnerWnd, WM_NEXTMENU, (WPARAM16)vk,
2048 (IS_SYSTEM_MENU(menu)) ? GetSubMenu16(pmt->hTopMenu,0) : pmt->hTopMenu );
2050 TRACE(menu,"%04x [%04x] -> %04x [%04x]\n",
2051 (UINT16)pmt->hCurrentMenu, (UINT16)pmt->hOwnerWnd, LOWORD(l), HIWORD(l) );
2055 wndPtr = WIN_FindWndPtr(pmt->hOwnerWnd);
2057 hNewWnd = pmt->hOwnerWnd;
2058 if( IS_SYSTEM_MENU(menu) )
2060 /* switch to the menu bar */
2062 if( wndPtr->dwStyle & WS_CHILD || !wndPtr->wIDmenu )
2065 hNewMenu = wndPtr->wIDmenu;
2068 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hNewMenu );
2069 id = menu->nItems - 1;
2072 else if( wndPtr->dwStyle & WS_SYSMENU )
2074 /* switch to the system menu */
2075 hNewMenu = wndPtr->hSysMenu;
2079 else /* application returned a new menu to switch to */
2081 hNewMenu = LOWORD(l); hNewWnd = HIWORD(l);
2083 if( IsMenu32(hNewMenu) && IsWindow32(hNewWnd) )
2085 wndPtr = WIN_FindWndPtr(hNewWnd);
2087 if( wndPtr->dwStyle & WS_SYSMENU &&
2088 GetSubMenu16(wndPtr->hSysMenu, 0) == hNewMenu )
2090 /* get the real system menu */
2091 hNewMenu = wndPtr->hSysMenu;
2093 else if( wndPtr->dwStyle & WS_CHILD || wndPtr->wIDmenu != hNewMenu )
2095 /* FIXME: Not sure what to do here, perhaps,
2096 * try to track hNewMenu as a popup? */
2098 TRACE(menu," -- got confused.\n");
2105 if( hNewMenu != pmt->hTopMenu )
2107 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE );
2108 if( pmt->hCurrentMenu != pmt->hTopMenu )
2109 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2112 if( hNewWnd != pmt->hOwnerWnd )
2115 pmt->hOwnerWnd = hNewWnd;
2116 EVENT_Capture( pmt->hOwnerWnd, HTMENU );
2119 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2120 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE );
2127 /***********************************************************************
2130 * The idea is not to show the popup if the next input message is
2131 * going to hide it anyway.
2133 static BOOL32 MENU_SuspendPopup( MTRACKER* pmt, UINT16 uMsg )
2137 msg.hwnd = pmt->hOwnerWnd;
2139 PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2140 pmt->trackFlags |= TF_SKIPREMOVE;
2145 PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2146 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2148 PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2149 PeekMessage16( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2150 if( msg.message == WM_KEYDOWN &&
2151 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2153 pmt->trackFlags |= TF_SUSPENDPOPUP;
2160 /* failures go through this */
2161 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2165 /***********************************************************************
2168 * Handle a VK_LEFT key event in a menu.
2170 static void MENU_KeyLeft( MTRACKER* pmt )
2173 HMENU32 hmenutmp, hmenuprev;
2176 hmenuprev = hmenutmp = pmt->hTopMenu;
2177 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenutmp );
2179 /* Try to move 1 column left (if possible) */
2180 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2181 NO_SELECTED_ITEM ) {
2183 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2188 /* close topmost popup */
2189 while (hmenutmp != pmt->hCurrentMenu)
2191 hmenuprev = hmenutmp;
2192 hmenutmp = MENU_GetSubPopup( hmenuprev );
2195 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2196 pmt->hCurrentMenu = hmenuprev;
2198 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2200 /* move menu bar selection if no more popups are left */
2202 if( !MENU_DoNextMenu( pmt, VK_LEFT) )
2203 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2205 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2207 /* A sublevel menu was displayed - display the next one
2208 * unless there is another displacement coming up */
2210 if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2211 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd,
2212 pmt->hTopMenu, TRUE );
2218 /***********************************************************************
2221 * Handle a VK_RIGHT key event in a menu.
2223 static void MENU_KeyRight( MTRACKER* pmt )
2226 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( pmt->hTopMenu );
2229 TRACE(menu, "MENU_KeyRight called, cur %x (%s), top %x (%s).\n",
2231 ((POPUPMENU *)USER_HEAP_LIN_ADDR(pmt->hCurrentMenu))->
2233 pmt->hTopMenu, menu->items[0].text );
2235 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2237 /* If already displaying a popup, try to display sub-popup */
2239 hmenutmp = pmt->hCurrentMenu;
2240 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hmenutmp, TRUE );
2242 /* if subpopup was displayed then we are done */
2243 if (hmenutmp != pmt->hCurrentMenu) return;
2246 /* Check to see if there's another column */
2247 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
2248 NO_SELECTED_ITEM ) {
2249 TRACE(menu, "Going to %d.\n", nextcol );
2250 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2255 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
2257 if( pmt->hCurrentMenu != pmt->hTopMenu )
2259 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2260 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2261 } else hmenutmp = 0;
2263 /* try to move to the next item */
2264 if( !MENU_DoNextMenu( pmt, VK_RIGHT) )
2265 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
2267 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2268 if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
2269 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd,
2270 pmt->hTopMenu, TRUE );
2275 /***********************************************************************
2278 * Menu tracking code.
2280 static BOOL32 MENU_TrackMenu( HMENU32 hmenu, UINT32 wFlags, INT32 x, INT32 y,
2281 HWND32 hwnd, const RECT32 *lprect )
2286 MTRACKER mt = { 0, hmenu, hmenu, hwnd, {x, y} }; /* control struct */
2289 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu ))) return FALSE;
2291 if (wFlags & TPM_BUTTONDOWN) MENU_ButtonDown( &mt, hmenu );
2293 EVENT_Capture( mt.hOwnerWnd, HTMENU );
2297 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( mt.hCurrentMenu );
2298 msg.hwnd = (wFlags & TPM_ENTERIDLEEX && menu->wFlags & MF_POPUP) ? menu->hWnd : 0;
2300 /* we have to keep the message in the queue until it's
2301 * clear that menu loop is not over yet. */
2303 if (!MSG_InternalGetMessage( &msg, msg.hwnd, mt.hOwnerWnd,
2304 MSGF_MENU, PM_NOREMOVE, TRUE )) break;
2306 TranslateMessage16( &msg );
2307 CONV_POINT16TO32( &msg.pt, &mt.pt );
2310 if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
2312 /* Find a menu for this mouse event */
2314 hmenu = MENU_PtMenu( mt.hTopMenu, msg.pt );
2318 /* no WM_NC... messages in captured state */
2320 case WM_RBUTTONDBLCLK:
2321 case WM_RBUTTONDOWN:
2322 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2324 case WM_LBUTTONDBLCLK:
2325 case WM_LBUTTONDOWN:
2326 fEndMenu |= !MENU_ButtonDown( &mt, hmenu );
2330 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2333 /* If outside all menus but inside lprect, ignore it */
2334 if (hmenu || !lprect || !PtInRect32(lprect, mt.pt))
2336 fEndMenu |= !MENU_ButtonUp( &mt, hmenu );
2342 if ((msg.wParam & MK_LBUTTON) || ((wFlags & TPM_RIGHTBUTTON)
2343 && (msg.wParam & MK_RBUTTON)))
2345 fEndMenu |= !MENU_MouseMove( &mt, hmenu );
2347 } /* switch(msg.message) - mouse */
2349 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
2351 fRemove = TRUE; /* Keyboard messages are always removed */
2359 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
2360 NO_SELECTED_ITEM, FALSE );
2363 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
2364 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
2367 case VK_DOWN: /* If on menu bar, pull-down the menu */
2369 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( mt.hCurrentMenu );
2370 if (!(menu->wFlags & MF_POPUP))
2371 mt.hCurrentMenu = MENU_ShowSubPopup( mt.hOwnerWnd, mt.hTopMenu, TRUE );
2372 else /* otherwise try to move selection */
2373 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, ITEM_NEXT );
2377 MENU_KeyLeft( &mt );
2381 MENU_KeyRight( &mt );
2391 break; /* WM_KEYDOWN */
2401 break; /* WM_SYSKEYDOWN */
2407 if (msg.wParam == '\r' || msg.wParam == ' ')
2409 fEndMenu |= !MENU_ExecFocusedItem( &mt, mt.hCurrentMenu );
2413 /* Hack to avoid control chars. */
2414 /* We will find a better way real soon... */
2415 if ((msg.wParam <= 32) || (msg.wParam >= 127)) break;
2417 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
2418 msg.wParam, FALSE );
2419 if (pos == (UINT32)-2) fEndMenu = TRUE;
2420 else if (pos == (UINT32)-1) MessageBeep32(0);
2423 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos, TRUE );
2424 fEndMenu |= !MENU_ExecFocusedItem( &mt, mt.hCurrentMenu );
2428 } /* switch(msg.message) - kbd */
2432 DispatchMessage16( &msg );
2435 if (!fEndMenu) fRemove = TRUE;
2437 /* finally remove message from the queue */
2439 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
2440 PeekMessage16( &msg, 0, msg.message, msg.message, PM_REMOVE );
2441 else mt.trackFlags &= ~TF_SKIPREMOVE;
2445 if( IsWindow32( mt.hOwnerWnd ) )
2447 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE );
2449 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( mt.hTopMenu );
2450 if (menu && menu->wFlags & MF_POPUP)
2452 ShowWindow32( menu->hWnd, SW_HIDE );
2455 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE );
2456 SendMessage16( mt.hOwnerWnd, WM_MENUSELECT, 0, MAKELONG( 0xffff, 0 ) );
2462 /***********************************************************************
2465 static BOOL32 MENU_InitTracking(HWND32 hWnd, HMENU32 hMenu)
2468 SendMessage16( hWnd, WM_ENTERMENULOOP, 0, 0 );
2469 SendMessage16( hWnd, WM_SETCURSOR, hWnd, HTCAPTION );
2470 SendMessage16( hWnd, WM_INITMENU, hMenu, 0 );
2474 /***********************************************************************
2475 * MENU_TrackMouseMenuBar
2477 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
2479 void MENU_TrackMouseMenuBar( WND* wndPtr, INT32 ht, POINT32 pt )
2481 HWND32 hWnd = wndPtr->hwndSelf;
2482 HMENU32 hMenu = (ht == HTSYSMENU) ? wndPtr->hSysMenu : wndPtr->wIDmenu;
2484 if (IsMenu32(hMenu))
2486 MENU_InitTracking( hWnd, hMenu );
2487 MENU_TrackMenu( hMenu, TPM_ENTERIDLEEX | TPM_BUTTONDOWN |
2488 TPM_LEFTALIGN | TPM_LEFTBUTTON, pt.x, pt.y, hWnd, NULL );
2490 SendMessage16( hWnd, WM_EXITMENULOOP, 0, 0 );
2496 /***********************************************************************
2497 * MENU_TrackKbdMenuBar
2499 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
2501 void MENU_TrackKbdMenuBar( WND* wndPtr, UINT32 wParam, INT32 vkey)
2503 UINT32 uItem = NO_SELECTED_ITEM;
2506 /* find window that has a menu */
2508 while( wndPtr->dwStyle & WS_CHILD && !(wndPtr->dwStyle & WS_SYSMENU) )
2509 if( !(wndPtr = wndPtr->parent) ) return;
2511 /* check if we have to track a system menu */
2513 if( (wndPtr->dwStyle & (WS_CHILD | WS_MINIMIZE)) ||
2514 !wndPtr->wIDmenu || vkey == VK_SPACE )
2516 if( !(wndPtr->dwStyle & WS_SYSMENU) ) return;
2517 hTrackMenu = wndPtr->hSysMenu;
2519 wParam |= HTSYSMENU; /* prevent item lookup */
2522 hTrackMenu = wndPtr->wIDmenu;
2524 if (IsMenu32( hTrackMenu ))
2526 MENU_InitTracking( wndPtr->hwndSelf, hTrackMenu );
2528 if( vkey && vkey != VK_SPACE )
2530 uItem = MENU_FindItemByKey( wndPtr->hwndSelf, hTrackMenu,
2531 vkey, (wParam & HTSYSMENU) );
2532 if( uItem >= (UINT32)(-2) )
2534 if( uItem == (UINT32)(-1) ) MessageBeep32(0);
2541 MENU_SelectItem( wndPtr->hwndSelf, hTrackMenu, uItem, TRUE );
2543 if( uItem == NO_SELECTED_ITEM )
2544 MENU_MoveSelection( wndPtr->hwndSelf, hTrackMenu, ITEM_NEXT );
2546 PostMessage16( wndPtr->hwndSelf, WM_KEYDOWN, VK_DOWN, 0L );
2548 MENU_TrackMenu( hTrackMenu, TPM_ENTERIDLEEX | TPM_LEFTALIGN | TPM_LEFTBUTTON,
2549 0, 0, wndPtr->hwndSelf, NULL );
2551 SendMessage16( wndPtr->hwndSelf, WM_EXITMENULOOP, 0, 0 );
2557 /**********************************************************************
2558 * TrackPopupMenu16 (USER.416)
2560 BOOL16 WINAPI TrackPopupMenu16( HMENU16 hMenu, UINT16 wFlags, INT16 x, INT16 y,
2561 INT16 nReserved, HWND16 hWnd, const RECT16 *lpRect )
2565 CONV_RECT16TO32( lpRect, &r );
2566 return TrackPopupMenu32( hMenu, wFlags, x, y, nReserved, hWnd,
2567 lpRect ? &r : NULL );
2571 /**********************************************************************
2572 * TrackPopupMenu32 (USER32.549)
2574 BOOL32 WINAPI TrackPopupMenu32( HMENU32 hMenu, UINT32 wFlags, INT32 x, INT32 y,
2575 INT32 nReserved, HWND32 hWnd, const RECT32 *lpRect )
2580 SendMessage16( hWnd, WM_INITMENUPOPUP, (WPARAM16)hMenu, 0);
2581 if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 ))
2582 ret = MENU_TrackMenu( hMenu, wFlags & ~TPM_INTERNAL, 0, 0, hWnd, lpRect );
2587 /**********************************************************************
2588 * TrackPopupMenuEx (USER32.550)
2590 BOOL32 WINAPI TrackPopupMenuEx( HMENU32 hMenu, UINT32 wFlags, INT32 x, INT32 y,
2591 HWND32 hWnd, LPTPMPARAMS lpTpm )
2593 FIXME(menu, "not fully implemented\n" );
2594 return TrackPopupMenu32( hMenu, wFlags, x, y, 0, hWnd,
2595 lpTpm ? &lpTpm->rcExclude : NULL );
2598 /***********************************************************************
2601 * NOTE: Windows has totally different (and undocumented) popup wndproc.
2603 LRESULT WINAPI PopupMenuWndProc( HWND32 hwnd, UINT32 message, WPARAM32 wParam,
2606 WND* wndPtr = WIN_FindWndPtr(hwnd);
2612 CREATESTRUCT32A *cs = (CREATESTRUCT32A*)lParam;
2613 SetWindowLong32A( hwnd, 0, (LONG)cs->lpCreateParams );
2617 case WM_MOUSEACTIVATE: /* We don't want to be activated */
2618 return MA_NOACTIVATE;
2623 BeginPaint32( hwnd, &ps );
2624 MENU_DrawPopupMenu( hwnd, ps.hdc,
2625 (HMENU32)GetWindowLong32A( hwnd, 0 ) );
2626 EndPaint32( hwnd, &ps );
2634 /* zero out global pointer in case resident popup window
2635 * was somehow destroyed. */
2639 if( hwnd == pTopPopupWnd->hwndSelf )
2641 ERR(menu, "resident popup destroyed!\n");
2643 pTopPopupWnd = NULL;
2655 if( !(*(HMENU32*)wndPtr->wExtra) )
2656 ERR(menu,"no menu to display\n");
2659 *(HMENU32*)wndPtr->wExtra = 0;
2662 case MM_SETMENUHANDLE:
2664 *(HMENU32*)wndPtr->wExtra = (HMENU32)wParam;
2667 case MM_GETMENUHANDLE:
2669 return *(HMENU32*)wndPtr->wExtra;
2672 return DefWindowProc32A( hwnd, message, wParam, lParam );
2678 /***********************************************************************
2679 * MENU_GetMenuBarHeight
2681 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
2683 UINT32 MENU_GetMenuBarHeight( HWND32 hwnd, UINT32 menubarWidth,
2684 INT32 orgX, INT32 orgY )
2691 TRACE(menu, "HWND 0x%x, width %d, "
2692 "at (%d, %d).\n", hwnd, menubarWidth, orgX, orgY );
2694 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
2695 if (!(lppop = (LPPOPUPMENU)USER_HEAP_LIN_ADDR((HMENU16)wndPtr->wIDmenu)))
2697 hdc = GetDCEx32( hwnd, 0, DCX_CACHE | DCX_WINDOW );
2698 SetRect32(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+SYSMETRICS_CYMENU);
2699 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
2700 ReleaseDC32( hwnd, hdc );
2701 return lppop->Height;
2705 /*******************************************************************
2706 * ChangeMenu16 (USER.153)
2708 BOOL16 WINAPI ChangeMenu16( HMENU16 hMenu, UINT16 pos, SEGPTR data,
2709 UINT16 id, UINT16 flags )
2711 TRACE(menu,"menu=%04x pos=%d data=%08lx id=%04x flags=%04x\n",
2712 hMenu, pos, (DWORD)data, id, flags );
2713 if (flags & MF_APPEND) return AppendMenu16( hMenu, flags & ~MF_APPEND,
2716 /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
2717 /* for MF_DELETE. We should check the parameters for all others */
2718 /* MF_* actions also (anybody got a doc on ChangeMenu?). */
2720 if (flags & MF_DELETE) return DeleteMenu16(hMenu, pos, flags & ~MF_DELETE);
2721 if (flags & MF_CHANGE) return ModifyMenu16(hMenu, pos, flags & ~MF_CHANGE,
2723 if (flags & MF_REMOVE) return RemoveMenu16(hMenu,
2724 flags & MF_BYPOSITION ? pos : id,
2725 flags & ~MF_REMOVE );
2726 /* Default: MF_INSERT */
2727 return InsertMenu16( hMenu, pos, flags, id, data );
2731 /*******************************************************************
2732 * ChangeMenu32A (USER32.23)
2734 BOOL32 WINAPI ChangeMenu32A( HMENU32 hMenu, UINT32 pos, LPCSTR data,
2735 UINT32 id, UINT32 flags )
2737 TRACE(menu,"menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
2738 hMenu, pos, (DWORD)data, id, flags );
2739 if (flags & MF_APPEND) return AppendMenu32A( hMenu, flags & ~MF_APPEND,
2741 if (flags & MF_DELETE) return DeleteMenu32(hMenu, pos, flags & ~MF_DELETE);
2742 if (flags & MF_CHANGE) return ModifyMenu32A(hMenu, pos, flags & ~MF_CHANGE,
2744 if (flags & MF_REMOVE) return RemoveMenu32( hMenu,
2745 flags & MF_BYPOSITION ? pos : id,
2746 flags & ~MF_REMOVE );
2747 /* Default: MF_INSERT */
2748 return InsertMenu32A( hMenu, pos, flags, id, data );
2752 /*******************************************************************
2753 * ChangeMenu32W (USER32.24)
2755 BOOL32 WINAPI ChangeMenu32W( HMENU32 hMenu, UINT32 pos, LPCWSTR data,
2756 UINT32 id, UINT32 flags )
2758 TRACE(menu,"menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
2759 hMenu, pos, (DWORD)data, id, flags );
2760 if (flags & MF_APPEND) return AppendMenu32W( hMenu, flags & ~MF_APPEND,
2762 if (flags & MF_DELETE) return DeleteMenu32(hMenu, pos, flags & ~MF_DELETE);
2763 if (flags & MF_CHANGE) return ModifyMenu32W(hMenu, pos, flags & ~MF_CHANGE,
2765 if (flags & MF_REMOVE) return RemoveMenu32( hMenu,
2766 flags & MF_BYPOSITION ? pos : id,
2767 flags & ~MF_REMOVE );
2768 /* Default: MF_INSERT */
2769 return InsertMenu32W( hMenu, pos, flags, id, data );
2773 /*******************************************************************
2774 * CheckMenuItem16 (USER.154)
2776 BOOL16 WINAPI CheckMenuItem16( HMENU16 hMenu, UINT16 id, UINT16 flags )
2778 return (BOOL16)CheckMenuItem32( hMenu, id, flags );
2782 /*******************************************************************
2783 * CheckMenuItem32 (USER32.46)
2785 DWORD WINAPI CheckMenuItem32( HMENU32 hMenu, UINT32 id, UINT32 flags )
2790 TRACE(menu,"%04x %04x %04x\n", hMenu, id, flags );
2791 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
2792 ret = item->fState & MF_CHECKED;
2793 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
2794 else item->fState &= ~MF_CHECKED;
2799 /**********************************************************************
2800 * EnableMenuItem16 (USER.155)
2802 UINT16 WINAPI EnableMenuItem16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
2804 return EnableMenuItem32( hMenu, wItemID, wFlags );
2808 /**********************************************************************
2809 * EnableMenuItem32 (USER32.170)
2811 UINT32 WINAPI EnableMenuItem32( HMENU32 hMenu, UINT32 wItemID, UINT32 wFlags )
2816 TRACE(menu,"(%04x, %04X, %04X) !\n",
2817 hMenu, wItemID, wFlags);
2819 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
2822 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
2823 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
2828 /*******************************************************************
2829 * GetMenuString16 (USER.161)
2831 INT16 WINAPI GetMenuString16( HMENU16 hMenu, UINT16 wItemID,
2832 LPSTR str, INT16 nMaxSiz, UINT16 wFlags )
2834 return GetMenuString32A( hMenu, wItemID, str, nMaxSiz, wFlags );
2838 /*******************************************************************
2839 * GetMenuString32A (USER32.268)
2841 INT32 WINAPI GetMenuString32A( HMENU32 hMenu, UINT32 wItemID,
2842 LPSTR str, INT32 nMaxSiz, UINT32 wFlags )
2846 TRACE(menu, "menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
2847 hMenu, wItemID, str, nMaxSiz, wFlags );
2848 if (!str || !nMaxSiz) return 0;
2850 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
2851 if (!IS_STRING_ITEM(item->fType)) return 0;
2852 lstrcpyn32A( str, item->text, nMaxSiz );
2853 TRACE(menu, "returning '%s'\n", str );
2858 /*******************************************************************
2859 * GetMenuString32W (USER32.269)
2861 INT32 WINAPI GetMenuString32W( HMENU32 hMenu, UINT32 wItemID,
2862 LPWSTR str, INT32 nMaxSiz, UINT32 wFlags )
2866 TRACE(menu, "menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
2867 hMenu, wItemID, str, nMaxSiz, wFlags );
2868 if (!str || !nMaxSiz) return 0;
2870 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
2871 if (!IS_STRING_ITEM(item->fType)) return 0;
2872 lstrcpynAtoW( str, item->text, nMaxSiz );
2873 return lstrlen32W(str);
2877 /**********************************************************************
2878 * HiliteMenuItem16 (USER.162)
2880 BOOL16 WINAPI HiliteMenuItem16( HWND16 hWnd, HMENU16 hMenu, UINT16 wItemID,
2883 return HiliteMenuItem32( hWnd, hMenu, wItemID, wHilite );
2887 /**********************************************************************
2888 * HiliteMenuItem32 (USER32.318)
2890 BOOL32 WINAPI HiliteMenuItem32( HWND32 hWnd, HMENU32 hMenu, UINT32 wItemID,
2894 TRACE(menu,"(%04x, %04x, %04x, %04x);\n",
2895 hWnd, hMenu, wItemID, wHilite);
2896 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
2897 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
2898 if (menu->FocusedItem == wItemID) return TRUE;
2899 MENU_HideSubPopups( hWnd, hMenu, FALSE );
2900 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE );
2905 /**********************************************************************
2906 * GetMenuState16 (USER.250)
2908 UINT16 WINAPI GetMenuState16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
2910 return GetMenuState32( hMenu, wItemID, wFlags );
2914 /**********************************************************************
2915 * GetMenuState32 (USER32.267)
2917 UINT32 WINAPI GetMenuState32( HMENU32 hMenu, UINT32 wItemID, UINT32 wFlags )
2920 TRACE(menu,"(%04x, %04x, %04x);\n",
2921 hMenu, wItemID, wFlags);
2922 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
2923 debug_print_menuitem (" item: ", item, "");
2924 if (item->fType & MF_POPUP)
2926 POPUPMENU *menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( item->hSubMenu );
2927 if (!menu) return -1;
2928 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
2932 /* We used to (from way back then) mask the result to 0xff. */
2933 /* I don't know why and it seems wrong as the documented */
2934 /* return flag MF_SEPARATOR is outside that mask. */
2935 return (item->fType | item->fState);
2940 /**********************************************************************
2941 * GetMenuItemCount16 (USER.263)
2943 INT16 WINAPI GetMenuItemCount16( HMENU16 hMenu )
2945 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
2946 if (!IS_A_MENU(menu)) return -1;
2947 TRACE(menu,"(%04x) returning %d\n",
2948 hMenu, menu->nItems );
2949 return menu->nItems;
2953 /**********************************************************************
2954 * GetMenuItemCount32 (USER32.262)
2956 INT32 WINAPI GetMenuItemCount32( HMENU32 hMenu )
2958 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
2959 if (!IS_A_MENU(menu)) return -1;
2960 TRACE(menu,"(%04x) returning %d\n",
2961 hMenu, menu->nItems );
2962 return menu->nItems;
2966 /**********************************************************************
2967 * GetMenuItemID16 (USER.264)
2969 UINT16 WINAPI GetMenuItemID16( HMENU16 hMenu, INT16 nPos )
2973 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return -1;
2974 if ((nPos < 0) || ((UINT16) nPos >= menu->nItems)) return -1;
2975 if (menu->items[nPos].fType & MF_POPUP) return -1;
2976 return menu->items[nPos].wID;
2980 /**********************************************************************
2981 * GetMenuItemID32 (USER32.263)
2983 UINT32 WINAPI GetMenuItemID32( HMENU32 hMenu, INT32 nPos )
2987 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return -1;
2988 if ((nPos < 0) || (nPos >= menu->nItems)) return -1;
2989 if (menu->items[nPos].fType & MF_POPUP) return -1;
2990 return menu->items[nPos].wID;
2994 /*******************************************************************
2995 * InsertMenu16 (USER.410)
2997 BOOL16 WINAPI InsertMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
2998 UINT16 id, SEGPTR data )
3000 UINT32 pos32 = (UINT32)pos;
3001 if ((pos == (UINT16)-1) && (flags & MF_BYPOSITION)) pos32 = (UINT32)-1;
3002 if (IS_STRING_ITEM(flags) && data)
3003 return InsertMenu32A( hMenu, pos32, flags, id,
3004 (LPSTR)PTR_SEG_TO_LIN(data) );
3005 return InsertMenu32A( hMenu, pos32, flags, id, (LPSTR)data );
3009 /*******************************************************************
3010 * InsertMenu32A (USER32.322)
3012 BOOL32 WINAPI InsertMenu32A( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3013 UINT32 id, LPCSTR str )
3017 if (IS_STRING_ITEM(flags) && str)
3018 TRACE(menu, "hMenu %04x, pos %d, flags %08x, "
3019 "id %04x, str '%s'\n",
3020 hMenu, pos, flags, id, str );
3021 else TRACE(menu, "hMenu %04x, pos %d, flags %08x, "
3022 "id %04x, str %08lx (not a string)\n",
3023 hMenu, pos, flags, id, (DWORD)str );
3025 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3027 if (!(MENU_SetItemData( item, flags, id, str )))
3029 RemoveMenu32( hMenu, pos, flags );
3033 if (flags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
3034 ((POPUPMENU *)USER_HEAP_LIN_ADDR((HMENU16)id))->wFlags |= MF_POPUP;
3036 item->hCheckBit = item->hUnCheckBit = 0;
3041 /*******************************************************************
3042 * InsertMenu32W (USER32.325)
3044 BOOL32 WINAPI InsertMenu32W( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3045 UINT32 id, LPCWSTR str )
3049 if (IS_STRING_ITEM(flags) && str)
3051 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3052 ret = InsertMenu32A( hMenu, pos, flags, id, newstr );
3053 HeapFree( GetProcessHeap(), 0, newstr );
3056 else return InsertMenu32A( hMenu, pos, flags, id, (LPCSTR)str );
3060 /*******************************************************************
3061 * AppendMenu16 (USER.411)
3063 BOOL16 WINAPI AppendMenu16(HMENU16 hMenu, UINT16 flags, UINT16 id, SEGPTR data)
3065 return InsertMenu16( hMenu, -1, flags | MF_BYPOSITION, id, data );
3069 /*******************************************************************
3070 * AppendMenu32A (USER32.5)
3072 BOOL32 WINAPI AppendMenu32A( HMENU32 hMenu, UINT32 flags,
3073 UINT32 id, LPCSTR data )
3075 return InsertMenu32A( hMenu, -1, flags | MF_BYPOSITION, id, data );
3079 /*******************************************************************
3080 * AppendMenu32W (USER32.6)
3082 BOOL32 WINAPI AppendMenu32W( HMENU32 hMenu, UINT32 flags,
3083 UINT32 id, LPCWSTR data )
3085 return InsertMenu32W( hMenu, -1, flags | MF_BYPOSITION, id, data );
3089 /**********************************************************************
3090 * RemoveMenu16 (USER.412)
3092 BOOL16 WINAPI RemoveMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3094 return RemoveMenu32( hMenu, nPos, wFlags );
3098 /**********************************************************************
3099 * RemoveMenu32 (USER32.441)
3101 BOOL32 WINAPI RemoveMenu32( HMENU32 hMenu, UINT32 nPos, UINT32 wFlags )
3106 TRACE(menu,"(%04x, %04x, %04x)\n",hMenu, nPos, wFlags);
3107 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3108 if (!(menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
3112 MENU_FreeItemData( item );
3114 if (--menu->nItems == 0)
3116 HeapFree( SystemHeap, 0, menu->items );
3121 while(nPos < menu->nItems)
3127 menu->items = HeapReAlloc( SystemHeap, 0, menu->items,
3128 menu->nItems * sizeof(MENUITEM) );
3134 /**********************************************************************
3135 * DeleteMenu16 (USER.413)
3137 BOOL16 WINAPI DeleteMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3139 return DeleteMenu32( hMenu, nPos, wFlags );
3143 /**********************************************************************
3144 * DeleteMenu32 (USER32.129)
3146 BOOL32 WINAPI DeleteMenu32( HMENU32 hMenu, UINT32 nPos, UINT32 wFlags )
3148 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3149 if (!item) return FALSE;
3150 if (item->fType & MF_POPUP) DestroyMenu32( item->hSubMenu );
3151 /* nPos is now the position of the item */
3152 RemoveMenu32( hMenu, nPos, wFlags | MF_BYPOSITION );
3157 /*******************************************************************
3158 * ModifyMenu16 (USER.414)
3160 BOOL16 WINAPI ModifyMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
3161 UINT16 id, SEGPTR data )
3163 if (IS_STRING_ITEM(flags))
3164 return ModifyMenu32A( hMenu, pos, flags, id,
3165 (LPSTR)PTR_SEG_TO_LIN(data) );
3166 return ModifyMenu32A( hMenu, pos, flags, id, (LPSTR)data );
3170 /*******************************************************************
3171 * ModifyMenu32A (USER32.397)
3173 BOOL32 WINAPI ModifyMenu32A( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3174 UINT32 id, LPCSTR str )
3178 if (IS_STRING_ITEM(flags))
3180 TRACE(menu, "%04x %d %04x %04x '%s'\n",
3181 hMenu, pos, flags, id, str ? str : "#NULL#" );
3182 if (!str) return FALSE;
3186 TRACE(menu, "%04x %d %04x %04x %08lx\n",
3187 hMenu, pos, flags, id, (DWORD)str );
3190 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3191 return MENU_SetItemData( item, flags, id, str );
3195 /*******************************************************************
3196 * ModifyMenu32W (USER32.398)
3198 BOOL32 WINAPI ModifyMenu32W( HMENU32 hMenu, UINT32 pos, UINT32 flags,
3199 UINT32 id, LPCWSTR str )
3203 if (IS_STRING_ITEM(flags) && str)
3205 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3206 ret = ModifyMenu32A( hMenu, pos, flags, id, newstr );
3207 HeapFree( GetProcessHeap(), 0, newstr );
3210 else return ModifyMenu32A( hMenu, pos, flags, id, (LPCSTR)str );
3214 /**********************************************************************
3215 * CreatePopupMenu16 (USER.415)
3217 HMENU16 WINAPI CreatePopupMenu16(void)
3219 return CreatePopupMenu32();
3223 /**********************************************************************
3224 * CreatePopupMenu32 (USER32.82)
3226 HMENU32 WINAPI CreatePopupMenu32(void)
3231 if (!(hmenu = CreateMenu32())) return 0;
3232 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
3233 menu->wFlags |= MF_POPUP;
3238 /**********************************************************************
3239 * GetMenuCheckMarkDimensions (USER.417) (USER32.258)
3241 DWORD WINAPI GetMenuCheckMarkDimensions(void)
3243 return MAKELONG( check_bitmap_width, check_bitmap_height );
3247 /**********************************************************************
3248 * SetMenuItemBitmaps16 (USER.418)
3250 BOOL16 WINAPI SetMenuItemBitmaps16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags,
3251 HBITMAP16 hNewUnCheck, HBITMAP16 hNewCheck)
3253 return SetMenuItemBitmaps32( hMenu, nPos, wFlags, hNewUnCheck, hNewCheck );
3257 /**********************************************************************
3258 * SetMenuItemBitmaps32 (USER32.490)
3260 BOOL32 WINAPI SetMenuItemBitmaps32( HMENU32 hMenu, UINT32 nPos, UINT32 wFlags,
3261 HBITMAP32 hNewUnCheck, HBITMAP32 hNewCheck)
3264 TRACE(menu,"(%04x, %04x, %04x, %04x, %04x)\n",
3265 hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3266 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3268 if (!hNewCheck && !hNewUnCheck)
3270 item->fState &= ~MF_USECHECKBITMAPS;
3272 else /* Install new bitmaps */
3274 item->hCheckBit = hNewCheck;
3275 item->hUnCheckBit = hNewUnCheck;
3276 item->fState |= MF_USECHECKBITMAPS;
3282 /**********************************************************************
3283 * CreateMenu16 (USER.151)
3285 HMENU16 WINAPI CreateMenu16(void)
3287 return CreateMenu32();
3291 /**********************************************************************
3292 * CreateMenu32 (USER32.81)
3294 HMENU32 WINAPI CreateMenu32(void)
3298 if (!(hMenu = USER_HEAP_ALLOC( sizeof(POPUPMENU) ))) return 0;
3299 menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3301 menu->wMagic = MENU_MAGIC;
3308 menu->FocusedItem = NO_SELECTED_ITEM;
3309 TRACE(menu, "return %04x\n", hMenu );
3314 /**********************************************************************
3315 * DestroyMenu16 (USER.152)
3317 BOOL16 WINAPI DestroyMenu16( HMENU16 hMenu )
3319 return DestroyMenu32( hMenu );
3323 /**********************************************************************
3324 * DestroyMenu32 (USER32.134)
3326 BOOL32 WINAPI DestroyMenu32( HMENU32 hMenu )
3328 TRACE(menu,"(%04x)\n", hMenu);
3330 /* Silently ignore attempts to destroy default system popup */
3332 if (hMenu && hMenu != MENU_DefSysPopup)
3334 LPPOPUPMENU lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3336 if( pTopPopupWnd && (hMenu == *(HMENU32*)pTopPopupWnd->wExtra) )
3337 *(UINT32*)pTopPopupWnd->wExtra = 0;
3339 if (IS_A_MENU( lppop ))
3341 lppop->wMagic = 0; /* Mark it as destroyed */
3343 if ((lppop->wFlags & MF_POPUP) && lppop->hWnd &&
3344 (!pTopPopupWnd || (lppop->hWnd != pTopPopupWnd->hwndSelf)))
3345 DestroyWindow32( lppop->hWnd );
3347 if (lppop->items) /* recursively destroy submenus */
3350 MENUITEM *item = lppop->items;
3351 for (i = lppop->nItems; i > 0; i--, item++)
3353 if (item->fType & MF_POPUP) DestroyMenu32(item->hSubMenu);
3354 MENU_FreeItemData( item );
3356 HeapFree( SystemHeap, 0, lppop->items );
3358 USER_HEAP_FREE( hMenu );
3362 return (hMenu != MENU_DefSysPopup);
3366 /**********************************************************************
3367 * GetSystemMenu16 (USER.156)
3369 HMENU16 WINAPI GetSystemMenu16( HWND16 hWnd, BOOL16 bRevert )
3371 return GetSystemMenu32( hWnd, bRevert );
3375 /**********************************************************************
3376 * GetSystemMenu32 (USER32.291)
3378 HMENU32 WINAPI GetSystemMenu32( HWND32 hWnd, BOOL32 bRevert )
3380 WND *wndPtr = WIN_FindWndPtr( hWnd );
3384 if( wndPtr->hSysMenu )
3388 DestroyMenu32(wndPtr->hSysMenu);
3389 wndPtr->hSysMenu = 0;
3393 POPUPMENU *menu = (POPUPMENU*)
3394 USER_HEAP_LIN_ADDR(wndPtr->hSysMenu);
3395 if( menu->items[0].hSubMenu == MENU_DefSysPopup )
3396 menu->items[0].hSubMenu = MENU_CopySysPopup();
3400 if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
3401 wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, (HMENU32)(-1) );
3403 if( wndPtr->hSysMenu )
3404 return GetSubMenu16(wndPtr->hSysMenu, 0);
3410 /*******************************************************************
3411 * SetSystemMenu16 (USER.280)
3413 BOOL16 WINAPI SetSystemMenu16( HWND16 hwnd, HMENU16 hMenu )
3415 return SetSystemMenu32( hwnd, hMenu );
3419 /*******************************************************************
3420 * SetSystemMenu32 (USER32.508)
3422 BOOL32 WINAPI SetSystemMenu32( HWND32 hwnd, HMENU32 hMenu )
3424 WND *wndPtr = WIN_FindWndPtr(hwnd);
3428 if (wndPtr->hSysMenu) DestroyMenu32( wndPtr->hSysMenu );
3429 wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
3436 /**********************************************************************
3437 * GetMenu16 (USER.157)
3439 HMENU16 WINAPI GetMenu16( HWND16 hWnd )
3441 WND * wndPtr = WIN_FindWndPtr(hWnd);
3442 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD))
3443 return (HMENU16)wndPtr->wIDmenu;
3448 /**********************************************************************
3449 * GetMenu32 (USER32.257)
3451 HMENU32 WINAPI GetMenu32( HWND32 hWnd )
3453 WND * wndPtr = WIN_FindWndPtr(hWnd);
3454 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD))
3455 return (HMENU32)wndPtr->wIDmenu;
3460 /**********************************************************************
3461 * SetMenu16 (USER.158)
3463 BOOL16 WINAPI SetMenu16( HWND16 hWnd, HMENU16 hMenu )
3465 return SetMenu32( hWnd, hMenu );
3469 /**********************************************************************
3470 * SetMenu32 (USER32.487)
3472 BOOL32 WINAPI SetMenu32( HWND32 hWnd, HMENU32 hMenu )
3474 WND * wndPtr = WIN_FindWndPtr(hWnd);
3476 TRACE(menu,"(%04x, %04x);\n", hWnd, hMenu);
3478 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD))
3480 if (GetCapture32() == hWnd) ReleaseCapture();
3482 wndPtr->wIDmenu = (UINT32)hMenu;
3487 if (!(lpmenu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return FALSE;
3488 lpmenu->hWnd = hWnd;
3489 lpmenu->wFlags &= ~MF_POPUP; /* Can't be a popup */
3490 lpmenu->Height = 0; /* Make sure we recalculate the size */
3492 if (IsWindowVisible32(hWnd))
3493 SetWindowPos32( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3494 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3502 /**********************************************************************
3503 * GetSubMenu16 (USER.159)
3505 HMENU16 WINAPI GetSubMenu16( HMENU16 hMenu, INT16 nPos )
3507 return GetSubMenu32( hMenu, nPos );
3511 /**********************************************************************
3512 * GetSubMenu32 (USER32.288)
3514 HMENU32 WINAPI GetSubMenu32( HMENU32 hMenu, INT32 nPos )
3518 if (!(lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu))) return 0;
3519 if ((UINT32)nPos >= lppop->nItems) return 0;
3520 if (!(lppop->items[nPos].fType & MF_POPUP)) return 0;
3521 return lppop->items[nPos].hSubMenu;
3525 /**********************************************************************
3526 * DrawMenuBar16 (USER.160)
3528 void WINAPI DrawMenuBar16( HWND16 hWnd )
3530 DrawMenuBar32( hWnd );
3534 /**********************************************************************
3535 * DrawMenuBar32 (USER32.161)
3537 BOOL32 WINAPI DrawMenuBar32( HWND32 hWnd )
3540 WND *wndPtr = WIN_FindWndPtr(hWnd);
3541 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD) && wndPtr->wIDmenu)
3543 lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR((HMENU16)wndPtr->wIDmenu);
3544 if (lppop == NULL) return FALSE;
3546 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
3547 SetWindowPos32( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3548 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3555 /***********************************************************************
3556 * EndMenu (USER.187) (USER32.175)
3558 void WINAPI EndMenu(void)
3564 /***********************************************************************
3565 * LookupMenuHandle (USER.217)
3567 HMENU16 WINAPI LookupMenuHandle( HMENU16 hmenu, INT16 id )
3569 HMENU32 hmenu32 = hmenu;
3571 if (!MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND )) return 0;
3572 else return hmenu32;
3576 /**********************************************************************
3577 * LoadMenu16 (USER.150)
3579 HMENU16 WINAPI LoadMenu16( HINSTANCE16 instance, SEGPTR name )
3587 char *str = (char *)PTR_SEG_TO_LIN( name );
3588 TRACE(menu, "(%04x,'%s')\n", instance, str );
3589 if (str[0] == '#') name = (SEGPTR)atoi( str + 1 );
3592 TRACE(resource,"(%04x,%04x)\n",instance,LOWORD(name));
3594 if (!name) return 0;
3596 /* check for Win32 module */
3597 if (HIWORD(instance))
3598 return LoadMenu32A(instance,PTR_SEG_TO_LIN(name));
3599 instance = GetExePtr( instance );
3601 if (!(hRsrc = FindResource16( instance, name, RT_MENU16 ))) return 0;
3602 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
3603 hMenu = LoadMenuIndirect16(LockResource16(handle));
3604 FreeResource16( handle );
3609 /*****************************************************************
3610 * LoadMenu32A (USER32.370)
3612 HMENU32 WINAPI LoadMenu32A( HINSTANCE32 instance, LPCSTR name )
3614 HRSRC32 hrsrc = FindResource32A( instance, name, RT_MENU32A );
3615 if (!hrsrc) return 0;
3616 return LoadMenuIndirect32A( (LPCVOID)LoadResource32( instance, hrsrc ));
3620 /*****************************************************************
3621 * LoadMenu32W (USER32.373)
3623 HMENU32 WINAPI LoadMenu32W( HINSTANCE32 instance, LPCWSTR name )
3625 HRSRC32 hrsrc = FindResource32W( instance, name, RT_MENU32W );
3626 if (!hrsrc) return 0;
3627 return LoadMenuIndirect32W( (LPCVOID)LoadResource32( instance, hrsrc ));
3631 /**********************************************************************
3632 * LoadMenuIndirect16 (USER.220)
3634 HMENU16 WINAPI LoadMenuIndirect16( LPCVOID template )
3637 WORD version, offset;
3638 LPCSTR p = (LPCSTR)template;
3640 TRACE(menu,"(%p)\n", template );
3641 version = GET_WORD(p);
3645 WARN(menu, "version must be 0 for Win16\n" );
3648 offset = GET_WORD(p);
3649 p += sizeof(WORD) + offset;
3650 if (!(hMenu = CreateMenu32())) return 0;
3651 if (!MENU_ParseResource( p, hMenu, FALSE ))
3653 DestroyMenu32( hMenu );
3660 /**********************************************************************
3661 * LoadMenuIndirect32A (USER32.371)
3663 HMENU32 WINAPI LoadMenuIndirect32A( LPCVOID template )
3666 WORD version, offset;
3667 LPCSTR p = (LPCSTR)template;
3669 TRACE(menu,"%p\n", template );
3670 version = GET_WORD(p);
3675 offset = GET_WORD(p);
3676 p += sizeof(WORD) + offset;
3677 if (!(hMenu = CreateMenu32())) return 0;
3678 if (!MENU_ParseResource( p, hMenu, TRUE ))
3680 DestroyMenu32( hMenu );
3685 offset = GET_WORD(p);
3686 p += sizeof(WORD) + offset;
3687 if (!(hMenu = CreateMenu32())) return 0;
3688 if (!MENUEX_ParseResource( p, hMenu))
3690 DestroyMenu32( hMenu );
3695 ERR(menu, "version %d not supported.\n", version);
3701 /**********************************************************************
3702 * LoadMenuIndirect32W (USER32.372)
3704 HMENU32 WINAPI LoadMenuIndirect32W( LPCVOID template )
3706 /* FIXME: is there anything different between A and W? */
3707 return LoadMenuIndirect32A( template );
3711 /**********************************************************************
3712 * IsMenu16 (USER.358)
3714 BOOL16 WINAPI IsMenu16( HMENU16 hmenu )
3716 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
3717 return IS_A_MENU(menu);
3721 /**********************************************************************
3722 * IsMenu32 (USER32.346)
3724 BOOL32 WINAPI IsMenu32(HMENU32 hmenu)
3726 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
3727 return IS_A_MENU(menu);
3730 /**********************************************************************
3731 * GetMenuItemInfo32_common
3734 static BOOL32 GetMenuItemInfo32_common ( HMENU32 hmenu, UINT32 item,
3736 LPMENUITEMINFO32A lpmii,
3739 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
3740 debug_print_menuitem("GetMenuItemInfo32_common: ", menu, "");
3744 if (lpmii->fMask & MIIM_TYPE) {
3745 lpmii->fType = menu->fType;
3746 switch (MENU_ITEM_TYPE(menu->fType)) {
3748 if (menu->text && lpmii->dwTypeData && lpmii->cch) {
3750 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData,
3754 lstrcpyn32A(lpmii->dwTypeData,
3761 lpmii->dwTypeData = menu->text;
3767 if (lpmii->fMask & MIIM_STATE)
3768 lpmii->fState = menu->fState;
3770 if (lpmii->fMask & MIIM_ID)
3771 lpmii->wID = menu->wID;
3773 if (lpmii->fMask & MIIM_SUBMENU)
3774 lpmii->hSubMenu = menu->hSubMenu;
3776 if (lpmii->fMask & MIIM_CHECKMARKS) {
3777 lpmii->hbmpChecked = menu->hCheckBit;
3778 lpmii->hbmpUnchecked = menu->hUnCheckBit;
3780 if (lpmii->fMask & MIIM_DATA)
3781 lpmii->dwItemData = menu->dwItemData;
3786 /**********************************************************************
3787 * GetMenuItemInfo32A (USER32.264)
3789 BOOL32 WINAPI GetMenuItemInfo32A( HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3790 LPMENUITEMINFO32A lpmii)
3792 return GetMenuItemInfo32_common (hmenu, item, bypos, lpmii, FALSE);
3795 /**********************************************************************
3796 * GetMenuItemInfo32W (USER32.265)
3798 BOOL32 WINAPI GetMenuItemInfo32W( HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3799 LPMENUITEMINFO32W lpmii)
3801 return GetMenuItemInfo32_common (hmenu, item, bypos,
3802 (LPMENUITEMINFO32A)lpmii, TRUE);
3805 /**********************************************************************
3806 * SetMenuItemInfo32_common
3809 static BOOL32 SetMenuItemInfo32_common(MENUITEM * menu,
3810 const MENUITEMINFO32A *lpmii,
3813 if (!menu) return FALSE;
3815 if (lpmii->fMask & MIIM_TYPE) {
3816 /* Get rid of old string. */
3817 if (IS_STRING_ITEM(menu->fType) && menu->text)
3818 HeapFree(SystemHeap, 0, menu->text);
3820 menu->fType = lpmii->fType;
3821 menu->text = lpmii->dwTypeData;
3822 if (IS_STRING_ITEM(menu->fType) && menu->text) {
3825 ? HEAP_strdupWtoA(SystemHeap, 0,
3826 (LPWSTR) lpmii->dwTypeData)
3827 : HEAP_strdupA(SystemHeap, 0, lpmii->dwTypeData);
3830 if (lpmii->fMask & MIIM_STATE)
3831 menu->fState = lpmii->fState;
3833 if (lpmii->fMask & MIIM_ID)
3834 menu->wID = lpmii->wID;
3836 if (lpmii->fMask & MIIM_SUBMENU) {
3837 menu->hSubMenu = lpmii->hSubMenu;
3838 if (menu->hSubMenu) {
3839 POPUPMENU *subMenu = (POPUPMENU *)USER_HEAP_LIN_ADDR((UINT16)menu->hSubMenu);
3840 if (IS_A_MENU(subMenu)) {
3841 subMenu->wFlags |= MF_POPUP;
3842 menu->fType |= MF_POPUP;
3845 /* FIXME: Return an error ? */
3846 menu->fType &= ~MF_POPUP;
3849 menu->fType &= ~MF_POPUP;
3852 if (lpmii->fMask & MIIM_CHECKMARKS)
3854 menu->hCheckBit = lpmii->hbmpChecked;
3855 menu->hUnCheckBit = lpmii->hbmpUnchecked;
3857 if (lpmii->fMask & MIIM_DATA)
3858 menu->dwItemData = lpmii->dwItemData;
3860 debug_print_menuitem("SetMenuItemInfo32_common: ", menu, "");
3864 /**********************************************************************
3865 * SetMenuItemInfo32A (USER32.491)
3867 BOOL32 WINAPI SetMenuItemInfo32A(HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3868 const MENUITEMINFO32A *lpmii)
3870 return SetMenuItemInfo32_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
3874 /**********************************************************************
3875 * SetMenuItemInfo32W (USER32.492)
3877 BOOL32 WINAPI SetMenuItemInfo32W(HMENU32 hmenu, UINT32 item, BOOL32 bypos,
3878 const MENUITEMINFO32W *lpmii)
3880 return SetMenuItemInfo32_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
3881 (const MENUITEMINFO32A*)lpmii, TRUE);
3884 /**********************************************************************
3885 * SetMenuDefaultItem32 (USER32.489)
3887 BOOL32 WINAPI SetMenuDefaultItem32(HMENU32 hmenu, UINT32 item, UINT32 bypos)
3889 MENUITEM *menuitem = MENU_FindItem(&hmenu, &item, bypos);
3892 if (!menuitem) return FALSE;
3893 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hmenu))) return FALSE;
3895 menu->defitem = item; /* position */
3897 debug_print_menuitem("SetMenuDefaultItem32: ", menuitem, "");
3898 FIXME(menu, "(0x%x,%d,%d), empty stub!\n",
3899 hmenu, item, bypos);
3903 /**********************************************************************
3904 * GetMenuDefaultItem32 (USER32.260)
3906 UINT32 WINAPI GetMenuDefaultItem32(HMENU32 hmenu, UINT32 bypos, UINT32 flags)
3910 if (!(menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hmenu)))
3913 FIXME(menu, "(0x%x,%d,%d), stub!\n", hmenu, bypos, flags);
3914 if (bypos & MF_BYPOSITION)
3915 return menu->defitem;
3917 FIXME (menu, "default item 0x%x\n", menu->defitem);
3918 if ((menu->defitem > 0) && (menu->defitem < menu->nItems))
3919 return menu->items[menu->defitem].wID;
3924 /*******************************************************************
3925 * InsertMenuItem16 (USER.441)
3929 BOOL16 WINAPI InsertMenuItem16( HMENU16 hmenu, UINT16 pos, BOOL16 byposition,
3930 const MENUITEMINFO16 *mii )
3932 MENUITEMINFO32A miia;
3934 miia.cbSize = sizeof(miia);
3935 miia.fMask = mii->fMask;
3936 miia.dwTypeData = mii->dwTypeData;
3937 miia.fType = mii->fType;
3938 miia.fState = mii->fState;
3939 miia.wID = mii->wID;
3940 miia.hSubMenu = mii->hSubMenu;
3941 miia.hbmpChecked = mii->hbmpChecked;
3942 miia.hbmpUnchecked = mii->hbmpUnchecked;
3943 miia.dwItemData = mii->dwItemData;
3944 miia.cch = mii->cch;
3945 if (IS_STRING_ITEM(miia.fType))
3946 miia.dwTypeData = PTR_SEG_TO_LIN(miia.dwTypeData);
3947 return InsertMenuItem32A( hmenu, pos, byposition, &miia );
3951 /**********************************************************************
3952 * InsertMenuItem32A (USER32.323)
3954 BOOL32 WINAPI InsertMenuItem32A(HMENU32 hMenu, UINT32 uItem, BOOL32 bypos,
3955 const MENUITEMINFO32A *lpmii)
3957 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
3958 return SetMenuItemInfo32_common(item, lpmii, FALSE);
3962 /**********************************************************************
3963 * InsertMenuItem32W (USER32.324)
3965 BOOL32 WINAPI InsertMenuItem32W(HMENU32 hMenu, UINT32 uItem, BOOL32 bypos,
3966 const MENUITEMINFO32W *lpmii)
3968 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
3969 return SetMenuItemInfo32_common(item, (const MENUITEMINFO32A*)lpmii, TRUE);
3972 /**********************************************************************
3973 * CheckMenuRadioItem32 (USER32.47)
3976 BOOL32 WINAPI CheckMenuRadioItem32(HMENU32 hMenu,
3977 UINT32 first, UINT32 last, UINT32 check,
3980 MENUITEM *mifirst, *milast, *micheck;
3981 HMENU32 mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
3983 TRACE(menu, "ox%x: %d-%d, check %d, bypos=%d\n",
3984 hMenu, first, last, check, bypos);
3986 mifirst = MENU_FindItem (&mfirst, &first, bypos);
3987 milast = MENU_FindItem (&mlast, &last, bypos);
3988 micheck = MENU_FindItem (&mcheck, &check, bypos);
3990 if (mifirst == NULL || milast == NULL || micheck == NULL ||
3991 mifirst > milast || mfirst != mlast || mfirst != mcheck ||
3992 micheck > milast || micheck < mifirst)
3995 while (mifirst <= milast)
3997 if (mifirst == micheck)
3999 mifirst->fType |= MFT_RADIOCHECK;
4000 mifirst->fState |= MFS_CHECKED;
4002 mifirst->fType &= ~MFT_RADIOCHECK;
4003 mifirst->fState &= ~MFS_CHECKED;
4011 /**********************************************************************
4012 * CheckMenuRadioItem16 (not a Windows API)
4015 BOOL16 WINAPI CheckMenuRadioItem16(HMENU16 hMenu,
4016 UINT16 first, UINT16 last, UINT16 check,
4019 return CheckMenuRadioItem32 (hMenu, first, last, check, bypos);
4022 /**********************************************************************
4023 * GetMenuItemRect32 (USER32.266)
4026 BOOL32 WINAPI GetMenuItemRect32 (HWND32 hwnd, HMENU32 hMenu, UINT32 uItem,
4029 RECT32 saverect, clientrect;
4034 HMENU32 orghMenu = hMenu;
4036 TRACE(menu, "(0x%x,0x%x,%d,%p)\n",
4037 hwnd, hMenu, uItem, rect);
4039 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4040 wndPtr = WIN_FindWndPtr (hwnd);
4041 if (!rect || !item || !wndPtr) return FALSE;
4043 GetClientRect32( hwnd, &clientrect );
4044 hdc = GetDCEx32( hwnd, 0, DCX_CACHE | DCX_WINDOW );
4045 barp = (hMenu == orghMenu);
4047 saverect = item->rect;
4048 MENU_CalcItemSize (hdc, item, hwnd,
4049 clientrect.left, clientrect.top, barp);
4051 item->rect = saverect;
4053 ReleaseDC32( hwnd, hdc );
4057 /**********************************************************************
4058 * GetMenuItemRect16 (USER.665)
4061 BOOL16 WINAPI GetMenuItemRect16 (HWND16 hwnd, HMENU16 hMenu, UINT16 uItem,
4067 if (!rect) return FALSE;
4068 res = GetMenuItemRect32 (hwnd, hMenu, uItem, &r32);
4069 CONV_RECT32TO16 (&r32, rect);
4073 /**********************************************************************
4074 * SetMenuContextHelpId16 (USER.384)
4076 BOOL16 WINAPI SetMenuContextHelpId16( HMENU16 hMenu, DWORD dwContextHelpId)
4078 return SetMenuContextHelpId32( hMenu, dwContextHelpId );
4082 /**********************************************************************
4083 * SetMenuContextHelpId32 (USER32.488)
4085 BOOL32 WINAPI SetMenuContextHelpId32( HMENU32 hMenu, DWORD dwContextHelpId)
4087 FIXME(menu, "SetMenuContextHelpId, stub\n");
4091 /**********************************************************************
4092 * GetMenuContextHelpId16 (USER.385)
4094 DWORD WINAPI GetMenuContextHelpId16( HMENU16 hMenu )
4096 return GetMenuContextHelpId16( hMenu );
4099 /**********************************************************************
4100 * GetMenuContextHelpId32 (USER32.488)
4102 DWORD WINAPI GetMenuContextHelpId32( HMENU32 hMenu )
4104 FIXME(menu, "GetMenuContextHelpId, stub\n");