4 * Copyright 1993 Martin Ayotte
5 * Copyright 1994 Alexandre Julliard
6 * Copyright 1997 Morten Welinder
7 * Copyright 2005 Maxime Bellengé
8 * Copyright 2006 Phil Krylov
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * Note: the style MF_MOUSESELECT is used to mark popup items that
27 * have been selected, i.e. their popup menu is currently displayed.
28 * This is probably not the meaning this style has in MS-Windows.
30 * Note 2: where there is a difference, these menu API's are according
31 * the behavior of Windows 2k and Windows XP. Known differences with
32 * Windows 9x/ME are documented in the comments, in case an application
33 * is found to depend on the old behavior.
43 #include "wine/port.h"
54 #include "wine/winbase16.h"
55 #include "wine/winuser16.h"
57 #include "wine/server.h"
58 #include "wine/unicode.h"
59 #include "wine/exception.h"
62 #include "user_private.h"
63 #include "wine/debug.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(menu);
66 WINE_DECLARE_DEBUG_CHANNEL(accel);
68 /* internal popup menu window messages */
70 #define MM_SETMENUHANDLE (WM_USER + 0)
71 #define MM_GETMENUHANDLE (WM_USER + 1)
73 /* Menu item structure */
75 /* ----------- MENUITEMINFO Stuff ----------- */
76 UINT fType; /* Item type. */
77 UINT fState; /* Item state. */
78 UINT_PTR wID; /* Item id. */
79 HMENU hSubMenu; /* Pop-up menu. */
80 HBITMAP hCheckBit; /* Bitmap when checked. */
81 HBITMAP hUnCheckBit; /* Bitmap when unchecked. */
82 LPWSTR text; /* Item text. */
83 ULONG_PTR dwItemData; /* Application defined. */
84 LPWSTR dwTypeData; /* depends on fMask */
85 HBITMAP hbmpItem; /* bitmap */
86 /* ----------- Wine stuff ----------- */
87 RECT rect; /* Item area (relative to menu window) */
88 UINT xTab; /* X position of text after Tab */
89 SIZE bmpsize; /* size needed for the HBMMENU_CALLBACK
93 /* Popup menu structure */
95 WORD wFlags; /* Menu flags (MF_POPUP, MF_SYSMENU) */
96 WORD wMagic; /* Magic number */
97 WORD Width; /* Width of the whole menu */
98 WORD Height; /* Height of the whole menu */
99 UINT nItems; /* Number of items in the menu */
100 HWND hWnd; /* Window containing the menu */
101 MENUITEM *items; /* Array of menu items */
102 UINT FocusedItem; /* Currently focused item */
103 HWND hwndOwner; /* window receiving the messages for ownerdraw */
104 BOOL bTimeToHide; /* Request hiding when receiving a second click in the top-level menu item */
105 BOOL bScrolling; /* Scroll arrows are active */
106 UINT nScrollPos; /* Current scroll position */
107 UINT nTotalHeight; /* Total height of menu items inside menu */
108 /* ------------ MENUINFO members ------ */
109 DWORD dwStyle; /* Extended menu style */
110 UINT cyMax; /* max height of the whole menu, 0 is screen height */
111 HBRUSH hbrBack; /* brush for menu background */
112 DWORD dwContextHelpID;
113 DWORD dwMenuData; /* application defined value */
114 HMENU hSysMenuOwner; /* Handle to the dummy sys menu holder */
115 SIZE maxBmpSize; /* Maximum size of the bitmap items */
116 } POPUPMENU, *LPPOPUPMENU;
118 /* internal flags for menu tracking */
120 #define TF_ENDMENU 0x10000
121 #define TF_SUSPENDPOPUP 0x20000
122 #define TF_SKIPREMOVE 0x40000
127 HMENU hCurrentMenu; /* current submenu (can be equal to hTopMenu)*/
128 HMENU hTopMenu; /* initial menu */
129 HWND hOwnerWnd; /* where notifications are sent */
133 #define MENU_MAGIC 0x554d /* 'MU' */
138 /* Internal MENU_TrackMenu() flags */
139 #define TPM_INTERNAL 0xF0000000
140 #define TPM_BUTTONDOWN 0x40000000 /* menu was clicked before tracking */
141 #define TPM_POPUPMENU 0x20000000 /* menu is a popup menu */
143 /* Space between 2 columns */
144 #define MENU_COL_SPACE 4
146 /* top and bottom margins for popup menus */
147 #define MENU_TOP_MARGIN 3
148 #define MENU_BOTTOM_MARGIN 2
150 /* (other menu->FocusedItem values give the position of the focused item) */
151 #define NO_SELECTED_ITEM 0xffff
153 #define MENU_ITEM_TYPE(flags) \
154 ((flags) & (MF_STRING | MF_BITMAP | MF_OWNERDRAW | MF_SEPARATOR))
156 /* macro to test that flags do not indicate bitmap, ownerdraw or separator */
157 #define IS_STRING_ITEM(flags) (MENU_ITEM_TYPE ((flags)) == MF_STRING)
158 #define IS_MAGIC_BITMAP(id) ((id) && ((INT_PTR)(id) < 12) && ((INT_PTR)(id) >= -1))
160 #define IS_SYSTEM_MENU(menu) \
161 (!((menu)->wFlags & MF_POPUP) && ((menu)->wFlags & MF_SYSMENU))
163 #define MENUITEMINFO_TYPE_MASK \
164 (MFT_STRING | MFT_BITMAP | MFT_OWNERDRAW | MFT_SEPARATOR | \
165 MFT_MENUBARBREAK | MFT_MENUBREAK | MFT_RADIOCHECK | \
166 MFT_RIGHTORDER | MFT_RIGHTJUSTIFY /* same as MF_HELP */ )
167 #define TYPE_MASK (MENUITEMINFO_TYPE_MASK | MF_POPUP | MF_SYSMENU)
168 #define STATE_MASK (~TYPE_MASK)
169 #define MENUITEMINFO_STATE_MASK (STATE_MASK & ~(MF_BYPOSITION | MF_MOUSESELECT))
171 #define WIN_ALLOWED_MENU(style) ((style & (WS_CHILD | WS_POPUP)) != WS_CHILD)
173 static SIZE menucharsize;
174 static UINT ODitemheight; /* default owner drawn item height */
176 /* Use global popup window because there's no way 2 menus can
177 * be tracked at the same time. */
178 static HWND top_popup;
179 static HMENU top_popup_hmenu;
181 /* Flag set by EndMenu() to force an exit from menu tracking */
182 static BOOL fEndMenu = FALSE;
184 static LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
186 DWORD WINAPI DrawMenuBarTemp(HWND hwnd, HDC hDC, LPRECT lprect, HMENU hMenu, HFONT hFont);
188 /*********************************************************************
189 * menu class descriptor
191 const struct builtin_class_descr MENU_builtin_class =
193 (LPCWSTR)POPUPMENU_CLASS_ATOM, /* name */
194 CS_DROPSHADOW | CS_SAVEBITS | CS_DBLCLKS, /* style */
195 NULL, /* procA (winproc is Unicode only) */
196 PopupMenuWndProc, /* procW */
197 sizeof(HMENU), /* extra */
198 IDC_ARROW, /* cursor */
199 (HBRUSH)(COLOR_MENU+1) /* brush */
203 /***********************************************************************
204 * debug_print_menuitem
206 * Print a menuitem in readable form.
209 #define debug_print_menuitem(pre, mp, post) \
210 do { if (TRACE_ON(menu)) do_debug_print_menuitem(pre, mp, post); } while (0)
212 #define MENUOUT(text) \
213 TRACE("%s%s", (count++ ? "," : ""), (text))
215 #define MENUFLAG(bit,text) \
217 if (flags & (bit)) { flags &= ~(bit); MENUOUT ((text)); } \
220 static void do_debug_print_menuitem(const char *prefix, const MENUITEM *mp,
223 static const char * const hbmmenus[] = { "HBMMENU_CALLBACK", "", "HBMMENU_SYSTEM",
224 "HBMMENU_MBAR_RESTORE", "HBMMENU_MBAR_MINIMIZE", "UNKNOWN BITMAP", "HBMMENU_MBAR_CLOSE",
225 "HBMMENU_MBAR_CLOSE_D", "HBMMENU_MBAR_MINIMIZE_D", "HBMMENU_POPUP_CLOSE",
226 "HBMMENU_POPUP_RESTORE", "HBMMENU_POPUP_MAXIMIZE", "HBMMENU_POPUP_MINIMIZE"};
227 TRACE("%s ", prefix);
229 UINT flags = mp->fType;
230 TRACE( "{ ID=0x%lx", mp->wID);
232 TRACE( ", Sub=%p", mp->hSubMenu);
236 MENUFLAG( MFT_SEPARATOR, "sep");
237 MENUFLAG( MFT_OWNERDRAW, "own");
238 MENUFLAG( MFT_BITMAP, "bit");
239 MENUFLAG(MF_POPUP, "pop");
240 MENUFLAG(MFT_MENUBARBREAK, "barbrk");
241 MENUFLAG(MFT_MENUBREAK, "brk");
242 MENUFLAG(MFT_RADIOCHECK, "radio");
243 MENUFLAG(MFT_RIGHTORDER, "rorder");
244 MENUFLAG(MF_SYSMENU, "sys");
245 MENUFLAG(MFT_RIGHTJUSTIFY, "right"); /* same as MF_HELP */
247 TRACE( "+0x%x", flags);
253 MENUFLAG(MFS_GRAYED, "grey");
254 MENUFLAG(MFS_DEFAULT, "default");
255 MENUFLAG(MFS_DISABLED, "dis");
256 MENUFLAG(MFS_CHECKED, "check");
257 MENUFLAG(MFS_HILITE, "hi");
258 MENUFLAG(MF_USECHECKBITMAPS, "usebit");
259 MENUFLAG(MF_MOUSESELECT, "mouse");
261 TRACE( "+0x%x", flags);
264 TRACE( ", Chk=%p", mp->hCheckBit);
266 TRACE( ", Unc=%p", mp->hUnCheckBit);
268 TRACE( ", Text=%s", debugstr_w(mp->text));
270 TRACE( ", ItemData=0x%08lx", mp->dwItemData);
273 if( IS_MAGIC_BITMAP(mp->hbmpItem))
274 TRACE( ", hbitmap=%s", hbmmenus[ (INT_PTR)mp->hbmpItem + 1]);
276 TRACE( ", hbitmap=%p", mp->hbmpItem);
281 TRACE(" %s\n", postfix);
288 /***********************************************************************
291 * Validate the given menu handle and returns the menu structure pointer.
293 static POPUPMENU *MENU_GetMenu(HMENU hMenu)
295 POPUPMENU *menu = USER_HEAP_LIN_ADDR(hMenu);
296 if (!menu || menu->wMagic != MENU_MAGIC)
298 WARN("invalid menu handle=%p, ptr=%p, magic=%x\n", hMenu, menu, menu? menu->wMagic:0);
304 /***********************************************************************
307 * Get the system menu of a window
309 static HMENU get_win_sys_menu( HWND hwnd )
312 WND *win = WIN_GetPtr( hwnd );
313 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
316 WIN_ReleasePtr( win );
321 /***********************************************************************
324 static HFONT get_menu_font( BOOL bold )
326 static HFONT hMenuFont, hMenuFontBold;
328 HFONT ret = bold ? hMenuFontBold : hMenuFont;
332 NONCLIENTMETRICSW ncm;
335 ncm.cbSize = sizeof(NONCLIENTMETRICSW);
336 SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &ncm, 0);
340 ncm.lfMenuFont.lfWeight += 300;
341 if (ncm.lfMenuFont.lfWeight > 1000) ncm.lfMenuFont.lfWeight = 1000;
343 if (!(ret = CreateFontIndirectW( &ncm.lfMenuFont ))) return 0;
344 prev = InterlockedCompareExchangePointer( (void **)(bold ? &hMenuFontBold : &hMenuFont),
348 /* another thread beat us to it */
356 /***********************************************************************
359 static HBITMAP get_arrow_bitmap(void)
361 static HBITMAP arrow_bitmap;
363 if (!arrow_bitmap) arrow_bitmap = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_MNARROW));
367 /***********************************************************************
368 * get_down_arrow_bitmap
370 static HBITMAP get_down_arrow_bitmap(void)
372 static HBITMAP arrow_bitmap;
374 if (!arrow_bitmap) arrow_bitmap = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_DNARROW));
378 /***********************************************************************
379 * get_down_arrow_inactive_bitmap
381 static HBITMAP get_down_arrow_inactive_bitmap(void)
383 static HBITMAP arrow_bitmap;
385 if (!arrow_bitmap) arrow_bitmap = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_DNARROWI));
389 /***********************************************************************
390 * get_up_arrow_bitmap
392 static HBITMAP get_up_arrow_bitmap(void)
394 static HBITMAP arrow_bitmap;
396 if (!arrow_bitmap) arrow_bitmap = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_UPARROW));
400 /***********************************************************************
401 * get_up_arrow_inactive_bitmap
403 static HBITMAP get_up_arrow_inactive_bitmap(void)
405 static HBITMAP arrow_bitmap;
407 if (!arrow_bitmap) arrow_bitmap = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_UPARROWI));
411 /***********************************************************************
414 * Return the default system menu.
416 static HMENU MENU_CopySysPopup(void)
418 static const WCHAR sysmenuW[] = {'S','Y','S','M','E','N','U',0};
419 HMENU hMenu = LoadMenuW(user32_module, sysmenuW);
423 MENUITEMINFOW miteminfo;
424 POPUPMENU* menu = MENU_GetMenu(hMenu);
425 menu->wFlags |= MF_SYSMENU | MF_POPUP;
426 /* decorate the menu with bitmaps */
427 minfo.cbSize = sizeof( MENUINFO);
428 minfo.dwStyle = MNS_CHECKORBMP;
429 minfo.fMask = MIM_STYLE;
430 SetMenuInfo( hMenu, &minfo);
431 miteminfo.cbSize = sizeof( MENUITEMINFOW);
432 miteminfo.fMask = MIIM_BITMAP;
433 miteminfo.hbmpItem = HBMMENU_POPUP_CLOSE;
434 SetMenuItemInfoW( hMenu, SC_CLOSE, FALSE, &miteminfo);
435 miteminfo.hbmpItem = HBMMENU_POPUP_RESTORE;
436 SetMenuItemInfoW( hMenu, SC_RESTORE, FALSE, &miteminfo);
437 miteminfo.hbmpItem = HBMMENU_POPUP_MAXIMIZE;
438 SetMenuItemInfoW( hMenu, SC_MAXIMIZE, FALSE, &miteminfo);
439 miteminfo.hbmpItem = HBMMENU_POPUP_MINIMIZE;
440 SetMenuItemInfoW( hMenu, SC_MINIMIZE, FALSE, &miteminfo);
441 SetMenuDefaultItem(hMenu, SC_CLOSE, FALSE);
444 ERR("Unable to load default system menu\n" );
446 TRACE("returning %p.\n", hMenu );
452 /**********************************************************************
455 * Create a copy of the system menu. System menu in Windows is
456 * a special menu bar with the single entry - system menu popup.
457 * This popup is presented to the outside world as a "system menu".
458 * However, the real system menu handle is sometimes seen in the
459 * WM_MENUSELECT parameters (and Word 6 likes it this way).
461 static HMENU MENU_GetSysMenu( HWND hWnd, HMENU hPopupMenu )
465 TRACE("loading system menu, hWnd %p, hPopupMenu %p\n", hWnd, hPopupMenu);
466 if ((hMenu = CreateMenu()))
468 POPUPMENU *menu = MENU_GetMenu(hMenu);
469 menu->wFlags = MF_SYSMENU;
470 menu->hWnd = WIN_GetFullHandle( hWnd );
471 TRACE("hWnd %p (hMenu %p)\n", menu->hWnd, hMenu);
474 hPopupMenu = MENU_CopySysPopup();
478 if (GetClassLongW(hWnd, GCL_STYLE) & CS_NOCLOSE)
479 DeleteMenu(hPopupMenu, SC_CLOSE, MF_BYCOMMAND);
481 InsertMenuW( hMenu, -1, MF_SYSMENU | MF_POPUP | MF_BYPOSITION,
482 (UINT_PTR)hPopupMenu, NULL );
484 menu->items[0].fType = MF_SYSMENU | MF_POPUP;
485 menu->items[0].fState = 0;
486 if ((menu = MENU_GetMenu(hPopupMenu))) menu->wFlags |= MF_SYSMENU;
488 TRACE("hMenu=%p (hPopup %p)\n", hMenu, hPopupMenu );
491 DestroyMenu( hMenu );
493 ERR("failed to load system menu!\n");
498 /***********************************************************************
499 * MENU_InitSysMenuPopup
501 * Grey the appropriate items in System menu.
503 static void MENU_InitSysMenuPopup( HMENU hmenu, DWORD style, DWORD clsStyle )
507 gray = !(style & WS_THICKFRAME) || (style & (WS_MAXIMIZE | WS_MINIMIZE));
508 EnableMenuItem( hmenu, SC_SIZE, (gray ? MF_GRAYED : MF_ENABLED) );
509 gray = ((style & WS_MAXIMIZE) != 0);
510 EnableMenuItem( hmenu, SC_MOVE, (gray ? MF_GRAYED : MF_ENABLED) );
511 gray = !(style & WS_MINIMIZEBOX) || (style & WS_MINIMIZE);
512 EnableMenuItem( hmenu, SC_MINIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
513 gray = !(style & WS_MAXIMIZEBOX) || (style & WS_MAXIMIZE);
514 EnableMenuItem( hmenu, SC_MAXIMIZE, (gray ? MF_GRAYED : MF_ENABLED) );
515 gray = !(style & (WS_MAXIMIZE | WS_MINIMIZE));
516 EnableMenuItem( hmenu, SC_RESTORE, (gray ? MF_GRAYED : MF_ENABLED) );
517 gray = (clsStyle & CS_NOCLOSE) != 0;
519 /* The menu item must keep its state if it's disabled */
521 EnableMenuItem( hmenu, SC_CLOSE, MF_GRAYED);
525 /******************************************************************************
527 * UINT MENU_GetStartOfNextColumn(
530 *****************************************************************************/
532 static UINT MENU_GetStartOfNextColumn(
535 POPUPMENU *menu = MENU_GetMenu(hMenu);
539 return NO_SELECTED_ITEM;
541 i = menu->FocusedItem + 1;
542 if( i == NO_SELECTED_ITEM )
545 for( ; i < menu->nItems; ++i ) {
546 if (menu->items[i].fType & (MF_MENUBREAK | MF_MENUBARBREAK))
550 return NO_SELECTED_ITEM;
554 /******************************************************************************
556 * UINT MENU_GetStartOfPrevColumn(
559 *****************************************************************************/
561 static UINT MENU_GetStartOfPrevColumn(
564 POPUPMENU *menu = MENU_GetMenu(hMenu);
568 return NO_SELECTED_ITEM;
570 if( menu->FocusedItem == 0 || menu->FocusedItem == NO_SELECTED_ITEM )
571 return NO_SELECTED_ITEM;
573 /* Find the start of the column */
575 for(i = menu->FocusedItem; i != 0 &&
576 !(menu->items[i].fType & (MF_MENUBREAK | MF_MENUBARBREAK));
580 return NO_SELECTED_ITEM;
582 for(--i; i != 0; --i) {
583 if (menu->items[i].fType & (MF_MENUBREAK | MF_MENUBARBREAK))
587 TRACE("ret %d.\n", i );
594 /***********************************************************************
597 * Find a menu item. Return a pointer on the item, and modifies *hmenu
598 * in case the item was in a sub-menu.
600 static MENUITEM *MENU_FindItem( HMENU *hmenu, UINT *nPos, UINT wFlags )
603 MENUITEM *fallback = NULL;
604 UINT fallback_pos = 0;
607 if ((*hmenu == (HMENU)0xffff) || (!(menu = MENU_GetMenu(*hmenu)))) return NULL;
608 if (wFlags & MF_BYPOSITION)
610 if (*nPos >= menu->nItems) return NULL;
611 return &menu->items[*nPos];
615 MENUITEM *item = menu->items;
616 for (i = 0; i < menu->nItems; i++, item++)
618 if (item->fType & MF_POPUP)
620 HMENU hsubmenu = item->hSubMenu;
621 MENUITEM *subitem = MENU_FindItem( &hsubmenu, nPos, wFlags );
627 else if (item->wID == *nPos)
629 /* fallback to this item if nothing else found */
634 else if (item->wID == *nPos)
643 *nPos = fallback_pos;
648 /***********************************************************************
651 * Find a Sub menu. Return the position of the submenu, and modifies
652 * *hmenu in case it is found in another sub-menu.
653 * If the submenu cannot be found, NO_SELECTED_ITEM is returned.
655 UINT MENU_FindSubMenu( HMENU *hmenu, HMENU hSubTarget )
660 if (((*hmenu)==(HMENU)0xffff) ||
661 (!(menu = MENU_GetMenu(*hmenu))))
662 return NO_SELECTED_ITEM;
664 for (i = 0; i < menu->nItems; i++, item++) {
665 if(!(item->fType & MF_POPUP)) continue;
666 if (item->hSubMenu == hSubTarget) {
670 HMENU hsubmenu = item->hSubMenu;
671 UINT pos = MENU_FindSubMenu( &hsubmenu, hSubTarget );
672 if (pos != NO_SELECTED_ITEM) {
678 return NO_SELECTED_ITEM;
681 /***********************************************************************
684 static void MENU_FreeItemData( MENUITEM* item )
687 HeapFree( GetProcessHeap(), 0, item->text );
690 /***********************************************************************
691 * MENU_AdjustMenuItemRect
693 * Adjust menu item rectangle according to scrolling state.
696 MENU_AdjustMenuItemRect(const POPUPMENU *menu, LPRECT rect)
698 if (menu->bScrolling)
700 UINT arrow_bitmap_height;
703 GetObjectW(get_up_arrow_bitmap(), sizeof(bmp), &bmp);
704 arrow_bitmap_height = bmp.bmHeight;
705 rect->top += arrow_bitmap_height - menu->nScrollPos;
706 rect->bottom += arrow_bitmap_height - menu->nScrollPos;
711 /***********************************************************************
712 * MENU_FindItemByCoords
714 * Find the item at the specified coordinates (screen coords). Does
715 * not work for child windows and therefore should not be called for
716 * an arbitrary system menu.
718 static MENUITEM *MENU_FindItemByCoords( const POPUPMENU *menu,
719 POINT pt, UINT *pos )
725 if (!GetWindowRect(menu->hWnd, &rect)) return NULL;
729 for (i = 0; i < menu->nItems; i++, item++)
732 MENU_AdjustMenuItemRect(menu, &rect);
733 if (PtInRect(&rect, pt))
743 /***********************************************************************
746 * Find the menu item selected by a key press.
747 * Return item id, -1 if none, -2 if we should close the menu.
749 static UINT MENU_FindItemByKey( HWND hwndOwner, HMENU hmenu,
750 WCHAR key, BOOL forceMenuChar )
752 TRACE("\tlooking for '%c' (0x%02x) in [%p]\n", (char)key, key, hmenu );
754 if (!IsMenu( hmenu )) hmenu = GetSubMenu( get_win_sys_menu(hwndOwner), 0);
758 POPUPMENU *menu = MENU_GetMenu( hmenu );
759 MENUITEM *item = menu->items;
766 for (i = 0; i < menu->nItems; i++, item++)
770 WCHAR *p = item->text - 2;
773 p = strchrW (p + 2, '&');
775 while (p != NULL && p [1] == '&');
776 if (p && (toupperW(p[1]) == toupperW(key))) return i;
780 menuchar = SendMessageW( hwndOwner, WM_MENUCHAR,
781 MAKEWPARAM( key, menu->wFlags ), (LPARAM)hmenu );
782 if (HIWORD(menuchar) == 2) return LOWORD(menuchar);
783 if (HIWORD(menuchar) == 1) return (UINT)(-2);
789 /***********************************************************************
790 * MENU_GetBitmapItemSize
792 * Get the size of a bitmap item.
794 static void MENU_GetBitmapItemSize( MENUITEM *lpitem, SIZE *size,
798 HBITMAP bmp = lpitem->hbmpItem;
800 size->cx = size->cy = 0;
802 /* check if there is a magic menu item associated with this item */
803 switch( (INT_PTR) bmp )
805 case (INT_PTR)HBMMENU_CALLBACK:
807 MEASUREITEMSTRUCT measItem;
808 measItem.CtlType = ODT_MENU;
810 measItem.itemID = lpitem->wID;
811 measItem.itemWidth = lpitem->rect.right - lpitem->rect.left;
812 measItem.itemHeight = lpitem->rect.bottom - lpitem->rect.top;
813 measItem.itemData = lpitem->dwItemData;
814 SendMessageW( hwndOwner, WM_MEASUREITEM, lpitem->wID, (LPARAM)&measItem);
815 size->cx = measItem.itemWidth;
816 size->cy = measItem.itemHeight;
820 case (INT_PTR)HBMMENU_SYSTEM:
821 if (lpitem->dwItemData)
823 bmp = (HBITMAP)lpitem->dwItemData;
827 case (INT_PTR)HBMMENU_MBAR_RESTORE:
828 case (INT_PTR)HBMMENU_MBAR_MINIMIZE:
829 case (INT_PTR)HBMMENU_MBAR_MINIMIZE_D:
830 case (INT_PTR)HBMMENU_MBAR_CLOSE:
831 case (INT_PTR)HBMMENU_MBAR_CLOSE_D:
832 size->cx = GetSystemMetrics( SM_CYMENU ) - 4;
835 case (INT_PTR)HBMMENU_POPUP_CLOSE:
836 case (INT_PTR)HBMMENU_POPUP_RESTORE:
837 case (INT_PTR)HBMMENU_POPUP_MAXIMIZE:
838 case (INT_PTR)HBMMENU_POPUP_MINIMIZE:
839 size->cx = GetSystemMetrics( SM_CXMENUSIZE);
840 size->cy = GetSystemMetrics( SM_CYMENUSIZE);
843 if (GetObjectW(bmp, sizeof(bm), &bm ))
845 size->cx = bm.bmWidth;
846 size->cy = bm.bmHeight;
850 /***********************************************************************
851 * MENU_DrawBitmapItem
853 * Draw a bitmap item.
855 static void MENU_DrawBitmapItem( HDC hdc, MENUITEM *lpitem, const RECT *rect,
856 HMENU hmenu, HWND hwndOwner, UINT odaction, BOOL menuBar)
862 int w = rect->right - rect->left;
863 int h = rect->bottom - rect->top;
866 HBITMAP hbmToDraw = lpitem->hbmpItem;
869 /* Check if there is a magic menu item associated with this item */
870 if (IS_MAGIC_BITMAP(hbmToDraw))
876 switch((INT_PTR)hbmToDraw)
878 case (INT_PTR)HBMMENU_SYSTEM:
879 if (lpitem->dwItemData)
881 bmp = (HBITMAP)lpitem->dwItemData;
882 if (!GetObjectW( bmp, sizeof(bm), &bm )) return;
886 static HBITMAP hBmpSysMenu;
888 if (!hBmpSysMenu) hBmpSysMenu = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CLOSE));
890 if (!GetObjectW( bmp, sizeof(bm), &bm )) return;
891 /* only use right half of the bitmap */
892 bmp_xoffset = bm.bmWidth / 2;
893 bm.bmWidth -= bmp_xoffset;
896 case (INT_PTR)HBMMENU_MBAR_RESTORE:
897 flags = DFCS_CAPTIONRESTORE;
899 case (INT_PTR)HBMMENU_MBAR_MINIMIZE:
900 flags = DFCS_CAPTIONMIN;
902 case (INT_PTR)HBMMENU_MBAR_MINIMIZE_D:
903 flags = DFCS_CAPTIONMIN | DFCS_INACTIVE;
905 case (INT_PTR)HBMMENU_MBAR_CLOSE:
906 flags = DFCS_CAPTIONCLOSE;
908 case (INT_PTR)HBMMENU_MBAR_CLOSE_D:
909 flags = DFCS_CAPTIONCLOSE | DFCS_INACTIVE;
911 case (INT_PTR)HBMMENU_CALLBACK:
913 DRAWITEMSTRUCT drawItem;
914 drawItem.CtlType = ODT_MENU;
916 drawItem.itemID = lpitem->wID;
917 drawItem.itemAction = odaction;
918 drawItem.itemState = (lpitem->fState & MF_CHECKED)?ODS_CHECKED:0;
919 drawItem.itemState |= (lpitem->fState & MF_DEFAULT)?ODS_DEFAULT:0;
920 drawItem.itemState |= (lpitem->fState & MF_DISABLED)?ODS_DISABLED:0;
921 drawItem.itemState |= (lpitem->fState & MF_GRAYED)?ODS_GRAYED|ODS_DISABLED:0;
922 drawItem.itemState |= (lpitem->fState & MF_HILITE)?ODS_SELECTED:0;
923 drawItem.hwndItem = (HWND)hmenu;
925 drawItem.itemData = lpitem->dwItemData;
926 drawItem.rcItem = *rect;
927 SendMessageW( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&drawItem);
931 case (INT_PTR)HBMMENU_POPUP_CLOSE:
934 case (INT_PTR)HBMMENU_POPUP_RESTORE:
937 case (INT_PTR)HBMMENU_POPUP_MAXIMIZE:
940 case (INT_PTR)HBMMENU_POPUP_MINIMIZE:
944 FIXME("Magic %p not implemented\n", hbmToDraw);
949 /* draw the magic bitmaps using marlett font characters */
950 /* FIXME: fontsize and the position (x,y) could probably be better */
951 HFONT hfont, hfontsav;
952 LOGFONTW logfont = { 0, 0, 0, 0, FW_NORMAL,
953 0, 0, 0, SYMBOL_CHARSET, 0, 0, 0, 0,
954 { 'M','a','r','l','e','t','t',0 } };
955 logfont.lfHeight = min( h, w) - 5 ;
956 TRACE(" height %d rect %s\n", logfont.lfHeight, wine_dbgstr_rect( rect));
957 hfont = CreateFontIndirectW( &logfont);
958 hfontsav = SelectObject(hdc, hfont);
959 TextOutW( hdc, rect->left, rect->top + 2, &bmchr, 1);
960 SelectObject(hdc, hfontsav);
961 DeleteObject( hfont);
966 InflateRect( &r, -1, -1 );
967 if (lpitem->fState & MF_HILITE) flags |= DFCS_PUSHED;
968 DrawFrameControl( hdc, &r, DFC_CAPTION, flags );
973 if (!bmp || !GetObjectW( bmp, sizeof(bm), &bm )) return;
976 hdcMem = CreateCompatibleDC( hdc );
977 SelectObject( hdcMem, bmp );
979 /* handle fontsize > bitmap_height */
980 top = (h>bm.bmHeight) ? rect->top+(h-bm.bmHeight)/2 : rect->top;
982 rop=((lpitem->fState & MF_HILITE) && !IS_MAGIC_BITMAP(hbmToDraw)) ? NOTSRCCOPY : SRCCOPY;
983 if ((lpitem->fState & MF_HILITE) && lpitem->hbmpItem)
984 SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
985 BitBlt( hdc, left, top, w, h, hdcMem, bmp_xoffset, 0, rop );
990 /***********************************************************************
993 * Calculate the size of the menu item and store it in lpitem->rect.
995 static void MENU_CalcItemSize( HDC hdc, MENUITEM *lpitem, HWND hwndOwner,
996 INT orgX, INT orgY, BOOL menuBar, POPUPMENU* lppop )
999 UINT check_bitmap_width = GetSystemMetrics( SM_CXMENUCHECK );
1000 UINT arrow_bitmap_width;
1004 TRACE("dc=%p owner=%p (%d,%d)\n", hdc, hwndOwner, orgX, orgY);
1005 debug_print_menuitem("MENU_CalcItemSize: menuitem:", lpitem,
1006 (menuBar ? " (MenuBar)" : ""));
1008 GetObjectW( get_arrow_bitmap(), sizeof(bm), &bm );
1009 arrow_bitmap_width = bm.bmWidth;
1011 /* not done in Menu_Init: GetDialogBaseUnits() breaks there */
1012 if( !menucharsize.cx ) {
1013 menucharsize.cx = GdiGetCharDimensions( hdc, NULL, &menucharsize.cy );
1014 /* Win95/98/ME will use menucharsize.cy here. Testing is possible
1015 * but it is unlikely an application will depend on that */
1016 ODitemheight = HIWORD( GetDialogBaseUnits());
1019 SetRect( &lpitem->rect, orgX, orgY, orgX, orgY );
1021 if (lpitem->fType & MF_OWNERDRAW)
1023 MEASUREITEMSTRUCT mis;
1024 mis.CtlType = ODT_MENU;
1026 mis.itemID = lpitem->wID;
1027 mis.itemData = lpitem->dwItemData;
1028 mis.itemHeight = ODitemheight;
1030 SendMessageW( hwndOwner, WM_MEASUREITEM, 0, (LPARAM)&mis );
1031 /* Tests reveal that Windows ( Win95 thru WinXP) adds twice the average
1032 * width of a menufont character to the width of an owner-drawn menu.
1034 lpitem->rect.right += mis.itemWidth + 2 * menucharsize.cx;
1036 /* under at least win95 you seem to be given a standard
1037 height for the menu and the height value is ignored */
1038 lpitem->rect.bottom += GetSystemMetrics(SM_CYMENUSIZE);
1040 lpitem->rect.bottom += mis.itemHeight;
1042 TRACE("id=%04lx size=%dx%d\n",
1043 lpitem->wID, lpitem->rect.right-lpitem->rect.left,
1044 lpitem->rect.bottom-lpitem->rect.top);
1048 if (lpitem->fType & MF_SEPARATOR)
1050 lpitem->rect.bottom += GetSystemMetrics( SM_CYMENUSIZE)/2;
1052 lpitem->rect.right += arrow_bitmap_width + menucharsize.cx;
1060 if (lpitem->hbmpItem) {
1063 MENU_GetBitmapItemSize(lpitem, &size, hwndOwner);
1064 /* Keep the size of the bitmap in callback mode to be able
1065 * to draw it correctly */
1066 lpitem->bmpsize = size;
1067 lppop->maxBmpSize.cx = max( lppop->maxBmpSize.cx, size.cx);
1068 lppop->maxBmpSize.cy = max( lppop->maxBmpSize.cy, size.cy);
1069 lpitem->rect.right += size.cx + 2;
1070 itemheight = size.cy + 2;
1072 if( !(lppop->dwStyle & MNS_NOCHECK))
1073 lpitem->rect.right += check_bitmap_width;
1074 lpitem->rect.right += 4 + menucharsize.cx;
1075 lpitem->xTab = lpitem->rect.right;
1076 lpitem->rect.right += arrow_bitmap_width;
1077 } else if (lpitem->hbmpItem) { /* menuBar */
1080 MENU_GetBitmapItemSize( lpitem, &size, hwndOwner );
1081 lpitem->bmpsize = size;
1082 lpitem->rect.right += size.cx;
1083 if( lpitem->text) lpitem->rect.right += 2;
1084 itemheight = size.cy;
1087 /* it must be a text item - unless it's the system menu */
1088 if (!(lpitem->fType & MF_SYSMENU) && lpitem->text) {
1089 HFONT hfontOld = NULL;
1090 RECT rc = lpitem->rect;
1091 LONG txtheight, txtwidth;
1093 if ( lpitem->fState & MFS_DEFAULT ) {
1094 hfontOld = SelectObject( hdc, get_menu_font(TRUE) );
1097 txtheight = DrawTextW( hdc, lpitem->text, -1, &rc,
1098 DT_SINGLELINE|DT_CALCRECT);
1099 lpitem->rect.right += rc.right - rc.left;
1100 itemheight = max( max( itemheight, txtheight),
1101 GetSystemMetrics( SM_CYMENU) - 1);
1102 lpitem->rect.right += 2 * menucharsize.cx;
1104 if ((p = strchrW( lpitem->text, '\t' )) != NULL) {
1107 int n = (int)( p - lpitem->text);
1108 /* Item contains a tab (only meaningful in popup menus) */
1109 /* get text size before the tab */
1110 txtheight = DrawTextW( hdc, lpitem->text, n, &rc,
1111 DT_SINGLELINE|DT_CALCRECT);
1112 txtwidth = rc.right - rc.left;
1113 p += 1; /* advance past the Tab */
1114 /* get text size after the tab */
1115 tmpheight = DrawTextW( hdc, p, -1, &tmprc,
1116 DT_SINGLELINE|DT_CALCRECT);
1117 lpitem->xTab += txtwidth;
1118 txtheight = max( txtheight, tmpheight);
1119 txtwidth += menucharsize.cx + /* space for the tab */
1120 tmprc.right - tmprc.left; /* space for the short cut */
1122 txtheight = DrawTextW( hdc, lpitem->text, -1, &rc,
1123 DT_SINGLELINE|DT_CALCRECT);
1124 txtwidth = rc.right - rc.left;
1125 lpitem->xTab += txtwidth;
1127 lpitem->rect.right += 2 + txtwidth;
1128 itemheight = max( itemheight,
1129 max( txtheight + 2, menucharsize.cy + 4));
1131 if (hfontOld) SelectObject (hdc, hfontOld);
1132 } else if( menuBar) {
1133 itemheight = max( itemheight, GetSystemMetrics(SM_CYMENU)-1);
1135 lpitem->rect.bottom += itemheight;
1136 TRACE("%s\n", wine_dbgstr_rect( &lpitem->rect));
1140 /***********************************************************************
1141 * MENU_GetMaxPopupHeight
1144 MENU_GetMaxPopupHeight(const POPUPMENU *lppop)
1147 return lppop->cyMax;
1148 return GetSystemMetrics(SM_CYSCREEN) - GetSystemMetrics(SM_CYBORDER);
1152 /***********************************************************************
1153 * MENU_PopupMenuCalcSize
1155 * Calculate the size of a popup menu.
1157 static void MENU_PopupMenuCalcSize( LPPOPUPMENU lppop )
1162 int orgX, orgY, maxX, maxTab, maxTabWidth, maxHeight;
1164 lppop->Width = lppop->Height = 0;
1165 if (lppop->nItems == 0) return;
1168 SelectObject( hdc, get_menu_font(FALSE));
1173 lppop->maxBmpSize.cx = 0;
1174 lppop->maxBmpSize.cy = 0;
1176 while (start < lppop->nItems)
1178 lpitem = &lppop->items[start];
1180 if( lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))
1181 orgX += MENU_COL_SPACE;
1182 orgY = MENU_TOP_MARGIN;
1184 maxTab = maxTabWidth = 0;
1185 /* Parse items until column break or end of menu */
1186 for (i = start; i < lppop->nItems; i++, lpitem++)
1189 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
1191 MENU_CalcItemSize( hdc, lpitem, lppop->hwndOwner, orgX, orgY, FALSE, lppop );
1192 maxX = max( maxX, lpitem->rect.right );
1193 orgY = lpitem->rect.bottom;
1194 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
1196 maxTab = max( maxTab, lpitem->xTab );
1197 maxTabWidth = max(maxTabWidth,lpitem->rect.right-lpitem->xTab);
1201 /* Finish the column (set all items to the largest width found) */
1202 maxX = max( maxX, maxTab + maxTabWidth );
1203 for (lpitem = &lppop->items[start]; start < i; start++, lpitem++)
1205 lpitem->rect.right = maxX;
1206 if (IS_STRING_ITEM(lpitem->fType) && lpitem->xTab)
1207 lpitem->xTab = maxTab;
1210 lppop->Height = max( lppop->Height, orgY );
1213 lppop->Width = maxX;
1215 /* space for 3d border */
1216 lppop->Height += MENU_BOTTOM_MARGIN;
1219 /* Adjust popup height if it exceeds maximum */
1220 maxHeight = MENU_GetMaxPopupHeight(lppop);
1221 lppop->nTotalHeight = lppop->Height - MENU_TOP_MARGIN;
1222 if (lppop->Height >= maxHeight)
1224 lppop->Height = maxHeight;
1225 lppop->bScrolling = TRUE;
1229 lppop->bScrolling = FALSE;
1232 ReleaseDC( 0, hdc );
1236 /***********************************************************************
1237 * MENU_MenuBarCalcSize
1239 * FIXME: Word 6 implements its own MDI and its own 'close window' bitmap
1240 * height is off by 1 pixel which causes lengthy window relocations when
1241 * active document window is maximized/restored.
1243 * Calculate the size of the menu bar.
1245 static void MENU_MenuBarCalcSize( HDC hdc, LPRECT lprect,
1246 LPPOPUPMENU lppop, HWND hwndOwner )
1249 UINT start, i, helpPos;
1250 int orgX, orgY, maxY;
1252 if ((lprect == NULL) || (lppop == NULL)) return;
1253 if (lppop->nItems == 0) return;
1254 TRACE("lprect %p %s\n", lprect, wine_dbgstr_rect( lprect));
1255 lppop->Width = lprect->right - lprect->left;
1257 maxY = lprect->top+1;
1260 lppop->maxBmpSize.cx = 0;
1261 lppop->maxBmpSize.cy = 0;
1262 while (start < lppop->nItems)
1264 lpitem = &lppop->items[start];
1265 orgX = lprect->left;
1268 /* Parse items until line break or end of menu */
1269 for (i = start; i < lppop->nItems; i++, lpitem++)
1271 if ((helpPos == ~0U) && (lpitem->fType & MF_RIGHTJUSTIFY)) helpPos = i;
1273 (lpitem->fType & (MF_MENUBREAK | MF_MENUBARBREAK))) break;
1275 TRACE("calling MENU_CalcItemSize org=(%d, %d)\n", orgX, orgY );
1276 debug_print_menuitem (" item: ", lpitem, "");
1277 MENU_CalcItemSize( hdc, lpitem, hwndOwner, orgX, orgY, TRUE, lppop );
1279 if (lpitem->rect.right > lprect->right)
1281 if (i != start) break;
1282 else lpitem->rect.right = lprect->right;
1284 maxY = max( maxY, lpitem->rect.bottom );
1285 orgX = lpitem->rect.right;
1288 /* Finish the line (set all items to the largest height found) */
1289 while (start < i) lppop->items[start++].rect.bottom = maxY;
1292 lprect->bottom = maxY;
1293 lppop->Height = lprect->bottom - lprect->top;
1295 /* Flush right all items between the MF_RIGHTJUSTIFY and */
1296 /* the last item (if several lines, only move the last line) */
1297 if (helpPos == ~0U) return;
1298 lpitem = &lppop->items[lppop->nItems-1];
1299 orgY = lpitem->rect.top;
1300 orgX = lprect->right;
1301 for (i = lppop->nItems - 1; i >= helpPos; i--, lpitem--) {
1302 if (lpitem->rect.top != orgY) break; /* Other line */
1303 if (lpitem->rect.right >= orgX) break; /* Too far right already */
1304 lpitem->rect.left += orgX - lpitem->rect.right;
1305 lpitem->rect.right = orgX;
1306 orgX = lpitem->rect.left;
1311 /***********************************************************************
1312 * MENU_DrawScrollArrows
1314 * Draw scroll arrows.
1317 MENU_DrawScrollArrows(const POPUPMENU *lppop, HDC hdc)
1319 HDC hdcMem = CreateCompatibleDC(hdc);
1320 HBITMAP hOrigBitmap;
1321 UINT arrow_bitmap_width, arrow_bitmap_height;
1325 GetObjectW(get_down_arrow_bitmap(), sizeof(bmp), &bmp);
1326 arrow_bitmap_width = bmp.bmWidth;
1327 arrow_bitmap_height = bmp.bmHeight;
1330 if (lppop->nScrollPos)
1331 hOrigBitmap = SelectObject(hdcMem, get_up_arrow_bitmap());
1333 hOrigBitmap = SelectObject(hdcMem, get_up_arrow_inactive_bitmap());
1336 rect.right = lppop->Width;
1337 rect.bottom = arrow_bitmap_height;
1338 FillRect(hdc, &rect, GetSysColorBrush(COLOR_MENU));
1339 BitBlt(hdc, (lppop->Width - arrow_bitmap_width) / 2, 0,
1340 arrow_bitmap_width, arrow_bitmap_height, hdcMem, 0, 0, SRCCOPY);
1341 rect.top = lppop->Height - arrow_bitmap_height;
1342 rect.bottom = lppop->Height;
1343 FillRect(hdc, &rect, GetSysColorBrush(COLOR_MENU));
1344 if (lppop->nScrollPos < lppop->nTotalHeight - (MENU_GetMaxPopupHeight(lppop) - 2 * arrow_bitmap_height))
1345 SelectObject(hdcMem, get_down_arrow_bitmap());
1347 SelectObject(hdcMem, get_down_arrow_inactive_bitmap());
1348 BitBlt(hdc, (lppop->Width - arrow_bitmap_width) / 2,
1349 lppop->Height - arrow_bitmap_height,
1350 arrow_bitmap_width, arrow_bitmap_height, hdcMem, 0, 0, SRCCOPY);
1351 SelectObject(hdcMem, hOrigBitmap);
1356 /***********************************************************************
1359 * Draws the popup-menu arrow.
1361 static void draw_popup_arrow( HDC hdc, RECT rect, UINT arrow_bitmap_width,
1362 UINT arrow_bitmap_height)
1364 HDC hdcMem = CreateCompatibleDC( hdc );
1365 HBITMAP hOrigBitmap;
1367 hOrigBitmap = SelectObject( hdcMem, get_arrow_bitmap() );
1368 BitBlt( hdc, rect.right - arrow_bitmap_width - 1,
1369 (rect.top + rect.bottom - arrow_bitmap_height) / 2,
1370 arrow_bitmap_width, arrow_bitmap_height,
1371 hdcMem, 0, 0, SRCCOPY );
1372 SelectObject( hdcMem, hOrigBitmap );
1375 /***********************************************************************
1378 * Draw a single menu item.
1380 static void MENU_DrawMenuItem( HWND hwnd, HMENU hmenu, HWND hwndOwner, HDC hdc, MENUITEM *lpitem,
1381 UINT height, BOOL menuBar, UINT odaction )
1384 BOOL flat_menu = FALSE;
1386 UINT arrow_bitmap_width = 0, arrow_bitmap_height = 0;
1387 POPUPMENU *menu = MENU_GetMenu(hmenu);
1390 debug_print_menuitem("MENU_DrawMenuItem: ", lpitem, "");
1394 GetObjectW( get_arrow_bitmap(), sizeof(bmp), &bmp );
1395 arrow_bitmap_width = bmp.bmWidth;
1396 arrow_bitmap_height = bmp.bmHeight;
1399 if (lpitem->fType & MF_SYSMENU)
1401 if( !IsIconic(hwnd) )
1402 NC_DrawSysButton( hwnd, hdc, lpitem->fState & (MF_HILITE | MF_MOUSESELECT) );
1406 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
1407 bkgnd = (menuBar && flat_menu) ? COLOR_MENUBAR : COLOR_MENU;
1411 if (lpitem->fState & MF_HILITE)
1413 if(menuBar && !flat_menu) {
1414 SetTextColor(hdc, GetSysColor(COLOR_MENUTEXT));
1415 SetBkColor(hdc, GetSysColor(COLOR_MENU));
1417 if(lpitem->fState & MF_GRAYED)
1418 SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
1420 SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
1421 SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT));
1426 if (lpitem->fState & MF_GRAYED)
1427 SetTextColor( hdc, GetSysColor( COLOR_GRAYTEXT ) );
1429 SetTextColor( hdc, GetSysColor( COLOR_MENUTEXT ) );
1430 SetBkColor( hdc, GetSysColor( bkgnd ) );
1433 TRACE("rect=%s\n", wine_dbgstr_rect( &lpitem->rect));
1434 rect = lpitem->rect;
1435 MENU_AdjustMenuItemRect(MENU_GetMenu(hmenu), &rect);
1437 if (lpitem->fType & MF_OWNERDRAW)
1440 ** Experimentation under Windows reveals that an owner-drawn
1441 ** menu is given the rectangle which includes the space it requested
1442 ** in its response to WM_MEASUREITEM _plus_ width for a checkmark
1443 ** and a popup-menu arrow. This is the value of lpitem->rect.
1444 ** Windows will leave all drawing to the application except for
1445 ** the popup-menu arrow. Windows always draws that itself, after
1446 ** the menu owner has finished drawing.
1450 dis.CtlType = ODT_MENU;
1452 dis.itemID = lpitem->wID;
1453 dis.itemData = lpitem->dwItemData;
1455 if (lpitem->fState & MF_CHECKED) dis.itemState |= ODS_CHECKED;
1456 if (lpitem->fState & MF_GRAYED) dis.itemState |= ODS_GRAYED|ODS_DISABLED;
1457 if (lpitem->fState & MF_HILITE) dis.itemState |= ODS_SELECTED;
1458 dis.itemAction = odaction; /* ODA_DRAWENTIRE | ODA_SELECT | ODA_FOCUS; */
1459 dis.hwndItem = (HWND)hmenu;
1462 TRACE("Ownerdraw: owner=%p itemID=%d, itemState=%d, itemAction=%d, "
1463 "hwndItem=%p, hdc=%p, rcItem=%s\n", hwndOwner,
1464 dis.itemID, dis.itemState, dis.itemAction, dis.hwndItem,
1465 dis.hDC, wine_dbgstr_rect( &dis.rcItem));
1466 SendMessageW( hwndOwner, WM_DRAWITEM, 0, (LPARAM)&dis );
1467 /* Draw the popup-menu arrow */
1468 if (lpitem->fType & MF_POPUP)
1469 draw_popup_arrow( hdc, rect, arrow_bitmap_width,
1470 arrow_bitmap_height);
1474 if (menuBar && (lpitem->fType & MF_SEPARATOR)) return;
1476 if (lpitem->fState & MF_HILITE)
1480 InflateRect (&rect, -1, -1);
1481 FillRect(hdc, &rect, GetSysColorBrush(COLOR_MENUHILIGHT));
1482 InflateRect (&rect, 1, 1);
1483 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1488 DrawEdge(hdc, &rect, BDR_SUNKENOUTER, BF_RECT);
1490 FillRect(hdc, &rect, GetSysColorBrush(COLOR_HIGHLIGHT));
1494 FillRect( hdc, &rect, GetSysColorBrush(bkgnd) );
1496 SetBkMode( hdc, TRANSPARENT );
1498 /* vertical separator */
1499 if (!menuBar && (lpitem->fType & MF_MENUBARBREAK))
1504 rc.left -= MENU_COL_SPACE / 2 + 1;
1506 rc.bottom = height - 3;
1509 oldPen = SelectObject( hdc, SYSCOLOR_GetPen(COLOR_BTNSHADOW) );
1510 MoveToEx( hdc, rc.left, rc.top, NULL );
1511 LineTo( hdc, rc.left, rc.bottom );
1512 SelectObject( hdc, oldPen );
1515 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_LEFT);
1518 /* horizontal separator */
1519 if (lpitem->fType & MF_SEPARATOR)
1526 rc.top = ( rc.top + rc.bottom) / 2;
1529 oldPen = SelectObject( hdc, SYSCOLOR_GetPen(COLOR_BTNSHADOW) );
1530 MoveToEx( hdc, rc.left, rc.top, NULL );
1531 LineTo( hdc, rc.right, rc.top );
1532 SelectObject( hdc, oldPen );
1535 DrawEdge (hdc, &rc, EDGE_ETCHED, BF_TOP);
1539 /* helper lines for debugging */
1540 /* FrameRect(hdc, &rect, GetStockObject(BLACK_BRUSH));
1541 SelectObject( hdc, SYSCOLOR_GetPen(COLOR_WINDOWFRAME) );
1542 MoveToEx( hdc, rect.left, (rect.top + rect.bottom)/2, NULL );
1543 LineTo( hdc, rect.right, (rect.top + rect.bottom)/2 );
1546 if (lpitem->hbmpItem) {
1547 /* calculate the bitmap rectangle in coordinates relative
1548 * to the item rectangle */
1550 if( lpitem->hbmpItem == HBMMENU_CALLBACK)
1553 bmprc.left = lpitem->text ? menucharsize.cx : 0;
1555 else if (menu->dwStyle & MNS_NOCHECK)
1557 else if (menu->dwStyle & MNS_CHECKORBMP)
1560 bmprc.left = 4 + GetSystemMetrics(SM_CXMENUCHECK);
1561 bmprc.right = bmprc.left + lpitem->bmpsize.cx;
1562 if( menuBar && !(lpitem->hbmpItem == HBMMENU_CALLBACK))
1565 bmprc.top = (rect.bottom - rect.top -
1566 lpitem->bmpsize.cy) / 2;
1567 bmprc.bottom = bmprc.top + lpitem->bmpsize.cy;
1573 INT y = rect.top + rect.bottom;
1575 int checked = FALSE;
1576 UINT check_bitmap_width = GetSystemMetrics( SM_CXMENUCHECK );
1577 UINT check_bitmap_height = GetSystemMetrics( SM_CYMENUCHECK );
1578 /* Draw the check mark
1581 * Custom checkmark bitmaps are monochrome but not always 1bpp.
1583 if( !(menu->dwStyle & MNS_NOCHECK)) {
1584 bm = (lpitem->fState & MF_CHECKED) ? lpitem->hCheckBit :
1585 lpitem->hUnCheckBit;
1586 if (bm) /* we have a custom bitmap */
1588 HDC hdcMem = CreateCompatibleDC( hdc );
1590 SelectObject( hdcMem, bm );
1591 BitBlt( hdc, rc.left, (y - check_bitmap_height) / 2,
1592 check_bitmap_width, check_bitmap_height,
1593 hdcMem, 0, 0, SRCCOPY );
1597 else if (lpitem->fState & MF_CHECKED) /* standard bitmaps */
1600 HBITMAP bm = CreateBitmap( check_bitmap_width,
1601 check_bitmap_height, 1, 1, NULL );
1602 HDC hdcMem = CreateCompatibleDC( hdc );
1604 SelectObject( hdcMem, bm );
1605 SetRect( &r, 0, 0, check_bitmap_width, check_bitmap_height);
1606 DrawFrameControl( hdcMem, &r, DFC_MENU,
1607 (lpitem->fType & MFT_RADIOCHECK) ?
1608 DFCS_MENUBULLET : DFCS_MENUCHECK );
1609 BitBlt( hdc, rc.left, (y - r.bottom) / 2, r.right, r.bottom,
1610 hdcMem, 0, 0, SRCCOPY );
1616 if( lpitem->hbmpItem &&
1617 !( checked && (menu->dwStyle & MNS_CHECKORBMP))) {
1619 /* some applications make this assumption on the DC's origin */
1620 SetViewportOrgEx( hdc, rect.left, rect.top, &origorg);
1621 MENU_DrawBitmapItem(hdc, lpitem, &bmprc, hmenu, hwndOwner,
1623 SetViewportOrgEx( hdc, origorg.x, origorg.y, NULL);
1625 /* Draw the popup-menu arrow */
1626 if (lpitem->fType & MF_POPUP)
1627 draw_popup_arrow( hdc, rect, arrow_bitmap_width,
1628 arrow_bitmap_height);
1630 if( !(menu->dwStyle & MNS_NOCHECK))
1631 rect.left += check_bitmap_width;
1632 rect.right -= arrow_bitmap_width;
1634 else if( lpitem->hbmpItem)
1635 { /* Draw the bitmap */
1638 SetViewportOrgEx( hdc, rect.left, rect.top, &origorg);
1639 MENU_DrawBitmapItem( hdc, lpitem, &bmprc, hmenu, hwndOwner,
1641 SetViewportOrgEx( hdc, origorg.x, origorg.y, NULL);
1643 /* process text if present */
1649 UINT uFormat = (menuBar) ?
1650 DT_CENTER | DT_VCENTER | DT_SINGLELINE :
1651 DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1653 if( !(menu->dwStyle & MNS_CHECKORBMP))
1654 rect.left += menu->maxBmpSize.cx;
1656 if ( lpitem->fState & MFS_DEFAULT )
1658 hfontOld = SelectObject( hdc, get_menu_font(TRUE) );
1662 if( lpitem->hbmpItem)
1663 rect.left += lpitem->bmpsize.cx;
1664 if( !(lpitem->hbmpItem == HBMMENU_CALLBACK))
1665 rect.left += menucharsize.cx;
1666 rect.right -= menucharsize.cx;
1669 for (i = 0; lpitem->text[i]; i++)
1670 if ((lpitem->text[i] == '\t') || (lpitem->text[i] == '\b'))
1673 if(lpitem->fState & MF_GRAYED)
1675 if (!(lpitem->fState & MF_HILITE) )
1677 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1678 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1679 DrawTextW( hdc, lpitem->text, i, &rect, uFormat );
1680 --rect.left; --rect.top; --rect.right; --rect.bottom;
1682 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1685 DrawTextW( hdc, lpitem->text, i, &rect, uFormat);
1687 /* paint the shortcut text */
1688 if (!menuBar && lpitem->text[i]) /* There's a tab or flush-right char */
1690 if (lpitem->text[i] == '\t')
1692 rect.left = lpitem->xTab;
1693 uFormat = DT_LEFT | DT_VCENTER | DT_SINGLELINE;
1697 rect.right = lpitem->xTab;
1698 uFormat = DT_RIGHT | DT_VCENTER | DT_SINGLELINE;
1701 if(lpitem->fState & MF_GRAYED)
1703 if (!(lpitem->fState & MF_HILITE) )
1705 ++rect.left; ++rect.top; ++rect.right; ++rect.bottom;
1706 SetTextColor(hdc, RGB(0xff, 0xff, 0xff));
1707 DrawTextW( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1708 --rect.left; --rect.top; --rect.right; --rect.bottom;
1710 SetTextColor(hdc, RGB(0x80, 0x80, 0x80));
1712 DrawTextW( hdc, lpitem->text + i + 1, -1, &rect, uFormat );
1716 SelectObject (hdc, hfontOld);
1721 /***********************************************************************
1722 * MENU_DrawPopupMenu
1724 * Paint a popup menu.
1726 static void MENU_DrawPopupMenu( HWND hwnd, HDC hdc, HMENU hmenu )
1728 HBRUSH hPrevBrush = 0;
1731 TRACE("wnd=%p dc=%p menu=%p\n", hwnd, hdc, hmenu);
1733 GetClientRect( hwnd, &rect );
1735 if((hPrevBrush = SelectObject( hdc, GetSysColorBrush(COLOR_MENU) ))
1736 && (SelectObject( hdc, get_menu_font(FALSE))))
1740 Rectangle( hdc, rect.left, rect.top, rect.right, rect.bottom );
1742 hPrevPen = SelectObject( hdc, GetStockObject( NULL_PEN ) );
1746 BOOL flat_menu = FALSE;
1748 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
1750 FrameRect(hdc, &rect, GetSysColorBrush(COLOR_BTNSHADOW));
1752 DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT);
1754 if( (menu = MENU_GetMenu( hmenu )))
1756 /* draw menu items */
1763 for( u = menu->nItems; u > 0; u--, item++)
1764 MENU_DrawMenuItem( hwnd, hmenu, menu->hwndOwner, hdc,
1765 item, menu->Height, FALSE, ODA_DRAWENTIRE );
1767 /* draw scroll arrows */
1768 if (menu->bScrolling)
1769 MENU_DrawScrollArrows(menu, hdc);
1773 SelectObject( hdc, hPrevBrush );
1778 /***********************************************************************
1781 * Paint a menu bar. Returns the height of the menu bar.
1782 * called from [windows/nonclient.c]
1784 UINT MENU_DrawMenuBar( HDC hDC, LPRECT lprect, HWND hwnd,
1789 HMENU hMenu = GetMenu(hwnd);
1791 lppop = MENU_GetMenu( hMenu );
1792 if (lppop == NULL || lprect == NULL)
1794 return GetSystemMetrics(SM_CYMENU);
1799 hfontOld = SelectObject( hDC, get_menu_font(FALSE));
1801 if (lppop->Height == 0)
1802 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
1804 lprect->bottom = lprect->top + lppop->Height;
1806 if (hfontOld) SelectObject( hDC, hfontOld);
1807 return lppop->Height;
1810 return DrawMenuBarTemp(hwnd, hDC, lprect, hMenu, NULL);
1814 /***********************************************************************
1817 * Display a popup menu.
1819 static BOOL MENU_ShowPopup( HWND hwndOwner, HMENU hmenu, UINT id, UINT flags,
1820 INT x, INT y, INT xanchor, INT yanchor )
1828 TRACE("owner=%p hmenu=%p id=0x%04x x=0x%04x y=0x%04x xa=0x%04x ya=0x%04x\n",
1829 hwndOwner, hmenu, id, x, y, xanchor, yanchor);
1831 if (!(menu = MENU_GetMenu( hmenu ))) return FALSE;
1832 if (menu->FocusedItem != NO_SELECTED_ITEM)
1834 menu->items[menu->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1835 menu->FocusedItem = NO_SELECTED_ITEM;
1838 /* store the owner for DrawItem */
1839 menu->hwndOwner = hwndOwner;
1841 menu->nScrollPos = 0;
1842 MENU_PopupMenuCalcSize( menu );
1844 /* adjust popup menu pos so that it fits within the desktop */
1846 width = menu->Width + GetSystemMetrics(SM_CXBORDER);
1847 height = menu->Height + GetSystemMetrics(SM_CYBORDER);
1849 /* FIXME: should use item rect */
1852 monitor = MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST );
1853 info.cbSize = sizeof(info);
1854 GetMonitorInfoW( monitor, &info );
1856 if( flags & TPM_RIGHTALIGN ) x -= width;
1857 if( flags & TPM_CENTERALIGN ) x -= width / 2;
1859 if( flags & TPM_BOTTOMALIGN ) y -= height;
1860 if( flags & TPM_VCENTERALIGN ) y -= height / 2;
1862 if( x + width > info.rcWork.right)
1864 if( xanchor && x >= width - xanchor )
1865 x -= width - xanchor;
1867 if( x + width > info.rcWork.right)
1868 x = info.rcWork.right - width;
1870 if( x < info.rcWork.left ) x = info.rcWork.left;
1872 if( y + height > info.rcWork.bottom)
1874 if( yanchor && y >= height + yanchor )
1875 y -= height + yanchor;
1877 if( y + height > info.rcWork.bottom)
1878 y = info.rcWork.bottom - height;
1880 if( y < info.rcWork.top ) y = info.rcWork.top;
1882 /* NOTE: In Windows, top menu popup is not owned. */
1883 menu->hWnd = CreateWindowExW( 0, (LPCWSTR)POPUPMENU_CLASS_ATOM, NULL,
1884 WS_POPUP, x, y, width, height,
1885 hwndOwner, 0, (HINSTANCE)GetWindowLongPtrW(hwndOwner, GWLP_HINSTANCE),
1887 if( !menu->hWnd ) return FALSE;
1889 top_popup = menu->hWnd;
1890 top_popup_hmenu = hmenu;
1892 /* Display the window */
1894 SetWindowPos( menu->hWnd, HWND_TOPMOST, 0, 0, 0, 0,
1895 SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
1896 UpdateWindow( menu->hWnd );
1901 /***********************************************************************
1902 * MENU_EnsureMenuItemVisible
1905 MENU_EnsureMenuItemVisible(LPPOPUPMENU lppop, UINT wIndex, HDC hdc)
1907 if (lppop->bScrolling)
1909 MENUITEM *item = &lppop->items[wIndex];
1910 UINT nMaxHeight = MENU_GetMaxPopupHeight(lppop);
1911 UINT nOldPos = lppop->nScrollPos;
1913 UINT arrow_bitmap_height;
1916 GetClientRect(lppop->hWnd, &rc);
1918 GetObjectW(get_down_arrow_bitmap(), sizeof(bmp), &bmp);
1919 arrow_bitmap_height = bmp.bmHeight;
1921 rc.top += arrow_bitmap_height;
1922 rc.bottom -= arrow_bitmap_height + MENU_BOTTOM_MARGIN;
1924 nMaxHeight -= GetSystemMetrics(SM_CYBORDER) + 2 * arrow_bitmap_height;
1925 if (item->rect.bottom > lppop->nScrollPos + nMaxHeight)
1928 lppop->nScrollPos = item->rect.bottom - nMaxHeight;
1929 ScrollWindow(lppop->hWnd, 0, nOldPos - lppop->nScrollPos, &rc, &rc);
1930 MENU_DrawScrollArrows(lppop, hdc);
1932 else if (item->rect.top - MENU_TOP_MARGIN < lppop->nScrollPos)
1934 lppop->nScrollPos = item->rect.top - MENU_TOP_MARGIN;
1935 ScrollWindow(lppop->hWnd, 0, nOldPos - lppop->nScrollPos, &rc, &rc);
1936 MENU_DrawScrollArrows(lppop, hdc);
1942 /***********************************************************************
1945 static void MENU_SelectItem( HWND hwndOwner, HMENU hmenu, UINT wIndex,
1946 BOOL sendMenuSelect, HMENU topmenu )
1951 TRACE("owner=%p menu=%p index=0x%04x select=0x%04x\n", hwndOwner, hmenu, wIndex, sendMenuSelect);
1953 lppop = MENU_GetMenu( hmenu );
1954 if ((!lppop) || (!lppop->nItems) || (!lppop->hWnd)) return;
1956 if (lppop->FocusedItem == wIndex) return;
1957 if (lppop->wFlags & MF_POPUP) hdc = GetDC( lppop->hWnd );
1958 else hdc = GetDCEx( lppop->hWnd, 0, DCX_CACHE | DCX_WINDOW);
1960 top_popup = lppop->hWnd;
1961 top_popup_hmenu = hmenu;
1964 SelectObject( hdc, get_menu_font(FALSE));
1966 /* Clear previous highlighted item */
1967 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1969 lppop->items[lppop->FocusedItem].fState &= ~(MF_HILITE|MF_MOUSESELECT);
1970 MENU_DrawMenuItem(lppop->hWnd, hmenu, hwndOwner, hdc,&lppop->items[lppop->FocusedItem],
1971 lppop->Height, !(lppop->wFlags & MF_POPUP),
1975 /* Highlight new item (if any) */
1976 lppop->FocusedItem = wIndex;
1977 if (lppop->FocusedItem != NO_SELECTED_ITEM)
1979 if(!(lppop->items[wIndex].fType & MF_SEPARATOR)) {
1980 lppop->items[wIndex].fState |= MF_HILITE;
1981 MENU_EnsureMenuItemVisible(lppop, wIndex, hdc);
1982 MENU_DrawMenuItem( lppop->hWnd, hmenu, hwndOwner, hdc,
1983 &lppop->items[wIndex], lppop->Height,
1984 !(lppop->wFlags & MF_POPUP), ODA_SELECT );
1988 MENUITEM *ip = &lppop->items[lppop->FocusedItem];
1989 SendMessageW( hwndOwner, WM_MENUSELECT,
1990 MAKEWPARAM(ip->fType & MF_POPUP ? wIndex: ip->wID,
1991 ip->fType | ip->fState |
1992 (lppop->wFlags & MF_SYSMENU)), (LPARAM)hmenu);
1995 else if (sendMenuSelect) {
1998 if((pos=MENU_FindSubMenu(&topmenu, hmenu))!=NO_SELECTED_ITEM){
1999 POPUPMENU *ptm = MENU_GetMenu( topmenu );
2000 MENUITEM *ip = &ptm->items[pos];
2001 SendMessageW( hwndOwner, WM_MENUSELECT, MAKEWPARAM(pos,
2002 ip->fType | ip->fState |
2003 (ptm->wFlags & MF_SYSMENU)), (LPARAM)topmenu);
2007 ReleaseDC( lppop->hWnd, hdc );
2011 /***********************************************************************
2012 * MENU_MoveSelection
2014 * Moves currently selected item according to the offset parameter.
2015 * If there is no selection then it should select the last item if
2016 * offset is ITEM_PREV or the first item if offset is ITEM_NEXT.
2018 static void MENU_MoveSelection( HWND hwndOwner, HMENU hmenu, INT offset )
2023 TRACE("hwnd=%p hmenu=%p off=0x%04x\n", hwndOwner, hmenu, offset);
2025 menu = MENU_GetMenu( hmenu );
2026 if ((!menu) || (!menu->items)) return;
2028 if ( menu->FocusedItem != NO_SELECTED_ITEM )
2030 if( menu->nItems == 1 ) return; else
2031 for (i = menu->FocusedItem + offset ; i >= 0 && i < menu->nItems
2033 if (!(menu->items[i].fType & MF_SEPARATOR))
2035 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
2040 for ( i = (offset > 0) ? 0 : menu->nItems - 1;
2041 i >= 0 && i < menu->nItems ; i += offset)
2042 if (!(menu->items[i].fType & MF_SEPARATOR))
2044 MENU_SelectItem( hwndOwner, hmenu, i, TRUE, 0 );
2050 /**********************************************************************
2053 * Set an item's flags, id and text ptr. Called by InsertMenu() and
2056 static BOOL MENU_SetItemData( MENUITEM *item, UINT flags, UINT_PTR id,
2059 debug_print_menuitem("MENU_SetItemData from: ", item, "");
2060 TRACE("flags=%x str=%p\n", flags, str);
2062 if (IS_STRING_ITEM(flags))
2064 LPWSTR prevText = item->text;
2067 flags |= MF_SEPARATOR;
2073 /* Item beginning with a backspace is a help item */
2079 if (!(text = HeapAlloc( GetProcessHeap(), 0, (strlenW(str)+1) * sizeof(WCHAR) )))
2081 strcpyW( text, str );
2084 item->hbmpItem = NULL;
2085 HeapFree( GetProcessHeap(), 0, prevText );
2087 else if(( flags & MFT_BITMAP)) {
2088 item->hbmpItem = HBITMAP_32(LOWORD(str));
2089 /* setting bitmap clears text */
2090 HeapFree( GetProcessHeap(), 0, item->text );
2094 if (flags & MF_SEPARATOR) flags |= MF_GRAYED | MF_DISABLED;
2096 if (flags & MF_OWNERDRAW)
2097 item->dwItemData = (DWORD_PTR)str;
2099 item->dwItemData = 0;
2101 if ((item->fType & MF_POPUP) && (flags & MF_POPUP) && (item->hSubMenu != (HMENU)id) )
2102 DestroyMenu( item->hSubMenu ); /* ModifyMenu() spec */
2104 if (flags & MF_POPUP)
2106 POPUPMENU *menu = MENU_GetMenu((HMENU)id);
2107 if (menu) menu->wFlags |= MF_POPUP;
2119 if (flags & MF_POPUP) item->hSubMenu = (HMENU)id;
2121 if ((item->fType & MF_POPUP) && !(flags & MF_POPUP) )
2122 flags |= MF_POPUP; /* keep popup */
2124 item->fType = flags & TYPE_MASK;
2125 /* MFS_DEFAULT is not accepted. MF_HILITE is not listed as a valid flag
2126 for ModifyMenu, but Windows accepts it */
2127 item->fState = flags & MENUITEMINFO_STATE_MASK & ~MFS_DEFAULT;
2129 /* Don't call SetRectEmpty here! */
2131 debug_print_menuitem("MENU_SetItemData to : ", item, "");
2136 /**********************************************************************
2139 * Insert (allocate) a new item into a menu.
2141 static MENUITEM *MENU_InsertItem( HMENU hMenu, UINT pos, UINT flags )
2146 if (!(menu = MENU_GetMenu(hMenu)))
2149 /* Find where to insert new item */
2151 if (flags & MF_BYPOSITION) {
2152 if (pos > menu->nItems)
2155 if (!MENU_FindItem( &hMenu, &pos, flags ))
2158 if (!(menu = MENU_GetMenu( hMenu )))
2163 /* Make sure that MDI system buttons stay on the right side.
2164 * Note: XP treats only bitmap handles 1 - 6 as "magic" ones
2165 * regardless of their id.
2167 while (pos > 0 && (INT_PTR)menu->items[pos - 1].hbmpItem >= (INT_PTR)HBMMENU_SYSTEM &&
2168 (INT_PTR)menu->items[pos - 1].hbmpItem <= (INT_PTR)HBMMENU_MBAR_CLOSE_D)
2171 TRACE("inserting at %u by pos %u\n", pos, flags & MF_BYPOSITION);
2173 /* Create new items array */
2175 newItems = HeapAlloc( GetProcessHeap(), 0, sizeof(MENUITEM) * (menu->nItems+1) );
2178 WARN("allocation failed\n" );
2181 if (menu->nItems > 0)
2183 /* Copy the old array into the new one */
2184 if (pos > 0) memcpy( newItems, menu->items, pos * sizeof(MENUITEM) );
2185 if (pos < menu->nItems) memcpy( &newItems[pos+1], &menu->items[pos],
2186 (menu->nItems-pos)*sizeof(MENUITEM) );
2187 HeapFree( GetProcessHeap(), 0, menu->items );
2189 menu->items = newItems;
2191 memset( &newItems[pos], 0, sizeof(*newItems) );
2192 menu->Height = 0; /* force size recalculate */
2193 return &newItems[pos];
2197 /**********************************************************************
2198 * MENU_ParseResource
2200 * Parse a standard menu resource and add items to the menu.
2201 * Return a pointer to the end of the resource.
2203 * NOTE: flags is equivalent to the mtOption field
2205 static LPCSTR MENU_ParseResource( LPCSTR res, HMENU hMenu, BOOL unicode )
2213 flags = GET_WORD(res);
2214 end_flag = flags & MF_END;
2215 /* Remove MF_END because it has the same value as MF_HILITE */
2217 res += sizeof(WORD);
2218 if (!(flags & MF_POPUP))
2221 res += sizeof(WORD);
2224 if (!unicode) res += strlen(str) + 1;
2225 else res += (strlenW((LPCWSTR)str) + 1) * sizeof(WCHAR);
2226 if (flags & MF_POPUP)
2228 HMENU hSubMenu = CreatePopupMenu();
2229 if (!hSubMenu) return NULL;
2230 if (!(res = MENU_ParseResource( res, hSubMenu, unicode )))
2232 if (!unicode) AppendMenuA( hMenu, flags, (UINT_PTR)hSubMenu, str );
2233 else AppendMenuW( hMenu, flags, (UINT_PTR)hSubMenu, (LPCWSTR)str );
2235 else /* Not a popup */
2237 if (!unicode) AppendMenuA( hMenu, flags, id, *str ? str : NULL );
2238 else AppendMenuW( hMenu, flags, id,
2239 *(LPCWSTR)str ? (LPCWSTR)str : NULL );
2241 } while (!end_flag);
2246 /**********************************************************************
2247 * MENUEX_ParseResource
2249 * Parse an extended menu resource and add items to the menu.
2250 * Return a pointer to the end of the resource.
2252 static LPCSTR MENUEX_ParseResource( LPCSTR res, HMENU hMenu)
2258 mii.cbSize = sizeof(mii);
2259 mii.fMask = MIIM_STATE | MIIM_ID | MIIM_TYPE;
2260 mii.fType = GET_DWORD(res);
2261 res += sizeof(DWORD);
2262 mii.fState = GET_DWORD(res);
2263 res += sizeof(DWORD);
2264 mii.wID = GET_DWORD(res);
2265 res += sizeof(DWORD);
2266 resinfo = GET_WORD(res); /* FIXME: for 16-bit apps this is a byte. */
2267 res += sizeof(WORD);
2268 /* Align the text on a word boundary. */
2269 res += (~((UINT_PTR)res - 1)) & 1;
2270 mii.dwTypeData = (LPWSTR) res;
2271 res += (1 + strlenW(mii.dwTypeData)) * sizeof(WCHAR);
2272 /* Align the following fields on a dword boundary. */
2273 res += (~((UINT_PTR)res - 1)) & 3;
2275 TRACE("Menu item: [%08x,%08x,%04x,%04x,%s]\n",
2276 mii.fType, mii.fState, mii.wID, resinfo, debugstr_w(mii.dwTypeData));
2278 if (resinfo & 1) { /* Pop-up? */
2279 /* DWORD helpid = GET_DWORD(res); FIXME: use this. */
2280 res += sizeof(DWORD);
2281 mii.hSubMenu = CreatePopupMenu();
2284 if (!(res = MENUEX_ParseResource(res, mii.hSubMenu))) {
2285 DestroyMenu(mii.hSubMenu);
2288 mii.fMask |= MIIM_SUBMENU;
2289 mii.fType |= MF_POPUP;
2291 else if(!*mii.dwTypeData && !(mii.fType & MF_SEPARATOR))
2293 WARN("Converting NULL menu item %04x, type %04x to SEPARATOR\n",
2294 mii.wID, mii.fType);
2295 mii.fType |= MF_SEPARATOR;
2297 InsertMenuItemW(hMenu, -1, MF_BYPOSITION, &mii);
2298 } while (!(resinfo & MF_END));
2303 /***********************************************************************
2306 * Return the handle of the selected sub-popup menu (if any).
2308 static HMENU MENU_GetSubPopup( HMENU hmenu )
2313 menu = MENU_GetMenu( hmenu );
2315 if ((!menu) || (menu->FocusedItem == NO_SELECTED_ITEM)) return 0;
2317 item = &menu->items[menu->FocusedItem];
2318 if ((item->fType & MF_POPUP) && (item->fState & MF_MOUSESELECT))
2319 return item->hSubMenu;
2324 /***********************************************************************
2325 * MENU_HideSubPopups
2327 * Hide the sub-popup menus of this menu.
2329 static void MENU_HideSubPopups( HWND hwndOwner, HMENU hmenu,
2330 BOOL sendMenuSelect, UINT wFlags )
2332 POPUPMENU *menu = MENU_GetMenu( hmenu );
2334 TRACE("owner=%p hmenu=%p 0x%04x\n", hwndOwner, hmenu, sendMenuSelect);
2336 if (menu && top_popup)
2342 if (menu->FocusedItem != NO_SELECTED_ITEM)
2344 item = &menu->items[menu->FocusedItem];
2345 if (!(item->fType & MF_POPUP) ||
2346 !(item->fState & MF_MOUSESELECT)) return;
2347 item->fState &= ~MF_MOUSESELECT;
2348 hsubmenu = item->hSubMenu;
2351 if (!(submenu = MENU_GetMenu( hsubmenu ))) return;
2352 MENU_HideSubPopups( hwndOwner, hsubmenu, FALSE, wFlags );
2353 MENU_SelectItem( hwndOwner, hsubmenu, NO_SELECTED_ITEM, sendMenuSelect, 0 );
2354 DestroyWindow( submenu->hWnd );
2357 if (!(wFlags & TPM_NONOTIFY))
2358 SendMessageW( hwndOwner, WM_UNINITMENUPOPUP, (WPARAM)hsubmenu,
2359 MAKELPARAM(0, IS_SYSTEM_MENU(submenu)) );
2364 /***********************************************************************
2367 * Display the sub-menu of the selected item of this menu.
2368 * Return the handle of the submenu, or hmenu if no submenu to display.
2370 static HMENU MENU_ShowSubPopup( HWND hwndOwner, HMENU hmenu,
2371 BOOL selectFirst, UINT wFlags )
2378 TRACE("owner=%p hmenu=%p 0x%04x\n", hwndOwner, hmenu, selectFirst);
2380 if (!(menu = MENU_GetMenu( hmenu ))) return hmenu;
2382 if (menu->FocusedItem == NO_SELECTED_ITEM) return hmenu;
2384 item = &menu->items[menu->FocusedItem];
2385 if (!(item->fType & MF_POPUP) || (item->fState & (MF_GRAYED | MF_DISABLED)))
2388 /* message must be sent before using item,
2389 because nearly everything may be changed by the application ! */
2391 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
2392 if (!(wFlags & TPM_NONOTIFY))
2393 SendMessageW( hwndOwner, WM_INITMENUPOPUP, (WPARAM)item->hSubMenu,
2394 MAKELPARAM( menu->FocusedItem, IS_SYSTEM_MENU(menu) ));
2396 item = &menu->items[menu->FocusedItem];
2399 /* correct item if modified as a reaction to WM_INITMENUPOPUP message */
2400 if (!(item->fState & MF_HILITE))
2402 if (menu->wFlags & MF_POPUP) hdc = GetDC( menu->hWnd );
2403 else hdc = GetDCEx( menu->hWnd, 0, DCX_CACHE | DCX_WINDOW);
2405 SelectObject( hdc, get_menu_font(FALSE));
2407 item->fState |= MF_HILITE;
2408 MENU_DrawMenuItem( menu->hWnd, hmenu, hwndOwner, hdc, item, menu->Height, !(menu->wFlags & MF_POPUP), ODA_DRAWENTIRE );
2409 ReleaseDC( menu->hWnd, hdc );
2411 if (!item->rect.top && !item->rect.left && !item->rect.bottom && !item->rect.right)
2414 item->fState |= MF_MOUSESELECT;
2416 if (IS_SYSTEM_MENU(menu))
2418 MENU_InitSysMenuPopup(item->hSubMenu,
2419 GetWindowLongW( menu->hWnd, GWL_STYLE ),
2420 GetClassLongW( menu->hWnd, GCL_STYLE));
2422 NC_GetSysPopupPos( menu->hWnd, &rect );
2423 rect.top = rect.bottom;
2424 rect.right = GetSystemMetrics(SM_CXSIZE);
2425 rect.bottom = GetSystemMetrics(SM_CYSIZE);
2429 GetWindowRect( menu->hWnd, &rect );
2430 if (menu->wFlags & MF_POPUP)
2432 RECT rc = item->rect;
2434 MENU_AdjustMenuItemRect(menu, &rc);
2436 /* The first item in the popup menu has to be at the
2437 same y position as the focused menu item */
2438 rect.left += rc.right - GetSystemMetrics(SM_CXBORDER);
2439 rect.top += rc.top - MENU_TOP_MARGIN;
2440 rect.right = rc.left - rc.right + GetSystemMetrics(SM_CXBORDER);
2441 rect.bottom = rc.top - rc.bottom - MENU_TOP_MARGIN
2442 - MENU_BOTTOM_MARGIN - GetSystemMetrics(SM_CYBORDER);
2446 rect.left += item->rect.left;
2447 rect.top += item->rect.bottom;
2448 rect.right = item->rect.right - item->rect.left;
2449 rect.bottom = item->rect.bottom - item->rect.top;
2453 MENU_ShowPopup( hwndOwner, item->hSubMenu, menu->FocusedItem, 0,
2454 rect.left, rect.top, rect.right, rect.bottom );
2456 MENU_MoveSelection( hwndOwner, item->hSubMenu, ITEM_NEXT );
2457 return item->hSubMenu;
2462 /**********************************************************************
2465 HWND MENU_IsMenuActive(void)
2470 /**********************************************************************
2473 * Calls EndMenu() if the hwnd parameter belongs to the menu owner
2475 * Does the (menu stuff) of the default window handling of WM_CANCELMODE
2477 void MENU_EndMenu( HWND hwnd )
2480 menu = top_popup_hmenu ? MENU_GetMenu( top_popup_hmenu ) : NULL;
2481 if (menu && hwnd == menu->hwndOwner) EndMenu();
2484 /***********************************************************************
2487 * Walks menu chain trying to find a menu pt maps to.
2489 static HMENU MENU_PtMenu( HMENU hMenu, POINT pt )
2491 POPUPMENU *menu = MENU_GetMenu( hMenu );
2492 UINT item = menu->FocusedItem;
2495 /* try subpopup first (if any) */
2496 ret = (item != NO_SELECTED_ITEM &&
2497 (menu->items[item].fType & MF_POPUP) &&
2498 (menu->items[item].fState & MF_MOUSESELECT))
2499 ? MENU_PtMenu(menu->items[item].hSubMenu, pt) : 0;
2501 if (!ret) /* check the current window (avoiding WM_HITTEST) */
2503 INT ht = NC_HandleNCHitTest( menu->hWnd, pt );
2504 if( menu->wFlags & MF_POPUP )
2506 if (ht != HTNOWHERE && ht != HTERROR) ret = hMenu;
2508 else if (ht == HTSYSMENU)
2509 ret = get_win_sys_menu( menu->hWnd );
2510 else if (ht == HTMENU)
2511 ret = GetMenu( menu->hWnd );
2516 /***********************************************************************
2517 * MENU_ExecFocusedItem
2519 * Execute a menu item (for instance when user pressed Enter).
2520 * Return the wID of the executed item. Otherwise, -1 indicating
2521 * that no menu item was executed, -2 if a popup is shown;
2522 * Have to receive the flags for the TrackPopupMenu options to avoid
2523 * sending unwanted message.
2526 static INT MENU_ExecFocusedItem( MTRACKER* pmt, HMENU hMenu, UINT wFlags )
2529 POPUPMENU *menu = MENU_GetMenu( hMenu );
2531 TRACE("%p hmenu=%p\n", pmt, hMenu);
2533 if (!menu || !menu->nItems ||
2534 (menu->FocusedItem == NO_SELECTED_ITEM)) return -1;
2536 item = &menu->items[menu->FocusedItem];
2538 TRACE("hMenu %p wID %08lx hSubMenu %p fType %04x\n", hMenu, item->wID, item->hSubMenu, item->fType);
2540 if (!(item->fType & MF_POPUP))
2542 if (!(item->fState & (MF_GRAYED | MF_DISABLED)) && !(item->fType & MF_SEPARATOR))
2544 /* If TPM_RETURNCMD is set you return the id, but
2545 do not send a message to the owner */
2546 if(!(wFlags & TPM_RETURNCMD))
2548 if( menu->wFlags & MF_SYSMENU )
2549 PostMessageW( pmt->hOwnerWnd, WM_SYSCOMMAND, item->wID,
2550 MAKELPARAM((INT16)pmt->pt.x, (INT16)pmt->pt.y) );
2553 POPUPMENU *topmenu = MENU_GetMenu( pmt->hTopMenu );
2554 DWORD dwStyle = menu->dwStyle | (topmenu ? topmenu->dwStyle : 0);
2556 if (dwStyle & MNS_NOTIFYBYPOS)
2557 PostMessageW( pmt->hOwnerWnd, WM_MENUCOMMAND, menu->FocusedItem,
2560 PostMessageW( pmt->hOwnerWnd, WM_COMMAND, item->wID, 0 );
2568 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hMenu, TRUE, wFlags);
2575 /***********************************************************************
2576 * MENU_SwitchTracking
2578 * Helper function for menu navigation routines.
2580 static void MENU_SwitchTracking( MTRACKER* pmt, HMENU hPtMenu, UINT id, UINT wFlags )
2582 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2583 POPUPMENU *topmenu = MENU_GetMenu( pmt->hTopMenu );
2585 TRACE("%p hmenu=%p 0x%04x\n", pmt, hPtMenu, id);
2587 if( pmt->hTopMenu != hPtMenu &&
2588 !((ptmenu->wFlags | topmenu->wFlags) & MF_POPUP) )
2590 /* both are top level menus (system and menu-bar) */
2591 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE, wFlags );
2592 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
2593 pmt->hTopMenu = hPtMenu;
2595 else MENU_HideSubPopups( pmt->hOwnerWnd, hPtMenu, FALSE, wFlags );
2596 MENU_SelectItem( pmt->hOwnerWnd, hPtMenu, id, TRUE, 0 );
2600 /***********************************************************************
2603 * Return TRUE if we can go on with menu tracking.
2605 static BOOL MENU_ButtonDown( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2607 TRACE("%p hPtMenu=%p\n", pmt, hPtMenu);
2612 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2615 if( IS_SYSTEM_MENU(ptmenu) )
2616 item = ptmenu->items;
2618 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2622 if( ptmenu->FocusedItem != id )
2623 MENU_SwitchTracking( pmt, hPtMenu, id, wFlags );
2625 /* If the popup menu is not already "popped" */
2626 if(!(item->fState & MF_MOUSESELECT ))
2628 pmt->hCurrentMenu = MENU_ShowSubPopup( pmt->hOwnerWnd, hPtMenu, FALSE, wFlags );
2633 /* Else the click was on the menu bar, finish the tracking */
2638 /***********************************************************************
2641 * Return the value of MENU_ExecFocusedItem if
2642 * the selected item was not a popup. Else open the popup.
2643 * A -1 return value indicates that we go on with menu tracking.
2646 static INT MENU_ButtonUp( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags)
2648 TRACE("%p hmenu=%p\n", pmt, hPtMenu);
2653 POPUPMENU *ptmenu = MENU_GetMenu( hPtMenu );
2656 if( IS_SYSTEM_MENU(ptmenu) )
2657 item = ptmenu->items;
2659 item = MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2661 if( item && (ptmenu->FocusedItem == id ))
2663 debug_print_menuitem ("FocusedItem: ", item, "");
2665 if( !(item->fType & MF_POPUP) )
2667 INT executedMenuId = MENU_ExecFocusedItem( pmt, hPtMenu, wFlags);
2668 if (executedMenuId == -1 || executedMenuId == -2) return -1;
2669 return executedMenuId;
2672 /* If we are dealing with the top-level menu */
2673 /* and this is a click on an already "popped" item: */
2674 /* Stop the menu tracking and close the opened submenus */
2675 if((pmt->hTopMenu == hPtMenu) && ptmenu->bTimeToHide)
2678 ptmenu->bTimeToHide = TRUE;
2684 /***********************************************************************
2687 * Return TRUE if we can go on with menu tracking.
2689 static BOOL MENU_MouseMove( MTRACKER* pmt, HMENU hPtMenu, UINT wFlags )
2691 UINT id = NO_SELECTED_ITEM;
2692 POPUPMENU *ptmenu = NULL;
2696 ptmenu = MENU_GetMenu( hPtMenu );
2697 if( IS_SYSTEM_MENU(ptmenu) )
2700 MENU_FindItemByCoords( ptmenu, pmt->pt, &id );
2703 if( id == NO_SELECTED_ITEM )
2705 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2706 NO_SELECTED_ITEM, TRUE, pmt->hTopMenu);
2709 else if( ptmenu->FocusedItem != id )
2711 MENU_SwitchTracking( pmt, hPtMenu, id, wFlags );
2712 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hPtMenu, FALSE, wFlags);
2718 /***********************************************************************
2721 * NOTE: WM_NEXTMENU documented in Win32 is a bit different.
2723 static LRESULT MENU_DoNextMenu( MTRACKER* pmt, UINT vk, UINT wFlags )
2725 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2728 /* When skipping left, we need to do something special after the
2730 if (vk == VK_LEFT && menu->FocusedItem == 0)
2734 /* When skipping right, for the non-system menu, we need to
2735 handle the last non-special menu item (ie skip any window
2736 icons such as MDI maximize, restore or close) */
2737 else if ((vk == VK_RIGHT) && !IS_SYSTEM_MENU(menu))
2739 UINT i = menu->FocusedItem + 1;
2740 while (i < menu->nItems) {
2741 if ((menu->items[i].wID >= SC_SIZE &&
2742 menu->items[i].wID <= SC_RESTORE)) {
2746 if (i == menu->nItems) {
2750 /* When skipping right, we need to cater for the system menu */
2751 else if ((vk == VK_RIGHT) && IS_SYSTEM_MENU(menu))
2753 if (menu->FocusedItem == (menu->nItems - 1)) {
2760 MDINEXTMENU next_menu;
2765 next_menu.hmenuIn = (IS_SYSTEM_MENU(menu)) ? GetSubMenu(pmt->hTopMenu,0) : pmt->hTopMenu;
2766 next_menu.hmenuNext = 0;
2767 next_menu.hwndNext = 0;
2768 SendMessageW( pmt->hOwnerWnd, WM_NEXTMENU, vk, (LPARAM)&next_menu );
2770 TRACE("%p [%p] -> %p [%p]\n",
2771 pmt->hCurrentMenu, pmt->hOwnerWnd, next_menu.hmenuNext, next_menu.hwndNext );
2773 if (!next_menu.hmenuNext || !next_menu.hwndNext)
2775 DWORD style = GetWindowLongW( pmt->hOwnerWnd, GWL_STYLE );
2776 hNewWnd = pmt->hOwnerWnd;
2777 if( IS_SYSTEM_MENU(menu) )
2779 /* switch to the menu bar */
2781 if(style & WS_CHILD || !(hNewMenu = GetMenu(hNewWnd))) return FALSE;
2785 menu = MENU_GetMenu( hNewMenu );
2786 id = menu->nItems - 1;
2788 /* Skip backwards over any system predefined icons,
2789 eg. MDI close, restore etc icons */
2791 (menu->items[id].wID >= SC_SIZE &&
2792 menu->items[id].wID <= SC_RESTORE)) id--;
2795 else if (style & WS_SYSMENU )
2797 /* switch to the system menu */
2798 hNewMenu = get_win_sys_menu( hNewWnd );
2802 else /* application returned a new menu to switch to */
2804 hNewMenu = next_menu.hmenuNext;
2805 hNewWnd = WIN_GetFullHandle( next_menu.hwndNext );
2807 if( IsMenu(hNewMenu) && IsWindow(hNewWnd) )
2809 DWORD style = GetWindowLongW( hNewWnd, GWL_STYLE );
2811 if (style & WS_SYSMENU &&
2812 GetSubMenu(get_win_sys_menu(hNewWnd), 0) == hNewMenu )
2814 /* get the real system menu */
2815 hNewMenu = get_win_sys_menu(hNewWnd);
2817 else if (style & WS_CHILD || GetMenu(hNewWnd) != hNewMenu )
2819 /* FIXME: Not sure what to do here;
2820 * perhaps try to track hNewMenu as a popup? */
2822 TRACE(" -- got confused.\n");
2829 if( hNewMenu != pmt->hTopMenu )
2831 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, NO_SELECTED_ITEM,
2833 if( pmt->hCurrentMenu != pmt->hTopMenu )
2834 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE, wFlags );
2837 if( hNewWnd != pmt->hOwnerWnd )
2839 pmt->hOwnerWnd = hNewWnd;
2840 set_capture_window( pmt->hOwnerWnd, GUI_INMENUMODE, NULL );
2843 pmt->hTopMenu = pmt->hCurrentMenu = hNewMenu; /* all subpopups are hidden */
2844 MENU_SelectItem( pmt->hOwnerWnd, pmt->hTopMenu, id, TRUE, 0 );
2851 /***********************************************************************
2854 * The idea is not to show the popup if the next input message is
2855 * going to hide it anyway.
2857 static BOOL MENU_SuspendPopup( MTRACKER* pmt, UINT16 uMsg )
2861 msg.hwnd = pmt->hOwnerWnd;
2863 PeekMessageW( &msg, 0, uMsg, uMsg, PM_NOYIELD | PM_REMOVE);
2864 pmt->trackFlags |= TF_SKIPREMOVE;
2869 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2870 if( msg.message == WM_KEYUP || msg.message == WM_PAINT )
2872 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_REMOVE);
2873 PeekMessageW( &msg, 0, 0, 0, PM_NOYIELD | PM_NOREMOVE);
2874 if( msg.message == WM_KEYDOWN &&
2875 (msg.wParam == VK_LEFT || msg.wParam == VK_RIGHT))
2877 pmt->trackFlags |= TF_SUSPENDPOPUP;
2884 /* failures go through this */
2885 pmt->trackFlags &= ~TF_SUSPENDPOPUP;
2889 /***********************************************************************
2892 * Handle a VK_ESCAPE key event in a menu.
2894 static BOOL MENU_KeyEscape(MTRACKER* pmt, UINT wFlags)
2896 BOOL bEndMenu = TRUE;
2898 if (pmt->hCurrentMenu != pmt->hTopMenu)
2900 POPUPMENU *menu = MENU_GetMenu(pmt->hCurrentMenu);
2902 if (menu->wFlags & MF_POPUP)
2904 HMENU hmenutmp, hmenuprev;
2906 hmenuprev = hmenutmp = pmt->hTopMenu;
2908 /* close topmost popup */
2909 while (hmenutmp != pmt->hCurrentMenu)
2911 hmenuprev = hmenutmp;
2912 hmenutmp = MENU_GetSubPopup( hmenuprev );
2915 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE, wFlags );
2916 pmt->hCurrentMenu = hmenuprev;
2924 /***********************************************************************
2927 * Handle a VK_LEFT key event in a menu.
2929 static void MENU_KeyLeft( MTRACKER* pmt, UINT wFlags )
2932 HMENU hmenutmp, hmenuprev;
2935 hmenuprev = hmenutmp = pmt->hTopMenu;
2936 menu = MENU_GetMenu( hmenutmp );
2938 /* Try to move 1 column left (if possible) */
2939 if( (prevcol = MENU_GetStartOfPrevColumn( pmt->hCurrentMenu )) !=
2940 NO_SELECTED_ITEM ) {
2942 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
2947 /* close topmost popup */
2948 while (hmenutmp != pmt->hCurrentMenu)
2950 hmenuprev = hmenutmp;
2951 hmenutmp = MENU_GetSubPopup( hmenuprev );
2954 MENU_HideSubPopups( pmt->hOwnerWnd, hmenuprev, TRUE, wFlags );
2955 pmt->hCurrentMenu = hmenuprev;
2957 if ( (hmenuprev == pmt->hTopMenu) && !(menu->wFlags & MF_POPUP) )
2959 /* move menu bar selection if no more popups are left */
2961 if( !MENU_DoNextMenu( pmt, VK_LEFT, wFlags ) )
2962 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_PREV );
2964 if ( hmenuprev != hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
2966 /* A sublevel menu was displayed - display the next one
2967 * unless there is another displacement coming up */
2969 if( !MENU_SuspendPopup( pmt, WM_KEYDOWN ) )
2970 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
2971 pmt->hTopMenu, TRUE, wFlags);
2977 /***********************************************************************
2980 * Handle a VK_RIGHT key event in a menu.
2982 static void MENU_KeyRight( MTRACKER* pmt, UINT wFlags )
2985 POPUPMENU *menu = MENU_GetMenu( pmt->hTopMenu );
2988 TRACE("MENU_KeyRight called, cur %p (%s), top %p (%s).\n",
2990 debugstr_w((MENU_GetMenu(pmt->hCurrentMenu))->items[0].text),
2991 pmt->hTopMenu, debugstr_w(menu->items[0].text) );
2993 if ( (menu->wFlags & MF_POPUP) || (pmt->hCurrentMenu != pmt->hTopMenu))
2995 /* If already displaying a popup, try to display sub-popup */
2997 hmenutmp = pmt->hCurrentMenu;
2998 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd, hmenutmp, TRUE, wFlags);
3000 /* if subpopup was displayed then we are done */
3001 if (hmenutmp != pmt->hCurrentMenu) return;
3004 /* Check to see if there's another column */
3005 if( (nextcol = MENU_GetStartOfNextColumn( pmt->hCurrentMenu )) !=
3006 NO_SELECTED_ITEM ) {
3007 TRACE("Going to %d.\n", nextcol );
3008 MENU_SelectItem( pmt->hOwnerWnd, pmt->hCurrentMenu,
3013 if (!(menu->wFlags & MF_POPUP)) /* menu bar tracking */
3015 if( pmt->hCurrentMenu != pmt->hTopMenu )
3017 MENU_HideSubPopups( pmt->hOwnerWnd, pmt->hTopMenu, FALSE, wFlags );
3018 hmenutmp = pmt->hCurrentMenu = pmt->hTopMenu;
3019 } else hmenutmp = 0;
3021 /* try to move to the next item */
3022 if( !MENU_DoNextMenu( pmt, VK_RIGHT, wFlags ) )
3023 MENU_MoveSelection( pmt->hOwnerWnd, pmt->hTopMenu, ITEM_NEXT );
3025 if( hmenutmp || pmt->trackFlags & TF_SUSPENDPOPUP )
3026 if( !MENU_SuspendPopup(pmt, WM_KEYDOWN) )
3027 pmt->hCurrentMenu = MENU_ShowSubPopup(pmt->hOwnerWnd,
3028 pmt->hTopMenu, TRUE, wFlags);
3032 static void CALLBACK release_capture( BOOL __normal )
3034 set_capture_window( 0, GUI_INMENUMODE, NULL );
3037 /***********************************************************************
3040 * Menu tracking code.
3042 static BOOL MENU_TrackMenu( HMENU hmenu, UINT wFlags, INT x, INT y,
3043 HWND hwnd, const RECT *lprect )
3048 INT executedMenuId = -1;
3050 BOOL enterIdleSent = FALSE;
3054 mt.hCurrentMenu = hmenu;
3055 mt.hTopMenu = hmenu;
3056 mt.hOwnerWnd = WIN_GetFullHandle( hwnd );
3060 TRACE("hmenu=%p flags=0x%08x (%d,%d) hwnd=%p %s\n",
3061 hmenu, wFlags, x, y, hwnd, wine_dbgstr_rect( lprect));
3064 if (!(menu = MENU_GetMenu( hmenu )))
3066 WARN("Invalid menu handle %p\n", hmenu);
3067 SetLastError(ERROR_INVALID_MENU_HANDLE);
3071 if (wFlags & TPM_BUTTONDOWN)
3073 /* Get the result in order to start the tracking or not */
3074 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
3075 fEndMenu = !fRemove;
3078 if (wFlags & TF_ENDMENU) fEndMenu = TRUE;
3080 /* owner may not be visible when tracking a popup, so use the menu itself */
3081 capture_win = (wFlags & TPM_POPUPMENU) ? menu->hWnd : mt.hOwnerWnd;
3082 set_capture_window( capture_win, GUI_INMENUMODE, NULL );
3084 __TRY while (!fEndMenu)
3086 menu = MENU_GetMenu( mt.hCurrentMenu );
3087 if (!menu) /* sometimes happens if I do a window manager close */
3090 /* we have to keep the message in the queue until it's
3091 * clear that menu loop is not over yet. */
3095 if (PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE ))
3097 if (!CallMsgFilterW( &msg, MSGF_MENU )) break;
3098 /* remove the message from the queue */
3099 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3105 HWND win = menu->wFlags & MF_POPUP ? menu->hWnd : 0;
3106 enterIdleSent = TRUE;
3107 SendMessageW( mt.hOwnerWnd, WM_ENTERIDLE, MSGF_MENU, (LPARAM)win );
3113 /* check if EndMenu() tried to cancel us, by posting this message */
3114 if(msg.message == WM_CANCELMODE)
3116 /* we are now out of the loop */
3119 /* remove the message from the queue */
3120 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3122 /* break out of internal loop, ala ESCAPE */
3126 TranslateMessage( &msg );
3129 if ( (msg.hwnd==menu->hWnd) || (msg.message!=WM_TIMER) )
3130 enterIdleSent=FALSE;
3133 if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST))
3136 * Use the mouse coordinates in lParam instead of those in the MSG
3137 * struct to properly handle synthetic messages. They are already
3138 * in screen coordinates.
3140 mt.pt.x = (short)LOWORD(msg.lParam);
3141 mt.pt.y = (short)HIWORD(msg.lParam);
3143 /* Find a menu for this mouse event */
3144 hmenu = MENU_PtMenu( mt.hTopMenu, mt.pt );
3148 /* no WM_NC... messages in captured state */
3150 case WM_RBUTTONDBLCLK:
3151 case WM_RBUTTONDOWN:
3152 if (!(wFlags & TPM_RIGHTBUTTON)) break;
3154 case WM_LBUTTONDBLCLK:
3155 case WM_LBUTTONDOWN:
3156 /* If the message belongs to the menu, removes it from the queue */
3157 /* Else, end menu tracking */
3158 fRemove = MENU_ButtonDown( &mt, hmenu, wFlags );
3159 fEndMenu = !fRemove;
3163 if (!(wFlags & TPM_RIGHTBUTTON)) break;
3166 /* Check if a menu was selected by the mouse */
3169 executedMenuId = MENU_ButtonUp( &mt, hmenu, wFlags);
3170 TRACE("executedMenuId %d\n", executedMenuId);
3172 /* End the loop if executedMenuId is an item ID */
3173 /* or if the job was done (executedMenuId = 0). */
3174 fEndMenu = fRemove = (executedMenuId != -1);
3176 /* No menu was selected by the mouse */
3177 /* if the function was called by TrackPopupMenu, continue
3178 with the menu tracking. If not, stop it */
3180 fEndMenu = ((wFlags & TPM_POPUPMENU) ? FALSE : TRUE);
3185 /* the selected menu item must be changed every time */
3186 /* the mouse moves. */
3189 fEndMenu |= !MENU_MouseMove( &mt, hmenu, wFlags );
3191 } /* switch(msg.message) - mouse */
3193 else if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
3195 fRemove = TRUE; /* Keyboard messages are always removed */
3208 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu,
3209 NO_SELECTED_ITEM, FALSE, 0 );
3210 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
3211 (msg.wParam == VK_HOME)? ITEM_NEXT : ITEM_PREV );
3215 case VK_DOWN: /* If on menu bar, pull-down the menu */
3217 menu = MENU_GetMenu( mt.hCurrentMenu );
3218 if (!(menu->wFlags & MF_POPUP))
3219 mt.hCurrentMenu = MENU_ShowSubPopup(mt.hOwnerWnd, mt.hTopMenu, TRUE, wFlags);
3220 else /* otherwise try to move selection */
3221 MENU_MoveSelection( mt.hOwnerWnd, mt.hCurrentMenu,
3222 (msg.wParam == VK_UP)? ITEM_PREV : ITEM_NEXT );
3226 MENU_KeyLeft( &mt, wFlags );
3230 MENU_KeyRight( &mt, wFlags );
3234 fEndMenu = MENU_KeyEscape(&mt, wFlags);
3240 hi.cbSize = sizeof(HELPINFO);
3241 hi.iContextType = HELPINFO_MENUITEM;
3242 if (menu->FocusedItem == NO_SELECTED_ITEM)
3245 hi.iCtrlId = menu->items[menu->FocusedItem].wID;
3246 hi.hItemHandle = hmenu;
3247 hi.dwContextId = menu->dwContextHelpID;
3248 hi.MousePos = msg.pt;
3249 SendMessageW(hwnd, WM_HELP, 0, (LPARAM)&hi);
3256 break; /* WM_KEYDOWN */
3263 if (msg.wParam == '\r' || msg.wParam == ' ')
3265 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3266 fEndMenu = (executedMenuId != -2);
3271 /* Hack to avoid control chars. */
3272 /* We will find a better way real soon... */
3273 if (msg.wParam < 32) break;
3275 pos = MENU_FindItemByKey( mt.hOwnerWnd, mt.hCurrentMenu,
3276 LOWORD(msg.wParam), FALSE );
3277 if (pos == (UINT)-2) fEndMenu = TRUE;
3278 else if (pos == (UINT)-1) MessageBeep(0);
3281 MENU_SelectItem( mt.hOwnerWnd, mt.hCurrentMenu, pos,
3283 executedMenuId = MENU_ExecFocusedItem(&mt,mt.hCurrentMenu, wFlags);
3284 fEndMenu = (executedMenuId != -2);
3288 } /* switch(msg.message) - kbd */
3292 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3293 DispatchMessageW( &msg );
3297 if (!fEndMenu) fRemove = TRUE;
3299 /* finally remove message from the queue */
3301 if (fRemove && !(mt.trackFlags & TF_SKIPREMOVE) )
3302 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
3303 else mt.trackFlags &= ~TF_SKIPREMOVE;
3305 __FINALLY( release_capture )
3307 /* If dropdown is still painted and the close box is clicked on
3308 then the menu will be destroyed as part of the DispatchMessage above.
3309 This will then invalidate the menu handle in mt.hTopMenu. We should
3310 check for this first. */
3311 if( IsMenu( mt.hTopMenu ) )
3313 menu = MENU_GetMenu( mt.hTopMenu );
3315 if( IsWindow( mt.hOwnerWnd ) )
3317 MENU_HideSubPopups( mt.hOwnerWnd, mt.hTopMenu, FALSE, wFlags );
3319 if (menu && (menu->wFlags & MF_POPUP))
3321 DestroyWindow( menu->hWnd );
3324 if (!(wFlags & TPM_NONOTIFY))
3325 SendMessageW( mt.hOwnerWnd, WM_UNINITMENUPOPUP, (WPARAM)mt.hTopMenu,
3326 MAKELPARAM(0, IS_SYSTEM_MENU(menu)) );
3328 MENU_SelectItem( mt.hOwnerWnd, mt.hTopMenu, NO_SELECTED_ITEM, FALSE, 0 );
3329 SendMessageW( mt.hOwnerWnd, WM_MENUSELECT, MAKEWPARAM(0,0xffff), 0 );
3332 /* Reset the variable for hiding menu */
3333 if( menu ) menu->bTimeToHide = FALSE;
3336 /* The return value is only used by TrackPopupMenu */
3337 if (!(wFlags & TPM_RETURNCMD)) return TRUE;
3338 if (executedMenuId == -1) executedMenuId = 0;
3339 return executedMenuId;
3342 /***********************************************************************
3345 static BOOL MENU_InitTracking(HWND hWnd, HMENU hMenu, BOOL bPopup, UINT wFlags)
3349 TRACE("hwnd=%p hmenu=%p\n", hWnd, hMenu);
3353 /* Send WM_ENTERMENULOOP and WM_INITMENU message only if TPM_NONOTIFY flag is not specified */
3354 if (!(wFlags & TPM_NONOTIFY))
3355 SendMessageW( hWnd, WM_ENTERMENULOOP, bPopup, 0 );
3357 SendMessageW( hWnd, WM_SETCURSOR, (WPARAM)hWnd, HTCAPTION );
3359 if (!(wFlags & TPM_NONOTIFY))
3361 SendMessageW( hWnd, WM_INITMENU, (WPARAM)hMenu, 0 );
3362 /* If an app changed/recreated menu bar entries in WM_INITMENU
3363 * menu sizes will be recalculated once the menu created/shown.
3367 /* This makes the menus of applications built with Delphi work.
3368 * It also enables menus to be displayed in more than one window,
3369 * but there are some bugs left that need to be fixed in this case.
3371 if ((menu = MENU_GetMenu( hMenu ))) menu->hWnd = hWnd;
3375 /***********************************************************************
3378 static BOOL MENU_ExitTracking(HWND hWnd)
3380 TRACE("hwnd=%p\n", hWnd);
3382 SendMessageW( hWnd, WM_EXITMENULOOP, 0, 0 );
3385 top_popup_hmenu = NULL;
3389 /***********************************************************************
3390 * MENU_TrackMouseMenuBar
3392 * Menu-bar tracking upon a mouse event. Called from NC_HandleSysCommand().
3394 void MENU_TrackMouseMenuBar( HWND hWnd, INT ht, POINT pt )
3396 HMENU hMenu = (ht == HTSYSMENU) ? get_win_sys_menu( hWnd ) : GetMenu( hWnd );
3397 UINT wFlags = TPM_BUTTONDOWN | TPM_LEFTALIGN | TPM_LEFTBUTTON;
3399 TRACE("wnd=%p ht=0x%04x %s\n", hWnd, ht, wine_dbgstr_point( &pt));
3403 MENU_InitTracking( hWnd, hMenu, FALSE, wFlags );
3404 MENU_TrackMenu( hMenu, wFlags, pt.x, pt.y, hWnd, NULL );
3405 MENU_ExitTracking(hWnd);
3410 /***********************************************************************
3411 * MENU_TrackKbdMenuBar
3413 * Menu-bar tracking upon a keyboard event. Called from NC_HandleSysCommand().
3415 void MENU_TrackKbdMenuBar( HWND hwnd, UINT wParam, WCHAR wChar)
3417 UINT uItem = NO_SELECTED_ITEM;
3419 UINT wFlags = TPM_LEFTALIGN | TPM_LEFTBUTTON;
3421 TRACE("hwnd %p wParam 0x%04x wChar 0x%04x\n", hwnd, wParam, wChar);
3423 /* find window that has a menu */
3425 while (!WIN_ALLOWED_MENU(GetWindowLongW( hwnd, GWL_STYLE )))
3426 if (!(hwnd = GetAncestor( hwnd, GA_PARENT ))) return;
3428 /* check if we have to track a system menu */
3430 hTrackMenu = GetMenu( hwnd );
3431 if (!hTrackMenu || IsIconic(hwnd) || wChar == ' ' )
3433 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return;
3434 hTrackMenu = get_win_sys_menu( hwnd );
3436 wParam |= HTSYSMENU; /* prevent item lookup */
3439 if (!IsMenu( hTrackMenu )) return;
3441 MENU_InitTracking( hwnd, hTrackMenu, FALSE, wFlags );
3443 if( wChar && wChar != ' ' )
3445 uItem = MENU_FindItemByKey( hwnd, hTrackMenu, wChar, (wParam & HTSYSMENU) );
3446 if ( uItem >= (UINT)(-2) )
3448 if( uItem == (UINT)(-1) ) MessageBeep(0);
3449 /* schedule end of menu tracking */
3450 wFlags |= TF_ENDMENU;
3455 MENU_SelectItem( hwnd, hTrackMenu, uItem, TRUE, 0 );
3457 if (!(wParam & HTSYSMENU) || wChar == ' ')
3459 if( uItem == NO_SELECTED_ITEM )
3460 MENU_MoveSelection( hwnd, hTrackMenu, ITEM_NEXT );
3462 PostMessageW( hwnd, WM_KEYDOWN, VK_DOWN, 0L );
3466 MENU_TrackMenu( hTrackMenu, wFlags, 0, 0, hwnd, NULL );
3467 MENU_ExitTracking( hwnd );
3470 /**********************************************************************
3471 * TrackPopupMenuEx (USER32.@)
3473 BOOL WINAPI TrackPopupMenuEx( HMENU hMenu, UINT wFlags, INT x, INT y,
3474 HWND hWnd, LPTPMPARAMS lpTpm )
3478 TRACE("hmenu %p flags %04x (%d,%d) hwnd %p lpTpm %p rect %s\n",
3479 hMenu, wFlags, x, y, hWnd, lpTpm,
3480 lpTpm ? wine_dbgstr_rect( &lpTpm->rcExclude) : "-" );
3482 /* Parameter check */
3483 /* FIXME: this check is performed several times, here and in the called
3484 functions. That could be optimized */
3485 if (!MENU_GetMenu( hMenu ))
3487 SetLastError( ERROR_INVALID_MENU_HANDLE );
3491 MENU_InitTracking(hWnd, hMenu, TRUE, wFlags);
3493 /* Send WM_INITMENUPOPUP message only if TPM_NONOTIFY flag is not specified */
3494 if (!(wFlags & TPM_NONOTIFY))
3495 SendMessageW( hWnd, WM_INITMENUPOPUP, (WPARAM)hMenu, 0);
3497 if (MENU_ShowPopup( hWnd, hMenu, 0, wFlags, x, y, 0, 0 ))
3498 ret = MENU_TrackMenu( hMenu, wFlags | TPM_POPUPMENU, 0, 0, hWnd,
3499 lpTpm ? &lpTpm->rcExclude : NULL );
3500 MENU_ExitTracking(hWnd);
3505 /**********************************************************************
3506 * TrackPopupMenu (USER32.@)
3508 * Like the win32 API, the function return the command ID only if the
3509 * flag TPM_RETURNCMD is on.
3512 BOOL WINAPI TrackPopupMenu( HMENU hMenu, UINT wFlags, INT x, INT y,
3513 INT nReserved, HWND hWnd, const RECT *lpRect )
3515 return TrackPopupMenuEx( hMenu, wFlags, x, y, hWnd, NULL);
3518 /***********************************************************************
3521 * NOTE: Windows has totally different (and undocumented) popup wndproc.
3523 static LRESULT WINAPI PopupMenuWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
3525 TRACE("hwnd=%p msg=0x%04x wp=0x%04lx lp=0x%08lx\n", hwnd, message, wParam, lParam);
3531 CREATESTRUCTW *cs = (CREATESTRUCTW*)lParam;
3532 SetWindowLongPtrW( hwnd, 0, (LONG_PTR)cs->lpCreateParams );
3536 case WM_MOUSEACTIVATE: /* We don't want to be activated */
3537 return MA_NOACTIVATE;
3542 BeginPaint( hwnd, &ps );
3543 MENU_DrawPopupMenu( hwnd, ps.hdc,
3544 (HMENU)GetWindowLongPtrW( hwnd, 0 ) );
3545 EndPaint( hwnd, &ps );
3552 /* zero out global pointer in case resident popup window was destroyed. */
3553 if (hwnd == top_popup) {
3555 top_popup_hmenu = NULL;
3563 if (!GetWindowLongPtrW( hwnd, 0 )) ERR("no menu to display\n");
3566 SetWindowLongPtrW( hwnd, 0, 0 );
3569 case MM_SETMENUHANDLE:
3570 SetWindowLongPtrW( hwnd, 0, wParam );
3573 case MM_GETMENUHANDLE:
3574 return GetWindowLongPtrW( hwnd, 0 );
3577 return DefWindowProcW( hwnd, message, wParam, lParam );
3583 /***********************************************************************
3584 * MENU_GetMenuBarHeight
3586 * Compute the size of the menu bar height. Used by NC_HandleNCCalcSize().
3588 UINT MENU_GetMenuBarHeight( HWND hwnd, UINT menubarWidth,
3589 INT orgX, INT orgY )
3595 TRACE("HWND %p, width %d, at (%d, %d).\n", hwnd, menubarWidth, orgX, orgY );
3597 if (!(lppop = MENU_GetMenu( GetMenu(hwnd) ))) return 0;
3599 hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
3600 SelectObject( hdc, get_menu_font(FALSE));
3601 SetRect(&rectBar, orgX, orgY, orgX+menubarWidth, orgY+GetSystemMetrics(SM_CYMENU));
3602 MENU_MenuBarCalcSize( hdc, &rectBar, lppop, hwnd );
3603 ReleaseDC( hwnd, hdc );
3604 return lppop->Height;
3608 /*******************************************************************
3609 * ChangeMenuA (USER32.@)
3611 BOOL WINAPI ChangeMenuA( HMENU hMenu, UINT pos, LPCSTR data,
3612 UINT id, UINT flags )
3614 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3615 if (flags & MF_APPEND) return AppendMenuA( hMenu, flags & ~MF_APPEND,
3617 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3618 if (flags & MF_CHANGE) return ModifyMenuA(hMenu, pos, flags & ~MF_CHANGE,
3620 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3621 flags & MF_BYPOSITION ? pos : id,
3622 flags & ~MF_REMOVE );
3623 /* Default: MF_INSERT */
3624 return InsertMenuA( hMenu, pos, flags, id, data );
3628 /*******************************************************************
3629 * ChangeMenuW (USER32.@)
3631 BOOL WINAPI ChangeMenuW( HMENU hMenu, UINT pos, LPCWSTR data,
3632 UINT id, UINT flags )
3634 TRACE("menu=%p pos=%d data=%p id=%08x flags=%08x\n", hMenu, pos, data, id, flags );
3635 if (flags & MF_APPEND) return AppendMenuW( hMenu, flags & ~MF_APPEND,
3637 if (flags & MF_DELETE) return DeleteMenu(hMenu, pos, flags & ~MF_DELETE);
3638 if (flags & MF_CHANGE) return ModifyMenuW(hMenu, pos, flags & ~MF_CHANGE,
3640 if (flags & MF_REMOVE) return RemoveMenu( hMenu,
3641 flags & MF_BYPOSITION ? pos : id,
3642 flags & ~MF_REMOVE );
3643 /* Default: MF_INSERT */
3644 return InsertMenuW( hMenu, pos, flags, id, data );
3648 /*******************************************************************
3649 * CheckMenuItem (USER32.@)
3651 DWORD WINAPI CheckMenuItem( HMENU hMenu, UINT id, UINT flags )
3656 if (!(item = MENU_FindItem( &hMenu, &id, flags ))) return -1;
3657 ret = item->fState & MF_CHECKED;
3658 if (flags & MF_CHECKED) item->fState |= MF_CHECKED;
3659 else item->fState &= ~MF_CHECKED;
3664 /**********************************************************************
3665 * EnableMenuItem (USER32.@)
3667 BOOL WINAPI EnableMenuItem( HMENU hMenu, UINT wItemID, UINT wFlags )
3673 TRACE("(%p, %04x, %04x) !\n", hMenu, wItemID, wFlags);
3675 /* Get the Popupmenu to access the owner menu */
3676 if (!(menu = MENU_GetMenu(hMenu)))
3679 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags )))
3682 oldflags = item->fState & (MF_GRAYED | MF_DISABLED);
3683 item->fState ^= (oldflags ^ wFlags) & (MF_GRAYED | MF_DISABLED);
3685 /* If the close item in the system menu change update the close button */
3686 if((item->wID == SC_CLOSE) && (oldflags != wFlags))
3688 if (menu->hSysMenuOwner != 0)
3691 POPUPMENU* parentMenu;
3693 /* Get the parent menu to access*/
3694 if (!(parentMenu = MENU_GetMenu(menu->hSysMenuOwner)))
3697 /* Refresh the frame to reflect the change */
3698 GetWindowRect(parentMenu->hWnd, &rc);
3699 MapWindowPoints(0, parentMenu->hWnd, (POINT *)&rc, 2);
3701 RedrawWindow(parentMenu->hWnd, &rc, 0, RDW_FRAME | RDW_INVALIDATE | RDW_NOCHILDREN);
3709 /*******************************************************************
3710 * GetMenuStringA (USER32.@)
3712 INT WINAPI GetMenuStringA(
3713 HMENU hMenu, /* [in] menuhandle */
3714 UINT wItemID, /* [in] menu item (dep. on wFlags) */
3715 LPSTR str, /* [out] outbuffer. If NULL, func returns entry length*/
3716 INT nMaxSiz, /* [in] length of buffer. if 0, func returns entry len*/
3717 UINT wFlags /* [in] MF_ flags */
3721 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3722 if (str && nMaxSiz) str[0] = '\0';
3723 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) {
3724 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
3727 if (!item->text) return 0;
3728 if (!str || !nMaxSiz) return strlenW(item->text);
3729 if (!WideCharToMultiByte( CP_ACP, 0, item->text, -1, str, nMaxSiz, NULL, NULL ))
3731 TRACE("returning %s\n", debugstr_a(str));
3736 /*******************************************************************
3737 * GetMenuStringW (USER32.@)
3739 INT WINAPI GetMenuStringW( HMENU hMenu, UINT wItemID,
3740 LPWSTR str, INT nMaxSiz, UINT wFlags )
3744 TRACE("menu=%p item=%04x ptr=%p len=%d flags=%04x\n", hMenu, wItemID, str, nMaxSiz, wFlags );
3745 if (str && nMaxSiz) str[0] = '\0';
3746 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) {
3747 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
3750 if (!str || !nMaxSiz) return item->text ? strlenW(item->text) : 0;
3751 if( !(item->text)) {
3755 lstrcpynW( str, item->text, nMaxSiz );
3756 TRACE("returning %s\n", debugstr_w(str));
3757 return strlenW(str);
3761 /**********************************************************************
3762 * HiliteMenuItem (USER32.@)
3764 BOOL WINAPI HiliteMenuItem( HWND hWnd, HMENU hMenu, UINT wItemID,
3768 TRACE("(%p, %p, %04x, %04x);\n", hWnd, hMenu, wItemID, wHilite);
3769 if (!MENU_FindItem( &hMenu, &wItemID, wHilite )) return FALSE;
3770 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3771 if (menu->FocusedItem == wItemID) return TRUE;
3772 MENU_HideSubPopups( hWnd, hMenu, FALSE, 0 );
3773 MENU_SelectItem( hWnd, hMenu, wItemID, TRUE, 0 );
3778 /**********************************************************************
3779 * GetMenuState (USER32.@)
3781 UINT WINAPI GetMenuState( HMENU hMenu, UINT wItemID, UINT wFlags )
3784 TRACE("(menu=%p, id=%04x, flags=%04x);\n", hMenu, wItemID, wFlags);
3785 if (!(item = MENU_FindItem( &hMenu, &wItemID, wFlags ))) return -1;
3786 debug_print_menuitem (" item: ", item, "");
3787 if (item->fType & MF_POPUP)
3789 POPUPMENU *menu = MENU_GetMenu( item->hSubMenu );
3790 if (!menu) return -1;
3791 else return (menu->nItems << 8) | ((item->fState|item->fType) & 0xff);
3795 /* We used to (from way back then) mask the result to 0xff. */
3796 /* I don't know why and it seems wrong as the documented */
3797 /* return flag MF_SEPARATOR is outside that mask. */
3798 return (item->fType | item->fState);
3803 /**********************************************************************
3804 * GetMenuItemCount (USER32.@)
3806 INT WINAPI GetMenuItemCount( HMENU hMenu )
3808 LPPOPUPMENU menu = MENU_GetMenu(hMenu);
3809 if (!menu) return -1;
3810 TRACE("(%p) returning %d\n", hMenu, menu->nItems );
3811 return menu->nItems;
3815 /**********************************************************************
3816 * GetMenuItemID (USER32.@)
3818 UINT WINAPI GetMenuItemID( HMENU hMenu, INT nPos )
3822 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return -1;
3823 if (lpmi->fType & MF_POPUP) return -1;
3829 /*******************************************************************
3830 * InsertMenuW (USER32.@)
3832 BOOL WINAPI InsertMenuW( HMENU hMenu, UINT pos, UINT flags,
3833 UINT_PTR id, LPCWSTR str )
3837 if (IS_STRING_ITEM(flags) && str)
3838 TRACE("hMenu %p, pos %d, flags %08x, id %04lx, str %s\n",
3839 hMenu, pos, flags, id, debugstr_w(str) );
3840 else TRACE("hMenu %p, pos %d, flags %08x, id %04lx, str %p (not a string)\n",
3841 hMenu, pos, flags, id, str );
3843 if (!(item = MENU_InsertItem( hMenu, pos, flags ))) return FALSE;
3845 if (!(MENU_SetItemData( item, flags, id, str )))
3847 RemoveMenu( hMenu, pos, flags );
3851 item->hCheckBit = item->hUnCheckBit = 0;
3856 /*******************************************************************
3857 * InsertMenuA (USER32.@)
3859 BOOL WINAPI InsertMenuA( HMENU hMenu, UINT pos, UINT flags,
3860 UINT_PTR id, LPCSTR str )
3864 if (IS_STRING_ITEM(flags) && str)
3866 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
3867 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3870 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
3871 ret = InsertMenuW( hMenu, pos, flags, id, newstr );
3872 HeapFree( GetProcessHeap(), 0, newstr );
3876 else return InsertMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3880 /*******************************************************************
3881 * AppendMenuA (USER32.@)
3883 BOOL WINAPI AppendMenuA( HMENU hMenu, UINT flags,
3884 UINT_PTR id, LPCSTR data )
3886 return InsertMenuA( hMenu, -1, flags | MF_BYPOSITION, id, data );
3890 /*******************************************************************
3891 * AppendMenuW (USER32.@)
3893 BOOL WINAPI AppendMenuW( HMENU hMenu, UINT flags,
3894 UINT_PTR id, LPCWSTR data )
3896 return InsertMenuW( hMenu, -1, flags | MF_BYPOSITION, id, data );
3900 /**********************************************************************
3901 * RemoveMenu (USER32.@)
3903 BOOL WINAPI RemoveMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3908 TRACE("(menu=%p pos=%04x flags=%04x)\n",hMenu, nPos, wFlags);
3909 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
3910 if (!(menu = MENU_GetMenu(hMenu))) return FALSE;
3914 MENU_FreeItemData( item );
3916 if (--menu->nItems == 0)
3918 HeapFree( GetProcessHeap(), 0, menu->items );
3923 while(nPos < menu->nItems)
3929 menu->items = HeapReAlloc( GetProcessHeap(), 0, menu->items,
3930 menu->nItems * sizeof(MENUITEM) );
3936 /**********************************************************************
3937 * DeleteMenu (USER32.@)
3939 BOOL WINAPI DeleteMenu( HMENU hMenu, UINT nPos, UINT wFlags )
3941 MENUITEM *item = MENU_FindItem( &hMenu, &nPos, wFlags );
3942 if (!item) return FALSE;
3943 if (item->fType & MF_POPUP) DestroyMenu( item->hSubMenu );
3944 /* nPos is now the position of the item */
3945 RemoveMenu( hMenu, nPos, wFlags | MF_BYPOSITION );
3950 /*******************************************************************
3951 * ModifyMenuW (USER32.@)
3953 BOOL WINAPI ModifyMenuW( HMENU hMenu, UINT pos, UINT flags,
3954 UINT_PTR id, LPCWSTR str )
3958 if (IS_STRING_ITEM(flags))
3959 TRACE("%p %d %04x %04lx %s\n", hMenu, pos, flags, id, debugstr_w(str) );
3961 TRACE("%p %d %04x %04lx %p\n", hMenu, pos, flags, id, str );
3963 if (!(item = MENU_FindItem( &hMenu, &pos, flags ))) return FALSE;
3964 MENU_GetMenu(hMenu)->Height = 0; /* force size recalculate */
3965 return MENU_SetItemData( item, flags, id, str );
3969 /*******************************************************************
3970 * ModifyMenuA (USER32.@)
3972 BOOL WINAPI ModifyMenuA( HMENU hMenu, UINT pos, UINT flags,
3973 UINT_PTR id, LPCSTR str )
3977 if (IS_STRING_ITEM(flags) && str)
3979 INT len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
3980 LPWSTR newstr = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
3983 MultiByteToWideChar( CP_ACP, 0, str, -1, newstr, len );
3984 ret = ModifyMenuW( hMenu, pos, flags, id, newstr );
3985 HeapFree( GetProcessHeap(), 0, newstr );
3989 else return ModifyMenuW( hMenu, pos, flags, id, (LPCWSTR)str );
3993 /**********************************************************************
3994 * CreatePopupMenu (USER32.@)
3996 HMENU WINAPI CreatePopupMenu(void)
4001 if (!(hmenu = CreateMenu())) return 0;
4002 menu = MENU_GetMenu( hmenu );
4003 menu->wFlags |= MF_POPUP;
4004 menu->bTimeToHide = FALSE;
4009 /**********************************************************************
4010 * GetMenuCheckMarkDimensions (USER.417)
4011 * GetMenuCheckMarkDimensions (USER32.@)
4013 DWORD WINAPI GetMenuCheckMarkDimensions(void)
4015 return MAKELONG( GetSystemMetrics(SM_CXMENUCHECK), GetSystemMetrics(SM_CYMENUCHECK) );
4019 /**********************************************************************
4020 * SetMenuItemBitmaps (USER32.@)
4022 BOOL WINAPI SetMenuItemBitmaps( HMENU hMenu, UINT nPos, UINT wFlags,
4023 HBITMAP hNewUnCheck, HBITMAP hNewCheck)
4027 if (!(item = MENU_FindItem( &hMenu, &nPos, wFlags ))) return FALSE;
4029 if (!hNewCheck && !hNewUnCheck)
4031 item->fState &= ~MF_USECHECKBITMAPS;
4033 else /* Install new bitmaps */
4035 item->hCheckBit = hNewCheck;
4036 item->hUnCheckBit = hNewUnCheck;
4037 item->fState |= MF_USECHECKBITMAPS;
4043 /**********************************************************************
4044 * CreateMenu (USER32.@)
4046 HMENU WINAPI CreateMenu(void)
4050 if (!(hMenu = USER_HEAP_ALLOC( sizeof(POPUPMENU) ))) return 0;
4051 menu = USER_HEAP_LIN_ADDR(hMenu);
4053 ZeroMemory(menu, sizeof(POPUPMENU));
4054 menu->wMagic = MENU_MAGIC;
4055 menu->FocusedItem = NO_SELECTED_ITEM;
4056 menu->bTimeToHide = FALSE;
4058 TRACE("return %p\n", hMenu );
4064 /**********************************************************************
4065 * DestroyMenu (USER32.@)
4067 BOOL WINAPI DestroyMenu( HMENU hMenu )
4069 LPPOPUPMENU lppop = MENU_GetMenu(hMenu);
4071 TRACE("(%p)\n", hMenu);
4074 if (!lppop) return FALSE;
4076 lppop->wMagic = 0; /* Mark it as destroyed */
4078 /* DestroyMenu should not destroy system menu popup owner */
4079 if ((lppop->wFlags & (MF_POPUP | MF_SYSMENU)) == MF_POPUP && lppop->hWnd)
4081 DestroyWindow( lppop->hWnd );
4085 if (lppop->items) /* recursively destroy submenus */
4088 MENUITEM *item = lppop->items;
4089 for (i = lppop->nItems; i > 0; i--, item++)
4091 if (item->fType & MF_POPUP) DestroyMenu(item->hSubMenu);
4092 MENU_FreeItemData( item );
4094 HeapFree( GetProcessHeap(), 0, lppop->items );
4096 USER_HEAP_FREE( hMenu );
4101 /**********************************************************************
4102 * GetSystemMenu (USER32.@)
4104 HMENU WINAPI GetSystemMenu( HWND hWnd, BOOL bRevert )
4106 WND *wndPtr = WIN_GetPtr( hWnd );
4109 if (wndPtr == WND_DESKTOP) return 0;
4110 if (wndPtr == WND_OTHER_PROCESS)
4112 if (IsWindow( hWnd )) FIXME( "not supported on other process window %p\n", hWnd );
4116 if (wndPtr->hSysMenu && bRevert)
4118 DestroyMenu(wndPtr->hSysMenu);
4119 wndPtr->hSysMenu = 0;
4122 if(!wndPtr->hSysMenu && (wndPtr->dwStyle & WS_SYSMENU) )
4123 wndPtr->hSysMenu = MENU_GetSysMenu( hWnd, 0 );
4125 if( wndPtr->hSysMenu )
4128 retvalue = GetSubMenu(wndPtr->hSysMenu, 0);
4130 /* Store the dummy sysmenu handle to facilitate the refresh */
4131 /* of the close button if the SC_CLOSE item change */
4132 menu = MENU_GetMenu(retvalue);
4134 menu->hSysMenuOwner = wndPtr->hSysMenu;
4136 WIN_ReleasePtr( wndPtr );
4138 return bRevert ? 0 : retvalue;
4142 /*******************************************************************
4143 * SetSystemMenu (USER32.@)
4145 BOOL WINAPI SetSystemMenu( HWND hwnd, HMENU hMenu )
4147 WND *wndPtr = WIN_GetPtr( hwnd );
4149 if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
4151 if (wndPtr->hSysMenu) DestroyMenu( wndPtr->hSysMenu );
4152 wndPtr->hSysMenu = MENU_GetSysMenu( hwnd, hMenu );
4153 WIN_ReleasePtr( wndPtr );
4160 /**********************************************************************
4161 * GetMenu (USER32.@)
4163 HMENU WINAPI GetMenu( HWND hWnd )
4165 HMENU retvalue = (HMENU)GetWindowLongPtrW( hWnd, GWLP_ID );
4166 TRACE("for %p returning %p\n", hWnd, retvalue);
4170 /**********************************************************************
4171 * GetMenuBarInfo (USER32.@)
4173 BOOL WINAPI GetMenuBarInfo( HWND hwnd, LONG idObject, LONG idItem, PMENUBARINFO pmbi )
4175 FIXME( "(%p,0x%08x,0x%08x,%p)\n", hwnd, idObject, idItem, pmbi );
4179 /**********************************************************************
4182 * Helper for SetMenu. Also called by WIN_CreateWindowEx to avoid the
4183 * SetWindowPos call that would result if SetMenu were called directly.
4185 BOOL MENU_SetMenu( HWND hWnd, HMENU hMenu )
4187 TRACE("(%p, %p);\n", hWnd, hMenu);
4189 if (hMenu && !IsMenu(hMenu))
4191 WARN("hMenu %p is not a menu handle\n", hMenu);
4194 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
4197 hWnd = WIN_GetFullHandle( hWnd );
4198 if (GetCapture() == hWnd)
4199 set_capture_window( 0, GUI_INMENUMODE, NULL ); /* release the capture */
4205 if (!(lpmenu = MENU_GetMenu(hMenu))) return FALSE;
4207 lpmenu->hWnd = hWnd;
4208 lpmenu->Height = 0; /* Make sure we recalculate the size */
4210 SetWindowLongPtrW( hWnd, GWLP_ID, (LONG_PTR)hMenu );
4215 /**********************************************************************
4216 * SetMenu (USER32.@)
4218 BOOL WINAPI SetMenu( HWND hWnd, HMENU hMenu )
4220 if(!MENU_SetMenu(hWnd, hMenu))
4223 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4224 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4229 /**********************************************************************
4230 * GetSubMenu (USER32.@)
4232 HMENU WINAPI GetSubMenu( HMENU hMenu, INT nPos )
4236 if (!(lpmi = MENU_FindItem(&hMenu,(UINT*)&nPos,MF_BYPOSITION))) return 0;
4237 if (!(lpmi->fType & MF_POPUP)) return 0;
4238 return lpmi->hSubMenu;
4242 /**********************************************************************
4243 * DrawMenuBar (USER32.@)
4245 BOOL WINAPI DrawMenuBar( HWND hWnd )
4248 HMENU hMenu = GetMenu(hWnd);
4250 if (!WIN_ALLOWED_MENU(GetWindowLongW( hWnd, GWL_STYLE )))
4252 if (!hMenu || !(lppop = MENU_GetMenu( hMenu ))) return FALSE;
4254 lppop->Height = 0; /* Make sure we call MENU_MenuBarCalcSize */
4255 lppop->hwndOwner = hWnd;
4256 SetWindowPos( hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
4257 SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED );
4261 /***********************************************************************
4262 * DrawMenuBarTemp (USER32.@)
4266 * called by W98SE desk.cpl Control Panel Applet
4268 * Not 100% sure about the param names, but close.
4270 DWORD WINAPI DrawMenuBarTemp(HWND hwnd, HDC hDC, LPRECT lprect, HMENU hMenu, HFONT hFont)
4275 BOOL flat_menu = FALSE;
4277 SystemParametersInfoW (SPI_GETFLATMENU, 0, &flat_menu, 0);
4280 hMenu = GetMenu(hwnd);
4283 hFont = get_menu_font(FALSE);
4285 lppop = MENU_GetMenu( hMenu );
4286 if (lppop == NULL || lprect == NULL)
4288 retvalue = GetSystemMetrics(SM_CYMENU);
4292 TRACE("(%p, %p, %p, %p, %p)\n", hwnd, hDC, lprect, hMenu, hFont);
4294 hfontOld = SelectObject( hDC, hFont);
4296 if (lppop->Height == 0)
4297 MENU_MenuBarCalcSize(hDC, lprect, lppop, hwnd);
4299 lprect->bottom = lprect->top + lppop->Height;
4301 FillRect(hDC, lprect, GetSysColorBrush(flat_menu ? COLOR_MENUBAR : COLOR_MENU) );
4303 SelectObject( hDC, SYSCOLOR_GetPen(COLOR_3DFACE));
4304 MoveToEx( hDC, lprect->left, lprect->bottom, NULL );
4305 LineTo( hDC, lprect->right, lprect->bottom );
4307 if (lppop->nItems == 0)
4309 retvalue = GetSystemMetrics(SM_CYMENU);
4313 for (i = 0; i < lppop->nItems; i++)
4315 MENU_DrawMenuItem( hwnd, hMenu, hwnd,
4316 hDC, &lppop->items[i], lppop->Height, TRUE, ODA_DRAWENTIRE );
4318 retvalue = lppop->Height;
4321 if (hfontOld) SelectObject (hDC, hfontOld);
4325 /***********************************************************************
4326 * EndMenu (USER.187)
4327 * EndMenu (USER32.@)
4329 BOOL WINAPI EndMenu(void)
4331 /* if we are in the menu code, and it is active */
4332 if (!fEndMenu && top_popup)
4334 /* terminate the menu handling code */
4337 /* needs to be posted to wakeup the internal menu handler */
4338 /* which will now terminate the menu, in the event that */
4339 /* the main window was minimized, or lost focus, so we */
4340 /* don't end up with an orphaned menu */
4341 PostMessageW( top_popup, WM_CANCELMODE, 0, 0);
4347 /***********************************************************************
4348 * LookupMenuHandle (USER.217)
4350 HMENU16 WINAPI LookupMenuHandle16( HMENU16 hmenu, INT16 id )
4352 HMENU hmenu32 = HMENU_32(hmenu);
4354 if (!MENU_FindItem( &hmenu32, &id32, MF_BYCOMMAND )) return 0;
4355 else return HMENU_16(hmenu32);
4359 /**********************************************************************
4360 * LoadMenu (USER.150)
4362 HMENU16 WINAPI LoadMenu16( HINSTANCE16 instance, LPCSTR name )
4368 if (HIWORD(name) && name[0] == '#') name = ULongToPtr(atoi( name + 1 ));
4369 if (!name) return 0;
4371 instance = GetExePtr( instance );
4372 if (!(hRsrc = FindResource16( instance, name, (LPSTR)RT_MENU ))) return 0;
4373 if (!(handle = LoadResource16( instance, hRsrc ))) return 0;
4374 hMenu = LoadMenuIndirect16(LockResource16(handle));
4375 FreeResource16( handle );
4380 /*****************************************************************
4381 * LoadMenuA (USER32.@)
4383 HMENU WINAPI LoadMenuA( HINSTANCE instance, LPCSTR name )
4385 HRSRC hrsrc = FindResourceA( instance, name, (LPSTR)RT_MENU );
4386 if (!hrsrc) return 0;
4387 return LoadMenuIndirectA( LoadResource( instance, hrsrc ));
4391 /*****************************************************************
4392 * LoadMenuW (USER32.@)
4394 HMENU WINAPI LoadMenuW( HINSTANCE instance, LPCWSTR name )
4396 HRSRC hrsrc = FindResourceW( instance, name, (LPWSTR)RT_MENU );
4397 if (!hrsrc) return 0;
4398 return LoadMenuIndirectW( LoadResource( instance, hrsrc ));
4402 /**********************************************************************
4403 * LoadMenuIndirect (USER.220)
4405 HMENU16 WINAPI LoadMenuIndirect16( LPCVOID template )
4408 WORD version, offset;
4409 LPCSTR p = template;
4411 TRACE("(%p)\n", template );
4412 version = GET_WORD(p);
4416 WARN("version must be 0 for Win16\n" );
4419 offset = GET_WORD(p);
4420 p += sizeof(WORD) + offset;
4421 if (!(hMenu = CreateMenu())) return 0;
4422 if (!MENU_ParseResource( p, hMenu, FALSE ))
4424 DestroyMenu( hMenu );
4427 return HMENU_16(hMenu);
4431 /**********************************************************************
4432 * LoadMenuIndirectW (USER32.@)
4434 HMENU WINAPI LoadMenuIndirectW( LPCVOID template )
4437 WORD version, offset;
4438 LPCSTR p = template;
4440 version = GET_WORD(p);
4442 TRACE("%p, ver %d\n", template, version );
4445 case 0: /* standard format is version of 0 */
4446 offset = GET_WORD(p);
4447 p += sizeof(WORD) + offset;
4448 if (!(hMenu = CreateMenu())) return 0;
4449 if (!MENU_ParseResource( p, hMenu, TRUE ))
4451 DestroyMenu( hMenu );
4455 case 1: /* extended format is version of 1 */
4456 offset = GET_WORD(p);
4457 p += sizeof(WORD) + offset;
4458 if (!(hMenu = CreateMenu())) return 0;
4459 if (!MENUEX_ParseResource( p, hMenu))
4461 DestroyMenu( hMenu );
4466 ERR("version %d not supported.\n", version);
4472 /**********************************************************************
4473 * LoadMenuIndirectA (USER32.@)
4475 HMENU WINAPI LoadMenuIndirectA( LPCVOID template )
4477 return LoadMenuIndirectW( template );
4481 /**********************************************************************
4484 BOOL WINAPI IsMenu(HMENU hmenu)
4486 LPPOPUPMENU menu = MENU_GetMenu(hmenu);
4490 SetLastError(ERROR_INVALID_MENU_HANDLE);
4496 /**********************************************************************
4497 * GetMenuItemInfo_common
4500 static BOOL GetMenuItemInfo_common ( HMENU hmenu, UINT item, BOOL bypos,
4501 LPMENUITEMINFOW lpmii, BOOL unicode)
4503 MENUITEM *menu = MENU_FindItem (&hmenu, &item, bypos ? MF_BYPOSITION : 0);
4505 debug_print_menuitem("GetMenuItemInfo_common: ", menu, "");
4508 SetLastError( ERROR_MENU_ITEM_NOT_FOUND);
4512 if( lpmii->fMask & MIIM_TYPE) {
4513 if( lpmii->fMask & ( MIIM_STRING | MIIM_FTYPE | MIIM_BITMAP)) {
4514 WARN("invalid combination of fMask bits used\n");
4515 /* this does not happen on Win9x/ME */
4516 SetLastError( ERROR_INVALID_PARAMETER);
4519 lpmii->fType = menu->fType & MENUITEMINFO_TYPE_MASK;
4520 if( menu->hbmpItem) lpmii->fType |= MFT_BITMAP;
4521 lpmii->hbmpItem = menu->hbmpItem; /* not on Win9x/ME */
4522 if( lpmii->fType & MFT_BITMAP) {
4523 lpmii->dwTypeData = (LPWSTR) menu->hbmpItem;
4525 } else if( lpmii->fType & (MFT_OWNERDRAW | MFT_SEPARATOR)) {
4526 /* this does not happen on Win9x/ME */
4527 lpmii->dwTypeData = 0;
4532 /* copy the text string */
4533 if ((lpmii->fMask & (MIIM_TYPE|MIIM_STRING))) {
4535 if(lpmii->dwTypeData && lpmii->cch) {
4538 *((WCHAR *)lpmii->dwTypeData) = 0;
4540 *((CHAR *)lpmii->dwTypeData) = 0;
4546 len = strlenW(menu->text);
4547 if(lpmii->dwTypeData && lpmii->cch)
4548 lstrcpynW(lpmii->dwTypeData, menu->text, lpmii->cch);
4552 len = WideCharToMultiByte( CP_ACP, 0, menu->text, -1, NULL,
4553 0, NULL, NULL ) - 1;
4554 if(lpmii->dwTypeData && lpmii->cch)
4555 if (!WideCharToMultiByte( CP_ACP, 0, menu->text, -1,
4556 (LPSTR)lpmii->dwTypeData, lpmii->cch, NULL, NULL ))
4557 ((LPSTR)lpmii->dwTypeData)[lpmii->cch - 1] = 0;
4559 /* if we've copied a substring we return its length */
4560 if(lpmii->dwTypeData && lpmii->cch)
4561 if (lpmii->cch <= len + 1)
4566 /* return length of string */
4567 /* not on Win9x/ME if fType & MFT_BITMAP */
4573 if (lpmii->fMask & MIIM_FTYPE)
4574 lpmii->fType = menu->fType & MENUITEMINFO_TYPE_MASK;
4576 if (lpmii->fMask & MIIM_BITMAP)
4577 lpmii->hbmpItem = menu->hbmpItem;
4579 if (lpmii->fMask & MIIM_STATE)
4580 lpmii->fState = menu->fState & MENUITEMINFO_STATE_MASK;
4582 if (lpmii->fMask & MIIM_ID)
4583 lpmii->wID = menu->wID;
4585 if (lpmii->fMask & MIIM_SUBMENU)
4586 lpmii->hSubMenu = menu->hSubMenu;
4588 /* hSubMenu is always cleared
4589 * (not on Win9x/ME ) */
4590 lpmii->hSubMenu = 0;
4593 if (lpmii->fMask & MIIM_CHECKMARKS) {
4594 lpmii->hbmpChecked = menu->hCheckBit;
4595 lpmii->hbmpUnchecked = menu->hUnCheckBit;
4597 if (lpmii->fMask & MIIM_DATA)
4598 lpmii->dwItemData = menu->dwItemData;
4603 /**********************************************************************
4604 * GetMenuItemInfoA (USER32.@)
4606 BOOL WINAPI GetMenuItemInfoA( HMENU hmenu, UINT item, BOOL bypos,
4607 LPMENUITEMINFOA lpmii)
4611 if( lpmii->cbSize != sizeof( mii) &&
4612 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4613 SetLastError( ERROR_INVALID_PARAMETER);
4616 memcpy( &mii, lpmii, lpmii->cbSize);
4617 mii.cbSize = sizeof( mii);
4618 ret = GetMenuItemInfo_common (hmenu, item, bypos,
4619 (LPMENUITEMINFOW)&mii, FALSE);
4620 mii.cbSize = lpmii->cbSize;
4621 memcpy( lpmii, &mii, mii.cbSize);
4625 /**********************************************************************
4626 * GetMenuItemInfoW (USER32.@)
4628 BOOL WINAPI GetMenuItemInfoW( HMENU hmenu, UINT item, BOOL bypos,
4629 LPMENUITEMINFOW lpmii)
4633 if( lpmii->cbSize != sizeof( mii) &&
4634 lpmii->cbSize != sizeof( mii) - sizeof ( mii.hbmpItem)) {
4635 SetLastError( ERROR_INVALID_PARAMETER);
4638 memcpy( &mii, lpmii, lpmii->cbSize);
4639 mii.cbSize = sizeof( mii);
4640 ret = GetMenuItemInfo_common (hmenu, item, bypos, &mii, TRUE);
4641 mii.cbSize = lpmii->cbSize;
4642 memcpy( lpmii, &mii, mii.cbSize);
4647 /* set a menu item text from a ASCII or Unicode string */
4648 static inline void set_menu_item_text( MENUITEM *menu, LPCWSTR text, BOOL unicode )
4654 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, (strlenW(text)+1) * sizeof(WCHAR) )))
4655 strcpyW( menu->text, text );
4659 LPCSTR str = (LPCSTR)text;
4660 int len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
4661 if ((menu->text = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
4662 MultiByteToWideChar( CP_ACP, 0, str, -1, menu->text, len );
4667 /**********************************************************************
4668 * SetMenuItemInfo_common
4671 static BOOL SetMenuItemInfo_common(MENUITEM * menu,
4672 const MENUITEMINFOW *lpmii,
4675 if (!menu) return FALSE;
4677 debug_print_menuitem("SetMenuItemInfo_common from: ", menu, "");
4679 if (lpmii->fMask & MIIM_TYPE ) {
4680 if( lpmii->fMask & ( MIIM_STRING | MIIM_FTYPE | MIIM_BITMAP)) {
4681 WARN("invalid combination of fMask bits used\n");
4682 /* this does not happen on Win9x/ME */
4683 SetLastError( ERROR_INVALID_PARAMETER);
4687 /* Remove the old type bits and replace them with the new ones */
4688 menu->fType &= ~MENUITEMINFO_TYPE_MASK;
4689 menu->fType |= lpmii->fType & MENUITEMINFO_TYPE_MASK;
4691 if (IS_STRING_ITEM(menu->fType)) {
4692 HeapFree(GetProcessHeap(), 0, menu->text);
4693 set_menu_item_text( menu, lpmii->dwTypeData, unicode );
4694 } else if( (menu->fType) & MFT_BITMAP)
4695 menu->hbmpItem = HBITMAP_32(LOWORD(lpmii->dwTypeData));
4698 if (lpmii->fMask & MIIM_FTYPE ) {
4699 if(( lpmii->fType & MFT_BITMAP)) {
4700 SetLastError( ERROR_INVALID_PARAMETER);
4703 menu->fType &= ~MENUITEMINFO_TYPE_MASK;
4704 menu->fType |= lpmii->fType & MENUITEMINFO_TYPE_MASK;
4706 if (lpmii->fMask & MIIM_STRING ) {
4707 /* free the string when used */
4708 HeapFree(GetProcessHeap(), 0, menu->text);
4709 set_menu_item_text( menu, lpmii->dwTypeData, unicode );
4712 if (lpmii->fMask & MIIM_STATE)
4714 /* Other menu items having MFS_DEFAULT are not converted
4716 menu->fState = lpmii->fState & MENUITEMINFO_STATE_MASK;
4719 if (lpmii->fMask & MIIM_ID)
4720 menu->wID = lpmii->wID;
4722 if (lpmii->fMask & MIIM_SUBMENU) {
4723 menu->hSubMenu = lpmii->hSubMenu;
4724 if (menu->hSubMenu) {
4725 POPUPMENU *subMenu = MENU_GetMenu(menu->hSubMenu);
4727 subMenu->wFlags |= MF_POPUP;
4728 menu->fType |= MF_POPUP;
4731 SetLastError( ERROR_INVALID_PARAMETER);
4736 menu->fType &= ~MF_POPUP;
4739 if (lpmii->fMask & MIIM_CHECKMARKS)
4741 menu->hCheckBit = lpmii->hbmpChecked;
4742 menu->hUnCheckBit = lpmii->hbmpUnchecked;
4744 if (lpmii->fMask & MIIM_DATA)
4745 menu->dwItemData = lpmii->dwItemData;
4747 if (lpmii->fMask & MIIM_BITMAP)
4748 menu->hbmpItem = lpmii->hbmpItem;
4750 if( !menu->text && !(menu->fType & MFT_OWNERDRAW) && !menu->hbmpItem)
4751 menu->fType |= MFT_SEPARATOR;
4753 debug_print_menuitem("SetMenuItemInfo_common to : ", menu, "");
4757 /**********************************************************************
4758 * MENU_NormalizeMenuItemInfoStruct
4760 * Helper for SetMenuItemInfo and InsertMenuItemInfo:
4761 * check, copy and extend the MENUITEMINFO struct from the version that the application
4762 * supplied to the version used by wine source. */
4763 static BOOL MENU_NormalizeMenuItemInfoStruct( const MENUITEMINFOW *pmii_in,
4764 MENUITEMINFOW *pmii_out )
4766 /* do we recognize the size? */
4767 if( pmii_in->cbSize != sizeof( MENUITEMINFOW) &&
4768 pmii_in->cbSize != sizeof( MENUITEMINFOW) - sizeof( pmii_in->hbmpItem)) {
4769 SetLastError( ERROR_INVALID_PARAMETER);
4772 /* copy the fields that we have */
4773 memcpy( pmii_out, pmii_in, pmii_in->cbSize);
4774 /* if the hbmpItem member is missing then extend */
4775 if( pmii_in->cbSize != sizeof( MENUITEMINFOW)) {
4776 pmii_out->cbSize = sizeof( MENUITEMINFOW);
4777 pmii_out->hbmpItem = NULL;
4782 /**********************************************************************
4783 * SetMenuItemInfoA (USER32.@)
4785 BOOL WINAPI SetMenuItemInfoA(HMENU hmenu, UINT item, BOOL bypos,
4786 const MENUITEMINFOA *lpmii)
4790 TRACE("hmenu %p, item %u, by pos %d, info %p\n", hmenu, item, bypos, lpmii);
4792 if (!MENU_NormalizeMenuItemInfoStruct( (MENUITEMINFOW *)lpmii, &mii )) return FALSE;
4794 return SetMenuItemInfo_common(MENU_FindItem(&hmenu, &item, bypos? MF_BYPOSITION : 0),
4798 /**********************************************************************
4799 * SetMenuItemInfoW (USER32.@)
4801 BOOL WINAPI SetMenuItemInfoW(HMENU hmenu, UINT item, BOOL bypos,
4802 const MENUITEMINFOW *lpmii)
4806 TRACE("hmenu %p, item %u, by pos %d, info %p\n", hmenu, item, bypos, lpmii);
4808 if (!MENU_NormalizeMenuItemInfoStruct( lpmii, &mii )) return FALSE;
4809 return SetMenuItemInfo_common(MENU_FindItem(&hmenu,
4810 &item, bypos? MF_BYPOSITION : 0), &mii, TRUE);
4813 /**********************************************************************
4814 * SetMenuDefaultItem (USER32.@)
4817 BOOL WINAPI SetMenuDefaultItem(HMENU hmenu, UINT uItem, UINT bypos)
4823 TRACE("(%p,%d,%d)\n", hmenu, uItem, bypos);
4825 if (!(menu = MENU_GetMenu(hmenu))) return FALSE;
4827 /* reset all default-item flags */
4829 for (i = 0; i < menu->nItems; i++, item++)
4831 item->fState &= ~MFS_DEFAULT;
4834 /* no default item */
4843 if ( uItem >= menu->nItems ) return FALSE;
4844 item[uItem].fState |= MFS_DEFAULT;
4849 for (i = 0; i < menu->nItems; i++, item++)
4851 if (item->wID == uItem)
4853 item->fState |= MFS_DEFAULT;
4862 /**********************************************************************
4863 * GetMenuDefaultItem (USER32.@)
4865 UINT WINAPI GetMenuDefaultItem(HMENU hmenu, UINT bypos, UINT flags)
4871 TRACE("(%p,%d,%d)\n", hmenu, bypos, flags);
4873 if (!(menu = MENU_GetMenu(hmenu))) return -1;
4875 /* find default item */
4879 if (! item) return -1;
4881 while ( !( item->fState & MFS_DEFAULT ) )
4884 if (i >= menu->nItems ) return -1;
4887 /* default: don't return disabled items */
4888 if ( (!(GMDI_USEDISABLED & flags)) && (item->fState & MFS_DISABLED )) return -1;
4890 /* search rekursiv when needed */
4891 if ( (item->fType & MF_POPUP) && (flags & GMDI_GOINTOPOPUPS) )
4894 ret = GetMenuDefaultItem( item->hSubMenu, bypos, flags );
4895 if ( -1 != ret ) return ret;
4897 /* when item not found in submenu, return the popup item */
4899 return ( bypos ) ? i : item->wID;
4904 /**********************************************************************
4905 * InsertMenuItemA (USER32.@)
4907 BOOL WINAPI InsertMenuItemA(HMENU hMenu, UINT uItem, BOOL bypos,
4908 const MENUITEMINFOA *lpmii)
4913 TRACE("hmenu %p, item %04x, by pos %d, info %p\n", hMenu, uItem, bypos, lpmii);
4915 if (!MENU_NormalizeMenuItemInfoStruct( (MENUITEMINFOW *)lpmii, &mii )) return FALSE;
4917 item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4918 return SetMenuItemInfo_common(item, &mii, FALSE);
4922 /**********************************************************************
4923 * InsertMenuItemW (USER32.@)
4925 BOOL WINAPI InsertMenuItemW(HMENU hMenu, UINT uItem, BOOL bypos,
4926 const MENUITEMINFOW *lpmii)
4931 TRACE("hmenu %p, item %04x, by pos %d, info %p\n", hMenu, uItem, bypos, lpmii);
4933 if (!MENU_NormalizeMenuItemInfoStruct( lpmii, &mii )) return FALSE;
4935 item = MENU_InsertItem(hMenu, uItem, bypos ? MF_BYPOSITION : 0 );
4936 return SetMenuItemInfo_common(item, &mii, TRUE);
4939 /**********************************************************************
4940 * CheckMenuRadioItem (USER32.@)
4943 BOOL WINAPI CheckMenuRadioItem(HMENU hMenu,
4944 UINT first, UINT last, UINT check,
4949 MENUITEM *mi_first = NULL, *mi_check;
4950 HMENU m_first, m_check;
4952 for (i = first; i <= last; i++)
4959 mi_first = MENU_FindItem(&m_first, &pos, bypos);
4960 if (!mi_first) continue;
4961 mi_check = mi_first;
4967 mi_check = MENU_FindItem(&m_check, &pos, bypos);
4968 if (!mi_check) continue;
4971 if (m_first != m_check) continue;
4972 if (mi_check->fType == MFT_SEPARATOR) continue;
4976 mi_check->fType |= MFT_RADIOCHECK;
4977 mi_check->fState |= MFS_CHECKED;
4982 /* MSDN is wrong, Windows does not remove MFT_RADIOCHECK */
4983 mi_check->fState &= ~MFS_CHECKED;
4991 /**********************************************************************
4992 * GetMenuItemRect (USER32.@)
4994 * ATTENTION: Here, the returned values in rect are the screen
4995 * coordinates of the item just like if the menu was
4996 * always on the upper left side of the application.
4999 BOOL WINAPI GetMenuItemRect (HWND hwnd, HMENU hMenu, UINT uItem,
5002 POPUPMENU *itemMenu;
5006 TRACE("(%p,%p,%d,%p)\n", hwnd, hMenu, uItem, rect);
5008 item = MENU_FindItem (&hMenu, &uItem, MF_BYPOSITION);
5009 referenceHwnd = hwnd;
5013 itemMenu = MENU_GetMenu(hMenu);
5014 if (itemMenu == NULL)
5017 if(itemMenu->hWnd == 0)
5019 referenceHwnd = itemMenu->hWnd;
5022 if ((rect == NULL) || (item == NULL))
5027 MapWindowPoints(referenceHwnd, 0, (LPPOINT)rect, 2);
5032 /**********************************************************************
5033 * SetMenuInfo (USER32.@)
5036 * actually use the items to draw the menu
5037 * (recalculate and/or redraw)
5039 static BOOL menu_SetMenuInfo( HMENU hMenu, LPCMENUINFO lpmi)
5042 if( !(menu = MENU_GetMenu(hMenu))) return FALSE;
5044 if (lpmi->fMask & MIM_BACKGROUND)
5045 menu->hbrBack = lpmi->hbrBack;
5047 if (lpmi->fMask & MIM_HELPID)
5048 menu->dwContextHelpID = lpmi->dwContextHelpID;
5050 if (lpmi->fMask & MIM_MAXHEIGHT)
5051 menu->cyMax = lpmi->cyMax;
5053 if (lpmi->fMask & MIM_MENUDATA)
5054 menu->dwMenuData = lpmi->dwMenuData;
5056 if (lpmi->fMask & MIM_STYLE)
5057 menu->dwStyle = lpmi->dwStyle;
5059 if( lpmi->fMask & MIM_APPLYTOSUBMENUS) {
5061 MENUITEM *item = menu->items;
5062 for( i = menu->nItems; i; i--, item++)
5063 if( item->fType & MF_POPUP)
5064 menu_SetMenuInfo( item->hSubMenu, lpmi);
5069 BOOL WINAPI SetMenuInfo (HMENU hMenu, LPCMENUINFO lpmi)
5071 TRACE("(%p %p)\n", hMenu, lpmi);
5072 if( lpmi && (lpmi->cbSize == sizeof( MENUINFO)) && (menu_SetMenuInfo( hMenu, lpmi))) {
5073 if( lpmi->fMask & MIM_STYLE) {
5074 if (lpmi->dwStyle & MNS_AUTODISMISS) FIXME("MNS_AUTODISMISS unimplemented\n");
5075 if (lpmi->dwStyle & MNS_DRAGDROP) FIXME("MNS_DRAGDROP unimplemented\n");
5076 if (lpmi->dwStyle & MNS_MODELESS) FIXME("MNS_MODELESS unimplemented\n");
5080 SetLastError( ERROR_INVALID_PARAMETER);
5084 /**********************************************************************
5085 * GetMenuInfo (USER32.@)
5091 BOOL WINAPI GetMenuInfo (HMENU hMenu, LPMENUINFO lpmi)
5094 TRACE("(%p %p)\n", hMenu, lpmi);
5096 if (lpmi && (lpmi->cbSize == sizeof( MENUINFO)) && (menu = MENU_GetMenu(hMenu)))
5099 if (lpmi->fMask & MIM_BACKGROUND)
5100 lpmi->hbrBack = menu->hbrBack;
5102 if (lpmi->fMask & MIM_HELPID)
5103 lpmi->dwContextHelpID = menu->dwContextHelpID;
5105 if (lpmi->fMask & MIM_MAXHEIGHT)
5106 lpmi->cyMax = menu->cyMax;
5108 if (lpmi->fMask & MIM_MENUDATA)
5109 lpmi->dwMenuData = menu->dwMenuData;
5111 if (lpmi->fMask & MIM_STYLE)
5112 lpmi->dwStyle = menu->dwStyle;
5116 SetLastError( ERROR_INVALID_PARAMETER);
5121 /**********************************************************************
5122 * SetMenuContextHelpId (USER32.@)
5124 BOOL WINAPI SetMenuContextHelpId( HMENU hMenu, DWORD dwContextHelpID)
5128 TRACE("(%p 0x%08x)\n", hMenu, dwContextHelpID);
5130 if ((menu = MENU_GetMenu(hMenu)))
5132 menu->dwContextHelpID = dwContextHelpID;
5139 /**********************************************************************
5140 * GetMenuContextHelpId (USER32.@)
5142 DWORD WINAPI GetMenuContextHelpId( HMENU hMenu )
5146 TRACE("(%p)\n", hMenu);
5148 if ((menu = MENU_GetMenu(hMenu)))
5150 return menu->dwContextHelpID;
5155 /**********************************************************************
5156 * MenuItemFromPoint (USER32.@)
5158 INT WINAPI MenuItemFromPoint(HWND hWnd, HMENU hMenu, POINT ptScreen)
5160 POPUPMENU *menu = MENU_GetMenu(hMenu);
5163 /*FIXME: Do we have to handle hWnd here? */
5164 if (!menu) return -1;
5165 if (!MENU_FindItemByCoords(menu, ptScreen, &pos)) return -1;
5170 /**********************************************************************
5171 * translate_accelerator
5173 static BOOL translate_accelerator( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam,
5174 BYTE fVirt, WORD key, WORD cmd )
5179 if (wParam != key) return FALSE;
5181 if (GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
5182 if (GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
5183 if (GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
5185 if (message == WM_CHAR || message == WM_SYSCHAR)
5187 if ( !(fVirt & FVIRTKEY) && (mask & FALT) == (fVirt & FALT) )
5189 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(wParam) & 0xff);
5195 if(fVirt & FVIRTKEY)
5197 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
5198 wParam, 0xff & HIWORD(lParam));
5200 if(mask == (fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
5201 TRACE_(accel)(", but incorrect SHIFT/CTRL/ALT-state\n");
5205 if (!(lParam & 0x01000000)) /* no special_key */
5207 if ((fVirt & FALT) && (lParam & 0x20000000))
5208 { /* ^^ ALT pressed */
5209 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(wParam) & 0xff);
5218 if (message == WM_KEYUP || message == WM_SYSKEYUP)
5222 HMENU hMenu, hSubMenu, hSysMenu;
5223 UINT uSysStat = (UINT)-1, uStat = (UINT)-1, nPos;
5225 hMenu = (GetWindowLongW( hWnd, GWL_STYLE ) & WS_CHILD) ? 0 : GetMenu(hWnd);
5226 hSysMenu = get_win_sys_menu( hWnd );
5228 /* find menu item and ask application to initialize it */
5229 /* 1. in the system menu */
5230 hSubMenu = hSysMenu;
5232 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
5236 if (!IsWindowEnabled(hWnd))
5240 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hSysMenu, 0L);
5241 if(hSubMenu != hSysMenu)
5243 nPos = MENU_FindSubMenu(&hSysMenu, hSubMenu);
5244 TRACE_(accel)("hSysMenu = %p, hSubMenu = %p, nPos = %d\n", hSysMenu, hSubMenu, nPos);
5245 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, TRUE));
5247 uSysStat = GetMenuState(GetSubMenu(hSysMenu, 0), cmd, MF_BYCOMMAND);
5250 else /* 2. in the window's menu */
5254 if(MENU_FindItem(&hSubMenu, &nPos, MF_BYCOMMAND))
5258 if (!IsWindowEnabled(hWnd))
5262 SendMessageW(hWnd, WM_INITMENU, (WPARAM)hMenu, 0L);
5263 if(hSubMenu != hMenu)
5265 nPos = MENU_FindSubMenu(&hMenu, hSubMenu);
5266 TRACE_(accel)("hMenu = %p, hSubMenu = %p, nPos = %d\n", hMenu, hSubMenu, nPos);
5267 SendMessageW(hWnd, WM_INITMENUPOPUP, (WPARAM)hSubMenu, MAKELPARAM(nPos, FALSE));
5269 uStat = GetMenuState(hMenu, cmd, MF_BYCOMMAND);
5276 if (uSysStat != (UINT)-1)
5278 if (uSysStat & (MF_DISABLED|MF_GRAYED))
5285 if (uStat != (UINT)-1)
5291 if (uStat & (MF_DISABLED|MF_GRAYED))
5303 if( mesg==WM_COMMAND )
5305 TRACE_(accel)(", sending WM_COMMAND, wParam=%0x\n", 0x10000 | cmd);
5306 SendMessageW(hWnd, mesg, 0x10000 | cmd, 0L);
5308 else if( mesg==WM_SYSCOMMAND )
5310 TRACE_(accel)(", sending WM_SYSCOMMAND, wParam=%0x\n", cmd);
5311 SendMessageW(hWnd, mesg, cmd, 0x00010000L);
5315 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
5316 * #0: unknown (please report!)
5317 * #1: for WM_KEYUP,WM_SYSKEYUP
5318 * #2: mouse is captured
5319 * #3: window is disabled
5320 * #4: it's a disabled system menu option
5321 * #5: it's a menu option, but window is iconic
5322 * #6: it's a menu option, but disabled
5324 TRACE_(accel)(", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
5326 ERR_(accel)(" unknown reason - please report!\n");
5331 /**********************************************************************
5332 * TranslateAcceleratorA (USER32.@)
5333 * TranslateAccelerator (USER32.@)
5335 INT WINAPI TranslateAcceleratorA( HWND hWnd, HACCEL hAccel, LPMSG msg )
5338 LPACCEL16 lpAccelTbl;
5342 if (!hWnd || !msg) return 0;
5344 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(HACCEL_16(hAccel))))
5346 WARN_(accel)("invalid accel handle=%p\n", hAccel);
5350 wParam = msg->wParam;
5352 switch (msg->message)
5361 char ch = LOWORD(wParam);
5363 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
5364 wParam = MAKEWPARAM(wch, HIWORD(wParam));
5372 TRACE_(accel)("hAccel %p, hWnd %p, msg->hwnd %p, msg->message %04x, wParam %08lx, lParam %08lx\n",
5373 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
5377 if (translate_accelerator( hWnd, msg->message, wParam, msg->lParam,
5378 lpAccelTbl[i].fVirt, lpAccelTbl[i].key, lpAccelTbl[i].cmd))
5380 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
5385 /**********************************************************************
5386 * TranslateAcceleratorW (USER32.@)
5388 INT WINAPI TranslateAcceleratorW( HWND hWnd, HACCEL hAccel, LPMSG msg )
5391 LPACCEL16 lpAccelTbl;
5394 if (!hWnd || !msg) return 0;
5396 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(HACCEL_16(hAccel))))
5398 WARN_(accel)("invalid accel handle=%p\n", hAccel);
5402 switch (msg->message)
5414 TRACE_(accel)("hAccel %p, hWnd %p, msg->hwnd %p, msg->message %04x, wParam %08lx, lParam %08lx\n",
5415 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
5419 if (translate_accelerator( hWnd, msg->message, msg->wParam, msg->lParam,
5420 lpAccelTbl[i].fVirt, lpAccelTbl[i].key, lpAccelTbl[i].cmd))
5422 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);