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 "wine/winbase16.h"
23 #include "wine/winuser16.h"
28 #include "nonclient.h"
33 #include "debugtools.h"
35 DEFAULT_DEBUG_CHANNEL(menu)
38 /* internal popup menu window messages */
40 #define MM_SETMENUHANDLE (WM_USER + 0)
41 #define MM_GETMENUHANDLE (WM_USER + 1)
43 /* Menu item structure */
45 /* ----------- MENUITEMINFO Stuff ----------- */
46 UINT fType; /* Item type. */
47 UINT fState; /* Item state. */
48 UINT wID; /* Item id. */
49 HMENU hSubMenu; /* Pop-up menu. */
50 HBITMAP hCheckBit; /* Bitmap when checked. */
51 HBITMAP hUnCheckBit; /* Bitmap when unchecked. */
52 LPSTR text; /* Item text or bitmap handle. */
53 DWORD dwItemData; /* Application defined. */
54 DWORD dwTypeData; /* depends on fMask */
55 HBITMAP hbmpItem; /* bitmap in win98 style menus */
56 /* ----------- Wine stuff ----------- */
57 RECT rect; /* Item area (relative to menu window) */
58 UINT xTab; /* X position of text after Tab */
61 /* Popup menu structure */
63 WORD wFlags; /* Menu flags (MF_POPUP, MF_SYSMENU) */
64 WORD wMagic; /* Magic number */
65 HQUEUE16 hTaskQ; /* Task queue for this menu */
66 WORD Width; /* Width of the whole menu */
67 WORD Height; /* Height of the whole menu */
68 WORD nItems; /* Number of items in the menu */
69 HWND hWnd; /* Window containing the menu */
70 MENUITEM *items; /* Array of menu items */
71 UINT FocusedItem; /* Currently focused item */
72 HWND hwndOwner; /* window receiving the messages for ownerdraw */
73 BOOL bTimeToHide; /* Request hiding when receiving a second click in the top-level menu item */
74 /* ------------ MENUINFO members ------ */
75 DWORD dwStyle; /* Extended mennu style */
76 UINT cyMax; /* max hight of the whole menu, 0 is screen hight */
77 HBRUSH hbrBack; /* brush for menu background */
78 DWORD dwContextHelpID;
79 DWORD dwMenuData; /* application defined value */
80 HMENU hSysMenuOwner; /* Handle to the dummy sys menu holder */
81 } POPUPMENU, *LPPOPUPMENU;
83 /* internal flags for menu tracking */
85 #define TF_ENDMENU 0x0001
86 #define TF_SUSPENDPOPUP 0x0002
87 #define TF_SKIPREMOVE 0x0004
92 HMENU hCurrentMenu; /* current submenu (can be equal to hTopMenu)*/
93 HMENU hTopMenu; /* initial menu */
94 HWND hOwnerWnd; /* where notifications are sent */
98 #define MENU_MAGIC 0x554d /* 'MU' */
99 #define IS_A_MENU(pmenu) ((pmenu) && (pmenu)->wMagic == MENU_MAGIC)
104 /* Internal MENU_TrackMenu() flags */
105 #define TPM_INTERNAL 0xF0000000
106 #define TPM_ENTERIDLEEX 0x80000000 /* set owner window for WM_ENTERIDLE */
107 #define TPM_BUTTONDOWN 0x40000000 /* menu was clicked before tracking */
108 #define TPM_POPUPMENU 0x20000000 /* menu is a popup menu */
110 /* popup menu shade thickness */
111 #define POPUP_XSHADE 4
112 #define POPUP_YSHADE 4
114 /* Space between 2 menu bar items */
115 #define MENU_BAR_ITEMS_SPACE 12
117 /* Minimum width of a tab character */
118 #define MENU_TAB_SPACE 8
120 /* Height of a separator item */
121 #define SEPARATOR_HEIGHT 5
123 /* (other menu->FocusedItem values give the position of the focused item) */
124 #define NO_SELECTED_ITEM 0xffff
126 #define MENU_ITEM_TYPE(flags) \
127 ((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR))
129 #define IS_STRING_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_STRING)
130 #define IS_BITMAP_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_BITMAP)
132 #define IS_SYSTEM_MENU(menu) \
133 (!((menu)->wFlags & MF_POPUP) && (menu)->wFlags & MF_SYSMENU)
135 #define IS_SYSTEM_POPUP(menu) \
136 ((menu)->wFlags & MF_POPUP && (menu)->wFlags & MF_SYSMENU)
138 #define TYPE_MASK (MFT_STRING | MFT_BITMAP | MFT_OWNERDRAW | MFT_SEPARATOR | \
139 MFT_MENUBARBREAK | MFT_MENUBREAK | MFT_RADIOCHECK | \
140 MFT_RIGHTORDER | MFT_RIGHTJUSTIFY | \
141 MF_POPUP | MF_SYSMENU | MF_HELP)
142 #define STATE_MASK (~TYPE_MASK)
144 /* Dimension of the menu bitmaps */
145 static WORD check_bitmap_width = 0, check_bitmap_height = 0;
146 static WORD arrow_bitmap_width = 0, arrow_bitmap_height = 0;
148 static HBITMAP hStdRadioCheck = 0;
149 static HBITMAP hStdCheck = 0;
150 static HBITMAP hStdMnArrow = 0;
152 /* Minimze/restore/close buttons to be inserted in menubar */
153 static HBITMAP hBmpMinimize = 0;
154 static HBITMAP hBmpMinimizeD = 0;
155 static HBITMAP hBmpMaximize = 0;
156 static HBITMAP hBmpMaximizeD = 0;
157 static HBITMAP hBmpClose = 0;
158 static HBITMAP hBmpCloseD = 0;
161 static HBRUSH hShadeBrush = 0;
162 static HFONT hMenuFont = 0;
163 static HFONT hMenuFontBold = 0;
165 static HMENU MENU_DefSysPopup = 0; /* Default system menu popup */
167 /* Use global popup window because there's no way 2 menus can
168 * be tracked at the same time. */
170 static WND* pTopPopupWnd = 0;
171 static UINT uSubPWndLevel = 0;
173 /* Flag set by EndMenu() to force an exit from menu tracking */
174 static BOOL fEndMenu = FALSE;
177 /***********************************************************************
178 * debug_print_menuitem
180 * Print a menuitem in readable form.
183 #define debug_print_menuitem(pre, mp, post) \
184 if(!TRACE_ON(menu)) ; else do_debug_print_menuitem(pre, mp, post)
186 #define MENUOUT(text) \
187 DPRINTF("%s%s", (count++ ? "," : ""), (text))
189 #define MENUFLAG(bit,text) \
191 if (flags & (bit)) { flags &= ~(bit); MENUOUT ((text)); } \
194 static void do_debug_print_menuitem(const char *prefix, MENUITEM * mp,
197 TRACE("%s ", prefix);
199 UINT flags = mp->fType;
200 int typ = MENU_ITEM_TYPE(flags);
201 DPRINTF( "{ ID=0x%x", mp->wID);
202 if (flags & MF_POPUP)
203 DPRINTF( ", Sub=0x%x", mp->hSubMenu);
207 if (typ == MFT_STRING)
209 else if (typ == MFT_SEPARATOR)
211 else if (typ == MFT_OWNERDRAW)
213 else if (typ == MFT_BITMAP)
219 MENUFLAG(MF_POPUP, "pop");
220 MENUFLAG(MFT_MENUBARBREAK, "barbrk");
221 MENUFLAG(MFT_MENUBREAK, "brk");
222 MENUFLAG(MFT_RADIOCHECK, "radio");
223 MENUFLAG(MFT_RIGHTORDER, "rorder");
224 MENUFLAG(MF_SYSMENU, "sys");
225 MENUFLAG(MFT_RIGHTJUSTIFY, "right"); /* same as MF_HELP */
228 DPRINTF( "+0x%x", flags);
233 DPRINTF( ", State=");
234 MENUFLAG(MFS_GRAYED, "grey");
235 MENUFLAG(MFS_DEFAULT, "default");
236 MENUFLAG(MFS_DISABLED, "dis");
237 MENUFLAG(MFS_CHECKED, "check");
238 MENUFLAG(MFS_HILITE, "hi");
239 MENUFLAG(MF_USECHECKBITMAPS, "usebit");
240 MENUFLAG(MF_MOUSESELECT, "mouse");
242 DPRINTF( "+0x%x", flags);
245 DPRINTF( ", Chk=0x%x", mp->hCheckBit);
247 DPRINTF( ", Unc=0x%x", mp->hUnCheckBit);
249 if (typ == MFT_STRING) {
251 DPRINTF( ", Text=\"%s\"", mp->text);
253 DPRINTF( ", Text=Null");
254 } else if (mp->text == NULL)
257 DPRINTF( ", Text=%p", mp->text);
259 DPRINTF( ", ItemData=0x%08lx", mp->dwItemData);
265 DPRINTF(" %s\n", postfix);
272 /***********************************************************************
275 * Validate the given menu handle and returns the menu structure pointer.
277 POPUPMENU *MENU_GetMenu(HMENU hMenu)
280 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hMenu);
281 if (!IS_A_MENU(menu))
283 ERR("invalid menu handle=%x, ptr=%p, magic=%x\n", hMenu, menu, menu? menu->wMagic:0);
289 /***********************************************************************
292 * Return the default system menu.
294 static HMENU MENU_CopySysPopup(void)
296 HMENU hMenu = LoadMenuA(GetModuleHandleA("USER32"), "SYSMENU");
299 POPUPMENU* menu = (POPUPMENU *) USER_HEAP_LIN_ADDR(hMenu);
300 menu->wFlags |= MF_SYSMENU | MF_POPUP;
301 SetMenuDefaultItem(hMenu, SC_CLOSE, FALSE);
305 ERR("Unable to load default system menu\n" );
308 TRACE("returning %x.\n", hMenu );
313 /***********************************************************************
314 * MENU_GetTopPopupWnd()
316 * Return the locked pointer pTopPopupWnd.
318 static WND *MENU_GetTopPopupWnd()
320 return WIN_LockWndPtr(pTopPopupWnd);
322 /***********************************************************************
323 * MENU_ReleaseTopPopupWnd()
325 * Realease the locked pointer pTopPopupWnd.
327 static void MENU_ReleaseTopPopupWnd()
329 WIN_ReleaseWndPtr(pTopPopupWnd);
331 /***********************************************************************
332 * MENU_DestroyTopPopupWnd()
334 * Destroy the locked pointer pTopPopupWnd.
336 static void MENU_DestroyTopPopupWnd()
338 WND *tmpWnd = pTopPopupWnd;
340 WIN_ReleaseWndPtr(tmpWnd);
345 /**********************************************************************
348 * Create a copy of the system menu. System menu in Windows is
349 * a special menu-bar with the single entry - system menu popup.
350 * This popup is presented to the outside world as a "system menu".
351 * However, the real system menu handle is sometimes seen in the
352 * WM_MENUSELECT paramemters (and Word 6 likes it this way).
354 HMENU MENU_GetSysMenu( HWND hWnd, HMENU hPopupMenu )
358 if ((hMenu = CreateMenu()))
360 POPUPMENU *menu = (POPUPMENU*) USER_HEAP_LIN_ADDR(hMenu);
361 menu->wFlags = MF_SYSMENU;
364 if (hPopupMenu == (HMENU)(-1))
365 hPopupMenu = MENU_CopySysPopup();
366 else if( !hPopupMenu ) hPopupMenu = MENU_DefSysPopup;
370 InsertMenuA( hMenu, -1, MF_SYSMENU | MF_POPUP | MF_BYPOSITION, hPopupMenu, NULL );
372 menu->items[0].fType = MF_SYSMENU | MF_POPUP;
373 menu->items[0].fState = 0;
374 menu = (POPUPMENU*) USER_HEAP_LIN_ADDR(hPopupMenu);
375 menu->wFlags |= MF_SYSMENU;
377 TRACE("GetSysMenu hMenu=%04x (%04x)\n", hMenu, hPopupMenu );
380 DestroyMenu( hMenu );
382 ERR("failed to load system menu!\n");
387 /***********************************************************************
390 * Menus initialisation.
395 NONCLIENTMETRICSA ncm;
397 static unsigned char shade_bits[16] = { 0x55, 0, 0xAA, 0,
402 /* Load menu bitmaps */
403 hStdCheck = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_CHECK));
404 hStdRadioCheck = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_RADIOCHECK));
405 hStdMnArrow = LoadBitmapA(0, MAKEINTRESOURCEA(OBM_MNARROW));
406 /* Load system buttons bitmaps */
407 hBmpMinimize = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_REDUCE));
408 hBmpMinimizeD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_REDUCED));
409 hBmpMaximize = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_RESTORE));
410 hBmpMaximizeD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_RESTORED));
411 hBmpClose = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_CLOSE));
412 hBmpCloseD = LoadBitmapA(0,MAKEINTRESOURCEA(OBM_CLOSED));
417 GetObjectA( hStdCheck, sizeof(bm), &bm );
418 check_bitmap_width = bm.bmWidth;
419 check_bitmap_height = bm.bmHeight;
423 /* Assume that radio checks have the same size as regular check. */
430 GetObjectA( hStdMnArrow, sizeof(bm), &bm );
431 arrow_bitmap_width = bm.bmWidth;
432 arrow_bitmap_height = bm.bmHeight;
436 if (! (hBitmap = CreateBitmap( 8, 8, 1, 1, shade_bits)))
439 if(!(hShadeBrush = CreatePatternBrush( hBitmap )))
442 DeleteObject( hBitmap );
443 if (!(MENU_DefSysPopup = MENU_CopySysPopup()))
446 ncm.cbSize = sizeof (NONCLIENTMETRICSA);
447 if (!(SystemParametersInfoA(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSA), &ncm, 0)))
450 if (!(hMenuFont = CreateFontIndirectA( &ncm.lfMenuFont )))
453 ncm.lfMenuFont.lfWeight += 300;
454 if ( ncm.lfMenuFont.lfWeight > 1000)
455 ncm.lfMenuFont.lfWeight = 1000;
457 if (!(hMenuFontBold = CreateFontIndirectA( &ncm.lfMenuFont )))
463 /***********************************************************************
464 * MENU_InitSysMenuPopup
466 * Grey the appropriate items in System menu.
468 static void MENU_InitSysMenuPopup( HMENU hmenu, DWORD style, DWORD clsStyle )
472 gray = !(style & WS_THICKFRAME) || (style & (WS_MAXIMIZE | WS_MINIMIZE));
473 EnableMenuItem( hmenu, SC_SIZE, (gray ? MF_GRAYED : MF_ENABLED) );
474 gray = ((style & WS_MAXIMIZE) != 0);
475 EnableMenuItem( hmenu, SC_MOVE, (gray ? MF_GRAYED : MF_ENABLED) );
476 gray = !(style & WS_MINIMIZEBOX) || (style & WS_MINIMIZE);
477 EnableMenuItem( hmenu, SC_MINIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
478 gray = !(style & WS_MAXIMIZEBOX) || (style & WS_MAXIMIZE);
479 EnableMenuItem( hmenu, SC_MAXIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
480 gray = !(style & (WS_MAXIMIZE | WS_MINIMIZE));
481 EnableMenuItem( hmenu, SC_RESTORE, (gray ? MF_GRAYED : MF_ENABLED) );
482 gray = (clsStyle & CS_NOCLOSE) != 0;
484 /* The menu item must keep its state if it's disabled */
486 EnableMenuItem( hmenu, SC_CLOSE, MF_GRAYED);
490 /******************************************************************************
492 * UINT32 MENU_GetStartOfNextColumn(
495 *****************************************************************************/
497 static UINT MENU_GetStartOfNextColumn(
500 POPUPMENU *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu);
501 UINT i = menu->FocusedItem + 1;
504 return NO_SELECTED_ITEM;
506 if( i == NO_SELECTED_ITEM )
509 for( ; i < menu->nItems; ++i ) {
510 if (menu->items[i].fType & MF_MENUBARBREAK)
514 return NO_SELECTED_ITEM;
518 /******************************************************************************
520 * UINT32 MENU_GetStartOfPrevColumn(
523 *****************************************************************************/
525 static UINT MENU_GetStartOfPrevColumn(
528 POPUPMENU const *menu = (POPUPMENU *)USER_HEAP_LIN_ADDR(hMenu);
532 return NO_SELECTED_ITEM;
534 if( menu->FocusedItem == 0 || menu->FocusedItem == NO_SELECTED_ITEM )
535 return NO_SELECTED_ITEM;
537 /* Find the start of the column */
539 for(i = menu->FocusedItem; i != 0 &&
540 !(menu->items[i].fType & MF_MENUBARBREAK);
544 return NO_SELECTED_ITEM;
546 for(--i; i != 0; --i) {
547 if (menu->items[i].fType & MF_MENUBARBREAK)
551 TRACE("ret %d.\n", i );
558 /***********************************************************************
561 * Find a menu item. Return a pointer on the item, and modifies *hmenu
562 * in case the item was in a sub-menu.
564 static MENUITEM *MENU_FindItem( HMENU *hmenu, UINT *nPos, UINT wFlags )
569 if (((*hmenu)==0xffff) || (!(menu = MENU_GetMenu(*hmenu)))) return NULL;
570 if (wFlags & MF_BYPOSITION)
572 if (*nPos >= menu->nItems) return NULL;
573 return &menu->items[*nPos];
577 MENUITEM *item = menu->items;
578 for (i = 0; i < menu->nItems; i++, item++)
580 if (item->wID == *nPos)
585 else if (item->fType & MF_POPUP)
587 HMENU hsubmenu = item->hSubMenu;
588 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
600 /***********************************************************************
603 * Find a Sub menu. Return the position of the submenu, and modifies
604 * *hmenu in case it is found in another sub-menu.
605 * If the submenu cannot be found, NO_SELECTED_ITEM is returned.
607 UINT MENU_FindSubMenu( HMENU *hmenu, HMENU hSubTarget )
612 if (((*hmenu)==0xffff) ||
613 (!(menu = MENU_GetMenu(*hmenu))))
614 return NO_SELECTED_ITEM;
616 for (i = 0; i < menu->nItems; i++, item++) {
617 if(!(item->fType & MF_POPUP)) continue;
618 if (item->hSubMenu == hSubTarget) {
622 HMENU hsubmenu = item->hSubMenu;
623 UINT pos = MENU_FindSubMenu( &hsubmenu, hSubTarget );
624 if (pos != NO_SELECTED_ITEM) {
630 return NO_SELECTED_ITEM;
633 /***********************************************************************
636 static void MENU_FreeItemData( MENUITEM* item )
639 if (IS_STRING_ITEM(item->fType) && item->text)
640 HeapFree( SystemHeap, 0, item->text );
643 /***********************************************************************
644 * MENU_FindItemByCoords
646 * Find the item at the specified coordinates (screen coords). Does
647 * not work for child windows and therefore should not be called for
648 * an arbitrary system menu.
650 static MENUITEM *MENU_FindItemByCoords( POPUPMENU *menu,
651 POINT pt, UINT *pos )
657 if (!GetWindowRect(menu->hWnd,&wrect)) return NULL;
658 pt.x -= wrect.left;pt.y -= wrect.top;
660 for (i = 0; i < menu->nItems; i++, item++)
662 if ((pt.x >= item->rect.left) && (pt.x < item->rect.right) &&
663 (pt.y >= item->rect.top) && (pt.y < item->rect.bottom))
673 /***********************************************************************
676 * Find the menu item selected by a key press.
677 * Return item id, -1 if none, -2 if we should close the menu.
679 static UINT MENU_FindItemByKey( HWND hwndOwner, HMENU hmenu,
680 UINT key, BOOL forceMenuChar )
682 TRACE("\tlooking for '%c' in [%04x]\n", (char)key, (UINT16)hmenu );
684 if (!IsMenu( hmenu ))
686 WND* w = WIN_FindWndPtr(hwndOwner);
687 hmenu = GetSubMenu(w->hSysMenu, 0);
688 WIN_ReleaseWndPtr(w);
693 POPUPMENU *menu = MENU_GetMenu( hmenu );
694 MENUITEM *item = menu->items;
702 for (i = 0; i < menu->nItems; i++, item++)
704 if (item->text && (IS_STRING_ITEM(item->fType)))
706 char *p = item->text - 2;
709 p = strchr (p + 2, '&');
711 while (p != NULL && p [1] == '&');
712 if (p && (toupper(p[1]) == key)) return i;
716 menuchar = SendMessageA( hwndOwner, WM_MENUCHAR,
717 MAKEWPARAM( key, menu->wFlags ), hmenu );
718 if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
719 if (HIWORD(menuchar) == 1) return (UINT)(-2);
723 /***********************************************************************
726 * Load the bitmap associated with the magic menu item and its style
729 static HBITMAP MENU_LoadMagicItem(UINT id, BOOL hilite, DWORD dwItemData)
732 * Magic menu item id's section
733 * These magic id's are used by windows to insert "standard" mdi
734 * buttons (minimize,restore,close) on menu. Under windows,
735 * these magic id's make sure the right things appear when those
736 * bitmap buttons are pressed/selected/released.
740 { case HBMMENU_SYSTEM:
741 return (dwItemData) ?
742 (HBITMAP)dwItemData :
743 (hilite ? hBmpMinimizeD : hBmpMinimize);
744 case HBMMENU_MBAR_RESTORE:
745 return (hilite ? hBmpMaximizeD: hBmpMaximize);
746 case HBMMENU_MBAR_MINIMIZE:
747 return (hilite ? hBmpMinimizeD : hBmpMinimize);
748 case HBMMENU_MBAR_CLOSE:
749 return (hilite ? hBmpCloseD : hBmpClose);
750 case HBMMENU_CALLBACK:
751 case HBMMENU_MBAR_CLOSE_D:
752 case HBMMENU_MBAR_MINIMIZE_D:
753 case HBMMENU_POPUP_CLOSE:
754 case HBMMENU_POPUP_RESTORE:
755 case HBMMENU_POPUP_MAXIMIZE:
756 case HBMMENU_POPUP_MINIMIZE:
758 FIXME("Magic 0x%08x not implemented\n", id);
764 /***********************************************************************
767 * Calculate the size of the menu item and store it in lpitem->rect.
769 static void MENU_CalcItemSize( HDC hdc, MENUITEM *lpitem, HWND hwndOwner,
770 INT orgX, INT orgY, BOOL menuBar )
774 TRACE("dc=0x%04x owner=0x%04x (%d,%d)\n", hdc, hwndOwner, orgX, orgY);
775 debug_print_menuitem("MENU_CalcItemSize: menuitem:", lpitem,
776 (menuBar ? " (MenuBar)" : ""));
778 SetRect( &lpitem->rect, orgX, orgY, orgX, orgY );
780 if (lpitem->fType & MF_OWNERDRAW)
782 MEASUREITEMSTRUCT mis;
783 mis.CtlType = ODT_MENU;
785 mis.itemID = lpitem->wID;
786 mis.itemData = (DWORD)lpitem->dwItemData;
789 SendMessageA( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)&mis );
790 lpitem->rect.bottom += mis.itemHeight;
791 lpitem->rect.right += mis.itemWidth;
792 TRACE("id=%04x size=%dx%d\n",
793 lpitem->wID, mis.itemWidth, mis.itemHeight);
797 if (lpitem->fType & MF_SEPARATOR)
799 lpitem->rect.bottom += SEPARATOR_HEIGHT;
805 lpitem->rect.right += 2 * check_bitmap_width;
806 if (lpitem->fType & MF_POPUP)
807 lpitem->rect.right += arrow_bitmap_width;
810 if (IS_BITMAP_ITEM(lpitem->fType))
815 /* Check if there is a magic menu item associated with this item */
816 if((LOWORD((int)lpitem->text))<12)
818 resBmp = MENU_LoadMagicItem((int)lpitem->text, (lpitem->fType & MF_HILITE),
822 resBmp = (HBITMAP)lpitem->text;
824 if (GetObjectA(resBmp, sizeof(bm), &bm ))
826 lpitem->rect.right += bm.bmWidth;
827 lpitem->rect.bottom += bm.bmHeight;
833 /* If we get here, then it must be a text item */
834 if (IS_STRING_ITEM( lpitem->fType ))
837 GetTextExtentPoint32A(hdc, lpitem->text, strlen(lpitem->text), &size);
839 lpitem->rect.right += size.cx;
840 if (TWEAK_WineLook == WIN31_LOOK)
841 lpitem->rect.bottom += MAX( size.cy, GetSystemMetrics(SM_CYMENU) );
843 lpitem->rect.bottom += MAX (size.cy, GetSystemMetrics(SM_CYMENU)-1);
848 lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
850 else if ((p = strchr( lpitem->text, '\t' )) != NULL)
852 /* Item contains a tab (only meaningful in popup menus) */
853 GetTextExtentPoint32A(hdc, lpitem->text, (int)(p - lpitem->text) , &size);
854 lpitem->xTab = check_bitmap_width + MENU_TAB_SPACE + size.cx;
855 lpitem->rect.right += MENU_TAB_SPACE;
859 if (strchr( lpitem->text, '\b' ))
860 lpitem->rect.right += MENU_TAB_SPACE;
861 lpitem->xTab = lpitem->rect.right - check_bitmap_width
862 - arrow_bitmap_width;
865 TRACE("(%d,%d)-(%d,%d)\n", lpitem->rect.left, lpitem->rect.top, lpitem->rect.right, lpitem->rect.bottom);
869 /***********************************************************************
870 * MENU_PopupMenuCalcSize
872 * Calculate the size of a popup menu.
874 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, HWND hwndOwner )
879 int orgX, orgY, maxX, maxTab, maxTabWidth;
881 lppop->Width = lppop->Height = 0;
882 if (lppop->nItems == 0) return;
885 SelectObject( hdc, hMenuFont);
888 maxX = (TWEAK_WineLook == WIN31_LOOK) ? GetSystemMetrics(SM_CXBORDER) : 2 ;
890 while (start < lppop->nItems)
892 lpitem = &lppop->items[start];
894 orgY = (TWEAK_WineLook == WIN31_LOOK) ? GetSystemMetrics(SM_CYBORDER) : 2;
896 maxTab = maxTabWidth = 0;
898 /* Parse items until column break or end of menu */
899 for (i = start; i < lppop->nItems; i++, lpitem++)
902 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
904 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, FALSE );
906 if (lpitem->fType & MF_MENUBARBREAK) orgX++;
907 maxX = MAX( maxX, lpitem->rect.right );
908 orgY = lpitem->rect.bottom;
909 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
911 maxTab = MAX( maxTab, lpitem->xTab );
912 maxTabWidth = MAX(maxTabWidth,lpitem->rect.right-lpitem->xTab);
916 /* Finish the column (set all items to the largest width found) */
917 maxX = MAX( maxX, maxTab + maxTabWidth );
918 for (lpitem = &lppop->items[start]; start < i; start++, lpitem++)
920 lpitem->rect.right = maxX;
921 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
922 lpitem->xTab = maxTab;
925 lppop->Height = MAX( lppop->Height, orgY );
930 /* space for 3d border */
931 if(TWEAK_WineLook > WIN31_LOOK)
941 /***********************************************************************
942 * MENU_MenuBarCalcSize
944 * FIXME: Word 6 implements its own MDI and its own 'close window' bitmap
945 * height is off by 1 pixel which causes lengthy window relocations when
946 * active document window is maximized/restored.
948 * Calculate the size of the menu bar.
950 static void MENU_MenuBarCalcSize( HDC hdc, LPRECT lprect,
951 LPPOPUPMENU lppop, HWND hwndOwner )
954 int start, i, orgX, orgY, maxY, helpPos;
956 if ((lprect == NULL) || (lppop == NULL)) return;
957 if (lppop->nItems == 0) return;
958 TRACE("left=%d top=%d right=%d bottom=%d\n",
959 lprect->left, lprect->top, lprect->right, lprect->bottom);
960 lppop->Width = lprect->right - lprect->left;
965 while (start < lppop->nItems)
967 lpitem = &lppop->items[start];
971 /* Parse items until line break or end of menu */
972 for (i = start; i < lppop->nItems; i++, lpitem++)
974 if ((helpPos == -1) && (lpitem->fType & MF_HELP)) helpPos = i;
976 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
978 TRACE("calling MENU_CalcItemSize org=(%d, %d)\n",
980 debug_print_menuitem (" item: ", lpitem, "");
981 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE );
983 if (lpitem->rect.right > lprect->right)
985 if (i != start) break;
986 else lpitem->rect.right = lprect->right;
988 maxY = MAX( maxY, lpitem->rect.bottom );
989 orgX = lpitem->rect.right;
992 /* Finish the line (set all items to the largest height found) */
993 while (start < i) lppop->items[start++].rect.bottom = maxY;
996 lprect->bottom = maxY;
997 lppop->Height = lprect->bottom - lprect->top;
999 /* Flush right all magic items and items between the MF_HELP and */
1000 /* the last item (if several lines, only move the last line) */
1001 lpitem = &lppop->items[lppop->nItems-1];
1002 orgY = lpitem->rect.top;
1003 orgX = lprect->right;
1004 for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--)
1006 if ( !IS_BITMAP_ITEM(lpitem->fType) && ((helpPos ==-1) ? TRUE : (helpPos>i) ))
1008 if (lpitem->rect.top != orgY) break; /* Other line */
1009 if (lpitem->rect.right >= orgX) break; /* Too far right already */
1010 lpitem->rect.left += orgX - lpitem->rect.right;
1011 lpitem->rect.right = orgX;
1012 orgX = lpitem->rect.left;
1016 /***********************************************************************
1019 * Draw a single menu item.
1021 static void MENU_DrawMenuItem( HWND hwnd, HMENU hmenu, HWND hwndOwner, HDC hdc, MENUITEM *lpitem,
1022 UINT height, BOOL menuBar, UINT odaction )
1026 debug_print_menuitem("MENU_DrawMenuItem: ", lpitem, "");
1028 if (lpitem->fType & MF_SYSMENU)
1030 if( !IsIconic(hwnd) ) {
1031 if (TWEAK_WineLook > WIN31_LOOK)
1032 NC_DrawSysButton95( hwnd, hdc,
1034 (MF_HILITE | MF_MOUSESELECT) );
1036 NC_DrawSysButton( hwnd, hdc,
1038 (MF_HILITE | MF_MOUSESELECT) );
1044 if (lpitem->fType & MF_OWNERDRAW)
1048 dis.CtlType = ODT_MENU;
1050 dis.itemID = lpitem->wID;
1051 dis.itemData = (DWORD)lpitem->dwItemData;
1053 if (lpitem->fState & MF_CHECKED) dis.itemState |= ODS_CHECKED;
1054 if (lpitem->fState & MF_GRAYED) dis.itemState |= ODS_GRAYED;
1055 if (lpitem->fState & MF_HILITE) dis.itemState |= ODS_SELECTED;
1056 dis.itemAction = odaction; /* ODA_DRAWENTIRE | ODA_SELECT | ODA_FOCUS; */
1057 dis.hwndItem = hmenu;
1059 dis.rcItem = lpitem->rect;
1060 TRACE("Ownerdraw: owner=%04x itemID=%d, itemState=%d, itemAction=%d, "
1061 "hwndItem=%04x, hdc=%04x, rcItem={%d,%d,%d,%d}\n", hwndOwner,
1062 dis.itemID, dis.itemState, dis.itemAction, dis.hwndItem,
1063 dis.hDC, dis.rcItem.left, dis.rcItem.top, dis.rcItem.right,
1065 SendMessageA( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&dis );
1069 TRACE("rect={%d,%d,%d,%d}\n", lpitem->rect.left, lpitem->rect.top,
1070 lpitem->rect.right,lpitem->rect.bottom);
1072 if (menuBar && (lpitem->fType & MF_SEPARATOR)) return;
1074 rect = lpitem->rect;
1076 if ((lpitem->fState & MF_HILITE) && !(IS_BITMAP_ITEM(lpitem->fType)))
1077 if ((menuBar) && (TWEAK_WineLook==WIN98_LOOK))
1078 DrawEdge(hdc, &rect, BDR_SUNKENOUTER, BF_RECT);
1080 FillRect( hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT) );
1082 FillRect( hdc, &rect, GetSysColorBrush(COLOR_MENU) );
1084 SetBkMode( hdc, TRANSPARENT );
1086 /* vertical separator */
1087 if (!menuBar && (lpitem->fType & MF_MENUBARBREAK))
1089 if (TWEAK_WineLook > WIN31_LOOK)
1093 rc.bottom = height - 3;
1094 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_LEFT);
1098 SelectObject( hdc, GetSysColorPen(COLOR_WINDOWFRAME) );
1099 MoveTo16( hdc, rect.left, 0 );
1100 LineTo( hdc, rect.left, height );
1104 /* horizontal separator */
1105 if (lpitem->fType & MF_SEPARATOR)
1107 if (TWEAK_WineLook > WIN31_LOOK)
1112 rc.top += SEPARATOR_HEIGHT / 2;
1113 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_TOP);
1117 SelectObject( hdc, GetSysColorPen(COLOR_WINDOWFRAME) );
1118 MoveTo16( hdc, rect.left, rect.top + SEPARATOR_HEIGHT/2 );
1119 LineTo( hdc, rect.right, rect.top + SEPARATOR_HEIGHT/2 );
1126 if ((lpitem->fState & MF_HILITE) && !(IS_BITMAP_ITEM(lpitem->fType)) )
1128 if (lpitem->fState & MF_GRAYED)
1129 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1131 SetTextColor( hdc, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
1132 SetBkColor( hdc, GetSysColor( COLOR_HIGHLIGHT ) );
1136 if (lpitem->fState & MF_GRAYED)
1137 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1139 SetTextColor( hdc, GetSysColor( COLOR_MENUTEXT ) );
1140 SetBkColor( hdc, GetSysColor( COLOR_MENU ) );
1143 /* helper lines for debugging */
1144 /* FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
1145 SelectObject( hdc, GetSysColorPen(COLOR_WINDOWFRAME) );
1146 MoveTo16( hdc, rect.left, (rect.top + rect.bottom)/2 );
1147 LineTo( hdc, rect.right, (rect.top + rect.bottom)/2 );
1152 INT y = rect.top + rect.bottom;
1154 /* Draw the check mark
1157 * Custom checkmark bitmaps are monochrome but not always 1bpp.
1160 if (lpitem->fState & MF_CHECKED)
1162 HBITMAP bm = lpitem->hCheckBit ? lpitem->hCheckBit :
1163 ((lpitem->fType & MFT_RADIOCHECK) ? hStdRadioCheck : hStdCheck);
1164 HDC hdcMem = CreateCompatibleDC( hdc );
1166 SelectObject( hdcMem, bm );
1167 BitBlt( hdc, rect.left, (y - check_bitmap_height) / 2,
1168 check_bitmap_width, check_bitmap_height,
1169 hdcMem, 0, 0, SRCCOPY );
1172 else if (lpitem->hUnCheckBit)
1174 HDC hdcMem = CreateCompatibleDC( hdc );
1176 SelectObject( hdcMem, lpitem->hUnCheckBit );
1177 BitBlt( hdc, rect.left, (y - check_bitmap_height) / 2,
1178 check_bitmap_width, check_bitmap_height,
1179 hdcMem, 0, 0, SRCCOPY );
1183 /* Draw the popup-menu arrow */
1184 if (lpitem->fType & MF_POPUP)
1186 HDC hdcMem = CreateCompatibleDC( hdc );
1188 SelectObject( hdcMem, hStdMnArrow );
1189 BitBlt( hdc, rect.right - arrow_bitmap_width - 1,
1190 (y - arrow_bitmap_height) / 2,
1191 arrow_bitmap_width, arrow_bitmap_height,
1192 hdcMem, 0, 0, SRCCOPY );
1196 rect.left += check_bitmap_width;
1197 rect.right -= arrow_bitmap_width;
1200 /* Draw the item text or bitmap */
1201 if (IS_BITMAP_ITEM(lpitem->fType))
1206 HDC hdcMem = CreateCompatibleDC( hdc );
1209 * Check if there is a magic menu item associated with this item
1210 * and load the appropriate bitmap
1212 if((LOWORD((int)lpitem->text)) < 12)
1214 resBmp = MENU_LoadMagicItem((int)lpitem->text, (lpitem->fState & MF_HILITE),
1215 lpitem->dwItemData);
1218 resBmp = (HBITMAP)lpitem->text;
1223 GetObjectA( resBmp, sizeof(bm), &bm );
1225 SelectObject(hdcMem,resBmp );
1227 /* handle fontsize > bitmap_height */
1228 top = ((rect.bottom-rect.top)>bm.bmHeight) ?
1229 rect.top+(rect.bottom-rect.top-bm.bmHeight)/2 : rect.top;
1231 BitBlt( hdc, rect.left, top, rect.right - rect.left,
1232 rect.bottom - rect.top, hdcMem, 0, 0, SRCCOPY );
1239 /* No bitmap - process text if present */
1240 else if (IS_STRING_ITEM(lpitem->fType))
1245 UINT uFormat = (menuBar) ?
1246 DT_CENTER | DT_VCENTER | DT_SINGLELINE :
1247 DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1249 if ( lpitem->fState & MFS_DEFAULT )
1251 hfontOld = SelectObject( hdc, hMenuFontBold);
1256 rect.left += MENU_BAR_ITEMS_SPACE / 2;
1257 rect.right -= MENU_BAR_ITEMS_SPACE / 2;
1258 i = strlen( lpitem->text );
1262 for (i = 0; lpitem->text[i]; i++)
1263 if ((lpitem->text[i] == '\t') || (lpitem->text[i] == '\b'))
1267 if( !(TWEAK_WineLook == WIN31_LOOK) && (lpitem->fState & MF_GRAYED))
1269 if (!(lpitem->fState & MF_HILITE) )
1271 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1272 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1273 DrawTextA( hdc, lpitem->text, i, &rect, uFormat );
1274 --rect.left; --rect.top; --rect.right; --rect.bottom;
1276 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1279 DrawTextA( hdc, lpitem->text, i, &rect, uFormat);
1281 /* paint the shortcut text */
1282 if (lpitem->text[i]) /* There's a tab or flush-right char */
1284 if (lpitem->text[i] == '\t')
1286 rect.left = lpitem->xTab;
1287 uFormat = DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1291 uFormat = DT_RIGHT | DT_VCENTER | DT_SINGLELINE;
1294 if( !(TWEAK_WineLook == WIN31_LOOK) && (lpitem->fState & MF_GRAYED))
1296 if (!(lpitem->fState & MF_HILITE) )
1298 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1299 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1300 DrawTextA( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1301 --rect.left; --rect.top; --rect.right; --rect.bottom;
1303 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1305 DrawTextA( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1309 SelectObject (hdc, hfontOld);
1314 /***********************************************************************
1315 * MENU_DrawPopupMenu
1317 * Paint a popup menu.
1319 static void MENU_DrawPopupMenu( HWND hwnd, HDC hdc, HMENU hmenu )
1321 HBRUSH hPrevBrush = 0;
1324 TRACE("wnd=0x%04x dc=0x%04x menu=0x%04x\n", hwnd, hdc, hmenu);
1326 GetClientRect( hwnd, &rect );
1328 if(TWEAK_WineLook == WIN31_LOOK)
1330 rect.bottom -= POPUP_YSHADE * GetSystemMetrics(SM_CYBORDER);
1331 rect.right -= POPUP_XSHADE * GetSystemMetrics(SM_CXBORDER);
1334 if((hPrevBrush = SelectObject( hdc, GetSysColorBrush(COLOR_MENU) ))
1335 && (SelectObject( hdc, hMenuFont)))
1339 Rectangle( hdc, rect.left, rect.top, rect.right, rect.bottom );
1341 hPrevPen = SelectObject( hdc, GetStockObject( NULL_PEN ) );
1347 /* draw 3-d shade */
1348 if(TWEAK_WineLook == WIN31_LOOK) {
1349 SelectObject( hdc, hShadeBrush );
1350 SetBkMode( hdc, TRANSPARENT );
1351 ropPrev = SetROP2( hdc, R2_MASKPEN );
1353 i = rect.right; /* why SetBrushOrg() doesn't? */
1354 PatBlt( hdc, i & 0xfffffffe,
1355 rect.top + POPUP_YSHADE*GetSystemMetrics(SM_CYBORDER),
1356 i%2 + POPUP_XSHADE*GetSystemMetrics(SM_CXBORDER),
1357 rect.bottom - rect.top, 0x00a000c9 );
1359 PatBlt( hdc, rect.left + POPUP_XSHADE*GetSystemMetrics(SM_CXBORDER),
1360 i & 0xfffffffe,rect.right - rect.left,
1361 i%2 + POPUP_YSHADE*GetSystemMetrics(SM_CYBORDER), 0x00a000c9 );
1362 SelectObject( hdc, hPrevPen );
1363 SelectObject( hdc, hPrevBrush );
1364 SetROP2( hdc, ropPrev );
1367 DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT);
1369 /* draw menu items */
1371 menu = MENU_GetMenu( hmenu );
1372 if (menu && menu->nItems)
1377 for (u = menu->nItems, item = menu->items; u > 0; u--, item++)
1378 MENU_DrawMenuItem( hwnd, hmenu, menu->hwndOwner, hdc, item,
1379 menu->Height, FALSE, ODA_DRAWENTIRE );
1384 SelectObject( hdc, hPrevBrush );
1389 /***********************************************************************
1392 * Paint a menu bar. Returns the height of the menu bar.
1393 * called from [windows/nonclient.c]
1395 UINT MENU_DrawMenuBar( HDC hDC, LPRECT lprect, HWND hwnd,
1402 WND *wndPtr = WIN_FindWndPtr( hwnd );
1404 lppop = MENU_GetMenu ((HMENU)wndPtr->wIDmenu );
1405 if (lppop == NULL || lprect == NULL)
1407 retvalue = GetSystemMetrics(SM_CYMENU);
1411 TRACE("(%04x, %p, %p)\n", hDC, lprect, lppop);
1413 hfontOld = SelectObject( hDC, hMenuFont);
1415 if (lppop->Height == 0)
1416 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
1418 lprect->bottom = lprect->top + lppop->Height;
1422 retvalue = lppop->Height;
1426 FillRect(hDC, lprect, GetSysColorBrush(COLOR_MENU) );
1428 if (TWEAK_WineLook == WIN31_LOOK)
1430 SelectObject( hDC, GetSysColorPen(COLOR_WINDOWFRAME) );
1431 MoveTo16( hDC, lprect->left, lprect->bottom );
1432 LineTo( hDC, lprect->right, lprect->bottom );
1436 SelectObject( hDC, GetSysColorPen(COLOR_3DFACE));
1437 MoveTo16( hDC, lprect->left, lprect->bottom );
1438 LineTo( hDC, lprect->right, lprect->bottom );
1441 if (lppop->nItems == 0)
1443 retvalue = GetSystemMetrics(SM_CYMENU);
1447 for (i = 0; i < lppop->nItems; i++)
1449 MENU_DrawMenuItem( hwnd, (HMENU)wndPtr->wIDmenu, GetWindow(hwnd,GW_OWNER),
1450 hDC, &lppop->items[i], lppop->Height, TRUE, ODA_DRAWENTIRE );
1452 retvalue = lppop->Height;
1456 SelectObject (hDC, hfontOld);
1458 WIN_ReleaseWndPtr(wndPtr);
1462 /***********************************************************************
1463 * MENU_PatchResidentPopup
1465 BOOL MENU_PatchResidentPopup( HQUEUE16 checkQueue, WND* checkWnd )
1467 WND *pTPWnd = MENU_GetTopPopupWnd();
1473 TRACE("patching resident popup: %04x %04x [%04x %04x]\n",
1474 checkQueue, checkWnd ? checkWnd->hwndSelf : 0, pTPWnd->hmemTaskQ,
1475 pTPWnd->owner ? pTPWnd->owner->hwndSelf : 0);
1477 switch( checkQueue )
1479 case 0: /* checkWnd is the new popup owner */
1482 pTPWnd->owner = checkWnd;
1483 if( pTPWnd->hmemTaskQ != checkWnd->hmemTaskQ )
1484 hTask = QUEUE_GetQueueTask( checkWnd->hmemTaskQ );
1488 case 0xFFFF: /* checkWnd is destroyed */
1489 if( pTPWnd->owner == checkWnd )
1490 pTPWnd->owner = NULL;
1491 MENU_ReleaseTopPopupWnd();
1494 default: /* checkQueue is exiting */
1495 if( pTPWnd->hmemTaskQ == checkQueue )
1497 hTask = QUEUE_GetQueueTask( pTPWnd->hmemTaskQ );
1498 hTask = TASK_GetNextTask( hTask );
1505 TDB* task = (TDB*)GlobalLock16( hTask );
1508 pTPWnd->hInstance = task->hInstance;
1509 pTPWnd->hmemTaskQ = task->hQueue;
1510 MENU_ReleaseTopPopupWnd();
1513 else WARN("failed to patch resident popup.\n");
1516 MENU_ReleaseTopPopupWnd();
1520 /***********************************************************************
1523 * Display a popup menu.
1525 static BOOL MENU_ShowPopup( HWND hwndOwner, HMENU hmenu, UINT id,
1526 INT x, INT y, INT xanchor, INT yanchor )
1529 WND *wndOwner = NULL;
1531 TRACE("owner=0x%04x hmenu=0x%04x id=0x%04x x=0x%04x y=0x%04x xa=0x%04x ya=0x%04x\n",
1532 hwndOwner, hmenu, id, x, y, xanchor, yanchor);
1534 if (!(menu = MENU_GetMenu( hmenu ))) return FALSE;
1535 if (menu->FocusedItem != NO_SELECTED_ITEM)
1537 menu->items[menu->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1538 menu->FocusedItem = NO_SELECTED_ITEM;
1541 /* store the owner for DrawItem*/
1542 menu->hwndOwner = hwndOwner;
1544 if( (wndOwner = WIN_FindWndPtr( hwndOwner )) )
1548 MENU_PopupMenuCalcSize( menu, hwndOwner );
1550 /* adjust popup menu pos so that it fits within the desktop */
1552 width = menu->Width + GetSystemMetrics(SM_CXBORDER);
1553 height = menu->Height + GetSystemMetrics(SM_CYBORDER);
1555 if( x + width > GetSystemMetrics(SM_CXSCREEN ))
1558 x -= width - xanchor;
1559 if( x + width > GetSystemMetrics(SM_CXSCREEN))
1560 x = GetSystemMetrics(SM_CXSCREEN) - width;
1564 if( y + height > GetSystemMetrics(SM_CYSCREEN ))
1567 y -= height + yanchor;
1568 if( y + height > GetSystemMetrics(SM_CYSCREEN ))
1569 y = GetSystemMetrics(SM_CYSCREEN) - height;
1573 if( TWEAK_WineLook == WIN31_LOOK )
1575 width += POPUP_XSHADE * GetSystemMetrics(SM_CXBORDER); /* add space for shading */
1576 height += POPUP_YSHADE * GetSystemMetrics(SM_CYBORDER);
1579 /* NOTE: In Windows, top menu popup is not owned. */
1580 if (!pTopPopupWnd) /* create top level popup menu window */
1582 assert( uSubPWndLevel == 0 );
1584 pTopPopupWnd = WIN_FindWndPtr(CreateWindowA( POPUPMENU_CLASS_ATOM, NULL,
1585 WS_POPUP, x, y, width, height,
1586 hwndOwner, 0, wndOwner->hInstance,
1590 WIN_ReleaseWndPtr(wndOwner);
1593 menu->hWnd = pTopPopupWnd->hwndSelf;
1594 MENU_ReleaseTopPopupWnd();
1599 /* create a new window for the submenu */
1601 menu->hWnd = CreateWindowA( POPUPMENU_CLASS_ATOM, NULL,
1602 WS_POPUP, x, y, width, height,
1603 hwndOwner, 0, wndOwner->hInstance,
1607 WIN_ReleaseWndPtr(wndOwner);
1611 else /* top level popup menu window already exists */
1613 WND *pTPWnd = MENU_GetTopPopupWnd();
1614 menu->hWnd = pTPWnd->hwndSelf;
1616 MENU_PatchResidentPopup( 0, wndOwner );
1617 SendMessageA( pTPWnd->hwndSelf, MM_SETMENUHANDLE, (WPARAM16)hmenu, 0L);
1619 /* adjust its size */
1621 SetWindowPos( menu->hWnd, 0, x, y, width, height,
1622 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOREDRAW);
1623 MENU_ReleaseTopPopupWnd();
1626 uSubPWndLevel++; /* menu level counter */
1628 /* Display the window */
1630 SetWindowPos( menu->hWnd, HWND_TOP, 0, 0, 0, 0,
1631 SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1632 UpdateWindow( menu->hWnd );
1633 WIN_ReleaseWndPtr(wndOwner);
1640 /***********************************************************************
1643 static void MENU_SelectItem( HWND hwndOwner, HMENU hmenu, UINT wIndex,
1644 BOOL sendMenuSelect, HMENU topmenu )
1649 TRACE("owner=0x%04x menu=0x%04x index=0x%04x select=0x%04x\n", hwndOwner, hmenu, wIndex, sendMenuSelect);
1651 lppop = MENU_GetMenu( hmenu );
1652 if ((!lppop) || (!lppop->nItems)) return;
1654 if (lppop->FocusedItem == wIndex) return;
1655 if (lppop->wFlags & MF_POPUP) hdc = GetDC( lppop->hWnd );
1656 else hdc = GetDCEx( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1658 SelectObject( hdc, hMenuFont);
1660 /* Clear previous highlighted item */
1661 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1663 lppop->items[lppop->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1664 MENU_DrawMenuItem(lppop->hWnd, hmenu, hwndOwner, hdc,&lppop->items[lppop->FocusedItem],
1665 lppop->Height, !(lppop->wFlags & MF_POPUP),
1669 /* Highlight new item (if any) */
1670 lppop->FocusedItem = wIndex;
1671 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1673 if(!(lppop->items[wIndex].fType & MF_SEPARATOR)) {
1674 lppop->items[wIndex].fState |= MF_HILITE;
1675 MENU_DrawMenuItem( lppop->hWnd, hmenu, hwndOwner, hdc,
1676 &lppop->items[wIndex], lppop->Height,
1677 !(lppop->wFlags & MF_POPUP), ODA_SELECT );
1681 MENUITEM *ip = &lppop->items[lppop->FocusedItem];
1682 SendMessageA( hwndOwner, WM_MENUSELECT,
1683 MAKELONG(ip->fType & MF_POPUP ? wIndex: ip->wID,
1684 ip->fType | ip->fState | MF_MOUSESELECT |
1685 (lppop->wFlags & MF_SYSMENU)), hmenu);
1688 else if (sendMenuSelect) {
1691 if((pos=MENU_FindSubMenu(&topmenu, hmenu))!=NO_SELECTED_ITEM){
1692 POPUPMENU *ptm = (POPUPMENU *) USER_HEAP_LIN_ADDR( topmenu );
1693 MENUITEM *ip = &ptm->items[pos];
1694 SendMessageA( hwndOwner, WM_MENUSELECT, MAKELONG(pos,
1695 ip->fType | ip->fState | MF_MOUSESELECT |
1696 (ptm->wFlags & MF_SYSMENU)), topmenu);
1700 ReleaseDC( lppop->hWnd, hdc );
1704 /***********************************************************************
1705 * MENU_MoveSelection
1707 * Moves currently selected item according to the offset parameter.
1708 * If there is no selection then it should select the last item if
1709 * offset is ITEM_PREV or the first item if offset is ITEM_NEXT.
1711 static void MENU_MoveSelection( HWND hwndOwner, HMENU hmenu, INT offset )
1716 TRACE("hwnd=0x%04x hmenu=0x%04x off=0x%04x\n", hwndOwner, hmenu, offset);
1718 menu = MENU_GetMenu( hmenu );
1719 if ((!menu) || (!menu->items)) return;
1721 if ( menu->FocusedItem != NO_SELECTED_ITEM )
1723 if( menu->nItems == 1 ) return; else
1724 for (i = menu->FocusedItem + offset ; i >= 0 && i < menu->nItems
1726 if (!(menu->items[i].fType & MF_SEPARATOR))
1728 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
1733 for ( i = (offset > 0) ? 0 : menu->nItems - 1;
1734 i >= 0 && i < menu->nItems ; i += offset)
1735 if (!(menu->items[i].fType & MF_SEPARATOR))
1737 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
1743 /**********************************************************************
1746 * Set an item flags, id and text ptr. Called by InsertMenu() and
1749 static BOOL MENU_SetItemData( MENUITEM *item, UINT flags, UINT id,
1752 LPSTR prevText = IS_STRING_ITEM(item->fType) ? item->text : NULL;
1754 debug_print_menuitem("MENU_SetItemData from: ", item, "");
1756 if (IS_STRING_ITEM(flags))
1760 flags |= MF_SEPARATOR;
1766 /* Item beginning with a backspace is a help item */
1772 if (!(text = HEAP_strdupA( SystemHeap, 0, str ))) return FALSE;
1776 else if (IS_BITMAP_ITEM(flags))
1777 item->text = (LPSTR)(HBITMAP)LOWORD(str);
1778 else item->text = NULL;
1780 if (flags & MF_OWNERDRAW)
1781 item->dwItemData = (DWORD)str;
1783 item->dwItemData = 0;
1785 if ((item->fType & MF_POPUP) && (flags & MF_POPUP) && (item->hSubMenu != id) )
1786 DestroyMenu( item->hSubMenu ); /* ModifyMenu() spec */
1788 if (flags & MF_POPUP)
1790 POPUPMENU *menu = MENU_GetMenu((UINT16)id);
1791 if (menu) menu->wFlags |= MF_POPUP;
1803 if (flags & MF_POPUP)
1804 item->hSubMenu = id;
1806 if ((item->fType & MF_POPUP) && !(flags & MF_POPUP) )
1807 flags |= MF_POPUP; /* keep popup */
1809 item->fType = flags & TYPE_MASK;
1810 item->fState = (flags & STATE_MASK) &
1811 ~(MF_HILITE | MF_MOUSESELECT | MF_BYPOSITION);
1814 /* Don't call SetRectEmpty here! */
1817 if (prevText) HeapFree( SystemHeap, 0, prevText );
1819 debug_print_menuitem("MENU_SetItemData to : ", item, "");
1824 /**********************************************************************
1827 * Insert a new item into a menu.
1829 static MENUITEM *MENU_InsertItem( HMENU hMenu, UINT pos, UINT flags )
1834 if (!(menu = MENU_GetMenu(hMenu)))
1837 /* Find where to insert new item */
1839 if ((pos==(UINT)-1) || ((flags & MF_BYPOSITION) && (pos == menu->nItems))) {
1840 /* Special case: append to menu */
1841 /* Some programs specify the menu length to do that */
1844 if (!MENU_FindItem( &hMenu, &pos, flags ))
1846 FIXME("item %x not found\n", pos );
1849 if (!(menu = MENU_GetMenu(hMenu)))
1853 /* Create new items array */
1855 newItems = HeapAlloc( SystemHeap, 0, sizeof(MENUITEM) * (menu->nItems+1) );
1858 WARN("allocation failed\n" );
1861 if (menu->nItems > 0)
1863 /* Copy the old array into the new */
1864 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
1865 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
1866 (menu->nItems-pos)*sizeof(MENUITEM) );
1867 HeapFree( SystemHeap, 0, menu->items );
1869 menu->items = newItems;
1871 memset( &newItems[pos], 0, sizeof(*newItems) );
1872 menu->Height = 0; /* force size recalculate */
1873 return &newItems[pos];
1877 /**********************************************************************
1878 * MENU_ParseResource
1880 * Parse a standard menu resource and add items to the menu.
1881 * Return a pointer to the end of the resource.
1883 static LPCSTR MENU_ParseResource( LPCSTR res, HMENU hMenu, BOOL unicode )
1890 flags = GET_WORD(res);
1891 res += sizeof(WORD);
1892 if (!(flags & MF_POPUP))
1895 res += sizeof(WORD);
1897 if (!IS_STRING_ITEM(flags))
1898 ERR("not a string item %04x\n", flags );
1900 if (!unicode) res += strlen(str) + 1;
1901 else res += (lstrlenW((LPCWSTR)str) + 1) * sizeof(WCHAR);
1902 if (flags & MF_POPUP)
1904 HMENU hSubMenu = CreatePopupMenu();
1905 if (!hSubMenu) return NULL;
1906 if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
1908 if (!unicode) AppendMenuA( hMenu, flags, (UINT)hSubMenu, str );
1909 else AppendMenuW( hMenu, flags, (UINT)hSubMenu, (LPCWSTR)str );
1911 else /* Not a popup */
1913 if (!unicode) AppendMenuA( hMenu, flags, id, *str ? str : NULL );
1914 else AppendMenuW( hMenu, flags, id,
1915 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
1917 } while (!(flags & MF_END));
1922 /**********************************************************************
1923 * MENUEX_ParseResource
1925 * Parse an extended menu resource and add items to the menu.
1926 * Return a pointer to the end of the resource.
1928 static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU hMenu)
1934 mii.cbSize = sizeof(mii);
1935 mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
1936 mii.fType = GET_DWORD(res);
1937 res += sizeof(DWORD);
1938 mii.fState = GET_DWORD(res);
1939 res += sizeof(DWORD);
1940 mii.wID = GET_DWORD(res);
1941 res += sizeof(DWORD);
1942 resinfo = GET_WORD(res); /* FIXME: for 16-bit apps this is a byte. */
1943 res += sizeof(WORD);
1944 /* Align the text on a word boundary. */
1945 res += (~((int)res - 1)) & 1;
1946 mii.dwTypeData = (LPWSTR) res;
1947 res += (1 + lstrlenW(mii.dwTypeData)) * sizeof(WCHAR);
1948 /* Align the following fields on a dword boundary. */
1949 res += (~((int)res - 1)) & 3;
1951 /* FIXME: This is inefficient and cannot be optimised away by gcc. */
1953 LPSTR newstr = HEAP_strdupWtoA(GetProcessHeap(),
1955 TRACE("Menu item: [%08x,%08x,%04x,%04x,%s]\n",
1956 mii.fType, mii.fState, mii.wID, resinfo, newstr);
1957 HeapFree( GetProcessHeap(), 0, newstr );
1960 if (resinfo & 1) { /* Pop-up? */
1961 /* DWORD helpid = GET_DWORD(res); FIXME: use this. */
1962 res += sizeof(DWORD);
1963 mii.hSubMenu = CreatePopupMenu();
1966 if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
1967 DestroyMenu(mii.hSubMenu);
1970 mii.fMask |= MIIM_SUBMENU;
1971 mii.fType |= MF_POPUP;
1973 InsertMenuItemW(hMenu, -1, MF_BYPOSITION, &mii);
1974 } while (!(resinfo & MF_END));
1979 /***********************************************************************
1982 * Return the handle of the selected sub-popup menu (if any).
1984 static HMENU MENU_GetSubPopup( HMENU hmenu )
1989 menu = MENU_GetMenu( hmenu );
1991 if ((!menu) || (menu->FocusedItem == NO_SELECTED_ITEM)) return 0;
1993 item = &menu->items[menu->FocusedItem];
1994 if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
1995 return item->hSubMenu;
2000 /***********************************************************************
2001 * MENU_HideSubPopups
2003 * Hide the sub-popup menus of this menu.
2005 static void MENU_HideSubPopups( HWND hwndOwner, HMENU hmenu,
2006 BOOL sendMenuSelect )
2008 POPUPMENU *menu = MENU_GetMenu( hmenu );
2010 TRACE("owner=0x%04x hmenu=0x%04x 0x%04x\n", hwndOwner, hmenu, sendMenuSelect);
2012 if (menu && uSubPWndLevel)
2018 if (menu->FocusedItem != NO_SELECTED_ITEM)
2020 item = &menu->items[menu->FocusedItem];
2021 if (!(item->fType & MF_POPUP) ||
2022 !(item->fState & MF_MOUSESELECT)) return;
2023 item->fState &= ~MF_MOUSESELECT;
2024 hsubmenu = item->hSubMenu;
2027 submenu = MENU_GetMenu( hsubmenu );
2028 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE );
2029 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect, 0 );
2031 if (submenu->hWnd == MENU_GetTopPopupWnd()->hwndSelf )
2033 ShowWindow( submenu->hWnd, SW_HIDE );
2038 DestroyWindow( submenu->hWnd );
2041 MENU_ReleaseTopPopupWnd();
2046 /***********************************************************************
2049 * Display the sub-menu of the selected item of this menu.
2050 * Return the handle of the submenu, or hmenu if no submenu to display.
2052 static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu,
2053 BOOL selectFirst, UINT wFlags )
2061 TRACE("owner=0x%04x hmenu=0x%04x 0x%04x\n", hwndOwner, hmenu, selectFirst);
2063 if (!(menu = MENU_GetMenu( hmenu ))) return hmenu;
2065 if (!(wndPtr = WIN_FindWndPtr( menu->hWnd )) ||
2066 (menu->FocusedItem == NO_SELECTED_ITEM))
2068 WIN_ReleaseWndPtr(wndPtr);
2072 item = &menu->items[menu->FocusedItem];
2073 if (!(item->fType & MF_POPUP) ||
2074 (item->fState & (MF_GRAYED | MF_DISABLED)))
2076 WIN_ReleaseWndPtr(wndPtr);
2080 /* message must be send before using item,
2081 because nearly everything may by changed by the application ! */
2083 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
2084 if (!(wFlags & TPM_NONOTIFY))
2085 SendMessageA( hwndOwner, WM_INITMENUPOPUP, item->hSubMenu,
2086 MAKELONG( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
2088 item = &menu->items[menu->FocusedItem];
2091 /* correct item if modified as a reaction to WM_INITMENUPOPUP-message */
2092 if (!(item->fState & MF_HILITE))
2094 if (menu->wFlags & MF_POPUP) hdc = GetDC( menu->hWnd );
2095 else hdc = GetDCEx( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
2097 SelectObject( hdc, hMenuFont);
2099 item->fState |= MF_HILITE;
2100 MENU_DrawMenuItem( menu->hWnd, hmenu, hwndOwner, hdc, item, menu->Height, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE );
2101 ReleaseDC( menu->hWnd, hdc );
2103 if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
2106 item->fState |= MF_MOUSESELECT;
2108 if (IS_SYSTEM_MENU(menu))
2110 MENU_InitSysMenuPopup(item->hSubMenu, wndPtr->dwStyle, GetClassLongA(wndPtr->hwndSelf, GCL_STYLE));
2112 NC_GetSysPopupPos( wndPtr, &rect );
2113 rect.top = rect.bottom;
2114 rect.right = GetSystemMetrics(SM_CXSIZE);
2115 rect.bottom = GetSystemMetrics(SM_CYSIZE);
2119 if (menu->wFlags & MF_POPUP)
2121 rect.left = wndPtr->rectWindow.left + item->rect.right-arrow_bitmap_width;
2122 rect.top = wndPtr->rectWindow.top + item->rect.top;
2123 rect.right = item->rect.left - item->rect.right + 2*arrow_bitmap_width;
2124 rect.bottom = item->rect.top - item->rect.bottom;
2128 rect.left = wndPtr->rectWindow.left + item->rect.left;
2129 rect.top = wndPtr->rectWindow.top + item->rect.bottom;
2130 rect.right = item->rect.right - item->rect.left;
2131 rect.bottom = item->rect.bottom - item->rect.top;
2135 MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem,
2136 rect.left, rect.top, rect.right, rect.bottom );
2138 MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
2139 WIN_ReleaseWndPtr(wndPtr);
2140 return item->hSubMenu;
2145 /**********************************************************************
2148 BOOL MENU_IsMenuActive(void)
2150 return pTopPopupWnd && (pTopPopupWnd->dwStyle & WS_VISIBLE);
2153 /***********************************************************************
2156 * Walks menu chain trying to find a menu pt maps to.
2158 static HMENU MENU_PtMenu( HMENU hMenu, POINT16 pt )
2160 POPUPMENU *menu = MENU_GetMenu( hMenu );
2161 register UINT ht = menu->FocusedItem;
2163 /* try subpopup first (if any) */
2164 ht = (ht != NO_SELECTED_ITEM &&
2165 (menu->items[ht].fType & MF_POPUP) &&
2166 (menu->items[ht].fState & MF_MOUSESELECT))
2167 ? (UINT) MENU_PtMenu(menu->items[ht].hSubMenu, pt) : 0;
2169 if( !ht ) /* check the current window (avoiding WM_HITTEST) */
2171 ht = (UINT)NC_HandleNCHitTest( menu->hWnd, pt );
2172 if( menu->wFlags & MF_POPUP )
2173 ht = (ht != (UINT)HTNOWHERE &&
2174 ht != (UINT)HTERROR) ? (UINT)hMenu : 0;
2177 WND* wndPtr = WIN_FindWndPtr(menu->hWnd);
2179 ht = ( ht == HTSYSMENU ) ? (UINT)(wndPtr->hSysMenu)
2180 : ( ht == HTMENU ) ? (UINT)(wndPtr->wIDmenu) : 0;
2181 WIN_ReleaseWndPtr(wndPtr);
2187 /***********************************************************************
2188 * MENU_ExecFocusedItem
2190 * Execute a menu item (for instance when user pressed Enter).
2191 * Return the wID of the executed item. Otherwise, -1 indicating
2192 * that no menu item wase executed;
2193 * Have to receive the flags for the TrackPopupMenu options to avoid
2194 * sending unwanted message.
2197 static INT MENU_ExecFocusedItem( MTRACKER* pmt, HMENU hMenu, UINT wFlags )
2200 POPUPMENU *menu = MENU_GetMenu( hMenu );
2202 TRACE("%p hmenu=0x%04x\n", pmt, hMenu);
2204 if (!menu || !menu->nItems ||
2205 (menu->FocusedItem == NO_SELECTED_ITEM)) return -1;
2207 item = &menu->items[menu->FocusedItem];
2209 TRACE("%08x %08x %08x\n",
2210 hMenu, item->wID, item->hSubMenu);
2212 if (!(item->fType & MF_POPUP))
2214 if (!(item->fState & (MF_GRAYED | MF_DISABLED)))
2216 /* If TPM_RETURNCMD is set you return the id, but
2217 do not send a message to the owner */
2218 if(!(wFlags & TPM_RETURNCMD))
2220 if( menu->wFlags & MF_SYSMENU )
2221 PostMessageA( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
2222 MAKELPARAM((INT16)pmt->pt.x, (INT16)pmt->pt.y) );
2224 PostMessageA( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
2230 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hMenu, TRUE, wFlags);
2235 /***********************************************************************
2236 * MENU_SwitchTracking
2238 * Helper function for menu navigation routines.
2240 static void MENU_SwitchTracking( MTRACKER* pmt, HMENU hPtMenu, UINT id )
2242 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2243 POPUPMENU *topmenu = MENU_GetMenu( pmt->hTopMenu );
2245 TRACE("%p hmenu=0x%04x 0x%04x\n", pmt, hPtMenu, id);
2247 if( pmt->hTopMenu != hPtMenu &&
2248 !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
2250 /* both are top level menus (system and menu-bar) */
2251 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2252 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2253 pmt->hTopMenu = hPtMenu;
2255 else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE );
2256 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE, 0 );
2260 /***********************************************************************
2263 * Return TRUE if we can go on with menu tracking.
2265 static BOOL MENU_ButtonDown( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2267 TRACE("%p hmenu=0x%04x\n", pmt, hPtMenu);
2272 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2275 if( IS_SYSTEM_MENU(ptmenu) )
2276 item = ptmenu->items;
2278 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2282 if( ptmenu->FocusedItem != id )
2283 MENU_SwitchTracking( pmt, hPtMenu, id );
2285 /* If the popup menu is not already "popped" */
2286 if(!(item->fState & MF_MOUSESELECT ))
2288 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE, wFlags );
2290 /* In win31, a newly popped menu always remain opened for the next buttonup */
2291 if(TWEAK_WineLook == WIN31_LOOK)
2292 ptmenu->bTimeToHide = FALSE;
2297 /* Else the click was on the menu bar, finish the tracking */
2302 /***********************************************************************
2305 * Return the value of MENU_ExecFocusedItem if
2306 * the selected item was not a popup. Else open the popup.
2307 * A -1 return value indicates that we go on with menu tracking.
2310 static INT MENU_ButtonUp( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags)
2312 TRACE("%p hmenu=0x%04x\n", pmt, hPtMenu);
2317 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2320 if( IS_SYSTEM_MENU(ptmenu) )
2321 item = ptmenu->items;
2323 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2325 if( item && (ptmenu->FocusedItem == id ))
2327 if( !(item->fType & MF_POPUP) )
2328 return MENU_ExecFocusedItem( pmt, hPtMenu, wFlags);
2330 /* If we are dealing with the top-level menu and that this */
2331 /* is a click on an already "poppped" item */
2332 /* Stop the menu tracking and close the opened submenus */
2333 if((pmt->hTopMenu == hPtMenu) && (ptmenu->bTimeToHide == TRUE))
2336 ptmenu->bTimeToHide = TRUE;
2342 /***********************************************************************
2345 * Return TRUE if we can go on with menu tracking.
2347 static BOOL MENU_MouseMove( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2349 UINT id = NO_SELECTED_ITEM;
2350 POPUPMENU *ptmenu = NULL;
2354 ptmenu = MENU_GetMenu( hPtMenu );
2355 if( IS_SYSTEM_MENU(ptmenu) )
2358 MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2361 if( id == NO_SELECTED_ITEM )
2363 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2364 NO_SELECTED_ITEM, TRUE, pmt->hTopMenu);
2367 else if( ptmenu->FocusedItem != id )
2369 MENU_SwitchTracking( pmt, hPtMenu, id );
2370 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hPtMenu, FALSE, wFlags);
2376 /***********************************************************************
2379 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2381 static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT vk )
2383 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2385 if( (vk == VK_LEFT && menu->FocusedItem == 0 ) ||
2386 (vk == VK_RIGHT && menu->FocusedItem == menu->nItems - 1))
2392 LRESULT l = SendMessageA( pmt->hOwnerWnd, WM_NEXTMENU, vk,
2393 (IS_SYSTEM_MENU(menu)) ? GetSubMenu16(pmt->hTopMenu,0) : pmt->hTopMenu );
2395 TRACE("%04x [%04x] -> %04x [%04x]\n",
2396 (UINT16)pmt->hCurrentMenu, (UINT16)pmt->hOwnerWnd, LOWORD(l), HIWORD(l) );
2400 wndPtr = WIN_FindWndPtr(pmt->hOwnerWnd);
2402 hNewWnd = pmt->hOwnerWnd;
2403 if( IS_SYSTEM_MENU(menu) )
2405 /* switch to the menu bar */
2407 if( wndPtr->dwStyle & WS_CHILD || !wndPtr->wIDmenu )
2409 WIN_ReleaseWndPtr(wndPtr);
2413 hNewMenu = wndPtr->wIDmenu;
2416 menu = MENU_GetMenu( hNewMenu );
2417 id = menu->nItems - 1;
2420 else if( wndPtr->dwStyle & WS_SYSMENU )
2422 /* switch to the system menu */
2423 hNewMenu = wndPtr->hSysMenu;
2427 WIN_ReleaseWndPtr(wndPtr);
2430 WIN_ReleaseWndPtr(wndPtr);
2432 else /* application returned a new menu to switch to */
2434 hNewMenu = LOWORD(l); hNewWnd = HIWORD(l);
2436 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2438 wndPtr = WIN_FindWndPtr(hNewWnd);
2440 if( wndPtr->dwStyle & WS_SYSMENU &&
2441 GetSubMenu16(wndPtr->hSysMenu, 0) == hNewMenu )
2443 /* get the real system menu */
2444 hNewMenu = wndPtr->hSysMenu;
2446 else if( wndPtr->dwStyle & WS_CHILD || wndPtr->wIDmenu != hNewMenu )
2448 /* FIXME: Not sure what to do here, perhaps,
2449 * try to track hNewMenu as a popup? */
2451 TRACE(" -- got confused.\n");
2452 WIN_ReleaseWndPtr(wndPtr);
2455 WIN_ReleaseWndPtr(wndPtr);
2460 if( hNewMenu != pmt->hTopMenu )
2462 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM,
2464 if( pmt->hCurrentMenu != pmt->hTopMenu )
2465 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2468 if( hNewWnd != pmt->hOwnerWnd )
2471 pmt->hOwnerWnd = hNewWnd;
2472 EVENT_Capture( pmt->hOwnerWnd, HTMENU );
2475 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2476 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE, 0 );
2483 /***********************************************************************
2486 * The idea is not to show the popup if the next input message is
2487 * going to hide it anyway.
2489 static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT16 uMsg )
2493 msg.hwnd = pmt->hOwnerWnd;
2495 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2496 pmt->trackFlags |= TF_SKIPREMOVE;
2501 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2502 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2504 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2505 PeekMessageA( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2506 if( msg.message == WM_KEYDOWN &&
2507 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2509 pmt->trackFlags |= TF_SUSPENDPOPUP;
2516 /* failures go through this */
2517 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2521 /***********************************************************************
2524 * Handle a VK_LEFT key event in a menu.
2526 static void MENU_KeyLeft( MTRACKER* pmt, UINT wFlags )
2529 HMENU hmenutmp, hmenuprev;
2532 hmenuprev = hmenutmp = pmt->hTopMenu;
2533 menu = MENU_GetMenu( hmenutmp );
2535 /* Try to move 1 column left (if possible) */
2536 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2537 NO_SELECTED_ITEM ) {
2539 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2544 /* close topmost popup */
2545 while (hmenutmp != pmt->hCurrentMenu)
2547 hmenuprev = hmenutmp;
2548 hmenutmp = MENU_GetSubPopup( hmenuprev );
2551 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2552 pmt->hCurrentMenu = hmenuprev;
2554 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2556 /* move menu bar selection if no more popups are left */
2558 if( !MENU_DoNextMenu( pmt, VK_LEFT) )
2559 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2561 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2563 /* A sublevel menu was displayed - display the next one
2564 * unless there is another displacement coming up */
2566 if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2567 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2568 pmt->hTopMenu, TRUE, wFlags);
2574 /***********************************************************************
2577 * Handle a VK_RIGHT key event in a menu.
2579 static void MENU_KeyRight( MTRACKER* pmt, UINT wFlags )
2582 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2585 TRACE("MENU_KeyRight called, cur %x (%s), top %x (%s).\n",
2587 (MENU_GetMenu(pmt->hCurrentMenu))->
2589 pmt->hTopMenu, menu->items[0].text );
2591 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2593 /* If already displaying a popup, try to display sub-popup */
2595 hmenutmp = pmt->hCurrentMenu;
2596 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hmenutmp, TRUE, wFlags);
2598 /* if subpopup was displayed then we are done */
2599 if (hmenutmp != pmt->hCurrentMenu) return;
2602 /* Check to see if there's another column */
2603 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
2604 NO_SELECTED_ITEM ) {
2605 TRACE("Going to %d.\n", nextcol );
2606 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2611 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
2613 if( pmt->hCurrentMenu != pmt->hTopMenu )
2615 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2616 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2617 } else hmenutmp = 0;
2619 /* try to move to the next item */
2620 if( !MENU_DoNextMenu( pmt, VK_RIGHT) )
2621 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
2623 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2624 if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
2625 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2626 pmt->hTopMenu, TRUE, wFlags);
2630 /***********************************************************************
2633 * Menu tracking code.
2635 static INT MENU_TrackMenu( HMENU hmenu, UINT wFlags, INT x, INT y,
2636 HWND hwnd, const RECT *lprect )
2641 INT executedMenuId = -1;
2643 BOOL enterIdleSent = FALSE;
2646 mt.hCurrentMenu = hmenu;
2647 mt.hTopMenu = hmenu;
2648 mt.hOwnerWnd = hwnd;
2652 TRACE("hmenu=0x%04x flags=0x%08x (%d,%d) hwnd=0x%04x (%d,%d)-(%d,%d)\n",
2653 hmenu, wFlags, x, y, hwnd, (lprect) ? lprect->left : 0, (lprect) ? lprect->top : 0,
2654 (lprect) ? lprect->right : 0, (lprect) ? lprect->bottom : 0);
2657 if (!(menu = MENU_GetMenu( hmenu ))) return FALSE;
2659 if (wFlags & TPM_BUTTONDOWN)
2661 /* Get the result in order to start the tracking or not */
2662 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2663 fEndMenu = !fRemove;
2666 EVENT_Capture( mt.hOwnerWnd, HTMENU );
2670 menu = MENU_GetMenu( mt.hCurrentMenu );
2671 msg.hwnd = (wFlags & TPM_ENTERIDLEEX && menu->wFlags & MF_POPUP) ? menu->hWnd : 0;
2673 /* we have to keep the message in the queue until it's
2674 * clear that menu loop is not over yet. */
2676 if (!MSG_InternalGetMessage( QMSG_WIN32A, &msg, msg.hwnd, mt.hOwnerWnd,
2677 MSGF_MENU, PM_NOREMOVE, !enterIdleSent, &enterIdleSent )) break;
2679 TranslateMessage( &msg );
2682 if ( (msg.hwnd==menu->hWnd) || (msg.message!=WM_TIMER) )
2683 enterIdleSent=FALSE;
2686 if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
2688 /* Find a menu for this mouse event */
2690 CONV_POINT32TO16( &msg.pt, &pt16 );
2691 hmenu = MENU_PtMenu( mt.hTopMenu, pt16 );
2695 /* no WM_NC... messages in captured state */
2697 case WM_RBUTTONDBLCLK:
2698 case WM_RBUTTONDOWN:
2699 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2701 case WM_LBUTTONDBLCLK:
2702 case WM_LBUTTONDOWN:
2703 /* If the message belongs to the menu, removes it from the queue */
2704 /* Else, end menu tracking */
2705 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2706 fEndMenu = !fRemove;
2710 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2713 /* Check if a menu was selected by the mouse */
2716 executedMenuId = MENU_ButtonUp( &mt, hmenu, wFlags);
2718 /* End the loop if executedMenuId is an item ID */
2719 /* or if the job was done (executedMenuId = 0). */
2720 fEndMenu = fRemove = (executedMenuId != -1);
2722 /* No menu was selected by the mouse */
2723 /* if the function was called by TrackPopupMenu, continue
2724 with the menu tracking. If not, stop it */
2726 fEndMenu = ((wFlags & TPM_POPUPMENU) ? FALSE : TRUE);
2731 /* In win95 winelook, the selected menu item must be changed every time the
2732 mouse moves. In Win31 winelook, the mouse button has to be held down */
2734 if ( (TWEAK_WineLook > WIN31_LOOK) ||
2735 ( (msg.wParam & MK_LBUTTON) ||
2736 ((wFlags & TPM_RIGHTBUTTON) && (msg.wParam & MK_RBUTTON))) )
2738 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
2740 } /* switch(msg.message) - mouse */
2742 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
2744 fRemove = TRUE; /* Keyboard messages are always removed */
2752 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
2753 NO_SELECTED_ITEM, FALSE, 0 );
2756 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
2757 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
2760 case VK_DOWN: /* If on menu bar, pull-down the menu */
2762 menu = MENU_GetMenu( mt.hCurrentMenu );
2763 if (!(menu->wFlags & MF_POPUP))
2764 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, mt.hTopMenu, TRUE, wFlags);
2765 else /* otherwise try to move selection */
2766 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, ITEM_NEXT );
2770 MENU_KeyLeft( &mt, wFlags );
2774 MENU_KeyRight( &mt, wFlags );
2784 hi.cbSize = sizeof(HELPINFO);
2785 hi.iContextType = HELPINFO_MENUITEM;
2786 if (menu->FocusedItem == NO_SELECTED_ITEM)
2789 hi.iCtrlId = menu->items[menu->FocusedItem].wID;
2790 hi.hItemHandle = hmenu;
2791 hi.dwContextId = menu->dwContextHelpID;
2792 hi.MousePos = msg.pt;
2793 SendMessageA(hwnd, WM_HELP, 0, (LPARAM)&hi);
2800 break; /* WM_KEYDOWN */
2810 break; /* WM_SYSKEYDOWN */
2816 if (msg.wParam == '\r' || msg.wParam == ' ')
2818 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
2819 fEndMenu = (executedMenuId != -1);
2824 /* Hack to avoid control chars. */
2825 /* We will find a better way real soon... */
2826 if ((msg.wParam <= 32) || (msg.wParam >= 127)) break;
2828 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
2829 LOWORD(msg.wParam), FALSE );
2830 if (pos == (UINT)-2) fEndMenu = TRUE;
2831 else if (pos == (UINT)-1) MessageBeep(0);
2834 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos,
2836 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
2837 fEndMenu = (executedMenuId != -1);
2841 } /* switch(msg.message) - kbd */
2845 DispatchMessageA( &msg );
2848 if (!fEndMenu) fRemove = TRUE;
2850 /* finally remove message from the queue */
2852 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
2853 PeekMessageA( &msg, 0, msg.message, msg.message, PM_REMOVE );
2854 else mt.trackFlags &= ~TF_SKIPREMOVE;
2859 menu = MENU_GetMenu( mt.hTopMenu );
2861 if( IsWindow( mt.hOwnerWnd ) )
2863 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE );
2865 if (menu && menu->wFlags & MF_POPUP)
2867 ShowWindow( menu->hWnd, SW_HIDE );
2870 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2871 SendMessageA( mt.hOwnerWnd, WM_MENUSELECT, MAKELONG(0,0xffff), 0 );
2874 /* Reset the variable for hiding menu */
2875 menu->bTimeToHide = FALSE;
2877 /* The return value is only used by TrackPopupMenu */
2878 return ((executedMenuId != -1) ? executedMenuId : 0);
2881 /***********************************************************************
2884 static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu, BOOL bPopup, UINT wFlags)
2886 TRACE("hwnd=0x%04x hmenu=0x%04x\n", hWnd, hMenu);
2890 /* Send WM_ENTERMENULOOP and WM_INITMENU message only if TPM_NONOTIFY flag is not specified */
2891 if (!(wFlags & TPM_NONOTIFY))
2892 SendMessageA( hWnd, WM_ENTERMENULOOP, bPopup, 0 );
2894 SendMessageA( hWnd, WM_SETCURSOR, hWnd, HTCAPTION );
2896 if (!(wFlags & TPM_NONOTIFY))
2897 SendMessageA( hWnd, WM_INITMENU, hMenu, 0 );
2901 /***********************************************************************
2904 static BOOL MENU_ExitTracking(HWND hWnd)
2906 TRACE("hwnd=0x%04x\n", hWnd);
2908 SendMessageA( hWnd, WM_EXITMENULOOP, 0, 0 );
2913 /***********************************************************************
2914 * MENU_TrackMouseMenuBar
2916 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
2918 void MENU_TrackMouseMenuBar( WND* wndPtr, INT ht, POINT pt )
2920 HWND hWnd = wndPtr->hwndSelf;
2921 HMENU hMenu = (ht == HTSYSMENU) ? wndPtr->hSysMenu : wndPtr->wIDmenu;
2922 UINT wFlags = TPM_ENTERIDLEEX | TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
2924 TRACE("pwnd=%p ht=0x%04x (%ld,%ld)\n", wndPtr, ht, pt.x, pt.y);
2928 MENU_InitTracking( hWnd, hMenu, FALSE, wFlags );
2929 MENU_TrackMenu( hMenu, wFlags, pt.x, pt.y, hWnd, NULL );
2930 MENU_ExitTracking(hWnd);
2935 /***********************************************************************
2936 * MENU_TrackKbdMenuBar
2938 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
2940 void MENU_TrackKbdMenuBar( WND* wndPtr, UINT wParam, INT vkey)
2942 UINT uItem = NO_SELECTED_ITEM;
2944 UINT wFlags = TPM_ENTERIDLEEX | TPM_LEFTALIGN | TPM_LEFTBUTTON;
2946 /* find window that has a menu */
2948 while( wndPtr->dwStyle & WS_CHILD)
2949 if( !(wndPtr = wndPtr->parent) ) return;
2951 /* check if we have to track a system menu */
2953 if( (wndPtr->dwStyle & (WS_CHILD | WS_MINIMIZE)) ||
2954 !wndPtr->wIDmenu || vkey == VK_SPACE )
2956 if( !(wndPtr->dwStyle & WS_SYSMENU) ) return;
2957 hTrackMenu = wndPtr->hSysMenu;
2959 wParam |= HTSYSMENU; /* prevent item lookup */
2962 hTrackMenu = wndPtr->wIDmenu;
2964 if (IsMenu( hTrackMenu ))
2966 MENU_InitTracking( wndPtr->hwndSelf, hTrackMenu, FALSE, wFlags );
2968 if( vkey && vkey != VK_SPACE )
2970 uItem = MENU_FindItemByKey( wndPtr->hwndSelf, hTrackMenu,
2971 vkey, (wParam & HTSYSMENU) );
2972 if( uItem >= (UINT)(-2) )
2974 if( uItem == (UINT)(-1) ) MessageBeep(0);
2981 MENU_SelectItem( wndPtr->hwndSelf, hTrackMenu, uItem, TRUE, 0 );
2983 if( uItem == NO_SELECTED_ITEM )
2984 MENU_MoveSelection( wndPtr->hwndSelf, hTrackMenu, ITEM_NEXT );
2986 PostMessageA( wndPtr->hwndSelf, WM_KEYDOWN, VK_DOWN, 0L );
2988 MENU_TrackMenu( hTrackMenu, wFlags, 0, 0, wndPtr->hwndSelf, NULL );
2991 MENU_ExitTracking (wndPtr->hwndSelf);
2996 /**********************************************************************
2997 * TrackPopupMenu16 (USER.416)
2999 BOOL16 WINAPI TrackPopupMenu16( HMENU16 hMenu, UINT16 wFlags, INT16 x, INT16 y,
3000 INT16 nReserved, HWND16 hWnd, const RECT16 *lpRect )
3004 CONV_RECT16TO32( lpRect, &r );
3005 return TrackPopupMenu( hMenu, wFlags, x, y, nReserved, hWnd,
3006 lpRect ? &r : NULL );
3010 /**********************************************************************
3011 * TrackPopupMenu (USER32.549)
3013 * Like the win32 API, the function return the command ID only if the
3014 * flag TPM_RETURNCMD is on.
3017 BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
3018 INT nReserved, HWND hWnd, const RECT *lpRect )
3022 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3024 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3025 if (!(wFlags & TPM_NONOTIFY))
3026 SendMessageA( hWnd, WM_INITMENUPOPUP, hMenu, 0);
3028 if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 ))
3029 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd, lpRect );
3030 MENU_ExitTracking(hWnd);
3032 if( (!(wFlags & TPM_RETURNCMD)) && (ret != FALSE) )
3038 /**********************************************************************
3039 * TrackPopupMenuEx (USER32.550)
3041 BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3042 HWND hWnd, LPTPMPARAMS lpTpm )
3044 FIXME("not fully implemented\n" );
3045 return TrackPopupMenu( hMenu, wFlags, x, y, 0, hWnd,
3046 lpTpm ? &lpTpm->rcExclude : NULL );
3049 /***********************************************************************
3052 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3054 LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam,
3057 WND* wndPtr = WIN_FindWndPtr(hwnd);
3060 TRACE("hwnd=0x%04x msg=0x%04x wp=0x%04x lp=0x%08lx\n",
3061 hwnd, message, wParam, lParam);
3067 CREATESTRUCTA *cs = (CREATESTRUCTA*)lParam;
3068 SetWindowLongA( hwnd, 0, (LONG)cs->lpCreateParams );
3073 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3074 retvalue = MA_NOACTIVATE;
3080 BeginPaint( hwnd, &ps );
3081 MENU_DrawPopupMenu( hwnd, ps.hdc,
3082 (HMENU)GetWindowLongA( hwnd, 0 ) );
3083 EndPaint( hwnd, &ps );
3093 /* zero out global pointer in case resident popup window
3094 * was somehow destroyed. */
3096 if(MENU_GetTopPopupWnd() )
3098 if( hwnd == pTopPopupWnd->hwndSelf )
3100 ERR("resident popup destroyed!\n");
3102 MENU_DestroyTopPopupWnd();
3107 MENU_ReleaseTopPopupWnd();
3115 if( !(*(HMENU*)wndPtr->wExtra) )
3116 ERR("no menu to display\n");
3119 *(HMENU*)wndPtr->wExtra = 0;
3122 case MM_SETMENUHANDLE:
3124 *(HMENU*)wndPtr->wExtra = (HMENU)wParam;
3127 case MM_GETMENUHANDLE:
3129 retvalue = *(HMENU*)wndPtr->wExtra;
3133 retvalue = DefWindowProcA( hwnd, message, wParam, lParam );
3138 WIN_ReleaseWndPtr(wndPtr);
3143 /***********************************************************************
3144 * MENU_GetMenuBarHeight
3146 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3148 UINT MENU_GetMenuBarHeight( HWND hwnd, UINT menubarWidth,
3149 INT orgX, INT orgY )
3157 TRACE("HWND 0x%x, width %d, at (%d, %d).\n",
3158 hwnd, menubarWidth, orgX, orgY );
3160 if (!(wndPtr = WIN_FindWndPtr( hwnd )))
3163 if (!(lppop = MENU_GetMenu((HMENU16)wndPtr->wIDmenu)))
3165 WIN_ReleaseWndPtr(wndPtr);
3169 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3170 SelectObject( hdc, hMenuFont);
3171 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3172 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3173 ReleaseDC( hwnd, hdc );
3174 retvalue = lppop->Height;
3175 WIN_ReleaseWndPtr(wndPtr);
3180 /*******************************************************************
3181 * ChangeMenu16 (USER.153)
3183 BOOL16 WINAPI ChangeMenu16( HMENU16 hMenu, UINT16 pos, SEGPTR data,
3184 UINT16 id, UINT16 flags )
3186 TRACE("menu=%04x pos=%d data=%08lx id=%04x flags=%04x\n",
3187 hMenu, pos, (DWORD)data, id, flags );
3188 if (flags & MF_APPEND) return AppendMenu16( hMenu, flags & ~MF_APPEND,
3191 /* FIXME: Word passes the item id in 'pos' and 0 or 0xffff as id */
3192 /* for MF_DELETE. We should check the parameters for all others */
3193 /* MF_* actions also (anybody got a doc on ChangeMenu?). */
3195 if (flags & MF_DELETE) return DeleteMenu16(hMenu, pos, flags & ~MF_DELETE);
3196 if (flags & MF_CHANGE) return ModifyMenu16(hMenu, pos, flags & ~MF_CHANGE,
3198 if (flags & MF_REMOVE) return RemoveMenu16(hMenu,
3199 flags & MF_BYPOSITION ? pos : id,
3200 flags & ~MF_REMOVE );
3201 /* Default: MF_INSERT */
3202 return InsertMenu16( hMenu, pos, flags, id, data );
3206 /*******************************************************************
3207 * ChangeMenu32A (USER32.23)
3209 BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3210 UINT id, UINT flags )
3212 TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3213 hMenu, pos, (DWORD)data, id, flags );
3214 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3216 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3217 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3219 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3220 flags & MF_BYPOSITION ? pos : id,
3221 flags & ~MF_REMOVE );
3222 /* Default: MF_INSERT */
3223 return InsertMenuA( hMenu, pos, flags, id, data );
3227 /*******************************************************************
3228 * ChangeMenu32W (USER32.24)
3230 BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3231 UINT id, UINT flags )
3233 TRACE("menu=%08x pos=%d data=%08lx id=%08x flags=%08x\n",
3234 hMenu, pos, (DWORD)data, id, flags );
3235 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3237 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3238 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3240 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3241 flags & MF_BYPOSITION ? pos : id,
3242 flags & ~MF_REMOVE );
3243 /* Default: MF_INSERT */
3244 return InsertMenuW( hMenu, pos, flags, id, data );
3248 /*******************************************************************
3249 * CheckMenuItem16 (USER.154)
3251 BOOL16 WINAPI CheckMenuItem16( HMENU16 hMenu, UINT16 id, UINT16 flags )
3253 return (BOOL16)CheckMenuItem( hMenu, id, flags );
3257 /*******************************************************************
3258 * CheckMenuItem (USER32.46)
3260 DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3265 TRACE("menu=%04x id=%04x flags=%04x\n", hMenu, id, flags );
3266 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3267 ret = item->fState & MF_CHECKED;
3268 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3269 else item->fState &= ~MF_CHECKED;
3274 /**********************************************************************
3275 * EnableMenuItem16 (USER.155)
3277 UINT16 WINAPI EnableMenuItem16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
3279 return EnableMenuItem( hMenu, wItemID, wFlags );
3283 /**********************************************************************
3284 * EnableMenuItem32 (USER32.170)
3286 UINT WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3292 TRACE("(%04x, %04X, %04X) !\n",
3293 hMenu, wItemID, wFlags);
3295 /* Get the Popupmenu to access the owner menu */
3296 if (!(menu = MENU_GetMenu(hMenu)))
3299 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3302 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3303 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3305 /* In win95 if the close item in the system menu change update the close button */
3306 if (TWEAK_WineLook == WIN95_LOOK)
3307 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3309 if (menu->hSysMenuOwner != 0)
3311 POPUPMENU* parentMenu;
3313 /* Get the parent menu to access*/
3314 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3317 /* Refresh the frame to reflect the change*/
3318 SetWindowPos(parentMenu->hWnd, 0, 0, 0, 0, 0,
3319 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
3327 /*******************************************************************
3328 * GetMenuString16 (USER.161)
3330 INT16 WINAPI GetMenuString16( HMENU16 hMenu, UINT16 wItemID,
3331 LPSTR str, INT16 nMaxSiz, UINT16 wFlags )
3333 return GetMenuStringA( hMenu, wItemID, str, nMaxSiz, wFlags );
3337 /*******************************************************************
3338 * GetMenuString32A (USER32.268)
3340 INT WINAPI GetMenuStringA(
3341 HMENU hMenu, /* [in] menuhandle */
3342 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3343 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3344 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3345 UINT wFlags /* [in] MF_ flags */
3349 TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3350 hMenu, wItemID, str, nMaxSiz, wFlags );
3351 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
3352 if (!IS_STRING_ITEM(item->fType)) return 0;
3353 if (!str || !nMaxSiz) return strlen(item->text);
3355 lstrcpynA( str, item->text, nMaxSiz );
3356 TRACE("returning '%s'\n", str );
3361 /*******************************************************************
3362 * GetMenuString32W (USER32.269)
3364 INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3365 LPWSTR str, INT nMaxSiz, UINT wFlags )
3369 TRACE("menu=%04x item=%04x ptr=%p len=%d flags=%04x\n",
3370 hMenu, wItemID, str, nMaxSiz, wFlags );
3371 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
3372 if (!IS_STRING_ITEM(item->fType)) return 0;
3373 if (!str || !nMaxSiz) return strlen(item->text);
3375 lstrcpynAtoW( str, item->text, nMaxSiz );
3376 return lstrlenW(str);
3380 /**********************************************************************
3381 * HiliteMenuItem16 (USER.162)
3383 BOOL16 WINAPI HiliteMenuItem16( HWND16 hWnd, HMENU16 hMenu, UINT16 wItemID,
3386 return HiliteMenuItem( hWnd, hMenu, wItemID, wHilite );
3390 /**********************************************************************
3391 * HiliteMenuItem32 (USER32.318)
3393 BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3397 TRACE("(%04x, %04x, %04x, %04x);\n",
3398 hWnd, hMenu, wItemID, wHilite);
3399 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3400 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3401 if (menu->FocusedItem == wItemID) return TRUE;
3402 MENU_HideSubPopups( hWnd, hMenu, FALSE );
3403 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3408 /**********************************************************************
3409 * GetMenuState16 (USER.250)
3411 UINT16 WINAPI GetMenuState16( HMENU16 hMenu, UINT16 wItemID, UINT16 wFlags )
3413 return GetMenuState( hMenu, wItemID, wFlags );
3417 /**********************************************************************
3418 * GetMenuState (USER32.267)
3420 UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3423 TRACE("(menu=%04x, id=%04x, flags=%04x);\n",
3424 hMenu, wItemID, wFlags);
3425 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3426 debug_print_menuitem (" item: ", item, "");
3427 if (item->fType & MF_POPUP)
3429 POPUPMENU *menu = MENU_GetMenu( item->hSubMenu );
3430 if (!menu) return -1;
3431 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3435 /* We used to (from way back then) mask the result to 0xff. */
3436 /* I don't know why and it seems wrong as the documented */
3437 /* return flag MF_SEPARATOR is outside that mask. */
3438 return (item->fType | item->fState);
3443 /**********************************************************************
3444 * GetMenuItemCount16 (USER.263)
3446 INT16 WINAPI GetMenuItemCount16( HMENU16 hMenu )
3448 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3449 if (!menu) return -1;
3450 TRACE("(%04x) returning %d\n",
3451 hMenu, menu->nItems );
3452 return menu->nItems;
3456 /**********************************************************************
3457 * GetMenuItemCount32 (USER32.262)
3459 INT WINAPI GetMenuItemCount( HMENU hMenu )
3461 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3462 if (!menu) return -1;
3463 TRACE("(%04x) returning %d\n",
3464 hMenu, menu->nItems );
3465 return menu->nItems;
3468 /**********************************************************************
3469 * GetMenuItemID16 (USER.264)
3471 UINT16 WINAPI GetMenuItemID16( HMENU16 hMenu, INT16 nPos )
3473 return (UINT16) GetMenuItemID (hMenu, nPos);
3476 /**********************************************************************
3477 * GetMenuItemID32 (USER32.263)
3479 UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3483 if (!(lpmi = MENU_FindItem(&hMenu,&nPos,MF_BYPOSITION))) return 0;
3484 if (lpmi->fType & MF_POPUP) return -1;
3489 /*******************************************************************
3490 * InsertMenu16 (USER.410)
3492 BOOL16 WINAPI InsertMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
3493 UINT16 id, SEGPTR data )
3495 UINT pos32 = (UINT)pos;
3496 if ((pos == (UINT16)-1) && (flags & MF_BYPOSITION)) pos32 = (UINT)-1;
3497 if (IS_STRING_ITEM(flags) && data)
3498 return InsertMenuA( hMenu, pos32, flags, id,
3499 (LPSTR)PTR_SEG_TO_LIN(data) );
3500 return InsertMenuA( hMenu, pos32, flags, id, (LPSTR)data );
3504 /*******************************************************************
3505 * InsertMenu32A (USER32.322)
3507 BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3508 UINT id, LPCSTR str )
3512 if (IS_STRING_ITEM(flags) && str)
3513 TRACE("hMenu %04x, pos %d, flags %08x, "
3514 "id %04x, str '%s'\n",
3515 hMenu, pos, flags, id, str );
3516 else TRACE("hMenu %04x, pos %d, flags %08x, "
3517 "id %04x, str %08lx (not a string)\n",
3518 hMenu, pos, flags, id, (DWORD)str );
3520 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3522 if (!(MENU_SetItemData( item, flags, id, str )))
3524 RemoveMenu( hMenu, pos, flags );
3528 if (flags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
3529 (MENU_GetMenu((HMENU16)id))->wFlags |= MF_POPUP;
3531 item->hCheckBit = item->hUnCheckBit = 0;
3536 /*******************************************************************
3537 * InsertMenu32W (USER32.325)
3539 BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3540 UINT id, LPCWSTR str )
3544 if (IS_STRING_ITEM(flags) && str)
3546 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3547 ret = InsertMenuA( hMenu, pos, flags, id, newstr );
3548 HeapFree( GetProcessHeap(), 0, newstr );
3551 else return InsertMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3555 /*******************************************************************
3556 * AppendMenu16 (USER.411)
3558 BOOL16 WINAPI AppendMenu16(HMENU16 hMenu, UINT16 flags, UINT16 id, SEGPTR data)
3560 return InsertMenu16( hMenu, -1, flags | MF_BYPOSITION, id, data );
3564 /*******************************************************************
3565 * AppendMenu32A (USER32.5)
3567 BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3568 UINT id, LPCSTR data )
3570 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3574 /*******************************************************************
3575 * AppendMenu32W (USER32.6)
3577 BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3578 UINT id, LPCWSTR data )
3580 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3584 /**********************************************************************
3585 * RemoveMenu16 (USER.412)
3587 BOOL16 WINAPI RemoveMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3589 return RemoveMenu( hMenu, nPos, wFlags );
3593 /**********************************************************************
3594 * RemoveMenu (USER32.441)
3596 BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3601 TRACE("(menu=%04x pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3602 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3603 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3607 MENU_FreeItemData( item );
3609 if (--menu->nItems == 0)
3611 HeapFree( SystemHeap, 0, menu->items );
3616 while(nPos < menu->nItems)
3622 menu->items = HeapReAlloc( SystemHeap, 0, menu->items,
3623 menu->nItems * sizeof(MENUITEM) );
3629 /**********************************************************************
3630 * DeleteMenu16 (USER.413)
3632 BOOL16 WINAPI DeleteMenu16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags )
3634 return DeleteMenu( hMenu, nPos, wFlags );
3638 /**********************************************************************
3639 * DeleteMenu32 (USER32.129)
3641 BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3643 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3644 if (!item) return FALSE;
3645 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
3646 /* nPos is now the position of the item */
3647 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
3652 /*******************************************************************
3653 * ModifyMenu16 (USER.414)
3655 BOOL16 WINAPI ModifyMenu16( HMENU16 hMenu, UINT16 pos, UINT16 flags,
3656 UINT16 id, SEGPTR data )
3658 if (IS_STRING_ITEM(flags))
3659 return ModifyMenuA( hMenu, pos, flags, id,
3660 (LPSTR)PTR_SEG_TO_LIN(data) );
3661 return ModifyMenuA( hMenu, pos, flags, id, (LPSTR)data );
3665 /*******************************************************************
3666 * ModifyMenu32A (USER32.397)
3668 BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
3669 UINT id, LPCSTR str )
3673 if (IS_STRING_ITEM(flags))
3675 TRACE("%04x %d %04x %04x '%s'\n",
3676 hMenu, pos, flags, id, str ? str : "#NULL#" );
3677 if (!str) return FALSE;
3681 TRACE("%04x %d %04x %04x %08lx\n",
3682 hMenu, pos, flags, id, (DWORD)str );
3685 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3686 return MENU_SetItemData( item, flags, id, str );
3690 /*******************************************************************
3691 * ModifyMenu32W (USER32.398)
3693 BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
3694 UINT id, LPCWSTR str )
3698 if (IS_STRING_ITEM(flags) && str)
3700 LPSTR newstr = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
3701 ret = ModifyMenuA( hMenu, pos, flags, id, newstr );
3702 HeapFree( GetProcessHeap(), 0, newstr );
3705 else return ModifyMenuA( hMenu, pos, flags, id, (LPCSTR)str );
3709 /**********************************************************************
3710 * CreatePopupMenu16 (USER.415)
3712 HMENU16 WINAPI CreatePopupMenu16(void)
3714 return CreatePopupMenu();
3718 /**********************************************************************
3719 * CreatePopupMenu32 (USER32.82)
3721 HMENU WINAPI CreatePopupMenu(void)
3726 if (!(hmenu = CreateMenu())) return 0;
3727 menu = (POPUPMENU *) USER_HEAP_LIN_ADDR( hmenu );
3728 menu->wFlags |= MF_POPUP;
3729 menu->bTimeToHide = FALSE;
3734 /**********************************************************************
3735 * GetMenuCheckMarkDimensions (USER.417) (USER32.258)
3737 DWORD WINAPI GetMenuCheckMarkDimensions(void)
3739 return MAKELONG( check_bitmap_width, check_bitmap_height );
3743 /**********************************************************************
3744 * SetMenuItemBitmaps16 (USER.418)
3746 BOOL16 WINAPI SetMenuItemBitmaps16( HMENU16 hMenu, UINT16 nPos, UINT16 wFlags,
3747 HBITMAP16 hNewUnCheck, HBITMAP16 hNewCheck)
3749 return SetMenuItemBitmaps( hMenu, nPos, wFlags, hNewUnCheck, hNewCheck );
3753 /**********************************************************************
3754 * SetMenuItemBitmaps32 (USER32.490)
3756 BOOL WINAPI SetMenuItemBitmaps( HMENU hMenu, UINT nPos, UINT wFlags,
3757 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
3760 TRACE("(%04x, %04x, %04x, %04x, %04x)\n",
3761 hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3762 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3764 if (!hNewCheck && !hNewUnCheck)
3766 item->fState &= ~MF_USECHECKBITMAPS;
3768 else /* Install new bitmaps */
3770 item->hCheckBit = hNewCheck;
3771 item->hUnCheckBit = hNewUnCheck;
3772 item->fState |= MF_USECHECKBITMAPS;
3778 /**********************************************************************
3779 * CreateMenu16 (USER.151)
3781 HMENU16 WINAPI CreateMenu16(void)
3783 return CreateMenu();
3787 /**********************************************************************
3788 * CreateMenu (USER32.81)
3790 HMENU WINAPI CreateMenu(void)
3794 if (!(hMenu = USER_HEAP_ALLOC( sizeof(POPUPMENU) ))) return 0;
3795 menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3797 ZeroMemory(menu, sizeof(POPUPMENU));
3798 menu->wMagic = MENU_MAGIC;
3799 menu->FocusedItem = NO_SELECTED_ITEM;
3800 menu->bTimeToHide = FALSE;
3802 TRACE("return %04x\n", hMenu );
3808 /**********************************************************************
3809 * DestroyMenu16 (USER.152)
3811 BOOL16 WINAPI DestroyMenu16( HMENU16 hMenu )
3813 return DestroyMenu( hMenu );
3817 /**********************************************************************
3818 * DestroyMenu32 (USER32.134)
3820 BOOL WINAPI DestroyMenu( HMENU hMenu )
3822 TRACE("(%04x)\n", hMenu);
3824 /* Silently ignore attempts to destroy default system popup */
3826 if (hMenu && hMenu != MENU_DefSysPopup)
3828 LPPOPUPMENU lppop = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3829 WND *pTPWnd = MENU_GetTopPopupWnd();
3831 if( pTPWnd && (hMenu == *(HMENU*)pTPWnd->wExtra) )
3832 *(UINT*)pTPWnd->wExtra = 0;
3834 if (!IS_A_MENU(lppop)) lppop = NULL;
3837 lppop->wMagic = 0; /* Mark it as destroyed */
3839 if ((lppop->wFlags & MF_POPUP) && lppop->hWnd &&
3840 (!pTPWnd || (lppop->hWnd != pTPWnd->hwndSelf)))
3841 DestroyWindow( lppop->hWnd );
3843 if (lppop->items) /* recursively destroy submenus */
3846 MENUITEM *item = lppop->items;
3847 for (i = lppop->nItems; i > 0; i--, item++)
3849 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
3850 MENU_FreeItemData( item );
3852 HeapFree( SystemHeap, 0, lppop->items );
3854 USER_HEAP_FREE( hMenu );
3855 MENU_ReleaseTopPopupWnd();
3859 MENU_ReleaseTopPopupWnd();
3863 return (hMenu != MENU_DefSysPopup);
3867 /**********************************************************************
3868 * GetSystemMenu16 (USER.156)
3870 HMENU16 WINAPI GetSystemMenu16( HWND16 hWnd, BOOL16 bRevert )
3872 return GetSystemMenu( hWnd, bRevert );
3876 /**********************************************************************
3877 * GetSystemMenu32 (USER32.291)
3879 HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
3881 WND *wndPtr = WIN_FindWndPtr( hWnd );
3886 if( wndPtr->hSysMenu )
3890 DestroyMenu(wndPtr->hSysMenu);
3891 wndPtr->hSysMenu = 0;
3895 POPUPMENU *menu = MENU_GetMenu( wndPtr->hSysMenu );
3898 if( menu->nItems > 0 && menu->items[0].hSubMenu == MENU_DefSysPopup )
3899 menu->items[0].hSubMenu = MENU_CopySysPopup();
3903 WARN("Current sys-menu (%04x) of wnd %04x is broken\n",
3904 wndPtr->hSysMenu, hWnd);
3905 wndPtr->hSysMenu = 0;
3910 if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
3911 wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, (HMENU)(-1) );
3913 if( wndPtr->hSysMenu )
3916 retvalue = GetSubMenu16(wndPtr->hSysMenu, 0);
3918 /* Store the dummy sysmenu handle to facilitate the refresh */
3919 /* of the close button if the SC_CLOSE item change */
3920 menu = MENU_GetMenu(retvalue);
3922 menu->hSysMenuOwner = wndPtr->hSysMenu;
3924 WIN_ReleaseWndPtr(wndPtr);
3930 /*******************************************************************
3931 * SetSystemMenu16 (USER.280)
3933 BOOL16 WINAPI SetSystemMenu16( HWND16 hwnd, HMENU16 hMenu )
3935 return SetSystemMenu( hwnd, hMenu );
3939 /*******************************************************************
3940 * SetSystemMenu32 (USER32.508)
3942 BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
3944 WND *wndPtr = WIN_FindWndPtr(hwnd);
3948 if (wndPtr->hSysMenu) DestroyMenu( wndPtr->hSysMenu );
3949 wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
3950 WIN_ReleaseWndPtr(wndPtr);
3957 /**********************************************************************
3958 * GetMenu16 (USER.157)
3960 HMENU16 WINAPI GetMenu16( HWND16 hWnd )
3962 return (HMENU16)GetMenu(hWnd);
3966 /**********************************************************************
3967 * GetMenu32 (USER32.257)
3969 HMENU WINAPI GetMenu( HWND hWnd )
3972 WND * wndPtr = WIN_FindWndPtr(hWnd);
3973 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD))
3975 retvalue = (HMENU)wndPtr->wIDmenu;
3980 WIN_ReleaseWndPtr(wndPtr);
3985 /**********************************************************************
3986 * SetMenu16 (USER.158)
3988 BOOL16 WINAPI SetMenu16( HWND16 hWnd, HMENU16 hMenu )
3990 return SetMenu( hWnd, hMenu );
3994 /**********************************************************************
3995 * SetMenu32 (USER32.487)
3997 BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
3999 WND * wndPtr = WIN_FindWndPtr(hWnd);
4001 TRACE("(%04x, %04x);\n", hWnd, hMenu);
4003 if (hMenu && !IsMenu(hMenu))
4005 WARN("hMenu is not a menu handle\n");
4010 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD))
4012 if (GetCapture() == hWnd) ReleaseCapture();
4014 wndPtr->wIDmenu = (UINT)hMenu;
4019 if (!(lpmenu = MENU_GetMenu(hMenu)))
4021 WIN_ReleaseWndPtr(wndPtr);
4024 lpmenu->hWnd = hWnd;
4025 lpmenu->wFlags &= ~MF_POPUP; /* Can't be a popup */
4026 lpmenu->Height = 0; /* Make sure we recalculate the size */
4028 if (IsWindowVisible(hWnd))
4029 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4030 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4031 WIN_ReleaseWndPtr(wndPtr);
4034 WIN_ReleaseWndPtr(wndPtr);
4040 /**********************************************************************
4041 * GetSubMenu16 (USER.159)
4043 HMENU16 WINAPI GetSubMenu16( HMENU16 hMenu, INT16 nPos )
4045 return GetSubMenu( hMenu, nPos );
4049 /**********************************************************************
4050 * GetSubMenu32 (USER32.288)
4052 HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
4056 if (!(lpmi = MENU_FindItem(&hMenu,&nPos,MF_BYPOSITION))) return 0;
4057 if (!(lpmi->fType & MF_POPUP)) return 0;
4058 return lpmi->hSubMenu;
4062 /**********************************************************************
4063 * DrawMenuBar16 (USER.160)
4065 void WINAPI DrawMenuBar16( HWND16 hWnd )
4067 DrawMenuBar( hWnd );
4071 /**********************************************************************
4072 * DrawMenuBar (USER32.161)
4074 BOOL WINAPI DrawMenuBar( HWND hWnd )
4077 WND *wndPtr = WIN_FindWndPtr(hWnd);
4078 if (wndPtr && !(wndPtr->dwStyle & WS_CHILD) && wndPtr->wIDmenu)
4080 lppop = MENU_GetMenu((HMENU16)wndPtr->wIDmenu);
4083 WIN_ReleaseWndPtr(wndPtr);
4087 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
4088 lppop->hwndOwner = hWnd;
4089 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4090 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4091 WIN_ReleaseWndPtr(wndPtr);
4094 WIN_ReleaseWndPtr(wndPtr);
4099 /***********************************************************************
4100 * EndMenu (USER.187) (USER32.175)
4102 void WINAPI EndMenu(void)
4105 * FIXME: NOT ENOUGH! This has to cancel menu tracking right away.
4112 /***********************************************************************
4113 * LookupMenuHandle (USER.217)
4115 HMENU16 WINAPI LookupMenuHandle16( HMENU16 hmenu, INT16 id )
4117 HMENU hmenu32 = hmenu;
4119 if (!MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND )) return 0;
4120 else return hmenu32;
4124 /**********************************************************************
4125 * LoadMenu16 (USER.150)
4127 HMENU16 WINAPI LoadMenu16( HINSTANCE16 instance, SEGPTR name )
4135 char *str = (char *)PTR_SEG_TO_LIN( name );
4136 TRACE("(%04x,'%s')\n", instance, str );
4137 if (str[0] == '#') name = (SEGPTR)atoi( str + 1 );
4140 TRACE("(%04x,%04x)\n",instance,LOWORD(name));
4142 if (!name) return 0;
4144 /* check for Win32 module */
4145 if (HIWORD(instance))
4146 return LoadMenuA(instance,PTR_SEG_TO_LIN(name));
4147 instance = GetExePtr( instance );
4149 if (!(hRsrc = FindResource16( instance, name, RT_MENU16 ))) return 0;
4150 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
4151 hMenu = LoadMenuIndirect16(LockResource16(handle));
4152 FreeResource16( handle );
4157 /*****************************************************************
4158 * LoadMenu32A (USER32.370)
4160 HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
4162 HRSRC hrsrc = FindResourceA( instance, name, RT_MENUA );
4163 if (!hrsrc) return 0;
4164 return LoadMenuIndirectA( (LPCVOID)LoadResource( instance, hrsrc ));
4168 /*****************************************************************
4169 * LoadMenu32W (USER32.373)
4171 HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
4173 HRSRC hrsrc = FindResourceW( instance, name, RT_MENUW );
4174 if (!hrsrc) return 0;
4175 return LoadMenuIndirectW( (LPCVOID)LoadResource( instance, hrsrc ));
4179 /**********************************************************************
4180 * LoadMenuIndirect16 (USER.220)
4182 HMENU16 WINAPI LoadMenuIndirect16( LPCVOID template )
4185 WORD version, offset;
4186 LPCSTR p = (LPCSTR)template;
4188 TRACE("(%p)\n", template );
4189 version = GET_WORD(p);
4193 WARN("version must be 0 for Win16\n" );
4196 offset = GET_WORD(p);
4197 p += sizeof(WORD) + offset;
4198 if (!(hMenu = CreateMenu())) return 0;
4199 if (!MENU_ParseResource( p, hMenu, FALSE ))
4201 DestroyMenu( hMenu );
4208 /**********************************************************************
4209 * LoadMenuIndirect32A (USER32.371)
4211 HMENU WINAPI LoadMenuIndirectA( LPCVOID template )
4214 WORD version, offset;
4215 LPCSTR p = (LPCSTR)template;
4217 TRACE("%p\n", template );
4218 version = GET_WORD(p);
4223 offset = GET_WORD(p);
4224 p += sizeof(WORD) + offset;
4225 if (!(hMenu = CreateMenu())) return 0;
4226 if (!MENU_ParseResource( p, hMenu, TRUE ))
4228 DestroyMenu( hMenu );
4233 offset = GET_WORD(p);
4234 p += sizeof(WORD) + offset;
4235 if (!(hMenu = CreateMenu())) return 0;
4236 if (!MENUEX_ParseResource( p, hMenu))
4238 DestroyMenu( hMenu );
4243 ERR("version %d not supported.\n", version);
4249 /**********************************************************************
4250 * LoadMenuIndirect32W (USER32.372)
4252 HMENU WINAPI LoadMenuIndirectW( LPCVOID template )
4254 /* FIXME: is there anything different between A and W? */
4255 return LoadMenuIndirectA( template );
4259 /**********************************************************************
4260 * IsMenu16 (USER.358)
4262 BOOL16 WINAPI IsMenu16( HMENU16 hmenu )
4264 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
4265 return IS_A_MENU(menu);
4269 /**********************************************************************
4270 * IsMenu32 (USER32.346)
4272 BOOL WINAPI IsMenu(HMENU hmenu)
4274 LPPOPUPMENU menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hmenu);
4275 return IS_A_MENU(menu);
4278 /**********************************************************************
4279 * GetMenuItemInfo32_common
4282 static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4283 LPMENUITEMINFOA lpmii, BOOL unicode)
4285 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
4287 debug_print_menuitem("GetMenuItemInfo32_common: ", menu, "");
4292 if (lpmii->fMask & MIIM_TYPE) {
4293 lpmii->fType = menu->fType;
4294 switch (MENU_ITEM_TYPE(menu->fType)) {
4296 if (menu->text && lpmii->dwTypeData && lpmii->cch) {
4298 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData, menu->text, lpmii->cch);
4299 lpmii->cch = lstrlenW((LPWSTR)menu->text);
4301 lstrcpynA(lpmii->dwTypeData, menu->text, lpmii->cch);
4302 lpmii->cch = lstrlenA(menu->text);
4308 lpmii->dwTypeData = menu->text;
4315 if (lpmii->fMask & MIIM_STRING) {
4317 lstrcpynAtoW((LPWSTR) lpmii->dwTypeData, menu->text, lpmii->cch);
4318 lpmii->cch = lstrlenW((LPWSTR)menu->text);
4320 lstrcpynA(lpmii->dwTypeData, menu->text, lpmii->cch);
4321 lpmii->cch = lstrlenA(menu->text);
4325 if (lpmii->fMask & MIIM_FTYPE)
4326 lpmii->fType = menu->fType;
4328 if (lpmii->fMask & MIIM_BITMAP)
4329 lpmii->hbmpItem = menu->hbmpItem;
4331 if (lpmii->fMask & MIIM_STATE)
4332 lpmii->fState = menu->fState;
4334 if (lpmii->fMask & MIIM_ID)
4335 lpmii->wID = menu->wID;
4337 if (lpmii->fMask & MIIM_SUBMENU)
4338 lpmii->hSubMenu = menu->hSubMenu;
4340 if (lpmii->fMask & MIIM_CHECKMARKS) {
4341 lpmii->hbmpChecked = menu->hCheckBit;
4342 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4344 if (lpmii->fMask & MIIM_DATA)
4345 lpmii->dwItemData = menu->dwItemData;
4350 /**********************************************************************
4351 * GetMenuItemInfoA (USER32.264)
4353 BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4354 LPMENUITEMINFOA lpmii)
4356 return GetMenuItemInfo_common (hmenu, item, bypos, lpmii, FALSE);
4359 /**********************************************************************
4360 * GetMenuItemInfoW (USER32.265)
4362 BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4363 LPMENUITEMINFOW lpmii)
4365 return GetMenuItemInfo_common (hmenu, item, bypos,
4366 (LPMENUITEMINFOA)lpmii, TRUE);
4369 /**********************************************************************
4370 * SetMenuItemInfo32_common
4373 static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4374 const MENUITEMINFOA *lpmii,
4377 if (!menu) return FALSE;
4379 if (lpmii->fMask & MIIM_TYPE ) {
4380 /* Get rid of old string. */
4381 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4382 HeapFree(SystemHeap, 0, menu->text);
4386 /* make only MENU_ITEM_TYPE bits in menu->fType equal lpmii->fType */
4387 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4388 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4390 menu->text = lpmii->dwTypeData;
4392 if (IS_STRING_ITEM(menu->fType) && menu->text) {
4394 menu->text = HEAP_strdupWtoA(SystemHeap, 0, (LPWSTR) lpmii->dwTypeData);
4396 menu->text = HEAP_strdupA(SystemHeap, 0, lpmii->dwTypeData);
4400 if (lpmii->fMask & MIIM_FTYPE ) {
4401 /* free the string when the type is changing */
4402 if ( (!IS_STRING_ITEM(lpmii->fType)) && IS_STRING_ITEM(menu->fType) && menu->text) {
4403 HeapFree(SystemHeap, 0, menu->text);
4406 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4407 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4410 if (lpmii->fMask & MIIM_STRING ) {
4411 /* free the string when used */
4412 if ( IS_STRING_ITEM(menu->fType) && menu->text) {
4413 HeapFree(SystemHeap, 0, menu->text);
4415 menu->text = HEAP_strdupWtoA(SystemHeap, 0, (LPWSTR) lpmii->dwTypeData);
4417 menu->text = HEAP_strdupA(SystemHeap, 0, lpmii->dwTypeData);
4421 if (lpmii->fMask & MIIM_STATE)
4423 /* fixme: MFS_DEFAULT do we have to reset the other menu items? */
4424 menu->fState = lpmii->fState;
4427 if (lpmii->fMask & MIIM_ID)
4428 menu->wID = lpmii->wID;
4430 if (lpmii->fMask & MIIM_SUBMENU) {
4431 menu->hSubMenu = lpmii->hSubMenu;
4432 if (menu->hSubMenu) {
4433 POPUPMENU *subMenu = MENU_GetMenu((UINT16)menu->hSubMenu);
4435 subMenu->wFlags |= MF_POPUP;
4436 menu->fType |= MF_POPUP;
4439 /* FIXME: Return an error ? */
4440 menu->fType &= ~MF_POPUP;
4443 menu->fType &= ~MF_POPUP;
4446 if (lpmii->fMask & MIIM_CHECKMARKS)
4448 menu->hCheckBit = lpmii->hbmpChecked;
4449 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4451 if (lpmii->fMask & MIIM_DATA)
4452 menu->dwItemData = lpmii->dwItemData;
4454 debug_print_menuitem("SetMenuItemInfo32_common: ", menu, "");
4458 /**********************************************************************
4459 * SetMenuItemInfo32A (USER32.491)
4461 BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4462 const MENUITEMINFOA *lpmii)
4464 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4468 /**********************************************************************
4469 * SetMenuItemInfo32W (USER32.492)
4471 BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4472 const MENUITEMINFOW *lpmii)
4474 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4475 (const MENUITEMINFOA*)lpmii, TRUE);
4478 /**********************************************************************
4479 * SetMenuDefaultItem (USER32.489)
4482 BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4488 TRACE("(0x%x,%d,%d)\n", hmenu, uItem, bypos);
4490 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4492 /* reset all default-item flags */
4494 for (i = 0; i < menu->nItems; i++, item++)
4496 item->fState &= ~MFS_DEFAULT;
4499 /* no default item */
4508 if ( uItem >= menu->nItems ) return FALSE;
4509 item[uItem].fState |= MFS_DEFAULT;
4514 for (i = 0; i < menu->nItems; i++, item++)
4516 if (item->wID == uItem)
4518 item->fState |= MFS_DEFAULT;
4527 /**********************************************************************
4528 * GetMenuDefaultItem (USER32.260)
4530 UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4536 TRACE("(0x%x,%d,%d)\n", hmenu, bypos, flags);
4538 if (!(menu = MENU_GetMenu(hmenu))) return -1;
4540 /* find default item */
4544 if (! item) return -1;
4546 while ( !( item->fState & MFS_DEFAULT ) )
4549 if (i >= menu->nItems ) return -1;
4552 /* default: don't return disabled items */
4553 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
4555 /* search rekursiv when needed */
4556 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
4559 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
4560 if ( -1 != ret ) return ret;
4562 /* when item not found in submenu, return the popup item */
4564 return ( bypos ) ? i : item->wID;
4568 /*******************************************************************
4569 * InsertMenuItem16 (USER.441)
4573 BOOL16 WINAPI InsertMenuItem16( HMENU16 hmenu, UINT16 pos, BOOL16 byposition,
4574 const MENUITEMINFO16 *mii )
4578 miia.cbSize = sizeof(miia);
4579 miia.fMask = mii->fMask;
4580 miia.dwTypeData = mii->dwTypeData;
4581 miia.fType = mii->fType;
4582 miia.fState = mii->fState;
4583 miia.wID = mii->wID;
4584 miia.hSubMenu = mii->hSubMenu;
4585 miia.hbmpChecked = mii->hbmpChecked;
4586 miia.hbmpUnchecked = mii->hbmpUnchecked;
4587 miia.dwItemData = mii->dwItemData;
4588 miia.cch = mii->cch;
4589 if (IS_STRING_ITEM(miia.fType))
4590 miia.dwTypeData = PTR_SEG_TO_LIN(miia.dwTypeData);
4591 return InsertMenuItemA( hmenu, pos, byposition, &miia );
4595 /**********************************************************************
4596 * InsertMenuItem32A (USER32.323)
4598 BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
4599 const MENUITEMINFOA *lpmii)
4601 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4602 return SetMenuItemInfo_common(item, lpmii, FALSE);
4606 /**********************************************************************
4607 * InsertMenuItem32W (USER32.324)
4609 BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
4610 const MENUITEMINFOW *lpmii)
4612 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4613 return SetMenuItemInfo_common(item, (const MENUITEMINFOA*)lpmii, TRUE);
4616 /**********************************************************************
4617 * CheckMenuRadioItem32 (USER32.47)
4620 BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
4621 UINT first, UINT last, UINT check,
4624 MENUITEM *mifirst, *milast, *micheck;
4625 HMENU mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
4627 TRACE("ox%x: %d-%d, check %d, bypos=%d\n",
4628 hMenu, first, last, check, bypos);
4630 mifirst = MENU_FindItem (&mfirst, &first, bypos);
4631 milast = MENU_FindItem (&mlast, &last, bypos);
4632 micheck = MENU_FindItem (&mcheck, &check, bypos);
4634 if (mifirst == NULL || milast == NULL || micheck == NULL ||
4635 mifirst > milast || mfirst != mlast || mfirst != mcheck ||
4636 micheck > milast || micheck < mifirst)
4639 while (mifirst <= milast)
4641 if (mifirst == micheck)
4643 mifirst->fType |= MFT_RADIOCHECK;
4644 mifirst->fState |= MFS_CHECKED;
4646 mifirst->fType &= ~MFT_RADIOCHECK;
4647 mifirst->fState &= ~MFS_CHECKED;
4655 /**********************************************************************
4656 * CheckMenuRadioItem16 (not a Windows API)
4659 BOOL16 WINAPI CheckMenuRadioItem16(HMENU16 hMenu,
4660 UINT16 first, UINT16 last, UINT16 check,
4663 return CheckMenuRadioItem (hMenu, first, last, check, bypos);
4666 /**********************************************************************
4667 * GetMenuItemRect32 (USER32.266)
4669 * ATTENTION: Here, the returned values in rect are the screen
4670 * coordinates of the item just like if the menu was
4671 * always on the upper left side of the application.
4674 BOOL WINAPI GetMenuItemRect (HWND hwnd, HMENU hMenu, UINT uItem,
4677 POPUPMENU *itemMenu;
4681 TRACE("(0x%x,0x%x,%d,%p)\n", hwnd, hMenu, uItem, rect);
4683 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4684 referenceHwnd = hwnd;
4688 itemMenu = MENU_GetMenu(hMenu);
4689 if (itemMenu == NULL)
4692 if(itemMenu->hWnd == 0)
4694 referenceHwnd = itemMenu->hWnd;
4697 if ((rect == NULL) || (item == NULL))
4702 MapWindowPoints(referenceHwnd, 0, (LPPOINT)rect, 2);
4707 /**********************************************************************
4708 * GetMenuItemRect16 (USER.665)
4711 BOOL16 WINAPI GetMenuItemRect16 (HWND16 hwnd, HMENU16 hMenu, UINT16 uItem,
4717 if (!rect) return FALSE;
4718 res = GetMenuItemRect (hwnd, hMenu, uItem, &r32);
4719 CONV_RECT32TO16 (&r32, rect);
4723 /**********************************************************************
4727 * MIM_APPLYTOSUBMENUS
4728 * actually use the items to draw the menu
4730 BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
4734 TRACE("(0x%04x %p)\n", hMenu, lpmi);
4736 if (lpmi && (lpmi->cbSize==sizeof(MENUINFO)) && (menu = MENU_GetMenu(hMenu)))
4739 if (lpmi->fMask & MIM_BACKGROUND)
4740 menu->hbrBack = lpmi->hbrBack;
4742 if (lpmi->fMask & MIM_HELPID)
4743 menu->dwContextHelpID = lpmi->dwContextHelpID;
4745 if (lpmi->fMask & MIM_MAXHEIGHT)
4746 menu->cyMax = lpmi->cyMax;
4748 if (lpmi->fMask & MIM_MENUDATA)
4749 menu->dwMenuData = lpmi->dwMenuData;
4751 if (lpmi->fMask & MIM_STYLE)
4752 menu->dwStyle = lpmi->dwStyle;
4759 /**********************************************************************
4766 BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
4769 TRACE("(0x%04x %p)\n", hMenu, lpmi);
4771 if (lpmi && (menu = MENU_GetMenu(hMenu)))
4774 if (lpmi->fMask & MIM_BACKGROUND)
4775 lpmi->hbrBack = menu->hbrBack;
4777 if (lpmi->fMask & MIM_HELPID)
4778 lpmi->dwContextHelpID = menu->dwContextHelpID;
4780 if (lpmi->fMask & MIM_MAXHEIGHT)
4781 lpmi->cyMax = menu->cyMax;
4783 if (lpmi->fMask & MIM_MENUDATA)
4784 lpmi->dwMenuData = menu->dwMenuData;
4786 if (lpmi->fMask & MIM_STYLE)
4787 lpmi->dwStyle = menu->dwStyle;
4794 /**********************************************************************
4795 * SetMenuContextHelpId16 (USER.384)
4797 BOOL16 WINAPI SetMenuContextHelpId16( HMENU16 hMenu, DWORD dwContextHelpID)
4799 return SetMenuContextHelpId( hMenu, dwContextHelpID );
4803 /**********************************************************************
4804 * SetMenuContextHelpId (USER32.488)
4806 BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
4810 TRACE("(0x%04x 0x%08lx)\n", hMenu, dwContextHelpID);
4812 if ((menu = MENU_GetMenu(hMenu)))
4814 menu->dwContextHelpID = dwContextHelpID;
4820 /**********************************************************************
4821 * GetMenuContextHelpId16 (USER.385)
4823 DWORD WINAPI GetMenuContextHelpId16( HMENU16 hMenu )
4825 return GetMenuContextHelpId( hMenu );
4828 /**********************************************************************
4829 * GetMenuContextHelpId (USER32.488)
4831 DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
4835 TRACE("(0x%04x)\n", hMenu);
4837 if ((menu = MENU_GetMenu(hMenu)))
4839 return menu->dwContextHelpID;
4844 /**********************************************************************
4845 * MenuItemFromPoint (USER32.387)
4847 UINT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
4849 FIXME("(0x%04x,0x%04x,(%ld,%ld)):stub\n",
4850 hWnd, hMenu, ptScreen.x, ptScreen.y);