4 * Copyright 1993 Martin Ayotte
5 * Copyright 1994 Alexandre Julliard
6 * Copyright 1997 Morten Welinder
7 * Copyright 2005 Maxime Bellengé
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Note: the style MF_MOUSESELECT is used to mark popup items that
26 * have been selected, i.e. their popup menu is currently displayed.
27 * This is probably not the meaning this style has in MS-Windows.
39 #include "wine/port.h"
48 #include "wine/winbase16.h"
49 #include "wine/winuser16.h"
51 #include "wine/server.h"
52 #include "wine/unicode.h"
55 #include "user_private.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(menu);
59 WINE_DECLARE_DEBUG_CHANNEL(accel);
61 /* internal popup menu window messages */
63 #define MM_SETMENUHANDLE (WM_USER + 0)
64 #define MM_GETMENUHANDLE (WM_USER + 1)
66 /* Menu item structure */
68 /* ----------- MENUITEMINFO Stuff ----------- */
69 UINT fType; /* Item type. */
70 UINT fState; /* Item state. */
71 UINT_PTR wID; /* Item id. */
72 HMENU hSubMenu; /* Pop-up menu. */
73 HBITMAP hCheckBit; /* Bitmap when checked. */
74 HBITMAP hUnCheckBit; /* Bitmap when unchecked. */
75 LPWSTR text; /* Item text or bitmap handle. */
76 ULONG_PTR dwItemData; /* Application defined. */
77 LPWSTR dwTypeData; /* depends on fMask */
78 HBITMAP hbmpItem; /* bitmap in win98 style menus */
79 /* ----------- Wine stuff ----------- */
80 RECT rect; /* Item area (relative to menu window) */
81 UINT xTab; /* X position of text after Tab */
84 /* Popup menu structure */
86 WORD wFlags; /* Menu flags (MF_POPUP, MF_SYSMENU) */
87 WORD wMagic; /* Magic number */
88 WORD Width; /* Width of the whole menu */
89 WORD Height; /* Height of the whole menu */
90 UINT nItems; /* Number of items in the menu */
91 HWND hWnd; /* Window containing the menu */
92 MENUITEM *items; /* Array of menu items */
93 UINT FocusedItem; /* Currently focused item */
94 HWND hwndOwner; /* window receiving the messages for ownerdraw */
95 BOOL bTimeToHide; /* Request hiding when receiving a second click in the top-level menu item */
96 /* ------------ MENUINFO members ------ */
97 DWORD dwStyle; /* Extended mennu style */
98 UINT cyMax; /* max hight of the whole menu, 0 is screen hight */
99 HBRUSH hbrBack; /* brush for menu background */
100 DWORD dwContextHelpID;
101 DWORD dwMenuData; /* application defined value */
102 HMENU hSysMenuOwner; /* Handle to the dummy sys menu holder */
103 SIZE maxBmpSize; /* Maximum size of the bitmap items in MIIM_BITMAP state */
104 } POPUPMENU, *LPPOPUPMENU;
106 /* internal flags for menu tracking */
108 #define TF_ENDMENU 0x0001
109 #define TF_SUSPENDPOPUP 0x0002
110 #define TF_SKIPREMOVE 0x0004
115 HMENU hCurrentMenu; /* current submenu (can be equal to hTopMenu)*/
116 HMENU hTopMenu; /* initial menu */
117 HWND hOwnerWnd; /* where notifications are sent */
121 #define MENU_MAGIC 0x554d /* 'MU' */
126 /* Internal MENU_TrackMenu() flags */
127 #define TPM_INTERNAL 0xF0000000
128 #define TPM_ENTERIDLEEX 0x80000000 /* set owner window for WM_ENTERIDLE */
129 #define TPM_BUTTONDOWN 0x40000000 /* menu was clicked before tracking */
130 #define TPM_POPUPMENU 0x20000000 /* menu is a popup menu */
132 /* Space between 2 menu bar items */
133 #define MENU_BAR_ITEMS_SPACE 12
135 /* Minimum width of a tab character */
136 #define MENU_TAB_SPACE 8
138 /* Height of a separator item */
139 #define SEPARATOR_HEIGHT 5
141 /* Space between 2 columns */
142 #define MENU_COL_SPACE 4
144 /* (other menu->FocusedItem values give the position of the focused item) */
145 #define NO_SELECTED_ITEM 0xffff
147 #define MENU_ITEM_TYPE(flags) \
148 ((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR))
150 #define IS_STRING_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_STRING)
151 #define IS_BITMAP_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_BITMAP)
152 #define IS_MAGIC_ITEM(id) ((id) && ((INT_PTR)(id) < 12) && ((INT_PTR)(id) >= -1))
154 #define IS_SYSTEM_MENU(menu) \
155 (!((menu)->wFlags & MF_POPUP) && ((menu)->wFlags & MF_SYSMENU))
157 #define TYPE_MASK (MFT_STRING | MFT_BITMAP | MFT_OWNERDRAW | MFT_SEPARATOR | \
158 MFT_MENUBARBREAK | MFT_MENUBREAK | MFT_RADIOCHECK | \
159 MFT_RIGHTORDER | MFT_RIGHTJUSTIFY | \
160 MF_POPUP | MF_SYSMENU | MF_HELP)
161 #define STATE_MASK (~TYPE_MASK)
163 #define WIN_ALLOWED_MENU(style) ((style & (WS_CHILD | WS_POPUP)) != WS_CHILD)
165 static SIZE menucharsize;
166 static UINT ODitemheight; /* default owner drawn item height */
168 /* Use global popup window because there's no way 2 menus can
169 * be tracked at the same time. */
170 static HWND top_popup;
172 /* Flag set by EndMenu() to force an exit from menu tracking */
173 static BOOL fEndMenu = FALSE;
175 static LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
177 DWORD WINAPI DrawMenuBarTemp(HWND hwnd, HDC hDC, LPRECT lprect, HMENU hMenu, HFONT hFont);
179 /*********************************************************************
180 * menu class descriptor
182 const struct builtin_class_descr MENU_builtin_class =
184 POPUPMENU_CLASS_ATOMA, /* name */
185 CS_DROPSHADOW | CS_SAVEBITS | CS_DBLCLKS, /* style */
186 NULL, /* procA (winproc is Unicode only) */
187 PopupMenuWndProc, /* procW */
188 sizeof(HMENU), /* extra */
189 IDC_ARROW, /* cursor */
190 (HBRUSH)(COLOR_MENU+1) /* brush */
194 /***********************************************************************
195 * debug_print_menuitem
197 * Print a menuitem in readable form.
200 #define debug_print_menuitem(pre, mp, post) \
201 do { if (TRACE_ON(menu)) do_debug_print_menuitem(pre, mp, post); } while (0)
203 #define MENUOUT(text) \
204 TRACE("%s%s", (count++ ? "," : ""), (text))
206 #define MENUFLAG(bit,text) \
208 if (flags & (bit)) { flags &= ~(bit); MENUOUT ((text)); } \
211 static void do_debug_print_menuitem(const char *prefix, MENUITEM * mp,
214 TRACE("%s ", prefix);
216 UINT flags = mp->fType;
217 int type = MENU_ITEM_TYPE(flags);
218 TRACE( "{ ID=0x%x", mp->wID);
219 if (flags & MF_POPUP)
220 TRACE( ", Sub=%p", mp->hSubMenu);
224 if (type == MFT_STRING)
226 else if (type == MFT_SEPARATOR)
228 else if (type == MFT_OWNERDRAW)
230 else if (type == MFT_BITMAP)
236 MENUFLAG(MF_POPUP, "pop");
237 MENUFLAG(MFT_MENUBARBREAK, "barbrk");
238 MENUFLAG(MFT_MENUBREAK, "brk");
239 MENUFLAG(MFT_RADIOCHECK, "radio");
240 MENUFLAG(MFT_RIGHTORDER, "rorder");
241 MENUFLAG(MF_SYSMENU, "sys");
242 MENUFLAG(MFT_RIGHTJUSTIFY, "right"); /* same as MF_HELP */
245 TRACE( "+0x%x", flags);
251 MENUFLAG(MFS_GRAYED, "grey");
252 MENUFLAG(MFS_DEFAULT, "default");
253 MENUFLAG(MFS_DISABLED, "dis");
254 MENUFLAG(MFS_CHECKED, "check");
255 MENUFLAG(MFS_HILITE, "hi");
256 MENUFLAG(MF_USECHECKBITMAPS, "usebit");
257 MENUFLAG(MF_MOUSESELECT, "mouse");
259 TRACE( "+0x%x", flags);
262 TRACE( ", Chk=%p", mp->hCheckBit);
264 TRACE( ", Unc=%p", mp->hUnCheckBit);
266 if (type == MFT_STRING) {
268 TRACE( ", Text=%s", debugstr_w(mp->text));
270 TRACE( ", Text=Null");
271 } else if (mp->text == NULL)
274 TRACE( ", Text=%p", mp->text);
276 TRACE( ", ItemData=0x%08lx", mp->dwItemData);
282 TRACE(" %s\n", postfix);
289 /***********************************************************************
292 * Validate the given menu handle and returns the menu structure pointer.
294 static POPUPMENU *MENU_GetMenu(HMENU hMenu)
296 POPUPMENU *menu = USER_HEAP_LIN_ADDR(hMenu);
297 if (!menu || menu->wMagic != MENU_MAGIC)
299 WARN("invalid menu handle=%p, ptr=%p, magic=%x\n", hMenu, menu, menu? menu->wMagic:0);
305 /***********************************************************************
308 * Get the system menu of a window
310 static HMENU get_win_sys_menu( HWND hwnd )
313 WND *win = WIN_GetPtr( hwnd );
314 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
317 WIN_ReleasePtr( win );
322 /***********************************************************************
325 static HFONT get_menu_font( BOOL bold )
327 static HFONT hMenuFont, hMenuFontBold;
329 HFONT ret = bold ? hMenuFontBold : hMenuFont;
333 NONCLIENTMETRICSW ncm;
336 ncm.cbSize = sizeof(NONCLIENTMETRICSW);
337 SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &ncm, 0);
341 ncm.lfMenuFont.lfWeight += 300;
342 if (ncm.lfMenuFont.lfWeight > 1000) ncm.lfMenuFont.lfWeight = 1000;
344 if (!(ret = CreateFontIndirectW( &ncm.lfMenuFont ))) return 0;
345 prev = InterlockedCompareExchangePointer( (void **)(bold ? &hMenuFontBold : &hMenuFont),
349 /* another thread beat us to it */
357 /***********************************************************************
360 static HBITMAP get_arrow_bitmap(void)
362 static HBITMAP arrow_bitmap;
364 if (!arrow_bitmap) arrow_bitmap = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_MNARROW));
368 /***********************************************************************
371 * Return the default system menu.
373 static HMENU MENU_CopySysPopup(void)
375 static const WCHAR sysmenuW[] = {'S','Y','S','M','E','N','U',0};
376 HMENU hMenu = LoadMenuW(user32_module, sysmenuW);
379 POPUPMENU* menu = MENU_GetMenu(hMenu);
380 menu->wFlags |= MF_SYSMENU | MF_POPUP;
381 SetMenuDefaultItem(hMenu, SC_CLOSE, FALSE);
384 ERR("Unable to load default system menu\n" );
386 TRACE("returning %p.\n", hMenu );
392 /**********************************************************************
395 * Create a copy of the system menu. System menu in Windows is
396 * a special menu bar with the single entry - system menu popup.
397 * This popup is presented to the outside world as a "system menu".
398 * However, the real system menu handle is sometimes seen in the
399 * WM_MENUSELECT parameters (and Word 6 likes it this way).
401 HMENU MENU_GetSysMenu( HWND hWnd, HMENU hPopupMenu )
405 TRACE("loading system menu, hWnd %p, hPopupMenu %p\n", hWnd, hPopupMenu);
406 if ((hMenu = CreateMenu()))
408 POPUPMENU *menu = MENU_GetMenu(hMenu);
409 menu->wFlags = MF_SYSMENU;
410 menu->hWnd = WIN_GetFullHandle( hWnd );
411 TRACE("hWnd %p (hMenu %p)\n", menu->hWnd, hMenu);
414 hPopupMenu = MENU_CopySysPopup();
418 if (GetClassLongW(hWnd, GCL_STYLE) & CS_NOCLOSE)
419 DeleteMenu(hPopupMenu, SC_CLOSE, MF_BYCOMMAND);
421 InsertMenuW( hMenu, -1, MF_SYSMENU | MF_POPUP | MF_BYPOSITION,
422 (UINT_PTR)hPopupMenu, NULL );
424 menu->items[0].fType = MF_SYSMENU | MF_POPUP;
425 menu->items[0].fState = 0;
426 if ((menu = MENU_GetMenu(hPopupMenu))) menu->wFlags |= MF_SYSMENU;
428 TRACE("hMenu=%p (hPopup %p)\n", hMenu, hPopupMenu );
431 DestroyMenu( hMenu );
433 ERR("failed to load system menu!\n");
438 /***********************************************************************
439 * MENU_InitSysMenuPopup
441 * Grey the appropriate items in System menu.
443 static void MENU_InitSysMenuPopup( HMENU hmenu, DWORD style, DWORD clsStyle )
447 gray = !(style & WS_THICKFRAME) || (style & (WS_MAXIMIZE | WS_MINIMIZE));
448 EnableMenuItem( hmenu, SC_SIZE, (gray ? MF_GRAYED : MF_ENABLED) );
449 gray = ((style & WS_MAXIMIZE) != 0);
450 EnableMenuItem( hmenu, SC_MOVE, (gray ? MF_GRAYED : MF_ENABLED) );
451 gray = !(style & WS_MINIMIZEBOX) || (style & WS_MINIMIZE);
452 EnableMenuItem( hmenu, SC_MINIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
453 gray = !(style & WS_MAXIMIZEBOX) || (style & WS_MAXIMIZE);
454 EnableMenuItem( hmenu, SC_MAXIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
455 gray = !(style & (WS_MAXIMIZE | WS_MINIMIZE));
456 EnableMenuItem( hmenu, SC_RESTORE, (gray ? MF_GRAYED : MF_ENABLED) );
457 gray = (clsStyle & CS_NOCLOSE) != 0;
459 /* The menu item must keep its state if it's disabled */
461 EnableMenuItem( hmenu, SC_CLOSE, MF_GRAYED);
465 /******************************************************************************
467 * UINT MENU_GetStartOfNextColumn(
470 *****************************************************************************/
472 static UINT MENU_GetStartOfNextColumn(
475 POPUPMENU *menu = MENU_GetMenu(hMenu);
479 return NO_SELECTED_ITEM;
481 i = menu->FocusedItem + 1;
482 if( i == NO_SELECTED_ITEM )
485 for( ; i < menu->nItems; ++i ) {
486 if (menu->items[i].fType & MF_MENUBARBREAK)
490 return NO_SELECTED_ITEM;
494 /******************************************************************************
496 * UINT MENU_GetStartOfPrevColumn(
499 *****************************************************************************/
501 static UINT MENU_GetStartOfPrevColumn(
504 POPUPMENU *menu = MENU_GetMenu(hMenu);
508 return NO_SELECTED_ITEM;
510 if( menu->FocusedItem == 0 || menu->FocusedItem == NO_SELECTED_ITEM )
511 return NO_SELECTED_ITEM;
513 /* Find the start of the column */
515 for(i = menu->FocusedItem; i != 0 &&
516 !(menu->items[i].fType & MF_MENUBARBREAK);
520 return NO_SELECTED_ITEM;
522 for(--i; i != 0; --i) {
523 if (menu->items[i].fType & MF_MENUBARBREAK)
527 TRACE("ret %d.\n", i );
534 /***********************************************************************
537 * Find a menu item. Return a pointer on the item, and modifies *hmenu
538 * in case the item was in a sub-menu.
540 static MENUITEM *MENU_FindItem( HMENU *hmenu, UINT *nPos, UINT wFlags )
545 if ((*hmenu == (HMENU)0xffff) || (!(menu = MENU_GetMenu(*hmenu)))) return NULL;
546 if (wFlags & MF_BYPOSITION)
548 if (*nPos >= menu->nItems) return NULL;
549 return &menu->items[*nPos];
553 MENUITEM *item = menu->items;
554 for (i = 0; i < menu->nItems; i++, item++)
556 if (item->wID == *nPos)
561 else if (item->fType & MF_POPUP)
563 HMENU hsubmenu = item->hSubMenu;
564 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
576 /***********************************************************************
579 * Find a Sub menu. Return the position of the submenu, and modifies
580 * *hmenu in case it is found in another sub-menu.
581 * If the submenu cannot be found, NO_SELECTED_ITEM is returned.
583 UINT MENU_FindSubMenu( HMENU *hmenu, HMENU hSubTarget )
588 if (((*hmenu)==(HMENU)0xffff) ||
589 (!(menu = MENU_GetMenu(*hmenu))))
590 return NO_SELECTED_ITEM;
592 for (i = 0; i < menu->nItems; i++, item++) {
593 if(!(item->fType & MF_POPUP)) continue;
594 if (item->hSubMenu == hSubTarget) {
598 HMENU hsubmenu = item->hSubMenu;
599 UINT pos = MENU_FindSubMenu( &hsubmenu, hSubTarget );
600 if (pos != NO_SELECTED_ITEM) {
606 return NO_SELECTED_ITEM;
609 /***********************************************************************
612 static void MENU_FreeItemData( MENUITEM* item )
615 if (IS_STRING_ITEM(item->fType) && item->text)
616 HeapFree( GetProcessHeap(), 0, item->text );
619 /***********************************************************************
620 * MENU_FindItemByCoords
622 * Find the item at the specified coordinates (screen coords). Does
623 * not work for child windows and therefore should not be called for
624 * an arbitrary system menu.
626 static MENUITEM *MENU_FindItemByCoords( const POPUPMENU *menu,
627 POINT pt, UINT *pos )
633 if (!GetWindowRect(menu->hWnd,&wrect)) return NULL;
634 pt.x -= wrect.left;pt.y -= wrect.top;
636 for (i = 0; i < menu->nItems; i++, item++)
638 if ((pt.x >= item->rect.left) && (pt.x < item->rect.right) &&
639 (pt.y >= item->rect.top) && (pt.y < item->rect.bottom))
649 /***********************************************************************
652 * Find the menu item selected by a key press.
653 * Return item id, -1 if none, -2 if we should close the menu.
655 static UINT MENU_FindItemByKey( HWND hwndOwner, HMENU hmenu,
656 WCHAR key, BOOL forceMenuChar )
658 TRACE("\tlooking for '%c' (0x%02x) in [%p]\n", (char)key, key, hmenu );
660 if (!IsMenu( hmenu )) hmenu = GetSubMenu( get_win_sys_menu(hwndOwner), 0);
664 POPUPMENU *menu = MENU_GetMenu( hmenu );
665 MENUITEM *item = menu->items;
672 for (i = 0; i < menu->nItems; i++, item++)
674 if (IS_STRING_ITEM(item->fType) && item->text)
676 WCHAR *p = item->text - 2;
679 p = strchrW (p + 2, '&');
681 while (p != NULL && p [1] == '&');
682 if (p && (toupperW(p[1]) == toupperW(key))) return i;
686 menuchar = SendMessageW( hwndOwner, WM_MENUCHAR,
687 MAKEWPARAM( key, menu->wFlags ), (LPARAM)hmenu );
688 if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
689 if (HIWORD(menuchar) == 1) return (UINT)(-2);
695 /***********************************************************************
696 * MENU_GetBitmapItemSize
698 * Get the size of a bitmap item.
700 static void MENU_GetBitmapItemSize( HBITMAP bmp, DWORD data, SIZE *size )
704 size->cx = size->cy = 0;
706 /* check if there is a magic menu item associated with this item */
707 switch( (INT_PTR)bmp )
709 case (INT_PTR)HBMMENU_SYSTEM:
716 case (INT_PTR)HBMMENU_MBAR_RESTORE:
717 case (INT_PTR)HBMMENU_MBAR_MINIMIZE:
718 case (INT_PTR)HBMMENU_MBAR_MINIMIZE_D:
719 case (INT_PTR)HBMMENU_MBAR_CLOSE:
720 case (INT_PTR)HBMMENU_MBAR_CLOSE_D:
721 size->cx = GetSystemMetrics( SM_CYMENU ) - 4;
724 case (INT_PTR)HBMMENU_CALLBACK:
725 case (INT_PTR)HBMMENU_POPUP_CLOSE:
726 case (INT_PTR)HBMMENU_POPUP_RESTORE:
727 case (INT_PTR)HBMMENU_POPUP_MAXIMIZE:
728 case (INT_PTR)HBMMENU_POPUP_MINIMIZE:
729 FIXME("Magic %p not implemented\n", bmp );
732 if (GetObjectW(bmp, sizeof(bm), &bm ))
734 size->cx = bm.bmWidth;
735 size->cy = bm.bmHeight;
739 /***********************************************************************
740 * MENU_DrawBitmapItem
742 * Draw a bitmap item.
743 * drawhbmbitmap : True to draw the hbmbitmap(MIIM_BITMAP)/False to draw the MF_BITMAP
745 static void MENU_DrawBitmapItem( HDC hdc, MENUITEM *lpitem, const RECT *rect, BOOL menuBar, BOOL drawhbmbitmap )
750 HBITMAP bmp = (HBITMAP)lpitem->text;
751 int w = rect->right - rect->left;
752 int h = rect->bottom - rect->top;
755 HBITMAP hbmToDraw = (drawhbmbitmap)?lpitem->hbmpItem:(HBITMAP)lpitem->text;
757 /* Check if there is a magic menu item associated with this item */
758 if (IS_MAGIC_ITEM(hbmToDraw))
763 switch((INT_PTR)hbmToDraw)
765 case (INT_PTR)HBMMENU_SYSTEM:
766 if (lpitem->dwItemData)
768 bmp = (HBITMAP)lpitem->dwItemData;
769 if (!GetObjectW( bmp, sizeof(bm), &bm )) return;
773 static HBITMAP hBmpSysMenu;
775 if (!hBmpSysMenu) hBmpSysMenu = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CLOSE));
777 if (!GetObjectW( bmp, sizeof(bm), &bm )) return;
778 /* only use right half of the bitmap */
779 bmp_xoffset = bm.bmWidth / 2;
780 bm.bmWidth -= bmp_xoffset;
783 case (INT_PTR)HBMMENU_MBAR_RESTORE:
784 flags = DFCS_CAPTIONRESTORE;
786 case (INT_PTR)HBMMENU_MBAR_MINIMIZE:
787 flags = DFCS_CAPTIONMIN;
789 case (INT_PTR)HBMMENU_MBAR_MINIMIZE_D:
790 flags = DFCS_CAPTIONMIN | DFCS_INACTIVE;
792 case (INT_PTR)HBMMENU_MBAR_CLOSE:
793 flags = DFCS_CAPTIONCLOSE;
795 case (INT_PTR)HBMMENU_MBAR_CLOSE_D:
796 flags = DFCS_CAPTIONCLOSE | DFCS_INACTIVE;
798 case (INT_PTR)HBMMENU_CALLBACK:
799 case (INT_PTR)HBMMENU_POPUP_CLOSE:
800 case (INT_PTR)HBMMENU_POPUP_RESTORE:
801 case (INT_PTR)HBMMENU_POPUP_MAXIMIZE:
802 case (INT_PTR)HBMMENU_POPUP_MINIMIZE:
804 FIXME("Magic %p not implemented\n", hbmToDraw);
808 InflateRect( &r, -1, -1 );
809 if (lpitem->fState & MF_HILITE) flags |= DFCS_PUSHED;
810 DrawFrameControl( hdc, &r, DFC_CAPTION, flags );
814 if (!bmp || !GetObjectW( bmp, sizeof(bm), &bm )) return;
817 hdcMem = CreateCompatibleDC( hdc );
818 SelectObject( hdcMem, bmp );
820 /* handle fontsize > bitmap_height */
821 top = (h>bm.bmHeight) ? rect->top+(h-bm.bmHeight)/2 : rect->top;
823 rop=((lpitem->fState & MF_HILITE) && !IS_MAGIC_ITEM(hbmToDraw)) ? NOTSRCCOPY : SRCCOPY;
824 if ((lpitem->fState & MF_HILITE) && IS_BITMAP_ITEM(lpitem->fType))
825 SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
826 BitBlt( hdc, left, top, w, h, hdcMem, bmp_xoffset, 0, rop );
831 /***********************************************************************
834 * Calculate the size of the menu item and store it in lpitem->rect.
836 static void MENU_CalcItemSize( HDC hdc, MENUITEM *lpitem, HWND hwndOwner,
837 INT orgX, INT orgY, BOOL menuBar, POPUPMENU* lppop )
840 UINT check_bitmap_width = GetSystemMetrics( SM_CXMENUCHECK );
841 UINT arrow_bitmap_width;
844 TRACE("dc=%p owner=%p (%d,%d)\n", hdc, hwndOwner, orgX, orgY);
845 debug_print_menuitem("MENU_CalcItemSize: menuitem:", lpitem,
846 (menuBar ? " (MenuBar)" : ""));
848 GetObjectW( get_arrow_bitmap(), sizeof(bm), &bm );
849 arrow_bitmap_width = bm.bmWidth;
851 SetRect( &lpitem->rect, orgX, orgY, orgX, orgY );
853 if (lpitem->fType & MF_OWNERDRAW)
855 MEASUREITEMSTRUCT mis;
856 /* not done in Menu_Init: GetDialogBaseUnits() breaks there */
857 if( !menucharsize.cx ) {
858 menucharsize.cx = GdiGetCharDimensions( hdc, NULL, &menucharsize.cy );
859 /* Win95/98/ME will use menucharsize.cy here. Testing is possible
860 * but it is unlikely an application will depend on that */
861 ODitemheight = HIWORD( GetDialogBaseUnits());
863 mis.CtlType = ODT_MENU;
865 mis.itemID = lpitem->wID;
866 mis.itemData = lpitem->dwItemData;
867 mis.itemHeight = ODitemheight;
869 SendMessageW( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)&mis );
870 /* Tests reveal that Windows ( Win95 thru WinXP) adds twice the average
871 * width of a menufont character to the width of an owner-drawn menu.
873 lpitem->rect.right += mis.itemWidth + 2 * menucharsize.cx;
875 /* under at least win95 you seem to be given a standard
876 height for the menu and the height value is ignored */
877 lpitem->rect.bottom += GetSystemMetrics(SM_CYMENUSIZE);
879 lpitem->rect.bottom += mis.itemHeight;
881 TRACE("id=%04x size=%ldx%ld\n",
882 lpitem->wID, lpitem->rect.right-lpitem->rect.left,
883 lpitem->rect.bottom-lpitem->rect.top);
887 if (lpitem->fType & MF_SEPARATOR)
889 lpitem->rect.bottom += SEPARATOR_HEIGHT;
895 /* New style MIIM_BITMAP */
896 if (lpitem->hbmpItem)
898 if (lpitem->hbmpItem == HBMMENU_CALLBACK)
900 MEASUREITEMSTRUCT measItem;
901 measItem.CtlType = ODT_MENU;
903 measItem.itemID = lpitem->wID;
904 measItem.itemWidth = lpitem->rect.right - lpitem->rect.left;
905 measItem.itemHeight = lpitem->rect.bottom - lpitem->rect.top;
906 measItem.itemData = lpitem->dwItemData;
908 SendMessageW( hwndOwner, WM_MEASUREITEM, lpitem->wID, (LPARAM)&measItem);
910 /* Keep the size of the bitmap in callback mode to be able to draw it correctly */
911 lppop->maxBmpSize.cx = max(lppop->maxBmpSize.cx, measItem.itemWidth - (lpitem->rect.right - lpitem->rect.left));
912 lppop->maxBmpSize.cy = max(lppop->maxBmpSize.cy, measItem.itemHeight - (lpitem->rect.bottom - lpitem->rect.top));
913 lpitem->rect.right = lpitem->rect.left + measItem.itemWidth;
916 MENU_GetBitmapItemSize(lpitem->hbmpItem, lpitem->dwItemData, &size);
917 lppop->maxBmpSize.cx = max(lppop->maxBmpSize.cx, size.cx);
918 lppop->maxBmpSize.cy = max(lppop->maxBmpSize.cy, size.cy);
919 lpitem->rect.right += size.cx;
920 lpitem->rect.bottom += size.cy;
922 if (lppop->dwStyle & MNS_CHECKORBMP)
923 lpitem->rect.right += check_bitmap_width;
925 lpitem->rect.right += 2 * check_bitmap_width;
927 lpitem->rect.right += 2 * check_bitmap_width;
928 if (lpitem->fType & MF_POPUP)
929 lpitem->rect.right += arrow_bitmap_width;
932 if (IS_BITMAP_ITEM(lpitem->fType))
936 MENU_GetBitmapItemSize( (HBITMAP) lpitem->text, lpitem->dwItemData, &size );
937 lpitem->rect.right += size.cx;
938 lpitem->rect.bottom += size.cy;
939 /* Leave space for the sunken border */
940 lpitem->rect.right += 2;
941 lpitem->rect.bottom += 2;
945 /* it must be a text item - unless it's the system menu */
946 if (!(lpitem->fType & MF_SYSMENU) && IS_STRING_ITEM( lpitem->fType ))
949 GetTextExtentPoint32W(hdc, lpitem->text, strlenW(lpitem->text), &size);
951 lpitem->rect.right += size.cx;
952 lpitem->rect.bottom += max(max(size.cy, GetSystemMetrics(SM_CYMENU)-1), lppop->maxBmpSize.cy);
957 lpitem->rect.right += MENU_BAR_ITEMS_SPACE;
959 else if ((p = strchrW( lpitem->text, '\t' )) != NULL)
961 /* Item contains a tab (only meaningful in popup menus) */
962 GetTextExtentPoint32W(hdc, lpitem->text, (int)(p - lpitem->text) , &size);
963 lpitem->xTab = check_bitmap_width + MENU_TAB_SPACE + size.cx;
964 lpitem->rect.right += MENU_TAB_SPACE;
968 if (strchrW( lpitem->text, '\b' ))
969 lpitem->rect.right += MENU_TAB_SPACE;
970 lpitem->xTab = lpitem->rect.right - check_bitmap_width
971 - arrow_bitmap_width;
974 TRACE("(%ld,%ld)-(%ld,%ld)\n", lpitem->rect.left, lpitem->rect.top, lpitem->rect.right, lpitem->rect.bottom);
978 /***********************************************************************
979 * MENU_PopupMenuCalcSize
981 * Calculate the size of a popup menu.
983 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop, HWND hwndOwner )
988 int orgX, orgY, maxX, maxTab, maxTabWidth;
990 lppop->Width = lppop->Height = 0;
991 if (lppop->nItems == 0) return;
994 SelectObject( hdc, get_menu_font(FALSE));
999 lppop->maxBmpSize.cx = 0;
1000 lppop->maxBmpSize.cy = 0;
1002 while (start < lppop->nItems)
1004 lpitem = &lppop->items[start];
1006 if( lpitem->fType & MF_MENUBREAK)
1007 orgX += MENU_COL_SPACE;
1010 maxTab = maxTabWidth = 0;
1011 /* Parse items until column break or end of menu */
1012 for (i = start; i < lppop->nItems; i++, lpitem++)
1015 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
1017 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, FALSE, lppop );
1019 if (lpitem->fType & MF_MENUBARBREAK) orgX++;
1020 maxX = max( maxX, lpitem->rect.right );
1021 orgY = lpitem->rect.bottom;
1022 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
1024 maxTab = max( maxTab, lpitem->xTab );
1025 maxTabWidth = max(maxTabWidth,lpitem->rect.right-lpitem->xTab);
1029 /* Finish the column (set all items to the largest width found) */
1030 maxX = max( maxX, maxTab + maxTabWidth );
1031 for (lpitem = &lppop->items[start]; start < i; start++, lpitem++)
1033 lpitem->rect.right = maxX;
1034 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
1035 lpitem->xTab = maxTab;
1038 lppop->Height = max( lppop->Height, orgY );
1041 lppop->Width = maxX;
1043 /* space for 3d border */
1047 ReleaseDC( 0, hdc );
1051 /***********************************************************************
1052 * MENU_MenuBarCalcSize
1054 * FIXME: Word 6 implements its own MDI and its own 'close window' bitmap
1055 * height is off by 1 pixel which causes lengthy window relocations when
1056 * active document window is maximized/restored.
1058 * Calculate the size of the menu bar.
1060 static void MENU_MenuBarCalcSize( HDC hdc, LPRECT lprect,
1061 LPPOPUPMENU lppop, HWND hwndOwner )
1064 int start, i, orgX, orgY, maxY, helpPos;
1066 if ((lprect == NULL) || (lppop == NULL)) return;
1067 if (lppop->nItems == 0) return;
1068 TRACE("left=%ld top=%ld right=%ld bottom=%ld\n",
1069 lprect->left, lprect->top, lprect->right, lprect->bottom);
1070 lppop->Width = lprect->right - lprect->left;
1072 maxY = lprect->top+1;
1075 lppop->maxBmpSize.cx = 0;
1076 lppop->maxBmpSize.cy = 0;
1077 while (start < lppop->nItems)
1079 lpitem = &lppop->items[start];
1080 orgX = lprect->left;
1083 /* Parse items until line break or end of menu */
1084 for (i = start; i < lppop->nItems; i++, lpitem++)
1086 if ((helpPos == -1) && (lpitem->fType & MF_RIGHTJUSTIFY)) helpPos = i;
1088 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
1090 TRACE("calling MENU_CalcItemSize org=(%d, %d)\n",
1092 debug_print_menuitem (" item: ", lpitem, "");
1093 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE, lppop );
1095 if (lpitem->rect.right > lprect->right)
1097 if (i != start) break;
1098 else lpitem->rect.right = lprect->right;
1100 maxY = max( maxY, lpitem->rect.bottom );
1101 orgX = lpitem->rect.right;
1104 /* Finish the line (set all items to the largest height found) */
1105 while (start < i) lppop->items[start++].rect.bottom = maxY;
1108 lprect->bottom = maxY;
1109 lppop->Height = lprect->bottom - lprect->top;
1111 /* Flush right all items between the MF_RIGHTJUSTIFY and */
1112 /* the last item (if several lines, only move the last line) */
1113 lpitem = &lppop->items[lppop->nItems-1];
1114 orgY = lpitem->rect.top;
1115 orgX = lprect->right;
1116 for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--) {
1117 if ( (helpPos==-1) || (helpPos>i) )
1119 if (lpitem->rect.top != orgY) break; /* Other line */
1120 if (lpitem->rect.right >= orgX) break; /* Too far right already */
1121 lpitem->rect.left += orgX - lpitem->rect.right;
1122 lpitem->rect.right = orgX;
1123 orgX = lpitem->rect.left;
1127 /***********************************************************************
1130 * Draw a single menu item.
1132 static void MENU_DrawMenuItem( HWND hwnd, HMENU hmenu, HWND hwndOwner, HDC hdc, MENUITEM *lpitem,
1133 UINT height, BOOL menuBar, UINT odaction )
1136 BOOL flat_menu = FALSE;
1139 debug_print_menuitem("MENU_DrawMenuItem: ", lpitem, "");
1141 if (lpitem->fType & MF_SYSMENU)
1143 if( !IsIconic(hwnd) )
1144 NC_DrawSysButton( hwnd, hdc, lpitem->fState & (MF_HILITE | MF_MOUSESELECT) );
1148 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
1149 bkgnd = (menuBar && flat_menu) ? COLOR_MENUBAR : COLOR_MENU;
1153 if (lpitem->fState & MF_HILITE)
1155 if(menuBar && !flat_menu) {
1156 SetTextColor(hdc, GetSysColor(COLOR_MENUTEXT));
1157 SetBkColor(hdc, GetSysColor(COLOR_MENU));
1159 if(lpitem->fState & MF_GRAYED)
1160 SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
1162 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1163 SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
1168 if (lpitem->fState & MF_GRAYED)
1169 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1171 SetTextColor( hdc, GetSysColor( COLOR_MENUTEXT ) );
1172 SetBkColor( hdc, GetSysColor( bkgnd ) );
1175 if (lpitem->fType & MF_OWNERDRAW)
1178 ** Experimentation under Windows reveals that an owner-drawn
1179 ** menu is given the rectangle which includes the space it requested
1180 ** in its response to WM_MEASUREITEM _plus_ width for a checkmark
1181 ** and a popup-menu arrow. This is the value of lpitem->rect.
1182 ** Windows will leave all drawing to the application except for
1183 ** the popup-menu arrow. Windows always draws that itself, after
1184 ** the menu owner has finished drawing.
1188 dis.CtlType = ODT_MENU;
1190 dis.itemID = lpitem->wID;
1191 dis.itemData = lpitem->dwItemData;
1193 if (lpitem->fState & MF_CHECKED) dis.itemState |= ODS_CHECKED;
1194 if (lpitem->fState & MF_GRAYED) dis.itemState |= ODS_GRAYED|ODS_DISABLED;
1195 if (lpitem->fState & MF_HILITE) dis.itemState |= ODS_SELECTED;
1196 dis.itemAction = odaction; /* ODA_DRAWENTIRE | ODA_SELECT | ODA_FOCUS; */
1197 dis.hwndItem = (HWND)hmenu;
1199 dis.rcItem = lpitem->rect;
1200 TRACE("Ownerdraw: owner=%p itemID=%d, itemState=%d, itemAction=%d, "
1201 "hwndItem=%p, hdc=%p, rcItem={%ld,%ld,%ld,%ld}\n", hwndOwner,
1202 dis.itemID, dis.itemState, dis.itemAction, dis.hwndItem,
1203 dis.hDC, dis.rcItem.left, dis.rcItem.top, dis.rcItem.right,
1205 SendMessageW( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&dis );
1206 /* Fall through to draw popup-menu arrow */
1209 TRACE("rect={%ld,%ld,%ld,%ld}\n", lpitem->rect.left, lpitem->rect.top,
1210 lpitem->rect.right,lpitem->rect.bottom);
1212 if (menuBar && (lpitem->fType & MF_SEPARATOR)) return;
1214 rect = lpitem->rect;
1216 if (!(lpitem->fType & MF_OWNERDRAW))
1218 if (lpitem->fState & MF_HILITE)
1222 InflateRect (&rect, -1, -1);
1223 FillRect(hdc, &rect, GetSysColorBrush(COLOR_MENUHILIGHT));
1224 InflateRect (&rect, 1, 1);
1225 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1230 DrawEdge(hdc, &rect, BDR_SUNKENOUTER, BF_RECT);
1232 FillRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1236 FillRect( hdc, &rect, GetSysColorBrush(bkgnd) );
1239 SetBkMode( hdc, TRANSPARENT );
1241 if (!(lpitem->fType & MF_OWNERDRAW))
1245 /* vertical separator */
1246 if (!menuBar && (lpitem->fType & MF_MENUBARBREAK))
1250 rc.bottom = height - 3;
1253 oldPen = SelectObject( hdc, SYSCOLOR_GetPen(COLOR_BTNSHADOW) );
1254 MoveToEx( hdc, rc.left, rc.top, NULL );
1255 LineTo( hdc, rc.left, rc.bottom );
1256 SelectObject( hdc, oldPen );
1259 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_LEFT);
1262 /* horizontal separator */
1263 if (lpitem->fType & MF_SEPARATOR)
1268 rc.top += SEPARATOR_HEIGHT / 2;
1271 oldPen = SelectObject( hdc, SYSCOLOR_GetPen(COLOR_BTNSHADOW) );
1272 MoveToEx( hdc, rc.left, rc.top, NULL );
1273 LineTo( hdc, rc.right, rc.top );
1274 SelectObject( hdc, oldPen );
1277 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_TOP);
1282 /* helper lines for debugging */
1283 /* FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
1284 SelectObject( hdc, SYSCOLOR_GetPen(COLOR_WINDOWFRAME) );
1285 MoveToEx( hdc, rect.left, (rect.top + rect.bottom)/2, NULL );
1286 LineTo( hdc, rect.right, (rect.top + rect.bottom)/2 );
1292 INT y = rect.top + rect.bottom;
1293 UINT check_bitmap_width = GetSystemMetrics( SM_CXMENUCHECK );
1294 UINT check_bitmap_height = GetSystemMetrics( SM_CYMENUCHECK );
1295 UINT arrow_bitmap_width, arrow_bitmap_height;
1298 GetObjectW( get_arrow_bitmap(), sizeof(bmp), &bmp );
1299 arrow_bitmap_width = bmp.bmWidth;
1300 arrow_bitmap_height = bmp.bmHeight;
1302 if (!(lpitem->fType & MF_OWNERDRAW))
1306 /* New style MIIM_BITMAP */
1307 if (lpitem->hbmpItem)
1309 POPUPMENU *menu = MENU_GetMenu(hmenu);
1310 if (menu->dwStyle & MNS_CHECKORBMP)
1311 rc.left += menu->maxBmpSize.cx - check_bitmap_width;
1313 rc.left += menu->maxBmpSize.cx;
1315 /* Draw the check mark
1318 * Custom checkmark bitmaps are monochrome but not always 1bpp.
1320 bm = (lpitem->fState & MF_CHECKED) ? lpitem->hCheckBit : lpitem->hUnCheckBit;
1321 if (bm) /* we have a custom bitmap */
1323 HDC hdcMem = CreateCompatibleDC( hdc );
1324 SelectObject( hdcMem, bm );
1325 BitBlt( hdc, rc.left, (y - check_bitmap_height) / 2,
1326 check_bitmap_width, check_bitmap_height,
1327 hdcMem, 0, 0, SRCCOPY );
1330 else if (lpitem->fState & MF_CHECKED) /* standard bitmaps */
1333 HBITMAP bm = CreateBitmap( check_bitmap_width, check_bitmap_height, 1, 1, NULL );
1334 HDC hdcMem = CreateCompatibleDC( hdc );
1335 SelectObject( hdcMem, bm );
1336 SetRect( &r, 0, 0, check_bitmap_width, check_bitmap_height );
1337 DrawFrameControl( hdcMem, &r, DFC_MENU,
1338 (lpitem->fType & MFT_RADIOCHECK) ?
1339 DFCS_MENUBULLET : DFCS_MENUCHECK );
1340 BitBlt( hdc, rc.left, (y - r.bottom) / 2, r.right, r.bottom,
1341 hdcMem, 0, 0, SRCCOPY );
1345 /* New style MIIM_BITMAP */
1346 if (lpitem->hbmpItem)
1348 HBITMAP hbm = lpitem->hbmpItem;
1350 if (hbm == HBMMENU_CALLBACK)
1352 DRAWITEMSTRUCT drawItem;
1354 drawItem.CtlType = ODT_MENU;
1356 drawItem.itemID = lpitem->wID;
1357 drawItem.itemAction = odaction;
1358 drawItem.itemState = (lpitem->fState & MF_CHECKED)?ODS_CHECKED:0;
1359 drawItem.itemState |= (lpitem->fState & MF_DEFAULT)?ODS_DEFAULT:0;
1360 drawItem.itemState |= (lpitem->fState & MF_DISABLED)?ODS_DISABLED:0;
1361 drawItem.itemState |= (lpitem->fState & MF_GRAYED)?ODS_GRAYED|ODS_DISABLED:0;
1362 drawItem.itemState |= (lpitem->fState & MF_HILITE)?ODS_SELECTED:0;
1363 drawItem.hwndItem = (HWND)hmenu;
1365 drawItem.rcItem = lpitem->rect;
1366 drawItem.itemData = lpitem->dwItemData;
1367 /* some applications make this assumption on the DC's origin */
1368 SetViewportOrgEx( hdc, lpitem->rect.left, lpitem->rect.top, &origorg);
1369 OffsetRect( &drawItem.rcItem, - lpitem->rect.left, - lpitem->rect.top);
1370 SendMessageW( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&drawItem);
1371 SetViewportOrgEx( hdc, origorg.x, origorg.y, NULL);
1374 MENU_DrawBitmapItem(hdc, lpitem, &rect, FALSE, TRUE);
1379 /* Draw the popup-menu arrow */
1380 if (lpitem->fType & MF_POPUP)
1382 HDC hdcMem = CreateCompatibleDC( hdc );
1383 HBITMAP hOrigBitmap;
1385 hOrigBitmap = SelectObject( hdcMem, get_arrow_bitmap() );
1386 BitBlt( hdc, rect.right - arrow_bitmap_width - 1,
1387 (y - arrow_bitmap_height) / 2,
1388 arrow_bitmap_width, arrow_bitmap_height,
1389 hdcMem, 0, 0, SRCCOPY );
1390 SelectObject( hdcMem, hOrigBitmap );
1394 rect.left += check_bitmap_width;
1395 rect.right -= arrow_bitmap_width;
1398 /* Done for owner-drawn */
1399 if (lpitem->fType & MF_OWNERDRAW)
1402 /* Draw the item text or bitmap */
1403 if (IS_BITMAP_ITEM(lpitem->fType))
1405 MENU_DrawBitmapItem( hdc, lpitem, &rect, menuBar, FALSE);
1408 /* No bitmap - process text if present */
1409 else if (IS_STRING_ITEM(lpitem->fType))
1414 UINT uFormat = (menuBar) ?
1415 DT_CENTER | DT_VCENTER | DT_SINGLELINE :
1416 DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1418 if ( lpitem->fState & MFS_DEFAULT )
1420 hfontOld = SelectObject( hdc, get_menu_font(TRUE) );
1425 rect.left += MENU_BAR_ITEMS_SPACE / 2;
1426 rect.right -= MENU_BAR_ITEMS_SPACE / 2;
1429 for (i = 0; lpitem->text[i]; i++)
1430 if ((lpitem->text[i] == '\t') || (lpitem->text[i] == '\b'))
1433 if(lpitem->fState & MF_GRAYED)
1435 if (!(lpitem->fState & MF_HILITE) )
1437 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1438 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1439 DrawTextW( hdc, lpitem->text, i, &rect, uFormat );
1440 --rect.left; --rect.top; --rect.right; --rect.bottom;
1442 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1445 DrawTextW( hdc, lpitem->text, i, &rect, uFormat);
1447 /* paint the shortcut text */
1448 if (!menuBar && lpitem->text[i]) /* There's a tab or flush-right char */
1450 if (lpitem->text[i] == '\t')
1452 rect.left = lpitem->xTab;
1453 uFormat = DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1457 uFormat = DT_RIGHT | DT_VCENTER | DT_SINGLELINE;
1460 if(lpitem->fState & MF_GRAYED)
1462 if (!(lpitem->fState & MF_HILITE) )
1464 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1465 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1466 DrawTextW( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1467 --rect.left; --rect.top; --rect.right; --rect.bottom;
1469 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1471 DrawTextW( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1475 SelectObject (hdc, hfontOld);
1480 /***********************************************************************
1481 * MENU_DrawPopupMenu
1483 * Paint a popup menu.
1485 static void MENU_DrawPopupMenu( HWND hwnd, HDC hdc, HMENU hmenu )
1487 HBRUSH hPrevBrush = 0;
1490 TRACE("wnd=%p dc=%p menu=%p\n", hwnd, hdc, hmenu);
1492 GetClientRect( hwnd, &rect );
1494 if((hPrevBrush = SelectObject( hdc, GetSysColorBrush(COLOR_MENU) ))
1495 && (SelectObject( hdc, get_menu_font(FALSE))))
1499 Rectangle( hdc, rect.left, rect.top, rect.right, rect.bottom );
1501 hPrevPen = SelectObject( hdc, GetStockObject( NULL_PEN ) );
1505 BOOL flat_menu = FALSE;
1507 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
1509 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_BTNSHADOW));
1511 DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT);
1513 /* draw menu items */
1515 menu = MENU_GetMenu( hmenu );
1516 if (menu && menu->nItems)
1521 for (u = menu->nItems, item = menu->items; u > 0; u--, item++)
1522 MENU_DrawMenuItem( hwnd, hmenu, menu->hwndOwner, hdc, item,
1523 menu->Height, FALSE, ODA_DRAWENTIRE );
1528 SelectObject( hdc, hPrevBrush );
1533 /***********************************************************************
1536 * Paint a menu bar. Returns the height of the menu bar.
1537 * called from [windows/nonclient.c]
1539 UINT MENU_DrawMenuBar( HDC hDC, LPRECT lprect, HWND hwnd,
1544 HMENU hMenu = GetMenu(hwnd);
1546 lppop = MENU_GetMenu( hMenu );
1547 if (lppop == NULL || lprect == NULL)
1549 return GetSystemMetrics(SM_CYMENU);
1554 hfontOld = SelectObject( hDC, get_menu_font(FALSE));
1556 if (lppop->Height == 0)
1557 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
1559 lprect->bottom = lprect->top + lppop->Height;
1561 if (hfontOld) SelectObject( hDC, hfontOld);
1562 return lppop->Height;
1565 return DrawMenuBarTemp(hwnd, hDC, lprect, hMenu, NULL);
1569 /***********************************************************************
1572 * Display a popup menu.
1574 static BOOL MENU_ShowPopup( HWND hwndOwner, HMENU hmenu, UINT id,
1575 INT x, INT y, INT xanchor, INT yanchor )
1580 TRACE("owner=%p hmenu=%p id=0x%04x x=0x%04x y=0x%04x xa=0x%04x ya=0x%04x\n",
1581 hwndOwner, hmenu, id, x, y, xanchor, yanchor);
1583 if (!(menu = MENU_GetMenu( hmenu ))) return FALSE;
1584 if (menu->FocusedItem != NO_SELECTED_ITEM)
1586 menu->items[menu->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1587 menu->FocusedItem = NO_SELECTED_ITEM;
1590 /* store the owner for DrawItem */
1591 menu->hwndOwner = hwndOwner;
1593 MENU_PopupMenuCalcSize( menu, hwndOwner );
1595 /* adjust popup menu pos so that it fits within the desktop */
1597 width = menu->Width + GetSystemMetrics(SM_CXBORDER);
1598 height = menu->Height + GetSystemMetrics(SM_CYBORDER);
1600 if( x + width > GetSystemMetrics(SM_CXSCREEN ))
1603 x -= width - xanchor;
1604 if( x + width > GetSystemMetrics(SM_CXSCREEN))
1605 x = GetSystemMetrics(SM_CXSCREEN) - width;
1609 if( y + height > GetSystemMetrics(SM_CYSCREEN ))
1612 y -= height + yanchor;
1613 if( y + height > GetSystemMetrics(SM_CYSCREEN ))
1614 y = GetSystemMetrics(SM_CYSCREEN) - height;
1618 /* NOTE: In Windows, top menu popup is not owned. */
1619 menu->hWnd = CreateWindowExW( 0, POPUPMENU_CLASS_ATOMW, NULL,
1620 WS_POPUP, x, y, width, height,
1621 hwndOwner, 0, (HINSTANCE)GetWindowLongPtrW(hwndOwner, GWLP_HINSTANCE),
1623 if( !menu->hWnd ) return FALSE;
1624 if (!top_popup) top_popup = menu->hWnd;
1626 /* Display the window */
1628 SetWindowPos( menu->hWnd, HWND_TOP, 0, 0, 0, 0,
1629 SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1630 UpdateWindow( menu->hWnd );
1635 /***********************************************************************
1638 static void MENU_SelectItem( HWND hwndOwner, HMENU hmenu, UINT wIndex,
1639 BOOL sendMenuSelect, HMENU topmenu )
1644 TRACE("owner=%p menu=%p index=0x%04x select=0x%04x\n", hwndOwner, hmenu, wIndex, sendMenuSelect);
1646 lppop = MENU_GetMenu( hmenu );
1647 if ((!lppop) || (!lppop->nItems) || (!lppop->hWnd)) return;
1649 if (lppop->FocusedItem == wIndex) return;
1650 if (lppop->wFlags & MF_POPUP) hdc = GetDC( lppop->hWnd );
1651 else hdc = GetDCEx( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1652 if (!top_popup) top_popup = lppop->hWnd;
1654 SelectObject( hdc, get_menu_font(FALSE));
1656 /* Clear previous highlighted item */
1657 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1659 lppop->items[lppop->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1660 MENU_DrawMenuItem(lppop->hWnd, hmenu, hwndOwner, hdc,&lppop->items[lppop->FocusedItem],
1661 lppop->Height, !(lppop->wFlags & MF_POPUP),
1665 /* Highlight new item (if any) */
1666 lppop->FocusedItem = wIndex;
1667 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1669 if(!(lppop->items[wIndex].fType & MF_SEPARATOR)) {
1670 lppop->items[wIndex].fState |= MF_HILITE;
1671 MENU_DrawMenuItem( lppop->hWnd, hmenu, hwndOwner, hdc,
1672 &lppop->items[wIndex], lppop->Height,
1673 !(lppop->wFlags & MF_POPUP), ODA_SELECT );
1677 MENUITEM *ip = &lppop->items[lppop->FocusedItem];
1678 SendMessageW( hwndOwner, WM_MENUSELECT,
1679 MAKELONG(ip->fType & MF_POPUP ? wIndex: ip->wID,
1680 ip->fType | ip->fState |
1681 (lppop->wFlags & MF_SYSMENU)), (LPARAM)hmenu);
1684 else if (sendMenuSelect) {
1687 if((pos=MENU_FindSubMenu(&topmenu, hmenu))!=NO_SELECTED_ITEM){
1688 POPUPMENU *ptm = MENU_GetMenu( topmenu );
1689 MENUITEM *ip = &ptm->items[pos];
1690 SendMessageW( hwndOwner, WM_MENUSELECT, MAKELONG(pos,
1691 ip->fType | ip->fState |
1692 (ptm->wFlags & MF_SYSMENU)), (LPARAM)topmenu);
1696 ReleaseDC( lppop->hWnd, hdc );
1700 /***********************************************************************
1701 * MENU_MoveSelection
1703 * Moves currently selected item according to the offset parameter.
1704 * If there is no selection then it should select the last item if
1705 * offset is ITEM_PREV or the first item if offset is ITEM_NEXT.
1707 static void MENU_MoveSelection( HWND hwndOwner, HMENU hmenu, INT offset )
1712 TRACE("hwnd=%p hmenu=%p off=0x%04x\n", hwndOwner, hmenu, offset);
1714 menu = MENU_GetMenu( hmenu );
1715 if ((!menu) || (!menu->items)) return;
1717 if ( menu->FocusedItem != NO_SELECTED_ITEM )
1719 if( menu->nItems == 1 ) return; else
1720 for (i = menu->FocusedItem + offset ; i >= 0 && i < menu->nItems
1722 if (!(menu->items[i].fType & MF_SEPARATOR))
1724 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
1729 for ( i = (offset > 0) ? 0 : menu->nItems - 1;
1730 i >= 0 && i < menu->nItems ; i += offset)
1731 if (!(menu->items[i].fType & MF_SEPARATOR))
1733 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
1739 /**********************************************************************
1742 * Set an item's flags, id and text ptr. Called by InsertMenu() and
1745 static BOOL MENU_SetItemData( MENUITEM *item, UINT flags, UINT_PTR id,
1748 LPWSTR prevText = IS_STRING_ITEM(item->fType) ? item->text : NULL;
1750 debug_print_menuitem("MENU_SetItemData from: ", item, "");
1751 TRACE("flags=%x str=%p\n", flags, str);
1753 if (IS_STRING_ITEM(flags))
1757 flags |= MF_SEPARATOR;
1763 /* Item beginning with a backspace is a help item */
1769 if (!(text = HeapAlloc( GetProcessHeap(), 0, (strlenW(str)+1) * sizeof(WCHAR) )))
1771 strcpyW( text, str );
1775 else if (IS_BITMAP_ITEM(flags))
1776 item->text = (LPWSTR)HBITMAP_32(LOWORD(str));
1777 else item->text = NULL;
1779 if (flags & MF_OWNERDRAW)
1780 item->dwItemData = (DWORD_PTR)str;
1782 item->dwItemData = 0;
1784 if ((item->fType & MF_POPUP) && (flags & MF_POPUP) && (item->hSubMenu != (HMENU)id) )
1785 DestroyMenu( item->hSubMenu ); /* ModifyMenu() spec */
1787 if (flags & MF_POPUP)
1789 POPUPMENU *menu = MENU_GetMenu((HMENU)id);
1790 if (menu) menu->wFlags |= MF_POPUP;
1802 if (flags & MF_POPUP) item->hSubMenu = (HMENU)id;
1804 if ((item->fType & MF_POPUP) && !(flags & MF_POPUP) )
1805 flags |= MF_POPUP; /* keep popup */
1807 item->fType = flags & TYPE_MASK;
1808 item->fState = (flags & STATE_MASK) &
1809 ~(MF_HILITE | MF_MOUSESELECT | MF_BYPOSITION);
1812 /* Don't call SetRectEmpty here! */
1815 HeapFree( GetProcessHeap(), 0, prevText );
1817 debug_print_menuitem("MENU_SetItemData to : ", item, "");
1822 /**********************************************************************
1825 * Insert (allocate) a new item into a menu.
1827 static MENUITEM *MENU_InsertItem( HMENU hMenu, UINT pos, UINT flags )
1832 if (!(menu = MENU_GetMenu(hMenu)))
1835 /* Find where to insert new item */
1837 if (flags & MF_BYPOSITION) {
1838 if (pos > menu->nItems)
1841 if (!MENU_FindItem( &hMenu, &pos, flags ))
1844 if (!(menu = MENU_GetMenu( hMenu )))
1849 /* Create new items array */
1851 newItems = HeapAlloc( GetProcessHeap(), 0, sizeof(MENUITEM) * (menu->nItems+1) );
1854 WARN("allocation failed\n" );
1857 if (menu->nItems > 0)
1859 /* Copy the old array into the new one */
1860 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
1861 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
1862 (menu->nItems-pos)*sizeof(MENUITEM) );
1863 HeapFree( GetProcessHeap(), 0, menu->items );
1865 menu->items = newItems;
1867 memset( &newItems[pos], 0, sizeof(*newItems) );
1868 menu->Height = 0; /* force size recalculate */
1869 return &newItems[pos];
1873 /**********************************************************************
1874 * MENU_ParseResource
1876 * Parse a standard menu resource and add items to the menu.
1877 * Return a pointer to the end of the resource.
1879 * NOTE: flags is equivalent to the mtOption field
1881 static LPCSTR MENU_ParseResource( LPCSTR res, HMENU hMenu, BOOL unicode )
1888 flags = GET_WORD(res);
1889 res += sizeof(WORD);
1890 if (!(flags & MF_POPUP))
1893 res += sizeof(WORD);
1896 if (!unicode) res += strlen(str) + 1;
1897 else res += (strlenW((LPCWSTR)str) + 1) * sizeof(WCHAR);
1898 if (flags & MF_POPUP)
1900 HMENU hSubMenu = CreatePopupMenu();
1901 if (!hSubMenu) return NULL;
1902 if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
1904 if (!unicode) AppendMenuA( hMenu, flags, (UINT_PTR)hSubMenu, str );
1905 else AppendMenuW( hMenu, flags, (UINT_PTR)hSubMenu, (LPCWSTR)str );
1907 else /* Not a popup */
1909 if (!unicode) AppendMenuA( hMenu, flags, id, *str ? str : NULL );
1910 else AppendMenuW( hMenu, flags, id,
1911 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
1913 } while (!(flags & MF_END));
1918 /**********************************************************************
1919 * MENUEX_ParseResource
1921 * Parse an extended menu resource and add items to the menu.
1922 * Return a pointer to the end of the resource.
1924 static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU hMenu)
1930 mii.cbSize = sizeof(mii);
1931 mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
1932 mii.fType = GET_DWORD(res);
1933 res += sizeof(DWORD);
1934 mii.fState = GET_DWORD(res);
1935 res += sizeof(DWORD);
1936 mii.wID = GET_DWORD(res);
1937 res += sizeof(DWORD);
1938 resinfo = GET_WORD(res); /* FIXME: for 16-bit apps this is a byte. */
1939 res += sizeof(WORD);
1940 /* Align the text on a word boundary. */
1941 res += (~((UINT_PTR)res - 1)) & 1;
1942 mii.dwTypeData = (LPWSTR) res;
1943 res += (1 + strlenW(mii.dwTypeData)) * sizeof(WCHAR);
1944 /* Align the following fields on a dword boundary. */
1945 res += (~((UINT_PTR)res - 1)) & 3;
1947 TRACE("Menu item: [%08x,%08x,%04x,%04x,%s]\n",
1948 mii.fType, mii.fState, mii.wID, resinfo, debugstr_w(mii.dwTypeData));
1950 if (resinfo & 1) { /* Pop-up? */
1951 /* DWORD helpid = GET_DWORD(res); FIXME: use this. */
1952 res += sizeof(DWORD);
1953 mii.hSubMenu = CreatePopupMenu();
1956 if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
1957 DestroyMenu(mii.hSubMenu);
1960 mii.fMask |= MIIM_SUBMENU;
1961 mii.fType |= MF_POPUP;
1963 else if(!*mii.dwTypeData && !(mii.fType & MF_SEPARATOR))
1965 WARN("Converting NULL menu item %04x, type %04x to SEPARATOR\n",
1966 mii.wID, mii.fType);
1967 mii.fType |= MF_SEPARATOR;
1969 InsertMenuItemW(hMenu, -1, MF_BYPOSITION, &mii);
1970 } while (!(resinfo & MF_END));
1975 /***********************************************************************
1978 * Return the handle of the selected sub-popup menu (if any).
1980 static HMENU MENU_GetSubPopup( HMENU hmenu )
1985 menu = MENU_GetMenu( hmenu );
1987 if ((!menu) || (menu->FocusedItem == NO_SELECTED_ITEM)) return 0;
1989 item = &menu->items[menu->FocusedItem];
1990 if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
1991 return item->hSubMenu;
1996 /***********************************************************************
1997 * MENU_HideSubPopups
1999 * Hide the sub-popup menus of this menu.
2001 static void MENU_HideSubPopups( HWND hwndOwner, HMENU hmenu,
2002 BOOL sendMenuSelect )
2004 POPUPMENU *menu = MENU_GetMenu( hmenu );
2006 TRACE("owner=%p hmenu=%p 0x%04x\n", hwndOwner, hmenu, sendMenuSelect);
2008 if (menu && top_popup)
2014 if (menu->FocusedItem != NO_SELECTED_ITEM)
2016 item = &menu->items[menu->FocusedItem];
2017 if (!(item->fType & MF_POPUP) ||
2018 !(item->fState & MF_MOUSESELECT)) return;
2019 item->fState &= ~MF_MOUSESELECT;
2020 hsubmenu = item->hSubMenu;
2023 submenu = MENU_GetMenu( hsubmenu );
2024 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE );
2025 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect, 0 );
2026 DestroyWindow( submenu->hWnd );
2032 /***********************************************************************
2035 * Display the sub-menu of the selected item of this menu.
2036 * Return the handle of the submenu, or hmenu if no submenu to display.
2038 static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu,
2039 BOOL selectFirst, UINT wFlags )
2046 TRACE("owner=%p hmenu=%p 0x%04x\n", hwndOwner, hmenu, selectFirst);
2048 if (!(menu = MENU_GetMenu( hmenu ))) return hmenu;
2050 if (menu->FocusedItem == NO_SELECTED_ITEM) return hmenu;
2052 item = &menu->items[menu->FocusedItem];
2053 if (!(item->fType & MF_POPUP) || (item->fState & (MF_GRAYED | MF_DISABLED)))
2056 /* message must be sent before using item,
2057 because nearly everything may be changed by the application ! */
2059 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
2060 if (!(wFlags & TPM_NONOTIFY))
2061 SendMessageW( hwndOwner, WM_INITMENUPOPUP, (WPARAM)item->hSubMenu,
2062 MAKELONG( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
2064 item = &menu->items[menu->FocusedItem];
2067 /* correct item if modified as a reaction to WM_INITMENUPOPUP message */
2068 if (!(item->fState & MF_HILITE))
2070 if (menu->wFlags & MF_POPUP) hdc = GetDC( menu->hWnd );
2071 else hdc = GetDCEx( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
2073 SelectObject( hdc, get_menu_font(FALSE));
2075 item->fState |= MF_HILITE;
2076 MENU_DrawMenuItem( menu->hWnd, hmenu, hwndOwner, hdc, item, menu->Height, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE );
2077 ReleaseDC( menu->hWnd, hdc );
2079 if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
2082 item->fState |= MF_MOUSESELECT;
2084 if (IS_SYSTEM_MENU(menu))
2086 MENU_InitSysMenuPopup(item->hSubMenu,
2087 GetWindowLongW( menu->hWnd, GWL_STYLE ),
2088 GetClassLongW( menu->hWnd, GCL_STYLE));
2090 NC_GetSysPopupPos( menu->hWnd, &rect );
2091 rect.top = rect.bottom;
2092 rect.right = GetSystemMetrics(SM_CXSIZE);
2093 rect.bottom = GetSystemMetrics(SM_CYSIZE);
2097 GetWindowRect( menu->hWnd, &rect );
2098 if (menu->wFlags & MF_POPUP)
2100 rect.left += item->rect.right - GetSystemMetrics(SM_CXBORDER);
2101 rect.top += item->rect.top;
2102 rect.right = item->rect.left - item->rect.right + GetSystemMetrics(SM_CXBORDER);
2103 rect.bottom = item->rect.top - item->rect.bottom;
2107 rect.left += item->rect.left;
2108 rect.top += item->rect.bottom;
2109 rect.right = item->rect.right - item->rect.left;
2110 rect.bottom = item->rect.bottom - item->rect.top;
2114 MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem,
2115 rect.left, rect.top, rect.right, rect.bottom );
2117 MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
2118 return item->hSubMenu;
2123 /**********************************************************************
2126 HWND MENU_IsMenuActive(void)
2131 /***********************************************************************
2134 * Walks menu chain trying to find a menu pt maps to.
2136 static HMENU MENU_PtMenu( HMENU hMenu, POINT pt )
2138 POPUPMENU *menu = MENU_GetMenu( hMenu );
2139 UINT item = menu->FocusedItem;
2142 /* try subpopup first (if any) */
2143 ret = (item != NO_SELECTED_ITEM &&
2144 (menu->items[item].fType & MF_POPUP) &&
2145 (menu->items[item].fState & MF_MOUSESELECT))
2146 ? MENU_PtMenu(menu->items[item].hSubMenu, pt) : 0;
2148 if (!ret) /* check the current window (avoiding WM_HITTEST) */
2150 INT ht = NC_HandleNCHitTest( menu->hWnd, pt );
2151 if( menu->wFlags & MF_POPUP )
2153 if (ht != HTNOWHERE && ht != HTERROR) ret = hMenu;
2155 else if (ht == HTSYSMENU)
2156 ret = get_win_sys_menu( menu->hWnd );
2157 else if (ht == HTMENU)
2158 ret = GetMenu( menu->hWnd );
2163 /***********************************************************************
2164 * MENU_ExecFocusedItem
2166 * Execute a menu item (for instance when user pressed Enter).
2167 * Return the wID of the executed item. Otherwise, -1 indicating
2168 * that no menu item was executed;
2169 * Have to receive the flags for the TrackPopupMenu options to avoid
2170 * sending unwanted message.
2173 static INT MENU_ExecFocusedItem( MTRACKER* pmt, HMENU hMenu, UINT wFlags )
2176 POPUPMENU *menu = MENU_GetMenu( hMenu );
2178 TRACE("%p hmenu=%p\n", pmt, hMenu);
2180 if (!menu || !menu->nItems ||
2181 (menu->FocusedItem == NO_SELECTED_ITEM)) return -1;
2183 item = &menu->items[menu->FocusedItem];
2185 TRACE("%p %08x %p\n", hMenu, item->wID, item->hSubMenu);
2187 if (!(item->fType & MF_POPUP))
2189 if (!(item->fState & (MF_GRAYED | MF_DISABLED)) && !(item->fType & MF_SEPARATOR))
2191 /* If TPM_RETURNCMD is set you return the id, but
2192 do not send a message to the owner */
2193 if(!(wFlags & TPM_RETURNCMD))
2195 if( menu->wFlags & MF_SYSMENU )
2196 PostMessageW( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
2197 MAKELPARAM((INT16)pmt->pt.x, (INT16)pmt->pt.y) );
2199 PostMessageW( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
2205 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hMenu, TRUE, wFlags);
2210 /***********************************************************************
2211 * MENU_SwitchTracking
2213 * Helper function for menu navigation routines.
2215 static void MENU_SwitchTracking( MTRACKER* pmt, HMENU hPtMenu, UINT id )
2217 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2218 POPUPMENU *topmenu = MENU_GetMenu( pmt->hTopMenu );
2220 TRACE("%p hmenu=%p 0x%04x\n", pmt, hPtMenu, id);
2222 if( pmt->hTopMenu != hPtMenu &&
2223 !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
2225 /* both are top level menus (system and menu-bar) */
2226 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2227 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2228 pmt->hTopMenu = hPtMenu;
2230 else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE );
2231 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE, 0 );
2235 /***********************************************************************
2238 * Return TRUE if we can go on with menu tracking.
2240 static BOOL MENU_ButtonDown( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2242 TRACE("%p hPtMenu=%p\n", pmt, hPtMenu);
2247 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2250 if( IS_SYSTEM_MENU(ptmenu) )
2251 item = ptmenu->items;
2253 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2257 if( ptmenu->FocusedItem != id )
2258 MENU_SwitchTracking( pmt, hPtMenu, id );
2260 /* If the popup menu is not already "popped" */
2261 if(!(item->fState & MF_MOUSESELECT ))
2263 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE, wFlags );
2268 /* Else the click was on the menu bar, finish the tracking */
2273 /***********************************************************************
2276 * Return the value of MENU_ExecFocusedItem if
2277 * the selected item was not a popup. Else open the popup.
2278 * A -1 return value indicates that we go on with menu tracking.
2281 static INT MENU_ButtonUp( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags)
2283 TRACE("%p hmenu=%p\n", pmt, hPtMenu);
2288 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2291 if( IS_SYSTEM_MENU(ptmenu) )
2292 item = ptmenu->items;
2294 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2296 if( item && (ptmenu->FocusedItem == id ))
2298 if( !(item->fType & MF_POPUP) )
2299 return MENU_ExecFocusedItem( pmt, hPtMenu, wFlags);
2301 /* If we are dealing with the top-level menu */
2302 /* and this is a click on an already "popped" item: */
2303 /* Stop the menu tracking and close the opened submenus */
2304 if((pmt->hTopMenu == hPtMenu) && ptmenu->bTimeToHide)
2307 ptmenu->bTimeToHide = TRUE;
2313 /***********************************************************************
2316 * Return TRUE if we can go on with menu tracking.
2318 static BOOL MENU_MouseMove( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2320 UINT id = NO_SELECTED_ITEM;
2321 POPUPMENU *ptmenu = NULL;
2325 ptmenu = MENU_GetMenu( hPtMenu );
2326 if( IS_SYSTEM_MENU(ptmenu) )
2329 MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2332 if( id == NO_SELECTED_ITEM )
2334 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2335 NO_SELECTED_ITEM, TRUE, pmt->hTopMenu);
2338 else if( ptmenu->FocusedItem != id )
2340 MENU_SwitchTracking( pmt, hPtMenu, id );
2341 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hPtMenu, FALSE, wFlags);
2347 /***********************************************************************
2350 static void MENU_SetCapture( HWND hwnd )
2354 SERVER_START_REQ( set_capture_window )
2357 req->flags = CAPTURE_MENU;
2358 if (!wine_server_call_err( req ))
2360 previous = reply->previous;
2361 hwnd = reply->full_handle;
2366 if (previous && previous != hwnd)
2367 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
2371 /***********************************************************************
2374 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2376 static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT vk )
2378 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2380 if( (vk == VK_LEFT && menu->FocusedItem == 0 ) ||
2381 (vk == VK_RIGHT && menu->FocusedItem == menu->nItems - 1))
2383 MDINEXTMENU next_menu;
2388 next_menu.hmenuIn = (IS_SYSTEM_MENU(menu)) ? GetSubMenu(pmt->hTopMenu,0) : pmt->hTopMenu;
2389 next_menu.hmenuNext = 0;
2390 next_menu.hwndNext = 0;
2391 SendMessageW( pmt->hOwnerWnd, WM_NEXTMENU, vk, (LPARAM)&next_menu );
2393 TRACE("%p [%p] -> %p [%p]\n",
2394 pmt->hCurrentMenu, pmt->hOwnerWnd, next_menu.hmenuNext, next_menu.hwndNext );
2396 if (!next_menu.hmenuNext || !next_menu.hwndNext)
2398 DWORD style = GetWindowLongW( pmt->hOwnerWnd, GWL_STYLE );
2399 hNewWnd = pmt->hOwnerWnd;
2400 if( IS_SYSTEM_MENU(menu) )
2402 /* switch to the menu bar */
2404 if(style & WS_CHILD || !(hNewMenu = GetMenu(hNewWnd))) return FALSE;
2408 menu = MENU_GetMenu( hNewMenu );
2409 id = menu->nItems - 1;
2412 else if (style & WS_SYSMENU )
2414 /* switch to the system menu */
2415 hNewMenu = get_win_sys_menu( hNewWnd );
2419 else /* application returned a new menu to switch to */
2421 hNewMenu = next_menu.hmenuNext;
2422 hNewWnd = WIN_GetFullHandle( next_menu.hwndNext );
2424 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2426 DWORD style = GetWindowLongW( hNewWnd, GWL_STYLE );
2428 if (style & WS_SYSMENU &&
2429 GetSubMenu(get_win_sys_menu(hNewWnd), 0) == hNewMenu )
2431 /* get the real system menu */
2432 hNewMenu = get_win_sys_menu(hNewWnd);
2434 else if (style & WS_CHILD || GetMenu(hNewWnd) != hNewMenu )
2436 /* FIXME: Not sure what to do here;
2437 * perhaps try to track hNewMenu as a popup? */
2439 TRACE(" -- got confused.\n");
2446 if( hNewMenu != pmt->hTopMenu )
2448 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM,
2450 if( pmt->hCurrentMenu != pmt->hTopMenu )
2451 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2454 if( hNewWnd != pmt->hOwnerWnd )
2456 pmt->hOwnerWnd = hNewWnd;
2457 MENU_SetCapture( pmt->hOwnerWnd );
2460 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2461 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE, 0 );
2468 /***********************************************************************
2471 * The idea is not to show the popup if the next input message is
2472 * going to hide it anyway.
2474 static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT16 uMsg )
2478 msg.hwnd = pmt->hOwnerWnd;
2480 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2481 pmt->trackFlags |= TF_SKIPREMOVE;
2486 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2487 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2489 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2490 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2491 if( msg.message == WM_KEYDOWN &&
2492 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2494 pmt->trackFlags |= TF_SUSPENDPOPUP;
2501 /* failures go through this */
2502 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2506 /***********************************************************************
2509 * Handle a VK_ESCAPE key event in a menu.
2511 static BOOL MENU_KeyEscape(MTRACKER* pmt, UINT wFlags)
2513 BOOL bEndMenu = TRUE;
2515 if (pmt->hCurrentMenu != pmt->hTopMenu)
2517 POPUPMENU *menu = MENU_GetMenu(pmt->hCurrentMenu);
2519 if (menu->wFlags & MF_POPUP)
2521 HMENU hmenutmp, hmenuprev;
2523 hmenuprev = hmenutmp = pmt->hTopMenu;
2525 /* close topmost popup */
2526 while (hmenutmp != pmt->hCurrentMenu)
2528 hmenuprev = hmenutmp;
2529 hmenutmp = MENU_GetSubPopup( hmenuprev );
2532 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2533 pmt->hCurrentMenu = hmenuprev;
2541 /***********************************************************************
2544 * Handle a VK_LEFT key event in a menu.
2546 static void MENU_KeyLeft( MTRACKER* pmt, UINT wFlags )
2549 HMENU hmenutmp, hmenuprev;
2552 hmenuprev = hmenutmp = pmt->hTopMenu;
2553 menu = MENU_GetMenu( hmenutmp );
2555 /* Try to move 1 column left (if possible) */
2556 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2557 NO_SELECTED_ITEM ) {
2559 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2564 /* close topmost popup */
2565 while (hmenutmp != pmt->hCurrentMenu)
2567 hmenuprev = hmenutmp;
2568 hmenutmp = MENU_GetSubPopup( hmenuprev );
2571 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE );
2572 pmt->hCurrentMenu = hmenuprev;
2574 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2576 /* move menu bar selection if no more popups are left */
2578 if( !MENU_DoNextMenu( pmt, VK_LEFT) )
2579 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2581 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2583 /* A sublevel menu was displayed - display the next one
2584 * unless there is another displacement coming up */
2586 if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2587 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2588 pmt->hTopMenu, TRUE, wFlags);
2594 /***********************************************************************
2597 * Handle a VK_RIGHT key event in a menu.
2599 static void MENU_KeyRight( MTRACKER* pmt, UINT wFlags )
2602 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2605 TRACE("MENU_KeyRight called, cur %p (%s), top %p (%s).\n",
2607 debugstr_w((MENU_GetMenu(pmt->hCurrentMenu))->items[0].text),
2608 pmt->hTopMenu, debugstr_w(menu->items[0].text) );
2610 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2612 /* If already displaying a popup, try to display sub-popup */
2614 hmenutmp = pmt->hCurrentMenu;
2615 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hmenutmp, TRUE, wFlags);
2617 /* if subpopup was displayed then we are done */
2618 if (hmenutmp != pmt->hCurrentMenu) return;
2621 /* Check to see if there's another column */
2622 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
2623 NO_SELECTED_ITEM ) {
2624 TRACE("Going to %d.\n", nextcol );
2625 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2630 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
2632 if( pmt->hCurrentMenu != pmt->hTopMenu )
2634 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE );
2635 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
2636 } else hmenutmp = 0;
2638 /* try to move to the next item */
2639 if( !MENU_DoNextMenu( pmt, VK_RIGHT) )
2640 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
2642 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2643 if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
2644 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2645 pmt->hTopMenu, TRUE, wFlags);
2649 /***********************************************************************
2652 * Menu tracking code.
2654 static BOOL MENU_TrackMenu( HMENU hmenu, UINT wFlags, INT x, INT y,
2655 HWND hwnd, const RECT *lprect )
2660 INT executedMenuId = -1;
2662 BOOL enterIdleSent = FALSE;
2665 mt.hCurrentMenu = hmenu;
2666 mt.hTopMenu = hmenu;
2667 mt.hOwnerWnd = WIN_GetFullHandle( hwnd );
2671 TRACE("hmenu=%p flags=0x%08x (%d,%d) hwnd=%p (%ld,%ld)-(%ld,%ld)\n",
2672 hmenu, wFlags, x, y, hwnd, (lprect) ? lprect->left : 0, (lprect) ? lprect->top : 0,
2673 (lprect) ? lprect->right : 0, (lprect) ? lprect->bottom : 0);
2676 if (!(menu = MENU_GetMenu( hmenu )))
2678 WARN("Invalid menu handle %p\n", hmenu);
2679 SetLastError(ERROR_INVALID_MENU_HANDLE);
2683 if (wFlags & TPM_BUTTONDOWN)
2685 /* Get the result in order to start the tracking or not */
2686 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2687 fEndMenu = !fRemove;
2690 if (wFlags & TF_ENDMENU) fEndMenu = TRUE;
2692 MENU_SetCapture( mt.hOwnerWnd );
2696 menu = MENU_GetMenu( mt.hCurrentMenu );
2697 if (!menu) /* sometimes happens if I do a window manager close */
2700 /* we have to keep the message in the queue until it's
2701 * clear that menu loop is not over yet. */
2705 if (PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE ))
2707 if (!CallMsgFilterW( &msg, MSGF_MENU )) break;
2708 /* remove the message from the queue */
2709 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
2715 HWND win = (wFlags & TPM_ENTERIDLEEX && menu->wFlags & MF_POPUP) ? menu->hWnd : 0;
2716 enterIdleSent = TRUE;
2717 SendMessageW( mt.hOwnerWnd, WM_ENTERIDLE, MSGF_MENU, (LPARAM)win );
2723 /* check if EndMenu() tried to cancel us, by posting this message */
2724 if(msg.message == WM_CANCELMODE)
2726 /* we are now out of the loop */
2729 /* remove the message from the queue */
2730 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
2732 /* break out of internal loop, ala ESCAPE */
2736 TranslateMessage( &msg );
2739 if ( (msg.hwnd==menu->hWnd) || (msg.message!=WM_TIMER) )
2740 enterIdleSent=FALSE;
2743 if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
2746 * Use the mouse coordinates in lParam instead of those in the MSG
2747 * struct to properly handle synthetic messages. They are already
2748 * in screen coordinates.
2750 mt.pt.x = (short)LOWORD(msg.lParam);
2751 mt.pt.y = (short)HIWORD(msg.lParam);
2753 /* Find a menu for this mouse event */
2754 hmenu = MENU_PtMenu( mt.hTopMenu, mt.pt );
2758 /* no WM_NC... messages in captured state */
2760 case WM_RBUTTONDBLCLK:
2761 case WM_RBUTTONDOWN:
2762 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2764 case WM_LBUTTONDBLCLK:
2765 case WM_LBUTTONDOWN:
2766 /* If the message belongs to the menu, removes it from the queue */
2767 /* Else, end menu tracking */
2768 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
2769 fEndMenu = !fRemove;
2773 if (!(wFlags & TPM_RIGHTBUTTON)) break;
2776 /* Check if a menu was selected by the mouse */
2779 executedMenuId = MENU_ButtonUp( &mt, hmenu, wFlags);
2781 /* End the loop if executedMenuId is an item ID */
2782 /* or if the job was done (executedMenuId = 0). */
2783 fEndMenu = fRemove = (executedMenuId != -1);
2785 /* No menu was selected by the mouse */
2786 /* if the function was called by TrackPopupMenu, continue
2787 with the menu tracking. If not, stop it */
2789 fEndMenu = ((wFlags & TPM_POPUPMENU) ? FALSE : TRUE);
2794 /* the selected menu item must be changed every time */
2795 /* the mouse moves. */
2798 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
2800 } /* switch(msg.message) - mouse */
2802 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
2804 fRemove = TRUE; /* Keyboard messages are always removed */
2817 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
2818 NO_SELECTED_ITEM, FALSE, 0 );
2821 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
2822 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
2825 case VK_DOWN: /* If on menu bar, pull-down the menu */
2827 menu = MENU_GetMenu( mt.hCurrentMenu );
2828 if (!(menu->wFlags & MF_POPUP))
2829 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, mt.hTopMenu, TRUE, wFlags);
2830 else /* otherwise try to move selection */
2831 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu, ITEM_NEXT );
2835 MENU_KeyLeft( &mt, wFlags );
2839 MENU_KeyRight( &mt, wFlags );
2843 fEndMenu = MENU_KeyEscape(&mt, wFlags);
2849 hi.cbSize = sizeof(HELPINFO);
2850 hi.iContextType = HELPINFO_MENUITEM;
2851 if (menu->FocusedItem == NO_SELECTED_ITEM)
2854 hi.iCtrlId = menu->items[menu->FocusedItem].wID;
2855 hi.hItemHandle = hmenu;
2856 hi.dwContextId = menu->dwContextHelpID;
2857 hi.MousePos = msg.pt;
2858 SendMessageW(hwnd, WM_HELP, 0, (LPARAM)&hi);
2865 break; /* WM_KEYDOWN */
2872 if (msg.wParam == '\r' || msg.wParam == ' ')
2874 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
2875 fEndMenu = (executedMenuId != -1);
2880 /* Hack to avoid control chars. */
2881 /* We will find a better way real soon... */
2882 if (msg.wParam < 32) break;
2884 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
2885 LOWORD(msg.wParam), FALSE );
2886 if (pos == (UINT)-2) fEndMenu = TRUE;
2887 else if (pos == (UINT)-1) MessageBeep(0);
2890 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos,
2892 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
2893 fEndMenu = (executedMenuId != -1);
2897 } /* switch(msg.message) - kbd */
2901 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
2902 DispatchMessageW( &msg );
2906 if (!fEndMenu) fRemove = TRUE;
2908 /* finally remove message from the queue */
2910 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
2911 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
2912 else mt.trackFlags &= ~TF_SKIPREMOVE;
2915 MENU_SetCapture(0); /* release the capture */
2917 /* If dropdown is still painted and the close box is clicked on
2918 then the menu will be destroyed as part of the DispatchMessage above.
2919 This will then invalidate the menu handle in mt.hTopMenu. We should
2920 check for this first. */
2921 if( IsMenu( mt.hTopMenu ) )
2923 menu = MENU_GetMenu( mt.hTopMenu );
2925 if( IsWindow( mt.hOwnerWnd ) )
2927 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE );
2929 if (menu && (menu->wFlags & MF_POPUP))
2931 DestroyWindow( menu->hWnd );
2934 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2935 SendMessageW( mt.hOwnerWnd, WM_MENUSELECT, MAKELONG(0,0xffff), 0 );
2938 /* Reset the variable for hiding menu */
2939 if( menu ) menu->bTimeToHide = FALSE;
2942 /* The return value is only used by TrackPopupMenu */
2943 if (!(wFlags & TPM_RETURNCMD)) return TRUE;
2944 if (executedMenuId == -1) executedMenuId = 0;
2945 return executedMenuId;
2948 /***********************************************************************
2951 static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu, BOOL bPopup, UINT wFlags)
2955 TRACE("hwnd=%p hmenu=%p\n", hWnd, hMenu);
2959 /* Send WM_ENTERMENULOOP and WM_INITMENU message only if TPM_NONOTIFY flag is not specified */
2960 if (!(wFlags & TPM_NONOTIFY))
2961 SendMessageW( hWnd, WM_ENTERMENULOOP, bPopup, 0 );
2963 SendMessageW( hWnd, WM_SETCURSOR, (WPARAM)hWnd, HTCAPTION );
2965 if (!(wFlags & TPM_NONOTIFY))
2967 SendMessageW( hWnd, WM_INITMENU, (WPARAM)hMenu, 0 );
2968 /* If an app changed/recreated menu bar entries in WM_INITMENU
2969 * menu sizes will be recalculated once the menu created/shown.
2973 /* This makes the menus of applications built with Delphi work.
2974 * It also enables menus to be displayed in more than one window,
2975 * but there are some bugs left that need to be fixed in this case.
2977 if ((menu = MENU_GetMenu( hMenu ))) menu->hWnd = hWnd;
2981 /***********************************************************************
2984 static BOOL MENU_ExitTracking(HWND hWnd)
2986 TRACE("hwnd=%p\n", hWnd);
2988 SendMessageW( hWnd, WM_EXITMENULOOP, 0, 0 );
2994 /***********************************************************************
2995 * MENU_TrackMouseMenuBar
2997 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
2999 void MENU_TrackMouseMenuBar( HWND hWnd, INT ht, POINT pt )
3001 HMENU hMenu = (ht == HTSYSMENU) ? get_win_sys_menu( hWnd ) : GetMenu( hWnd );
3002 UINT wFlags = TPM_ENTERIDLEEX | TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3004 TRACE("wnd=%p ht=0x%04x (%ld,%ld)\n", hWnd, ht, pt.x, pt.y);
3008 MENU_InitTracking( hWnd, hMenu, FALSE, wFlags );
3009 MENU_TrackMenu( hMenu, wFlags, pt.x, pt.y, hWnd, NULL );
3010 MENU_ExitTracking(hWnd);
3015 /***********************************************************************
3016 * MENU_TrackKbdMenuBar
3018 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
3020 void MENU_TrackKbdMenuBar( HWND hwnd, UINT wParam, WCHAR wChar)
3022 UINT uItem = NO_SELECTED_ITEM;
3024 UINT wFlags = TPM_ENTERIDLEEX | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3026 TRACE("hwnd %p wParam 0x%04x wChar 0x%04x\n", hwnd, wParam, wChar);
3028 /* find window that has a menu */
3030 while (!WIN_ALLOWED_MENU(GetWindowLongW( hwnd, GWL_STYLE )))
3031 if (!(hwnd = GetAncestor( hwnd, GA_PARENT ))) return;
3033 /* check if we have to track a system menu */
3035 hTrackMenu = GetMenu( hwnd );
3036 if (!hTrackMenu || IsIconic(hwnd) || wChar == ' ' )
3038 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return;
3039 hTrackMenu = get_win_sys_menu( hwnd );
3041 wParam |= HTSYSMENU; /* prevent item lookup */
3044 if (!IsMenu( hTrackMenu )) return;
3046 MENU_InitTracking( hwnd, hTrackMenu, FALSE, wFlags );
3048 if( wChar && wChar != ' ' )
3050 uItem = MENU_FindItemByKey( hwnd, hTrackMenu, wChar, (wParam & HTSYSMENU) );
3051 if ( uItem >= (UINT)(-2) )
3053 if( uItem == (UINT)(-1) ) MessageBeep(0);
3054 /* schedule end of menu tracking */
3055 wFlags |= TF_ENDMENU;
3060 MENU_SelectItem( hwnd, hTrackMenu, uItem, TRUE, 0 );
3062 if (wParam & HTSYSMENU)
3064 /* prevent sysmenu activation for managed windows on Alt down/up */
3065 if (GetPropA( hwnd, "__wine_x11_managed" ))
3066 wFlags |= TF_ENDMENU; /* schedule end of menu tracking */
3070 if( uItem == NO_SELECTED_ITEM )
3071 MENU_MoveSelection( hwnd, hTrackMenu, ITEM_NEXT );
3073 PostMessageW( hwnd, WM_KEYDOWN, VK_DOWN, 0L );
3077 MENU_TrackMenu( hTrackMenu, wFlags, 0, 0, hwnd, NULL );
3078 MENU_ExitTracking( hwnd );
3082 /**********************************************************************
3083 * TrackPopupMenu (USER32.@)
3085 * Like the win32 API, the function return the command ID only if the
3086 * flag TPM_RETURNCMD is on.
3089 BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
3090 INT nReserved, HWND hWnd, const RECT *lpRect )
3094 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3096 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3097 if (!(wFlags & TPM_NONOTIFY))
3098 SendMessageW( hWnd, WM_INITMENUPOPUP, (WPARAM)hMenu, 0);
3100 if (MENU_ShowPopup( hWnd, hMenu, 0, x, y, 0, 0 ))
3101 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd, lpRect );
3102 MENU_ExitTracking(hWnd);
3107 /**********************************************************************
3108 * TrackPopupMenuEx (USER32.@)
3110 BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3111 HWND hWnd, LPTPMPARAMS lpTpm )
3113 FIXME("not fully implemented\n" );
3114 return TrackPopupMenu( hMenu, wFlags, x, y, 0, hWnd,
3115 lpTpm ? &lpTpm->rcExclude : NULL );
3118 /***********************************************************************
3121 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3123 static LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
3125 TRACE("hwnd=%p msg=0x%04x wp=0x%04x lp=0x%08lx\n", hwnd, message, wParam, lParam);
3131 CREATESTRUCTW *cs = (CREATESTRUCTW*)lParam;
3132 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)cs->lpCreateParams );
3136 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3137 return MA_NOACTIVATE;
3142 BeginPaint( hwnd, &ps );
3143 MENU_DrawPopupMenu( hwnd, ps.hdc,
3144 (HMENU)GetWindowLongPtrW( hwnd, 0 ) );
3145 EndPaint( hwnd, &ps );
3152 /* zero out global pointer in case resident popup window was destroyed. */
3153 if (hwnd == top_popup) top_popup = 0;
3160 if (!GetWindowLongPtrW( hwnd, 0 )) ERR("no menu to display\n");
3163 SetWindowLongPtrW( hwnd, 0, 0 );
3166 case MM_SETMENUHANDLE:
3167 SetWindowLongPtrW( hwnd, 0, wParam );
3170 case MM_GETMENUHANDLE:
3171 return GetWindowLongPtrW( hwnd, 0 );
3174 return DefWindowProcW( hwnd, message, wParam, lParam );
3180 /***********************************************************************
3181 * MENU_GetMenuBarHeight
3183 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3185 UINT MENU_GetMenuBarHeight( HWND hwnd, UINT menubarWidth,
3186 INT orgX, INT orgY )
3192 TRACE("HWND %p, width %d, at (%d, %d).\n", hwnd, menubarWidth, orgX, orgY );
3194 if (!(lppop = MENU_GetMenu( GetMenu(hwnd) ))) return 0;
3196 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3197 SelectObject( hdc, get_menu_font(FALSE));
3198 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3199 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3200 ReleaseDC( hwnd, hdc );
3201 return lppop->Height;
3205 /*******************************************************************
3206 * ChangeMenuA (USER32.@)
3208 BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3209 UINT id, UINT flags )
3211 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3212 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3214 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3215 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3217 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3218 flags & MF_BYPOSITION ? pos : id,
3219 flags & ~MF_REMOVE );
3220 /* Default: MF_INSERT */
3221 return InsertMenuA( hMenu, pos, flags, id, data );
3225 /*******************************************************************
3226 * ChangeMenuW (USER32.@)
3228 BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3229 UINT id, UINT flags )
3231 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3232 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3234 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3235 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3237 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3238 flags & MF_BYPOSITION ? pos : id,
3239 flags & ~MF_REMOVE );
3240 /* Default: MF_INSERT */
3241 return InsertMenuW( hMenu, pos, flags, id, data );
3245 /*******************************************************************
3246 * CheckMenuItem (USER32.@)
3248 DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3253 TRACE("menu=%p id=%04x flags=%04x\n", hMenu, id, flags );
3254 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3255 ret = item->fState & MF_CHECKED;
3256 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3257 else item->fState &= ~MF_CHECKED;
3262 /**********************************************************************
3263 * EnableMenuItem (USER32.@)
3265 UINT WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3271 TRACE("(%p, %04x, %04x) !\n", hMenu, wItemID, wFlags);
3273 /* Get the Popupmenu to access the owner menu */
3274 if (!(menu = MENU_GetMenu(hMenu)))
3277 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3280 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3281 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3283 /* If the close item in the system menu change update the close button */
3284 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3286 if (menu->hSysMenuOwner != 0)
3289 POPUPMENU* parentMenu;
3291 /* Get the parent menu to access*/
3292 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3295 /* Refresh the frame to reflect the change */
3296 GetWindowRect(parentMenu->hWnd, &rc);
3297 MapWindowPoints(0, parentMenu->hWnd, (POINT *)&rc, 2);
3299 RedrawWindow(parentMenu->hWnd, &rc, 0, RDW_FRAME | RDW_INVALIDATE | RDW_NOCHILDREN);
3307 /*******************************************************************
3308 * GetMenuStringA (USER32.@)
3310 INT WINAPI GetMenuStringA(
3311 HMENU hMenu, /* [in] menuhandle */
3312 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3313 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3314 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3315 UINT wFlags /* [in] MF_ flags */
3319 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3320 if (str && nMaxSiz) str[0] = '\0';
3321 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
3322 if (!IS_STRING_ITEM(item->fType)) return 0;
3323 if (!str || !nMaxSiz) return strlenW(item->text);
3324 if (!WideCharToMultiByte( CP_ACP, 0, item->text, -1, str, nMaxSiz, NULL, NULL ))
3326 TRACE("returning '%s'\n", str );
3331 /*******************************************************************
3332 * GetMenuStringW (USER32.@)
3334 INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3335 LPWSTR str, INT nMaxSiz, UINT wFlags )
3339 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3340 if (str && nMaxSiz) str[0] = '\0';
3341 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return 0;
3342 if (!IS_STRING_ITEM(item->fType)) return 0;
3343 if (!str || !nMaxSiz) return strlenW(item->text);
3344 lstrcpynW( str, item->text, nMaxSiz );
3345 return strlenW(str);
3349 /**********************************************************************
3350 * HiliteMenuItem (USER32.@)
3352 BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3356 TRACE("(%p, %p, %04x, %04x);\n", hWnd, hMenu, wItemID, wHilite);
3357 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3358 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3359 if (menu->FocusedItem == wItemID) return TRUE;
3360 MENU_HideSubPopups( hWnd, hMenu, FALSE );
3361 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3366 /**********************************************************************
3367 * GetMenuState (USER32.@)
3369 UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3372 TRACE("(menu=%p, id=%04x, flags=%04x);\n", hMenu, wItemID, wFlags);
3373 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3374 debug_print_menuitem (" item: ", item, "");
3375 if (item->fType & MF_POPUP)
3377 POPUPMENU *menu = MENU_GetMenu( item->hSubMenu );
3378 if (!menu) return -1;
3379 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3383 /* We used to (from way back then) mask the result to 0xff. */
3384 /* I don't know why and it seems wrong as the documented */
3385 /* return flag MF_SEPARATOR is outside that mask. */
3386 return (item->fType | item->fState);
3391 /**********************************************************************
3392 * GetMenuItemCount (USER32.@)
3394 INT WINAPI GetMenuItemCount( HMENU hMenu )
3396 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3397 if (!menu) return -1;
3398 TRACE("(%p) returning %d\n", hMenu, menu->nItems );
3399 return menu->nItems;
3403 /**********************************************************************
3404 * GetMenuItemID (USER32.@)
3406 UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3410 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return -1;
3411 if (lpmi->fType & MF_POPUP) return -1;
3417 /*******************************************************************
3418 * InsertMenuW (USER32.@)
3420 BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3421 UINT_PTR id, LPCWSTR str )
3425 if (IS_STRING_ITEM(flags) && str)
3426 TRACE("hMenu %p, pos %d, flags %08x, id %04x, str %s\n",
3427 hMenu, pos, flags, id, debugstr_w(str) );
3428 else TRACE("hMenu %p, pos %d, flags %08x, id %04x, str %p (not a string)\n",
3429 hMenu, pos, flags, id, str );
3431 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3433 if (!(MENU_SetItemData( item, flags, id, str )))
3435 RemoveMenu( hMenu, pos, flags );
3439 if (flags & MF_POPUP) /* Set the MF_POPUP flag on the popup-menu */
3440 (MENU_GetMenu((HMENU)id))->wFlags |= MF_POPUP;
3442 item->hCheckBit = item->hUnCheckBit = 0;
3447 /*******************************************************************
3448 * InsertMenuA (USER32.@)
3450 BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3451 UINT_PTR id, LPCSTR str )
3455 if (IS_STRING_ITEM(flags) && str)
3457 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
3458 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3461 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
3462 ret = InsertMenuW( hMenu, pos, flags, id, newstr );
3463 HeapFree( GetProcessHeap(), 0, newstr );
3467 else return InsertMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3471 /*******************************************************************
3472 * AppendMenuA (USER32.@)
3474 BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3475 UINT_PTR id, LPCSTR data )
3477 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3481 /*******************************************************************
3482 * AppendMenuW (USER32.@)
3484 BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3485 UINT_PTR id, LPCWSTR data )
3487 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3491 /**********************************************************************
3492 * RemoveMenu (USER32.@)
3494 BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3499 TRACE("(menu=%p pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3500 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3501 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3505 MENU_FreeItemData( item );
3507 if (--menu->nItems == 0)
3509 HeapFree( GetProcessHeap(), 0, menu->items );
3514 while(nPos < menu->nItems)
3520 menu->items = HeapReAlloc( GetProcessHeap(), 0, menu->items,
3521 menu->nItems * sizeof(MENUITEM) );
3527 /**********************************************************************
3528 * DeleteMenu (USER32.@)
3530 BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3532 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3533 if (!item) return FALSE;
3534 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
3535 /* nPos is now the position of the item */
3536 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
3541 /*******************************************************************
3542 * ModifyMenuW (USER32.@)
3544 BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
3545 UINT_PTR id, LPCWSTR str )
3549 if (IS_STRING_ITEM(flags))
3551 TRACE("%p %d %04x %04x %s\n", hMenu, pos, flags, id, debugstr_w(str) );
3555 TRACE("%p %d %04x %04x %p\n", hMenu, pos, flags, id, str );
3558 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3559 MENU_GetMenu(hMenu)->Height = 0; /* force size recalculate */
3560 return MENU_SetItemData( item, flags, id, str );
3564 /*******************************************************************
3565 * ModifyMenuA (USER32.@)
3567 BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
3568 UINT_PTR id, LPCSTR str )
3572 if (IS_STRING_ITEM(flags) && str)
3574 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
3575 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3578 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
3579 ret = ModifyMenuW( hMenu, pos, flags, id, newstr );
3580 HeapFree( GetProcessHeap(), 0, newstr );
3584 else return ModifyMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3588 /**********************************************************************
3589 * CreatePopupMenu (USER32.@)
3591 HMENU WINAPI CreatePopupMenu(void)
3596 if (!(hmenu = CreateMenu())) return 0;
3597 menu = MENU_GetMenu( hmenu );
3598 menu->wFlags |= MF_POPUP;
3599 menu->bTimeToHide = FALSE;
3604 /**********************************************************************
3605 * GetMenuCheckMarkDimensions (USER.417)
3606 * GetMenuCheckMarkDimensions (USER32.@)
3608 DWORD WINAPI GetMenuCheckMarkDimensions(void)
3610 return MAKELONG( GetSystemMetrics(SM_CXMENUCHECK), GetSystemMetrics(SM_CYMENUCHECK) );
3614 /**********************************************************************
3615 * SetMenuItemBitmaps (USER32.@)
3617 BOOL WINAPI SetMenuItemBitmaps( HMENU hMenu, UINT nPos, UINT wFlags,
3618 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
3621 TRACE("(%p, %04x, %04x, %p, %p)\n",
3622 hMenu, nPos, wFlags, hNewCheck, hNewUnCheck);
3623 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3625 if (!hNewCheck && !hNewUnCheck)
3627 item->fState &= ~MF_USECHECKBITMAPS;
3629 else /* Install new bitmaps */
3631 item->hCheckBit = hNewCheck;
3632 item->hUnCheckBit = hNewUnCheck;
3633 item->fState |= MF_USECHECKBITMAPS;
3639 /**********************************************************************
3640 * CreateMenu (USER32.@)
3642 HMENU WINAPI CreateMenu(void)
3646 if (!(hMenu = USER_HEAP_ALLOC( sizeof(POPUPMENU) ))) return 0;
3647 menu = (LPPOPUPMENU) USER_HEAP_LIN_ADDR(hMenu);
3649 ZeroMemory(menu, sizeof(POPUPMENU));
3650 menu->wMagic = MENU_MAGIC;
3651 menu->FocusedItem = NO_SELECTED_ITEM;
3652 menu->bTimeToHide = FALSE;
3654 TRACE("return %p\n", hMenu );
3660 /**********************************************************************
3661 * DestroyMenu (USER32.@)
3663 BOOL WINAPI DestroyMenu( HMENU hMenu )
3665 LPPOPUPMENU lppop = MENU_GetMenu(hMenu);
3667 TRACE("(%p)\n", hMenu);
3670 if (!lppop) return FALSE;
3672 lppop->wMagic = 0; /* Mark it as destroyed */
3674 /* DestroyMenu should not destroy system menu popup owner */
3675 if ((lppop->wFlags & (MF_POPUP | MF_SYSMENU)) == MF_POPUP && lppop->hWnd)
3677 DestroyWindow( lppop->hWnd );
3681 if (lppop->items) /* recursively destroy submenus */
3684 MENUITEM *item = lppop->items;
3685 for (i = lppop->nItems; i > 0; i--, item++)
3687 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
3688 MENU_FreeItemData( item );
3690 HeapFree( GetProcessHeap(), 0, lppop->items );
3692 USER_HEAP_FREE( hMenu );
3697 /**********************************************************************
3698 * GetSystemMenu (USER32.@)
3700 HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
3702 WND *wndPtr = WIN_GetPtr( hWnd );
3705 if (wndPtr == WND_DESKTOP) return 0;
3706 if (wndPtr == WND_OTHER_PROCESS)
3708 if (IsWindow( hWnd )) FIXME( "not supported on other process window %p\n", hWnd );
3712 if (wndPtr->hSysMenu && bRevert)
3714 DestroyMenu(wndPtr->hSysMenu);
3715 wndPtr->hSysMenu = 0;
3718 if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
3719 wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, 0 );
3721 if( wndPtr->hSysMenu )
3724 retvalue = GetSubMenu(wndPtr->hSysMenu, 0);
3726 /* Store the dummy sysmenu handle to facilitate the refresh */
3727 /* of the close button if the SC_CLOSE item change */
3728 menu = MENU_GetMenu(retvalue);
3730 menu->hSysMenuOwner = wndPtr->hSysMenu;
3732 WIN_ReleasePtr( wndPtr );
3734 return bRevert ? 0 : retvalue;
3738 /*******************************************************************
3739 * SetSystemMenu (USER32.@)
3741 BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
3743 WND *wndPtr = WIN_GetPtr( hwnd );
3745 if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
3747 if (wndPtr->hSysMenu) DestroyMenu( wndPtr->hSysMenu );
3748 wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
3749 WIN_ReleasePtr( wndPtr );
3756 /**********************************************************************
3757 * GetMenu (USER32.@)
3759 HMENU WINAPI GetMenu( HWND hWnd )
3761 HMENU retvalue = (HMENU)GetWindowLongPtrW( hWnd, GWLP_ID );
3762 TRACE("for %p returning %p\n", hWnd, retvalue);
3766 /**********************************************************************
3767 * GetMenuBarInfo (USER32.@)
3769 BOOL WINAPI GetMenuBarInfo( HWND hwnd, LONG idObject, LONG idItem, PMENUBARINFO pmbi )
3771 FIXME( "(%p,0x%08lx,0x%08lx,%p)\n", hwnd, idObject, idItem, pmbi );
3775 /**********************************************************************
3778 * Helper for SetMenu. Also called by WIN_CreateWindowEx to avoid the
3779 * SetWindowPos call that would result if SetMenu were called directly.
3781 BOOL MENU_SetMenu( HWND hWnd, HMENU hMenu )
3783 TRACE("(%p, %p);\n", hWnd, hMenu);
3785 if (hMenu && !IsMenu(hMenu))
3787 WARN("hMenu %p is not a menu handle\n", hMenu);
3788 SetLastError(ERROR_INVALID_MENU_HANDLE);
3791 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
3794 hWnd = WIN_GetFullHandle( hWnd );
3795 if (GetCapture() == hWnd) MENU_SetCapture(0); /* release the capture */
3801 if (!(lpmenu = MENU_GetMenu(hMenu))) return FALSE;
3803 lpmenu->hWnd = hWnd;
3804 lpmenu->Height = 0; /* Make sure we recalculate the size */
3806 SetWindowLongPtrW( hWnd, GWLP_ID, (LONG_PTR)hMenu );
3811 /**********************************************************************
3812 * SetMenu (USER32.@)
3814 BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
3816 if(!MENU_SetMenu(hWnd, hMenu))
3819 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3820 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3825 /**********************************************************************
3826 * GetSubMenu (USER32.@)
3828 HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
3832 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0;
3833 if (!(lpmi->fType & MF_POPUP)) return 0;
3834 return lpmi->hSubMenu;
3838 /**********************************************************************
3839 * DrawMenuBar (USER32.@)
3841 BOOL WINAPI DrawMenuBar( HWND hWnd )
3844 HMENU hMenu = GetMenu(hWnd);
3846 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
3848 if (!hMenu || !(lppop = MENU_GetMenu( hMenu ))) return FALSE;
3850 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
3851 lppop->hwndOwner = hWnd;
3852 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
3853 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
3857 /***********************************************************************
3858 * DrawMenuBarTemp (USER32.@)
3862 * called by W98SE desk.cpl Control Panel Applet
3864 * Not 100% sure about the param names, but close.
3866 DWORD WINAPI DrawMenuBarTemp(HWND hwnd, HDC hDC, LPRECT lprect, HMENU hMenu, HFONT hFont)
3871 BOOL flat_menu = FALSE;
3873 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
3876 hMenu = GetMenu(hwnd);
3879 hFont = get_menu_font(FALSE);
3881 lppop = MENU_GetMenu( hMenu );
3882 if (lppop == NULL || lprect == NULL)
3884 retvalue = GetSystemMetrics(SM_CYMENU);
3888 TRACE("(%p, %p, %p, %p, %p)\n", hwnd, hDC, lprect, hMenu, hFont);
3890 hfontOld = SelectObject( hDC, hFont);
3892 if (lppop->Height == 0)
3893 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
3895 lprect->bottom = lprect->top + lppop->Height;
3897 FillRect(hDC, lprect, GetSysColorBrush(flat_menu ? COLOR_MENUBAR : COLOR_MENU) );
3899 SelectObject( hDC, SYSCOLOR_GetPen(COLOR_3DFACE));
3900 MoveToEx( hDC, lprect->left, lprect->bottom, NULL );
3901 LineTo( hDC, lprect->right, lprect->bottom );
3903 if (lppop->nItems == 0)
3905 retvalue = GetSystemMetrics(SM_CYMENU);
3909 for (i = 0; i < lppop->nItems; i++)
3911 MENU_DrawMenuItem( hwnd, hMenu, hwnd,
3912 hDC, &lppop->items[i], lppop->Height, TRUE, ODA_DRAWENTIRE );
3914 retvalue = lppop->Height;
3917 if (hfontOld) SelectObject (hDC, hfontOld);
3921 /***********************************************************************
3922 * EndMenu (USER.187)
3923 * EndMenu (USER32.@)
3925 void WINAPI EndMenu(void)
3927 /* if we are in the menu code, and it is active */
3928 if (!fEndMenu && top_popup)
3930 /* terminate the menu handling code */
3933 /* needs to be posted to wakeup the internal menu handler */
3934 /* which will now terminate the menu, in the event that */
3935 /* the main window was minimized, or lost focus, so we */
3936 /* don't end up with an orphaned menu */
3937 PostMessageW( top_popup, WM_CANCELMODE, 0, 0);
3942 /***********************************************************************
3943 * LookupMenuHandle (USER.217)
3945 HMENU16 WINAPI LookupMenuHandle16( HMENU16 hmenu, INT16 id )
3947 HMENU hmenu32 = HMENU_32(hmenu);
3949 if (!MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND )) return 0;
3950 else return HMENU_16(hmenu32);
3954 /**********************************************************************
3955 * LoadMenu (USER.150)
3957 HMENU16 WINAPI LoadMenu16( HINSTANCE16 instance, LPCSTR name )
3963 if (HIWORD(name) && name[0] == '#') name = (LPCSTR)atoi( name + 1 );
3964 if (!name) return 0;
3966 instance = GetExePtr( instance );
3967 if (!(hRsrc = FindResource16( instance, name, (LPSTR)RT_MENU ))) return 0;
3968 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
3969 hMenu = LoadMenuIndirect16(LockResource16(handle));
3970 FreeResource16( handle );
3975 /*****************************************************************
3976 * LoadMenuA (USER32.@)
3978 HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
3980 HRSRC hrsrc = FindResourceA( instance, name, (LPSTR)RT_MENU );
3981 if (!hrsrc) return 0;
3982 return LoadMenuIndirectA( (LPCVOID)LoadResource( instance, hrsrc ));
3986 /*****************************************************************
3987 * LoadMenuW (USER32.@)
3989 HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
3991 HRSRC hrsrc = FindResourceW( instance, name, (LPWSTR)RT_MENU );
3992 if (!hrsrc) return 0;
3993 return LoadMenuIndirectW( (LPCVOID)LoadResource( instance, hrsrc ));
3997 /**********************************************************************
3998 * LoadMenuIndirect (USER.220)
4000 HMENU16 WINAPI LoadMenuIndirect16( LPCVOID template )
4003 WORD version, offset;
4004 LPCSTR p = (LPCSTR)template;
4006 TRACE("(%p)\n", template );
4007 version = GET_WORD(p);
4011 WARN("version must be 0 for Win16\n" );
4014 offset = GET_WORD(p);
4015 p += sizeof(WORD) + offset;
4016 if (!(hMenu = CreateMenu())) return 0;
4017 if (!MENU_ParseResource( p, hMenu, FALSE ))
4019 DestroyMenu( hMenu );
4022 return HMENU_16(hMenu);
4026 /**********************************************************************
4027 * LoadMenuIndirectW (USER32.@)
4029 HMENU WINAPI LoadMenuIndirectW( LPCVOID template )
4032 WORD version, offset;
4033 LPCSTR p = (LPCSTR)template;
4035 version = GET_WORD(p);
4037 TRACE("%p, ver %d\n", template, version );
4040 case 0: /* standard format is version of 0 */
4041 offset = GET_WORD(p);
4042 p += sizeof(WORD) + offset;
4043 if (!(hMenu = CreateMenu())) return 0;
4044 if (!MENU_ParseResource( p, hMenu, TRUE ))
4046 DestroyMenu( hMenu );
4050 case 1: /* extended format is version of 1 */
4051 offset = GET_WORD(p);
4052 p += sizeof(WORD) + offset;
4053 if (!(hMenu = CreateMenu())) return 0;
4054 if (!MENUEX_ParseResource( p, hMenu))
4056 DestroyMenu( hMenu );
4061 ERR("version %d not supported.\n", version);
4067 /**********************************************************************
4068 * LoadMenuIndirectA (USER32.@)
4070 HMENU WINAPI LoadMenuIndirectA( LPCVOID template )
4072 return LoadMenuIndirectW( template );
4076 /**********************************************************************
4079 BOOL WINAPI IsMenu(HMENU hmenu)
4081 LPPOPUPMENU menu = MENU_GetMenu(hmenu);
4082 return menu != NULL;
4085 /**********************************************************************
4086 * GetMenuItemInfo_common
4089 static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4090 LPMENUITEMINFOW lpmii, BOOL unicode)
4092 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos? MF_BYPOSITION : 0);
4094 debug_print_menuitem("GetMenuItemInfo_common: ", menu, "");
4099 if (lpmii->fMask & MIIM_TYPE) {
4100 lpmii->fType = menu->fType;
4101 switch (MENU_ITEM_TYPE(menu->fType)) {
4103 break; /* will be done below */
4106 lpmii->dwTypeData = menu->text;
4113 /* copy the text string */
4114 if ((lpmii->fMask & (MIIM_TYPE|MIIM_STRING)) &&
4115 (MENU_ITEM_TYPE(menu->fType) == MF_STRING) && menu->text)
4120 len = strlenW(menu->text);
4121 if(lpmii->dwTypeData && lpmii->cch)
4122 lstrcpynW(lpmii->dwTypeData, menu->text, lpmii->cch);
4126 len = WideCharToMultiByte( CP_ACP, 0, menu->text, -1, NULL, 0, NULL, NULL );
4127 if(lpmii->dwTypeData && lpmii->cch)
4128 if (!WideCharToMultiByte( CP_ACP, 0, menu->text, -1,
4129 (LPSTR)lpmii->dwTypeData, lpmii->cch, NULL, NULL ))
4130 ((LPSTR)lpmii->dwTypeData)[lpmii->cch-1] = 0;
4132 /* if we've copied a substring we return its length */
4133 if(lpmii->dwTypeData && lpmii->cch)
4135 if (lpmii->cch <= len) lpmii->cch--;
4137 else /* return length of string */
4141 if (lpmii->fMask & MIIM_FTYPE)
4142 lpmii->fType = menu->fType;
4144 if (lpmii->fMask & MIIM_BITMAP)
4145 lpmii->hbmpItem = menu->hbmpItem;
4147 if (lpmii->fMask & MIIM_STATE)
4148 lpmii->fState = menu->fState;
4150 if (lpmii->fMask & MIIM_ID)
4151 lpmii->wID = menu->wID;
4153 if (lpmii->fMask & MIIM_SUBMENU)
4154 lpmii->hSubMenu = menu->hSubMenu;
4156 if (lpmii->fMask & MIIM_CHECKMARKS) {
4157 lpmii->hbmpChecked = menu->hCheckBit;
4158 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4160 if (lpmii->fMask & MIIM_DATA)
4161 lpmii->dwItemData = menu->dwItemData;
4166 /**********************************************************************
4167 * GetMenuItemInfoA (USER32.@)
4169 BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4170 LPMENUITEMINFOA lpmii)
4172 return GetMenuItemInfo_common (hmenu, item, bypos,
4173 (LPMENUITEMINFOW)lpmii, FALSE);
4176 /**********************************************************************
4177 * GetMenuItemInfoW (USER32.@)
4179 BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4180 LPMENUITEMINFOW lpmii)
4182 return GetMenuItemInfo_common (hmenu, item, bypos,
4187 /* set a menu item text from a ASCII or Unicode string */
4188 inline static void set_menu_item_text( MENUITEM *menu, LPCWSTR text, BOOL unicode )
4193 menu->fType |= MF_SEPARATOR;
4197 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, (strlenW(text)+1) * sizeof(WCHAR) )))
4198 strcpyW( menu->text, text );
4202 LPCSTR str = (LPCSTR)text;
4203 int len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
4204 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
4205 MultiByteToWideChar( CP_ACP, 0, str, -1, menu->text, len );
4210 /**********************************************************************
4211 * SetMenuItemInfo_common
4214 static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4215 const MENUITEMINFOW *lpmii,
4218 if (!menu) return FALSE;
4220 debug_print_menuitem("MENU_SetItemInfo_common from: ", menu, "");
4222 if (lpmii->fMask & MIIM_TYPE ) {
4223 /* Get rid of old string. */
4224 if (IS_STRING_ITEM(menu->fType) && menu->text) {
4225 HeapFree(GetProcessHeap(), 0, menu->text);
4229 /* make only MENU_ITEM_TYPE bits in menu->fType equal lpmii->fType */
4230 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4231 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4233 menu->text = lpmii->dwTypeData;
4235 if (IS_STRING_ITEM(menu->fType))
4236 set_menu_item_text( menu, lpmii->dwTypeData, unicode );
4239 if (lpmii->fMask & MIIM_FTYPE ) {
4240 /* free the string when the type is changing */
4241 if ( (!IS_STRING_ITEM(lpmii->fType)) && IS_STRING_ITEM(menu->fType) && menu->text) {
4242 HeapFree(GetProcessHeap(), 0, menu->text);
4245 menu->fType &= ~MENU_ITEM_TYPE(menu->fType);
4246 menu->fType |= MENU_ITEM_TYPE(lpmii->fType);
4247 if ( IS_STRING_ITEM(menu->fType) && !menu->text )
4248 menu->fType |= MF_SEPARATOR;
4251 if (lpmii->fMask & MIIM_STRING ) {
4252 if (IS_STRING_ITEM(menu->fType)) {
4253 /* free the string when used */
4254 HeapFree(GetProcessHeap(), 0, menu->text);
4255 set_menu_item_text( menu, lpmii->dwTypeData, unicode );
4259 if (lpmii->fMask & MIIM_STATE)
4261 /* FIXME: MFS_DEFAULT do we have to reset the other menu items? */
4262 menu->fState = lpmii->fState;
4265 if (lpmii->fMask & MIIM_ID)
4266 menu->wID = lpmii->wID;
4268 if (lpmii->fMask & MIIM_SUBMENU) {
4269 menu->hSubMenu = lpmii->hSubMenu;
4270 if (menu->hSubMenu) {
4271 POPUPMENU *subMenu = MENU_GetMenu(menu->hSubMenu);
4273 subMenu->wFlags |= MF_POPUP;
4274 menu->fType |= MF_POPUP;
4277 /* FIXME: Return an error ? */
4278 menu->fType &= ~MF_POPUP;
4281 menu->fType &= ~MF_POPUP;
4284 if (lpmii->fMask & MIIM_CHECKMARKS)
4286 if (lpmii->fType & MFT_RADIOCHECK)
4287 menu->fType |= MFT_RADIOCHECK;
4289 menu->hCheckBit = lpmii->hbmpChecked;
4290 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4292 if (lpmii->fMask & MIIM_DATA)
4293 menu->dwItemData = lpmii->dwItemData;
4295 if (lpmii->fMask & MIIM_BITMAP)
4296 menu->hbmpItem = lpmii->hbmpItem;
4298 debug_print_menuitem("SetMenuItemInfo_common to : ", menu, "");
4302 /**********************************************************************
4303 * SetMenuItemInfoA (USER32.@)
4305 BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4306 const MENUITEMINFOA *lpmii)
4308 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4309 (const MENUITEMINFOW *)lpmii, FALSE);
4312 /**********************************************************************
4313 * SetMenuItemInfoW (USER32.@)
4315 BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4316 const MENUITEMINFOW *lpmii)
4318 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4322 /**********************************************************************
4323 * SetMenuDefaultItem (USER32.@)
4326 BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4332 TRACE("(%p,%d,%d)\n", hmenu, uItem, bypos);
4334 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4336 /* reset all default-item flags */
4338 for (i = 0; i < menu->nItems; i++, item++)
4340 item->fState &= ~MFS_DEFAULT;
4343 /* no default item */
4352 if ( uItem >= menu->nItems ) return FALSE;
4353 item[uItem].fState |= MFS_DEFAULT;
4358 for (i = 0; i < menu->nItems; i++, item++)
4360 if (item->wID == uItem)
4362 item->fState |= MFS_DEFAULT;
4371 /**********************************************************************
4372 * GetMenuDefaultItem (USER32.@)
4374 UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4380 TRACE("(%p,%d,%d)\n", hmenu, bypos, flags);
4382 if (!(menu = MENU_GetMenu(hmenu))) return -1;
4384 /* find default item */
4388 if (! item) return -1;
4390 while ( !( item->fState & MFS_DEFAULT ) )
4393 if (i >= menu->nItems ) return -1;
4396 /* default: don't return disabled items */
4397 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
4399 /* search rekursiv when needed */
4400 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
4403 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
4404 if ( -1 != ret ) return ret;
4406 /* when item not found in submenu, return the popup item */
4408 return ( bypos ) ? i : item->wID;
4413 /**********************************************************************
4414 * InsertMenuItemA (USER32.@)
4416 BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
4417 const MENUITEMINFOA *lpmii)
4419 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4420 return SetMenuItemInfo_common(item, (const MENUITEMINFOW *)lpmii, FALSE);
4424 /**********************************************************************
4425 * InsertMenuItemW (USER32.@)
4427 BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
4428 const MENUITEMINFOW *lpmii)
4430 MENUITEM *item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4431 return SetMenuItemInfo_common(item, lpmii, TRUE);
4434 /**********************************************************************
4435 * CheckMenuRadioItem (USER32.@)
4438 BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
4439 UINT first, UINT last, UINT check,
4442 MENUITEM *mifirst, *milast, *micheck;
4443 HMENU mfirst = hMenu, mlast = hMenu, mcheck = hMenu;
4445 TRACE("%p: %d-%d, check %d, bypos=%d\n", hMenu, first, last, check, bypos);
4447 mifirst = MENU_FindItem (&mfirst, &first, bypos);
4448 milast = MENU_FindItem (&mlast, &last, bypos);
4449 micheck = MENU_FindItem (&mcheck, &check, bypos);
4451 if (mifirst == NULL || milast == NULL || micheck == NULL ||
4452 mifirst > milast || mfirst != mlast || mfirst != mcheck ||
4453 micheck > milast || micheck < mifirst)
4456 while (mifirst <= milast)
4458 if (mifirst == micheck)
4460 mifirst->fType |= MFT_RADIOCHECK;
4461 mifirst->fState |= MFS_CHECKED;
4463 mifirst->fType &= ~MFT_RADIOCHECK;
4464 mifirst->fState &= ~MFS_CHECKED;
4473 /**********************************************************************
4474 * GetMenuItemRect (USER32.@)
4476 * ATTENTION: Here, the returned values in rect are the screen
4477 * coordinates of the item just like if the menu was
4478 * always on the upper left side of the application.
4481 BOOL WINAPI GetMenuItemRect (HWND hwnd, HMENU hMenu, UINT uItem,
4484 POPUPMENU *itemMenu;
4488 TRACE("(%p,%p,%d,%p)\n", hwnd, hMenu, uItem, rect);
4490 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
4491 referenceHwnd = hwnd;
4495 itemMenu = MENU_GetMenu(hMenu);
4496 if (itemMenu == NULL)
4499 if(itemMenu->hWnd == 0)
4501 referenceHwnd = itemMenu->hWnd;
4504 if ((rect == NULL) || (item == NULL))
4509 MapWindowPoints(referenceHwnd, 0, (LPPOINT)rect, 2);
4515 /**********************************************************************
4516 * SetMenuInfo (USER32.@)
4519 * MIM_APPLYTOSUBMENUS
4520 * actually use the items to draw the menu
4522 BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
4526 TRACE("(%p %p)\n", hMenu, lpmi);
4528 if (lpmi && (lpmi->cbSize==sizeof(MENUINFO)) && (menu = MENU_GetMenu(hMenu)))
4531 if (lpmi->fMask & MIM_BACKGROUND)
4532 menu->hbrBack = lpmi->hbrBack;
4534 if (lpmi->fMask & MIM_HELPID)
4535 menu->dwContextHelpID = lpmi->dwContextHelpID;
4537 if (lpmi->fMask & MIM_MAXHEIGHT)
4538 menu->cyMax = lpmi->cyMax;
4540 if (lpmi->fMask & MIM_MENUDATA)
4541 menu->dwMenuData = lpmi->dwMenuData;
4543 if (lpmi->fMask & MIM_STYLE)
4545 menu->dwStyle = lpmi->dwStyle;
4546 if (menu->dwStyle & MNS_AUTODISMISS) FIXME("MNS_AUTODISMISS unimplemented\n");
4547 if (menu->dwStyle & MNS_DRAGDROP) FIXME("MNS_DRAGDROP unimplemented\n");
4548 if (menu->dwStyle & MNS_MODELESS) FIXME("MNS_MODELESS unimplemented\n");
4549 if (menu->dwStyle & MNS_NOCHECK) FIXME("MNS_NOCHECK unimplemented\n");
4550 if (menu->dwStyle & MNS_NOTIFYBYPOS) FIXME("MNS_NOTIFYBYPOS unimplemented\n");
4558 /**********************************************************************
4559 * GetMenuInfo (USER32.@)
4565 BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
4568 TRACE("(%p %p)\n", hMenu, lpmi);
4570 if (lpmi && (menu = MENU_GetMenu(hMenu)))
4573 if (lpmi->fMask & MIM_BACKGROUND)
4574 lpmi->hbrBack = menu->hbrBack;
4576 if (lpmi->fMask & MIM_HELPID)
4577 lpmi->dwContextHelpID = menu->dwContextHelpID;
4579 if (lpmi->fMask & MIM_MAXHEIGHT)
4580 lpmi->cyMax = menu->cyMax;
4582 if (lpmi->fMask & MIM_MENUDATA)
4583 lpmi->dwMenuData = menu->dwMenuData;
4585 if (lpmi->fMask & MIM_STYLE)
4586 lpmi->dwStyle = menu->dwStyle;
4594 /**********************************************************************
4595 * SetMenuContextHelpId (USER32.@)
4597 BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
4601 TRACE("(%p 0x%08lx)\n", hMenu, dwContextHelpID);
4603 if ((menu = MENU_GetMenu(hMenu)))
4605 menu->dwContextHelpID = dwContextHelpID;
4612 /**********************************************************************
4613 * GetMenuContextHelpId (USER32.@)
4615 DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
4619 TRACE("(%p)\n", hMenu);
4621 if ((menu = MENU_GetMenu(hMenu)))
4623 return menu->dwContextHelpID;
4628 /**********************************************************************
4629 * MenuItemFromPoint (USER32.@)
4631 INT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
4633 POPUPMENU *menu = MENU_GetMenu(hMenu);
4636 /*FIXME: Do we have to handle hWnd here? */
4637 if (!menu) return -1;
4638 if (!MENU_FindItemByCoords(menu, ptScreen, &pos)) return -1;
4643 /**********************************************************************
4644 * translate_accelerator
4646 static BOOL translate_accelerator( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam,
4647 BYTE fVirt, WORD key, WORD cmd )
4652 if (wParam != key) return FALSE;
4654 if (GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
4655 if (GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
4656 if (GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
4658 if (message == WM_CHAR || message == WM_SYSCHAR)
4660 if ( !(fVirt & FVIRTKEY) && (mask & FALT) == (fVirt & FALT) )
4662 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", wParam & 0xff);
4668 if(fVirt & FVIRTKEY)
4670 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
4671 wParam, 0xff & HIWORD(lParam));
4673 if(mask == (fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
4674 TRACE_(accel)(", but incorrect SHIFT/CTRL/ALT-state\n");
4678 if (!(lParam & 0x01000000)) /* no special_key */
4680 if ((fVirt & FALT) && (lParam & 0x20000000))
4681 { /* ^^ ALT pressed */
4682 TRACE_(accel)("found accel for Alt-%c\n", wParam & 0xff);
4691 if (message == WM_KEYUP || message == WM_SYSKEYUP)
4695 HMENU hMenu, hSubMenu, hSysMenu;
4696 UINT uSysStat = (UINT)-1, uStat = (UINT)-1, nPos;
4698 hMenu = (GetWindowLongW( hWnd, GWL_STYLE ) & WS_CHILD) ? 0 : GetMenu(hWnd);
4699 hSysMenu = get_win_sys_menu( hWnd );
4701 /* find menu item and ask application to initialize it */
4702 /* 1. in the system menu */
4703 hSubMenu = hSysMenu;
4705 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
4709 if (!IsWindowEnabled(hWnd))
4713 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hSysMenu, 0L);
4714 if(hSubMenu != hSysMenu)
4716 nPos = MENU_FindSubMenu(&hSysMenu, hSubMenu);
4717 TRACE_(accel)("hSysMenu = %p, hSubMenu = %p, nPos = %d\n", hSysMenu, hSubMenu, nPos);
4718 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, TRUE));
4720 uSysStat = GetMenuState(GetSubMenu(hSysMenu, 0), cmd, MF_BYCOMMAND);
4723 else /* 2. in the window's menu */
4727 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
4731 if (!IsWindowEnabled(hWnd))
4735 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hMenu, 0L);
4736 if(hSubMenu != hMenu)
4738 nPos = MENU_FindSubMenu(&hMenu, hSubMenu);
4739 TRACE_(accel)("hMenu = %p, hSubMenu = %p, nPos = %d\n", hMenu, hSubMenu, nPos);
4740 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, FALSE));
4742 uStat = GetMenuState(hMenu, cmd, MF_BYCOMMAND);
4749 if (uSysStat != (UINT)-1)
4751 if (uSysStat & (MF_DISABLED|MF_GRAYED))
4758 if (uStat != (UINT)-1)
4764 if (uStat & (MF_DISABLED|MF_GRAYED))
4776 if( mesg==WM_COMMAND )
4778 TRACE_(accel)(", sending WM_COMMAND, wParam=%0x\n", 0x10000 | cmd);
4779 SendMessageW(hWnd, mesg, 0x10000 | cmd, 0L);
4781 else if( mesg==WM_SYSCOMMAND )
4783 TRACE_(accel)(", sending WM_SYSCOMMAND, wParam=%0x\n", cmd);
4784 SendMessageW(hWnd, mesg, cmd, 0x00010000L);
4788 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
4789 * #0: unknown (please report!)
4790 * #1: for WM_KEYUP,WM_SYSKEYUP
4791 * #2: mouse is captured
4792 * #3: window is disabled
4793 * #4: it's a disabled system menu option
4794 * #5: it's a menu option, but window is iconic
4795 * #6: it's a menu option, but disabled
4797 TRACE_(accel)(", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
4799 ERR_(accel)(" unknown reason - please report!\n");
4804 /**********************************************************************
4805 * TranslateAccelerator (USER32.@)
4806 * TranslateAcceleratorA (USER32.@)
4808 INT WINAPI TranslateAcceleratorA( HWND hWnd, HACCEL hAccel, LPMSG msg )
4811 LPACCEL16 lpAccelTbl;
4815 if (!hWnd || !msg) return 0;
4817 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(HACCEL_16(hAccel))))
4819 WARN_(accel)("invalid accel handle=%p\n", hAccel);
4823 wParam = msg->wParam;
4825 switch (msg->message)
4834 char ch = LOWORD(wParam);
4836 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
4837 wParam = MAKEWPARAM(wch, HIWORD(wParam));
4845 TRACE_(accel)("hAccel %p, hWnd %p, msg->hwnd %p, msg->message %04x, wParam %08x, lParam %08lx\n",
4846 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
4850 if (translate_accelerator( hWnd, msg->message, wParam, msg->lParam,
4851 lpAccelTbl[i].fVirt, lpAccelTbl[i].key, lpAccelTbl[i].cmd))
4853 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
4858 /**********************************************************************
4859 * TranslateAcceleratorW (USER32.@)
4861 INT WINAPI TranslateAcceleratorW( HWND hWnd, HACCEL hAccel, LPMSG msg )
4864 LPACCEL16 lpAccelTbl;
4867 if (!hWnd || !msg) return 0;
4869 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(HACCEL_16(hAccel))))
4871 WARN_(accel)("invalid accel handle=%p\n", hAccel);
4875 switch (msg->message)
4887 TRACE_(accel)("hAccel %p, hWnd %p, msg->hwnd %p, msg->message %04x, wParam %08x, lParam %08lx\n",
4888 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
4892 if (translate_accelerator( hWnd, msg->message, msg->wParam, msg->lParam,
4893 lpAccelTbl[i].fVirt, lpAccelTbl[i].key, lpAccelTbl[i].cmd))
4895 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);